Skip to content

Commit a884e2d

Browse files
authored
Add std.file.readAsArrayBuffer() (#835)
Like std.file.readAsString(), except it returns an ArrayBuffer and doesn't assume anything about the file's character encoding.
1 parent d07f6b5 commit a884e2d

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

docs/docs/stdlib.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,16 @@ position `position` (wrapper to the libc `fwrite`).
538538
Return the next line from the file, assuming UTF-8 encoding, excluding
539539
the trailing line feed.
540540

541+
#### `readAsArrayBuffer(max_size = undefined)`
542+
543+
Read `max_size` bytes from the file and return them as an ArrayBuffer.
544+
If `max_size` is not present, the file is read until its end.
545+
541546
#### `readAsString(max_size = undefined)`
542547

543548
Read `max_size` bytes from the file and return them as a string
544549
assuming UTF-8 encoding. If `max_size` is not present, the file
545-
is read up its end.
550+
is read until its end.
546551

547552
#### `getByte()`
548553

quickjs-libc.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,8 +1365,8 @@ static JSValue js_std_file_getline(JSContext *ctx, JSValue this_val,
13651365
}
13661366

13671367
/* XXX: could use less memory and go faster */
1368-
static JSValue js_std_file_readAsString(JSContext *ctx, JSValue this_val,
1369-
int argc, JSValue *argv)
1368+
static JSValue js_std_file_readAs(JSContext *ctx, JSValue this_val,
1369+
int argc, JSValue *argv, int magic)
13701370
{
13711371
FILE *f = js_std_file_get(ctx, this_val);
13721372
int c;
@@ -1402,7 +1402,11 @@ static JSValue js_std_file_readAsString(JSContext *ctx, JSValue this_val,
14021402
}
14031403
max_size--;
14041404
}
1405-
obj = JS_NewStringLen(ctx, (const char *)dbuf.buf, dbuf.size);
1405+
if (magic) {
1406+
obj = JS_NewStringLen(ctx, (const char *)dbuf.buf, dbuf.size);
1407+
} else {
1408+
obj = JS_NewArrayBufferCopy(ctx, dbuf.buf, dbuf.size);
1409+
}
14061410
dbuf_free(&dbuf);
14071411
return obj;
14081412
}
@@ -1694,7 +1698,8 @@ static const JSCFunctionListEntry js_std_file_proto_funcs[] = {
16941698
JS_CFUNC_MAGIC_DEF("read", 3, js_std_file_read_write, 0 ),
16951699
JS_CFUNC_MAGIC_DEF("write", 3, js_std_file_read_write, 1 ),
16961700
JS_CFUNC_DEF("getline", 0, js_std_file_getline ),
1697-
JS_CFUNC_DEF("readAsString", 0, js_std_file_readAsString ),
1701+
JS_CFUNC_MAGIC_DEF("readAsArrayBuffer", 0, js_std_file_readAs, 0 ),
1702+
JS_CFUNC_MAGIC_DEF("readAsString", 0, js_std_file_readAs, 1 ),
16981703
JS_CFUNC_DEF("getByte", 0, js_std_file_getByte ),
16991704
JS_CFUNC_DEF("putByte", 1, js_std_file_putByte ),
17001705
/* setvbuf, ... */

tests/test_std.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ function test_printf()
1919

2020
function test_file1()
2121
{
22-
var f, len, str, size, buf, ret, i, str1;
22+
var f, len, str, size, buf, ret, i, str1, ab;
2323

2424
f = std.tmpfile();
2525
str = "hello world\n";
2626
f.puts(str);
2727

28+
f.seek(0, std.SEEK_SET);
29+
ab = f.readAsArrayBuffer();
30+
assert([...new Uint8Array(ab)], str.split("").map(c => c.charCodeAt(0)));
31+
2832
f.seek(0, std.SEEK_SET);
2933
str1 = f.readAsString();
3034
assert(str1, str);

0 commit comments

Comments
 (0)