porffor 0.25.2 → 0.25.3
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/CONTRIBUTING.md +142 -3
- package/README.md +28 -16
- package/compiler/builtins_precompiled.js +37 -37
- package/compiler/codegen.js +5 -0
- package/compiler/decompile.js +16 -6
- package/compiler/wrap.js +1 -5
- package/package.json +1 -1
- package/runner/index.js +41 -6
package/compiler/codegen.js
CHANGED
@@ -845,6 +845,11 @@ const nullish = (scope, wasm, type, intIn = false, intOut = false) => {
|
|
845
845
|
...(!useTmp ? [] : [ [ Opcodes.local_set, tmp ] ]),
|
846
846
|
|
847
847
|
...typeSwitch(scope, type, {
|
848
|
+
[TYPES.empty]: [
|
849
|
+
// empty
|
850
|
+
...(!useTmp ? [ [ Opcodes.drop ] ] : []),
|
851
|
+
...number(1, intOut ? Valtype.i32 : valtypeBinary)
|
852
|
+
],
|
848
853
|
[TYPES.undefined]: [
|
849
854
|
// undefined
|
850
855
|
...(!useTmp ? [ [ Opcodes.drop ] ] : []),
|
package/compiler/decompile.js
CHANGED
@@ -11,17 +11,26 @@ export default (wasm, name = '', ind = 0, locals = {}, params = [], returns = []
|
|
11
11
|
const invLocals = inv(locals, x => x.idx);
|
12
12
|
const invGlobals = inv(globals, x => x.idx);
|
13
13
|
|
14
|
-
const makeSignature = (params, returns
|
14
|
+
const makeSignature = (params, returns, locals) => {
|
15
|
+
if (locals) {
|
16
|
+
const localNames = inv(locals, x => x.idx);
|
17
|
+
return `(${params.map((x, i) => `${localNames[i]}(${i}): ${invValtype[x]}`).join(', ')}) -> (${returns.map(x => invValtype[x]).join(', ')})`;
|
18
|
+
}
|
19
|
+
|
20
|
+
return `(${params.map((x, i) => invValtype[x]).join(', ')}) -> (${returns.map(x => invValtype[x]).join(', ')})`;
|
21
|
+
}
|
15
22
|
|
16
|
-
let out = '', depth =
|
17
|
-
if (name) out += `${makeSignature(params, returns)}
|
23
|
+
let out = '', depth = 0;
|
24
|
+
if (name) out += `${name}(${ind}) ${makeSignature(params, returns, locals)}\n`;
|
18
25
|
|
19
26
|
const justLocals = Object.values(locals).sort((a, b) => a.idx - b.idx).slice(params.length);
|
20
|
-
|
27
|
+
for (const x of justLocals) {
|
28
|
+
out += `;; local ${invLocals[x.idx]}(${x.idx}): ${invValtype[x.type]}\n`
|
29
|
+
}
|
21
30
|
|
22
31
|
let i = -1, lastInst;
|
23
32
|
let byte = 0;
|
24
|
-
for (let inst of wasm
|
33
|
+
for (let inst of wasm) {
|
25
34
|
i++;
|
26
35
|
if (inst[0] === null) continue;
|
27
36
|
|
@@ -102,6 +111,7 @@ export default (wasm, name = '', ind = 0, locals = {}, params = [], returns = []
|
|
102
111
|
const name = invLocals[inst[1]];
|
103
112
|
const type = invValtype[locals[name]?.type];
|
104
113
|
if (name) out += ` ;; $${name}${type !== valtype ? ` (${type})` : ''}`;
|
114
|
+
else out += ` ;; unknown local`
|
105
115
|
}
|
106
116
|
|
107
117
|
if (inst[0] === Opcodes.global_get || inst[0] === Opcodes.global_set) {
|
@@ -130,4 +140,4 @@ export const highlightAsm = asm => asm
|
|
130
140
|
.replace(/(block|loop|if|end|else|try|catch_all|catch|delegate)/g, _ => `\x1B[95m${_}\x1B[0m`)
|
131
141
|
.replace(/unreachable/g, _ => `\x1B[91m${_}\x1B[0m`)
|
132
142
|
.replace(/ \-?[0-9\.]+/g, _ => ` \x1B[33m${_.slice(1)}\x1B[0m`)
|
133
|
-
.replace(
|
143
|
+
.replace(/;;.*$/gm, _ => `\x1B[90m${_.replaceAll(/\x1B\[[0-9]+m/g, '')}\x1B[0m`);
|
package/compiler/wrap.js
CHANGED
@@ -280,10 +280,6 @@ export default (source, flags = [ 'module' ], customImports = {}, print = str =>
|
|
280
280
|
const printDecomp = (middleIndex, func, funcs, globals, exceptions) => {
|
281
281
|
console.log(`\x1B[35m\x1B[1mporffor backtrace\u001b[0m`);
|
282
282
|
|
283
|
-
const strParams = func.params.map(v => invValtype[v]);
|
284
|
-
const strReturns = func.returns.map(v => invValtype[v]);
|
285
|
-
console.log(`\x1B[1m${func.name}\x1B[0m \x1B[90m(${strParams.join(', ')}) -> (${strReturns.join(', ')})\x1B[0m`);
|
286
|
-
|
287
283
|
const surrounding = Prefs.backtraceSurrounding ?? 5;
|
288
284
|
let min = middleIndex - surrounding;
|
289
285
|
let max = middleIndex + surrounding + 1;
|
@@ -292,7 +288,7 @@ export default (source, flags = [ 'module' ], customImports = {}, print = str =>
|
|
292
288
|
max = func.wasm.length;
|
293
289
|
}
|
294
290
|
|
295
|
-
const decomp = decompile(func.wasm.slice(min, max),
|
291
|
+
const decomp = decompile(func.wasm.slice(min, max), func.name, 0, func.locals, func.params, func.returns, funcs, globals, exceptions).slice(0, -1).split('\n');
|
296
292
|
|
297
293
|
const noAnsi = s => s.replace(/\u001b\[[0-9]+m/g, '');
|
298
294
|
let longest = 0;
|
package/package.json
CHANGED
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.25.
|
3
|
+
globalThis.version = '0.25.3+8f4d9af84';
|
4
4
|
|
5
5
|
// deno compat
|
6
6
|
if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
|
@@ -108,7 +108,33 @@ if (['precompile', 'run', 'wasm', 'native', 'c', 'profile', 'debug', 'debug-wasm
|
|
108
108
|
|
109
109
|
globalThis.file = file;
|
110
110
|
|
111
|
-
|
111
|
+
let source = '', printOutput = false;
|
112
|
+
if (process.argv.length >= 4) {
|
113
|
+
let evalIndex = process.argv.indexOf('-e');
|
114
|
+
if (evalIndex === -1) evalIndex = process.argv.indexOf('--eval');
|
115
|
+
if (evalIndex !== -1) {
|
116
|
+
source = process.argv[evalIndex + 1];
|
117
|
+
if (source) {
|
118
|
+
if (source.startsWith('"') || source.startsWith("'")) source = source.slice(1, -1);
|
119
|
+
process.argv.splice(evalIndex, 2); // remove flag and value
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
let printIndex = process.argv.indexOf('-p');
|
124
|
+
if (printIndex === -1) printIndex = process.argv.indexOf('--print');
|
125
|
+
if (printIndex !== -1) {
|
126
|
+
process.argv.push('--no-opt-unused');
|
127
|
+
source = process.argv[printIndex + 1];
|
128
|
+
if (source) {
|
129
|
+
if (source.startsWith('"') || source.startsWith("'")) source = source.slice(1, -1);
|
130
|
+
process.argv.splice(printIndex, 2); // remove flag and value
|
131
|
+
}
|
132
|
+
|
133
|
+
printOutput = true;
|
134
|
+
}
|
135
|
+
}
|
136
|
+
|
137
|
+
if (!file && !source) {
|
112
138
|
if (process.argv.includes('-v') || process.argv.includes('--version')) {
|
113
139
|
// just print version
|
114
140
|
console.log(globalThis.version);
|
@@ -120,7 +146,7 @@ if (!file) {
|
|
120
146
|
await done();
|
121
147
|
}
|
122
148
|
|
123
|
-
|
149
|
+
source ||= fs.readFileSync(file, 'utf8');
|
124
150
|
|
125
151
|
const compile = (await import('../compiler/wrap.js')).default;
|
126
152
|
|
@@ -137,19 +163,20 @@ const print = str => {
|
|
137
163
|
};
|
138
164
|
|
139
165
|
let runStart;
|
166
|
+
let ret;
|
140
167
|
try {
|
141
168
|
if (process.argv.includes('-b')) {
|
142
169
|
const { wasm, exports } = compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
|
143
170
|
|
144
171
|
runStart = performance.now();
|
145
|
-
if (!process.argv.includes('--no-run')) exports.main();
|
172
|
+
if (!process.argv.includes('--no-run')) ret = exports.main();
|
146
173
|
|
147
174
|
console.log(`\n\nwasm size: ${wasm.byteLength} bytes`);
|
148
175
|
} else {
|
149
176
|
const { exports } = compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
|
150
177
|
|
151
178
|
runStart = performance.now();
|
152
|
-
if (!process.argv.includes('--no-run')) exports.main();
|
179
|
+
if (!process.argv.includes('--no-run')) ret = exports.main();
|
153
180
|
}
|
154
181
|
// if (cache) process.stdout.write(cache);
|
155
182
|
} catch (e) {
|
@@ -159,4 +186,12 @@ try {
|
|
159
186
|
console.error(out);
|
160
187
|
}
|
161
188
|
|
162
|
-
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`);
|
189
|
+
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`);
|
190
|
+
|
191
|
+
if (printOutput) {
|
192
|
+
if (process.argv.includes('-d') && ret?.type != null) {
|
193
|
+
ret = ret.js;
|
194
|
+
}
|
195
|
+
|
196
|
+
console.log(ret);
|
197
|
+
}
|