/
lentil.js
4055 lines (3693 loc) · 116 KB
/
lentil.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
var DEBUG_LOAD_CLASS = false;
var DEBUG_METHOD_CALLS = false;
var DEBUG_NEW_INSTANCE = false;
var DEBUG_SHOW_DISASSEMBLY = false;
var DEBUG_TRACE_STACK = false;
var DEBUG_TRACE_DISASSEMBLE = false;
var CONSTANT_Utf8 = 1;
var CONSTANT_Integer = 3;
var CONSTANT_Float = 4;
var CONSTANT_Long = 5;
var CONSTANT_Double = 6;
var CONSTANT_Class = 7;
var CONSTANT_String = 8;
var CONSTANT_Fieldref = 9;
var CONSTANT_Methodref = 10;
var CONSTANT_InterfaceMethodref = 11;
var CONSTANT_NameAndType = 12;
var ACC_PUBLIC = 0x0001;
var ACC_PRIVATE = 0x0002;
var ACC_PROTECTED = 0x0004;
var ACC_STATIC = 0x0008;
var ACC_FINAL = 0x0010;
var ACC_SUPR = 0x0020;
var ACC_SYNCHRONIZED = 0x0020;
var ACC_VOLATILE = 0x0040;
var ACC_TRANSIENT = 0x0080;
var ACC_NATIVE = 0x0100;
var ACC_INTERFACE = 0x0200;
var ACC_ABSTRACT = 0x0400;
var ACC_STRICT = 0x0800;
var T_BOOLEAN = 4;
var T_CHAR = 5;
var T_FLOAT = 6;
var T_DOUBLE = 7;
var T_BYTE = 8;
var T_SHORT = 9;
var T_INT = 10;
var T_LONG = 11;
var ArrayTypeChar = {};
ArrayTypeChar[T_BOOLEAN] = "Z";
ArrayTypeChar[T_CHAR ] = "C";
ArrayTypeChar[T_FLOAT ] = "F";
ArrayTypeChar[T_DOUBLE ] = "D";
ArrayTypeChar[T_BYTE ] = "B";
ArrayTypeChar[T_SHORT ] = "S";
ArrayTypeChar[T_INT ] = "I";
ArrayTypeChar[T_LONG ] = "J";
var op_nop = 0;
var op_aconst_null = 1;
var op_iconst_m1 = 2;
var op_iconst_0 = 3;
var op_iconst_1 = 4;
var op_iconst_2 = 5;
var op_iconst_3 = 6;
var op_iconst_4 = 7;
var op_iconst_5 = 8;
var op_lconst_0 = 9;
var op_lconst_1 = 10;
var op_fconst_0 = 11;
var op_fconst_1 = 12;
var op_fconst_2 = 13;
var op_dconst_0 = 14;
var op_dconst_1 = 15;
var op_bipush = 16;
var op_sipush = 17;
var op_ldc = 18;
var op_ldc_w = 19;
var op_ldc2_w = 20;
var op_iload = 21;
var op_lload = 22;
var op_fload = 23;
var op_dload = 24;
var op_aload = 25;
var op_iload_0 = 26;
var op_iload_1 = 27;
var op_iload_2 = 28;
var op_iload_3 = 29;
var op_lload_0 = 30;
var op_lload_1 = 31;
var op_lload_2 = 32;
var op_lload_3 = 33;
var op_fload_0 = 34;
var op_fload_1 = 35;
var op_fload_2 = 36;
var op_fload_3 = 37;
var op_dload_0 = 38;
var op_dload_1 = 39;
var op_dload_2 = 40;
var op_dload_3 = 41;
var op_aload_0 = 42;
var op_aload_1 = 43;
var op_aload_2 = 44;
var op_aload_3 = 45;
var op_iaload = 46;
var op_laload = 47;
var op_faload = 48;
var op_daload = 49;
var op_aaload = 50;
var op_baload = 51;
var op_caload = 52;
var op_saload = 53;
var op_istore = 54;
var op_lstore = 55;
var op_fstore = 56;
var op_dstore = 57;
var op_astore = 58;
var op_istore_0 = 59;
var op_istore_1 = 60;
var op_istore_2 = 61;
var op_istore_3 = 62;
var op_lstore_0 = 63;
var op_lstore_1 = 64;
var op_lstore_2 = 65;
var op_lstore_3 = 66;
var op_fstore_0 = 67;
var op_fstore_1 = 68;
var op_fstore_2 = 69;
var op_fstore_3 = 70;
var op_dstore_0 = 71;
var op_dstore_1 = 72;
var op_dstore_2 = 73;
var op_dstore_3 = 74;
var op_astore_0 = 75;
var op_astore_1 = 76;
var op_astore_2 = 77;
var op_astore_3 = 78;
var op_iastore = 79;
var op_lastore = 80;
var op_fastore = 81;
var op_dastore = 82;
var op_aastore = 83;
var op_bastore = 84;
var op_castore = 85;
var op_sastore = 86;
var op_pop = 87;
var op_pop2 = 88;
var op_dup = 89;
var op_dup_x1 = 90;
var op_dup_x2 = 91;
var op_dup2 = 92;
var op_dup2_x1 = 93;
var op_dup2_x2 = 94;
var op_swap = 95;
var op_iadd = 96;
var op_ladd = 97;
var op_fadd = 98;
var op_dadd = 99;
var op_isub = 100;
var op_lsub = 101;
var op_fsub = 102;
var op_dsub = 103;
var op_imul = 104;
var op_lmul = 105;
var op_fmul = 106;
var op_dmul = 107;
var op_idiv = 108;
var op_ldiv = 109;
var op_fdiv = 110;
var op_ddiv = 111;
var op_irem = 112;
var op_lrem = 113;
var op_frem = 114;
var op_drem = 115;
var op_ineg = 116;
var op_lneg = 117;
var op_fneg = 118;
var op_dneg = 119;
var op_ishl = 120;
var op_lshl = 121;
var op_ishr = 122;
var op_lshr = 123;
var op_iushr = 124;
var op_lushr = 125;
var op_iand = 126;
var op_land = 127;
var op_ior = 128;
var op_lor = 129;
var op_ixor = 130;
var op_lxor = 131;
var op_iinc = 132;
var op_i2l = 133;
var op_i2f = 134;
var op_i2d = 135;
var op_l2i = 136;
var op_l2f = 137;
var op_l2d = 138;
var op_f2i = 139;
var op_f2l = 140;
var op_f2d = 141;
var op_d2i = 142;
var op_d2l = 143;
var op_d2f = 144;
var op_i2b = 145;
var op_i2c = 146;
var op_i2s = 147;
var op_lcmp = 148;
var op_fcmpl = 149;
var op_fcmpg = 150;
var op_dcmpl = 151;
var op_dcmpg = 152;
var op_ifeq = 153;
var op_ifne = 154;
var op_iflt = 155;
var op_ifge = 156;
var op_ifgt = 157;
var op_ifle = 158;
var op_if_icmpeq = 159;
var op_if_icmpne = 160;
var op_if_icmplt = 161;
var op_if_icmpge = 162;
var op_if_icmpgt = 163;
var op_if_icmple = 164;
var op_if_acmpeq = 165;
var op_if_acmpne = 166;
var op_goto = 167;
var op_jsr = 168;
var op_ret = 169;
var op_tableswitch = 170;
var op_lookupswitch = 171;
var op_ireturn = 172;
var op_lreturn = 173;
var op_freturn = 174;
var op_dreturn = 175;
var op_areturn = 176;
var op_return = 177;
var op_getstatic = 178;
var op_putstatic = 179;
var op_getfield = 180;
var op_putfield = 181;
var op_invokevirtual = 182;
var op_invokespecial = 183;
var op_invokestatic = 184;
var op_invokeinterface = 185;
var op_new = 187;
var op_newarray = 188;
var op_anewarray = 189;
var op_arraylength = 190;
var op_athrow = 191;
var op_checkcast = 192;
var op_instanceof = 193;
var op_monitorenter = 194;
var op_monitorexit = 195;
var op_wide = 196;
var op_multianewarray = 197;
var op_ifnull = 198;
var op_ifnonnull = 199;
var op_goto_w = 200;
var op_jsr_w = 201;
var op_breakpoint = 202;
var op_ret_w = 209;
OpcodeName = [
"nop", "aconst_null", "iconst_m1", "iconst_0", "iconst_1",
"iconst_2", "iconst_3", "iconst_4", "iconst_5", "lconst_0",
"lconst_1", "fconst_0", "fconst_1", "fconst_2", "dconst_0",
"dconst_1", "bipush", "sipush", "ldc", "ldc_w", "ldc2_w",
"iload", "lload", "fload", "dload", "aload", "iload_0",
"iload_1", "iload_2", "iload_3", "lload_0", "lload_1",
"lload_2", "lload_3", "fload_0", "fload_1", "fload_2",
"fload_3", "dload_0", "dload_1", "dload_2", "dload_3",
"aload_0", "aload_1", "aload_2", "aload_3", "iaload",
"laload", "faload", "daload", "aaload", "baload", "caload",
"saload", "istore", "lstore", "fstore", "dstore", "astore",
"istore_0", "istore_1", "istore_2", "istore_3", "lstore_0",
"lstore_1", "lstore_2", "lstore_3", "fstore_0", "fstore_1",
"fstore_2", "fstore_3", "dstore_0", "dstore_1", "dstore_2",
"dstore_3", "astore_0", "astore_1", "astore_2", "astore_3",
"iastore", "lastore", "fastore", "dastore", "aastore",
"bastore", "castore", "sastore", "pop", "pop2", "dup",
"dup_x1", "dup_x2", "dup2", "dup2_x1", "dup2_x2", "swap",
"iadd", "ladd", "fadd", "dadd", "isub", "lsub", "fsub",
"dsub", "imul", "lmul", "fmul", "dmul", "idiv", "ldiv",
"fdiv", "ddiv", "irem", "lrem", "frem", "drem", "ineg",
"lneg", "fneg", "dneg", "ishl", "lshl", "ishr", "lshr",
"iushr", "lushr", "iand", "land", "ior", "lor", "ixor",
"lxor", "iinc", "i2l", "i2f", "i2d", "l2i", "l2f",
"l2d", "f2i", "f2l", "f2d", "d2i", "d2l", "d2f",
"i2b", "i2c", "i2s", "lcmp", "fcmpl",
"fcmpg", "dcmpl", "dcmpg", "ifeq", "ifne", "iflt", "ifge",
"ifgt", "ifle", "if_icmpeq", "if_icmpne", "if_icmplt",
"if_icmpge", "if_icmpgt", "if_icmple", "if_acmpeq", "if_acmpne",
"goto", "jsr", "ret", "tableswitch", "lookupswitch",
"ireturn", "lreturn", "freturn", "dreturn", "areturn",
"return", "getstatic", "putstatic", "getfield", "putfield",
"invokevirtual", "invokespecial", "invokestatic",
"invokeinterface", "op_186", "new", "newarray", "anewarray",
"arraylength", "athrow", "checkcast", "instanceof",
"monitorenter", "monitorexit", "wide", "multianewarray",
"ifnull", "ifnonnull", "goto_w", "jsr_w", "breakpoint",
"op_203", "op_204", "op_205", "op_206", "op_207", "ret_w"
];
var ClassesToLink = [];
var JClass;
var JString;
var CurrentThread;
function JException(name, constructor, args, argcats) {
this.name = name;
this.constructor = constructor;
this.args = args;
this.argcats = argcats;
}
NativeMethod = {
"java/io/VMFile": {
"isDirectory(Ljava/lang/String;)Z": function(env, d) {
return false;
}
},
"java/lang/Object": {
"hashCode": function(env) {
return 1;
}
},
"java/lang/VMClass": {
"forName(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;": function(env, name, initialize, classloader) {
var c = env.cls.classloader.getInternalClass(name.s);
if (c === null) {
throw new JException("java/lang/ClassNotFoundException", "<init>()V", [], []);
}
return c.jclass;
},
"getClassLoader(Ljava/lang/Class;)Ljava/lang/ClassLoader;": function(env, c) {
return null; // TODO
},
"getName(Ljava/lang/Class;)Ljava/lang/String;": function(env, c) {
return internString(c.vmdata.name);
},
"isArray(Ljava/lang/Class;)Z": function(env, c) {
return c.vmdata instanceof JArray;
},
"isInterface(Ljava/lang/Class;)Z": function(env, c) {
return (c.vmdata.access_flags & ACC_INTERFACE) != 0;
},
"isPrimitive(Ljava/lang/Class;)Z": function(env, c) {
return false; // TODO
}
},
"java/lang/VMClassLoader": {
"getBootPackages()[Ljava/lang/String;": function(env) {
return null;
},
"getPrimitiveClass(C)Ljava/lang/Class;": function(env, c) {
return {"primitive_class": c};
},
"getSystemClassLoader()Ljava/lang/ClassLoader;": function(env) {
return null;
}
},
"java/lang/VMObject": {
"clone(Ljava/lang/Cloneable;)Ljava/lang/Object;": function(env, obj) {
var r = {};
for (p in obj) {
r[p] = obj[p];
}
return r;
},
"getClass(Ljava/lang/Object;)Ljava/lang/Class;": function(env, obj) {
return obj.__jvm_class.jclass;
}
},
"java/lang/VMSystem": {
"arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V": function(env, src, srcStart, dest, destStart, len) {
if (src === null || dest === null) {
throw ("null pointer exception");
}
if ((!src instanceof JArray && dest instanceof JArray)) {
throw ("arraycopy with not arrays");
}
if (len < 0 || srcStart < 0 || srcStart + len > src.len() || destStart < 0 || destStart + len > dest.len()) {
throw ("index out of bounds");
}
if (src.__jvm_class.name !== dest.__jvm_class.name) {
throw ("incompatible arrays: " + src.__jvm_class.name + " " + dest.__jvm_class.name);
}
if (src.s !== undefined && dest.s !== undefined) {
var t = src.s.substr(srcStart, len);
dest.s = dest.s.substring(0, destStart) + t + dest.s.substring(destStart + len);
} else {
var t = src.a.slice(srcStart, srcStart + len);
for (var i = 0; i < len; i++) {
dest.a[destStart + i] = t[i];
}
}
},
"identityHashCode(Ljava/lang/Object;)I": function(env, obj) {
// TODO: return id(obj);
return 1;
}
},
"java/lang/VMThread": {
"currentThread()Ljava/lang/Thread;": function(env) {
return env.thread;
}
},
"java/lang/VMThrowable": {
"fillInStackTrace(Ljava/lang/Throwable;)Ljava/lang/VMThrowable;": function(env, t) {
return t;
}
},
"gnu/classpath/VMSystemProperties": {
"preInit(Ljava/util/Properties;)V": function(env, p) {
var sp = {
"java.version" : "1.5.0",
"java.vendor" : "hewgill.com",
"java.vendor.url" : "http://hewgill.com",
"java.home" : "/none",
"java.vm.specification.version" : "1.0",
"java.vm.specification.vendor" : "Sun Microsystems Inc.",
"java.vm.specification.name" : "Java Virtual Machine Specification",
"java.vm.version" : "1.5.0_1",
"java.vm.vendor" : "hewgill.com",
"java.vm.name" : "Lentil",
"java.specification.version" : "1.5",
"java.specification.vendor" : "Sun Microsystems Inc.",
"java.specification.name" : "Java Platform API Specification",
"java.class.version" : "49.0",
"java.class.path" : ".",
"java.library.path" : ".",
"java.io.tmpdir" : "/tmp",
"java.compiler" : "Lentil",
"java.ext.dirs" : "",
"os.name" : "ECMAscript",
"os.arch" : "ECMAscript",
"os.version" : "1.5",
"file.separator" : "/",
"path.separator" : ":",
"line.separator" : "\n",
"user.name" : "",
"user.home" : "",
"user.dir" : "",
"gnu.cpu.endian" : "little"
};
for (var k in sp) {
loop(startMethod(null, p.__jvm_class, "setProperty(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;", 0, p, [internString(k), internString(sp[k])], [1, 1]));
}
}
},
"gnu/java/nio/VMChannel": {
"initIDs()V": function(env) {
},
"stdin_fd()I": function(env) {
return 0;
},
"stdout_fd()I": function(env) {
return 1;
},
"stderr_fd()I": function(env) {
return 2;
},
"write(ILjava/nio/ByteBuffer;)I": function(env, fd, buffer) {
importClass(java.lang.System);
System.out.print(buffer.backing_buffer.a.slice(buffer.position, buffer.limit));
return buffer.limit - buffer.position;
}
}
}
function indent(i) {
var r = "";
while (i--) {
r += " ";
}
return r;
}
function ClassError(msg) {
this.name = "ClassError";
this.message = msg;
}
function DataInput(bytes) {
this.data = bytes;
this.index = 0;
this.readBytes = function(n) {
var r = this.data.substr(this.index, n);
this.index += n;
return r;
}
this.readInt = function() {
return this.readN(4);
};
this.readUnsignedByte = function() {
return this.readN(1);
}
this.readUnsignedInt = function() {
return this.readN(4);
};
this.readUnsignedLong = function() {
return this.readN(8); // TODO: loss of precision
}
this.readUnsignedShort = function() {
return this.readN(2);
}
this.readN = function(n) {
var r = 0;
while (n > 0) {
r = (r << 8) | (this.data.charCodeAt(this.index) & 0xff);
this.index++;
n--;
}
return r;
}
this.remaining = function() {
return this.data.length - this.index;
}
}
function dump(x, indent) {
if (indent === undefined) {
indent = " ";
}
var r = "";
if (x === undefined) {
r += "undefined";
} else if (x === null) {
r += "null";
} else if (typeof(x) === "function") {
r += "function";
} else if (typeof(x) === "object") {
r += "\n";
for (var p in x) {
v = x[p];
r += indent + p + ": ";
if (p === "__jvm_class") {
r += v;
} else {
r += dump(v, indent + " ");
}
r += "\n";
}
} else {
r = "(" + typeof(x) + ") " + x;
}
return r;
}
function s8(data, index) {
var b = data.charCodeAt(index);
if (b & 0x80) {
return (b & 0x7f) - 0x80;
} else {
return b;
}
}
function u16(data, index) {
var hi = data.charCodeAt(index);
var lo = data.charCodeAt(index + 1);
return (hi << 8) | lo;
}
function s16(data, index) {
var hi = data.charCodeAt(index);
var lo = data.charCodeAt(index + 1);
if (hi & 0x80) {
return (((hi & 0x7f) << 8) | lo) - 0x8000;
} else {
return (hi << 8) | lo;
}
}
function s32(data, index) {
var b0 = data.charCodeAt(index );
var b1 = data.charCodeAt(index + 1);
var b2 = data.charCodeAt(index + 2);
var b3 = data.charCodeAt(index + 3);
if (b0 & 0x80) {
return (((b0 & 0x7f) << 24) | (b1 << 16) | (b2 << 8) | b3) - 0x80000000;
} else {
return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
}
}
function x8(x) {
if (x & 0x80) {
return -0x80 + (x & 0x7f);
} else {
return x & 0x7f;
}
}
function x16(x) {
if (x & 0x8000) {
return -0x8000 + (x & 0x7fff);
} else {
return x & 0x7fff;
}
}
function x32(x) {
if (x & 0x80000000) {
return -0x80000000 + (x & 0x7fffffff);
} else {
return x & 0x7fffffff;
}
}
function x64(x) {
return x;
// TODO: need real 64-bit integer implementation
if (x & 0x8000000000000000) {
return -0x8000000000000000 + (x & 0x7fffffffffffffff);
} else {
return x & 0x7fffffffffffffff;
}
}
function fromIEEE754Single(x) {
if (x === 0x7f800000) {
return Number.POSITIVE_INFINITY;
} else if (x === 0xff800000) {
return Number.NEGATIVE_INFINITY;
} else if ((x >= 0x7f800001 && x <= 0x7fffffff) || (x >= 0xff800001 && x <= 0xffffffff)) {
return NaN;
} else {
var s = (x & 0x80000000) ? -1 : 1;
var e = (x >> 23) & 0xff;
var m = (e == 0) ? (x & 0x7fffff) << 1 : (x & 0x7fffff) | 0x800000;
return s * m * Math.pow(2, e - 150);
}
}
function fromIEEE754Double(b) {
var b0 = b.charCodeAt(0);
var b1 = b.charCodeAt(1);
var b2 = b.charCodeAt(2);
var b3 = b.charCodeAt(3);
var b4 = b.charCodeAt(4);
var b5 = b.charCodeAt(5);
var b6 = b.charCodeAt(6);
var b7 = b.charCodeAt(7);
if (b0 === 0x7f && b1 === 0xf0 && b2 === 0 && b3 === 0 && b4 === 0 && b5 === 0 && b6 === 0 && b7 === 0) {
return Number.POSITIVE_INFINITY;
} else if (b0 === 0xff && b1 === 0xf0 && b2 === 0 && b3 === 0 && b4 === 0 && b5 === 0 && b6 === 0 && b7 === 0) {
return Number.NEGATIVE_INFINITY;
} else if ((b0 == 0x7f && b1 >= 0xf0) || (b1 == 0xff && b1 >= 0xf0)) {
return NaN;
} else {
var s = (b0 & 0x80) ? -1 : 1;
var e = ((b0 & 0x7f) << 4) | (b1 >> 4);
var m;
if (e == 0) {
m = ((b1 & 0x0f) * Math.pow(2, 49)) +
( b2 * Math.pow(2, 41)) +
( b3 * Math.pow(2, 33)) +
( b4 * Math.pow(2, 25)) +
( b5 * Math.pow(2, 17)) +
( b6 * Math.pow(2, 9)) +
( b7 * 2 );
} else {
m = Math.pow(2, 52) +
((b1 & 0x0f) * Math.pow(2, 48)) +
( b2 * Math.pow(2, 40)) +
( b3 * Math.pow(2, 32)) +
( b4 * Math.pow(2, 24)) +
( b5 * Math.pow(2, 16)) +
( b6 * Math.pow(2, 8)) +
( b7 );
}
return s * m * Math.pow(2, e - 1075);
}
}
function disassemble1(pc, opcode) {
var ins = OpcodeName[opcode[0]];
for (var j = 1; j < opcode.length; j++) {
ins += j === 1 ? " " : ", ";
ins += opcode[j];
}
print(" " + pc + " " + ins);
}
function disassemble(code) {
for (var i = 0; i < code.length; i++) {
disassemble1(i, code[i]);
}
}
var Intern = {};
function internString(s) {
var r = Intern[s];
if (r !== undefined) {
r.__jvm_class = JString;
return r;
}
r = {};
r.__jvm_class = JString;
r.value = new JArray("C", s.length, 0);
r.value.s = s;
r.offset = 0;
r.count = s.length;
r.cachedHashCode = 1; // TODO
Intern[s] = r;
return r;
}
function ConstantClass(din) {
this.name_index = din.readUnsignedShort();
this.resolve = function(cp) {
this.name = cp[this.name_index].value();
return this;
}
this.toString = function() {
return "<class " + this.name + ">";
}
this.value = function(cl) {
return cl.getInternalClass(this.name).jclass;
}
}
function ConstantDouble(din) {
this.bytes = fromIEEE754Double(din.readBytes(8));
this.resolve = function(cp) {
return this;
}
this.toString = function() {
return this.bytes;
}
this.value = function() {
return this.bytes;
}
}
function ConstantFieldref(din) {
this.class_index = din.readUnsignedShort();
this.name_and_type_index = din.readUnsignedShort();
this.resolve = function(cp) {
this.classname = cp[this.class_index].resolve(cp).name;
this.name_and_type = cp[this.name_and_type_index];
return this;
}
this.toString = function() {
return "<fieldref " + this.classname + " " + this.name_and_type + ">";
}
}
function ConstantFloat(din) {
this.bytes = fromIEEE754Single(din.readUnsignedInt());
this.resolve = function(cp) {
return this;
}
this.toString = function() {
return this.bytes;
}
this.value = function() {
return this.bytes;
}
}
function ConstantInteger(din) {
this.bytes = din.readUnsignedInt();
this.resolve = function(cp) {
return this;
}
this.toString = function() {
return this.bytes;
}
this.value = function() {
return this.bytes;
}
}
function ConstantInterfaceMethodref(din) {
this.class_index = din.readUnsignedShort();
this.name_and_type_index = din.readUnsignedShort();
this.resolve = function(cp) {
this.classname = cp[this.class_index].resolve(cp).name;
this.name_and_type = cp[this.name_and_type_index];
return this;
}
this.toString = function() {
return "<interfacemethodref " + this.classname + " " + this.name_and_type + ">";
}
}
function ConstantLong(din) {
this.bytes = din.readUnsignedLong();
this.resolve = function(cp) {
return this;
}
this.toString = function() {
return this.bytes;
}
this.value = function() {
return this.bytes;
}
}
function ConstantMethodref(din) {
this.class_index = din.readUnsignedShort();
this.name_and_type_index = din.readUnsignedShort();
this.resolve = function(cp) {
this.classname = cp[this.class_index].resolve(cp).name;
this.name_and_type = cp[this.name_and_type_index];
return this;
}
this.toString = function() {
return "<methodref " + this.classname + " " + this.name_and_type + ">";
}
}
function ConstantNameAndType(din) {
this.name_index = din.readUnsignedShort();
this.descriptor_index = din.readUnsignedShort();
this.resolve = function(cp) {
this.name = cp[this.name_index].value();
this.descriptor = cp[this.descriptor_index].value();
return this;
}
this.toString = function() {
return "<nameandtype " + this.name + " " + this.descriptor + ">";
}
}
function ConstantString(din) {
this.string_index = din.readUnsignedShort();
this.resolve = function(cp) {
this.string = cp[this.string_index].value();
return this;
}
this.toString = function() {
return "<string " + this.string + ">";
}
this.value = function() {
return internString(this.string);
}
}
function ConstantUtf8(din) {
this.length = din.readUnsignedShort();
this.bytes = din.readBytes(this.length);
this.resolve = function(cp) {
return this;
}
this.toString = function() {
return "<utf8 " + this.bytes + ">";
}
this.value = function() {
return this.bytes;
}
}
function ExceptionTableEntry(cls, din) {
this.start_pc = din.readUnsignedShort();
this.end_pc = din.readUnsignedShort();
this.handler_pc = din.readUnsignedShort();
this.catch_type = din.readUnsignedShort();
this.fixup = function(pc_to_index) {
this.start_pc = pc_to_index[this.start_pc];
this.end_pc = pc_to_index[this.end_pc];
this.handler_pc = pc_to_index[this.handler_pc];
}
this.dump = function() {
print(" start_pc:", this.start_pc);
print(" end_pc:", this.end_pc);
print(" handler_pc:", this.handler_pc);
print(" catch_type:", this.catch_type, this.catch_class);
}
}
function LineNumberTableEntry(cls, din) {
this.start_pc = din.readUnsignedShort();
this.line_number = din.readUnsignedShort();
this.fixup = function(pc_to_index) {
this.start_pc = pc_to_index[this.start_pc];
}
this.dump = function() {
print(" start_pc:", this.start_pc);
print(" line_number:", this.line_number);
}
}
var AttributeDecoder = {
"Code": function(cls, din) {
this.cls = cls;
this.max_stack = din.readUnsignedShort();
this.max_locals = din.readUnsignedShort();
this.code_length = din.readUnsignedInt();
this.code = din.readBytes(this.code_length);
this.exception_table_length = din.readUnsignedShort();
this.exception_table = [];
for (var i = 0; i < this.exception_table_length; i++) {
this.exception_table[i] = new ExceptionTableEntry(cls, din);
}
this.attributes_count = din.readUnsignedShort();
this.attributes = [];
this.attribute_by_name = {};
for (var i = 0; i < this.attributes_count; i++) {
var a = new Attribute(cls, din);
this.attributes[i] = a;
this.attribute_by_name[a.attribute_name] = a;
}
this.decodeBytecode = function(cp) {
var r = [];
var code = this.code;
this.pc_to_index = [];
var fixup = [];
for (var i = 0; i < code.length; i++) {
this.pc_to_index[i] = r.length;
var ins;
switch (code.charCodeAt(i)) {
case op_nop:
ins = [op_nop];
break;
case op_aconst_null:
ins = [op_aconst_null];
break;
case op_iconst_m1:
ins = [op_iconst_m1];
break;
case op_iconst_0:
ins = [op_iconst_0];
break;
case op_iconst_1:
ins = [op_iconst_1];
break;
case op_iconst_2:
ins = [op_iconst_2];
break;
case op_iconst_3:
ins = [op_iconst_3];
break;
case op_iconst_4:
ins = [op_iconst_4];
break;
case op_iconst_5:
ins = [op_iconst_5];
break;
case op_lconst_0:
ins = [op_lconst_0];
break;
case op_lconst_1:
ins = [op_lconst_1];
break;
case op_fconst_0:
ins = [op_fconst_0];
break;
case op_fconst_1:
ins = [op_fconst_1];
break;
case op_fconst_2:
ins = [op_fconst_2];
break;
case op_dconst_0:
ins = [op_dconst_0];
break;
case op_dconst_1:
ins = [op_dconst_1];
break;
case op_bipush: