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,755 @@
1
+ import { OP_ORIGINAL as OP } from "./compiler.js";
2
+ const BYTECODE = [];
3
+ const MAIN_START_PC = 0;
4
+ const CONSTANTS = [];
5
+ const ENCODE_BYTECODE = false;
6
+ const TIMING_CHECKS = false;
7
+ // The text above is not included in the compiled output - for type intellisense only
8
+ // @START
9
+
10
+ function decodeBytecode(s) {
11
+ if (!ENCODE_BYTECODE) return s;
12
+ var b = typeof Buffer !== "undefined" ? Buffer.from(s, "base64") : Uint8Array.from(atob(s), function (c) {
13
+ return c.charCodeAt(0);
14
+ });
15
+ var r = new Int32Array(b.length / 4);
16
+ for (var i = 0; i < r.length; i++) r[i] = b[i * 4] | b[i * 4 + 1] << 8 | b[i * 4 + 2] << 16 | b[i * 4 + 3] << 24;
17
+ return r;
18
+ }
19
+
20
+ // Closure symbol
21
+ // Used to tag shell functions so the VM can fast-path back to the
22
+ // inner Closure instead of going through a sub-VM on internal calls.
23
+ var CLOSURE_SYM = Symbol(); // Nameless for obfuscation
24
+
25
+ // Upvalue
26
+ // While the outer frame is alive: reads/writes go to frame.locals[slot].
27
+ // After the outer frame returns (closed): reads/writes hit this.value.
28
+ function Upvalue(frame, slot) {
29
+ this._frame = frame;
30
+ this._slot = slot;
31
+ this._closed = false;
32
+ this._value = undefined;
33
+ }
34
+ Upvalue.prototype._read = function () {
35
+ return this._closed ? this._value : this._frame.locals[this._slot];
36
+ };
37
+ Upvalue.prototype._write = function (v) {
38
+ if (this._closed) this._value = v;else this._frame.locals[this._slot] = v;
39
+ };
40
+ Upvalue.prototype._close = function () {
41
+ this._value = this._frame.locals[this._slot];
42
+ this._closed = true;
43
+ };
44
+
45
+ // Closure & Frame
46
+ function Closure(fn) {
47
+ this.fn = fn;
48
+ this.upvalues = [];
49
+ this.prototype = {}; // <- default prototype object for \`new\`
50
+ }
51
+ function Frame(closure, returnPc, parent, thisVal) {
52
+ this.closure = closure;
53
+ this.locals = new Array(closure.fn.localCount).fill(undefined);
54
+ this._pc = closure.fn.startPc; // <- initialize from fn descriptor
55
+ this._returnPc = returnPc; // pc to resume in parent frame after RETURN
56
+ this._parent = parent;
57
+ this.thisVal = thisVal !== undefined ? thisVal : undefined;
58
+ this._newObj = null; // <- set by NEW so RETURN can see it
59
+ this._handlerStack = []; // <- exception handlers pushed by TRY_SETUP
60
+ }
61
+
62
+ // VM
63
+ function VM(bytecode, mainStartPc, constants, globals) {
64
+ this.bytecode = bytecode;
65
+ this.constants = constants;
66
+ this.globals = globals;
67
+ this._stack = [];
68
+ this._frameStack = [];
69
+ this._openUpvalues = []; // all currently open Upvalue objects across all frames
70
+
71
+ var mainFn = {
72
+ paramCount: 0,
73
+ localCount: 0,
74
+ startPc: mainStartPc // <- where main begins
75
+ };
76
+ this._currentFrame = new Frame(new Closure(mainFn), null, null);
77
+ }
78
+ VM.prototype._push = function (v) {
79
+ this._stack.push(v);
80
+ };
81
+ VM.prototype._pop = function () {
82
+ return this._stack.pop();
83
+ };
84
+ VM.prototype.peek = function () {
85
+ return this._stack[this._stack.length - 1];
86
+ };
87
+
88
+ // Read one instruction word from this.bytecode at `pc`, unwrapping the
89
+ // encoding so callers always get a plain { op, operand } pair regardless
90
+ // of whether ENCODE_BYTECODE is active.
91
+ VM.prototype.readWord = function (pc) {
92
+ var word = this.bytecode[pc];
93
+ if (ENCODE_BYTECODE) {
94
+ return {
95
+ op: word & 0xff,
96
+ operand: word >>> 8
97
+ };
98
+ } else {
99
+ return {
100
+ op: word[0],
101
+ operand: word[1]
102
+ };
103
+ }
104
+ };
105
+ VM.prototype.captureUpvalue = function (frame, slot) {
106
+ // Reuse existing open upvalue for this frame+slot if one exists.
107
+ // This is what makes two closures share the same mutable cell.
108
+ for (var i = 0; i < this._openUpvalues.length; i++) {
109
+ var uv = this._openUpvalues[i];
110
+ if (uv._frame === frame && uv._slot === slot) return uv;
111
+ }
112
+ var uv = new Upvalue(frame, slot);
113
+ this._openUpvalues.push(uv);
114
+ return uv;
115
+ };
116
+ VM.prototype._closeUpvaluesFor = function (frame) {
117
+ // Called on RETURN - close every upvalue that was pointing into this frame.
118
+ // After this, closures that captured from the frame read from upvalue.value.
119
+ this._openUpvalues = this._openUpvalues.filter(function (uv) {
120
+ if (uv._frame === frame) {
121
+ uv._close();
122
+ return false;
123
+ }
124
+ return true;
125
+ });
126
+ };
127
+ VM.prototype.run = function () {
128
+ var now = () => {
129
+ return performance.now();
130
+ };
131
+ var t = now();
132
+ while (true) {
133
+ var frame = this._currentFrame;
134
+ var bc = this.bytecode;
135
+ if (frame._pc >= bc.length) break;
136
+ var op, operand;
137
+ var word = this.readWord(frame._pc++);
138
+ op = word.op;
139
+ operand = word.operand;
140
+
141
+ // console.log(frame._pc - 1, op, operand);
142
+
143
+ // Debugging protection
144
+ if (TIMING_CHECKS) {
145
+ var t2 = now();
146
+ var isTamper = t2 - t > 1000;
147
+ t = t2;
148
+ if (isTamper) {
149
+ op = OP.POP;
150
+ }
151
+ }
152
+ try {
153
+ /* @SWITCH */
154
+ switch (op) {
155
+ case OP.LOAD_CONST:
156
+ this._push(this.constants[operand]);
157
+ break;
158
+ case OP.LOAD_INT:
159
+ this._push(operand);
160
+ break;
161
+ case OP.LOAD_LOCAL:
162
+ this._push(frame.locals[operand]);
163
+ break;
164
+ case OP.STORE_LOCAL:
165
+ frame.locals[operand] = this._pop();
166
+ break;
167
+ case OP.LOAD_GLOBAL:
168
+ this._push(this.globals[this.constants[operand]]);
169
+ break;
170
+ case OP.STORE_GLOBAL:
171
+ this.globals[this.constants[operand]] = this._pop();
172
+ break;
173
+ case OP.GET_PROP:
174
+ {
175
+ // Stack: [..., obj, key] -> [..., obj, obj[key]]
176
+ // obj is PEEKED (not popped) - CALL_METHOD needs it as receiver
177
+ var key = this._pop();
178
+ var obj = this.peek();
179
+ this._push(obj[key]);
180
+ break;
181
+ }
182
+ case OP.ADD:
183
+ {
184
+ var b = this._pop();
185
+ this._push(this._pop() + b);
186
+ break;
187
+ }
188
+ case OP.SUB:
189
+ {
190
+ var b = this._pop();
191
+ this._push(this._pop() - b);
192
+ break;
193
+ }
194
+ case OP.MUL:
195
+ {
196
+ var b = this._pop();
197
+ this._push(this._pop() * b);
198
+ break;
199
+ }
200
+ case OP.DIV:
201
+ {
202
+ var b = this._pop();
203
+ this._push(this._pop() / b);
204
+ break;
205
+ }
206
+ case OP.MOD:
207
+ {
208
+ var b = this._pop();
209
+ this._push(this._pop() % b);
210
+ break;
211
+ }
212
+ case OP.BAND:
213
+ {
214
+ var b = this._pop();
215
+ this._push(this._pop() & b);
216
+ break;
217
+ }
218
+ case OP.BOR:
219
+ {
220
+ var b = this._pop();
221
+ this._push(this._pop() | b);
222
+ break;
223
+ }
224
+ case OP.BXOR:
225
+ {
226
+ var b = this._pop();
227
+ this._push(this._pop() ^ b);
228
+ break;
229
+ }
230
+ case OP.SHL:
231
+ {
232
+ var b = this._pop();
233
+ this._push(this._pop() << b);
234
+ break;
235
+ }
236
+ case OP.SHR:
237
+ {
238
+ var b = this._pop();
239
+ this._push(this._pop() >> b);
240
+ break;
241
+ }
242
+ case OP.USHR:
243
+ {
244
+ var b = this._pop();
245
+ this._push(this._pop() >>> b);
246
+ break;
247
+ }
248
+ case OP.LT:
249
+ {
250
+ var b = this._pop();
251
+ this._push(this._pop() < b);
252
+ break;
253
+ }
254
+ case OP.GT:
255
+ {
256
+ var b = this._pop();
257
+ this._push(this._pop() > b);
258
+ break;
259
+ }
260
+ case OP.EQ:
261
+ {
262
+ var b = this._pop();
263
+ this._push(this._pop() === b);
264
+ break;
265
+ }
266
+ case OP.LTE:
267
+ {
268
+ var b = this._pop();
269
+ this._push(this._pop() <= b);
270
+ break;
271
+ }
272
+ case OP.GTE:
273
+ {
274
+ var b = this._pop();
275
+ this._push(this._pop() >= b);
276
+ break;
277
+ }
278
+ case OP.NEQ:
279
+ {
280
+ var b = this._pop();
281
+ this._push(this._pop() !== b);
282
+ break;
283
+ }
284
+ case OP.LOOSE_EQ:
285
+ {
286
+ var b = this._pop();
287
+ this._push(this._pop() == b);
288
+ break;
289
+ }
290
+ case OP.LOOSE_NEQ:
291
+ {
292
+ var b = this._pop();
293
+ this._push(this._pop() != b);
294
+ break;
295
+ }
296
+ case OP.IN:
297
+ {
298
+ var b = this._pop();
299
+ this._push(this._pop() in b);
300
+ break;
301
+ }
302
+ case OP.INSTANCEOF:
303
+ {
304
+ var ctor = this._pop();
305
+ var obj = this._pop();
306
+ if (typeof ctor === "function") {
307
+ // Native constructor (e.g. Array, Date) - native instanceof is fine
308
+ this._push(obj instanceof ctor);
309
+ } else {
310
+ // VM Closure - ctor.prototype was set by MAKE_CLOSURE / user assignment.
311
+ // Walk obj's prototype chain looking for identity with ctor.prototype.
312
+ var proto = ctor.prototype; // the .prototype property on the Closure
313
+ var target = Object.getPrototypeOf(obj);
314
+ var result = false;
315
+ while (target !== null) {
316
+ if (target === proto) {
317
+ result = true;
318
+ break;
319
+ }
320
+ target = Object.getPrototypeOf(target);
321
+ }
322
+ this._push(result);
323
+ }
324
+ break;
325
+ }
326
+ case OP.UNARY_NEG:
327
+ this._push(-this._pop());
328
+ break;
329
+ case OP.UNARY_POS:
330
+ this._push(this._pop());
331
+ break;
332
+ case OP.UNARY_NOT:
333
+ this._push(!this._pop());
334
+ break;
335
+ case OP.UNARY_BITNOT:
336
+ this._push(~this._pop());
337
+ break;
338
+ case OP.TYPEOF:
339
+ this._push(typeof this._pop());
340
+ break;
341
+ case OP.VOID:
342
+ this._pop();
343
+ this._push(undefined);
344
+ break;
345
+ case OP.TYPEOF_SAFE:
346
+ {
347
+ // operand is a const index holding the variable name string.
348
+ // Mimics JS semantics: typeof undeclaredVar === "undefined" (no throw).
349
+ var name = this._pop(); // LOAD_CONST pushed the name - consume it
350
+ var val = Object.prototype.hasOwnProperty.call(this.globals, name) ? this.globals[name] : undefined;
351
+ this._push(typeof val);
352
+ break;
353
+ }
354
+ case OP.JUMP:
355
+ frame._pc = operand;
356
+ break;
357
+ case OP.JUMP_IF_FALSE:
358
+ if (!this._pop()) frame._pc = operand;
359
+ break;
360
+ case OP.JUMP_IF_TRUE_OR_POP:
361
+ // || semantics: if truthy, we're done - leave value, jump over RHS.
362
+ // If falsy, discard it and fall through to evaluate RHS.
363
+ if (this.peek()) {
364
+ frame._pc = operand;
365
+ } else {
366
+ this._pop();
367
+ }
368
+ break;
369
+ case OP.JUMP_IF_FALSE_OR_POP:
370
+ // && semantics: if falsy, we're done - leave value, jump over RHS.
371
+ // If truthy, discard it and fall through to evaluate RHS.
372
+ if (!this.peek()) {
373
+ frame._pc = operand;
374
+ } else {
375
+ this._pop();
376
+ }
377
+ break;
378
+ case OP.MAKE_CLOSURE:
379
+ {
380
+ // operand = startPc: absolute index of the function body's first instruction.
381
+ // Metadata is read from the value stack (pushed by _emitClosureMetadata).
382
+ // Stack layout when we arrive here (top is rightmost):
383
+ // [isLocal_0, idx_0, ..., isLocal_N-1, idx_N-1, uvCount, localCount, paramCount]
384
+ var startPc = operand;
385
+ var paramCount = this._pop();
386
+ var localCount = this._pop();
387
+ var uvCount = this._pop();
388
+
389
+ // Upvalues were pushed in order 0..N-1 so we pop them in reverse.
390
+ var uvDescs = new Array(uvCount);
391
+ for (var i = uvCount - 1; i >= 0; i--) {
392
+ var uvIndex = this._pop();
393
+ var isLocalRaw = this._pop();
394
+ uvDescs[i] = {
395
+ isLocal: isLocalRaw,
396
+ _index: uvIndex
397
+ };
398
+ }
399
+ var fn = {
400
+ paramCount: paramCount,
401
+ localCount: localCount,
402
+ startPc: startPc,
403
+ upvalueDescriptors: uvDescs
404
+ };
405
+ var closure = new Closure(fn);
406
+ for (var i = 0; i < uvDescs.length; i++) {
407
+ var uvd = uvDescs[i];
408
+ if (uvd.isLocal) {
409
+ // Capture directly from current frame's local slot
410
+ closure.upvalues.push(this.captureUpvalue(frame, uvd._index));
411
+ } else {
412
+ // Relay - take upvalue from the enclosing closure's list
413
+ closure.upvalues.push(frame.closure.upvalues[uvd._index]);
414
+ }
415
+ }
416
+ // Wrap in a native callable shell so host code (array methods,
417
+ // test assertions, setTimeout, etc.) can invoke VM closures.
418
+ // CLOSURE_SYM lets VM-internal CALL/NEW bypass the sub-VM entirely.
419
+ var self = this;
420
+ var shell = function (c) {
421
+ return function () {
422
+ var args = Array.prototype.slice.call(arguments);
423
+ var sub = new VM(self.bytecode, 0, self.constants, self.globals);
424
+ // Sloppy-mode: null/undefined thisArg → global object
425
+ var f = new Frame(c, null, null, this == null ? self.globals : this);
426
+ for (var i = 0; i < args.length; i++) f.locals[i] = args[i];
427
+ f.locals[c.fn.paramCount] = args;
428
+ sub._currentFrame = f;
429
+ return sub.run();
430
+ };
431
+ }(closure);
432
+ shell[CLOSURE_SYM] = closure;
433
+ shell.prototype = closure.prototype; // unified prototype for new/instanceof
434
+ this._push(shell);
435
+ break;
436
+ }
437
+ case OP.DATA:
438
+ // Should never appear in compiled output (reserved opcode slot).
439
+ throw new Error("DATA opcode executed at pc " + (frame._pc - 1));
440
+ case OP.LOAD_UPVALUE:
441
+ this._push(frame.closure.upvalues[operand]._read());
442
+ break;
443
+ case OP.STORE_UPVALUE:
444
+ frame.closure.upvalues[operand]._write(this._pop());
445
+ break;
446
+ case OP.BUILD_ARRAY:
447
+ {
448
+ // Pop \`operand\` values off the stack in reverse, assemble array.
449
+ var elems = this._stack.splice(this._stack.length - operand);
450
+ this._push(elems);
451
+ break;
452
+ }
453
+ case OP.BUILD_OBJECT:
454
+ {
455
+ // Stack has: key0, val0, key1, val1 ... keyN, valN (pushed left->right)
456
+ // Pop all pairs and build the object.
457
+ var pairs = this._stack.splice(this._stack.length - operand * 2);
458
+ var o = {};
459
+ for (var i = 0; i < pairs.length; i += 2) {
460
+ o[pairs[i]] = pairs[i + 1]; // key at even index, val at odd
461
+ }
462
+ this._push(o);
463
+ break;
464
+ }
465
+ case OP.SET_PROP:
466
+ {
467
+ // Stack: [..., obj, key, val]
468
+ // Leaves val on stack - assignment is an expression in JS.
469
+ var val = this._pop();
470
+ var key = this._pop();
471
+ var obj = this._pop();
472
+ // Reflect.set performs [[Set]] without throwing on failure,
473
+ // correctly simulating sloppy-mode assignment from a strict-mode host
474
+ // (output.js is an ES module). This also properly invokes inherited
475
+ // or prototype-chain setter functions.
476
+ Reflect.set(obj, key, val);
477
+ this._push(val); // assignment expression evaluates to the assigned value
478
+ break;
479
+ }
480
+ case OP.GET_PROP_COMPUTED:
481
+ {
482
+ // Stack: [..., obj, key] - key is a runtime value (nums[i])
483
+ // Mirrors GET_PROP but pops the key that was pushed dynamically.
484
+ var key = this._pop();
485
+ var obj = this._pop();
486
+ this._push(obj[key]);
487
+ break;
488
+ }
489
+ case OP.DELETE_PROP:
490
+ {
491
+ var key = this._pop();
492
+ var obj = this._pop();
493
+ this._push(delete obj[key]);
494
+ break;
495
+ }
496
+ case OP.CALL:
497
+ {
498
+ var args = this._stack.splice(this._stack.length - operand);
499
+ var callee = this._pop();
500
+ if (callee && callee[CLOSURE_SYM]) {
501
+ // VM closure - run directly in this VM, no sub-VM overhead
502
+ var c = callee[CLOSURE_SYM];
503
+ // Sloppy-mode: plain function call → global object as this
504
+ var f = new Frame(c, frame._pc, frame, this.globals);
505
+ for (var i = 0; i < args.length; i++) f.locals[i] = args[i];
506
+ f.locals[c.fn.paramCount] = args;
507
+ this._frameStack.push(this._currentFrame);
508
+ this._currentFrame = f;
509
+ } else {
510
+ // Native function
511
+ this._push(callee.apply(null, args));
512
+ }
513
+ break;
514
+ }
515
+ case OP.CALL_METHOD:
516
+ {
517
+ var args = this._stack.splice(this._stack.length - operand);
518
+ var callee = this._pop();
519
+ var receiver = this._pop(); // left on stack by GET_PROP
520
+ if (callee && callee[CLOSURE_SYM]) {
521
+ // VM closure - run directly in this VM with receiver as this
522
+ var c = callee[CLOSURE_SYM];
523
+ var f = new Frame(c, frame._pc, frame, receiver);
524
+ for (var i = 0; i < args.length; i++) f.locals[i] = args[i];
525
+ f.locals[c.fn.paramCount] = args;
526
+ this._frameStack.push(this._currentFrame);
527
+ this._currentFrame = f;
528
+ } else {
529
+ // Native method
530
+ this._push(callee.apply(receiver, args));
531
+ }
532
+ break;
533
+ }
534
+ case OP.LOAD_THIS:
535
+ this._push(frame.thisVal);
536
+ break;
537
+ case OP.NEW:
538
+ {
539
+ var args = this._stack.splice(this._stack.length - operand);
540
+ var callee = this._pop();
541
+ if (callee && callee[CLOSURE_SYM]) {
542
+ // VM closure constructor - prototype is unified via shell.prototype = closure.prototype
543
+ var c = callee[CLOSURE_SYM];
544
+ var newObj = Object.create(c.prototype || null);
545
+ var f = new Frame(c, frame._pc, frame, newObj);
546
+ f._newObj = newObj;
547
+ for (var i = 0; i < args.length; i++) f.locals[i] = args[i];
548
+ f.locals[c.fn.paramCount] = args;
549
+ this._frameStack.push(this._currentFrame);
550
+ this._currentFrame = f;
551
+ } else {
552
+ // Native constructor (e.g. new Error(), new Date()).
553
+ // Reflect.construct is required - Object.create+apply does NOT set
554
+ // internal slots ([[NumberData]], [[StringData]], etc.) for built-ins.
555
+ this._push(Reflect.construct(callee, args));
556
+ }
557
+ break;
558
+ }
559
+ case OP.RETURN:
560
+ {
561
+ var retVal = this._pop();
562
+ this._closeUpvaluesFor(frame); // must happen before frame is abandoned
563
+ if (this._frameStack.length === 0) return retVal;
564
+
565
+ // new-call rule: primitive return -> discard, use the constructed object instead
566
+ if (frame._newObj !== null) {
567
+ if (typeof retVal !== "object" || retVal === null) retVal = frame._newObj;
568
+ }
569
+ this._currentFrame = this._frameStack.pop();
570
+ this._push(retVal);
571
+ break;
572
+ }
573
+ case OP.POP:
574
+ this._pop();
575
+ break;
576
+ case OP.DUP:
577
+ this._push(this.peek());
578
+ break;
579
+ case OP.THROW:
580
+ throw this._pop();
581
+ case OP.FOR_IN_SETUP:
582
+ {
583
+ // Pop the object; build an ordered list of all enumerable own+inherited
584
+ // string keys by walking the prototype chain manually.
585
+ // Uses getOwnPropertyNames (includes non-enumerable) + descriptor check,
586
+ // so we never rely on Object.keys() and we handle inheritance correctly.
587
+ var obj = this._pop();
588
+ var keys = [];
589
+ if (obj !== null && obj !== undefined) {
590
+ var seen = Object.create(null);
591
+ var cur = Object(obj); // box primitives
592
+ while (cur !== null) {
593
+ var ownNames = Object.getOwnPropertyNames(cur);
594
+ for (var i = 0; i < ownNames.length; i++) {
595
+ var k = ownNames[i];
596
+ if (!(k in seen)) {
597
+ seen[k] = true;
598
+ var propDesc = Object.getOwnPropertyDescriptor(cur, k);
599
+ if (propDesc && propDesc.enumerable) {
600
+ keys.push(k);
601
+ }
602
+ }
603
+ }
604
+ cur = Object.getPrototypeOf(cur);
605
+ }
606
+ }
607
+ this._push({
608
+ _keys: keys,
609
+ i: 0
610
+ });
611
+ break;
612
+ }
613
+ case OP.FOR_IN_NEXT:
614
+ {
615
+ // operand = jump target for the done case.
616
+ // Pop the iterator; if exhausted jump to exit, otherwise push next key.
617
+ var iter = this._pop();
618
+ if (iter.i >= iter._keys.length) {
619
+ frame._pc = operand;
620
+ } else {
621
+ this._push(iter._keys[iter.i++]);
622
+ }
623
+ break;
624
+ }
625
+ case OP.PATCH:
626
+ {
627
+ // Writes at operand the bytecode[arg1:arg2]
628
+ var destPc = operand;
629
+ var instructions = this.bytecode.slice(this._pop(), this._pop());
630
+ for (var i = 0; i < instructions.length; i++) {
631
+ this.bytecode[destPc + i] = instructions[i];
632
+ }
633
+ break;
634
+ }
635
+ case OP.TRY_SETUP:
636
+ {
637
+ // Push an exception handler record onto the current frame.
638
+ // Saves: catch PC (operand), current stack depth, current frame-stack depth.
639
+ // If an exception is thrown before TRY_END fires, the VM jumps here.
640
+ frame._handlerStack.push({
641
+ handlerPc: operand,
642
+ stackDepth: this._stack.length,
643
+ frameStackDepth: this._frameStack.length
644
+ });
645
+ break;
646
+ }
647
+ case OP.TRY_END:
648
+ {
649
+ // Normal exit from a try block — disarm the exception handler.
650
+ frame._handlerStack.pop();
651
+ break;
652
+ }
653
+ case OP.DEFINE_GETTER:
654
+ {
655
+ // Stack: [..., obj, key, getterFn]
656
+ // Pops all three; defines an enumerable, configurable getter on obj.
657
+ // If a setter was already defined for this key, it is preserved.
658
+ var getterFn = this._pop();
659
+ var key = this._pop();
660
+ var obj = this._pop();
661
+ var existingDesc = Object.getOwnPropertyDescriptor(obj, key);
662
+ var getDesc = {
663
+ get: getterFn,
664
+ configurable: true,
665
+ enumerable: true
666
+ };
667
+ if (existingDesc && typeof existingDesc.set === "function") {
668
+ getDesc.set = existingDesc.set;
669
+ }
670
+ Object.defineProperty(obj, key, getDesc);
671
+ break;
672
+ }
673
+ case OP.DEFINE_SETTER:
674
+ {
675
+ // Stack: [..., obj, key, setterFn]
676
+ // Pops all three; defines an enumerable, configurable setter on obj.
677
+ // If a getter was already defined for this key, it is preserved.
678
+ var setterFn = this._pop();
679
+ var key = this._pop();
680
+ var obj = this._pop();
681
+ var existingDesc = Object.getOwnPropertyDescriptor(obj, key);
682
+ var setDesc = {
683
+ set: setterFn,
684
+ configurable: true,
685
+ enumerable: true
686
+ };
687
+ if (existingDesc && typeof existingDesc.get === "function") {
688
+ setDesc.get = existingDesc.get;
689
+ }
690
+ Object.defineProperty(obj, key, setDesc);
691
+ break;
692
+ }
693
+ case OP.DEBUGGER:
694
+ {
695
+ debugger;
696
+ break;
697
+ }
698
+ default:
699
+ throw new Error("Unknown opcode: " + op + " at pc " + (frame._pc - 1));
700
+ }
701
+ } catch (err) {
702
+ // Exception handler unwinding (CPython-style frame walk, Lua-style upvalue close).
703
+ // Walk from the current frame upward until we find a frame that has an open
704
+ // exception handler (TRY_SETUP without a matching TRY_END).
705
+ // For every frame we abandon along the way, close its captured upvalues.
706
+ var handledFrame = null;
707
+ var searchFrame = this._currentFrame;
708
+ while (true) {
709
+ if (searchFrame._handlerStack.length > 0) {
710
+ handledFrame = searchFrame;
711
+ break;
712
+ }
713
+ // No handler in this frame — abandon it and walk up.
714
+ this._closeUpvaluesFor(searchFrame);
715
+ if (this._frameStack.length === 0) break;
716
+ searchFrame = this._frameStack.pop();
717
+ this._currentFrame = searchFrame;
718
+ }
719
+ if (!handledFrame) throw err; // no handler anywhere — propagate to host
720
+
721
+ var h = handledFrame._handlerStack.pop();
722
+ // Restore the VM value stack to the depth recorded at TRY_SETUP time,
723
+ // then push the caught exception so the catch binding can store it.
724
+ this._stack.length = h.stackDepth;
725
+ this._push(err);
726
+ // Discard any call-frames that were pushed inside the try body
727
+ // (functions called from within the try block that are still live).
728
+ this._frameStack.length = h.frameStackDepth;
729
+ // Jump to the catch block.
730
+ handledFrame._pc = h.handlerPc;
731
+ this._currentFrame = handledFrame;
732
+ }
733
+ }
734
+ };
735
+
736
+ // Boot
737
+ var globals = {}; // global object for globals
738
+
739
+ // Always pull built-ins from globalThis so eval() scoping can't shadow them
740
+ // with a local `window` variable (e.g. the test harness fake window).
741
+ for (var k of Object.getOwnPropertyNames(globalThis)) {
742
+ globals[k] = globalThis[k];
743
+ }
744
+ // If a window object is in scope (browser or test harness), capture it
745
+ // explicitly so VM code can read/write window.TEST_OUTPUT etc.
746
+ if (typeof window !== "undefined") {
747
+ globals["window"] = window;
748
+ }
749
+
750
+ // Transfer common primitives
751
+ globals.undefined = undefined;
752
+ globals.Infinity = Infinity;
753
+ globals.NaN = NaN;
754
+ var vm = new VM(decodeBytecode(BYTECODE), MAIN_START_PC, CONSTANTS, globals);
755
+ vm.run();