Skip to content

Commit b30d6dd

Browse files
committed
Use record type for Fun payload in j.ml.
1 parent 8fa4b5a commit b30d6dd

13 files changed

+343
-307
lines changed

jscomp/core/j.ml

+14-28
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,17 @@
3232
*)
3333

3434
type mutable_flag = Js_op.mutable_flag
35-
3635
type binop = Js_op.binop
37-
3836
type int_op = Js_op.int_op
39-
4037
type kind = Js_op.kind
41-
4238
type property = Js_op.property
43-
4439
type number = Js_op.number
45-
4640
type ident_info = Js_op.ident_info
47-
4841
type exports = Js_op.exports
49-
5042
type tag_info = Js_op.tag_info
51-
5243
type property_name = Js_op.property_name
5344

5445
type label = string
55-
5646
and ident = Ident.t
5747
(* we override `method ident` *)
5848

@@ -64,7 +54,6 @@ and ident = Ident.t
6454
and module_id = { id : ident; kind : Js_op.kind }
6555

6656
and required_modules = module_id list
67-
6857
and vident = Id of ident | Qualified of module_id * string option
6958
(* Since camldot is only available for toplevel module accessors,
7059
we don't need print `A.length$2`
@@ -83,13 +72,9 @@ and vident = Id of ident | Qualified of module_id * string option
8372
*)
8473

8574
and exception_ident = ident
86-
8775
and for_ident = ident
88-
8976
and for_direction = Js_op.direction_flag
90-
9177
and property_map = (property_name * expression) list
92-
9378
and length_object = Js_op.length_object
9479

9580
and expression_desc =
@@ -142,13 +127,15 @@ and expression_desc =
142127
*)
143128
| New of expression * expression list option (* TODO: option remove *)
144129
| Var of vident
145-
| Fun of bool * ident list * block * Js_fun_env.t * bool * bool
146-
(* The first parameter by default is false,
147-
it will be true when it's a method
148-
The second-last pararemter [true] return unit
149-
The last pararemter [true] means async
150-
*)
151-
| Str of {delim: string option; txt: string}
130+
| Fun of {
131+
is_method : bool;
132+
params : ident list;
133+
body : block;
134+
env : Js_fun_env.t;
135+
return_unit : bool;
136+
async : bool;
137+
}
138+
| Str of { delim : string option; txt : string }
152139
(* A string is UTF-8 encoded, and may contain
153140
escape sequences.
154141
*)
@@ -267,7 +254,6 @@ and case_clause = {
267254
}
268255

269256
and string_clause = string * case_clause
270-
271257
and int_clause = int * case_clause
272258

