diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index 95c9c16f02..af5bebecfd 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -120,7 +120,6 @@ and expression_desc = [typeof] is an operator *) | Typeof of expression - | Caml_not of expression (* 1 - v *) | Js_not of expression (* !v *) | String_of_small_int_array of expression (* String.fromCharCode.apply(null, args) *) diff --git a/jscomp/core/js_analyzer.ml b/jscomp/core/js_analyzer.ml index 5534103c8f..0ac91704fd 100644 --- a/jscomp/core/js_analyzer.ml +++ b/jscomp/core/js_analyzer.ml @@ -119,7 +119,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Array_copy _ (* | Tag_ml_obj _ *) | J.Anything_to_number _ - | Caml_not _ | Js_not _ | String_of_small_int_array _ | Json_stringify _ @@ -273,7 +272,6 @@ let rec eq_expression | Anything_to_number _ | Typeof _ - | Caml_not _ | Js_not _ | String_of_small_int_array _ | Json_stringify _ diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index 663018a318..d73b48f037 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -664,8 +664,6 @@ and if l > 0 then P.paren_group f 1 action else action () - - | Caml_not e | Js_not e -> let action () = P.string f "!" ; @@ -1150,7 +1148,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Typeof _ | Bind _ | Number _ - | Caml_not _ (* FIXME*) | Js_not _ | Bool _ | New _ diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index 0f22f8ded0..039fc15120 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -488,12 +488,7 @@ let obj ?comment properties : t = (* Dot .....................**) -(** Convert a javascript boolean to ocaml bool - It's necessary for return value - this should be optmized away for [if] ,[cond] to produce - more readable code -*) -let bool_of_boolean ?comment (e : t) : t = e + (* var (Jident.create_js "true") *) @@ -549,7 +544,7 @@ let rec triple_equal ?comment (e0 : t) (e1 : t ) : t = | Char_of_int a , Char_of_int b -> triple_equal ?comment a b | _ -> - bool_of_boolean {expression_desc = Bin(EqEqEq, e0,e1); comment} + {expression_desc = Bin(EqEqEq, e0,e1); comment} let bin ?comment (op : J.binop) e0 e1 : t = match op with @@ -627,8 +622,7 @@ let rec or_ ?comment (e1 : t) (e2 : t) = let rec not ({expression_desc; comment} as e : t) : t = match expression_desc with | Number (Int {i; _}) -> - if i <> 0l then caml_false else caml_true - | Caml_not e -> e + bool (i = 0l ) | Js_not e -> e (* match expression_desc with *) (* can still hapen after some optimizations *) @@ -640,42 +634,11 @@ let rec not ({expression_desc; comment} as e : t) : t = | Bin(Le,a,b) -> {e with expression_desc = Bin (Gt,a,b)} | Bin(Gt,a,b) -> {e with expression_desc = Bin (Le,a,b)} | Bool b -> if b then caml_false else caml_true - | x -> {expression_desc = Caml_not e ; comment = None} - -let rec ocaml_boolean_under_condition (b : t) = - match b.expression_desc with - | Bin (And, x,y) -> - let x' = ocaml_boolean_under_condition x in - let y' = ocaml_boolean_under_condition y in - if x == x' && y==y' then b - else {b with expression_desc = Bin(And,x',y')} - | Bin(Or,x,y) -> - let x' = ocaml_boolean_under_condition x in - let y' = ocaml_boolean_under_condition y in - if x == x' && y == y' then b - else {b with expression_desc = Bin(Or,x',y')} - (** TODO: settle down Not semantics *) - | Caml_not u - | Js_not u - -> - let u' = ocaml_boolean_under_condition u in - if u' == u then b - else {b with expression_desc = Js_not u'} - | _ -> b - -(* TODO: could be more non undefined cases - check [caml_obj_is_block] - acutally we should avoid introducing undefined - as much as we can, this kind of inlining and mirco-optimization - can be done after we can inline runtime in the future - *) -(* | Bin (NotEqEq, ({expression_desc = Length _; _} as e1) , *) -(* {expression_desc = Var (Id ({name = "undefined"; _} as id))}), *) -(* _, _ *) -(* when Ext_ident.is_js id -> *) -(* econd e1 t f *) -(* | (Bin (Bor, v , {expression_desc = Number (Int {i = 0l ; _})})), _, _ - -> econd v t f *) + | x -> {expression_desc = Js_not e ; comment = None} + + + + let rec econd ?comment (b : t) (t : t) (f : t) : t = match b.expression_desc , t.expression_desc, f.expression_desc with @@ -736,12 +699,10 @@ let rec econd ?comment (b : t) (t : t) (f : t) : t = (* the same as above except we revert the [cond] expression *) econd (or_ b (not p1')) t branch_code0 - | Caml_not e, _, _ | Js_not e, _, _ -> econd ?comment e f t | _ -> - let b = ocaml_boolean_under_condition b in if Js_analyzer.eq_expression t f then if no_side_effect b then t else seq ?comment b t else @@ -793,7 +754,7 @@ let rec float_equal ?comment (e0 : t) (e1 : t) : t = float_equal ?comment a b | _ -> - bool_of_boolean {expression_desc = Bin(EqEqEq, e0,e1); comment} + {expression_desc = Bin(EqEqEq, e0,e1); comment} let int_equal = float_equal @@ -807,7 +768,7 @@ let rec string_equal ?comment (e0 : t) (e1 : t) : t = | Unicode a0, Unicode b0 -> bool (Ext_string.equal a0 b0) | _ , _ -> - bool_of_boolean {expression_desc = Bin(EqEqEq, e0,e1); comment} + {expression_desc = Bin(EqEqEq, e0,e1); comment} let is_type_number ?comment (e : t) : t = @@ -935,7 +896,7 @@ let uint32 ?comment n : J.expression = let string_comp cmp ?comment e0 e1 = - bool_of_boolean @@ bin ?comment cmp e0 e1 + bin ?comment cmp e0 e1 let set_length ?comment e tag : t = seq {expression_desc = Caml_block_set_length (e,tag); comment } unit @@ -967,7 +928,7 @@ let rec int_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) = } , args, call_info)} | Ceq, _, _ -> int_equal e0 e1 | _ -> - bool_of_boolean @@ bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 + bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 let bool_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) = match e0.expression_desc, e1.expression_desc with @@ -1001,10 +962,10 @@ let bool_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) = | _ , _ -> bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 let float_comp cmp ?comment e0 e1 = - bool_of_boolean @@ bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 + bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 let js_comp cmp ?comment e0 e1 = - bool_of_boolean @@ bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 + bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 let rec int32_lsr ?comment @@ -1286,17 +1247,13 @@ let of_block ?comment ?e block : t = let is_null ?comment x = triple_equal ?comment x nil -let js_true : t = caml_true -let js_false : t = caml_false -let js_bool = bool let is_undef ?comment x = triple_equal ?comment x undefined -let for_sure_js_null_undefined_boolean (x : t) = +let for_sure_js_null_undefined (x : t) = match x.expression_desc with | Var (Id ({name = "undefined" | "null"} as id)) -> Ext_ident.is_js id - | Bool _ -> true | _ -> false let is_null_undefined ?comment (x: t) : t = @@ -1306,7 +1263,7 @@ let is_null_undefined ?comment (x: t) : t = -> caml_true | Number _ | Array _ | Caml_block _ -> caml_false | _ -> - bool_of_boolean + { comment ; expression_desc = Is_null_undefined_to_boolean x } @@ -1328,10 +1285,9 @@ let eq_null_undefined_boolean ?comment (a : t) (b : t) = | Var (Id ({name = "null" | "undefined" as n1 } as id1) ), Var (Id ({name = "null" | "undefined" as n2 } as id2) ) when Ext_ident.is_js id1 && Ext_ident.is_js id2 - -> - if n1 = n2 then caml_true else caml_false + -> bool (n1 = n2) | _ -> - bool_of_boolean {expression_desc = Bin(EqEqEq, a, b); comment} + {expression_desc = Bin(EqEqEq, a, b); comment} @@ -1355,7 +1311,7 @@ let neq_null_undefined_boolean ?comment (a : t) (b : t) = -> if n1 <> n2 then caml_true else caml_false | _ -> - bool_of_boolean {expression_desc = Bin(NotEqEq, a, b); comment} + {expression_desc = Bin(NotEqEq, a, b); comment} diff --git a/jscomp/core/js_exp_make.mli b/jscomp/core/js_exp_make.mli index 35ae3a966e..52a1ebad19 100644 --- a/jscomp/core/js_exp_make.mli +++ b/jscomp/core/js_exp_make.mli @@ -57,11 +57,6 @@ type binary_op = ?comment:string -> t -> t -> t type unary_op = ?comment:string -> t -> t -(** simplify - {[if b then ]} - there is no need to convert b into OCaml boolean under this scenario -*) -val ocaml_boolean_under_condition : t -> t @@ -322,7 +317,7 @@ val set_tag : ?comment:string -> J.expression -> J.expression -> t val set_length : ?comment:string -> J.expression -> J.expression -> t val obj_length : ?comment:string -> J.expression -> t -val bool_of_boolean : unary_op + val and_ : binary_op val or_ : binary_op @@ -340,8 +335,8 @@ val raw_js_code : ?comment:string -> J.code_info -> string -> t val nil : t val is_null : unary_op -val js_bool : bool -> t + val is_undef : unary_op -val for_sure_js_null_undefined_boolean : J.expression -> bool +val for_sure_js_null_undefined : J.expression -> bool val is_null_undefined : unary_op val not_implemented : ?comment:string -> string -> t diff --git a/jscomp/core/js_fold.ml b/jscomp/core/js_fold.ml index 2fc63b6fed..d5b2d7aa36 100644 --- a/jscomp/core/js_fold.ml +++ b/jscomp/core/js_fold.ml @@ -135,8 +135,7 @@ class virtual fold = (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) - (* 1 - v *) (* !v *) - (* String.fromCharCode.apply(null, args) *) + (* !v *) (* String.fromCharCode.apply(null, args) *) (* Convert JS boolean into OCaml boolean like [+true], note this ast talks using js terminnology unless explicity stated @@ -377,7 +376,6 @@ class virtual fold = | Anything_to_number _x -> let o = o#expression _x in o | Bool _x -> let o = o#bool _x in o | Typeof _x -> let o = o#expression _x in o - | Caml_not _x -> let o = o#expression _x in o | Js_not _x -> let o = o#expression _x in o | String_of_small_int_array _x -> let o = o#expression _x in o | Json_stringify _x -> let o = o#expression _x in o diff --git a/jscomp/core/js_map.ml b/jscomp/core/js_map.ml index dae520a0ae..73c6f97b21 100644 --- a/jscomp/core/js_map.ml +++ b/jscomp/core/js_map.ml @@ -148,8 +148,7 @@ class virtual map = (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) - (* 1 - v *) (* !v *) - (* String.fromCharCode.apply(null, args) *) + (* !v *) (* String.fromCharCode.apply(null, args) *) (* Convert JS boolean into OCaml boolean like [+true], note this ast talks using js terminnology unless explicity stated @@ -406,7 +405,6 @@ class virtual map = let _x = o#expression _x in Anything_to_number _x | Bool _x -> let _x = o#bool _x in Bool _x | Typeof _x -> let _x = o#expression _x in Typeof _x - | Caml_not _x -> let _x = o#expression _x in Caml_not _x | Js_not _x -> let _x = o#expression _x in Js_not _x | String_of_small_int_array _x -> let _x = o#expression _x in String_of_small_int_array _x diff --git a/jscomp/core/js_stmt_make.ml b/jscomp/core/js_stmt_make.ml index ae916d76b4..67ce93164f 100644 --- a/jscomp/core/js_stmt_make.ml +++ b/jscomp/core/js_stmt_make.ml @@ -230,7 +230,6 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) exp (E.econd e b a) :: acc | _, [], [] -> exp e :: acc - | Caml_not e, _ , _ :: _ | Js_not e, _ , _ :: _ -> aux ?comment e else_ then_ acc | _, [], _ @@ -313,7 +312,6 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) aux ?comment (E.or_ e (E.not pred)) cont then_ acc | _ -> - let e = E.ocaml_boolean_under_condition e in { statement_desc = If (e, then_, @@ -359,7 +357,6 @@ let rec while_ ?comment ?label ?env (e : E.t) (st : J.block) : t = (* | {expression_desc = Int_of_boolean e; _} -> *) (* while_ ?comment ?label e st *) | _ -> - let e = E.ocaml_boolean_under_condition e in let env = match env with | None -> Js_closure.empty () diff --git a/jscomp/core/lam.ml b/jscomp/core/lam.ml index 31b2e717ac..c2ac867eb2 100644 --- a/jscomp/core/lam.ml +++ b/jscomp/core/lam.ml @@ -1828,10 +1828,6 @@ let convert exports lam : _ * _ = prim ~primitive:Pdebugger ~args:[] loc | _ when s = "#null" -> Lconst (Const_js_null) - | _ when s = "#true" -> - Lconst (Const_js_true) - | _ when s = "#false" -> - Lconst (Const_js_false) | _ when s = "#undefined" -> Lconst (Const_js_undefined) | _ when s = "#init_mod" -> diff --git a/jscomp/core/lam_compile_const.ml b/jscomp/core/lam_compile_const.ml index a2c334352f..1182d7f013 100644 --- a/jscomp/core/lam_compile_const.ml +++ b/jscomp/core/lam_compile_const.ml @@ -33,8 +33,8 @@ module E = Js_exp_make let rec translate (x : Lam.constant ) : J.expression = match x with - | Const_js_true -> E.js_bool true - | Const_js_false -> E.js_bool false + | Const_js_true -> E.bool true + | Const_js_false -> E.bool false | Const_js_null -> E.nil | Const_js_undefined -> E.undefined | Const_int i -> E.int (Int32.of_int i) @@ -111,5 +111,5 @@ let translate_arg_cst (cst : External_arg_spec.cst) = | Arg_js_json s -> E.raw_js_code Exp s - | Arg_js_true -> E.js_bool true - | Arg_js_false -> E.js_bool false + | Arg_js_true -> E.bool true + | Arg_js_false -> E.bool false diff --git a/jscomp/core/lam_compile_external_call.ml b/jscomp/core/lam_compile_external_call.ml index 9fb9619194..dee4fc6885 100644 --- a/jscomp/core/lam_compile_external_call.ml +++ b/jscomp/core/lam_compile_external_call.ml @@ -322,8 +322,6 @@ let translate_ffi we need know whether we should call [add_js_module] or not *) begin match name, handle_external_opt external_module_name , scopes with - | "true", None, [] -> E.js_bool true - | "false", None, [] -> E.js_bool false | "null", None, [] -> E.nil | "undefined", None, [] -> E.undefined | _, _, _ -> diff --git a/jscomp/core/lam_dispatch_primitive.ml b/jscomp/core/lam_dispatch_primitive.ml index a72b399020..2c4b743ede 100644 --- a/jscomp/core/lam_dispatch_primitive.ml +++ b/jscomp/core/lam_dispatch_primitive.ml @@ -479,7 +479,15 @@ let translate loc (prim_name : string) | _ -> call Js_runtime_modules.string end - | "caml_bool_compare" + | "caml_bool_compare" -> + begin match args with + | [{expression_desc = Bool a} ; {expression_desc = Bool b} ] + -> + let c = compare a b in + E.int (if c = 0 then 0l else if c > 0 then 1l else -1l) + | _ -> + call Js_runtime_modules.caml_primitive + end | "caml_int_compare" | "caml_int32_compare" | "caml_nativeint_compare" @@ -718,8 +726,8 @@ let translate loc (prim_name : string) | "caml_notequal" -> begin match args with | [a1;b1] when - E.for_sure_js_null_undefined_boolean a1 - || E.for_sure_js_null_undefined_boolean b1 + E.for_sure_js_null_undefined a1 + || E.for_sure_js_null_undefined b1 -> E.neq_null_undefined_boolean a1 b1 (* FIXME address_equal *) @@ -730,7 +738,7 @@ let translate loc (prim_name : string) | "caml_equal" -> begin match args with | [a1;b1] when - E.for_sure_js_null_undefined_boolean a1 || E.for_sure_js_null_undefined_boolean b1 + E.for_sure_js_null_undefined a1 || E.for_sure_js_null_undefined b1 -> E.eq_null_undefined_boolean a1 b1 (* FIXME address_equal *) diff --git a/jscomp/test/gpr496_test.js b/jscomp/test/gpr496_test.js index 82a7876371..6fb81872b9 100644 --- a/jscomp/test/gpr496_test.js +++ b/jscomp/test/gpr496_test.js @@ -26,23 +26,15 @@ function eq(loc, x, y) { return /* () */0; } -var expected_004 = Caml_primitive.caml_bool_compare(false, true); - -var expected_005 = Caml_primitive.caml_bool_compare(true, false); - -var expected_006 = Caml_primitive.caml_bool_compare(false, false); - -var expected_007 = Caml_primitive.caml_bool_compare(true, true); - var expected = /* tuple */[ false, false, true, true, - expected_004, - expected_005, - expected_006, - expected_007 + -1, + 1, + 0, + 0 ]; var expected2 = /* tuple */[ @@ -56,23 +48,15 @@ var expected2 = /* tuple */[ 0 ]; -var u_004 = Caml_primitive.caml_bool_compare(false, true); - -var u_005 = Caml_primitive.caml_bool_compare(true, false); - -var u_006 = Caml_primitive.caml_bool_compare(false, false); - -var u_007 = Caml_primitive.caml_bool_compare(true, true); - var u = /* tuple */[ false, false, true, true, - u_004, - u_005, - u_006, - u_007 + -1, + 1, + 0, + 0 ]; eq("File \"gpr496_test.ml\", line 42, characters 12-19", expected, u); diff --git a/lib/whole_compiler.ml b/lib/whole_compiler.ml index 4d0c1e0a11..e034ff265b 100644 --- a/lib/whole_compiler.ml +++ b/lib/whole_compiler.ml @@ -63319,7 +63319,6 @@ and expression_desc = [typeof] is an operator *) | Typeof of expression - | Caml_not of expression (* 1 - v *) | Js_not of expression (* !v *) | String_of_small_int_array of expression (* String.fromCharCode.apply(null, args) *) @@ -68742,10 +68741,6 @@ let convert exports lam : _ * _ = prim ~primitive:Pdebugger ~args:[] loc | _ when s = "#null" -> Lconst (Const_js_null) - | _ when s = "#true" -> - Lconst (Const_js_true) - | _ when s = "#false" -> - Lconst (Const_js_false) | _ when s = "#undefined" -> Lconst (Const_js_undefined) | _ when s = "#init_mod" -> @@ -69628,8 +69623,7 @@ class virtual fold = (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) - (* 1 - v *) (* !v *) - (* String.fromCharCode.apply(null, args) *) + (* !v *) (* String.fromCharCode.apply(null, args) *) (* Convert JS boolean into OCaml boolean like [+true], note this ast talks using js terminnology unless explicity stated @@ -69870,7 +69864,6 @@ class virtual fold = | Anything_to_number _x -> let o = o#expression _x in o | Bool _x -> let o = o#bool _x in o | Typeof _x -> let o = o#expression _x in o - | Caml_not _x -> let o = o#expression _x in o | Js_not _x -> let o = o#expression _x in o | String_of_small_int_array _x -> let o = o#expression _x in o | Json_stringify _x -> let o = o#expression _x in o @@ -70181,7 +70174,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Array_copy _ (* | Tag_ml_obj _ *) | J.Anything_to_number _ - | Caml_not _ | Js_not _ | String_of_small_int_array _ | Json_stringify _ @@ -70335,7 +70327,6 @@ let rec eq_expression | Anything_to_number _ | Typeof _ - | Caml_not _ | Js_not _ | String_of_small_int_array _ | Json_stringify _ @@ -70892,11 +70883,6 @@ type binary_op = ?comment:string -> t -> t -> t type unary_op = ?comment:string -> t -> t -(** simplify - {[if b then ]} - there is no need to convert b into OCaml boolean under this scenario -*) -val ocaml_boolean_under_condition : t -> t @@ -71157,7 +71143,7 @@ val set_tag : ?comment:string -> J.expression -> J.expression -> t val set_length : ?comment:string -> J.expression -> J.expression -> t val obj_length : ?comment:string -> J.expression -> t -val bool_of_boolean : unary_op + val and_ : binary_op val or_ : binary_op @@ -71175,9 +71161,9 @@ val raw_js_code : ?comment:string -> J.code_info -> string -> t val nil : t val is_null : unary_op -val js_bool : bool -> t + val is_undef : unary_op -val for_sure_js_null_undefined_boolean : J.expression -> bool +val for_sure_js_null_undefined : J.expression -> bool val is_null_undefined : unary_op val not_implemented : ?comment:string -> string -> t @@ -71673,12 +71659,7 @@ let obj ?comment properties : t = (* Dot .....................**) -(** Convert a javascript boolean to ocaml bool - It's necessary for return value - this should be optmized away for [if] ,[cond] to produce - more readable code -*) -let bool_of_boolean ?comment (e : t) : t = e + (* var (Jident.create_js "true") *) @@ -71734,7 +71715,7 @@ let rec triple_equal ?comment (e0 : t) (e1 : t ) : t = | Char_of_int a , Char_of_int b -> triple_equal ?comment a b | _ -> - bool_of_boolean {expression_desc = Bin(EqEqEq, e0,e1); comment} + {expression_desc = Bin(EqEqEq, e0,e1); comment} let bin ?comment (op : J.binop) e0 e1 : t = match op with @@ -71812,8 +71793,7 @@ let rec or_ ?comment (e1 : t) (e2 : t) = let rec not ({expression_desc; comment} as e : t) : t = match expression_desc with | Number (Int {i; _}) -> - if i <> 0l then caml_false else caml_true - | Caml_not e -> e + bool (i = 0l ) | Js_not e -> e (* match expression_desc with *) (* can still hapen after some optimizations *) @@ -71825,42 +71805,11 @@ let rec not ({expression_desc; comment} as e : t) : t = | Bin(Le,a,b) -> {e with expression_desc = Bin (Gt,a,b)} | Bin(Gt,a,b) -> {e with expression_desc = Bin (Le,a,b)} | Bool b -> if b then caml_false else caml_true - | x -> {expression_desc = Caml_not e ; comment = None} - -let rec ocaml_boolean_under_condition (b : t) = - match b.expression_desc with - | Bin (And, x,y) -> - let x' = ocaml_boolean_under_condition x in - let y' = ocaml_boolean_under_condition y in - if x == x' && y==y' then b - else {b with expression_desc = Bin(And,x',y')} - | Bin(Or,x,y) -> - let x' = ocaml_boolean_under_condition x in - let y' = ocaml_boolean_under_condition y in - if x == x' && y == y' then b - else {b with expression_desc = Bin(Or,x',y')} - (** TODO: settle down Not semantics *) - | Caml_not u - | Js_not u - -> - let u' = ocaml_boolean_under_condition u in - if u' == u then b - else {b with expression_desc = Js_not u'} - | _ -> b - -(* TODO: could be more non undefined cases - check [caml_obj_is_block] - acutally we should avoid introducing undefined - as much as we can, this kind of inlining and mirco-optimization - can be done after we can inline runtime in the future - *) -(* | Bin (NotEqEq, ({expression_desc = Length _; _} as e1) , *) -(* {expression_desc = Var (Id ({name = "undefined"; _} as id))}), *) -(* _, _ *) -(* when Ext_ident.is_js id -> *) -(* econd e1 t f *) -(* | (Bin (Bor, v , {expression_desc = Number (Int {i = 0l ; _})})), _, _ - -> econd v t f *) + | x -> {expression_desc = Js_not e ; comment = None} + + + + let rec econd ?comment (b : t) (t : t) (f : t) : t = match b.expression_desc , t.expression_desc, f.expression_desc with @@ -71921,12 +71870,10 @@ let rec econd ?comment (b : t) (t : t) (f : t) : t = (* the same as above except we revert the [cond] expression *) econd (or_ b (not p1')) t branch_code0 - | Caml_not e, _, _ | Js_not e, _, _ -> econd ?comment e f t | _ -> - let b = ocaml_boolean_under_condition b in if Js_analyzer.eq_expression t f then if no_side_effect b then t else seq ?comment b t else @@ -71978,7 +71925,7 @@ let rec float_equal ?comment (e0 : t) (e1 : t) : t = float_equal ?comment a b | _ -> - bool_of_boolean {expression_desc = Bin(EqEqEq, e0,e1); comment} + {expression_desc = Bin(EqEqEq, e0,e1); comment} let int_equal = float_equal @@ -71992,7 +71939,7 @@ let rec string_equal ?comment (e0 : t) (e1 : t) : t = | Unicode a0, Unicode b0 -> bool (Ext_string.equal a0 b0) | _ , _ -> - bool_of_boolean {expression_desc = Bin(EqEqEq, e0,e1); comment} + {expression_desc = Bin(EqEqEq, e0,e1); comment} let is_type_number ?comment (e : t) : t = @@ -72120,7 +72067,7 @@ let uint32 ?comment n : J.expression = let string_comp cmp ?comment e0 e1 = - bool_of_boolean @@ bin ?comment cmp e0 e1 + bin ?comment cmp e0 e1 let set_length ?comment e tag : t = seq {expression_desc = Caml_block_set_length (e,tag); comment } unit @@ -72152,7 +72099,7 @@ let rec int_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) = } , args, call_info)} | Ceq, _, _ -> int_equal e0 e1 | _ -> - bool_of_boolean @@ bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 + bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 let bool_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) = match e0.expression_desc, e1.expression_desc with @@ -72186,10 +72133,10 @@ let bool_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) = | _ , _ -> bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 let float_comp cmp ?comment e0 e1 = - bool_of_boolean @@ bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 + bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 let js_comp cmp ?comment e0 e1 = - bool_of_boolean @@ bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 + bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1 let rec int32_lsr ?comment @@ -72471,17 +72418,13 @@ let of_block ?comment ?e block : t = let is_null ?comment x = triple_equal ?comment x nil -let js_true : t = caml_true -let js_false : t = caml_false -let js_bool = bool let is_undef ?comment x = triple_equal ?comment x undefined -let for_sure_js_null_undefined_boolean (x : t) = +let for_sure_js_null_undefined (x : t) = match x.expression_desc with | Var (Id ({name = "undefined" | "null"} as id)) -> Ext_ident.is_js id - | Bool _ -> true | _ -> false let is_null_undefined ?comment (x: t) : t = @@ -72491,7 +72434,7 @@ let is_null_undefined ?comment (x: t) : t = -> caml_true | Number _ | Array _ | Caml_block _ -> caml_false | _ -> - bool_of_boolean + { comment ; expression_desc = Is_null_undefined_to_boolean x } @@ -72513,10 +72456,9 @@ let eq_null_undefined_boolean ?comment (a : t) (b : t) = | Var (Id ({name = "null" | "undefined" as n1 } as id1) ), Var (Id ({name = "null" | "undefined" as n2 } as id2) ) when Ext_ident.is_js id1 && Ext_ident.is_js id2 - -> - if n1 = n2 then caml_true else caml_false + -> bool (n1 = n2) | _ -> - bool_of_boolean {expression_desc = Bin(EqEqEq, a, b); comment} + {expression_desc = Bin(EqEqEq, a, b); comment} @@ -72540,7 +72482,7 @@ let neq_null_undefined_boolean ?comment (a : t) (b : t) = -> if n1 <> n2 then caml_true else caml_false | _ -> - bool_of_boolean {expression_desc = Bin(NotEqEq, a, b); comment} + {expression_desc = Bin(NotEqEq, a, b); comment} @@ -73634,7 +73576,6 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) exp (E.econd e b a) :: acc | _, [], [] -> exp e :: acc - | Caml_not e, _ , _ :: _ | Js_not e, _ , _ :: _ -> aux ?comment e else_ then_ acc | _, [], _ @@ -73717,7 +73658,6 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) aux ?comment (E.or_ e (E.not pred)) cont then_ acc | _ -> - let e = E.ocaml_boolean_under_condition e in { statement_desc = If (e, then_, @@ -73763,7 +73703,6 @@ let rec while_ ?comment ?label ?env (e : E.t) (st : J.block) : t = (* | {expression_desc = Int_of_boolean e; _} -> *) (* while_ ?comment ?label e st *) | _ -> - let e = E.ocaml_boolean_under_condition e in let env = match env with | None -> Js_closure.empty () @@ -84950,8 +84889,6 @@ and if l > 0 then P.paren_group f 1 action else action () - - | Caml_not e | Js_not e -> let action () = P.string f "!" ; @@ -85436,7 +85373,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Typeof _ | Bind _ | Number _ - | Caml_not _ (* FIXME*) | Js_not _ | Bool _ | New _ @@ -88457,8 +88393,7 @@ class virtual map = (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) - (* 1 - v *) (* !v *) - (* String.fromCharCode.apply(null, args) *) + (* !v *) (* String.fromCharCode.apply(null, args) *) (* Convert JS boolean into OCaml boolean like [+true], note this ast talks using js terminnology unless explicity stated @@ -88715,7 +88650,6 @@ class virtual map = let _x = o#expression _x in Anything_to_number _x | Bool _x -> let _x = o#bool _x in Bool _x | Typeof _x -> let _x = o#expression _x in Typeof _x - | Caml_not _x -> let _x = o#expression _x in Caml_not _x | Js_not _x -> let _x = o#expression _x in Js_not _x | String_of_small_int_array _x -> let _x = o#expression _x in String_of_small_int_array _x @@ -93454,8 +93388,8 @@ module E = Js_exp_make let rec translate (x : Lam.constant ) : J.expression = match x with - | Const_js_true -> E.js_bool true - | Const_js_false -> E.js_bool false + | Const_js_true -> E.bool true + | Const_js_false -> E.bool false | Const_js_null -> E.nil | Const_js_undefined -> E.undefined | Const_int i -> E.int (Int32.of_int i) @@ -93532,8 +93466,8 @@ let translate_arg_cst (cst : External_arg_spec.cst) = | Arg_js_json s -> E.raw_js_code Exp s - | Arg_js_true -> E.js_bool true - | Arg_js_false -> E.js_bool false + | Arg_js_true -> E.bool true + | Arg_js_false -> E.bool false end module Js_of_lam_option : sig @@ -94188,8 +94122,6 @@ let translate_ffi we need know whether we should call [add_js_module] or not *) begin match name, handle_external_opt external_module_name , scopes with - | "true", None, [] -> E.js_bool true - | "false", None, [] -> E.js_bool false | "null", None, [] -> E.nil | "undefined", None, [] -> E.undefined | _, _, _ -> @@ -95468,7 +95400,15 @@ let translate loc (prim_name : string) | _ -> call Js_runtime_modules.string end - | "caml_bool_compare" + | "caml_bool_compare" -> + begin match args with + | [{expression_desc = Bool a} ; {expression_desc = Bool b} ] + -> + let c = compare a b in + E.int (if c = 0 then 0l else if c > 0 then 1l else -1l) + | _ -> + call Js_runtime_modules.caml_primitive + end | "caml_int_compare" | "caml_int32_compare" | "caml_nativeint_compare" @@ -95707,8 +95647,8 @@ let translate loc (prim_name : string) | "caml_notequal" -> begin match args with | [a1;b1] when - E.for_sure_js_null_undefined_boolean a1 - || E.for_sure_js_null_undefined_boolean b1 + E.for_sure_js_null_undefined a1 + || E.for_sure_js_null_undefined b1 -> E.neq_null_undefined_boolean a1 b1 (* FIXME address_equal *) @@ -95719,7 +95659,7 @@ let translate loc (prim_name : string) | "caml_equal" -> begin match args with | [a1;b1] when - E.for_sure_js_null_undefined_boolean a1 || E.for_sure_js_null_undefined_boolean b1 + E.for_sure_js_null_undefined a1 || E.for_sure_js_null_undefined b1 -> E.eq_null_undefined_boolean a1 b1 (* FIXME address_equal *)