Skip to content

Commit 517e9e2

Browse files
committed
Add ability to load file as Uint8Array in std.loadFile
1 parent 5cfb0ec commit 517e9e2

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

docs/docs/stdlib.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ The worker class has the following static properties:
311311
worker and is used to send or receive messages.
312312

313313
The worker instances have the following properties:
314-
314+
315315
- `postMessage(msg)` - Send a message to the corresponding worker. `msg` is cloned in
316316
the destination worker using an algorithm similar to the `HTML`
317317
structured clone algorithm. `SharedArrayBuffer` are shared
@@ -348,11 +348,13 @@ optional properties:
348348

349349
Evaluate the file `filename` as a script (global eval).
350350

351-
### `loadFile(filename)`
351+
### `loadFile(filename, [options])`
352352

353353
Load the file `filename` and return it as a string assuming UTF-8
354354
encoding. Return `null` in case of I/O error.
355355

356+
If `options.binary` is set to `true` a `Uint8Array` is returned instead.
357+
356358
### `open(filename, flags, errorObj = undefined)`
357359

358360
Open a file (wrapper to the libc `fopen()`). Return the FILE

quickjs-libc.c

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -478,14 +478,41 @@ static JSValue js_loadScript(JSContext *ctx, JSValue this_val,
478478
return ret;
479479
}
480480

481-
/* load a file as a UTF-8 encoded string */
481+
static int get_bool_option(JSContext *ctx, BOOL *pbool,
482+
JSValue obj,
483+
const char *option)
484+
{
485+
JSValue val;
486+
val = JS_GetPropertyStr(ctx, obj, option);
487+
if (JS_IsException(val))
488+
return -1;
489+
if (!JS_IsUndefined(val)) {
490+
*pbool = JS_ToBool(ctx, val);
491+
}
492+
JS_FreeValue(ctx, val);
493+
return 0;
494+
}
495+
496+
static void free_buf(JSRuntime *rt, void *opaque, void *ptr) {
497+
js_free_rt(rt, ptr);
498+
}
499+
500+
/* load a file as a UTF-8 encoded string or Uint8Array */
482501
static JSValue js_std_loadFile(JSContext *ctx, JSValue this_val,
483502
int argc, JSValue *argv)
484503
{
485504
uint8_t *buf;
486505
const char *filename;
487-
JSValue ret;
506+
JSValue ret, options_obj;
488507
size_t buf_len;
508+
BOOL binary = FALSE;
509+
510+
if (argc >= 2) {
511+
options_obj = argv[1];
512+
if (get_bool_option(ctx, &binary, options_obj,
513+
"binary"))
514+
return JS_EXCEPTION;
515+
}
489516

490517
filename = JS_ToCString(ctx, argv[0]);
491518
if (!filename)
@@ -494,8 +521,13 @@ static JSValue js_std_loadFile(JSContext *ctx, JSValue this_val,
494521
JS_FreeCString(ctx, filename);
495522
if (!buf)
496523
return JS_NULL;
497-
ret = JS_NewStringLen(ctx, (char *)buf, buf_len);
498-
js_free(ctx, buf);
524+
if (binary) {
525+
ret = JS_NewUint8Array(ctx, buf, buf_len, free_buf, NULL, FALSE);
526+
} else {
527+
ret = JS_NewStringLen(ctx, (char *)buf, buf_len);
528+
js_free(ctx, buf);
529+
}
530+
499531
return ret;
500532
}
501533

@@ -822,21 +854,6 @@ static int interrupt_handler(JSRuntime *rt, void *opaque)
822854
return (os_pending_signals >> SIGINT) & 1;
823855
}
824856

825-
static int get_bool_option(JSContext *ctx, BOOL *pbool,
826-
JSValue obj,
827-
const char *option)
828-
{
829-
JSValue val;
830-
val = JS_GetPropertyStr(ctx, obj, option);
831-
if (JS_IsException(val))
832-
return -1;
833-
if (!JS_IsUndefined(val)) {
834-
*pbool = JS_ToBool(ctx, val);
835-
}
836-
JS_FreeValue(ctx, val);
837-
return 0;
838-
}
839-
840857
static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
841858
int argc, JSValue *argv)
842859
{

0 commit comments

Comments
 (0)