273259
and statement_desc =
@@ -276,7 +262,8 @@ and statement_desc =
276262
(* Function declaration and Variable declaration *)
277263
| Exp of expression
278264
| If of expression * block * block
279-
| While of label option * expression * block * Js_closure.t (* check if it contains loop mutable values, happens in nested loop *)
265+
| While of label option * expression * block * Js_closure.t
266+
(* check if it contains loop mutable values, happens in nested loop *)
280267
| ForRange of
281268
for_ident_expression option
282269
* finish_ident_expression
@@ -286,8 +273,9 @@ and statement_desc =
286273
* Js_closure.t
287274
| Continue of label
288275
| Break (* only used when inline a fucntion *)
289-
| Return of expression (* Here we need track back a bit ?, move Return to Function ...
290-
Then we can only have one Return, which is not good *)
276+
| Return of expression
277+
(* Here we need track back a bit ?, move Return to Function ...
278+
Then we can only have one Return, which is not good *)
291279
(* since in ocaml, it's expression oriented langauge, [return] in
292280
general has no jumps, it only happens when we do
293281
tailcall conversion, in that case there is a jump.
@@ -305,7 +293,6 @@ and statement_desc =
305293
| Debugger
306294

307295
and expression = { expression_desc : expression_desc; comment : string option }
308-
309296
and statement = { statement_desc : statement_desc; comment : string option }
310297

311298
and variable_declaration = {
@@ -319,7 +306,6 @@ and variable_declaration = {
319306
be concatenated in both ways
320307
*)
321308
and block = statement list
322-
323309
and program = { block : block; exports : exports; export_set : Set_ident.t }
324310

325311
and deps_program = {

jscomp/core/js_analyzer.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ let free_variables (stats : idents_stats) =
5353
expression =
5454
(fun self exp ->
5555
match exp.expression_desc with
56-
| Fun (_, _, _, env, _, _)
56+
| Fun {env}
5757
(* a optimization to avoid walking into funciton again
5858
if it's already comuted
5959
*) ->

jscomp/core/js_dump.ml

+20-16
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,11 @@ module Curry_gen = struct
9393
end
9494

9595
let return_indent = String.length L.return / Ext_pp.indent_length
96-
9796
let throw_indent = String.length L.throw / Ext_pp.indent_length
9897

9998
type cxt = Ext_pp_scope.t
10099

101100
let semi f = P.string f L.semi
102-
103101
let comma f = P.string f L.comma
104102

105103
let exn_block_as_obj ~(stack : bool) (el : J.expression list) (ext : J.tag_info)
@@ -531,10 +529,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
531529
let cxt = expression ~level:0 cxt f e1 in
532530
comma_sp f;
533531
expression ~level:0 cxt f e2)
534-
| Fun (is_method, l, b, env, return_unit, async) ->
532+
| Fun { is_method; params; body; env; return_unit; async } ->
535533
(* TODO: dump for comments *)
536-
pp_function ~is_method cxt f ~fn_state:default_fn_exp_state l b env
537-
~return_unit ~async
534+
pp_function ~is_method cxt f ~fn_state:default_fn_exp_state params body
535+
env ~return_unit ~async
538536
(* TODO:
539537
when [e] is [Js_raw_code] with arity
540538
print it in a more precise way
@@ -555,12 +553,20 @@ and expression_desc cxt ~(level : int) f x : cxt =
555553
| [
556554
{
557555
expression_desc =
558-
Fun (is_method, l, b, env, return_unit, async);
556+
Fun
557+
{
558+
is_method;
559+
params;
560+
body;
561+
env;
562+
return_unit;
563+
async;
564+
};
559565
};
560566
] ->
561567
pp_function ~is_method ~return_unit ~async cxt f
562568
~fn_state:(No_name { single_arg = true })
563-
l b env
569+
params body env
564570
| _ -> arguments cxt f el)
565571
| _, _ ->
566572
let len = List.length el in
@@ -587,16 +593,14 @@ and expression_desc cxt ~(level : int) f x : cxt =
587593
P.string f L.codePointAt;
588594
(* FIXME: use code_point_at *)
589595
P.paren_group f 1 (fun _ -> expression ~level:0 cxt f b))
590-
| Str {delim; txt} ->
596+
| Str { delim; txt } ->
591597
(*TODO --
592598
when utf8-> it will not escape '\\' which is definitely not we want
593599
*)
594600
if delim = Some "j" || delim = Some "*j" then
595601
P.string f ("\"" ^ txt ^ "\"")
596-
else if delim = Some "json" then
597-
P.string f txt
598-
else
599-
Js_dump_string.pp_string f txt;
602+
else if delim = Some "json" then P.string f txt
603+
else Js_dump_string.pp_string f txt;
600604
cxt
601605
| Raw_js_code { code = s; code_info = info } -> (
602606
match info with
@@ -902,10 +906,10 @@ and variable_declaration top cxt f (variable : J.variable_declaration) : cxt =
902906
statement_desc top cxt f (J.Exp e)
903907
| _ -> (
904908
match e.expression_desc with
905-
| Fun (is_method, params, b, env, return_unit, async) ->
909+
| Fun { is_method; params; body; env; return_unit; async } ->
906910
pp_function ~is_method cxt f ~return_unit ~async
907911
~fn_state:(if top then Name_top name else Name_non_top name)
908-
params b env
912+
params body env
909913
| _ ->
910914
let cxt = pp_var_assign cxt f name in
911915
let cxt = expression ~level:1 cxt f e in
@@ -1129,10 +1133,10 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11291133
cxt
11301134
| Return e -> (
11311135
match e.expression_desc with
1132-
| Fun (is_method, l, b, env, return_unit, async) ->
1136+
| Fun { is_method; params; body; env; return_unit; async } ->
11331137
let cxt =
11341138
pp_function ~return_unit ~is_method ~async cxt f ~fn_state:Is_return
1135-
l b env
1139+
params body env
11361140
in
11371141
semi f;
11381142
cxt

jscomp/core/js_exp_make.ml

+34-15
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,35 @@ let unit : t = { expression_desc = Undefined; comment = None }
197197
[Js_fun_env.empty] is a mutable state ..
198198
*)
199199

200-
let ocaml_fun ?comment ?immutable_mask ~return_unit ~async params block : t =
200+
let ocaml_fun ?comment ?immutable_mask ~return_unit ~async params body : t =
201201
let len = List.length params in
202202
{
203203
expression_desc =
204204
Fun
205-
(false, params, block, Js_fun_env.make ?immutable_mask len, return_unit, async);
205+
{
206+
is_method = false;
207+
params;
208+
body;
209+
env = Js_fun_env.make ?immutable_mask len;
210+
return_unit;
211+
async;
212+
};
206213
comment;
207214
}
208215

209-
let method_ ?comment ?immutable_mask ~return_unit params block : t =
216+
let method_ ?comment ?immutable_mask ~return_unit params body : t =
210217
let len = List.length params in
211218
{
212219
expression_desc =
213-
Fun (true, params, block, Js_fun_env.make ?immutable_mask len, return_unit, false);
220+
Fun
221+
{
222+
is_method = true;
223+
params;
224+
body;
225+
env = Js_fun_env.make ?immutable_mask len;
226+
return_unit;
227+
async = false;
228+
};
214229
comment;
215230
}
216231

@@ -484,10 +499,10 @@ let bytes_length ?comment (e : t) : t =
484499

485500
let function_length ?comment (e : t) : t =
486501
match e.expression_desc with
487-
| Fun (b, params, _, _, _, _) ->
502+
| Fun { is_method; params } ->
488503
let params_length = List.length params in
489504
int ?comment
490-
(Int32.of_int (if b then params_length - 1 else params_length))
505+
(Int32.of_int (if is_method then params_length - 1 else params_length))
491506
| _ -> { expression_desc = Length (e, Function); comment }
492507

493508
(** no dependency introduced *)
@@ -1150,15 +1165,19 @@ let of_block ?comment ?e block : t =
11501165
comment;
11511166
expression_desc =
11521167
Fun
1153-
( false,
1154-
[],
1155-
(match e with
1156-
| None -> block
1157-
| Some e ->
1158-
Ext_list.append block
1159-
[ { J.statement_desc = Return e; comment } ]),
1160-
Js_fun_env.make 0,
1161-
return_unit, (* async *) false);
1168+
{
1169+
is_method = false;
1170+
params = [];
1171+
body =
1172+
(match e with
1173+
| None -> block
1174+
| Some e ->
1175+
Ext_list.append block
1176+
[ { J.statement_desc = Return e; comment } ]);
1177+
env = Js_fun_env.make 0;
1178+
return_unit;
1179+
async = false;
1180+
};
11621181
}
11631182
[]
11641183

jscomp/core/js_fold.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ class fold =
146146
| Var _x0 ->
147147
let _self = _self#vident _x0 in
148148
_self
149-
| Fun (_x0, _x1, _x2, _x3, _x4, _x5) ->
150-
let _self = list (fun _self -> _self#ident) _self _x1 in
151-
let _self = _self#block _x2 in
149+
| Fun {params=x1; body=x2} ->
150+
let _self = list (fun _self -> _self#ident) _self x1 in
151+
let _self = _self#block x2 in
152152
_self
153153
| Str _ -> _self
154154
| Raw_js_code _ -> _self

jscomp/core/js_pass_scope.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ let record_scope_pass =
137137
expression =
138138
(fun self state x ->
139139
match x.expression_desc with
140-
| Fun (_method_, params, block, env, _return_unit, _async) ->
140+
| Fun {params; body; env} ->
141141
(* Function is the only place to introduce a new scope in
142142
ES5
143143
TODO: check
@@ -155,7 +155,7 @@ let record_scope_pass =
155155
mutable_values =
156156
Set_ident.of_list (Js_fun_env.get_mutable_params params env);
157157
}
158-
block
158+
body
159159
in
160160
(* let defined_idents', used_idents' =
161161
obj#get_defined_idents, obj#get_used_idents in *)

jscomp/core/js_pass_tailcall_inline.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ let subst (export_set : Set_ident.t) stats =
165165
Some
166166
{
167167
expression_desc =
168-
Fun (false, params, block, env, _return_unit, _async);
168+
Fun {is_method=false; params; body; env};
169169
comment = _;
170170
};
171171
(*TODO: don't inline method tail call yet,
@@ -179,7 +179,7 @@ let subst (export_set : Set_ident.t) stats =
179179
Js_op_util.update_used_stats v.ident_info Dead_pure;
180180
let no_tailcall = Js_fun_env.no_tailcall env in
181181
let processed_blocks =
182-
self.block self block
182+
self.block self body
183183
(* see #278 before changes*)
184184
in
185185
inline_call no_tailcall params args processed_blocks
@@ -200,7 +200,7 @@ let subst (export_set : Set_ident.t) stats =
200200
Call
201201
( {
202202
expression_desc =
203-
Fun (false, params, block, env, _return_unit, _async);
203+
Fun {is_method=false; params; body; env};
204204
},
205205
args,
206206
_info );
@@ -210,7 +210,7 @@ let subst (export_set : Set_ident.t) stats =
210210
when Ext_list.same_length params args ->
211211
let no_tailcall = Js_fun_env.no_tailcall env in
212212
let processed_blocks =
213-
self.block self block
213+
self.block self body
214214
(* see #278 before changes*)
215215
in
216216
inline_call no_tailcall params args processed_blocks

jscomp/core/js_record_fold.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ let expression_desc : 'a. ('a, expression_desc) fn =
152152
| Var _x0 ->
153153
let st = _self.vident _self st _x0 in
154154
st
155-
| Fun (_x0, _x1, _x2, _x3, _x4, _x5) ->
156-
let st = list _self.ident _self st _x1 in
157-
let st = _self.block _self st _x2 in
155+
| Fun {params; body} ->
156+
let st = list _self.ident _self st params in
157+
let st = _self.block _self st body in
158158
st
159159
| Str _ -> st
160160
| Raw_js_code _ -> st

jscomp/core/js_record_iter.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ let expression_desc : expression_desc fn =
118118
_self.expression _self _x0;
119119
option (fun _self arg -> list _self.expression _self arg) _self _x1
120120
| Var _x0 -> _self.vident _self _x0
121-
| Fun (_x0, _x1, _x2, _x3, _x4, _x5) ->
122-
list _self.ident _self _x1;
123-
_self.block _self _x2
121+
| Fun {params; body} ->
122+
list _self.ident _self params;
123+
_self.block _self body
124124
| Str _ -> ()
125125
| Raw_js_code _ -> ()
126126
| Array (_x0, _x1) -> list _self.expression _self _x0

0 commit comments

Comments
 (0)