Skip to content

Commit 60ca072

Browse files
authored
Merge pull request #4079 from BuckleScript/seprate_method_uncurried_compilation
separate method/setter compilation from uncurried compilation
2 parents 78c36ad + afa0d5f commit 60ca072

14 files changed

+244
-208
lines changed

jscomp/core/lam.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ type ident = Ident.t
2626

2727
type apply_status =
2828
| App_na
29-
| App_ml_full
30-
| App_js_full
29+
| App_infer_full
30+
| App_uncurry
3131

3232

3333
module Types = struct

jscomp/core/lam.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ type lambda_switch =
3737
sw_names: Lambda.switch_names option }
3838
and apply_status =
3939
| App_na
40-
| App_ml_full
41-
| App_js_full
40+
| App_infer_full
41+
| App_uncurry
4242
and apply_info = private
4343
{ ap_func : t ;
4444
ap_args : t list ;

jscomp/core/lam_analysis.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
175175
| Pjs_unsafe_downgrade _
176176
| Pdebugger
177177
| Pjs_fn_run _
178+
| Pmethod_run
178179
| Pjs_fn_method _
179180
(* TODO *)
180181
| Praw_js_code_exp _

jscomp/core/lam_compile.ml

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ module S = Js_stmt_make
2828

2929
let call_info_of_ap_status (ap_status : Lam.apply_status) : Js_call_info.t =
3030
match ap_status with
31-
| App_ml_full ->
31+
| App_infer_full ->
3232
{arity = Full ; call_info = Call_ml}
33-
| App_js_full ->
33+
| App_uncurry ->
3434
{arity = Full ; call_info = Call_na}
3535
| App_na ->
3636
{arity = NA; call_info = Call_ml }
@@ -250,7 +250,7 @@ and compile_external_field_apply
250250
let fn = E.ml_var_dot module_id ident_info.name in
251251
let expression =
252252
match appinfo.ap_status with
253-
| App_ml_full | App_js_full as ap_status ->
253+
| App_infer_full | App_uncurry as ap_status ->
254254
E.call ~info:(call_info_of_ap_status ap_status) fn args
255255
| App_na ->
256256
match ident_info.arity with
@@ -1419,53 +1419,54 @@ and compile_prim (prim_info : Lam.prim_info) (lambda_cxt : Lam_compile_context.t
14191419
| Some (x, b) ->
14201420
Ext_list.append_one block x, E.dot (E.var b) property in
14211421
Js_output.output_of_block_and_expression lambda_cxt.continuation blocks ret)
1422-
| {primitive = Pjs_fn_run _; args = args_lambda}
1422+
| {primitive = Pmethod_run; args = [Lprim{
1423+
primitive =
1424+
Pjs_unsafe_downgrade {name = property; loc; setter = true};
1425+
args = [obj]} ;
1426+
setter_val]} ->
1427+
let need_value_no_return_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in
1428+
let obj_output = compile_lambda need_value_no_return_cxt obj in
1429+
let arg_output = compile_lambda need_value_no_return_cxt setter_val in
1430+
let cont obj_block arg_block obj_code =
1431+
Js_output.output_of_block_and_expression lambda_cxt.continuation
1432+
(
1433+
match obj_code with
1434+
| None -> Ext_list.append obj_block arg_block
1435+
| Some obj_code -> Ext_list.append obj_block (obj_code :: arg_block)
1436+
)
1437+
in
1438+
(match obj_output, arg_output with
1439+
| {value = None}, _ | _, {value = None} -> assert false
1440+
| {block = obj_block; value = Some obj },
1441+
{block = arg_block; value = Some value}
1442+
->
1443+
match Js_ast_util.named_expression obj with
1444+
| None ->
1445+
cont obj_block arg_block None
1446+
(E.seq (E.assign (E.dot obj property) value) E.unit)
1447+
| Some (obj_code, obj)
1448+
->
1449+
cont obj_block arg_block (Some obj_code)
1450+
(E.seq (E.assign (E.dot (E.var obj) property) value) E.unit)
1451+
)
1452+
| {primitive = Pmethod_run; args = Lprim{
1453+
primitive =
1454+
Pjs_unsafe_downgrade {name = property; loc; setter = true};
1455+
} :: _
1456+
} -> assert false
1457+
| {primitive = Pjs_fn_run _ | Pmethod_run ; args; loc}
14231458
->
1424-
(* 1. prevent eta-conversion
1425-
by using [App_js_full]
1459+
(* 1. uncurried call should not do eta-conversion
1460+
since `fn.length` will broken
14261461
2. invariant: `external` declaration will guarantee
14271462
the function application is saturated
14281463
3. we need a location for Pccall in the call site
14291464
*)
14301465

1431-
(match args_lambda with
1432-
| [Lprim{
1433-
primitive =
1434-
Pjs_unsafe_downgrade {name = property; loc; setter = true};
1435-
args = args_l} ;
1436-
setter_val] (** x##name arg could be specialized as a setter *)
1437-
->
1438-
let obj = Ext_list.singleton_exn args_l in
1439-
let need_value_no_return_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in
1440-
let obj_output = compile_lambda need_value_no_return_cxt obj in
1441-
let arg_output = compile_lambda need_value_no_return_cxt setter_val in
1442-
let cont obj_block arg_block obj_code =
1443-
Js_output.output_of_block_and_expression lambda_cxt.continuation
1444-
(
1445-
match obj_code with
1446-
| None -> Ext_list.append obj_block arg_block
1447-
| Some obj_code -> Ext_list.append obj_block (obj_code :: arg_block)
1448-
)
1449-
in
1450-
(match obj_output, arg_output with
1451-
| {value = None}, _ | _, {value = None} -> assert false
1452-
| {block = obj_block; value = Some obj },
1453-
{block = arg_block; value = Some value}
1454-
->
1455-
match Js_ast_util.named_expression obj with
1456-
| None ->
1457-
cont obj_block arg_block None
1458-
(E.seq (E.assign (E.dot obj property) value) E.unit)
1459-
| Some (obj_code, obj)
1460-
->
1461-
cont obj_block arg_block (Some obj_code)
1462-
(E.seq (E.assign (E.dot (E.var obj) property) value) E.unit)
1463-
)
1466+
(match args with
14641467
| fn :: rest ->
14651468
compile_lambda lambda_cxt
1466-
(Lam.apply fn rest
1467-
Location.none (*TODO*)
1468-
App_js_full)
1469+
(Lam.apply fn rest loc App_uncurry)
14691470
| [] -> assert false)
14701471

14711472
| {primitive = Pjs_fn_method arity; args = args_lambda} ->

jscomp/core/lam_compile_primitive.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ let translate loc
120120
| Pjs_unsafe_downgrade _
121121
| Pdebugger
122122
| Pjs_fn_run _
123+
| Pmethod_run
123124
| Pjs_fn_make _
124125
-> assert false (* already handled by {!Lam_compile} *)
125126
| Pjs_fn_method _ -> assert false

jscomp/core/lam_convert.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,9 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
525525
| "#unsafe_neq" -> Pjscomp Cneq
526526

527527
| "#typeof" -> Pjs_typeof
528-
| "#fn_run" | "#method_run" -> Pjs_fn_run(Ext_pervasives.nat_of_string_exn p.prim_native_name)
528+
| "#fn_run" ->
529+
Pjs_fn_run(Ext_pervasives.nat_of_string_exn p.prim_native_name)
530+
| "#method_run" -> Pmethod_run
529531
| "#fn_mk" -> Pjs_fn_make (Ext_pervasives.nat_of_string_exn p.prim_native_name)
530532
| "#fn_method" -> Pjs_fn_method (Ext_pervasives.nat_of_string_exn p.prim_native_name)
531533
| "#unsafe_downgrade" -> Pjs_unsafe_downgrade {name = Ext_string.empty; loc ; setter = false}

jscomp/core/lam_eta_conversion.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
191191
~params:extra_args
192192
~body:(
193193
let first_args, rest_args = Ext_list.split_at extra_args from in
194-
Lam.apply (Lam.apply new_fn (Ext_list.map first_args Lam.var) loc App_ml_full) (Ext_list.map rest_args Lam.var) loc App_na ) in
194+
Lam.apply (Lam.apply new_fn (Ext_list.map first_args Lam.var) loc App_infer_full) (Ext_list.map rest_args Lam.var) loc App_na ) in
195195
begin match wrapper with
196196
| None -> cont
197197
| Some partial_arg ->
@@ -245,7 +245,7 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
245245
(Ext_list.map extra_inner_args Lam.var)
246246
Lam.var
247247
)
248-
loc App_ml_full)
248+
loc App_infer_full)
249249
) in
250250
begin match wrapper with
251251
| None -> cont

