js-confuser-vm 0.0.1 → 0.0.3

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