js-confuser-vm 0.0.3 → 0.0.5

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.
Files changed (51) hide show
  1. package/CHANGELOG.md +125 -28
  2. package/LICENSE +21 -21
  3. package/README.MD +370 -196
  4. package/babel-plugin-inline-runtime.cjs +34 -34
  5. package/babel.config.json +23 -23
  6. package/dist/build-runtime.js +53 -0
  7. package/dist/compiler.js +107 -117
  8. package/dist/runtime.js +78 -84
  9. package/dist/transforms/bytecode/macroOpcodes.js +152 -0
  10. package/dist/transforms/{resolveContants.js → bytecode/resolveContants.js} +16 -6
  11. package/dist/transforms/bytecode/resolveLabels.js +80 -0
  12. package/dist/transforms/{selfModifying.js → bytecode/selfModifying.js} +33 -33
  13. package/dist/transforms/bytecode/specializedOpcodes.js +103 -0
  14. package/dist/transforms/runtime/macroOpcodes.js +88 -0
  15. package/dist/transforms/runtime/minify.js +1 -0
  16. package/dist/transforms/runtime/shuffleOpcodes.js +20 -0
  17. package/dist/transforms/runtime/specializedOpcodes.js +102 -0
  18. package/dist/transforms/utils/op-utils.js +25 -0
  19. package/dist/{random.js → transforms/utils/random-utils.js} +3 -3
  20. package/dist/types.js +4 -2
  21. package/index.ts +34 -22
  22. package/jest-strip-types.js +10 -10
  23. package/jest.config.js +35 -28
  24. package/package.json +49 -48
  25. package/src/build-runtime.ts +57 -0
  26. package/src/compiler.ts +2069 -2066
  27. package/src/index.ts +14 -14
  28. package/src/minify.ts +21 -21
  29. package/src/options.ts +14 -12
  30. package/src/runtime.ts +771 -779
  31. package/src/transforms/bytecode/macroOpcodes.ts +177 -0
  32. package/src/transforms/bytecode/resolveContants.ts +62 -0
  33. package/src/transforms/bytecode/resolveLabels.ts +107 -0
  34. package/src/transforms/{selfModifying.ts → bytecode/selfModifying.ts} +37 -40
  35. package/src/transforms/bytecode/specializedOpcodes.ts +118 -0
  36. package/src/transforms/runtime/macroOpcodes.ts +111 -0
  37. package/src/transforms/runtime/minify.ts +1 -0
  38. package/src/transforms/runtime/shuffleOpcodes.ts +24 -0
  39. package/src/transforms/runtime/specializedOpcodes.ts +146 -0
  40. package/src/transforms/utils/op-utils.ts +26 -0
  41. package/src/{random.ts → transforms/utils/random-utils.ts} +31 -31
  42. package/src/types.ts +33 -24
  43. package/src/utilts.ts +3 -3
  44. package/tsconfig.json +12 -12
  45. package/dist/runtimeObf.js +0 -56
  46. package/dist/transforms/controlFlowFlattening.js +0 -22
  47. package/dist/transforms/resolveLabels.js +0 -59
  48. package/src/runtimeObf.ts +0 -62
  49. package/src/transforms/controlFlowFlattening.ts +0 -30
  50. package/src/transforms/resolveContants.ts +0 -42
  51. package/src/transforms/resolveLabels.ts +0 -83
