Skip to content

Commit 01275f2

Browse files
committed
clean up
1 parent 5ec3c57 commit 01275f2

8 files changed

+164
-182
lines changed

jscomp/core/js_exp_make.ml

+65-74
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,10 @@ let string_length ?comment (e : t) : t =
399399
(* No optimization for {j||j}*)
400400
| _ -> { expression_desc = Length (e, String) ; comment }
401401

402+
(* TODO: use [Buffer] instead? *)
402403
let bytes_length ?comment (e : t) : t =
403404
match e.expression_desc with
404-
(* TODO: use array instead? *)
405405
| Array (l, _) -> int ?comment (Int32.of_int (List.length l))
406-
| Str(_,v) -> int ?comment (Int32.of_int @@ String.length v)
407-
(* No optimization for unicode *)
408406
| _ -> { expression_desc = Length (e, Bytes) ; comment }
409407

410408
let function_length ?comment (e : t) : t =
@@ -416,7 +414,6 @@ let function_length ?comment (e : t) : t =
416414
(Int32.of_int
417415
(if b then params_length - 1
418416
else params_length))
419-
(* TODO: optimize if [e] is know at compile time *)
420417
| _ -> { expression_desc = Length (e, Function) ; comment }
421418

422419
(** no dependency introduced *)
@@ -467,11 +464,11 @@ let obj ?comment properties : t =
467464

468465

469466
(* var (Jident.create_js "true") *)
470-
let caml_true : t = {comment = None; expression_desc = Bool true }
467+
let true_ : t = {comment = None; expression_desc = Bool true }
471468

472-
let caml_false : t = {comment = None; expression_desc = Bool false }
469+
let false_ : t = {comment = None; expression_desc = Bool false }
473470

474-
let bool v = if v then caml_true else caml_false
471+
let bool v = if v then true_ else false_
475472

476473
(** Arith operators *)
477474
(* Dot .....................**)
@@ -499,13 +496,13 @@ let rec triple_equal ?comment (e0 : t) (e1 : t ) : t =
499496
| Bool _ | Number _ | Typeof _
500497
| Fun _ | Array _ | Caml_block _ )
501498
when no_side_effect e1 ->
502-
caml_false (* TODO: rename it as [caml_false] *)
499+
false_ (* TODO: rename it as [caml_false] *)
503500
|
504501
(Char_of_int _ | Char_to_int _
505502
| Bool _ | Number _ | Typeof _
506503
| Fun _ | Array _ | Caml_block _ ), (Null|Undefined)
507504
when no_side_effect e0 ->
508-
caml_false
505+
false_
509506
| Str (_,x), Str (_,y) -> (* CF*)
510507
bool (Ext_string.equal x y)
511508
| Char_to_int a , Char_to_int b ->
@@ -523,9 +520,9 @@ let rec triple_equal ?comment (e0 : t) (e1 : t ) : t =
523520
| Undefined, Optional_block _
524521
| Optional_block _, Undefined
525522
| Null, Undefined
526-
| Undefined, Null -> caml_false
523+
| Undefined, Null -> false_
527524
| Null, Null
528-
| Undefined, Undefined -> caml_true
525+
| Undefined, Undefined -> true_
529526
| _ ->
530527
{expression_desc = Bin(EqEqEq, e0,e1); comment}
531528

