mirror of
https://github.com/DoneJS-Runtime/quickjs-done-nextgen.git
synced 2025-01-09 17:43:15 +00:00
Add ability to load file as Uint8Array in std.loadFile
This commit is contained in:
parent
5cfb0ec260
commit
517e9e274e
2 changed files with 40 additions and 21 deletions
|
@ -348,11 +348,13 @@ optional properties:
|
||||||
|
|
||||||
Evaluate the file `filename` as a script (global eval).
|
Evaluate the file `filename` as a script (global eval).
|
||||||
|
|
||||||
### `loadFile(filename)`
|
### `loadFile(filename, [options])`
|
||||||
|
|
||||||
Load the file `filename` and return it as a string assuming UTF-8
|
Load the file `filename` and return it as a string assuming UTF-8
|
||||||
encoding. Return `null` in case of I/O error.
|
encoding. Return `null` in case of I/O error.
|
||||||
|
|
||||||
|
If `options.binary` is set to `true` a `Uint8Array` is returned instead.
|
||||||
|
|
||||||
### `open(filename, flags, errorObj = undefined)`
|
### `open(filename, flags, errorObj = undefined)`
|
||||||
|
|
||||||
Open a file (wrapper to the libc `fopen()`). Return the FILE
|
Open a file (wrapper to the libc `fopen()`). Return the FILE
|
||||||
|
|
|
@ -478,14 +478,41 @@ static JSValue js_loadScript(JSContext *ctx, JSValue this_val,
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* load a file as a UTF-8 encoded string */
|
static int get_bool_option(JSContext *ctx, BOOL *pbool,
|
||||||
|
JSValue obj,
|
||||||
|
const char *option)
|
||||||
|
{
|
||||||
|
JSValue val;
|
||||||
|
val = JS_GetPropertyStr(ctx, obj, option);
|
||||||
|
if (JS_IsException(val))
|
||||||
|
return -1;
|
||||||
|
if (!JS_IsUndefined(val)) {
|
||||||
|
*pbool = JS_ToBool(ctx, val);
|
||||||
|
}
|
||||||
|
JS_FreeValue(ctx, val);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void free_buf(JSRuntime *rt, void *opaque, void *ptr) {
|
||||||
|
js_free_rt(rt, ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* load a file as a UTF-8 encoded string or Uint8Array */
|
||||||
static JSValue js_std_loadFile(JSContext *ctx, JSValue this_val,
|
static JSValue js_std_loadFile(JSContext *ctx, JSValue this_val,
|
||||||
int argc, JSValue *argv)
|
int argc, JSValue *argv)
|
||||||
{
|
{
|
||||||
uint8_t *buf;
|
uint8_t *buf;
|
||||||
const char *filename;
|
const char *filename;
|
||||||
JSValue ret;
|
JSValue ret, options_obj;
|
||||||
size_t buf_len;
|
size_t buf_len;
|
||||||
|
BOOL binary = FALSE;
|
||||||
|
|
||||||
|
if (argc >= 2) {
|
||||||
|
options_obj = argv[1];
|
||||||
|
if (get_bool_option(ctx, &binary, options_obj,
|
||||||
|
"binary"))
|
||||||
|
return JS_EXCEPTION;
|
||||||
|
}
|
||||||
|
|
||||||
filename = JS_ToCString(ctx, argv[0]);
|
filename = JS_ToCString(ctx, argv[0]);
|
||||||
if (!filename)
|
if (!filename)
|
||||||
|
@ -494,8 +521,13 @@ static JSValue js_std_loadFile(JSContext *ctx, JSValue this_val,
|
||||||
JS_FreeCString(ctx, filename);
|
JS_FreeCString(ctx, filename);
|
||||||
if (!buf)
|
if (!buf)
|
||||||
return JS_NULL;
|
return JS_NULL;
|
||||||
|
if (binary) {
|
||||||
|
ret = JS_NewUint8Array(ctx, buf, buf_len, free_buf, NULL, FALSE);
|
||||||
|
} else {
|
||||||
ret = JS_NewStringLen(ctx, (char *)buf, buf_len);
|
ret = JS_NewStringLen(ctx, (char *)buf, buf_len);
|
||||||
js_free(ctx, buf);
|
js_free(ctx, buf);
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -822,21 +854,6 @@ static int interrupt_handler(JSRuntime *rt, void *opaque)
|
||||||
return (os_pending_signals >> SIGINT) & 1;
|
return (os_pending_signals >> SIGINT) & 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_bool_option(JSContext *ctx, BOOL *pbool,
|
|
||||||
JSValue obj,
|
|
||||||
const char *option)
|
|
||||||
{
|
|
||||||
JSValue val;
|
|
||||||
val = JS_GetPropertyStr(ctx, obj, option);
|
|
||||||
if (JS_IsException(val))
|
|
||||||
return -1;
|
|
||||||
if (!JS_IsUndefined(val)) {
|
|
||||||
*pbool = JS_ToBool(ctx, val);
|
|
||||||
}
|
|
||||||
JS_FreeValue(ctx, val);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
|
static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
|
||||||
int argc, JSValue *argv)
|
int argc, JSValue *argv)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue