quickjs 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -257,6 +257,32 @@ static const JSMallocFunctions trace_mf = {
257
257
  js_trace_malloc_usable_size,
258
258
  };
259
259
 
260
+ static size_t get_suffixed_size(const char *str)
261
+ {
262
+ char *p;
263
+ size_t v;
264
+ v = (size_t)strtod(str, &p);
265
+ switch(*p) {
266
+ case 'G':
267
+ v <<= 30;
268
+ break;
269
+ case 'M':
270
+ v <<= 20;
271
+ break;
272
+ case 'k':
273
+ case 'K':
274
+ v <<= 10;
275
+ break;
276
+ default:
277
+ if (*p != '\0') {
278
+ fprintf(stderr, "qjs: invalid suffix: %s\n", p);
279
+ exit(1);
280
+ }
281
+ break;
282
+ }
283
+ return v;
284
+ }
285
+
260
286
  #define PROG_NAME "qjs"
261
287
 
262
288
  void help(void)
@@ -272,9 +298,11 @@ void help(void)
272
298
  " --std make 'std' and 'os' available to the loaded script\n"
273
299
  "-T --trace trace memory allocation\n"
274
300
  "-d --dump dump the memory usage stats\n"
275
- " --memory-limit n limit the memory usage to 'n' bytes\n"
276
- " --stack-size n limit the stack size to 'n' bytes\n"
277
- " --unhandled-rejection dump unhandled promise rejections\n"
301
+ " --memory-limit n limit the memory usage to 'n' bytes (SI suffixes allowed)\n"
302
+ " --stack-size n limit the stack size to 'n' bytes (SI suffixes allowed)\n"
303
+ " --no-unhandled-rejection ignore unhandled promise rejections\n"
304
+ "-s strip all the debug info\n"
305
+ " --strip-source strip the source code\n"
278
306
  "-q --quit just instantiate the interpreter and quit\n");
279
307
  exit(1);
280
308
  }
@@ -292,10 +320,11 @@ int main(int argc, char **argv)
292
320
  int empty_run = 0;
293
321
  int module = -1;
294
322
  int load_std = 0;
295
- int dump_unhandled_promise_rejection = 0;
323
+ int dump_unhandled_promise_rejection = 1;
296
324
  size_t memory_limit = 0;
297
325
  char *include_list[32];
298
326
  int i, include_count = 0;
327
+ int strip_flags = 0;
299
328
  size_t stack_size = 0;
300
329
 
