Skip to content

Commit 8dbadd3

Browse files
authored
Runtime: fix poly compare with null and undefined (#920)
1 parent 34219a1 commit 8dbadd3

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/tests/poly_compare.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ let%expect_test "poly compare" =
101101
; Pack None
102102
; Pack (Js.Unsafe.obj [| "a", Js.Unsafe.inject 0 |])
103103
; Pack (Js.Unsafe.obj [| "a", Js.Unsafe.inject 0 |])
104+
; Pack (Js.undefined)
105+
; Pack (Js.null)
104106
]
105107
|> List.mapi (fun i x -> i, x)
106108
in
@@ -112,6 +114,8 @@ let%expect_test "poly compare" =
112114
3
113115
2
114116
0
117+
6
118+
7
115119
5
116120
4 |}];
117121
let l' = List.sort (fun (_, a) (_, b) -> compare a b) (List.rev l) in
@@ -124,7 +128,9 @@ let%expect_test "poly compare" =
124128
2
125129
0
126130
4
127-
5 |}];
131+
5
132+
7
133+
6 |}];
128134
List.iter (fun (i, _) -> Printf.printf "%d\n" i) l'';
129135
print_endline "";
130136
[%expect {|
@@ -133,4 +139,6 @@ let%expect_test "poly compare" =
133139
2
134140
0
135141
4
136-
5 |}]
142+
5
143+
6
144+
7 |}]

runtime/stdlib.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ function caml_compare_val_tag(a){
373373
else if (a instanceof String) return 1252; // javascript string, like string_tag (252)
374374
else if (typeof a == "string") return 1252; // javascript string, like string_tag (252)
375375
else if (a instanceof Number) return 1000; // int_tag (we use it for all numbers)
376-
else if (a.caml_custom) return 1255; // like custom_tag (255)
377-
else if (a.compare) return 1256; // like custom_tag (255)
376+
else if (a && a.caml_custom) return 1255; // like custom_tag (255)
377+
else if (a && a.compare) return 1256; // like custom_tag (255)
378378
else if (typeof a == "function") return 1247; // like closure_tag (247)
379379
else if (typeof a == "symbol") return 1251;
380380
return 1001; //out_of_heap_tag

0 commit comments

Comments
 (0)