@@ -412,6 +412,7 @@ module Exp = struct
412412 true_
413413 | _ ->
414414 to_ocaml_boolean {expression_desc = Bin (EqEqEq , e0,e1); comment}
415+ let int_equal = float_equal
415416 let rec string_equal ?comment (e0 : t ) (e1 : t ) : t =
416417 match e0.expression_desc, e1.expression_desc with
417418 | Str (_, a0), Str (_, b0)
@@ -514,27 +515,44 @@ module Exp = struct
514515 bit different from Javascript, so that we can change it in the future
515516 *)
516517 let rec and_ ?comment (e1 : t ) (e2 : t ) =
517- match e1, e2 with
518- | {expression_desc = Int_of_boolean e1;_} ,
519- {expression_desc = Int_of_boolean e2;_} ->
518+ match e1.expression_desc, e2.expression_desc with
519+ | Int_of_boolean e1 , Int_of_boolean e2 ->
520520 and_ ?comment e1 e2
521- | {expression_desc = Int_of_boolean e1; _} ,
522- e2 -> and_ ?comment e1 e2
523- | e1, {expression_desc = Int_of_boolean e2;_}
521+ | Int_of_boolean e1 , _ -> and_ ?comment e1 e2
522+ | _, Int_of_boolean e2
524523 -> and_ ?comment e1 e2
525- | e1 , e2 ->
524+ (* optimization if [e1 = e2], then and_ e1 e2 -> e2
525+ be careful for side effect
526+ *)
527+ | Var i, Var j when Js_op_util. same_vident i j
528+ ->
529+ to_ocaml_boolean e1
530+ | Var i,
531+ (Bin (And , {expression_desc = Var j ; _}, _)
532+ | Bin (And , _, {expression_desc = Var j ; _}))
533+ when Js_op_util. same_vident i j
534+ ->
535+ to_ocaml_boolean e2
536+ | _ , _ ->
526537 to_ocaml_boolean @@ bin ?comment And e1 e2
527538
528539 let rec or_ ?comment (e1 : t ) (e2 : t ) =
529- match e1, e2 with
530- | {expression_desc = Int_of_boolean e1;_} ,
531- {expression_desc = Int_of_boolean e2;_} ->
540+ match e1.expression_desc , e2.expression_desc with
541+ | Int_of_boolean e1 , Int_of_boolean e2
542+ ->
532543 or_ ?comment e1 e2
533- | {expression_desc = Int_of_boolean e1;_} ,
534- e2 -> or_ ?comment e1 e2
535- | e1, {expression_desc = Int_of_boolean e2;_}
544+ | Int_of_boolean e1 , _ -> or_ ?comment e1 e2
545+ | _, Int_of_boolean e2
536546 -> or_ ?comment e1 e2
537- | e1 , e2 ->
547+ | Var i, Var j when Js_op_util. same_vident i j
548+ ->
549+ to_ocaml_boolean e1
550+ | Var i,
551+ (Bin (Or , {expression_desc = Var j ; _}, _)
552+ | Bin (Or , _, {expression_desc = Var j ; _}))
553+ when Js_op_util. same_vident i j
554+ -> to_ocaml_boolean e2
555+ | _ , _ ->
538556 to_ocaml_boolean @@ bin ?comment Or e1 e2
539557
540558 let string_of_small_int_array ?comment xs : t =
@@ -594,8 +612,21 @@ module Exp = struct
594612
595613 let string_comp cmp ?comment e0 e1 =
596614 to_ocaml_boolean @@ bin ?comment cmp e0 e1
597- let int_comp cmp ?comment e0 e1 =
598- to_ocaml_boolean @@ bin ?comment (Lam_compile_util. jsop_of_comp cmp) e0 e1
615+
616+
617+ let rec int_comp (cmp : Lambda.comparison ) ?comment (e0 : t ) (e1 : t ) =
618+ match cmp, e0.expression_desc, e1.expression_desc with
619+ | _, Call ({
620+ expression_desc =
621+ Var (Qualified
622+ (_, Runtime ,
623+ Some (" caml_int_compare" | " caml_int32_compare" ))); _},
624+ [l;r], _),
625+ Number (Int {i = 0 })
626+ -> int_comp cmp l r (* = 0 > 0 < 0 *)
627+ | _ ->
628+ to_ocaml_boolean @@ bin ?comment (Lam_compile_util. jsop_of_comp cmp) e0 e1
629+
599630 let float_comp cmp ?comment e0 e1 =
600631 to_ocaml_boolean @@ bin ?comment (Lam_compile_util. jsop_of_comp cmp) e0 e1
601632
0 commit comments