Skip to content

Commit c6f93af

Browse files
committed
fix bool comparison
1 parent 1dcc516 commit c6f93af

File tree

5 files changed

+89
-36
lines changed

5 files changed

+89
-36
lines changed

jscomp/core/js_exp_make.ml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ let rec seq ?comment (e0 : t) (e1 : t) : t =
265265
| _, ( Seq(a,( {expression_desc = Number _ ; } as v ) ))->
266266
(* Return value could not be changed*)
267267
seq ?comment (seq e0 a) v
268-
268+
| (Number _ | Var _) , _ -> e1
269269
| _ ->
270270
{expression_desc = Seq(e0,e1); comment}
271271

@@ -901,8 +901,8 @@ let rec int_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) =
901901
bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
902902

903903
let bool_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) =
904-
match e0.expression_desc, e1.expression_desc with
905-
| Bool l, Bool r ->
904+
match e0, e1 with
905+
| {expression_desc = Bool l}, {expression_desc = Bool r} ->
906906
bool (match cmp with
907907
| Ceq -> l = r
908908
| Cneq -> l <> r
@@ -911,19 +911,22 @@ let bool_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) =
911911
| Cle -> l <= r
912912
| Cge -> l >= r
913913
)
914-
| Bool l, _ ->
914+
| {expression_desc = Bool true}, rest
915+
| rest, {expression_desc = Bool false} ->
915916
begin match cmp with
916-
| Clt -> seq e1 caml_false
917-
| Cge -> seq e1 caml_true
917+
| Clt -> seq rest caml_false
918+
| Cge -> seq rest caml_true
918919
| Cle
919920
| Cgt
920921
| Ceq
921922
| Cneq -> bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
922923
end
923-
| _, Bool r ->
924+
| rest, {expression_desc = Bool true}
925+
| {expression_desc = Bool false}, rest
926+
->
924927
begin match cmp with
925-
| Cle -> seq e0 caml_true
926-
| Cgt -> seq e0 caml_false
928+
| Cle -> seq rest caml_true
929+
| Cgt -> seq rest caml_false
927930
| Clt
928931
| Cge
929932
| Ceq

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@ let translate loc (prim_name : string)
370370
E.string_comp Ge e0 e1
371371
| _ -> assert false
372372
end
373+
| "caml_string_greaterthan"
374+
->
375+
begin match args with
376+
| [e0; e1]
377+
->
378+
E.string_comp Gt e0 e1
379+
| _ -> assert false
380+
end
373381
| "caml_bool_notequal"
374382
->
375383
begin match args with
@@ -402,7 +410,13 @@ let translate loc (prim_name : string)
402410
E.bool_comp Cge e0 e1
403411
| _ -> assert false
404412
end
405-
413+
| "caml_bool_greaterthan"
414+
->
415+
begin match args with
416+
| [e0;e1] ->
417+
E.bool_comp Cgt e0 e1
418+
| _ -> assert false
419+
end
406420
| "caml_bool_equal"
407421
| "caml_bool_equal_null"
408422
| "caml_bool_equal_nullable"
@@ -451,14 +465,7 @@ let translate loc (prim_name : string)
451465
| _ -> assert false
452466
end
453467