@@ -540,12 +537,8 @@ let bin ?comment (op : J.binop) e0 e1 : t =
540537
optimizations
541538
We wrap all boolean functions here, since OCaml boolean is a
542539
bit different from Javascript, so that we can change it in the future
543-
*)
544540
545-
let rec and_ ?comment (e1 : t) (e2 : t) : t =
546-
match e1.expression_desc, e2.expression_desc with
547-
(*
548-
{[ a && (b && c) === (a && b ) && c ]}
541+
{[ a && (b && c) === (a && b ) && c ]}
549542
is not used: benefit is not clear
550543
| Int_of_boolean e10, Bin(And, {expression_desc = Int_of_boolean e20 }, e3)
551544
->
@@ -555,15 +548,15 @@ let rec and_ ?comment (e1 : t) (e2 : t) : t =
555548
J.Int_of_boolean { expression_desc = Bin (And, e10,e20); comment = None}
556549
}
557550
e3
558-
*)
559-
(* Note that
560-
{[ "" && 3 ]}
551+
Note that
552+
{[ "" && 3 ]}
561553
return "" instead of false, so [e1] is indeed useful
562-
*)
563-
564-
(* optimization if [e1 = e2], then and_ e1 e2 -> e2
554+
optimization if [e1 = e2], then and_ e1 e2 -> e2
565555
be careful for side effect
566-
*)
556+
*)
557+
558+
let rec and_ ?comment (e1 : t) (e2 : t) : t =
559+
match e1.expression_desc, e2.expression_desc with
567560
| Var i, Var j when Js_op_util.same_vident i j
568561
->
569562
e1
@@ -600,50 +593,48 @@ let rec or_ ?comment (e1 : t) (e2 : t) =
600593
(* TODO:
601594
when comparison with Int
602595
it is right that !(x > 3 ) -> x <= 3 *)
603-
let rec not ({expression_desc; comment} as e : t) : t =
604-
match expression_desc with
605-
| Number (Int {i; _}) ->
606-
bool (i = 0l )
596+
let rec not ( e : t) : t =
597+
match e.expression_desc with
598+
| Number (Int {i; _}) -> bool (i = 0l )
607599
| Js_not e -> e
608-
(* match expression_desc with *)
609-
(* can still hapen after some optimizations *)
610-
| Bin(EqEqEq , e0,e1)
611-
-> {expression_desc = Bin(NotEqEq, e0,e1); comment}
612-
| Bin(NotEqEq , e0,e1) -> {expression_desc = Bin(EqEqEq, e0,e1); comment}
600+
| Bool b -> if b then false_ else true_
601+
| Bin(EqEqEq , e0,e1) -> {e with expression_desc = Bin(NotEqEq, e0,e1)}
602+
| Bin(NotEqEq , e0,e1) -> {e with expression_desc = Bin(EqEqEq, e0,e1)}
613603
| Bin(Lt, a, b) -> {e with expression_desc = Bin (Ge,a,b)}
614604
| Bin(Ge,a,b) -> {e with expression_desc = Bin (Lt,a,b)}
615605
| Bin(Le,a,b) -> {e with expression_desc = Bin (Gt,a,b)}
616606
| Bin(Gt,a,b) -> {e with expression_desc = Bin (Le,a,b)}
617-
| Bool b -> if b then caml_false else caml_true
618607
| x -> {expression_desc = Js_not e ; comment = None}
619608

620609

621610

622611

623612

624-
let rec econd ?comment (b : t) (t : t) (f : t) : t =
625-
match b.expression_desc , t.expression_desc, f.expression_desc with
626-
| Bool false, _, _ -> f
613+
let rec econd ?comment (pred : t) (ifso : t) (ifnot : t) : t =
614+
match pred.expression_desc , ifso.expression_desc, ifnot.expression_desc with
615+
| Bool false, _, _ -> ifnot
627616
| Number ((Int { i = 0l; _}) ), _, _
628-
-> f (* TODO: constant folding: could be refined *)
629-
| (Number _ | Array _ | Caml_block _ | Optional_block _), _, _ when no_side_effect b
630-
-> t (* a block can not be false in OCAML, CF - relies on flow inference*)
631-
| Bool true, _, _ -> t
617+
-> ifnot
618+
| (Number _ | Array _ | Caml_block _ | Optional_block _), _, _
619+
when no_side_effect pred
620+
-> ifso (* a block can not be false in OCAML, CF - relies on flow inference*)
621+
| Bool true, _, _ -> ifso
632622
| (Bin (Ge,
633623
({expression_desc = Length _ ;
634624
_}), {expression_desc = Number (Int { i = 0l ; _})})), _, _
635-
-> f
625+
-> ifnot
636626
| (Bin (Gt,
637627
({expression_desc = Length _;
638628
_} as pred ),
639629
({expression_desc = Number (Int {i = 0l; }) } as zero) )), _, _
640630
->
641631
(** Add comment when simplified *)
642-
econd ?comment {b with expression_desc = (Bin (NotEqEq,
643-
pred , zero ))} t f
644-
645-
| _, (Cond (p1, branch_code0, branch_code1)), _
646-
when Js_analyzer.eq_expression branch_code1 f
632+
econd ?comment
633+
{pred with
634+
expression_desc =
635+
Bin (NotEqEq, pred, zero)} ifso ifnot
636+
| _, (Cond (pred1, ifso1, ifnot1)), _
637+
when Js_analyzer.eq_expression ifnot1 ifnot
647638
->
648639
(* {[
649640
if b then (if p1 then branch_code0 else branch_code1)
@@ -654,15 +645,15 @@ let rec econd ?comment (b : t) (t : t) (f : t) : t =
654645
if b && p1 then branch_code0 else branch_code1
655646
]}
656647
*)
657-
econd (and_ b p1) branch_code0 f
648+
econd (and_ pred pred1) ifso1 ifnot
658649
| _, (Cond (p1, branch_code0, branch_code1)), _
659-
when Js_analyzer.eq_expression branch_code0 f
650+
when Js_analyzer.eq_expression branch_code0 ifnot
660651
->
661652
(* the same as above except we revert the [cond] expression *)
662-
econd (and_ b (not p1)) branch_code1 f
653+
econd (and_ pred (not p1)) branch_code1 ifnot
663654

