@opencode/codemode 2.2.4 → 2.2.6
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 +70 -1
- package/dist/codemode.d.ts +55 -4
- package/dist/codemode.js +63 -8
- package/dist/extension.d.ts +14 -9
- package/dist/extension.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/interpreter/execute.d.ts +1 -1
- package/dist/interpreter/execute.js +16 -4
- package/dist/interpreter/extensions.d.ts +3 -3
- package/dist/interpreter/extensions.js +17 -11
- package/dist/interpreter/globals.js +10 -0
- package/dist/interpreter/interpreter.d.ts +11 -1
- package/dist/interpreter/interpreter.js +86 -2
- package/dist/interpreter/modules.d.ts +4 -0
- package/dist/interpreter/modules.js +33 -0
- package/dist/suspend-error.d.ts +23 -0
- package/dist/suspend-error.js +25 -0
- package/dist/tool-runtime.d.ts +4 -1
- package/dist/vm/async.d.ts +78 -0
- package/dist/vm/async.js +335 -0
- package/dist/vm/code.d.ts +122 -0
- package/dist/vm/code.js +407 -0
- package/dist/vm/compiler/compile.d.ts +13 -0
- package/dist/vm/compiler/compile.js +1477 -0
- package/dist/vm/compiler/scope.d.ts +74 -0
- package/dist/vm/compiler/scope.js +515 -0
- package/dist/vm/data.d.ts +7 -0
- package/dist/vm/data.js +13 -0
- package/dist/vm/generators.d.ts +42 -0
- package/dist/vm/generators.js +413 -0
- package/dist/vm/host.d.ts +103 -0
- package/dist/vm/host.js +226 -0
- package/dist/vm/index.d.ts +6 -0
- package/dist/vm/index.js +6 -0
- package/dist/vm/internal-prelude.d.ts +45 -0
- package/dist/vm/internal-prelude.js +534 -0
- package/dist/vm/opcodes.d.ts +222 -0
- package/dist/vm/opcodes.js +235 -0
- package/dist/vm/prelude/bytecode.d.ts +18 -0
- package/dist/vm/prelude/bytecode.js +31 -0
- package/dist/vm/prelude/bytecode.json +1 -0
- package/dist/vm/prelude/helpers.d.ts +8 -0
- package/dist/vm/prelude/helpers.js +258 -0
- package/dist/vm/prelude/prelude.d.ts +21 -0
- package/dist/vm/prelude/prelude.js +53 -0
- package/dist/vm/prelude/source.d.ts +1 -0
- package/dist/vm/prelude/source.js +652 -0
- package/dist/vm/runtime/callback.d.ts +25 -0
- package/dist/vm/runtime/callback.js +51 -0
- package/dist/vm/runtime/driver.d.ts +58 -0
- package/dist/vm/runtime/driver.js +184 -0
- package/dist/vm/runtime/errors.d.ts +23 -0
- package/dist/vm/runtime/errors.js +169 -0
- package/dist/vm/runtime/execute.d.ts +80 -0
- package/dist/vm/runtime/execute.js +414 -0
- package/dist/vm/runtime/extensions.d.ts +16 -0
- package/dist/vm/runtime/extensions.js +270 -0
- package/dist/vm/runtime/generators.d.ts +6 -0
- package/dist/vm/runtime/generators.js +44 -0
- package/dist/vm/runtime/globals.d.ts +6 -0
- package/dist/vm/runtime/globals.js +137 -0
- package/dist/vm/runtime/interpreter.d.ts +50 -0
- package/dist/vm/runtime/interpreter.js +94 -0
- package/dist/vm/runtime/intrinsic-names.d.ts +56 -0
- package/dist/vm/runtime/intrinsic-names.js +166 -0
- package/dist/vm/runtime/intrinsics.d.ts +15 -0
- package/dist/vm/runtime/intrinsics.js +111 -0
- package/dist/vm/runtime/limits.d.ts +10 -0
- package/dist/vm/runtime/limits.js +19 -0
- package/dist/vm/runtime/model.d.ts +58 -0
- package/dist/vm/runtime/model.js +57 -0
- package/dist/vm/runtime/modules.d.ts +4 -0
- package/dist/vm/runtime/modules.js +33 -0
- package/dist/vm/runtime/native.d.ts +19 -0
- package/dist/vm/runtime/native.js +41 -0
- package/dist/vm/runtime/objects.d.ts +403 -0
- package/dist/vm/runtime/objects.js +784 -0
- package/dist/vm/runtime/operations.d.ts +49 -0
- package/dist/vm/runtime/operations.js +288 -0
- package/dist/vm/runtime/promises.d.ts +55 -0
- package/dist/vm/runtime/promises.js +232 -0
- package/dist/vm/runtime/references.d.ts +8 -0
- package/dist/vm/runtime/references.js +51 -0
- package/dist/vm/runtime/signals.d.ts +69 -0
- package/dist/vm/runtime/signals.js +68 -0
- package/dist/vm/runtime/source.d.ts +29 -0
- package/dist/vm/runtime/source.js +63 -0
- package/dist/vm/runtime/table.d.ts +43 -0
- package/dist/vm/runtime/table.js +132 -0
- package/dist/vm/scheduler.d.ts +178 -0
- package/dist/vm/scheduler.js +63 -0
- package/dist/vm/snapshot/codec.d.ts +87 -0
- package/dist/vm/snapshot/codec.js +751 -0
- package/dist/vm/snapshot/snapshot.d.ts +49 -0
- package/dist/vm/snapshot/snapshot.js +70 -0
- package/dist/vm/snapshot.d.ts +32 -0
- package/dist/vm/snapshot.js +306 -0
- package/dist/vm/stdlib/array.d.ts +16 -0
- package/dist/vm/stdlib/array.js +311 -0
- package/dist/vm/stdlib/bytes.d.ts +11 -0
- package/dist/vm/stdlib/bytes.js +222 -0
- package/dist/vm/stdlib/collections.d.ts +7 -0
- package/dist/vm/stdlib/collections.js +188 -0
- package/dist/vm/stdlib/console.d.ts +6 -0
- package/dist/vm/stdlib/console.js +93 -0
- package/dist/vm/stdlib/date.d.ts +2 -0
- package/dist/vm/stdlib/date.js +123 -0
- package/dist/vm/stdlib/headers.d.ts +2 -0
- package/dist/vm/stdlib/headers.js +92 -0
- package/dist/vm/stdlib/iterator.d.ts +2 -0
- package/dist/vm/stdlib/iterator.js +30 -0
- package/dist/vm/stdlib/json.d.ts +3 -0
- package/dist/vm/stdlib/json.js +42 -0
- package/dist/vm/stdlib/math.d.ts +3 -0
- package/dist/vm/stdlib/math.js +73 -0
- package/dist/vm/stdlib/number.d.ts +3 -0
- package/dist/vm/stdlib/number.js +72 -0
- package/dist/vm/stdlib/object.d.ts +6 -0
- package/dist/vm/stdlib/object.js +201 -0
- package/dist/vm/stdlib/regexp.d.ts +8 -0
- package/dist/vm/stdlib/regexp.js +119 -0
- package/dist/vm/stdlib/string.d.ts +2 -0
- package/dist/vm/stdlib/string.js +272 -0
- package/dist/vm/stdlib/url.d.ts +25 -0
- package/dist/vm/stdlib/url.js +255 -0
- package/dist/vm/stdlib/value.d.ts +6 -0
- package/dist/vm/stdlib/value.js +32 -0
- package/dist/vm/stdlib/web.d.ts +5 -0
- package/dist/vm/stdlib/web.js +97 -0
- package/dist/vm/vm.d.ts +225 -0
- package/dist/vm/vm.js +2931 -0
- package/package.json +6 -3
|
@@ -0,0 +1,1477 @@
|
|
|
1
|
+
import { supportedSyntaxMessage } from "../runtime/model.js";
|
|
2
|
+
import { nodeId } from "../runtime/source.js";
|
|
3
|
+
import { Code, CodeBuilder, CodeSizeOverflow, hashOf } from "../code.js";
|
|
4
|
+
import { BinOp, CheckBaseKind, CodeFlags, CondKind, ImportKind, IterContext, Op, PutKind, UnaryOp, } from "../opcodes.js";
|
|
5
|
+
import { analyzeScopes, } from "./scope.js";
|
|
6
|
+
/** Each binary operator as the op and `a` operand that apply it. */
|
|
7
|
+
const BinaryOps = {
|
|
8
|
+
"+": [Op.ADD, 0],
|
|
9
|
+
"-": [Op.SUB, 0],
|
|
10
|
+
"*": [Op.MUL, 0],
|
|
11
|
+
"<": [Op.LT, 0],
|
|
12
|
+
"<=": [Op.LE, 0],
|
|
13
|
+
">": [Op.GT, 0],
|
|
14
|
+
">=": [Op.GE, 0],
|
|
15
|
+
"===": [Op.SEQ, 0],
|
|
16
|
+
"!==": [Op.SNE, 0],
|
|
17
|
+
in: [Op.IN, 0],
|
|
18
|
+
instanceof: [Op.INSTANCEOF, 0],
|
|
19
|
+
"/": [Op.BINOP, BinOp.DIV],
|
|
20
|
+
"%": [Op.BINOP, BinOp.MOD],
|
|
21
|
+
"**": [Op.BINOP, BinOp.EXP],
|
|
22
|
+
"&": [Op.BINOP, BinOp.BAND],
|
|
23
|
+
"|": [Op.BINOP, BinOp.BOR],
|
|
24
|
+
"^": [Op.BINOP, BinOp.BXOR],
|
|
25
|
+
"<<": [Op.BINOP, BinOp.SHL],
|
|
26
|
+
">>": [Op.BINOP, BinOp.SAR],
|
|
27
|
+
">>>": [Op.BINOP, BinOp.SHR],
|
|
28
|
+
"==": [Op.BINOP, BinOp.EQ],
|
|
29
|
+
"!=": [Op.BINOP, BinOp.NE],
|
|
30
|
+
};
|
|
31
|
+
const UnaryOps = {
|
|
32
|
+
"-": UnaryOp.NEG,
|
|
33
|
+
"+": UnaryOp.PLUS,
|
|
34
|
+
"~": UnaryOp.BNOT,
|
|
35
|
+
typeof: UnaryOp.TYPEOF,
|
|
36
|
+
void: UnaryOp.VOID,
|
|
37
|
+
};
|
|
38
|
+
/** The jump that skips the right operand of `||`, `&&`, `??` and their assignments. */
|
|
39
|
+
const logicalCond = (operator) => operator.startsWith("||")
|
|
40
|
+
? CondKind.TRUTHY_KEEP
|
|
41
|
+
: operator.startsWith("&&")
|
|
42
|
+
? CondKind.FALSY_KEEP
|
|
43
|
+
: CondKind.NOT_NULLISH_KEEP;
|
|
44
|
+
/** NamedEvaluation: whether `node` is an anonymous function definition, which takes the name it is assigned to. */
|
|
45
|
+
const isAnonymousFunction = (node) => node.type === "ArrowFunctionExpression" || (node.type === "FunctionExpression" && !node.id);
|
|
46
|
+
const idx = (node) => (node ? (nodeId(node)?.index ?? -1) : -1);
|
|
47
|
+
/** Bump when the compiler emits different bytecode for the same source; it is part of the snapshot build id. */
|
|
48
|
+
export const COMPILER_VERSION = 1;
|
|
49
|
+
const compileCache = new Map();
|
|
50
|
+
const MAX_CACHE_SIZE = 64;
|
|
51
|
+
/**
|
|
52
|
+
* Compiles a `Source` into an immutable `Code[]` array indexed by pre-order function index (`0` = program).
|
|
53
|
+
*/
|
|
54
|
+
export const compileSource = (source, options) => {
|
|
55
|
+
const internal = options?.internal ?? false;
|
|
56
|
+
const forceExtra = options?.forceExtra ?? false;
|
|
57
|
+
const cacheable = !internal && !forceExtra && !options?.noCache && source.id === "main";
|
|
58
|
+
const cached = cacheable ? compileCache.get(source.code) : undefined;
|
|
59
|
+
// Sites resolve against this Source's nodes.
|
|
60
|
+
if (cached !== undefined)
|
|
61
|
+
return cached.map((code) => code.withSource(source));
|
|
62
|
+
const analysis = analyzeScopes(source, internal);
|
|
63
|
+
const preliminary = analysis.functions.map((fnInfo) => compileFunction(source, { id: source.id, hash: "" }, analysis, fnInfo, forceExtra));
|
|
64
|
+
const finalRef = { id: source.id, hash: hashOf(preliminary) };
|
|
65
|
+
const codes = preliminary.map((code) => new Code({ ...code, source: finalRef }));
|
|
66
|
+
if (cacheable) {
|
|
67
|
+
if (compileCache.size >= MAX_CACHE_SIZE)
|
|
68
|
+
compileCache.delete(compileCache.keys().next().value);
|
|
69
|
+
compileCache.set(source.code, codes);
|
|
70
|
+
}
|
|
71
|
+
return codes;
|
|
72
|
+
};
|
|
73
|
+
const compileFunction = (source, sourceRef, analysis, fnInfo, forceExtra) => {
|
|
74
|
+
try {
|
|
75
|
+
return compileFunctionBody(source, sourceRef, analysis, fnInfo, forceExtra, false);
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
if (err instanceof CodeSizeOverflow) {
|
|
79
|
+
return compileFunctionBody(source, sourceRef, analysis, fnInfo, forceExtra, true);
|
|
80
|
+
}
|
|
81
|
+
throw err;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const compileFunctionBody = (source, sourceRef, analysis, fnInfo, forceExtra, wideJumps) => {
|
|
85
|
+
const isProgram = fnInfo.index === 0;
|
|
86
|
+
const internal = (fnInfo.flags & CodeFlags.INTERNAL) !== 0;
|
|
87
|
+
const builder = new CodeBuilder(sourceRef, source, fnInfo.index, fnInfo.name, fnInfo.flags, fnInfo.length, fnInfo.params.length, fnInfo.locals, [...fnInfo.localNames], Int32Array.from(fnInfo.upvalues.flatMap((u) => [u.from, u.index])), fnInfo.upvalues.map((u) => u.decl.name));
|
|
88
|
+
builder.forceExtra = forceExtra;
|
|
89
|
+
builder.wideJumps = wideJumps;
|
|
90
|
+
const controlStack = [];
|
|
91
|
+
let pendingLabels;
|
|
92
|
+
let retSlot = -1;
|
|
93
|
+
const allocSlot = (name) => {
|
|
94
|
+
builder.localNames.push(name);
|
|
95
|
+
return builder.locals++;
|
|
96
|
+
};
|
|
97
|
+
const emitThrowError = (site, node) => {
|
|
98
|
+
builder.emit(Op.THROW_ERROR, 0, builder.constant(site), idx(node), 0);
|
|
99
|
+
builder.unreachable();
|
|
100
|
+
};
|
|
101
|
+
const emitUnsupported = (kindName, node) => emitThrowError({
|
|
102
|
+
type: "SyntaxError",
|
|
103
|
+
message: `Syntax '${kindName}' is not supported. ${supportedSyntaxMessage}`,
|
|
104
|
+
kind: "UnsupportedSyntax",
|
|
105
|
+
suggestions: [supportedSyntaxMessage],
|
|
106
|
+
}, node);
|
|
107
|
+
// `super.x` parses inside object methods but is not supported. Code after the error is unreachable, so it emits nothing.
|
|
108
|
+
const unsupportedSuper = (node) => {
|
|
109
|
+
if (node.object.type !== "Super")
|
|
110
|
+
return false;
|
|
111
|
+
emitUnsupported("Super", node.object);
|
|
112
|
+
return true;
|
|
113
|
+
};
|
|
114
|
+
const putKind = (decl, isInit) => isInit ? PutKind.INIT : decl.mutable ? PutKind.ASSIGN : PutKind.CONST;
|
|
115
|
+
const emitLoadDecl = (decl, node) => builder.emit(decl.captured ? Op.GET_CELL : Op.GET_LOCAL, 0, decl.slot, idx(node), 1);
|
|
116
|
+
const emitStoreDecl = (decl, isInit, node) => builder.emit(decl.captured ? Op.PUT_CELL : Op.PUT_LOCAL, putKind(decl, isInit), decl.slot, idx(node), -1);
|
|
117
|
+
const emitLoadRef = (ref, node, isTypeof = false) => {
|
|
118
|
+
if (ref.kind === "local")
|
|
119
|
+
emitLoadDecl(ref.decl, node);
|
|
120
|
+
else if (ref.kind === "upval")
|
|
121
|
+
builder.emit(Op.GET_UPVAL, 0, ref.upvalIndex, idx(node), 1);
|
|
122
|
+
else if (internal && !isTypeof) {
|
|
123
|
+
// Internal code can't shadow `undefined`.
|
|
124
|
+
if (ref.name === "undefined")
|
|
125
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, idx(node), 1);
|
|
126
|
+
else
|
|
127
|
+
builder.emit(Op.GET_INTRINSIC, 0, builder.constant(ref.name), idx(node), 1);
|
|
128
|
+
}
|
|
129
|
+
else
|
|
130
|
+
builder.emit(Op.GET_GLOBAL, isTypeof ? 1 : 0, builder.constant(ref.name), idx(node), 1);
|
|
131
|
+
};
|
|
132
|
+
const emitStoreRef = (ref, isInit, node) => {
|
|
133
|
+
if (ref.kind === "local")
|
|
134
|
+
emitStoreDecl(ref.decl, isInit, node);
|
|
135
|
+
else if (ref.kind === "upval")
|
|
136
|
+
builder.emit(Op.PUT_UPVAL, putKind(ref.decl, isInit), ref.upvalIndex, idx(node), -1);
|
|
137
|
+
// Global assignment: checks if global exists (`Cannot assign to constant`) or unknown (`Unknown identifier`)
|
|
138
|
+
else
|
|
139
|
+
builder.emit(Op.GET_GLOBAL, 2, builder.constant(ref.name), idx(node), -1);
|
|
140
|
+
};
|
|
141
|
+
const emitScopePrologue = (scope, resetUncapturedLexicals) => {
|
|
142
|
+
for (const decl of scope.decls.values()) {
|
|
143
|
+
if (decl.kind === "this" || decl.kind === "arguments" || decl.kind === "param" || decl.kind === "self")
|
|
144
|
+
continue;
|
|
145
|
+
if (decl.captured)
|
|
146
|
+
builder.emit(Op.NEW_CELL, 0, decl.slot);
|
|
147
|
+
else if (resetUncapturedLexicals && (decl.kind === "let" || decl.kind === "const")) {
|
|
148
|
+
builder.emit(Op.HOLE_LOCAL, 0, decl.slot);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
for (const decl of scope.decls.values()) {
|
|
152
|
+
if (decl.kind !== "var")
|
|
153
|
+
continue;
|
|
154
|
+
if (decl.copyFromParam !== undefined)
|
|
155
|
+
emitLoadDecl(decl.copyFromParam);
|
|
156
|
+
else
|
|
157
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, -1, 1);
|
|
158
|
+
emitStoreDecl(decl, true);
|
|
159
|
+
}
|
|
160
|
+
emitHoistedFunctions(scope);
|
|
161
|
+
};
|
|
162
|
+
const emitHoistedFunctions = (scope) => {
|
|
163
|
+
for (const hoisted of scope.hoistedFunctions) {
|
|
164
|
+
if (hoisted.fnIndex < 0)
|
|
165
|
+
continue;
|
|
166
|
+
builder.emit(Op.CLOSURE, 0, hoisted.fnIndex, idx(hoisted.node), 1);
|
|
167
|
+
emitStoreDecl(hoisted.decl, true, hoisted.node);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const emitImports = (stmts) => {
|
|
171
|
+
for (const stmt of stmts) {
|
|
172
|
+
if (stmt.type !== "ImportDeclaration")
|
|
173
|
+
continue;
|
|
174
|
+
const specifier = String(stmt.source.value);
|
|
175
|
+
if (stmt.specifiers.length === 0) {
|
|
176
|
+
builder.emit(Op.IMPORT, ImportKind.SIDE_EFFECT, builder.constant({ specifier }), idx(stmt), 0);
|
|
177
|
+
}
|
|
178
|
+
for (const spec of stmt.specifiers) {
|
|
179
|
+
if (spec.type === "ImportSpecifier") {
|
|
180
|
+
const imported = spec.imported.type === "Identifier" ? spec.imported.name : String(spec.imported.value);
|
|
181
|
+
builder.emit(Op.IMPORT, ImportKind.NAMED, builder.constant({ specifier, imported }), idx(spec), 1);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
const kind = spec.type === "ImportDefaultSpecifier" ? ImportKind.DEFAULT : ImportKind.NAMESPACE;
|
|
185
|
+
builder.emit(Op.IMPORT, kind, builder.constant({ specifier }), idx(spec), 1);
|
|
186
|
+
}
|
|
187
|
+
emitStoreDecl(fnInfo.bodyScope.decls.get(spec.local.name), true, spec);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
// --- Iteration (fast paths inline, a custom iterator's calls out of line) ---
|
|
192
|
+
// Opens an iterator over the value on top into `itSlot`. The returned function emits the out-of-line path that
|
|
193
|
+
// calls a custom `[Symbol.iterator]` method.
|
|
194
|
+
const emitIterOpen = (context, itSlot, base, node) => {
|
|
195
|
+
const slow = builder.newLabel(base + 2);
|
|
196
|
+
const join = builder.newLabel(base + 1);
|
|
197
|
+
builder.emitJump(Op.ITER_OPEN, context, slow, idx(node), 0, 1);
|
|
198
|
+
builder.emitJump(Op.JUMP, 0, slow, idx(node), 0, null);
|
|
199
|
+
builder.mark(join);
|
|
200
|
+
builder.emit(Op.PUT_LOCAL, PutKind.INIT, itSlot, idx(node), -1);
|
|
201
|
+
return () => {
|
|
202
|
+
builder.mark(slow);
|
|
203
|
+
builder.emit(Op.CALL, 0, 0, idx(node), -1);
|
|
204
|
+
builder.emit(Op.ITER_INIT, context, 0, idx(node), 0);
|
|
205
|
+
builder.emitJump(Op.JUMP, 0, join, idx(node), 0, null);
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
// Pushes the next value, or jumps to `done`. The returned function emits the out-of-line path that calls a custom
|
|
209
|
+
// `next` method.
|
|
210
|
+
const emitIterStep = (itSlot, base, done, node, isAwait = false) => {
|
|
211
|
+
const got = builder.newLabel(base + 1);
|
|
212
|
+
const slow = builder.newLabel(base + 2);
|
|
213
|
+
builder.emitJump(Op.ITER_STEP, itSlot, done, idx(node), 0, 2);
|
|
214
|
+
builder.emitJump(Op.JUMP, 0, slow, idx(node), 0, null);
|
|
215
|
+
builder.mark(got);
|
|
216
|
+
return () => {
|
|
217
|
+
builder.mark(slow);
|
|
218
|
+
builder.emit(Op.CALL, 0, 0, idx(node), -1);
|
|
219
|
+
if (isAwait)
|
|
220
|
+
builder.emit(Op.AWAIT, 0, 0, idx(node), 0);
|
|
221
|
+
builder.emitJump(Op.ITER_RESULT, itSlot, done, idx(node), -1, 0);
|
|
222
|
+
builder.emitJump(Op.JUMP, 0, got, idx(node), 0, null);
|
|
223
|
+
};
|
|
224
|
+
};
|
|
225
|
+
// Appends what is left of the iterator to the array on top (at `base`). `beforeSlow` emits other out-of-line code.
|
|
226
|
+
const emitAppendRest = (itSlot, base, node, beforeSlow) => {
|
|
227
|
+
const loop = builder.newLabel(base);
|
|
228
|
+
const done = builder.newLabel(base);
|
|
229
|
+
builder.emitJump(Op.ITER_APPEND_ALL, itSlot, loop, idx(node), 0, 0);
|
|
230
|
+
builder.emitJump(Op.JUMP, 0, done, idx(node), 0, 0);
|
|
231
|
+
builder.mark(loop);
|
|
232
|
+
const slow = emitIterStep(itSlot, base, done, node);
|
|
233
|
+
builder.emit(Op.ARRAY_APPEND, 0, 0, idx(node), -1);
|
|
234
|
+
builder.emitJump(Op.JUMP, 0, loop, idx(node), 0, 0);
|
|
235
|
+
beforeSlow?.();
|
|
236
|
+
slow();
|
|
237
|
+
builder.mark(done);
|
|
238
|
+
};
|
|
239
|
+
const emitIteratorCloseNormal = (itSlot, iterAsync, node) => {
|
|
240
|
+
const skipLabel = builder.newLabel(builder.depth);
|
|
241
|
+
builder.emitJump(Op.ITER_CLOSE, itSlot, skipLabel, idx(node), 0, 2);
|
|
242
|
+
builder.emit(Op.CALL, 0, 0, idx(node), -1);
|
|
243
|
+
if (iterAsync)
|
|
244
|
+
builder.emit(Op.AWAIT, 0, 0, idx(node), 0);
|
|
245
|
+
builder.emit(Op.ITER_INIT, IterContext.CHECK_OBJECT, 0, idx(node), 0);
|
|
246
|
+
builder.emit(Op.POP, 0, 0, idx(node), -1);
|
|
247
|
+
builder.mark(skipLabel);
|
|
248
|
+
};
|
|
249
|
+
// The handler for a throw out of `ranges`: closes the iterator, ignoring an error from closing it, and rethrows.
|
|
250
|
+
const emitCloseOnThrow = (ranges, itSlot, errSlot, entryDepth, node, isAwait = false) => {
|
|
251
|
+
const catchLabel = builder.newLabel(entryDepth + 1);
|
|
252
|
+
builder.mark(catchLabel);
|
|
253
|
+
for (const [start, end] of ranges)
|
|
254
|
+
builder.addHandler(start, end, catchLabel.pc, entryDepth);
|
|
255
|
+
builder.emit(Op.PUT_LOCAL, PutKind.INIT, errSlot, idx(node), -1);
|
|
256
|
+
const rethrowLabel = builder.newLabel(entryDepth);
|
|
257
|
+
const swallowLabel = builder.newLabel(entryDepth + 1);
|
|
258
|
+
const closeStart = builder.pc;
|
|
259
|
+
builder.emitJump(Op.ITER_CLOSE, itSlot, rethrowLabel, idx(node), 0, 2);
|
|
260
|
+
builder.emit(Op.CALL, 0, 0, idx(node), -1);
|
|
261
|
+
if (isAwait)
|
|
262
|
+
builder.emit(Op.AWAIT, 0, 0, idx(node), 0);
|
|
263
|
+
builder.emit(Op.POP, 0, 0, idx(node), -1);
|
|
264
|
+
const closeEnd = builder.pc;
|
|
265
|
+
builder.mark(rethrowLabel);
|
|
266
|
+
builder.emit(Op.GET_LOCAL, 0, errSlot, idx(node), 1);
|
|
267
|
+
builder.emit(Op.THROW, 0, 0, idx(node), -1);
|
|
268
|
+
builder.mark(swallowLabel);
|
|
269
|
+
builder.emit(Op.POP, 0, 0, idx(node), -1);
|
|
270
|
+
builder.emitJump(Op.JUMP, 0, rethrowLabel, idx(node), 0, null);
|
|
271
|
+
builder.addHandler(closeStart, closeEnd, swallowLabel.pc, entryDepth);
|
|
272
|
+
};
|
|
273
|
+
// --- Exit Plan (return, break, continue across iterators and finally blocks; C1, C3, C4) ---
|
|
274
|
+
const findExitTarget = (kind, label) => {
|
|
275
|
+
for (let i = controlStack.length - 1; i >= 0; i--) {
|
|
276
|
+
const e = controlStack[i];
|
|
277
|
+
if (e.kind === "finally" || e.kind === "iterator")
|
|
278
|
+
continue;
|
|
279
|
+
if (kind === "break" && (label !== undefined ? e.labels.has(label) : e.kind === "loop" || e.kind === "switch"))
|
|
280
|
+
return e;
|
|
281
|
+
if (kind === "continue" && e.kind === "loop" && (label === undefined || e.labels.has(label)))
|
|
282
|
+
return e;
|
|
283
|
+
}
|
|
284
|
+
return null;
|
|
285
|
+
};
|
|
286
|
+
const exitPlan = (kind, target, node) => {
|
|
287
|
+
if (builder.depth < 0)
|
|
288
|
+
return;
|
|
289
|
+
const stop = target ? controlStack.indexOf(target) : -1;
|
|
290
|
+
const crossing = controlStack.slice(stop + 1).reverse();
|
|
291
|
+
const needsRet = kind === "return" &&
|
|
292
|
+
crossing.some((e) => e.kind === "finally" || e.kind === "iterator" || (e.kind === "loop" && e.iterSlot >= 0));
|
|
293
|
+
if (kind === "return" && !needsRet) {
|
|
294
|
+
// C1: RETURN discards the frame directly when nothing needs closing or routing
|
|
295
|
+
builder.emit(Op.RETURN, 0, 0, idx(node), -1);
|
|
296
|
+
builder.unreachable();
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
if (needsRet) {
|
|
300
|
+
if (retSlot === -1)
|
|
301
|
+
retSlot = allocSlot("%ret");
|
|
302
|
+
builder.emit(Op.PUT_LOCAL, PutKind.INIT, retSlot, idx(node), -1);
|
|
303
|
+
}
|
|
304
|
+
const popTo = (d) => {
|
|
305
|
+
const drop = builder.depth - d;
|
|
306
|
+
if (drop > 0)
|
|
307
|
+
builder.emit(Op.POPN, 0, drop, idx(node), -drop);
|
|
308
|
+
};
|
|
309
|
+
for (const e of crossing) {
|
|
310
|
+
popTo(e.entryDepth);
|
|
311
|
+
if (e.kind === "iterator")
|
|
312
|
+
emitIteratorCloseNormal(e.itSlot, false, node);
|
|
313
|
+
else if (e.kind === "loop" && e.iterSlot >= 0)
|
|
314
|
+
emitIteratorCloseNormal(e.iterSlot, e.iterAsync, node);
|
|
315
|
+
else if (e.kind === "finally") {
|
|
316
|
+
const key = `${kind}:${target ? stop : ""}`;
|
|
317
|
+
let exit = e.exits.get(key);
|
|
318
|
+
if (!exit) {
|
|
319
|
+
exit = { tag: e.exits.size + 2, kind, target };
|
|
320
|
+
e.exits.set(key, exit);
|
|
321
|
+
}
|
|
322
|
+
if (kind === "return") {
|
|
323
|
+
builder.emit(Op.GET_LOCAL, 0, retSlot, idx(node), 1);
|
|
324
|
+
builder.emit(Op.PUT_LOCAL, PutKind.INIT, e.valSlot, idx(node), -1);
|
|
325
|
+
}
|
|
326
|
+
builder.emitNumber(exit.tag, idx(node));
|
|
327
|
+
builder.emit(Op.PUT_LOCAL, PutKind.INIT, e.tagSlot, idx(node), -1);
|
|
328
|
+
builder.emitJump(Op.JUMP, 0, e.finallyLabel, idx(node), 0, null);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
if (kind === "return") {
|
|
333
|
+
popTo(0);
|
|
334
|
+
builder.emit(Op.GET_LOCAL, 0, retSlot, idx(node), 1);
|
|
335
|
+
builder.emit(Op.RETURN, 0, 0, idx(node), -1);
|
|
336
|
+
builder.unreachable();
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
if (target && target.kind !== "iterator" && target.kind !== "finally") {
|
|
340
|
+
popTo(target.entryDepth);
|
|
341
|
+
if (kind === "break" && target.kind === "loop" && target.iterSlot >= 0) {
|
|
342
|
+
emitIteratorCloseNormal(target.iterSlot, target.iterAsync, node);
|
|
343
|
+
}
|
|
344
|
+
const dst = kind === "break" ? target.breakLabel : target.continueLabel;
|
|
345
|
+
builder.emitJump(Op.JUMP, 0, dst, idx(node), 0, null);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
emitThrowError({ type: "TypeError", message: `Unexpected '${kind}' outside of a loop.` }, node);
|
|
349
|
+
};
|
|
350
|
+
// --- Pattern Compilation (value to bind is on top of stack: depth -> depth - 1) ---
|
|
351
|
+
const compilePattern = (pattern, isDecl, isInit, contextNode) => {
|
|
352
|
+
switch (pattern.type) {
|
|
353
|
+
case "Identifier":
|
|
354
|
+
emitStoreRef(analysis.refs.get(pattern), isInit, isDecl ? contextNode : pattern);
|
|
355
|
+
return;
|
|
356
|
+
case "MemberExpression": {
|
|
357
|
+
// Stack has [value]. Evaluate target object and key, then write [value].
|
|
358
|
+
const valSlot = allocSlot("%mval");
|
|
359
|
+
builder.emit(Op.PUT_LOCAL, PutKind.INIT, valSlot, idx(pattern), -1);
|
|
360
|
+
if (unsupportedSuper(pattern))
|
|
361
|
+
return;
|
|
362
|
+
compileExpression(pattern.object);
|
|
363
|
+
if (pattern.computed) {
|
|
364
|
+
compileExpression(pattern.property);
|
|
365
|
+
emitWritableKey(pattern);
|
|
366
|
+
builder.emit(Op.GET_LOCAL, 0, valSlot, idx(pattern), 1);
|
|
367
|
+
builder.emit(Op.PUT_ELEM, 0, 0, idx(pattern), -2);
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
const k = builder.constant(propertyName(pattern));
|
|
371
|
+
builder.emit(Op.CHECK_BASE, CheckBaseKind.WRITE_PROP, k, idx(pattern), 0);
|
|
372
|
+
builder.emit(Op.GET_LOCAL, 0, valSlot, idx(pattern), 1);
|
|
373
|
+
builder.emit(Op.PUT_PROP, 0, k, idx(pattern), -1);
|
|
374
|
+
}
|
|
375
|
+
builder.emit(Op.POP, 0, 0, idx(pattern), -1);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
case "AssignmentPattern": {
|
|
379
|
+
const definedLabel = builder.newLabel(builder.depth);
|
|
380
|
+
builder.emitJump(Op.JUMP_COND, CondKind.DEFINED, definedLabel, idx(pattern), 0, -1);
|
|
381
|
+
if (pattern.left.type === "Identifier")
|
|
382
|
+
compileNamedExpression(pattern.right, pattern.left.name);
|
|
383
|
+
else
|
|
384
|
+
compileExpression(pattern.right);
|
|
385
|
+
builder.mark(definedLabel);
|
|
386
|
+
compilePattern(pattern.left, isDecl, isInit, contextNode);
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
case "ObjectPattern":
|
|
390
|
+
return compileObjectPattern(pattern, isDecl, isInit);
|
|
391
|
+
case "ArrayPattern":
|
|
392
|
+
return compileArrayPattern(pattern, isDecl, isInit);
|
|
393
|
+
default:
|
|
394
|
+
emitThrowError({ type: "TypeError", message: `Unsupported binding pattern '${pattern.type}'.` }, pattern);
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
// A computed member's key as a write target: `[o, k] -> [o, key]`.
|
|
398
|
+
const emitWritableKey = (node) => {
|
|
399
|
+
builder.emit(Op.CHECK_BASE, CheckBaseKind.READ, 0, idx(node), 0);
|
|
400
|
+
builder.emit(Op.TO_KEY, 0, 0, idx(node.property), 0);
|
|
401
|
+
builder.emit(Op.CHECK_BASE, CheckBaseKind.WRITE_ELEM, 0, idx(node), 0);
|
|
402
|
+
};
|
|
403
|
+
const compileObjectPattern = (pattern, isDecl, isInit) => {
|
|
404
|
+
builder.emit(Op.CHECK_BASE, CheckBaseKind.DESTRUCTURE, 0, idx(pattern), 0);
|
|
405
|
+
const props = pattern.properties;
|
|
406
|
+
const lastProp = props.at(-1);
|
|
407
|
+
if (lastProp === undefined) {
|
|
408
|
+
builder.emit(Op.POP, 0, 0, idx(pattern), -1);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
if (lastProp.type !== "RestElement") {
|
|
412
|
+
props.forEach((prop, i) => {
|
|
413
|
+
if (prop.type === "RestElement")
|
|
414
|
+
return;
|
|
415
|
+
if (i < props.length - 1)
|
|
416
|
+
builder.emit(Op.DUP, 0, 0, idx(prop), 1);
|
|
417
|
+
if (prop.computed) {
|
|
418
|
+
compileExpression(prop.key);
|
|
419
|
+
builder.emit(Op.TO_KEY, 0, 0, idx(prop.key), 0);
|
|
420
|
+
builder.emit(Op.GET_ELEM, 2, 0, idx(prop), -1);
|
|
421
|
+
}
|
|
422
|
+
else
|
|
423
|
+
builder.emit(Op.GET_PROP, 2, builder.constant(keyName(prop.key)), idx(prop), 0);
|
|
424
|
+
compilePattern(prop.value, isDecl, isInit, prop);
|
|
425
|
+
});
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
// ObjectPattern with RestElement at the end: keep `[src, k1, ..., kn]` on the stack for `COPY_REST n`
|
|
429
|
+
const count = props.length - 1;
|
|
430
|
+
props.slice(0, count).forEach((prop, i) => {
|
|
431
|
+
if (prop.type === "RestElement")
|
|
432
|
+
return;
|
|
433
|
+
if (prop.computed) {
|
|
434
|
+
compileExpression(prop.key);
|
|
435
|
+
builder.emit(Op.TO_KEY, 0, 0, idx(prop.key), 0);
|
|
436
|
+
}
|
|
437
|
+
else
|
|
438
|
+
builder.emitString(keyName(prop.key), idx(prop.key));
|
|
439
|
+
// Stack is `[src, k1, ..., k_{i+1}]`. Pick `src` (at offset i + 1) and `k_{i+1}` (at offset 1 after picking src).
|
|
440
|
+
builder.emit(Op.PICK, i + 1, 0, idx(prop), 1);
|
|
441
|
+
builder.emit(Op.PICK, 1, 0, idx(prop), 1);
|
|
442
|
+
builder.emit(Op.GET_ELEM, 2, 0, idx(prop), -1);
|
|
443
|
+
compilePattern(prop.value, isDecl, isInit, prop);
|
|
444
|
+
});
|
|
445
|
+
builder.emit(Op.COPY_REST, 0, count, idx(lastProp), -count);
|
|
446
|
+
compilePattern(lastProp.argument, isDecl, isInit, lastProp);
|
|
447
|
+
};
|
|
448
|
+
const compileArrayPattern = (pattern, isDecl, isInit) => {
|
|
449
|
+
const itSlot = allocSlot("%it");
|
|
450
|
+
const errSlot = allocSlot("%e");
|
|
451
|
+
const entryDepth = builder.depth - 1;
|
|
452
|
+
const openSlow = emitIterOpen(IterContext.DESTRUCTURE, itSlot, entryDepth, pattern);
|
|
453
|
+
controlStack.push({ kind: "iterator", itSlot, entryDepth });
|
|
454
|
+
const protectedRanges = [];
|
|
455
|
+
const bind = (target, node) => {
|
|
456
|
+
const start = builder.pc;
|
|
457
|
+
compilePattern(target, isDecl, isInit, node);
|
|
458
|
+
protectedRanges.push([start, builder.pc]);
|
|
459
|
+
};
|
|
460
|
+
for (const el of pattern.elements) {
|
|
461
|
+
if (el?.type === "RestElement") {
|
|
462
|
+
// Drain remaining elements into a new array, then bind
|
|
463
|
+
builder.emit(Op.NEW_ARRAY, 0, 0, idx(el), 1);
|
|
464
|
+
emitAppendRest(itSlot, entryDepth + 1, el);
|
|
465
|
+
bind(el.argument, el);
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
const node = el ?? pattern;
|
|
469
|
+
const doneLabel = builder.newLabel(entryDepth);
|
|
470
|
+
const afterLabel = builder.newLabel(entryDepth);
|
|
471
|
+
const slow = emitIterStep(itSlot, entryDepth, doneLabel, node);
|
|
472
|
+
// Elision: step iterator and discard value
|
|
473
|
+
if (el === null)
|
|
474
|
+
builder.emit(Op.POP, 0, 0, idx(pattern), -1);
|
|
475
|
+
else
|
|
476
|
+
bind(el, el);
|
|
477
|
+
builder.emitJump(Op.JUMP, 0, afterLabel, idx(node), 0, 0);
|
|
478
|
+
slow();
|
|
479
|
+
builder.mark(doneLabel);
|
|
480
|
+
if (el !== null) {
|
|
481
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, idx(el), 1);
|
|
482
|
+
bind(el, el);
|
|
483
|
+
}
|
|
484
|
+
builder.mark(afterLabel);
|
|
485
|
+
}
|
|
486
|
+
controlStack.pop();
|
|
487
|
+
emitIteratorCloseNormal(itSlot, false, pattern);
|
|
488
|
+
const endLabel = builder.newLabel(entryDepth);
|
|
489
|
+
builder.emitJump(Op.JUMP, 0, endLabel, idx(pattern), 0, 0);
|
|
490
|
+
openSlow();
|
|
491
|
+
emitCloseOnThrow(protectedRanges, itSlot, errSlot, entryDepth, pattern);
|
|
492
|
+
builder.mark(endLabel);
|
|
493
|
+
};
|
|
494
|
+
// --- Statements ---
|
|
495
|
+
const compileStatement = (stmt) => {
|
|
496
|
+
const labels = pendingLabels;
|
|
497
|
+
pendingLabels = undefined;
|
|
498
|
+
if (builder.depth < 0)
|
|
499
|
+
return;
|
|
500
|
+
const compile = statements[stmt.type];
|
|
501
|
+
if (compile)
|
|
502
|
+
compile(stmt, labels);
|
|
503
|
+
else
|
|
504
|
+
emitUnsupported(stmt.type, stmt);
|
|
505
|
+
};
|
|
506
|
+
const withStatementLabels = (labels, body) => {
|
|
507
|
+
if (!labels || labels.size === 0)
|
|
508
|
+
return body();
|
|
509
|
+
const breakLabel = builder.newLabel(builder.depth);
|
|
510
|
+
controlStack.push({ kind: "label", labels, breakLabel, entryDepth: builder.depth });
|
|
511
|
+
body();
|
|
512
|
+
controlStack.pop();
|
|
513
|
+
builder.mark(breakLabel);
|
|
514
|
+
};
|
|
515
|
+
const pushLoop = (labels, breakLabel, continueLabel, entryDepth, iterSlot = -1, iterAsync = false) => controlStack.push({
|
|
516
|
+
kind: "loop",
|
|
517
|
+
labels: labels ?? new Set(),
|
|
518
|
+
breakLabel,
|
|
519
|
+
continueLabel,
|
|
520
|
+
entryDepth,
|
|
521
|
+
iterSlot,
|
|
522
|
+
iterAsync,
|
|
523
|
+
});
|
|
524
|
+
const compileVariableDeclaration = (node) => {
|
|
525
|
+
const isVar = node.kind === "var";
|
|
526
|
+
for (const decl of node.declarations) {
|
|
527
|
+
if (isVar && !decl.init && decl.id.type === "Identifier")
|
|
528
|
+
continue;
|
|
529
|
+
if (!decl.init)
|
|
530
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, idx(decl), 1);
|
|
531
|
+
else if (decl.id.type === "Identifier")
|
|
532
|
+
compileNamedExpression(decl.init, decl.id.name);
|
|
533
|
+
else
|
|
534
|
+
compileExpression(decl.init);
|
|
535
|
+
compilePattern(decl.id, !isVar, !isVar, decl);
|
|
536
|
+
}
|
|
537
|
+
};
|
|
538
|
+
const compileBlockStatement = (node, labels) => withStatementLabels(labels, () => {
|
|
539
|
+
const scope = analysis.scopeOfNode.get(node);
|
|
540
|
+
if (scope && (scope.decls.size > 0 || scope.hoistedFunctions.length > 0))
|
|
541
|
+
emitScopePrologue(scope, true);
|
|
542
|
+
for (const s of node.body)
|
|
543
|
+
compileStatement(s);
|
|
544
|
+
});
|
|
545
|
+
const compileIfStatement = (node, labels) => withStatementLabels(labels, () => {
|
|
546
|
+
const elseLabel = builder.newLabel(builder.depth);
|
|
547
|
+
const endLabel = builder.newLabel(builder.depth);
|
|
548
|
+
compileExpression(node.test);
|
|
549
|
+
builder.emitJump(Op.JUMP_IF_FALSE, 0, node.alternate ? elseLabel : endLabel, idx(node), -1, -1);
|
|
550
|
+
compileStatement(node.consequent);
|
|
551
|
+
if (node.alternate) {
|
|
552
|
+
builder.emitJump(Op.JUMP, 0, endLabel, idx(node), 0, null);
|
|
553
|
+
builder.mark(elseLabel);
|
|
554
|
+
compileStatement(node.alternate);
|
|
555
|
+
}
|
|
556
|
+
builder.mark(endLabel);
|
|
557
|
+
});
|
|
558
|
+
const compileWhileStatement = (node, labels) => {
|
|
559
|
+
const entryDepth = builder.depth;
|
|
560
|
+
const testLabel = builder.newLabel(entryDepth);
|
|
561
|
+
const continueLabel = builder.newLabel(entryDepth);
|
|
562
|
+
const breakLabel = builder.newLabel(entryDepth);
|
|
563
|
+
builder.mark(testLabel);
|
|
564
|
+
compileExpression(node.test);
|
|
565
|
+
builder.emitJump(Op.JUMP_IF_FALSE, 0, breakLabel, idx(node), -1, -1);
|
|
566
|
+
pushLoop(labels, breakLabel, continueLabel, entryDepth);
|
|
567
|
+
compileStatement(node.body);
|
|
568
|
+
controlStack.pop();
|
|
569
|
+
builder.mark(continueLabel);
|
|
570
|
+
builder.emitJump(Op.LOOP, 0, testLabel, idx(node), 0, null);
|
|
571
|
+
builder.mark(breakLabel);
|
|
572
|
+
};
|
|
573
|
+
const compileDoWhileStatement = (node, labels) => {
|
|
574
|
+
const entryDepth = builder.depth;
|
|
575
|
+
const bodyLabel = builder.newLabel(entryDepth);
|
|
576
|
+
const continueLabel = builder.newLabel(entryDepth);
|
|
577
|
+
const breakLabel = builder.newLabel(entryDepth);
|
|
578
|
+
builder.mark(bodyLabel);
|
|
579
|
+
pushLoop(labels, breakLabel, continueLabel, entryDepth);
|
|
580
|
+
compileStatement(node.body);
|
|
581
|
+
controlStack.pop();
|
|
582
|
+
builder.mark(continueLabel);
|
|
583
|
+
compileExpression(node.test);
|
|
584
|
+
builder.emitJump(Op.JUMP_IF_FALSE, 0, breakLabel, idx(node), -1, -1);
|
|
585
|
+
builder.emitJump(Op.LOOP, 0, bodyLabel, idx(node), 0, null);
|
|
586
|
+
builder.mark(breakLabel);
|
|
587
|
+
};
|
|
588
|
+
const compileForStatement = (node, labels) => {
|
|
589
|
+
const entryDepth = builder.depth;
|
|
590
|
+
const forScope = analysis.scopeOfNode.get(node);
|
|
591
|
+
emitScopePrologue(forScope, true);
|
|
592
|
+
if (node.init?.type === "VariableDeclaration")
|
|
593
|
+
compileVariableDeclaration(node.init);
|
|
594
|
+
else if (node.init)
|
|
595
|
+
compileEffect(node.init);
|
|
596
|
+
// Each iteration gets fresh cells for the bindings its closures capture.
|
|
597
|
+
const copyCells = () => {
|
|
598
|
+
for (const d of forScope.decls.values())
|
|
599
|
+
if (d.captured)
|
|
600
|
+
builder.emit(Op.COPY_CELL, 0, d.slot, idx(node), 0);
|
|
601
|
+
};
|
|
602
|
+
copyCells();
|
|
603
|
+
const testLabel = builder.newLabel(entryDepth);
|
|
604
|
+
const continueLabel = builder.newLabel(entryDepth);
|
|
605
|
+
const breakLabel = builder.newLabel(entryDepth);
|
|
606
|
+
builder.mark(testLabel);
|
|
607
|
+
if (node.test) {
|
|
608
|
+
compileExpression(node.test);
|
|
609
|
+
builder.emitJump(Op.JUMP_IF_FALSE, 0, breakLabel, idx(node), -1, -1);
|
|
610
|
+
}
|
|
611
|
+
pushLoop(labels, breakLabel, continueLabel, entryDepth);
|
|
612
|
+
compileStatement(node.body);
|
|
613
|
+
controlStack.pop();
|
|
614
|
+
builder.mark(continueLabel);
|
|
615
|
+
copyCells();
|
|
616
|
+
if (node.update)
|
|
617
|
+
compileEffect(node.update);
|
|
618
|
+
builder.emitJump(Op.LOOP, 0, testLabel, idx(node), 0, null);
|
|
619
|
+
builder.mark(breakLabel);
|
|
620
|
+
};
|
|
621
|
+
// The binding a for...in or for...of assigns each value to, inside the loop's own scope when it is lexical.
|
|
622
|
+
const compileLoopBinding = (node) => {
|
|
623
|
+
if (node.left.type !== "VariableDeclaration")
|
|
624
|
+
return compilePattern(node.left, false, false, node.left);
|
|
625
|
+
const lexical = node.left.kind !== "var";
|
|
626
|
+
if (lexical)
|
|
627
|
+
emitScopePrologue(analysis.scopeOfNode.get(node), true);
|
|
628
|
+
compilePattern(node.left.declarations[0].id, lexical, lexical, node.left);
|
|
629
|
+
};
|
|
630
|
+
const loopScopePrologue = (node) => {
|
|
631
|
+
if (node.left.type === "VariableDeclaration" && node.left.kind !== "var") {
|
|
632
|
+
emitScopePrologue(analysis.scopeOfNode.get(node), true);
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
const compileForInStatement = (node, labels) => {
|
|
636
|
+
const entryDepth = builder.depth;
|
|
637
|
+
loopScopePrologue(node);
|
|
638
|
+
compileExpression(node.right);
|
|
639
|
+
const itSlot = allocSlot("%it");
|
|
640
|
+
builder.emit(Op.FOR_IN, 0, 0, idx(node.right), 0);
|
|
641
|
+
builder.emit(Op.PUT_LOCAL, PutKind.INIT, itSlot, idx(node), -1);
|
|
642
|
+
const stepLabel = builder.newLabel(entryDepth);
|
|
643
|
+
const continueLabel = builder.newLabel(entryDepth);
|
|
644
|
+
const breakLabel = builder.newLabel(entryDepth);
|
|
645
|
+
builder.mark(stepLabel);
|
|
646
|
+
const stepSlow = emitIterStep(itSlot, entryDepth, breakLabel, node);
|
|
647
|
+
compileLoopBinding(node);
|
|
648
|
+
pushLoop(labels, breakLabel, continueLabel, entryDepth);
|
|
649
|
+
compileStatement(node.body);
|
|
650
|
+
controlStack.pop();
|
|
651
|
+
builder.mark(continueLabel);
|
|
652
|
+
builder.emitJump(Op.LOOP, 0, stepLabel, idx(node), 0, null);
|
|
653
|
+
stepSlow();
|
|
654
|
+
builder.mark(breakLabel);
|
|
655
|
+
};
|
|
656
|
+
const compileForOfStatement = (node, labels) => {
|
|
657
|
+
const isAwait = node.await;
|
|
658
|
+
const entryDepth = builder.depth;
|
|
659
|
+
loopScopePrologue(node);
|
|
660
|
+
compileExpression(node.right);
|
|
661
|
+
const itSlot = allocSlot("%it");
|
|
662
|
+
const errSlot = allocSlot("%e");
|
|
663
|
+
const openSlow = emitIterOpen(isAwait ? IterContext.FOR_AWAIT : IterContext.FOR_OF, itSlot, entryDepth, node);
|
|
664
|
+
const stepLabel = builder.newLabel(entryDepth);
|
|
665
|
+
const continueLabel = builder.newLabel(entryDepth);
|
|
666
|
+
const breakLabel = builder.newLabel(entryDepth);
|
|
667
|
+
builder.mark(stepLabel);
|
|
668
|
+
const stepSlow = emitIterStep(itSlot, entryDepth, breakLabel, node, isAwait);
|
|
669
|
+
// C4: iterSlot belongs to the loop entry itself (`break` and `return` close it, `continue` does not)
|
|
670
|
+
pushLoop(labels, breakLabel, continueLabel, entryDepth, itSlot, isAwait);
|
|
671
|
+
const bodyStart = builder.pc;
|
|
672
|
+
compileLoopBinding(node);
|
|
673
|
+
compileStatement(node.body);
|
|
674
|
+
controlStack.pop();
|
|
675
|
+
builder.mark(continueLabel);
|
|
676
|
+
builder.emitJump(Op.LOOP, 0, stepLabel, idx(node), 0, null);
|
|
677
|
+
const bodyEnd = builder.pc;
|
|
678
|
+
openSlow();
|
|
679
|
+
stepSlow();
|
|
680
|
+
emitCloseOnThrow([[bodyStart, bodyEnd]], itSlot, errSlot, entryDepth, node, isAwait);
|
|
681
|
+
builder.mark(breakLabel);
|
|
682
|
+
};
|
|
683
|
+
const compileSwitchStatement = (node, labels) => {
|
|
684
|
+
const entryDepth = builder.depth;
|
|
685
|
+
compileExpression(node.discriminant);
|
|
686
|
+
const discSlot = allocSlot("%disc");
|
|
687
|
+
builder.emit(Op.PUT_LOCAL, PutKind.INIT, discSlot, idx(node), -1);
|
|
688
|
+
emitScopePrologue(analysis.scopeOfNode.get(node), true);
|
|
689
|
+
const breakLabel = builder.newLabel(entryDepth);
|
|
690
|
+
const caseLabels = node.cases.map(() => builder.newLabel(entryDepth));
|
|
691
|
+
let defaultLabel;
|
|
692
|
+
node.cases.forEach((clause, i) => {
|
|
693
|
+
if (!clause.test) {
|
|
694
|
+
defaultLabel = caseLabels[i];
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
builder.emit(Op.GET_LOCAL, 0, discSlot, idx(clause), 1);
|
|
698
|
+
compileExpression(clause.test);
|
|
699
|
+
builder.emit(Op.SEQ, 0, 0, idx(clause), -1);
|
|
700
|
+
builder.emitJump(Op.JUMP_IF_TRUE, 0, caseLabels[i], idx(clause), -1, -1);
|
|
701
|
+
});
|
|
702
|
+
builder.emitJump(Op.JUMP, 0, defaultLabel ?? breakLabel, idx(node), 0, 0);
|
|
703
|
+
controlStack.push({ kind: "switch", labels: labels ?? new Set(), breakLabel, entryDepth });
|
|
704
|
+
node.cases.forEach((clause, i) => {
|
|
705
|
+
builder.mark(caseLabels[i]);
|
|
706
|
+
for (const s of clause.consequent)
|
|
707
|
+
compileStatement(s);
|
|
708
|
+
});
|
|
709
|
+
controlStack.pop();
|
|
710
|
+
builder.mark(breakLabel);
|
|
711
|
+
};
|
|
712
|
+
const compileLabeledStatement = (node, existingLabels) => {
|
|
713
|
+
const labels = existingLabels ?? new Set();
|
|
714
|
+
let cur = node;
|
|
715
|
+
for (; cur.type === "LabeledStatement"; cur = cur.body)
|
|
716
|
+
labels.add(cur.label.name);
|
|
717
|
+
pendingLabels = labels;
|
|
718
|
+
compileStatement(cur);
|
|
719
|
+
};
|
|
720
|
+
const compileTryCatch = (block, handler, parentNode) => {
|
|
721
|
+
const entryDepth = builder.depth;
|
|
722
|
+
const catchLabel = builder.newLabel(entryDepth + 1);
|
|
723
|
+
const endLabel = builder.newLabel(entryDepth);
|
|
724
|
+
const tryStart = builder.pc;
|
|
725
|
+
compileBlockStatement(block);
|
|
726
|
+
const tryEnd = builder.pc;
|
|
727
|
+
builder.emitJump(Op.JUMP, 0, endLabel, idx(parentNode), 0, null);
|
|
728
|
+
builder.mark(catchLabel);
|
|
729
|
+
builder.addHandler(tryStart, tryEnd, catchLabel.pc, entryDepth);
|
|
730
|
+
emitScopePrologue(analysis.scopeOfNode.get(handler), true);
|
|
731
|
+
if (handler.param)
|
|
732
|
+
compilePattern(handler.param, true, true, handler);
|
|
733
|
+
else
|
|
734
|
+
builder.emit(Op.POP, 0, 0, idx(handler), -1);
|
|
735
|
+
compileBlockStatement(handler.body);
|
|
736
|
+
builder.mark(endLabel);
|
|
737
|
+
};
|
|
738
|
+
const compileTryStatement = (node, labels) => withStatementLabels(labels, () => {
|
|
739
|
+
if (!node.finalizer)
|
|
740
|
+
return compileTryCatch(node.block, node.handler, node);
|
|
741
|
+
// try/[catch]/finally with static tag routing (§3.5, C3)
|
|
742
|
+
const entryDepth = builder.depth;
|
|
743
|
+
const level = builder.reserveFinally();
|
|
744
|
+
const tagSlot = allocSlot(`%ftag${level}`);
|
|
745
|
+
const valSlot = allocSlot(`%fval${level}`);
|
|
746
|
+
const finallyLabel = builder.newLabel(entryDepth);
|
|
747
|
+
const afterFinallyLabel = builder.newLabel(entryDepth);
|
|
748
|
+
const finallyThrowLabel = builder.newLabel(entryDepth + 1);
|
|
749
|
+
const finallyEntry = { kind: "finally", tagSlot, valSlot, finallyLabel, entryDepth, exits: new Map() };
|
|
750
|
+
controlStack.push(finallyEntry);
|
|
751
|
+
const tryStart = builder.pc;
|
|
752
|
+
if (node.handler)
|
|
753
|
+
compileTryCatch(node.block, node.handler, node);
|
|
754
|
+
else
|
|
755
|
+
compileBlockStatement(node.block);
|
|
756
|
+
const tryEnd = builder.pc;
|
|
757
|
+
builder.emitNumber(0, idx(node));
|
|
758
|
+
builder.emit(Op.PUT_LOCAL, PutKind.INIT, tagSlot, idx(node), -1);
|
|
759
|
+
builder.emitJump(Op.JUMP, 0, finallyLabel, idx(node), 0, null);
|
|
760
|
+
// Throw out of try/catch -> tag = 1, val = e, fall into finally
|
|
761
|
+
builder.mark(finallyThrowLabel);
|
|
762
|
+
builder.addHandler(tryStart, tryEnd, finallyThrowLabel.pc, entryDepth);
|
|
763
|
+
builder.emit(Op.PUT_LOCAL, PutKind.INIT, valSlot, idx(node), -1);
|
|
764
|
+
builder.emitNumber(1, idx(node));
|
|
765
|
+
builder.emit(Op.PUT_LOCAL, PutKind.INIT, tagSlot, idx(node), -1);
|
|
766
|
+
controlStack.pop();
|
|
767
|
+
// Finally body at `entryDepth`
|
|
768
|
+
builder.mark(finallyLabel);
|
|
769
|
+
compileBlockStatement(node.finalizer);
|
|
770
|
+
const targets = new Array(finallyEntry.exits.size + 2).fill(-1);
|
|
771
|
+
if (builder.depth >= 0) {
|
|
772
|
+
builder.emit(Op.END_FINALLY, 0, level, idx(node), 0);
|
|
773
|
+
builder.unreachable();
|
|
774
|
+
for (const exit of finallyEntry.exits.values()) {
|
|
775
|
+
targets[exit.tag] = builder.pc;
|
|
776
|
+
builder.setDepth(entryDepth);
|
|
777
|
+
if (exit.kind === "return")
|
|
778
|
+
builder.emit(Op.GET_LOCAL, 0, valSlot, idx(node), 1);
|
|
779
|
+
exitPlan(exit.kind, exit.target, node);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
builder.mark(afterFinallyLabel);
|
|
783
|
+
targets[0] = afterFinallyLabel.pc;
|
|
784
|
+
builder.setFinally(level, tagSlot, valSlot, entryDepth, targets);
|
|
785
|
+
});
|
|
786
|
+
// --- Expressions (depth -> depth + 1, or `compileEffect` depth -> depth) ---
|
|
787
|
+
const compileEffect = (expr) => {
|
|
788
|
+
if (expr.type === "AssignmentExpression" && expr.left.type === "Identifier" && !isLogicalAssignment(expr)) {
|
|
789
|
+
return compileAssignmentExpression(expr, true);
|
|
790
|
+
}
|
|
791
|
+
if (expr.type === "UpdateExpression" && expr.argument.type === "Identifier") {
|
|
792
|
+
const ref = analysis.refs.get(expr.argument);
|
|
793
|
+
emitLoadRef(ref, expr.argument);
|
|
794
|
+
builder.emit(Op.INC, expr.operator === "++" ? 0 : 1, 0, idx(expr), 0);
|
|
795
|
+
emitStoreRef(ref, false, expr.argument);
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
compileExpression(expr);
|
|
799
|
+
builder.emit(Op.POP, 0, 0, idx(expr), -1);
|
|
800
|
+
};
|
|
801
|
+
const compileNamedExpression = (expr, targetName) => {
|
|
802
|
+
compileExpression(expr);
|
|
803
|
+
if (isAnonymousFunction(expr))
|
|
804
|
+
builder.emit(Op.SET_NAME, 0, builder.constant(targetName), idx(expr), 0);
|
|
805
|
+
};
|
|
806
|
+
const compileExpression = (expr, chainShortCircuit) => {
|
|
807
|
+
const compile = expressions[expr.type];
|
|
808
|
+
if (compile)
|
|
809
|
+
compile(expr, chainShortCircuit);
|
|
810
|
+
else
|
|
811
|
+
emitUnsupported(expr.type, expr);
|
|
812
|
+
};
|
|
813
|
+
const compileLiteral = (node) => {
|
|
814
|
+
const v = node.value;
|
|
815
|
+
if (node.regex) {
|
|
816
|
+
const k = builder.constant({ pattern: node.regex.pattern, flags: node.regex.flags });
|
|
817
|
+
builder.emit(Op.REGEXP, 0, k, idx(node), 1);
|
|
818
|
+
}
|
|
819
|
+
else if (typeof v === "bigint")
|
|
820
|
+
emitThrowError({ type: "TypeError", message: "BigInt literals are not supported." }, node);
|
|
821
|
+
else if (v === null)
|
|
822
|
+
builder.emit(Op.PUSH_NULL, 0, 0, idx(node), 1);
|
|
823
|
+
else if (typeof v === "boolean")
|
|
824
|
+
builder.emit(Op.PUSH_BOOL, v ? 1 : 0, 0, idx(node), 1);
|
|
825
|
+
else if (typeof v === "number")
|
|
826
|
+
builder.emitNumber(v, idx(node));
|
|
827
|
+
else
|
|
828
|
+
builder.emitString(v, idx(node));
|
|
829
|
+
};
|
|
830
|
+
// Stack has `[arr]`: appends every value the spread argument iterates to it.
|
|
831
|
+
const compileSpreadAppend = (spreadNode) => {
|
|
832
|
+
const baseDepth = builder.depth;
|
|
833
|
+
const itSlot = allocSlot("%it");
|
|
834
|
+
compileExpression(spreadNode.argument);
|
|
835
|
+
const openSlow = emitIterOpen(IterContext.SPREAD, itSlot, baseDepth, spreadNode);
|
|
836
|
+
emitAppendRest(itSlot, baseDepth, spreadNode, openSlow);
|
|
837
|
+
};
|
|
838
|
+
const compileArrayExpression = (node) => {
|
|
839
|
+
const elements = node.elements;
|
|
840
|
+
if (!elements.some((el) => el === null || el.type === "SpreadElement")) {
|
|
841
|
+
for (const el of elements)
|
|
842
|
+
compileExpression(el);
|
|
843
|
+
builder.emit(Op.NEW_ARRAY, 0, elements.length, idx(node), 1 - elements.length);
|
|
844
|
+
return;
|
|
845
|
+
}
|
|
846
|
+
builder.emit(Op.NEW_ARRAY, 0, 0, idx(node), 1);
|
|
847
|
+
for (const el of elements) {
|
|
848
|
+
if (el === null)
|
|
849
|
+
builder.emit(Op.ARRAY_APPEND, 1, 0, idx(node), 0);
|
|
850
|
+
else if (el.type === "SpreadElement")
|
|
851
|
+
compileSpreadAppend(el);
|
|
852
|
+
else {
|
|
853
|
+
compileExpression(el);
|
|
854
|
+
builder.emit(Op.ARRAY_APPEND, 0, 0, idx(node), -1);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
// Pushes call arguments, or one array of them when one is spread (or there are too many for an operand): true then.
|
|
859
|
+
const emitArguments = (args, node) => {
|
|
860
|
+
if (!args.some((a) => a.type === "SpreadElement") && args.length <= 254) {
|
|
861
|
+
for (const arg of args)
|
|
862
|
+
compileExpression(arg);
|
|
863
|
+
return false;
|
|
864
|
+
}
|
|
865
|
+
builder.emit(Op.NEW_ARRAY, 0, 0, idx(node), 1);
|
|
866
|
+
for (const arg of args) {
|
|
867
|
+
if (arg.type === "SpreadElement")
|
|
868
|
+
compileSpreadAppend(arg);
|
|
869
|
+
else {
|
|
870
|
+
compileExpression(arg);
|
|
871
|
+
builder.emit(Op.ARRAY_APPEND, 0, 0, idx(arg), -1);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
return true;
|
|
875
|
+
};
|
|
876
|
+
const compileObjectExpression = (node) => {
|
|
877
|
+
builder.emit(Op.NEW_OBJECT, 0, 0, idx(node), 1);
|
|
878
|
+
for (const prop of node.properties) {
|
|
879
|
+
if (prop.type === "SpreadElement") {
|
|
880
|
+
compileExpression(prop.argument);
|
|
881
|
+
builder.emit(Op.COPY_DATA_PROPS, 0, 0, idx(prop), -1);
|
|
882
|
+
}
|
|
883
|
+
else if (prop.kind !== "init") {
|
|
884
|
+
emitThrowError({ type: "TypeError", message: "Only init object properties are supported." }, prop);
|
|
885
|
+
}
|
|
886
|
+
else if (prop.computed) {
|
|
887
|
+
compileExpression(prop.key);
|
|
888
|
+
builder.emit(Op.TO_KEY, 0, 0, idx(prop.key), 0);
|
|
889
|
+
compileExpression(prop.value);
|
|
890
|
+
if (isAnonymousFunction(prop.value))
|
|
891
|
+
builder.emit(Op.SET_NAME, 1, 0, idx(prop.value), 0);
|
|
892
|
+
builder.emit(Op.DEFINE_FIELD, 1, 0, idx(prop), -2);
|
|
893
|
+
}
|
|
894
|
+
else {
|
|
895
|
+
const bigint = prop.key.type === "Literal" && typeof prop.key.value === "bigint";
|
|
896
|
+
if (bigint)
|
|
897
|
+
emitThrowError({ type: "TypeError", message: "BigInt literals are not supported." }, prop.key);
|
|
898
|
+
const name = bigint ? "" : keyName(prop.key);
|
|
899
|
+
compileNamedExpression(prop.value, name);
|
|
900
|
+
builder.emit(Op.DEFINE_FIELD, 0, builder.constant(name), idx(prop), -1);
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
const compileTemplateLiteral = (node) => {
|
|
905
|
+
const quasis = node.quasis;
|
|
906
|
+
const exprs = node.expressions;
|
|
907
|
+
// Untagged templates always have cooked strings: an invalid escape is a syntax error.
|
|
908
|
+
if (exprs.length === 0)
|
|
909
|
+
return builder.emitString(quasis[0].value.cooked, idx(node));
|
|
910
|
+
let parts = 0;
|
|
911
|
+
quasis.forEach((q, i) => {
|
|
912
|
+
if (q.value.cooked.length > 0 || parts === 0) {
|
|
913
|
+
builder.emitString(q.value.cooked, idx(q));
|
|
914
|
+
parts++;
|
|
915
|
+
}
|
|
916
|
+
if (i >= exprs.length)
|
|
917
|
+
return;
|
|
918
|
+
compileExpression(exprs[i]);
|
|
919
|
+
builder.emit(Op.TO_STRING, 0, 0, idx(exprs[i]), 0);
|
|
920
|
+
parts++;
|
|
921
|
+
});
|
|
922
|
+
if (parts > 1)
|
|
923
|
+
builder.emit(Op.CONCAT, 0, parts, idx(node), 1 - parts);
|
|
924
|
+
};
|
|
925
|
+
const compileTaggedTemplate = (node) => {
|
|
926
|
+
if (node.tag.type === "MemberExpression")
|
|
927
|
+
compileMemberExpression(node.tag, true);
|
|
928
|
+
else {
|
|
929
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, idx(node), 1);
|
|
930
|
+
compileExpression(node.tag);
|
|
931
|
+
}
|
|
932
|
+
const siteConst = builder.constant({
|
|
933
|
+
cooked: node.quasi.quasis.map((q) => q.value.cooked ?? undefined),
|
|
934
|
+
raw: node.quasi.quasis.map((q) => q.value.raw),
|
|
935
|
+
nodeIndex: idx(node),
|
|
936
|
+
});
|
|
937
|
+
const argc = 1 + node.quasi.expressions.length;
|
|
938
|
+
if (argc <= 254) {
|
|
939
|
+
builder.emit(Op.TEMPLATE, 0, siteConst, idx(node), 1);
|
|
940
|
+
for (const expr of node.quasi.expressions)
|
|
941
|
+
compileExpression(expr);
|
|
942
|
+
builder.emit(Op.CALL, argc, 0, idx(node), -(argc + 1));
|
|
943
|
+
return;
|
|
944
|
+
}
|
|
945
|
+
builder.emit(Op.NEW_ARRAY, 0, 0, idx(node), 1);
|
|
946
|
+
builder.emit(Op.TEMPLATE, 0, siteConst, idx(node), 1);
|
|
947
|
+
builder.emit(Op.ARRAY_APPEND, 0, 0, idx(node), -1);
|
|
948
|
+
for (const expr of node.quasi.expressions) {
|
|
949
|
+
compileExpression(expr);
|
|
950
|
+
builder.emit(Op.ARRAY_APPEND, 0, 0, idx(expr), -1);
|
|
951
|
+
}
|
|
952
|
+
builder.emit(Op.CALL_SPREAD, 0, 0, idx(node), -2);
|
|
953
|
+
};
|
|
954
|
+
const compileUnaryExpression = (node) => {
|
|
955
|
+
const op = node.operator;
|
|
956
|
+
if (op === "delete")
|
|
957
|
+
return compileDeleteExpression(node);
|
|
958
|
+
const ref = node.argument.type === "Identifier" ? analysis.refs.get(node.argument) : undefined;
|
|
959
|
+
if (op === "typeof" && ref?.kind === "global")
|
|
960
|
+
return emitLoadRef(ref, node.argument, true);
|
|
961
|
+
compileExpression(node.argument);
|
|
962
|
+
if (op === "!")
|
|
963
|
+
builder.emit(Op.NOT, 0, 0, idx(node), 0);
|
|
964
|
+
else
|
|
965
|
+
builder.emit(Op.UNARY, UnaryOps[op], 0, idx(node), 0);
|
|
966
|
+
};
|
|
967
|
+
const compileDeleteExpression = (node) => {
|
|
968
|
+
const arg = node.argument;
|
|
969
|
+
const isChain = arg.type === "ChainExpression";
|
|
970
|
+
const target = isChain ? arg.expression : arg;
|
|
971
|
+
if (target.type === "Identifier") {
|
|
972
|
+
return emitThrowError({ type: "TypeError", message: "Only data fields may be deleted." }, target);
|
|
973
|
+
}
|
|
974
|
+
if (target.type !== "MemberExpression") {
|
|
975
|
+
compileExpression(arg);
|
|
976
|
+
builder.emit(Op.POP, 0, 0, idx(node), -1);
|
|
977
|
+
builder.emit(Op.PUSH_BOOL, 1, 0, idx(node), 1);
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
if (unsupportedSuper(target))
|
|
981
|
+
return;
|
|
982
|
+
const shortLabel = isChain || target.optional ? builder.newLabel(builder.depth + 1) : undefined;
|
|
983
|
+
compileExpression(target.object, shortLabel);
|
|
984
|
+
if (target.optional && shortLabel) {
|
|
985
|
+
builder.emitJump(Op.JUMP_COND, CondKind.NULLISH, shortLabel, idx(target), 0, 0);
|
|
986
|
+
}
|
|
987
|
+
if (target.computed) {
|
|
988
|
+
compileExpression(target.property);
|
|
989
|
+
builder.emit(Op.DELETE_ELEM, 0, 0, idx(target), -1);
|
|
990
|
+
}
|
|
991
|
+
else
|
|
992
|
+
builder.emit(Op.DELETE_PROP, 0, builder.constant(propertyName(target)), idx(target), 0);
|
|
993
|
+
if (shortLabel && shortLabel.patches.length > 0) {
|
|
994
|
+
const endLabel = builder.newLabel(builder.depth);
|
|
995
|
+
builder.emitJump(Op.JUMP, 0, endLabel, idx(node), 0, 0);
|
|
996
|
+
builder.mark(shortLabel);
|
|
997
|
+
builder.emit(Op.POP, 0, 0, idx(node), -1);
|
|
998
|
+
builder.emit(Op.PUSH_BOOL, 1, 0, idx(node), 1);
|
|
999
|
+
builder.mark(endLabel);
|
|
1000
|
+
}
|
|
1001
|
+
};
|
|
1002
|
+
const compileUpdateExpression = (node) => {
|
|
1003
|
+
const incFlag = node.operator === "++" ? 0 : 1;
|
|
1004
|
+
const arg = node.argument;
|
|
1005
|
+
if (arg.type === "Identifier") {
|
|
1006
|
+
const ref = analysis.refs.get(arg);
|
|
1007
|
+
emitLoadRef(ref, arg);
|
|
1008
|
+
if (node.prefix)
|
|
1009
|
+
builder.emit(Op.INC, incFlag, 0, idx(node), 0);
|
|
1010
|
+
else
|
|
1011
|
+
builder.emit(Op.TO_NUMERIC, incFlag, 0, idx(arg), 0);
|
|
1012
|
+
builder.emit(Op.DUP, 0, 0, idx(node), 1);
|
|
1013
|
+
if (!node.prefix)
|
|
1014
|
+
builder.emit(Op.INC, incFlag, 0, idx(node), 0);
|
|
1015
|
+
emitStoreRef(ref, false, arg);
|
|
1016
|
+
return;
|
|
1017
|
+
}
|
|
1018
|
+
if (arg.type !== "MemberExpression") {
|
|
1019
|
+
return emitThrowError({ type: "TypeError", message: "Update target must be an Identifier or MemberExpression." }, arg);
|
|
1020
|
+
}
|
|
1021
|
+
if (unsupportedSuper(arg))
|
|
1022
|
+
return;
|
|
1023
|
+
compileExpression(arg.object);
|
|
1024
|
+
const k = arg.computed ? 0 : builder.constant(propertyName(arg));
|
|
1025
|
+
if (arg.computed) {
|
|
1026
|
+
compileExpression(arg.property);
|
|
1027
|
+
emitWritableKey(arg);
|
|
1028
|
+
builder.emit(Op.PICK, 1, 0, idx(arg), 1);
|
|
1029
|
+
builder.emit(Op.PICK, 1, 0, idx(arg), 1);
|
|
1030
|
+
builder.emit(Op.GET_ELEM, 0, 0, idx(arg), -1);
|
|
1031
|
+
}
|
|
1032
|
+
else {
|
|
1033
|
+
builder.emit(Op.CHECK_BASE, CheckBaseKind.WRITE_PROP, k, idx(arg), 0);
|
|
1034
|
+
builder.emit(Op.DUP, 0, 0, idx(arg), 1);
|
|
1035
|
+
builder.emit(Op.GET_PROP, 0, k, idx(arg), 0);
|
|
1036
|
+
}
|
|
1037
|
+
builder.emit(Op.TO_NUMERIC, incFlag, 0, idx(arg), 0);
|
|
1038
|
+
if (!node.prefix) {
|
|
1039
|
+
builder.emit(Op.DUP, 0, 0, idx(node), 1);
|
|
1040
|
+
builder.emit(Op.ROT, arg.computed ? 3 : 2, 0, idx(node), 0);
|
|
1041
|
+
}
|
|
1042
|
+
builder.emit(Op.INC, incFlag, 0, idx(node), 0);
|
|
1043
|
+
if (arg.computed)
|
|
1044
|
+
builder.emit(Op.PUT_ELEM, 0, 0, idx(arg), -2);
|
|
1045
|
+
else
|
|
1046
|
+
builder.emit(Op.PUT_PROP, 0, k, idx(arg), -1);
|
|
1047
|
+
if (!node.prefix)
|
|
1048
|
+
builder.emit(Op.POP, 0, 0, idx(node), -1);
|
|
1049
|
+
};
|
|
1050
|
+
const emitBinaryOp = (op, node) => {
|
|
1051
|
+
const [code, a] = BinaryOps[op];
|
|
1052
|
+
builder.emit(code, a, 0, idx(node), -1);
|
|
1053
|
+
};
|
|
1054
|
+
const compileBinaryExpression = (node) => {
|
|
1055
|
+
compileExpression(node.left);
|
|
1056
|
+
compileExpression(node.right);
|
|
1057
|
+
emitBinaryOp(node.operator, node);
|
|
1058
|
+
};
|
|
1059
|
+
const compileLogicalExpression = (node) => {
|
|
1060
|
+
const endLabel = builder.newLabel(builder.depth + 1);
|
|
1061
|
+
compileExpression(node.left);
|
|
1062
|
+
builder.emitJump(Op.JUMP_COND, logicalCond(node.operator), endLabel, idx(node), 0, -1);
|
|
1063
|
+
compileExpression(node.right);
|
|
1064
|
+
builder.mark(endLabel);
|
|
1065
|
+
};
|
|
1066
|
+
const compileConditionalExpression = (node) => {
|
|
1067
|
+
const elseLabel = builder.newLabel(builder.depth);
|
|
1068
|
+
const endLabel = builder.newLabel(builder.depth + 1);
|
|
1069
|
+
compileExpression(node.test);
|
|
1070
|
+
builder.emitJump(Op.JUMP_IF_FALSE, 0, elseLabel, idx(node), -1, -1);
|
|
1071
|
+
compileExpression(node.consequent);
|
|
1072
|
+
builder.emitJump(Op.JUMP, 0, endLabel, idx(node), 0, -1);
|
|
1073
|
+
builder.mark(elseLabel);
|
|
1074
|
+
compileExpression(node.alternate);
|
|
1075
|
+
builder.mark(endLabel);
|
|
1076
|
+
};
|
|
1077
|
+
const isLogicalAssignment = (node) => node.operator === "&&=" || node.operator === "||=" || node.operator === "??=";
|
|
1078
|
+
// `discard` skips keeping the assigned value, for an assignment to an identifier used as a statement.
|
|
1079
|
+
const compileAssignmentExpression = (node, discard = false) => {
|
|
1080
|
+
const left = node.left;
|
|
1081
|
+
const op = node.operator;
|
|
1082
|
+
const isLogical = isLogicalAssignment(node);
|
|
1083
|
+
if (left.type === "Identifier") {
|
|
1084
|
+
const ref = analysis.refs.get(left);
|
|
1085
|
+
const endLabel = builder.newLabel(builder.depth + 1);
|
|
1086
|
+
if (op !== "=")
|
|
1087
|
+
emitLoadRef(ref, left);
|
|
1088
|
+
if (isLogical)
|
|
1089
|
+
builder.emitJump(Op.JUMP_COND, logicalCond(op), endLabel, idx(node), 0, -1);
|
|
1090
|
+
if (op === "=" || isLogical)
|
|
1091
|
+
compileNamedExpression(node.right, left.name);
|
|
1092
|
+
else {
|
|
1093
|
+
compileExpression(node.right);
|
|
1094
|
+
emitBinaryOp(op.slice(0, -1), node);
|
|
1095
|
+
}
|
|
1096
|
+
if (!discard)
|
|
1097
|
+
builder.emit(Op.DUP, 0, 0, idx(node), 1);
|
|
1098
|
+
emitStoreRef(ref, false, left);
|
|
1099
|
+
if (isLogical)
|
|
1100
|
+
builder.mark(endLabel);
|
|
1101
|
+
return;
|
|
1102
|
+
}
|
|
1103
|
+
if (left.type === "ObjectPattern" || left.type === "ArrayPattern") {
|
|
1104
|
+
compileExpression(node.right);
|
|
1105
|
+
builder.emit(Op.DUP, 0, 0, idx(node), 1);
|
|
1106
|
+
compilePattern(left, false, false, node);
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
if (left.type !== "MemberExpression") {
|
|
1110
|
+
return emitThrowError({ type: "TypeError", message: "Assignment target must be an Identifier or MemberExpression." }, left);
|
|
1111
|
+
}
|
|
1112
|
+
if (unsupportedSuper(left))
|
|
1113
|
+
return;
|
|
1114
|
+
const baseDepth = builder.depth;
|
|
1115
|
+
compileExpression(left.object);
|
|
1116
|
+
const k = left.computed ? 0 : builder.constant(propertyName(left));
|
|
1117
|
+
const put = () => left.computed ? builder.emit(Op.PUT_ELEM, 0, 0, idx(left), -2) : builder.emit(Op.PUT_PROP, 0, k, idx(left), -1);
|
|
1118
|
+
if (left.computed) {
|
|
1119
|
+
compileExpression(left.property);
|
|
1120
|
+
emitWritableKey(left);
|
|
1121
|
+
}
|
|
1122
|
+
else
|
|
1123
|
+
builder.emit(Op.CHECK_BASE, CheckBaseKind.WRITE_PROP, k, idx(left), 0);
|
|
1124
|
+
if (op === "=") {
|
|
1125
|
+
compileExpression(node.right);
|
|
1126
|
+
put();
|
|
1127
|
+
return;
|
|
1128
|
+
}
|
|
1129
|
+
if (left.computed) {
|
|
1130
|
+
builder.emit(Op.PICK, 1, 0, idx(left), 1);
|
|
1131
|
+
builder.emit(Op.PICK, 1, 0, idx(left), 1);
|
|
1132
|
+
builder.emit(Op.GET_ELEM, 0, 0, idx(left), -1);
|
|
1133
|
+
}
|
|
1134
|
+
else {
|
|
1135
|
+
builder.emit(Op.DUP, 0, 0, idx(left), 1);
|
|
1136
|
+
builder.emit(Op.GET_PROP, 0, k, idx(left), 0);
|
|
1137
|
+
}
|
|
1138
|
+
if (!isLogical) {
|
|
1139
|
+
compileExpression(node.right);
|
|
1140
|
+
emitBinaryOp(op.slice(0, -1), node);
|
|
1141
|
+
put();
|
|
1142
|
+
return;
|
|
1143
|
+
}
|
|
1144
|
+
// Short-circuits with `[o, key, current]` (or `[o, current]`) on the stack, and keeps `current`.
|
|
1145
|
+
const shortLabel = builder.newLabel(baseDepth + (left.computed ? 3 : 2));
|
|
1146
|
+
const endLabel = builder.newLabel(baseDepth + 1);
|
|
1147
|
+
builder.emitJump(Op.JUMP_COND, logicalCond(op), shortLabel, idx(node), 0, -1);
|
|
1148
|
+
compileExpression(node.right);
|
|
1149
|
+
put();
|
|
1150
|
+
builder.emitJump(Op.JUMP, 0, endLabel, idx(node), 0, -1);
|
|
1151
|
+
builder.mark(shortLabel);
|
|
1152
|
+
builder.emit(Op.ROT, left.computed ? 2 : 1, 0, idx(node), 0);
|
|
1153
|
+
builder.emit(Op.POP, 0, 0, idx(node), -1);
|
|
1154
|
+
if (left.computed)
|
|
1155
|
+
builder.emit(Op.POP, 0, 0, idx(node), -1);
|
|
1156
|
+
builder.mark(endLabel);
|
|
1157
|
+
};
|
|
1158
|
+
const compileMemberExpression = (node, keepReceiver, chainShortCircuit) => {
|
|
1159
|
+
if (unsupportedSuper(node))
|
|
1160
|
+
return;
|
|
1161
|
+
compileExpression(node.object, chainShortCircuit);
|
|
1162
|
+
if (node.optional && chainShortCircuit) {
|
|
1163
|
+
builder.emitJump(Op.JUMP_COND, CondKind.NULLISH, chainShortCircuit, idx(node), 0, 0);
|
|
1164
|
+
}
|
|
1165
|
+
const keepFlag = keepReceiver ? 1 : 0;
|
|
1166
|
+
if (node.computed) {
|
|
1167
|
+
compileExpression(node.property);
|
|
1168
|
+
if (keepReceiver) {
|
|
1169
|
+
builder.emit(Op.CHECK_BASE, CheckBaseKind.READ, 0, idx(node), 0);
|
|
1170
|
+
builder.emit(Op.TO_KEY, 0, 0, idx(node.property), 0);
|
|
1171
|
+
}
|
|
1172
|
+
builder.emit(Op.GET_ELEM, keepFlag, 0, idx(node), keepReceiver ? 0 : -1);
|
|
1173
|
+
}
|
|
1174
|
+
else {
|
|
1175
|
+
builder.emit(Op.GET_PROP, keepFlag, builder.constant(propertyName(node)), idx(node), keepReceiver ? 1 : 0);
|
|
1176
|
+
}
|
|
1177
|
+
};
|
|
1178
|
+
const compileCallExpression = (node, chainShortCircuit) => {
|
|
1179
|
+
if (node.callee.type === "Super")
|
|
1180
|
+
return emitUnsupported("Super", node.callee);
|
|
1181
|
+
const args = node.arguments;
|
|
1182
|
+
// Lower `$Call(fn, thisArg, ...args)` in internal/prelude code directly to `Op.CALL` (§5.1)
|
|
1183
|
+
if (internal &&
|
|
1184
|
+
!node.optional &&
|
|
1185
|
+
node.callee.type === "Identifier" &&
|
|
1186
|
+
node.callee.name === "$Call" &&
|
|
1187
|
+
args.length >= 2 &&
|
|
1188
|
+
args.length - 2 <= 254 &&
|
|
1189
|
+
!args.some((a) => a.type === "SpreadElement")) {
|
|
1190
|
+
for (const arg of [args[1], args[0], ...args.slice(2)])
|
|
1191
|
+
compileExpression(arg);
|
|
1192
|
+
builder.emit(Op.CALL, args.length - 2, 0, idx(node), -(args.length - 1));
|
|
1193
|
+
return;
|
|
1194
|
+
}
|
|
1195
|
+
const shortLabel = chainShortCircuit ?? (node.optional ? builder.newLabel(builder.depth + 1) : undefined);
|
|
1196
|
+
if (node.callee.type === "MemberExpression") {
|
|
1197
|
+
compileMemberExpression(node.callee, true, shortLabel);
|
|
1198
|
+
if (node.optional && shortLabel) {
|
|
1199
|
+
builder.emitJump(Op.JUMP_COND, CondKind.NULLISH_METHOD, shortLabel, idx(node), -1, 0);
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
else if (shortLabel) {
|
|
1203
|
+
compileExpression(node.callee, shortLabel);
|
|
1204
|
+
if (node.optional)
|
|
1205
|
+
builder.emitJump(Op.JUMP_COND, CondKind.NULLISH, shortLabel, idx(node), 0, 0);
|
|
1206
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, idx(node), 1);
|
|
1207
|
+
builder.emit(Op.ROT, 1, 0, idx(node), 0);
|
|
1208
|
+
}
|
|
1209
|
+
else {
|
|
1210
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, idx(node), 1);
|
|
1211
|
+
compileExpression(node.callee);
|
|
1212
|
+
}
|
|
1213
|
+
if (emitArguments(args, node))
|
|
1214
|
+
builder.emit(Op.CALL_SPREAD, 0, 0, idx(node), -2);
|
|
1215
|
+
else
|
|
1216
|
+
builder.emit(Op.CALL, args.length, 0, idx(node), -(args.length + 1));
|
|
1217
|
+
if (!chainShortCircuit && shortLabel)
|
|
1218
|
+
builder.mark(shortLabel);
|
|
1219
|
+
};
|
|
1220
|
+
const compileNewExpression = (node) => {
|
|
1221
|
+
compileExpression(node.callee);
|
|
1222
|
+
// Check constructibility before evaluating arguments
|
|
1223
|
+
builder.emit(Op.NEW, 255, 0, idx(node), 0);
|
|
1224
|
+
const args = node.arguments;
|
|
1225
|
+
if (emitArguments(args, node))
|
|
1226
|
+
builder.emit(Op.NEW_SPREAD, 0, 0, idx(node), -1);
|
|
1227
|
+
else
|
|
1228
|
+
builder.emit(Op.NEW, args.length, 0, idx(node), -args.length);
|
|
1229
|
+
};
|
|
1230
|
+
const compileImportExpression = (node) => {
|
|
1231
|
+
compileExpression(node.source);
|
|
1232
|
+
builder.emit(Op.TO_STRING, 1, 0, idx(node.source), 0);
|
|
1233
|
+
if (node.options) {
|
|
1234
|
+
compileExpression(node.options);
|
|
1235
|
+
builder.emit(Op.POP, 0, 0, idx(node.options), -1);
|
|
1236
|
+
}
|
|
1237
|
+
builder.emit(Op.IMPORT, ImportKind.DYNAMIC, 0, idx(node), 0);
|
|
1238
|
+
};
|
|
1239
|
+
const compileAwaitExpression = (node) => {
|
|
1240
|
+
compileExpression(node.argument);
|
|
1241
|
+
builder.emit(Op.AWAIT, internal ? 1 : 0, 0, idx(node), 0);
|
|
1242
|
+
};
|
|
1243
|
+
const isAsync = (fnInfo.flags & CodeFlags.ASYNC) !== 0;
|
|
1244
|
+
const compileYieldExpression = (node) => {
|
|
1245
|
+
if (node.delegate)
|
|
1246
|
+
return compileYieldDelegate(node);
|
|
1247
|
+
const targetDepth = builder.depth + 1;
|
|
1248
|
+
if (node.argument)
|
|
1249
|
+
compileExpression(node.argument);
|
|
1250
|
+
else
|
|
1251
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, idx(node), 1);
|
|
1252
|
+
if (isAsync)
|
|
1253
|
+
builder.emit(Op.AWAIT, 0, 0, idx(node), 0);
|
|
1254
|
+
builder.emit(Op.YIELD, 0, 0, idx(node), 1);
|
|
1255
|
+
// C2: RESUME jumps to `normalLabel` on next (`[recv]`), throws on throw, and falls through on return (`[recv]`).
|
|
1256
|
+
const normalLabel = builder.newLabel(targetDepth);
|
|
1257
|
+
builder.emitJump(Op.RESUME, 0, normalLabel, idx(node), -1, -1);
|
|
1258
|
+
if (isAsync)
|
|
1259
|
+
builder.emit(Op.AWAIT, 1, 0, idx(node), 0);
|
|
1260
|
+
exitPlan("return", null, node);
|
|
1261
|
+
builder.mark(normalLabel);
|
|
1262
|
+
};
|
|
1263
|
+
// Pushes whether the resume kind in `kindSlot` (or on top, when `kindSlot` is -1) is `return`.
|
|
1264
|
+
const emitIsReturn = (kindSlot, node) => {
|
|
1265
|
+
if (kindSlot < 0)
|
|
1266
|
+
builder.emit(Op.DUP, 0, 0, idx(node), 1);
|
|
1267
|
+
else
|
|
1268
|
+
builder.emit(Op.GET_LOCAL, 0, kindSlot, idx(node), 1);
|
|
1269
|
+
builder.emitNumber(2, idx(node));
|
|
1270
|
+
builder.emit(Op.SEQ, 0, 0, idx(node), -1);
|
|
1271
|
+
};
|
|
1272
|
+
const compileYieldDelegate = (node) => {
|
|
1273
|
+
const baseDepth = builder.depth;
|
|
1274
|
+
const itSlot = allocSlot("%it");
|
|
1275
|
+
const kindSlot = allocSlot("%kind");
|
|
1276
|
+
compileExpression(node.argument);
|
|
1277
|
+
const openSlow = emitIterOpen(isAsync ? IterContext.ASYNC_DELEGATE : IterContext.DELEGATE, itSlot, baseDepth, node);
|
|
1278
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, idx(node), 1);
|
|
1279
|
+
builder.emitNumber(0, idx(node));
|
|
1280
|
+
const loopLabel = builder.newLabel(baseDepth + 2);
|
|
1281
|
+
const callLabel = builder.newLabel(baseDepth + 3);
|
|
1282
|
+
const noThrowLabel = builder.newLabel(baseDepth + 1);
|
|
1283
|
+
const doneLabel = builder.newLabel(baseDepth + 1);
|
|
1284
|
+
const exprDoneLabel = builder.newLabel(baseDepth + 1);
|
|
1285
|
+
builder.mark(loopLabel);
|
|
1286
|
+
builder.emit(Op.DUP, 0, 0, idx(node), 1);
|
|
1287
|
+
builder.emit(Op.PUT_LOCAL, PutKind.INIT, kindSlot, idx(node), -1);
|
|
1288
|
+
// YSTAR jumps to `callLabel` (`[iterator, method, recv]`) when the method exists, and falls through (`[recv]`) when missing.
|
|
1289
|
+
builder.emitJump(Op.YSTAR, itSlot, callLabel, idx(node), 1, -1);
|
|
1290
|
+
emitIsReturn(kindSlot, node);
|
|
1291
|
+
builder.emitJump(Op.JUMP_IF_FALSE, 0, noThrowLabel, idx(node), -1, -1);
|
|
1292
|
+
// Missing `return` method: await received value in async generators, then return
|
|
1293
|
+
if (isAsync)
|
|
1294
|
+
builder.emit(Op.AWAIT, 0, 0, idx(node), 0);
|
|
1295
|
+
exitPlan("return", null, node);
|
|
1296
|
+
// Missing `throw` method: close iterator, then throw TypeError
|
|
1297
|
+
builder.mark(noThrowLabel);
|
|
1298
|
+
builder.emit(Op.POP, 0, 0, idx(node), -1);
|
|
1299
|
+
emitIteratorCloseNormal(itSlot, isAsync, node);
|
|
1300
|
+
emitThrowError({ type: "TypeError", message: "The delegated iterator does not provide a throw() method." }, node);
|
|
1301
|
+
builder.mark(callLabel);
|
|
1302
|
+
builder.emit(Op.CALL, 1, 0, idx(node), -2);
|
|
1303
|
+
if (isAsync)
|
|
1304
|
+
builder.emit(Op.AWAIT, 0, 0, idx(node), 0);
|
|
1305
|
+
builder.emitJump(Op.YSTAR_RESULT, kindSlot, doneLabel, idx(node), 0, 0);
|
|
1306
|
+
builder.emit(Op.YIELD, 0, 0, idx(node), 1);
|
|
1307
|
+
if (isAsync) {
|
|
1308
|
+
const notReturnLabel = builder.newLabel(baseDepth + 2);
|
|
1309
|
+
emitIsReturn(-1, node);
|
|
1310
|
+
builder.emitJump(Op.JUMP_IF_FALSE, 0, notReturnLabel, idx(node), -1, -1);
|
|
1311
|
+
builder.emit(Op.ROT, 1, 0, idx(node), 0);
|
|
1312
|
+
builder.emit(Op.AWAIT, 1, 0, idx(node), 0);
|
|
1313
|
+
builder.emit(Op.ROT, 1, 0, idx(node), 0);
|
|
1314
|
+
builder.mark(notReturnLabel);
|
|
1315
|
+
}
|
|
1316
|
+
builder.emitJump(Op.LOOP, 0, loopLabel, idx(node), 0, null);
|
|
1317
|
+
openSlow();
|
|
1318
|
+
builder.mark(doneLabel);
|
|
1319
|
+
emitIsReturn(kindSlot, node);
|
|
1320
|
+
builder.emitJump(Op.JUMP_IF_FALSE, 0, exprDoneLabel, idx(node), -1, -1);
|
|
1321
|
+
exitPlan("return", null, node);
|
|
1322
|
+
builder.mark(exprDoneLabel);
|
|
1323
|
+
};
|
|
1324
|
+
const statements = {
|
|
1325
|
+
EmptyStatement: () => { },
|
|
1326
|
+
FunctionDeclaration: () => { },
|
|
1327
|
+
ImportDeclaration: (stmt) => {
|
|
1328
|
+
if (isProgram)
|
|
1329
|
+
return;
|
|
1330
|
+
const message = "An import declaration may only appear at the top level of the program.";
|
|
1331
|
+
emitThrowError({ type: "SyntaxError", message }, stmt);
|
|
1332
|
+
},
|
|
1333
|
+
ExpressionStatement: (stmt) => compileEffect(stmt.expression),
|
|
1334
|
+
VariableDeclaration: compileVariableDeclaration,
|
|
1335
|
+
BlockStatement: compileBlockStatement,
|
|
1336
|
+
IfStatement: compileIfStatement,
|
|
1337
|
+
WhileStatement: compileWhileStatement,
|
|
1338
|
+
DoWhileStatement: compileDoWhileStatement,
|
|
1339
|
+
ForStatement: compileForStatement,
|
|
1340
|
+
ForInStatement: compileForInStatement,
|
|
1341
|
+
ForOfStatement: compileForOfStatement,
|
|
1342
|
+
SwitchStatement: compileSwitchStatement,
|
|
1343
|
+
LabeledStatement: compileLabeledStatement,
|
|
1344
|
+
BreakStatement: (stmt) => exitPlan("break", findExitTarget("break", stmt.label?.name), stmt),
|
|
1345
|
+
ContinueStatement: (stmt) => exitPlan("continue", findExitTarget("continue", stmt.label?.name), stmt),
|
|
1346
|
+
ReturnStatement: (stmt) => {
|
|
1347
|
+
if (stmt.argument) {
|
|
1348
|
+
compileExpression(stmt.argument);
|
|
1349
|
+
if (isAsync && (fnInfo.flags & CodeFlags.GENERATOR) !== 0)
|
|
1350
|
+
builder.emit(Op.AWAIT, 0, 0, idx(stmt), 0);
|
|
1351
|
+
}
|
|
1352
|
+
else
|
|
1353
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, idx(stmt), 1);
|
|
1354
|
+
exitPlan("return", null, stmt);
|
|
1355
|
+
},
|
|
1356
|
+
ThrowStatement: (stmt) => {
|
|
1357
|
+
compileExpression(stmt.argument);
|
|
1358
|
+
builder.emit(Op.THROW, 0, 0, idx(stmt), -1);
|
|
1359
|
+
builder.unreachable();
|
|
1360
|
+
},
|
|
1361
|
+
TryStatement: compileTryStatement,
|
|
1362
|
+
};
|
|
1363
|
+
const expressions = {
|
|
1364
|
+
Literal: compileLiteral,
|
|
1365
|
+
Identifier: (expr) => emitLoadRef(analysis.refs.get(expr), expr),
|
|
1366
|
+
ThisExpression: (expr) => emitLoadRef(analysis.thisRefs.get(expr), expr),
|
|
1367
|
+
FunctionExpression: (expr) => builder.emit(Op.CLOSURE, 0, analysis.nodeToFunction.get(expr).index, idx(expr), 1),
|
|
1368
|
+
ArrowFunctionExpression: (expr) => builder.emit(Op.CLOSURE, 0, analysis.nodeToFunction.get(expr).index, idx(expr), 1),
|
|
1369
|
+
ArrayExpression: compileArrayExpression,
|
|
1370
|
+
ObjectExpression: compileObjectExpression,
|
|
1371
|
+
TemplateLiteral: compileTemplateLiteral,
|
|
1372
|
+
TaggedTemplateExpression: compileTaggedTemplate,
|
|
1373
|
+
UnaryExpression: compileUnaryExpression,
|
|
1374
|
+
UpdateExpression: compileUpdateExpression,
|
|
1375
|
+
BinaryExpression: compileBinaryExpression,
|
|
1376
|
+
LogicalExpression: compileLogicalExpression,
|
|
1377
|
+
ConditionalExpression: compileConditionalExpression,
|
|
1378
|
+
SequenceExpression: (expr) => expr.expressions.forEach((e, i) => {
|
|
1379
|
+
compileExpression(e);
|
|
1380
|
+
if (i < expr.expressions.length - 1)
|
|
1381
|
+
builder.emit(Op.POP, 0, 0, idx(e), -1);
|
|
1382
|
+
}),
|
|
1383
|
+
AssignmentExpression: (expr) => compileAssignmentExpression(expr),
|
|
1384
|
+
MemberExpression: (expr, short) => compileMemberExpression(expr, false, short),
|
|
1385
|
+
CallExpression: compileCallExpression,
|
|
1386
|
+
NewExpression: compileNewExpression,
|
|
1387
|
+
ChainExpression: (expr) => {
|
|
1388
|
+
const shortLabel = builder.newLabel(builder.depth + 1);
|
|
1389
|
+
compileExpression(expr.expression, shortLabel);
|
|
1390
|
+
builder.mark(shortLabel);
|
|
1391
|
+
},
|
|
1392
|
+
ImportExpression: compileImportExpression,
|
|
1393
|
+
AwaitExpression: compileAwaitExpression,
|
|
1394
|
+
YieldExpression: compileYieldExpression,
|
|
1395
|
+
};
|
|
1396
|
+
// --- Emit Function / Program Body ---
|
|
1397
|
+
// `this`, `arguments`, and a named function expression's own name, when the body uses them.
|
|
1398
|
+
const emitImplicit = (decl, argsKind) => {
|
|
1399
|
+
if (!decl?.used)
|
|
1400
|
+
return;
|
|
1401
|
+
if (decl.captured)
|
|
1402
|
+
builder.emit(Op.NEW_CELL, 0, decl.slot);
|
|
1403
|
+
if (isProgram)
|
|
1404
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, -1, 1);
|
|
1405
|
+
else
|
|
1406
|
+
builder.emit(Op.ARGS, argsKind, 0, idx(fnInfo.node), 1);
|
|
1407
|
+
emitStoreDecl(decl, true, isProgram ? undefined : fnInfo.node);
|
|
1408
|
+
};
|
|
1409
|
+
if (isProgram) {
|
|
1410
|
+
const prog = fnInfo.node;
|
|
1411
|
+
emitImplicit(fnInfo.thisDecl, 3);
|
|
1412
|
+
for (const decl of fnInfo.bodyScope.decls.values()) {
|
|
1413
|
+
if (decl.kind !== "this" && decl.captured)
|
|
1414
|
+
builder.emit(Op.NEW_CELL, 0, decl.slot);
|
|
1415
|
+
}
|
|
1416
|
+
emitImports(prog.body);
|
|
1417
|
+
for (const decl of fnInfo.bodyScope.decls.values()) {
|
|
1418
|
+
if (decl.kind !== "var")
|
|
1419
|
+
continue;
|
|
1420
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, -1, 1);
|
|
1421
|
+
emitStoreDecl(decl, true);
|
|
1422
|
+
}
|
|
1423
|
+
emitHoistedFunctions(fnInfo.bodyScope);
|
|
1424
|
+
const last = prog.body.at(-1);
|
|
1425
|
+
for (const s of prog.body)
|
|
1426
|
+
if (s !== last || s.type !== "ExpressionStatement")
|
|
1427
|
+
compileStatement(s);
|
|
1428
|
+
if (last?.type === "ExpressionStatement") {
|
|
1429
|
+
compileExpression(last.expression);
|
|
1430
|
+
builder.emit(Op.END_PROGRAM, 0, 0, idx(last), -1);
|
|
1431
|
+
}
|
|
1432
|
+
else {
|
|
1433
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, idx(prog), 1);
|
|
1434
|
+
builder.emit(Op.END_PROGRAM, 0, 0, idx(prog), -1);
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
else {
|
|
1438
|
+
emitImplicit(fnInfo.selfScope?.decls.values().next().value, 2);
|
|
1439
|
+
emitImplicit(fnInfo.thisDecl, 3);
|
|
1440
|
+
const simple = (fnInfo.flags & CodeFlags.SIMPLE_PARAMS) !== 0;
|
|
1441
|
+
for (const decl of fnInfo.paramScope.decls.values()) {
|
|
1442
|
+
// A simple parameter's argument is already in its slot: it moves into the new cell.
|
|
1443
|
+
if (decl.kind === "param" && decl.captured)
|
|
1444
|
+
builder.emit(Op.NEW_CELL, simple ? 1 : 0, decl.slot, idx(fnInfo.node), 0);
|
|
1445
|
+
}
|
|
1446
|
+
emitImplicit(fnInfo.argumentsDecl, 0);
|
|
1447
|
+
if (!simple) {
|
|
1448
|
+
fnInfo.params.forEach((param, i) => {
|
|
1449
|
+
if (param.type === "RestElement") {
|
|
1450
|
+
builder.emit(Op.ARGS, 1, i, idx(param), 1);
|
|
1451
|
+
return compilePattern(param.argument, true, true, param);
|
|
1452
|
+
}
|
|
1453
|
+
// Duplicate plain identifier parameter: only the last occurrence initializes the binding
|
|
1454
|
+
const name = param.type === "Identifier" ? param.name : undefined;
|
|
1455
|
+
if (name !== undefined && fnInfo.params.findLastIndex((p) => p.type === "Identifier" && p.name === name) !== i)
|
|
1456
|
+
return;
|
|
1457
|
+
builder.emit(Op.ARGS, 4, i, idx(param), 1);
|
|
1458
|
+
compilePattern(param, true, true, param);
|
|
1459
|
+
});
|
|
1460
|
+
}
|
|
1461
|
+
emitScopePrologue(fnInfo.bodyScope, false);
|
|
1462
|
+
if ((fnInfo.flags & CodeFlags.GENERATOR) !== 0)
|
|
1463
|
+
builder.emit(Op.GENERATOR_START, 0, 0, idx(fnInfo.node), 0);
|
|
1464
|
+
const body = fnInfo.node.body;
|
|
1465
|
+
if (body.type === "BlockStatement") {
|
|
1466
|
+
for (const s of body.body)
|
|
1467
|
+
compileStatement(s);
|
|
1468
|
+
builder.emit(Op.PUSH_UNDEF, 0, 0, idx(body), 1);
|
|
1469
|
+
}
|
|
1470
|
+
else
|
|
1471
|
+
compileExpression(body);
|
|
1472
|
+
builder.emit(Op.RETURN, 0, 0, idx(body), -1);
|
|
1473
|
+
}
|
|
1474
|
+
return builder.finish();
|
|
1475
|
+
};
|
|
1476
|
+
const propertyName = (node) => node.property.name;
|
|
1477
|
+
const keyName = (key) => key.type === "Identifier" ? key.name : String(key.value);
|