porffor 0.37.28 → 0.37.30

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.
@@ -32,7 +32,7 @@ const todo = (scope, msg, expectsValue = undefined) => {
32
32
  throw new TodoError(msg);
33
33
 
34
34
  case 'runtime':
35
- return internalThrow(scope, 'TodoError', msg, expectsValue);
35
+ return internalThrow(scope, '__Porffor_TodoError', msg, expectsValue);
36
36
  }
37
37
  };
38
38
 
@@ -1735,7 +1735,7 @@ const createThisArg = (scope, decl) => {
1735
1735
  if (decl._new) {
1736
1736
  // if precompiling or builtin func, just make empty object
1737
1737
  if (globalThis.precompile || Object.hasOwn(builtinFuncs, name)) return [
1738
- ...makeObject(scope, {}),
1738
+ ...number(NULL),
1739
1739
  ...number(TYPES.object, Valtype.i32)
1740
1740
  ];
1741
1741
 
@@ -1793,7 +1793,8 @@ const createThisArg = (scope, decl) => {
1793
1793
  ];
1794
1794
  }
1795
1795
 
1796
- // do not generate globalThis for builtins
1796
+ // do not generate globalThis now,
1797
+ // do it dynamically in generateThis in the func later
1797
1798
  return [
1798
1799
  ...number(NULL),
1799
1800
  ...number(TYPES.object, Valtype.i32)
@@ -4722,6 +4723,8 @@ const generateThrow = (scope, decl) => {
4722
4723
  idx: tags.length
4723
4724
  });
4724
4725
 
4726
+ if (constructor && constructor.startsWith('__')) constructor = constructor.split('_').pop();
4727
+
4725
4728
  let exceptId = exceptions.findIndex(x => x.constructor === constructor && x.message === message);
4726
4729
  if (exceptId === -1) exceptId = exceptions.push({ constructor, message }) - 1;
4727
4730
 
@@ -4747,61 +4750,6 @@ const generateThrow = (scope, decl) => {
4747
4750
  [ Opcodes.throw, tags[0].idx ]
4748
4751
  ];
4749
4752
  }
4750
-
4751
- if (exceptionMode === 'stackest') {
4752
- let message = decl.argument, constructor = null;
4753
-
4754
- // support `throw (new)? Error(...)`
4755
- if (message.type === 'NewExpression' || message.type === 'CallExpression') {
4756
- constructor = decl.argument.callee;
4757
- message = decl.argument.arguments[0];
4758
- }
4759
-
4760
- message ??= DEFAULT_VALUE();
4761
-
4762
- if (tags.length === 0) tags.push({
4763
- params: [ valtypeBinary, valtypeBinary, Valtype.i32 ],
4764
- results: [],
4765
- idx: tags.length
4766
- });
4767
-
4768
- return [
4769
- ...(constructor == null ? number(-1) : generate(scope, constructor)),
4770
- ...generate(scope, message),
4771
- ...getNodeType(scope, message),
4772
- [ Opcodes.throw, tags[0].idx ]
4773
- ];
4774
- }
4775
-
4776
- if (exceptionMode === 'partial') {
4777
- let message = decl.argument, constructor = null;
4778
-
4779
- // support `throw (new)? Error(...)`
4780
- if (message.type === 'NewExpression' || message.type === 'CallExpression') {
4781
- constructor = decl.argument.callee.name;
4782
- message = decl.argument.arguments[0];
4783
- }
4784
-
4785
- message ??= DEFAULT_VALUE();
4786
-
4787
- if (tags.length === 0) tags.push({
4788
- params: [ Valtype.i32, valtypeBinary, Valtype.i32 ],
4789
- results: [],
4790
- idx: tags.length
4791
- });
4792
-
4793
- let exceptId = exceptions.push({ constructor }) - 1;
4794
-
4795
- scope.exceptions ??= [];
4796
- scope.exceptions.push(exceptId);
4797
-
4798
- return [
4799
- ...number(exceptId, Valtype.i32),
4800
- ...generate(scope, message),
4801
- ...getNodeType(scope, message),
4802
- [ Opcodes.throw, tags[0].idx ]
4803
- ];
4804
- }
4805
4753
  };
4806
4754
 
