From 6bfaf0b8253e392751e0dffcb40aa9bd8746cbf1 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Fri, 20 Sep 2024 02:31:30 +0900 Subject: [PATCH] runtime: cleanup Signed-off-by: Sora Morimoto --- biome.json | 3 ++- runtime/array.js | 37 ++++++------------------------------ runtime/bigarray.js | 10 +++++----- runtime/bigstring.js | 10 +++++----- runtime/blake2.js | 4 ++-- runtime/fail.js | 2 +- runtime/graphics.js | 6 +++--- runtime/ieee_754.js | 9 +++++---- runtime/int64.js | 8 ++++---- runtime/ints.js | 2 +- runtime/jslib.js | 4 +--- runtime/jslib_js_of_ocaml.js | 2 +- runtime/mlBytes.js | 14 +++++--------- runtime/nat.js | 2 +- runtime/str.js | 4 ++-- runtime/unix.js | 2 +- runtime/zstd.js | 2 +- 17 files changed, 46 insertions(+), 75 deletions(-) diff --git a/biome.json b/biome.json index 39e0cd5e47..e146aa1372 100644 --- a/biome.json +++ b/biome.json @@ -13,7 +13,8 @@ "rules": { "recommended": true, "style": { - "noParameterAssign": "off" + "noParameterAssign": "off", + "useNamingConvention": "off" }, "suspicious": { "noAssignInExpressions": "off", 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/bigarray.js b/runtime/bigarray.js index c45620c0e2..b852c8f95b 100644 --- a/runtime/bigarray.js +++ b/runtime/bigarray.js @@ -37,7 +37,7 @@ function caml_ba_get_size(dims) { for (let i = 0; i < n_dims; i++) { if (dims[i] < 0) caml_invalid_argument("Bigarray.create: negative dimension"); - size = size * dims[i]; + size *= dims[i]; } return size; } @@ -539,12 +539,12 @@ function caml_ba_sub(ba, ofs, len) { let changed_dim; let mul = 1; if (ba.layout === 0) { - for (let i = 1; i < ba.dims.length; i++) mul = mul * ba.dims[i]; + for (let i = 1; i < ba.dims.length; i++) mul *= ba.dims[i]; changed_dim = 0; } else { - for (let i = 0; i < ba.dims.length - 1; i++) mul = mul * ba.dims[i]; + for (let i = 0; i < ba.dims.length - 1; i++) mul *= ba.dims[i]; changed_dim = ba.dims.length - 1; - ofs = ofs - 1; + ofs -= 1; } if (ofs < 0 || len < 0 || ofs + len > ba.dims[changed_dim]) { caml_invalid_argument("Bigarray.sub: bad sub-array"); @@ -606,7 +606,7 @@ function caml_ba_reshape(ba, vind) { new_dim[i] = vind[i]; if (new_dim[i] < 0) caml_invalid_argument("Bigarray.reshape: negative dimension"); - num_elts = num_elts * new_dim[i]; + num_elts *= new_dim[i]; } const size = caml_ba_get_size(ba.dims); diff --git a/runtime/bigstring.js b/runtime/bigstring.js index ed9e55682a..c4f9771686 100644 --- a/runtime/bigstring.js +++ b/runtime/bigstring.js @@ -49,9 +49,9 @@ function caml_bigstring_memcmp(s1, pos1, s2, pos2, len) { //Provides: caml_bigstring_blit_ba_to_ba //Requires: caml_invalid_argument, caml_array_bound_error function caml_bigstring_blit_ba_to_ba(ba1, pos1, ba2, pos2, len) { - if (12 !== ba1.kind) + if (ba1.kind !== 12) caml_invalid_argument("caml_bigstring_blit_ba_to_ba: kind mismatch"); - if (12 !== ba2.kind) + if (ba2.kind !== 12) caml_invalid_argument("caml_bigstring_blit_ba_to_ba: kind mismatch"); if (len === 0) return 0; const ofs1 = ba1.offset(pos1); @@ -71,7 +71,7 @@ function caml_bigstring_blit_ba_to_ba(ba1, pos1, ba2, pos2, len) { //Requires: caml_invalid_argument, caml_array_bound_error, caml_uint8_array_of_string //Requires: caml_ml_string_length function caml_bigstring_blit_string_to_ba(str1, pos1, ba2, pos2, len) { - if (12 !== ba2.kind) + if (ba2.kind !== 12) caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch"); if (len === 0) return 0; const ofs2 = ba2.offset(pos2); @@ -90,7 +90,7 @@ function caml_bigstring_blit_string_to_ba(str1, pos1, ba2, pos2, len) { //Requires: caml_invalid_argument, caml_array_bound_error, caml_uint8_array_of_bytes //Requires: caml_ml_bytes_length function caml_bigstring_blit_bytes_to_ba(str1, pos1, ba2, pos2, len) { - if (12 !== ba2.kind) + if (ba2.kind !== 12) caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch"); if (len === 0) return 0; const ofs2 = ba2.offset(pos2); @@ -110,7 +110,7 @@ function caml_bigstring_blit_bytes_to_ba(str1, pos1, ba2, pos2, len) { //Requires: caml_blit_bytes, caml_bytes_of_array //Requires: caml_ml_bytes_length function caml_bigstring_blit_ba_to_bytes(ba1, pos1, bytes2, pos2, len) { - if (12 !== ba1.kind) + if (ba1.kind !== 12) caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch"); if (len === 0) return 0; const ofs1 = ba1.offset(pos1); diff --git a/runtime/blake2.js b/runtime/blake2.js index 2203294e6e..6c084559d7 100644 --- a/runtime/blake2.js +++ b/runtime/blake2.js @@ -120,8 +120,8 @@ const blake2b = (() => { } // low 64 bits of offset - v[24] = v[24] ^ ctx.t; - v[25] = v[25] ^ (ctx.t / 0x100000000); + v[24] ^= ctx.t; + v[25] ^= ctx.t / 0x100000000; // high 64 bits not supported, offset may not be higher than 2**53-1 // last block flag set ? diff --git a/runtime/fail.js b/runtime/fail.js index 1d716f180c..c59ba3a5c1 100644 --- a/runtime/fail.js +++ b/runtime/fail.js @@ -31,7 +31,7 @@ function caml_raise_with_arg(tag, arg) { //Provides: caml_raise_with_args (const, mutable) //Requires: caml_maybe_attach_backtrace function caml_raise_with_args(tag, args) { - throw caml_maybe_attach_backtrace([0, tag].concat(args)); + throw caml_maybe_attach_backtrace([0, tag, ...args]); } //Provides: caml_raise_with_string (const, const) diff --git a/runtime/graphics.js b/runtime/graphics.js index 6ea87f4911..62a2a9482b 100644 --- a/runtime/graphics.js +++ b/runtime/graphics.js @@ -452,7 +452,9 @@ function caml_gr_dump_image(im) { //Requires: caml_gr_state_get function caml_gr_draw_image(im, x, y) { const s = caml_gr_state_get(); - if (!im.image) { + if (im.image) { + s.context.drawImage(im.image, x, s.height - im.height - y); + } else { const canvas = document.createElement("canvas"); canvas.width = s.width; canvas.height = s.height; @@ -463,8 +465,6 @@ function caml_gr_draw_image(im, x, y) { im.image = image; }; image.src = canvas.toDataURL("image/png"); - } else { - s.context.drawImage(im.image, x, s.height - im.height - y); } return 0; } diff --git a/runtime/ieee_754.js b/runtime/ieee_754.js index 26aaf35887..77980a0648 100644 --- a/runtime/ieee_754.js +++ b/runtime/ieee_754.js @@ -518,10 +518,7 @@ function caml_format_float(fmt, x) { if (Number.isNaN(x)) { s = "nan"; f.filler = " "; - } else if (!Number.isFinite(x)) { - s = "inf"; - f.filler = " "; - } else + } else if (Number.isFinite(x)) switch (f.conv) { case "e": { s = x.toExponential(prec); @@ -565,6 +562,10 @@ function caml_format_float(fmt, x) { break; } } + else { + s = "inf"; + f.filler = " "; + } return caml_finish_formatting(f, s); } diff --git a/runtime/int64.js b/runtime/int64.js index 282ebb1764..2fdf8c46da 100644 --- a/runtime/int64.js +++ b/runtime/int64.js @@ -96,7 +96,7 @@ MlInt64.prototype.xor = function (x) { return new MlInt64(this.lo ^ x.lo, this.mi ^ x.mi, this.hi ^ x.hi); }; MlInt64.prototype.shift_left = function (s) { - s = s & 63; + s &= 63; if (s === 0) return this; if (s < 24) { return new MlInt64( @@ -114,7 +114,7 @@ MlInt64.prototype.shift_left = function (s) { return new MlInt64(0, 0, this.lo << (s - 48)); }; MlInt64.prototype.shift_right_unsigned = function (s) { - s = s & 63; + s &= 63; if (s === 0) return this; if (s < 24) return new MlInt64( @@ -131,7 +131,7 @@ MlInt64.prototype.shift_right_unsigned = function (s) { return new MlInt64(this.hi >> (s - 48), 0, 0); }; MlInt64.prototype.shift_right = function (s) { - s = s & 63; + s &= 63; if (s === 0) return this; const h = (this.hi << 16) >> 16; if (s < 24) @@ -157,7 +157,7 @@ MlInt64.prototype.lsl1 = function () { MlInt64.prototype.lsr1 = function () { this.lo = ((this.lo >>> 1) | (this.mi << 23)) & 0xffffff; this.mi = ((this.mi >>> 1) | (this.hi << 23)) & 0xffffff; - this.hi = this.hi >>> 1; + this.hi >>>= 1; }; MlInt64.prototype.udivmod = function (x) { let offset = 0; diff --git a/runtime/ints.js b/runtime/ints.js index 3e8335faa3..500ba3f046 100644 --- a/runtime/ints.js +++ b/runtime/ints.js @@ -121,7 +121,7 @@ function caml_int_of_string(s) { // For base different from 10, we expect an unsigned representation, // hence any value of 'res' (less than 'threshold') is acceptable. // But we have to convert the result back to a signed integer. - res = sign * res; + res *= sign; if (signedness && (res | 0) !== res) /* Signed representation expected, allow -2^(nbits-1) to 2^(nbits-1) - 1 */ caml_failwith("int_of_string"); diff --git a/runtime/jslib.js b/runtime/jslib.js index ca31b7d754..38f8d71bec 100644 --- a/runtime/jslib.js +++ b/runtime/jslib.js @@ -105,9 +105,7 @@ function caml_callback(f, args) { const saved_fiber_stack = caml_fiber_stack; let res = { joo_tramp: f, - joo_args: args.concat((x) => { - return x; - }), + joo_args: [...args, (x) => x], }; try { caml_exn_stack = 0; diff --git a/runtime/jslib_js_of_ocaml.js b/runtime/jslib_js_of_ocaml.js index 810eb48194..9c6b60b9d9 100644 --- a/runtime/jslib_js_of_ocaml.js +++ b/runtime/jslib_js_of_ocaml.js @@ -28,7 +28,7 @@ function caml_js_on_ie() { } //Provides: caml_js_html_escape const (const) -const caml_js_regexps = { amp: /&/g, lt: / 0; i += 1024, len -= 1024) s += f.apply(null, a.slice(i, i + Math.min(len, 1024))); return s; } @@ -92,8 +92,7 @@ function caml_utf8_of_utf16(s) { let j = i + 1; for (; j < l && (c = s.charCodeAt(j)) < 0x80; j++); if (j - i > 512) { - t.substr(0, 1); - b += t; + b += t.slice(0, 1); t = ""; b += s.slice(i, j); } else t += s.slice(i, j); @@ -128,8 +127,7 @@ function caml_utf8_of_utf16(s) { ); } if (t.length > 1024) { - t.substr(0, 1); - b += t; + b += t.slice(0, 1); t = ""; } } @@ -146,8 +144,7 @@ function caml_utf16_of_utf8(s) { let j = i + 1; for (; j < l && (c1 = s.charCodeAt(j)) < 0x80; j++); if (j - i > 512) { - t.substr(0, 1); - b += t; + b += t.slice(0, 1); t = ""; b += s.slice(i, j); } else t += s.slice(i, j); @@ -189,8 +186,7 @@ function caml_utf16_of_utf8(s) { t += String.fromCharCode(0xd7c0 + (v >> 10), 0xdc00 + (v & 0x3ff)); else t += String.fromCharCode(v); if (t.length > 1024) { - t.substr(0, 1); - b += t; + b += t.slice(0, 1); t = ""; } } diff --git a/runtime/nat.js b/runtime/nat.js index 8f01996815..392ee2cfd1 100644 --- a/runtime/nat.js +++ b/runtime/nat.js @@ -366,7 +366,7 @@ function div_nat(nat1, ofs1, len1, nat2, ofs2, len2) { nat1.data[ofs1 + i] !== 0 || compare_nat(nat1, ofs1 + i - len2, len2, nat2, ofs2, len2) >= 0 ) { - quo = quo + 1; + quo += 1; sub_nat(nat1, ofs1 + i - len2, len2 + 1, nat2, ofs2, len2, 1); } diff --git a/runtime/str.js b/runtime/str.js index 25bdf020e7..4e053433aa 100644 --- a/runtime/str.js +++ b/runtime/str.js @@ -87,7 +87,7 @@ const re_match = (() => { groups[0].start = pos; const backtrack = () => { - while (stack.length) { + while (stack.length > 0) { const item = stack.pop(); if (item.undo) { item.undo.obj[item.undo.prop] = item.undo.value; @@ -267,7 +267,7 @@ const re_match = (() => { case opcodes.ACCEPT: return accept(); case opcodes.GOTO: - pc = pc + sarg; + pc += sarg; break; case opcodes.PUSHBACK: push({ pos: { pc: pc + sarg, txt: pos } }); diff --git a/runtime/unix.js b/runtime/unix.js index a513c9b951..b0643a91ae 100644 --- a/runtime/unix.js +++ b/runtime/unix.js @@ -392,7 +392,7 @@ function caml_unix_rewinddir(dir_handle) { function caml_unix_findfirst(path) { // The Windows code adds this glob to the path, so we need to remove it let path_js = caml_jsstring_of_string(path); - path_js = path_js.replace(/(^|[\\\/])\*\.\*$/, ""); + path_js = path_js.replace(/(^|[\\/])\*\.\*$/, ""); path = caml_string_of_jsstring(path_js); // *.* is now stripped const dir_handle = caml_unix_opendir(path); diff --git a/runtime/zstd.js b/runtime/zstd.js index eb5793b5d7..40e907bd7b 100644 --- a/runtime/zstd.js +++ b/runtime/zstd.js @@ -678,7 +678,7 @@ const zstd_decompress = (() => { const bufs = []; const nb = +!buf; let ol = 0; - while (dat.length) { + while (dat.length > 0) { const st = rzfh(dat, nb || buf); if (typeof st === "object") { if (nb) {