porffor 0.55.2 → 0.55.4

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.
@@ -474,9 +474,8 @@ const lookup = (scope, name, failEarly = false) => {
474
474
 
475
475
  // no local var with name
476
476
  if (Object.hasOwn(globals, name)) return [ [ Opcodes.global_get, globals[name].idx ] ];
477
-
478
- if (Object.hasOwn(importedFuncs, name)) return [ number(importedFuncs[name] - importedFuncs.length) ];
479
477
  if (Object.hasOwn(funcIndex, name)) return funcRef(funcByName(name));
478
+ if (Object.hasOwn(importedFuncs, name)) return [ number(importedFuncs[name] - importedFuncs.length) ];
480
479
 
481
480
  if (name.startsWith('__')) {
482
481
  // return undefined if unknown key in already known var
@@ -567,13 +566,8 @@ const generateReturn = (scope, decl) => {
567
566
  ...generate(scope, arg),
568
567
  ...getNodeType(scope, arg),
569
568
 
570
- [ Opcodes.call, includeBuiltin(scope, scope.async ? '__Porffor_AsyncGenerator_prototype_return' : '__Porffor_Generator_prototype_return').index ],
571
- [ Opcodes.drop ],
572
- [ Opcodes.drop ],
573
-
574
- // return generator
575
- [ Opcodes.local_get, scope.locals['#generator_out'].idx ],
576
- number(scope.async ? TYPES.__porffor_asyncgenerator : TYPES.__porffor_generator, Valtype.i32),
569
+ [ Opcodes.call, includeBuiltin(scope, scope.async ? '__Porffor_AsyncGenerator_return' : '__Porffor_Generator_return').index ],
570
+ // returns generator
577
571
  [ Opcodes.return ]
578
572
  ];
579
573
  }
@@ -1709,6 +1703,7 @@ const getNodeType = (scope, node) => {
1709
1703
  }
1710
1704
 
1711
1705
  if (node.type === 'ThisExpression') {
1706
+ if (scope.overrideThisType) return scope.overrideThisType;
1712
1707
  if (!scope.constr) return getType(scope, 'globalThis');
1713
1708
  return [ [ Opcodes.local_get, scope.locals['#this#type'].idx ] ];
1714
1709
  }
@@ -2709,7 +2704,7 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
2709
2704
  }
2710
2705
 
2711
2706
  if ((builtinFuncs[name] && builtinFuncs[name].returns?.length === 0) ||
2712
- (importedFuncs[name] != null && importedFuncs[importedFuncs[name]]?.returns === 0)) {
2707
+ (idx === importedFuncs[name] && importedFuncs[importedFuncs[name]]?.returns === 0)) {
2713
2708
  out.push(number(UNDEFINED));
2714
2709
  }
2715
2710
 
@@ -2717,18 +2712,18 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
2717
2712
  };
2718
2713
 
2719
2714
  const generateThis = (scope, decl) => {
2715
+ if (scope.overrideThis) return scope.overrideThis;
2716
+
2720
2717
  if (!scope.constr) {
2721
2718
  // this in a non-constructor context is a reference to globalThis
2722
2719
  return [
2723
- ...generate(scope, { type: 'Identifier', name: 'globalThis' }),
2724
- ...setLastType(scope, getType(scope, 'globalThis'))
2720
+ ...generate(scope, { type: 'Identifier', name: 'globalThis' })
2725
2721
  ];
2726
2722
  }
2727
2723
 
2728
2724
  // opt: do not check for pure constructors or strict mode
2729
2725
  if ((!globalThis.precompile && scope.strict) || scope._onlyConstr || scope._onlyThisMethod || decl._noGlobalThis) return [
2730
- [ Opcodes.local_get, scope.locals['#this'].idx ],
2731
- ...setLastType(scope, [ [ Opcodes.local_get, scope.locals['#this#type'].idx ] ])
2726
+ [ Opcodes.local_get, scope.locals['#this'].idx ]
2732
2727
  ];
2733
2728
 
2734
2729
  return [
@@ -2749,8 +2744,7 @@ const generateThis = (scope, decl) => {
2749
2744
  ];
2750
2745
  }, 0 ],
