From d56aafca2865db012777c7403f0166e9fc27df78 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Fri, 20 Sep 2024 02:24:34 +0900 Subject: [PATCH] runtime: cleanup Signed-off-by: Sora Morimoto --- runtime/array.js | 37 ++++++------------------------------- runtime/backtrace.js | 14 +++++++------- runtime/bigarray.js | 8 +++----- 3 files changed, 16 insertions(+), 43 deletions(-) diff --git a/runtime/array.js b/runtime/array.js index 23b329eb50..22aaaf3628 100644 --- a/runtime/array.js +++ b/runtime/array.js @@ -19,26 +19,12 @@ //Provides: caml_array_sub mutable function caml_array_sub(a, i, len) { - const a2 = new Array(len + 1); - a2[0] = 0; - for (let i2 = 1, i1 = i + 1; i2 <= len; i2++, i1++) { - a2[i2] = a[i1]; - } - return a2; + return [0, ...a.slice(i + 1, i + len + 1)]; } //Provides: caml_array_append mutable function caml_array_append(a1, a2) { - const l1 = a1.length; - const l2 = a2.length; - const l = l1 + l2 - 1; - const a = new Array(l); - a[0] = 0; - let i = 1; - let j = 1; - for (; i < l1; i++) a[i] = a1[i]; - for (; i < l; i++, j++) a[i] = a2[j]; - return a; + return [0, ...a1.slice(1), ...a2.slice(1)]; } //Provides: caml_array_concat mutable @@ -107,30 +93,19 @@ function caml_check_bound(array, index) { //Requires: caml_array_bound_error function caml_make_vect(len, init) { if (len < 0) caml_array_bound_error(); - const len_ = (len + 1) | 0; - const b = new Array(len_); - b[0] = 0; - for (let i = 1; i < len_; i++) b[i] = init; - return b; + return [0, ...new Array(len | 0).fill(init)]; } //Provides: caml_make_float_vect const (const) //Requires: caml_array_bound_error function caml_make_float_vect(len) { if (len < 0) caml_array_bound_error(); - const len_ = (len + 1) | 0; - const b = new Array(len); - b[0] = 254; - for (let i = 1; i < len_; i++) b[i] = 0; - return b; + return [254, ...new Array(len).fill(0)]; } + //Provides: caml_floatarray_create const (const) //Requires: caml_array_bound_error function caml_floatarray_create(len) { if (len < 0) caml_array_bound_error(); - const len_ = (len + 1) | 0; - const b = new Array(len); - b[0] = 254; - for (let i = 1; i < len_; i++) b[i] = 0; - return b; + return [254, ...new Array(len).fill(0)]; } diff --git a/runtime/backtrace.js b/runtime/backtrace.js index b2a44f8002..8af469ba34 100644 --- a/runtime/backtrace.js +++ b/runtime/backtrace.js @@ -20,16 +20,16 @@ let caml_record_backtrace_env_flag = FLAG("with-js-error"); (() => { - const r = jsoo_sys_getenv("OCAMLRUNPARAM"); - if (r !== undefined) { - const l = r.split(","); - for (let i = 0; i < l.length; i++) { - if (l[i] === "b") { + const run_param = jsoo_sys_getenv("OCAMLRUNPARAM"); + if (run_param !== undefined) { + const params = run_param.split(","); + for (const param of params) { + if (param === "b") { caml_record_backtrace_env_flag = 1; break; } - if (l[i].startsWith("b=")) { - caml_record_backtrace_env_flag = +l[i].slice(2); + if (param.startsWith("b=")) { + caml_record_backtrace_env_flag = +param.slice(2); } } } diff --git a/runtime/bigarray.js b/runtime/bigarray.js index c45620c0e2..4cb44d196c 100644 --- a/runtime/bigarray.js +++ b/runtime/bigarray.js @@ -32,12 +32,10 @@ function caml_ba_init() { //Provides: caml_ba_get_size //Requires: caml_invalid_argument function caml_ba_get_size(dims) { - const n_dims = dims.length; let size = 1; - for (let i = 0; i < n_dims; i++) { - if (dims[i] < 0) - caml_invalid_argument("Bigarray.create: negative dimension"); - size = size * dims[i]; + for (const dim of dims) { + if (dim < 0) caml_invalid_argument("Bigarray.create: negative dimension"); + size *= dim; } return size; }