-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathcaml_obj.js
337 lines (310 loc) · 8.51 KB
/
caml_obj.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
'use strict';
var Block = require("./block.js");
var Caml_primitive = require("./caml_primitive.js");
var Caml_builtin_exceptions = require("./caml_builtin_exceptions.js");
function caml_obj_block(tag, size) {
var v = new Array(size);
v.tag = tag;
return v;
}
function caml_obj_dup(x) {
var len = x.length | 0;
var v = new Array(len);
for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){
v[i] = x[i];
}
v.tag = x.tag | 0;
return v;
}
function caml_obj_truncate(x, new_size) {
var len = x.length | 0;
if (new_size <= 0 || new_size > len) {
throw [
Caml_builtin_exceptions.invalid_argument,
"Obj.truncate"
];
} else if (len !== new_size) {
for(var i = new_size ,i_finish = len - 1 | 0; i <= i_finish; ++i){
x[i] = 0;
}
x.length = new_size;
return /* () */0;
} else {
return 0;
}
}
function caml_lazy_make_forward(x) {
return Block.__(250, [x]);
}
function caml_update_dummy(x, y) {
var len = y.length | 0;
for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){
x[i] = y[i];
}
var y_tag = y.tag | 0;
if (y_tag !== 0) {
x.tag = y_tag;
return /* () */0;
} else {
return 0;
}
}
function caml_compare(_a, _b) {
while(true) {
var b = _b;
var a = _a;
if (a === b) {
return 0;
} else {
var a_type = typeof a;
var b_type = typeof b;
if (a_type === "string") {
return Caml_primitive.caml_string_compare(a, b);
} else {
var is_a_number = +(a_type === "number");
var is_b_number = +(b_type === "number");
if (is_a_number !== 0) {
if (is_b_number !== 0) {
return Caml_primitive.caml_int_compare(a, b);
} else {
return -1;
}
} else if (is_b_number !== 0) {
return 1;
} else if (a_type === "boolean" || a_type === "undefined" || a === null) {
var x = a;
var y = b;
if (x === y) {
return 0;
} else if (x < y) {
return -1;
} else {
return 1;
}
} else if (a_type === "function" || b_type === "function") {
throw [
Caml_builtin_exceptions.invalid_argument,
"compare: functional value"
];
} else {
var tag_a = a.tag | 0;
var tag_b = b.tag | 0;
if (tag_a === 250) {
_a = a[0];
continue ;
} else if (tag_b === 250) {
_b = b[0];
continue ;
} else if (tag_a === 248) {
return Caml_primitive.caml_int_compare(a[1], b[1]);
} else if (tag_a === 251) {
throw [
Caml_builtin_exceptions.invalid_argument,
"equal: abstract value"
];
} else if (tag_a !== tag_b) {
if (tag_a < tag_b) {
return -1;
} else {
return 1;
}
} else {
var len_a = a.length | 0;
var len_b = b.length | 0;
if (len_a === len_b) {
var a$1 = a;
var b$1 = b;
var _i = 0;
var same_length = len_a;
while(true) {
var i = _i;
if (i === same_length) {
return 0;
} else {
var res = caml_compare(a$1[i], b$1[i]);
if (res !== 0) {
return res;
} else {
_i = i + 1 | 0;
continue ;
}
}
};
} else if (len_a < len_b) {
var a$2 = a;
var b$2 = b;
var _i$1 = 0;
var short_length = len_a;
while(true) {
var i$1 = _i$1;
if (i$1 === short_length) {
return -1;
} else {
var res$1 = caml_compare(a$2[i$1], b$2[i$1]);
if (res$1 !== 0) {
return res$1;
} else {
_i$1 = i$1 + 1 | 0;
continue ;
}
}
};
} else {
var a$3 = a;
var b$3 = b;
var _i$2 = 0;
var short_length$1 = len_b;
while(true) {
var i$2 = _i$2;
if (i$2 === short_length$1) {
return 1;
} else {
var res$2 = caml_compare(a$3[i$2], b$3[i$2]);
if (res$2 !== 0) {
return res$2;
} else {
_i$2 = i$2 + 1 | 0;
continue ;
}
}
};
}
}
}
}
}
};
}
function caml_equal(_a, _b) {
while(true) {
var b = _b;
var a = _a;
if (a === b) {
return /* true */1;
} else {
var a_type = typeof a;
if (a_type === "string" || a_type === "number" || a_type === "boolean" || a_type === "undefined" || a === null) {
return /* false */0;
} else {
var b_type = typeof b;
if (a_type === "function" || b_type === "function") {
throw [
Caml_builtin_exceptions.invalid_argument,
"equal: functional value"
];
} else if (b_type === "number" || b_type === "undefined" || b === null) {
return /* false */0;
} else {
var tag_a = a.tag | 0;
var tag_b = b.tag | 0;
if (tag_a === 250) {
_a = a[0];
continue ;
} else if (tag_b === 250) {
_b = b[0];
continue ;
} else if (tag_a === 248) {
return +(a[1] === b[1]);
} else if (tag_a === 251) {
throw [
Caml_builtin_exceptions.invalid_argument,
"equal: abstract value"
];
} else if (tag_a !== tag_b) {
return /* false */0;
} else {
var len_a = a.length | 0;
var len_b = b.length | 0;
if (len_a === len_b) {
var a$1 = a;
var b$1 = b;
var _i = 0;
var same_length = len_a;
while(true) {
var i = _i;
if (i === same_length) {
return /* true */1;
} else if (caml_equal(a$1[i], b$1[i])) {
_i = i + 1 | 0;
continue ;
} else {
return /* false */0;
}
};
} else {
return /* false */0;
}
}
}
}
}
};
}
function caml_equal_null(x, y) {
if (y !== null) {
return caml_equal(x, y);
} else {
return +(x === y);
}
}
function caml_equal_undefined(x, y) {
if (y !== undefined) {
return caml_equal(x, y);
} else {
return +(x === y);
}
}
function caml_equal_nullable(x, y) {
if (y == null) {
return +(x === y);
} else {
return caml_equal(x, y);
}
}
function caml_notequal(a, b) {
return 1 - caml_equal(a, b);
}
function caml_greaterequal(a, b) {
return +(caml_compare(a, b) >= 0);
}
function caml_greaterthan(a, b) {
return +(caml_compare(a, b) > 0);
}
function caml_lessequal(a, b) {
return +(caml_compare(a, b) <= 0);
}
function caml_lessthan(a, b) {
return +(caml_compare(a, b) < 0);
}
function caml_min(x, y) {
if (caml_compare(x, y) <= 0) {
return x;
} else {
return y;
}
}
function caml_max(x, y) {
if (caml_compare(x, y) >= 0) {
return x;
} else {
return y;
}
}
exports.caml_obj_block = caml_obj_block;
exports.caml_obj_dup = caml_obj_dup;
exports.caml_obj_truncate = caml_obj_truncate;
exports.caml_lazy_make_forward = caml_lazy_make_forward;
exports.caml_update_dummy = caml_update_dummy;
exports.caml_compare = caml_compare;
exports.caml_equal = caml_equal;
exports.caml_equal_null = caml_equal_null;
exports.caml_equal_undefined = caml_equal_undefined;
exports.caml_equal_nullable = caml_equal_nullable;
exports.caml_notequal = caml_notequal;
exports.caml_greaterequal = caml_greaterequal;
exports.caml_greaterthan = caml_greaterthan;
exports.caml_lessthan = caml_lessthan;
exports.caml_lessequal = caml_lessequal;
exports.caml_min = caml_min;
exports.caml_max = caml_max;
/* No side effect */