Skip to content

Commit 42a7092

Browse files
committed
codegen: rewrite class property def
make computed properties gen at class-time not construction-time, follows spec. also less jank test262: 56.51% (+0.02) | πŸ§ͺ 50259 | 🀠 28399 (+9) | ❌ 7222 (-9) | πŸ’€ 13701 | πŸ—οΈ 32 | πŸ’₯ 177 | ⏰ 11 | πŸ“ 717
1 parent 3712a69 commit 42a7092

File tree

4 files changed

+56
-27
lines changed

4 files changed

+56
-27
lines changed

β€Žcompiler/codegen.js

+53-24
Original file line numberDiff line numberDiff line change
@@ -3089,7 +3089,7 @@ const typeIsNotOneOf = (type, types, valtype = Valtype.i32) => {
30893089
return out;
30903090
};
30913091

3092-
const allocVar = (scope, name, global = false, type = true, redecl = false) => {
3092+
const allocVar = (scope, name, global = false, type = true, redecl = false, i32 = false) => {
30933093
const target = global ? globals : scope.locals;
30943094

30953095
// already declared
@@ -3104,7 +3104,7 @@ const allocVar = (scope, name, global = false, type = true, redecl = false) => {
31043104
}
31053105

31063106
let idx = global ? globals['#ind']++ : scope.localInd++;
3107-
target[name] = { idx, type: valtypeBinary };
3107+
target[name] = { idx, type: i32 ? Valtype.i32 : valtypeBinary };
31083108

31093109
if (type) {
31103110
let typeIdx = global ? globals['#ind']++ : scope.localInd++;
@@ -6201,18 +6201,6 @@ const generateClass = (scope, decl) => {
62016201
// default value to undefined
62026202
value ??= DEFAULT_VALUE();
62036203

6204-
let outArr = out, outOp = 'push', outScope = scope;
6205-
if (type === 'PropertyDefinition' && !_static) {
6206-
// define in construction instead
6207-
outArr = func.wasm;
6208-
outOp = 'unshift';
6209-
object = {
6210-
type: 'ThisExpression',
6211-
_noGlobalThis: true
6212-
};
6213-
outScope = func;
6214-
}
6215-
62166204
if (isFuncType(value.type)) {
62176205
let id = value.id;
62186206

@@ -6230,19 +6218,60 @@ const generateClass = (scope, decl) => {
62306218
};
62316219
}
62326220

6233-
outArr[outOp](
6234-
...generate(outScope, object),
6235-
Opcodes.i32_to_u,
6236-
...getNodeType(outScope, object),
6221+
if (type === 'PropertyDefinition' && !_static) {
6222+
// define in construction instead
6223+
object = {
6224+
type: 'ThisExpression',
6225+
_noGlobalThis: true
6226+
};
62376227

6238-
...toPropertyKey(outScope, generate(outScope, k), getNodeType(outScope, k), computed, true),
6228+
let computedTmp;
6229+
if (computed) {
6230+
// compute now, reference in construction
6231+
computedTmp = allocVar(scope, `#class_computed_prop${uniqId()}`, true, true, false, true);
62396232

6240-
...generate(outScope, value),
6241-
...(initKind !== 'value' && initKind !== 'method' ? [ Opcodes.i32_to_u ] : []),
6242-
...getNodeType(outScope, value),
6233+
out.push(
6234+
...toPropertyKey(scope, generate(scope, k), getNodeType(scope, k), computed, true),
6235+
[ Opcodes.global_set, computedTmp + 1 ],
6236+
[ Opcodes.global_set, computedTmp ]
6237+
);
6238+
}
62436239

6244-
[ Opcodes.call, includeBuiltin(outScope, `__Porffor_object_class_${initKind}`).index ]
6245-
);
6240+
func.wasm.unshift(
6241+
...generate(func, object),
6242+
Opcodes.i32_to_u,
6243+
...getNodeType(func, object),
6244+
6245+
...(computed ? [
6246+
[ Opcodes.global_get, computedTmp ],
6247+
[ Opcodes.global_get, computedTmp + 1 ],
6248+
] : [
6249+
...generate(func, k),
6250+
Opcodes.i32_to_u,
6251+
...getNodeType(func, k)
6252+
]),
6253+
6254+
...generate(func, value),
6255+
...(initKind !== 'value' && initKind !== 'method' ? [ Opcodes.i32_to_u ] : []),
6256+
...getNodeType(func, value),
6257+
6258+
[ Opcodes.call, includeBuiltin(func, `__Porffor_object_class_${initKind}`).index ]
6259+
);
6260+
} else {
6261+
out.push(
6262+
...generate(scope, object),
6263+
Opcodes.i32_to_u,
6264+
...getNodeType(scope, object),
6265+
6266+
...toPropertyKey(scope, generate(scope, k), getNodeType(scope, k), computed, true),
6267+
6268+
...generate(scope, value),
6269+
...(initKind !== 'value' && initKind !== 'method' ? [ Opcodes.i32_to_u ] : []),
6270+
...getNodeType(scope, value),
6271+
6272+
[ Opcodes.call, includeBuiltin(scope, `__Porffor_object_class_${initKind}`).index ]
6273+
);
6274+
}
62466275
}
62476276

62486277
delete scope.overrideThis;

β€Žpackage.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "porffor",
33
"description": "An ahead-of-time JavaScript compiler",
4-
"version": "0.55.21",
4+
"version": "0.55.22",
55
"author": "Oliver Medhurst <[email protected]>",
66
"license": "MIT",
77
"scripts": {},

β€Žrunner/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
import fs from 'node:fs';
3-
globalThis.version = '0.55.21';
3+
globalThis.version = '0.55.22';
44

55
// deno compat
66
if (typeof process === 'undefined' && typeof Deno !== 'undefined') {

β€Žtest262/history.json

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
Β (0)