From d352b66371ced3ed674b82ab031356ec2b5377cc Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Wed, 11 Sep 2024 05:08:29 +0900 Subject: [PATCH 1/2] improve variadic call using spread syntax --- jscomp/core/j.ml | 1 + jscomp/core/js_analyzer.ml | 2 + jscomp/core/js_dump.ml | 5 ++ jscomp/core/js_exp_make.ml | 6 +++ jscomp/core/js_exp_make.mli | 2 + jscomp/core/js_fold.ml | 3 ++ jscomp/core/js_record_fold.ml | 3 ++ jscomp/core/js_record_iter.ml | 1 + jscomp/core/js_record_map.ml | 3 ++ jscomp/core/lam_compile_external_call.ml | 47 ++++++---------- jscomp/ext/js_runtime_modules.ml | 2 - jscomp/runtime/caml_splice_call.res | 68 ------------------------ jscomp/runtime/caml_splice_call.resi | 31 ----------- jscomp/runtime/release.ninja | 4 +- jscomp/test/bs_auto_uncurry.js | 6 +-- jscomp/test/module_splice_test.js | 3 +- jscomp/test/splice_test.js | 28 +++------- jscomp/test/tagged_template_test.js | 6 +-- lib/es6/caml_splice_call.js | 52 ------------------ lib/es6/js_array.js | 13 ++--- lib/es6/js_string.js | 3 +- lib/es6/jsxPPXReactSupport.js | 11 ++-- lib/js/caml_splice_call.js | 50 ----------------- lib/js/js_array.js | 13 ++--- lib/js/js_string.js | 3 +- lib/js/jsxPPXReactSupport.js | 11 ++-- packages/artifacts.txt | 2 - 27 files changed, 70 insertions(+), 309 deletions(-) delete mode 100644 jscomp/runtime/caml_splice_call.res delete mode 100644 jscomp/runtime/caml_splice_call.resi delete mode 100644 lib/es6/caml_splice_call.js delete mode 100644 lib/js/caml_splice_call.js diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index 3ab6112495..a862748598 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -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*) diff --git a/jscomp/core/js_analyzer.ml b/jscomp/core/js_analyzer.ml index 0bd1003653..24a9765139 100644 --- a/jscomp/core/js_analyzer.ml +++ b/jscomp/core/js_analyzer.ml @@ -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 @@ -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 diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index 9a4fee77d2..0902ce2762 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -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 @@ -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 diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index 2f403529db..b889c139f3 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -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 diff --git a/jscomp/core/js_exp_make.mli b/jscomp/core/js_exp_make.mli index f7d36bcc56..fd04bacf24 100644 --- a/jscomp/core/js_exp_make.mli +++ b/jscomp/core/js_exp_make.mli @@ -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 diff --git a/jscomp/core/js_fold.ml b/jscomp/core/js_fold.ml index 8b76f8a075..ebbd4d9a6b 100644 --- a/jscomp/core/js_fold.ml +++ b/jscomp/core/js_fold.ml @@ -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 diff --git a/jscomp/core/js_record_fold.ml b/jscomp/core/js_record_fold.ml index c748eec33f..6baefff018 100644 --- a/jscomp/core/js_record_fold.ml +++ b/jscomp/core/js_record_fold.ml @@ -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 diff --git a/jscomp/core/js_record_iter.ml b/jscomp/core/js_record_iter.ml index 4e2903b42c..2a1a9cf7c5 100644 --- a/jscomp/core/js_record_iter.ml +++ b/jscomp/core/js_record_iter.ml @@ -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 diff --git a/jscomp/core/js_record_map.ml b/jscomp/core/js_record_map.ml index 5310a94197..351a2f7955 100644 --- a/jscomp/core/js_record_map.ml +++ b/jscomp/core/js_record_map.ml @@ -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 diff --git a/jscomp/core/lam_compile_external_call.ml b/jscomp/core/lam_compile_external_call.ml index befb39b57b..cf09812e33 100644 --- a/jscomp/core/lam_compile_external_call.ml +++ b/jscomp/core/lam_compile_external_call.ml @@ -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 @@ -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 @@ -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 *) @@ -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 @@ -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 diff --git a/jscomp/ext/js_runtime_modules.ml b/jscomp/ext/js_runtime_modules.ml index b6881a490a..40f6ddf945 100644 --- a/jscomp/ext/js_runtime_modules.ml +++ b/jscomp/ext/js_runtime_modules.ml @@ -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" diff --git a/jscomp/runtime/caml_splice_call.res b/jscomp/runtime/caml_splice_call.res deleted file mode 100644 index 7c2334c084..0000000000 --- a/jscomp/runtime/caml_splice_call.res +++ /dev/null @@ -1,68 +0,0 @@ -/* Copyright (C) 2019- Hongbo Zhang, Authors of ReScript - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * In addition to the permissions granted to you by the LGPL, you may combine - * or link a "work that uses the Library" with a publicly distributed version - * of this file to produce a combined library or application, then distribute - * that combined work under the terms of your choosing, with no requirement - * to comply with the obligations normally placed on you by section 4 of the - * LGPL version 3 (or the corresponding section of a later version of the LGPL - * should you choose to use a later version). - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - -type obj = Obj.t - -let spliceApply: (obj, obj) => obj = %raw(`function(fn,args){ - var i, argLen; - argLen = args.length - var applied = [] - for(i = 0; i < argLen - 1; ++i){ - applied.push(args[i]) - } - var lastOne = args[argLen - 1] - for(i = 0; i < lastOne.length; ++i ){ - applied.push(lastOne[i]) - } - return fn.apply(null,applied) -}`) - -let spliceNewApply: (obj, obj) => obj = %raw(`function (ctor,args){ - var i, argLen; - argLen = args.length - var applied = [null] // Function.prototype.bind.apply(fn, args) requires the first element in \`args\` to be \`null\` - for(i = 0; i < argLen - 1; ++i){ - applied.push(args[i]) - } - var lastOne = args[argLen - 1] - for(i = 0; i < lastOne.length; ++i ){ - applied.push(lastOne[i]) - } - var C = Function.prototype.bind.apply(ctor, applied) - return new C() -}`) - -let spliceObjApply: (obj, obj, obj) => obj = %raw(`function(obj,name,args){ - var i, argLen; - argLen = args.length - var applied = [] - for(i = 0; i < argLen - 1; ++i){ - applied.push(args[i]) - } - var lastOne = args[argLen - 1] - for(i = 0; i < lastOne.length; ++i ){ - applied.push(lastOne[i]) - } - return (obj[name]).apply(obj,applied) -}`) diff --git a/jscomp/runtime/caml_splice_call.resi b/jscomp/runtime/caml_splice_call.resi deleted file mode 100644 index 16480895bd..0000000000 --- a/jscomp/runtime/caml_splice_call.resi +++ /dev/null @@ -1,31 +0,0 @@ -/* Copyright (C) 2019- Authors of ReScript - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * In addition to the permissions granted to you by the LGPL, you may combine - * or link a "work that uses the Library" with a publicly distributed version - * of this file to produce a combined library or application, then distribute - * that combined work under the terms of your choosing, with no requirement - * to comply with the obligations normally placed on you by section 4 of the - * LGPL version 3 (or the corresponding section of a later version of the LGPL - * should you choose to use a later version). - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - -type obj = Obj.t - -let spliceApply: (obj, obj) => obj - -let spliceNewApply: (obj, obj) => obj - -let spliceObjApply: (obj, obj, obj) => obj diff --git a/jscomp/runtime/release.ninja b/jscomp/runtime/release.ninja index b848fd71bc..528c515610 100644 --- a/jscomp/runtime/release.ninja +++ b/jscomp/runtime/release.ninja @@ -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 @@ -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 diff --git a/jscomp/test/bs_auto_uncurry.js b/jscomp/test/bs_auto_uncurry.js index 1b0f3d0dd9..d9601239d7 100644 --- a/jscomp/test/bs_auto_uncurry.js +++ b/jscomp/test/bs_auto_uncurry.js @@ -1,7 +1,6 @@ // Generated by ReScript, PLEASE EDIT WITH CARE 'use strict'; -let Caml_splice_call = require("../../lib/js/caml_splice_call.js"); let Curry = {}; @@ -112,10 +111,7 @@ function unit_magic() { let f_unit_magic = unit_magic(); function hh(xs, a) { - Caml_splice_call.spliceApply(f_0002, [ - xs, - a - ]); + f_0002(xs, ...a); } exports.Curry = Curry; diff --git a/jscomp/test/module_splice_test.js b/jscomp/test/module_splice_test.js index 2472d3f58b..4ac8920689 100644 --- a/jscomp/test/module_splice_test.js +++ b/jscomp/test/module_splice_test.js @@ -3,7 +3,6 @@ let Mt = require("./mt.js"); let JoinClasses = require("./joinClasses"); -let Caml_splice_call = require("../../lib/js/caml_splice_call.js"); let suites = { contents: /* [] */0 @@ -31,7 +30,7 @@ function eq(loc, param) { } function joinClasses(prim) { - return Caml_splice_call.spliceApply(JoinClasses, [prim]); + return JoinClasses(...prim); } let a = JoinClasses(1, 2, 3); diff --git a/jscomp/test/splice_test.js b/jscomp/test/splice_test.js index 44d8589fa6..5bf879221e 100644 --- a/jscomp/test/splice_test.js +++ b/jscomp/test/splice_test.js @@ -3,7 +3,6 @@ let Mt = require("./mt.js"); let Caml_array = require("../../lib/js/caml_array.js"); -let Caml_splice_call = require("../../lib/js/caml_splice_call.js"); let suites = { contents: /* [] */0 @@ -17,7 +16,7 @@ function eq(loc, x, y) { Mt.eq_suites(test_id, suites, loc, x, y); } -let Caml_splice_call$1 = {}; +let Caml_splice_call = {}; Math.max(1); @@ -38,10 +37,7 @@ eq("File \"splice_test.res\", line 23, characters 5-12", a, [ function dynamic(arr) { let a = []; - Caml_splice_call.spliceObjApply(a, "push", [ - 1, - arr - ]); + a.push(1, ...arr); eq("File \"splice_test.res\", line 29, characters 5-12", a, Caml_array.concat({ hd: [1], tl: { @@ -75,11 +71,7 @@ eq("File \"splice_test.res\", line 44, characters 5-12", a$1, [ ]); function dynamicNew(arr) { - let a = Caml_splice_call.spliceNewApply(Array, [ - 1, - 2, - arr - ]); + let a = new Array(1, 2, ...arr); eq("File \"splice_test.res\", line 49, characters 5-12", a, Caml_array.concat({ hd: [ 1, @@ -120,7 +112,7 @@ eq("File \"splice_test.res\", line 71, characters 5-12", f.names, [ ]); function dynamicFoo(arr) { - let f = Caml_splice_call.spliceNewApply(Foo, [arr]); + let f = new Foo(...arr); eq("File \"splice_test.res\", line 76, characters 5-12", f.names, arr); } @@ -147,10 +139,7 @@ eq("File \"splice_test.res\", line 93, characters 7-14", a$2, [ function dynamic$1(arr) { let a = []; - Caml_splice_call.spliceObjApply(a, "push", [ - 1, - arr - ]); + a.push(1, ...arr); eq("File \"splice_test.res\", line 99, characters 7-14", a, Caml_array.concat({ hd: [1], tl: { @@ -179,10 +168,7 @@ let Pipe = { }; function f1(c) { - return Caml_splice_call.spliceApply(Math.max, [ - 1, - c - ]); + return Math.max(1, ...c); } eq("File \"splice_test.res\", line 109, characters 3-10", Math.max(1, 2, 3), 3); @@ -196,7 +182,7 @@ Mt.from_pair_suites("splice_test.res", suites.contents); exports.suites = suites; exports.test_id = test_id; exports.eq = eq; -exports.Caml_splice_call = Caml_splice_call$1; +exports.Caml_splice_call = Caml_splice_call; exports.f00 = f00; exports.dynamic = dynamic; exports.dynamicNew = dynamicNew; diff --git a/jscomp/test/tagged_template_test.js b/jscomp/test/tagged_template_test.js index f4537c3b6c..df3b6e4541 100644 --- a/jscomp/test/tagged_template_test.js +++ b/jscomp/test/tagged_template_test.js @@ -3,14 +3,10 @@ let Mt = require("./mt.js"); let Caml_array = require("../../lib/js/caml_array.js"); -let Caml_splice_call = require("../../lib/js/caml_splice_call.js"); let Tagged_template_libJs = require("./tagged_template_lib.js"); function sql(prim0, prim1) { - return Caml_splice_call.spliceApply(Tagged_template_libJs.sql, [ - prim0, - prim1 - ]); + return Tagged_template_libJs.sql(prim0, ...prim1); } let Pg = { diff --git a/lib/es6/caml_splice_call.js b/lib/es6/caml_splice_call.js deleted file mode 100644 index 6de399047a..0000000000 --- a/lib/es6/caml_splice_call.js +++ /dev/null @@ -1,52 +0,0 @@ - - - -let spliceApply = (function(fn,args){ - var i, argLen; - argLen = args.length - var applied = [] - for(i = 0; i < argLen - 1; ++i){ - applied.push(args[i]) - } - var lastOne = args[argLen - 1] - for(i = 0; i < lastOne.length; ++i ){ - applied.push(lastOne[i]) - } - return fn.apply(null,applied) -}); - -let spliceNewApply = (function (ctor,args){ - var i, argLen; - argLen = args.length - var applied = [null] // Function.prototype.bind.apply(fn, args) requires the first element in \`args\` to be \`null\` - for(i = 0; i < argLen - 1; ++i){ - applied.push(args[i]) - } - var lastOne = args[argLen - 1] - for(i = 0; i < lastOne.length; ++i ){ - applied.push(lastOne[i]) - } - var C = Function.prototype.bind.apply(ctor, applied) - return new C() -}); - -let spliceObjApply = (function(obj,name,args){ - var i, argLen; - argLen = args.length - var applied = [] - for(i = 0; i < argLen - 1; ++i){ - applied.push(args[i]) - } - var lastOne = args[argLen - 1] - for(i = 0; i < lastOne.length; ++i ){ - applied.push(lastOne[i]) - } - return (obj[name]).apply(obj,applied) -}); - -export { - spliceApply, - spliceNewApply, - spliceObjApply, -} -/* No side effect */ diff --git a/lib/es6/js_array.js b/lib/es6/js_array.js index 83191f169c..792c8b9f75 100644 --- a/lib/es6/js_array.js +++ b/lib/es6/js_array.js @@ -1,7 +1,6 @@ import * as Caml_option from "./caml_option.js"; -import * as Caml_splice_call from "./caml_splice_call.js"; function copyWithin(to_, obj) { return obj.copyWithin(to_); @@ -32,7 +31,7 @@ function push(arg1, obj) { } function pushMany(arg1, obj) { - return Caml_splice_call.spliceObjApply(obj, "push", [arg1]); + return obj.push(...arg1); } function sortInPlaceWith(arg1, obj) { @@ -40,11 +39,7 @@ function sortInPlaceWith(arg1, obj) { } function spliceInPlace(pos, remove, add, obj) { - return Caml_splice_call.spliceObjApply(obj, "splice", [ - pos, - remove, - add - ]); + return obj.splice(pos, remove, ...add); } function removeFromInPlace(pos, obj) { @@ -60,7 +55,7 @@ function unshift(arg1, obj) { } function unshiftMany(arg1, obj) { - return Caml_splice_call.spliceObjApply(obj, "unshift", [arg1]); + return obj.unshift(...arg1); } function concat(arg1, obj) { @@ -68,7 +63,7 @@ function concat(arg1, obj) { } function concatMany(arg1, obj) { - return Caml_splice_call.spliceObjApply(obj, "concat", [arg1]); + return obj.concat(...arg1); } function includes(arg1, obj) { diff --git a/lib/es6/js_string.js b/lib/es6/js_string.js index 0346faf3c4..05ba50bd0c 100644 --- a/lib/es6/js_string.js +++ b/lib/es6/js_string.js @@ -1,7 +1,6 @@ import * as Caml_option from "./caml_option.js"; -import * as Caml_splice_call from "./caml_splice_call.js"; function charAt(arg1, obj) { return obj.charAt(arg1); @@ -20,7 +19,7 @@ function concat(arg1, obj) { } function concatMany(arg1, obj) { - return Caml_splice_call.spliceObjApply(obj, "concat", [arg1]); + return obj.concat(...arg1); } function endsWith(arg1, obj) { diff --git a/lib/es6/jsxPPXReactSupport.js b/lib/es6/jsxPPXReactSupport.js index c6995106b8..0c02a43e4d 100644 --- a/lib/es6/jsxPPXReactSupport.js +++ b/lib/es6/jsxPPXReactSupport.js @@ -1,7 +1,6 @@ import * as React from "react"; -import * as Caml_splice_call from "./caml_splice_call.js"; function createElementWithKey(key, component, props) { return React.createElement(component, key !== undefined ? Object.assign({ @@ -10,13 +9,9 @@ function createElementWithKey(key, component, props) { } function createElementVariadicWithKey(key, component, props, elements) { - return Caml_splice_call.spliceApply(React.createElement, [ - component, - key !== undefined ? Object.assign({ - key: key - }, props) : props, - elements - ]); + return React.createElement(component, key !== undefined ? Object.assign({ + key: key + }, props) : props, ...elements); } export { diff --git a/lib/js/caml_splice_call.js b/lib/js/caml_splice_call.js deleted file mode 100644 index f5a5b17c99..0000000000 --- a/lib/js/caml_splice_call.js +++ /dev/null @@ -1,50 +0,0 @@ -'use strict'; - - -let spliceApply = (function(fn,args){ - var i, argLen; - argLen = args.length - var applied = [] - for(i = 0; i < argLen - 1; ++i){ - applied.push(args[i]) - } - var lastOne = args[argLen - 1] - for(i = 0; i < lastOne.length; ++i ){ - applied.push(lastOne[i]) - } - return fn.apply(null,applied) -}); - -let spliceNewApply = (function (ctor,args){ - var i, argLen; - argLen = args.length - var applied = [null] // Function.prototype.bind.apply(fn, args) requires the first element in \`args\` to be \`null\` - for(i = 0; i < argLen - 1; ++i){ - applied.push(args[i]) - } - var lastOne = args[argLen - 1] - for(i = 0; i < lastOne.length; ++i ){ - applied.push(lastOne[i]) - } - var C = Function.prototype.bind.apply(ctor, applied) - return new C() -}); - -let spliceObjApply = (function(obj,name,args){ - var i, argLen; - argLen = args.length - var applied = [] - for(i = 0; i < argLen - 1; ++i){ - applied.push(args[i]) - } - var lastOne = args[argLen - 1] - for(i = 0; i < lastOne.length; ++i ){ - applied.push(lastOne[i]) - } - return (obj[name]).apply(obj,applied) -}); - -exports.spliceApply = spliceApply; -exports.spliceNewApply = spliceNewApply; -exports.spliceObjApply = spliceObjApply; -/* No side effect */ diff --git a/lib/js/js_array.js b/lib/js/js_array.js index 889ea1c35e..69df233cf0 100644 --- a/lib/js/js_array.js +++ b/lib/js/js_array.js @@ -1,7 +1,6 @@ 'use strict'; let Caml_option = require("./caml_option.js"); -let Caml_splice_call = require("./caml_splice_call.js"); function copyWithin(to_, obj) { return obj.copyWithin(to_); @@ -32,7 +31,7 @@ function push(arg1, obj) { } function pushMany(arg1, obj) { - return Caml_splice_call.spliceObjApply(obj, "push", [arg1]); + return obj.push(...arg1); } function sortInPlaceWith(arg1, obj) { @@ -40,11 +39,7 @@ function sortInPlaceWith(arg1, obj) { } function spliceInPlace(pos, remove, add, obj) { - return Caml_splice_call.spliceObjApply(obj, "splice", [ - pos, - remove, - add - ]); + return obj.splice(pos, remove, ...add); } function removeFromInPlace(pos, obj) { @@ -60,7 +55,7 @@ function unshift(arg1, obj) { } function unshiftMany(arg1, obj) { - return Caml_splice_call.spliceObjApply(obj, "unshift", [arg1]); + return obj.unshift(...arg1); } function concat(arg1, obj) { @@ -68,7 +63,7 @@ function concat(arg1, obj) { } function concatMany(arg1, obj) { - return Caml_splice_call.spliceObjApply(obj, "concat", [arg1]); + return obj.concat(...arg1); } function includes(arg1, obj) { diff --git a/lib/js/js_string.js b/lib/js/js_string.js index 52e683195e..93d840b104 100644 --- a/lib/js/js_string.js +++ b/lib/js/js_string.js @@ -1,7 +1,6 @@ 'use strict'; let Caml_option = require("./caml_option.js"); -let Caml_splice_call = require("./caml_splice_call.js"); function charAt(arg1, obj) { return obj.charAt(arg1); @@ -20,7 +19,7 @@ function concat(arg1, obj) { } function concatMany(arg1, obj) { - return Caml_splice_call.spliceObjApply(obj, "concat", [arg1]); + return obj.concat(...arg1); } function endsWith(arg1, obj) { diff --git a/lib/js/jsxPPXReactSupport.js b/lib/js/jsxPPXReactSupport.js index dcf807669c..80d0be56af 100644 --- a/lib/js/jsxPPXReactSupport.js +++ b/lib/js/jsxPPXReactSupport.js @@ -1,7 +1,6 @@ 'use strict'; let React = require("react"); -let Caml_splice_call = require("./caml_splice_call.js"); function createElementWithKey(key, component, props) { return React.createElement(component, key !== undefined ? Object.assign({ @@ -10,13 +9,9 @@ function createElementWithKey(key, component, props) { } function createElementVariadicWithKey(key, component, props, elements) { - return Caml_splice_call.spliceApply(React.createElement, [ - component, - key !== undefined ? Object.assign({ - key: key - }, props) : props, - elements - ]); + return React.createElement(component, key !== undefined ? Object.assign({ + key: key + }, props) : props, ...elements); } exports.createElementWithKey = createElementWithKey; diff --git a/packages/artifacts.txt b/packages/artifacts.txt index f63d74dd55..dd4d48af97 100644 --- a/packages/artifacts.txt +++ b/packages/artifacts.txt @@ -97,7 +97,6 @@ lib/es6/caml_nativeint_extern.js lib/es6/caml_obj.js lib/es6/caml_option.js lib/es6/caml_parser.js -lib/es6/caml_splice_call.js lib/es6/caml_string.js lib/es6/caml_string_extern.js lib/es6/caml_sys.js @@ -256,7 +255,6 @@ lib/js/caml_nativeint_extern.js lib/js/caml_obj.js lib/js/caml_option.js lib/js/caml_parser.js -lib/js/caml_splice_call.js lib/js/caml_string.js lib/js/caml_string_extern.js lib/js/caml_sys.js From 0f82db9f91552883b3fbd3c42f2d4805cc6a7a6e Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Wed, 11 Sep 2024 05:16:00 +0900 Subject: [PATCH 2/2] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 057a68fa93..8f1be5ebd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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