porffor 0.17.0-55999d22b → 0.17.0-7a608f8cb

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.
package/compiler/types.js CHANGED
@@ -20,10 +20,26 @@ export const TYPE_NAMES = {
20
20
  [TYPES.bigint]: 'BigInt'
21
21
  };
22
22
 
23
+ // flags
24
+ export const TYPE_FLAGS = {
25
+ // iterable: 0b10000000,
26
+ length: 0b01000000,
27
+ };
28
+
29
+ // TYPES.string |= TYPE_FLAGS.iterable;
30
+ TYPES.string |= TYPE_FLAGS.length;
31
+
32
+ export const typeHasFlag = (type, flag) => (type & flag) !== 0;
33
+
23
34
  export const INTERNAL_TYPE_BASE = 0x10;
24
35
  let internalTypeIndex = INTERNAL_TYPE_BASE;
25
- const registerInternalType = name => {
26
- const n = internalTypeIndex++;
36
+ const registerInternalType = (name, flags = []) => {
37
+ let n = internalTypeIndex++;
38
+
39
+ for (const x of flags) {
40
+ if (TYPE_FLAGS[x]) n |= TYPE_FLAGS[x];
41
+ }
42
+
27
43
  TYPES[name.toLowerCase()] = n;
28
44
  TYPE_NAMES[n] = name;
29
45
  };
@@ -31,8 +47,18 @@ const registerInternalType = name => {
31
47
  // note: when adding a new internal type, please also add a deserializer to wrap.js
32
48
  // (it is okay to add a throw todo deserializer for wips)
33
49
 
34
- registerInternalType('Array');
50
+ registerInternalType('Array', ['iterable', 'length']);
35
51
  registerInternalType('RegExp');
36
- registerInternalType('ByteString');
52
+ registerInternalType('ByteString', ['iterable', 'length']);
37
53
  registerInternalType('Date');
38
- registerInternalType('Set');
54
+ registerInternalType('Set', ['iterable']);
55
+
56
+ registerInternalType('Uint8Array', ['iterable', 'length']);
57
+ registerInternalType('Int8Array', ['iterable', 'length']);
58
+ registerInternalType('Uint8ClampedArray', ['iterable', 'length']);
59
+ registerInternalType('Uint16Array', ['iterable', 'length']);
60
+ registerInternalType('Int16Array', ['iterable', 'length']);
61
+ registerInternalType('Uint32Array', ['iterable', 'length']);
62
+ registerInternalType('Int32Array', ['iterable', 'length']);
63
+ registerInternalType('Float32Array', ['iterable', 'length']);
64
+ registerInternalType('Float64Array', ['iterable', 'length']);
@@ -68,6 +68,7 @@ export const Opcodes = {
68
68
 
69
69
  i32_load: 0x28,
70
70
  i64_load: 0x29,
71
+ f32_load: 0x2a,
71
72
  f64_load: 0x2b,
72
73
 
73
74
  i32_load8_s: 0x2c,
@@ -82,6 +83,7 @@ export const Opcodes = {
82
83
 
83
84
  i32_store: 0x36,
84
85
  i64_store: 0x37,
86
+ f32_store: 0x38,
85
87
  f64_store: 0x39,
86
88
 
87
89
  i32_store8: 0x3a,
package/compiler/wrap.js CHANGED
@@ -2,7 +2,7 @@ import { encodeVector, encodeLocal } from './encoding.js';
2
2
  import { importedFuncs } from './builtins.js';
3
3
  import compile from './index.js';
4
4
  import decompile from './decompile.js';
5
- import { TYPES } from './types.js';
5
+ import { TYPES, TYPE_NAMES } from './types.js';
6
6
  import { log } from './log.js';
7
7
  import Prefs from './prefs.js';
8
8
 
@@ -115,6 +115,19 @@ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
115
115
  return Symbol(desc);
116
116
  }
117
117
 
118
+ case TYPES.uint8array:
119
+ case TYPES.int8array:
120
+ case TYPES.uint8clampedarray:
121
+ case TYPES.uint16array:
122
+ case TYPES.int16array:
123
+ case TYPES.uint32array:
124
+ case TYPES.int32array:
125
+ case TYPES.float32array:
126
+ case TYPES.float64array: {
127
+ const length = (new Int32Array(memory.buffer, value, 1))[0];
128
+ return new globalThis[TYPE_NAMES[type]](memory.buffer, value + 4, length);
129
+ }
130
+
118
131
  default: return value;
119
132
  }
120
133
  };
@@ -127,7 +140,7 @@ export default (source, flags = [ 'module' ], customImports = {}, print = str =>
127
140
 
128
141
  globalThis.porfDebugInfo = { funcs, globals };
129
142
 
130
- if (source.includes?.('export ')) flags.push('module');
143
+ // if (process.argv[1].includes('/runner') && source.includes?.('export ')) flags.push('module');
131
144
 
132
145
  // fs.writeFileSync('out.wasm', Buffer.from(wasm));
133
146
 
@@ -297,16 +310,58 @@ export default (source, flags = [ 'module' ], customImports = {}, print = str =>
297
310
  return porfToJSValue({ memory, funcs, pages }, ret[0], ret[1]);
298
311
  } catch (e) {
299
312
  if (e.is && e.is(exceptTag)) {
300
- const exceptId = e.getArg(exceptTag, 0);
301
- const exception = exceptions[exceptId];
313
+ const exceptionMode = Prefs.exceptionMode ?? 'lut';
314
+ if (exceptionMode === 'lut') {
315
+ const exceptId = e.getArg(exceptTag, 0);
316
+ const exception = exceptions[exceptId];
317
+
318
+ const constructorName = exception.constructor;
319
+
320
+ // no constructor, just throw message
321
+ if (!constructorName) throw exception.message;
322
+
323
+ const constructor = globalThis[constructorName] ?? eval(`class ${constructorName} extends Error { constructor(message) { super(message); this.name = "${constructorName}"; } }; ${constructorName}`);
324
+ throw new constructor(exception.message);
325
+ }
326
+
327
+ if (exceptionMode === 'stack') {
328
+ const value = e.getArg(exceptTag, 0);
329
+ const type = e.getArg(exceptTag, 1);
330
+
331
+ throw porfToJSValue({ memory, funcs, pages }, value, type);
332
+ }
333
+
334
+ if (exceptionMode === 'stackest') {
335
+ const constructorIdx = e.getArg(exceptTag, 0);
336
+ const constructorName = constructorIdx == -1 ? null : funcs.find(x => ((x.originalIndex ?? x.index) - importedFuncs.length) === constructorIdx)?.name;
337
+
338
+ const value = e.getArg(exceptTag, 1);
339
+ const type = e.getArg(exceptTag, 2);
340
+ const message = porfToJSValue({ memory, funcs, pages }, value, type);
302
341
 
303
- const constructorName = exception.constructor;
342
+ // no constructor, just throw message
343
+ if (!constructorName) throw message;
344
+
345
+ const constructor = globalThis[constructorName] ?? eval(`class ${constructorName} extends Error { constructor(message) { super(message); this.name = "${constructorName}"; } }; ${constructorName}`);
346
+ throw new constructor(message);
347
+ }
304
348
 
305
- // no constructor, just throw message
306
- if (!constructorName) throw exception.message;
349
+ if (exceptionMode === 'partial') {
350
+ const exceptId = e.getArg(exceptTag, 0);
351
+ const exception = exceptions[exceptId];
307
352
 
308
- const constructor = globalThis[constructorName] ?? eval(`class ${constructorName} extends Error { constructor(message) { super(message); this.name = "${constructorName}"; } }; ${constructorName}`);
309
- throw new constructor(exception.message);
353
+ const constructorName = exception.constructor;
354
+
355
+ const value = e.getArg(exceptTag, 1);
356
+ const type = e.getArg(exceptTag, 2);
357
+ const message = porfToJSValue({ memory, funcs, pages }, value, type);
358
+
359
+ // no constructor, just throw message
360
+ if (!constructorName) throw message;
361
+
362
+ const constructor = globalThis[constructorName] ?? eval(`class ${constructorName} extends Error { constructor(message) { super(message); this.name = "${constructorName}"; } }; ${constructorName}`);
363
+ throw new constructor(message);
364
+ }
310
365
  }
311
366
 
312
367
  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.17.0-55999d22b",
4
+ "version": "0.17.0-7a608f8cb",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {},
package/runner/debug.js CHANGED
@@ -43,7 +43,7 @@ let lastLine;
43
43
  let output = '';
44
44
 
45
45
  try {
46
- const { exports } = await compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {
46
+ const { exports } = compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {
47
47
  y: n => {
48
48
  if (callStarts[callStarts.length - 1] === n - 1) {
49
49
  // end of call
package/runner/index.js CHANGED
@@ -130,14 +130,14 @@ const print = str => {
130
130
  let runStart;
131
131
  try {
132
132
  if (process.argv.includes('-b')) {
133
- const { wasm, exports } = await compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
133
+ const { wasm, exports } = compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
134
134
 
135
135
  runStart = performance.now();
136
136
  if (!process.argv.includes('--no-run')) exports.main();
137
137
 
138
138
  console.log(`\n\nwasm size: ${wasm.byteLength} bytes`);
139
139
  } else {
140
- const { exports } = await compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
140
+ const { exports } = compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
141
141
 
142
142
  runStart = performance.now();
143
143
  if (!process.argv.includes('--no-run')) exports.main();
@@ -145,7 +145,9 @@ try {
145
145
  // if (cache) process.stdout.write(cache);
146
146
  } catch (e) {
147
147
  // if (cache) process.stdout.write(cache);
148
- console.error(process.argv.includes('-i') ? e : `${e.constructor.name}: ${e.message}`);
148
+ let out = e;
149
+ if (!process.argv.includes('-i') && Object.getPrototypeOf(e).message != null) out = `${e.constructor.name}${e.message != null ? `: ${e.message}` : ''}`;
150
+ console.error(out);
149
151
  }
150
152
 
151
153
  if (process.argv.includes('-t')) console.log(`${process.argv.includes('-b') ? '' : '\n\n'}total time: ${(performance.now() - start).toFixed(2)}ms\nexecution time: ${(performance.now() - runStart).toFixed(2)}ms`);
package/runner/profile.js CHANGED
@@ -20,7 +20,7 @@ let spin = 0;
20
20
  let last = 0;
21
21
 
22
22
  try {
23
- const { exports } = await compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {
23
+ const { exports } = compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {
24
24
  y: n => {
25
25
  tmp[n] = performance.now();
26
26
  },
package/runner/repl.js CHANGED
@@ -40,10 +40,9 @@ let lastMemory, lastPages;
40
40
  const PageSize = 65536;
41
41
  const memoryToString = mem => {
42
42
  let out = '';
43
- const pages = lastPages.length;
44
43
  const wasmPages = mem.buffer.byteLength / PageSize;
45
44
 
46
- out += `\x1B[1mallocated ${mem.buffer.byteLength / 1024}KB\x1B[0m for ${pages} thing${pages === 1 ? '' : 's'} using ${wasmPages} Wasm page${wasmPages === 1 ? '' : 's'}\n\n`;
45
+ out += `\x1B[1mallocated ${mem.buffer.byteLength / 1024}KiB\x1B[0m (using ${wasmPages} Wasm page${wasmPages === 1 ? '' : 's'})\n\n`;
47
46
 
48
47
  const buf = new Uint8Array(mem.buffer);
49
48
 
@@ -55,10 +54,16 @@ const memoryToString = mem => {
55
54
  }
56
55
 
57
56
  out += `\x1B[0m\x1B[1m name${' '.repeat(longestName - 4)} \x1B[0m\x1B[90m│\x1B[0m\x1B[1m type${' '.repeat(longestType - 4)} \x1B[0m\x1B[90m│\x1B[0m\x1B[1m memory\x1B[0m\n`; // ─
58
- for (let i = 0; i < pages; i++) {
59
- const [ type, name ] = lastPages[i].split(': ');
60
- // out += `\x1B[36m${lastPages[i].replace(':', '\x1B[90m:\x1B[34m')}\x1B[90m${' '.repeat(longestName - lastPages[i].length)} | \x1B[0m`;
61
- out += ` \x1B[34m${name}${' '.repeat(longestName - name.length)} \x1B[90m│\x1B[0m \x1B[36m${type}${' '.repeat(longestType - type.length)} \x1B[90m│\x1B[0m `;
57
+ for (let i = 0; i < wasmPages; i++) {
58
+ if (lastPages[i]) {
59
+ const [ type, name ] = lastPages[i].split(': ');
60
+ // out += `\x1B[36m${lastPages[i].replace(':', '\x1B[90m:\x1B[34m')}\x1B[90m${' '.repeat(longestName - lastPages[i].length)} | \x1B[0m`;
61
+ out += ` \x1B[34m${name}${' '.repeat(longestName - name.length)} \x1B[90m│\x1B[0m \x1B[36m${type}${' '.repeat(longestType - type.length)} \x1B[90m│\x1B[0m `;
62
+ } else {
63
+ const type = '???';
64
+ const name = '???';
65
+ out += ` \x1B[34m${name}${' '.repeat(longestName - name.length)} \x1B[90m│\x1B[0m \x1B[36m${type}${' '.repeat(longestType - type.length)} \x1B[90m│\x1B[0m `;
66
+ }
62
67
 
63
68
  for (let j = 0; j < 40; j++) {
64
69
  const val = buf[i * pageSize + j];
@@ -75,13 +80,13 @@ const memoryToString = mem => {
75
80
  };
76
81
 
77
82
  let prev = '';
78
- const run = async (source, _context, _filename, callback, run = true) => {
83
+ const run = (source, _context, _filename, callback, run = true) => {
79
84
  // hack: print "secret" before latest code ran to only enable printing for new code
80
85
 
81
86
  let toRun = (prev ? (prev + `;\nprint(-0x1337);\n`) : '') + source.trim();
82
87
 
83
88
  let shouldPrint = !prev;
84
- const { exports, pages } = await compile(toRun, [], {}, str => {
89
+ const { exports, pages } = compile(toRun, [], {}, str => {
85
90
  if (shouldPrint) process.stdout.write(str);
86
91
  if (str === '-4919') shouldPrint = true;
87
92
  });
@@ -122,12 +127,12 @@ replServer.defineCommand('memory', {
122
127
  });
123
128
  replServer.defineCommand('asm', {
124
129
  help: 'Log Wasm decompiled bytecode',
125
- async action() {
130
+ action() {
126
131
  this.clearBufferedCommand();
127
132
 
128
133
  try {
129
134
  process.argv.push('--opt-funcs');
130
- await run('', null, null, () => {}, false);
135
+ run('', null, null, () => {}, false);
131
136
  process.argv.pop();
132
137
  } catch { }
133
138
 
@@ -136,7 +141,7 @@ replServer.defineCommand('asm', {
136
141
  });
137
142
  replServer.defineCommand('js', {
138
143
  help: 'Log JS being actually ran',
139
- async action() {
144
+ action() {
140
145
  this.clearBufferedCommand();
141
146
  console.log(prev);
142
147
  this.displayPrompt();