Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve variadic call using spread syntax #7030

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#### :nail_care: Polish

- Improve bigint literal comparison. https://github.com/rescript-lang/rescript-compiler/pull/7029
- Improve output of `@variadic` bindings. https://github.com/rescript-lang/rescript-compiler/pull/7030

# 12.0.0-alpha.3

Expand Down
1 change: 1 addition & 0 deletions jscomp/core/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ and expression_desc =
| Undefined of {is_unit: bool}
| Null
| Await of expression
| Spread of expression

and for_ident_expression = expression
(* pure*)
Expand Down
2 changes: 2 additions & 0 deletions jscomp/core/js_analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) =
(* actually true? *) ->
false
| Await _ -> false
| Spread _ -> false

and no_side_effect (x : J.expression) =
no_side_effect_expression_desc x.expression_desc
Expand Down Expand Up @@ -212,6 +213,7 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
| Number (Uint _) ->
false
| Await _ -> false
| Spread _ -> false

and eq_expression_list xs ys = Ext_list.for_all2_no_exn xs ys eq_expression

Expand Down
5 changes: 5 additions & 0 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ let rec exp_need_paren ?(arrow=false) (e : J.expression) =
| Js_not _ | Bool _ | New _ ->
false
| Await _ -> false
| Spread _ -> false
| Tagged_template _ -> false
| Optional_block (e, true) when arrow -> exp_need_paren ~arrow e
| Optional_block _ -> false
Expand Down Expand Up @@ -907,6 +908,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
P.cond_paren_group f (level > 13) (fun _ ->
P.string f "await ";
expression ~level:13 cxt f e)
| Spread e ->
P.cond_paren_group f (level > 13) (fun _ ->
P.string f "...";
expression ~level:13 cxt f e)

and property_name_and_value_list cxt f (l : J.property_map) =
iter_lst cxt f l
Expand Down
6 changes: 6 additions & 0 deletions jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1368,3 +1368,9 @@ let neq_null_undefined_boolean ?comment (a : t) (b : t) =

let make_exception (s : string) =
pure_runtime_call Js_runtime_modules.exceptions Literals.create [ str s ]

let rec variadic_args (args : t list) =
match args with
| [] -> []
| [last] -> [{ last with expression_desc = Spread last }]
| arg :: args -> arg :: variadic_args args
2 changes: 2 additions & 0 deletions jscomp/core/js_exp_make.mli
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,5 @@ val for_sure_js_null_undefined : J.expression -> bool
val is_null_undefined : ?comment:string -> t -> t

val make_exception : string -> t

val variadic_args : t list -> t list
3 changes: 3 additions & 0 deletions jscomp/core/js_fold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ class fold =
| Await _x0 ->
let _self = _self#expression _x0 in
_self
| Spread _x0 ->
let _self = _self#expression _x0 in
_self

method for_ident_expression : for_ident_expression -> 'self_type =
_self#expression
Expand Down
3 changes: 3 additions & 0 deletions jscomp/core/js_record_fold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ let expression_desc : 'a. ('a, expression_desc) fn =
| Await _x0 ->
let st = _self.expression _self st _x0 in
st
| Spread _x0 ->
let st = _self.expression _self st _x0 in
st

let for_ident_expression : 'a. ('a, for_ident_expression) fn =
fun _self arg -> _self.expression _self arg
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/js_record_iter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ let expression_desc : expression_desc fn =
| Undefined _ -> ()
| Null -> ()
| Await _x0 -> _self.expression _self _x0
| Spread _x0 -> _self.expression _self _x0

let for_ident_expression : for_ident_expression fn =
fun _self arg -> _self.expression _self arg
Expand Down
3 changes: 3 additions & 0 deletions jscomp/core/js_record_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ let expression_desc : expression_desc fn =
| Await _x0 ->
let _x0 = _self.expression _self _x0 in
Await _x0
| Spread _x0 ->
let _x0 = _self.expression _self _x0 in
Spread _x0

let for_ident_expression : for_ident_expression fn =
fun _self arg -> _self.expression _self arg
Expand Down
47 changes: 17 additions & 30 deletions jscomp/core/lam_compile_external_call.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@

module E = Js_exp_make

let splice_apply fn args =
E.runtime_call Js_runtime_modules.caml_splice_call "spliceApply"
[ fn; E.array Immutable args ]

