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