4807
4755
  const generateTry = (scope, decl) => {
@@ -5975,6 +5923,18 @@ const generateFunc = (scope, decl) => {
5975
5923
  generate() {
5976
5924
  if (func.wasm) return func.wasm;
5977
5925
 
5926
+ let errorWasm = null;
5927
+ if (decl.generator) {
5928
+ errorWasm = todo(func, 'generator functions are not supported');
5929
+ }
5930
+
5931
+ if (errorWasm) {
5932
+ return func.wasm = errorWasm.concat([
5933
+ ...number(UNDEFINED),
5934
+ ...number(TYPES.undefined, Valtype.i32)
5935
+ ]);
5936
+ }
5937
+
5978
5938
  // generating, stub _wasm
5979
5939
  func.wasm = [];
5980
5940
 
@@ -6066,7 +6026,7 @@ const generateFunc = (scope, decl) => {
6066
6026
  // inject promise job runner func at the end of main if promises are made
6067
6027
  if (Object.hasOwn(funcIndex, 'Promise') || Object.hasOwn(funcIndex, '__Promise_resolve') || Object.hasOwn(funcIndex, '__Promise_reject')) {
6068
6028
  wasm.push(
6069
- [ Opcodes.call, includeBuiltin(scope, '__Porffor_promise_runJobs').index ],
6029
+ [ Opcodes.call, includeBuiltin(func, '__Porffor_promise_runJobs').index ],
6070
6030
  [ Opcodes.drop ],
6071
6031
  [ Opcodes.drop ]
6072
6032
  );
@@ -6085,17 +6045,6 @@ const generateFunc = (scope, decl) => {
6085
6045
  funcIndex[name] = func.index;
6086
6046
  funcs.push(func);
6087
6047
 
6088
- let errorWasm = null;
6089
- if (decl.generator) errorWasm = todo(scope, 'generator functions are not supported');
6090
-
6091
- if (errorWasm) {
6092
- // func.params = [];
6093
- func.wasm = errorWasm.concat([
6094
- ...number(UNDEFINED),
6095
- ...number(TYPES.undefined, Valtype.i32)
6096
- ]);
6097
- }
6098
-
6099
6048
  if (typedInput && decl.returnType) {
6100
6049
  const { type } = extractTypeAnnotation(decl.returnType);
6101
6050
  if (type != null && !Prefs.indirectCalls) {
package/compiler/wrap.js CHANGED
@@ -524,38 +524,6 @@ export default (source, flags = [ 'module' ], customImports = {}, print = str =>
524
524
 
525
525
  throw porfToJSValue({ memory, funcs, pages }, value, type);
526
526
  }
527
-
528
- if (exceptionMode === 'stackest') {
529
- const constructorIdx = e.getArg(exceptTag, 0);
530
- const constructorName = constructorIdx == -1 ? null : funcs.find(x => (x.index - importedFuncs.length) === constructorIdx)?.name;
531
-
532
- const value = e.getArg(exceptTag, 1);
533
- const type = e.getArg(exceptTag, 2);
534
- const message = porfToJSValue({ memory, funcs, pages }, value, type);
535
-
536
- // no constructor, just throw message
537
- if (!constructorName) throw message;
538
-
539
- const constructor = globalThis[constructorName] ?? eval(`class ${constructorName} extends Error { constructor(message) { super(message); this.name = "${constructorName}"; } }; ${constructorName}`);
540
- throw new constructor(message);
541
- }
542
-
543
- if (exceptionMode === 'partial') {
544
- const exceptId = e.getArg(exceptTag, 0);
545
- const exception = exceptions[exceptId];
546
-
547
- const constructorName = exception.constructor;
548
-
549
- const value = e.getArg(exceptTag, 1);
550
- const type = e.getArg(exceptTag, 2);
551
- const message = porfToJSValue({ memory, funcs, pages }, value, type);
552
-
553
- // no constructor, just throw message
554
- if (!constructorName) throw message;
555
-
556
- const constructor = globalThis[constructorName] ?? eval(`class ${constructorName} extends Error { constructor(message) { super(message); this.name = "${constructorName}"; } }; ${constructorName}`);
557
- throw new constructor(message);
558
- }
559
527
  }
560
528
 
561
529
  if (e instanceof WebAssembly.RuntimeError) {
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.37.28+2a5616118",
4
+ "version": "0.37.30+d663c99a2",
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.37.28+2a5616118';
3
+ globalThis.version = '0.37.30+d663c99a2';
4
4
 
5
5
  // deno compat
6
6
  if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
package/runner/repl.js CHANGED
@@ -88,36 +88,31 @@ const run = (source, _context, _filename, callback, run = true) => {
88
88
  let toRun = (prev ? (prev + `;\nprint(-0x1337);\n`) : '') + source;
89
89
 
90
90
  let shouldPrint = !prev;
91
- let exports, pages;
92
91
  try {
93
- 0, { exports, pages } = compile(toRun, process.argv.includes('--module') ? [ 'module' ] : [], {}, str => {
92
+ const { exports, pages } = compile(toRun, process.argv.includes('--module') ? [ 'module' ] : [], {}, str => {
94
93
  if (shouldPrint) process.stdout.write(str);
95
94
  if (str === '-4919') shouldPrint = true;
96
95
  });
97
- } catch (e) {
98
- console.log(e);
99
- callback();
100
- return;
101
- }
102
96
 
103
- if (run && exports.$) {
104
- lastMemory = exports.$;
105
- lastPages = [...pages.keys()];
106
- }
107
-
108
- let ret = run ? exports.main() : undefined;
109
- let value, type;
110
- if (ret?.type != null) {
111
- value = ret.value;
112
- type = ret.type;
113
- ret = ret.js;
114
- }
97
+ if (run && exports.$) {
98
+ lastMemory = exports.$;
99
+ lastPages = [...pages.keys()];
100
+ }
115
101
 
116
- console.log(util.inspect(ret, false, 2, true), (value != null ? `\x1B[34m\x1B[3m(value: ${value}, type: ${TYPE_NAMES[type]})\x1B[0m` : ''));
102
+ let ret = run ? exports.main() : undefined;
103
+ let value, type;
104
+ if (ret?.type != null) {
105
+ value = ret.value;
106
+ type = ret.type;
107
+ ret = ret.js;
108
+ }
117
109
 
118
- // callback(null, ret);
110
+ console.log(util.inspect(ret, false, 2, true), (value != null ? `\x1B[34m\x1B[3m(value: ${value}, type: ${TYPE_NAMES[type]})\x1B[0m` : ''));
119
111
 
120
- prev = prev + ';\n' + source.trim();
112
+ prev = prev + ';\n' + source.trim();
113
+ } catch (e) {
114
+ console.log('Uncaught', e.stack);
115
+ }
121
116
 
122
117
  callback();
123
118
  };