let splice_new_apply fn args =
E.runtime_call Js_runtime_modules.caml_splice_call "spliceNewApply"
[ fn; E.array Immutable args ]

let splice_obj_apply obj name args =
E.runtime_call Js_runtime_modules.caml_splice_call "spliceObjApply"
[ obj; E.str name; E.array Immutable args ]

(**
[bind_name] is a hint to the compiler to generate
better names for external module
Expand Down Expand Up @@ -278,17 +266,18 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
(match args with
| [ {expression_desc = Array (strings, _); _}; {expression_desc = Array (values, _); _} ] ->
E.tagged_template fn strings values
| _ -> let args, eff, dynamic = assemble_args_has_splice arg_types args in
add_eff eff
(if dynamic then splice_apply fn args
else E.call ~info:{ arity = Full; call_info = Call_na } fn args))
| _ ->
let args, eff, dynamic = assemble_args_has_splice arg_types args in
let args = if dynamic then E.variadic_args args else args in
add_eff eff (
E.call ~info:{ arity = Full; call_info = Call_na } fn args))
| Js_call { external_module_name = module_name; name = fn; splice; scopes; tagged_template = false } ->
let fn = translate_scoped_module_val module_name fn scopes ~dynamic_import in
if splice then
let args, eff, dynamic = assemble_args_has_splice arg_types args in
add_eff eff
(if dynamic then splice_apply fn args
else E.call ~info:{ arity = Full; call_info = Call_na } fn args)
let args = if dynamic then E.variadic_args args else args in
add_eff eff (
E.call ~info:{ arity = Full; call_info = Call_na } fn args)
else
let args, eff = assemble_args_no_splice arg_types args in
add_eff eff
Expand All @@ -297,10 +286,9 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
let fn = external_var external_module_name ~dynamic_import in
if splice then
let args, eff, dynamic = assemble_args_has_splice arg_types args in
(* TODO: fix in rest calling convention *)
add_eff eff
(if dynamic then splice_apply fn args
else E.call ~info:{ arity = Full; call_info = Call_na } fn args)
let args = if dynamic then E.variadic_args args else args in
add_eff eff (
E.call ~info:{ arity = Full; call_info = Call_na } fn args)
else
let args, eff = assemble_args_no_splice arg_types args in
(* TODO: fix in rest calling convention *)
Expand All @@ -324,11 +312,11 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
in
if splice then
let args, eff, dynamic = assemble_args_has_splice arg_types args in
let args = if dynamic then E.variadic_args args else args in
let fn = translate_scoped_module_val module_name fn scopes ~dynamic_import in
add_eff eff
(mark ();
if dynamic then splice_new_apply fn args
else E.new_ fn args)
E.new_ fn args)
else
let args, eff = assemble_args_no_splice arg_types args in
let fn = translate_scoped_module_val module_name fn scopes ~dynamic_import in
Expand All @@ -342,13 +330,12 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
let[@warning "-8"] (_self_type :: arg_types) = arg_types in
if splice then
let args, eff, dynamic = assemble_args_has_splice arg_types args in
let args = if dynamic then E.variadic_args args else args in
add_eff eff
(let self = translate_scoped_access js_send_scopes self in
if dynamic then splice_obj_apply self name args
else
E.call
~info:{ arity = Full; call_info = Call_na }
(E.dot self name) args)
E.call
~info:{ arity = Full; call_info = Call_na }
(E.dot self name) args)
else
let args, eff = assemble_args_no_splice arg_types args in
add_eff eff
Expand Down
2 changes: 0 additions & 2 deletions jscomp/ext/js_runtime_modules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ let module_ = "Caml_module"

let caml_js_exceptions = "Caml_js_exceptions"

let caml_splice_call = "Caml_splice_call"

let deriving = "Runtime_deriving"

let promise = "Runtime_promise"
Expand Down
68 changes: 0 additions & 68 deletions jscomp/runtime/caml_splice_call.res

This file was deleted.

31 changes: 0 additions & 31 deletions jscomp/runtime/caml_splice_call.resi

This file was deleted.

