Skip to content

Commit 8924fdc

Browse files
authored
Merge pull request #2702 from BuckleScript/ocaml_boolean_cleanup
clean up/remove unused js code
2 parents e629430 + 34d14c8 commit 8924fdc

14 files changed

+88
-224
lines changed

jscomp/core/j.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ and expression_desc =
120120
[typeof] is an operator
121121
*)
122122
| Typeof of expression
123-
| Caml_not of expression (* 1 - v *)
124123
| Js_not of expression (* !v *)
125124
| String_of_small_int_array of expression
126125
(* String.fromCharCode.apply(null, args) *)

jscomp/core/js_analyzer.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) =
119119
| Array_copy _
120120
(* | Tag_ml_obj _ *)
121121
| J.Anything_to_number _
122-
| Caml_not _
123122
| Js_not _
124123
| String_of_small_int_array _
125124
| Json_stringify _
@@ -273,7 +272,6 @@ let rec eq_expression
273272
| Anything_to_number _
274273

275274
| Typeof _
276-
| Caml_not _
277275
| Js_not _
278276
| String_of_small_int_array _
279277
| Json_stringify _

jscomp/core/js_dump.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,6 @@ and
664664
if l > 0 then
665665
P.paren_group f 1 action
666666
else action ()
667-
668-
| Caml_not e
669667
| Js_not e ->
670668
let action () =
671669
P.string f "!" ;
@@ -1150,7 +1148,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t =
11501148
| Typeof _
11511149
| Bind _
11521150
| Number _
1153-
| Caml_not _ (* FIXME*)
11541151
| Js_not _
11551152
| Bool _
11561153
| New _

jscomp/core/js_exp_make.ml

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,7 @@ let obj ?comment properties : t =
488488

489489
(* Dot .....................**)
490490

491-
(** Convert a javascript boolean to ocaml bool
492-
It's necessary for return value
493-
this should be optmized away for [if] ,[cond] to produce
494-
more readable code
495-
*)
496-
let bool_of_boolean ?comment (e : t) : t = e
491+
497492

498493

499494
(* var (Jident.create_js "true") *)
@@ -549,7 +544,7 @@ let rec triple_equal ?comment (e0 : t) (e1 : t ) : t =
549544
| Char_of_int a , Char_of_int b ->
550545
triple_equal ?comment a b
551546
| _ ->
552-
bool_of_boolean {expression_desc = Bin(EqEqEq, e0,e1); comment}
547+
{expression_desc = Bin(EqEqEq, e0,e1); comment}
553548

