Skip to content

Commit

Permalink
refactor: usages of Js_stmt_make.named_expression
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Nov 4, 2024
1 parent 2492514 commit b5d9cae
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 86 deletions.
2 changes: 1 addition & 1 deletion jscomp/core/js_of_lam_option.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
9 changes: 0 additions & 9 deletions jscomp/core/js_op.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 17 additions & 29 deletions jscomp/core/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down Expand Up @@ -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 =
Expand Down
74 changes: 27 additions & 47 deletions jscomp/core/lam_compile_external_obj.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 )

0 comments on commit b5d9cae

Please sign in to comment.