454-
| "caml_string_greaterthan"
455-
->
456-
begin match args with
457-
| [e0; e1]
458-
->
459-
E.string_comp Gt e0 e1
460-
| _ -> assert false
461-
end
468+
462469
| "caml_create_string" ->
463470
(* Bytes.create *)
464471
(* Note that for invalid range, JS raise an Exception RangeError,

jscomp/test/js_bool_test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ function ffadd(x, y) {
9696
return x + y;
9797
}
9898

99+
function ss(x) {
100+
return "xx" > x;
101+
}
102+
103+
function bb(x) {
104+
return /* tuple */[
105+
true > x,
106+
false,
107+
true,
108+
true <= x,
109+
false,
110+
false < x,
111+
false >= x,
112+
true
113+
];
114+
}
115+
99116
var bool_array = /* array */[
100117
true,
101118
false
@@ -115,5 +132,7 @@ exports.fi = fi;
115132
exports.fb = fb;
116133
exports.fadd = fadd;
117134
exports.ffadd = ffadd;
135+
exports.ss = ss;
136+
exports.bb = bb;
118137
exports.bool_array = bool_array;
119138
/* u Not a pure module */

jscomp/test/js_bool_test.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,19 @@ let fb (x : bool) y = x = y
3232
let fadd (x : int) y = x + y
3333
let ffadd (x : float) y = x +. y
3434

35+
let ss x = "xx" > x
36+
37+
let bb x =
38+
( true > x,
39+
true < x,
40+
true >= x ,
41+
true <= x,
42+
false > x ,
43+
false < x ,
44+
false >= x,
45+
false <= x
46+
)
47+
48+
3549
let bool_array = [|true; false|]
3650
;; Mt.from_pair_suites __FILE__ suites

lib/whole_compiler.ml

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71276,7 +71276,7 @@ let rec seq ?comment (e0 : t) (e1 : t) : t =
7127671276
| _, ( Seq(a,( {expression_desc = Number _ ; } as v ) ))->
7127771277
(* Return value could not be changed*)
7127871278
seq ?comment (seq e0 a) v
71279-
71279+
| (Number _ | Var _) , _ -> e1
7128071280
| _ ->
7128171281
{expression_desc = Seq(e0,e1); comment}
7128271282

@@ -71912,8 +71912,8 @@ let rec int_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) =
7191271912
bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
7191371913

7191471914
let bool_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) =
71915-
match e0.expression_desc, e1.expression_desc with
71916-
| Bool l, Bool r ->
71915+
match e0, e1 with
71916+
| {expression_desc = Bool l}, {expression_desc = Bool r} ->
7191771917
bool (match cmp with
7191871918
| Ceq -> l = r
7191971919
| Cneq -> l <> r
@@ -71922,19 +71922,22 @@ let bool_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) =
7192271922
| Cle -> l <= r
7192371923
| Cge -> l >= r
7192471924
)
71925-
| Bool l, _ ->
71925+
| {expression_desc = Bool true}, rest
71926+
| rest, {expression_desc = Bool false} ->
7192671927
begin match cmp with
71927-
| Clt -> seq e1 caml_false
71928-
| Cge -> seq e1 caml_true
71928+
| Clt -> seq rest caml_false
71929+
| Cge -> seq rest caml_true
7192971930
| Cle
7193071931
| Cgt
7193171932
| Ceq
7193271933
| Cneq -> bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
7193371934
end
71934-
| _, Bool r ->
71935+
| rest, {expression_desc = Bool true}
71936+
| {expression_desc = Bool false}, rest
71937+
->
7193571938
begin match cmp with
71936-
| Cle -> seq e0 caml_true
71937-
| Cgt -> seq e0 caml_false
71939+
| Cle -> seq rest caml_true
71940+
| Cgt -> seq rest caml_false
7193871941
| Clt
7193971942
| Cge
7194071943
| Ceq
@@ -94941,6 +94944,14 @@ let translate loc (prim_name : string)
9494194944
E.string_comp Ge e0 e1
9494294945
| _ -> assert false
9494394946
end
94947+
| "caml_string_greaterthan"
94948+
->
94949+
begin match args with
94950+
| [e0; e1]
94951+
->
94952+
E.string_comp Gt e0 e1
94953+
| _ -> assert false
94954+
end
9494494955
| "caml_bool_notequal"
9494594956
->
9494694957
begin match args with
@@ -94973,7 +94984,13 @@ let translate loc (prim_name : string)
9497394984
E.bool_comp Cge e0 e1
9497494985
| _ -> assert false
9497594986
end
94976-
94987+
| "caml_bool_greaterthan"
94988+
->
94989+
begin match args with
94990+
| [e0;e1] ->
94991+
E.bool_comp Cgt e0 e1
94992+
| _ -> assert false
94993+
end
9497794994
| "caml_bool_equal"
9497894995
| "caml_bool_equal_null"
9497994996
| "caml_bool_equal_nullable"
@@ -95022,14 +95039,7 @@ let translate loc (prim_name : string)
9502295039
| _ -> assert false
9502395040
end
9502495041

95025-
| "caml_string_greaterthan"
95026-
->
95027-
begin match args with
95028-
| [e0; e1]
95029-
->
95030-
E.string_comp Gt e0 e1
95031-
| _ -> assert false
95032-
end
95042+
9503395043
| "caml_create_string" ->
9503495044
(* Bytes.create *)
9503595045
(* Note that for invalid range, JS raise an Exception RangeError,

0 commit comments

Comments
 (0)