554549
let bin ?comment (op : J.binop) e0 e1 : t =
555550
match op with
@@ -627,8 +622,7 @@ let rec or_ ?comment (e1 : t) (e2 : t) =
627622
let rec not ({expression_desc; comment} as e : t) : t =
628623
match expression_desc with
629624
| Number (Int {i; _}) ->
630-
if i <> 0l then caml_false else caml_true
631-
| Caml_not e -> e
625+
bool (i = 0l )
632626
| Js_not e -> e
633627
(* match expression_desc with *)
634628
(* can still hapen after some optimizations *)
@@ -640,42 +634,11 @@ let rec not ({expression_desc; comment} as e : t) : t =
640634
| Bin(Le,a,b) -> {e with expression_desc = Bin (Gt,a,b)}
641635
| Bin(Gt,a,b) -> {e with expression_desc = Bin (Le,a,b)}
642636
| Bool b -> if b then caml_false else caml_true
643-
| x -> {expression_desc = Caml_not e ; comment = None}
644-
645-
let rec ocaml_boolean_under_condition (b : t) =
646-
match b.expression_desc with
647-
| Bin (And, x,y) ->
648-
let x' = ocaml_boolean_under_condition x in
649-
let y' = ocaml_boolean_under_condition y in
650-
if x == x' && y==y' then b
651-
else {b with expression_desc = Bin(And,x',y')}
652-
| Bin(Or,x,y) ->
653-
let x' = ocaml_boolean_under_condition x in
654-
let y' = ocaml_boolean_under_condition y in
655-
if x == x' && y == y' then b
656-
else {b with expression_desc = Bin(Or,x',y')}
657-
(** TODO: settle down Not semantics *)
658-
| Caml_not u
659-
| Js_not u
660-
->
661-
let u' = ocaml_boolean_under_condition u in
662-
if u' == u then b
663-
else {b with expression_desc = Js_not u'}
664-
| _ -> b
665-
666-
(* TODO: could be more non undefined cases
667-
check [caml_obj_is_block]
668-
acutally we should avoid introducing undefined
669-
as much as we can, this kind of inlining and mirco-optimization
670-
can be done after we can inline runtime in the future
671-
*)
672-
(* | Bin (NotEqEq, ({expression_desc = Length _; _} as e1) , *)
673-
(* {expression_desc = Var (Id ({name = "undefined"; _} as id))}), *)
674-
(* _, _ *)
675-
(* when Ext_ident.is_js id -> *)
676-
(* econd e1 t f *)
677-
(* | (Bin (Bor, v , {expression_desc = Number (Int {i = 0l ; _})})), _, _
678-
-> econd v t f *)
637+
| x -> {expression_desc = Js_not e ; comment = None}
638+
639+
640+
641+
679642

680643
let rec econd ?comment (b : t) (t : t) (f : t) : t =
681644
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 =
736699
(* the same as above except we revert the [cond] expression *)
737700
econd (or_ b (not p1')) t branch_code0
738701

739-
| Caml_not e, _, _
740702
| Js_not e, _, _
741703
->
742704
econd ?comment e f t
743705
| _ ->
744-
let b = ocaml_boolean_under_condition b in
745706
if Js_analyzer.eq_expression t f then
746707
if no_side_effect b then t else seq ?comment b t
747708
else
@@ -793,7 +754,7 @@ let rec float_equal ?comment (e0 : t) (e1 : t) : t =
793754
float_equal ?comment a b
794755

795756
| _ ->
796-
bool_of_boolean {expression_desc = Bin(EqEqEq, e0,e1); comment}
757+
{expression_desc = Bin(EqEqEq, e0,e1); comment}
797758

798759

799760
let int_equal = float_equal
@@ -807,7 +768,7 @@ let rec string_equal ?comment (e0 : t) (e1 : t) : t =
807768
| Unicode a0, Unicode b0 -> bool (Ext_string.equal a0 b0)
808769
| _ , _
809770
->
810-
bool_of_boolean {expression_desc = Bin(EqEqEq, e0,e1); comment}
771+
{expression_desc = Bin(EqEqEq, e0,e1); comment}
811772

812773

813774
let is_type_number ?comment (e : t) : t =
@@ -935,7 +896,7 @@ let uint32 ?comment n : J.expression =
935896

936897

937898
let string_comp cmp ?comment e0 e1 =
938-
bool_of_boolean @@ bin ?comment cmp e0 e1
899+
bin ?comment cmp e0 e1
939900

940901
let set_length ?comment e tag : t =
941902
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) =
967928
} , args, call_info)}
968929
| Ceq, _, _ -> int_equal e0 e1
969930
| _ ->
970-
bool_of_boolean @@ bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
931+
bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
971932

972933
let bool_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) =
973934
match e0.expression_desc, e1.expression_desc with
@@ -1001,10 +962,10 @@ let bool_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) =
1001962
| _ , _ ->
1002963
bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
1003964
let float_comp cmp ?comment e0 e1 =
1004-
bool_of_boolean @@ bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
965+
bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
1005966

1006967
let js_comp cmp ?comment e0 e1 =
1007-
bool_of_boolean @@ bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
968+
bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
1008969

1009970

1010971
let rec int32_lsr ?comment
@@ -1286,17 +1247,13 @@ let of_block ?comment ?e block : t =
12861247

