From b5d9cae699773236f8ce927787346f71fcbdf9a5 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Sun, 3 Nov 2024 21:13:53 -0800 Subject: [PATCH] refactor: usages of `Js_stmt_make.named_expression` --- jscomp/core/js_of_lam_option.ml | 2 +- jscomp/core/js_op.ml | 9 --- jscomp/core/lam_compile.ml | 46 ++++++--------- jscomp/core/lam_compile_external_obj.ml | 74 +++++++++---------------- 4 files changed, 45 insertions(+), 86 deletions(-) diff --git a/jscomp/core/js_of_lam_option.ml b/jscomp/core/js_of_lam_option.ml index 81b27d64b..6f3f79582 100644 --- a/jscomp/core/js_of_lam_option.ml +++ b/jscomp/core/js_of_lam_option.ml @@ -58,7 +58,7 @@ let is_not_none (e : J.expression) : J.expression = - avoid duplicate evlauation of [arg] when it is not a variable - {!Js_ast_util.named_expression} does not help + {!Js_stmt_make.named_expression} does not help since we need an expression here, it might be a statement *) let val_from_option (arg : J.expression) = diff --git a/jscomp/core/js_op.ml b/jscomp/core/js_op.ml index 1def0f992..c1c7bba2a 100644 --- a/jscomp/core/js_op.ml +++ b/jscomp/core/js_op.ml @@ -188,15 +188,6 @@ type length_object = Array | String | Bytes | Function | Caml_block for precedence *) -(* val op_prec : Js_op.binop -> int * int * int -val op_str : Js_op.binop -> string -val op_int_prec : Js_op.int_op -> int * int * int -val op_int_str : Js_op.int_op -> string -val str_of_used_stats : Js_op.used_stats -> string -val update_used_stats : J.ident_info -> Js_op.used_stats -> unit -val of_lam_mutable_flag : Asttypes.mutable_flag -> Js_op.mutable_flag -val is_cons : string -> bool -*) let op_prec (op : binop) = match op with | Eq -> (1, 13, 1) diff --git a/jscomp/core/lam_compile.ml b/jscomp/core/lam_compile.ml index 044852d63..13c3be854 100644 --- a/jscomp/core/lam_compile.ml +++ b/jscomp/core/lam_compile.ml @@ -1257,21 +1257,14 @@ and compile_send (meth_kind : Lam_compat.meth_kind) (met : Lam.t) (obj : Lam.t) | _, ([] | [ _ ]) -> assert false | args_code, label :: nobj :: args -> ( let cont3 nobj k = - match S.named_expression nobj with - | None -> - let cont = - Js_output.output_of_block_and_expression lambda_cxt.continuation - (List.concat args_code) - in - cont (k nobj) - | Some (obj_code, v) -> - let cont2 obj_code v = - Js_output.output_of_block_and_expression lambda_cxt.continuation - (List.concat args_code @ [ obj_code ]) - v - in - let cobj = E.var v in - cont2 obj_code (k cobj) + let obj_code, cobj = + match S.named_expression nobj with + | None -> ([], nobj) + | Some (obj_code, v) -> ([ obj_code ], E.var v) + in + Js_output.output_of_block_and_expression lambda_cxt.continuation + (List.concat args_code @ obj_code) + (k cobj) in match meth_kind with | Self -> @@ -1645,23 +1638,18 @@ and compile_prim (prim_info : Lam.prim_info) in let obj_output = compile_lambda need_value_no_return_cxt obj in let arg_output = compile_lambda need_value_no_return_cxt setter_val in - let cont obj_block arg_block obj_code = - Js_output.output_of_block_and_expression lambda_cxt.continuation - (match obj_code with - | None -> List.append obj_block arg_block - | Some obj_code -> List.append obj_block (obj_code :: arg_block)) - in match (obj_output, arg_output) with | { value = None; _ }, _ | _, { value = None; _ } -> assert false | ( { block = obj_block; value = Some obj; _ }, - { block = arg_block; value = Some value; _ } ) -> ( - match S.named_expression obj with - | None -> - cont obj_block arg_block None - (E.seq (E.assign (E.dot obj property) value) E.unit) - | Some (obj_code, obj) -> - cont obj_block arg_block (Some obj_code) - (E.seq (E.assign (E.dot (E.var obj) property) value) E.unit))) + { block = arg_block; value = Some value; _ } ) -> + let obj_code, obj = + match S.named_expression obj with + | None -> ([], obj) + | Some (obj_code, obj) -> ([ obj_code ], E.var obj) + in + Js_output.output_of_block_and_expression lambda_cxt.continuation + (List.append obj_block (obj_code @ arg_block)) + (E.seq (E.assign (E.dot obj property) value) E.unit)) | { primitive = Pfull_apply; args = diff --git a/jscomp/core/lam_compile_external_obj.ml b/jscomp/core/lam_compile_external_obj.ml index b48257339..b5b798412 100644 --- a/jscomp/core/lam_compile_external_obj.ml +++ b/jscomp/core/lam_compile_external_obj.ml @@ -122,53 +122,33 @@ let assemble_obj_args (* Need make sure whether assignment is effectful or not to avoid code duplication *) - match S.named_expression arg with - | None -> ( - let acc, new_eff = - Lam_compile_external_call.ocaml_to_js_eff - ~arg_label:Arg_empty ~arg_type:xlabel.arg_type - (if for_sure_no_nested_option then arg - else Js_of_lam_option.val_from_option arg) - in - match acc with - | Splice1 v -> - [ - S.if_ - (Js_of_lam_option.is_not_none arg) - [ - S.exp - (E.assign (E.dot var_v label) - (match new_eff with - | [] -> v - | x :: xs -> E.seq (E.fuse_to_seq x xs) v)); - ]; - ] - | Splice0 | Splice2 _ -> assert false) - | Some (st, id) -> ( - (* FIXME: see #2503 *) - let arg = E.var id in - let acc, new_eff = - Lam_compile_external_call.ocaml_to_js_eff - ~arg_label:Arg_empty ~arg_type:xlabel.arg_type - (if for_sure_no_nested_option then arg - else Js_of_lam_option.val_from_option arg) - in - match acc with - | Splice1 v -> - st - :: [ - S.if_ - (Js_of_lam_option.is_not_none arg) - [ - S.exp - (E.assign (E.dot var_v label) - (match new_eff with - | [] -> v - | x :: xs -> - E.seq (E.fuse_to_seq x xs) v)); - ]; - ] - | Splice0 | Splice2 _ -> assert false)) + let st, arg = + match S.named_expression arg with + | None -> ([], arg) + | Some (st, id) -> ([ st ], E.var id) + in + (* FIXME: see #2503 *) + let acc, new_eff = + Lam_compile_external_call.ocaml_to_js_eff + ~arg_label:Arg_empty ~arg_type:xlabel.arg_type + (if for_sure_no_nested_option then arg + else Js_of_lam_option.val_from_option arg) + in + match acc with + | Splice1 v -> + st + @ [ + S.if_ + (Js_of_lam_option.is_not_none arg) + [ + S.exp + (E.assign (E.dot var_v label) + (match new_eff with + | [] -> v + | x :: xs -> E.seq (E.fuse_to_seq x xs) v)); + ]; + ] + | Splice0 | Splice2 _ -> assert false) | _ -> assert false) assignment, var_v )