porffor 0.31.0 → 0.33.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.
@@ -548,7 +548,8 @@ export const __Porffor_object_isObjectOrSymbol = (arg: any): boolean => {
548
548
 
549
549
 
550
550
  // used for { foo: 5 }
551
- export const __Porffor_object_expr_init = (obj: object, key: any, value: any): void => {
551
+ export const __Porffor_object_expr_init = (obj: any, key: any, value: any): void => {
552
+ if (Porffor.wasm`local.get ${obj+1}` != Porffor.TYPES.object) obj = __Porffor_object_getObject(obj);
552
553
  let entryPtr: i32 = __Porffor_object_lookup(obj, key);
553
554
  if (entryPtr == -1) {
554
555
  // add new entry
@@ -573,7 +574,8 @@ export const __Porffor_object_expr_init = (obj: object, key: any, value: any): v
573
574
  };
574
575
 
575
576
  // used for { get foo() {} }
576
- export const __Porffor_object_expr_get = (obj: object, key: any, get: any): void => {
577
+ export const __Porffor_object_expr_get = (obj: any, key: any, get: any): void => {
578
+ if (Porffor.wasm`local.get ${obj+1}` != Porffor.TYPES.object) obj = __Porffor_object_getObject(obj);
577
579
  let entryPtr: i32 = __Porffor_object_lookup(obj, key);
578
580
  let set: any = undefined;
579
581
  if (entryPtr == -1) {
@@ -602,7 +604,8 @@ export const __Porffor_object_expr_get = (obj: object, key: any, get: any): void
602
604
  };
603
605
 
604
606
  // used for { set foo(v) {} }
605
- export const __Porffor_object_expr_set = (obj: object, key: any, set: any): void => {
607
+ export const __Porffor_object_expr_set = (obj: any, key: any, set: any): void => {
608
+ if (Porffor.wasm`local.get ${obj+1}` != Porffor.TYPES.object) obj = __Porffor_object_getObject(obj);
606
609
  let entryPtr: i32 = __Porffor_object_lookup(obj, key);
607
610
  let get: any = undefined;
608
611
  if (entryPtr == -1) {
@@ -96,19 +96,19 @@ params:[127,127],typedParams:1,returns:[127,127],typedReturns:1,
96
96
  locals:[127],localNames:["arg","arg#type","t"],
97
97
  };
98
98
  this.__Porffor_object_expr_init = {
99
- wasm:(_,{builtin})=>[[32,0],[65,7],[32,2],[32,3],[16,builtin('__Porffor_object_lookup')],[26],[34,6],[65,127],[70],[4,64],[32,0],[40,0,0],[33,8],[32,0],[32,8],[65,1],[106],[54,0,0],[32,0],[65,5],[106],[32,8],[65,14],[108],[106],[34,6],[65,1],[32,2],[32,3],[16,builtin('__Porffor_object_writeKey')],[33,7],[26],[11],[32,6],[32,4],[57,0,4],[32,6],[65,14],[32,5],[65,8],[116],[106],[59,0,12],[65,0],[65,128,1],[15]],
99
+ wasm:(_,{builtin})=>[[32,1],[65,7],[71],[4,64],[32,0],[184],[32,1],[16,builtin('__Porffor_object_getObject')],[33,6],[252,2],[34,0],[32,6],[33,1],[26],[11],[32,0],[32,1],[32,2],[32,3],[16,builtin('__Porffor_object_lookup')],[26],[34,7],[65,127],[70],[4,64],[32,0],[40,0,0],[33,8],[32,0],[32,8],[65,1],[106],[54,0,0],[32,0],[65,5],[106],[32,8],[65,14],[108],[106],[34,7],[65,1],[32,2],[32,3],[16,builtin('__Porffor_object_writeKey')],[33,6],[26],[11],[32,7],[32,4],[57,0,4],[32,7],[65,14],[32,5],[65,8],[116],[106],[59,0,12],[65,0],[65,128,1],[15]],
100
100
  params:[127,127,127,127,124,127],typedParams:1,returns:[127,127],typedReturns:1,
101
- locals:[127,127,127],localNames:["obj","obj#type","key","key#type","value","value#type","entryPtr","#last_type","size"],
101
+ locals:[127,127,127],localNames:["obj","obj#type","key","key#type","value","value#type","#last_type","entryPtr","size"],
102
102
  };
103
103
  this.__Porffor_object_expr_get = {
104
- wasm:(_,{builtin})=>[[32,0],[65,7],[32,2],[32,3],[16,builtin('__Porffor_object_lookup')],[26],[33,6],[65,0],[33,8],[65,128,1],[33,9],[32,6],[65,127],[70],[4,64],[32,0],[40,0,0],[33,10],[32,0],[32,10],[65,1],[106],[54,0,0],[32,0],[65,5],[106],[32,10],[65,14],[108],[106],[34,6],[65,1],[32,2],[32,3],[16,builtin('__Porffor_object_writeKey')],[33,7],[26],[5],[32,6],[65,1],[16,builtin('__Porffor_object_accessorSet')],[33,9],[33,8],[11],[32,6],[32,4],[32,5],[32,8],[32,9],[16,builtin('__Porffor_object_packAccessor')],[33,7],[57,0,4],[32,6],[65,15],[65,1],[65,8],[116],[106],[59,0,12],[65,0],[65,128,1],[15]],
104
+ wasm:(_,{builtin})=>[[32,1],[65,7],[71],[4,64],[32,0],[184],[32,1],[16,builtin('__Porffor_object_getObject')],[33,6],[252,2],[34,0],[32,6],[33,1],[26],[11],[32,0],[32,1],[32,2],[32,3],[16,builtin('__Porffor_object_lookup')],[26],[33,7],[65,0],[33,8],[65,128,1],[33,9],[32,7],[65,127],[70],[4,64],[32,0],[40,0,0],[33,10],[32,0],[32,10],[65,1],[106],[54,0,0],[32,0],[65,5],[106],[32,10],[65,14],[108],[106],[34,7],[65,1],[32,2],[32,3],[16,builtin('__Porffor_object_writeKey')],[33,6],[26],[5],[32,7],[65,1],[16,builtin('__Porffor_object_accessorSet')],[33,9],[33,8],[11],[32,7],[32,4],[32,5],[32,8],[32,9],[16,builtin('__Porffor_object_packAccessor')],[33,6],[57,0,4],[32,7],[65,15],[65,1],[65,8],[116],[106],[59,0,12],[65,0],[65,128,1],[15]],
105
105
  params:[127,127,127,127,127,127],typedParams:1,returns:[127,127],typedReturns:1,
106
- locals:[127,127,127,127,127],localNames:["obj","obj#type","key","key#type","get","get#type","entryPtr","#last_type","set","set#type","size"],
106
+ locals:[127,127,127,127,127],localNames:["obj","obj#type","key","key#type","get","get#type","#last_type","entryPtr","set","set#type","size"],
107
107
  };
108
108
  this.__Porffor_object_expr_set = {
109
- wasm:(_,{builtin})=>[[32,0],[65,7],[32,2],[32,3],[16,builtin('__Porffor_object_lookup')],[26],[33,6],[65,0],[33,8],[65,128,1],[33,9],[32,6],[65,127],[70],[4,64],[32,0],[40,0,0],[33,10],[32,0],[32,10],[65,1],[106],[54,0,0],[32,0],[65,5],[106],[32,10],[65,14],[108],[106],[34,6],[65,1],[32,2],[32,3],[16,builtin('__Porffor_object_writeKey')],[33,7],[26],[5],[32,6],[65,1],[16,builtin('__Porffor_object_accessorGet')],[33,9],[33,8],[11],[32,6],[32,8],[32,9],[32,4],[32,5],[16,builtin('__Porffor_object_packAccessor')],[33,7],[57,0,4],[32,6],[65,15],[65,1],[65,8],[116],[106],[59,0,12],[65,0],[65,128,1],[15]],
109
+ wasm:(_,{builtin})=>[[32,1],[65,7],[71],[4,64],[32,0],[184],[32,1],[16,builtin('__Porffor_object_getObject')],[33,6],[252,2],[34,0],[32,6],[33,1],[26],[11],[32,0],[32,1],[32,2],[32,3],[16,builtin('__Porffor_object_lookup')],[26],[33,7],[65,0],[33,8],[65,128,1],[33,9],[32,7],[65,127],[70],[4,64],[32,0],[40,0,0],[33,10],[32,0],[32,10],[65,1],[106],[54,0,0],[32,0],[65,5],[106],[32,10],[65,14],[108],[106],[34,7],[65,1],[32,2],[32,3],[16,builtin('__Porffor_object_writeKey')],[33,6],[26],[5],[32,7],[65,1],[16,builtin('__Porffor_object_accessorGet')],[33,9],[33,8],[11],[32,7],[32,8],[32,9],[32,4],[32,5],[16,builtin('__Porffor_object_packAccessor')],[33,6],[57,0,4],[32,7],[65,15],[65,1],[65,8],[116],[106],[59,0,12],[65,0],[65,128,1],[15]],
110
110
  params:[127,127,127,127,127,127],typedParams:1,returns:[127,127],typedReturns:1,
111
- locals:[127,127,127,127,127],localNames:["obj","obj#type","key","key#type","set","set#type","entryPtr","#last_type","get","get#type","size"],
111
+ locals:[127,127,127,127,127],localNames:["obj","obj#type","key","key#type","set","set#type","#last_type","entryPtr","get","get#type","size"],
112
112
  };
113
113
  this.__String_prototype_big = {
114
114
  wasm:(_,{allocPage})=>[...number(allocPage(_,'string: __String_prototype_big/out','i16')*pageSize,127),[34,2],[65,10],[106],[33,3],[32,0],[33,4],[32,0],[40,1,0],[33,5],[32,4],[32,5],[65,2],[108],[106],[33,6],[3,64],[32,4],[32,6],[72],[4,64],[32,4],[47,0,4],[33,7],[32,3],[32,7],[59,0,4],[32,4],[65,2],[106],[33,4],[32,3],[65,2],[106],[33,3],[12,1],[11],[11],[32,3],[65,60],[59,0,4],[32,3],[65,47],[59,0,6],[32,3],[65,226,0],[59,0,8],[32,3],[65,233,0],[59,0,10],[32,3],[65,231,0],[59,0,12],[32,3],[65,62],[59,0,14],[32,2],[34,9],[32,5],[65,11],[106],[34,8],[54,1,0],[32,2],[65,195,0],[15]],
@@ -37,7 +37,8 @@ const todo = (scope, msg, expectsValue = undefined) => {
37
37
  };
38
38
 
39
39
  const isFuncType = type =>
40
- type === 'FunctionDeclaration' || type === 'FunctionExpression' || type === 'ArrowFunctionExpression';
40
+ type === 'FunctionDeclaration' || type === 'FunctionExpression' || type === 'ArrowFunctionExpression' ||
41
+ type === 'ClassDeclaration' || type === 'ClassExpression';
41
42
  const hasFuncWithName = name =>
42
43
  Object.hasOwn(funcIndex, name) || Object.hasOwn(builtinFuncs, name) || Object.hasOwn(importedFuncs, name) || Object.hasOwn(internalConstrs, name);
43
44
 
@@ -70,7 +71,7 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
70
71
 
71
72
  case 'ArrowFunctionExpression':
72
73
  case 'FunctionDeclaration':
73
- case 'FunctionExpression':
74
+ case 'FunctionExpression': {
74
75
  const func = generateFunc(scope, decl);
75
76
 
76
77
  if (decl.type.endsWith('Expression')) {
@@ -78,6 +79,7 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
78
79
  }
79
80
 
80
81
  return cacheAst(decl, []);
82
+ }
81
83
 
82
84
  case 'BlockStatement':
83
85
  return cacheAst(decl, generateCode(scope, decl));
@@ -175,6 +177,10 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
175
177
  case 'MemberExpression':
176
178
  return cacheAst(decl, generateMember(scope, decl, global, name));
177
179
 
180
+ case 'ClassExpression':
181
+ case 'ClassDeclaration':
182
+ return cacheAst(decl, generateClass(scope, decl));
183
+
178
184
  case 'ExportNamedDeclaration':
179
185
  if (!decl.declaration) return todo(scope, 'unsupported export declaration');
180
186
 
@@ -1696,12 +1702,12 @@ const getNodeType = (scope, node) => {
1696
1702
  }
1697
1703
  }
1698
1704
 
1699
- if (node.type == 'ThisExpression') {
1705
+ if (node.type === 'ThisExpression') {
1700
1706
  if (!scope.constr) return getType(scope, 'globalThis');
1701
1707
  return [ [ Opcodes.local_get, '#this#type' ] ];
1702
1708
  }
1703
1709
 
1704
- if (node.type == 'MetaProperty') {
1710
+ if (node.type === 'MetaProperty') {
1705
1711
  switch (`${node.meta.name}.${node.property.name}`) {
1706
1712
  case 'new.target': {
1707
1713
  return [ [ Opcodes.local_get, '#newtarget#type' ] ];
@@ -5765,6 +5771,90 @@ const generateMember = (scope, decl, _global, _name, _objectWasm = undefined) =>
5765
5771
  return out;
5766
5772
  };
5767
5773
 
5774
+ const generateClass = (scope, decl) => {
5775
+ const expr = decl.type === 'ClassExpression';
5776
+ if (decl.superClass) return todo(scope, 'class extends is not supported yet', expr);
5777
+
5778
+ const name = decl.id.name;
5779
+ if (name == null) return todo(scope, 'unknown name for class', expr);
5780
+
5781
+ const body = decl.body.body;
5782
+ const root = {
5783
+ type: 'Identifier',
5784
+ name
5785
+ };
5786
+
5787
+ const constr = body.find(x => x.kind === 'constructor')?.value ?? {
5788
+ type: 'FunctionExpression',
5789
+ id: root,
5790
+ params: [],
5791
+ body: {
5792
+ type: 'BlockStatement',
5793
+ body: []
5794
+ }
5795
+ };
5796
+
5797
+ const func = generateFunc(scope, {
5798
+ ...constr,
5799
+ _onlyConstr: `Class constructor ${name} requires 'new'`,
5800
+ type: expr ? 'FunctionExpression' : 'FunctionDeclaration'
5801
+ });
5802
+
5803
+ const out = [];
5804
+
5805
+ for (const x of body) {
5806
+ let { type, key, value, kind, static: _static, computed } = x;
5807
+ if (type !== 'MethodDefinition' && type !== 'PropertyDefinition') return todo(scope, `class body type ${type} is not supported yet`, expr);
5808
+
5809
+ if (kind === 'constructor') continue;
5810
+
5811
+ const object = _static ? root : {
5812
+ type: 'MemberExpression',
5813
+ object: root,
5814
+ property: {
5815
+ type: 'Identifier',
5816
+ name: 'prototype'
5817
+ },
5818
+ computed: false,
5819
+ optional: false
5820
+ };
5821
+
5822
+ let k = key;
5823
+ if (!computed && key.type !== 'Literal') k = {
5824
+ type: 'Literal',
5825
+ value: key.name
5826
+ };
5827
+
5828
+ let initKind = 'init';
5829
+ if (kind === 'get' || kind === 'set') initKind = kind;
5830
+
5831
+ // default value to undefined
5832
+ value ??= DEFAULT_VALUE();
5833
+
5834
+ out.push(
5835
+ ...generate(scope, object),
5836
+ Opcodes.i32_to_u,
5837
+ ...getNodeType(scope, object),
5838
+
5839
+ ...generate(scope, k),
5840
+ ...getNodeType(scope, k),
5841
+ ...toPropertyKey(scope, true),
5842
+
5843
+ ...generate(scope, value),
5844
+ ...(initKind !== 'init' ? [ Opcodes.i32_to_u ] : []),
5845
+ ...getNodeType(scope, value),
5846
+
5847
+ [ Opcodes.call, includeBuiltin(scope, `__Porffor_object_expr_${initKind}`).index ],
5848
+
5849
+ [ Opcodes.drop ],
5850
+ [ Opcodes.drop ]
5851
+ );
5852
+ }
5853
+
5854
+ if (expr) out.push(funcRef(func.index, func.name));
5855
+ return out;
5856
+ };
5857
+
5768
5858
  globalThis._uniqId = 0;
5769
5859
  const uniqId = () => '_' + globalThis._uniqId++;
5770
5860
 
@@ -5977,17 +6067,39 @@ const generateFunc = (scope, decl) => {
5977
6067
  );
5978
6068
  }
5979
6069
 
6070
+ if (decl._onlyConstr) {
6071
+ wasm.unshift(
6072
+ // error if not being constructed
6073
+ [ Opcodes.local_get, '#newtarget' ],
6074
+ Opcodes.i32_to_u,
6075
+ [ Opcodes.i32_eqz ],
6076
+ [ Opcodes.if, Blocktype.void ],
6077
+ ...internalThrow(func, 'TypeError', decl._onlyConstr),
6078
+ [ Opcodes.end ]
6079
+ );
6080
+ }
6081
+
5980
6082
  if (name === 'main') {
5981
6083
  func.gotLastType = true;
5982
6084
  func.export = true;
5983
6085
  func.returns = [ valtypeBinary, Valtype.i32 ];
5984
6086
 
6087
+ let finalStatement = decl.body.body[decl.body.body.length - 1];
6088
+ if (finalStatement?.type === 'EmptyStatement') finalStatement = decl.body.body[decl.body.body.length - 2];
6089
+
5985
6090
  const lastInst = func.wasm[func.wasm.length - 1] ?? [ Opcodes.end ];
5986
6091
  if (lastInst[0] === Opcodes.drop) {
5987
- func.wasm.splice(func.wasm.length - 1, 1);
5988
-
5989
- const finalStatement = decl.body.body[decl.body.body.length - 1];
5990
- func.wasm.push(...getNodeType(func, finalStatement));
6092
+ if (finalStatement.type.endsWith('Declaration')) {
6093
+ // final statement is decl, force undefined
6094
+ disposeLeftover(wasm);
6095
+ func.wasm.push(
6096
+ ...number(UNDEFINED),
6097
+ ...number(TYPES.undefined, Valtype.i32)
6098
+ );
6099
+ } else {
6100
+ func.wasm.splice(func.wasm.length - 1, 1);
6101
+ func.wasm.push(...getNodeType(func, finalStatement));
6102
+ }
5991
6103
  }
5992
6104
 
5993
6105
  if (lastInst[0] === Opcodes.end || lastInst[0] === Opcodes.local_set || lastInst[0] === Opcodes.global_set) {
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.31.0+c8cd1d6c9",
4
+ "version": "0.33.0+0611169e7",
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.31.0+c8cd1d6c9';
3
+ globalThis.version = '0.33.0+0611169e7';
4
4
 
5
5
  // deno compat
6
6
  if (typeof process === 'undefined' && typeof Deno !== 'undefined') {