12871248
let is_null ?comment x = triple_equal ?comment x nil
12881249

1289-
let js_true : t = caml_true
1290-
let js_false : t = caml_false
1291-
let js_bool = bool
12921250

12931251
let is_undef ?comment x = triple_equal ?comment x undefined
12941252

1295-
let for_sure_js_null_undefined_boolean (x : t) =
1253+
let for_sure_js_null_undefined (x : t) =
12961254
match x.expression_desc with
12971255
| Var (Id ({name = "undefined" | "null"} as id))
12981256
-> Ext_ident.is_js id
1299-
| Bool _ -> true
13001257
| _ -> false
13011258

13021259
let is_null_undefined ?comment (x: t) : t =
@@ -1306,7 +1263,7 @@ let is_null_undefined ?comment (x: t) : t =
13061263
-> caml_true
13071264
| Number _ | Array _ | Caml_block _ -> caml_false
13081265
| _ ->
1309-
bool_of_boolean
1266+
13101267
{ comment ;
13111268
expression_desc = Is_null_undefined_to_boolean x
13121269
}
@@ -1328,10 +1285,9 @@ let eq_null_undefined_boolean ?comment (a : t) (b : t) =
13281285
| Var (Id ({name = "null" | "undefined" as n1 } as id1) ),
13291286
Var (Id ({name = "null" | "undefined" as n2 } as id2) )
13301287
when Ext_ident.is_js id1 && Ext_ident.is_js id2
1331-
->
1332-
if n1 = n2 then caml_true else caml_false
1288+
-> bool (n1 = n2)
13331289
| _ ->
1334-
bool_of_boolean {expression_desc = Bin(EqEqEq, a, b); comment}
1290+
{expression_desc = Bin(EqEqEq, a, b); comment}
13351291

13361292

13371293

@@ -1355,7 +1311,7 @@ let neq_null_undefined_boolean ?comment (a : t) (b : t) =
13551311
->
13561312
if n1 <> n2 then caml_true else caml_false
13571313
| _ ->
1358-
bool_of_boolean {expression_desc = Bin(NotEqEq, a, b); comment}
1314+
{expression_desc = Bin(NotEqEq, a, b); comment}
13591315

13601316

13611317

jscomp/core/js_exp_make.mli

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ type binary_op = ?comment:string -> t -> t -> t
5757

5858
type unary_op = ?comment:string -> t -> t
5959

60-
(** simplify
61-
{[if b then ]}
62-
there is no need to convert b into OCaml boolean under this scenario
63-
*)
64-
val ocaml_boolean_under_condition : t -> t
6560

6661

6762

@@ -322,7 +317,7 @@ val set_tag : ?comment:string -> J.expression -> J.expression -> t
322317

323318
val set_length : ?comment:string -> J.expression -> J.expression -> t
324319
val obj_length : ?comment:string -> J.expression -> t
325-
val bool_of_boolean : unary_op
320+
326321

327322
val and_ : binary_op
328323
val or_ : binary_op
@@ -340,8 +335,8 @@ val raw_js_code : ?comment:string -> J.code_info -> string -> t
340335
val nil : t
341336
val is_null : unary_op
342337

343-
val js_bool : bool -> t
338+
344339
val is_undef : unary_op
345-
val for_sure_js_null_undefined_boolean : J.expression -> bool
340+
val for_sure_js_null_undefined : J.expression -> bool
346341
val is_null_undefined : unary_op
347342
val not_implemented : ?comment:string -> string -> t