jscomp/core/lam_pass_alpha_conversion.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ let alpha_conversion (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
3838
| x :: xs ->
3939
if x = len
4040
then
41-
Lam.apply (simpl fn) (Ext_list.map args simpl) loc App_ml_full
41+
Lam.apply (simpl fn) (Ext_list.map args simpl) loc App_infer_full
4242
else if x > len
4343
then
4444
let fn = simpl fn in
4545
let args = Ext_list.map args simpl in
46-
Lam_eta_conversion.transform_under_supply (x - len) loc App_ml_full
46+
Lam_eta_conversion.transform_under_supply (x - len) loc App_infer_full
4747
fn args
4848
else
4949
let first,rest = Ext_list.split_at args x in
5050
Lam.apply (
5151
Lam.apply (simpl fn)
5252
(Ext_list.map first simpl )
53-
loc App_ml_full
53+
loc App_infer_full
5454
)
5555
(Ext_list.map rest simpl ) loc status (* TODO refien *)
5656

jscomp/core/lam_primitive.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ type t =
152152
| Praw_js_function of string * string list
153153
| Pjs_fn_make of int
154154
| Pjs_fn_run of int
155+
| Pmethod_run
155156
| Pjs_fn_method of int
156157

157158

@@ -327,6 +328,7 @@ let eq_primitive_approx ( lhs : t) (rhs : t) =
327328
| Pjs_unsafe_downgrade {name; loc=_; setter } -> (match rhs with Pjs_unsafe_downgrade rhs -> name = rhs.name && setter = rhs.setter | _ -> false)
328329
| Pjs_fn_make i -> (match rhs with Pjs_fn_make i1 -> i = i1 | _ -> false)
329330
| Pjs_fn_run i -> (match rhs with Pjs_fn_run i1 -> i = i1 | _ -> false)
331+
| Pmethod_run -> rhs = Pmethod_run
330332
| Pjs_fn_method i -> (match rhs with Pjs_fn_method i1 -> i = i1 | _ -> false )
331333
| Pbigarrayref _
332334
| Pbigarrayset _

jscomp/core/lam_primitive.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ type t =
147147
| Praw_js_function of string * string list
148148
| Pjs_fn_make of int
149149
| Pjs_fn_run of int
150+
| Pmethod_run
150151
| Pjs_fn_method of int
151152
| Pundefined_to_opt
152153
| Pnull_to_opt

0 commit comments

Comments
 (0)