664655
| _, _, (Cond (p1', branch_code0, branch_code1))
665-
when Js_analyzer.eq_expression t branch_code0
656+
when Js_analyzer.eq_expression ifso branch_code0
666657
(*
667658
{[
668659
if b then branch_code0 else (if p1' then branch_code0 else branch_code1)
@@ -673,21 +664,21 @@ let rec econd ?comment (b : t) (t : t) (f : t) : t =
673664
]}
674665
*)
675666
->
676-
econd (or_ b p1') t branch_code1
667+
econd (or_ pred p1') ifso branch_code1
677668
| _, _, (Cond (p1', branch_code0, branch_code1))
678-
when Js_analyzer.eq_expression t branch_code1
669+
when Js_analyzer.eq_expression ifso branch_code1
679670
->
680671
(* the same as above except we revert the [cond] expression *)
681-
econd (or_ b (not p1')) t branch_code0
672+
econd (or_ pred (not p1')) ifso branch_code0
682673

683674
| Js_not e, _, _
684675
->
685-
econd ?comment e f t
676+
econd ?comment e ifnot ifso
686677
| _ ->
687-
if Js_analyzer.eq_expression t f then
688-
if no_side_effect b then t else seq ?comment b t
678+
if Js_analyzer.eq_expression ifso ifnot then
679+
if no_side_effect pred then ifso else seq ?comment pred ifso
689680
else
690-
{expression_desc = Cond(b,t,f); comment}
681+
{expression_desc = Cond(pred,ifso,ifnot); comment}
691682

692683

693684
let rec float_equal ?comment (e0 : t) (e1 : t) : t =
@@ -724,7 +715,7 @@ let rec float_equal ?comment (e0 : t) (e1 : t) : t =
724715
*)
725716
float_equal ?comment a e1
726717
| Number (Float {f = f0; _}), Number (Float {f = f1 ; }) when f0 = f1 ->
727-
caml_true
718+
true_
728719

729720
| Char_to_int a , Char_to_int b ->
730721
float_equal ?comment a b
@@ -883,14 +874,14 @@ let rec int_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) =
883874
} , args, call_info)}
884875
| Ceq, Optional_block _, Undefined
885876
| Ceq, Undefined, Optional_block _
886-
-> caml_false
877+
-> false_
887878
| Ceq, _, _ -> int_equal e0 e1
888879

889880
| Cneq, Optional_block _, Undefined
890881
| Cneq, Undefined , Optional_block _
891882
| Cneq, Caml_block _ , Number _
892883
| Cneq, Number _, Caml_block _
893-
-> caml_true
884+
-> true_
894885
| _ ->
895886
bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
896887

@@ -908,8 +899,8 @@ let bool_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) =
908899
| {expression_desc = Bool true}, rest
909900
| rest, {expression_desc = Bool false} ->
910901
begin match cmp with
911-
| Clt -> seq rest caml_false
912-
| Cge -> seq rest caml_true
902+
| Clt -> seq rest false_
903+
| Cge -> seq rest true_
913904
| Cle
914905
| Cgt
915906
| Ceq
@@ -919,8 +910,8 @@ let bool_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) =
919910
| {expression_desc = Bool false}, rest
920911
->
921912
begin match cmp with
922-
| Cle -> seq rest caml_true
923-
| Cgt -> seq rest caml_false
913+
| Cle -> seq rest true_
914+
| Cgt -> seq rest false_
924915
| Clt
925916
| Cge
926917
| Ceq
@@ -1229,8 +1220,8 @@ let for_sure_js_null_undefined (x : t) =
12291220
let is_null_undefined ?comment (x: t) : t =
12301221
match x.expression_desc with
12311222
| Null | Undefined
1232-
-> caml_true
1233-
| Number _ | Array _ | Caml_block _ -> caml_false
1223+
-> true_
1224+
| Number _ | Array _ | Caml_block _ -> false_
12341225
| _ ->
12351226
{ comment ;
12361227
expression_desc = Is_null_or_undefined x
@@ -1243,18 +1234,18 @@ let eq_null_undefined_boolean ?comment (a : t) (b : t) =
12431234
| Bool _ | Number _ | Typeof _
12441235
| Fun _ | Array _ | Caml_block _ )
12451236
->
1246-
caml_false
1237+
false_
12471238
| (Char_of_int _ | Char_to_int _
12481239
| Bool _ | Number _ | Typeof _
12491240
| Fun _ | Array _ | Caml_block _ ),
12501241
(Null | Undefined)
12511242
->
1252-
caml_false
1243+
false_
12531244
| (Null, Undefined)
1254-
| (Undefined, Null) -> caml_false
1245+
| (Undefined, Null) -> false_
12551246
| (Null, Null)
12561247
| (Undefined, Undefined)
1257-
-> caml_true
1248+
-> true_
12581249
| _ ->
12591250
{expression_desc = Bin(EqEqEq, a, b); comment}
12601251

@@ -1267,19 +1258,19 @@ let neq_null_undefined_boolean ?comment (a : t) (b : t) =
12671258
| Bool _ | Number _ | Typeof _
12681259
| Fun _ | Array _ | Caml_block _ )
12691260
->
1270-
caml_true
1261+
true_
12711262
| (Char_of_int _ | Char_to_int _
12721263
| Bool _ | Number _ | Typeof _
12731264
| Fun _ | Array _ | Caml_block _ ),
12741265
(Null | Undefined)
12751266
->
1276-
caml_true
1267+
true_
12771268
| (Null , Null )
12781269
| (Undefined, Undefined)
1279-
-> caml_false
1270+
-> false_
12801271
| (Null, Undefined)
12811272
| (Undefined, Null)
1282-
-> caml_true
1273+
-> true_
12831274
| _ ->
12841275
{expression_desc = Bin(NotEqEq, a, b); comment}
12851276

jscomp/core/js_exp_make.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,9 @@ val obj :
270270
J.property_map ->
271271
t
272272

273-
val caml_true : t
273+
val true_ : t
274274

275-
val caml_false : t
275+
val false_ : t
276276

277277
val bool : bool -> t
278278

jscomp/core/js_of_lam_option.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ let is_none_static (arg : J.expression_desc ) = arg = Undefined
4949

5050
let is_not_none (e : J.expression) : J.expression =
5151
let desc = e.expression_desc in
52-
if is_none_static desc then E.caml_false
52+
if is_none_static desc then E.false_
5353
else match desc with
54-
| Optional_block _ -> E.caml_true
54+
| Optional_block _ -> E.true_
5555
| _ ->
5656
E.not (E.triple_equal e none)
5757

jscomp/core/js_op.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type binop =
4444
| And
4545
| EqEqEq
4646
| NotEqEq
47-
| InstanceOf
47+
(* | InstanceOf *)
4848

4949
| Lt
5050
| Le

jscomp/core/js_op_util.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let op_prec (op : Js_op.binop ) =
4040
| Or -> 3, 3, 3
4141
| And -> 4, 4, 4
4242
| EqEqEq | NotEqEq -> 8, 8, 9
43-
| Gt | Ge | Lt | Le | InstanceOf -> 9, 9, 10
43+
| Gt | Ge | Lt | Le (* | InstanceOf *) -> 9, 9, 10
4444
| Bor -> 5, 5, 5
4545
| Bxor -> 6, 6, 6
4646
| Band -> 7, 7, 7
@@ -81,7 +81,7 @@ let op_str (op : Js_op.binop) =
8181
| Le -> "<="
8282
| Gt -> ">"
8383
| Ge -> ">="
84-
| InstanceOf -> "instanceof"
84+
(* | InstanceOf -> "instanceof" *)
8585

8686
let op_int_str (op : Js_op.int_op) =
8787
match op with

0 commit comments

Comments
 (0)