jscomp/core/js_fold.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ class virtual fold =
135135
(* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
136136
[typeof] is an operator
137137
*)
138-
(* 1 - v *) (* !v *)
139-
(* String.fromCharCode.apply(null, args) *)
138+
(* !v *) (* String.fromCharCode.apply(null, args) *)
140139
(* Convert JS boolean into OCaml boolean
141140
like [+true], note this ast talks using js
142141
terminnology unless explicity stated
@@ -377,7 +376,6 @@ class virtual fold =
377376
| Anything_to_number _x -> let o = o#expression _x in o
378377
| Bool _x -> let o = o#bool _x in o
379378
| Typeof _x -> let o = o#expression _x in o
380-
| Caml_not _x -> let o = o#expression _x in o
381379
| Js_not _x -> let o = o#expression _x in o
382380
| String_of_small_int_array _x -> let o = o#expression _x in o
383381
| Json_stringify _x -> let o = o#expression _x in o

jscomp/core/js_map.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ class virtual map =
148148
(* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
149149
[typeof] is an operator
150150
*)
151-
(* 1 - v *) (* !v *)
152-
(* String.fromCharCode.apply(null, args) *)
151+
(* !v *) (* String.fromCharCode.apply(null, args) *)
153152
(* Convert JS boolean into OCaml boolean
154153
like [+true], note this ast talks using js
155154
terminnology unless explicity stated
@@ -406,7 +405,6 @@ class virtual map =
406405
let _x = o#expression _x in Anything_to_number _x
407406
| Bool _x -> let _x = o#bool _x in Bool _x
408407
| Typeof _x -> let _x = o#expression _x in Typeof _x
409-
| Caml_not _x -> let _x = o#expression _x in Caml_not _x
410408
| Js_not _x -> let _x = o#expression _x in Js_not _x
411409
| String_of_small_int_array _x ->
412410
let _x = o#expression _x in String_of_small_int_array _x

jscomp/core/js_stmt_make.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
230230
exp (E.econd e b a) :: acc
231231
| _, [], []
232232
-> exp e :: acc
233-
| Caml_not e, _ , _ :: _
234233
| Js_not e, _ , _ :: _
235234
-> aux ?comment e else_ then_ acc
236235
| _, [], _
@@ -313,7 +312,6 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
313312
aux ?comment (E.or_ e (E.not pred)) cont then_ acc
314313

315314
| _ ->
316-
let e = E.ocaml_boolean_under_condition e in
317315
{ statement_desc =
318316
If (e,
319317
then_,
@@ -359,7 +357,6 @@ let rec while_ ?comment ?label ?env (e : E.t) (st : J.block) : t =
359357
(* | {expression_desc = Int_of_boolean e; _} -> *)
360358
(* while_ ?comment ?label e st *)
361359
| _ ->
362-
let e = E.ocaml_boolean_under_condition e in
363360
let env =
364361
match env with
365362
| None -> Js_closure.empty ()

jscomp/core/lam.ml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,10 +1828,6 @@ let convert exports lam : _ * _ =
18281828
prim ~primitive:Pdebugger ~args:[] loc
18291829
| _ when s = "#null" ->
18301830
Lconst (Const_js_null)
1831-
| _ when s = "#true" ->
1832-
Lconst (Const_js_true)
1833-
| _ when s = "#false" ->
1834-
Lconst (Const_js_false)
18351831
| _ when s = "#undefined" ->
18361832
Lconst (Const_js_undefined)
18371833
| _ when s = "#init_mod" ->

jscomp/core/lam_compile_const.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ module E = Js_exp_make
3333

3434
let rec translate (x : Lam.constant ) : J.expression =
3535
match x with
36-
| Const_js_true -> E.js_bool true
37-
| Const_js_false -> E.js_bool false
36+
| Const_js_true -> E.bool true
37+
| Const_js_false -> E.bool false
3838
| Const_js_null -> E.nil
3939
| Const_js_undefined -> E.undefined
4040
| Const_int i -> E.int (Int32.of_int i)
@@ -111,5 +111,5 @@ let translate_arg_cst (cst : External_arg_spec.cst) =
111111
| Arg_js_json s
112112
-> E.raw_js_code Exp s
113113

114-
| Arg_js_true -> E.js_bool true
115-
| Arg_js_false -> E.js_bool false
114+
| Arg_js_true -> E.bool true
115+
| Arg_js_false -> E.bool false

0 commit comments

Comments
 (0)