Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion jscomp/core/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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) *)
Expand Down
2 changes: 0 additions & 2 deletions jscomp/core/js_analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
Expand Down Expand Up @@ -273,7 +272,6 @@ let rec eq_expression
| Anything_to_number _

| Typeof _
| Caml_not _
| Js_not _
| String_of_small_int_array _
| Json_stringify _
Expand Down
3 changes: 0 additions & 3 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "!" ;
Expand Down Expand Up @@ -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 _
Expand Down
82 changes: 19 additions & 63 deletions jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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") *)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 *)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand All @@ -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
}
Expand All @@ -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}



Expand All @@ -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}



Expand Down
11 changes: 3 additions & 8 deletions jscomp/core/js_exp_make.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down Expand Up @@ -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
Expand All @@ -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
4 changes: 1 addition & 3 deletions jscomp/core/js_fold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions jscomp/core/js_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions jscomp/core/js_stmt_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
| _, [], _
Expand Down Expand Up @@ -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_,
Expand Down Expand Up @@ -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 ()
Expand Down
4 changes: 0 additions & 4 deletions jscomp/core/lam.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ->
Expand Down
8 changes: 4 additions & 4 deletions jscomp/core/lam_compile_const.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
2 changes: 0 additions & 2 deletions jscomp/core/lam_compile_external_call.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
| _, _, _ ->
Expand Down
Loading