forked from mbebenita/j2me.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm.js
2581 lines (2418 loc) · 83.4 KB
/
vm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
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
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
396
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set shiftwidth=4 tabstop=4 autoindent cindent expandtab: */
'use strict';
var VM = {};
VM.Yield = {};
VM.Pause = {};
VM.DEBUG = false;
VM.DEBUG_PRINT_ALL_EXCEPTIONS = false;
VM.traceLog = "";
VM.trace = function(type, pid, methodInfo, returnVal) {
VM.traceLog += type + " " + pid + " " + methodInfo.classInfo.className + "." +
methodInfo.name + ":" + methodInfo.signature +
(returnVal ? (" " + returnVal) : "") + "\n";
}
function checkArrayAccess(ctx, refArray, idx) {
if (!refArray) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
}
if (idx < 0 || idx >= refArray.length) {
ctx.raiseExceptionAndYield("java/lang/ArrayIndexOutOfBoundsException", idx);
}
}
function resolve(ctx, cp, idx, isStatic) {
var constant = cp[idx];
if (!constant.tag)
return constant;
switch(constant.tag) {
case 3: // TAGS.CONSTANT_Integer
constant = constant.integer;
break;
case 4: // TAGS.CONSTANT_Float
constant = constant.float;
break;
case 8: // TAGS.CONSTANT_String
constant = util.newString(cp[constant.string_index].bytes);
break;
case 5: // TAGS.CONSTANT_Long
constant = Long.fromBits(constant.lowBits, constant.highBits);
break;
case 6: // TAGS.CONSTANT_Double
constant = constant.double;
break;
case 7: // TAGS.CONSTANT_Class
constant = CLASSES.getClass(cp[constant.name_index].bytes);
break;
case 9: // TAGS.CONSTANT_Fieldref
var classInfo = resolve(ctx, cp, constant.class_index, isStatic);
var fieldName = cp[cp[constant.name_and_type_index].name_index].bytes;
var signature = cp[cp[constant.name_and_type_index].signature_index].bytes;
constant = CLASSES.getField(classInfo, (isStatic ? "S" : "I") + "." + fieldName + "." + signature);
if (!constant)
ctx.raiseExceptionAndYield("java/lang/RuntimeException",
classInfo.className + "." + fieldName + "." + signature + " not found");
break;
case 10: // TAGS.CONSTANT_Methodref
case 11: // TAGS.CONSTANT_InterfaceMethodref
var classInfo = resolve(ctx, cp, constant.class_index, isStatic);
var methodName = cp[cp[constant.name_and_type_index].name_index].bytes;
var signature = cp[cp[constant.name_and_type_index].signature_index].bytes;
constant = CLASSES.getMethod(classInfo, (isStatic ? "S" : "I") + "." + methodName + "." + signature);
if (!constant)
ctx.raiseExceptionAndYield("java/lang/RuntimeException",
classInfo.className + "." + methodName + "." + signature + " not found");
break;
default:
throw new Error("not supported constant type");
}
return cp[idx] = constant;
}
function classInitCheck(ctx, frame, classInfo, ip) {
if (classInfo.isArrayClass || ctx.runtime.initialized[classInfo.className])
return;
frame.ip = ip;
ctx.pushClassInitFrame(classInfo);
throw VM.Yield;
}
function pushFrame(ctx, methodInfo) {
var frame = ctx.pushFrame(methodInfo);
if (methodInfo.isSynchronized) {
if (!frame.lockObject) {
frame.lockObject = methodInfo.isStatic
? methodInfo.classInfo.getClassObject(ctx)
: frame.getLocal(0);
}
ctx.monitorEnter(frame.lockObject);
}
return frame;
}
function popFrame(ctx, callee, consumes) {
if (callee.lockObject)
ctx.monitorExit(callee.lockObject);
var frame = ctx.popFrame();
var stack = frame.stack;
switch (consumes) {
case 2:
stack.push2(callee.stack.pop2());
break;
case 1:
stack.push(callee.stack.pop());
break;
}
return frame;
}
function buildExceptionLog(ex, stackTrace) {
var className = ex.class.className;
var detailMessage = util.fromJavaString(CLASSES.getField(ex.class, "I.detailMessage.Ljava/lang/String;").get(ex));
return className + ": " + (detailMessage || "") + "\n" + stackTrace.join("\n") + "\n\n";
}
function throw_(ex, ctx) {
var exClass = ex.class;
var frame = ctx.current();
var stack = frame.stack;
var cp = frame.cp;
var stackTrace = [];
do {
var exception_table = frame.methodInfo.exception_table;
var handler_pc = null;
for (var i=0; exception_table && i<exception_table.length; i++) {
if (frame.ip >= exception_table[i].start_pc && frame.ip <= exception_table[i].end_pc) {
if (exception_table[i].catch_type === 0) {
handler_pc = exception_table[i].handler_pc;
break;
} else {
var classInfo = resolve(ctx, cp, exception_table[i].catch_type);
if (ex.class.isAssignableTo(classInfo)) {
handler_pc = exception_table[i].handler_pc;
break;
}
}
}
}
var classInfo = frame.methodInfo.classInfo;
if (classInfo && classInfo.className) {
stackTrace.push(" - " + classInfo.className + "." + frame.methodInfo.name + "(), bci=" + frame.ip);
}
if (handler_pc != null) {
stack.length = 0;
stack.push(ex);
frame.ip = handler_pc;
if (VM.DEBUG_PRINT_ALL_EXCEPTIONS) {
console.error(buildExceptionLog(ex, stackTrace));
}
while (frame.methodInfo.compiled) {
frame = frame.methodInfo.compiled(ctx);
}
return frame;
}
if (ctx.frames.length == 1) {
break;
}
frame = popFrame(ctx, frame, 0);
stack = frame.stack;
cp = frame.cp;
} while (frame.methodInfo);
ctx.kill();
if (ctx.thread && ctx.thread.waiting && ctx.thread.waiting.length > 0) {
console.error(buildExceptionLog(ex, stackTrace));
ctx.thread.waiting.forEach(function(waitingCtx, n) {
ctx.thread.waiting[n] = null;
waitingCtx.wakeup(ctx.thread);
});
return frame;
} else {
throw new Error(buildExceptionLog(ex, stackTrace));
}
}
VM.execute = function(ctx) {
var frame = ctx.current();
while (frame && frame.methodInfo.compiled) {
frame = frame.methodInfo.compiled(ctx);
}
if (!frame) {
return;
}
var cp = frame.cp;
var stack = frame.stack;
while (true) {
var op = frame.read8();
switch (op) {
case 0x00: // nop
break;
case 0x01: // aconst_null
stack.push(null);
break;
case 0x02: // aconst_m1
stack.push(-1);
break;
case 0x03: // iconst_0
case 0x0b: // fconst_0
stack.push(0);
break;
case 0x0e: // dconst_0
stack.push2(0);
break;
case 0x04: // iconst_1
case 0x0c: // fconst_1
stack.push(1);
break;
case 0x0f: // dconst_1
stack.push2(1);
break;
case 0x05: // iconst_2
case 0x0d: // fconst_2
stack.push(2);
break;
case 0x06: // iconst_3
stack.push(3);
break;
case 0x07: // iconst_4
stack.push(4);
break;
case 0x08: // iconst_5
stack.push(5);
break;
case 0x09: // lconst_0
stack.push2(Long.fromInt(0));
break;
case 0x0a: // lconst_1
stack.push2(Long.fromInt(1));
break;
case 0x10: // bipush
stack.push(frame.read8signed());
break;
case 0x11: // sipush
stack.push(frame.read16signed());
break;
case 0x12: // ldc
case 0x13: // ldc_w
var idx = (op === 0x12) ? frame.read8() : frame.read16();
var constant = cp[idx];
if (constant.tag)
constant = resolve(ctx, cp, idx);
stack.push(constant);
break;
case 0x14: // ldc2_w
var idx = frame.read16();
var constant = cp[idx];
if (constant.tag)
constant = resolve(ctx, cp, idx);
stack.push2(constant);
break;
case 0x15: // iload
case 0x17: // fload
case 0x19: // aload
stack.push(frame.getLocal(frame.read8()));
break;
case 0x16: // lload
case 0x18: // dload
stack.push2(frame.getLocal(frame.read8()));
break;
case 0x1a: // iload_0
case 0x22: // fload_0
case 0x2a: // aload_0
stack.push(frame.getLocal(0));
break;
case 0x1b: // iload_1
case 0x23: // fload_1
case 0x2b: // aload_1
stack.push(frame.getLocal(1));
break;
case 0x1c: // iload_2
case 0x24: // fload_2
case 0x2c: // aload_2
stack.push(frame.getLocal(2));
break;
case 0x1d: // iload_3
case 0x25: // fload_3
case 0x2d: // aload_3
stack.push(frame.getLocal(3));
break;
case 0x1e: // lload_0
case 0x26: // dload_0
stack.push2(frame.getLocal(0));
break;
case 0x1f: // lload_1
case 0x27: // dload_1
stack.push2(frame.getLocal(1));
break;
case 0x20: // lload_2
case 0x28: // dload_2
stack.push2(frame.getLocal(2));
break;
case 0x21: // lload_3
case 0x29: // dload_3
stack.push2(frame.getLocal(3));
break;
case 0x2e: // iaload
case 0x30: // faload
case 0x32: // aaload
case 0x33: // baload
case 0x34: // caload
case 0x35: // saload
var idx = stack.pop();
var refArray = stack.pop();
checkArrayAccess(ctx, refArray, idx);
stack.push(refArray[idx]);
break;
case 0x2f: // laload
case 0x31: // daload
var idx = stack.pop();
var refArray = stack.pop();
checkArrayAccess(ctx, refArray, idx);
stack.push2(refArray[idx]);
break;
case 0x36: // istore
case 0x38: // fstore
case 0x3a: // astore
frame.setLocal(frame.read8(), stack.pop());
break;
case 0x37: // lstore
case 0x39: // dstore
frame.setLocal(frame.read8(), stack.pop2());
break;
case 0x3b: // istore_0
case 0x43: // fstore_0
case 0x4b: // astore_0
frame.setLocal(0, stack.pop());
break;
case 0x3c: // istore_1
case 0x44: // fstore_1
case 0x4c: // astore_1
frame.setLocal(1, stack.pop());
break;
case 0x3d: // istore_2
case 0x45: // fstore_2
case 0x4d: // astore_2
frame.setLocal(2, stack.pop());
break;
case 0x3e: // istore_3
case 0x46: // fstore_3
case 0x4e: // astore_3
frame.setLocal(3, stack.pop());
break;
case 0x3f: // lstore_0
case 0x47: // dstore_0
frame.setLocal(0, stack.pop2());
break;
case 0x40: // lstore_1
case 0x48: // dstore_1
frame.setLocal(1, stack.pop2());
break;
case 0x41: // lstore_2
case 0x49: // dstore_2
frame.setLocal(2, stack.pop2());
break;
case 0x42: // lstore_3
case 0x4a: // dstore_3
frame.setLocal(3, stack.pop2());
break;
case 0x4f: // iastore
case 0x51: // fastore
case 0x54: // bastore
case 0x55: // castore
case 0x56: // sastore
var val = stack.pop();
var idx = stack.pop();
var refArray = stack.pop();
checkArrayAccess(ctx, refArray, idx);
refArray[idx] = val;
break;
case 0x50: // lastore
case 0x52: // dastore
var val = stack.pop2();
var idx = stack.pop();
var refArray = stack.pop();
checkArrayAccess(ctx, refArray, idx);
refArray[idx] = val;
break;
case 0x53: // aastore
var val = stack.pop();
var idx = stack.pop();
var refArray = stack.pop();
checkArrayAccess(ctx, refArray, idx);
if (val && !val.class.isAssignableTo(refArray.class.elementClass)) {
ctx.raiseExceptionAndYield("java/lang/ArrayStoreException");
}
refArray[idx] = val;
break;
case 0x57: // pop
stack.pop();
break;
case 0x58: // pop2
stack.pop2();
break;
case 0x59: // dup
var val = stack.pop();
stack.push(val);
stack.push(val);
break;
case 0x5a: // dup_x1
var a = stack.pop();
var b = stack.pop();
stack.push(a);
stack.push(b);
stack.push(a);
break;
case 0x5b: // dup_x2
var a = stack.pop();
var b = stack.pop();
var c = stack.pop();
stack.push(a);
stack.push(c);
stack.push(b);
stack.push(a);
break;
case 0x5c: // dup2
var a = stack.pop();
var b = stack.pop();
stack.push(b);
stack.push(a);
stack.push(b);
stack.push(a);
break;
case 0x5d: // dup2_x1
var a = stack.pop();
var b = stack.pop();
var c = stack.pop();
stack.push(b);
stack.push(a);
stack.push(c);
stack.push(b);
stack.push(a);
break;
case 0x5e: // dup2_x2
var a = stack.pop();
var b = stack.pop();
var c = stack.pop();
var d = stack.pop();
stack.push(b);
stack.push(a);
stack.push(d);
stack.push(c);
stack.push(b);
stack.push(a);
break;
case 0x5f: // swap
var a = stack.pop();
var b = stack.pop();
stack.push(a);
stack.push(b);
break;
case 0x84: // iinc
var idx = frame.read8();
var val = frame.read8signed();
frame.setLocal(idx, frame.getLocal(idx) + val);
break;
case 0x60: // iadd
stack.push((stack.pop() + stack.pop())|0);
break;
case 0x61: // ladd
stack.push2(stack.pop2().add(stack.pop2()));
break;
case 0x62: // fadd
stack.push(Math.fround(stack.pop() + stack.pop()));
break;
case 0x63: // dadd
stack.push2(stack.pop2() + stack.pop2());
break;
case 0x64: // isub
stack.push((- stack.pop() + stack.pop())|0);
break;
case 0x65: // lsub
stack.push2(stack.pop2().negate().add(stack.pop2()));
break;
case 0x66: // fsub
stack.push(Math.fround(- stack.pop() + stack.pop()));
break;
case 0x67: // dsub
stack.push2(- stack.pop2() + stack.pop2());
break;
case 0x68: // imul
stack.push(Math.imul(stack.pop(), stack.pop()));
break;
case 0x69: // lmul
stack.push2(stack.pop2().multiply(stack.pop2()));
break;
case 0x6a: // fmul
stack.push(Math.fround(stack.pop() * stack.pop()));
break;
case 0x6b: // dmul
stack.push2(stack.pop2() * stack.pop2());
break;
case 0x6c: // idiv
var b = stack.pop();
var a = stack.pop();
if (!b) {
ctx.raiseExceptionAndYield("java/lang/ArithmeticException", "/ by zero");
}
stack.push((a === util.INT_MIN && b === -1) ? a : ((a / b)|0));
break;
case 0x6d: // ldiv
var b = stack.pop2();
var a = stack.pop2();
if (b.isZero()) {
ctx.raiseExceptionAndYield("java/lang/ArithmeticException", "/ by zero");
}
stack.push2(a.div(b));
break;
case 0x6e: // fdiv
var b = stack.pop();
var a = stack.pop();
stack.push(Math.fround(a / b));
break;
case 0x6f: // ddiv
var b = stack.pop2();
var a = stack.pop2();
stack.push2(a / b);
break;
case 0x70: // irem
var b = stack.pop();
var a = stack.pop();
if (!b) {
ctx.raiseExceptionAndYield("java/lang/ArithmeticException", "/ by zero");
}
stack.push(a % b);
break;
case 0x71: // lrem
var b = stack.pop2();
var a = stack.pop2();
if (b.isZero()) {
ctx.raiseExceptionAndYield("java/lang/ArithmeticException", "/ by zero");
}
stack.push2(a.modulo(b));
break;
case 0x72: // frem
var b = stack.pop();
var a = stack.pop();
stack.push(Math.fround(a % b));
break;
case 0x73: // drem
var b = stack.pop2();
var a = stack.pop2();
stack.push2(a % b);
break;
case 0x74: // ineg
stack.push((- stack.pop())|0);
break;
case 0x75: // lneg
stack.push2(stack.pop2().negate());
break;
case 0x76: // fneg
stack.push(- stack.pop());
break;
case 0x77: // dneg
stack.push2(- stack.pop2());
break;
case 0x78: // ishl
var b = stack.pop();
var a = stack.pop();
stack.push(a << b);
break;
case 0x79: // lshl
var b = stack.pop();
var a = stack.pop2();
stack.push2(a.shiftLeft(b));
break;
case 0x7a: // ishr
var b = stack.pop();
var a = stack.pop();
stack.push(a >> b);
break;
case 0x7b: // lshr
var b = stack.pop();
var a = stack.pop2();
stack.push2(a.shiftRight(b));
break;
case 0x7c: // iushr
var b = stack.pop();
var a = stack.pop();
stack.push(a >>> b);
break;
case 0x7d: // lushr
var b = stack.pop();
var a = stack.pop2();
stack.push2(a.shiftRightUnsigned(b));
break;
case 0x7e: // iand
stack.push(stack.pop() & stack.pop());
break;
case 0x7f: // land
stack.push2(stack.pop2().and(stack.pop2()));
break;
case 0x80: // ior
stack.push(stack.pop() | stack.pop());
break;
case 0x81: // lor
stack.push2(stack.pop2().or(stack.pop2()));
break;
case 0x82: // ixor
stack.push(stack.pop() ^ stack.pop());
break;
case 0x83: // lxor
stack.push2(stack.pop2().xor(stack.pop2()));
break;
case 0x94: // lcmp
var b = stack.pop2();
var a = stack.pop2();
if (a.greaterThan(b)) {
stack.push(1);
} else if (a.lessThan(b)) {
stack.push(-1);
} else {
stack.push(0);
}
break;
case 0x95: // fcmpl
var b = stack.pop();
var a = stack.pop();
if (isNaN(a) || isNaN(b)) {
stack.push(-1);
} else if (a > b) {
stack.push(1);
} else if (a < b) {
stack.push(-1);
} else {
stack.push(0);
}
break;
case 0x96: // fcmpg
var b = stack.pop();
var a = stack.pop();
if (isNaN(a) || isNaN(b)) {
stack.push(1);
} else if (a > b) {
stack.push(1);
} else if (a < b) {
stack.push(-1);
} else {
stack.push(0);
}
break;
case 0x97: // dcmpl
var b = stack.pop2();
var a = stack.pop2();
if (isNaN(a) || isNaN(b)) {
stack.push(-1);
} else if (a > b) {
stack.push(1);
} else if (a < b) {
stack.push(-1);
} else {
stack.push(0);
}
break;
case 0x98: // dcmpg
var b = stack.pop2();
var a = stack.pop2();
if (isNaN(a) || isNaN(b)) {
stack.push(1);
} else if (a > b) {
stack.push(1);
} else if (a < b) {
stack.push(-1);
} else {
stack.push(0);
}
break;
case 0x99: // ifeq
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() === 0 ? jmp : frame.ip;
break;
case 0x9a: // ifne
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() !== 0 ? jmp : frame.ip;
break;
case 0x9b: // iflt
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() < 0 ? jmp : frame.ip;
break;
case 0x9c: // ifge
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() >= 0 ? jmp : frame.ip;
break;
case 0x9d: // ifgt
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() > 0 ? jmp : frame.ip;
break;
case 0x9e: // ifle
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() <= 0 ? jmp : frame.ip;
break;
case 0x9f: // if_icmpeq
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() === stack.pop() ? jmp : frame.ip;
break;
case 0xa0: // if_cmpne
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() !== stack.pop() ? jmp : frame.ip;
break;
case 0xa1: // if_icmplt
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() > stack.pop() ? jmp : frame.ip;
break;
case 0xa2: // if_icmpge
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() <= stack.pop() ? jmp : frame.ip;
break;
case 0xa3: // if_icmpgt
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() < stack.pop() ? jmp : frame.ip;
break;
case 0xa4: // if_icmple
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() >= stack.pop() ? jmp : frame.ip;
break;
case 0xa5: // if_acmpeq
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() === stack.pop() ? jmp : frame.ip;
break;
case 0xa6: // if_acmpne
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() !== stack.pop() ? jmp : frame.ip;
break;
case 0xc6: // ifnull
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = !stack.pop() ? jmp : frame.ip;
break;
case 0xc7: // ifnonnull
var jmp = frame.ip - 1 + frame.read16signed();
frame.ip = stack.pop() ? jmp : frame.ip;
break;
case 0xa7: // goto
frame.ip += frame.read16signed() - 1;
break;
case 0xc8: // goto_w
frame.ip += frame.read32signed() - 1;
break;
case 0xa8: // jsr
var jmp = frame.read16();
stack.push(frame.ip);
frame.ip = jmp;
break;
case 0xc9: // jsr_w
var jmp = frame.read32();
stack.push(frame.ip);
frame.ip = jmp;
break;
case 0xa9: // ret
frame.ip = frame.getLocal(frame.read8());
break;
case 0x85: // i2l
stack.push2(Long.fromInt(stack.pop()));
break;
case 0x86: // i2f
break;
case 0x87: // i2d
stack.push2(stack.pop());
break;
case 0x88: // l2i
stack.push(stack.pop2().toInt());
break;
case 0x89: // l2f
stack.push(Math.fround(stack.pop2().toNumber()));
break;
case 0x8a: // l2d
stack.push2(stack.pop2().toNumber());
break;
case 0x8b: // f2i
stack.push(util.double2int(stack.pop()));
break;
case 0x8c: // f2l
stack.push2(Long.fromNumber(stack.pop()));
break;
case 0x8d: // f2d
stack.push2(stack.pop());
break;
case 0x8e: // d2i
stack.push(util.double2int(stack.pop2()));
break;
case 0x8f: // d2l
stack.push2(util.double2long(stack.pop2()));
break;
case 0x90: // d2f
stack.push(Math.fround(stack.pop2()));
break;
case 0x91: // i2b
stack.push((stack.pop() << 24) >> 24);
break;
case 0x92: // i2c
stack.push(stack.pop() & 0xffff);
break;
case 0x93: // i2s
stack.push((stack.pop() << 16) >> 16);
break;
case 0xaa: // tableswitch
var startip = frame.ip;
while ((frame.ip & 3) != 0)
frame.ip++;
var def = frame.read32signed();
var low = frame.read32signed();
var high = frame.read32signed();
var val = stack.pop();
var jmp;
if (val < low || val > high) {
jmp = def;
} else {
frame.ip += (val - low) << 2;
jmp = frame.read32signed();
}
frame.ip = startip - 1 + jmp;
break;
case 0xab: // lookupswitch
var startip = frame.ip;
while ((frame.ip & 3) != 0)
frame.ip++;
var jmp = frame.read32signed();
var size = frame.read32();
var val = frame.stack.pop();
lookup:
for (var i=0; i<size; i++) {
var key = frame.read32signed();
var offset = frame.read32signed();
if (key === val) {
jmp = offset;
}
if (key >= val) {
break lookup;
}
}
frame.ip = startip - 1 + jmp;
break;
case 0xbc: // newarray
var type = frame.read8();
var size = stack.pop();
if (size < 0) {
ctx.raiseExceptionAndYield("java/lang/NegativeArraySizeException", size);
}
stack.push(util.newPrimitiveArray("????ZCFDBSIJ"[type], size));
break;
case 0xbd: // anewarray
var idx = frame.read16();
var classInfo = cp[idx];
if (classInfo.tag)
classInfo = resolve(ctx, cp, idx);
var size = stack.pop();
if (size < 0) {
ctx.raiseExceptionAndYield("java/lang/NegativeArraySizeException", size);
}
var className = classInfo.className;
if (className[0] !== "[")
className = "L" + className + ";";
className = "[" + className;
stack.push(util.newArray(className, size));
break;
case 0xc5: // multianewarray
var idx = frame.read16();
var classInfo = cp[idx];
if (classInfo.tag)
classInfo = resolve(ctx, cp, idx);
var dimensions = frame.read8();
var lengths = new Array(dimensions);
for (var i=0; i<dimensions; i++)
lengths[i] = stack.pop();
stack.push(util.newMultiArray(classInfo.className, lengths.reverse()));
break;
case 0xbe: // arraylength
var obj = stack.pop();
if (!obj) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
}
stack.push(obj.length);
break;
case 0xb4: // getfield
var idx = frame.read16();
var field = cp[idx];
if (field.tag)
field = resolve(ctx, cp, idx, false);
var obj = stack.pop();
if (!obj) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
}
stack.pushType(field.signature, field.get(obj));
break;
case 0xb5: // putfield
var idx = frame.read16();
var field = cp[idx];
if (field.tag)
field = resolve(ctx, cp, idx, false);
var val = stack.popType(field.signature);
var obj = stack.pop();
if (!obj) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
}
field.set(obj, val);
break;
case 0xb2: // getstatic
var idx = frame.read16();
var field = cp[idx];
if (field.tag)
field = resolve(ctx, cp, idx, true);
classInitCheck(ctx, frame, field.classInfo, frame.ip-3);
var value = ctx.runtime.getStatic(field);
if (typeof value === "undefined") {
value = util.defaultValue(field.signature);
}
stack.pushType(field.signature, value);
break;
case 0xb3: // putstatic
var idx = frame.read16();
var field = cp[idx];
if (field.tag)
field = resolve(ctx, cp, idx, true);
classInitCheck(ctx, frame, field.classInfo, frame.ip-3);
ctx.runtime.setStatic(field, stack.popType(field.signature));
break;
case 0xbb: // new
var idx = frame.read16();
var classInfo = cp[idx];
if (classInfo.tag)
classInfo = resolve(ctx, cp, idx);
classInitCheck(ctx, frame, classInfo, frame.ip-3);
stack.push(util.newObject(classInfo));
break;
case 0xc0: // checkcast
var idx = frame.read16();
var classInfo = cp[idx];
if (classInfo.tag)
classInfo = resolve(ctx, cp, idx);
var obj = stack[stack.length - 1];
if (obj && !obj.class.isAssignableTo(classInfo)) {
ctx.raiseExceptionAndYield("java/lang/ClassCastException",
obj.class.className + " is not assignable to " +
classInfo.className);
}
break;
case 0xc1: // instanceof
var idx = frame.read16();
var classInfo = cp[idx];
if (classInfo.tag)
classInfo = resolve(ctx, cp, idx);
var obj = stack.pop();
var result = !obj ? false : obj.class.isAssignableTo(classInfo);
stack.push(result ? 1 : 0);
break;
case 0xbf: // athrow
var obj = stack.pop();
if (!obj) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
}
frame = throw_(obj, ctx);
stack = frame.stack;
cp = frame.cp;
break;
case 0xc2: // monitorenter
var obj = stack.pop();
if (!obj) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
}
ctx.monitorEnter(obj);
break;
case 0xc3: // monitorexit
var obj = stack.pop();
if (!obj) {
ctx.raiseExceptionAndYield("java/lang/NullPointerException");
}
ctx.monitorExit(obj);
break;
case 0xc4: // wide
switch (op = frame.read8()) {
case 0x15: // iload
case 0x17: // fload
case 0x19: // aload
stack.push(frame.getLocal(frame.read16()));
break;
case 0x16: // lload
case 0x18: // dload
stack.push2(frame.getLocal(frame.read16()));