porffor 0.14.0-4e46400a9 → 0.14.0-7bef6473d
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/codegen.js +16 -58
- package/compiler/wrap.js +5 -10
- package/package.json +1 -1
package/compiler/codegen.js
CHANGED
@@ -58,11 +58,10 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
|
|
58
58
|
|
59
59
|
case 'ArrowFunctionExpression':
|
60
60
|
case 'FunctionDeclaration':
|
61
|
-
case 'FunctionExpression':
|
62
61
|
const func = generateFunc(scope, decl);
|
63
62
|
|
64
63
|
if (decl.type.endsWith('Expression')) {
|
65
|
-
return number(func.index
|
64
|
+
return number(func.index);
|
66
65
|
}
|
67
66
|
|
68
67
|
return [];
|
@@ -188,7 +187,7 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
|
|
188
187
|
if (!Array.isArray(inst)) inst = [ inst ];
|
189
188
|
const immediates = asm.slice(1).map(x => {
|
190
189
|
const int = parseInt(x);
|
191
|
-
if (Number.isNaN(int)) return scope.locals[x]?.idx
|
190
|
+
if (Number.isNaN(int)) return scope.locals[x]?.idx;
|
192
191
|
return int;
|
193
192
|
});
|
194
193
|
|
@@ -314,10 +313,10 @@ const generateIdent = (scope, decl) => {
|
|
314
313
|
|
315
314
|
if (local?.idx === undefined) {
|
316
315
|
// no local var with name
|
317
|
-
if (Object.hasOwn(
|
316
|
+
if (Object.hasOwn(importedFuncs, name)) return number(importedFuncs[name]);
|
317
|
+
if (Object.hasOwn(funcIndex, name)) return number(funcIndex[name]);
|
318
318
|
|
319
|
-
if (Object.hasOwn(
|
320
|
-
if (Object.hasOwn(funcIndex, name)) return number(funcIndex[name] - importedFuncs.length);
|
319
|
+
if (Object.hasOwn(globals, name)) return [ [ Opcodes.global_get, globals[name].idx ] ];
|
321
320
|
}
|
322
321
|
|
323
322
|
if (local?.idx === undefined && rawName.startsWith('__')) {
|
@@ -1832,23 +1831,7 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1832
1831
|
}
|
1833
1832
|
|
1834
1833
|
if (idx === undefined) {
|
1835
|
-
if (scope.locals[name] !== undefined || globals[name] !== undefined || builtinVars[name] !== undefined) {
|
1836
|
-
if (!Prefs.indirectCalls) return internalThrow(scope, 'TypeError', `${unhackName(name)} is not a function`, true);
|
1837
|
-
|
1838
|
-
const [ local, global ] = lookupName(scope, name);
|
1839
|
-
funcs.table = true;
|
1840
|
-
|
1841
|
-
// todo: only works when:
|
1842
|
-
// 1. arg count matches arg count of function
|
1843
|
-
// 2. function uses typedParams and typedReturns
|
1844
|
-
return typeSwitch(scope, getNodeType(decl.callee), {
|
1845
|
-
[TYPES.function]: [
|
1846
|
-
[ global ? Opcodes.global_get : Opcodes.local_get, local.idx ],
|
1847
|
-
[ Opcodes.call_indirect ]
|
1848
|
-
],
|
1849
|
-
default: internalThrow(scope, 'TypeError', `${unhackName(name)} is not a function`, true)
|
1850
|
-
});
|
1851
|
-
}
|
1834
|
+
if (scope.locals[name] !== undefined || globals[name] !== undefined || builtinVars[name] !== undefined) return internalThrow(scope, 'TypeError', `${unhackName(name)} is not a function`, true);
|
1852
1835
|
return internalThrow(scope, 'ReferenceError', `${unhackName(name)} is not defined`, true);
|
1853
1836
|
}
|
1854
1837
|
|
@@ -1877,10 +1860,11 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1877
1860
|
const arg = args[i];
|
1878
1861
|
out = out.concat(generate(scope, arg));
|
1879
1862
|
|
1880
|
-
if (valtypeBinary !== Valtype.i32
|
1881
|
-
|
1882
|
-
|
1883
|
-
|
1863
|
+
if (builtinFuncs[name] && builtinFuncs[name].params[i * (typedParams ? 2 : 1)] === Valtype.i32 && valtypeBinary !== Valtype.i32) {
|
1864
|
+
out.push(Opcodes.i32_to);
|
1865
|
+
}
|
1866
|
+
|
1867
|
+
if (importedFuncs[name] && name.startsWith('profile')) {
|
1884
1868
|
out.push(Opcodes.i32_to);
|
1885
1869
|
}
|
1886
1870
|
|
@@ -2190,22 +2174,6 @@ const generateVar = (scope, decl) => {
|
|
2190
2174
|
}
|
2191
2175
|
|
2192
2176
|
if (x.init) {
|
2193
|
-
// if (isFuncType(x.init.type)) {
|
2194
|
-
// // let a = function () { ... }
|
2195
|
-
// x.init.id = { name };
|
2196
|
-
|
2197
|
-
// const func = generateFunc(scope, x.init);
|
2198
|
-
|
2199
|
-
// out.push(
|
2200
|
-
// ...number(func.index - importedFuncs.length),
|
2201
|
-
// [ global ? Opcodes.global_set : Opcodes.local_set, idx ],
|
2202
|
-
|
2203
|
-
// ...setType(scope, name, TYPES.function)
|
2204
|
-
// );
|
2205
|
-
|
2206
|
-
// continue;
|
2207
|
-
// }
|
2208
|
-
|
2209
2177
|
const generated = generate(scope, x.init, global, name);
|
2210
2178
|
if (scope.arrays?.get(name) != null) {
|
2211
2179
|
// hack to set local as pointer before
|
@@ -2217,7 +2185,6 @@ const generateVar = (scope, decl) => {
|
|
2217
2185
|
out = out.concat(generated);
|
2218
2186
|
out.push([ global ? Opcodes.global_set : Opcodes.local_set, idx ]);
|
2219
2187
|
}
|
2220
|
-
|
2221
2188
|
out.push(...setType(scope, name, getNodeType(scope, x.init)));
|
2222
2189
|
}
|
2223
2190
|
|
@@ -2231,7 +2198,6 @@ const generateVar = (scope, decl) => {
|
|
2231
2198
|
// todo: optimize this func for valueUnused
|
2232
2199
|
const generateAssign = (scope, decl, _global, _name, valueUnused = false) => {
|
2233
2200
|
const { type, name } = decl.left;
|
2234
|
-
const [ local, isGlobal ] = lookupName(scope, name);
|
2235
2201
|
|
2236
2202
|
if (type === 'ObjectPattern') {
|
2237
2203
|
// hack: ignore object parts of `var a = {} = 2`
|
@@ -2241,18 +2207,8 @@ const generateAssign = (scope, decl, _global, _name, valueUnused = false) => {
|
|
2241
2207
|
if (isFuncType(decl.right.type)) {
|
2242
2208
|
// hack for a = function () { ... }
|
2243
2209
|
decl.right.id = { name };
|
2244
|
-
|
2245
|
-
|
2246
|
-
|
2247
|
-
return [
|
2248
|
-
...number(func.index - importedFuncs.length),
|
2249
|
-
...(local != null ? [
|
2250
|
-
[ isGlobal ? Opcodes.global_set : Opcodes.local_set, local.idx ],
|
2251
|
-
[ isGlobal ? Opcodes.global_get : Opcodes.local_get, local.idx ],
|
2252
|
-
|
2253
|
-
...setType(scope, name, TYPES.function)
|
2254
|
-
] : [])
|
2255
|
-
];
|
2210
|
+
generateFunc(scope, decl.right);
|
2211
|
+
return [];
|
2256
2212
|
}
|
2257
2213
|
|
2258
2214
|
const op = decl.operator.slice(0, -1) || '=';
|
@@ -2351,6 +2307,8 @@ const generateAssign = (scope, decl, _global, _name, valueUnused = false) => {
|
|
2351
2307
|
|
2352
2308
|
if (!name) return todo(scope, 'destructuring is not supported yet', true);
|
2353
2309
|
|
2310
|
+
const [ local, isGlobal ] = lookupName(scope, name);
|
2311
|
+
|
2354
2312
|
if (local === undefined) {
|
2355
2313
|
// todo: this should be a sloppy mode only thing
|
2356
2314
|
|
@@ -3410,7 +3368,7 @@ const generateFunc = (scope, decl) => {
|
|
3410
3368
|
|
3411
3369
|
if (typedInput && decl.returnType) {
|
3412
3370
|
const { type } = extractTypeAnnotation(decl.returnType);
|
3413
|
-
if (type != null
|
3371
|
+
if (type != null) {
|
3414
3372
|
innerScope.returnType = type;
|
3415
3373
|
innerScope.returns = [ valtypeBinary ];
|
3416
3374
|
}
|
package/compiler/wrap.js
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
import { encodeVector, encodeLocal } from './encoding.js';
|
2
|
-
import { importedFuncs } from './builtins.js';
|
3
1
|
import compile from './index.js';
|
4
2
|
import decompile from './decompile.js';
|
3
|
+
import { encodeVector, encodeLocal } from './encoding.js';
|
5
4
|
import { TYPES } from './types.js';
|
6
5
|
import { log } from './log.js';
|
7
6
|
import Prefs from './prefs.js';
|
@@ -15,13 +14,9 @@ const porfToJSValue = (memory, funcs, value, type) => {
|
|
15
14
|
case TYPES.object: return value === 0 ? null : {};
|
16
15
|
|
17
16
|
case TYPES.function: {
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
} else {
|
22
|
-
func = funcs.find(x => ((x.originalIndex ?? x.index) - importedFuncs.length) === value);
|
23
|
-
}
|
24
|
-
|
17
|
+
// wasm func index, including all imports
|
18
|
+
const func = funcs.find(x => (x.originalIndex ?? x.index) === value);
|
19
|
+
// if (!func) return value;
|
25
20
|
if (!func) return function () {};
|
26
21
|
|
27
22
|
// make fake empty func for repl/etc
|
@@ -221,7 +216,7 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
|
|
221
216
|
|
222
217
|
if (ret == null) return undefined;
|
223
218
|
|
224
|
-
return porfToJSValue(memory, funcs, ret[0], ret[1])
|
219
|
+
return porfToJSValue(memory, funcs, ret[0], ret[1])
|
225
220
|
} catch (e) {
|
226
221
|
if (e.is && e.is(exceptTag)) {
|
227
222
|
const exceptId = e.getArg(exceptTag, 0);
|
package/package.json
CHANGED