301
330
  /* cannot use getopt because we want to pass the command line to
@@ -371,8 +400,8 @@ int main(int argc, char **argv)
371
400
  load_std = 1;
372
401
  continue;
373
402
  }
374
- if (!strcmp(longopt, "unhandled-rejection")) {
375
- dump_unhandled_promise_rejection = 1;
403
+ if (!strcmp(longopt, "no-unhandled-rejection")) {
404
+ dump_unhandled_promise_rejection = 0;
376
405
  continue;
377
406
  }
378
407
  if (opt == 'q' || !strcmp(longopt, "quit")) {
@@ -384,7 +413,7 @@ int main(int argc, char **argv)
384
413
  fprintf(stderr, "expecting memory limit");
385
414
  exit(1);
386
415
  }
387
- memory_limit = (size_t)strtod(argv[optind++], NULL);
416
+ memory_limit = get_suffixed_size(argv[optind++]);
388
417
  continue;
389
418
  }
390
419
  if (!strcmp(longopt, "stack-size")) {
@@ -392,7 +421,15 @@ int main(int argc, char **argv)
392
421
  fprintf(stderr, "expecting stack size");
393
422
  exit(1);
394
423
  }
395
- stack_size = (size_t)strtod(argv[optind++], NULL);
424
+ stack_size = get_suffixed_size(argv[optind++]);
425
+ continue;
426
+ }
427
+ if (opt == 's') {
428
+ strip_flags = JS_STRIP_DEBUG;
429
+ continue;
430
+ }
431
+ if (!strcmp(longopt, "strip-source")) {
432
+ strip_flags = JS_STRIP_SOURCE;
396
433
  continue;
397
434
  }
398
435
  if (opt) {
@@ -418,6 +455,7 @@ int main(int argc, char **argv)
418
455
  JS_SetMemoryLimit(rt, memory_limit);
419
456
  if (stack_size != 0)
420
457
  JS_SetMaxStackSize(rt, stack_size);
458
+ JS_SetStripInfo(rt, strip_flags);
421
459
  js_std_set_worker_new_context_func(JS_NewCustomContext);
422
460
  js_std_init_handlers(rt);
423
461
  ctx = JS_NewCustomContext(rt);
@@ -465,6 +503,7 @@ int main(int argc, char **argv)
465
503
  goto fail;
466
504
  }
467
505
  if (interactive) {
506
+ JS_SetHostPromiseRejectionTracker(rt, NULL, NULL);
468
507
  js_std_eval_binary(ctx, qjsc_repl, qjsc_repl_size, 0);
469
508
  }
470
509
  js_std_loop(ctx);
@@ -76,6 +76,7 @@ static const FeatureEntry feature_list[] = {
76
76
  { "promise", "Promise" },
77
77
  #define FE_MODULE_LOADER 9
78
78
  { "module-loader", NULL },
79
+ { "weakref", "WeakRef" },
79
80
  };
80
81
 
81
82
  void namelist_add(namelist_t *lp, const char *name, const char *short_name,
@@ -352,7 +353,9 @@ void help(void)
352
353
  "-M module_name[,cname] add initialization code for an external C module\n"
353
354
  "-x byte swapped output\n"
354
355
  "-p prefix set the prefix of the generated C names\n"
355
- "-S n set the maximum stack size to 'n' bytes (default=%d)\n",
356
+ "-S n set the maximum stack size to 'n' bytes (default=%d)\n"
357
+ "-s strip all the debug info\n"
358
+ "--keep-source keep the source code\n",
356
359
  JS_DEFAULT_STACK_SIZE);
357
360
  #ifdef CONFIG_LTO
358
361
  {
@@ -471,6 +474,31 @@ static int output_executable(const char *out_filename, const char *cfilename,
471
474
  }
472
475
  #endif
473
476
 
477
+ static size_t get_suffixed_size(const char *str)
478
+ {
479
+ char *p;
480
+ size_t v;
481
+ v = (size_t)strtod(str, &p);
482
+ switch(*p) {
483
+ case 'G':
484
+ v <<= 30;
485
+ break;
486
+ case 'M':
487
+ v <<= 20;
488
+ break;
489
+ case 'k':
490
+ case 'K':
491
+ v <<= 10;
492
+ break;
493
+ default:
494
+ if (*p != '\0') {
495
+ fprintf(stderr, "qjs: invalid suffix: %s\n", p);
496
+ exit(1);
497
+ }
498
+ break;
499
+ }
500
+ return v;
501
+ }
474
502
 
475
503
  typedef enum {
476
504
  OUTPUT_C,
@@ -478,9 +506,24 @@ typedef enum {
478
506
  OUTPUT_EXECUTABLE,
479
507
  } OutputTypeEnum;
480
508
 
509
+ static const char *get_short_optarg(int *poptind, int opt,
510
+ const char *arg, int argc, char **argv)
511
+ {
512
+ const char *optarg;
513
+ if (*arg) {
514
+ optarg = arg;
515
+ } else if (*poptind < argc) {
516
+ optarg = argv[(*poptind)++];
517
+ } else {
518
+ fprintf(stderr, "qjsc: expecting parameter for -%c\n", opt);
519
+ exit(1);
520
+ }
521
+ return optarg;
522
+ }
523
+
481
524
  int main(int argc, char **argv)
482
525
  {
483
- int c, i, verbose;
526
+ int i, verbose, strip_flags;
484
527
  const char *out_filename, *cname;
485
528
  char cfilename[1024];
486
529
  FILE *fo;
@@ -499,6 +542,7 @@ int main(int argc, char **argv)
499
542
  module = -1;
500
543
  byte_swap = FALSE;
501
544
  verbose = 0;
545
+ strip_flags = JS_STRIP_SOURCE;
502
546
  use_lto = FALSE;
503
547
  stack_size = 0;
504
548
  memset(&dynamic_module_list, 0, sizeof(dynamic_module_list));
@@ -507,30 +551,51 @@ int main(int argc, char **argv)
507
551
  namelist_add(&cmodule_list, "std", "std", 0);
508
552
  namelist_add(&cmodule_list, "os", "os", 0);
509
553
 
510
- for(;;) {
511
- c = getopt(argc, argv, "ho:cN:f:mxevM:p:S:D:");
512
- if (c == -1)
513
- break;
514
- switch(c) {
515
- case 'h':
516
- help();
517
- case 'o':
518
- out_filename = optarg;
519
- break;
520
- case 'c':
521
- output_type = OUTPUT_C;
522
- break;
523
- case 'e':
524
- output_type = OUTPUT_C_MAIN;
525
- break;
526
- case 'N':
527
- cname = optarg;
554
+ optind = 1;
555
+ while (optind < argc && *argv[optind] == '-') {
556
+ char *arg = argv[optind] + 1;
557
+ const char *longopt = "";
558
+ const char *optarg;
559
+ /* a single - is not an option, it also stops argument scanning */
560
+ if (!*arg)
528
561
  break;
