Skip to content

Commit 1fb566d

Browse files
committed
Done! no object used
1 parent 55dfb16 commit 1fb566d

18 files changed

+2306
-1418
lines changed

jscomp/core/js_analyzer.ml

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,47 +44,51 @@ let add_defined_idents (x : idents_stats) ident =
4444
Note such shaking is done in the toplevel, so that it requires us to
4545
flatten the statement first
4646
*)
47-
let free_variables (stats : idents_stats) : Js_iter.iter =
48-
object (self)
49-
inherit Js_iter.iter as super
50-
method! variable_declaration st =
51-
add_defined_idents stats st.ident;
52-
match st.value with
53-
| None
54-
-> ()
55-
| Some v
56-
->
57-
self # expression v
58-
method! ident id =
59-
(if not (Set_ident.mem stats.defined_idents id )then
60-
stats.used_idents <- Set_ident.add stats.used_idents id)
61-
62-
method! expression exp =
63-
match exp.expression_desc with
64-
| Fun(_, _,_, env)
65-
(** a optimization to avoid walking into funciton again
66-
if it's already comuted
67-
*)
68-
->
69-
stats.used_idents <-
70-
Set_ident.union (Js_fun_env.get_unbounded env) stats.used_idents
47+
let super = Js_record_iter.iter
48+
let free_variables (stats : idents_stats) = {
49+
super with
50+
variable_declaration = begin fun self st ->
51+
add_defined_idents stats st.ident;
52+
match st.value with
53+
| None
54+
-> ()
55+
| Some v
56+
->
57+
self.expression self v
7158

59+
end;
60+
ident = begin fun _ id ->
61+
if not (Set_ident.mem stats.defined_idents id )then
62+
stats.used_idents <- Set_ident.add stats.used_idents id
63+
end;
64+
expression = begin fun self exp ->
65+
match exp.expression_desc with
66+
| Fun(_, _,_, env)
67+
(** a optimization to avoid walking into funciton again
68+
if it's already comuted
69+
*)
70+
->
71+
stats.used_idents <-
72+
Set_ident.union (Js_fun_env.get_unbounded env) stats.used_idents
73+
| _
74+
->
75+
super.expression self exp
76+
end
77+
}
7278

73-
| _
74-
->
75-
super#expression exp
76-
end
7779

7880
let free_variables_of_statement st =
7981
let init = {used_idents = Set_ident.empty;
80-
defined_idents = Set_ident.empty} in
81-
let _ = (free_variables init)#statement st in
82+
defined_idents = Set_ident.empty} in
83+
let obj = free_variables init in
84+
obj.statement obj st ;
8285
Set_ident.diff init.used_idents init.defined_idents
8386

8487
let free_variables_of_expression st =
8588
let init = {used_idents = Set_ident.empty;
8689
defined_idents = Set_ident.empty} in
87-
let _ = (free_variables init)#expression st in
90+
let obj = free_variables init in
91+
obj.expression obj st ;
8892
Set_ident.diff init.used_idents init.defined_idents
8993

9094
let rec no_side_effect_expression_desc (x : J.expression_desc) =
@@ -139,26 +143,26 @@ and no_side_effect (x : J.expression) =
139143

140144
let no_side_effect_expression (x : J.expression) = no_side_effect x
141145

142-
let no_side_effect_obj : Js_iter.iter =
143-
object (self)
144-
inherit Js_iter.iter as super
145-
method! statement s =
146+
let super = Js_record_iter.iter
147+
let no_side_effect_obj =
148+
{super with
149+
statement = (fun self s ->
146150
match s.statement_desc with
147151
| Throw _
148152
| Debugger
149153
| Break
150154
| Variable _
151155
| Continue _ ->
152156
raise_notrace Not_found
153-
| Exp e -> self#expression e
157+
| Exp e -> self.expression self e
154158
| Int_switch _ | String_switch _ | ForRange _
155-
| If _ | While _ | Block _ | Return _ | Try _ -> super#statement s
156-
method! expression s =
159+
| If _ | While _ | Block _ | Return _ | Try _ -> super.statement self s );
160+
expression = begin fun _ s ->
157161
if not (no_side_effect_expression s) then raise_notrace Not_found
158-
end
162+
end}
159163
let no_side_effect_statement st =
160164
try
161-
no_side_effect_obj#statement st; true
165+
no_side_effect_obj.statement no_side_effect_obj st; true
162166
with _ -> false
163167

164168

jscomp/core/js_fold_basic.ml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,30 @@
2727

2828
let add_lam_module_ident = Lam_module_ident.Hash_set.add
2929
let create = Lam_module_ident.Hash_set.create
30-
let count_hard_dependencies hard_dependencies =
31-
object
32-
inherit Js_iter.iter as super
33-
method! module_id vid =
34-
add_lam_module_ident hard_dependencies vid
35-
method! expression x =
36-
(* check {!Js_pass_scope} when making changes *)
30+
31+
let super = Js_record_iter.iter
32+
let count_hard_dependencies hard_dependencies = {
33+
super with
34+
module_id = begin
35+
fun _ vid ->
36+
add_lam_module_ident hard_dependencies vid
37+
end;
38+
expression = begin
39+
fun self x ->
3740
(match Js_block_runtime.check_additional_id x with
3841
| Some id ->
3942
add_lam_module_ident hard_dependencies
4043
(Lam_module_ident.of_runtime
4144
id)
4245
| _ -> ());
43-
super#expression x
44-
end
46+
super.expression self x
47+
end
48+
}
4549

