1
1
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2
- *
2
+ *
3
3
* This program is free software: you can redistribute it and/or modify
4
4
* it under the terms of the GNU Lesser General Public License as published by
5
5
* the Free Software Foundation, either version 3 of the License, or
17
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
19
* GNU Lesser General Public License for more details.
20
- *
20
+ *
21
21
* You should have received a copy of the GNU Lesser General Public License
22
22
* along with this program; if not, write to the Free Software
23
23
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
36
36
let obj = Obj.new_block Obj.object_tag table.size
37
37
]}
38
38
39
- Here we only need generate expression like this
39
+ Here we only need generate expression like this
40
40
{[
41
- { tag : tag ; length : size }
41
+ { tag : tag ; length : size }
42
42
]}
43
- we don't need fill fields, since it is not required by GC
43
+ we don't need fill fields, since it is not required by GC
44
44
45
45
*)
46
46
47
- (* *
48
- Since now we change it back to use
49
- Array representation
50
- this function is higly dependent
47
+ (* *
48
+ Since now we change it back to use
49
+ Array representation
50
+ this function is higly dependent
51
51
on how objects are encoded in buckle.
52
52
53
53
There are potentially some issues with wrong implementation of
54
- `caml_obj_dup`, for example, people call `Obj.dup` for a record,
55
- and new record, since currently, `new record` will generate a
56
- `slice` function (which assume the record is an array), and the
54
+ `caml_obj_dup`, for example, people call `Obj.dup` for a record,
55
+ and new record, since currently, `new record` will generate a
56
+ `slice` function (which assume the record is an array), and the
57
57
output is no longer an array. (it might be something like { 0 : x , 1 : y} )
58
58
59
59
{[
60
60
let u : record = Obj.dup x in
61
61
let h = {u with x = 3}
62
- ]}
62
+ ]}
63
63
64
64
==>
65
65
66
66
{[
67
67
var u = caml_obj_dup (x)
68
68
var new_record = u.slice ()
69
-
69
+
70
70
]}
71
71
*)
72
72
73
- let caml_obj_dup (x : Obj.t ) =
73
+ let caml_obj_dup (x : Obj.t ) =
74
74
let len = Bs_obj. length x in
75
75
let v = Caml_array. new_uninitialized len in
76
- for i = 0 to len - 1 do
76
+ for i = 0 to len - 1 do
77
77
Array. unsafe_set v i (Obj. field x i)
78
78
done ;
79
79
Obj. set_tag (Obj. repr v) (Bs_obj. tag x );
80
- Obj. repr v
80
+ Obj. repr v
81
81
82
82
83
83
84
- let caml_obj_truncate (x : Obj.t ) (new_size : int ) =
84
+ let caml_obj_truncate (x : Obj.t ) (new_size : int ) =
85
85
let len = Bs_obj. length x in
86
- if new_size < = 0 || new_size > len then
86
+ if new_size < = 0 || new_size > len then
87
87
raise (Invalid_argument " Obj.truncate" )
88
- else
88
+ else
89
89
if len <> new_size then
90
- begin
90
+ begin
91
91
for i = new_size to len - 1 do
92
92
Obj. set_field x i (Obj. magic 0 )
93
93
done ;
94
- Bs_obj. set_length x new_size
94
+ Bs_obj. set_length x new_size
95
95
end
96
96
97
-
98
97
99
- let caml_lazy_make_forward x = lazy x
98
+
99
+ let caml_lazy_make_forward x = lazy x
100
100
101
101
(* * TODO: the dummy one should be [{}] *)
102
- let caml_update_dummy x y =
102
+ let caml_update_dummy x y =
103
103
let len = Bs_obj. length y in
104
- for i = 0 to len - 1 do
104
+ for i = 0 to len - 1 do
105
105
Obj. set_field x i (Obj. field y i)
106
106
done ;
107
107
Obj. set_tag x (Obj. tag y);
108
108
Bs_obj. set_length x (Bs_obj. length y)
109
109
110
- let caml_int_compare (x : int ) (y : int ) : int =
110
+ let caml_int_compare (x : int ) (y : int ) : int =
111
111
if x < y then - 1 else if x = y then 0 else 1
112
112
113
- let caml_string_compare (x : string ) (y : string ) : int =
113
+ let caml_string_compare (x : string ) (y : string ) : int =
114
114
if x < y then - 1 else if x = y then 0 else 1
115
115
116
- let unsafe_js_compare x y =
117
- if x == y then 0 else
118
- if Js. to_bool @@ Js. unsafe_lt x y then - 1
116
+ let unsafe_js_compare x y =
117
+ if x == y then 0 else
118
+ if Js. to_bool @@ Js. unsafe_lt x y then - 1
119
119
else 1
120
- (* * TODO: investigate total
121
- [compare x y] returns [0] if [x] is equal to [y],
122
- a negative integer if [x] is less than [y],
123
- and a positive integer if [x] is greater than [y].
124
- The ordering implemented by compare is compatible with the comparison
125
- predicates [=], [<] and [>] defined above, with one difference on the treatment of the float value
126
- [nan].
127
-
128
- Namely, the comparison predicates treat nan as different from any other float value,
129
- including itself; while compare treats [nan] as equal to itself and less than any other float value.
120
+ (* * TODO: investigate total
121
+ [compare x y] returns [0] if [x] is equal to [y],
122
+ a negative integer if [x] is less than [y],
123
+ and a positive integer if [x] is greater than [y].
124
+ The ordering implemented by compare is compatible with the comparison
125
+ predicates [=], [<] and [>] defined above, with one difference on the treatment of the float value
126
+ [nan].
127
+
128
+ Namely, the comparison predicates treat nan as different from any other float value,
129
+ including itself; while compare treats [nan] as equal to itself and less than any other float value.
130
130
This treatment of [nan] ensures that compare defines a total ordering relation.
131
- compare applied to functional values may raise Invalid_argument. compare applied to cyclic structures
131
+ compare applied to functional values may raise Invalid_argument. compare applied to cyclic structures
132
132
may not terminate.
133
133
134
134
The compare function can be used as the comparison function required by the [Set.Make] and [Map.Make] functors,
135
135
as well as the [List.sort] and [Array.sort] functions.
136
136
*)
137
- let rec caml_compare (a : Obj.t ) (b : Obj.t ) : int =
137
+ let rec caml_compare (a : Obj.t ) (b : Obj.t ) : int =
138
138
if Js. typeof a = " string" then
139
139
caml_string_compare (Obj. magic a) (Obj. magic b )
140
140
else if Js. typeof a = " number" then
141
141
caml_int_compare (Obj. magic a) (Obj. magic b )
142
- else if Js. typeof a = " boolean"
142
+ else if Js. typeof a = " boolean"
143
143
|| Js. typeof a = " null"
144
144
|| Js. typeof a = " undefined"
145
- then
146
- unsafe_js_compare a b
145
+ then
146
+ unsafe_js_compare a b
147
147
else
148
148
(* if js_is_instance_array a then *)
149
149
(* 0 *)
@@ -155,92 +155,91 @@ let rec caml_compare (a : Obj.t) (b : Obj.t) : int =
155
155
*)
156
156
if tag_a = 250 then
157
157
caml_compare (Obj. field a 0 ) b
158
- else if tag_b = 250 then
158
+ else if tag_b = 250 then
159
159
caml_compare a (Obj. field b 0 )
160
160
else if tag_a = 248 (* object/exception *) then
161
- caml_int_compare (Obj. magic @@ Obj. field a 1 ) (Obj. magic @@ Obj. field b 1 )
162
- else if tag_a = 251 (* abstract_tag *) then
161
+ caml_int_compare (Obj. magic @@ Obj. field a 1 ) (Obj. magic @@ Obj. field b 1 )
162
+ else if tag_a = 251 (* abstract_tag *) then
163
163
raise (Invalid_argument " equal: abstract value" )
164
164
else if tag_a <> tag_b then
165
165
if tag_a < tag_b then (- 1 ) else 1
166
166
else
167
- let len_a = Bs_obj. length a in
168
- let len_b = Bs_obj. length b in
169
- if len_a = len_b then
170
- aux_same_length a b 0 len_a
171
- else if len_a < len_b then
172
- aux_length_a_short a b 0 len_a
173
- else
167
+ let len_a = Bs_obj. length a in
168
+ let len_b = Bs_obj. length b in
169
+ if len_a = len_b then
170
+ aux_same_length a b 0 len_a
171
+ else if len_a < len_b then
172
+ aux_length_a_short a b 0 len_a
173
+ else
174
174
aux_length_b_short a b 0 len_b
175
- and aux_same_length (a : Obj.t ) (b : Obj.t ) i same_length =
175
+ and aux_same_length (a : Obj.t ) (b : Obj.t ) i same_length =
176
176
if i = same_length then
177
177
0
178
- else
178
+ else
179
179
let res = caml_compare (Obj. field a i) (Obj. field b i) in
180
180
if res <> 0 then res
181
181
else aux_same_length a b (i + 1 ) same_length
182
- and aux_length_a_short (a : Obj.t ) (b : Obj.t ) i short_length =
183
- if i = short_length then - 1
184
- else
182
+ and aux_length_a_short (a : Obj.t ) (b : Obj.t ) i short_length =
183
+ if i = short_length then - 1
184
+ else
185
185
let res = caml_compare (Obj. field a i) (Obj. field b i) in
186
186
if res <> 0 then res
187
187
else aux_length_a_short a b (i+ 1 ) short_length
188
- and aux_length_b_short (a : Obj.t ) (b : Obj.t ) i short_length =
188
+ and aux_length_b_short (a : Obj.t ) (b : Obj.t ) i short_length =
189
189
if i = short_length then 1
190
190
else
191
191
let res = caml_compare (Obj. field a i) (Obj. field b i) in
192
192
if res <> 0 then res
193
- else aux_length_b_short a b (i+ 1 ) short_length
193
+ else aux_length_b_short a b (i+ 1 ) short_length
194
194
195
195
type eq = Obj .t -> Obj .t -> bool
196
196
197
- let rec caml_equal (a : Obj.t ) (b : Obj.t ) : bool =
198
- if Js. typeof a = " string"
197
+ let rec caml_equal (a : Obj.t ) (b : Obj.t ) : bool =
198
+ (* first, check using reference equality *)
199
+ if a == b then true
200
+ else if Js. typeof a = " string"
199
201
|| Js. typeof a = " number"
200
202
|| Js. typeof a = " boolean"
201
203
|| Js. typeof a = " undefined"
202
204
|| Js. typeof a = " null"
203
- then a == b else
205
+ then false
206
+ else
204
207
let tag_a = Bs_obj. tag a in
205
208
let tag_b = Bs_obj. tag b in
206
209
(* double_array_tag: 254
207
210
forward_tag:250
208
211
*)
209
212
if tag_a = 250 then
210
213
caml_equal (Obj. field a 0 ) b
211
- else if tag_b = 250 then
214
+ else if tag_b = 250 then
212
215
caml_equal a (Obj. field b 0 )
213
216
else if tag_a = 248 (* object/exception *) then
214
- (Obj. magic @@ Obj. field a 1 ) == (Obj. magic @@ Obj. field b 1 )
215
- else if tag_a = 251 (* abstract_tag *) then
217
+ (Obj. magic @@ Obj. field a 1 ) == (Obj. magic @@ Obj. field b 1 )
218
+ else if tag_a = 251 (* abstract_tag *) then
216
219
raise (Invalid_argument " equal: abstract value" )
217
220
else if tag_a <> tag_b then
218
- false
221
+ false
219
222
else
220
- let len_a = Bs_obj. length a in
221
- let len_b = Bs_obj. length b in
222
- if len_a = len_b then
223
- aux_equal_length a b 0 len_a
223
+ let len_a = Bs_obj. length a in
224
+ let len_b = Bs_obj. length b in
225
+ if len_a = len_b then
226
+ aux_equal_length a b 0 len_a
224
227
else false
225
- and aux_equal_length (a : Obj.t ) (b : Obj.t ) i same_length =
228
+ and aux_equal_length (a : Obj.t ) (b : Obj.t ) i same_length =
226
229
if i = same_length then
227
230
true
228
- else
229
- caml_equal (Obj. field a i) (Obj. field b i)
231
+ else
232
+ caml_equal (Obj. field a i) (Obj. field b i)
230
233
&& aux_equal_length a b (i + 1 ) same_length
231
234
232
235
let caml_notequal a b = not (caml_equal a b)
233
236
234
237
let caml_int32_compare = caml_int_compare
235
238
let caml_nativeint_compare = caml_int_compare
236
- let caml_greaterequal a b = caml_compare a b > = 0
239
+ let caml_greaterequal a b = caml_compare a b > = 0
237
240
238
- let caml_greaterthan a b = caml_compare a b > 0
241
+ let caml_greaterthan a b = caml_compare a b > 0
239
242
240
243
let caml_lessequal a b = caml_compare a b < = 0
241
244
242
245
let caml_lessthan a b = caml_compare a b < 0
243
-
244
-
245
-
246
-
0 commit comments