529
- case 'f':
530
- {
562
+ optind++;
563
+ if (*arg == '-') {
564
+ longopt = arg + 1;
565
+ arg += strlen(arg);
566
+ /* -- stops argument scanning */
567
+ if (!*longopt)
568
+ break;
569
+ }
570
+ for (; *arg || *longopt; longopt = "") {
571
+ char opt = *arg;
572
+ if (opt)
573
+ arg++;
574
+ if (opt == 'h' || opt == '?' || !strcmp(longopt, "help")) {
575
+ help();
576
+ continue;
577
+ }
578
+ if (opt == 'o') {
579
+ out_filename = get_short_optarg(&optind, opt, arg, argc, argv);
580
+ break;
581
+ }
582
+ if (opt == 'c') {
583
+ output_type = OUTPUT_C;
584
+ continue;
585
+ }
586
+ if (opt == 'e') {
587
+ output_type = OUTPUT_C_MAIN;
588
+ continue;
589
+ }
590
+ if (opt == 'N') {
591
+ cname = get_short_optarg(&optind, opt, arg, argc, argv);
592
+ break;
593
+ }
594
+ if (opt == 'f') {
531
595
  const char *p;
596
+ optarg = get_short_optarg(&optind, opt, arg, argc, argv);
532
597
  p = optarg;
533
- if (!strcmp(optarg, "lto")) {
598
+ if (!strcmp(p, "lto")) {
534
599
  use_lto = TRUE;
535
600
  } else if (strstart(p, "no-", &p)) {
536
601
  use_lto = TRUE;
@@ -547,16 +612,18 @@ int main(int argc, char **argv)
547
612
  fprintf(stderr, "unsupported feature: %s\n", optarg);
548
613
  exit(1);
549
614
  }
615
+ break;
550
616
  }
551
- break;
552
- case 'm':
553
- module = 1;
554
- break;
555
- case 'M':
556
- {
617
+ if (opt == 'm') {
618
+ module = 1;
619
+ continue;
620
+ }
621
+ if (opt == 'M') {
557
622
  char *p;
558
623
  char path[1024];
559
624
  char cname[1024];
625
+
626
+ optarg = get_short_optarg(&optind, opt, arg, argc, argv);
560
627
  pstrcpy(path, sizeof(path), optarg);
561
628
  p = strchr(path, ',');
562
629
  if (p) {
@@ -566,25 +633,44 @@ int main(int argc, char **argv)
566
633
  get_c_name(cname, sizeof(cname), path);
567
634
  }
568
635
  namelist_add(&cmodule_list, path, cname, 0);
636
+ break;
569
637
  }
570
- break;
571
- case 'D':
572
- namelist_add(&dynamic_module_list, optarg, NULL, 0);
573
- break;
574
- case 'x':
575
- byte_swap = TRUE;
576
- break;
577
- case 'v':
578
- verbose++;
579
- break;
580
- case 'p':
581
- c_ident_prefix = optarg;
582
- break;
583
- case 'S':
584
- stack_size = (size_t)strtod(optarg, NULL);
585
- break;
586
- default:
587
- break;
638
+ if (opt == 'D') {
639
+ optarg = get_short_optarg(&optind, opt, arg, argc, argv);
640
+ namelist_add(&dynamic_module_list, optarg, NULL, 0);
641
+ break;
642
+ }
643
+ if (opt == 'x') {
644
+ byte_swap = 1;
645
+ continue;
646
+ }
647
+ if (opt == 'v') {
648
+ verbose++;
649
+ continue;
650
+ }
651
+ if (opt == 'p') {
652
+ c_ident_prefix = get_short_optarg(&optind, opt, arg, argc, argv);
653
+ break;
654
+ }
655
+ if (opt == 'S') {
656
+ optarg = get_short_optarg(&optind, opt, arg, argc, argv);
657
+ stack_size = get_suffixed_size(optarg);
658
+ break;
659
+ }
660
+ if (opt == 's') {
661
+ strip_flags = JS_STRIP_DEBUG;
662
+ continue;
663
+ }
664
+ if (!strcmp(longopt, "keep-source")) {
665
+ strip_flags = 0;
666
+ continue;
667
+ }
668
+ if (opt) {
669
+ fprintf(stderr, "qjsc: unknown option '-%c'\n", opt);
670
+ } else {
671
+ fprintf(stderr, "qjsc: unknown option '--%s'\n", longopt);
672
+ }
673
+ help();
588
674
  }
589
675
  }
590
676
 
@@ -620,6 +706,8 @@ int main(int argc, char **argv)
620
706
  rt = JS_NewRuntime();
621
707
  ctx = JS_NewContext(rt);
622
708
 
709
+ JS_SetStripInfo(rt, strip_flags);
710
+
623
711
  /* loader for ES6 modules */
624
712
  JS_SetModuleLoaderFunc(rt, NULL, jsc_module_loader, NULL);
625
713
 
@@ -81,6 +81,7 @@ DEF(empty_string, "")
81
81
  DEF(length, "length")
82
82
  DEF(fileName, "fileName")
83
83
  DEF(lineNumber, "lineNumber")
84
+ DEF(columnNumber, "columnNumber")
84
85
  DEF(message, "message")
85
86
  DEF(cause, "cause")
86
87
  DEF(errors, "errors")
@@ -172,6 +173,10 @@ DEF(status, "status")
172
173
  DEF(reason, "reason")
173
174
  DEF(globalThis, "globalThis")
174
175
  DEF(bigint, "bigint")
176
+ DEF(minus_zero, "-0")
177
+ DEF(Infinity, "Infinity")
178
+ DEF(minus_Infinity, "-Infinity")
179
+ DEF(NaN, "NaN")
175
180
  /* the following 3 atoms are only used with CONFIG_ATOMICS */
176
181
  DEF(not_equal, "not-equal")
177
182
  DEF(timed_out, "timed-out")
@@ -210,6 +215,8 @@ DEF(Float32Array, "Float32Array")
210
215
  DEF(Float64Array, "Float64Array")
211
216
  DEF(DataView, "DataView")
212
217
  DEF(BigInt, "BigInt")
218
+ DEF(WeakRef, "WeakRef")
219
+ DEF(FinalizationRegistry, "FinalizationRegistry")
213
220
  DEF(Map, "Map")
214
221
  DEF(Set, "Set") /* Map + 1 */
215
222
  DEF(WeakMap, "WeakMap") /* Map + 2 */