@@ -41,7 +41,12 @@ module S = Js_stmt_make
41
41
(* module E = Js_exp_make *)
42
42
43
43
44
-
44
+ let substitue_variables (map : Ident.t Map_ident.t ) =
45
+ object (self )
46
+ inherit Js_map. map
47
+ method! ident id =
48
+ Map_ident. find_default map id id
49
+ end
45
50
46
51
(* 1. recursive value ? let rec x = 1 :: x
47
52
non-terminating
@@ -52,6 +57,36 @@ module S = Js_stmt_make
52
57
we already have this? in [defined_idents]
53
58
*)
54
59
60
+ let inline_call
61
+ no_tailcall
62
+ params (args : J.expression list ) processed_blocks =
63
+ if no_tailcall then
64
+ let map, block =
65
+ Ext_list. fold_right2
66
+ params args (Map_ident. empty, processed_blocks)
67
+ (fun param arg (map ,acc ) ->
68
+ match arg.expression_desc with
69
+ | Var (Id id ) ->
70
+ Map_ident. add map param id, acc
71
+ | _ ->
72
+ map, S. define_variable ~kind: Variable param arg :: acc) in
73
+ if Map_ident. is_empty map then block
74
+ else (substitue_variables map) # block block
75
+ else
76
+ (*
77
+ At this time, when tailcall happened, the parameter can be assigned
78
+ for example {[
79
+ function (_x,y){
80
+ _x = u
81
+ }
82
+ ]}
83
+ if it is substitued, the assignment will align the value which is incorrect
84
+ *)
85
+ Ext_list. fold_right2
86
+ params args processed_blocks
87
+ (fun param arg acc ->
88
+ S. define_variable ~kind: Variable param arg :: acc)
89
+
55
90
(* * There is a side effect when traversing dead code, since
56
91
we assume that substitue a node would mark a node as dead node,
57
92
@@ -132,7 +167,7 @@ let subst (export_set : Set_ident.t) stats =
132
167
begin match Hash_ident. find_opt stats id with
133
168
134
169
| Some ({ value =
135
- Some {expression_desc = Fun (false , params, block, _env ) ; comment = _};
170
+ Some {expression_desc = Fun (false , params, block, env ) ; comment = _};
136
171
(* TODO: don't inline method tail call yet,
137
172
[this] semantics are weird
138
173
*)
@@ -143,10 +178,13 @@ let subst (export_set : Set_ident.t) stats =
143
178
when Ext_list. same_length params args
144
179
->
145
180
Js_op_util. update_used_stats v.ident_info Dead_pure ;
146
- Ext_list. fold_right2
147
- params args ( self#block block) (* see #278 before changes*)
181
+ let no_tailcall = Js_fun_env. no_tailcall env in
182
+ let processed_blocks = ( self#block block) (* see #278 before changes*) in
183
+ inline_call no_tailcall params args processed_blocks
184
+ (* Ext_list.fold_right2
185
+ params args processed_blocks
148
186
(fun param arg acc ->
149
- S. define_variable ~kind: Variable param arg :: acc)
187
+ S.define_variable ~kind:Variable param arg :: acc) *)
150
188
(* Mark a function as dead means it will never be scanned,
151
189
here we inline the function
152
190
*)
0 commit comments