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.
@@ -499,6 +499,9 @@ int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len,
499
499
  case CR_OP_XOR:
500
500
  is_in = (a_idx & 1) ^ (b_idx & 1);
501
501
  break;
502
+ case CR_OP_SUB:
503
+ is_in = (a_idx & 1) & ((b_idx & 1) ^ 1);
504
+ break;
502
505
  default:
503
506
  abort();
504
507
  }
@@ -511,14 +514,14 @@ int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len,
511
514
  return 0;
512
515
  }
513
516
 
514
- int cr_union1(CharRange *cr, const uint32_t *b_pt, int b_len)
517
+ int cr_op1(CharRange *cr, const uint32_t *b_pt, int b_len, int op)
515
518
  {
516
519
  CharRange a = *cr;
517
520
  int ret;
518
521
  cr->len = 0;
519
522
  cr->size = 0;
520
523
  cr->points = NULL;
521
- ret = cr_op(cr, a.points, a.len, b_pt, b_len, CR_OP_UNION);
524
+ ret = cr_op(cr, a.points, a.len, b_pt, b_len, op);
522
525
  cr_free(&a);
523
526
  return ret;
524
527
  }
@@ -1282,8 +1285,6 @@ int unicode_script(CharRange *cr,
1282
1285
  script_idx = unicode_find_name(unicode_script_name_table, script_name);
1283
1286
  if (script_idx < 0)
1284
1287
  return -2;
1285
- /* Note: we remove the "Unknown" Script */
1286
- script_idx += UNICODE_SCRIPT_Unknown + 1;
1287
1288
 
1288
1289
  is_common = (script_idx == UNICODE_SCRIPT_Common ||
1289
1290
  script_idx == UNICODE_SCRIPT_Inherited);
@@ -1313,17 +1314,21 @@ int unicode_script(CharRange *cr,
1313
1314
  n |= *p++;
1314
1315
  n += 96 + (1 << 12);
1315
1316
  }
1316
- if (type == 0)
1317
- v = 0;
1318
- else
1319
- v = *p++;
1320
1317
  c1 = c + n + 1;
1321
- if (v == script_idx) {
1322
- if (cr_add_interval(cr1, c, c1))
1323
- goto fail;
1318
+ if (type != 0) {
1319
+ v = *p++;
1320
+ if (v == script_idx || script_idx == UNICODE_SCRIPT_Unknown) {
1321
+ if (cr_add_interval(cr1, c, c1))
1322
+ goto fail;
1323
+ }
1324
1324
  }
1325
1325
  c = c1;
1326
1326
  }
1327
+ if (script_idx == UNICODE_SCRIPT_Unknown) {
1328
+ /* Unknown is all the characters outside scripts */
1329
+ if (cr_invert(cr1))
1330
+ goto fail;
1331
+ }
1327
1332
 
1328
1333
  if (is_ext) {
1329
1334
  /* add the script extensions */
@@ -1554,6 +1559,7 @@ static int unicode_prop_ops(CharRange *cr, ...)
1554
1559
  cr2 = &stack[stack_len - 1];
1555
1560
  cr3 = &stack[stack_len++];
1556
1561
  cr_init(cr3, cr->mem_opaque, cr->realloc_func);
1562
+ /* CR_OP_XOR may be used here */
1557
1563
  if (cr_op(cr3, cr1->points, cr1->len,
1558
1564
  cr2->points, cr2->len, op - POP_UNION + CR_OP_UNION))
1559
1565
  goto fail;
@@ -1908,3 +1914,210 @@ BOOL lre_is_space_non_ascii(uint32_t c)
1908
1914
  }
1909
1915
  return FALSE;
1910
1916
  }
