forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaml_obj.js
485 lines (463 loc) · 13.8 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
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
'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;
}
}
var for_in = function (o,foo){
for (var x in o) { foo(x) }
};
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;
var exit = 0;
switch (a_type) {
case "boolean" :
if (b_type === "boolean") {
return Caml_primitive.caml_bool_compare(a, b);
} else {
exit = 1;
}
break;
case "function" :
if (b_type === "function") {
throw [
Caml_builtin_exceptions.invalid_argument,
"compare: functional value"
];
} else {
exit = 1;
}
break;
case "number" :
if (b_type === "number") {
return Caml_primitive.caml_int_compare(a, b);
} else {
exit = 1;
}
break;
case "string" :
if (b_type === "string") {
return Caml_primitive.caml_string_compare(a, b);
} else {
return 1;
}
case "undefined" :
return -1;
default:
exit = 1;
}
if (exit === 1) {
switch (b_type) {
case "string" :
return -1;
case "undefined" :
return 1;
default:
if (a_type === "boolean") {
return 1;
} else if (b_type === "boolean") {
return -1;
} else if (a_type === "function") {
return 1;
} else if (b_type === "function") {
return -1;
} else if (a_type === "number") {
if (b === null || b.tag === 256) {
return 1;
} else {
return -1;
}
} else if (b_type === "number") {
if (a === null || a.tag === 256) {
return -1;
} else {
return 1;
}
} else if (a === null) {
if (b.tag === 256) {
return 1;
} else {
return -1;
}
} else if (b === null) {
if (a.tag === 256) {
return -1;
} else {
return 1;
}
} 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 === 256) {
if (tag_b === 256) {
return Caml_primitive.caml_int_compare(a[1], b[1]);
} else {
return -1;
}
} 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) {
if (Array.isArray(a)) {
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 {
var a$2 = a;
var b$2 = b;
var min_key_lhs = /* record */[/* contents */undefined];
var min_key_rhs = /* record */[/* contents */undefined];
var do_key = function (param, key) {
var min_key = param[2];
var b = param[1];
if (!b.hasOwnProperty(key) || caml_compare(param[0][key], b[key]) > 0) {
var match = min_key[0];
if (match !== undefined && key >= match) {
return 0;
} else {
min_key[0] = key;
return /* () */0;
}
} else {
return 0;
}
};
var partial_arg = /* tuple */[
a$2,
b$2,
min_key_rhs
];
var do_key_a = (function(partial_arg){
return function do_key_a(param) {
return do_key(partial_arg, param);
}
}(partial_arg));
var partial_arg$1 = /* tuple */[
b$2,
a$2,
min_key_lhs
];
var do_key_b = (function(partial_arg$1){
return function do_key_b(param) {
return do_key(partial_arg$1, param);
}
}(partial_arg$1));
for_in(a$2, do_key_a);
for_in(b$2, do_key_b);
var match = min_key_lhs[0];
var match$1 = min_key_rhs[0];
if (match !== undefined) {
if (match$1 !== undefined) {
return Caml_primitive.caml_string_compare(match, match$1);
} else {
return -1;
}
} else if (match$1 !== undefined) {
return 1;
} else {
return 0;
}
}
} else if (len_a < len_b) {
var a$3 = a;
var b$3 = 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$3[i$1], b$3[i$1]);
if (res$1 !== 0) {
return res$1;
} else {
_i$1 = i$1 + 1 | 0;
continue ;
}
}
};
} else {
var a$4 = a;
var b$4 = 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$4[i$2], b$4[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;
} else {
var a_type = typeof a;
if (a_type === "string" || a_type === "number" || a_type === "boolean" || a_type === "undefined" || a === null) {
return false;
} 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;
} 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;
} else if (tag_a === 256) {
return a[1] === b[1];
} else {
var len_a = a.length | 0;
var len_b = b.length | 0;
if (len_a === len_b) {
if (Array.isArray(a)) {
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;
} else if (caml_equal(a$1[i], b$1[i])) {
_i = i + 1 | 0;
continue ;
} else {
return false;
}
};
} else {
var a$2 = a;
var b$2 = b;
var result = /* record */[/* contents */true];
var do_key_a = (function(b$2,result){
return function do_key_a(key) {
if (b$2.hasOwnProperty(key)) {
return 0;
} else {
result[0] = false;
return /* () */0;
}
}
}(b$2,result));
var do_key_b = (function(a$2,b$2,result){
return function do_key_b(key) {
if (!a$2.hasOwnProperty(key) || !caml_equal(b$2[key], a$2[key])) {
result[0] = false;
return /* () */0;
} else {
return 0;
}
}
}(a$2,b$2,result));
for_in(a$2, do_key_a);
if (result[0]) {
for_in(b$2, do_key_b);
}
return result[0];
}
} else {
return false;
}
}
}
}
}
};
}
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 !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;
}
}
function caml_obj_set_tag(prim, prim$1) {
prim.tag = prim$1;
return /* () */0;
}
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;
exports.caml_obj_set_tag = caml_obj_set_tag;
/* No side effect */