4650
let calculate_hard_dependencies block =
4751
let hard_dependencies = create 17 in
48-
(count_hard_dependencies hard_dependencies)#block block ;
52+
let obj = (count_hard_dependencies hard_dependencies) in
53+
obj.block obj block ;
4954
hard_dependencies
5055

5156
(*

jscomp/core/js_pass_flatten.ml

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,19 @@
3636
*)
3737
module E = Js_exp_make
3838
module S = Js_stmt_make
39-
40-
let flatten_map =
41-
object(self)
42-
inherit Js_map.map as super
43-
method! statement x =
39+
let super = Js_record_map.super
40+
let flatten_map = { super with
41+
42+
statement = (fun self x ->
4443
match x.statement_desc with
4544
| Exp ({expression_desc = Seq _; _} as v) ->
46-
S.block ( List.rev_map self#statement (Js_analyzer.rev_flatten_seq v ))
45+
S.block ( List.rev_map (fun x -> self.statement self x) (Js_analyzer.rev_flatten_seq v ))
4746
| Exp {expression_desc = Caml_block (args, _mutable_flag, _tag, _tag_info )}
4847
->
49-
S.block (Ext_list.map args (fun arg -> self#statement (S.exp arg)))
48+
S.block (Ext_list.map args (fun arg -> self.statement self (S.exp arg)))
5049
| Exp ({expression_desc = Cond(a,b,c); comment} ) ->
51-
{ statement_desc = If (a, [ self#statement (S.exp b)],
52-
[ self#statement (S.exp c)]); comment}
50+
{ statement_desc = If (a, [ self.statement self (S.exp b)],
51+
[ self.statement self (S.exp c)]); comment}
5352

5453
| Exp ({expression_desc = Bin(Eq, a, ({expression_desc = Seq _; _ } as v)); _} )
5554
->
@@ -58,8 +57,8 @@ let flatten_map =
5857
| {statement_desc = Exp last_one ; _} :: rest_rev
5958
->
6059
S.block (Ext_list.rev_map_append rest_rev
61-
[self#statement @@ S.exp (E.assign a last_one)]
62-
self#statement
60+
[self.statement self (S.exp (E.assign a last_one))]
61+
(fun x -> self.statement self x)
6362
)
6463
(* TODO: here we introduce a block, should avoid it *)
6564
(* super#statement *)
@@ -69,36 +68,36 @@ let flatten_map =
6968
end
7069
| Return {expression_desc = Cond (a,b,c); comment}
7170
->
72-
{ statement_desc = If (a, [self#statement (S.return_stmt b)],
73-
[ self#statement (S.return_stmt c)]); comment}
71+
{ statement_desc = If (a, [self.statement self (S.return_stmt b)],
72+
[ self.statement self (S.return_stmt c)]); comment}
7473

7574
| Return ({expression_desc = Seq _; _} as v) ->
7675
let block = Js_analyzer.rev_flatten_seq v in
7776
begin match block with
7877
| {statement_desc = Exp last_one ; _} :: rest_rev
7978
->
80-
super#statement
81-
(S.block (Ext_list.rev_map_append rest_rev [S.return_stmt last_one] (self#statement)))
79+
super.statement self
80+
(S.block (Ext_list.rev_map_append rest_rev [S.return_stmt last_one] (fun x -> self.statement self x)))
8281
| _ -> assert false
8382
end
8483
| Block [x]
8584
->
86-
self#statement x
87-
| _ -> super#statement x
88-
89-
method! block b =
85+
self.statement self x
86+
| _ -> super.statement self x
87+
);
88+
block = fun self b ->
9089
match b with
9190
| {statement_desc = Block bs } :: rest ->
92-
self#block ( bs @ rest)
91+
self.block self ( bs @ rest)
9392
| x::rest
9493
->
95-
let st = self#statement x in
96-
let block = self#block rest in
94+
let st = self.statement self x in
95+
let block = self.block self rest in
9796
begin match st.statement_desc with
9897
| Block bs -> bs @ block
9998
| _ -> st :: block
10099
end
101100
| [] -> []
102-
end
101+
}
103102

104-
let program ( x : J.program) = flatten_map # program x
103+
let program ( x : J.program) = flatten_map.program flatten_map x

jscomp/core/js_pass_flatten_and_mark_dead.ml

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,35 @@ type meta_info =
3939
| Recursive
4040

4141

42+
let super = Js_record_iter.iter
4243

4344
let mark_dead_code (js : J.program) : J.program =
4445
let ident_use_stats : meta_info Hash_ident.t
4546
= Hash_ident.create 17 in
46-
let mark_dead : Js_iter.iter = object (self)
47-
inherit Js_iter.iter
48-
method! ident ident =
47+
let mark_dead = { super with
48+
ident = (fun _ ident ->
4949
(match Hash_ident.find_opt ident_use_stats ident with
5050
| None -> (* First time *)
5151
Hash_ident.add ident_use_stats ident Recursive
5252
(* recursive identifiers *)
5353
| Some Recursive
5454
-> ()
55-
| Some (Info x) -> Js_op_util.update_used_stats x Used )
56-
method! variable_declaration vd =
55+
| Some (Info x) -> Js_op_util.update_used_stats x Used ));
56+
variable_declaration = fun self vd ->
5757
match vd.ident_info.used_stats with
5858
| Dead_pure
5959
-> ()
6060
| Dead_non_pure ->
6161
begin match vd.value with
6262
| None -> ()
63-
| Some x -> self#expression x
63+
| Some x -> self.expression self x
6464
end
6565
| _ ->
6666
let ({ident; ident_info ; value ; _} : J.variable_declaration) = vd in
6767
let pure =
6868
match value with
6969
| None -> true
70-
| Some x -> (self#expression x); Js_analyzer.no_side_effect_expression x in
70+
| Some x -> self.expression self x; Js_analyzer.no_side_effect_expression x in
7171
(
7272
let () =
7373
if Set_ident.mem js.export_set ident then
@@ -87,8 +87,8 @@ let mark_dead_code (js : J.program) : J.program =
8787
Hash_ident.add ident_use_stats ident (Info ident_info);
8888
Js_op_util.update_used_stats ident_info
8989
(if pure then Scanning_pure else Scanning_non_pure))
90-
end in
91-
let () = (mark_dead#program js) in
90+
} in
91+
mark_dead.program mark_dead js;
9292
Hash_ident.iter ident_use_stats (fun _id (info : meta_info) ->
9393
match info with
9494
| Info ({used_stats = Scanning_pure} as info) ->
@@ -152,17 +152,12 @@ let mark_dead_code (js : J.program) : J.program =
152152
]}
153153
154154
*)
155-
let subst_map (substitution : J.expression Hash_ident.t) = object (self)
156-
inherit Js_map.map as super
157155

158-
159-
160-
161-
162-
method add_substitue (ident : Ident.t) (e:J.expression) =
156+
let super = Js_record_map.super
157+
let add_substitue substitution (ident : Ident.t) (e:J.expression) =
163158
Hash_ident.replace substitution ident e
164-
165-
method! statement v =
159+
let subst_map (substitution : J.expression Hash_ident.t) = { super
160+
with statement = (fun self v ->
166161
match v.statement_desc with
167162
| Variable ({ident = _; ident_info = {used_stats = Dead_pure } ; _}) ->
168163
{v with statement_desc = Block []}
@@ -193,7 +188,7 @@ let subst_map (substitution : J.expression Hash_ident.t) = object (self)
193188
bottomline, when the block size is one, no need to do
194189
this
195190
*)
196-
let v' = self#expression x in
191+
let v' = self.expression self x in
197192
let match_id =
198193
Ext_ident.create
199194
(ident.name ^ "_" ^
@@ -212,7 +207,7 @@ let subst_map (substitution : J.expression Hash_ident.t) = object (self)
212207
expression_desc =
213208
Caml_block(List.rev e, Immutable, tag, tag_info)
214209
} in
215-
let () = self#add_substitue ident e in
210+
let () = add_substitue substitution ident e in
216211
(* let bindings = !bindings in *)
217212
let original_statement =
218213
{ v with
@@ -228,9 +223,9 @@ let subst_map (substitution : J.expression Hash_ident.t) = object (self)
228223
(fun (id,v) ->
229224
S.define_variable ~kind:Strict id v) )
230225
end
231-
| _ -> super#statement v
232-
233-
method! expression x =
226+
| _ -> super.statement self v
227+
);
228+
expression = fun self x ->
234229
match x.expression_desc with
235230
| Array_index ({expression_desc = Var (Id (id))},
236231
{expression_desc = Number (Int {i; _})})
@@ -246,11 +241,11 @@ let subst_map (substitution : J.expression Hash_ident.t) = object (self)
246241
| Some ({expression_desc = J.Var _ | Number _ | Str _ | Undefined} as x)
247242
-> x
248243
| None | Some _ ->
249-
super#expression x )
250-
| Some _ | None -> super#expression x )
244+
super.expression self x )
245+
| Some _ | None -> super.expression self x )
251246

252-
| _ -> super#expression x
253-
end
247+
| _ -> super.expression self x
248+
}
254249

255250
(* Top down or bottom up ?*)
256251
(* A pass to support nullary argument in JS
@@ -259,9 +254,9 @@ end
259254
*)
260255

261256
let program (js : J.program) =
262-
js
263-
|> (subst_map (Hash_ident.create 32) )#program
264-
|> mark_dead_code
257+
let obj = (subst_map (Hash_ident.create 32) ) in
258+
let js = obj.program obj js in
259+
mark_dead_code js
265260
(* |> mark_dead_code *)
266261
(* mark dead code twice does have effect in some cases, however, we disabled it
267262
since the benefit is not obvious

0 commit comments

Comments
 (0)