porffor 0.24.13 → 0.24.14
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/assemble.js +2 -5
- package/compiler/builtins/_internal_object.ts +30 -1
- package/compiler/builtins.js +15 -0
- package/compiler/builtins_precompiled.js +334 -868
- package/compiler/codegen.js +44 -110
- package/compiler/precompile.js +1 -1
- package/package.json +1 -1
- package/runner/index.js +1 -1
package/compiler/codegen.js
CHANGED
@@ -359,7 +359,6 @@ const generateReturn = (scope, decl) => {
|
|
359
359
|
const out = [];
|
360
360
|
if (
|
361
361
|
scope.constr && // only do this in constructors
|
362
|
-
scope.newTarget && scope.usesThis && // sanity check
|
363
362
|
!globalThis.precompile // skip in precompiled built-ins, we should not require this and handle it ourselves
|
364
363
|
) {
|
365
364
|
// ignore return value and return this if being constructed
|
@@ -1242,7 +1241,7 @@ const asmFuncToAsm = (scope, func) => {
|
|
1242
1241
|
});
|
1243
1242
|
};
|
1244
1243
|
|
1245
|
-
const asmFunc = (name, { wasm, params = [], typedParams = false, locals: localTypes = [], globals: globalTypes = [], globalInits = [], returns = [], returnType, localNames = [], globalNames = [], data: _data = [], table = false, constr = false,
|
1244
|
+
const asmFunc = (name, { wasm, params = [], typedParams = false, locals: localTypes = [], globals: globalTypes = [], globalInits = [], returns = [], returnType, localNames = [], globalNames = [], data: _data = [], table = false, constr = false, hasRestArgument = false } = {}) => {
|
1246
1245
|
if (wasm == null) { // called with no builtin
|
1247
1246
|
log.warning('codegen', `${name} has no built-in!`);
|
1248
1247
|
wasm = [];
|
@@ -1279,8 +1278,6 @@ const asmFunc = (name, { wasm, params = [], typedParams = false, locals: localTy
|
|
1279
1278
|
index: currentFuncIndex++,
|
1280
1279
|
table,
|
1281
1280
|
constr,
|
1282
|
-
newTarget,
|
1283
|
-
usesThis,
|
1284
1281
|
globalInits
|
1285
1282
|
};
|
1286
1283
|
|
@@ -1787,7 +1784,7 @@ const createNewTarget = (scope, decl, idx) => {
|
|
1787
1784
|
...number(UNDEFINED),
|
1788
1785
|
...number(TYPES.undefined, Valtype.i32)
|
1789
1786
|
];
|
1790
|
-
}
|
1787
|
+
};
|
1791
1788
|
|
1792
1789
|
const createThisArg = (scope, decl, getFunc, knownThis = undefined) => {
|
1793
1790
|
if (knownThis) {
|
@@ -2274,71 +2271,36 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
2274
2271
|
// todo: i'm sure this could be made better somehow, probably only with #96?
|
2275
2272
|
return checkFlag(0b1,
|
2276
2273
|
// no type return
|
2277
|
-
checkFlag(
|
2278
|
-
// no type return &
|
2279
|
-
|
2280
|
-
|
2281
|
-
|
2282
|
-
|
2283
|
-
|
2284
|
-
|
2285
|
-
|
2286
|
-
|
2287
|
-
|
2288
|
-
|
2289
|
-
|
2290
|
-
|
2291
|
-
[ Opcodes.call_indirect, argc + 1, 0, 'no_type_return' ]
|
2292
|
-
]),
|
2293
|
-
// no type return
|
2294
|
-
checkFlag(0b100, [
|
2295
|
-
// no type return & new.target
|
2296
|
-
...createNewTarget(scope, decl),
|
2297
|
-
...argsOut,
|
2298
|
-
[ Opcodes.local_get, funcLocal ],
|
2299
|
-
[ Opcodes.call_indirect, argc + 1, 0, 'no_type_return' ],
|
2300
|
-
], [
|
2301
|
-
// no type return
|
2302
|
-
...argsOut,
|
2303
|
-
[ Opcodes.local_get, funcLocal ],
|
2304
|
-
[ Opcodes.call_indirect, argc, 0, 'no_type_return' ]
|
2305
|
-
]),
|
2306
|
-
),
|
2274
|
+
checkFlag(0b10, [
|
2275
|
+
// no type return & constr
|
2276
|
+
...createNewTarget(scope, decl),
|
2277
|
+
...createThisArg(scope, decl, [], knownThis),
|
2278
|
+
...argsOut,
|
2279
|
+
[ Opcodes.local_get, funcLocal ],
|
2280
|
+
[ Opcodes.call_indirect, argc + 2, 0, 'no_type_return' ],
|
2281
|
+
], [
|
2282
|
+
// no type return & not constr
|
2283
|
+
...argsOut,
|
2284
|
+
[ Opcodes.local_get, funcLocal ],
|
2285
|
+
[ Opcodes.call_indirect, argc, 0, 'no_type_return' ]
|
2286
|
+
]),
|
2287
|
+
|
2307
2288
|
// type return
|
2308
|
-
checkFlag(
|
2309
|
-
// type return &
|
2310
|
-
|
2311
|
-
|
2312
|
-
|
2313
|
-
|
2314
|
-
|
2315
|
-
|
2316
|
-
|
2317
|
-
...setLastType(scope),
|
2318
|
-
], [
|
2319
|
-
// type return & this
|
2320
|
-
...createThisArg(scope, decl),
|
2321
|
-
...argsOut,
|
2322
|
-
[ Opcodes.local_get, funcLocal ],
|
2323
|
-
[ Opcodes.call_indirect, argc + 1, 0 ],
|
2324
|
-
...setLastType(scope),
|
2325
|
-
]),
|
2289
|
+
checkFlag(0b10, [
|
2290
|
+
// type return & constr
|
2291
|
+
...createNewTarget(scope, decl),
|
2292
|
+
...createThisArg(scope, decl, [], knownThis),
|
2293
|
+
...argsOut,
|
2294
|
+
[ Opcodes.local_get, funcLocal ],
|
2295
|
+
[ Opcodes.call_indirect, argc + 2, 0 ],
|
2296
|
+
...setLastType(scope),
|
2297
|
+
], [
|
2326
2298
|
// type return
|
2327
|
-
|
2328
|
-
|
2329
|
-
|
2330
|
-
|
2331
|
-
|
2332
|
-
[ Opcodes.call_indirect, argc + 1, 0 ],
|
2333
|
-
...setLastType(scope),
|
2334
|
-
], [
|
2335
|
-
// type return
|
2336
|
-
...argsOut,
|
2337
|
-
[ Opcodes.local_get, funcLocal ],
|
2338
|
-
[ Opcodes.call_indirect, argc, 0 ],
|
2339
|
-
...setLastType(scope),
|
2340
|
-
]),
|
2341
|
-
)
|
2299
|
+
...argsOut,
|
2300
|
+
[ Opcodes.local_get, funcLocal ],
|
2301
|
+
[ Opcodes.call_indirect, argc, 0 ],
|
2302
|
+
...setLastType(scope),
|
2303
|
+
])
|
2342
2304
|
);
|
2343
2305
|
};
|
2344
2306
|
|
@@ -2408,14 +2370,10 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
2408
2370
|
return internalThrow(scope, 'TypeError', `${unhackName(name)} is not a constructor`, true);
|
2409
2371
|
}
|
2410
2372
|
|
2411
|
-
if (func && func.
|
2373
|
+
if (func && func.constr) {
|
2412
2374
|
out.push(...createNewTarget(scope, decl, idx - importedFuncs.length));
|
2413
|
-
paramOffset += 2;
|
2414
|
-
}
|
2415
|
-
|
2416
|
-
if (func && func.usesThis) {
|
2417
2375
|
out.push(...createThisArg(scope, decl, func));
|
2418
|
-
paramOffset +=
|
2376
|
+
paramOffset += 4;
|
2419
2377
|
}
|
2420
2378
|
|
2421
2379
|
let args = [...decl.arguments];
|
@@ -2509,13 +2467,11 @@ const generateThis = (scope, decl, _global, _name) => {
|
|
2509
2467
|
];
|
2510
2468
|
}
|
2511
2469
|
|
2512
|
-
scope.usesThis = true;
|
2513
|
-
|
2514
2470
|
return [
|
2515
2471
|
[ Opcodes.local_get, '#this' ],
|
2516
2472
|
...setLastType(scope, [ [ Opcodes.local_get, '#this#type' ] ])
|
2517
2473
|
];
|
2518
|
-
}
|
2474
|
+
};
|
2519
2475
|
|
2520
2476
|
// bad hack for undefined and null working without additional logic
|
2521
2477
|
const DEFAULT_VALUE = () => ({
|
@@ -4337,8 +4293,6 @@ const generateEmpty = (scope, decl) => {
|
|
4337
4293
|
const generateMeta = (scope, decl) => {
|
4338
4294
|
switch (`${decl.meta.name}.${decl.property.name}`) {
|
4339
4295
|
case 'new.target': {
|
4340
|
-
scope.newTarget = true;
|
4341
|
-
|
4342
4296
|
return [
|
4343
4297
|
[ Opcodes.local_get, '#newtarget' ],
|
4344
4298
|
];
|
@@ -4778,12 +4732,11 @@ const countParams = (func, name = undefined) => {
|
|
4778
4732
|
if (func.argc) return func.argc;
|
4779
4733
|
|
4780
4734
|
let params = func.params.length;
|
4781
|
-
if (func.
|
4782
|
-
if (func.usesThis) params -= 2;
|
4735
|
+
if (func.constr) params -= 4;
|
4783
4736
|
if (!func.internal || builtinFuncs[func.name]?.typedParams) params = Math.floor(params / 2);
|
4784
4737
|
|
4785
|
-
return params;
|
4786
|
-
}
|
4738
|
+
return func.argc = params;
|
4739
|
+
};
|
4787
4740
|
|
4788
4741
|
const generateMember = (scope, decl, _global, _name, _objectWasm = undefined) => {
|
4789
4742
|
const name = decl.object.name;
|
@@ -5235,13 +5188,6 @@ const generateFunc = (scope, decl) => {
|
|
5235
5188
|
|
5236
5189
|
func.params = Object.values(func.locals).map(x => x.type);
|
5237
5190
|
|
5238
|
-
func.argc = countParams(func, name);
|
5239
|
-
|
5240
|
-
if (func.constr) {
|
5241
|
-
func.newTarget = true;
|
5242
|
-
func.usesThis = true;
|
5243
|
-
}
|
5244
|
-
|
5245
5191
|
let body = objectHack(decl.body);
|
5246
5192
|
if (decl.type === 'ArrowFunctionExpression' && decl.expression) {
|
5247
5193
|
// hack: () => 0 -> () => return 0
|
@@ -5275,18 +5221,10 @@ const generateFunc = (scope, decl) => {
|
|
5275
5221
|
wasm.push(...generateReturn(func, {}));
|
5276
5222
|
}
|
5277
5223
|
|
5278
|
-
if (func.
|
5224
|
+
if (func.constr) {
|
5279
5225
|
const locals = func.locals;
|
5280
|
-
let idxOffset =
|
5281
|
-
func.params = [...func.params];
|
5282
|
-
if (func.usesThis) {
|
5283
|
-
func.params.unshift(valtypeBinary, Valtype.i32);
|
5284
|
-
idxOffset += 2;
|
5285
|
-
}
|
5286
|
-
if (func.newTarget) {
|
5287
|
-
func.params.unshift(valtypeBinary, Valtype.i32);
|
5288
|
-
idxOffset += 2;
|
5289
|
-
}
|
5226
|
+
let idxOffset = 4;
|
5227
|
+
func.params = [ valtypeBinary, Valtype.i32, valtypeBinary, Valtype.i32, ...func.params ];
|
5290
5228
|
|
5291
5229
|
// move all local indexes by idxOffset
|
5292
5230
|
func.localInd += idxOffset;
|
@@ -5295,15 +5233,11 @@ const generateFunc = (scope, decl) => {
|
|
5295
5233
|
}
|
5296
5234
|
|
5297
5235
|
let indexes = idxOffset;
|
5298
|
-
|
5299
|
-
|
5300
|
-
|
5301
|
-
|
5302
|
-
}
|
5303
|
-
if (func.newTarget) {
|
5304
|
-
locals['#newtarget#type'] = { idx: --indexes, type: Valtype.i32 };
|
5305
|
-
locals['#newtarget'] = { idx: --indexes, type: valtypeBinary };
|
5306
|
-
}
|
5236
|
+
|
5237
|
+
locals['#this#type'] = { idx: --indexes, type: Valtype.i32 };
|
5238
|
+
locals['#this'] = { idx: --indexes, type: valtypeBinary };
|
5239
|
+
locals['#newtarget#type'] = { idx: --indexes, type: Valtype.i32 };
|
5240
|
+
locals['#newtarget'] = { idx: --indexes, type: valtypeBinary };
|
5307
5241
|
|
5308
5242
|
for (let i = 0; i < wasm.length; i++) {
|
5309
5243
|
// note: these needs to be copied, even though they realistically shouldn't
|
package/compiler/precompile.js
CHANGED
@@ -217,7 +217,7 @@ ${funcs.map(x => {
|
|
217
217
|
returns: ${JSON.stringify(x.returns)}, ${x.returnType != null ? `returnType: ${JSON.stringify(x.returnType)}` : 'typedReturns: 1'},
|
218
218
|
locals: ${JSON.stringify(locals.slice(x.params.length).map(x => x[1].type))}, localNames: ${JSON.stringify(locals.map(x => x[0]))},
|
219
219
|
${x.globalInits ? ` globalInits: {${Object.keys(x.globalInits).map(y => `${y}: ${rewriteWasm(x.globalInits[y])}`).join(',')}},\n` : ''}${x.data && x.data.length > 0 ? ` data: [${x.data.map(x => `[${x.offset ?? 'null'},[${x.bytes.join(',')}]]`).join(',')}],` : ''}
|
220
|
-
${x.table ? ` table: 1,` : ''}${x.constr ? ` constr: 1,` : ''}${x.
|
220
|
+
${x.table ? ` table: 1,` : ''}${x.constr ? ` constr: 1,` : ''}${x.hasRestArgument ? ` hasRestArgument: 1,` : ''}
|
221
221
|
};`.replaceAll('\n\n', '\n').replaceAll('\n\n', '\n').replaceAll('\n\n', '\n');
|
222
222
|
}).join('\n')}
|
223
223
|
};`;
|
package/package.json
CHANGED