1917
+
1918
+ #define SEQ_MAX_LEN 16
1919
+
1920
+ static int unicode_sequence_prop1(int seq_prop_idx, UnicodeSequencePropCB *cb, void *opaque,
1921
+ CharRange *cr)
1922
+ {
1923
+ int i, c, j;
1924
+ uint32_t seq[SEQ_MAX_LEN];
1925
+
1926
+ switch(seq_prop_idx) {
1927
+ case UNICODE_SEQUENCE_PROP_Basic_Emoji:
1928
+ if (unicode_prop1(cr, UNICODE_PROP_Basic_Emoji1) < 0)
1929
+ return -1;
1930
+ for(i = 0; i < cr->len; i += 2) {
1931
+ for(c = cr->points[i]; c < cr->points[i + 1]; c++) {
1932
+ seq[0] = c;
1933
+ cb(opaque, seq, 1);
1934
+ }
1935
+ }
1936
+
1937
+ cr->len = 0;
1938
+
1939
+ if (unicode_prop1(cr, UNICODE_PROP_Basic_Emoji2) < 0)
1940
+ return -1;
1941
+ for(i = 0; i < cr->len; i += 2) {
1942
+ for(c = cr->points[i]; c < cr->points[i + 1]; c++) {
1943
+ seq[0] = c;
1944
+ seq[1] = 0xfe0f;
1945
+ cb(opaque, seq, 2);
1946
+ }
1947
+ }
1948
+
1949
+ break;
1950
+ case UNICODE_SEQUENCE_PROP_RGI_Emoji_Modifier_Sequence:
1951
+ if (unicode_prop1(cr, UNICODE_PROP_Emoji_Modifier_Base) < 0)
1952
+ return -1;
1953
+ for(i = 0; i < cr->len; i += 2) {
1954
+ for(c = cr->points[i]; c < cr->points[i + 1]; c++) {
1955
+ for(j = 0; j < 5; j++) {
1956
+ seq[0] = c;
1957
+ seq[1] = 0x1f3fb + j;
1958
+ cb(opaque, seq, 2);
1959
+ }
1960
+ }
1961
+ }
1962
+ break;
1963
+ case UNICODE_SEQUENCE_PROP_RGI_Emoji_Flag_Sequence:
1964
+ if (unicode_prop1(cr, UNICODE_PROP_RGI_Emoji_Flag_Sequence) < 0)
1965
+ return -1;
1966
+ for(i = 0; i < cr->len; i += 2) {
1967
+ for(c = cr->points[i]; c < cr->points[i + 1]; c++) {
1968
+ int c0, c1;
1969
+ c0 = c / 26;
1970
+ c1 = c % 26;
1971
+ seq[0] = 0x1F1E6 + c0;
1972
+ seq[1] = 0x1F1E6 + c1;
1973
+ cb(opaque, seq, 2);
1974
+ }
1975
+ }
1976
+ break;
1977
+ case UNICODE_SEQUENCE_PROP_RGI_Emoji_ZWJ_Sequence:
1978
+ {
1979
+ int len, code, pres, k, mod, mod_count, mod_pos[2], hc_pos, n_mod, n_hc, mod1;
1980
+ int mod_idx, hc_idx, i0, i1;
1981
+ const uint8_t *tab = unicode_rgi_emoji_zwj_sequence;
1982
+
1983
+ for(i = 0; i < countof(unicode_rgi_emoji_zwj_sequence);) {
1984
+ len = tab[i++];
1985
+ k = 0;
1986
+ mod = 0;
1987
+ mod_count = 0;
1988
+ hc_pos = -1;
1989
+ for(j = 0; j < len; j++) {
1990
+ code = tab[i++];
1991
+ code |= tab[i++] << 8;
1992
+ pres = code >> 15;
1993
+ mod1 = (code >> 13) & 3;
1994
+ code &= 0x1fff;
1995
+ if (code < 0x1000) {
1996
+ c = code + 0x2000;
1997
+ } else {
1998
+ c = 0x1f000 + (code - 0x1000);
1999
+ }
2000
+ if (c == 0x1f9b0)
2001
+ hc_pos = k;
2002
+ seq[k++] = c;
2003
+ if (mod1 != 0) {
2004
+ assert(mod_count < 2);
2005
+ mod = mod1;
2006
+ mod_pos[mod_count++] = k;
2007
+ seq[k++] = 0; /* will be filled later */
2008
+ }
2009
+ if (pres) {
2010
+ seq[k++] = 0xfe0f;
2011
+ }
2012
+ if (j < len - 1) {
2013
+ seq[k++] = 0x200d;
2014
+ }
2015
+ }
2016
+
2017
+ /* genrate all the variants */
2018
+ switch(mod) {
2019
+ case 1:
2020
+ n_mod = 5;
2021
+ break;
2022
+ case 2:
2023
+ n_mod = 25;
2024
+ break;
2025
+ case 3:
2026
+ n_mod = 20;
2027
+ break;
2028
+ default:
2029
+ n_mod = 1;
2030
+ break;
2031
+ }
2032
+ if (hc_pos >= 0)
2033
+ n_hc = 4;
2034
+ else
2035
+ n_hc = 1;
2036
+ for(hc_idx = 0; hc_idx < n_hc; hc_idx++) {
2037
+ for(mod_idx = 0; mod_idx < n_mod; mod_idx++) {
2038
+ if (hc_pos >= 0)
2039
+ seq[hc_pos] = 0x1f9b0 + hc_idx;
2040
+
2041
+ switch(mod) {
2042
+ case 1:
2043
+ seq[mod_pos[0]] = 0x1f3fb + mod_idx;
2044
+ break;
2045
+ case 2:
2046
+ case 3:
2047
+ i0 = mod_idx / 5;
2048
+ i1 = mod_idx % 5;
2049
+ /* avoid identical values */
2050
+ if (mod == 3 && i0 >= i1)
2051
+ i0++;
2052
+ seq[mod_pos[0]] = 0x1f3fb + i0;
2053
+ seq[mod_pos[1]] = 0x1f3fb + i1;
2054
+ break;
2055
+ default:
2056
+ break;
2057
+ }
2058
+ #if 0
2059
+ for(j = 0; j < k; j++)
2060
+ printf(" %04x", seq[j]);
2061
+ printf("\n");
2062
+ #endif
2063
+ cb(opaque, seq, k);
2064
+ }
2065
+ }
2066
+ }
2067
+ }
2068
+ break;
2069
+ case UNICODE_SEQUENCE_PROP_RGI_Emoji_Tag_Sequence:
2070
+ {
2071
+ for(i = 0; i < countof(unicode_rgi_emoji_tag_sequence);) {
2072
+ j = 0;
2073
+ seq[j++] = 0x1F3F4;
2074
+ for(;;) {
2075
+ c = unicode_rgi_emoji_tag_sequence[i++];
2076
+ if (c == 0x00)
2077
+ break;
2078
+ seq[j++] = 0xe0000 + c;
2079
+ }
2080
+ seq[j++] = 0xe007f;
2081
+ cb(opaque, seq, j);
2082
+ }
2083
+ }
2084
+ break;
2085
+ case UNICODE_SEQUENCE_PROP_Emoji_Keycap_Sequence:
2086
+ if (unicode_prop1(cr, UNICODE_PROP_Emoji_Keycap_Sequence) < 0)
2087
+ return -1;
2088
+ for(i = 0; i < cr->len; i += 2) {
2089
+ for(c = cr->points[i]; c < cr->points[i + 1]; c++) {
2090
+ seq[0] = c;
2091
+ seq[1] = 0xfe0f;
2092
+ seq[2] = 0x20e3;
2093
+ cb(opaque, seq, 3);
2094
+ }
2095
+ }
2096
+ break;
2097
+ case UNICODE_SEQUENCE_PROP_RGI_Emoji:
2098
+ /* all prevous sequences */
2099
+ for(i = UNICODE_SEQUENCE_PROP_Basic_Emoji; i <= UNICODE_SEQUENCE_PROP_RGI_Emoji_ZWJ_Sequence; i++) {
2100
+ int ret;
2101
+ ret = unicode_sequence_prop1(i, cb, opaque, cr);
2102
+ if (ret < 0)
2103
+ return ret;
2104
+ cr->len = 0;
2105
+ }
2106
+ break;
2107
+ default:
2108
+ return -2;
2109
+ }
2110
+ return 0;
2111
+ }
2112
+
2113
+ /* build a unicode sequence property */
2114
+ /* return -2 if not found, -1 if other error. 'cr' is used as temporary memory. */
2115
+ int unicode_sequence_prop(const char *prop_name, UnicodeSequencePropCB *cb, void *opaque,
2116
+ CharRange *cr)
2117
+ {
2118
+ int seq_prop_idx;
2119
+ seq_prop_idx = unicode_find_name(unicode_sequence_prop_name_table, prop_name);
2120
+ if (seq_prop_idx < 0)
2121
+ return -2;
2122
+ return unicode_sequence_prop1(seq_prop_idx, cb, opaque, cr);
2123
+ }
@@ -45,6 +45,7 @@ typedef enum {
45
45
  CR_OP_UNION,
46
46
  CR_OP_INTER,
47
47
  CR_OP_XOR,
48
+ CR_OP_SUB,
48
49
  } CharRangeOpEnum;
