quickjs 0.8.1 → 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.
- checksums.yaml +4 -4
- data/ext/quickjsrb/extconf.rb +4 -6
- data/ext/quickjsrb/quickjs/cutils.c +2 -0
- data/ext/quickjsrb/quickjs/cutils.h +20 -0
- data/ext/quickjsrb/quickjs/dtoa.c +1620 -0
- data/ext/quickjsrb/quickjs/dtoa.h +83 -0
- data/ext/quickjsrb/quickjs/libregexp.c +34 -6
- data/ext/quickjsrb/quickjs/libregexp.h +5 -0
- data/ext/quickjsrb/quickjs/libunicode-table.h +1792 -1619
- data/ext/quickjsrb/quickjs/libunicode.c +201 -201
- data/ext/quickjsrb/quickjs/qjs.c +47 -60
- data/ext/quickjsrb/quickjs/qjsc.c +136 -76
- data/ext/quickjsrb/quickjs/quickjs-atom.h +7 -17
- data/ext/quickjsrb/quickjs/quickjs-libc.c +206 -109
- data/ext/quickjsrb/quickjs/quickjs-opcode.h +4 -6
- data/ext/quickjsrb/quickjs/quickjs.c +5500 -6786
- data/ext/quickjsrb/quickjs/quickjs.h +69 -32
- data/ext/quickjsrb/quickjs/run-test262.c +10 -1
- data/ext/quickjsrb/quickjs/unicode_gen.c +19 -1
- data/ext/quickjsrb/quickjs/unicode_gen_def.h +12 -0
- data/ext/quickjsrb/quickjsrb.c +9 -10
- data/lib/quickjs/version.rb +1 -1
- metadata +4 -4
- data/ext/quickjsrb/quickjs/libbf.c +0 -8475
- data/ext/quickjsrb/quickjs/libbf.h +0 -535
@@ -537,6 +537,207 @@ int cr_invert(CharRange *cr)
|
|
537
537
|
return 0;
|
538
538
|
}
|
539
539
|
|
540
|
+
#define CASE_U (1 << 0)
|
541
|
+
#define CASE_L (1 << 1)
|
542
|
+
#define CASE_F (1 << 2)
|
543
|
+
|
544
|
+
/* use the case conversion table to generate range of characters.
|
545
|
+
CASE_U: set char if modified by uppercasing,
|
546
|
+
CASE_L: set char if modified by lowercasing,
|
547
|
+
CASE_F: set char if modified by case folding,
|
548
|
+
*/
|
549
|
+
static int unicode_case1(CharRange *cr, int case_mask)
|
550
|
+
{
|
551
|
+
#define MR(x) (1 << RUN_TYPE_ ## x)
|
552
|
+
const uint32_t tab_run_mask[3] = {
|
553
|
+
MR(U) | MR(UF) | MR(UL) | MR(LSU) | MR(U2L_399_EXT2) | MR(UF_D20) |
|
554
|
+
MR(UF_D1_EXT) | MR(U_EXT) | MR(UF_EXT2) | MR(UF_EXT3),
|
555
|
+
|
556
|
+
MR(L) | MR(LF) | MR(UL) | MR(LSU) | MR(U2L_399_EXT2) | MR(LF_EXT) | MR(LF_EXT2),
|
557
|
+
|
558
|
+
MR(UF) | MR(LF) | MR(UL) | MR(LSU) | MR(U2L_399_EXT2) | MR(LF_EXT) | MR(LF_EXT2) | MR(UF_D20) | MR(UF_D1_EXT) | MR(LF_EXT) | MR(UF_EXT2) | MR(UF_EXT3),
|
559
|
+
};
|
560
|
+
#undef MR
|
561
|
+
uint32_t mask, v, code, type, len, i, idx;
|
562
|
+
|
563
|
+
if (case_mask == 0)
|
564
|
+
return 0;
|
565
|
+
mask = 0;
|
566
|
+
for(i = 0; i < 3; i++) {
|
567
|
+
if ((case_mask >> i) & 1)
|
568
|
+
mask |= tab_run_mask[i];
|
569
|
+
}
|
570
|
+
for(idx = 0; idx < countof(case_conv_table1); idx++) {
|
571
|
+
v = case_conv_table1[idx];
|
572
|
+
type = (v >> (32 - 17 - 7 - 4)) & 0xf;
|
573
|
+
code = v >> (32 - 17);
|
574
|
+
len = (v >> (32 - 17 - 7)) & 0x7f;
|
575
|
+
if ((mask >> type) & 1) {
|
576
|
+
// printf("%d: type=%d %04x %04x\n", idx, type, code, code + len - 1);
|
577
|
+
switch(type) {
|
578
|
+
case RUN_TYPE_UL:
|
579
|
+
if ((case_mask & CASE_U) && (case_mask & (CASE_L | CASE_F)))
|
580
|
+
goto def_case;
|
581
|
+
code += ((case_mask & CASE_U) != 0);
|
582
|
+
for(i = 0; i < len; i += 2) {
|
583
|
+
if (cr_add_interval(cr, code + i, code + i + 1))
|
584
|
+
return -1;
|
585
|
+
}
|
586
|
+
break;
|
587
|
+
case RUN_TYPE_LSU:
|
588
|
+
if ((case_mask & CASE_U) && (case_mask & (CASE_L | CASE_F)))
|
589
|
+
goto def_case;
|
590
|
+
if (!(case_mask & CASE_U)) {
|
591
|
+
if (cr_add_interval(cr, code, code + 1))
|
592
|
+
return -1;
|
593
|
+
}
|
594
|
+
if (cr_add_interval(cr, code + 1, code + 2))
|
595
|
+
return -1;
|
596
|
+
if (case_mask & CASE_U) {
|
597
|
+
if (cr_add_interval(cr, code + 2, code + 3))
|
598
|
+
return -1;
|
599
|
+
}
|
600
|
+
break;
|
601
|
+
default:
|
602
|
+
def_case:
|
603
|
+
if (cr_add_interval(cr, code, code + len))
|
604
|
+
return -1;
|
605
|
+
break;
|
606
|
+
}
|
607
|
+
}
|
608
|
+
}
|
609
|
+
return 0;
|
610
|
+
}
|
611
|
+
|
612
|
+
static int point_cmp(const void *p1, const void *p2, void *arg)
|
613
|
+
{
|
614
|
+
uint32_t v1 = *(uint32_t *)p1;
|
615
|
+
uint32_t v2 = *(uint32_t *)p2;
|
616
|
+
return (v1 > v2) - (v1 < v2);
|
617
|
+
}
|
618
|
+
|
619
|
+
static void cr_sort_and_remove_overlap(CharRange *cr)
|
620
|
+
{
|
621
|
+
uint32_t start, end, start1, end1, i, j;
|
622
|
+
|
623
|
+
/* the resulting ranges are not necessarily sorted and may overlap */
|
624
|
+
rqsort(cr->points, cr->len / 2, sizeof(cr->points[0]) * 2, point_cmp, NULL);
|
625
|
+
j = 0;
|
626
|
+
for(i = 0; i < cr->len; ) {
|
627
|
+
start = cr->points[i];
|
628
|
+
end = cr->points[i + 1];
|
629
|
+
i += 2;
|
630
|
+
while (i < cr->len) {
|
631
|
+
start1 = cr->points[i];
|
632
|
+
end1 = cr->points[i + 1];
|
633
|
+
if (start1 > end) {
|
634
|
+
/* |------|
|
635
|
+
* |-------| */
|
636
|
+
break;
|
637
|
+
} else if (end1 <= end) {
|
638
|
+
/* |------|
|
639
|
+
* |--| */
|
640
|
+
i += 2;
|
641
|
+
} else {
|
642
|
+
/* |------|
|
643
|
+
* |-------| */
|
644
|
+
end = end1;
|
645
|
+
i += 2;
|
646
|
+
}
|
647
|
+
}
|
648
|
+
cr->points[j] = start;
|
649
|
+
cr->points[j + 1] = end;
|
650
|
+
j += 2;
|
651
|
+
}
|
652
|
+
cr->len = j;
|
653
|
+
}
|
654
|
+
|
655
|
+
/* canonicalize a character set using the JS regex case folding rules
|
656
|
+
(see lre_canonicalize()) */
|
657
|
+
int cr_regexp_canonicalize(CharRange *cr, BOOL is_unicode)
|
658
|
+
{
|
659
|
+
CharRange cr_inter, cr_mask, cr_result, cr_sub;
|
660
|
+
uint32_t v, code, len, i, idx, start, end, c, d_start, d_end, d;
|
661
|
+
|
662
|
+
cr_init(&cr_mask, cr->mem_opaque, cr->realloc_func);
|
663
|
+
cr_init(&cr_inter, cr->mem_opaque, cr->realloc_func);
|
664
|
+
cr_init(&cr_result, cr->mem_opaque, cr->realloc_func);
|
665
|
+
cr_init(&cr_sub, cr->mem_opaque, cr->realloc_func);
|
666
|
+
|
667
|
+
if (unicode_case1(&cr_mask, is_unicode ? CASE_F : CASE_U))
|
668
|
+
goto fail;
|
669
|
+
if (cr_op(&cr_inter, cr_mask.points, cr_mask.len, cr->points, cr->len, CR_OP_INTER))
|
670
|
+
goto fail;
|
671
|
+
|
672
|
+
if (cr_invert(&cr_mask))
|
673
|
+
goto fail;
|
674
|
+
if (cr_op(&cr_sub, cr_mask.points, cr_mask.len, cr->points, cr->len, CR_OP_INTER))
|
675
|
+
goto fail;
|
676
|
+
|
677
|
+
/* cr_inter = cr & cr_mask */
|
678
|
+
/* cr_sub = cr & ~cr_mask */
|
679
|
+
|
680
|
+
/* use the case conversion table to compute the result */
|
681
|
+
d_start = -1;
|
682
|
+
d_end = -1;
|
683
|
+
idx = 0;
|
684
|
+
v = case_conv_table1[idx];
|
685
|
+
code = v >> (32 - 17);
|
686
|
+
len = (v >> (32 - 17 - 7)) & 0x7f;
|
687
|
+
for(i = 0; i < cr_inter.len; i += 2) {
|
688
|
+
start = cr_inter.points[i];
|
689
|
+
end = cr_inter.points[i + 1];
|
690
|
+
|
691
|
+
for(c = start; c < end; c++) {
|
692
|
+
for(;;) {
|
693
|
+
if (c >= code && c < code + len)
|
694
|
+
break;
|
695
|
+
idx++;
|
696
|
+
assert(idx < countof(case_conv_table1));
|
697
|
+
v = case_conv_table1[idx];
|
698
|
+
code = v >> (32 - 17);
|
699
|
+
len = (v >> (32 - 17 - 7)) & 0x7f;
|
700
|
+
}
|
701
|
+
d = lre_case_folding_entry(c, idx, v, is_unicode);
|
702
|
+
/* try to merge with the current interval */
|
703
|
+
if (d_start == -1) {
|
704
|
+
d_start = d;
|
705
|
+
d_end = d + 1;
|
706
|
+
} else if (d_end == d) {
|
707
|
+
d_end++;
|
708
|
+
} else {
|
709
|
+
cr_add_interval(&cr_result, d_start, d_end);
|
710
|
+
d_start = d;
|
711
|
+
d_end = d + 1;
|
712
|
+
}
|
713
|
+
}
|
714
|
+
}
|
715
|
+
if (d_start != -1) {
|
716
|
+
if (cr_add_interval(&cr_result, d_start, d_end))
|
717
|
+
goto fail;
|
718
|
+
}
|
719
|
+
|
720
|
+
/* the resulting ranges are not necessarily sorted and may overlap */
|
721
|
+
cr_sort_and_remove_overlap(&cr_result);
|
722
|
+
|
723
|
+
/* or with the character not affected by the case folding */
|
724
|
+
cr->len = 0;
|
725
|
+
if (cr_op(cr, cr_result.points, cr_result.len, cr_sub.points, cr_sub.len, CR_OP_UNION))
|
726
|
+
goto fail;
|
727
|
+
|
728
|
+
cr_free(&cr_inter);
|
729
|
+
cr_free(&cr_mask);
|
730
|
+
cr_free(&cr_result);
|
731
|
+
cr_free(&cr_sub);
|
732
|
+
return 0;
|
733
|
+
fail:
|
734
|
+
cr_free(&cr_inter);
|
735
|
+
cr_free(&cr_mask);
|
736
|
+
cr_free(&cr_result);
|
737
|
+
cr_free(&cr_sub);
|
738
|
+
return -1;
|
739
|
+
}
|
740
|
+
|
540
741
|
#ifdef CONFIG_ALL_UNICODE
|
541
742
|
|
542
743
|
BOOL lre_is_id_start(uint32_t c)
|
@@ -1296,207 +1497,6 @@ static int unicode_prop1(CharRange *cr, int prop_idx)
|
|
1296
1497
|
return 0;
|
1297
1498
|
}
|
1298
1499
|
|
1299
|
-
#define CASE_U (1 << 0)
|
1300
|
-
#define CASE_L (1 << 1)
|
1301
|
-
#define CASE_F (1 << 2)
|
1302
|
-
|
1303
|
-
/* use the case conversion table to generate range of characters.
|
1304
|
-
CASE_U: set char if modified by uppercasing,
|
1305
|
-
CASE_L: set char if modified by lowercasing,
|
1306
|
-
CASE_F: set char if modified by case folding,
|
1307
|
-
*/
|
1308
|
-
static int unicode_case1(CharRange *cr, int case_mask)
|
1309
|
-
{
|
1310
|
-
#define MR(x) (1 << RUN_TYPE_ ## x)
|
1311
|
-
const uint32_t tab_run_mask[3] = {
|
1312
|
-
MR(U) | MR(UF) | MR(UL) | MR(LSU) | MR(U2L_399_EXT2) | MR(UF_D20) |
|
1313
|
-
MR(UF_D1_EXT) | MR(U_EXT) | MR(UF_EXT2) | MR(UF_EXT3),
|
1314
|
-
|
1315
|
-
MR(L) | MR(LF) | MR(UL) | MR(LSU) | MR(U2L_399_EXT2) | MR(LF_EXT) | MR(LF_EXT2),
|
1316
|
-
|
1317
|
-
MR(UF) | MR(LF) | MR(UL) | MR(LSU) | MR(U2L_399_EXT2) | MR(LF_EXT) | MR(LF_EXT2) | MR(UF_D20) | MR(UF_D1_EXT) | MR(LF_EXT) | MR(UF_EXT2) | MR(UF_EXT3),
|
1318
|
-
};
|
1319
|
-
#undef MR
|
1320
|
-
uint32_t mask, v, code, type, len, i, idx;
|
1321
|
-
|
1322
|
-
if (case_mask == 0)
|
1323
|
-
return 0;
|
1324
|
-
mask = 0;
|
1325
|
-
for(i = 0; i < 3; i++) {
|
1326
|
-
if ((case_mask >> i) & 1)
|
1327
|
-
mask |= tab_run_mask[i];
|
1328
|
-
}
|
1329
|
-
for(idx = 0; idx < countof(case_conv_table1); idx++) {
|
1330
|
-
v = case_conv_table1[idx];
|
1331
|
-
type = (v >> (32 - 17 - 7 - 4)) & 0xf;
|
1332
|
-
code = v >> (32 - 17);
|
1333
|
-
len = (v >> (32 - 17 - 7)) & 0x7f;
|
1334
|
-
if ((mask >> type) & 1) {
|
1335
|
-
// printf("%d: type=%d %04x %04x\n", idx, type, code, code + len - 1);
|
1336
|
-
switch(type) {
|
1337
|
-
case RUN_TYPE_UL:
|
1338
|
-
if ((case_mask & CASE_U) && (case_mask & (CASE_L | CASE_F)))
|
1339
|
-
goto def_case;
|
1340
|
-
code += ((case_mask & CASE_U) != 0);
|
1341
|
-
for(i = 0; i < len; i += 2) {
|
1342
|
-
if (cr_add_interval(cr, code + i, code + i + 1))
|
1343
|
-
return -1;
|
1344
|
-
}
|
1345
|
-
break;
|
1346
|
-
case RUN_TYPE_LSU:
|
1347
|
-
if ((case_mask & CASE_U) && (case_mask & (CASE_L | CASE_F)))
|
1348
|
-
goto def_case;
|
1349
|
-
if (!(case_mask & CASE_U)) {
|
1350
|
-
if (cr_add_interval(cr, code, code + 1))
|
1351
|
-
return -1;
|
1352
|
-
}
|
1353
|
-
if (cr_add_interval(cr, code + 1, code + 2))
|
1354
|
-
return -1;
|
1355
|
-
if (case_mask & CASE_U) {
|
1356
|
-
if (cr_add_interval(cr, code + 2, code + 3))
|
1357
|
-
return -1;
|
1358
|
-
}
|
1359
|
-
break;
|
1360
|
-
default:
|
1361
|
-
def_case:
|
1362
|
-
if (cr_add_interval(cr, code, code + len))
|
1363
|
-
return -1;
|
1364
|
-
break;
|
1365
|
-
}
|
1366
|
-
}
|
1367
|
-
}
|
1368
|
-
return 0;
|
1369
|
-
}
|
1370
|
-
|
1371
|
-
static int point_cmp(const void *p1, const void *p2, void *arg)
|
1372
|
-
{
|
1373
|
-
uint32_t v1 = *(uint32_t *)p1;
|
1374
|
-
uint32_t v2 = *(uint32_t *)p2;
|
1375
|
-
return (v1 > v2) - (v1 < v2);
|
1376
|
-
}
|
1377
|
-
|
1378
|
-
static void cr_sort_and_remove_overlap(CharRange *cr)
|
1379
|
-
{
|
1380
|
-
uint32_t start, end, start1, end1, i, j;
|
1381
|
-
|
1382
|
-
/* the resulting ranges are not necessarily sorted and may overlap */
|
1383
|
-
rqsort(cr->points, cr->len / 2, sizeof(cr->points[0]) * 2, point_cmp, NULL);
|
1384
|
-
j = 0;
|
1385
|
-
for(i = 0; i < cr->len; ) {
|
1386
|
-
start = cr->points[i];
|
1387
|
-
end = cr->points[i + 1];
|
1388
|
-
i += 2;
|
1389
|
-
while (i < cr->len) {
|
1390
|
-
start1 = cr->points[i];
|
1391
|
-
end1 = cr->points[i + 1];
|
1392
|
-
if (start1 > end) {
|
1393
|
-
/* |------|
|
1394
|
-
* |-------| */
|
1395
|
-
break;
|
1396
|
-
} else if (end1 <= end) {
|
1397
|
-
/* |------|
|
1398
|
-
* |--| */
|
1399
|
-
i += 2;
|
1400
|
-
} else {
|
1401
|
-
/* |------|
|
1402
|
-
* |-------| */
|
1403
|
-
end = end1;
|
1404
|
-
i += 2;
|
1405
|
-
}
|
1406
|
-
}
|
1407
|
-
cr->points[j] = start;
|
1408
|
-
cr->points[j + 1] = end;
|
1409
|
-
j += 2;
|
1410
|
-
}
|
1411
|
-
cr->len = j;
|
1412
|
-
}
|
1413
|
-
|
1414
|
-
/* canonicalize a character set using the JS regex case folding rules
|
1415
|
-
(see lre_canonicalize()) */
|
1416
|
-
int cr_regexp_canonicalize(CharRange *cr, BOOL is_unicode)
|
1417
|
-
{
|
1418
|
-
CharRange cr_inter, cr_mask, cr_result, cr_sub;
|
1419
|
-
uint32_t v, code, len, i, idx, start, end, c, d_start, d_end, d;
|
1420
|
-
|
1421
|
-
cr_init(&cr_mask, cr->mem_opaque, cr->realloc_func);
|
1422
|
-
cr_init(&cr_inter, cr->mem_opaque, cr->realloc_func);
|
1423
|
-
cr_init(&cr_result, cr->mem_opaque, cr->realloc_func);
|
1424
|
-
cr_init(&cr_sub, cr->mem_opaque, cr->realloc_func);
|
1425
|
-
|
1426
|
-
if (unicode_case1(&cr_mask, is_unicode ? CASE_F : CASE_U))
|
1427
|
-
goto fail;
|
1428
|
-
if (cr_op(&cr_inter, cr_mask.points, cr_mask.len, cr->points, cr->len, CR_OP_INTER))
|
1429
|
-
goto fail;
|
1430
|
-
|
1431
|
-
if (cr_invert(&cr_mask))
|
1432
|
-
goto fail;
|
1433
|
-
if (cr_op(&cr_sub, cr_mask.points, cr_mask.len, cr->points, cr->len, CR_OP_INTER))
|
1434
|
-
goto fail;
|
1435
|
-
|
1436
|
-
/* cr_inter = cr & cr_mask */
|
1437
|
-
/* cr_sub = cr & ~cr_mask */
|
1438
|
-
|
1439
|
-
/* use the case conversion table to compute the result */
|
1440
|
-
d_start = -1;
|
1441
|
-
d_end = -1;
|
1442
|
-
idx = 0;
|
1443
|
-
v = case_conv_table1[idx];
|
1444
|
-
code = v >> (32 - 17);
|
1445
|
-
len = (v >> (32 - 17 - 7)) & 0x7f;
|
1446
|
-
for(i = 0; i < cr_inter.len; i += 2) {
|
1447
|
-
start = cr_inter.points[i];
|
1448
|
-
end = cr_inter.points[i + 1];
|
1449
|
-
|
1450
|
-
for(c = start; c < end; c++) {
|
1451
|
-
for(;;) {
|
1452
|
-
if (c >= code && c < code + len)
|
1453
|
-
break;
|
1454
|
-
idx++;
|
1455
|
-
assert(idx < countof(case_conv_table1));
|
1456
|
-
v = case_conv_table1[idx];
|
1457
|
-
code = v >> (32 - 17);
|
1458
|
-
len = (v >> (32 - 17 - 7)) & 0x7f;
|
1459
|
-
}
|
1460
|
-
d = lre_case_folding_entry(c, idx, v, is_unicode);
|
1461
|
-
/* try to merge with the current interval */
|
1462
|
-
if (d_start == -1) {
|
1463
|
-
d_start = d;
|
1464
|
-
d_end = d + 1;
|
1465
|
-
} else if (d_end == d) {
|
1466
|
-
d_end++;
|
1467
|
-
} else {
|
1468
|
-
cr_add_interval(&cr_result, d_start, d_end);
|
1469
|
-
d_start = d;
|
1470
|
-
d_end = d + 1;
|
1471
|
-
}
|
1472
|
-
}
|
1473
|
-
}
|
1474
|
-
if (d_start != -1) {
|
1475
|
-
if (cr_add_interval(&cr_result, d_start, d_end))
|
1476
|
-
goto fail;
|
1477
|
-
}
|
1478
|
-
|
1479
|
-
/* the resulting ranges are not necessarily sorted and may overlap */
|
1480
|
-
cr_sort_and_remove_overlap(&cr_result);
|
1481
|
-
|
1482
|
-
/* or with the character not affected by the case folding */
|
1483
|
-
cr->len = 0;
|
1484
|
-
if (cr_op(cr, cr_result.points, cr_result.len, cr_sub.points, cr_sub.len, CR_OP_UNION))
|
1485
|
-
goto fail;
|
1486
|
-
|
1487
|
-
cr_free(&cr_inter);
|
1488
|
-
cr_free(&cr_mask);
|
1489
|
-
cr_free(&cr_result);
|
1490
|
-
cr_free(&cr_sub);
|
1491
|
-
return 0;
|
1492
|
-
fail:
|
1493
|
-
cr_free(&cr_inter);
|
1494
|
-
cr_free(&cr_mask);
|
1495
|
-
cr_free(&cr_result);
|
1496
|
-
cr_free(&cr_sub);
|
1497
|
-
return -1;
|
1498
|
-
}
|
1499
|
-
|
1500
1500
|
typedef enum {
|
1501
1501
|
POP_GC,
|
1502
1502
|
POP_PROP,
|
data/ext/quickjsrb/quickjs/qjs.c
CHANGED
@@ -45,11 +45,6 @@
|
|
45
45
|
|
46
46
|
extern const uint8_t qjsc_repl[];
|
47
47
|
extern const uint32_t qjsc_repl_size;
|
48
|
-
#ifdef CONFIG_BIGNUM
|
49
|
-
extern const uint8_t qjsc_qjscalc[];
|
50
|
-
extern const uint32_t qjsc_qjscalc_size;
|
51
|
-
static int bignum_ext;
|
52
|
-
#endif
|
53
48
|
|
54
49
|
static int eval_buf(JSContext *ctx, const void *buf, int buf_len,
|
55
50
|
const char *filename, int eval_flags)
|
@@ -112,14 +107,6 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
|
|
112
107
|
ctx = JS_NewContext(rt);
|
113
108
|
if (!ctx)
|
114
109
|
return NULL;
|
115
|
-
#ifdef CONFIG_BIGNUM
|
116
|
-
if (bignum_ext) {
|
117
|
-
JS_AddIntrinsicBigFloat(ctx);
|
118
|
-
JS_AddIntrinsicBigDecimal(ctx);
|
119
|
-
JS_AddIntrinsicOperators(ctx);
|
120
|
-
JS_EnableBignumExt(ctx, TRUE);
|
121
|
-
}
|
122
|
-
#endif
|
123
110
|
/* system modules */
|
124
111
|
js_init_module_std(ctx, "std");
|
125
112
|
js_init_module_os(ctx, "os");
|
@@ -270,6 +257,32 @@ static const JSMallocFunctions trace_mf = {
|
|
270
257
|
js_trace_malloc_usable_size,
|
271
258
|
};
|
272
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
|
+
|
273
286
|
#define PROG_NAME "qjs"
|
274
287
|
|
275
288
|
void help(void)
|
@@ -283,15 +296,13 @@ void help(void)
|
|
283
296
|
" --script load as ES6 script (default=autodetect)\n"
|
284
297
|
"-I --include file include an additional file\n"
|
285
298
|
" --std make 'std' and 'os' available to the loaded script\n"
|
286
|
-
#ifdef CONFIG_BIGNUM
|
287
|
-
" --bignum enable the bignum extensions (BigFloat, BigDecimal)\n"
|
288
|
-
" --qjscalc load the QJSCalc runtime (default if invoked as qjscalc)\n"
|
289
|
-
#endif
|
290
299
|
"-T --trace trace memory allocation\n"
|
291
300
|
"-d --dump dump the memory usage stats\n"
|
292
|
-
" --memory-limit n
|
293
|
-
" --stack-size n
|
294
|
-
" --unhandled-rejection
|
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"
|
295
306
|
"-q --quit just instantiate the interpreter and quit\n");
|
296
307
|
exit(1);
|
297
308
|
}
|
@@ -309,27 +320,13 @@ int main(int argc, char **argv)
|
|
309
320
|
int empty_run = 0;
|
310
321
|
int module = -1;
|
311
322
|
int load_std = 0;
|
312
|
-
int dump_unhandled_promise_rejection =
|
323
|
+
int dump_unhandled_promise_rejection = 1;
|
313
324
|
size_t memory_limit = 0;
|
314
325
|
char *include_list[32];
|
315
326
|
int i, include_count = 0;
|
316
|
-
|
317
|
-
int load_jscalc;
|
318
|
-
#endif
|
327
|
+
int strip_flags = 0;
|
319
328
|
size_t stack_size = 0;
|
320
329
|
|
321
|
-
#ifdef CONFIG_BIGNUM
|
322
|
-
/* load jscalc runtime if invoked as 'qjscalc' */
|
323
|
-
{
|
324
|
-
const char *p, *exename;
|
325
|
-
exename = argv[0];
|
326
|
-
p = strrchr(exename, '/');
|
327
|
-
if (p)
|
328
|
-
exename = p + 1;
|
329
|
-
load_jscalc = !strcmp(exename, "qjscalc");
|
330
|
-
}
|
331
|
-
#endif
|
332
|
-
|
333
330
|
/* cannot use getopt because we want to pass the command line to
|
334
331
|
the script */
|
335
332
|
optind = 1;
|
@@ -403,20 +400,10 @@ int main(int argc, char **argv)
|
|
403
400
|
load_std = 1;
|
404
401
|
continue;
|
405
402
|
}
|
406
|
-
if (!strcmp(longopt, "unhandled-rejection")) {
|
407
|
-
dump_unhandled_promise_rejection =
|
403
|
+
if (!strcmp(longopt, "no-unhandled-rejection")) {
|
404
|
+
dump_unhandled_promise_rejection = 0;
|
408
405
|
continue;
|
409
406
|
}
|
410
|
-
#ifdef CONFIG_BIGNUM
|
411
|
-
if (!strcmp(longopt, "bignum")) {
|
412
|
-
bignum_ext = 1;
|
413
|
-
continue;
|
414
|
-
}
|
415
|
-
if (!strcmp(longopt, "qjscalc")) {
|
416
|
-
load_jscalc = 1;
|
417
|
-
continue;
|
418
|
-
}
|
419
|
-
#endif
|
420
407
|
if (opt == 'q' || !strcmp(longopt, "quit")) {
|
421
408
|
empty_run++;
|
422
409
|
continue;
|
@@ -426,7 +413,7 @@ int main(int argc, char **argv)
|
|
426
413
|
fprintf(stderr, "expecting memory limit");
|
427
414
|
exit(1);
|
428
415
|
}
|
429
|
-
memory_limit = (
|
416
|
+
memory_limit = get_suffixed_size(argv[optind++]);
|
430
417
|
continue;
|
431
418
|
}
|
432
419
|
if (!strcmp(longopt, "stack-size")) {
|
@@ -434,7 +421,15 @@ int main(int argc, char **argv)
|
|
434
421
|
fprintf(stderr, "expecting stack size");
|
435
422
|
exit(1);
|
436
423
|
}
|
437
|
-
stack_size = (
|
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;
|
438
433
|
continue;
|
439
434
|
}
|
440
435
|
if (opt) {
|
@@ -446,11 +441,6 @@ int main(int argc, char **argv)
|
|
446
441
|
}
|
447
442
|
}
|
448
443
|
|
449
|
-
#ifdef CONFIG_BIGNUM
|
450
|
-
if (load_jscalc)
|
451
|
-
bignum_ext = 1;
|
452
|
-
#endif
|
453
|
-
|
454
444
|
if (trace_memory) {
|
455
445
|
js_trace_malloc_init(&trace_data);
|
456
446
|
rt = JS_NewRuntime2(&trace_mf, &trace_data);
|
@@ -465,6 +455,7 @@ int main(int argc, char **argv)
|
|
465
455
|
JS_SetMemoryLimit(rt, memory_limit);
|
466
456
|
if (stack_size != 0)
|
467
457
|
JS_SetMaxStackSize(rt, stack_size);
|
458
|
+
JS_SetStripInfo(rt, strip_flags);
|
468
459
|
js_std_set_worker_new_context_func(JS_NewCustomContext);
|
469
460
|
js_std_init_handlers(rt);
|
470
461
|
ctx = JS_NewCustomContext(rt);
|
@@ -482,11 +473,6 @@ int main(int argc, char **argv)
|
|
482
473
|
}
|
483
474
|
|
484
475
|
if (!empty_run) {
|
485
|
-
#ifdef CONFIG_BIGNUM
|
486
|
-
if (load_jscalc) {
|
487
|
-
js_std_eval_binary(ctx, qjsc_qjscalc, qjsc_qjscalc_size, 0);
|
488
|
-
}
|
489
|
-
#endif
|
490
476
|
js_std_add_helpers(ctx, argc - optind, argv + optind);
|
491
477
|
|
492
478
|
/* make 'std' and 'os' visible to non module code */
|
@@ -517,6 +503,7 @@ int main(int argc, char **argv)
|
|
517
503
|
goto fail;
|
518
504
|
}
|
519
505
|
if (interactive) {
|
506
|
+
JS_SetHostPromiseRejectionTracker(rt, NULL, NULL);
|
520
507
|
js_std_eval_binary(ctx, qjsc_repl, qjsc_repl_size, 0);
|
521
508
|
}
|
522
509
|
js_std_loop(ctx);
|