Skip to content

Commit 9aa7ca9

Browse files
authored
Merge pull request #1176 from dorafmon/Issue#1167
Issue #1167: a simple fix for case two
2 parents 23f11d1 + 80946da commit 9aa7ca9

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

jscomp/runtime/caml_obj.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ let unsafe_js_compare x y =
152152
as well as the [List.sort] and [Array.sort] functions.
153153
*)
154154
let rec caml_compare (a : Obj.t) (b : Obj.t) : int =
155+
(*front and formoest, we do not compare function values*)
155156
if Js.typeof a = "string" then
156157
caml_string_compare (Obj.magic a) (Obj.magic b )
157158
else if Js.typeof a = "number" then
@@ -161,6 +162,8 @@ let rec caml_compare (a : Obj.t) (b : Obj.t) : int =
161162
|| Js.typeof a = "undefined"
162163
then
163164
unsafe_js_compare a b
165+
else if Js.typeof a = "function" || Js.typeof b = "function"
166+
then raise (Invalid_argument "compare: functional value")
164167
else
165168
(* if js_is_instance_array a then *)
166169
(* 0 *)
@@ -212,14 +215,17 @@ and aux_length_b_short (a : Obj.t) (b : Obj.t) i short_length =
212215
type eq = Obj.t -> Obj.t -> bool
213216

214217
let rec caml_equal (a : Obj.t) (b : Obj.t) : bool =
215-
(* first, check using reference equality *)
218+
(*front and formoest, we do not compare function values*)
216219
if a == b then true
217220
else if Js.typeof a = "string"
218221
|| Js.typeof a = "number"
219222
|| Js.typeof a = "boolean"
220223
|| Js.typeof a = "undefined"
221224
|| Js.typeof a = "null"
222225
then false
226+
else if Js.typeof a = "function" || Js.typeof b = "function"
227+
then raise (Invalid_argument "equal: functional value")
228+
(* first, check using reference equality *)
223229
else
224230
let tag_a = Bs_obj.tag a in
225231
let tag_b = Bs_obj.tag b in

jscomp/test/caml_compare_test.js

Lines changed: 33 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/caml_compare_test.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
type u = A of int | B of int * bool | C of int
33

4+
let function_equal_test = try ((fun x -> x + 1) = (fun x -> x + 2)) with
5+
| Invalid_argument "equal: functional value" -> true
6+
| _ -> false
7+
48
let suites = Mt.[
59
"option", (fun _ -> Eq(true, None < Some 1));
610
"option2", (fun _ -> Eq(true, Some 1 < Some 2));
@@ -9,8 +13,10 @@ let suites = Mt.[
913
"listneq", (fun _ -> Eq(true, [1;2;3] > [1;2;2]));
1014
"custom_u", (fun _ -> Eq(true, ( A 3 , B (2,false) , C 1) > ( A 3, B (2,false) , C 0 )));
1115
"custom_u2", (fun _ -> Eq(true, ( A 3 , B (2,false) , C 1) = ( A 3, B (2,false) , C 1 )));
16+
"function", (fun _ -> Eq(true, function_equal_test))
1217
]
1318
;;
1419

1520

21+
1622
Mt.from_pair_suites __FILE__ suites

lib/es6/caml_obj.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ function caml_compare(_a, _b) {
9797
return 1;
9898
}
9999
}
100+
else if (typeof a === "function" || typeof b === "function") {
101+
throw [
102+
Caml_builtin_exceptions.invalid_argument,
103+
"compare: functional value"
104+
];
105+
}
100106
else {
101107
var tag_a = a.tag | 0;
102108
var tag_b = b.tag | 0;
@@ -214,6 +220,12 @@ function caml_equal(_a, _b) {
214220
else if (typeof a === "string" || typeof a === "number" || typeof a === "boolean" || typeof a === "undefined" || typeof a === "null") {
215221
return /* false */0;
216222
}
223+
else if (typeof a === "function" || typeof b === "function") {
224+
throw [
225+
Caml_builtin_exceptions.invalid_argument,
226+
"equal: functional value"
227+
];
228+
}
217229
else {
218230
var tag_a = a.tag | 0;
219231
var tag_b = b.tag | 0;

lib/js/caml_obj.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ function caml_compare(_a, _b) {
9797
return 1;
9898
}
9999
}
100+
else if (typeof a === "function" || typeof b === "function") {
101+
throw [
102+
Caml_builtin_exceptions.invalid_argument,
103+
"compare: functional value"
104+
];
105+
}
100106
else {
101107
var tag_a = a.tag | 0;
102108
var tag_b = b.tag | 0;
@@ -214,6 +220,12 @@ function caml_equal(_a, _b) {
214220
else if (typeof a === "string" || typeof a === "number" || typeof a === "boolean" || typeof a === "undefined" || typeof a === "null") {
215221
return /* false */0;
216222
}
223+
else if (typeof a === "function" || typeof b === "function") {
224+
throw [
225+
Caml_builtin_exceptions.invalid_argument,
226+
"equal: functional value"
227+
];
228+
}
217229
else {
218230
var tag_a = a.tag | 0;
219231
var tag_b = b.tag | 0;

0 commit comments

Comments
 (0)