quickjs 0.9.0 → 0.11.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.
@@ -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,
@@ -169,14 +170,24 @@ static void dump_hex(FILE *f, const uint8_t *buf, size_t len)
169
170
  fprintf(f, "\n");
170
171
  }
171
172
 
173
+ typedef enum {
174
+ CNAME_TYPE_SCRIPT,
175
+ CNAME_TYPE_MODULE,
176
+ CNAME_TYPE_JSON_MODULE,
177
+ } CNameTypeEnum;
178
+
172
179
  static void output_object_code(JSContext *ctx,
173
180
  FILE *fo, JSValueConst obj, const char *c_name,
174
- BOOL load_only)
181
+ CNameTypeEnum c_name_type)
175
182
  {
176
183
  uint8_t *out_buf;
177
184
  size_t out_buf_len;
178
185
  int flags;
179
- flags = JS_WRITE_OBJ_BYTECODE;
186
+
187
+ if (c_name_type == CNAME_TYPE_JSON_MODULE)
188
+ flags = 0;
189
+ else
190
+ flags = JS_WRITE_OBJ_BYTECODE;
180
191
  if (byte_swap)
181
192
  flags |= JS_WRITE_OBJ_BSWAP;
182
193
  out_buf = JS_WriteObject(ctx, &out_buf_len, obj, flags);
@@ -185,7 +196,7 @@ static void output_object_code(JSContext *ctx,
185
196
  exit(1);
186
197
  }
187
198
 
188
- namelist_add(&cname_list, c_name, NULL, load_only);
199
+ namelist_add(&cname_list, c_name, NULL, c_name_type);
189
200
 
190
201
  fprintf(fo, "const uint32_t %s_size = %u;\n\n",
191
202
  c_name, (unsigned int)out_buf_len);
@@ -226,7 +237,8 @@ static void find_unique_cname(char *cname, size_t cname_size)
226
237
  }
227
238
 
228
239
  JSModuleDef *jsc_module_loader(JSContext *ctx,
229
- const char *module_name, void *opaque)
240
+ const char *module_name, void *opaque,
241
+ JSValueConst attributes)
230
242
  {
231
243
  JSModuleDef *m;
232
244
  namelist_entry_t *e;
@@ -248,9 +260,9 @@ JSModuleDef *jsc_module_loader(JSContext *ctx,
248
260
  } else {
249
261
  size_t buf_len;
250
262
  uint8_t *buf;
251
- JSValue func_val;
252
263
  char cname[1024];
253
-
264
+ int res;
265
+
254
266
  buf = js_load_file(ctx, &buf_len, module_name);
255
267
  if (!buf) {
256
268
  JS_ThrowReferenceError(ctx, "could not load module filename '%s'",
@@ -258,21 +270,59 @@ JSModuleDef *jsc_module_loader(JSContext *ctx,
258
270
  return NULL;
259
271
  }
260
272
 
261
- /* compile the module */
262
- func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name,
263
- JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
264
- js_free(ctx, buf);
265
- if (JS_IsException(func_val))
266
- return NULL;
267
- get_c_name(cname, sizeof(cname), module_name);
268
- if (namelist_find(&cname_list, cname)) {
269
- find_unique_cname(cname, sizeof(cname));
270
- }
271
- output_object_code(ctx, outfile, func_val, cname, TRUE);
273
+ res = js_module_test_json(ctx, attributes);
274
+ if (has_suffix(module_name, ".json") || res > 0) {
275
+ /* compile as JSON or JSON5 depending on "type" */
276
+ JSValue val;
277
+ int flags;
278
+
279
+ if (res == 2)
280
+ flags = JS_PARSE_JSON_EXT;
281
+ else
282
+ flags = 0;
283
+ val = JS_ParseJSON2(ctx, (char *)buf, buf_len, module_name, flags);
284
+ js_free(ctx, buf);
285
+ if (JS_IsException(val))
286
+ return NULL;
287
+ /* create a dummy module */
288
+ m = JS_NewCModule(ctx, module_name, js_module_dummy_init);
289
+ if (!m) {
290
+ JS_FreeValue(ctx, val);
291
+ return NULL;
292
+ }
272
293
 
273
- /* the module is already referenced, so we must free it */
274
- m = JS_VALUE_GET_PTR(func_val);
275
- JS_FreeValue(ctx, func_val);
294
+ get_c_name(cname, sizeof(cname), module_name);
295
+ if (namelist_find(&cname_list, cname)) {
296
+ find_unique_cname(cname, sizeof(cname));
297
+ }
298
+
299
+ /* output the module name */
300
+ fprintf(outfile, "static const uint8_t %s_module_name[] = {\n",
301
+ cname);
302
+ dump_hex(outfile, (const uint8_t *)module_name, strlen(module_name) + 1);
303
+ fprintf(outfile, "};\n\n");
304
+
305
+ output_object_code(ctx, outfile, val, cname, CNAME_TYPE_JSON_MODULE);
306
+ JS_FreeValue(ctx, val);
307
+ } else {
308
+ JSValue func_val;
309
+
310
+ /* compile the module */
311
+ func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name,
312
+ JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
313
+ js_free(ctx, buf);
314
+ if (JS_IsException(func_val))
315
+ return NULL;
316
+ get_c_name(cname, sizeof(cname), module_name);
317
+ if (namelist_find(&cname_list, cname)) {
318
+ find_unique_cname(cname, sizeof(cname));
319
+ }
320
+ output_object_code(ctx, outfile, func_val, cname, CNAME_TYPE_MODULE);
321
+
322
+ /* the module is already referenced, so we must free it */
323
+ m = JS_VALUE_GET_PTR(func_val);
324
+ JS_FreeValue(ctx, func_val);
325
+ }
276
326
  }
277
327
  return m;
278
328
  }
@@ -312,8 +362,11 @@ static void compile_file(JSContext *ctx, FILE *fo,
312
362
  pstrcpy(c_name, sizeof(c_name), c_name1);
313
363
  } else {
314
364
  get_c_name(c_name, sizeof(c_name), filename);
365
+ if (namelist_find(&cname_list, c_name)) {
366
+ find_unique_cname(c_name, sizeof(c_name));
367
+ }
315
368
  }
316
- output_object_code(ctx, fo, obj, c_name, FALSE);
369
+ output_object_code(ctx, fo, obj, c_name, CNAME_TYPE_SCRIPT);
317
370
  JS_FreeValue(ctx, obj);
318
371
  }
319
372
 
@@ -352,7 +405,9 @@ void help(void)
352
405
  "-M module_name[,cname] add initialization code for an external C module\n"
353
406
  "-x byte swapped output\n"
354
407
  "-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",
408
+ "-S n set the maximum stack size to 'n' bytes (default=%d)\n"
409
+ "-s strip all the debug info\n"
410
+ "--keep-source keep the source code\n",
356
411
  JS_DEFAULT_STACK_SIZE);
