From 5cfb0ec260f44872b56373b44eb721385e132d82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Mon, 2 Dec 2024 13:26:38 +0100 Subject: [PATCH] Expose raw argv in CLI `scriptArgs` only contains arguments that the CLI didn't parse, the script might want to dig into all the arguments. --- qjs.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/qjs.c b/qjs.c index 9ccf841..fefd3d9 100644 --- a/qjs.c +++ b/qjs.c @@ -46,7 +46,9 @@ extern const uint8_t qjsc_repl[]; extern const uint32_t qjsc_repl_size; -static JSCFunctionListEntry argv0; +static int qjs__argc; +static char **qjs__argv; + static int eval_buf(JSContext *ctx, const void *buf, int buf_len, const char *filename, int eval_flags) @@ -171,7 +173,13 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt) JSValue global = JS_GetGlobalObject(ctx); JS_SetPropertyFunctionList(ctx, global, global_obj, countof(global_obj)); - JS_SetPropertyFunctionList(ctx, global, &argv0, 1); + JSValue args = JS_NewArray(ctx); + int i; + for(i = 0; i < qjs__argc; i++) { + JS_SetPropertyUint32(ctx, args, i, JS_NewString(ctx, qjs__argv[i])); + } + JS_SetPropertyStr(ctx, global, "__argv", args); + JS_SetPropertyStr(ctx, global, "argv0", JS_NewString(ctx, qjs__argv[0])); JSValue navigator_proto = JS_NewObject(ctx); JS_SetPropertyFunctionList(ctx, navigator_proto, navigator_proto_funcs, countof(navigator_proto_funcs)); JSValue navigator = JS_NewObjectProto(ctx, navigator_proto); @@ -354,8 +362,9 @@ int main(int argc, char **argv) int64_t memory_limit = -1; int64_t stack_size = -1; - argv0 = (JSCFunctionListEntry)JS_PROP_STRING_DEF("argv0", argv[0], - JS_PROP_C_W_E); + /* save for later */ + qjs__argc = argc; + qjs__argv = argv; dump_flags_str = getenv("QJS_DUMP_FLAGS"); dump_flags = dump_flags_str ? strtol(dump_flags_str, NULL, 16) : 0;