Skip to content

Commit

Permalink
Expose raw argv in CLI
Browse files Browse the repository at this point in the history
`scriptArgs` only contains arguments that the CLI didn't parse, the
script might want to dig into all the arguments.
  • Loading branch information
saghul committed Dec 2, 2024
1 parent 06cd3da commit 5cfb0ec
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5cfb0ec

Please sign in to comment.