4 changes: 1 addition & 3 deletions jscomp/runtime/release.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ o runtime/caml_option.cmj : cc_cmi runtime/caml_option.res | runtime/caml_option
o runtime/caml_option.cmi : cc runtime/caml_option.resi | runtime/bs_stdlib_mini.cmi runtime/caml_undefined_extern.cmj runtime/js.cmi runtime/js.cmj
o runtime/caml_parser.cmj : cc_cmi runtime/caml_parser.res | runtime/caml_parser.cmi
o runtime/caml_parser.cmi : cc runtime/caml_parser.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_splice_call.cmj : cc_cmi runtime/caml_splice_call.res | runtime/caml_splice_call.cmi
o runtime/caml_splice_call.cmi : cc runtime/caml_splice_call.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_string.cmj : cc_cmi runtime/caml_string.res | runtime/caml_string.cmi runtime/caml_string_extern.cmj
o runtime/caml_string.cmi : cc runtime/caml_string.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_sys.cmj : cc_cmi runtime/caml_sys.res | runtime/caml_array_extern.cmj runtime/caml_sys.cmi runtime/caml_undefined_extern.cmj
Expand All @@ -62,4 +60,4 @@ o runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj : cc runti
o runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj : cc runtime/caml_string_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj : cc runtime/caml_undefined_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/curry.cmi runtime/curry.cmj : cc runtime/curry.res | runtime/bs_stdlib_mini.cmi runtime/caml_array.cmj runtime/caml_array_extern.cmj runtime/js.cmi runtime/js.cmj
o runtime : phony runtime/bs_stdlib_mini.cmi runtime/js.cmj runtime/js.cmi runtime/caml.cmi runtime/caml.cmj runtime/caml_array.cmi runtime/caml_array.cmj runtime/caml_bigint.cmi runtime/caml_bigint.cmj runtime/caml_bytes.cmi runtime/caml_bytes.cmj runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj runtime/caml_float.cmi runtime/caml_float.cmj runtime/caml_format.cmi runtime/caml_format.cmj runtime/caml_hash.cmi runtime/caml_hash.cmj runtime/caml_hash_primitive.cmi runtime/caml_hash_primitive.cmj runtime/caml_int32.cmi runtime/caml_int32.cmj runtime/caml_int64.cmi runtime/caml_int64.cmj runtime/caml_lexer.cmi runtime/caml_lexer.cmj runtime/caml_md5.cmi runtime/caml_md5.cmj runtime/caml_module.cmi runtime/caml_module.cmj runtime/caml_obj.cmi runtime/caml_obj.cmj runtime/caml_option.cmi runtime/caml_option.cmj runtime/caml_parser.cmi runtime/caml_parser.cmj runtime/caml_splice_call.cmi runtime/caml_splice_call.cmj runtime/caml_string.cmi runtime/caml_string.cmj runtime/caml_sys.cmi runtime/caml_sys.cmj runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj runtime/caml_bigint_extern.cmi runtime/caml_bigint_extern.cmj runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj runtime/curry.cmi runtime/curry.cmj
o runtime : phony runtime/bs_stdlib_mini.cmi runtime/js.cmj runtime/js.cmi runtime/caml.cmi runtime/caml.cmj runtime/caml_array.cmi runtime/caml_array.cmj runtime/caml_bigint.cmi runtime/caml_bigint.cmj runtime/caml_bytes.cmi runtime/caml_bytes.cmj runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj runtime/caml_float.cmi runtime/caml_float.cmj runtime/caml_format.cmi runtime/caml_format.cmj runtime/caml_hash.cmi runtime/caml_hash.cmj runtime/caml_hash_primitive.cmi runtime/caml_hash_primitive.cmj runtime/caml_int32.cmi runtime/caml_int32.cmj runtime/caml_int64.cmi runtime/caml_int64.cmj runtime/caml_lexer.cmi runtime/caml_lexer.cmj runtime/caml_md5.cmi runtime/caml_md5.cmj runtime/caml_module.cmi runtime/caml_module.cmj runtime/caml_obj.cmi runtime/caml_obj.cmj runtime/caml_option.cmi runtime/caml_option.cmj runtime/caml_parser.cmi runtime/caml_parser.cmj runtime/caml_string.cmi runtime/caml_string.cmj runtime/caml_sys.cmi runtime/caml_sys.cmj runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj runtime/caml_bigint_extern.cmi runtime/caml_bigint_extern.cmj runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj runtime/curry.cmi runtime/curry.cmj
6 changes: 1 addition & 5 deletions jscomp/test/bs_auto_uncurry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions jscomp/test/module_splice_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading