porffor 0.49.6 → 0.49.8
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/README.md +3 -3
- package/compiler/2c.js +7 -4
- package/compiler/builtins/console.ts +8 -8
- package/compiler/builtins/date.ts +1 -1
- package/compiler/builtins/math.ts +7 -2
- package/compiler/builtins/string.ts +8 -8
- package/compiler/builtins_precompiled.js +58 -58
- package/compiler/codegen.js +168 -60
- package/compiler/cyclone.js +6 -2
- package/compiler/index.js +2 -0
- package/compiler/parse.js +1 -1
- package/compiler/precompile.js +4 -4
- package/package.json +1 -1
- package/runner/debug.js +0 -1
- package/runner/flamegraph.js +205 -0
- package/runner/{profile.js → hotlines.js} +0 -1
- package/runner/index.js +34 -34
- package/richards.js +0 -913
@@ -0,0 +1,205 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
import { Opcodes, Valtype } from '../compiler/wasmSpec.js';
|
3
|
+
import { number } from '../compiler/embedding.js';
|
4
|
+
import { importedFuncs } from '../compiler/builtins.js';
|
5
|
+
import compile from '../compiler/wrap.js';
|
6
|
+
import fs from 'node:fs';
|
7
|
+
|
8
|
+
const file = process.argv.slice(2).find(x => x[0] !== '-');
|
9
|
+
let source = fs.readFileSync(file, 'utf8');
|
10
|
+
|
11
|
+
const samplesFunc = [];
|
12
|
+
const samplesStart = [];
|
13
|
+
const samplesEnd = [];
|
14
|
+
|
15
|
+
const termWidth = process.stdout.columns || 80;
|
16
|
+
const termHeight = process.stdout.rows || 24;
|
17
|
+
|
18
|
+
let start, end;
|
19
|
+
const noAnsi = s => s.replace(/\u001b\[[0-9]+m/g, '');
|
20
|
+
const controls = {
|
21
|
+
};
|
22
|
+
|
23
|
+
const controlInfo = Object.keys(controls).reduce((acc, x, i) => acc + `\x1B[45m\x1B[97m${x}\x1b[105m\x1b[37m ${controls[x]} `, '');
|
24
|
+
const plainControlInfo = noAnsi(controlInfo);
|
25
|
+
|
26
|
+
const spinner = ['-', '\\', '|', '/'];
|
27
|
+
let spin = 0;
|
28
|
+
|
29
|
+
const onExit = () => {
|
30
|
+
process.stdout.write('\x1b[1;1H\x1b[J');
|
31
|
+
};
|
32
|
+
// process.on('exit', onExit);
|
33
|
+
// process.on('SIGINT', onExit);
|
34
|
+
|
35
|
+
let lastRenderTime = 0;
|
36
|
+
const render = () => {
|
37
|
+
const renderStart = performance.now();
|
38
|
+
process.stdout.write('\x1b[?2026h\x1b[1;1H\x1b[J\x1b[?25l');
|
39
|
+
|
40
|
+
let text = ' ';
|
41
|
+
if (!end) {
|
42
|
+
text += `${spinner[spin++ % 4]} `;
|
43
|
+
} else {
|
44
|
+
text += ' ';
|
45
|
+
}
|
46
|
+
|
47
|
+
const now = performance.now();
|
48
|
+
const realEnd = end ?? now;
|
49
|
+
const total = realEnd - start;
|
50
|
+
const _total = total;
|
51
|
+
text += `${total.toFixed(0)}ms`;
|
52
|
+
|
53
|
+
const samples = samplesFunc.length;
|
54
|
+
text += `${' '.repeat(12 - text.length)}┃ samples: ${samples}`;
|
55
|
+
text += `${' '.repeat(32 - text.length)}┃ render: ${lastRenderTime.toFixed(2)}ms`;
|
56
|
+
|
57
|
+
if (end != null && Prefs.live) {
|
58
|
+
const btHeight = 20;
|
59
|
+
const fgBottom = termHeight - btHeight - 10;
|
60
|
+
|
61
|
+
let lastEnds = [];
|
62
|
+
let timelineEnd = total;
|
63
|
+
|
64
|
+
const xScale = x => 1 + (((x - start) / timelineEnd) * (termWidth - 2));
|
65
|
+
const draw = (func, start, end, running = false) => {
|
66
|
+
let depth = lastEnds.length;
|
67
|
+
lastEnds.push(end);
|
68
|
+
|
69
|
+
let color = '103';
|
70
|
+
if (func.internal) color = '105';
|
71
|
+
|
72
|
+
start = xScale(start) | 0;
|
73
|
+
|
74
|
+
end = xScale(end) | 0;
|
75
|
+
if (end >= termWidth) end = termWidth - 1;
|
76
|
+
|
77
|
+
const width = end - start;
|
78
|
+
if (start >= termWidth || width === 0) return;
|
79
|
+
|
80
|
+
let text = func.name;
|
81
|
+
if (text.length > width) text = width < 5 ? ' '.repeat(width) : (text.slice(0, width - 1) + '…');
|
82
|
+
if (text.length < width) text += ' '.repeat(width - text.length);
|
83
|
+
|
84
|
+
let y = fgBottom - depth;
|
85
|
+
process.stdout.write(`\x1b[${1 + y};${1 + start}H\x1b[51m\x1b[30m\x1b[${color}m${text}`);
|
86
|
+
};
|
87
|
+
|
88
|
+
const funcTotalTaken = new Map(), funcMeta = new Map();
|
89
|
+
for (let i = 0; i < samples; i++) {
|
90
|
+
const func = funcLookup.get(samplesFunc[i]);
|
91
|
+
const start = samplesStart[i];
|
92
|
+
const end = samplesEnd[i] ?? now;
|
93
|
+
|
94
|
+
// DD
|
95
|
+
// BBCCEE
|
96
|
+
// AAAAAAAA FFFF
|
97
|
+
while (start > lastEnds.at(-1)) lastEnds.pop();
|
98
|
+
|
99
|
+
draw(func, start, end);
|
100
|
+
|
101
|
+
if (end == now) continue;
|
102
|
+
const taken = end - start;
|
103
|
+
funcTotalTaken.set(func.index, (funcTotalTaken.get(func.index) ?? 0) + taken);
|
104
|
+
|
105
|
+
if (!funcMeta.has(func.index)) funcMeta.set(func.index, [0, Infinity, -Infinity]);
|
106
|
+
const meta = funcMeta.get(func.index);
|
107
|
+
meta[0]++;
|
108
|
+
|
109
|
+
if (meta[1] > taken) meta[1] = taken;
|
110
|
+
if (taken > meta[2]) meta[2] = taken;
|
111
|
+
}
|
112
|
+
|
113
|
+
process.stdout.write(`\x1b[${termHeight - btHeight};1H\x1b[0m\x1b[90m${'▁'.repeat(termWidth)}\n`);
|
114
|
+
|
115
|
+
(() => {
|
116
|
+
const perTime = 18;
|
117
|
+
let text = ' ' + 'name';
|
118
|
+
text += `${' '.repeat(40 - text.length)}┃ total`;
|
119
|
+
text += `${' '.repeat(40 + 5 + perTime - text.length)}┃ min`;
|
120
|
+
text += `${' '.repeat(40 + 5 + (perTime * 2) - text.length)}┃ avg`;
|
121
|
+
text += `${' '.repeat(40 + 5 + (perTime * 3) - text.length)}┃ max`;
|
122
|
+
text += `${' '.repeat(40 + 5 + (perTime * 4) - text.length)}┃ count`;
|
123
|
+
process.stdout.write(`\x1b[0m\x1b[2m${text.replaceAll('┃', '\x1b[0m\x1b[90m┃\x1b[0m\x1b[2m')}${' '.repeat(termWidth - text.length)}\x1b[0m`);
|
124
|
+
})();
|
125
|
+
|
126
|
+
const topTakenFuncs = [...funcTotalTaken.keys()].sort((a, b) => funcTotalTaken.get(b) - funcTotalTaken.get(a));
|
127
|
+
for (let i = 0; i < btHeight - 2; i++) {
|
128
|
+
const func = funcLookup.get(topTakenFuncs[i]);
|
129
|
+
if (!func) continue;
|
130
|
+
|
131
|
+
const total = funcTotalTaken.get(func.index);
|
132
|
+
const [ count, min, max ] = funcMeta.get(func.index);
|
133
|
+
const avg = total / count;
|
134
|
+
|
135
|
+
const perTime = 18;
|
136
|
+
let text = ' \x1b[1m' + func.name + '\x1b[22m';
|
137
|
+
text += `${' '.repeat(49 - text.length)}┃ ${total.toFixed(2)}ms`;
|
138
|
+
text += `${' '.repeat(49 + perTime - text.length)}${((total / _total) * 100).toFixed(0)}%`;
|
139
|
+
text += `${' '.repeat(49 + 5 + perTime - text.length)}┃ ${min.toFixed(2)}ms`;
|
140
|
+
text += `${' '.repeat(49 + 5 + (perTime * 2) - text.length)}┃ ${avg.toFixed(2)}ms`;
|
141
|
+
text += `${' '.repeat(49 + 5 + (perTime * 3) - text.length)}┃ ${max.toFixed(2)}ms`;
|
142
|
+
text += `${' '.repeat(49 + 5 + (perTime * 4) - text.length)}┃ ${count}`;
|
143
|
+
process.stdout.write(`\x1b[${termHeight - btHeight + 2 + i};1H\x1b[0m${text.replaceAll('┃', '\x1b[90m┃\x1b[0m').replaceAll('ms', '\x1b[2mms\x1b[22m').replaceAll('%', '\x1b[2m%\x1b[22m')}${' '.repeat(termWidth - noAnsi(text).length)}`);
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
process.stdout.write(`\x1b[${termHeight};1H\x1b[107m\x1b[30m${text}${' '.repeat(termWidth - plainControlInfo.length - noAnsi(text).length - 1)}${controlInfo} \x1b[0m\x1b[?2026l`);
|
148
|
+
lastRenderTime = performance.now() - renderStart;
|
149
|
+
};
|
150
|
+
|
151
|
+
// importedFuncs[importedFuncs.profile2].params = [ Valtype.i32, Valtype.f64 ];
|
152
|
+
|
153
|
+
Prefs.treeshakeWasmImports = false;
|
154
|
+
let funcLookup = new Map();
|
155
|
+
globalThis.compileCallback = ({ funcs }) => {
|
156
|
+
for (const x of funcs) {
|
157
|
+
funcLookup.set(x.index, x);
|
158
|
+
|
159
|
+
const w = x.wasm;
|
160
|
+
for (let i = 0; i < w.length; i++) {
|
161
|
+
if (w[i][0] === Opcodes.call) {
|
162
|
+
const f = w[i][1];
|
163
|
+
if (f < importedFuncs.length) continue;
|
164
|
+
|
165
|
+
let local;
|
166
|
+
if (x.locals['#profile_tmp']) {
|
167
|
+
local = x.locals['#profile_tmp'].idx;
|
168
|
+
} else {
|
169
|
+
local = x.localInd++;
|
170
|
+
x.locals['#profile_tmp'] = { idx: local, type: Valtype.f64 };
|
171
|
+
}
|
172
|
+
|
173
|
+
w.splice(i + 1, 0, number(f, Valtype.i32)[0], [ Opcodes.call, importedFuncs.profile2 ]);
|
174
|
+
w.splice(i, 0, number(f, Valtype.i32)[0], [ Opcodes.call, importedFuncs.profile1 ]);
|
175
|
+
i += 4;
|
176
|
+
}
|
177
|
+
}
|
178
|
+
}
|
179
|
+
};
|
180
|
+
|
181
|
+
let last = 0;
|
182
|
+
let running = new Uint32Array(1024), runningIdx = 0;
|
183
|
+
const { exports } = compile(source, undefined, {
|
184
|
+
y: f => { // pre-call
|
185
|
+
samplesStart.push(performance.now());
|
186
|
+
running[runningIdx++] = samplesFunc.push(f) - 1;
|
187
|
+
},
|
188
|
+
z: f => { // post-call
|
189
|
+
const now = performance.now();
|
190
|
+
samplesEnd[running[--runningIdx]] = now;
|
191
|
+
|
192
|
+
if (now > last) {
|
193
|
+
last = now + 500;
|
194
|
+
render();
|
195
|
+
}
|
196
|
+
}
|
197
|
+
}, () => {});
|
198
|
+
|
199
|
+
start = performance.now();
|
200
|
+
render();
|
201
|
+
|
202
|
+
exports.main();
|
203
|
+
end = performance.now();
|
204
|
+
|
205
|
+
render();
|
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.49.
|
3
|
+
globalThis.version = '0.49.8';
|
4
4
|
|
5
5
|
// deno compat
|
6
6
|
if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
|
@@ -11,43 +11,38 @@ const start = performance.now();
|
|
11
11
|
|
12
12
|
if (process.argv.includes('--help')) {
|
13
13
|
// description + version
|
14
|
-
console.log(`\x1B[1m\x1B[35mPorffor\x1B[0m is a JavaScript engine/runtime
|
14
|
+
console.log(`\x1B[1m\x1B[35mPorffor\x1B[0m is a JavaScript/TypeScript engine/compiler/runtime. \x1B[90m(${globalThis.version})\x1B[0m`);
|
15
15
|
|
16
16
|
// basic usage
|
17
|
-
console.log(`Usage: \x1B[1mporf
|
17
|
+
console.log(`Usage: \x1B[1mporf <command> [...prefs] path/to/script.js [...args]\x1B[0m`);
|
18
18
|
|
19
19
|
// commands
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
wasm: [ 34, '
|
20
|
+
for (let [ cmd, [ color, post, desc ] ] of Object.entries({
|
21
|
+
'Compile': [],
|
22
|
+
'': [ 34, 'foo.js', 'Compile and execute a file' ],
|
23
|
+
wasm: [ 34, 'foo.js foo.wasm', 'Compile to a Wasm binary' ],
|
24
|
+
c: [ 94, 'foo.js foo.c', 'Compile to C source code' ],
|
25
|
+
native: [ 94, 'foo.js foo', 'Compile to a native binary' ],
|
26
|
+
|
27
|
+
'Profile': [],
|
28
|
+
flamegraph: [ 93, 'foo.js', 'View detailed func-by-func performance' ],
|
29
|
+
hotlines: [ 93, 'foo.js', 'View source with line-by-line performance' ],
|
30
|
+
|
31
|
+
'Debug': [],
|
32
|
+
debug: [ 33, 'foo.js', 'Debug the source of a file' ],
|
33
|
+
dissect: [ 33, 'foo.js', 'Debug the compiled Wasm of a file' ],
|
34
|
+
})) {
|
35
|
+
if (color == null) {
|
36
|
+
// header
|
37
|
+
console.log(`\n\x1B[1m\x1B[4m${cmd}\x1B[0m`);
|
38
|
+
continue;
|
39
|
+
}
|
24
40
|
|
25
|
-
|
26
|
-
native: [ 31, 'Compile a JS file to a native binary\n' ],
|
41
|
+
if (cmd.length > 0) post = ' ' + post;
|
27
42
|
|
28
|
-
|
29
|
-
debug: [ 33, 'Debug a JS file' ],
|
30
|
-
'debug-wasm': [ 33, 'Debug the compiled Wasm of a JS file' ],
|
31
|
-
})) {
|
32
|
-
console.log(` \x1B[1m\x1B[${color}m${cmd}\x1B[0m${' '.repeat(20 - cmd.length - (desc.startsWith('🧪') ? 3 : 0))}${desc}`);
|
43
|
+
console.log(` \x1B[90mporf\x1B[0m \x1B[1m\x1B[${color}m${cmd}\x1B[0m\x1B[2m${post}\x1B[0m ${' '.repeat(30 - cmd.length - post.length)}${desc}`);
|
33
44
|
}
|
34
45
|
|
35
|
-
// console.log();
|
36
|
-
|
37
|
-
// // options
|
38
|
-
// console.log(`\n\u001b[4mCommands\x1B[0m`);
|
39
|
-
// for (const [ cmd, [ color, desc ] ] of Object.entries({
|
40
|
-
// run: [ 34, 'Run a JS file' ],
|
41
|
-
// wasm: [ 34, 'Compile a JS file to a Wasm binary\n' ],
|
42
|
-
// c: [ 31, 'Compile a JS file to C source code' ],
|
43
|
-
// native: [ 31, 'Compile a JS file to a native binary\n' ],
|
44
|
-
// profile: [ 33, 'Profile a JS file' ],
|
45
|
-
// debug: [ 33, 'Debug a JS file' ],
|
46
|
-
// 'debug-wasm': [ 33, 'Debug the compiled Wasm of a JS file' ]
|
47
|
-
// })) {
|
48
|
-
// console.log(` \x1B[1m\x1B[${color}m${cmd}\x1B[0m${' '.repeat(20 - cmd.length - (desc.startsWith('🧪') ? 3 : 0))}${desc}`);
|
49
|
-
// }
|
50
|
-
|
51
46
|
console.log();
|
52
47
|
process.exit(0);
|
53
48
|
}
|
@@ -59,7 +54,7 @@ const done = async () => {
|
|
59
54
|
};
|
60
55
|
|
61
56
|
let file = process.argv.slice(2).find(x => x[0] !== '-');
|
62
|
-
if (['precompile', 'run', 'wasm', 'native', 'c', '
|
57
|
+
if (['precompile', 'run', 'wasm', 'native', 'c', 'flamegraph', 'hotlines', 'debug', 'dissect'].includes(file)) {
|
63
58
|
// remove this arg
|
64
59
|
process.argv.splice(process.argv.indexOf(file), 1);
|
65
60
|
|
@@ -68,8 +63,13 @@ if (['precompile', 'run', 'wasm', 'native', 'c', 'profile', 'debug', 'debug-wasm
|
|
68
63
|
await done();
|
69
64
|
}
|
70
65
|
|
71
|
-
if (file === '
|
72
|
-
await import('./
|
66
|
+
if (file === 'flamegraph') {
|
67
|
+
await import('./flamegraph.js');
|
68
|
+
await done();
|
69
|
+
}
|
70
|
+
|
71
|
+
if (file === 'hotlines') {
|
72
|
+
await import('./hotlines.js');
|
73
73
|
await done();
|
74
74
|
}
|
75
75
|
|
@@ -82,7 +82,7 @@ if (['precompile', 'run', 'wasm', 'native', 'c', 'profile', 'debug', 'debug-wasm
|
|
82
82
|
process.argv.push(`--target=${file}`);
|
83
83
|
}
|
84
84
|
|
85
|
-
if (file === '
|
85
|
+
if (file === 'dissect') {
|
86
86
|
process.argv.push('--asur', '--wasm-debug');
|
87
87
|
}
|
88
88
|
|