49
50
 
50
51
  void cr_init(CharRange *cr, void *mem_opaque, void *(*realloc_func)(void *opaque, void *ptr, size_t size));
@@ -73,19 +74,18 @@ static inline int cr_add_interval(CharRange *cr, uint32_t c1, uint32_t c2)
73
74
  return 0;
74
75
  }
75
76
 
76
- int cr_union1(CharRange *cr, const uint32_t *b_pt, int b_len);
77
+ int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len,
78
+ const uint32_t *b_pt, int b_len, int op);
79
+ int cr_op1(CharRange *cr, const uint32_t *b_pt, int b_len, int op);
77
80
 
78
81
  static inline int cr_union_interval(CharRange *cr, uint32_t c1, uint32_t c2)
79
82
  {
80
83
  uint32_t b_pt[2];
81
84
  b_pt[0] = c1;
82
85
  b_pt[1] = c2 + 1;
83
- return cr_union1(cr, b_pt, 2);
86
+ return cr_op1(cr, b_pt, 2, CR_OP_UNION);
84
87
  }
85
88
 
86
- int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len,
87
- const uint32_t *b_pt, int b_len, int op);
88
-
89
89
  int cr_invert(CharRange *cr);
90
90
 
91
91
  int cr_regexp_canonicalize(CharRange *cr, int is_unicode);
@@ -107,6 +107,10 @@ int unicode_script(CharRange *cr, const char *script_name, int is_ext);
107
107
  int unicode_general_category(CharRange *cr, const char *gc_name);
108
108
  int unicode_prop(CharRange *cr, const char *prop_name);
109
109
 
110
+ typedef void UnicodeSequencePropCB(void *opaque, const uint32_t *buf, int len);
111
+ int unicode_sequence_prop(const char *prop_name, UnicodeSequencePropCB *cb, void *opaque,
112
+ CharRange *cr);
113
+
110
114
  int lre_case_conv(uint32_t *res, uint32_t c, int conv_type);
111
115
  int lre_canonicalize(uint32_t c, int is_unicode);
112
116
 
@@ -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);
@@ -427,7 +465,7 @@ int main(int argc, char **argv)
427
465
  }
428
466
 
429
467
  /* loader for ES6 modules */
430
- JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
468
+ JS_SetModuleLoaderFunc2(rt, NULL, js_module_loader, js_module_check_attributes, NULL);
431
469
 
432
470
  if (dump_unhandled_promise_rejection) {
433
471
  JS_SetHostPromiseRejectionTracker(rt, js_std_promise_rejection_tracker,
@@ -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);