package/src/compiler.ts CHANGED
@@ -1,2066 +1,2069 @@
1
- import { parse } from "@babel/parser";
2
- import traverseImport from "@babel/traverse";
3
- import { generate } from "@babel/generator";
4
-
5
- import { readFileSync } from "fs";
6
- import { join } from "path";
7
- import { stripTypeScriptTypes } from "module";
8
- import JSON5 from "json5";
9
- import * as t from "@babel/types";
10
- import { ok } from "assert";
11
- import { obfuscateRuntime } from "./runtimeObf.ts";
12
- import { DEFAULT_OPTIONS, type Options } from "./options.ts";
13
- import { resolveLabels } from "./transforms/resolveLabels.ts";
14
- import { resolveConstants } from "./transforms/resolveContants.ts";
15
- import { selfModifying } from "./transforms/selfModifying.ts";
16
- import * as b from "./types.ts";
17
-
18
- const traverse = (traverseImport.default ||
19
- traverseImport) as typeof traverseImport.default;
20
-
21
- const readVMRuntimeFile = () => {
22
- let code;
23
- try {
24
- code = readFileSync(join(import.meta.dirname, "./runtime.ts"), "utf-8");
25
- } catch (e) {
26
- code = readFileSync(join(import.meta.dirname, "./runtime.js"), "utf-8");
27
- }
28
-
29
- return stripTypeScriptTypes?.(code) || code;
30
- };
31
-
32
- const VM_RUNTIME = readVMRuntimeFile().split("@START")[1];
33
- export const SOURCE_NODE_SYM = Symbol("SOURCE_NODE"); // Attach source node location to pseudo bytecode instructions
34
-
35
- // Opcodes
36
- export const OP_ORIGINAL = {
37
- LOAD_CONST: 0,
38
- LOAD_LOCAL: 1,
39
- STORE_LOCAL: 2,
40
- LOAD_GLOBAL: 3,
41
- STORE_GLOBAL: 4,
42
- GET_PROP: 5,
43
- ADD: 6, // a + b (both are popped)
44
- SUB: 7, // a - b
45
- MUL: 8, // a * b
46
- DIV: 9, // a / b
47
- MAKE_CLOSURE: 10,
48
- CALL: 11,
49
- CALL_METHOD: 12,
50
- RETURN: 13,
51
- POP: 14, // discard top of stack
52
- LT: 15, // pop b, pop a -> push (a < b)
53
- GT: 16, // pop b, pop a -> push (a > b)
54
- EQ: 17, // pop b, pop a -> push (a === b)
55
- JUMP: 18, // unconditional - operand = absolute bytecode index
56
- JUMP_IF_FALSE: 19, // pop value; jump if falsy
57
- LTE: 20, // a <= b
58
- GTE: 21, // a >= b
59
- NEQ: 22, // a !== b
60
- LOAD_UPVALUE: 23, // push frame.closure.upvalues[operand].read()
61
- STORE_UPVALUE: 24, // frame.closure.upvalues[operand].write(pop())
62
-
63
- // Unary
64
- UNARY_NEG: 25, // -x
65
- UNARY_POS: 26, // +x
66
- UNARY_NOT: 27, // !x
67
- UNARY_BITNOT: 28, // ~x
68
- TYPEOF: 29, // typeof x
69
- VOID: 30, // void x -> always undefined
70
-
71
- TYPEOF_SAFE: 31, // operand = name constIdx - typeof guard for undeclared globals
72
- BUILD_ARRAY: 32, // operand = element count - pops N values -> pushes array
73
- BUILD_OBJECT: 33, // operand = pair count - pops N*2 (key,val) -> pushes object
74
- SET_PROP: 34, // pop val, pop key, peek obj -> obj[key] = val (obj stays on stack)
75
- GET_PROP_COMPUTED: 35, // pop key, peek obj -> push obj[key] (computed: nums[i])
76
-
77
- MOD: 36, // a % b
78
- BAND: 37, // a & b
79
- BOR: 38, // a | b
80
- BXOR: 39, // a ^ b
81
- SHL: 40, // a << b
82
- SHR: 41, // a >> b
83
- USHR: 42, // a >>> b
84
-
85
- JUMP_IF_FALSE_OR_POP: 43, // && - if top falsy: jump (keep), else: pop, eval RHS
86
- JUMP_IF_TRUE_OR_POP: 44, // || - if top truthy: jump (keep), else: pop, eval RHS
87
-
88
- DELETE_PROP: 45,
89
- IN: 46, // a in b
90
- INSTANCEOF: 47, // a instanceof b
91
-
92
- // NEW
93
- LOAD_THIS: 48, // push frame.thisVal
94
- NEW: 49, // operand = argCount - construct a new object
95
- DUP: 50, // duplicate top of stack
96
- THROW: 51, // pop value, throw it
97
- LOOSE_EQ: 52, // a == b (abstract equality)
98
- LOOSE_NEQ: 53, // a != b (abstract inequality)
99
-
100
- FOR_IN_SETUP: 54, // pop obj -> build enumerable-key iterator -> push {keys,i}
101
- FOR_IN_NEXT: 55, // operand=exit_pc; pop iter; if done->jump; else push next key
102
-
103
- // Self-modifying bytecode
104
- PATCH: 56, // pop destPc; constants[operand]=word[]; write words into bytecode[destPc..]
105
-
106
- // Try-Catch
107
- TRY_SETUP: 57, // operand = catch_pc; push exception handler onto frame._handlerStack
108
- TRY_END: 58, // pop exception handler (normal exit from try body)
109
-
110
- // Getter / Setter (ES5 object literal accessor syntax)
111
- DEFINE_GETTER: 59, // pop fn, pop key, pop obj -> Object.defineProperty(obj, key, {get: fn})
112
- DEFINE_SETTER: 60, // pop fn, pop key, pop obj -> Object.defineProperty(obj, key, {set: fn})
113
-
114
- DEBUGGER: 61, // for dev/testing -- emits a "debugger" statement with a comment of the original source location
115
-
116
- // Push the raw integer operand directly onto the stack (no constant pool lookup).
117
- // Identical pipeline to JUMP ops: {type:"label"} pseudo-operands resolve to a
118
- // raw PC number that becomes the operand, which is pushed as-is at runtime.
119
- LOAD_INT: 62,
120
-
121
- // Reserved / unused opcode slot (formerly the inline DATA header word).
122
- // Kept to avoid renumbering; should never appear in compiled output.
123
- DATA: 63,
124
- };
125
-
126
- // Scope
127
- // Each function call gets its own Scope. Locals are resolved to
128
- // numeric slots at compile time -- zero name lookups at runtime.
129
- class Scope {
130
- parent: Scope | null;
131
- _locals: Map<string, number>;
132
- _next: number;
133
-
134
- constructor(parent = null) {
135
- this.parent = parent;
136
- this._locals = new Map(); // name -> slot index
137
- this._next = 0;
138
- }
139
-
140
- define(name) {
141
- if (!this._locals.has(name)) {
142
- this._locals.set(name, this._next++);
143
- }
144
- return this._locals.get(name);
145
- }
146
-
147
- // Walk up scope chain. If we fall off the top -> global.
148
- resolve(name) {
149
- if (this._locals.has(name)) {
150
- return { kind: "local", slot: this._locals.get(name) };
151
- }
152
- if (this.parent) return this.parent.resolve(name);
153
- return { kind: "global" };
154
- }
155
-
156
- get localCount() {
157
- return this._next;
158
- }
159
- }
160
-
161
- // FnContext
162
- // Compiler-side state for the function currently being compiled.
163
- // Distinct from runtime Frame -- this is compile-time only.
164
- class FnContext {
165
- upvalues: { name: string; isLocal: number; index: number }[];
166
- parentCtx: FnContext | null;
167
- scope: Scope;
168
- compiler: Compiler;
169
- bc: b.Instruction[];
170
-
171
- constructor(compiler, parentCtx = null) {
172
- this.compiler = compiler;
173
- this.parentCtx = parentCtx;
174
- this.scope = new Scope();
175
-
176
- this.bc = [];
177
- this.upvalues = []; // { name, isLocal, index }
178
- }
179
-
180
- // Find or register a captured variable as an upvalue.
181
- // isLocal=true -> captured directly from parent's locals[index]
182
- // isLocal=false -> relayed from parent's own upvalue list[index]
183
- addUpvalue(name, isLocal, index) {
184
- const existing = this.upvalues.findIndex((u) => u.name === name);
185
- if (existing !== -1) return existing;
186
- const idx = this.upvalues.length;
187
- this.upvalues.push({ name, isLocal, index: index });
188
- return idx;
189
- }
190
- }
191
-
192
- // Compiler
193
- export class Compiler {
194
- fnDescriptors: any[];
195
- bytecode: b.Bytecode;
196
- mainStartPc: number;
197
-
198
- _currentCtx: FnContext | null;
199
- _pendingLabel: string | null;
200
- _forInCount: number;
201
- _labelCount: number;
202
- _loopStack: {
203
- type: "loop" | "switch" | "block";
204
- label: string | null;
205
- // Label that break statements targeting this entry should jump to.
206
- breakLabel: string;
207
- // Label that continue statements targeting this entry should jump to.
208
- continueLabel: string;
209
- }[];
210
-
211
- options: Options;
212
- serializer: Serializer;
213
-
214
- OP: Partial<typeof OP_ORIGINAL>;
215
- OP_NAME: Record<number, string>;
216
- JUMP_OPS: Set<number>;
217
-
218
- emit(bc: b.Bytecode, instr: b.Instruction, node: t.Node) {
219
- bc.push(instr);
220
-
221
- instr[SOURCE_NODE_SYM] = node;
222
- }
223
-
224
- // DO NOT USE THIS KEY UNLESS YOU ARE "RESOLVE CONSTANTS"
225
- // CONSTANTS DURING COMPILATION MUST BE USED BY REFERENCE WITH b.constantOperand("myConstantHere")
226
- constants: any[];
227
-
228
- constructor(options: Options = DEFAULT_OPTIONS) {
229
- this.options = options;
230
- this.fnDescriptors = []; // populated in pass 1
231
- this.bytecode = [];
232
- this.mainStartPc = 0;
233
- this._currentCtx = null; // FnContext of the function being compiled, null at top-level
234
- this._loopStack = []; // per active loop/switch/block/try
235
- this._pendingLabel = null;
236
- this._forInCount = 0; // counter for synthetic for-in iterator global names
237
- this._labelCount = 0; // monotonically increasing counter for unique label names
238
-
239
- this.serializer = new Serializer(this);
240
-
241
- this.OP = {};
242
- // Construct randomized opcode mapping
243
- if (this.options.randomizeOpcodes) {
244
- let usedNumbers = new Set<number>();
245
- for (const key in OP_ORIGINAL) {
246
- let val;
247
- do {
248
- val = Math.floor(Math.random() * 256);
249
- } while (usedNumbers.has(val));
250
- usedNumbers.add(val);
251
- this.OP[key] = val;
252
- }
253
- } else {
254
- this.OP = OP_ORIGINAL;
255
- }
256
-
257
- // Reverse map for comment generation
258
- this.OP_NAME = Object.fromEntries(
259
- Object.entries(this.OP).map(([k, v]) => [v, k]),
260
- );
261
-
262
- this.JUMP_OPS = new Set([
263
- this.OP.JUMP,
264
- this.OP.JUMP_IF_FALSE,
265
- this.OP.JUMP_IF_TRUE_OR_POP,
266
- this.OP.JUMP_IF_FALSE_OR_POP,
267
- this.OP.FOR_IN_NEXT,
268
- this.OP.TRY_SETUP, // catch_pc operand needs offset adjustment like jump targets
269
- ]);
270
- }
271
-
272
- // Generate a globally unique label string with an optional hint for readability.
273
- _makeLabel(hint = ""): string {
274
- var id = this._labelCount++;
275
- return `${hint || "L"}_${id}`;
276
- }
277
-
278
- // Variable resolution
279
- // Walks up the FnContext chain. Crossing a context boundary means
280
- // we're capturing from an outer function - register an upvalue.
281
- _resolve(name, ctx) {
282
- if (!ctx) return { kind: "global" };
283
-
284
- // 1. Own locals
285
- if (ctx.scope._locals.has(name)) {
286
- return { kind: "local", slot: ctx.scope._locals.get(name) };
287
- }
288
-
289
- // 2. No parent context -> must be global
290
- if (!ctx.parentCtx) return { kind: "global" };
291
-
292
- // 3. Ask parent -- recurse up the chain
293
- const parentResult = this._resolve(name, ctx.parentCtx);
294
- if (parentResult.kind === "global") return { kind: "global" };
295
-
296
- // 4. Parent has it (as local or upvalue) -- register an upvalue here.
297
- // isLocal=true means "take it straight from parent's locals[index]"
298
- // isLocal=false means "relay parent's upvalue[index]" (multi-level capture)
299
- const isLocal = parentResult.kind === "local";
300
- const index = isLocal ? parentResult.slot : parentResult.index;
301
- const uvIdx = ctx.addUpvalue(name, isLocal, index);
302
- return { kind: "upvalue", index: uvIdx };
303
- }
304
-
305
- // Entry point
306
- compile(source: string) {
307
- const ast = parse(source, { sourceType: "script" });
308
-
309
- return this.compileAST(ast);
310
- }
311
-
312
- compileAST(ast: t.File) {
313
- // Pass 1 - compile every FunctionDeclaration into a descriptor.
314
- // Traverse finds them regardless of nesting depth.
315
- traverse(ast, {
316
- FunctionDeclaration: (path) => {
317
- // Only handle top-level functions for this MVP.
318
- // (Parent is Program node)
319
- if (path.parent.type !== "Program") return;
320
- this._compileFunctionDecl(path.node);
321
- path.skip(); // don't recurse into nested functions
322
- },
323
- });
324
-
325
- // Pass 2 -- compile top-level statements into BYTECODE.
326
- this._compileMain(ast.program.body);
327
-
328
- return this.bytecode;
329
- }
330
-
331
- // Function Declaration
332
-
333
- _compileFunctionDecl(node: t.FunctionDeclaration | t.FunctionExpression) {
334
- // Reserve a slot in fnDescriptors NOW, before compiling the body, so that
335
- // any nested _compileFunctionDecl calls see the correct .length and get a
336
- // distinct _fnIdx. The placeholder object is mutated in-place below once
337
- // the body and header are ready.
338
- var fnIdx = this.fnDescriptors.length;
339
- const entryLabel = this._makeLabel(`fn_${fnIdx}`);
340
- var desc: any = {}; // placeholder — filled in after compilation
341
- this.fnDescriptors.push(desc);
342
-
343
- // Create a context whose parent is whatever we're currently compiling.
344
- // This is what lets _resolve cross function boundaries correctly.
345
- const ctx = new FnContext(this, this._currentCtx);
346
- const savedCtx = this._currentCtx;
347
- this._currentCtx = ctx;
348
-
349
- // Isolate the loop stack so that try/loop entries from the outer scope
350
- // don't cause spurious TRY_END / extra jumps inside this function body.
351
- const savedLoopStack = this._loopStack;
352
- this._loopStack = [];
353
-
354
- // Params occupy the first N local slots (args are copied in on CALL)
355
- for (const param of node.params) {
356
- let identifier = param.type === "AssignmentPattern" ? param.left : param;
357
- ok(
358
- identifier.type === "Identifier",
359
- "Only simple identifiers allowed as parameters",
360
- );
361
-
362
- ctx.scope.define(identifier.name);
363
- }
364
-
365
- // Reserve the next slot for the implicit `arguments` object.
366
- // Slot index will always equal paramCount (params are 0..paramCount-1).
367
- ctx.scope.define("arguments");
368
-
369
- // Pass 2: emit default-value guards at top of fn body
370
- // Mirrors what JS engines do: if the caller passed undefined (or
371
- // nothing), evaluate the default expression and overwrite the slot.
372
- for (const param of node.params) {
373
- if (param.type !== "AssignmentPattern") continue;
374
-
375
- const slot = ctx.scope._locals.get((param.left as t.Identifier).name);
376
- const skipLabel = this._makeLabel("param_skip");
377
-
378
- // if (param === undefined) param = <default expr>
379
- this.emit(ctx.bc, [this.OP.LOAD_LOCAL, slot], param);
380
- this.emit(
381
- ctx.bc,
382
- [this.OP.LOAD_CONST, b.constantOperand(undefined)],
383
- param,
384
- );
385
- this.emit(ctx.bc, [this.OP.EQ], param);
386
- this.emit(
387
- ctx.bc,
388
- [this.OP.JUMP_IF_FALSE, { type: "label", label: skipLabel }],
389
- param,
390
- );
391
-
392
- this._compileExpr(param.right, ctx.scope, ctx.bc); // eval default
393
- this.emit(ctx.bc, [this.OP.STORE_LOCAL, slot], param);
394
-
395
- this.emit(
396
- ctx.bc,
397
- [null, { type: "defineLabel", label: skipLabel }],
398
- param,
399
- );
400
- }
401
-
402
- for (const stmt of node.body.body) {
403
- this._compileStatement(stmt, ctx.scope, ctx.bc);
404
- }
405
-
406
- // If we fall off the end of the function, implicitly return undefined.
407
- this.emit(ctx.bc, [this.OP.LOAD_CONST, b.constantOperand(undefined)], node);
408
- this.emit(ctx.bc, [this.OP.RETURN], node);
409
-
410
- this._currentCtx = savedCtx; // restore before touching fnDescriptors
411
- this._loopStack = savedLoopStack;
412
-
413
- (node as any)._fnIdx = fnIdx;
414
-
415
- // Fill the placeholder that was reserved at the top of this function.
416
- // Metadata (paramCount, localCount, upvalues) is stored on desc and emitted
417
- // as LOAD_INT instructions onto the value stack at each MAKE_CLOSURE call
418
- // site — the runtime reads them from the stack, not from DATA words.
419
- desc.name = node.id?.name || "<anonymous>";
420
- desc.entryLabel = entryLabel;
421
- desc.bytecode = ctx.bc as b.Bytecode;
422
- desc._fnIdx = fnIdx;
423
- desc.paramCount = node.params.length;
424
- desc.localCount = ctx.scope.localCount;
425
- desc.upvalues = ctx.upvalues.slice();
426
-
427
- return desc;
428
- }
429
-
430
- // Emit LOAD_INT instructions that push closure metadata onto the value stack
431
- // immediately before a MAKE_CLOSURE instruction. The runtime pops these
432
- // values in MAKE_CLOSURE instead of reading DATA words from bytecode.
433
- //
434
- // Stack layout when MAKE_CLOSURE executes (top is rightmost):
435
- // [isLocal_0, idx_0, ..., isLocal_N-1, idx_N-1, uvCount, localCount, paramCount]
436
- _emitClosureMetadata(desc: any, node: t.Node, bc: b.Bytecode) {
437
- // Push each upvalue descriptor in order; runtime pops them in reverse.
438
- for (const uv of desc.upvalues) {
439
- this.emit(bc, [this.OP.LOAD_INT, uv.isLocal ? 1 : 0], node);
440
- this.emit(bc, [this.OP.LOAD_INT, uv.index], node);
441
- }
442
- this.emit(bc, [this.OP.LOAD_INT, desc.upvalues.length], node);
443
- this.emit(bc, [this.OP.LOAD_INT, desc.localCount], node);
444
- this.emit(bc, [this.OP.LOAD_INT, desc.paramCount], node);
445
- }
446
-
447
- // Main (top-level)
448
- _compileMain(body: t.Statement[]) {
449
- const bc = this.bytecode;
450
-
451
- // Hoist all FunctionDeclarations: MAKE_CLOSURE -> STORE_GLOBAL
452
- // (mirrors JS hoisting -- functions are available before other code)
453
- for (const node of body) {
454
- if (node.type !== "FunctionDeclaration") continue;
455
- const desc = this.fnDescriptors.find(
456
- (d) => d._fnIdx === (node as any)._fnIdx,
457
- );
458
- const nameRef = b.constantOperand(node.id.name);
459
- this._emitClosureMetadata(desc, node, bc);
460
- this.emit(
461
- bc,
462
- [this.OP.MAKE_CLOSURE, { type: "label", label: desc.entryLabel }],
463
- node,
464
- );
465
- this.emit(bc, [this.OP.STORE_GLOBAL, nameRef], node);
466
- }
467
-
468
- // Compile everything else in order
469
- for (const node of body) {
470
- if (node.type === "FunctionDeclaration") continue;
471
- this._compileStatement(node, null, bc); // null scope -> global context
472
- }
473
-
474
- this.emit(bc, [this.OP.RETURN], null); // end program
475
-
476
- // Append all function bodies. Each function's entryLabel (already generated
477
- // in _compileFunctionDecl) points directly to the first body instruction;
478
- // metadata is pushed onto the stack at each call site, not stored inline.
479
- for (const descriptor of this.fnDescriptors) {
480
- this.bytecode.push([
481
- null,
482
- { type: "defineLabel", label: descriptor.entryLabel },
483
- ]);
484
- for (const instr of descriptor.bytecode) {
485
- this.bytecode.push(instr);
486
- }
487
- }
488
-
489
- if (this.bytecode.length > 0xffffff)
490
- throw new Error(
491
- `Program too large: ${this.bytecode.length} instructions, max 16,777,215`,
492
- );
493
-
494
- // if (this.constants.items.length > 0xffffff)
495
- // throw new Error(
496
- // `Constant pool too large: ${this.constants.items.length} entries, max 16,777,215`,
497
- // );
498
- }
499
-
500
- // Statements
501
- _compileStatement(node: t.Statement, scope: Scope, bc: b.Bytecode) {
502
- switch (node.type) {
503
- case "EmptyStatement": {
504
- // nothing to emit -- bare semicolon is a no-op
505
- break;
506
- }
507
-
508
- case "DebuggerStatement":
509
- this.emit(bc, [this.OP.DEBUGGER], node);
510
- break;
511
-
512
- case "BlockStatement": {
513
- for (const stmt of node.body) {
514
- this._compileStatement(stmt, scope, bc);
515
- }
516
- break;
517
- }
518
-
519
- case "FunctionDeclaration": {
520
- // Nested function -- compile it into a descriptor, then emit
521
- // MAKE_CLOSURE so it's captured as a live closure at runtime.
522
- // (_compileFunctionDecl pushes/pops _currentCtx internally)
523
- const desc = this._compileFunctionDecl(node);
524
- this._emitClosureMetadata(desc, node, bc);
525
- this.emit(
526
- bc,
527
- [this.OP.MAKE_CLOSURE, { type: "label", label: desc.entryLabel }],
528
- node,
529
- );
530
- if (scope) {
531
- const slot = scope.define(node.id.name);
532
- this.emit(bc, [this.OP.STORE_LOCAL, slot], node);
533
- } else {
534
- this.emit(
535
- bc,
536
- [this.OP.STORE_GLOBAL, b.constantOperand(node.id.name)],
537
- node,
538
- );
539
- }
540
- break;
541
- }
542
-
543
- case "ThrowStatement": {
544
- this._compileExpr(node.argument, scope, bc);
545
- this.emit(bc, [this.OP.THROW], node);
546
- break;
547
- }
548
-
549
- case "ReturnStatement": {
550
- if (node.argument) {
551
- this._compileExpr(node.argument, scope, bc);
552
- } else {
553
- this.emit(
554
- bc,
555
- [this.OP.LOAD_CONST, b.constantOperand(undefined)],
556
- node,
557
- );
558
- }
559
- // Disarm any open try handlers before leaving the function.
560
- // TRY_END only touches frame._handlerStack, not the value stack,
561
- // so the return value sitting on top is safe.
562
- for (let _ri = this._loopStack.length - 1; _ri >= 0; _ri--) {
563
- if ((this._loopStack[_ri].type as any) === "try") {
564
- this.emit(bc, [this.OP.TRY_END], node);
565
- }
566
- }
567
- this.emit(bc, [this.OP.RETURN], node);
568
- break;
569
- }
570
-
571
- case "ExpressionStatement": {
572
- this._compileExpr(node.expression, scope, bc);
573
- this.emit(bc, [this.OP.POP], node); // discard return value of statement-level expressions
574
- break;
575
- }
576
-
577
- case "VariableDeclaration": {
578
- for (const decl of node.declarations) {
579
- // Push the initialiser (or undefined if absent)
580
- if (decl.init) {
581
- this._compileExpr(decl.init, scope, bc);
582
- } else {
583
- this.emit(
584
- bc,
585
- [this.OP.LOAD_CONST, b.constantOperand(undefined)],
586
- node,
587
- );
588
- }
589
-
590
- ok(
591
- decl.id.type === "Identifier",
592
- "Only simple identifiers can be declared",
593
- );
594
-
595
- // Store: local slot if inside a function, global name otherwise
596
- if (scope) {
597
- const slot = scope.define(decl.id.name);
598
- this.emit(bc, [this.OP.STORE_LOCAL, slot], node);
599
- } else {
600
- this.emit(
601
- bc,
602
- [this.OP.STORE_GLOBAL, b.constantOperand(decl.id.name)],
603
- node,
604
- );
605
- }
606
- }
607
- break;
608
- }
609
-
610
- case "IfStatement": {
611
- const elseOrEndLabel = this._makeLabel("if_else");
612
- // 1. Compile the test expression -> leaves a value on the stack
613
- this._compileExpr(node.test, scope, bc);
614
- // 2. Emit JUMP_IF_FALSE to the else branch (or end if no else)
615
- this.emit(
616
- bc,
617
- [this.OP.JUMP_IF_FALSE, { type: "label", label: elseOrEndLabel }],
618
- node,
619
- );
620
- // 3. Compile the consequent block (the "then" branch)
621
- const consequentBody =
622
- node.consequent.type === "BlockStatement"
623
- ? node.consequent.body
624
- : [node.consequent];
625
- for (const stmt of consequentBody) {
626
- this._compileStatement(stmt, scope, bc);
627
- }
628
- if (node.alternate) {
629
- // 4a. Consequent needs to jump OVER the else block when done
630
- const endLabel = this._makeLabel("if_end");
631
- this.emit(
632
- bc,
633
- [this.OP.JUMP, { type: "label", label: endLabel }],
634
- node,
635
- );
636
- // Mark start of else
637
- this.emit(
638
- bc,
639
- [null, { type: "defineLabel", label: elseOrEndLabel }],
640
- node,
641
- );
642
- // 5. Compile the alternate (else) block
643
- const altBody =
644
- node.alternate.type === "BlockStatement"
645
- ? node.alternate.body
646
- : [node.alternate]; // handles `else if` -- it's just a nested IfStatement
647
- for (const stmt of altBody) {
648
- this._compileStatement(stmt, scope, bc);
649
- }
650
- // Mark end (consequent's jump lands here)
651
- this.emit(bc, [null, { type: "defineLabel", label: endLabel }], node);
652
- } else {
653
- // 4b. No else -- label lands right after the then block
654
- this.emit(
655
- bc,
656
- [null, { type: "defineLabel", label: elseOrEndLabel }],
657
- node,
658
- );
659
- }
660
- break;
661
- }
662
-
663
- case "WhileStatement": {
664
- const _wLabel = this._pendingLabel;
665
- this._pendingLabel = null;
666
-
667
- const loopTopLabel = this._makeLabel("while_top");
668
- const exitLabel = this._makeLabel("while_exit");
669
-
670
- this._loopStack.push({
671
- type: "loop",
672
- label: _wLabel,
673
- breakLabel: exitLabel,
674
- continueLabel: loopTopLabel, // continue re-evaluates the test
675
- });
676
-
677
- this.emit(
678
- bc,
679
- [null, { type: "defineLabel", label: loopTopLabel }],
680
- node,
681
- );
682
- this._compileExpr(node.test, scope, bc);
683
- this.emit(
684
- bc,
685
- [this.OP.JUMP_IF_FALSE, { type: "label", label: exitLabel }],
686
- node,
687
- );
688
-
689
- const whileBody =
690
- node.body.type === "BlockStatement" ? node.body.body : [node.body];
691
- for (const stmt of whileBody) {
692
- this._compileStatement(stmt, scope, bc);
693
- }
694
-
695
- this.emit(
696
- bc,
697
- [this.OP.JUMP, { type: "label", label: loopTopLabel }],
698
- node,
699
- );
700
- this.emit(bc, [null, { type: "defineLabel", label: exitLabel }], node);
701
-
702
- this._loopStack.pop();
703
- break;
704
- }
705
-
706
- case "DoWhileStatement": {
707
- const _dwLabel = this._pendingLabel;
708
- this._pendingLabel = null;
709
-
710
- const loopTopLabel = this._makeLabel("dowhile_top");
711
- const continueLabel = this._makeLabel("dowhile_cont");
712
- const exitLabel = this._makeLabel("dowhile_exit");
713
-
714
- this._loopStack.push({
715
- type: "loop",
716
- label: _dwLabel,
717
- breakLabel: exitLabel,
718
- continueLabel: continueLabel, // continue falls to the test
719
- });
720
-
721
- this.emit(
722
- bc,
723
- [null, { type: "defineLabel", label: loopTopLabel }],
724
- node,
725
- );
726
-
727
- const doWhileBody =
728
- node.body.type === "BlockStatement" ? node.body.body : [node.body];
729
- for (const stmt of doWhileBody) {
730
- this._compileStatement(stmt, scope, bc);
731
- }
732
-
733
- // continue -> skip rest of body, fall through to test
734
- this.emit(
735
- bc,
736
- [null, { type: "defineLabel", label: continueLabel }],
737
- node,
738
- );
739
- this._compileExpr(node.test, scope, bc);
740
- this.emit(
741
- bc,
742
- [this.OP.JUMP_IF_FALSE, { type: "label", label: exitLabel }],
743
- node,
744
- );
745
- this.emit(
746
- bc,
747
- [this.OP.JUMP, { type: "label", label: loopTopLabel }],
748
- node,
749
- );
750
-
751
- this.emit(bc, [null, { type: "defineLabel", label: exitLabel }], node);
752
-
753
- this._loopStack.pop();
754
- break;
755
- }
756
-
757
- case "ForStatement": {
758
- const _fLabel = this._pendingLabel;
759
- this._pendingLabel = null;
760
-
761
- const loopTopLabel = this._makeLabel("for_top");
762
- const exitLabel = this._makeLabel("for_exit");
763
- // continue jumps to the update clause if present, else straight to test
764
- const updateLabel = node.update
765
- ? this._makeLabel("for_update")
766
- : loopTopLabel;
767
-
768
- this._loopStack.push({
769
- type: "loop",
770
- label: _fLabel,
771
- breakLabel: exitLabel,
772
- continueLabel: updateLabel,
773
- });
774
-
775
- if (node.init) {
776
- if (node.init.type === "VariableDeclaration") {
777
- this._compileStatement(node.init, scope, bc);
778
- } else {
779
- this._compileExpr(node.init, scope, bc);
780
- this.emit(bc, [this.OP.POP], node);
781
- }
782
- }
783
-
784
- this.emit(
785
- bc,
786
- [null, { type: "defineLabel", label: loopTopLabel }],
787
- node,
788
- );
789
- if (node.test) {
790
- this._compileExpr(node.test, scope, bc);
791
- this.emit(
792
- bc,
793
- [this.OP.JUMP_IF_FALSE, { type: "label", label: exitLabel }],
794
- node,
795
- );
796
- }
797
-
798
- const forBody =
799
- node.body.type === "BlockStatement" ? node.body.body : [node.body];
800
- for (const stmt of forBody) {
801
- this._compileStatement(stmt, scope, bc);
802
- }
803
-
804
- // continue -> run update (if any) then back to test
805
- if (node.update) {
806
- this.emit(
807
- bc,
808
- [null, { type: "defineLabel", label: updateLabel }],
809
- node,
810
- );
811
- this._compileExpr(node.update, scope, bc);
812
- this.emit(bc, [this.OP.POP], node);
813
- }
814
-
815
- this.emit(
816
- bc,
817
- [this.OP.JUMP, { type: "label", label: loopTopLabel }],
818
- node,
819
- );
820
- this.emit(bc, [null, { type: "defineLabel", label: exitLabel }], node);
821
-
822
- this._loopStack.pop();
823
- break;
824
- }
825
-
826
- case "BreakStatement": {
827
- // Find the jump target in the loop stack.
828
- let _bTargetIdx = -1;
829
- if (node.label) {
830
- const _bLabelName = node.label.name;
831
- for (let _bi = this._loopStack.length - 1; _bi >= 0; _bi--) {
832
- if (this._loopStack[_bi].label === _bLabelName) {
833
- _bTargetIdx = _bi;
834
- break;
835
- }
836
- }
837
- if (_bTargetIdx === -1)
838
- throw new Error(`Label '${node.label.name}' not found`);
839
- } else {
840
- // Find innermost loop/switch/block (skip "try" entries)
841
- for (let _bi = this._loopStack.length - 1; _bi >= 0; _bi--) {
842
- if ((this._loopStack[_bi].type as any) !== "try") {
843
- _bTargetIdx = _bi;
844
- break;
845
- }
846
- }
847
- if (_bTargetIdx === -1) throw new Error("break outside loop");
848
- }
849
- // Emit TRY_END for every open try block between here and the target.
850
- for (let _bi = this._loopStack.length - 1; _bi > _bTargetIdx; _bi--) {
851
- if ((this._loopStack[_bi].type as any) === "try") {
852
- this.emit(bc, [this.OP.TRY_END], node);
853
- }
854
- }
855
- this.emit(
856
- bc,
857
- [
858
- this.OP.JUMP,
859
- { type: "label", label: this._loopStack[_bTargetIdx].breakLabel },
860
- ],
861
- node,
862
- );
863
- break;
864
- }
865
-
866
- case "ContinueStatement": {
867
- // Find the target loop in the loop stack.
868
- let _cTargetIdx = -1;
869
- if (node.label) {
870
- const _cLabelName = node.label.name;
871
- for (let _ci = this._loopStack.length - 1; _ci >= 0; _ci--) {
872
- if (
873
- this._loopStack[_ci].label === _cLabelName &&
874
- this._loopStack[_ci].type === "loop"
875
- ) {
876
- _cTargetIdx = _ci;
877
- break;
878
- }
879
- }
880
- if (_cTargetIdx === -1)
881
- throw new Error(
882
- `Label '${node.label.name}' not found for continue`,
883
- );
884
- } else {
885
- // Find the innermost loop (skip switch, block, and try contexts)
886
- for (let _ci = this._loopStack.length - 1; _ci >= 0; _ci--) {
887
- if (this._loopStack[_ci].type === "loop") {
888
- _cTargetIdx = _ci;
889
- break;
890
- }
891
- }
892
- if (_cTargetIdx === -1) throw new Error("continue outside loop");
893
- }
894
- // Emit TRY_END for every open try block between here and the target loop.
895
- for (let _ci = this._loopStack.length - 1; _ci > _cTargetIdx; _ci--) {
896
- if ((this._loopStack[_ci].type as any) === "try") {
897
- this.emit(bc, [this.OP.TRY_END], node);
898
- }
899
- }
900
- this.emit(
901
- bc,
902
- [
903
- this.OP.JUMP,
904
- {
905
- type: "label",
906
- label: this._loopStack[_cTargetIdx].continueLabel,
907
- },
908
- ],
909
- node,
910
- );
911
- break;
912
- }
913
-
914
- case "SwitchStatement": {
915
- const _swLabel = this._pendingLabel;
916
- this._pendingLabel = null;
917
-
918
- const switchBreakLabel = this._makeLabel("sw_break");
919
-
920
- this._loopStack.push({
921
- type: "switch",
922
- label: _swLabel,
923
- breakLabel: switchBreakLabel,
924
- continueLabel: switchBreakLabel, // not used for switch
925
- });
926
-
927
- // Compile the discriminant and leave it on the stack
928
- this._compileExpr(node.discriminant, scope, bc);
929
-
930
- const cases = node.cases;
931
- const defaultIdx = cases.findIndex((c) => c.test === null);
932
-
933
- // Pre-allocate a label for each case body so dispatch can reference them
934
- const caseLabels = cases.map((_, i) => this._makeLabel(`sw_case_${i}`));
935
-
936
- // Dispatch section: for each non-default case, check and jump to its body
937
- for (let i = 0; i < cases.length; i++) {
938
- const cas = cases[i];
939
- if (cas.test === null) continue; // skip default in dispatch
940
-
941
- const nextCheckLabel = this._makeLabel("sw_next");
942
- this.emit(bc, [this.OP.DUP], node);
943
- this._compileExpr(cas.test, scope, bc);
944
- this.emit(bc, [this.OP.EQ], node);
945
- // If not matched, fall through to the next check
946
- this.emit(
947
- bc,
948
- [this.OP.JUMP_IF_FALSE, { type: "label", label: nextCheckLabel }],
949
- node,
950
- );
951
- // If matched, jump directly to this case's body
952
- this.emit(
953
- bc,
954
- [this.OP.JUMP, { type: "label", label: caseLabels[i] }],
955
- node,
956
- );
957
- this.emit(
958
- bc,
959
- [null, { type: "defineLabel", label: nextCheckLabel }],
960
- node,
961
- );
962
- }
963
-
964
- // No case matched: jump to default body or exit (which pops discriminant)
965
- this.emit(
966
- bc,
967
- [
968
- this.OP.JUMP,
969
- {
970
- type: "label",
971
- label:
972
- defaultIdx !== -1 ? caseLabels[defaultIdx] : switchBreakLabel,
973
- },
974
- ],
975
- node,
976
- );
977
-
978
- // Body section: compile all case bodies in source order (fallthrough intact)
979
- for (let i = 0; i < cases.length; i++) {
980
- this.emit(
981
- bc,
982
- [null, { type: "defineLabel", label: caseLabels[i] }],
983
- node,
984
- );
985
- for (const stmt of cases[i].consequent) {
986
- this._compileStatement(stmt, scope, bc);
987
- }
988
- }
989
-
990
- // break label lands here; pop the discriminant and continue after switch
991
- this.emit(
992
- bc,
993
- [null, { type: "defineLabel", label: switchBreakLabel }],
994
- node,
995
- );
996
- this.emit(bc, [this.OP.POP], node);
997
-
998
- this._loopStack.pop();
999
- break;
1000
- }
1001
-
1002
- case "LabeledStatement": {
1003
- const _lName = node.label.name;
1004
- const _lBody = node.body;
1005
- const _lIsLoop =
1006
- _lBody.type === "ForStatement" ||
1007
- _lBody.type === "WhileStatement" ||
1008
- _lBody.type === "DoWhileStatement" ||
1009
- _lBody.type === "ForInStatement";
1010
- const _lIsSwitch = _lBody.type === "SwitchStatement";
1011
-
1012
- if (_lIsLoop || _lIsSwitch) {
1013
- // Pass label down to the loop/switch handler via _pendingLabel
1014
- this._pendingLabel = _lName;
1015
- this._compileStatement(_lBody, scope, bc);
1016
- this._pendingLabel = null; // safety clear if handler didn't consume it
1017
- } else {
1018
- // Non-loop labeled statement (e.g. labeled block) -- only break is valid
1019
- const blockBreakLabel = this._makeLabel("block_break");
1020
- this._loopStack.push({
1021
- type: "block",
1022
- label: _lName,
1023
- breakLabel: blockBreakLabel,
1024
- continueLabel: blockBreakLabel, // unused
1025
- });
1026
- this._compileStatement(_lBody, scope, bc);
1027
- this._loopStack.pop();
1028
- this.emit(
1029
- bc,
1030
- [null, { type: "defineLabel", label: blockBreakLabel }],
1031
- node,
1032
- );
1033
- }
1034
- break;
1035
- }
1036
-
1037
- case "ForInStatement": {
1038
- const _fiLabel = this._pendingLabel;
1039
- this._pendingLabel = null;
1040
-
1041
- // Evaluate the object expression -> on stack
1042
- this._compileExpr(node.right, scope, bc);
1043
- // FOR_IN_SETUP: pops obj, pushes iterator {keys, i}
1044
- this.emit(bc, [this.OP.FOR_IN_SETUP], node);
1045
-
1046
- // Store iterator in a hidden slot so break/continue need no cleanup
1047
- let emitLoadIter: () => void;
1048
- let emitStoreIter: () => void;
1049
- if (scope) {
1050
- // Reserve a hidden local slot (no name mapping needed)
1051
- const iterSlot = scope._next++;
1052
- emitLoadIter = () =>
1053
- this.emit(bc, [this.OP.LOAD_LOCAL, iterSlot], node);
1054
- emitStoreIter = () =>
1055
- this.emit(bc, [this.OP.STORE_LOCAL, iterSlot], node);
1056
- } else {
1057
- // Top level -- use a synthetic global that won't collide with user code
1058
- const iterNameIdx = b.constantOperand("__fi" + this._forInCount++);
1059
- emitLoadIter = () =>
1060
- this.emit(bc, [this.OP.LOAD_GLOBAL, iterNameIdx], node);
1061
- emitStoreIter = () =>
1062
- this.emit(bc, [this.OP.STORE_GLOBAL, iterNameIdx], node);
1063
- }
1064
- emitStoreIter();
1065
-
1066
- const loopTopLabel = this._makeLabel("forin_top");
1067
- const exitLabel = this._makeLabel("forin_exit");
1068
-
1069
- this._loopStack.push({
1070
- type: "loop",
1071
- label: _fiLabel,
1072
- breakLabel: exitLabel,
1073
- continueLabel: loopTopLabel, // continue re-checks the iterator
1074
- });
1075
-
1076
- this.emit(
1077
- bc,
1078
- [null, { type: "defineLabel", label: loopTopLabel }],
1079
- node,
1080
- );
1081
-
1082
- // Load iterator, attempt to get next key
1083
- emitLoadIter();
1084
- this.emit(
1085
- bc,
1086
- [this.OP.FOR_IN_NEXT, { type: "label", label: exitLabel }],
1087
- node,
1088
- );
1089
-
1090
- // Assign the key (now on top of stack) to the loop variable
1091
- if (node.left.type === "VariableDeclaration") {
1092
- const identifier = node.left.declarations[0].id;
1093
- ok(
1094
- identifier.type === "Identifier",
1095
- "Only simple identifiers can be declared in for-in loops",
1096
- );
1097
- const name = identifier.name;
1098
- if (scope) {
1099
- const slot = scope.define(name);
1100
- this.emit(bc, [this.OP.STORE_LOCAL, slot], node);
1101
- } else {
1102
- this.emit(
1103
- bc,
1104
- [this.OP.STORE_GLOBAL, b.constantOperand(name)],
1105
- node,
1106
- );
1107
- }
1108
- } else if (node.left.type === "Identifier") {
1109
- const res = this._resolve(node.left.name, this._currentCtx);
1110
- if (res.kind === "local") {
1111
- this.emit(bc, [this.OP.STORE_LOCAL, res.slot], node);
1112
- } else if (res.kind === "upvalue") {
1113
- this.emit(bc, [this.OP.STORE_UPVALUE, res.index], node);
1114
- } else {
1115
- this.emit(
1116
- bc,
1117
- [this.OP.STORE_GLOBAL, b.constantOperand(node.left.name)],
1118
- node,
1119
- );
1120
- }
1121
- } else {
1122
- const src = generate(node.left).code;
1123
- throw new Error(
1124
- `Unsupported for-in left-hand side: ${node.left.type}\n -> ${src}`,
1125
- );
1126
- }
1127
-
1128
- // Compile the loop body
1129
- const fiBody =
1130
- node.body.type === "BlockStatement" ? node.body.body : [node.body];
1131
- for (const stmt of fiBody) {
1132
- this._compileStatement(stmt, scope, bc);
1133
- }
1134
-
1135
- this.emit(
1136
- bc,
1137
- [this.OP.JUMP, { type: "label", label: loopTopLabel }],
1138
- node,
1139
- );
1140
- this.emit(bc, [null, { type: "defineLabel", label: exitLabel }], node);
1141
-
1142
- this._loopStack.pop();
1143
- break;
1144
- }
1145
-
1146
- case "TryStatement": {
1147
- if (node.finalizer) {
1148
- throw new Error(
1149
- "try..finally is not supported. Use a helper function instead",
1150
- );
1151
- }
1152
- if (!node.handler) {
1153
- // try without catch requires finally not supported
1154
- throw new Error(
1155
- "try without catch is not supported (requires finally).",
1156
- );
1157
- }
1158
-
1159
- const catchLabel = this._makeLabel("catch");
1160
- const afterCatchLabel = this._makeLabel("after_catch");
1161
-
1162
- // Emit TRY_SETUP with the catch block's label as the handler PC.
1163
- // At runtime: saves stack depth + frame stack depth, pushes handler.
1164
- this.emit(
1165
- bc,
1166
- [this.OP.TRY_SETUP, { type: "label", label: catchLabel }],
1167
- node,
1168
- );
1169
-
1170
- // Track the open try block so that break/continue/return inside the
1171
- // try body can emit the matching TRY_END before their jump.
1172
- this._loopStack.push({
1173
- type: "try" as any,
1174
- label: null,
1175
- breakLabel: "", // unused
1176
- continueLabel: "", // unused
1177
- });
1178
-
1179
- // Compile try body
1180
- for (const stmt of node.block.body) {
1181
- this._compileStatement(stmt, scope, bc);
1182
- }
1183
-
1184
- // Done compiling the try body — pop the tracking entry.
1185
- this._loopStack.pop();
1186
-
1187
- // Normal exit: disarm the exception handler.
1188
- this.emit(bc, [this.OP.TRY_END], node);
1189
-
1190
- // Jump over the catch block on normal path.
1191
- this.emit(
1192
- bc,
1193
- [this.OP.JUMP, { type: "label", label: afterCatchLabel }],
1194
- node,
1195
- );
1196
-
1197
- // Catch block: exception is on top of the stack (pushed by the VM).
1198
- this.emit(bc, [null, { type: "defineLabel", label: catchLabel }], node);
1199
-
1200
- const handler = node.handler;
1201
- if (handler.param) {
1202
- // Bind the exception value to the catch variable.
1203
- const name = (handler.param as t.Identifier).name;
1204
- if (scope) {
1205
- const slot = scope.define(name);
1206
- this.emit(bc, [this.OP.STORE_LOCAL, slot], node);
1207
- } else {
1208
- this.emit(
1209
- bc,
1210
- [this.OP.STORE_GLOBAL, b.constantOperand(name)],
1211
- node,
1212
- );
1213
- }
1214
- } else {
1215
- // Optional catch binding (catch without a variable — ES2019+)
1216
- this.emit(bc, [this.OP.POP], node);
1217
- }
1218
-
1219
- // Compile catch body
1220
- for (const stmt of handler.body.body) {
1221
- this._compileStatement(stmt, scope, bc);
1222
- }
1223
-
1224
- // Normal-path jump lands here (after the catch block).
1225
- this.emit(
1226
- bc,
1227
- [null, { type: "defineLabel", label: afterCatchLabel }],
1228
- node,
1229
- );
1230
- break;
1231
- }
1232
-
1233
- default: {
1234
- // Use @babel/generator to reproduce the source of unsupported nodes
1235
- // so we can emit a clear error with context.
1236
- const src = generate(node).code;
1237
- throw new Error(`Unsupported statement: ${node.type}\n -> ${src}`);
1238
- }
1239
- }
1240
- }
1241
-
1242
- // Expressions
1243
- _compileExpr(node, scope, bc) {
1244
- switch (node.type) {
1245
- case "NumericLiteral":
1246
- case "StringLiteral": {
1247
- this.emit(
1248
- bc,
1249
- [this.OP.LOAD_CONST, b.constantOperand(node.value)],
1250
- node,
1251
- );
1252
- break;
1253
- }
1254
-
1255
- case "BooleanLiteral": {
1256
- this.emit(
1257
- bc,
1258
- [this.OP.LOAD_CONST, b.constantOperand(node.value)],
1259
- node,
1260
- );
1261
- break;
1262
- }
1263
-
1264
- case "NullLiteral": {
1265
- this.emit(bc, [this.OP.LOAD_CONST, b.constantOperand(null)], node);
1266
- break;
1267
- }
1268
-
1269
- case "Identifier": {
1270
- // scope=null means we're at the top-level -> always global
1271
- const res = this._resolve(node.name, this._currentCtx);
1272
- if (res.kind === "local") {
1273
- this.emit(bc, [this.OP.LOAD_LOCAL, res.slot], node);
1274
- } else if (res.kind === "upvalue") {
1275
- this.emit(bc, [this.OP.LOAD_UPVALUE, res.index], node);
1276
- } else {
1277
- this.emit(
1278
- bc,
1279
- [this.OP.LOAD_GLOBAL, b.constantOperand(node.name)],
1280
- node,
1281
- );
1282
- }
1283
- break;
1284
- }
1285
-
1286
- case "ThisExpression": {
1287
- this.emit(bc, [this.OP.LOAD_THIS], node);
1288
- break;
1289
- }
1290
-
1291
- case "NewExpression": {
1292
- // Push callee, then args -- identical layout to CALL but uses NEW opcode
1293
- this._compileExpr(node.callee, scope, bc);
1294
- for (const arg of node.arguments) this._compileExpr(arg, scope, bc);
1295
- this.emit(bc, [this.OP.NEW, node.arguments.length], node);
1296
- break;
1297
- }
1298
-
1299
- case "SequenceExpression": {
1300
- // (a, b, c) -> eval a -> POP, eval b -> POP, eval c -> leave on stack
1301
- for (let i = 0; i < node.expressions.length - 1; i++) {
1302
- this._compileExpr(node.expressions[i], scope, bc);
1303
- this.emit(bc, [this.OP.POP], node); // discard intermediate result
1304
- }
1305
- // Last expression -- its value is the result of the whole sequence
1306
- this._compileExpr(
1307
- node.expressions[node.expressions.length - 1],
1308
- scope,
1309
- bc,
1310
- );
1311
- break;
1312
- }
1313
-
1314
- case "ConditionalExpression": {
1315
- // test ? consequent : alternate
1316
- const elseLabel = this._makeLabel("ternary_else");
1317
- const endLabel = this._makeLabel("ternary_end");
1318
-
1319
- this._compileExpr(node.test, scope, bc);
1320
- this.emit(
1321
- bc,
1322
- [this.OP.JUMP_IF_FALSE, { type: "label", label: elseLabel }],
1323
- node,
1324
- );
1325
-
1326
- this._compileExpr(node.consequent, scope, bc);
1327
- this.emit(bc, [this.OP.JUMP, { type: "label", label: endLabel }], node);
1328
-
1329
- this.emit(bc, [null, { type: "defineLabel", label: elseLabel }], node);
1330
- this._compileExpr(node.alternate, scope, bc);
1331
-
1332
- this.emit(bc, [null, { type: "defineLabel", label: endLabel }], node);
1333
- break;
1334
- }
1335
-
1336
- case "LogicalExpression": {
1337
- // Pattern (CPython-style):
1338
- // eval LHS
1339
- // JUMP_IF_*_OR_POP -> target (past RHS)
1340
- // eval RHS ← only reached if LHS didn't short-circuit
1341
- // [target lands here, stack top is the result either way]
1342
-
1343
- this._compileExpr(node.left, scope, bc);
1344
-
1345
- if (node.operator === "||") {
1346
- // Short-circuit if LHS is TRUTHY -- keep it, skip RHS
1347
- const endLabel = this._makeLabel("or_end");
1348
- this.emit(
1349
- bc,
1350
- [this.OP.JUMP_IF_TRUE_OR_POP, { type: "label", label: endLabel }],
1351
- node,
1352
- );
1353
- this._compileExpr(node.right, scope, bc);
1354
- this.emit(bc, [null, { type: "defineLabel", label: endLabel }], node);
1355
- } else if (node.operator === "&&") {
1356
- // Short-circuit if LHS is FALSY -- keep it, skip RHS
1357
- const endLabel = this._makeLabel("and_end");
1358
- this.emit(
1359
- bc,
1360
- [this.OP.JUMP_IF_FALSE_OR_POP, { type: "label", label: endLabel }],
1361
- node,
1362
- );
1363
- this._compileExpr(node.right, scope, bc);
1364
- this.emit(bc, [null, { type: "defineLabel", label: endLabel }], node);
1365
- } else {
1366
- throw new Error(`Unsupported logical operator: ${node.operator}`);
1367
- }
1368
- break;
1369
- }
1370
-
1371
- case "BinaryExpression": {
1372
- this._compileExpr(node.left, scope, bc);
1373
- this._compileExpr(node.right, scope, bc);
1374
- const arithOp = {
1375
- "+": this.OP.ADD,
1376
- "-": this.OP.SUB,
1377
- "*": this.OP.MUL,
1378
- "/": this.OP.DIV,
1379
- "%": this.OP.MOD,
1380
- "&": this.OP.BAND,
1381
- "|": this.OP.BOR,
1382
- "^": this.OP.BXOR,
1383
- "<<": this.OP.SHL,
1384
- ">>": this.OP.SHR,
1385
- ">>>": this.OP.USHR,
1386
- }[node.operator];
1387
-
1388
- const cmpOp = {
1389
- "<": this.OP.LT,
1390
- ">": this.OP.GT,
1391
- "===": this.OP.EQ,
1392
- "==": this.OP.LOOSE_EQ,
1393
- "<=": this.OP.LTE,
1394
- ">=": this.OP.GTE,
1395
- "!==": this.OP.NEQ,
1396
- "!=": this.OP.LOOSE_NEQ,
1397
- in: this.OP.IN, // ← add
1398
- instanceof: this.OP.INSTANCEOF, // add
1399
- }[node.operator];
1400
- const resolvedOp = arithOp ?? cmpOp;
1401
- if (resolvedOp === undefined)
1402
- throw new Error(`Unsupported operator: ${node.operator}`);
1403
- this.emit(bc, [resolvedOp], node);
1404
-
1405
- break;
1406
- }
1407
-
1408
- case "UpdateExpression": {
1409
- const res = this._resolve(node.argument.name, this._currentCtx);
1410
- const bumpOp = node.operator === "++" ? this.OP.ADD : this.OP.SUB;
1411
- const one = b.constantOperand(1);
1412
-
1413
- // Helper closures: emit load / store for whichever resolution kind we have
1414
- const emitLoad = () => {
1415
- if (res.kind === "local")
1416
- this.emit(bc, [this.OP.LOAD_LOCAL, res.slot], node);
1417
- else if (res.kind === "upvalue")
1418
- this.emit(bc, [this.OP.LOAD_UPVALUE, res.index], node);
1419
- else
1420
- this.emit(
1421
- bc,
1422
- [this.OP.LOAD_GLOBAL, b.constantOperand(node.argument.name)],
1423
- node,
1424
- );
1425
- };
1426
- const emitStore = () => {
1427
- if (res.kind === "local")
1428
- this.emit(bc, [this.OP.STORE_LOCAL, res.slot], node);
1429
- else if (res.kind === "upvalue")
1430
- this.emit(bc, [this.OP.STORE_UPVALUE, res.index], node);
1431
- else
1432
- this.emit(
1433
- bc,
1434
- [this.OP.STORE_GLOBAL, b.constantOperand(node.argument.name)],
1435
- node,
1436
- );
1437
- };
1438
-
1439
- emitLoad();
1440
- if (!node.prefix) this.emit(bc, [this.OP.DUP], node); // post: save old value before mutating
1441
- this.emit(bc, [this.OP.LOAD_CONST, one], node);
1442
- this.emit(bc, [bumpOp], node);
1443
- emitStore();
1444
- if (node.prefix) emitLoad(); // pre: reload new value as result
1445
-
1446
- break;
1447
- }
1448
-
1449
- case "AssignmentExpression": {
1450
- const compoundOp = {
1451
- "+=": this.OP.ADD,
1452
- "-=": this.OP.SUB,
1453
- "*=": this.OP.MUL,
1454
- "/=": this.OP.DIV,
1455
- "%=": this.OP.MOD,
1456
- "&=": this.OP.BAND,
1457
- "|=": this.OP.BOR,
1458
- "^=": this.OP.BXOR,
1459
- "<<=": this.OP.SHL,
1460
- ">>=": this.OP.SHR,
1461
- ">>>=": this.OP.USHR,
1462
- }[node.operator];
1463
-
1464
- const isCompound = compoundOp !== undefined;
1465
-
1466
- if (node.operator !== "=" && !isCompound) {
1467
- throw new Error(`Unsupported assignment operator: ${node.operator}`);
1468
- }
1469
-
1470
- // Member assignment: obj.x = val or arr[i] = val
1471
- if (node.left.type === "MemberExpression") {
1472
- this._compileExpr(node.left.object, scope, bc); // push obj
1473
-
1474
- if (node.left.computed) {
1475
- this._compileExpr(node.left.property, scope, bc); // push key (runtime)
1476
- } else {
1477
- this.emit(
1478
- bc,
1479
- [this.OP.LOAD_CONST, b.constantOperand(node.left.property.name)],
1480
- node,
1481
- );
1482
- }
1483
-
1484
- if (isCompound) {
1485
- // Duplicate obj+key on the stack so we can read before we write.
1486
- // Stack before DUP2: [..., obj, key]
1487
- // We need: [..., obj, key, obj, key] -> GET_PROP_COMPUTED -> [..., obj, key, currentVal]
1488
- // Cheapest approach without a DUP opcode: re-compile the member read.
1489
- // (emits obj + key again; a future peephole pass could DUP instead)
1490
- this._compileExpr(node.left.object, scope, bc);
1491
- if (node.left.computed) {
1492
- this._compileExpr(node.left.property, scope, bc);
1493
- } else {
1494
- this.emit(
1495
- bc,
1496
- [
1497
- this.OP.LOAD_CONST,
1498
- b.constantOperand(node.left.property.name),
1499
- ],
1500
- node,
1501
- );
1502
- }
1503
- this.emit(bc, [this.OP.GET_PROP_COMPUTED], node); // [..., obj, key, currentVal]
1504
- this._compileExpr(node.right, scope, bc); // [..., obj, key, currentVal, rhs]
1505
- this.emit(bc, [compoundOp], node); // [..., obj, key, newVal]
1506
- } else {
1507
- this._compileExpr(node.right, scope, bc); // [..., obj, key, val]
1508
- }
1509
-
1510
- this.emit(bc, [this.OP.SET_PROP], node); // obj[key] = val, leaves val on stack
1511
- break;
1512
- }
1513
-
1514
- // Plain identifier assignment
1515
- const res = this._resolve(node.left.name, this._currentCtx);
1516
-
1517
- if (isCompound) {
1518
- // Load the current value of the target first
1519
- if (res.kind === "local") {
1520
- this.emit(bc, [this.OP.LOAD_LOCAL, res.slot], node);
1521
- } else if (res.kind === "upvalue") {
1522
- this.emit(bc, [this.OP.LOAD_UPVALUE, res.index], node);
1523
- } else {
1524
- this.emit(
1525
- bc,
1526
- [this.OP.LOAD_GLOBAL, b.constantOperand(node.left.name)],
1527
- node,
1528
- );
1529
- }
1530
- }
1531
-
1532
- this._compileExpr(node.right, scope, bc); // push RHS
1533
-
1534
- if (isCompound) {
1535
- this.emit(bc, [compoundOp], node); // apply binary op -> leaves newVal on stack
1536
- }
1537
-
1538
- // Store & leave value on stack (assignment is an expression)
1539
- if (res.kind === "local") {
1540
- this.emit(bc, [this.OP.STORE_LOCAL, res.slot], node);
1541
- this.emit(bc, [this.OP.LOAD_LOCAL, res.slot], node);
1542
- } else if (res.kind === "upvalue") {
1543
- this.emit(bc, [this.OP.STORE_UPVALUE, res.index], node);
1544
- this.emit(bc, [this.OP.LOAD_UPVALUE, res.index], node);
1545
- } else {
1546
- const nameIdx = b.constantOperand(node.left.name);
1547
- this.emit(bc, [this.OP.STORE_GLOBAL, nameIdx], node);
1548
- this.emit(bc, [this.OP.LOAD_GLOBAL, nameIdx], node);
1549
- }
1550
- break;
1551
- }
1552
-
1553
- case "CallExpression": {
1554
- if (node.callee.type === "MemberExpression") {
1555
- // ── Method call: console.log(...)
1556
- // Push receiver first (GET_PROP leaves it; CALL_METHOD pops it as `this`)
1557
- this._compileExpr(node.callee.object, scope, bc);
1558
- const prop = node.callee.property.name;
1559
- const propIdx = b.constantOperand(prop);
1560
- this.emit(bc, [this.OP.LOAD_CONST, propIdx], node);
1561
- this.emit(bc, [this.OP.GET_PROP], node);
1562
- for (const arg of node.arguments) this._compileExpr(arg, scope, bc);
1563
- this.emit(bc, [this.OP.CALL_METHOD, node.arguments.length], node);
1564
- } else {
1565
- // ── Plain call: add(5, 10)
1566
- this._compileExpr(node.callee, scope, bc);
1567
- for (const arg of node.arguments) this._compileExpr(arg, scope, bc);
1568
- this.emit(bc, [this.OP.CALL, node.arguments.length], node);
1569
- }
1570
- break;
1571
- }
1572
-
1573
- case "UnaryExpression": {
1574
- // Special case: typeof on a bare identifier must not throw if undeclared.
1575
- // We emit TYPEOF_SAFE (operand = name constant index) instead of
1576
- // compiling the argument first. The VM does the guard itself.
1577
- if (node.operator === "typeof" && node.argument.type === "Identifier") {
1578
- const res = this._resolve(node.argument.name, this._currentCtx);
1579
- if (res.kind === "global") {
1580
- // Potentially undeclared -- let VM guard it
1581
- this.emit(
1582
- bc,
1583
- [this.OP.LOAD_CONST, b.constantOperand(node.argument.name)],
1584
- node,
1585
- );
1586
- this.emit(bc, [this.OP.TYPEOF_SAFE], node);
1587
- break;
1588
- }
1589
- // Known local or upvalue -- safe to load first, then typeof
1590
- }
1591
-
1592
- // Special case: delete -- argument must NOT be pre-evaluated.
1593
- if (node.operator === "delete") {
1594
- const arg = node.argument;
1595
- if (arg.type === "MemberExpression") {
1596
- this._compileExpr(arg.object, scope, bc);
1597
- if (arg.computed) {
1598
- this._compileExpr(arg.property, scope, bc);
1599
- } else {
1600
- this.emit(
1601
- bc,
1602
- [this.OP.LOAD_CONST, b.constantOperand(arg.property.name)],
1603
- node,
1604
- );
1605
- }
1606
- this.emit(bc, [this.OP.DELETE_PROP], node);
1607
- } else {
1608
- // delete x, delete 0, etc. -- always true in non-strict, just push true
1609
- this.emit(bc, [this.OP.LOAD_CONST, b.constantOperand(true)], node);
1610
- }
1611
- break;
1612
- }
1613
-
1614
- // All other unary ops: compile argument first, then apply operator
1615
- this._compileExpr(node.argument, scope, bc);
1616
- switch (node.operator) {
1617
- case "-":
1618
- this.emit(bc, [this.OP.UNARY_NEG], node);
1619
- break;
1620
- case "+":
1621
- this.emit(bc, [this.OP.UNARY_POS], node);
1622
- break;
1623
- case "!":
1624
- this.emit(bc, [this.OP.UNARY_NOT], node);
1625
- break;
1626
- case "~":
1627
- this.emit(bc, [this.OP.UNARY_BITNOT], node);
1628
- break;
1629
- case "typeof":
1630
- this.emit(bc, [this.OP.TYPEOF], node);
1631
- break;
1632
- case "void":
1633
- this.emit(bc, [this.OP.VOID], node);
1634
- break;
1635
-
1636
- default:
1637
- throw new Error(`Unsupported unary operator: ${node.operator}`);
1638
- }
1639
- break;
1640
- }
1641
-
1642
- case "RegExpLiteral": {
1643
- // Emit: new RegExp(pattern, flags)
1644
- // Fresh object per evaluation -- correct for stateful g/y flags.
1645
- this.emit(bc, [this.OP.LOAD_GLOBAL, b.constantOperand("RegExp")], node);
1646
- this.emit(
1647
- bc,
1648
- [this.OP.LOAD_CONST, b.constantOperand(node.pattern)],
1649
- node,
1650
- );
1651
- this.emit(
1652
- bc,
1653
- [this.OP.LOAD_CONST, b.constantOperand(node.flags)],
1654
- node,
1655
- );
1656
- this.emit(bc, [this.OP.NEW, 2], node);
1657
- break;
1658
- }
1659
-
1660
- case "FunctionExpression": {
1661
- // Compile into a descriptor exactly like a declaration,
1662
- // but leave the resulting closure ON THE STACK -- no store.
1663
- // The surrounding expression (assignment, call arg, return) consumes it.
1664
- const desc = this._compileFunctionDecl(node);
1665
- this._emitClosureMetadata(desc, node, bc);
1666
- this.emit(
1667
- bc,
1668
- [this.OP.MAKE_CLOSURE, { type: "label", label: desc.entryLabel }],
1669
- node,
1670
- );
1671
- break;
1672
- }
1673
-
1674
- case "MemberExpression": {
1675
- this._compileExpr(node.object, scope, bc);
1676
- if (node.computed) {
1677
- // nums[i] -- key is runtime value
1678
- this._compileExpr(node.property, scope, bc);
1679
- } else {
1680
- // point.x -- push key as string, same opcode handles both
1681
- this.emit(
1682
- bc,
1683
- [this.OP.LOAD_CONST, b.constantOperand(node.property.name)],
1684
- node,
1685
- );
1686
- }
1687
-
1688
- // GET_PROP_COMPUTED pops the object -- correct for value access.
1689
- // GET_PROP (peek) is only used in CallExpression's method call path
1690
- // where the receiver must survive on the stack for CALL_METHOD.
1691
- this.emit(bc, [this.OP.GET_PROP_COMPUTED], node);
1692
- break;
1693
- }
1694
-
1695
- case "ArrayExpression": {
1696
- // Compile each element left->right, then BUILD_ARRAY collapses them.
1697
- // Sparse arrays (holes) get explicit undefined per slot.
1698
- for (const el of node.elements) {
1699
- if (el === null) {
1700
- // hole: e.g. [1,,3]
1701
- this.emit(
1702
- bc,
1703
- [this.OP.LOAD_CONST, b.constantOperand(undefined)],
1704
- node,
1705
- );
1706
- } else {
1707
- this._compileExpr(el, scope, bc);
1708
- }
1709
- }
1710
- this.emit(bc, [this.OP.BUILD_ARRAY, node.elements.length], node);
1711
- break;
1712
- }
1713
- case "ObjectExpression": {
1714
- // Separate regular data properties from ES5 accessor methods (get/set).
1715
- const regularProps: t.ObjectProperty[] = [];
1716
- const accessorProps: t.ObjectMethod[] = [];
1717
-
1718
- for (const prop of node.properties) {
1719
- if (prop.type === "SpreadElement") {
1720
- throw new Error("Object spread not supported");
1721
- }
1722
- if (prop.type === "ObjectMethod") {
1723
- if (prop.kind === "get" || prop.kind === "set") {
1724
- if (prop.computed) {
1725
- throw new Error(
1726
- "Computed getter/setter keys are not supported",
1727
- );
1728
- }
1729
- accessorProps.push(prop);
1730
- } else {
1731
- throw new Error(`Shorthand method syntax is not supported`);
1732
- }
1733
- } else {
1734
- regularProps.push(prop as t.ObjectProperty);
1735
- }
1736
- }
1737
-
1738
- // Build the base object from data properties.
1739
- for (const prop of regularProps) {
1740
- const key = prop.key;
1741
- let keyStr: string;
1742
- if (key.type === "Identifier") {
1743
- keyStr = key.name;
1744
- } else if (
1745
- key.type === "StringLiteral" ||
1746
- key.type === "NumericLiteral"
1747
- ) {
1748
- keyStr = String(key.value);
1749
- } else {
1750
- throw new Error(`Unsupported object key type: ${key.type}`);
1751
- }
1752
- this.emit(bc, [this.OP.LOAD_CONST, b.constantOperand(keyStr)], node);
1753
- this._compileExpr(prop.value, scope, bc);
1754
- }
1755
- this.emit(bc, [this.OP.BUILD_OBJECT, regularProps.length], node);
1756
-
1757
- // Define each accessor on the object that is now on top of the stack.
1758
- // Stack after BUILD_OBJECT: [..., obj]
1759
- // For each accessor: DUP obj, push key, compile fn, DEFINE_GETTER/DEFINE_SETTER
1760
- // DEFINE_GETTER/DEFINE_SETTER pops fn+key+obj, leaving the original obj.
1761
- for (const prop of accessorProps) {
1762
- const key = prop.key;
1763
- let keyStr: string;
1764
- if (key.type === "Identifier") {
1765
- keyStr = key.name;
1766
- } else if (
1767
- key.type === "StringLiteral" ||
1768
- key.type === "NumericLiteral"
1769
- ) {
1770
- keyStr = String(key.value);
1771
- } else {
1772
- throw new Error(`Unsupported object key type: ${key.type}`);
1773
- }
1774
-
1775
- this.emit(bc, [this.OP.DUP], node); // dup so the original obj stays after the define
1776
- this.emit(bc, [this.OP.LOAD_CONST, b.constantOperand(keyStr)], node);
1777
-
1778
- // Compile the accessor body as an anonymous function descriptor.
1779
- const desc = this._compileFunctionDecl(prop as any);
1780
- this._emitClosureMetadata(desc, prop as any, bc);
1781
- this.emit(
1782
- bc,
1783
- [
1784
- this.OP.MAKE_CLOSURE,
1785
- {
1786
- type: "label",
1787
- label: desc.entryLabel,
1788
- },
1789
- ],
1790
- node,
1791
- );
1792
-
1793
- this.emit(
1794
- bc,
1795
- [
1796
- prop.kind === "get"
1797
- ? this.OP.DEFINE_GETTER
1798
- : this.OP.DEFINE_SETTER,
1799
- ],
1800
- node,
1801
- );
1802
- }
1803
-
1804
- break;
1805
- }
1806
-
1807
- default: {
1808
- throw new Error(`Unsupported expression: ${node.type}`);
1809
- }
1810
- }
1811
- }
1812
- }
1813
-
1814
- // Serializer
1815
- // Turns the compiled output into a commented JS source string.
1816
- // Expects fully-resolved bytecode (all label refs and constant refs already
1817
- // converted to plain integers by resolveLabels + resolveConstants passes).
1818
- class Serializer {
1819
- compiler: Compiler;
1820
-
1821
- constructor(compiler: Compiler) {
1822
- this.compiler = compiler;
1823
- }
1824
-
1825
- get options() {
1826
- return this.compiler.options;
1827
- }
1828
-
1829
- get OP() {
1830
- return this.compiler.OP;
1831
- }
1832
-
1833
- get OP_NAME() {
1834
- return this.compiler.OP_NAME;
1835
- }
1836
-
1837
- get JUMP_OPS() {
1838
- return this.compiler.JUMP_OPS;
1839
- }
1840
-
1841
- // Produce a JS literal for a constant pool entry
1842
- _serializeConst(val) {
1843
- if (val === null) return "null";
1844
- if (val === undefined) return "undefined";
1845
- return JSON.stringify(val); // number / string / bool
1846
- }
1847
-
1848
- // One instruction -> "[op, operand] // MNEMONIC description"
1849
- // Expects a fully-resolved instruction: operand is a plain number or undefined.
1850
- _serializeInstr(instr: b.Instruction, constants: any[]) {
1851
- const [op, rawOperand] = instr;
1852
-
1853
- ok(
1854
- rawOperand === undefined || typeof rawOperand === "number",
1855
- "Unresolved operand: " + JSON.stringify(rawOperand),
1856
- );
1857
- const operand = rawOperand as number | undefined;
1858
-
1859
- const name = this.OP_NAME[op] || `OP_${op}`;
1860
- let comment = name;
1861
-
1862
- const sourceNode = instr[SOURCE_NODE_SYM];
1863
- const sourceLocation = sourceNode
1864
- ? sourceNode.loc.start?.line +
1865
- ":" +
1866
- sourceNode.loc.start?.column +
1867
- "-" +
1868
- (sourceNode.loc.end?.line + ":" + sourceNode.loc.end?.column)
1869
- : "";
1870
-
1871
- // Annotate operand with its meaning
1872
- if (operand !== undefined) {
1873
- switch (op) {
1874
- case this.OP.LOAD_CONST: {
1875
- const val = constants[operand];
1876
- comment += ` ${this._serializeConst(val)}`;
1877
- break;
1878
- }
1879
- case this.OP.MAKE_CLOSURE: {
1880
- // operand is the absolute PC of the function body's first instruction
1881
- comment += ` PC ${operand}`;
1882
- break;
1883
- }
1884
- case this.OP.DATA: {
1885
- // Inline function header word — value is a raw integer
1886
- comment += ` ${operand}`;
1887
- break;
1888
- }
1889
- case this.OP.LOAD_LOCAL:
1890
- case this.OP.STORE_LOCAL:
1891
- comment += ` slot[${operand}]`;
1892
- break;
1893
- case this.OP.LOAD_UPVALUE:
1894
- case this.OP.STORE_UPVALUE:
1895
- comment += ` upvalue[${operand}]`;
1896
- break;
1897
- case this.OP.LOAD_GLOBAL:
1898
- case this.OP.STORE_GLOBAL:
1899
- comment += ` "${constants[operand]}"`;
1900
- break;
1901
- case this.OP.CALL:
1902
- case this.OP.CALL_METHOD:
1903
- comment += ` (${operand} args)`;
1904
- break;
1905
-
1906
- case this.OP.BUILD_ARRAY:
1907
- comment += ` (${operand} elements)`;
1908
- break;
1909
- case this.OP.BUILD_OBJECT:
1910
- comment += ` (${operand} pairs)`;
1911
- break;
1912
-
1913
- case this.OP.NEW:
1914
- comment += ` (${operand} args)`;
1915
- break;
1916
-
1917
- default:
1918
- comment += ` ${operand}`;
1919
- }
1920
- }
1921
-
1922
- comment = comment.padEnd(40) + sourceLocation;
1923
-
1924
- // Pack a [op, operand?] instruction pair into a single 32-bit word.
1925
- // Shared between the Serializer and the obfuscation path in _compileMain.
1926
-
1927
- const instrText = operand !== undefined ? `[${op}, ${operand}]` : `[${op}]`;
1928
- const text = `${(instrText + ",").padEnd(12)} ${comment}`;
1929
-
1930
- if (!this.options.encodeBytecode) {
1931
- return {
1932
- text: text,
1933
- value: operand !== undefined ? [op, operand] : [op],
1934
- };
1935
- }
1936
-
1937
- function packInstr(instr) {
1938
- const [op, operand] = instr;
1939
- if (operand !== undefined && !Number.isInteger(operand))
1940
- throw new Error(`Non-integer operand: ${operand}`);
1941
- if (operand !== undefined && operand < 0)
1942
- throw new Error(`Negative operand: ${operand}`);
1943
- if (operand !== undefined && operand > 0xffffff)
1944
- throw new Error(`Operand overflow (max 0xFFFFFF): ${operand}`);
1945
- return operand !== undefined ? (operand << 8) | op : op;
1946
- }
1947
-
1948
- return {
1949
- text: text,
1950
- value: packInstr(instr),
1951
- };
1952
- }
1953
-
1954
- // Serialize the CONSTANTS array
1955
- _serializeConstants(constants: any[]) {
1956
- const lines = ["var CONSTANTS = ["];
1957
- constants.forEach((val, idx) => {
1958
- lines.push(` /* ${idx} */ ${this._serializeConst(val)},`);
1959
- });
1960
- lines.push("];");
1961
- return lines.join("\n");
1962
- }
1963
-
1964
- // Filter out any remaining null-opcode pseudo-instructions.
1965
- // (defineLabel pseudo-ops are already stripped by resolveLabels.)
1966
- _serializeBytecode(bytecode: b.Bytecode): { bytecode: b.Bytecode } {
1967
- return {
1968
- bytecode: bytecode.filter((instr) => instr[0] !== null),
1969
- };
1970
- }
1971
-
1972
- __serializeBytecode(bytecode: b.Bytecode, constants: any[]) {
1973
- let words = [];
1974
-
1975
- // BYTECODE
1976
- for (const instr of bytecode) {
1977
- words.push(this._serializeInstr(instr, constants).value);
1978
- }
1979
-
1980
- // Convert packed words -> raw 4-byte little-endian binary -> base64
1981
- const buf = new Uint8Array(words.length * 4);
1982
- words.forEach((w, i) => {
1983
- buf[i * 4] = w & 0xff;
1984
- buf[i * 4 + 1] = (w >>> 8) & 0xff;
1985
- buf[i * 4 + 2] = (w >>> 16) & 0xff;
1986
- buf[i * 4 + 3] = (w >>> 24) & 0xff;
1987
- });
1988
- return Buffer.from(buf).toString("base64");
1989
- }
1990
-
1991
- serialize(bytecode: b.Bytecode, constants: any[], compiler: Compiler) {
1992
- const mainStartPc = compiler.mainStartPc;
1993
- let sections = [];
1994
-
1995
- var textForm = [];
1996
- var initBody = [];
1997
-
1998
- var bytecodeResult = this._serializeBytecode(bytecode);
1999
-
2000
- for (const instr of bytecodeResult.bytecode) {
2001
- const serialized = this._serializeInstr(instr, constants);
2002
- textForm.push(serialized.text);
2003
- }
2004
-
2005
- initBody.push(textForm.map((line) => `// ${line}`).join("\n"));
2006
-
2007
- if (this.options.encodeBytecode) {
2008
- sections.push(
2009
- `var BYTECODE = "${this.__serializeBytecode(bytecodeResult.bytecode, constants)}";`,
2010
- );
2011
- } else {
2012
- sections.push(
2013
- `var BYTECODE = [${bytecodeResult.bytecode.map((v) => "[" + v[0] + ", " + v[1] + "]").join(",")}]`,
2014
- );
2015
- }
2016
-
2017
- // MAIN_START_PC
2018
- sections.push(`var MAIN_START_PC = ${mainStartPc};`);
2019
- sections.push(`var ENCODE_BYTECODE = ${!!this.options.encodeBytecode};`);
2020
- sections.push(`var TIMING_CHECKS = ${!!this.options.timingChecks};`);
2021
- // Opcodes
2022
- sections.push(`var OP = ${JSON5.stringify(this.OP)};`);
2023
-
2024
- // Constants must be defined before the bytecode
2025
- initBody.push(this._serializeConstants(constants));
2026
-
2027
- sections = [...initBody, ...sections];
2028
-
2029
- // VM runtime
2030
- sections.push(VM_RUNTIME);
2031
-
2032
- return sections.join("\n\n");
2033
- }
2034
- }
2035
-
2036
- export async function compileAndSerialize(
2037
- sourceCode: string,
2038
- options: Options,
2039
- ) {
2040
- const compiler = new Compiler(options);
2041
- let bytecode = compiler.compile(sourceCode);
2042
-
2043
- // User transform passes (operate on unresolved IR with label/constant refs)
2044
- const passes = [...(options.selfModifying ? [selfModifying] : [])];
2045
- for (const pass of passes) {
2046
- const passResult = pass(bytecode, compiler);
2047
- bytecode = passResult.bytecode;
2048
- }
2049
-
2050
- // Assembler phases: resolve IR operands to plain integers before printing
2051
- const { bytecode: labelResolved } = resolveLabels(bytecode, compiler);
2052
- const { bytecode: finalBytecode, constants } =
2053
- resolveConstants(labelResolved);
2054
-
2055
- const output = compiler.serializer.serialize(
2056
- finalBytecode,
2057
- constants,
2058
- compiler,
2059
- );
2060
-
2061
- const finalOutput = await obfuscateRuntime(output, options);
2062
-
2063
- return {
2064
- code: finalOutput,
2065
- };
2066
- }
1
+ import { parse } from "@babel/parser";
2
+ import traverseImport from "@babel/traverse";
3
+ import { generate } from "@babel/generator";
4
+
5
+ import { readFileSync } from "fs";
6
+ import { join } from "path";
7
+ import { stripTypeScriptTypes } from "module";
8
+ import * as t from "@babel/types";
9
+ import { ok } from "assert";
10
+ import { obfuscateRuntime } from "./build-runtime.ts";
11
+ import { DEFAULT_OPTIONS, type Options } from "./options.ts";
12
+ import { resolveLabels } from "./transforms/bytecode/resolveLabels.ts";
13
+ import { resolveConstants } from "./transforms/bytecode/resolveContants.ts";
14
+ import { selfModifying } from "./transforms/bytecode/selfModifying.ts";
15
+ import { macroOpcodes } from "./transforms/bytecode/macroOpcodes.ts";
16
+ import * as b from "./types.ts";
17
+ import { specializedOpcodes } from "./transforms/bytecode/specializedOpcodes.ts";
18
+ import { getRandomInt } from "./transforms/utils/random-utils.ts";
19
+ import { U16_MAX } from "./transforms/utils/op-utils.ts";
20
+
21
+ const traverse = (traverseImport.default ||
22
+ traverseImport) as typeof traverseImport.default;
23
+
24
+ const readVMRuntimeFile = () => {
25
+ let code;
26
+ try {
27
+ code = readFileSync(join(import.meta.dirname, "./runtime.ts"), "utf-8");
28
+ } catch (e) {
29
+ code = readFileSync(join(import.meta.dirname, "./runtime.js"), "utf-8");
30
+ }
31
+
32
+ return stripTypeScriptTypes?.(code) || code;
33
+ };
34
+
35
+ export const VM_RUNTIME = readVMRuntimeFile().split("@START")[1];
36
+ export const SOURCE_NODE_SYM = Symbol("SOURCE_NODE"); // Attach source node location to pseudo bytecode instructions
37
+
38
+ // Opcodes
39
+ export const OP_ORIGINAL = {
40
+ LOAD_CONST: 0,
41
+ LOAD_LOCAL: 1,
42
+ STORE_LOCAL: 2,
43
+ LOAD_GLOBAL: 3,
44
+ STORE_GLOBAL: 4,
45
+ GET_PROP: 5,
46
+ ADD: 6, // a + b (both are popped)
47
+ SUB: 7, // a - b
48
+ MUL: 8, // a * b
49
+ DIV: 9, // a / b
50
+ MAKE_CLOSURE: 10,
51
+ CALL: 11,
52
+ CALL_METHOD: 12,
53
+ RETURN: 13,
54
+ POP: 14, // discard top of stack
55
+ LT: 15, // pop b, pop a -> push (a < b)
56
+ GT: 16, // pop b, pop a -> push (a > b)
57
+ EQ: 17, // pop b, pop a -> push (a === b)
58
+ JUMP: 18, // unconditional - operand = absolute bytecode index
59
+ JUMP_IF_FALSE: 19, // pop value; jump if falsy
60
+ LTE: 20, // a <= b
61
+ GTE: 21, // a >= b
62
+ NEQ: 22, // a !== b
63
+ LOAD_UPVALUE: 23, // push frame.closure.upvalues[operand].read()
64
+ STORE_UPVALUE: 24, // frame.closure.upvalues[operand].write(pop())
65
+
66
+ // Unary
67
+ UNARY_NEG: 25, // -x
68
+ UNARY_POS: 26, // +x
69
+ UNARY_NOT: 27, // !x
70
+ UNARY_BITNOT: 28, // ~x
71
+ TYPEOF: 29, // typeof x
72
+ VOID: 30, // void x -> always undefined
73
+
74
+ TYPEOF_SAFE: 31, // operand = name constIdx - typeof guard for undeclared globals
75
+ BUILD_ARRAY: 32, // operand = element count - pops N values -> pushes array
76
+ BUILD_OBJECT: 33, // operand = pair count - pops N*2 (key,val) -> pushes object
77
+ SET_PROP: 34, // pop val, pop key, peek obj -> obj[key] = val (obj stays on stack)
78
+ GET_PROP_COMPUTED: 35, // pop key, peek obj -> push obj[key] (computed: nums[i])
79
+
80
+ MOD: 36, // a % b
81
+ BAND: 37, // a & b
82
+ BOR: 38, // a | b
83
+ BXOR: 39, // a ^ b
84
+ SHL: 40, // a << b
85
+ SHR: 41, // a >> b
86
+ USHR: 42, // a >>> b
87
+
88
+ JUMP_IF_FALSE_OR_POP: 43, // && - if top falsy: jump (keep), else: pop, eval RHS
89
+ JUMP_IF_TRUE_OR_POP: 44, // || - if top truthy: jump (keep), else: pop, eval RHS
90
+
91
+ DELETE_PROP: 45,
92
+ IN: 46, // a in b
93
+ INSTANCEOF: 47, // a instanceof b
94
+
95
+ // NEW
96
+ LOAD_THIS: 48, // push frame.thisVal
97
+ NEW: 49, // operand = argCount - construct a new object
98
+ DUP: 50, // duplicate top of stack
99
+ THROW: 51, // pop value, throw it
100
+ LOOSE_EQ: 52, // a == b (abstract equality)
101
+ LOOSE_NEQ: 53, // a != b (abstract inequality)
102
+
103
+ FOR_IN_SETUP: 54, // pop obj -> build enumerable-key iterator -> push {keys,i}
104
+ FOR_IN_NEXT: 55, // operand=exit_pc; pop iter; if done->jump; else push next key
105
+
106
+ // Self-modifying bytecode
107
+ PATCH: 56, // pop destPc; constants[operand]=word[]; write words into bytecode[destPc..]
108
+
109
+ // Try-Catch
110
+ TRY_SETUP: 57, // operand = catch_pc; push exception handler onto frame._handlerStack
111
+ TRY_END: 58, // pop exception handler (normal exit from try body)
112
+
113
+ // Getter / Setter (ES5 object literal accessor syntax)
114
+ DEFINE_GETTER: 59, // pop fn, pop key, pop obj -> Object.defineProperty(obj, key, {get: fn})
115
+ DEFINE_SETTER: 60, // pop fn, pop key, pop obj -> Object.defineProperty(obj, key, {set: fn})
116
+
117
+ DEBUGGER: 61, // emits a "debugger" statement
118
+
119
+ // Push the raw integer operand directly onto the stack (no constant pool lookup).
120
+ // Identical pipeline to JUMP ops: {type:"label"} pseudo-operands resolve to a
121
+ // raw PC number that becomes the operand, which is pushed as-is at runtime.
122
+ LOAD_INT: 62,
123
+ };
124
+
125
+ // Scope
126
+ // Each function call gets its own Scope. Locals are resolved to
127
+ // numeric slots at compile time -- zero name lookups at runtime.
128
+ class Scope {
129
+ parent: Scope | null;
130
+ _locals: Map<string, number>;
131
+ _next: number;
132
+
133
+ constructor(parent = null) {
134
+ this.parent = parent;
135
+ this._locals = new Map(); // name -> slot index
136
+ this._next = 0;
137
+ }
138
+
139
+ define(name) {
140
+ if (!this._locals.has(name)) {
141
+ this._locals.set(name, this._next++);
142
+ }
143
+ return this._locals.get(name);
144
+ }
145
+
146
+ // Walk up scope chain. If we fall off the top -> global.
147
+ resolve(name) {
148
+ if (this._locals.has(name)) {
149
+ return { kind: "local", slot: this._locals.get(name) };
150
+ }
151
+ if (this.parent) return this.parent.resolve(name);
152
+ return { kind: "global" };
153
+ }
154
+
155
+ get localCount() {
156
+ return this._next;
157
+ }
158
+ }
159
+
160
+ // FnContext
161
+ // Compiler-side state for the function currently being compiled.
162
+ // Distinct from runtime Frame -- this is compile-time only.
163
+ class FnContext {
164
+ upvalues: { name: string; isLocal: number; index: number }[];
165
+ parentCtx: FnContext | null;
166
+ scope: Scope;
167
+ compiler: Compiler;
168
+ bc: b.Instruction[];
169
+
170
+ constructor(compiler, parentCtx = null) {
171
+ this.compiler = compiler;
172
+ this.parentCtx = parentCtx;
173
+ this.scope = new Scope();
174
+
175
+ this.bc = [];
176
+ this.upvalues = []; // { name, isLocal, index }
177
+ }
178
+
179
+ // Find or register a captured variable as an upvalue.
180
+ // isLocal=true -> captured directly from parent's locals[index]
181
+ // isLocal=false -> relayed from parent's own upvalue list[index]
182
+ addUpvalue(name, isLocal, index) {
183
+ const existing = this.upvalues.findIndex((u) => u.name === name);
184
+ if (existing !== -1) return existing;
185
+ const idx = this.upvalues.length;
186
+ this.upvalues.push({ name, isLocal, index: index });
187
+ return idx;
188
+ }
189
+ }
190
+
191
+ // Compiler
192
+ export class Compiler {
193
+ fnDescriptors: any[];
194
+ bytecode: b.Bytecode;
195
+ mainStartPc: number;
196
+
197
+ _currentCtx: FnContext | null;
198
+ _pendingLabel: string | null;
199
+ _forInCount: number;
200
+ _labelCount: number;
201
+ _loopStack: {
202
+ type: "loop" | "switch" | "block";
203
+ label: string | null;
204
+ // Label that break statements targeting this entry should jump to.
205
+ breakLabel: string;
206
+ // Label that continue statements targeting this entry should jump to.
207
+ continueLabel: string;
208
+ }[];
209
+
210
+ options: Options;
211
+ serializer: Serializer;
212
+
213
+ OP: Partial<typeof OP_ORIGINAL>;
214
+ MACRO_OPS: Record<number, number[]>;
215
+ SPECIALIZED_OPS: Record<
216
+ number,
217
+ {
218
+ originalOp: number;
219
+ operand: b.InstrOperand;
220
+ resolvedOperand?: b.InstrOperand;
221
+ }
222
+ >;
223
+
224
+ OP_NAME: Record<number, string>;
225
+ JUMP_OPS: Set<number>;
226
+
227
+ emit(bc: b.Bytecode, instr: b.Instruction, node: t.Node) {
228
+ bc.push(instr);
229
+
230
+ instr[SOURCE_NODE_SYM] = node;
231
+ }
232
+
233
+ // DO NOT USE THIS KEY UNLESS YOU ARE "RESOLVE CONSTANTS"
234
+ // CONSTANTS DURING COMPILATION MUST BE USED BY REFERENCE WITH b.constantOperand("myConstantHere")
235
+ constants: any[];
236
+
237
+ constructor(options: Options = DEFAULT_OPTIONS) {
238
+ this.options = options;
239
+ this.fnDescriptors = []; // populated in pass 1
240
+ this.bytecode = [];
241
+ this.mainStartPc = 0;
242
+ this._currentCtx = null; // FnContext of the function being compiled, null at top-level
243
+ this._loopStack = []; // per active loop/switch/block/try
244
+ this._pendingLabel = null;
245
+ this._forInCount = 0; // counter for synthetic for-in iterator global names
246
+ this._labelCount = 0; // monotonically increasing counter for unique label names
247
+
248
+ this.serializer = new Serializer(this);
249
+ this.MACRO_OPS = {};
250
+ this.SPECIALIZED_OPS = {};
251
+
252
+ this.OP = { ...OP_ORIGINAL };
253
+
254
+ // Construct randomized opcode mapping
255
+ if (this.options.randomizeOpcodes) {
256
+ let usedNumbers = new Set<number>();
257
+ for (const key in this.OP) {
258
+ let val;
259
+ do {
260
+ val = getRandomInt(0, U16_MAX);
261
+ } while (usedNumbers.has(val));
262
+ usedNumbers.add(val);
263
+ this.OP[key] = val;
264
+ }
265
+ }
266
+
267
+ // Reverse map for comment generation
268
+ this.OP_NAME = Object.fromEntries(
269
+ Object.entries(this.OP).map(([k, v]) => [v, k]),
270
+ );
271
+
272
+ this.JUMP_OPS = new Set([
273
+ this.OP.JUMP,
274
+ this.OP.JUMP_IF_FALSE,
275
+ this.OP.JUMP_IF_TRUE_OR_POP,
276
+ this.OP.JUMP_IF_FALSE_OR_POP,
277
+ this.OP.FOR_IN_NEXT,
278
+ this.OP.TRY_SETUP, // catch_pc operand needs offset adjustment like jump targets
279
+ ]);
280
+ }
281
+
282
+ // Generate a globally unique label string with an optional hint for readability.
283
+ _makeLabel(hint = ""): string {
284
+ var id = this._labelCount++;
285
+ return `${hint || "L"}_${id}`;
286
+ }
287
+
288
+ // Variable resolution
289
+ // Walks up the FnContext chain. Crossing a context boundary means
290
+ // we're capturing from an outer function - register an upvalue.
291
+ _resolve(name, ctx) {
292
+ if (!ctx) return { kind: "global" };
293
+
294
+ // 1. Own locals
295
+ if (ctx.scope._locals.has(name)) {
296
+ return { kind: "local", slot: ctx.scope._locals.get(name) };
297
+ }
298
+
299
+ // 2. No parent context -> must be global
300
+ if (!ctx.parentCtx) return { kind: "global" };
301
+
302
+ // 3. Ask parent -- recurse up the chain
303
+ const parentResult = this._resolve(name, ctx.parentCtx);
304
+ if (parentResult.kind === "global") return { kind: "global" };
305
+
306
+ // 4. Parent has it (as local or upvalue) -- register an upvalue here.
307
+ // isLocal=true means "take it straight from parent's locals[index]"
308
+ // isLocal=false means "relay parent's upvalue[index]" (multi-level capture)
309
+ const isLocal = parentResult.kind === "local";
310
+ const index = isLocal ? parentResult.slot : parentResult.index;
311
+ const uvIdx = ctx.addUpvalue(name, isLocal, index);
312
+ return { kind: "upvalue", index: uvIdx };
313
+ }
314
+
315
+ // Entry point
316
+ compile(source: string) {
317
+ const ast = parse(source, { sourceType: "script" });
318
+
319
+ return this.compileAST(ast);
320
+ }
321
+
322
+ compileAST(ast: t.File) {
323
+ // Pass 1 - compile every FunctionDeclaration into a descriptor.
324
+ // Traverse finds them regardless of nesting depth.
325
+ traverse(ast, {
326
+ FunctionDeclaration: (path) => {
327
+ // Only handle top-level functions for this MVP.
328
+ // (Parent is Program node)
329
+ if (path.parent.type !== "Program") return;
330
+ this._compileFunctionDecl(path.node);
331
+ path.skip(); // don't recurse into nested functions
332
+ },
333
+ });
334
+
335
+ // Pass 2 -- compile top-level statements into BYTECODE.
336
+ this._compileMain(ast.program.body);
337
+
338
+ return this.bytecode;
339
+ }
340
+
341
+ // Function Declaration
342
+
343
+ _compileFunctionDecl(node: t.FunctionDeclaration | t.FunctionExpression) {
344
+ // Reserve a slot in fnDescriptors NOW, before compiling the body, so that
345
+ // any nested _compileFunctionDecl calls see the correct .length and get a
346
+ // distinct _fnIdx. The placeholder object is mutated in-place below once
347
+ // the body and header are ready.
348
+ var fnIdx = this.fnDescriptors.length;
349
+ const entryLabel = this._makeLabel(`fn_${fnIdx}`);
350
+ var desc: any = {}; // placeholder filled in after compilation
351
+ this.fnDescriptors.push(desc);
352
+
353
+ // Create a context whose parent is whatever we're currently compiling.
354
+ // This is what lets _resolve cross function boundaries correctly.
355
+ const ctx = new FnContext(this, this._currentCtx);
356
+ const savedCtx = this._currentCtx;
357
+ this._currentCtx = ctx;
358
+
359
+ // Isolate the loop stack so that try/loop entries from the outer scope
360
+ // don't cause spurious TRY_END / extra jumps inside this function body.
361
+ const savedLoopStack = this._loopStack;
362
+ this._loopStack = [];
363
+
364
+ // Params occupy the first N local slots (args are copied in on CALL)
365
+ for (const param of node.params) {
366
+ let identifier = param.type === "AssignmentPattern" ? param.left : param;
367
+ ok(
368
+ identifier.type === "Identifier",
369
+ "Only simple identifiers allowed as parameters",
370
+ );
371
+
372
+ ctx.scope.define(identifier.name);
373
+ }
374
+
375
+ // Reserve the next slot for the implicit `arguments` object.
376
+ // Slot index will always equal paramCount (params are 0..paramCount-1).
377
+ ctx.scope.define("arguments");
378
+
379
+ // Pass 2: emit default-value guards at top of fn body
380
+ // Mirrors what JS engines do: if the caller passed undefined (or
381
+ // nothing), evaluate the default expression and overwrite the slot.
382
+ for (const param of node.params) {
383
+ if (param.type !== "AssignmentPattern") continue;
384
+
385
+ const slot = ctx.scope._locals.get((param.left as t.Identifier).name);
386
+ const skipLabel = this._makeLabel("param_skip");
387
+
388
+ // if (param === undefined) param = <default expr>
389
+ this.emit(ctx.bc, [this.OP.LOAD_LOCAL, slot], param);
390
+ this.emit(
391
+ ctx.bc,
392
+ [this.OP.LOAD_CONST, b.constantOperand(undefined)],
393
+ param,
394
+ );
395
+ this.emit(ctx.bc, [this.OP.EQ], param);
396
+ this.emit(
397
+ ctx.bc,
398
+ [this.OP.JUMP_IF_FALSE, { type: "label", label: skipLabel }],
399
+ param,
400
+ );
401
+
402
+ this._compileExpr(param.right, ctx.scope, ctx.bc); // eval default
403
+ this.emit(ctx.bc, [this.OP.STORE_LOCAL, slot], param);
404
+
405
+ this.emit(
406
+ ctx.bc,
407
+ [null, { type: "defineLabel", label: skipLabel }],
408
+ param,
409
+ );
410
+ }
411
+
412
+ for (const stmt of node.body.body) {
413
+ this._compileStatement(stmt, ctx.scope, ctx.bc);
414
+ }
415
+
416
+ // If we fall off the end of the function, implicitly return undefined.
417
+ this.emit(ctx.bc, [this.OP.LOAD_CONST, b.constantOperand(undefined)], node);
418
+ this.emit(ctx.bc, [this.OP.RETURN], node);
419
+
420
+ this._currentCtx = savedCtx; // restore before touching fnDescriptors
421
+ this._loopStack = savedLoopStack;
422
+
423
+ (node as any)._fnIdx = fnIdx;
424
+
425
+ // Fill the placeholder that was reserved at the top of this function.
426
+ // Metadata (paramCount, localCount, upvalues) is stored on desc and emitted
427
+ // as inline operands on the MAKE_CLOSURE instruction via _emitMakeClosure.
428
+ desc.name = node.id?.name || "<anonymous>";
429
+ desc.entryLabel = entryLabel;
430
+ desc.bytecode = ctx.bc as b.Bytecode;
431
+ desc._fnIdx = fnIdx;
432
+ desc.paramCount = node.params.length;
433
+ desc.localCount = ctx.scope.localCount;
434
+ desc.upvalues = ctx.upvalues.slice();
435
+
436
+ return desc;
437
+ }
438
+
439
+ // Emit a single MAKE_CLOSURE instruction with all closure metadata packed
440
+ // as inline operands. The runtime reads them via _operand() no stack
441
+ // shuffling needed.
442
+ //
443
+ // Flat operand layout: startPc, paramCount, localCount, uvCount,
444
+ // [isLocal_0, idx_0, isLocal_1, idx_1, ...]
445
+ _emitMakeClosure(desc: any, node: t.Node, bc: b.Bytecode) {
446
+ const uvOperands: (number | b.InstrOperand)[] = [];
447
+ for (const uv of desc.upvalues) {
448
+ uvOperands.push(uv.isLocal ? 1 : 0);
449
+ uvOperands.push(uv.index);
450
+ }
451
+ this.emit(
452
+ bc,
453
+ [
454
+ this.OP.MAKE_CLOSURE,
455
+ { type: "label", label: desc.entryLabel },
456
+ desc.paramCount,
457
+ desc.localCount,
458
+ desc.upvalues.length,
459
+ ...uvOperands,
460
+ ] as b.Instruction,
461
+ node,
462
+ );
463
+ }
464
+
465
+ // Main (top-level)
466
+ _compileMain(body: t.Statement[]) {
467
+ const bc = this.bytecode;
468
+
469
+ // Hoist all FunctionDeclarations: MAKE_CLOSURE -> STORE_GLOBAL
470
+ // (mirrors JS hoisting -- functions are available before other code)
471
+ for (const node of body) {
472
+ if (node.type !== "FunctionDeclaration") continue;
473
+ const desc = this.fnDescriptors.find(
474
+ (d) => d._fnIdx === (node as any)._fnIdx,
475
+ );
476
+ const nameRef = b.constantOperand(node.id.name);
477
+ this._emitMakeClosure(desc, node, bc);
478
+ this.emit(bc, [this.OP.STORE_GLOBAL, nameRef], node);
479
+ }
480
+
481
+ // Compile everything else in order
482
+ for (const node of body) {
483
+ if (node.type === "FunctionDeclaration") continue;
484
+ this._compileStatement(node, null, bc); // null scope -> global context
485
+ }
486
+
487
+ this.emit(bc, [this.OP.RETURN], null); // end program
488
+
489
+ // Append all function bodies. Each function's entryLabel (already generated
490
+ // in _compileFunctionDecl) points directly to the first body instruction;
491
+ // metadata is pushed onto the stack at each call site, not stored inline.
492
+ for (const descriptor of this.fnDescriptors) {
493
+ this.bytecode.push([
494
+ null,
495
+ { type: "defineLabel", label: descriptor.entryLabel },
496
+ ]);
497
+ for (const instr of descriptor.bytecode) {
498
+ this.bytecode.push(instr);
499
+ }
500
+ }
501
+ }
502
+
503
+ // Statements
504
+ _compileStatement(node: t.Statement, scope: Scope, bc: b.Bytecode) {
505
+ switch (node.type) {
506
+ case "EmptyStatement": {
507
+ // nothing to emit -- bare semicolon is a no-op
508
+ break;
509
+ }
510
+
511
+ case "DebuggerStatement":
512
+ this.emit(bc, [this.OP.DEBUGGER], node);
513
+ break;
514
+
515
+ case "BlockStatement": {
516
+ for (const stmt of node.body) {
517
+ this._compileStatement(stmt, scope, bc);
518
+ }
519
+ break;
520
+ }
521
+
522
+ case "FunctionDeclaration": {
523
+ // Nested function -- compile it into a descriptor, then emit
524
+ // MAKE_CLOSURE so it's captured as a live closure at runtime.
525
+ // (_compileFunctionDecl pushes/pops _currentCtx internally)
526
+ const desc = this._compileFunctionDecl(node);
527
+ this._emitMakeClosure(desc, node, bc);
528
+ if (scope) {
529
+ const slot = scope.define(node.id.name);
530
+ this.emit(bc, [this.OP.STORE_LOCAL, slot], node);
531
+ } else {
532
+ this.emit(
533
+ bc,
534
+ [this.OP.STORE_GLOBAL, b.constantOperand(node.id.name)],
535
+ node,
536
+ );
537
+ }
538
+ break;
539
+ }
540
+
541
+ case "ThrowStatement": {
542
+ this._compileExpr(node.argument, scope, bc);
543
+ this.emit(bc, [this.OP.THROW], node);
544
+ break;
545
+ }
546
+
547
+ case "ReturnStatement": {
548
+ if (node.argument) {
549
+ this._compileExpr(node.argument, scope, bc);
550
+ } else {
551
+ this.emit(
552
+ bc,
553
+ [this.OP.LOAD_CONST, b.constantOperand(undefined)],
554
+ node,
555
+ );
556
+ }
557
+ // Disarm any open try handlers before leaving the function.
558
+ // TRY_END only touches frame._handlerStack, not the value stack,
559
+ // so the return value sitting on top is safe.
560
+ for (let _ri = this._loopStack.length - 1; _ri >= 0; _ri--) {
561
+ if ((this._loopStack[_ri].type as any) === "try") {
562
+ this.emit(bc, [this.OP.TRY_END], node);
563
+ }
564
+ }
565
+ this.emit(bc, [this.OP.RETURN], node);
566
+ break;
567
+ }
568
+
569
+ case "ExpressionStatement": {
570
+ this._compileExpr(node.expression, scope, bc);
571
+ this.emit(bc, [this.OP.POP], node); // discard return value of statement-level expressions
572
+ break;
573
+ }
574
+
575
+ case "VariableDeclaration": {
576
+ for (const decl of node.declarations) {
577
+ // Push the initialiser (or undefined if absent)
578
+ if (decl.init) {
579
+ this._compileExpr(decl.init, scope, bc);
580
+ } else {
581
+ this.emit(
582
+ bc,
583
+ [this.OP.LOAD_CONST, b.constantOperand(undefined)],
584
+ node,
585
+ );
586
+ }
587
+
588
+ ok(
589
+ decl.id.type === "Identifier",
590
+ "Only simple identifiers can be declared",
591
+ );
592
+
593
+ // Store: local slot if inside a function, global name otherwise
594
+ if (scope) {
595
+ const slot = scope.define(decl.id.name);
596
+ this.emit(bc, [this.OP.STORE_LOCAL, slot], node);
597
+ } else {
598
+ this.emit(
599
+ bc,
600
+ [this.OP.STORE_GLOBAL, b.constantOperand(decl.id.name)],
601
+ node,
602
+ );
603
+ }
604
+ }
605
+ break;
606
+ }
607
+
608
+ case "IfStatement": {
609
+ const elseOrEndLabel = this._makeLabel("if_else");
610
+ // 1. Compile the test expression -> leaves a value on the stack
611
+ this._compileExpr(node.test, scope, bc);
612
+ // 2. Emit JUMP_IF_FALSE to the else branch (or end if no else)
613
+ this.emit(
614
+ bc,
615
+ [this.OP.JUMP_IF_FALSE, { type: "label", label: elseOrEndLabel }],
616
+ node,
617
+ );
618
+ // 3. Compile the consequent block (the "then" branch)
619
+ const consequentBody =
620
+ node.consequent.type === "BlockStatement"
621
+ ? node.consequent.body
622
+ : [node.consequent];
623
+ for (const stmt of consequentBody) {
624
+ this._compileStatement(stmt, scope, bc);
625
+ }
626
+ if (node.alternate) {
627
+ // 4a. Consequent needs to jump OVER the else block when done
628
+ const endLabel = this._makeLabel("if_end");
629
+ this.emit(
630
+ bc,
631
+ [this.OP.JUMP, { type: "label", label: endLabel }],
632
+ node,
633
+ );
634
+ // Mark start of else
635
+ this.emit(
636
+ bc,
637
+ [null, { type: "defineLabel", label: elseOrEndLabel }],
638
+ node,
639
+ );
640
+ // 5. Compile the alternate (else) block
641
+ const altBody =
642
+ node.alternate.type === "BlockStatement"
643
+ ? node.alternate.body
644
+ : [node.alternate]; // handles `else if` -- it's just a nested IfStatement
645
+ for (const stmt of altBody) {
646
+ this._compileStatement(stmt, scope, bc);
647
+ }
648
+ // Mark end (consequent's jump lands here)
649
+ this.emit(bc, [null, { type: "defineLabel", label: endLabel }], node);
650
+ } else {
651
+ // 4b. No else -- label lands right after the then block
652
+ this.emit(
653
+ bc,
654
+ [null, { type: "defineLabel", label: elseOrEndLabel }],
655
+ node,
656
+ );
657
+ }
658
+ break;
659
+ }
660
+
661
+ case "WhileStatement": {
662
+ const _wLabel = this._pendingLabel;
663
+ this._pendingLabel = null;
664
+
665
+ const loopTopLabel = this._makeLabel("while_top");
666
+ const exitLabel = this._makeLabel("while_exit");
667
+
668
+ this._loopStack.push({
669
+ type: "loop",
670
+ label: _wLabel,
671
+ breakLabel: exitLabel,
672
+ continueLabel: loopTopLabel, // continue re-evaluates the test
673
+ });
674
+
675
+ this.emit(
676
+ bc,
677
+ [null, { type: "defineLabel", label: loopTopLabel }],
678
+ node,
679
+ );
680
+ this._compileExpr(node.test, scope, bc);
681
+ this.emit(
682
+ bc,
683
+ [this.OP.JUMP_IF_FALSE, { type: "label", label: exitLabel }],
684
+ node,
685
+ );
686
+
687
+ const whileBody =
688
+ node.body.type === "BlockStatement" ? node.body.body : [node.body];
689
+ for (const stmt of whileBody) {
690
+ this._compileStatement(stmt, scope, bc);
691
+ }
692
+
693
+ this.emit(
694
+ bc,
695
+ [this.OP.JUMP, { type: "label", label: loopTopLabel }],
696
+ node,
697
+ );
698
+ this.emit(bc, [null, { type: "defineLabel", label: exitLabel }], node);
699
+
700
+ this._loopStack.pop();
701
+ break;
702
+ }
703
+
704
+ case "DoWhileStatement": {
705
+ const _dwLabel = this._pendingLabel;
706
+ this._pendingLabel = null;
707
+
708
+ const loopTopLabel = this._makeLabel("dowhile_top");
709
+ const continueLabel = this._makeLabel("dowhile_cont");
710
+ const exitLabel = this._makeLabel("dowhile_exit");
711
+
712
+ this._loopStack.push({
713
+ type: "loop",
714
+ label: _dwLabel,
715
+ breakLabel: exitLabel,
716
+ continueLabel: continueLabel, // continue falls to the test
717
+ });
718
+
719
+ this.emit(
720
+ bc,
721
+ [null, { type: "defineLabel", label: loopTopLabel }],
722
+ node,
723
+ );
724
+
725
+ const doWhileBody =
726
+ node.body.type === "BlockStatement" ? node.body.body : [node.body];
727
+ for (const stmt of doWhileBody) {
728
+ this._compileStatement(stmt, scope, bc);
729
+ }
730
+
731
+ // continue -> skip rest of body, fall through to test
732
+ this.emit(
733
+ bc,
734
+ [null, { type: "defineLabel", label: continueLabel }],
735
+ node,
736
+ );
737
+ this._compileExpr(node.test, scope, bc);
738
+ this.emit(
739
+ bc,
740
+ [this.OP.JUMP_IF_FALSE, { type: "label", label: exitLabel }],
741
+ node,
742
+ );
743
+ this.emit(
744
+ bc,
745
+ [this.OP.JUMP, { type: "label", label: loopTopLabel }],
746
+ node,
747
+ );
748
+
749
+ this.emit(bc, [null, { type: "defineLabel", label: exitLabel }], node);
750
+
751
+ this._loopStack.pop();
752
+ break;
753
+ }
754
+
755
+ case "ForStatement": {
756
+ const _fLabel = this._pendingLabel;
757
+ this._pendingLabel = null;
758
+
759
+ const loopTopLabel = this._makeLabel("for_top");
760
+ const exitLabel = this._makeLabel("for_exit");
761
+ // continue jumps to the update clause if present, else straight to test
762
+ const updateLabel = node.update
763
+ ? this._makeLabel("for_update")
764
+ : loopTopLabel;
765
+
766
+ this._loopStack.push({
767
+ type: "loop",
768
+ label: _fLabel,
769
+ breakLabel: exitLabel,
770
+ continueLabel: updateLabel,
771
+ });
772
+
773
+ if (node.init) {
774
+ if (node.init.type === "VariableDeclaration") {
775
+ this._compileStatement(node.init, scope, bc);
776
+ } else {
777
+ this._compileExpr(node.init, scope, bc);
778
+ this.emit(bc, [this.OP.POP], node);
779
+ }
780
+ }
781
+
782
+ this.emit(
783
+ bc,
784
+ [null, { type: "defineLabel", label: loopTopLabel }],
785
+ node,
786
+ );
787
+ if (node.test) {
788
+ this._compileExpr(node.test, scope, bc);
789
+ this.emit(
790
+ bc,
791
+ [this.OP.JUMP_IF_FALSE, { type: "label", label: exitLabel }],
792
+ node,
793
+ );
794
+ }
795
+
796
+ const forBody =
797
+ node.body.type === "BlockStatement" ? node.body.body : [node.body];
798
+ for (const stmt of forBody) {
799
+ this._compileStatement(stmt, scope, bc);
800
+ }
801
+
802
+ // continue -> run update (if any) then back to test
803
+ if (node.update) {
804
+ this.emit(
805
+ bc,
806
+ [null, { type: "defineLabel", label: updateLabel }],
807
+ node,
808
+ );
809
+ this._compileExpr(node.update, scope, bc);
810
+ this.emit(bc, [this.OP.POP], node);
811
+ }
812
+
813
+ this.emit(
814
+ bc,
815
+ [this.OP.JUMP, { type: "label", label: loopTopLabel }],
816
+ node,
817
+ );
818
+ this.emit(bc, [null, { type: "defineLabel", label: exitLabel }], node);
819
+
820
+ this._loopStack.pop();
821
+ break;
822
+ }
823
+
824
+ case "BreakStatement": {
825
+ // Find the jump target in the loop stack.
826
+ let _bTargetIdx = -1;
827
+ if (node.label) {
828
+ const _bLabelName = node.label.name;
829
+ for (let _bi = this._loopStack.length - 1; _bi >= 0; _bi--) {
830
+ if (this._loopStack[_bi].label === _bLabelName) {
831
+ _bTargetIdx = _bi;
832
+ break;
833
+ }
834
+ }
835
+ if (_bTargetIdx === -1)
836
+ throw new Error(`Label '${node.label.name}' not found`);
837
+ } else {
838
+ // Find innermost loop/switch/block (skip "try" entries)
839
+ for (let _bi = this._loopStack.length - 1; _bi >= 0; _bi--) {
840
+ if ((this._loopStack[_bi].type as any) !== "try") {
841
+ _bTargetIdx = _bi;
842
+ break;
843
+ }
844
+ }
845
+ if (_bTargetIdx === -1) throw new Error("break outside loop");
846
+ }
847
+ // Emit TRY_END for every open try block between here and the target.
848
+ for (let _bi = this._loopStack.length - 1; _bi > _bTargetIdx; _bi--) {
849
+ if ((this._loopStack[_bi].type as any) === "try") {
850
+ this.emit(bc, [this.OP.TRY_END], node);
851
+ }
852
+ }
853
+ this.emit(
854
+ bc,
855
+ [
856
+ this.OP.JUMP,
857
+ { type: "label", label: this._loopStack[_bTargetIdx].breakLabel },
858
+ ],
859
+ node,
860
+ );
861
+ break;
862
+ }
863
+
864
+ case "ContinueStatement": {
865
+ // Find the target loop in the loop stack.
866
+ let _cTargetIdx = -1;
867
+ if (node.label) {
868
+ const _cLabelName = node.label.name;
869
+ for (let _ci = this._loopStack.length - 1; _ci >= 0; _ci--) {
870
+ if (
871
+ this._loopStack[_ci].label === _cLabelName &&
872
+ this._loopStack[_ci].type === "loop"
873
+ ) {
874
+ _cTargetIdx = _ci;
875
+ break;
876
+ }
877
+ }
878
+ if (_cTargetIdx === -1)
879
+ throw new Error(
880
+ `Label '${node.label.name}' not found for continue`,
881
+ );
882
+ } else {
883
+ // Find the innermost loop (skip switch, block, and try contexts)
884
+ for (let _ci = this._loopStack.length - 1; _ci >= 0; _ci--) {
885
+ if (this._loopStack[_ci].type === "loop") {
886
+ _cTargetIdx = _ci;
887
+ break;
888
+ }
889
+ }
890
+ if (_cTargetIdx === -1) throw new Error("continue outside loop");
891
+ }
892
+ // Emit TRY_END for every open try block between here and the target loop.
893
+ for (let _ci = this._loopStack.length - 1; _ci > _cTargetIdx; _ci--) {
894
+ if ((this._loopStack[_ci].type as any) === "try") {
895
+ this.emit(bc, [this.OP.TRY_END], node);
896
+ }
897
+ }
898
+ this.emit(
899
+ bc,
900
+ [
901
+ this.OP.JUMP,
902
+ {
903
+ type: "label",
904
+ label: this._loopStack[_cTargetIdx].continueLabel,
905
+ },
906
+ ],
907
+ node,
908
+ );
909
+ break;
910
+ }
911
+
912
+ case "SwitchStatement": {
913
+ const _swLabel = this._pendingLabel;
914
+ this._pendingLabel = null;
915
+
916
+ const switchBreakLabel = this._makeLabel("sw_break");
917
+
918
+ this._loopStack.push({
919
+ type: "switch",
920
+ label: _swLabel,
921
+ breakLabel: switchBreakLabel,
922
+ continueLabel: switchBreakLabel, // not used for switch
923
+ });
924
+
925
+ // Compile the discriminant and leave it on the stack
926
+ this._compileExpr(node.discriminant, scope, bc);
927
+
928
+ const cases = node.cases;
929
+ const defaultIdx = cases.findIndex((c) => c.test === null);
930
+
931
+ // Pre-allocate a label for each case body so dispatch can reference them
932
+ const caseLabels = cases.map((_, i) => this._makeLabel(`sw_case_${i}`));
933
+
934
+ // Dispatch section: for each non-default case, check and jump to its body
935
+ for (let i = 0; i < cases.length; i++) {
936
+ const cas = cases[i];
937
+ if (cas.test === null) continue; // skip default in dispatch
938
+
939
+ const nextCheckLabel = this._makeLabel("sw_next");
940
+ this.emit(bc, [this.OP.DUP], node);
941
+ this._compileExpr(cas.test, scope, bc);
942
+ this.emit(bc, [this.OP.EQ], node);
943
+ // If not matched, fall through to the next check
944
+ this.emit(
945
+ bc,
946
+ [this.OP.JUMP_IF_FALSE, { type: "label", label: nextCheckLabel }],
947
+ node,
948
+ );
949
+ // If matched, jump directly to this case's body
950
+ this.emit(
951
+ bc,
952
+ [this.OP.JUMP, { type: "label", label: caseLabels[i] }],
953
+ node,
954
+ );
955
+ this.emit(
956
+ bc,
957
+ [null, { type: "defineLabel", label: nextCheckLabel }],
958
+ node,
959
+ );
960
+ }
961
+
962
+ // No case matched: jump to default body or exit (which pops discriminant)
963
+ this.emit(
964
+ bc,
965
+ [
966
+ this.OP.JUMP,
967
+ {
968
+ type: "label",
969
+ label:
970
+ defaultIdx !== -1 ? caseLabels[defaultIdx] : switchBreakLabel,
971
+ },
972
+ ],
973
+ node,
974
+ );
975
+
976
+ // Body section: compile all case bodies in source order (fallthrough intact)
977
+ for (let i = 0; i < cases.length; i++) {
978
+ this.emit(
979
+ bc,
980
+ [null, { type: "defineLabel", label: caseLabels[i] }],
981
+ node,
982
+ );
983
+ for (const stmt of cases[i].consequent) {
984
+ this._compileStatement(stmt, scope, bc);
985
+ }
986
+ }
987
+
988
+ // break label lands here; pop the discriminant and continue after switch
989
+ this.emit(
990
+ bc,
991
+ [null, { type: "defineLabel", label: switchBreakLabel }],
992
+ node,
993
+ );
994
+ this.emit(bc, [this.OP.POP], node);
995
+
996
+ this._loopStack.pop();
997
+ break;
998
+ }
999
+
1000
+ case "LabeledStatement": {
1001
+ const _lName = node.label.name;
1002
+ const _lBody = node.body;
1003
+ const _lIsLoop =
1004
+ _lBody.type === "ForStatement" ||
1005
+ _lBody.type === "WhileStatement" ||
1006
+ _lBody.type === "DoWhileStatement" ||
1007
+ _lBody.type === "ForInStatement";
1008
+ const _lIsSwitch = _lBody.type === "SwitchStatement";
1009
+
1010
+ if (_lIsLoop || _lIsSwitch) {
1011
+ // Pass label down to the loop/switch handler via _pendingLabel
1012
+ this._pendingLabel = _lName;
1013
+ this._compileStatement(_lBody, scope, bc);
1014
+ this._pendingLabel = null; // safety clear if handler didn't consume it
1015
+ } else {
1016
+ // Non-loop labeled statement (e.g. labeled block) -- only break is valid
1017
+ const blockBreakLabel = this._makeLabel("block_break");
1018
+ this._loopStack.push({
1019
+ type: "block",
1020
+ label: _lName,
1021
+ breakLabel: blockBreakLabel,
1022
+ continueLabel: blockBreakLabel, // unused
1023
+ });
1024
+ this._compileStatement(_lBody, scope, bc);
1025
+ this._loopStack.pop();
1026
+ this.emit(
1027
+ bc,
1028
+ [null, { type: "defineLabel", label: blockBreakLabel }],
1029
+ node,
1030
+ );
1031
+ }
1032
+ break;
1033
+ }
1034
+
1035
+ case "ForInStatement": {
1036
+ const _fiLabel = this._pendingLabel;
1037
+ this._pendingLabel = null;
1038
+
1039
+ // Evaluate the object expression -> on stack
1040
+ this._compileExpr(node.right, scope, bc);
1041
+ // FOR_IN_SETUP: pops obj, pushes iterator {keys, i}
1042
+ this.emit(bc, [this.OP.FOR_IN_SETUP], node);
1043
+
1044
+ // Store iterator in a hidden slot so break/continue need no cleanup
1045
+ let emitLoadIter: () => void;
1046
+ let emitStoreIter: () => void;
1047
+ if (scope) {
1048
+ // Reserve a hidden local slot (no name mapping needed)
1049
+ const iterSlot = scope._next++;
1050
+ emitLoadIter = () =>
1051
+ this.emit(bc, [this.OP.LOAD_LOCAL, iterSlot], node);
1052
+ emitStoreIter = () =>
1053
+ this.emit(bc, [this.OP.STORE_LOCAL, iterSlot], node);
1054
+ } else {
1055
+ // Top level -- use a synthetic global that won't collide with user code
1056
+ const iterNameIdx = b.constantOperand("__fi" + this._forInCount++);
1057
+ emitLoadIter = () =>
1058
+ this.emit(bc, [this.OP.LOAD_GLOBAL, iterNameIdx], node);
1059
+ emitStoreIter = () =>
1060
+ this.emit(bc, [this.OP.STORE_GLOBAL, iterNameIdx], node);
1061
+ }
1062
+ emitStoreIter();
1063
+
1064
+ const loopTopLabel = this._makeLabel("forin_top");
1065
+ const exitLabel = this._makeLabel("forin_exit");
1066
+
1067
+ this._loopStack.push({
1068
+ type: "loop",
1069
+ label: _fiLabel,
1070
+ breakLabel: exitLabel,
1071
+ continueLabel: loopTopLabel, // continue re-checks the iterator
1072
+ });
1073
+
1074
+ this.emit(
1075
+ bc,
1076
+ [null, { type: "defineLabel", label: loopTopLabel }],
1077
+ node,
1078
+ );
1079
+
1080
+ // Load iterator, attempt to get next key
1081
+ emitLoadIter();
1082
+ this.emit(
1083
+ bc,
1084
+ [this.OP.FOR_IN_NEXT, { type: "label", label: exitLabel }],
1085
+ node,
1086
+ );
1087
+
1088
+ // Assign the key (now on top of stack) to the loop variable
1089
+ if (node.left.type === "VariableDeclaration") {
1090
+ const identifier = node.left.declarations[0].id;
1091
+ ok(
1092
+ identifier.type === "Identifier",
1093
+ "Only simple identifiers can be declared in for-in loops",
1094
+ );
1095
+ const name = identifier.name;
1096
+ if (scope) {
1097
+ const slot = scope.define(name);
1098
+ this.emit(bc, [this.OP.STORE_LOCAL, slot], node);
1099
+ } else {
1100
+ this.emit(
1101
+ bc,
1102
+ [this.OP.STORE_GLOBAL, b.constantOperand(name)],
1103
+ node,
1104
+ );
1105
+ }
1106
+ } else if (node.left.type === "Identifier") {
1107
+ const res = this._resolve(node.left.name, this._currentCtx);
1108
+ if (res.kind === "local") {
1109
+ this.emit(bc, [this.OP.STORE_LOCAL, res.slot], node);
1110
+ } else if (res.kind === "upvalue") {
1111
+ this.emit(bc, [this.OP.STORE_UPVALUE, res.index], node);
1112
+ } else {
1113
+ this.emit(
1114
+ bc,
1115
+ [this.OP.STORE_GLOBAL, b.constantOperand(node.left.name)],
1116
+ node,
1117
+ );
1118
+ }
1119
+ } else {
1120
+ const src = generate(node.left).code;
1121
+ throw new Error(
1122
+ `Unsupported for-in left-hand side: ${node.left.type}\n -> ${src}`,
1123
+ );
1124
+ }
1125
+
1126
+ // Compile the loop body
1127
+ const fiBody =
1128
+ node.body.type === "BlockStatement" ? node.body.body : [node.body];
1129
+ for (const stmt of fiBody) {
1130
+ this._compileStatement(stmt, scope, bc);
1131
+ }
1132
+
1133
+ this.emit(
1134
+ bc,
1135
+ [this.OP.JUMP, { type: "label", label: loopTopLabel }],
1136
+ node,
1137
+ );
1138
+ this.emit(bc, [null, { type: "defineLabel", label: exitLabel }], node);
1139
+
1140
+ this._loopStack.pop();
1141
+ break;
1142
+ }
1143
+
1144
+ case "TryStatement": {
1145
+ if (node.finalizer) {
1146
+ throw new Error(
1147
+ "try..finally is not supported. Use a helper function instead",
1148
+ );
1149
+ }
1150
+ if (!node.handler) {
1151
+ // try without catch requires finally — not supported
1152
+ throw new Error(
1153
+ "try without catch is not supported (requires finally).",
1154
+ );
1155
+ }
1156
+
1157
+ const catchLabel = this._makeLabel("catch");
1158
+ const afterCatchLabel = this._makeLabel("after_catch");
1159
+
1160
+ // Emit TRY_SETUP with the catch block's label as the handler PC.
1161
+ // At runtime: saves stack depth + frame stack depth, pushes handler.
1162
+ this.emit(
1163
+ bc,
1164
+ [this.OP.TRY_SETUP, { type: "label", label: catchLabel }],
1165
+ node,
1166
+ );
1167
+
1168
+ // Track the open try block so that break/continue/return inside the
1169
+ // try body can emit the matching TRY_END before their jump.
1170
+ this._loopStack.push({
1171
+ type: "try" as any,
1172
+ label: null,
1173
+ breakLabel: "", // unused
1174
+ continueLabel: "", // unused
1175
+ });
1176
+
1177
+ // Compile try body
1178
+ for (const stmt of node.block.body) {
1179
+ this._compileStatement(stmt, scope, bc);
1180
+ }
1181
+
1182
+ // Done compiling the try body — pop the tracking entry.
1183
+ this._loopStack.pop();
1184
+
1185
+ // Normal exit: disarm the exception handler.
1186
+ this.emit(bc, [this.OP.TRY_END], node);
1187
+
1188
+ // Jump over the catch block on normal path.
1189
+ this.emit(
1190
+ bc,
1191
+ [this.OP.JUMP, { type: "label", label: afterCatchLabel }],
1192
+ node,
1193
+ );
1194
+
1195
+ // Catch block: exception is on top of the stack (pushed by the VM).
1196
+ this.emit(bc, [null, { type: "defineLabel", label: catchLabel }], node);
1197
+
1198
+ const handler = node.handler;
1199
+ if (handler.param) {
1200
+ // Bind the exception value to the catch variable.
1201
+ const name = (handler.param as t.Identifier).name;
1202
+ if (scope) {
1203
+ const slot = scope.define(name);
1204
+ this.emit(bc, [this.OP.STORE_LOCAL, slot], node);
1205
+ } else {
1206
+ this.emit(
1207
+ bc,
1208
+ [this.OP.STORE_GLOBAL, b.constantOperand(name)],
1209
+ node,
1210
+ );
1211
+ }
1212
+ } else {
1213
+ // Optional catch binding (catch without a variable — ES2019+)
1214
+ this.emit(bc, [this.OP.POP], node);
1215
+ }
1216
+
1217
+ // Compile catch body
1218
+ for (const stmt of handler.body.body) {
1219
+ this._compileStatement(stmt, scope, bc);
1220
+ }
1221
+
1222
+ // Normal-path jump lands here (after the catch block).
1223
+ this.emit(
1224
+ bc,
1225
+ [null, { type: "defineLabel", label: afterCatchLabel }],
1226
+ node,
1227
+ );
1228
+ break;
1229
+ }
1230
+
1231
+ default: {
1232
+ // Use @babel/generator to reproduce the source of unsupported nodes
1233
+ // so we can emit a clear error with context.
1234
+ const src = generate(node).code;
1235
+ throw new Error(`Unsupported statement: ${node.type}\n -> ${src}`);
1236
+ }
1237
+ }
1238
+ }
1239
+
1240
+ // Expressions
1241
+ _compileExpr(node, scope, bc) {
1242
+ switch (node.type) {
1243
+ case "NumericLiteral":
1244
+ case "StringLiteral": {
1245
+ this.emit(
1246
+ bc,
1247
+ [this.OP.LOAD_CONST, b.constantOperand(node.value)],
1248
+ node,
1249
+ );
1250
+ break;
1251
+ }
1252
+
1253
+ case "BooleanLiteral": {
1254
+ this.emit(
1255
+ bc,
1256
+ [this.OP.LOAD_CONST, b.constantOperand(node.value)],
1257
+ node,
1258
+ );
1259
+ break;
1260
+ }
1261
+
1262
+ case "NullLiteral": {
1263
+ this.emit(bc, [this.OP.LOAD_CONST, b.constantOperand(null)], node);
1264
+ break;
1265
+ }
1266
+
1267
+ case "Identifier": {
1268
+ // scope=null means we're at the top-level -> always global
1269
+ const res = this._resolve(node.name, this._currentCtx);
1270
+ if (res.kind === "local") {
1271
+ this.emit(bc, [this.OP.LOAD_LOCAL, res.slot], node);
1272
+ } else if (res.kind === "upvalue") {
1273
+ this.emit(bc, [this.OP.LOAD_UPVALUE, res.index], node);
1274
+ } else {
1275
+ this.emit(
1276
+ bc,
1277
+ [this.OP.LOAD_GLOBAL, b.constantOperand(node.name)],
1278
+ node,
1279
+ );
1280
+ }
1281
+ break;
1282
+ }
1283
+
1284
+ case "ThisExpression": {
1285
+ this.emit(bc, [this.OP.LOAD_THIS], node);
1286
+ break;
1287
+ }
1288
+
1289
+ case "NewExpression": {
1290
+ // Push callee, then args -- identical layout to CALL but uses NEW opcode
1291
+ this._compileExpr(node.callee, scope, bc);
1292
+ for (const arg of node.arguments) this._compileExpr(arg, scope, bc);
1293
+ this.emit(bc, [this.OP.NEW, node.arguments.length], node);
1294
+ break;
1295
+ }
1296
+
1297
+ case "SequenceExpression": {
1298
+ // (a, b, c) -> eval a -> POP, eval b -> POP, eval c -> leave on stack
1299
+ for (let i = 0; i < node.expressions.length - 1; i++) {
1300
+ this._compileExpr(node.expressions[i], scope, bc);
1301
+ this.emit(bc, [this.OP.POP], node); // discard intermediate result
1302
+ }
1303
+ // Last expression -- its value is the result of the whole sequence
1304
+ this._compileExpr(
1305
+ node.expressions[node.expressions.length - 1],
1306
+ scope,
1307
+ bc,
1308
+ );
1309
+ break;
1310
+ }
1311
+
1312
+ case "ConditionalExpression": {
1313
+ // test ? consequent : alternate
1314
+ const elseLabel = this._makeLabel("ternary_else");
1315
+ const endLabel = this._makeLabel("ternary_end");
1316
+
1317
+ this._compileExpr(node.test, scope, bc);
1318
+ this.emit(
1319
+ bc,
1320
+ [this.OP.JUMP_IF_FALSE, { type: "label", label: elseLabel }],
1321
+ node,
1322
+ );
1323
+
1324
+ this._compileExpr(node.consequent, scope, bc);
1325
+ this.emit(bc, [this.OP.JUMP, { type: "label", label: endLabel }], node);
1326
+
1327
+ this.emit(bc, [null, { type: "defineLabel", label: elseLabel }], node);
1328
+ this._compileExpr(node.alternate, scope, bc);
1329
+
1330
+ this.emit(bc, [null, { type: "defineLabel", label: endLabel }], node);
1331
+ break;
1332
+ }
1333
+
1334
+ case "LogicalExpression": {
1335
+ // Pattern (CPython-style):
1336
+ // eval LHS
1337
+ // JUMP_IF_*_OR_POP -> target (past RHS)
1338
+ // eval RHS ← only reached if LHS didn't short-circuit
1339
+ // [target lands here, stack top is the result either way]
1340
+
1341
+ this._compileExpr(node.left, scope, bc);
1342
+
1343
+ if (node.operator === "||") {
1344
+ // Short-circuit if LHS is TRUTHY -- keep it, skip RHS
1345
+ const endLabel = this._makeLabel("or_end");
1346
+ this.emit(
1347
+ bc,
1348
+ [this.OP.JUMP_IF_TRUE_OR_POP, { type: "label", label: endLabel }],
1349
+ node,
1350
+ );
1351
+ this._compileExpr(node.right, scope, bc);
1352
+ this.emit(bc, [null, { type: "defineLabel", label: endLabel }], node);
1353
+ } else if (node.operator === "&&") {
1354
+ // Short-circuit if LHS is FALSY -- keep it, skip RHS
1355
+ const endLabel = this._makeLabel("and_end");
1356
+ this.emit(
1357
+ bc,
1358
+ [this.OP.JUMP_IF_FALSE_OR_POP, { type: "label", label: endLabel }],
1359
+ node,
1360
+ );
1361
+ this._compileExpr(node.right, scope, bc);
1362
+ this.emit(bc, [null, { type: "defineLabel", label: endLabel }], node);
1363
+ } else {
1364
+ throw new Error(`Unsupported logical operator: ${node.operator}`);
1365
+ }
1366
+ break;
1367
+ }
1368
+
1369
+ case "BinaryExpression": {
1370
+ this._compileExpr(node.left, scope, bc);
1371
+ this._compileExpr(node.right, scope, bc);
1372
+ const arithOp = {
1373
+ "+": this.OP.ADD,
1374
+ "-": this.OP.SUB,
1375
+ "*": this.OP.MUL,
1376
+ "/": this.OP.DIV,
1377
+ "%": this.OP.MOD,
1378
+ "&": this.OP.BAND,
1379
+ "|": this.OP.BOR,
1380
+ "^": this.OP.BXOR,
1381
+ "<<": this.OP.SHL,
1382
+ ">>": this.OP.SHR,
1383
+ ">>>": this.OP.USHR,
1384
+ }[node.operator];
1385
+
1386
+ const cmpOp = {
1387
+ "<": this.OP.LT,
1388
+ ">": this.OP.GT,
1389
+ "===": this.OP.EQ,
1390
+ "==": this.OP.LOOSE_EQ,
1391
+ "<=": this.OP.LTE,
1392
+ ">=": this.OP.GTE,
1393
+ "!==": this.OP.NEQ,
1394
+ "!=": this.OP.LOOSE_NEQ,
1395
+ in: this.OP.IN, // ← add
1396
+ instanceof: this.OP.INSTANCEOF, // ← add
1397
+ }[node.operator];
1398
+ const resolvedOp = arithOp ?? cmpOp;
1399
+ if (resolvedOp === undefined)
1400
+ throw new Error(`Unsupported operator: ${node.operator}`);
1401
+ this.emit(bc, [resolvedOp], node);
1402
+
1403
+ break;
1404
+ }
1405
+
1406
+ case "UpdateExpression": {
1407
+ const res = this._resolve(node.argument.name, this._currentCtx);
1408
+ const bumpOp = node.operator === "++" ? this.OP.ADD : this.OP.SUB;
1409
+ const one = b.constantOperand(1);
1410
+
1411
+ // Helper closures: emit load / store for whichever resolution kind we have
1412
+ const emitLoad = () => {
1413
+ if (res.kind === "local")
1414
+ this.emit(bc, [this.OP.LOAD_LOCAL, res.slot], node);
1415
+ else if (res.kind === "upvalue")
1416
+ this.emit(bc, [this.OP.LOAD_UPVALUE, res.index], node);
1417
+ else
1418
+ this.emit(
1419
+ bc,
1420
+ [this.OP.LOAD_GLOBAL, b.constantOperand(node.argument.name)],
1421
+ node,
1422
+ );
1423
+ };
1424
+ const emitStore = () => {
1425
+ if (res.kind === "local")
1426
+ this.emit(bc, [this.OP.STORE_LOCAL, res.slot], node);
1427
+ else if (res.kind === "upvalue")
1428
+ this.emit(bc, [this.OP.STORE_UPVALUE, res.index], node);
1429
+ else
1430
+ this.emit(
1431
+ bc,
1432
+ [this.OP.STORE_GLOBAL, b.constantOperand(node.argument.name)],
1433
+ node,
1434
+ );
1435
+ };
1436
+
1437
+ emitLoad();
1438
+ if (!node.prefix) this.emit(bc, [this.OP.DUP], node); // post: save old value before mutating
1439
+ this.emit(bc, [this.OP.LOAD_CONST, one], node);
1440
+ this.emit(bc, [bumpOp], node);
1441
+ emitStore();
1442
+ if (node.prefix) emitLoad(); // pre: reload new value as result
1443
+
1444
+ break;
1445
+ }
1446
+
1447
+ case "AssignmentExpression": {
1448
+ const compoundOp = {
1449
+ "+=": this.OP.ADD,
1450
+ "-=": this.OP.SUB,
1451
+ "*=": this.OP.MUL,
1452
+ "/=": this.OP.DIV,
1453
+ "%=": this.OP.MOD,
1454
+ "&=": this.OP.BAND,
1455
+ "|=": this.OP.BOR,
1456
+ "^=": this.OP.BXOR,
1457
+ "<<=": this.OP.SHL,
1458
+ ">>=": this.OP.SHR,
1459
+ ">>>=": this.OP.USHR,
1460
+ }[node.operator];
1461
+
1462
+ const isCompound = compoundOp !== undefined;
1463
+
1464
+ if (node.operator !== "=" && !isCompound) {
1465
+ throw new Error(`Unsupported assignment operator: ${node.operator}`);
1466
+ }
1467
+
1468
+ // Member assignment: obj.x = val or arr[i] = val
1469
+ if (node.left.type === "MemberExpression") {
1470
+ this._compileExpr(node.left.object, scope, bc); // push obj
1471
+
1472
+ if (node.left.computed) {
1473
+ this._compileExpr(node.left.property, scope, bc); // push key (runtime)
1474
+ } else {
1475
+ this.emit(
1476
+ bc,
1477
+ [this.OP.LOAD_CONST, b.constantOperand(node.left.property.name)],
1478
+ node,
1479
+ );
1480
+ }
1481
+
1482
+ if (isCompound) {
1483
+ // Duplicate obj+key on the stack so we can read before we write.
1484
+ // Stack before DUP2: [..., obj, key]
1485
+ // We need: [..., obj, key, obj, key] -> GET_PROP_COMPUTED -> [..., obj, key, currentVal]
1486
+ // Cheapest approach without a DUP opcode: re-compile the member read.
1487
+ // (emits obj + key again; a future peephole pass could DUP instead)
1488
+ this._compileExpr(node.left.object, scope, bc);
1489
+ if (node.left.computed) {
1490
+ this._compileExpr(node.left.property, scope, bc);
1491
+ } else {
1492
+ this.emit(
1493
+ bc,
1494
+ [
1495
+ this.OP.LOAD_CONST,
1496
+ b.constantOperand(node.left.property.name),
1497
+ ],
1498
+ node,
1499
+ );
1500
+ }
1501
+ this.emit(bc, [this.OP.GET_PROP_COMPUTED], node); // [..., obj, key, currentVal]
1502
+ this._compileExpr(node.right, scope, bc); // [..., obj, key, currentVal, rhs]
1503
+ this.emit(bc, [compoundOp], node); // [..., obj, key, newVal]
1504
+ } else {
1505
+ this._compileExpr(node.right, scope, bc); // [..., obj, key, val]
1506
+ }
1507
+
1508
+ this.emit(bc, [this.OP.SET_PROP], node); // obj[key] = val, leaves val on stack
1509
+ break;
1510
+ }
1511
+
1512
+ // Plain identifier assignment
1513
+ const res = this._resolve(node.left.name, this._currentCtx);
1514
+
1515
+ if (isCompound) {
1516
+ // Load the current value of the target first
1517
+ if (res.kind === "local") {
1518
+ this.emit(bc, [this.OP.LOAD_LOCAL, res.slot], node);
1519
+ } else if (res.kind === "upvalue") {
1520
+ this.emit(bc, [this.OP.LOAD_UPVALUE, res.index], node);
1521
+ } else {
1522
+ this.emit(
1523
+ bc,
1524
+ [this.OP.LOAD_GLOBAL, b.constantOperand(node.left.name)],
1525
+ node,
1526
+ );
1527
+ }
1528
+ }
1529
+
1530
+ this._compileExpr(node.right, scope, bc); // push RHS
1531
+
1532
+ if (isCompound) {
1533
+ this.emit(bc, [compoundOp], node); // apply binary op -> leaves newVal on stack
1534
+ }
1535
+
1536
+ // Store & leave value on stack (assignment is an expression)
1537
+ if (res.kind === "local") {
1538
+ this.emit(bc, [this.OP.STORE_LOCAL, res.slot], node);
1539
+ this.emit(bc, [this.OP.LOAD_LOCAL, res.slot], node);
1540
+ } else if (res.kind === "upvalue") {
1541
+ this.emit(bc, [this.OP.STORE_UPVALUE, res.index], node);
1542
+ this.emit(bc, [this.OP.LOAD_UPVALUE, res.index], node);
1543
+ } else {
1544
+ const nameIdx = b.constantOperand(node.left.name);
1545
+ this.emit(bc, [this.OP.STORE_GLOBAL, nameIdx], node);
1546
+ this.emit(bc, [this.OP.LOAD_GLOBAL, nameIdx], node);
1547
+ }
1548
+ break;
1549
+ }
1550
+
1551
+ case "CallExpression": {
1552
+ if (node.callee.type === "MemberExpression") {
1553
+ // ── Method call: console.log(...)
1554
+ // Push receiver first (GET_PROP leaves it; CALL_METHOD pops it as `this`)
1555
+ this._compileExpr(node.callee.object, scope, bc);
1556
+ const prop = node.callee.property.name;
1557
+ const propIdx = b.constantOperand(prop);
1558
+ this.emit(bc, [this.OP.LOAD_CONST, propIdx], node);
1559
+ this.emit(bc, [this.OP.GET_PROP], node);
1560
+ for (const arg of node.arguments) this._compileExpr(arg, scope, bc);
1561
+ this.emit(bc, [this.OP.CALL_METHOD, node.arguments.length], node);
1562
+ } else {
1563
+ // ── Plain call: add(5, 10)
1564
+ this._compileExpr(node.callee, scope, bc);
1565
+ for (const arg of node.arguments) this._compileExpr(arg, scope, bc);
1566
+ this.emit(bc, [this.OP.CALL, node.arguments.length], node);
1567
+ }
1568
+ break;
1569
+ }
1570
+
1571
+ case "UnaryExpression": {
1572
+ // Special case: typeof on a bare identifier must not throw if undeclared.
1573
+ // We emit TYPEOF_SAFE (operand = name constant index) instead of
1574
+ // compiling the argument first. The VM does the guard itself.
1575
+ if (node.operator === "typeof" && node.argument.type === "Identifier") {
1576
+ const res = this._resolve(node.argument.name, this._currentCtx);
1577
+ if (res.kind === "global") {
1578
+ // Potentially undeclared -- let VM guard it
1579
+ this.emit(
1580
+ bc,
1581
+ [this.OP.LOAD_CONST, b.constantOperand(node.argument.name)],
1582
+ node,
1583
+ );
1584
+ this.emit(bc, [this.OP.TYPEOF_SAFE], node);
1585
+ break;
1586
+ }
1587
+ // Known local or upvalue -- safe to load first, then typeof
1588
+ }
1589
+
1590
+ // Special case: delete -- argument must NOT be pre-evaluated.
1591
+ if (node.operator === "delete") {
1592
+ const arg = node.argument;
1593
+ if (arg.type === "MemberExpression") {
1594
+ this._compileExpr(arg.object, scope, bc);
1595
+ if (arg.computed) {
1596
+ this._compileExpr(arg.property, scope, bc);
1597
+ } else {
1598
+ this.emit(
1599
+ bc,
1600
+ [this.OP.LOAD_CONST, b.constantOperand(arg.property.name)],
1601
+ node,
1602
+ );
1603
+ }
1604
+ this.emit(bc, [this.OP.DELETE_PROP], node);
1605
+ } else {
1606
+ // delete x, delete 0, etc. -- always true in non-strict, just push true
1607
+ this.emit(bc, [this.OP.LOAD_CONST, b.constantOperand(true)], node);
1608
+ }
1609
+ break;
1610
+ }
1611
+
1612
+ // All other unary ops: compile argument first, then apply operator
1613
+ this._compileExpr(node.argument, scope, bc);
1614
+ switch (node.operator) {
1615
+ case "-":
1616
+ this.emit(bc, [this.OP.UNARY_NEG], node);
1617
+ break;
1618
+ case "+":
1619
+ this.emit(bc, [this.OP.UNARY_POS], node);
1620
+ break;
1621
+ case "!":
1622
+ this.emit(bc, [this.OP.UNARY_NOT], node);
1623
+ break;
1624
+ case "~":
1625
+ this.emit(bc, [this.OP.UNARY_BITNOT], node);
1626
+ break;
1627
+ case "typeof":
1628
+ this.emit(bc, [this.OP.TYPEOF], node);
1629
+ break;
1630
+ case "void":
1631
+ this.emit(bc, [this.OP.VOID], node);
1632
+ break;
1633
+
1634
+ default:
1635
+ throw new Error(`Unsupported unary operator: ${node.operator}`);
1636
+ }
1637
+ break;
1638
+ }
1639
+
1640
+ case "RegExpLiteral": {
1641
+ // Emit: new RegExp(pattern, flags)
1642
+ // Fresh object per evaluation -- correct for stateful g/y flags.
1643
+ this.emit(bc, [this.OP.LOAD_GLOBAL, b.constantOperand("RegExp")], node);
1644
+ this.emit(
1645
+ bc,
1646
+ [this.OP.LOAD_CONST, b.constantOperand(node.pattern)],
1647
+ node,
1648
+ );
1649
+ this.emit(
1650
+ bc,
1651
+ [this.OP.LOAD_CONST, b.constantOperand(node.flags)],
1652
+ node,
1653
+ );
1654
+ this.emit(bc, [this.OP.NEW, 2], node);
1655
+ break;
1656
+ }
1657
+
1658
+ case "FunctionExpression": {
1659
+ // Compile into a descriptor exactly like a declaration,
1660
+ // but leave the resulting closure ON THE STACK -- no store.
1661
+ // The surrounding expression (assignment, call arg, return) consumes it.
1662
+ const desc = this._compileFunctionDecl(node);
1663
+ this._emitMakeClosure(desc, node, bc);
1664
+ break;
1665
+ }
1666
+
1667
+ case "MemberExpression": {
1668
+ this._compileExpr(node.object, scope, bc);
1669
+ if (node.computed) {
1670
+ // nums[i] -- key is runtime value
1671
+ this._compileExpr(node.property, scope, bc);
1672
+ } else {
1673
+ // point.x -- push key as string, same opcode handles both
1674
+ this.emit(
1675
+ bc,
1676
+ [this.OP.LOAD_CONST, b.constantOperand(node.property.name)],
1677
+ node,
1678
+ );
1679
+ }
1680
+
1681
+ // GET_PROP_COMPUTED pops the object -- correct for value access.
1682
+ // GET_PROP (peek) is only used in CallExpression's method call path
1683
+ // where the receiver must survive on the stack for CALL_METHOD.
1684
+ this.emit(bc, [this.OP.GET_PROP_COMPUTED], node);
1685
+ break;
1686
+ }
1687
+
1688
+ case "ArrayExpression": {
1689
+ // Compile each element left->right, then BUILD_ARRAY collapses them.
1690
+ // Sparse arrays (holes) get explicit undefined per slot.
1691
+ for (const el of node.elements) {
1692
+ if (el === null) {
1693
+ // hole: e.g. [1,,3]
1694
+ this.emit(
1695
+ bc,
1696
+ [this.OP.LOAD_CONST, b.constantOperand(undefined)],
1697
+ node,
1698
+ );
1699
+ } else {
1700
+ this._compileExpr(el, scope, bc);
1701
+ }
1702
+ }
1703
+ this.emit(bc, [this.OP.BUILD_ARRAY, node.elements.length], node);
1704
+ break;
1705
+ }
1706
+ case "ObjectExpression": {
1707
+ // Separate regular data properties from ES5 accessor methods (get/set).
1708
+ const regularProps: t.ObjectProperty[] = [];
1709
+ const accessorProps: t.ObjectMethod[] = [];
1710
+
1711
+ for (const prop of node.properties) {
1712
+ if (prop.type === "SpreadElement") {
1713
+ throw new Error("Object spread not supported");
1714
+ }
1715
+ if (prop.type === "ObjectMethod") {
1716
+ if (prop.kind === "get" || prop.kind === "set") {
1717
+ if (prop.computed) {
1718
+ throw new Error(
1719
+ "Computed getter/setter keys are not supported",
1720
+ );
1721
+ }
1722
+ accessorProps.push(prop);
1723
+ } else {
1724
+ throw new Error(`Shorthand method syntax is not supported`);
1725
+ }
1726
+ } else {
1727
+ regularProps.push(prop as t.ObjectProperty);
1728
+ }
1729
+ }
1730
+
1731
+ // Build the base object from data properties.
1732
+ for (const prop of regularProps) {
1733
+ const key = prop.key;
1734
+ let keyStr: string;
1735
+ if (key.type === "Identifier") {
1736
+ keyStr = key.name;
1737
+ } else if (
1738
+ key.type === "StringLiteral" ||
1739
+ key.type === "NumericLiteral"
1740
+ ) {
1741
+ keyStr = String(key.value);
1742
+ } else {
1743
+ throw new Error(`Unsupported object key type: ${key.type}`);
1744
+ }
1745
+ this.emit(bc, [this.OP.LOAD_CONST, b.constantOperand(keyStr)], node);
1746
+ this._compileExpr(prop.value, scope, bc);
1747
+ }
1748
+ this.emit(bc, [this.OP.BUILD_OBJECT, regularProps.length], node);
1749
+
1750
+ // Define each accessor on the object that is now on top of the stack.
1751
+ // Stack after BUILD_OBJECT: [..., obj]
1752
+ // For each accessor: DUP obj, push key, compile fn, DEFINE_GETTER/DEFINE_SETTER
1753
+ // DEFINE_GETTER/DEFINE_SETTER pops fn+key+obj, leaving the original obj.
1754
+ for (const prop of accessorProps) {
1755
+ const key = prop.key;
1756
+ let keyStr: string;
1757
+ if (key.type === "Identifier") {
1758
+ keyStr = key.name;
1759
+ } else if (
1760
+ key.type === "StringLiteral" ||
1761
+ key.type === "NumericLiteral"
1762
+ ) {
1763
+ keyStr = String(key.value);
1764
+ } else {
1765
+ throw new Error(`Unsupported object key type: ${key.type}`);
1766
+ }
1767
+
1768
+ this.emit(bc, [this.OP.DUP], node); // dup so the original obj stays after the define
1769
+ this.emit(bc, [this.OP.LOAD_CONST, b.constantOperand(keyStr)], node);
1770
+
1771
+ // Compile the accessor body as an anonymous function descriptor.
1772
+ const desc = this._compileFunctionDecl(prop as any);
1773
+ this._emitMakeClosure(desc, prop as any, bc);
1774
+
1775
+ this.emit(
1776
+ bc,
1777
+ [
1778
+ prop.kind === "get"
1779
+ ? this.OP.DEFINE_GETTER
1780
+ : this.OP.DEFINE_SETTER,
1781
+ ],
1782
+ node,
1783
+ );
1784
+ }
1785
+
1786
+ break;
1787
+ }
1788
+
1789
+ default: {
1790
+ throw new Error(`Unsupported expression: ${node.type}`);
1791
+ }
1792
+ }
1793
+ }
1794
+ }
1795
+
1796
+ // Serializer
1797
+ // Turns the compiled output into a commented JS source string.
1798
+ // Expects fully-resolved bytecode (all label refs and constant refs already
1799
+ // converted to plain integers by resolveLabels + resolveConstants passes).
1800
+ class Serializer {
1801
+ compiler: Compiler;
1802
+
1803
+ constructor(compiler: Compiler) {
1804
+ this.compiler = compiler;
1805
+ }
1806
+
1807
+ get options() {
1808
+ return this.compiler.options;
1809
+ }
1810
+
1811
+ get OP() {
1812
+ return this.compiler.OP;
1813
+ }
1814
+
1815
+ get OP_NAME() {
1816
+ return this.compiler.OP_NAME;
1817
+ }
1818
+
1819
+ get JUMP_OPS() {
1820
+ return this.compiler.JUMP_OPS;
1821
+ }
1822
+
1823
+ // Produce a JS literal for a constant pool entry
1824
+ _serializeConst(val) {
1825
+ if (val === null) return "null";
1826
+ if (val === undefined) return "undefined";
1827
+ return JSON.stringify(val); // number / string / bool
1828
+ }
1829
+
1830
+ // One instruction -> "[op, op1, op2, ...] // MNEMONIC description"
1831
+ // Expects a fully-resolved instruction: all operands are plain numbers.
1832
+ // Returns { text, values } where values is the flat u16 slots for this
1833
+ // instruction (opcode first, then one entry per operand).
1834
+ _serializeInstr(
1835
+ instr: b.Instruction,
1836
+ constants: any[],
1837
+ ): { text: string; values: number[] } {
1838
+ const op = instr[0] as number;
1839
+ const operands = instr.slice(1) as number[];
1840
+
1841
+ const resolvedOperands = operands
1842
+ .filter((operand) => (operand as any)?.placeholder !== true)
1843
+ .map((o) => (o as any)?.resolvedValue ?? o);
1844
+
1845
+ for (const o of resolvedOperands) {
1846
+ ok(typeof o === "number", "Unresolved operand: " + JSON.stringify(o));
1847
+ ok(o >= 0 && o <= 0xffff, `Operand overflow (max 0xFFFF u16): ${o}`);
1848
+ }
1849
+ ok(op >= 0 && op <= 0xffff, `Opcode overflow (max 0xFFFF u16): ${op}`);
1850
+
1851
+ const operand = resolvedOperands[0]; // first operand for single-operand comment cases
1852
+ const name = this.OP_NAME[op] || `OP_${op}`;
1853
+ let comment = name;
1854
+
1855
+ const sourceNode = instr[SOURCE_NODE_SYM];
1856
+ const sourceLocation = sourceNode
1857
+ ? sourceNode.loc.start?.line +
1858
+ ":" +
1859
+ sourceNode.loc.start?.column +
1860
+ "-" +
1861
+ (sourceNode.loc.end?.line + ":" + sourceNode.loc.end?.column)
1862
+ : "";
1863
+
1864
+ // Annotate with human-readable operand meaning
1865
+ if (resolvedOperands.length > 0) {
1866
+ switch (op) {
1867
+ case this.OP.LOAD_CONST: {
1868
+ const val = constants[operand];
1869
+ comment += ` ${this._serializeConst(val)}`;
1870
+ break;
1871
+ }
1872
+ case this.OP.MAKE_CLOSURE: {
1873
+ comment += ` PC ${operand} (params=${resolvedOperands[1]} locals=${resolvedOperands[2]} upvalues=${resolvedOperands[3]})`;
1874
+ break;
1875
+ }
1876
+ case this.OP.LOAD_LOCAL:
1877
+ case this.OP.STORE_LOCAL:
1878
+ comment += ` slot[${operand}]`;
1879
+ break;
1880
+ case this.OP.LOAD_UPVALUE:
1881
+ case this.OP.STORE_UPVALUE:
1882
+ comment += ` upvalue[${operand}]`;
1883
+ break;
1884
+ case this.OP.LOAD_GLOBAL:
1885
+ case this.OP.STORE_GLOBAL:
1886
+ comment += ` "${constants[operand]}"`;
1887
+ break;
1888
+ case this.OP.CALL:
1889
+ case this.OP.CALL_METHOD:
1890
+ comment += ` (${operand} args)`;
1891
+ break;
1892
+ case this.OP.BUILD_ARRAY:
1893
+ comment += ` (${operand} elements)`;
1894
+ break;
1895
+ case this.OP.BUILD_OBJECT:
1896
+ comment += ` (${operand} pairs)`;
1897
+ break;
1898
+ case this.OP.NEW:
1899
+ comment += ` (${operand} args)`;
1900
+ break;
1901
+ default:
1902
+ comment +=
1903
+ resolvedOperands.length === 1
1904
+ ? ` ${operand}`
1905
+ : ` [${resolvedOperands.join(", ")}]`;
1906
+ }
1907
+ }
1908
+
1909
+ comment = comment.padEnd(40) + sourceLocation;
1910
+
1911
+ const values = [op, ...resolvedOperands];
1912
+ const instrText = `[${values.join(", ")}]`;
1913
+ const text = `${(instrText + ",").padEnd(12)} ${comment}`;
1914
+
1915
+ return { text, values };
1916
+ }
1917
+
1918
+ // Serialize the CONSTANTS array
1919
+ _serializeConstants(constants: any[]) {
1920
+ const lines = ["var CONSTANTS = ["];
1921
+ constants.forEach((val, idx) => {
1922
+ lines.push(` /* ${idx} */ ${this._serializeConst(val)},`);
1923
+ });
1924
+ lines.push("];");
1925
+ return lines.join("\n");
1926
+ }
1927
+
1928
+ // Filter out any remaining null-opcode pseudo-instructions.
1929
+ // (defineLabel pseudo-ops are already stripped by resolveLabels.)
1930
+ _serializeBytecode(
1931
+ bytecode: b.Bytecode,
1932
+ compiler: Compiler,
1933
+ ): { bytecode: b.Bytecode } {
1934
+ const serialized = [];
1935
+ for (const instr of bytecode) {
1936
+ if (instr[0] === null) continue;
1937
+
1938
+ const specializedOpInfo = compiler.SPECIALIZED_OPS[instr[0]];
1939
+ if (specializedOpInfo) {
1940
+ const resolvedValue = (instr[1] as any)?.resolvedValue ?? instr[1];
1941
+ const originalName = compiler.OP_NAME[specializedOpInfo.originalOp];
1942
+
1943
+ compiler.OP_NAME[instr[0]] = `${originalName}_${resolvedValue}`;
1944
+ specializedOpInfo.resolvedOperand = instr[1];
1945
+ }
1946
+
1947
+ serialized.push(instr);
1948
+ }
1949
+
1950
+ return {
1951
+ bytecode: serialized,
1952
+ };
1953
+ }
1954
+
1955
+ _encodeBytecode(flat: number[]) {
1956
+ // Encode as little-endian Uint16Array -> base64.
1957
+ const buf = new Uint8Array(flat.length * 2);
1958
+ flat.forEach((w, i) => {
1959
+ buf[i * 2] = w & 0xff;
1960
+ buf[i * 2 + 1] = (w >>> 8) & 0xff;
1961
+ });
1962
+ return Buffer.from(buf).toString("base64");
1963
+ }
1964
+
1965
+ serialize(bytecode: b.Bytecode, constants: any[], compiler: Compiler) {
1966
+ const mainStartPc = compiler.mainStartPc;
1967
+ let sections = [];
1968
+
1969
+ var textForm = [];
1970
+ var initBody = [];
1971
+
1972
+ var bytecodeResult = this._serializeBytecode(bytecode, compiler);
1973
+
1974
+ for (const instr of bytecodeResult.bytecode) {
1975
+ const serialized = this._serializeInstr(instr, constants);
1976
+ textForm.push(serialized.text);
1977
+ }
1978
+
1979
+ initBody.push(textForm.map((line) => `// ${line}`).join("\n"));
1980
+
1981
+ const flat = bytecodeResult.bytecode.flatMap((instr) => {
1982
+ let filtered = instr.filter((x) => (x as any)?.placeholder !== true);
1983
+ let resolved = filtered.map((x) => (x as any)?.resolvedValue ?? x);
1984
+
1985
+ return resolved as number[];
1986
+ });
1987
+
1988
+ if (this.options.encodeBytecode) {
1989
+ sections.push(`var BYTECODE = "${this._encodeBytecode(flat)}";`);
1990
+ } else {
1991
+ // Flatten each [op, ...operands] instruction into individual u16 slots.
1992
+
1993
+ sections.push(`var BYTECODE = [${flat.join(",")}]`);
1994
+ }
1995
+
1996
+ // MAIN_START_PC
1997
+ sections.push(`var MAIN_START_PC = ${mainStartPc};`);
1998
+ sections.push(`var ENCODE_BYTECODE = ${!!this.options.encodeBytecode};`);
1999
+ sections.push(`var TIMING_CHECKS = ${!!this.options.timingChecks};`);
2000
+ // Opcodes
2001
+ const object = t.objectExpression(
2002
+ Object.entries(this.OP).map(([name, value]) =>
2003
+ t.objectProperty(t.identifier(name), t.numericLiteral(value)),
2004
+ ),
2005
+ );
2006
+ sections.push(`var OP = ${generate(object).code};`);
2007
+
2008
+ // Constants must be defined before the bytecode
2009
+ initBody.push(this._serializeConstants(constants));
2010
+
2011
+ sections = [...initBody, ...sections];
2012
+
2013
+ // VM runtime
2014
+ sections.push(VM_RUNTIME);
2015
+
2016
+ return sections.join("\n\n");
2017
+ }
2018
+ }
2019
+
2020
+ export async function compileAndSerialize(
2021
+ sourceCode: string,
2022
+ options: Options,
2023
+ ) {
2024
+ const compiler = new Compiler(options);
2025
+ let bytecode = compiler.compile(sourceCode);
2026
+
2027
+ // User transform passes (operate on unresolved IR with label/constant refs)
2028
+ // macroOpcodes must run after selfModifying (so PATCH-stub bodies are in place)
2029
+ const passes = [];
2030
+
2031
+ // Due to current implementation, specialized must run BEFORE macroOpcodes
2032
+ if (options.specializedOpcodes) {
2033
+ passes.push(specializedOpcodes);
2034
+ }
2035
+
2036
+ if (options.macroOpcodes) {
2037
+ passes.push(macroOpcodes);
2038
+ }
2039
+
2040
+ if (options.selfModifying) {
2041
+ passes.push(selfModifying);
2042
+ }
2043
+
2044
+ for (const pass of passes) {
2045
+ const passResult = pass(bytecode, compiler);
2046
+ bytecode = passResult.bytecode;
2047
+ }
2048
+
2049
+ // Assembler phases: resolve IR operands to plain integers before printing
2050
+ const { bytecode: labelResolved } = resolveLabels(bytecode, compiler);
2051
+ let { bytecode: finalBytecode, constants } = resolveConstants(labelResolved);
2052
+
2053
+ const output = compiler.serializer.serialize(
2054
+ finalBytecode,
2055
+ constants,
2056
+ compiler,
2057
+ );
2058
+
2059
+ const finalOutput = await obfuscateRuntime(
2060
+ output,
2061
+ finalBytecode,
2062
+ options,
2063
+ compiler,
2064
+ );
2065
+
2066
+ return {
2067
+ code: finalOutput,
2068
+ };
2069
+ }