Skip to content

Commit 43a7fc3

Browse files
committed
Improve dump option support
- DUMP_XXX defined as nothing or 0 produces unconditional output - DUMP_XXX defined as a bitmask produces conditional output based on command line option -d<bitmask> - add `JS_SetDumpFlags()` to select active dump options - accept -d[<hex mask>] and --dump[=<hex mask>] to specify active dump options, generalize command line option handling - improve DUMP_READ_OBJECT output, fix indentation issue
1 parent f62b90d commit 43a7fc3

File tree

3 files changed

+281
-152
lines changed

3 files changed

+281
-152
lines changed

qjs.c

+41-20
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ int main(int argc, char **argv)
289289
char *expr = NULL;
290290
int interactive = 0;
291291
int dump_memory = 0;
292+
int dump_flags = 0;
292293
int trace_memory = 0;
293294
int empty_run = 0;
294295
int module = -1;
@@ -308,36 +309,42 @@ int main(int argc, char **argv)
308309
while (optind < argc && *argv[optind] == '-') {
309310
char *arg = argv[optind] + 1;
310311
const char *longopt = "";
312+
char *opt_arg = NULL;
311313
/* a single - is not an option, it also stops argument scanning */
312314
if (!*arg)
313315
break;
314316
optind++;
315317
if (*arg == '-') {
316318
longopt = arg + 1;
319+
opt_arg = strchr(longopt, '=');
320+
if (opt_arg)
321+
*opt_arg++ = '\0';
317322
arg += strlen(arg);
318323
/* -- stops argument scanning */
319324
if (!*longopt)
320325
break;
321326
}
322327
for (; *arg || *longopt; longopt = "") {
323328
char opt = *arg;
324-
if (opt)
329+
if (opt) {
325330
arg++;
331+
if (!opt_arg && *arg)
332+
opt_arg = arg;
333+
}
326334
if (opt == 'h' || opt == '?' || !strcmp(longopt, "help")) {
327335
help();
328336
continue;
329337
}
330338
if (opt == 'e' || !strcmp(longopt, "eval")) {
331-
if (*arg) {
332-
expr = arg;
333-
break;
334-
}
335-
if (optind < argc) {
336-
expr = argv[optind++];
337-
break;
339+
if (!opt_arg) {
340+
if (optind >= argc) {
341+
fprintf(stderr, "qjs: missing expression for -e\n");
342+
exit(2);
343+
}
344+
opt_arg = argv[optind++];
338345
}
339-
fprintf(stderr, "qjs: missing expression for -e\n");
340-
exit(2);
346+
expr = opt_arg;
347+
break;
341348
}
342349
if (opt == 'I' || !strcmp(longopt, "include")) {
343350
if (optind >= argc) {
@@ -364,6 +371,10 @@ int main(int argc, char **argv)
364371
continue;
365372
}
366373
if (opt == 'd' || !strcmp(longopt, "dump")) {
374+
if (opt_arg) {
375+
dump_flags = strtol(opt_arg, NULL, 16);
376+
break;
377+
}
367378
dump_memory++;
368379
continue;
369380
}
@@ -384,20 +395,28 @@ int main(int argc, char **argv)
384395
continue;
385396
}
386397
if (!strcmp(longopt, "memory-limit")) {
387-
if (optind >= argc) {
388-
fprintf(stderr, "expecting memory limit");
389-
exit(1);
398+
if (!opt_arg) {
399+
if (optind >= argc) {
400+
fprintf(stderr, "expecting memory limit");
401+
exit(1);
402+
}
403+
opt_arg = argv[optind++];
390404
}
391-
memory_limit = (size_t)strtod(argv[optind++], NULL);
392-
continue;
405+
// TODO(chqrlie): accept kmg suffixes
406+
memory_limit = (size_t)strtod(opt_arg, NULL);
407+
break;
393408
}
394409
if (!strcmp(longopt, "stack-size")) {
395-
if (optind >= argc) {
396-
fprintf(stderr, "expecting stack size");
397-
exit(1);
410+
if (!opt_arg) {
411+
if (optind >= argc) {
412+
fprintf(stderr, "expecting stack size");
413+
exit(1);
414+
}
415+
opt_arg = argv[optind++];
398416
}
399-
stack_size = (size_t)strtod(argv[optind++], NULL);
400-
continue;
417+
// TODO(chqrlie): accept kmg suffixes
418+
stack_size = (size_t)strtod(opt_arg, NULL);
419+
break;
401420
}
402421
if (opt) {
403422
fprintf(stderr, "qjs: unknown option '-%c'\n", opt);
@@ -422,6 +441,8 @@ int main(int argc, char **argv)
422441
JS_SetMemoryLimit(rt, memory_limit);
423442
if (stack_size != 0)
424443
JS_SetMaxStackSize(rt, stack_size);
444+
if (dump_flags != 0)
445+
JS_SetDumpFlags(rt, dump_flags);
425446
js_std_set_worker_new_context_func(JS_NewCustomContext);
426447
js_std_init_handlers(rt);
427448
ctx = JS_NewCustomContext(rt);

0 commit comments

Comments
 (0)