Skip to content

Commit 136c320

Browse files
authored
Merge pull request #4913 from rescript-lang/support_inline
refactoring object usage, avoid functional objects
2 parents 226bfcf + ef94cb4 commit 136c320

16 files changed

+389
-732
lines changed

jscomp/core/j.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
1+
(* Copyright (C) 2015- Authors of ReScript
22
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
@@ -70,7 +70,6 @@ and required_modules = module_id list
7070
currently we always use quote
7171
*)
7272
and property_name = Js_op.property_name
73-
and jsint = int32
7473
and ident = Ident.t
7574
and module_id = {
7675
id : ident; kind : Js_op.kind

jscomp/core/js_analyzer.ml

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@
2929

3030

3131

32+
type idents_stats = {
33+
mutable used_idents : Set_ident.t ;
34+
mutable defined_idents : Set_ident.t;
35+
}
3236

37+
let add_defined_idents (x : idents_stats) ident =
38+
x.defined_idents <- Set_ident.add x.defined_idents ident
3339

3440
(* Assume that functions already calculated closure correctly
3541
Maybe in the future, we should add a dirty flag, to mark the calcuated
@@ -38,48 +44,48 @@
3844
Note such shaking is done in the toplevel, so that it requires us to
3945
flatten the statement first
4046
*)
41-
let free_variables used_idents defined_idents =
47+
let free_variables (stats : idents_stats) : Js_fold.fold =
4248
object (self)
4349
inherit Js_fold.fold as super
44-
val defined_idents = defined_idents
45-
val used_idents = used_idents
4650
method! variable_declaration st =
47-
match st with
48-
| { ident; value = None}
51+
add_defined_idents stats st.ident;
52+
match st.value with
53+
| None
54+
-> self
55+
| Some v
4956
->
50-
{< defined_idents = Set_ident.add defined_idents ident >}
51-
| { ident; value = Some v}
52-
->
53-
{< defined_idents = Set_ident.add defined_idents ident >} # expression v
57+
self # expression v
5458
method! ident id =
55-
if Set_ident.mem defined_idents id then self
56-
else {<used_idents = Set_ident.add used_idents id>}
59+
(if not (Set_ident.mem stats.defined_idents id )then
60+
stats.used_idents <- Set_ident.add stats.used_idents id);
61+
self
5762
method! expression exp =
58-
5963
match exp.expression_desc with
6064
| Fun(_, _,_, env)
6165
(** a optimization to avoid walking into funciton again
6266
if it's already comuted
6367
*)
6468
->
65-
{< used_idents =
66-
Set_ident.union (Js_fun_env.get_unbounded env) used_idents >}
69+
stats.used_idents <-
70+
Set_ident.union (Js_fun_env.get_unbounded env) stats.used_idents;
71+
self
6772

6873
| _
6974
->
7075
super#expression exp
71-
72-
method get_depenencies =
73-
Set_ident.diff used_idents defined_idents
74-
method get_used_idents = used_idents
75-
method get_defined_idents = defined_idents
7676
end
7777

78-
let free_variables_of_statement used_idents defined_idents st =
79-
((free_variables used_idents defined_idents)#statement st) # get_depenencies
78+
let free_variables_of_statement st =
79+
let init = {used_idents = Set_ident.empty;
80+
defined_idents = Set_ident.empty} in
81+
let _ = (free_variables init)#statement st in
82+
Set_ident.diff init.used_idents init.defined_idents
8083

81-
let free_variables_of_expression used_idents defined_idents st =
82-
((free_variables used_idents defined_idents)#expression st) # get_depenencies
84+
let free_variables_of_expression st =
85+
let init = {used_idents = Set_ident.empty;
86+
defined_idents = Set_ident.empty} in
87+
let _ = (free_variables init)#expression st in
88+
Set_ident.diff init.used_idents init.defined_idents
8389

8490
let rec no_side_effect_expression_desc (x : J.expression_desc) =
8591
match x with
@@ -133,33 +139,33 @@ and no_side_effect (x : J.expression) =
133139

134140
let no_side_effect_expression (x : J.expression) = no_side_effect x
135141

136-
let no_side_effect init =
142+
let no_side_effect clean : Js_fold.fold =
137143
object (self)
138144
inherit Js_fold.fold as super
139-
val no_side_effect = init
140-
method get_no_side_effect = no_side_effect
141-
142145
method! statement s =
143-
if not no_side_effect then self else
146+
if not !clean then self else
144147
match s.statement_desc with
145148
| Throw _
146149
| Debugger
147150
| Break
148151
| Variable _
149152
| Continue _ ->
150-
{< no_side_effect = false>}
153+
clean := false ; self
151154
| Exp e -> self#expression e
152155
| Int_switch _ | String_switch _ | ForRange _
153156
| If _ | While _ | Block _ | Return _ | Try _ -> super#statement s
154157
method! list f x =
155-
if not self#get_no_side_effect then self else super#list f x
158+
if not !clean then self else super#list f x
156159
method! expression s =
157-
if not no_side_effect then self
158-
else {< no_side_effect = no_side_effect_expression s >}
159-
160+
(if !clean then
161+
clean := no_side_effect_expression s);
162+
self
160163
(** only expression would cause side effec *)
161164
end
162-
let no_side_effect_statement st = ((no_side_effect true)#statement st)#get_no_side_effect
165+
let no_side_effect_statement st =
166+
let clean = ref true in
167+
let _ : Js_fold.fold = ((no_side_effect clean)#statement st) in
168+
!clean
163169

164170
(* TODO: generate [fold2]
165171
This make sense, for example:

jscomp/core/js_analyzer.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
*)
3636

3737
val free_variables_of_statement :
38-
Set_ident.t -> Set_ident.t -> J.statement -> Set_ident.t
38+
J.statement -> Set_ident.t
3939

4040
val free_variables_of_expression :
41-
Set_ident.t -> Set_ident.t -> J.finish_ident_expression -> Set_ident.t
41+
J.finish_ident_expression -> Set_ident.t
4242

4343
(* val no_side_effect_expression_desc :
4444
J.expression_desc -> bool *)

jscomp/core/js_fold.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ method exports : exports -> 'self_type = o#unknown
2828
method tag_info : tag_info -> 'self_type = o#unknown
2929
method required_modules : required_modules -> 'self_type = o#list (fun o -> o#module_id)
3030
method property_name : property_name -> 'self_type = o#unknown
31-
method jsint : jsint -> 'self_type = o#int32
3231
method ident : ident -> 'self_type = o#unknown
3332
method module_id : module_id -> 'self_type = fun { id = _x0;kind = _x1} -> let o = o#ident _x0 in
3433
let o = o#unknown _x1 in o

jscomp/core/js_fold_basic.ml

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,12 @@
2424

2525

2626

27-
(* class count_deps (add : Ident.t -> unit ) =
28-
object(self)
29-
inherit Js_fold.fold as super
30-
method! expression lam =
31-
match lam.expression_desc with
32-
| Fun (_, _, block, _) -> self#block block
33-
(** Call
34-
actually depends on parameter,
35-
since closure
36-
{[
37-
n = n - 1
38-
acc = () => n
39-
]}
40-
should be
41-
42-
{[
43-
acc = (function (n) {() => n} (n))
44-
n = n - 1
45-
]}
46-
*)
47-
| _ -> super#expression lam
48-
method! ident x = add x ; self
49-
end *)
5027

5128
let add_lam_module_ident = Lam_module_ident.Hash_set.add
5229
let create = Lam_module_ident.Hash_set.create
53-
class count_hard_dependencies =
30+
let count_hard_dependencies hard_dependencies =
5431
object(self : 'self_type)
5532
inherit Js_fold.fold as super
56-
val hard_dependencies = create 17
5733
method! module_id vid =
5834
add_lam_module_ident hard_dependencies vid; self
5935
method! expression x : 'self_type =
@@ -65,11 +41,12 @@ class count_hard_dependencies =
6541
id)
6642
| _ -> ());
6743
super#expression x
68-
method get_hard_dependencies = hard_dependencies
6944
end
7045

7146
let calculate_hard_dependencies block =
72-
((new count_hard_dependencies)#block block) # get_hard_dependencies
47+
let hard_dependencies = create 17 in
48+
let _ : Js_fold.fold = (count_hard_dependencies hard_dependencies)#block block in
49+
hard_dependencies
7350

7451
(*
7552
Given a set of [variables], count which variables [lam] will depend on

jscomp/core/js_map.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ method exports : exports -> exports = o#unknown
3131
method tag_info : tag_info -> tag_info = o#unknown
3232
method required_modules : required_modules -> required_modules = o#list (fun o -> o#module_id)
3333
method property_name : property_name -> property_name = o#unknown
34-
method jsint : jsint -> jsint = o#int32
3534
method ident : ident -> ident = o#unknown
3635
method module_id : module_id -> module_id = fun { id = _x0;kind = _x1} -> let _x0 = o#ident _x0 in
3736
let _x1 = o#unknown _x1 in {id = _x0;kind = _x1}

jscomp/core/js_of_lam_block.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ val make_block :
3838
val field :
3939
Lam_compat.field_dbg_info ->
4040
J.expression ->
41-
J.jsint ->
41+
int32 ->
4242
J.expression
4343

4444
val field_by_exp :
@@ -48,7 +48,7 @@ val field_by_exp :
4848

4949
val set_field :
5050
Lam_compat.set_field_dbg_info ->
51-
J.expression -> J.jsint -> J.expression -> J.expression
51+
J.expression -> int32 -> J.expression -> J.expression
5252

5353
val set_field_by_exp :
5454
J.expression ->

jscomp/core/js_pass_flatten_and_mark_dead.ml

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,7 @@
3232
module E = Js_exp_make
3333
module S = Js_stmt_make
3434

35-
(* class count var = object (self : 'self)
36-
val mutable appears = 0
37-
inherit Js_fold.fold as super
38-
method! ident x =
39-
(if Ident.same x var then
40-
appears <- appears + 1);
41-
self
42-
method get_appears = appears
43-
end *)
44-
45-
(* rewrite return for current block, but don't go into
46-
inner function, mostly for inlinning
47-
*)
48-
(* class rewrite_return ?return_value ()=
49-
let mk_return =
50-
match return_value with
51-
| None -> fun e -> S.exp e
52-
| Some ident -> fun e ->
53-
S.define_variable ~kind:Variable ident e in
54-
object (self : 'self)
55-
inherit Js_map.map as super
56-
method! statement x =
57-
match x.statement_desc with
58-
| Return {return_value = e} ->
59-
mk_return e
60-
| _ -> super#statement x
61-
method! expression x = x (* don't go inside *)
62-
end *)
35+
6336

6437
type meta_info =
6538
| Info of J.ident_info
@@ -70,7 +43,7 @@ type meta_info =
7043
let mark_dead_code (js : J.program) : J.program =
7144
let ident_use_stats : meta_info Hash_ident.t
7245
= Hash_ident.create 17 in
73-
let mark_dead = object (self)
46+
let mark_dead : Js_fold.fold = object (self)
7447
inherit Js_fold.fold
7548
method! ident ident =
7649
(match Hash_ident.find_opt ident_use_stats ident with
@@ -180,12 +153,12 @@ let mark_dead_code (js : J.program) : J.program =
180153
]}
181154
182155
*)
183-
let subst_map () = object (self)
156+
let subst_map (substitution : J.expression Hash_ident.t) = object (self)
184157
inherit Js_map.map as super
185158

186-
val mutable substitution : J.expression Hash_ident.t= Hash_ident.create 17
187159

188-
method get_substitution = substitution
160+
161+
189162

190163
method add_substitue (ident : Ident.t) (e:J.expression) =
191164
Hash_ident.replace substitution ident e
@@ -264,7 +237,7 @@ let subst_map () = object (self)
264237
{expression_desc = Number (Int {i; _})})
265238
| Static_index ({expression_desc = Var (Id (id))}, _, Some i)
266239
->
267-
(match Hash_ident.find_opt self#get_substitution id with
240+
(match Hash_ident.find_opt substitution id with
268241
| Some {expression_desc = Caml_block (ls, Immutable, _, _) }
269242
->
270243
(* user program can be wrong, we should not
@@ -288,7 +261,7 @@ end
288261

289262
let program (js : J.program) =
290263
js
291-
|> (subst_map () )#program
264+
|> (subst_map (Hash_ident.create 32) )#program
292265
|> mark_dead_code
293266
(* |> mark_dead_code *)
294267
(* mark dead code twice does have effect in some cases, however, we disabled it

0 commit comments

Comments
 (0)