Skip to content

Commit e18fd34

Browse files
author
Cristiano Calcagno
committed
Add comparison for undefined, and tests.
1 parent d0f9714 commit e18fd34

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

jscomp/runtime/caml_obj.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ let unsafe_js_compare x y =
167167
let rec caml_compare (a : Obj.t) (b : Obj.t) : int =
168168
if a == b then 0 else
169169
(*front and formoest, we do not compare function values*)
170-
if a == (Obj.repr Js_null.empty) then -1 else
171-
if b == (Obj.repr Js_null.empty) then 1 else
170+
if a == (Obj.repr Js.null) then -1 else
171+
if b == (Obj.repr Js.null) then 1 else
172+
if a == (Obj.repr Js.undefined) then -1 else
173+
if b == (Obj.repr Js.undefined) then 1 else
172174
let a_type = Js.typeof a in
173175
let b_type = Js.typeof b in
174176
if a_type = "string" then

jscomp/test/caml_compare_test.js

+45-1
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,51 @@ var suites_001 = /* :: */[
909909
]);
910910
})
911911
],
912-
/* [] */0
912+
/* :: */[
913+
/* tuple */[
914+
"File \"caml_compare_test.ml\", line 87, characters 4-11",
915+
(function () {
916+
return /* Eq */Block.__(0, [
917+
Caml_obj.caml_compare(null, 0),
918+
-1
919+
]);
920+
})
921+
],
922+
/* :: */[
923+
/* tuple */[
924+
"File \"caml_compare_test.ml\", line 90, characters 4-11",
925+
(function () {
926+
return /* Eq */Block.__(0, [
927+
Caml_obj.caml_compare(0, null),
928+
1
929+
]);
930+
})
931+
],
932+
/* :: */[
933+
/* tuple */[
934+
"File \"caml_compare_test.ml\", line 93, characters 4-11",
935+
(function () {
936+
return /* Eq */Block.__(0, [
937+
Caml_obj.caml_compare(undefined, 0),
938+
-1
939+
]);
940+
})
941+
],
942+
/* :: */[
943+
/* tuple */[
944+
"File \"caml_compare_test.ml\", line 96, characters 4-11",
945+
(function () {
946+
return /* Eq */Block.__(0, [
947+
Caml_obj.caml_compare(0, undefined),
948+
1
949+
]);
950+
})
951+
],
952+
/* [] */0
953+
]
954+
]
955+
]
956+
]
913957
]
914958
]
915959
]

jscomp/test/caml_compare_test.ml

+12-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,18 @@ let suites = Mt.[
8484
__LOC__ , begin fun _ ->
8585
Eq(compare (Js.Null.return [3]) Js.null, 1)
8686
end;
87-
87+
__LOC__ , begin fun _ ->
88+
Eq(compare Js.null (Js.Null.return 0), -1)
89+
end;
90+
__LOC__ , begin fun _ ->
91+
Eq(compare (Js.Null.return 0) Js.null, 1)
92+
end;
93+
__LOC__ , begin fun _ ->
94+
Eq(compare Js.Nullable.undefined (Js.Nullable.return 0), -1)
95+
end;
96+
__LOC__ , begin fun _ ->
97+
Eq(compare (Js.Nullable.return 0) Js.Nullable.undefined, 1)
98+
end;
8899
]
89100
;;
90101

lib/js/caml_obj.js

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ function caml_compare(_a, _b) {
7070
return -1;
7171
} else if (b === null) {
7272
return 1;
73+
} else if (a === undefined) {
74+
return -1;
75+
} else if (b === undefined) {
76+
return 1;
7377
} else {
7478
var a_type = typeof a;
7579
var b_type = typeof b;

0 commit comments

Comments
 (0)