This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm.js
5289 lines (5289 loc) · 204 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
var ie = (navigator.appName == "Microsoft Internet Explorer") ;
if (Array.map == null) {
Array.map = function (a,f) {
if (arguments.length == 1) {
f = a;
a = this;
}
var res = new Array ();
for (i in a) {
res[i] = f(a[i]);
}
return res;
}
}
var instr_name = new Array (
"ACC0", "ACC1", "ACC2", "ACC3", "ACC4", "ACC5", "ACC6", "ACC7", "ACC",
"PUSH", "PUSHACC0", "PUSHACC1", "PUSHACC2", "PUSHACC3", "PUSHACC4",
"PUSHACC5", "PUSHACC6", "PUSHACC7", "PUSHACC", "POP", "ASSIGN", "ENVACC1",
"ENVACC2", "ENVACC3", "ENVACC4", "ENVACC", "PUSHENVACC1", "PUSHENVACC2",
"PUSHENVACC3", "PUSHENVACC4", "PUSHENVACC", "PUSH_RETADDR", "APPLY",
"APPLY1", "APPLY2", "APPLY3", "APPTERM", "APPTERM1", "APPTERM2", "APPTERM3",
"RETURN", "RESTART", "GRAB", "CLOSURE", "CLOSUREREC", "OFFSETCLOSUREM2",
"OFFSETCLOSURE0", "OFFSETCLOSURE2", "OFFSETCLOSURE", "PUSHOFFSETCLOSUREM2",
"PUSHOFFSETCLOSURE0", "PUSHOFFSETCLOSURE2", "PUSHOFFSETCLOSURE",
"GETGLOBAL", "PUSHGETGLOBAL", "GETGLOBALFIELD", "PUSHGETGLOBALFIELD",
"SETGLOBAL", "ATOM0", "ATOM", "PUSHATOM0", "PUSHATOM", "MAKEBLOCK",
"MAKEBLOCK1", "MAKEBLOCK2", "MAKEBLOCK3", "MAKEFLOATBLOCK", "GETFIELD0",
"GETFIELD1", "GETFIELD2", "GETFIELD3", "GETFIELD", "GETFLOATFIELD",
"SETFIELD0", "SETFIELD1", "SETFIELD2", "SETFIELD3", "SETFIELD",
"SETFLOATFIELD", "VECTLENGTH", "GETVECTITEM", "SETVECTITEM",
"GETSTRINGCHAR", "SETSTRINGCHAR", "BRANCH", "BRANCHIF", "BRANCHIFNOT",
"SWITCH", "BOOLNOT", "PUSHTRAP", "POPTRAP", "RAISE", "CHECK_SIGNALS",
"JS_CALL1", "JS_CALL2", "JS_CALL3", "JS_CALL4", "JS_CALL5", "JS_CALLN",
"CONST0", "CONST1", "CONST2", "CONST3", "CONSTINT", "PUSHCONST0",
"PUSHCONST1", "PUSHCONST2", "PUSHCONST3", "PUSHCONSTINT", "NEGINT",
"ADDINT", "SUBINT", "MULINT", "DIVINT", "MODINT", "ANDINT", "ORINT",
"XORINT", "LSLINT", "LSRINT", "ASRINT", "EQ", "NEQ", "LTINT", "LEINT",
"GTINT", "GEINT", "OFFSETINT", "OFFSETREF", "ISINT", "GETMETHOD", "BEQ",
"BNEQ", "BLTINT", "BLEINT", "BGTINT", "BGEINT", "ULTINT", "UGEINT",
"BULTINT", "BUGEINT", "GETPUBMET", "GETDYNMET", "STOP", "EVENT", "BREAK"
);
function print_instr (code, ofs) {
console.debug (instr_name[code.get (ofs)]);
}
function is_in (v, a) {
for (var i in a) {
if (v == a[i])
return true;
}
return false;
}
function index_in (v, a) {
for (var i in a) {
if (v == a[i])
return i;
}
return -1;
}
function sprintf () {
if ((arguments.length >= 1) && (typeof arguments[0] == "string")) {
var narg = 1;
var format = arguments[0];
var res = "";
var i = 0;
while (i < format.length) {
if (format[i] == '%') {
i++;
if (i >= format.length) {
throw new Error ("printf: bad format");
} else {
if (format[i] == '%') {
res += "%";
i++;
break;
} else {
var pad = ' ';
if (!is_in(format[i], ['d','f','x','X','r'])) {
pad = format[i++];
if (i >= format.length)
throw new Error ("printf: bad format");
}
var len = 0;
var nums = ['0','1','2','3','4','5','6','7','8','9'];
while (is_in(format[i], nums)) {
len = len * 10 + index_in (format[i++], nums);
if (i >= format.length)
throw new Error ("printf: bad format");
}
if (narg >= arguments.length)
throw new Error ("printf: bad format");
var fmt;
switch (format[i++]) {
case 'r': {
var m = 10;
if (i < format.length) {
if (format[i] == '{')
i++;
if (i >= format.length)
throw new Error ("printf: bad format");
while (format[i] != '}') {
m = m * 10 + index_in (format[i++], nums);
if (i >= format.length)
throw new Error ("printf: bad format");
}
}
fmt = repr (arguments[narg++], m);
break;
}
case 'd' :
case 'f' :
fmt =
Number (arguments[narg++])
.toString (10);
break;
case 'x':
fmt =
Number (arguments[narg++])
.toString (16).toUpperCase ();
break;
case 'X':
fmt=
Number (arguments[narg++])
.toString (16).toUpperCase ();
break;
default :
throw new Error ("printf: bad format");
}
for (var j = 0;j < len - fmt.length;j++)
res += pad;
res += fmt;
}
}
} else {
res += format[i++];
}
}
} else {
throw new Error ("printf: bad format");
}
return res;
}
function char_from_event (e) {
if (e.charCode)
return e.charCode;
var c = e.keyCode;
if (e.shiftKey)
return e.keyCode;
if (e.keyCode >= e.DOM_VK_A && e.keyCode <= e.DOM_VK_Z)
return e.keyCode + ("a".charCodeAt(0) - "A".charCodeAt(0));
}
function set_cookie ( name, value) {
document.cookie = name + "=" + escape (value);
}
function get_cookie (name) {
if (document.cookie) {
var i = document.cookie.indexOf(name);
if ( i != -1) {
var s = (document.cookie.indexOf( "=", i) + 1);
var e = document.cookie.indexOf( ";", i);
if (e == -1) e = document.cookie.length;
return unescape(document.cookie.slice(s, e));
}
}
return null;
}
function make_inherit (daughter, mother) {
function tmp() {};
tmp.prototype = mother.prototype;
daughter.prototype = new tmp();
daughter.prototype.constructor = daughter;
daughter.prototype.mother_constructor = mother;
if (mother.base)
mother.prototype.base = mother.base;
daughter.base = mother.prototype;
}
function http_get (url, error) {
var xmlhttp=false;
if (ie) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
throw new Error ("Unsupported Internet Explorer");
}
}
} else {
if (typeof XMLHttpRequest != 'undefined')
xmlhttp = new XMLHttpRequest();
else
throw new Error ("Unsupported Browser");
}
try {
xmlhttp.open("GET", url, false);
xmlhttp.send(null);
} catch (e) {
throw new Error ("unable to load file " + url + ": " + e.message);
}
return xmlhttp.responseText;
}
function uudecode (s) {
var out = new Array ();
var n = 0;
var as = s.split ("\n");
for (var i = 1;i < as.length;i++) {
if (as[i].length > 1 && as[i] != "end") {
var len = as[i].charCodeAt(0) - 0x20;
var k = 0;
for (var j = 0;j < len / 3;j++) {
var b0 = as[i].charCodeAt(1 + j * 4 + 0),
b1 = as[i].charCodeAt(1 + j * 4 + 1),
b2 = as[i].charCodeAt(1 + j * 4 + 2),
b3 = as[i].charCodeAt(1 + j * 4 + 3);
if (k++ < len) out[n++] =
(((b0 - 0x20) & 0x3F) << 2 & 0xFC)
| (((b1 - 0x20) & 0x3F) >> 4 & 0x03);
if (k++ < len) out[n++] =
(((b1 - 0x20) & 0x3F) << 4 & 0xF0)
| (((b2 - 0x20) & 0x3F) >> 2 & 0x0F);
if (k++ < len) out[n++] =
(((b2 - 0x20) & 0x3F) << 6 & 0xC0)
| ((b3 - 0x20) & 0x3F);
}
}
}
return out;
}
function ult (a,b) {
return ((a >= 0) ? ((b < 0) || (a < b)) : ((b < 0) && (a > b)));
}
var RT = [];
function Block(size, tag) {
this.size = size;
this.tag = tag;
this.content = [];
this.offset = 0;
}
Block.prototype.get = function (i) {
return this.content[this.offset + i];
}
Block.prototype.set = function (i, v) {
this.content[this.offset + i] = v;
}
Block.prototype.shift = function (o) {
var nsize = this.size - o >= 0 ? this.size - o : 0;
var b = new Block (nsize, this.tag);
b.content = this.content;
b.offset = this.offset + o;
return b;
}
var ATOM = (new Block (0, 0));
function pair (v0, v1) {
var b = new Block (2, 0);
b.set (0, v0);
b.set (1, v1);
return b;
}
function singleton (v0) {
var b = new Block (1, 0);
b.set (0, v0);
return b;
}
function box_abstract (v0) {
var b = new Block (1, 251);
b.set (0, v0);
return b;
}
var nil = 0;
var cons = pair;
function float_of_int (x) {
var b = new Block (1, 253);
b.set (0, Number (x));
return b;
}
function box_float (x) {
var b = new Block (1, 253);
b.set (0, Number (x));
return b;
}
function float_of_bytes (bytes) {
var sign = ((bytes[0] >> 7) == 1);
var exponent = (((bytes[0] & 0x7F) << 4) | (bytes[1] >> 4 )) - 1023;
var ba = [];
for (var b = 1;b < 8;b++)
for (var d = 0;d < 8;d++)
ba[(b - 1) * 8 + d - 4] = (((bytes[b] >> (7 - d)) & 1) == 1);
var m = Number (1);
for (var i = 0;i < 52;i++)
if (ba[i])
m += Math.pow (2, -(i + 1));
return box_float ((sign ? (-1) : 1) * m * Math.pow (2, exponent));
}
function bytes_of_float (x) {
var x = x.get (0);
var e = Math.ceil (Math.log (Math.abs (x)) / Math.log (2));
var m = Math.abs (x * Math.pow (2, -e)) * 2 - 1;
e += 1022;
var bits = [];
bits[0] = (x > 0);
for (var i = 0;i <= 52 ; i++) {
bits [11 + i] = (m >= 1);
m = (m - Math.floor (m)) * 2;
}
for (var i = 0;i <= 10 ; i++) {
bits [11 - i] = (((e >>> i) & 1) == 1);
}
var bytes = [0,0,0,0,0,0,0,0];
for (var i = 0;i < 8 ; i++) {
for (var j = 0;j < 8 ; j++) {
bytes[i] = (bytes[i] * 2) | (bits[8 * i + j] ? 1 : 0);
}
}
return bytes;
}
function encode_utf8 (s) {
var v = (new Block (0, 252));
var vi = 0;
for (var i = 0; i < s.length; i++) {
var c = s.charCodeAt (i);
if (c < 0x80) {
v.size++;
v.set (vi++, c);
} else {
if(c < 0x800) {
v.size += 2;
v.set (vi++, (c >> 0x06) | 0xC0);
v.set (vi++, (c & 0x3F) | 0x80);
} else {
v.set (vi++, (c >> 12) | 0xE0);
v.set (vi++, ((c >> 6) & 0x3F) | 0x80);
v.set (vi++, (c & 0x3F) | 0x80);
}
}
}
v.size++;
v.set (vi, 0);
return v;
}
function decode_utf8 (v) {
var s = "";
for (var i = 0;i < v.size - 1;) {
var c1 = v.get (i++);
if (c1 < 0x80) {
s += String.fromCharCode(c1);
} else {
if((c1 >= 0xC0) && (c1 < 0xE0)) {
var c2 = v.get (i++);
s += String.fromCharCode (((c1 & 0x1F) << 6) | (c2 & 0x3F));
} else {
var c2 = v.get (i++);
var c3 = v.get (i++);
s += String.fromCharCode(((c1 & 0xF) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F));
}
}
}
return s;
}
var utf8_enabled = 1;
RT.caml_js_enable_utf8 = function (v) {
utf8_enabled = v;
return 0;
}
RT.caml_js_utf8_enabled = function () {
return utf8_enabled;
}
function value_from_string (s) {
if (utf8_enabled == 0) {
var b = (new Block (s.length + 1, 252));
for (var i = 0;i < s.length;i++) {
b.set(i,s.charCodeAt (i));
}
b.set(i, 0);
return b;
} else {
return encode_utf8 (s);
}
}
function string_from_value (v) {
if (utf8_enabled == 0) {
var s = "";
for (var i = 0;i < v.size - 1;i++) {
s += String.fromCharCode (v.get (i));
}
return s;
} else {
return decode_utf8 (v);
}
}
function string_array (a) {
var b = new Block (a.length);
for (var i = 0;i < a.length;i++)
b.set (i, value_from_string (a[i]));
return b;
}
function mk_array_from_js (s) {
var b = (new Block (s.length, 0));
for (var i = 0;i < s.length;i++) {
b.set(i,s[i]);
}
return b;
}
function repr (v, limit) {
var s = "";
function string_repr_rec (v) {
if ((!(v instanceof Block))) {
s += sprintf ("0x%X", v);
} else {
switch (v.tag) {
case 252:
s += "\"" + string_from_value (v) + "\"";
break;
case 253:
s += v.get (0).toExponential ();
break;
default: {
s += sprintf ("[(0x%02X) ", v.tag);
for (var i = 0;i < v.size - 1 && i < limit;i++) {
string_repr_rec (v.get (i));
s += ", ";
}
if (i >= limit) {
s += "...";
} else {
string_repr_rec (v.get (i));
}
s += "]";
}
}
}
}
string_repr_rec (v);
return s;
}
var customs = new Array ();
function register_custom (ops) {
customs[ops.id] = ops;
}
function find_custom (id) {
return customs[id];
}
function mk_custom (ops, val) {
var b = new Block (2, 255);
b.set (0, ops);
b.set (1, val);
return b;
}
int32_ops = {
id : "_i",
compare : function (a,b) {
return (a - b);
},
hash : function (a) {
return a;
},
serialize : function (writer) {
throw new Error ("int32 not supported yet");
},
deserialize : function (reader) {
return mk_custom (int32_ops, reader.read32u ());
}
};
register_custom (int32_ops);
RT.caml_int32_neg = function (a) {
var a = a.get (1);
return mk_custom (int32_ops, -a);
}
RT.caml_int32_add = function (va,vb) {
var a = va.get (1);
var b = vb.get (1) ;
return mk_custom (int32_ops, a + b);
}
RT.caml_int32_sub = function (a,b) {
var a = a.get (1);
var b = b.get (1) ;
return mk_custom (int32_ops, a - b);
}
RT.caml_int32_mul = function (a,b) {
var a = a.get (1);
var b = b.get (1) ;
return mk_custom (int32_ops, a * b);
}
RT.caml_int32_div = function (a,b) {
var a = a.get (1);
var b = b.get (1) ;
return mk_custom (int32_ops, a / b);
}
RT.caml_int32_mod = function (a,b) {
var a = a.get (1);
var b = b.get (1) ;
return mk_custom (int32_ops, a % b);
}
RT.caml_int32_and = function (a,b) {
var a = a.get (1);
var b = b.get (1) ;
return mk_custom (int32_ops, a & b);
}
RT.caml_int32_or = function (a,b) {
var a = a.get (1);
var b = b.get (1) ;
return mk_custom (int32_ops, a | b);
}
RT.caml_int32_xor = function (a,b) {
var a = a.get (1);
var b = b.get (1) ;
return mk_custom (int32_ops, a ^ b);
}
RT.caml_int32_lsl = function (a,b) {
var a = a.get (1);
return mk_custom (int32_ops, a << b);
}
RT.caml_int32_asr = function (a,b) {
var a = a.get (1);
return mk_custom (int32_ops, a >> b);
}
RT.caml_int32_lsr = function (a,b) {
var a = a.get (1);
return mk_custom (int32_ops, a >>> b);
}
RT.caml_int32_of_int = function (a) {
return mk_custom (int32_ops, a);
}
RT.caml_int32_to_int = function (a) {
return a.get (1);
}
RT.caml_int32_format = function (f, v) {
return RT.caml_format_int.call (this, f, v.get (1));
}
RT.caml_int32_of_string = function (s) {
return mk_custom (int32_ops, RT.caml_int_of_string.call (this, s));
}
nativeint_ops = {
id : "_n",
compare : function (a,b) {
return (a - b);
},
hash : function (a) {
return a;
},
serialize : function (writer) {
throw new Error ("nativeint not supported yet");
},
deserialize : function (reader) {
var l = reader.read8u ();
if (l == 1)
return mk_custom (nativeint_ops, reader.read32u ());
else
throw new Error ("> 32 bits native int not supported yet");
}
};
register_custom (nativeint_ops);
RT.caml_nativeint_shift_left = function (x, s) {
return (x << s);
}
RT.caml_nativeint_sub = function (a,b) {
return (a - b);
}
function Int64 () {}
int64_compare = function (a, b) {
var t = int64_sub (a, b).get (1).t;
var is_zero = true;
for (var i = 0;i < 8;i++)
if (t[i] != 0) is_zero = false;
if (is_zero)
return 0;
return (((t[0] >> 7) == 1) ? -1 : 1);
}
int64_add = function (a, b) {
a = a.get (1);
b = b.get (1);
var r = new Int64 ();
r.t = [];
var carry = 0;
for (var i = 0;i < 8;i++) {
r.t[i] = a.t[i] + b.t[i] + carry;
carry = r.t[i] >> 8;
r.t[i] &= 0xFF;
}
return mk_custom (int64_ops, r);
}
int64_neg = function (a) {
a = a.get (1);
var r = new Int64 ();
r.t = [];
for (var i = 0;i < 8;i++)
r.t[i] = a.t[i] ^ 0xFF;
var carry = 1;
for (var i = 0;i < 8;i++) {
r.t[i] = a.t[i] +carry;
carry = r.t[i] >> 8;
r.t[i] &= 0xFF;
}
return mk_custom (int64_ops, r);
}
int64_sub = function (a, b) {
return int64_add (a, int64_neg (b));
}
int64_of_int = function (x) {
var r = new Int64 ();
r.t = [];
for (var i = 0;i < INTEGER_SIZE / 8;i++)
r.t[7 - i] = (x >> (i * 8)) & 0xFF;
if (x >> (INTEGER_SIZE - 1) == 1)
for (var i = INTEGER_SIZE / 8 - 1;i < 8;i++)
r.t[7 - i] = 0xFF;
return mk_custom (int64_ops, r);
}
int64_of_bytes = function (bytes) {
var r = new Int64 ();
r.t = [];
for (var i = 0;i < 8;i++)
r.t[i] = bytes[i];
return mk_custom (int64_ops, r);
}
int64_of_bytes_le = function (bytes) {
var r = new Int64 ();
r.t = [];
for (var i = 0;i < 8;i++)
r.t[i] = bytes[7 - i];
return mk_custom (int64_ops, r);
}
int64_to_bytes = function (a) {
return a.get (1).t;
}
int64_ops = {
id : "_j",
compare : function (a,b) {
int64_compare (a, b);
},
hash : function (a) {
return 0;
},
serialize : function (v, writer) {
var t = int64_to_bytes (v.get (1));
for (var j = 0;j < 8;j++)
writer.write (8, t[j]);
writer.size_32 += 2 + ((8 + 3) >> 2);
writer.size_64 += 2 + ((8 + 7) >> 3);
},
deserialize : function (reader) {
var t = [];
for (var j = 0;j < 8;j++)
t[j] = reader.read8u();
return int64_of_bytes (t);
}
};
register_custom (int64_ops);
var INTEXT_MAGIC_NUMBER = 0x8495A6BE, PREFIX_SMALL_BLOCK = 0x80,
PREFIX_SMALL_INT = 0x40, PREFIX_SMALL_STRING = 0x20;
var CODE_INT8 = 0x00, CODE_INT16 = 0x01,
CODE_INT32 = 0x02, CODE_INT64 = 0x03,
CODE_SHARED8 = 0x04, CODE_SHARED16 = 0x05,
CODE_SHARED32 = 0x06, CODE_BLOCK32 = 0x08,
CODE_BLOCK64 = 0x13, CODE_STRING8 = 0x09,
CODE_STRING32 = 0x0A, CODE_DOUBLE_BIG = 0x0B,
CODE_DOUBLE_LITTLE = 0x0C, CODE_DOUBLE_ARRAY8_BIG = 0x0D,
CODE_DOUBLE_ARRAY8_LITTLE = 0x0E, CODE_DOUBLE_ARRAY32_BIG = 0x0F,
CODE_DOUBLE_ARRAY32_LITTLE = 0x07, CODE_CODEPOINTER = 0x10,
CODE_INFIXPOINTER = 0x11, CODE_CUSTOM = 0x12;
function Reader (chunk) {
this.chunk = chunk;
this.chunk_idx = 0;
}
Reader.prototype.read8u = function () {
return this.chunk[this.chunk_idx++];
}
Reader.prototype.read8s = function () {
var r = this.chunk[this.chunk_idx++];
if (r & 0x80)
return r - 256;
else
return r;
}
Reader.prototype.read16u = function () {
var r = this.chunk[this.chunk_idx++];
r = (r << 8) | this.chunk[this.chunk_idx++];
return r;
}
Reader.prototype.read16s = function () {
var r = this.chunk[this.chunk_idx++];
r = (r << 8) | this.chunk[this.chunk_idx++];
if (r & 0x8000)
return r - 65536;
else
return r;
}
Reader.prototype.read32u = function () {
var r = this.chunk[this.chunk_idx++];
r = (r << 8) | this.chunk[this.chunk_idx++];
r = (r << 8) | this.chunk[this.chunk_idx++];
r = (r << 8) | this.chunk[this.chunk_idx++];
return r;
}
Reader.prototype.read32s = function () {
var r = this.chunk[this.chunk_idx++];
r = (r << 8) | this.chunk[this.chunk_idx++];
r = (r << 8) | this.chunk[this.chunk_idx++];
r = (r << 8) | this.chunk[this.chunk_idx++];
return r;
}
function input_size (chunk, error) {
var reader = new Reader (chunk);
if (reader.read8u () != 0x84
|| reader.read8u () != 0x95
|| reader.read8u () != 0xA6
|| reader.read8u () != 0xBE){
error ("ill-formed message (bad magic)");
}
var block_len = reader.read32u ();
return block_len;
}
function input_val (chunk, error) {
var reader = new Reader (chunk);
if (reader.read8u () != 0x84
|| reader.read8u () != 0x95
|| reader.read8u () != 0xA6
|| reader.read8u () != 0xBE){
error ("ill-formed message (bad magic)");
}
var block_len = reader.read32u ();
var num_objects = reader.read32u ();
var size_32 = reader.read32u ();
var size_64 = reader.read32u ();
var intern_obj_table = [];
var obj_counter = 0;
function intern_rec () {
var code = reader.read8u ();
if (code >= PREFIX_SMALL_INT) {
if (code >= PREFIX_SMALL_BLOCK) {
var tag = code & 0xF;
var size = (code >> 4) & 0x7;
var v = (new Block (size, tag));
intern_obj_table[obj_counter++] = v;
for(var d = 0; d < size; d++)
v.set (d, intern_rec ());
return v;
} else {
return (code & 0x3F);
}
} else {
if (code >= PREFIX_SMALL_STRING) {
var len = (code & 0x1F);
var v = (new Block (len + 1, 252));
intern_obj_table[obj_counter++] = v;
for (var d = 0;d < len;d++) {
v.set (d, reader.read8u ());
}
v.set (len, 0);
return v;
} else {
switch(code) {
case CODE_INT8:
return reader.read8s ();
case CODE_INT16:
return reader.read16s ();
case CODE_INT32:
return reader.read32s ();
case CODE_INT64:
var t = [];
for (var j = 0;j < 8;j++)
t[j] = reader.read8u();
return int64_of_bytes (t);
case CODE_SHARED8: {
var ofs = reader.read8u ();
return intern_obj_table[obj_counter - ofs];
}
case CODE_SHARED16: {
var ofs = reader.read16u ();
return intern_obj_table[obj_counter - ofs];
}
case CODE_SHARED32: {
var ofs = reader.read32u ();
return intern_obj_table[obj_counter - ofs];
}
case CODE_BLOCK32: {
var header = reader.read32u ();
var tag = header & 0xFF;
var size = header >> 10;
var v = (new Block (size, tag));
intern_obj_table[obj_counter++] = v;
for(var d = 0; d < size; d++)
v.set (d, intern_rec ());
return v;
}
case CODE_BLOCK64:
error ("data block too large");
break;
case CODE_STRING8: {
var len = reader.read8u();
var v = (new Block (len + 1, 252));
intern_obj_table[obj_counter++] = v;
for (var d = 0;d < len;d++) {
v.set (d, reader.read8u ());
}
v.set (len, 0);
return v;
}
case CODE_STRING32: {
var len = reader.read32u();
var v = (new Block (len + 1, 252));
intern_obj_table[obj_counter++] = v;
for (var d = 0;d < len;d++) {
v.set (d, reader.read8u ());
}
v.set (len, 0);
return v;
}
case CODE_DOUBLE_LITTLE: {
var t = [];
for (var i = 0;i < 8;i++)
t[7 - i] = reader.read8u();
return float_of_bytes (t);
}
case CODE_DOUBLE_BIG: {
var t = [];
for (var i = 0;i < 8;i++)
t[i] = reader.read8u();
return float_of_bytes (t);
}
case CODE_DOUBLE_ARRAY8_LITTLE: {
var len = reader.read8u();
var v = (new Block (len, 254));
for (var i = 0;i < len;i++) {
var t = [];
for (var j = 0;j < 8;j++)
t[7 - j] = reader.read8u();
v.set (i, float_of_bytes (t).get (0));
}
return v;
}
case CODE_DOUBLE_ARRAY8_BIG: {
var len = reader.read8u();
var v = (new Block (len, 254));
for (var i = 0;i < len;i++) {
var t = [];
for (var j = 0;j < 8;j++)
t[j] = reader.read8u();
v.set (i, float_of_bytes (t).get (0));
}
return v;
}
case CODE_DOUBLE_ARRAY32_LITTLE: {
var len = reader.read32u();
var v = (new Block (len, 254));
for (var i = 0;i < len;i++) {
var t = [];
for (var j = 0;j < 8;j++)
t[7 - j] = reader.read8u();
v.set (i, float_of_bytes (t).get (0));
}
return v;
}
case CODE_DOUBLE_ARRAY32_BIG: {
var len = reader.read32u();
var v = (new Block (len, 254));
for (var i = 0;i < len;i++) {
var t = [];
for (var j = 0;j < 8;j++)
t[j] = reader.read8u();
v.set (i, float_of_bytes (t).get (0));
}
return v;
}
case CODE_CODEPOINTER:
return reader.read32u ();
case CODE_INFIXPOINTER: {
var ofs = reader.read32u ();
var clos = intern_rec ();
return clos.shift (ofs);
}
case CODE_CUSTOM: {
var s = "";
var si = reader.read8u ();
while (si != 0) {
s += String.fromCharCode (si);
si = reader.read8u ();
}
var c = find_custom (s);
if (!c)
error ("unknown custom identifier " + s);
else
return c.deserialize (reader);
}
default:
error ("ill-formed message (" + code + ")");
}
}
}
}
var v = intern_rec ();
return v;
}
RT.caml_input_value_from_string = function (s, ofs) {
return input_val (s.content.slice (s.offset + ofs, s.size),caml_failwith);
}
RT.caml_marshal_data_size = function (s, ofs) {
return input_size (s.content.slice (s.offset + ofs, s.size),caml_failwith);
}
function Writer () {
this.chunk = [];
this.chunk_idx = 20;
this.block_len = 0;
this.obj_counter = 0;
this.size_32 = 0;
this.size_64 = 0;
}
Writer.prototype.write = function (size, value) {
for (var i = size - 8;i >= 0;i -= 8)
this.chunk[this.chunk_idx++] = (value >> i) & 0xFF;
}
Writer.prototype.write_code = function (size, code, value) {
this.chunk[this.chunk_idx++] = code;
for (var i = size - 8;i >= 0;i -= 8)
this.chunk[this.chunk_idx++] = (value >> i) & 0xFF;
}
Writer.prototype.finalize = function () {
this.block_len = this.chunk_idx - 20;
this.chunk_idx = 0;
this.write (32, 0x8495A6BE);
this.write (32, this.block_len);
this.write (32, this.num_objects);
this.write (32, this.size_32);
this.write (32, this.size_64);
return this.chunk;
}
function output_val (v, error) {
var writer = new Writer ()
function extern_rec (v) {
if ((!(v instanceof Block))) {
if (v >= 0 && v < 0x40) {
writer.write (8, PREFIX_SMALL_INT + v);
} else {
if (v >= -(1 << 7) && v < (1 << 7)) {
writer.write_code(8, CODE_INT8, v);
} else {
if (v >= -(1 << 15) && v < (1 << 15)) {
writer.write_code(16, CODE_INT16, v);
} else {
writer.write_code(32, CODE_INT32, v);
}
}
}
} else {
if (v.size == 0) {
if (v.tag < 16)
writer.write (8, PREFIX_SMALL_BLOCK + v.tag);
else
writer.write_code (32, CODE_BLOCK32, (((v).size << 10) | (v).tag));
return;
}
if (v.dejavu) {
var d = writer.obj_counter - v.dejavu_location;
if (d < 0x100) {
writer.write_code (8, CODE_SHARED8, d);
} else {
if (d < 0x10000) {
writer.write_code (16, CODE_SHARED16, d);
} else {
writer.write_code (32, CODE_SHARED32, d);
}
}
return;
}
switch(v.tag) {
case 252: {
var len = v.size - 1;
if (len < 0x20) {
writer.write (8, PREFIX_SMALL_STRING + len);
} else if (len < 0x100) {
writer.write_code (8, CODE_STRING8, len);
} else {
writer.write_code (32, CODE_STRING32, len);
}
for (var i = 0;i < le;i++)
writer.write (8, v.get (i));
writer.size_32 += 1 + (len + 4) / 4;
writer.size_64 += 1 + (len + 8) / 8;
v.dejavu = true;
v.dejavu_location = writer.num_objects++;
break;
}
case 253: {
writer.write (8, CODE_DOUBLE_BIG);
var bytes = bytes_of_float (v);
for (var i = 0;i < 8;i++)
writer.write (8, bytes[i]);
writer.size_32 += 1 + 2;
writer.size_64 += 1 + 1;
v.dejavu = true;
v.dejavu_location = writer.num_objects++;
break;
}
case 254: {
if (v.size < 0x100)
writer.write_code (8, CODE_DOUBLE_ARRAY8_BIG, v.size);
else
writer.write_code (32, CODE_DOUBLE_ARRAY8_BIG, v.size);
for (var j = 0;j < v.size;j++) {
var bytes = bytes_of_float (v.get (j));
for (var i = 0;i < 8;i++)