2751
2746
 
2752
- [ Opcodes.local_get, scope.locals['#this'].idx ],
2753
- ...setLastType(scope, [ [ Opcodes.local_get, scope.locals['#this#type'].idx ] ])
2747
+ [ Opcodes.local_get, scope.locals['#this'].idx ]
2754
2748
  ];
2755
2749
  };
2756
2750
 
@@ -6190,12 +6184,25 @@ const generateClass = (scope, decl) => {
6190
6184
  );
6191
6185
  }
6192
6186
 
6187
+ scope.overrideThis = generate(scope, root);
6188
+ scope.overrideThisType = TYPES.function;
6189
+
6193
6190
  for (const x of body) {
6194
6191
  let { type, key, value, kind, static: _static, computed } = x;
6195
- if (type !== 'MethodDefinition' && type !== 'PropertyDefinition') return todo(scope, `class body type ${type} is not supported yet`, true);
6196
-
6197
6192
  if (kind === 'constructor') continue;
6198
6193
 
6194
+ if (type === 'StaticBlock') {
6195
+ // todo: make this more compliant
6196
+ out.push(
6197
+ ...generate(scope, {
6198
+ type: 'BlockStatement',
6199
+ body: x.body
6200
+ }),
6201
+ [ Opcodes.drop ]
6202
+ );
6203
+ continue;
6204
+ }
6205
+
6199
6206
  let object = _static ? root : proto;
6200
6207
 
6201
6208
  const k = getProperty(x, true);
@@ -6253,6 +6260,9 @@ const generateClass = (scope, decl) => {
6253
6260
  );
6254
6261
  }
6255
6262
 
6263
+ delete scope.overrideThis;
6264
+ delete scope.overrideThisType;
6265
+
6256
6266
  // error if not being constructed
6257
6267
  func.wasm.unshift(
6258
6268
  [ Opcodes.local_get, func.locals['#newtarget'].idx ],
@@ -6537,7 +6547,7 @@ const generateFunc = (scope, decl, forceNoExpr = false) => {
6537
6547
  addVarMetadata(func, name, false, typeAnno);
6538
6548
 
6539
6549
  // automatically add throws if unexpected this type to builtins
6540
- if (globalThis.precompile && i === 0 && func.name.includes('_prototype_')) {
6550
+ if (globalThis.precompile && i === 0 && func.name.includes('_prototype_') && !func.name.startsWith('__Porffor_')) {
6541
6551
  if (typeAnno.type === TYPES.array) {
6542
6552
  // Array.from
6543
6553
  wasm.push(
package/compiler/wrap.js CHANGED
@@ -366,10 +366,10 @@ export default (source, module = undefined, customImports = {}, print = str => p
366
366
  const disasm = disassemble(func.wasm.slice(min, max), func.name, 0, func.locals, func.params, func.returns, funcs, globals, exceptions)
367
367
  .slice(0, -1).split('\n').filter(x => !x.startsWith('\x1B[90m;;'));
368
368
 
369
- const noAnsi = s => s.replace(/\u001b\[[0-9]+m/g, '');
369
+ const noAnsi = s => s && s.replace(/\u001b\[[0-9]+m/g, '');
370
370
  let longest = 0;
371
371
  for (let j = 0; j < disassemble.length; j++) {
372
- longest = Math.max(longest, noAnsi(disassemble[j]).length);
372
+ longest = Math.max(longest, noAnsi(disassemble[j])?.length ?? 0);
373
373
  }
374
374
 
375
375
  if (middleIndex != -1) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "porffor",
3
3
  "description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
4
- "version": "0.55.2",
4
+ "version": "0.55.4",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {},
package/r.js ADDED
@@ -0,0 +1,2 @@
1
+ function print() {}
2
+ print();
package/runner/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import fs from 'node:fs';
3
- globalThis.version = '0.55.2';
3
+ globalThis.version = '0.55.4';
4
4
 
5
5
  // deno compat
6
6
  if (typeof process === 'undefined' && typeof Deno !== 'undefined') {