Skip to content

Commit b55ce87

Browse files
committed
Runtime: don't use double equal (part 2)
1 parent 7cc4334 commit b55ce87

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

lib/tests/test_poly_compare.ml

+15-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ let%expect_test "poly equal neg" =
3636
let%expect_test "poly compare" =
3737
let obj1 = Js.Unsafe.obj [| "a", Js.Unsafe.inject 0 |] in
3838
let obj2 = Js.Unsafe.obj [| "a", Js.Unsafe.inject 0 |] in
39-
(match List.sort compare [ obj1; obj1 ] with
39+
(match List.sort compare [ obj1; obj2 ] with
4040
| [ a; b ] ->
4141
if a == obj1 && b == obj2
4242
then print_endline "preserve"
@@ -98,6 +98,15 @@ let%expect_test "object comparison" =
9898
assert (compare s1 s2 = 1);
9999
assert (compare s2 s1 = 1)
100100

101+
let%expect_test "null/undefined comparison" =
102+
let s1 = Pack (Js.Unsafe.js_expr "undefined") in
103+
let s2 = Pack (Js.Unsafe.js_expr "null") in
104+
assert (s1 <> s2);
105+
assert (s1 = s1);
106+
assert (compare s1 s1 = 0);
107+
assert (compare s1 s2 = 1);
108+
assert (compare s2 s1 = 1)
109+
101110
let%expect_test "poly compare" =
102111
let l =
103112
[ Pack (object end)
@@ -119,10 +128,11 @@ let%expect_test "poly compare" =
119128
3
120129
2
121130
0
122-
6
123131
7
132+
6
124133
5
125-
4 |}];
134+
4
135+
|}];
126136
let l' = List.sort (fun (_, a) (_, b) -> compare a b) (List.rev l) in
127137
let l'' = List.sort (fun (_, a) (_, b) -> compare a b) (List.rev l') in
128138
List.iter (fun (i, _) -> Printf.printf "%d\n" i) l';
@@ -134,8 +144,9 @@ let%expect_test "poly compare" =
134144
0
135145
4
136146
5
147+
6
137148
7
138-
6 |}];
149+
|}];
139150
List.iter (fun (i, _) -> Printf.printf "%d\n" i) l'';
140151
print_endline "";
141152
[%expect {|

runtime/compare.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,16 @@ function caml_compare_val(a, b, total) {
109109
}
110110
return tag_a < tag_b ? -1 : 1;
111111
}
112+
// tag_a = tag_b
112113
switch (tag_a) {
113114
// 246: Lazy_tag handled bellow
114115
case 247: // Closure_tag
115116
// Cannot happen
116117
caml_invalid_argument("compare: functional value");
117118
break;
118119
case 248: // Object
119-
var x = caml_int_compare(a[2], b[2]);
120-
if (x !== 0) return x | 0;
120+
var x = caml_int_compare(a[2], b[2]) | 0;
121+
if (x !== 0) return x;
121122
break;
122123
case 249: // Infix
123124
// Cannot happen
@@ -132,8 +133,8 @@ function caml_compare_val(a, b, total) {
132133
break;
133134
case 252: // OCaml bytes
134135
if (a !== b) {
135-
var x = caml_bytes_compare(a, b);
136-
if (x !== 0) return x | 0;
136+
var x = caml_bytes_compare(a, b) | 0;
137+
if (x !== 0) return x;
137138
}
138139
break;
139140
case 253: // Double_tag
@@ -204,20 +205,15 @@ function caml_compare_val(a, b, total) {
204205
// - if a and b are strings, apply lexicographic comparison
205206
// - if a or b are not strings, convert a and b to number
206207
// and apply standard comparison
207-
//
208-
// Exception: `!=` will not coerce/convert if both a and b are objects
209208
if (a < b) return -1;
210209
if (a > b) return 1;
211210
if (a !== b) {
212-
if (!total) return Number.NaN;
213-
if (!Number.isNaN(a)) return 1;
214-
if (!Number.isNaN(b)) return -1;
211+
return total ? 1 : Number.NaN;
215212
}
216213
break;
217214
case 1251: // JavaScript Symbol, no ordering.
218215
if (a !== b) {
219-
if (!total) return Number.NaN;
220-
return 1;
216+
return total ? 1 : Number.NaN;
221217
}
222218
break;
223219
case 1252: // ocaml strings

0 commit comments

Comments
 (0)