porffor 0.2.0-536e463 → 0.2.0-5ac7ea0

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/wrap.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import compile from './index.js';
2
2
  import decompile from './decompile.js';
3
- // import fs from 'node:fs';
3
+ import { encodeVector, encodeLocal } from './encoding.js';
4
+ import Prefs from './prefs.js';
5
+ import { log } from './log.js';
4
6
 
5
7
  const bold = x => `\u001b[1m${x}\u001b[0m`;
6
8
 
@@ -18,7 +20,8 @@ const TYPES = {
18
20
 
19
21
  // internal
20
22
  [internalTypeBase]: '_array',
21
- [internalTypeBase + 1]: '_regexp'
23
+ [internalTypeBase + 1]: '_regexp',
24
+ [internalTypeBase + 2]: '_bytestring'
22
25
  };
23
26
 
24
27
  export default async (source, flags = [ 'module' ], customImports = {}, print = str => process.stdout.write(str)) => {
@@ -29,20 +32,110 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
29
32
 
30
33
  if (source.includes('export function')) flags.push('module');
31
34
 
32
- // fs.writeFileSync('out.wasm', Buffer.from(wasm));
35
+ // (await import('node:fs')).writeFileSync('out.wasm', Buffer.from(wasm));
33
36
 
34
37
  times.push(performance.now() - t1);
35
38
  if (flags.includes('info')) console.log(bold(`compiled in ${times[0].toFixed(2)}ms`));
36
39
 
37
40
  const t2 = performance.now();
38
- const { instance } = await WebAssembly.instantiate(wasm, {
39
- '': {
40
- p: valtype === 'i64' ? i => print(Number(i).toString()) : i => print(i.toString()),
41
- c: valtype === 'i64' ? i => print(String.fromCharCode(Number(i))) : i => print(String.fromCharCode(i)),
42
- t: _ => performance.now(),
43
- ...customImports
41
+
42
+ let instance;
43
+ try {
44
+ let wasmEngine = WebAssembly;
45
+ if (Prefs.asur) {
46
+ log.warning('wrap', 'using our !experimental! asur wasm engine instead of host to run');
47
+ wasmEngine = await import('../asur/index.js');
48
+ }
49
+
50
+ 0, { instance } = await wasmEngine.instantiate(wasm, {
51
+ '': {
52
+ p: valtype === 'i64' ? i => print(Number(i).toString()) : i => print(i.toString()),
53
+ c: valtype === 'i64' ? i => print(String.fromCharCode(Number(i))) : i => print(String.fromCharCode(i)),
54
+ t: _ => performance.now(),
55
+ ...customImports
56
+ }
57
+ });
58
+ } catch (e) {
59
+ // only backtrace for runner, not test262/etc
60
+ if (!process.argv[1].includes('/runner')) throw e;
61
+
62
+ const funcInd = parseInt(e.message.match(/function #([0-9]+) /)[1]);
63
+ const blobOffset = parseInt(e.message.split('@')[1]);
64
+
65
+ // convert blob offset -> function wasm offset.
66
+ // this is not good code and is somewhat duplicated
67
+ // I just want it to work for debugging, I don't care about perf/yes
68
+
69
+ const func = funcs.find(x => x.index === funcInd);
70
+ const locals = Object.values(func.locals).sort((a, b) => a.idx - b.idx).slice(func.params.length).sort((a, b) => a.idx - b.idx);
71
+
72
+ let localDecl = [], typeCount = 0, lastType;
73
+ for (let i = 0; i < locals.length; i++) {
74
+ const local = locals[i];
75
+ if (i !== 0 && local.type !== lastType) {
76
+ localDecl.push(encodeLocal(typeCount, lastType));
77
+ typeCount = 0;
78
+ }
79
+
80
+ typeCount++;
81
+ lastType = local.type;
82
+ }
83
+
84
+ if (typeCount !== 0) localDecl.push(encodeLocal(typeCount, lastType));
85
+
86
+ const toFind = encodeVector(localDecl).concat(func.wasm.flat().filter(x => x != null && x <= 0xff).slice(0, 40));
87
+
88
+ let i = 0;
89
+ for (; i < wasm.length; i++) {
90
+ let mismatch = false;
91
+ for (let j = 0; j < toFind.length; j++) {
92
+ if (wasm[i + j] !== toFind[j]) {
93
+ mismatch = true;
94
+ break;
95
+ }
96
+ }
97
+
98
+ if (!mismatch) break;
99
+ }
100
+
101
+ if (i === wasm.length) throw e;
102
+
103
+ const offset = (blobOffset - i) + encodeVector(localDecl).length;
104
+
105
+ let cumLen = 0;
106
+ i = 0;
107
+ for (; i < func.wasm.length; i++) {
108
+ cumLen += func.wasm[i].filter(x => x != null && x <= 0xff).length;
109
+ if (cumLen === offset) break;
44
110
  }
45
- });
111
+
112
+ if (cumLen !== offset) throw e;
113
+
114
+ i -= 1;
115
+
116
+ console.log(`\x1B[35m\x1B[1mporffor backtrace\u001b[0m`);
117
+
118
+ console.log('\x1B[4m' + func.name + '\x1B[0m');
119
+
120
+ const surrounding = 6;
121
+
122
+ const decomp = decompile(func.wasm.slice(i - surrounding, i + surrounding + 1), '', 0, func.locals, func.params, func.returns, funcs, globals, exceptions).slice(0, -1).split('\n');
123
+
124
+ const noAnsi = s => s.replace(/\u001b\[[0-9]+m/g, '');
125
+ let longest = 0;
126
+ for (let j = 0; j < decomp.length; j++) {
127
+ longest = Math.max(longest, noAnsi(decomp[j]).length);
128
+ }
129
+
130
+ const middle = Math.floor(decomp.length / 2);
131
+ decomp[middle] = `\x1B[47m\x1B[30m${noAnsi(decomp[middle])}${'\u00a0'.repeat(longest - noAnsi(decomp[middle]).length)}\x1B[0m`;
132
+
133
+ console.log('\x1B[90m...\x1B[0m');
134
+ console.log(decomp.join('\n'));
135
+ console.log('\x1B[90m...\x1B[0m\n');
136
+
137
+ throw e;
138
+ }
46
139
 
47
140
  times.push(performance.now() - t2);
48
141
  if (flags.includes('info')) console.log(`instantiated in ${times[1].toFixed(2)}ms`);
@@ -95,10 +188,18 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
95
188
  return Array.from(new Uint16Array(memory.buffer, pointer + 4, length)).map(x => String.fromCharCode(x)).join('');
96
189
  }
97
190
 
191
+ case '_bytestring': {
192
+ const pointer = ret;
193
+ const length = new Int32Array(memory.buffer, pointer, 1);
194
+
195
+ return Array.from(new Uint8Array(memory.buffer, pointer + 4, length)).map(x => String.fromCharCode(x)).join('');
196
+ }
197
+
98
198
  case 'function': {
99
199
  // wasm func index, including all imports
100
200
  const func = funcs.find(x => (x.originalIndex ?? x.index) === ret);
101
- if (!func) return ret;
201
+ // if (!func) return ret;
202
+ if (!func) return function () {};
102
203
 
103
204
  // make fake empty func for repl/etc
104
205
  return {[func.name]() {}}[func.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.2.0-536e463",
4
+ "version": "0.2.0-5ac7ea0",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "dependencies": {
package/porf ADDED
@@ -0,0 +1,2 @@
1
+ #!/bin/sh
2
+ node runner/index.js "$@"
package/rhemyn/compile.js CHANGED
@@ -2,6 +2,7 @@ import { Blocktype, Opcodes, Valtype, PageSize, ValtypeSize } from '../compiler/
2
2
  import { number } from '../compiler/embedding.js';
3
3
  import { signedLEB128, unsignedLEB128 } from '../compiler/encoding.js';
4
4
  import parse from './parse.js';
5
+ import Prefs from '../compiler/prefs.js';
5
6
 
6
7
  // local indexes
7
8
  const BasePointer = 0; // base string pointer
@@ -80,7 +81,7 @@ const generate = (node, negated = false, get = true, func = 'test') => {
80
81
  })[func], Valtype.i32)
81
82
  ];
82
83
 
83
- if (globalThis.regexLog) {
84
+ if (Prefs.regexLog) {
84
85
  const underline = x => `\u001b[4m\u001b[1m${x}\u001b[0m`;
85
86
  console.log(`\n${underline('ast')}`);
86
87
  console.log(node);
package/runner/index.js CHANGED
@@ -3,6 +3,8 @@
3
3
  import compile from '../compiler/wrap.js';
4
4
  import fs from 'node:fs';
5
5
 
6
+ const start = performance.now();
7
+
6
8
  if (process.argv.includes('-compile-hints')) {
7
9
  const v8 = await import('node:v8');
8
10
  v8.setFlagsFromString(`--experimental-wasm-compilation-hints`);
@@ -15,9 +17,28 @@ if (process.argv.includes('-compile-hints')) {
15
17
  // --experimental-wasm-return-call (on by default)
16
18
  }
17
19
 
18
- const file = process.argv.slice(2).find(x => x[0] !== '-');
20
+ let file = process.argv.slice(2).find(x => x[0] !== '-');
21
+ if (['run', 'wasm', 'native', 'c', 'profile'].includes(file)) {
22
+ if (file === 'profile') {
23
+ process.argv.splice(process.argv.indexOf(file), 1);
24
+ await import('./profiler.js');
25
+ await new Promise(() => {});
26
+ }
27
+
28
+ if (['wasm', 'native', 'c'].includes(file)) {
29
+ process.argv.push(`-target=${file}`);
30
+ }
31
+
32
+ file = process.argv.slice(process.argv.indexOf(file) + 1).find(x => x[0] !== '-');
33
+
34
+ const nonOptOutFile = process.argv.slice(process.argv.indexOf(file) + 1).find(x => x[0] !== '-');
35
+ if (nonOptOutFile) {
36
+ process.argv.push(`-o=${nonOptOutFile}`);
37
+ }
38
+ }
39
+
19
40
  if (!file) {
20
- if (process.argv.includes('-v')) {
41
+ if (process.argv.includes('-v') || process.argv.includes('--version')) {
21
42
  // just print version
22
43
  console.log((await import('./version.js')).default);
23
44
  process.exit(0);
@@ -52,4 +73,6 @@ try {
52
73
  } catch (e) {
53
74
  if (cache) process.stdout.write(cache);
54
75
  console.error(process.argv.includes('-i') ? e : `${e.constructor.name}: ${e.message}`);
55
- }
76
+ }
77
+
78
+ if (process.argv.includes('-t')) console.log(`\n\ntotal time: ${(performance.now() - start).toFixed(2)}ms`);
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env node
2
+
3
+ import compile from '../compiler/wrap.js';
4
+ import fs from 'node:fs';
5
+
6
+ import Prefs from '../compiler/prefs.js';
7
+
8
+ const fast = Prefs.profiler === 'fast';
9
+
10
+ const file = process.argv.slice(2).find(x => x[0] !== '-');
11
+ let source = fs.readFileSync(file, 'utf8');
12
+
13
+ let profileId = 0;
14
+ source = fast ? source.replace(/^[^\n}]*;$/mg, _ => `profile(${profileId++});${_}profile(${profileId++});`) : source.replace(/^[^\n}]*;$/mg, _ => `profile(${profileId++});profile(${profileId++});${_}profile(${profileId++});`);
15
+
16
+ // console.log(source);
17
+
18
+ let tmp = new Array(profileId).fill(0);
19
+ let samples = 0;
20
+
21
+ const percents = process.argv.includes('-%');
22
+
23
+ const spinner = ['-', '\\', '|', '/'];
24
+ let spin = 0;
25
+ let last = 0;
26
+
27
+ try {
28
+ const { exports } = await compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {
29
+ z: fast ? n => {
30
+ if (n % 2) {
31
+ tmp[n] += performance.now() - tmp[n - 1];
32
+ } else {
33
+ tmp[n] = performance.now();
34
+ }
35
+ } : n => {
36
+ if (n % 3 === 2) {
37
+ tmp[n] += (performance.now() - tmp[n - 1]) - (tmp[n - 1] - tmp[n - 2]);
38
+ samples++;
39
+
40
+ if (performance.now() > last) {
41
+ process.stdout.write(`\r${spinner[spin++ % 4]} running: collected ${samples} samples...`);
42
+ last = performance.now() + 100;
43
+ }
44
+ } else {
45
+ tmp[n] = performance.now();
46
+ }
47
+ }
48
+ });
49
+
50
+ const start = performance.now();
51
+
52
+ exports.main();
53
+
54
+ const total = performance.now() - start;
55
+
56
+ console.log(`\nsamples: ${fast ? 'not measured' : samples}\ntotal: ${total}ms\n\n` + source.split('\n').map(x => {
57
+ let time = 0;
58
+ if (x.startsWith('profile')) {
59
+ const id = parseInt(x.slice(8, x.indexOf(')')));
60
+ time = fast ? tmp[id + 1] : tmp[id + 2];
61
+ }
62
+
63
+ let color = [ 0, 0, 0 ];
64
+ if (time) {
65
+ const relativeTime = Math.sqrt(time / total);
66
+ if (percents) time = relativeTime;
67
+
68
+ color = [ (relativeTime * 250) | 0, (Math.sin(relativeTime * Math.PI) * 50) | 0, 0 ];
69
+ }
70
+
71
+ const ansiColor = `2;${color[0]};${color[1]};${color[2]}m`;
72
+
73
+ if (percents) return `\x1b[48;${ansiColor}\x1b[97m${time ? ((time * 100).toFixed(0).padStart(4, ' ') + '%') : ' '}\x1b[0m\x1b[38;${ansiColor}▌\x1b[0m ${x.replace(/profile\([0-9]+\);/g, '')}`;
74
+
75
+ let digits = 2;
76
+ if (time >= 100) digits = 1;
77
+ if (time >= 1000) digits = 0;
78
+
79
+ return `\x1b[48;${ansiColor}\x1b[97m${time ? time.toFixed(digits).padStart(6, ' ') : ' '}\x1b[0m\x1b[38;${ansiColor}▌\x1b[0m ${x.replace(/profile\([0-9]+\);/g, '')}`;
80
+ }).join('\n'));
81
+ } catch (e) {
82
+ console.error(e);
83
+ }
package/runner/repl.js CHANGED
@@ -45,9 +45,9 @@ let prev = '';
45
45
  const run = async (source, _context, _filename, callback, run = true) => {
46
46
  // hack: print "secret" before latest code ran to only enable printing for new code
47
47
 
48
- let toRun = prev + `;\nprint(-0x1337);\n` + source.trim();
48
+ let toRun = (prev ? (prev + `;\nprint(-0x1337);\n`) : '') + source.trim();
49
49
 
50
- let shouldPrint = false;
50
+ let shouldPrint = !prev;
51
51
  const { exports, wasm, pages } = await compile(toRun, [], {}, str => {
52
52
  if (shouldPrint) process.stdout.write(str);
53
53
  if (str === '-4919') shouldPrint = true;
@@ -1,92 +0,0 @@
1
- var btoa_a = str => {
2
- // todo: throw invalid character for unicode
3
-
4
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
5
- const mask = (1 << 6) - 1;
6
-
7
- let out = '';
8
- let bits = 0, buffer = 0;
9
- for (let i = 0; i < str.length; i++) {
10
- buffer = (buffer << 8) | (0xff & str.charCodeAt(i));
11
- bits += 8;
12
-
13
- while (bits > 6) {
14
- bits -= 6;
15
- out += chars[mask & (buffer >> bits)];
16
- }
17
- }
18
-
19
- if (bits) {
20
- out += chars[mask & (buffer << (6 - bits))]
21
- }
22
-
23
- while ((out.length * 6) & 7) {
24
- out += '=';
25
- }
26
-
27
- return out;
28
- };
29
-
30
- var btoa = function (input) {
31
- // todo: throw invalid character for unicode
32
- const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
33
-
34
- let output = "";
35
- let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
36
- let i = 0;
37
-
38
- while (i < input.length) {
39
- chr1 = input.charCodeAt(i++);
40
- chr2 = input.charCodeAt(i++);
41
- chr3 = input.charCodeAt(i++);
42
-
43
- enc1 = chr1 >> 2;
44
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
45
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
46
- enc4 = chr3 & 63;
47
-
48
- if (isNaN(chr2)) {
49
- enc3 = enc4 = 64;
50
- } else if (isNaN(chr3)) {
51
- enc4 = 64;
52
- }
53
-
54
- output += keyStr.charAt(enc1);
55
- output += keyStr.charAt(enc2);
56
- output += keyStr.charAt(enc3);
57
- output += keyStr.charAt(enc4);
58
- }
59
-
60
- return output;
61
- };
62
-
63
- var atob_b = function (input) {
64
- const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
65
-
66
- let output = "";
67
- let chr1, chr2, chr3;
68
- let enc1, enc2, enc3, enc4;
69
- let i = 0;
70
-
71
- while (i < input.length) {
72
- enc1 = keyStr.indexOf(input.charAt(i++));
73
- enc2 = keyStr.indexOf(input.charAt(i++));
74
- enc3 = keyStr.indexOf(input.charAt(i++));
75
- enc4 = keyStr.indexOf(input.charAt(i++));
76
-
77
- chr1 = (enc1 << 2) | (enc2 >> 4);
78
- chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
79
- chr3 = ((enc3 & 3) << 6) | enc4;
80
-
81
- output += String.fromCharCode(chr1);
82
-
83
- if (enc3 != 64) {
84
- output += String.fromCharCode(chr2);
85
- }
86
- if (enc4 != 64) {
87
- output += String.fromCharCode(chr3);
88
- }
89
- }
90
-
91
- return output;
92
- };
package/runner/profile.js DELETED
@@ -1,46 +0,0 @@
1
- import compile from '../compiler/index.js';
2
- import fs from 'node:fs';
3
-
4
- let csv = `phase,time\n`;
5
-
6
- csv += `node,${performance.now()}\n`;
7
-
8
- const t0 = performance.now();
9
- const file = process.argv.slice(2).find(x => x[0] !== '-');
10
- const source = fs.readFileSync(file, 'utf8');
11
- csv += `read,${performance.now() - t0}\n`;
12
-
13
- console.log = x => {
14
- if (x.includes(' in ')) {
15
- csv += [ 'parse', 'codegen', 'opt', 'sections' ][parseInt(x[0]) - 1] + ',' + x.split(' in ')[1].slice(0, -2) + '\n';
16
- }
17
- };
18
-
19
- const wasm = compile(source, [ 'info' ]);
20
-
21
- let cache = '';
22
- const print = str => {
23
- cache += str;
24
-
25
- if (str === '\n') {
26
- process.stdout.write(cache);
27
- cache = '';
28
- }
29
- };
30
-
31
- const t1 = performance.now();
32
- const { instance } = await WebAssembly.instantiate(wasm, {
33
- '': {
34
- p: i => print(Number(i).toString()),
35
- c: i => print(String.fromCharCode(Number(i)))
36
- }
37
- });
38
- csv += `inst,${performance.now() - t1}\n`;
39
-
40
- const t2 = performance.now();
41
- instance.exports.m();
42
- print('\n');
43
-
44
- csv += `exec,${performance.now() - t2}`;
45
-
46
- fs.writeFileSync(`profile.csv`, csv);