Skip to content

Commit 5cfb0ec

Browse files
committed
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.
1 parent 06cd3da commit 5cfb0ec

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

qjs.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
extern const uint8_t qjsc_repl[];
4747
extern const uint32_t qjsc_repl_size;
4848

49-
static JSCFunctionListEntry argv0;
49+
static int qjs__argc;
50+
static char **qjs__argv;
51+
5052

5153
static int eval_buf(JSContext *ctx, const void *buf, int buf_len,
5254
const char *filename, int eval_flags)
@@ -171,7 +173,13 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
171173

172174
JSValue global = JS_GetGlobalObject(ctx);
173175
JS_SetPropertyFunctionList(ctx, global, global_obj, countof(global_obj));
174-
JS_SetPropertyFunctionList(ctx, global, &argv0, 1);
176+
JSValue args = JS_NewArray(ctx);
177+
int i;
178+
for(i = 0; i < qjs__argc; i++) {
179+
JS_SetPropertyUint32(ctx, args, i, JS_NewString(ctx, qjs__argv[i]));
180+
}
181+
JS_SetPropertyStr(ctx, global, "__argv", args);
182+
JS_SetPropertyStr(ctx, global, "argv0", JS_NewString(ctx, qjs__argv[0]));
175183
JSValue navigator_proto = JS_NewObject(ctx);
176184
JS_SetPropertyFunctionList(ctx, navigator_proto, navigator_proto_funcs, countof(navigator_proto_funcs));
177185
JSValue navigator = JS_NewObjectProto(ctx, navigator_proto);
@@ -354,8 +362,9 @@ int main(int argc, char **argv)
354362
int64_t memory_limit = -1;
355363
int64_t stack_size = -1;
356364

357-
argv0 = (JSCFunctionListEntry)JS_PROP_STRING_DEF("argv0", argv[0],
358-
JS_PROP_C_W_E);
365+
/* save for later */
366+
qjs__argc = argc;
367+
qjs__argv = argv;
359368

360369
dump_flags_str = getenv("QJS_DUMP_FLAGS");
361370
dump_flags = dump_flags_str ? strtol(dump_flags_str, NULL, 16) : 0;

0 commit comments

Comments
 (0)