porffor 0.50.28 → 0.55.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.
@@ -1,7 +1,10 @@
1
1
  export default () => {
2
2
  let out = '';
3
3
 
4
- const error = name => out += `export const ${name} = function (message: any) {
4
+ const errors = [];
5
+ const error = name => {
6
+ errors.push(name);
7
+ out += `export const ${name} = function (message: any) {
5
8
  if (message === undefined) message = '';
6
9
  else message = ecma262.ToString(message);
7
10
 
@@ -25,6 +28,7 @@ export const __${name}_prototype_toString = (_this: ${name}) => {
25
28
 
26
29
  return obj.name + ': ' + message;
27
30
  };`;
31
+ };
28
32
 
29
33
  error('Error');
30
34
  error('AggregateError');
@@ -38,7 +42,12 @@ export const __${name}_prototype_toString = (_this: ${name}) => {
38
42
  error('Test262Error');
39
43
  error('TodoError');
40
44
 
41
- out += `\nexport const __Test262Error_thrower = message => { throw new Test262Error(message); };`;
45
+ out += `
46
+ export const __Test262Error_thrower = message => {
47
+ throw new Test262Error(message);
48
+ };
49
+
50
+ export const __Error_isError = (x: unknown): boolean => Porffor.fastOr(${errors.map(x => `Porffor.rawType(x) == Porffor.TYPES.${x.toLowerCase()}`).join(', ')});`;
42
51
 
43
52
  return out;
44
53
  };
@@ -14,4 +14,9 @@ export const __Function_prototype_toLocaleString = (_this: Function) => __Functi
14
14
  export const __Function_prototype_apply = (_this: Function, thisArg: any, argsArray: any) => {
15
15
  argsArray = Array.from(argsArray ?? []);
16
16
  return Reflect.apply(_this, thisArg, argsArray);
17
+ };
18
+
19
+ export const __Function_prototype_bind = (_this: Function, thisArg: any, argsArray: any) => {
20
+ // todo: no good way to bind without dynamic functions or closure yet, just return function
21
+ return _this;
17
22
  };
@@ -1691,6 +1691,11 @@ params:[124,127],typedParams:1,returns:[124,127],typedReturns:1,
1691
1691
  locals:[],localNames:["message","message#type"],
1692
1692
  usesTag:1,
1693
1693
  }
1694
+ this.__Error_isError = {
1695
+ wasm:()=>[[32,1],[184],[68,40],[97],[32,1],[184],[68,41],[97],[114],[32,1],[184],[68,42],[97],[114],[32,1],[184],[68,43],[97],[114],[32,1],[184],[68,44],[97],[114],[32,1],[184],[68,45],[97],[114],[32,1],[184],[68,46],[97],[114],[32,1],[184],[68,47],[97],[114],[32,1],[184],[68,48],[97],[114],[32,1],[184],[68,49],[97],[114],[184],[65,2],[15]],
1696
+ params:[124,127],typedParams:1,returns:[124,127],typedReturns:1,
1697
+ locals:[],localNames:["x","x#type"],
1698
+ }
1694
1699
  this.escape = {
1695
1700
  wasm:(_,{i32ify,makeString,builtin})=>[...i32ify(makeString(_,"\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0001\\u0000\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0000\\u0000\\u0000\\u0000\\u0001\\u0000\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000",1)),[33,2],[32,0],[40,1,0],[34,3],[33,4],[32,0],[33,5],[32,1],[65,195,1],[70],[4,64],[32,5],[32,3],[106],[33,6],[3,64],[32,5],[32,6],[72],[4,64],[32,5],[32,5],[65,1],[106],[33,5],[45,0,4],[34,7],[65,128,1],[72],[4,64],[32,2],[32,7],[106],[45,0,4],[4,64],[12,3],[26],[11],[11],[32,4],[65,2],[106],[33,4],[12,1],[11],[11],[32,4],[32,3],[70],[4,64],[32,0],[32,1],[15],[26],[11],[16,builtin('__Porffor_allocate')],[34,8],[32,4],[54,1,0],[32,0],[33,5],[32,8],[33,9],[3,64],[32,5],[32,6],[72],[4,64],[32,5],[32,5],[65,1],[106],[33,5],[45,0,4],[34,7],[65,128,1],[72],[4,64],[32,2],[32,7],[106],[45,0,4],[4,64],[32,9],[32,9],[65,1],[106],[33,9],[32,7],[58,0,4],[12,3],[26],[11],[11],[32,9],[32,9],[65,1],[106],[33,9],[65,37],[58,0,4],[32,7],[65,15],[113],[65,48],[106],[34,10],[65,57],[74],[4,64],[32,10],[65,7],[106],[33,10],[11],[32,7],[65,4],[117],[65,48],[106],[34,11],[65,57],[74],[4,64],[32,11],[65,7],[106],[33,11],[11],[32,9],[32,9],[65,1],[106],[33,9],[32,11],[58,0,4],[32,9],[32,9],[65,1],[106],[33,9],[32,10],[58,0,4],[12,1],[11],[11],[32,8],[65,195,1],[15],[26],[11],[32,5],[32,3],[65,2],[108],[106],[33,6],[3,64],[32,5],[32,6],[72],[4,64],[32,5],[47,0,4],[33,7],[32,5],[65,2],[106],[33,5],[32,7],[65,128,1],[72],[4,64],[32,2],[32,7],[106],[45,0,4],[4,64],[12,3],[26],[11],[11],[32,7],[65,128,2],[72],[4,64],[32,4],[65,2],[106],[33,4],[5],[32,4],[65,5],[106],[33,4],[11],[12,1],[11],[11],[32,4],[32,3],[70],[4,64],[32,0],[32,1],[15],[26],[11],[16,builtin('__Porffor_allocate')],[34,8],[32,4],[54,1,0],[32,0],[33,5],[32,8],[33,9],[3,64],[32,5],[32,6],[72],[4,64],[32,5],[47,0,4],[33,7],[32,5],[65,2],[106],[33,5],[32,7],[65,128,1],[72],[4,64],[32,2],[32,7],[106],[45,0,4],[4,64],[32,9],[32,9],[65,1],[106],[33,9],[32,7],[58,0,4],[12,3],[26],[11],[11],[32,7],[65,128,2],[72],[4,64],[32,9],[32,9],[65,1],[106],[33,9],[65,37],[58,0,4],[32,7],[65,15],[113],[65,48],[106],[34,10],[65,57],[74],[4,64],[32,10],[65,7],[106],[33,10],[11],[32,7],[65,4],[117],[65,48],[106],[34,11],[65,57],[74],[4,64],[32,11],[65,7],[106],[33,11],[11],[32,9],[32,9],[65,1],[106],[33,9],[32,11],[58,0,4],[32,9],[32,9],[65,1],[106],[33,9],[32,10],[58,0,4],[5],[32,9],[65,165,234,1],[59,0,4],[32,9],[65,2],[106],[33,9],[32,7],[65,12],[117],[65,15],[113],[65,48],[106],[34,12],[65,57],[74],[4,64],[32,12],[65,7],[106],[33,12],[11],[32,9],[32,9],[65,1],[106],[33,9],[32,12],[58,0,4],[32,7],[65,8],[117],[65,15],[113],[65,48],[106],[34,12],[65,57],[74],[4,64],[32,12],[65,7],[106],[33,12],[11],[32,9],[32,9],[65,1],[106],[33,9],[32,12],[58,0,4],[32,7],[65,4],[117],[65,15],[113],[65,48],[106],[34,12],[65,57],[74],[4,64],[32,12],[65,7],[106],[33,12],[11],[32,9],[32,9],[65,1],[106],[33,9],[32,12],[58,0,4],[32,7],[65,15],[113],[65,48],[106],[34,12],[65,57],[74],[4,64],[32,12],[65,7],[106],[33,12],[11],[32,9],[32,9],[65,1],[106],[33,9],[32,12],[58,0,4],[11],[12,1],[11],[11],[32,8],[65,195,1],[15]],
1696
1701
  params:[127,127],typedParams:1,returns:[127,127],typedReturns:1,
@@ -1717,6 +1722,12 @@ locals:[124,124,127,127],localNames:["_this","_this#type","thisArg","thisArg#typ
1717
1722
  usedTypes:[80],
1718
1723
  usesTag:1,
1719
1724
  }
1725
+ this.__Function_prototype_bind = {
1726
+ wasm:(_,{internalThrow})=>[[32,1],[65,6],[71],[4,64],...internalThrow(_,'TypeError',`Function.prototype.bind expects 'this' to be a Function`),[11],[32,0],[65,6],[15]],
1727
+ params:[124,127,124,127,124,127],typedParams:1,returns:[124,127],typedReturns:1,
1728
+ locals:[],localNames:["_this","_this#type","thisArg","thisArg#type","argsArray","argsArray#type"],
1729
+ usesTag:1,
1730
+ }
1720
1731
  this.__Porffor_Generator = {
1721
1732
  wasm:()=>[[32,0],[34,2],[65,50],[15]],
1722
1733
  params:[124,127],typedParams:1,returns:[124,127],typedReturns:1,
@@ -492,6 +492,11 @@ const lookup = (scope, name, failEarly = false) => {
492
492
  if (parentLookup != null) return [ number(UNDEFINED) ];
493
493
  }
494
494
 
495
+ if (scope.name === name) {
496
+ // fallback for own func but with a different var/id name
497
+ return funcRef(funcByIndex(scope.index));
498
+ }
499
+
495
500
  if (failEarly) return null;
496
501
 
497
502
  return [ [ null, () => hoistLookup(scope, name), 1 ] ];
@@ -514,6 +519,18 @@ const generateIdent = (scope, decl) => {
514
519
  const generateYield = (scope, decl) => {
515
520
  const arg = decl.argument ?? DEFAULT_VALUE();
516
521
 
522
+ if (!scope.generator) {
523
+ // todo: access upper-scoped generator
524
+ return [
525
+ ...generate(scope, arg),
526
+ [ Opcodes.drop ],
527
+
528
+ // use undefined as yield expression value
529
+ number(0),
530
+ ...setLastType(scope, TYPES.undefined)
531
+ ];
532
+ }
533
+
517
534
  // just support a single yield like a return for now
518
535
  return [
519
536
  // return value in generator
@@ -2419,6 +2436,9 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
2419
2436
  let idx;
2420
2437
  if (Object.hasOwn(funcIndex, name)) {
2421
2438
  idx = funcIndex[name];
2439
+ } else if (scope.name === name) {
2440
+ // fallback for own func but with a different var/id name
2441
+ idx = scope.index;
2422
2442
  } else if (Object.hasOwn(importedFuncs, name)) {
2423
2443
  idx = importedFuncs[name];
2424
2444
  scope.usesImports = true;
@@ -3234,18 +3254,23 @@ const generateVarDstr = (scope, kind, pattern, init, defaultValue, global) => {
3234
3254
 
3235
3255
  const topLevel = scope.name === '#main';
3236
3256
  if (pattern.type === 'Identifier') {
3237
- let out = [];
3238
3257
  const name = pattern.name;
3239
3258
 
3240
3259
  hoist(scope, name, kind === 'var' ? 1 : 2, global);
3241
3260
 
3242
3261
  if (init && isFuncType(init.type)) {
3243
- // hack for let a = function () { ... }
3244
- if (!init.id) {
3245
- setDefaultFuncName(init, name);
3246
- generateFunc(scope, init, true);
3247
- return out;
3262
+ // opt: make decl with func expression like declaration
3263
+ setDefaultFuncName(init, name);
3264
+ const [ _func, out ] = generateFunc(scope, init, true);
3265
+
3266
+ const funcName = init.id?.name;
3267
+ if (name !== funcName) {
3268
+ funcIndex[name] = funcIndex[funcName];
3269
+ delete funcIndex[funcName];
3248
3270
  }
3271
+
3272
+ out.push([ Opcodes.drop ]);
3273
+ return out;
3249
3274
  }
3250
3275
 
3251
3276
  if (defaultValue && isFuncType(defaultValue.type)) {
@@ -3253,6 +3278,7 @@ const generateVarDstr = (scope, kind, pattern, init, defaultValue, global) => {
3253
3278
  setDefaultFuncName(defaultValue, name);
3254
3279
  }
3255
3280
 
3281
+ let out = [];
3256
3282
  if (topLevel && Object.hasOwn(builtinVars, name)) {
3257
3283
  // cannot redeclare
3258
3284
  if (kind !== 'var') return internalThrow(scope, 'SyntaxError', `Identifier '${unhackName(name)}' has already been declared`);
@@ -3611,23 +3637,6 @@ const generateAssign = (scope, decl, _global, _name, valueUnused = false) => {
3611
3637
  const { type, name } = decl.left;
3612
3638
  const [ local, isGlobal ] = lookupName(scope, name);
3613
3639
 
3614
- // if (isFuncType(decl.right.type)) {
3615
- // // hack for a = function () { ... }
3616
- // decl.right.id = { name };
3617
-
3618
- // const func = generateFunc(scope, decl.right);
3619
-
3620
- // return [
3621
- // number(func.index - importedFuncs.length),
3622
- // ...(local != null ? [
3623
- // [ isGlobal ? Opcodes.global_set : Opcodes.local_set, local.idx ],
3624
- // [ isGlobal ? Opcodes.global_get : Opcodes.local_get, local.idx ],
3625
-
3626
- // ...setType(scope, name, TYPES.function)
3627
- // ] : [])
3628
- // ];
3629
- // }
3630
-
3631
3640
  const op = decl.operator.slice(0, -1) || '=';
3632
3641
 
3633
3642
  // hack: .length setter
@@ -5299,7 +5308,7 @@ const generateMeta = (scope, decl) => {
5299
5308
  [ Opcodes.local_get, scope.locals['#newtarget'].idx ]
5300
5309
  ];
5301
5310
 
5302
- // not constructor so does not exist, return undefined
5311
+ // todo: access upper-scoped new.target
5303
5312
  return [ number(UNDEFINED) ];
5304
5313
  }
5305
5314
 
@@ -5679,6 +5688,9 @@ const generateMember = (scope, decl, _global, _name) => {
5679
5688
 
5680
5689
  // hack: .name
5681
5690
  if (decl.property.name === 'name' && hasFuncWithName(name) && !scope.noFastFuncMembers) {
5691
+ // function name can mismatch variable/access name
5692
+ name = funcByName(name)?.name ?? name;
5693
+
5682
5694
  // eg: __String_prototype_toLowerCase -> toLowerCase
5683
5695
  if (name.startsWith('__')) name = name.split('_').pop();
5684
5696
  if (name.startsWith('#')) name = '';
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.50.28",
4
+ "version": "0.55.0",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {},
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.50.28';
3
+ globalThis.version = '0.55.0';
4
4
 
5
5
  // deno compat
6
6
  if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
@@ -162,7 +162,7 @@ if (process.argv.length >= 4) {
162
162
  }
163
163
  }
164
164
 
165
- if (file.startsWith('https://')) { // https only :)
165
+ if (file && file.startsWith('https://')) { // https only :)
166
166
  // rce warning, make user confirm (disabled)
167
167
  // const rl = (await import('readline')).createInterface({ input: process.stdin, output: process.stdout });
168
168
  // const ans = await new Promise(resolve => rl.question(`\u001b[1mAre you sure you want to download this URL:\u001b[0m ${file} (y/n)? `, ans => {