porffor 0.17.0-0564424f4 → 0.17.0-3c2d70d66

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.
@@ -46,6 +46,34 @@ export const __Array_prototype_slice = (_this: any[], start: number, end: number
46
46
  return out;
47
47
  };
48
48
 
49
+ // @porf-typed-array
50
+ export const __Array_prototype_fill = (_this: any[], value: any, start: any, end: any) => {
51
+ const len: i32 = _this.length;
52
+
53
+ if (Porffor.rawType(start) == Porffor.TYPES.undefined) start = 0;
54
+ if (Porffor.rawType(end) == Porffor.TYPES.undefined) end = len;
55
+
56
+ start |= 0;
57
+ end |= 0;
58
+
59
+ if (start < 0) {
60
+ start = len + start;
61
+ if (start < 0) start = 0;
62
+ }
63
+ if (start > len) start = len;
64
+ if (end < 0) {
65
+ end = len + end;
66
+ if (end < 0) end = 0;
67
+ }
68
+ if (end > len) end = len;
69
+
70
+ for (let i: i32 = start; i < end; i++) {
71
+ _this[i] = value;
72
+ }
73
+
74
+ return _this;
75
+ };
76
+
49
77
  // @porf-typed-array
50
78
  export const __Array_prototype_indexOf = (_this: any[], searchElement: any, position: number) => {
51
79
  const len: i32 = _this.length;
@@ -2403,9 +2403,120 @@ const generateVar = (scope, decl) => {
2403
2403
  const global = topLevel || decl._bare;
2404
2404
 
2405
2405
  for (const x of decl.declarations) {
2406
- const name = mapName(x.id.name);
2406
+ if (x.id.type === 'ArrayPattern') {
2407
+ const decls = [];
2408
+ const tmpName = '#destructure' + randId();
2409
+
2410
+ let i = 0;
2411
+ const elements = [...x.id.elements];
2412
+ for (const e of elements) {
2413
+ switch (e?.type) {
2414
+ case 'RestElement': { // let [ ...foo ] = []
2415
+ if (e.argument.type === 'ArrayPattern') {
2416
+ // let [ ...[a, b, c] ] = []
2417
+ elements.push(...e.argument.elements);
2418
+ } else {
2419
+ decls.push({
2420
+ type: 'VariableDeclarator',
2421
+ id: { type: 'Identifier', name: e.argument.name },
2422
+ init: {
2423
+ type: 'CallExpression',
2424
+ callee: {
2425
+ type: 'Identifier',
2426
+ name: '__Array_prototype_slice'
2427
+ },
2428
+ arguments: [
2429
+ { type: 'Identifier', name: tmpName },
2430
+ { type: 'Literal', value: i },
2431
+ {
2432
+ type: 'MemberExpression',
2433
+ object: { type: 'Identifier', name: tmpName, },
2434
+ property: { type: 'Identifier', name: 'length', }
2435
+ }
2436
+ ]
2437
+ }
2438
+ });
2439
+ }
2440
+
2441
+ continue; // skip i++
2442
+ }
2443
+
2444
+ case 'Identifier': { // let [ foo ] = []
2445
+ decls.push({
2446
+ type: 'VariableDeclarator',
2447
+ id: e,
2448
+ init: {
2449
+ type: 'MemberExpression',
2450
+ object: { type: 'Identifier', name: tmpName },
2451
+ property: { type: 'Literal', value: i }
2452
+ }
2453
+ });
2454
+
2455
+ break;
2456
+ }
2457
+
2458
+ case 'AssignmentPattern': { // let [ foo = defaultValue ] = []
2459
+ decls.push({
2460
+ type: 'VariableDeclarator',
2461
+ id: e.left,
2462
+ init: {
2463
+ type: 'LogicalExpression',
2464
+ operator: '??',
2465
+ left: {
2466
+ type: 'MemberExpression',
2467
+ object: { type: 'Identifier', name: tmpName },
2468
+ property: { type: 'Literal', value: i }
2469
+ },
2470
+ right: e.right
2471
+ }
2472
+ });
2473
+
2474
+ break;
2475
+ }
2476
+
2477
+ case 'ArrayPattern': { // let [ [ foo, bar ] ] = []
2478
+ decls.push({
2479
+ type: 'VariableDeclarator',
2480
+ id: e,
2481
+ init: {
2482
+ type: 'MemberExpression',
2483
+ object: { type: 'Identifier', name: tmpName },
2484
+ property: { type: 'Literal', value: i }
2485
+ }
2486
+ });
2407
2487
 
2408
- if (!name) return todo(scope, 'destructuring is not supported yet');
2488
+ break;
2489
+ }
2490
+
2491
+ case 'ObjectPattern':
2492
+ return todo(scope, 'object destructuring is not supported yet')
2493
+ }
2494
+
2495
+ i++;
2496
+ }
2497
+
2498
+ out = out.concat([
2499
+ ...generateVar(scope, {
2500
+ type: 'VariableDeclaration',
2501
+ declarations: [{
2502
+ type: 'VariableDeclarator',
2503
+ id: { type: 'Identifier', name: tmpName },
2504
+ init: x.init
2505
+ }],
2506
+ kind: decl.kind
2507
+ }),
2508
+ ...generateVar(scope, {
2509
+ type: 'VariableDeclaration',
2510
+ declarations: decls,
2511
+ kind: decl.kind
2512
+ })
2513
+ ]);
2514
+
2515
+ continue;
2516
+ }
2517
+
2518
+ const name = mapName(x.id.name);
2519
+ if (!name) return todo(scope, 'object destructuring is not supported yet')
2409
2520
 
2410
2521
  if (x.init && isFuncType(x.init.type)) {
2411
2522
  // hack for let a = function () { ... }
@@ -2440,7 +2551,9 @@ const generateVar = (scope, decl) => {
2440
2551
  // hack to set local as pointer before
2441
2552
  out.push(...number(scope.arrays.get(name)), [ global ? Opcodes.global_set : Opcodes.local_set, idx ]);
2442
2553
  if (generated.at(-1) == Opcodes.i32_from_u) generated.pop();
2443
- generated.pop();
2554
+ // generated.pop();
2555
+ generated.push([ Opcodes.drop ]);
2556
+
2444
2557
  out = out.concat(generated);
2445
2558
  } else {
2446
2559
  out = out.concat(generated);
@@ -2507,8 +2620,8 @@ const generateAssign = (scope, decl, _global, _name, valueUnused = false) => {
2507
2620
 
2508
2621
  // arr[i]
2509
2622
  if (decl.left.type === 'MemberExpression' && decl.left.computed) {
2510
- const newValueTmp = localTmp(scope, '__member_setter_val_tmp');
2511
- const pointerTmp = op === '=' ? -1 : localTmp(scope, '__member_setter_ptr_tmp', Valtype.i32);
2623
+ const newValueTmp = localTmp(scope, '#member_setter_val_tmp');
2624
+ const pointerTmp = localTmp(scope, '#member_setter_ptr_tmp', Valtype.i32);
2512
2625
 
2513
2626
  return [
2514
2627
  ...typeSwitch(scope, getNodeType(scope, decl.left.object), {
@@ -2520,11 +2633,11 @@ const generateAssign = (scope, decl, _global, _name, valueUnused = false) => {
2520
2633
  ...generate(scope, decl.left.property),
2521
2634
  Opcodes.i32_to_u,
2522
2635
 
2523
- // turn into byte offset by * valtypeSize (4 for i32, 8 for i64/f64)
2636
+ // turn into byte offset by * valtypeSize + 1
2524
2637
  ...number(ValtypeSize[valtype] + 1, Valtype.i32),
2525
2638
  [ Opcodes.i32_mul ],
2526
2639
  [ Opcodes.i32_add ],
2527
- ...(op === '=' ? [] : [ [ Opcodes.local_tee, pointerTmp ] ]),
2640
+ [ Opcodes.local_tee, pointerTmp ],
2528
2641
 
2529
2642
  ...(op === '=' ? generate(scope, decl.right) : performOp(scope, op, [
2530
2643
  [ Opcodes.local_get, pointerTmp ],
@@ -2534,7 +2647,11 @@ const generateAssign = (scope, decl, _global, _name, valueUnused = false) => {
2534
2647
  [ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ]
2535
2648
  ], getNodeType(scope, decl.right), false, name, true)),
2536
2649
  [ Opcodes.local_tee, newValueTmp ],
2537
- [ Opcodes.store, 0, ValtypeSize.i32 ]
2650
+ [ Opcodes.store, 0, ValtypeSize.i32 ],
2651
+
2652
+ [ Opcodes.local_get, pointerTmp ],
2653
+ ...getNodeType(scope, decl),
2654
+ [ Opcodes.i32_store8, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
2538
2655
  ],
2539
2656
 
2540
2657
  ...wrapBC({