porffor 0.17.0-418ce1445 → 0.17.0-41e9c641a

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.
@@ -4,7 +4,7 @@ import { operatorOpcode } from './expression.js';
4
4
  import { BuiltinFuncs, BuiltinVars, importedFuncs, NULL, UNDEFINED } from './builtins.js';
5
5
  import { PrototypeFuncs } from './prototype.js';
6
6
  import { number } from './embedding.js';
7
- import { TYPES, TYPE_NAMES } from './types.js';
7
+ import { TYPES, TYPE_FLAGS, TYPE_NAMES, typeHasFlag } from './types.js';
8
8
  import * as Rhemyn from '../rhemyn/compile.js';
9
9
  import parse from './parse.js';
10
10
  import { log } from './log.js';
@@ -72,6 +72,9 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
72
72
  case 'ExpressionStatement':
73
73
  return generateExp(scope, decl);
74
74
 
75
+ case 'SequenceExpression':
76
+ return generateSequence(scope, decl);
77
+
75
78
  case 'CallExpression':
76
79
  return generateCall(scope, decl, global, name, valueUnused);
77
80
 
@@ -264,10 +267,12 @@ const internalThrow = (scope, constructor, message, expectsValue = Prefs.alwaysV
264
267
  argument: {
265
268
  type: 'NewExpression',
266
269
  callee: {
270
+ type: 'Identifier',
267
271
  name: constructor
268
272
  },
269
273
  arguments: [
270
274
  {
275
+ type: 'Literal',
271
276
  value: message
272
277
  }
273
278
  ]
@@ -289,12 +294,13 @@ const generateIdent = (scope, decl) => {
289
294
  return wasm.slice();
290
295
  }
291
296
 
292
- if (Object.hasOwn(builtinFuncs, name) || Object.hasOwn(internalConstrs, name)) {
293
- // todo: return an actual something
294
- return number(1);
295
- }
297
+ // todo: enable this by default in future
298
+ // if (!Object.hasOwn(funcIndex, name) && Object.hasOwn(builtinFuncs, name)) {
299
+ // includeBuiltin(scope, name);
300
+ // return number(funcIndex[name] - importedFuncs.length);
301
+ // }
296
302
 
297
- if (isExistingProtoFunc(name)) {
303
+ if (isExistingProtoFunc(name) || Object.hasOwn(internalConstrs, name) || Object.hasOwn(builtinFuncs, name)) {
298
304
  // todo: return an actual something
299
305
  return number(1);
300
306
  }
@@ -613,11 +619,14 @@ const compareStrings = (scope, left, right, bytestrings = false) => {
613
619
  };
614
620
 
615
621
  const truthy = (scope, wasm, type, intIn = false, intOut = false, forceTruthyMode = undefined) => {
616
- // if (isIntToFloatOp(wasm[wasm.length - 1])) return [
617
- // ...wasm,
618
- // ...(!intIn && intOut ? [ Opcodes.i32_to_u ] : [])
619
- // ];
620
- // if (isIntOp(wasm[wasm.length - 1])) return [ ...wasm ];
622
+ if (isIntToFloatOp(wasm[wasm.length - 1])) return [
623
+ ...wasm,
624
+ ...(!intIn && intOut ? [ Opcodes.i32_to_u ] : [])
625
+ ];
626
+ if (isIntOp(wasm[wasm.length - 1])) return [
627
+ ...wasm,
628
+ ...(intOut ? [] : [ Opcodes.i32_from ]),
629
+ ];
621
630
 
622
631
  // todo/perf: use knownType and custom bytecode here instead of typeSwitch
623
632
 
@@ -973,6 +982,33 @@ const performOp = (scope, op, left, right, leftType, rightType, _global = false,
973
982
  };
974
983
 
975
984
  const generateBinaryExp = (scope, decl, _global, _name) => {
985
+ if (decl.operator === 'instanceof') {
986
+ // very hacky basic instanceof
987
+ // todo: support dynamic right-hand side
988
+
989
+ const out = generate(scope, decl.left);
990
+ disposeLeftover(out);
991
+
992
+ const rightName = decl.right.name;
993
+ if (!rightName) return todo(scope, 'instanceof dynamic right-hand side is not supported yet', true);
994
+
995
+ const checkType = TYPES[rightName.toLowerCase()];
996
+ if (checkType == null || rightName !== TYPE_NAMES[checkType] || checkType === TYPES.undefined) return todo(scope, 'instanceof right-hand side type unsupported', true);
997
+
998
+ if ([TYPES.number, TYPES.boolean, TYPES.string, TYPES.symbol, TYPES.object].includes(checkType)) {
999
+ out.push(...number(0));
1000
+ } else {
1001
+ out.push(
1002
+ ...getNodeType(scope, decl.left),
1003
+ ...number(checkType, Valtype.i32),
1004
+ [ Opcodes.i32_eq ],
1005
+ Opcodes.i32_from_u
1006
+ );
1007
+ }
1008
+
1009
+ return out;
1010
+ }
1011
+
976
1012
  const out = performOp(scope, decl.operator, generate(scope, decl.left), generate(scope, decl.right), getNodeType(scope, decl.left), getNodeType(scope, decl.right), _global, _name);
977
1013
 
978
1014
  if (valtype !== 'i32' && ['==', '===', '!=', '!==', '>', '>=', '<', '<='].includes(decl.operator)) out.push(Opcodes.i32_from_u);
@@ -1279,7 +1315,7 @@ const getNodeType = (scope, node) => {
1279
1315
  }
1280
1316
 
1281
1317
  if (node.type === 'BinaryExpression') {
1282
- if (['==', '===', '!=', '!==', '>', '>=', '<', '<='].includes(node.operator)) return TYPES.boolean;
1318
+ if (['==', '===', '!=', '!==', '>', '>=', '<', '<=', 'instanceof'].includes(node.operator)) return TYPES.boolean;
1283
1319
  if (node.operator !== '+') return TYPES.number;
1284
1320
 
1285
1321
  const knownLeft = knownType(scope, getNodeType(scope, node.left));
@@ -1312,7 +1348,7 @@ const getNodeType = (scope, node) => {
1312
1348
  const objectKnownType = knownType(scope, getNodeType(scope, node.object));
1313
1349
  if (objectKnownType != null) {
1314
1350
  if (name === 'length') {
1315
- if ([ TYPES.string, TYPES.bytestring, TYPES.array ].includes(objectKnownType)) return TYPES.number;
1351
+ if (typeHasFlag(objectKnownType, TYPE_FLAGS.length)) return TYPES.number;
1316
1352
  else return TYPES.undefined;
1317
1353
  }
1318
1354
 
@@ -1384,9 +1420,9 @@ const countLeftover = wasm => {
1384
1420
 
1385
1421
  if (depth === 0)
1386
1422
  if ([Opcodes.throw, Opcodes.drop, Opcodes.local_set, Opcodes.global_set].includes(inst[0])) count--;
1387
- else if ([null, Opcodes.i32_eqz, Opcodes.i64_eqz, Opcodes.f64_ceil, Opcodes.f64_floor, Opcodes.f64_trunc, Opcodes.f64_nearest, Opcodes.f64_sqrt, Opcodes.local_tee, Opcodes.i32_wrap_i64, Opcodes.i64_extend_i32_s, Opcodes.i64_extend_i32_u, Opcodes.f32_demote_f64, Opcodes.f64_promote_f32, Opcodes.f64_convert_i32_s, Opcodes.f64_convert_i32_u, Opcodes.i32_clz, Opcodes.i32_ctz, Opcodes.i32_popcnt, Opcodes.f64_neg, Opcodes.end, Opcodes.i32_trunc_sat_f64_s[0], Opcodes.i32x4_extract_lane, Opcodes.i16x8_extract_lane, Opcodes.i32_load, Opcodes.i64_load, Opcodes.f64_load, Opcodes.v128_load, Opcodes.i32_load16_u, Opcodes.i32_load16_s, Opcodes.i32_load8_u, Opcodes.i32_load8_s, Opcodes.memory_grow].includes(inst[0]) && (inst[0] !== 0xfc || inst[1] < 0x04)) {}
1423
+ else if ([null, Opcodes.i32_eqz, Opcodes.i64_eqz, Opcodes.f64_ceil, Opcodes.f64_floor, Opcodes.f64_trunc, Opcodes.f64_nearest, Opcodes.f64_sqrt, Opcodes.local_tee, Opcodes.i32_wrap_i64, Opcodes.i64_extend_i32_s, Opcodes.i64_extend_i32_u, Opcodes.f32_demote_f64, Opcodes.f64_promote_f32, Opcodes.f64_convert_i32_s, Opcodes.f64_convert_i32_u, Opcodes.i32_clz, Opcodes.i32_ctz, Opcodes.i32_popcnt, Opcodes.f64_neg, Opcodes.end, Opcodes.i32_trunc_sat_f64_s[0], Opcodes.i32x4_extract_lane, Opcodes.i16x8_extract_lane, Opcodes.i32_load, Opcodes.i64_load, Opcodes.f64_load, Opcodes.f32_load, Opcodes.v128_load, Opcodes.i32_load16_u, Opcodes.i32_load16_s, Opcodes.i32_load8_u, Opcodes.i32_load8_s, Opcodes.memory_grow].includes(inst[0]) && (inst[0] !== 0xfc || inst[1] < 0x04)) {}
1388
1424
  else if ([Opcodes.local_get, Opcodes.global_get, Opcodes.f64_const, Opcodes.i32_const, Opcodes.i64_const, Opcodes.v128_const, Opcodes.memory_size].includes(inst[0])) count++;
1389
- else if ([Opcodes.i32_store, Opcodes.i64_store, Opcodes.f64_store, Opcodes.i32_store16, Opcodes.i32_store8].includes(inst[0])) count -= 2;
1425
+ else if ([Opcodes.i32_store, Opcodes.i64_store, Opcodes.f64_store, Opcodes.f32_store, Opcodes.i32_store16, Opcodes.i32_store8].includes(inst[0])) count -= 2;
1390
1426
  else if (inst[0] === Opcodes.memory_copy[0] && (inst[1] === Opcodes.memory_copy[1] || inst[1] === Opcodes.memory_init[1])) count -= 3;
1391
1427
  else if (inst[0] === Opcodes.return) count = 0;
1392
1428
  else if (inst[0] === Opcodes.call) {
@@ -1424,6 +1460,18 @@ const generateExp = (scope, decl) => {
1424
1460
  return out;
1425
1461
  };
1426
1462
 
1463
+ const generateSequence = (scope, decl) => {
1464
+ let out = [];
1465
+
1466
+ const exprs = decl.expressions;
1467
+ for (let i = 0; i < exprs.length; i++) {
1468
+ if (i > 0) disposeLeftover(out);
1469
+ out.push(...generate(scope, exprs[i]));
1470
+ }
1471
+
1472
+ return out;
1473
+ };
1474
+
1427
1475
  const CTArrayUtil = {
1428
1476
  getLengthI32: pointer => [
1429
1477
  ...number(0, Valtype.i32),
@@ -1542,7 +1590,7 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1542
1590
  if (!name && decl.callee.type === 'MemberExpression') {
1543
1591
  // megahack for /regex/.func()
1544
1592
  const funcName = decl.callee.property.name;
1545
- if (decl.callee.object.regex && Object.hasOwn(Rhemyn, funcName)) {
1593
+ if (decl.callee.object.regex && ['test'].includes(funcName)) {
1546
1594
  const regex = decl.callee.object.regex.pattern;
1547
1595
  const rhemynName = `regex_${funcName}_${regex}`;
1548
1596
 
@@ -1565,7 +1613,7 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1565
1613
  [ Opcodes.call, idx ],
1566
1614
  Opcodes.i32_from_u,
1567
1615
 
1568
- ...setLastType(scope, TYPES.boolean)
1616
+ ...setLastType(scope, Rhemyn.types[funcName])
1569
1617
  ];
1570
1618
  }
1571
1619
 
@@ -1574,24 +1622,35 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1574
1622
  target = decl.callee.object;
1575
1623
  }
1576
1624
 
1577
- // if (protoName && baseType === TYPES.string && Rhemyn[protoName]) {
1578
- // const func = Rhemyn[protoName](decl.arguments[0].regex.pattern, currentFuncIndex++);
1625
+ if (protoName) {
1626
+ if (['search'].includes(protoName)) {
1627
+ const regex = decl.arguments[0].regex.pattern;
1628
+ const rhemynName = `regex_${protoName}_${regex}`;
1579
1629
 
1580
- // funcIndex[func.name] = func.index;
1581
- // funcs.push(func);
1630
+ if (!funcIndex[rhemynName]) {
1631
+ const func = Rhemyn[protoName](regex, currentFuncIndex++, rhemynName);
1632
+ func.internal = true;
1582
1633
 
1583
- // return [
1584
- // generate(scope, decl.callee.object)
1634
+ funcIndex[func.name] = func.index;
1635
+ funcs.push(func);
1636
+ }
1585
1637
 
1586
- // // call regex func
1587
- // [ Opcodes.call, func.index ],
1588
- // Opcodes.i32_from_u
1589
- // ];
1590
- // }
1638
+ const idx = funcIndex[rhemynName];
1639
+ return [
1640
+ // make string arg
1641
+ ...generate(scope, target),
1642
+ Opcodes.i32_to_u,
1643
+ ...getNodeType(scope, target),
1591
1644
 
1592
- if (protoName) {
1593
- const protoBC = {};
1645
+ // call regex func
1646
+ [ Opcodes.call, idx ],
1647
+ Opcodes.i32_from_u,
1648
+
1649
+ ...setLastType(scope, Rhemyn.types[protoName])
1650
+ ];
1651
+ }
1594
1652
 
1653
+ const protoBC = {};
1595
1654
  const builtinProtoCands = Object.keys(builtinFuncs).filter(x => x.startsWith('__') && x.endsWith('_prototype_' + protoName));
1596
1655
 
1597
1656
  if (!decl._protoInternalCall && builtinProtoCands.length > 0) {
@@ -2012,7 +2071,7 @@ const unhackName = name => {
2012
2071
 
2013
2072
  const knownType = (scope, type) => {
2014
2073
  if (type.length === 1 && type[0][0] === Opcodes.i32_const) {
2015
- return type[0][1];
2074
+ return read_signedLEB128(type[0].slice(1));
2016
2075
  }
2017
2076
 
2018
2077
  if (typedInput && type.length === 1 && type[0][0] === Opcodes.local_get) {
@@ -2299,11 +2358,6 @@ const generateAssign = (scope, decl, _global, _name, valueUnused = false) => {
2299
2358
  const { type, name } = decl.left;
2300
2359
  const [ local, isGlobal ] = lookupName(scope, name);
2301
2360
 
2302
- if (type === 'ObjectPattern') {
2303
- // hack: ignore object parts of `var a = {} = 2`
2304
- return generate(scope, decl.right);
2305
- }
2306
-
2307
2361
  if (isFuncType(decl.right.type)) {
2308
2362
  // hack for a = function () { ... }
2309
2363
  decl.right.id = { name };
@@ -2376,10 +2430,160 @@ const generateAssign = (scope, decl, _global, _name, valueUnused = false) => {
2376
2430
  [ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ]
2377
2431
  ], getNodeType(scope, decl.right), false, name, true)),
2378
2432
  [ Opcodes.local_tee, newValueTmp ],
2379
-
2380
2433
  [ Opcodes.store, 0, ValtypeSize.i32 ]
2381
2434
  ],
2382
2435
 
2436
+ ...wrapBC({
2437
+ [TYPES.uint8array]: [
2438
+ [ Opcodes.i32_add ],
2439
+ ...(op === '=' ? [] : [ [ Opcodes.local_tee, pointerTmp ] ]),
2440
+
2441
+ ...(op === '=' ? generate(scope, decl.right) : performOp(scope, op, [
2442
+ [ Opcodes.local_get, pointerTmp ],
2443
+ [ Opcodes.i32_load8_u, 0, 4 ],
2444
+ Opcodes.i32_from_u
2445
+ ], generate(scope, decl.right), number(TYPES.number, Valtype.i32), getNodeType(scope, decl.right), false, name, true)),
2446
+ [ Opcodes.local_tee, newValueTmp ],
2447
+
2448
+ Opcodes.i32_to_u,
2449
+ [ Opcodes.i32_store8, 0, 4 ]
2450
+ ],
2451
+ [TYPES.uint8clampedarray]: [
2452
+ [ Opcodes.i32_add ],
2453
+ ...(op === '=' ? [] : [ [ Opcodes.local_tee, pointerTmp ] ]),
2454
+
2455
+ ...(op === '=' ? generate(scope, decl.right) : performOp(scope, op, [
2456
+ [ Opcodes.local_get, pointerTmp ],
2457
+ [ Opcodes.i32_load8_u, 0, 4 ],
2458
+ Opcodes.i32_from_u
2459
+ ], generate(scope, decl.right), number(TYPES.number, Valtype.i32), getNodeType(scope, decl.right), false, name, true)),
2460
+ [ Opcodes.local_tee, newValueTmp ],
2461
+
2462
+ ...number(0),
2463
+ [ Opcodes.f64_max ],
2464
+ ...number(255),
2465
+ [ Opcodes.f64_min ],
2466
+ Opcodes.i32_to_u,
2467
+ [ Opcodes.i32_store8, 0, 4 ]
2468
+ ],
2469
+ [TYPES.int8array]: [
2470
+ [ Opcodes.i32_add ],
2471
+ ...(op === '=' ? [] : [ [ Opcodes.local_tee, pointerTmp ] ]),
2472
+
2473
+ ...(op === '=' ? generate(scope, decl.right) : performOp(scope, op, [
2474
+ [ Opcodes.local_get, pointerTmp ],
2475
+ [ Opcodes.i32_load8_s, 0, 4 ],
2476
+ Opcodes.i32_from
2477
+ ], generate(scope, decl.right), number(TYPES.number, Valtype.i32), getNodeType(scope, decl.right), false, name, true)),
2478
+ [ Opcodes.local_tee, newValueTmp ],
2479
+
2480
+ Opcodes.i32_to,
2481
+ [ Opcodes.i32_store8, 0, 4 ]
2482
+ ],
2483
+ [TYPES.uint16array]: [
2484
+ ...number(2, Valtype.i32),
2485
+ [ Opcodes.i32_mul ],
2486
+ [ Opcodes.i32_add ],
2487
+ ...(op === '=' ? [] : [ [ Opcodes.local_tee, pointerTmp ] ]),
2488
+
2489
+ ...(op === '=' ? generate(scope, decl.right) : performOp(scope, op, [
2490
+ [ Opcodes.local_get, pointerTmp ],
2491
+ [ Opcodes.i32_load16_u, 0, 4 ],
2492
+ Opcodes.i32_from_u
2493
+ ], generate(scope, decl.right), number(TYPES.number, Valtype.i32), getNodeType(scope, decl.right), false, name, true)),
2494
+ [ Opcodes.local_tee, newValueTmp ],
2495
+
2496
+ Opcodes.i32_to_u,
2497
+ [ Opcodes.i32_store16, 0, 4 ]
2498
+ ],
2499
+ [TYPES.int16array]: [
2500
+ ...number(2, Valtype.i32),
2501
+ [ Opcodes.i32_mul ],
2502
+ [ Opcodes.i32_add ],
2503
+ ...(op === '=' ? [] : [ [ Opcodes.local_tee, pointerTmp ] ]),
2504
+
2505
+ ...(op === '=' ? generate(scope, decl.right) : performOp(scope, op, [
2506
+ [ Opcodes.local_get, pointerTmp ],
2507
+ [ Opcodes.i32_load16_s, 0, 4 ],
2508
+ Opcodes.i32_from
2509
+ ], generate(scope, decl.right), number(TYPES.number, Valtype.i32), getNodeType(scope, decl.right), false, name, true)),
2510
+ [ Opcodes.local_tee, newValueTmp ],
2511
+
2512
+ Opcodes.i32_to,
2513
+ [ Opcodes.i32_store16, 0, 4 ]
2514
+ ],
2515
+ [TYPES.uint32array]: [
2516
+ ...number(4, Valtype.i32),
2517
+ [ Opcodes.i32_mul ],
2518
+ [ Opcodes.i32_add ],
2519
+ ...(op === '=' ? [] : [ [ Opcodes.local_tee, pointerTmp ] ]),
2520
+
2521
+ ...(op === '=' ? generate(scope, decl.right) : performOp(scope, op, [
2522
+ [ Opcodes.local_get, pointerTmp ],
2523
+ [ Opcodes.i32_load, 0, 4 ],
2524
+ Opcodes.i32_from_u
2525
+ ], generate(scope, decl.right), number(TYPES.number, Valtype.i32), getNodeType(scope, decl.right), false, name, true)),
2526
+ [ Opcodes.local_tee, newValueTmp ],
2527
+
2528
+ Opcodes.i32_to_u,
2529
+ [ Opcodes.i32_store, 0, 4 ]
2530
+ ],
2531
+ [TYPES.int32array]: [
2532
+ ...number(4, Valtype.i32),
2533
+ [ Opcodes.i32_mul ],
2534
+ [ Opcodes.i32_add ],
2535
+ ...(op === '=' ? [] : [ [ Opcodes.local_tee, pointerTmp ] ]),
2536
+
2537
+ ...(op === '=' ? generate(scope, decl.right) : performOp(scope, op, [
2538
+ [ Opcodes.local_get, pointerTmp ],
2539
+ [ Opcodes.i32_load, 0, 4 ],
2540
+ Opcodes.i32_from
2541
+ ], generate(scope, decl.right), number(TYPES.number, Valtype.i32), getNodeType(scope, decl.right), false, name, true)),
2542
+ [ Opcodes.local_tee, newValueTmp ],
2543
+
2544
+ Opcodes.i32_to,
2545
+ [ Opcodes.i32_store, 0, 4 ]
2546
+ ],
2547
+ [TYPES.float32array]: [
2548
+ ...number(4, Valtype.i32),
2549
+ [ Opcodes.i32_mul ],
2550
+ [ Opcodes.i32_add ],
2551
+ ...(op === '=' ? [] : [ [ Opcodes.local_tee, pointerTmp ] ]),
2552
+
2553
+ ...(op === '=' ? generate(scope, decl.right) : performOp(scope, op, [
2554
+ [ Opcodes.local_get, pointerTmp ],
2555
+ [ Opcodes.f32_load, 0, 4 ],
2556
+ [ Opcodes.f64_promote_f32 ]
2557
+ ], generate(scope, decl.right), number(TYPES.number, Valtype.i32), getNodeType(scope, decl.right), false, name, true)),
2558
+ [ Opcodes.local_tee, newValueTmp ],
2559
+
2560
+ [ Opcodes.f32_demote_f64 ],
2561
+ [ Opcodes.f32_store, 0, 4 ]
2562
+ ],
2563
+ [TYPES.float64array]: [
2564
+ ...number(8, Valtype.i32),
2565
+ [ Opcodes.i32_mul ],
2566
+ [ Opcodes.i32_add ],
2567
+ ...(op === '=' ? [] : [ [ Opcodes.local_tee, pointerTmp ] ]),
2568
+
2569
+ ...(op === '=' ? generate(scope, decl.right) : performOp(scope, op, [
2570
+ [ Opcodes.local_get, pointerTmp ],
2571
+ [ Opcodes.f64_load, 0, 4 ]
2572
+ ], generate(scope, decl.right), number(TYPES.number, Valtype.i32), getNodeType(scope, decl.right), false, name, true)),
2573
+ [ Opcodes.local_tee, newValueTmp ],
2574
+
2575
+ [ Opcodes.f64_store, 0, 4 ]
2576
+ ],
2577
+ }, {
2578
+ prelude: [
2579
+ ...generate(scope, decl.left.object),
2580
+ Opcodes.i32_to_u,
2581
+ ...generate(scope, decl.left.property),
2582
+ Opcodes.i32_to_u,
2583
+ ],
2584
+ postlude: setLastType(scope, TYPES.number)
2585
+ }),
2586
+
2383
2587
  default: internalThrow(scope, 'TypeError', `Cannot assign member with non-array`)
2384
2588
  }, Blocktype.void),
2385
2589
 
@@ -2763,7 +2967,9 @@ const generateForOf = (scope, decl) => {
2763
2967
  // // todo: we should only do this for strings but we don't know at compile-time :(
2764
2968
  // hack: this is naughty and will break things!
2765
2969
  let newOut = number(0, Valtype.i32), newPointer = number(0, Valtype.i32);
2766
- if (pages.hasAnyString) {
2970
+
2971
+ const known = knownType(scope, getNodeType(scope, decl.right));
2972
+ if ((known === TYPES.string || known === TYPES.bytestring) || (pages.hasAnyString && known == null)) {
2767
2973
  // todo: we use i16 even for bytestrings which should not make a bad thing happen, just be confusing for debugging?
2768
2974
  0, [ newOut, newPointer ] = makeArray(scope, {
2769
2975
  rawElements: new Array(0)
@@ -2811,6 +3017,7 @@ const generateForOf = (scope, decl) => {
2811
3017
  [ Opcodes.end ],
2812
3018
  [ Opcodes.end ]
2813
3019
  ],
3020
+
2814
3021
  [TYPES.string]: [
2815
3022
  ...setType(scope, leftName, TYPES.string),
2816
3023
 
@@ -2919,6 +3126,7 @@ const generateForOf = (scope, decl) => {
2919
3126
  [ Opcodes.end ],
2920
3127
  [ Opcodes.end ]
2921
3128
  ],
3129
+
2922
3130
  [TYPES.set]: [
2923
3131
  [ Opcodes.loop, Blocktype.void ],
2924
3132
 
@@ -2957,6 +3165,106 @@ const generateForOf = (scope, decl) => {
2957
3165
  [ Opcodes.end ],
2958
3166
  [ Opcodes.end ]
2959
3167
  ],
3168
+
3169
+ ...wrapBC({
3170
+ [TYPES.uint8array]: [
3171
+ [ Opcodes.i32_add ],
3172
+
3173
+ [ Opcodes.i32_load8_u, 0, 4 ],
3174
+ Opcodes.i32_from_u
3175
+ ],
3176
+ [TYPES.uint8clampedarray]: [
3177
+ [ Opcodes.i32_add ],
3178
+
3179
+ [ Opcodes.i32_load8_u, 0, 4 ],
3180
+ Opcodes.i32_from_u
3181
+ ],
3182
+ [TYPES.int8array]: [
3183
+ [ Opcodes.i32_add ],
3184
+
3185
+ [ Opcodes.i32_load8_s, 0, 4 ],
3186
+ Opcodes.i32_from
3187
+ ],
3188
+ [TYPES.uint16array]: [
3189
+ ...number(2, Valtype.i32),
3190
+ [ Opcodes.i32_mul ],
3191
+ [ Opcodes.i32_add ],
3192
+
3193
+ [ Opcodes.i32_load16_u, 0, 4 ],
3194
+ Opcodes.i32_from_u
3195
+ ],
3196
+ [TYPES.int16array]: [
3197
+ ...number(2, Valtype.i32),
3198
+ [ Opcodes.i32_mul ],
3199
+ [ Opcodes.i32_add ],
3200
+
3201
+ [ Opcodes.i32_load16_s, 0, 4 ],
3202
+ Opcodes.i32_from
3203
+ ],
3204
+ [TYPES.uint32array]: [
3205
+ ...number(4, Valtype.i32),
3206
+ [ Opcodes.i32_mul ],
3207
+ [ Opcodes.i32_add ],
3208
+
3209
+ [ Opcodes.i32_load, 0, 4 ],
3210
+ Opcodes.i32_from_u
3211
+ ],
3212
+ [TYPES.int32array]: [
3213
+ ...number(4, Valtype.i32),
3214
+ [ Opcodes.i32_mul ],
3215
+ [ Opcodes.i32_add ],
3216
+
3217
+ [ Opcodes.i32_load, 0, 4 ],
3218
+ Opcodes.i32_from
3219
+ ],
3220
+ [TYPES.float32array]: [
3221
+ ...number(4, Valtype.i32),
3222
+ [ Opcodes.i32_mul ],
3223
+ [ Opcodes.i32_add ],
3224
+
3225
+ [ Opcodes.f32_load, 0, 4 ],
3226
+ [ Opcodes.f64_promote_f32 ]
3227
+ ],
3228
+ [TYPES.float64array]: [
3229
+ ...number(8, Valtype.i32),
3230
+ [ Opcodes.i32_mul ],
3231
+ [ Opcodes.i32_add ],
3232
+
3233
+ [ Opcodes.f64_load, 0, 4 ]
3234
+ ],
3235
+ }, {
3236
+ prelude: [
3237
+ ...setType(scope, leftName, TYPES.number),
3238
+
3239
+ [ Opcodes.loop, Blocktype.void ],
3240
+
3241
+ [ Opcodes.local_get, pointer ],
3242
+ [ Opcodes.local_get, counter ]
3243
+ ],
3244
+ postlude: [
3245
+ [ isGlobal ? Opcodes.global_set : Opcodes.local_set, local.idx ],
3246
+
3247
+ [ Opcodes.block, Blocktype.void ],
3248
+ [ Opcodes.block, Blocktype.void ],
3249
+ ...generate(scope, decl.body),
3250
+ [ Opcodes.end ],
3251
+
3252
+ // increment counter by 1
3253
+ [ Opcodes.local_get, counter ],
3254
+ ...number(1, Valtype.i32),
3255
+ [ Opcodes.i32_add ],
3256
+ [ Opcodes.local_tee, counter ],
3257
+
3258
+ // loop if counter != length
3259
+ [ Opcodes.local_get, length ],
3260
+ [ Opcodes.i32_ne ],
3261
+ [ Opcodes.br_if, 1 ],
3262
+
3263
+ [ Opcodes.end ],
3264
+ [ Opcodes.end ]
3265
+ ]
3266
+ }),
3267
+
2960
3268
  default: internalThrow(scope, 'TypeError', `Tried for..of on non-iterable type`)
2961
3269
  }, Blocktype.void));
2962
3270
 
@@ -3029,32 +3337,102 @@ const generateLabel = (scope, decl) => {
3029
3337
  const generateThrow = (scope, decl) => {
3030
3338
  scope.throws = true;
3031
3339
 
3032
- let message = decl.argument.value, constructor = null;
3340
+ const exceptionMode = Prefs.exceptionMode ?? 'lut';
3341
+ if (exceptionMode === 'lut') {
3342
+ let message = decl.argument.value, constructor = null;
3343
+
3344
+ // support `throw (new)? Error(...)`
3345
+ if (!message && (decl.argument.type === 'NewExpression' || decl.argument.type === 'CallExpression')) {
3346
+ constructor = decl.argument.callee.name;
3347
+ message = decl.argument.arguments[0]?.value ?? '';
3348
+ }
3349
+
3350
+ if (tags.length === 0) tags.push({
3351
+ params: [ Valtype.i32 ],
3352
+ results: [],
3353
+ idx: tags.length
3354
+ });
3355
+
3356
+ let exceptId = exceptions.findIndex(x => x.constructor === constructor && x.message === message);
3357
+ if (exceptId === -1) exceptId = exceptions.push({ constructor, message }) - 1;
3033
3358
 
3034
- // hack: throw new X("...") -> throw "..."
3035
- if (!message && (decl.argument.type === 'NewExpression' || decl.argument.type === 'CallExpression')) {
3036
- constructor = decl.argument.callee.name;
3037
- message = decl.argument.arguments[0]?.value ?? '';
3359
+ scope.exceptions ??= [];
3360
+ scope.exceptions.push(exceptId);
3361
+
3362
+ return [
3363
+ ...number(exceptId, Valtype.i32),
3364
+ [ Opcodes.throw, tags[0].idx ]
3365
+ ];
3038
3366
  }
3039
3367
 
3040
- if (tags.length === 0) tags.push({
3041
- params: [ Valtype.i32 ],
3042
- results: [],
3043
- idx: tags.length
3044
- });
3368
+ if (exceptionMode === 'stack') {
3369
+ if (tags.length === 0) tags.push({
3370
+ params: [ valtypeBinary, Valtype.i32 ],
3371
+ results: [],
3372
+ idx: tags.length
3373
+ });
3045
3374
 
3046
- let exceptId = exceptions.push({ constructor, message }) - 1;
3047
- let tagIdx = tags[0].idx;
3375
+ return [
3376
+ ...generate(scope, decl.argument),
3377
+ ...getNodeType(scope, decl.argument),
3378
+ [ Opcodes.throw, tags[0].idx ]
3379
+ ];
3380
+ }
3381
+
3382
+ if (exceptionMode === 'stackest') {
3383
+ let message = decl.argument, constructor = null;
3048
3384
 
3049
- scope.exceptions ??= [];
3050
- scope.exceptions.push(exceptId);
3385
+ // support `throw (new)? Error(...)`
3386
+ if (message.type === 'NewExpression' || message.type === 'CallExpression') {
3387
+ constructor = decl.argument.callee;
3388
+ message = decl.argument.arguments[0];
3389
+ }
3051
3390
 
3052
- // todo: write a description of how this works lol
3391
+ message ??= DEFAULT_VALUE;
3053
3392
 
3054
- return [
3055
- [ Opcodes.i32_const, signedLEB128(exceptId) ],
3056
- [ Opcodes.throw, tagIdx ]
3057
- ];
3393
+ if (tags.length === 0) tags.push({
3394
+ params: [ valtypeBinary, valtypeBinary, Valtype.i32 ],
3395
+ results: [],
3396
+ idx: tags.length
3397
+ });
3398
+
3399
+ return [
3400
+ ...(constructor == null ? number(-1) : generate(scope, constructor)),
3401
+ ...generate(scope, message),
3402
+ ...getNodeType(scope, message),
3403
+ [ Opcodes.throw, tags[0].idx ]
3404
+ ];
3405
+ }
3406
+
3407
+ if (exceptionMode === 'partial') {
3408
+ let message = decl.argument, constructor = null;
3409
+
3410
+ // support `throw (new)? Error(...)`
3411
+ if (message.type === 'NewExpression' || message.type === 'CallExpression') {
3412
+ constructor = decl.argument.callee.name;
3413
+ message = decl.argument.arguments[0];
3414
+ }
3415
+
3416
+ message ??= DEFAULT_VALUE;
3417
+
3418
+ if (tags.length === 0) tags.push({
3419
+ params: [ Valtype.i32, valtypeBinary, Valtype.i32 ],
3420
+ results: [],
3421
+ idx: tags.length
3422
+ });
3423
+
3424
+ let exceptId = exceptions.push({ constructor }) - 1;
3425
+
3426
+ scope.exceptions ??= [];
3427
+ scope.exceptions.push(exceptId);
3428
+
3429
+ return [
3430
+ ...number(exceptId, Valtype.i32),
3431
+ ...generate(scope, message),
3432
+ ...getNodeType(scope, message),
3433
+ [ Opcodes.throw, tags[0].idx ]
3434
+ ];
3435
+ }
3058
3436
  };
3059
3437
 
3060
3438
  const generateTry = (scope, decl) => {
@@ -3407,6 +3785,19 @@ const withType = (scope, wasm, type) => [
3407
3785
  ...setLastType(scope, type)
3408
3786
  ];
3409
3787
 
3788
+ const wrapBC = (bc, { prelude = [], postlude = [] } = {}) => {
3789
+ const out = {};
3790
+ for (const x in bc) {
3791
+ out[x] = [
3792
+ ...prelude,
3793
+ ...bc[x],
3794
+ ...postlude
3795
+ ];
3796
+ }
3797
+
3798
+ return out;
3799
+ };
3800
+
3410
3801
  const generateMember = (scope, decl, _global, _name) => {
3411
3802
  const name = decl.object.name;
3412
3803
 
@@ -3460,7 +3851,7 @@ const generateMember = (scope, decl, _global, _name) => {
3460
3851
  const type = getNodeType(scope, decl.object);
3461
3852
  const known = knownType(scope, type);
3462
3853
  if (known != null) {
3463
- if ([ TYPES.string, TYPES.bytestring, TYPES.array ].includes(known)) return [
3854
+ if (typeHasFlag(known, TYPE_FLAGS.length)) return [
3464
3855
  ...generate(scope, decl.object),
3465
3856
  Opcodes.i32_to_u,
3466
3857
 
@@ -3472,7 +3863,9 @@ const generateMember = (scope, decl, _global, _name) => {
3472
3863
  }
3473
3864
 
3474
3865
  return [
3475
- ...typeIsOneOf(getNodeType(scope, decl.object), [ TYPES.string, TYPES.bytestring, TYPES.array ]),
3866
+ ...getNodeType(scope, decl.object),
3867
+ ...number(TYPE_FLAGS.length, Valtype.i32),
3868
+ [ Opcodes.i32_and ],
3476
3869
  [ Opcodes.if, valtypeBinary ],
3477
3870
  ...generate(scope, decl.object),
3478
3871
  Opcodes.i32_to_u,
@@ -3521,7 +3914,9 @@ const generateMember = (scope, decl, _global, _name) => {
3521
3914
  // // todo: we should only do this for strings but we don't know at compile-time :(
3522
3915
  // hack: this is naughty and will break things!
3523
3916
  let newOut = number(0, Valtype.i32), newPointer = number(0, Valtype.i32);
3524
- if (pages.hasAnyString && knownType(scope, getNodeType(scope, decl.object)) !== TYPES.array) {
3917
+
3918
+ const known = knownType(scope, getNodeType(scope, decl.object));
3919
+ if ((known === TYPES.string || known === TYPES.bytestring) || (pages.hasAnyString && known == null)) {
3525
3920
  // todo: we use i16 even for bytestrings which should not make a bad thing happen, just be confusing for debugging?
3526
3921
  0, [ newOut, newPointer ] = makeArray(scope, {
3527
3922
  rawElements: new Array(0)
@@ -3595,6 +3990,82 @@ const generateMember = (scope, decl, _global, _name) => {
3595
3990
  ...setLastType(scope, TYPES.bytestring)
3596
3991
  ],
3597
3992
 
3993
+ ...wrapBC({
3994
+ [TYPES.uint8array]: [
3995
+ [ Opcodes.i32_add ],
3996
+
3997
+ [ Opcodes.i32_load8_u, 0, 4 ],
3998
+ Opcodes.i32_from_u
3999
+ ],
4000
+ [TYPES.uint8clampedarray]: [
4001
+ [ Opcodes.i32_add ],
4002
+
4003
+ [ Opcodes.i32_load8_u, 0, 4 ],
4004
+ Opcodes.i32_from_u
4005
+ ],
4006
+ [TYPES.int8array]: [
4007
+ [ Opcodes.i32_add ],
4008
+
4009
+ [ Opcodes.i32_load8_s, 0, 4 ],
4010
+ Opcodes.i32_from
4011
+ ],
4012
+ [TYPES.uint16array]: [
4013
+ ...number(2, Valtype.i32),
4014
+ [ Opcodes.i32_mul ],
4015
+ [ Opcodes.i32_add ],
4016
+
4017
+ [ Opcodes.i32_load16_u, 0, 4 ],
4018
+ Opcodes.i32_from_u
4019
+ ],
4020
+ [TYPES.int16array]: [
4021
+ ...number(2, Valtype.i32),
4022
+ [ Opcodes.i32_mul ],
4023
+ [ Opcodes.i32_add ],
4024
+
4025
+ [ Opcodes.i32_load16_s, 0, 4 ],
4026
+ Opcodes.i32_from
4027
+ ],
4028
+ [TYPES.uint32array]: [
4029
+ ...number(4, Valtype.i32),
4030
+ [ Opcodes.i32_mul ],
4031
+ [ Opcodes.i32_add ],
4032
+
4033
+ [ Opcodes.i32_load, 0, 4 ],
4034
+ Opcodes.i32_from_u
4035
+ ],
4036
+ [TYPES.int32array]: [
4037
+ ...number(4, Valtype.i32),
4038
+ [ Opcodes.i32_mul ],
4039
+ [ Opcodes.i32_add ],
4040
+
4041
+ [ Opcodes.i32_load, 0, 4 ],
4042
+ Opcodes.i32_from
4043
+ ],
4044
+ [TYPES.float32array]: [
4045
+ ...number(4, Valtype.i32),
4046
+ [ Opcodes.i32_mul ],
4047
+ [ Opcodes.i32_add ],
4048
+
4049
+ [ Opcodes.f32_load, 0, 4 ],
4050
+ [ Opcodes.f64_promote_f32 ]
4051
+ ],
4052
+ [TYPES.float64array]: [
4053
+ ...number(8, Valtype.i32),
4054
+ [ Opcodes.i32_mul ],
4055
+ [ Opcodes.i32_add ],
4056
+
4057
+ [ Opcodes.f64_load, 0, 4 ]
4058
+ ],
4059
+ }, {
4060
+ prelude: [
4061
+ ...object,
4062
+ Opcodes.i32_to_u,
4063
+ ...property,
4064
+ Opcodes.i32_to_u
4065
+ ],
4066
+ postlude: setLastType(scope, TYPES.number)
4067
+ }),
4068
+
3598
4069
  default: internalThrow(scope, 'TypeError', 'Member expression is not supported for non-string non-array yet', true)
3599
4070
  });
3600
4071
  };
@@ -3772,7 +4243,7 @@ const internalConstrs = {
3772
4243
 
3773
4244
  // todo: check in wasm instead of here
3774
4245
  const literalValue = arg.value ?? 0;
3775
- if (literalValue < 0 || !Number.isFinite(literalValue) || literalValue > 4294967295) return internalThrow(scope, 'RangeThrow', 'Invalid array length', true);
4246
+ if (literalValue < 0 || !Number.isFinite(literalValue) || literalValue > 4294967295) return internalThrow(scope, 'RangeError', 'Invalid array length', true);
3776
4247
 
3777
4248
  return [
3778
4249
  ...out,