porffor 0.42.2 → 0.42.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.
@@ -429,7 +429,7 @@ const generateIdent = (scope, decl) => {
429
429
  ];
430
430
  };
431
431
 
432
- return lookup(decl.name);
432
+ return lookup(decl.name, scope.identFailEarly);
433
433
  };
434
434
 
435
435
  const generateReturn = (scope, decl) => {
@@ -1922,7 +1922,7 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1922
1922
 
1923
1923
  // opt: virtualize iifes
1924
1924
  if (isFuncType(decl.callee.type)) {
1925
- const [ func ] = generateFunc(scope, decl.callee);
1925
+ const [ func ] = generateFunc(scope, decl.callee, true);
1926
1926
  name = func.name;
1927
1927
  }
1928
1928
 
@@ -2999,7 +2999,7 @@ const generateVarDstr = (scope, kind, pattern, init, defaultValue, global) => {
2999
2999
  // hack for let a = function () { ... }
3000
3000
  if (!init.id) {
3001
3001
  init.id = { name };
3002
- generateFunc(scope, init);
3002
+ generateFunc(scope, init, true);
3003
3003
  return out;
3004
3004
  }
3005
3005
  }
@@ -5988,7 +5988,7 @@ const funcByIndex = idx => {
5988
5988
  };
5989
5989
  const funcByName = name => funcByIndex(funcIndex[name]);
5990
5990
 
5991
- const generateFunc = (scope, decl) => {
5991
+ const generateFunc = (scope, decl, forceNoExpr = false) => {
5992
5992
  const name = decl.id ? decl.id.name : `#anonymous${uniqId()}`;
5993
5993
  if (decl.type.startsWith('Class')) {
5994
5994
  const out = generateClass(scope, {
@@ -6022,20 +6022,8 @@ const generateFunc = (scope, decl) => {
6022
6022
  generate() {
6023
6023
  if (func.wasm) return func.wasm;
6024
6024
 
6025
- let errorWasm = null;
6026
- if (decl.generator) {
6027
- errorWasm = todo(func, 'generator functions are not supported');
6028
- }
6029
-
6030
- if (errorWasm) {
6031
- return func.wasm = errorWasm.concat([
6032
- ...number(UNDEFINED),
6033
- ...number(TYPES.undefined, Valtype.i32)
6034
- ]);
6035
- }
6036
-
6037
6025
  // generating, stub _wasm
6038
- func.wasm = [];
6026
+ let wasm = func.wasm = [];
6039
6027
 
6040
6028
  let body = objectHack(decl.body);
6041
6029
  if (decl.type === 'ArrowFunctionExpression' && decl.expression) {
@@ -6046,27 +6034,75 @@ const generateFunc = (scope, decl) => {
6046
6034
  };
6047
6035
  }
6048
6036
 
6049
- for (const x in defaultValues) {
6050
- prelude.push(
6051
- ...getType(func, x),
6037
+ func.identFailEarly = true;
6038
+ let localInd = args.length * 2;
6039
+ for (let i = 0; i < args.length; i++) {
6040
+ const { name, def, destr, type } = args[i];
6041
+
6042
+ func.localInd = i * 2;
6043
+ allocVar(func, name, false);
6044
+
6045
+ func.localInd = localInd;
6046
+ if (type) {
6047
+ const typeAnno = extractTypeAnnotation(type);
6048
+ addVarMetadata(func, name, false, typeAnno);
6049
+
6050
+ // automatically add throws if unexpected this type to builtins
6051
+ if (globalThis.precompile && i === 0 && func.name.includes('_prototype_') && [
6052
+ TYPES.date, TYPES.number, TYPES.promise, TYPES.symbol,
6053
+ TYPES.set, TYPES.map,
6054
+ TYPES.weakref, TYPES.weakset, TYPES.weakmap,
6055
+ TYPES.arraybuffer, TYPES.sharedarraybuffer, TYPES.dataview
6056
+ ].includes(typeAnno.type)) {
6057
+ let types = [ typeAnno.type ];
6058
+ if (typeAnno.type === TYPES.number) types.push(TYPES.numberobject);
6059
+ if (typeAnno.type === TYPES.string) types.push(TYPES.stringobject);
6060
+
6061
+ wasm.push(
6062
+ ...typeIsNotOneOf([ [ Opcodes.local_get, func.locals[name].idx + 1 ] ], types),
6063
+ [ Opcodes.if, Blocktype.void ],
6064
+ ...internalThrow(func, 'TypeError', `${unhackName(func.name)} expects 'this' to be a ${TYPE_NAMES[typeAnno.type]}`),
6065
+ [ Opcodes.end ]
6066
+ );
6067
+ }
6068
+
6069
+ // todo: if string, try converting to it to one
6070
+ }
6071
+
6072
+ if (def) wasm.push(
6073
+ ...getType(func, name),
6052
6074
  ...number(TYPES.undefined, Valtype.i32),
6053
6075
  [ Opcodes.i32_eq ],
6054
6076
  [ Opcodes.if, Blocktype.void ],
6055
- ...generate(func, defaultValues[x], false, x),
6056
- [ Opcodes.local_set, func.locals[x].idx ],
6077
+ ...generate(func, def, false, name),
6078
+ [ Opcodes.local_set, func.locals[name].idx ],
6057
6079
 
6058
- ...setType(func, x, getNodeType(func, defaultValues[x])),
6080
+ ...setType(func, name, getNodeType(func, def)),
6059
6081
  [ Opcodes.end ]
6060
6082
  );
6061
- }
6062
6083
 
6063
- for (const x in destructuredArgs) {
6064
- prelude.push(
6065
- ...generateVarDstr(func, 'var', destructuredArgs[x], { type: 'Identifier', name: x }, undefined, false)
6084
+ if (destr) wasm.push(
6085
+ ...generateVarDstr(func, 'var', destr, { type: 'Identifier', name }, undefined, false)
6066
6086
  );
6087
+
6088
+ localInd = func.localInd;
6089
+ }
6090
+
6091
+ func.identFailEarly = false;
6092
+
6093
+ if (globalThis.valtypeOverrides) {
6094
+ if (globalThis.valtypeOverrides.returns[name]) func.returns = globalThis.valtypeOverrides.returns[name];
6095
+ if (globalThis.valtypeOverrides.params[name]) {
6096
+ func.params = globalThis.valtypeOverrides.params[name];
6097
+
6098
+ const localsVals = Object.values(func.locals);
6099
+ for (let i = 0; i < func.params.length; i++) {
6100
+ localsVals[i].type = func.params[i];
6101
+ }
6102
+ }
6067
6103
  }
6068
6104
 
6069
- if (decl.async) {
6105
+ if (decl.async && !decl.generator) {
6070
6106
  // make out promise local
6071
6107
  allocVar(func, '#async_out_promise', false, false);
6072
6108
 
@@ -6074,9 +6110,9 @@ const generateFunc = (scope, decl) => {
6074
6110
  typeUsed(func, TYPES.promise);
6075
6111
  }
6076
6112
 
6077
- const wasm = prelude.concat(generate(func, body));
6113
+ wasm = wasm.concat(generate(func, body));
6078
6114
 
6079
- if (decl.async) {
6115
+ if (func.async) {
6080
6116
  // make promise at the start
6081
6117
  wasm.unshift(
6082
6118
  [ Opcodes.call, includeBuiltin(func, '__Porffor_promise_create').index ],
@@ -6177,17 +6213,12 @@ const generateFunc = (scope, decl) => {
6177
6213
  }
6178
6214
  }
6179
6215
 
6180
- if (func.constr) {
6181
- allocVar(func, '#newtarget', false);
6182
- allocVar(func, '#this', false);
6183
- }
6216
+ const args = [];
6217
+ if (func.constr) args.push({ name: '#newtarget' }, { name: '#this' });
6184
6218
 
6185
- const prelude = [];
6186
- const defaultValues = {};
6187
- const destructuredArgs = {};
6188
6219
  let jsLength = 0;
6189
6220
  for (let i = 0; i < params.length; i++) {
6190
- let name;
6221
+ let name, def, destr;
6191
6222
  const x = params[i];
6192
6223
  switch (x.type) {
6193
6224
  case 'Identifier': {
@@ -6197,13 +6228,12 @@ const generateFunc = (scope, decl) => {
6197
6228
  }
6198
6229
 
6199
6230
  case 'AssignmentPattern': {
6231
+ def = x.right;
6200
6232
  if (x.left.name) {
6201
6233
  name = x.left.name;
6202
- defaultValues[name] = x.right;
6203
6234
  } else {
6204
6235
  name = '#arg_dstr' + i;
6205
- destructuredArgs[name] = x.left;
6206
- defaultValues[name] = x.right;
6236
+ destr = x.left;
6207
6237
  }
6208
6238
 
6209
6239
  break;
@@ -6217,61 +6247,24 @@ const generateFunc = (scope, decl) => {
6217
6247
 
6218
6248
  default:
6219
6249
  name = '#arg_dstr' + i;
6220
- destructuredArgs[name] = x;
6250
+ destr = x;
6221
6251
  jsLength++;
6222
6252
  break;
6223
6253
  }
6224
6254
 
6225
- allocVar(func, name, false);
6226
- if (typedInput && params[i].typeAnnotation) {
6227
- const typeAnno = extractTypeAnnotation(params[i]);
6228
- addVarMetadata(func, name, false, typeAnno);
6229
-
6230
- // automatically add throws if unexpected this type to builtins
6231
- if (globalThis.precompile && i === 0 && func.name.includes('_prototype_') && [
6232
- TYPES.date, TYPES.number, TYPES.promise, TYPES.symbol,
6233
- TYPES.set, TYPES.map,
6234
- TYPES.weakref, TYPES.weakset, TYPES.weakmap,
6235
- TYPES.arraybuffer, TYPES.sharedarraybuffer, TYPES.dataview
6236
- ].includes(typeAnno.type)) {
6237
- let types = [ typeAnno.type ];
6238
- if (typeAnno.type === TYPES.number) types.push(TYPES.numberobject);
6239
- if (typeAnno.type === TYPES.string) types.push(TYPES.stringobject);
6240
-
6241
- prelude.push(
6242
- ...typeIsNotOneOf([ [ Opcodes.local_get, func.locals[name].idx + 1 ] ], types),
6243
- [ Opcodes.if, Blocktype.void ],
6244
- ...internalThrow(func, 'TypeError', `${unhackName(func.name)} expects 'this' to be a ${TYPE_NAMES[typeAnno.type]}`),
6245
- [ Opcodes.end ]
6246
- );
6247
- }
6248
-
6249
- // todo: if string, try converting to it to one
6250
- }
6255
+ args.push({ name, def, destr, type: typedInput && params[i].typeAnnotation });
6251
6256
  }
6252
6257
 
6253
- func.params = Object.values(func.locals).map(x => x.type);
6258
+ func.params = new Array((params.length + (func.constr ? 2 : 0)) * 2).fill(0).map((_, i) => i % 2 ? Valtype.i32 : valtypeBinary);
6254
6259
  func.jsLength = jsLength;
6255
6260
 
6256
- if (globalThis.valtypeOverrides) {
6257
- if (globalThis.valtypeOverrides.returns[name]) func.returns = globalThis.valtypeOverrides.returns[name];
6258
- if (globalThis.valtypeOverrides.params[name]) {
6259
- func.params = globalThis.valtypeOverrides.params[name];
6260
-
6261
- const localsVals = Object.values(func.locals);
6262
- for (let i = 0; i < func.params.length; i++) {
6263
- localsVals[i].type = func.params[i];
6264
- }
6265
- }
6266
- }
6267
-
6268
6261
  // force generate for main
6269
6262
  if (name === 'main') func.generate();
6270
6263
 
6271
6264
  // force generate all for precompile
6272
6265
  if (globalThis.precompile) func.generate();
6273
6266
 
6274
- const out = decl.type.endsWith('Expression') ? funcRef(func) : [];
6267
+ const out = decl.type.endsWith('Expression') && !forceNoExpr ? funcRef(func) : [];
6275
6268
  astCache.set(decl, out);
6276
6269
  return [ func, out ];
6277
6270
  };
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.42.2+801066453",
4
+ "version": "0.42.4+82c4bc5cf",
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.42.2+801066453';
3
+ globalThis.version = '0.42.4+82c4bc5cf';
4
4
 
5
5
  // deno compat
6
6
  if (typeof process === 'undefined' && typeof Deno !== 'undefined') {