From a9a2d8bb5f72cc18a4e20316b27211afbab74c77 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Thu, 21 Nov 2024 10:50:36 +0100 Subject: [PATCH] Runtime: no longer use arguments --- biome.json | 1 - runtime/js/jslib.js | 55 ++++++++++++------------------------- runtime/js/stdlib.js | 21 +++++--------- runtime/js/stdlib_modern.js | 21 +++++--------- 4 files changed, 31 insertions(+), 67 deletions(-) diff --git a/biome.json b/biome.json index 9a06585594..1779bafa6c 100644 --- a/biome.json +++ b/biome.json @@ -26,7 +26,6 @@ "noSubstr": "error" }, "style": { - "noArguments": "off", "noCommaOperator": "off", "noParameterAssign": "off", "noUselessElse": "off", diff --git a/runtime/js/jslib.js b/runtime/js/jslib.js index b7d78e6d8c..da4db1a2bb 100644 --- a/runtime/js/jslib.js +++ b/runtime/js/jslib.js @@ -379,12 +379,8 @@ function caml_ojs_new_arr(c, a) { //Provides: caml_js_wrap_callback const (const) //Requires: caml_callback function caml_js_wrap_callback(f) { - return function () { - var len = arguments.length; - if (len > 0) { - var args = new Array(len); - for (var i = 0; i < len; i++) args[i] = arguments[i]; - } else { + return function (...args) { + if (args.length === 0) { args = [undefined]; } var res = caml_callback(f, args); @@ -395,75 +391,58 @@ function caml_js_wrap_callback(f) { //Provides: caml_js_wrap_callback_arguments //Requires: caml_callback function caml_js_wrap_callback_arguments(f) { - return function () { - var len = arguments.length; - var args = new Array(len); - for (var i = 0; i < len; i++) args[i] = arguments[i]; + return function (...args) { return caml_callback(f, [args]); }; } //Provides: caml_js_wrap_callback_strict const //Requires: caml_callback function caml_js_wrap_callback_strict(arity, f) { - return function () { - var n = arguments.length; - var args = new Array(arity); - var len = Math.min(arguments.length, arity); - for (var i = 0; i < len; i++) args[i] = arguments[i]; + return function (...args) { + args.length = arity; return caml_callback(f, args); }; } //Provides: caml_js_wrap_callback_unsafe const (const) //Requires: caml_callback, caml_js_function_arity function caml_js_wrap_callback_unsafe(f) { - return function () { + return function (...args) { var len = caml_js_function_arity(f); - var args = new Array(len); - for (var i = 0; i < len; i++) args[i] = arguments[i]; + args.length = len; return caml_callback(f, args); }; } //Provides: caml_js_wrap_meth_callback const (const) //Requires: caml_callback, caml_js_wrap_callback function caml_js_wrap_meth_callback(f) { - return function () { - var len = arguments.length; - var args = new Array(len + 1); - args[0] = this; - for (var i = 0; i < len; i++) args[i + 1] = arguments[i]; - var res = caml_callback(f, args); + return function (...args) { + var res = caml_callback(f, [this, ...args]); return res instanceof Function ? caml_js_wrap_callback(res) : res; }; } //Provides: caml_js_wrap_meth_callback_arguments const (const) //Requires: caml_callback function caml_js_wrap_meth_callback_arguments(f) { - return function () { - var len = arguments.length; - var args = new Array(len); - for (var i = 0; i < len; i++) args[i] = arguments[i]; + return function (...args) { return caml_callback(f, [this, args]); }; } //Provides: caml_js_wrap_meth_callback_strict const //Requires: caml_callback function caml_js_wrap_meth_callback_strict(arity, f) { - return function () { - var args = new Array(arity + 1); - var len = Math.min(arguments.length, arity); - args[0] = this; - for (var i = 0; i < len; i++) args[i + 1] = arguments[i]; + return function (...args) { + args = [this, ...args]; + args.length = arity + 1; return caml_callback(f, args); }; } //Provides: caml_js_wrap_meth_callback_unsafe const (const) //Requires: caml_callback, caml_js_function_arity function caml_js_wrap_meth_callback_unsafe(f) { - return function () { - var len = caml_js_function_arity(f) - 1; - var args = new Array(len + 1); - args[0] = this; - for (var i = 0; i < len; i++) args[i + 1] = arguments[i]; + return function (...args) { + var len = caml_js_function_arity(f); + args = [this, ...args]; + args.length = len; return caml_callback(f, args); }; } diff --git a/runtime/js/stdlib.js b/runtime/js/stdlib.js index 433f53ab7c..cd33308551 100644 --- a/runtime/js/stdlib.js +++ b/runtime/js/stdlib.js @@ -51,13 +51,9 @@ function caml_call_gen(f, args) { break; } default: { - var g = function () { - var extra_args = arguments.length === 0 ? 1 : arguments.length; - var nargs = new Array(args.length + extra_args); - for (var i = 0; i < args.length; i++) nargs[i] = args[i]; - for (var i = 0; i < arguments.length; i++) - nargs[args.length + i] = arguments[i]; - return caml_call_gen(f, nargs); + var g = function (...extra_args) { + if (extra_args.length === 0) extra_args = [undefined]; + return caml_call_gen(f, [...args, ...extra_args]); }; } } @@ -112,13 +108,10 @@ function caml_call_gen(f, args) { break; } default: { - var g = function () { - var extra_args = arguments.length === 0 ? 1 : arguments.length; - var nargs = new Array(argsLen + extra_args); - for (var i = 0; i < argsLen; i++) nargs[i] = args[i]; - for (var i = 0; i < arguments.length; i++) - nargs[argsLen + i] = arguments[i]; - return caml_call_gen(f, nargs); + args.length = argsLen; + var g = function (...extra_args) { + if (extra_args.length === 0) extra_args = [undefined]; + return caml_call_gen(f, [...args, ...extra_args]); }; } } diff --git a/runtime/js/stdlib_modern.js b/runtime/js/stdlib_modern.js index ab779fa6c3..82ca3c7fee 100644 --- a/runtime/js/stdlib_modern.js +++ b/runtime/js/stdlib_modern.js @@ -49,13 +49,9 @@ function caml_call_gen(f, args) { break; } default: { - var g = function () { - var extra_args = arguments.length === 0 ? 1 : arguments.length; - var nargs = new Array(args.length + extra_args); - for (var i = 0; i < args.length; i++) nargs[i] = args[i]; - for (var i = 0; i < arguments.length; i++) - nargs[args.length + i] = arguments[i]; - return caml_call_gen(f, nargs); + var g = function (...extra_args) { + if (extra_args.length === 0) extra_args = [undefined]; + return caml_call_gen(f, [...args, ...extra_args]); }; } } @@ -108,13 +104,10 @@ function caml_call_gen(f, args) { break; } default: { - var g = function () { - var extra_args = arguments.length === 0 ? 1 : arguments.length; - var nargs = new Array(argsLen + extra_args); - for (var i = 0; i < argsLen; i++) nargs[i] = args[i]; - for (var i = 0; i < arguments.length; i++) - nargs[argsLen + i] = arguments[i]; - return caml_call_gen(f, nargs); + args.length = argsLen; + var g = function (...extra_args) { + if (extra_args.length === 0) extra_args = [undefined]; + return caml_call_gen(f, [...args, ...extra_args]); }; } }