forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimitive_object.res
385 lines (350 loc) · 11.5 KB
/
Primitive_object.res
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
/* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
module Array = Primitive_array_extern
module Js = Primitive_js_extern
type t = Primitive_object_extern.t
// Note: this only works as intended as long as the runtime is compiled
// with -bs-cross-module-opt.
let repr = Primitive_object_extern.repr
let magic = Primitive_object_extern.magic
let tag = Primitive_object_extern.tag
let field = Primitive_object_extern.getField
let size = Primitive_object_extern.size
module O = {
@val external isArray: 'a => bool = "Array.isArray"
type key = string
let for_in: (t, key => unit) => unit = %raw(`function(o,foo){
for (var x in o) { foo(x) }}
`)
@scope(("Object", "prototype", "hasOwnProperty"))
@val
/**
JS objects are not guaranteed to have `Object` in their prototype
chain so calling `some_obj.hasOwnProperty(key)` can sometimes throw
an exception when dealing with JS interop. This mainly occurs when
objects are created via `Object.create(null)`. The only safe way
to call this function is directly, e.g. `Object.prototype.hasOwnProperty.call(some_obj, key)`.
*/
external hasOwnProperty: (t, key) => bool = "call"
@get_index external get_value: (t, key) => t = ""
}
let updateDummy = Primitive_object_extern.updateDummy
/** TODO: investigate total
[compare x y] returns [0] if [x] is equal to [y],
a negative integer if [x] is less than [y],
and a positive integer if [x] is greater than [y].
The ordering implemented by compare is compatible with the comparison
predicates [=], [<] and [>] defined above, with one difference on the treatment of the float value
[nan].
Namely, the comparison predicates treat nan as different from any other float value,
including itself; while compare treats [nan] as equal to itself and less than any other float value.
This treatment of [nan] ensures that compare defines a total ordering relation.
compare applied to functional values may raise Invalid_argument. compare applied to cyclic structures
may not terminate.
The compare function can be used as the comparison function required by the [Set.Make] and [Map.Make] functors,
as well as the [List.sort] and [Array.sort] functions.
*/
let rec compare = (a: t, b: t): int =>
if a === b {
0
} else {
/* front and formoest, we do not compare function values */
let a_type = Js.typeof(a)
let b_type = Js.typeof(b)
switch (a_type, b_type) {
| ("undefined", _) => -1
| (_, "undefined") => 1
/* [a] is of type string, b can not be None,
[a] could be (Some (Some x)) in that case [b] could be [Some None] or [null]
so [b] has to be of type string or null */
| ("string", "string") => Pervasives.compare((magic(a): string), magic(b))
| ("string", _) => /* [b] could be [Some None] or [null] */
1
| (_, "string") => -1
| ("boolean", "boolean") => Pervasives.compare((magic(a): bool), magic(b))
| ("boolean", _) => 1
| (_, "boolean") => -1
| ("function", "function") => raise(Invalid_argument("compare: functional value"))
| ("function", _) => 1
| (_, "function") => -1
| ("bigint", "bigint")
| ("number", "number") =>
Pervasives.compare((magic(a): float), (magic(b): float))
| ("number", _) =>
if b === repr(Js.null) || Primitive_option.isNested(b) {
1
} else {
/* Some (Some ..) < x */
-1
} /* Integer < Block in OCaml runtime GPR #1195, except Some.. */
| (_, "number") =>
if a === repr(Js.null) || Primitive_option.isNested(a) {
-1
} else {
1
}
| _ =>
if a === repr(Js.null) {
/* [b] could not be null otherwise would equal */
if Primitive_option.isNested(b) {
1
} else {
-1
}
} else if b === repr(Js.null) {
if Primitive_option.isNested(a) {
-1
} else {
1
}
} else if (
/* double_array_tag: 254
*/
Primitive_option.isNested(a)
) {
if Primitive_option.isNested(b) {
aux_obj_compare(a, b)
} else {
/* Some (None) < Some (Some (None)) */
/* b could not be undefined/None */
/* Some (None) < Some (...) */
-1
}
} else {
let tag_a = tag(a)
let tag_b = tag(b)
if tag_a == 248 /* object/exception */ {
Pervasives.compare((magic(field(a, 1)): int), magic(field(b, 1)))
} else if tag_a == 251 /* abstract_tag */ {
raise(Invalid_argument("equal: abstract value"))
} else if tag_a != tag_b {
if tag_a < tag_b {
-1
} else {
1
}
} else {
let len_a = size(a)
let len_b = size(b)
if len_a == len_b {
if O.isArray(a) {
aux_same_length((magic(a): array<t>), (magic(b): array<t>), 0, len_a)
} else if %raw(`a instanceof Date && b instanceof Date`) {
%raw(`a - b`)
} else {
aux_obj_compare(a, b)
}
} else if len_a < len_b {
/* at least one is not zero, so it is an array block */
aux_length_a_short((magic(a): array<t>), (magic(b): array<t>), 0, len_a)
} else {
aux_length_b_short((magic(a): array<t>), (magic(b): array<t>), 0, len_b)
}
}
}
}
}
and aux_same_length = (a: array<t>, b: array<t>, i, same_length) =>
if i == same_length {
0
} else {
let res = compare(Array.getUnsafe(a, i), Array.getUnsafe(b, i))
if res != 0 {
res
} else {
aux_same_length(a, b, i + 1, same_length)
}
}
and aux_length_a_short = (a: array<t>, b: array<t>, i, short_length) =>
if i == short_length {
-1
} else {
let res = compare(Array.getUnsafe(a, i), Array.getUnsafe(b, i))
if res != 0 {
res
} else {
aux_length_a_short(a, b, i + 1, short_length)
}
}
and aux_length_b_short = (a: array<t>, b: array<t>, i, short_length) =>
if i == short_length {
1
} else {
let res = compare(Array.getUnsafe(a, i), Array.getUnsafe(b, i))
if res != 0 {
res
} else {
aux_length_b_short(a, b, i + 1, short_length)
}
}
and aux_obj_compare = (a: t, b: t) => {
let min_key_lhs = ref(None)
let min_key_rhs = ref(None)
let do_key = ((a, b, min_key), key) =>
if !O.hasOwnProperty(b, key) || compare(O.get_value(a, key), O.get_value(b, key)) > 0 {
switch min_key.contents {
| None => min_key.contents = Some(key)
| Some(mk) =>
if key < mk {
min_key.contents = Some(key)
}
}
}
let do_key_a = key => do_key((a, b, min_key_rhs), key)
let do_key_b = key => do_key((b, a, min_key_lhs), key)
O.for_in(a, do_key_a)
O.for_in(b, do_key_b)
let res = switch (min_key_lhs.contents, min_key_rhs.contents) {
| (None, None) => 0
| (Some(_), None) => -1
| (None, Some(_)) => 1
| (Some(x), Some(y)) => Pervasives.compare(x, y)
}
res
}
type eq = (t, t) => bool
/** It is easier to do equality check than comparision, since as long as its
basic type is not the same, it will not equal
*/
let rec equal = (a: t, b: t): bool =>
/* front and formoest, we do not compare function values */
if a === b {
true
} else {
let a_type = Js.typeof(a)
if (
a_type == "string" ||
(a_type == "number" ||
(a_type == "bigint" ||
(a_type == "boolean" ||
(a_type == "undefined" || a === %raw(`null`)))))
) {
false
} else {
let b_type = Js.typeof(b)
if a_type == "function" || b_type == "function" {
raise(Invalid_argument("equal: functional value"))
} /* first, check using reference equality */
else if (
/* a_type = "object" || "symbol" */
b_type == "number" || (b_type == "bigint" || (b_type == "undefined" || b === %raw(`null`)))
) {
false
} else {
/* [a] [b] could not be null, so it can not raise */
let tag_a = tag(a)
let tag_b = tag(b)
if tag_a == 248 /* object/exception */ {
magic(field(a, 1)) === magic(field(b, 1))
} else if tag_a == 251 /* abstract_tag */ {
raise(Invalid_argument("equal: abstract value"))
} else if tag_a != tag_b {
false
} else {
let len_a = size(a)
let len_b = size(b)
if len_a == len_b {
if O.isArray(a) {
aux_equal_length((magic(a): array<t>), (magic(b): array<t>), 0, len_a)
} else if %raw(`a instanceof Date && b instanceof Date`) {
!(Js.gt(a, b) || Js.lt(a, b))
} else {
aux_obj_equal(a, b)
}
} else {
false
}
}
}
}
}
and aux_equal_length = (a: array<t>, b: array<t>, i, same_length) =>
if i == same_length {
true
} else {
equal(Array.getUnsafe(a, i), Array.getUnsafe(b, i)) &&
aux_equal_length(a, b, i + 1, same_length)
}
and aux_obj_equal = (a: t, b: t) => {
let result = ref(true)
let do_key_a = key =>
if !O.hasOwnProperty(b, key) {
result.contents = false
}
let do_key_b = key =>
if !O.hasOwnProperty(a, key) || !equal(O.get_value(b, key), O.get_value(a, key)) {
result.contents = false
}
O.for_in(a, do_key_a)
if result.contents {
O.for_in(b, do_key_b)
}
result.contents
}
@inline
let isNumberOrBigInt = a => Js.typeof(a) == "number" || Js.typeof(a) == "bigint"
@inline
let canNumericCompare = (a, b) => isNumberOrBigInt(a) && isNumberOrBigInt(b)
let notequal = (a, b) =>
if canNumericCompare(a, b) {
(magic(a): float) != (magic(b): float)
} else {
!equal(a, b)
}
let greaterequal = (a, b) =>
if canNumericCompare(a, b) {
(magic(a): float) >= (magic(b): float)
} else {
compare(a, b) >= 0
}
let greaterthan = (a, b) =>
if canNumericCompare(a, b) {
(magic(a): float) > (magic(b): float)
} else {
compare(a, b) > 0
}
let lessequal = (a, b) =>
if canNumericCompare(a, b) {
(magic(a): float) <= (magic(b): float)
} else {
compare(a, b) <= 0
}
let lessthan = (a, b) =>
if canNumericCompare(a, b) {
(magic(a): float) < (magic(b): float)
} else {
compare(a, b) < 0
}
let min = (x: t, y) =>
if compare(x, y) <= 0 {
x
} else {
y
}
let max = (x: t, y) =>
if compare(x, y) >= 0 {
x
} else {
y
}