357
412
  #ifdef CONFIG_LTO
358
413
  {
@@ -471,6 +526,31 @@ static int output_executable(const char *out_filename, const char *cfilename,
471
526
  }
472
527
  #endif
473
528
 
529
+ static size_t get_suffixed_size(const char *str)
530
+ {
531
+ char *p;
532
+ size_t v;
533
+ v = (size_t)strtod(str, &p);
534
+ switch(*p) {
535
+ case 'G':
536
+ v <<= 30;
537
+ break;
538
+ case 'M':
539
+ v <<= 20;
540
+ break;
541
+ case 'k':
542
+ case 'K':
543
+ v <<= 10;
544
+ break;
545
+ default:
546
+ if (*p != '\0') {
547
+ fprintf(stderr, "qjs: invalid suffix: %s\n", p);
548
+ exit(1);
549
+ }
550
+ break;
551
+ }
552
+ return v;
553
+ }
474
554
 
475
555
  typedef enum {
476
556
  OUTPUT_C,
@@ -478,9 +558,24 @@ typedef enum {
478
558
  OUTPUT_EXECUTABLE,
479
559
  } OutputTypeEnum;
480
560
 
561
+ static const char *get_short_optarg(int *poptind, int opt,
562
+ const char *arg, int argc, char **argv)
563
+ {
564
+ const char *optarg;
565
+ if (*arg) {
566
+ optarg = arg;
567
+ } else if (*poptind < argc) {
568
+ optarg = argv[(*poptind)++];
569
+ } else {
570
+ fprintf(stderr, "qjsc: expecting parameter for -%c\n", opt);
571
+ exit(1);
572
+ }
573
+ return optarg;
574
+ }
575
+
481
576
  int main(int argc, char **argv)
482
577
  {
483
- int c, i, verbose;
578
+ int i, verbose, strip_flags;
484
579
  const char *out_filename, *cname;
485
580
  char cfilename[1024];
486
581
  FILE *fo;
@@ -499,6 +594,7 @@ int main(int argc, char **argv)
499
594
  module = -1;
500
595
  byte_swap = FALSE;
501
596
  verbose = 0;
597
+ strip_flags = JS_STRIP_SOURCE;
502
598
  use_lto = FALSE;
503
599
  stack_size = 0;
504
600
  memset(&dynamic_module_list, 0, sizeof(dynamic_module_list));
@@ -507,30 +603,51 @@ int main(int argc, char **argv)
507
603
  namelist_add(&cmodule_list, "std", "std", 0);
508
604
  namelist_add(&cmodule_list, "os", "os", 0);
509
605
 
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;
606
+ optind = 1;
607
+ while (optind < argc && *argv[optind] == '-') {
608
+ char *arg = argv[optind] + 1;
609
+ const char *longopt = "";
610
+ const char *optarg;
611
+ /* a single - is not an option, it also stops argument scanning */
612
+ if (!*arg)
522
613
  break;
523
- case 'e':
524
- output_type = OUTPUT_C_MAIN;
525
- break;
526
- case 'N':
527
- cname = optarg;
528
- break;
529
- case 'f':
530
- {
614
+ optind++;
615
+ if (*arg == '-') {
616
+ longopt = arg + 1;
617
+ arg += strlen(arg);
618
+ /* -- stops argument scanning */
619
+ if (!*longopt)
620
+ break;
621
+ }
622
+ for (; *arg || *longopt; longopt = "") {
623
+ char opt = *arg;
624
+ if (opt)
625
+ arg++;
626
+ if (opt == 'h' || opt == '?' || !strcmp(longopt, "help")) {
627
+ help();
628
+ continue;
629
+ }
630
+ if (opt == 'o') {
631
+ out_filename = get_short_optarg(&optind, opt, arg, argc, argv);
632
+ break;
633
+ }
634
+ if (opt == 'c') {
635
+ output_type = OUTPUT_C;
636
+ continue;
637
+ }
638
+ if (opt == 'e') {
639
+ output_type = OUTPUT_C_MAIN;
640
+ continue;
641
+ }
642
+ if (opt == 'N') {
643
+ cname = get_short_optarg(&optind, opt, arg, argc, argv);
644
+ break;
645
+ }
646
+ if (opt == 'f') {
531
647
  const char *p;
648
+ optarg = get_short_optarg(&optind, opt, arg, argc, argv);
532
649
  p = optarg;
533
- if (!strcmp(optarg, "lto")) {
650
+ if (!strcmp(p, "lto")) {
534
651
  use_lto = TRUE;
535
652
  } else if (strstart(p, "no-", &p)) {
536
653
  use_lto = TRUE;
@@ -547,16 +664,18 @@ int main(int argc, char **argv)
547
664
  fprintf(stderr, "unsupported feature: %s\n", optarg);
548
665
  exit(1);
549
666
  }
667
+ break;
550
668
  }
551
- break;
552
- case 'm':
553
- module = 1;
554
- break;
555
- case 'M':
556
- {
669
+ if (opt == 'm') {
670
+ module = 1;
671
+ continue;
672
+ }
673
+ if (opt == 'M') {
557
674
  char *p;
558
675
  char path[1024];
559
676
  char cname[1024];
677
+
678
+ optarg = get_short_optarg(&optind, opt, arg, argc, argv);
560
679
  pstrcpy(path, sizeof(path), optarg);
561
680
  p = strchr(path, ',');
562
681
  if (p) {
@@ -566,25 +685,44 @@ int main(int argc, char **argv)
566
685
  get_c_name(cname, sizeof(cname), path);
567
686
  }
568
687
  namelist_add(&cmodule_list, path, cname, 0);
688
+ break;
569
689
  }
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;
690
+ if (opt == 'D') {
691
+ optarg = get_short_optarg(&optind, opt, arg, argc, argv);
692
+ namelist_add(&dynamic_module_list, optarg, NULL, 0);
693
+ break;
694
+ }
695
+ if (opt == 'x') {
696
+ byte_swap = 1;
697
+ continue;
698
+ }
699
+ if (opt == 'v') {
700
+ verbose++;
701
+ continue;
702
+ }
703
+ if (opt == 'p') {
704
+ c_ident_prefix = get_short_optarg(&optind, opt, arg, argc, argv);
705
+ break;
706
+ }
707
+ if (opt == 'S') {
708
+ optarg = get_short_optarg(&optind, opt, arg, argc, argv);
709
+ stack_size = get_suffixed_size(optarg);
710
+ break;
711
+ }
712
+ if (opt == 's') {
713
+ strip_flags = JS_STRIP_DEBUG;
714
+ continue;
715
+ }
716
+ if (!strcmp(longopt, "keep-source")) {
717
+ strip_flags = 0;
718
+ continue;
719
+ }
720
+ if (opt) {
721
+ fprintf(stderr, "qjsc: unknown option '-%c'\n", opt);
722
+ } else {
723
+ fprintf(stderr, "qjsc: unknown option '--%s'\n", longopt);
724
+ }
725
+ help();
588
726
  }
589
727
  }
590
728
 
@@ -620,8 +758,10 @@ int main(int argc, char **argv)
620
758
  rt = JS_NewRuntime();
621
759
  ctx = JS_NewContext(rt);
622
760
 
761
+ JS_SetStripInfo(rt, strip_flags);
762
+
623
763
  /* loader for ES6 modules */
624
- JS_SetModuleLoaderFunc(rt, NULL, jsc_module_loader, NULL);
764
+ JS_SetModuleLoaderFunc2(rt, NULL, jsc_module_loader, NULL, NULL);
625
765
 
626
766
  fprintf(fo, "/* File generated automatically by the QuickJS compiler. */\n"
627
767
  "\n"
@@ -644,7 +784,7 @@ int main(int argc, char **argv)
644
784
  }
645
785
 
646
786
  for(i = 0; i < dynamic_module_list.count; i++) {
647
- if (!jsc_module_loader(ctx, dynamic_module_list.array[i].name, NULL)) {
787
+ if (!jsc_module_loader(ctx, dynamic_module_list.array[i].name, NULL, JS_UNDEFINED)) {
648
788
  fprintf(stderr, "Could not load dynamic module '%s'\n",
649
789
  dynamic_module_list.array[i].name);
650
790
  exit(1);
@@ -682,9 +822,12 @@ int main(int argc, char **argv)
682
822
  }
683
823
  for(i = 0; i < cname_list.count; i++) {
684
824
  namelist_entry_t *e = &cname_list.array[i];
685
- if (e->flags) {
825
+ if (e->flags == CNAME_TYPE_MODULE) {
686
826
  fprintf(fo, " js_std_eval_binary(ctx, %s, %s_size, 1);\n",
687
827
  e->name, e->name);
828
+ } else if (e->flags == CNAME_TYPE_JSON_MODULE) {
829
+ fprintf(fo, " js_std_eval_binary_json_module(ctx, %s, %s_size, (const char *)%s_module_name);\n",
830
+ e->name, e->name, e->name);
688
831
  }
689
832
  }
690
833
  fprintf(fo,
@@ -700,7 +843,7 @@ int main(int argc, char **argv)
700
843
 
701
844
  /* add the module loader if necessary */
702
845
  if (feature_bitmap & (1 << FE_MODULE_LOADER)) {
703
- fprintf(fo, " JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);\n");
846
+ fprintf(fo, " JS_SetModuleLoaderFunc2(rt, NULL, js_module_loader, js_module_check_attributes, NULL);\n");
704
847
  }
705
848
 
706
849
  fprintf(fo,
@@ -709,7 +852,7 @@ int main(int argc, char **argv)
709
852
 
710
853
  for(i = 0; i < cname_list.count; i++) {
711
854
  namelist_entry_t *e = &cname_list.array[i];
712
- if (!e->flags) {
855
+ if (e->flags == CNAME_TYPE_SCRIPT) {
713
856
  fprintf(fo, " js_std_eval_binary(ctx, %s, %s_size, 0);\n",
714
857
  e->name, e->name);
715
858
  }
@@ -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,16 @@ 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")
180
+ DEF(hasIndices, "hasIndices")
181
+ DEF(ignoreCase, "ignoreCase")
182
+ DEF(multiline, "multiline")
183
+ DEF(dotAll, "dotAll")
184
+ DEF(sticky, "sticky")
185
+ DEF(unicodeSets, "unicodeSets")
175
186
  /* the following 3 atoms are only used with CONFIG_ATOMICS */
176
187
  DEF(not_equal, "not-equal")
177
188
  DEF(timed_out, "timed-out")
@@ -206,10 +217,13 @@ DEF(Int32Array, "Int32Array")
206
217
  DEF(Uint32Array, "Uint32Array")
207
218
  DEF(BigInt64Array, "BigInt64Array")
208
219
  DEF(BigUint64Array, "BigUint64Array")
220
+ DEF(Float16Array, "Float16Array")
209
221
  DEF(Float32Array, "Float32Array")
210
222
  DEF(Float64Array, "Float64Array")
211
223
  DEF(DataView, "DataView")
212
224
  DEF(BigInt, "BigInt")
225
+ DEF(WeakRef, "WeakRef")
226
+ DEF(FinalizationRegistry, "FinalizationRegistry")
213
227
  DEF(Map, "Map")
214
228
  DEF(Set, "Set") /* Map + 1 */
215
229
  DEF(WeakMap, "WeakMap") /* Map + 2 */