From 5fdb95d21c9585e8289735dfc8e21c204d7f8b50 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Fri, 20 Sep 2024 00:56:29 +0900 Subject: [PATCH] runtime: more... Signed-off-by: Sora Morimoto --- biome.json | 17 +- runtime/compare.js | 35 ++-- runtime/dynlink.js | 2 +- runtime/format.js | 7 +- runtime/fs.js | 8 +- runtime/fs_fake.js | 5 +- runtime/fs_node.js | 2 +- runtime/hash.js | 6 +- runtime/ieee_754.js | 58 +++--- runtime/internalMod.js | 18 +- runtime/io.js | 9 +- runtime/jslib.js | 70 +++---- runtime/jslib_js_of_ocaml.js | 10 +- runtime/lexing.js | 4 +- runtime/marshal.js | 342 +++++++++++++++++------------------ runtime/mlBytes.js | 15 +- runtime/nat.js | 9 +- runtime/obj.js | 13 +- runtime/parsing.js | 16 +- runtime/stdlib.js | 147 ++++++++------- runtime/stdlib_modern.js | 146 ++++++++------- runtime/str.js | 6 +- runtime/sys.js | 25 ++- runtime/toplevel.js | 6 +- runtime/unix.js | 5 +- runtime/weak.js | 8 +- runtime/zstd.js | 43 +++-- 27 files changed, 492 insertions(+), 540 deletions(-) diff --git a/biome.json b/biome.json index e2288d5742..438aa63609 100644 --- a/biome.json +++ b/biome.json @@ -1,5 +1,5 @@ { - "$schema": "https://biomejs.dev/schemas/1.9.1/schema.json", + "$schema": "https://biomejs.dev/schemas/1.9.2/schema.json", "files": { "include": ["runtime"], "ignore": ["runtime/zstd.ts"] @@ -9,7 +9,20 @@ "useEditorconfig": true }, "linter": { - "enabled": false + "enabled": true, + "rules": { + "recommended": true, + "style": { + "noParameterAssign": "off" + }, + "suspicious": { + "noFallthroughSwitchClause": "off", + "noRedeclare": "off", + "noAssignInExpressions": "off", + "noSelfCompare": "off", + "useDefaultSwitchClauseLast": "off" + } + } }, "organizeImports": { "enabled": true diff --git a/runtime/compare.js b/runtime/compare.js index a6a2098132..bdb668fb29 100644 --- a/runtime/compare.js +++ b/runtime/compare.js @@ -18,31 +18,23 @@ //Provides: caml_compare_val_tag //Requires: caml_is_ml_string, caml_is_ml_bytes function caml_compare_val_tag(a) { - if (typeof a === "number") - return 1000; // int_tag (we use it for all numbers) - else if (caml_is_ml_bytes(a)) - return 252; // string_tag - else if (caml_is_ml_string(a)) - return 1252; // ocaml string (if different from bytes) - else if (Array.isArray(a) && a[0] === a[0] >>> 0 && a[0] <= 255) { + if (typeof a === "number") return 1000; + if (caml_is_ml_bytes(a)) return 252; + if (caml_is_ml_string(a)) return 1252; + if (Array.isArray(a) && a[0] === a[0] >>> 0 && a[0] <= 255) { // Look like an ocaml block const tag = a[0] | 0; // ignore double_array_tag because we cannot accurately set // this tag when we create an array of float. return tag === 254 ? 0 : tag; - } else if (a instanceof String) - return 12520; // javascript string, like string_tag (252) - else if (typeof a === "string") - return 12520; // javascript string, like string_tag (252) - else if (a instanceof Number) - return 1000; // int_tag (we use it for all numbers) - else if (a && a.caml_custom) - return 1255; // like custom_tag (255) - else if (a && a.compare) - return 1256; // like custom_tag (255) - else if (typeof a === "function") - return 1247; // like closure_tag (247) - else if (typeof a === "symbol") return 1251; + } + if (a instanceof String) return 12520; + if (typeof a === "string") return 12520; + if (a instanceof Number) return 1000; + if (a?.caml_custom) return 1255; + if (a?.compare) return 1256; + if (typeof a === "function") return 1247; + if (typeof a === "symbol") return 1251; return 1001; //out_of_heap_tag } @@ -214,6 +206,7 @@ function caml_compare_val(a, b, total) { // Exception: `!=` will not coerce/convert if both a and b are objects if (a < b) return -1; if (a > b) return 1; + // biome-ignore lint/suspicious/noDoubleEquals: type-coercive comparison if (a != b) { if (!total) return Number.NaN; if (a === a) return 1; @@ -246,8 +239,6 @@ function caml_compare_val(a, b, total) { } break; } - case 246: // Lazy_tag - case 254: // Double_array default: // Block with other tag if (caml_is_continuation_tag(tag_a)) { caml_invalid_argument("compare: continuation value"); diff --git a/runtime/dynlink.js b/runtime/dynlink.js index e2316cfc70..73e1617124 100644 --- a/runtime/dynlink.js +++ b/runtime/dynlink.js @@ -50,7 +50,7 @@ function caml_dynlink_lookup_symbol(idx, fun_name) { const name = caml_jsstring_of_string(fun_name); console.log("Dynlink: looking for symbol", name); const current_libs = get_current_libs(); - if (current_libs[idx] && current_libs[idx][name]) + if (current_libs[idx]?.[name]) return { name: name, symbol: current_libs[idx][name] }; return 0; } diff --git a/runtime/format.js b/runtime/format.js index fc7f55512d..ba2444f70f 100644 --- a/runtime/format.js +++ b/runtime/format.js @@ -60,18 +60,19 @@ function caml_parse_format(fmt) { case "6": case "7": case "8": - case "9": + case "9": { f.width = 0; - while (((c = fmt.charCodeAt(i) - 48), c >= 0 && c <= 9)) { + while ((c = fmt.charCodeAt(i) - 48) && c >= 0 && c <= 9) { f.width = f.width * 10 + c; i++; } i--; break; + } case ".": f.prec = 0; i++; - while (((c = fmt.charCodeAt(i) - 48), c >= 0 && c <= 9)) { + while ((c = fmt.charCodeAt(i) - 48) && c >= 0 && c <= 9) { f.prec = f.prec * 10 + c; i++; } diff --git a/runtime/fs.js b/runtime/fs.js index 8c2a83f396..236b9f8cf8 100644 --- a/runtime/fs.js +++ b/runtime/fs.js @@ -79,7 +79,8 @@ function make_path_is_absolute() { globalThis.process.platform ) { return globalThis.process.platform === "win32" ? win32 : posix; - } else return posix; + } + return posix; } const path_is_absolute = make_path_is_absolute(); @@ -162,7 +163,7 @@ function resolve_fs_device(name) { } if (!res && fs_node_supported()) { const root = caml_get_root(name_); - if (root && root.match(/^[a-zA-Z]:\/$/)) { + if (root?.match(/^[a-zA-Z]:\/$/)) { const m = { path: root, device: new MlNodeDevice(root) }; jsoo_mount_point.push(m); res = { @@ -212,9 +213,8 @@ function caml_sys_chdir(dir) { caml_current_dir = caml_trailing_slash(root.path + root.rest); else caml_current_dir = root.path; return 0; - } else { - caml_raise_no_such_file(caml_jsbytes_of_string(dir)); } + caml_raise_no_such_file(caml_jsbytes_of_string(dir)); } //Provides: caml_raise_no_such_file diff --git a/runtime/fs_fake.js b/runtime/fs_fake.js index 21a0a0e60c..96394f24a9 100644 --- a/runtime/fs_fake.js +++ b/runtime/fs_fake.js @@ -68,9 +68,8 @@ MlFakeDevice.prototype.exists = function (name) { MlFakeDevice.prototype.isFile = function (name) { if (this.exists(name) && !this.is_dir(name)) { return 1; - } else { - return 0; } + return 0; }; MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) { const unix_error = raise_unix && caml_named_value("Unix.Unix_error"); @@ -85,7 +84,7 @@ MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) { } } let parent = /^(.*)\/[^/]+/.exec(name); - parent = (parent && parent[1]) || ""; + parent = parent?.[1] || ""; if (!this.exists(parent)) { if (unix_error) { caml_raise_with_args( diff --git a/runtime/fs_node.js b/runtime/fs_node.js index eecfbdef4d..be18cc8555 100644 --- a/runtime/fs_node.js +++ b/runtime/fs_node.js @@ -303,7 +303,7 @@ MlNodeFd.prototype.read = function (offset, a, buf_offset, len) { try { if (this.flags.isCharacterDevice) return this.fs.readSync(this.fd, a, buf_offset, len); - else return this.fs.readSync(this.fd, a, buf_offset, len, offset); + return this.fs.readSync(this.fd, a, buf_offset, len, offset); } catch (err) { caml_raise_sys_error(err.toString()); } diff --git a/runtime/hash.js b/runtime/hash.js index f0f2655ea9..dcd5baf8b5 100644 --- a/runtime/hash.js +++ b/runtime/hash.js @@ -72,7 +72,7 @@ function caml_hash_univ_param(count, limit, obj) { count--; const p = caml_int64_to_bytes(caml_int64_bits_of_float(obj)); for (let i = 7; i >= 0; i--) hash_accu = (hash_accu * 19 + p[i]) | 0; - } else if (obj && obj.caml_custom) { + } else if (obj?.caml_custom) { if ( caml_custom_ops[obj.caml_custom] && caml_custom_ops[obj.caml_custom].hash @@ -184,7 +184,7 @@ function caml_hash_mix_bytes_arr(h, s) { function caml_hash_mix_bytes(h, v) { const content = caml_ml_bytes_content(v); if (typeof content === "string") return caml_hash_mix_jsbytes(h, content); - /* ARRAY */ else return caml_hash_mix_bytes_arr(h, content); + /* ARRAY */ return caml_hash_mix_bytes_arr(h, content); } //Provides: caml_hash_mix_string @@ -218,7 +218,7 @@ function caml_hash(count, limit, seed, obj) { wr = 1; while (rd < wr && num > 0) { v = queue[rd++]; - if (v && v.caml_custom) { + if (v?.caml_custom) { if ( caml_custom_ops[v.caml_custom] && caml_custom_ops[v.caml_custom].hash diff --git a/runtime/ieee_754.js b/runtime/ieee_754.js index b174f20ce9..26aaf35887 100644 --- a/runtime/ieee_754.js +++ b/runtime/ieee_754.js @@ -43,7 +43,7 @@ function caml_int64_bits_of_float(x) { if (!Number.isFinite(x)) { if (Number.isNaN(x)) return caml_int64_create_lo_mi_hi(1, 0, 0x7ff0); if (x > 0) return caml_int64_create_lo_mi_hi(0, 0, 0x7ff0); - else return caml_int64_create_lo_mi_hi(0, 0, 0xfff0); + return caml_int64_create_lo_mi_hi(0, 0, 0xfff0); } const sign = x === 0 && 1 / x === Number.NEGATIVE_INFINITY @@ -159,7 +159,7 @@ function caml_int64_float_of_bits(x) { if (exp === 2047) { if ((lo | mi | (hi & 0xf)) === 0) return hi & 0x8000 ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY; - else return Number.NaN; + return Number.NaN; } const k = 2 ** -24; let res = (lo * k + mi) * k + (hi & 0xf); @@ -178,7 +178,7 @@ function caml_nextafter_float(x, y) { if (x === y) return y; if (x === 0) { if (y < 0) return -(2 ** -1074); - else return 2 ** -1074; + return 2 ** -1074; } let bits = caml_int64_bits_of_float(x); const one = caml_int64_of_int32(1); @@ -340,10 +340,9 @@ function caml_round_float(x) { if (x >= 0) { const y = Math.floor(x); return x - y >= 0.5 ? y + 1 : y; - } else { - const y = Math.ceil(x); - return y - x >= 0.5 ? y - 1 : y; } + const y = Math.ceil(x); + return y - x >= 0.5 ? y - 1 : y; } //Provides: caml_cbrt_float const function caml_cbrt_float(x) { @@ -496,18 +495,18 @@ function caml_format_float(fmt, x) { function toFixed(x, dp) { if (Math.abs(x) < 1.0) { return x.toFixed(dp); - } else { - let e = Number.parseInt(x.toString().split("+")[1]); - if (e > 20) { - e -= 20; - x /= 10 ** e; - x += new Array(e + 1).join("0"); - if (dp > 0) { - x = `${x}.${new Array(dp + 1).join("0")}`; - } - return x; - } else return x.toFixed(dp); } + let e = Number.parseInt(x.toString().split("+")[1]); + if (e > 20) { + e -= 20; + x /= 10 ** e; + x += new Array(e + 1).join("0"); + if (dp > 0) { + x = `${x}.${new Array(dp + 1).join("0")}`; + } + return x; + } + return x.toFixed(dp); } let s; const f = caml_parse_format(fmt); @@ -550,19 +549,18 @@ function caml_format_float(fmt, x) { if (s.charAt(i - 3) === "e") s = `${s.slice(0, i - 1)}0${s.slice(i - 1)}`; break; - } else { - let p = prec; - if (exp < 0) { - p -= exp + 1; - s = x.toFixed(p); - } else while (((s = x.toFixed(p)), s.length > prec + 1)) p--; - if (p) { - // remove trailing zeroes - let i = s.length - 1; - while (s.charAt(i) === "0") i--; - if (s.charAt(i) === ".") i--; - s = s.slice(0, i + 1); - } + } + let p = prec; + if (exp < 0) { + p -= exp + 1; + s = x.toFixed(p); + } else while ((s = x.toFixed(p)) && s.length > prec + 1) p--; + if (p) { + // remove trailing zeroes + let i = s.length - 1; + while (s.charAt(i) === "0") i--; + if (s.charAt(i) === ".") i--; + s = s.slice(0, i + 1); } break; } diff --git a/runtime/internalMod.js b/runtime/internalMod.js index 102606fff6..01a6783301 100644 --- a/runtime/internalMod.js +++ b/runtime/internalMod.js @@ -61,14 +61,7 @@ function caml_CamlinternalMod_init_mod(loc, shape) { //If: !effects //Version: < 4.13 function caml_CamlinternalMod_update_mod(shape, real, x) { - if (typeof shape === "number") - switch (shape) { - case 0: //function - case 1: //lazy - case 2: //class - default: - caml_update_dummy(real, x); - } + if (typeof shape === "number") caml_update_dummy(real, x); else switch (shape[0]) { case 0: //module @@ -126,14 +119,7 @@ function caml_CamlinternalMod_init_mod(loc, shape, cont) { //Version: < 4.13 function caml_CamlinternalMod_update_mod(shape, real, x, cont) { function loop(shape, real, x) { - if (typeof shape === "number") - switch (shape) { - case 0: //function - case 1: //lazy - case 2: //class - default: - caml_update_dummy(real, x); - } + if (typeof shape === "number") caml_update_dummy(real, x); else switch (shape[0]) { case 0: //module diff --git a/runtime/io.js b/runtime/io.js index 358d28946f..5d7f8b5c0e 100644 --- a/runtime/io.js +++ b/runtime/io.js @@ -96,7 +96,8 @@ function caml_sys_open(name, flags, _perms) { function file(fd, flags) { if (fs_node_supported()) { return caml_sys_open_for_node(fd, flags); - } else return new MlFakeFd_out(fd, flags); + } + return new MlFakeFd_out(fd, flags); } caml_sys_open_internal( file(0, { rdonly: 1, altname: "/dev/stdin", isCharacterDevice: true }), @@ -152,11 +153,7 @@ function caml_ml_channel_get(id) { function caml_ml_out_channels_list() { let l = 0; for (let c = 0; c < caml_ml_channels.length; c++) { - if ( - caml_ml_channels[c] && - caml_ml_channels[c].opened && - caml_ml_channels[c].out - ) + if (caml_ml_channels[c]?.opened && caml_ml_channels[c].out) l = [0, caml_ml_channels[c].fd, l]; } return l; diff --git a/runtime/jslib.js b/runtime/jslib.js index 2d9d526048..ca31b7d754 100644 --- a/runtime/jslib.js +++ b/runtime/jslib.js @@ -53,7 +53,7 @@ function caml_js_typeof(o) { //Provides:caml_trampoline function caml_trampoline(res) { let c = 1; - while (res && res.joo_tramp) { + while (res?.joo_tramp) { res = res.joo_tramp.apply(null, res.joo_args); c++; } @@ -126,7 +126,7 @@ function caml_callback(f, args) { caml_exn_stack = caml_exn_stack[2]; res = { joo_tramp: handler, joo_args: [caml_wrap_exception(e)] }; } - } while (res && res.joo_args); + } while (res?.joo_args); } finally { caml_stack_depth = saved_stack_depth; caml_exn_stack = saved_exn_stack; @@ -181,7 +181,8 @@ function caml_wrap_exception(e) { // We already have an error at hand, let's use it. if (e instanceof globalThis.Error) exn.js_error = e; return exn; - } else return e; + } + return e; } //Provides: caml_maybe_attach_backtrace @@ -195,7 +196,7 @@ function caml_maybe_attach_backtrace(exn, force) { // it's way to slow. Here, we force the end-user to opt-in to backtraces. if (caml_record_backtrace_env_flag && caml_record_backtrace_runtime_flag) return caml_exn_with_js_backtrace(exn, force); - else return exn; + return exn; } // Experimental @@ -281,8 +282,8 @@ function caml_js_var(x) { console.error( `caml_js_var: "${x_}" is not a valid JavaScript variable. continuing ..`, ); - //console.error("Js.Unsafe.eval_string") } + // biome-ignore lint/security/noGlobalEval: expected behaviour return eval(x_); } //Provides: caml_js_call (const, mutable, shallow) @@ -376,13 +377,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 () { - const len = arguments.length; - let args; - if (len > 0) { - args = new Array(len); - for (let i = 0; i < len; i++) args[i] = arguments[i]; - } else { + return (...args) => { + if (args.length <= 0) { args = [undefined]; } const res = caml_callback(f, args); @@ -393,76 +389,57 @@ function caml_js_wrap_callback(f) { //Provides: caml_js_wrap_callback_arguments //Requires: caml_callback function caml_js_wrap_callback_arguments(f) { - return function () { - const len = arguments.length; - const args = new Array(len); - for (let i = 0; i < len; i++) args[i] = arguments[i]; + return (...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 () { - const n = arguments.length; - const args = new Array(arity); - const len = Math.min(arguments.length, arity); - for (let i = 0; i < len; i++) args[i] = arguments[i]; + return (...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 (...args) => { const len = caml_js_function_arity(f); - const args = new Array(len); - for (let 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 () { - const len = arguments.length; - const args = new Array(len + 1); - args[0] = this; - for (let i = 0; i < len; i++) args[i + 1] = arguments[i]; - const res = caml_callback(f, args); + return function (...args) { + const 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 () { - const len = arguments.length; - const args = new Array(len); - for (let 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 () { - const args = new Array(arity + 1); - const len = Math.min(arguments.length, arity); - args[0] = this; - for (let i = 0; i < len; i++) args[i + 1] = arguments[i]; - return caml_callback(f, args); + return function (...args) { + args.length = arity; + return caml_callback(f, [this, ...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 () { + return function (...args) { const len = caml_js_function_arity(f) - 1; - const args = new Array(len + 1); - args[0] = this; - for (let i = 0; i < len; i++) args[i + 1] = arguments[i]; - return caml_callback(f, args); + args.length = len; + return caml_callback(f, [this, ...args]); }; } @@ -493,6 +470,7 @@ function caml_js_strict_equals(x, y) { //Provides: caml_js_eval_string (const) //Requires: caml_jsstring_of_string function caml_js_eval_string(s) { + // biome-ignore lint/security/noGlobalEval: expected behaviour return eval(caml_jsstring_of_string(s)); } @@ -500,6 +478,7 @@ function caml_js_eval_string(s) { //Requires: caml_jsstring_of_string function caml_js_expr(s) { console.error("caml_js_expr: fallback to runtime evaluation\n"); + // biome-ignore lint/security/noGlobalEval: expected behaviour return eval(caml_jsstring_of_string(s)); } @@ -507,6 +486,7 @@ function caml_js_expr(s) { //Requires: caml_jsstring_of_string function caml_pure_js_expr(s) { console.error("caml_pure_js_expr: fallback to runtime evaluation\n"); + // biome-ignore lint/security/noGlobalEval: expected behaviour return eval(caml_jsstring_of_string(s)); } diff --git a/runtime/jslib_js_of_ocaml.js b/runtime/jslib_js_of_ocaml.js index ab50cb19be..810eb48194 100644 --- a/runtime/jslib_js_of_ocaml.js +++ b/runtime/jslib_js_of_ocaml.js @@ -21,10 +21,9 @@ //Provides: caml_js_on_ie const function caml_js_on_ie() { - const ua = - globalThis.navigator && globalThis.navigator.userAgent - ? globalThis.navigator.userAgent - : ""; + const ua = globalThis.navigator?.userAgent + ? globalThis.navigator.userAgent + : ""; return ua.indexOf("MSIE") !== -1 && ua.indexOf("Opera") !== 0; } @@ -47,9 +46,8 @@ function caml_js_html_entities(s) { const str = temp.textContent || temp.innerText; temp = null; return str; - } else { - return null; } + return null; } //Provides: caml_js_get_console const diff --git a/runtime/lexing.js b/runtime/lexing.js index c723fb16e9..93f45fd438 100644 --- a/runtime/lexing.js +++ b/runtime/lexing.js @@ -76,7 +76,7 @@ function caml_lex_engine(tbl, start_state, lexbuf) { /* See if we need a refill */ if (lexbuf[lex_curr_pos] >= lexbuf[lex_buffer_len]) { if (lexbuf[lex_eof_reached] === 0) return -state - 1; - else c = 256; + c = 256; } else { /* Read next input char */ c = buffer[lexbuf[lex_curr_pos]]; @@ -200,7 +200,7 @@ function caml_new_lex_engine(tbl, start_state, lexbuf) { /* See if we need a refill */ if (lexbuf[lex_curr_pos] >= lexbuf[lex_buffer_len]) { if (lexbuf[lex_eof_reached] === 0) return -state - 1; - else c = 256; + c = 256; } else { /* Read next input char */ c = buffer[lexbuf[lex_curr_pos]]; diff --git a/runtime/marshal.js b/runtime/marshal.js index c3dde6c132..02b12e913c 100644 --- a/runtime/marshal.js +++ b/runtime/marshal.js @@ -395,185 +395,181 @@ function caml_input_value_from_reader(reader, ofs) { if (intern_obj_table) intern_obj_table[obj_counter++] = v; stack.push(v, size); return v; - } else return code & 0x3f; - } else { - if (code >= 0x20 /*cst.PREFIX_SMALL_STRING */) { - const len = code & 0x1f; + } + return code & 0x3f; + } + if (code >= 0x20 /*cst.PREFIX_SMALL_STRING */) { + const len = code & 0x1f; + const v = reader.readstr(len); + if (intern_obj_table) intern_obj_table[obj_counter++] = v; + return v; + } + switch (code) { + case 0x00: //cst.CODE_INT8: + return reader.read8s(); + case 0x01: //cst.CODE_INT16: + return reader.read16s(); + case 0x02: //cst.CODE_INT32: + return reader.read32s(); + case 0x03: //cst.CODE_INT64: + caml_failwith("input_value: integer too large"); + break; + case 0x04: { + //cst.CODE_SHARED8: + let offset = reader.read8u(); + if (compressed === 0) offset = obj_counter - offset; + return intern_obj_table[offset]; + } + case 0x05: { + //cst.CODE_SHARED16: + let offset = reader.read16u(); + if (compressed === 0) offset = obj_counter - offset; + return intern_obj_table[offset]; + } + case 0x06: { + //cst.CODE_SHARED32: + let offset = reader.read32u(); + if (compressed === 0) offset = obj_counter - offset; + return intern_obj_table[offset]; + } + case 0x08: { + //cst.CODE_BLOCK32: + const header = reader.read32u(); + const tag = header & 0xff; + const size = header >> 10; + const v = [tag]; + if (size === 0) return v; + if (intern_obj_table) intern_obj_table[obj_counter++] = v; + stack.push(v, size); + return v; + } + case 0x13: //cst.CODE_BLOCK64: + caml_failwith("input_value: data block too large"); + break; + case 0x09: { + //cst.CODE_STRING8: + const len = reader.read8u(); const v = reader.readstr(len); if (intern_obj_table) intern_obj_table[obj_counter++] = v; return v; - } else { + } + case 0x0a: { + //cst.CODE_STRING32: + const len = reader.read32u(); + const v = reader.readstr(len); + if (intern_obj_table) intern_obj_table[obj_counter++] = v; + return v; + } + case 0x0c: { + //cst.CODE_DOUBLE_LITTLE: + const t = new Array(8); + for (let i = 0; i < 8; i++) t[7 - i] = reader.read8u(); + const v = caml_float_of_bytes(t); + if (intern_obj_table) intern_obj_table[obj_counter++] = v; + return v; + } + case 0x0b: { + //cst.CODE_DOUBLE_BIG: + const t = new Array(8); + for (let i = 0; i < 8; i++) t[i] = reader.read8u(); + const v = caml_float_of_bytes(t); + if (intern_obj_table) intern_obj_table[obj_counter++] = v; + return v; + } + case 0x0e: { + //cst.CODE_DOUBLE_ARRAY8_LITTLE: + const len = reader.read8u(); + const v = new Array(len + 1); + v[0] = 254; + const t = new Array(8); + if (intern_obj_table) intern_obj_table[obj_counter++] = v; + for (let i = 1; i <= len; i++) { + for (let j = 0; j < 8; j++) t[7 - j] = reader.read8u(); + v[i] = caml_float_of_bytes(t); + } + return v; + } + case 0x0d: { + //cst.CODE_DOUBLE_ARRAY8_BIG: + const len = reader.read8u(); + const v = new Array(len + 1); + v[0] = 254; + const t = new Array(8); + if (intern_obj_table) intern_obj_table[obj_counter++] = v; + for (let i = 1; i <= len; i++) { + for (let j = 0; j < 8; j++) t[j] = reader.read8u(); + v[i] = caml_float_of_bytes(t); + } + return v; + } + case 0x07: { + //cst.CODE_DOUBLE_ARRAY32_LITTLE: + const len = reader.read32u(); + const v = new Array(len + 1); + v[0] = 254; + if (intern_obj_table) intern_obj_table[obj_counter++] = v; + const t = new Array(8); + for (let i = 1; i <= len; i++) { + for (let j = 0; j < 8; j++) t[7 - j] = reader.read8u(); + v[i] = caml_float_of_bytes(t); + } + return v; + } + case 0x0f: { + //cst.CODE_DOUBLE_ARRAY32_BIG: + const len = reader.read32u(); + const v = new Array(len + 1); + v[0] = 254; + const t = new Array(8); + for (let i = 1; i <= len; i++) { + for (let j = 0; j < 8; j++) t[j] = reader.read8u(); + v[i] = caml_float_of_bytes(t); + } + return v; + } + case 0x10: //cst.CODE_CODEPOINTER: + case 0x11: //cst.CODE_INFIXPOINTER: + caml_failwith("input_value: code pointer"); + break; + case 0x12: //cst.CODE_CUSTOM: + case 0x18: //cst.CODE_CUSTOM_LEN: + case 0x19: { + //cst.CODE_CUSTOM_FIXED: + let c; + let s = ""; + while ((c = reader.read8u()) !== 0) s += String.fromCharCode(c); + const ops = caml_custom_ops[s]; + let expected_size; + if (!ops) caml_failwith("input_value: unknown custom block identifier"); switch (code) { - case 0x00: //cst.CODE_INT8: - return reader.read8s(); - case 0x01: //cst.CODE_INT16: - return reader.read16s(); - case 0x02: //cst.CODE_INT32: - return reader.read32s(); - case 0x03: //cst.CODE_INT64: - caml_failwith("input_value: integer too large"); + case 0x12: // cst.CODE_CUSTOM (deprecated) break; - case 0x04: { - //cst.CODE_SHARED8: - let offset = reader.read8u(); - if (compressed === 0) offset = obj_counter - offset; - return intern_obj_table[offset]; - } - case 0x05: { - //cst.CODE_SHARED16: - let offset = reader.read16u(); - if (compressed === 0) offset = obj_counter - offset; - return intern_obj_table[offset]; - } - case 0x06: { - //cst.CODE_SHARED32: - let offset = reader.read32u(); - if (compressed === 0) offset = obj_counter - offset; - return intern_obj_table[offset]; - } - case 0x08: { - //cst.CODE_BLOCK32: - const header = reader.read32u(); - const tag = header & 0xff; - const size = header >> 10; - const v = [tag]; - if (size === 0) return v; - if (intern_obj_table) intern_obj_table[obj_counter++] = v; - stack.push(v, size); - return v; - } - case 0x13: //cst.CODE_BLOCK64: - caml_failwith("input_value: data block too large"); + case 0x19: // cst.CODE_CUSTOM_FIXED + if (!ops.fixed_length) + caml_failwith("input_value: expected a fixed-size custom block"); + expected_size = ops.fixed_length; break; - case 0x09: { - //cst.CODE_STRING8: - const len = reader.read8u(); - const v = reader.readstr(len); - if (intern_obj_table) intern_obj_table[obj_counter++] = v; - return v; - } - case 0x0a: { - //cst.CODE_STRING32: - const len = reader.read32u(); - const v = reader.readstr(len); - if (intern_obj_table) intern_obj_table[obj_counter++] = v; - return v; - } - case 0x0c: { - //cst.CODE_DOUBLE_LITTLE: - const t = new Array(8); - for (let i = 0; i < 8; i++) t[7 - i] = reader.read8u(); - const v = caml_float_of_bytes(t); - if (intern_obj_table) intern_obj_table[obj_counter++] = v; - return v; - } - case 0x0b: { - //cst.CODE_DOUBLE_BIG: - const t = new Array(8); - for (let i = 0; i < 8; i++) t[i] = reader.read8u(); - const v = caml_float_of_bytes(t); - if (intern_obj_table) intern_obj_table[obj_counter++] = v; - return v; - } - case 0x0e: { - //cst.CODE_DOUBLE_ARRAY8_LITTLE: - const len = reader.read8u(); - const v = new Array(len + 1); - v[0] = 254; - const t = new Array(8); - if (intern_obj_table) intern_obj_table[obj_counter++] = v; - for (let i = 1; i <= len; i++) { - for (let j = 0; j < 8; j++) t[7 - j] = reader.read8u(); - v[i] = caml_float_of_bytes(t); - } - return v; - } - case 0x0d: { - //cst.CODE_DOUBLE_ARRAY8_BIG: - const len = reader.read8u(); - const v = new Array(len + 1); - v[0] = 254; - const t = new Array(8); - if (intern_obj_table) intern_obj_table[obj_counter++] = v; - for (let i = 1; i <= len; i++) { - for (let j = 0; j < 8; j++) t[j] = reader.read8u(); - v[i] = caml_float_of_bytes(t); - } - return v; - } - case 0x07: { - //cst.CODE_DOUBLE_ARRAY32_LITTLE: - const len = reader.read32u(); - const v = new Array(len + 1); - v[0] = 254; - if (intern_obj_table) intern_obj_table[obj_counter++] = v; - const t = new Array(8); - for (let i = 1; i <= len; i++) { - for (let j = 0; j < 8; j++) t[7 - j] = reader.read8u(); - v[i] = caml_float_of_bytes(t); - } - return v; - } - case 0x0f: { - //cst.CODE_DOUBLE_ARRAY32_BIG: - const len = reader.read32u(); - const v = new Array(len + 1); - v[0] = 254; - const t = new Array(8); - for (let i = 1; i <= len; i++) { - for (let j = 0; j < 8; j++) t[j] = reader.read8u(); - v[i] = caml_float_of_bytes(t); - } - return v; - } - case 0x10: //cst.CODE_CODEPOINTER: - case 0x11: //cst.CODE_INFIXPOINTER: - caml_failwith("input_value: code pointer"); + case 0x18: // cst.CODE_CUSTOM_LEN + expected_size = reader.read32u(); + // Skip size64 + reader.read32s(); + reader.read32s(); break; - case 0x12: //cst.CODE_CUSTOM: - case 0x18: //cst.CODE_CUSTOM_LEN: - case 0x19: { - //cst.CODE_CUSTOM_FIXED: - let c; - let s = ""; - while ((c = reader.read8u()) !== 0) s += String.fromCharCode(c); - const ops = caml_custom_ops[s]; - let expected_size; - if (!ops) - caml_failwith("input_value: unknown custom block identifier"); - switch (code) { - case 0x12: // cst.CODE_CUSTOM (deprecated) - break; - case 0x19: // cst.CODE_CUSTOM_FIXED - if (!ops.fixed_length) - caml_failwith( - "input_value: expected a fixed-size custom block", - ); - expected_size = ops.fixed_length; - break; - case 0x18: // cst.CODE_CUSTOM_LEN - expected_size = reader.read32u(); - // Skip size64 - reader.read32s(); - reader.read32s(); - break; - } - const old_pos = reader.i; - const size = [0]; - const v = ops.deserialize(reader, size); - if (expected_size !== undefined) { - if (expected_size !== size[0]) - caml_failwith( - "input_value: incorrect length of serialized custom block", - ); - } - if (intern_obj_table) intern_obj_table[obj_counter++] = v; - return v; - } - default: - caml_failwith("input_value: ill-formed message"); } + const old_pos = reader.i; + const size = [0]; + const v = ops.deserialize(reader, size); + if (expected_size !== undefined) { + if (expected_size !== size[0]) + caml_failwith( + "input_value: incorrect length of serialized custom block", + ); + } + if (intern_obj_table) intern_obj_table[obj_counter++] = v; + return v; } + default: + caml_failwith("input_value: ill-formed message"); } } if (compressed) { @@ -643,7 +639,6 @@ function caml_marshal_data_size(s, ofs) { } break; } - case 0x8495a6bf: /* Intext_magic_number_big */ default: caml_failwith("Marshal.data_size: bad object"); break; @@ -766,10 +761,9 @@ const caml_output_val = (() => { if (existing_offset) { writer.write_shared(existing_offset); return true; - } else { - intern_obj_table.store(v); - return false; } + intern_obj_table.store(v); + return false; } function extern_rec(v) { diff --git a/runtime/mlBytes.js b/runtime/mlBytes.js index 81d95062f1..d785d591f9 100644 --- a/runtime/mlBytes.js +++ b/runtime/mlBytes.js @@ -204,7 +204,9 @@ function jsoo_is_ascii(s) { // Spidermonkey gets much slower when s.length >= 24 (on 64 bit archs) for (let i = 0; i < s.length; i++) if (s.charCodeAt(i) > 127) return false; return true; - } else return !/[^\x00-\x7f]/.test(s); + } + // biome-ignore lint/suspicious/noControlCharactersInRegex: expected behaviour + return !/[^\x00-\x7f]/.test(s); } //Provides: caml_bytes_unsafe_get mutable @@ -435,9 +437,11 @@ function caml_bytes_set(s, i, c) { //Provides: caml_bytes_of_utf16_jsstring //Requires: jsoo_is_ascii, caml_utf8_of_utf16, MlBytes function caml_bytes_of_utf16_jsstring(s) { - let tag = 9 /* BYTES | ASCII */; - if (!jsoo_is_ascii(s)) - (tag = 8) /* BYTES | NOT_ASCII */, (s = caml_utf8_of_utf16(s)); + let tag = 9; /* BYTES | ASCII */ + if (!jsoo_is_ascii(s)) { + tag = 8; /* BYTES | NOT_ASCII */ + s = caml_utf8_of_utf16(s); + } return new MlBytes(tag, s, s.length); } @@ -795,7 +799,7 @@ function caml_jsstring_of_string(s) { //If: js-string function caml_string_of_jsstring(s) { if (jsoo_is_ascii(s)) return caml_string_of_jsbytes(s); - else return caml_string_of_jsbytes(caml_utf8_of_utf16(s)); + return caml_string_of_jsbytes(caml_utf8_of_utf16(s)); } //Provides: caml_bytes_of_jsbytes const @@ -918,6 +922,7 @@ function caml_ml_bytes_content(s) { //Requires: jsoo_is_ascii //If: js-string function caml_is_ml_string(s) { + // biome-ignore lint/suspicious/noControlCharactersInRegex: expected behaviour return typeof s === "string" && !/[^\x00-\xff]/.test(s); } diff --git a/runtime/nat.js b/runtime/nat.js index 7712301188..8f01996815 100644 --- a/runtime/nat.js +++ b/runtime/nat.js @@ -158,9 +158,8 @@ function incr_nat(nat, ofs, len, carry_in) { if (x === x >>> 0) { carry = 0; break; - } else { - carry = 1; } + carry = 1; } return carry; } @@ -199,9 +198,8 @@ function decr_nat(nat, ofs, len, carry_in) { if (x >= 0) { borrow = 0; break; - } else { - borrow = 1; } + borrow = 1; } return borrow === 1 ? 0 : 1; } @@ -254,9 +252,8 @@ function mult_digit_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3) { 1, 0, ); - } else { - return carry; } + return carry; } // nat1 += nat2 * nat3 diff --git a/runtime/obj.js b/runtime/obj.js index e099d0ca69..8d7eb63564 100644 --- a/runtime/obj.js +++ b/runtime/obj.js @@ -47,11 +47,11 @@ function caml_obj_is_block(x) { //Requires: caml_is_ml_bytes, caml_is_ml_string function caml_obj_tag(x) { if (Array.isArray(x) && x[0] === x[0] >>> 0) return x[0]; - else if (caml_is_ml_bytes(x)) return 252; - else if (caml_is_ml_string(x)) return 252; - else if (x instanceof Function || typeof x === "function") return 247; - else if (x && x.caml_custom) return 255; - else return 1000; + if (caml_is_ml_bytes(x)) return 252; + if (caml_is_ml_string(x)) return 252; + if (x instanceof Function || typeof x === "function") return 247; + if (x?.caml_custom) return 255; + return 1000; } //Provides: caml_obj_set_tag (mutable, const) @@ -199,9 +199,8 @@ function caml_lazy_update_to_forcing(o) { caml_obj_update_tag(o, 246, 244) ) { return 0; - } else { - return 1; } + return 1; } //Provides: caml_lazy_update_to_forward diff --git a/runtime/parsing.js b/runtime/parsing.js index 281d86a9a4..6f71951825 100644 --- a/runtime/parsing.js +++ b/runtime/parsing.js @@ -131,6 +131,7 @@ function caml_parse_engine(tables, env, cmd, arg) { let errflag = env[env_errflag]; exit: for (;;) { + // biome-ignore lint/suspicious/noConfusingLabels: expected behaviour next: switch (cmd) { case 0: //START: state = 0; @@ -208,15 +209,14 @@ function caml_parse_engine(tables, env, cmd, arg) { if (caml_parser_trace) log(`Recovering in state ${state1}`); cmd = shift_recover; break next; - } else { - if (caml_parser_trace) log(`Discarding state ${state1}`); - if (sp <= env[env_stackbase]) { - if (caml_parser_trace) log("No more states to discard"); - return RAISE_PARSE_ERROR; - } - /* The ML code raises Parse_error */ - sp--; } + if (caml_parser_trace) log(`Discarding state ${state1}`); + if (sp <= env[env_stackbase]) { + if (caml_parser_trace) log("No more states to discard"); + return RAISE_PARSE_ERROR; + } + /* The ML code raises Parse_error */ + sp--; } } else { if (env[env_curr_char] === 0) diff --git a/runtime/stdlib.js b/runtime/stdlib.js index ee856a6430..2f5c7349f9 100644 --- a/runtime/stdlib.js +++ b/runtime/stdlib.js @@ -25,46 +25,44 @@ function caml_call_gen(f, args) { const argsLen = args.length; const d = n - argsLen; if (d === 0) return f.apply(null, args); - else if (d < 0) { + if (d < 0) { const g = f.apply(null, args.slice(0, n)); if (typeof g !== "function") return g; return caml_call_gen(g, args.slice(n)); - } else { - let g; - switch (d) { - case 1: { - g = function (x) { - const nargs = new Array(argsLen + 1); - for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; - nargs[argsLen] = x; - return f.apply(null, nargs); - }; - break; - } - case 2: { - g = function (x, y) { - const nargs = new Array(argsLen + 2); - for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; - nargs[argsLen] = x; - nargs[argsLen + 1] = y; - return f.apply(null, nargs); - }; - break; - } - default: { - g = function () { - const extra_args = arguments.length == 0 ? 1 : arguments.length; - const nargs = new Array(args.length + extra_args); - for (let i = 0; i < args.length; i++) nargs[i] = args[i]; - for (let i = 0; i < arguments.length; i++) - nargs[args.length + i] = arguments[i]; - return caml_call_gen(f, nargs); - }; - } + } + let g; + switch (d) { + case 1: { + g = (x) => { + const nargs = new Array(argsLen + 1); + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; + nargs[argsLen] = x; + return f.apply(null, nargs); + }; + break; + } + case 2: { + g = (x, y) => { + const nargs = new Array(argsLen + 2); + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; + nargs[argsLen] = x; + nargs[argsLen + 1] = y; + return f.apply(null, nargs); + }; + break; + } + default: { + g = (...extra_args) => { + const nargs = new Array(args.length + extra_args.length); + for (let i = 0; i < args.length; i++) nargs[i] = args[i]; + for (let i = 0; i < extra_args.length; i++) + nargs[args.length + i] = extra_args[i]; + return caml_call_gen(f, nargs); + }; } - g.l = d; - return g; } + g.l = d; + return g; } //Provides: caml_call_gen (const, shallow) @@ -76,57 +74,56 @@ function caml_call_gen(f, args) { const d = n - argsLen; if (d === 0) { return f.apply(null, args); - } else if (d < 0) { + } + if (d < 0) { const rest = args.slice(n - 1); const k = args[argsLen - 1]; args = args.slice(0, n); - args[n - 1] = function (g) { + args[n - 1] = (g) => { if (typeof g !== "function") return k(g); const args = rest.slice(); args[args.length - 1] = k; return caml_call_gen(g, args); }; return f.apply(null, args); - } else { - argsLen--; - const k = args[argsLen]; - let g; - switch (d) { - case 1: { - g = function (x, y) { - const nargs = new Array(argsLen + 2); - for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; - nargs[argsLen] = x; - nargs[argsLen + 1] = y; - return f.apply(null, nargs); - }; - break; - } - case 2: { - g = function (x, y, z) { - const nargs = new Array(argsLen + 3); - for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; - nargs[argsLen] = x; - nargs[argsLen + 1] = y; - nargs[argsLen + 2] = z; - return f.apply(null, nargs); - }; - break; - } - default: { - g = function () { - const extra_args = arguments.length == 0 ? 1 : arguments.length; - const nargs = new Array(argsLen + extra_args); - for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; - for (let i = 0; i < arguments.length; i++) - nargs[argsLen + i] = arguments[i]; - return caml_call_gen(f, nargs); - }; - } + } + argsLen--; + const k = args[argsLen]; + let g; + switch (d) { + case 1: { + g = (x, y) => { + const nargs = new Array(argsLen + 2); + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; + nargs[argsLen] = x; + nargs[argsLen + 1] = y; + return f.apply(null, nargs); + }; + break; + } + case 2: { + g = (x, y, z) => { + const nargs = new Array(argsLen + 3); + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; + nargs[argsLen] = x; + nargs[argsLen + 1] = y; + nargs[argsLen + 2] = z; + return f.apply(null, nargs); + }; + break; + } + default: { + g = (...extra_args) => { + const nargs = new Array(argsLen + extra_args.length); + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; + for (let i = 0; i < extra_args.length; i++) + nargs[argsLen + i] = extra_args[i]; + return caml_call_gen(f, nargs); + }; } - g.l = d + 1; - return k(g); } + g.l = d + 1; + return k(g); } //Provides: caml_named_values diff --git a/runtime/stdlib_modern.js b/runtime/stdlib_modern.js index eabb36a714..4a81c75921 100644 --- a/runtime/stdlib_modern.js +++ b/runtime/stdlib_modern.js @@ -23,46 +23,44 @@ function caml_call_gen(f, args) { const argsLen = args.length; const d = n - argsLen; if (d === 0) return f(...args); - else if (d < 0) { + if (d < 0) { const g = f(...args.slice(0, n)); if (typeof g !== "function") return g; return caml_call_gen(g, args.slice(n)); - } else { - let g; - switch (d) { - case 1: { - g = function (x) { - const nargs = new Array(argsLen + 1); - for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; - nargs[argsLen] = x; - return f.apply(null, nargs); - }; - break; - } - case 2: { - g = function (x, y) { - const nargs = new Array(argsLen + 2); - for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; - nargs[argsLen] = x; - nargs[argsLen + 1] = y; - return f.apply(null, nargs); - }; - break; - } - default: { - g = function () { - const extra_args = arguments.length === 0 ? 1 : arguments.length; - const nargs = new Array(args.length + extra_args); - for (let i = 0; i < args.length; i++) nargs[i] = args[i]; - for (let i = 0; i < arguments.length; i++) - nargs[args.length + i] = arguments[i]; - return caml_call_gen(f, nargs); - }; - } + } + let g; + switch (d) { + case 1: { + g = (x) => { + const nargs = new Array(argsLen + 1); + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; + nargs[argsLen] = x; + return f.apply(null, nargs); + }; + break; + } + case 2: { + g = (x, y) => { + const nargs = new Array(argsLen + 2); + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; + nargs[argsLen] = x; + nargs[argsLen + 1] = y; + return f.apply(null, nargs); + }; + break; + } + default: { + g = (...extra_args) => { + const nargs = new Array(args.length + extra_args.length); + for (let i = 0; i < args.length; i++) nargs[i] = args[i]; + for (let i = 0; i < extra_args.length; i++) + nargs[args.length + i] = extra_args[i]; + return caml_call_gen(f, nargs); + }; } - g.l = d; - return g; } + g.l = d; + return g; } //Provides: caml_call_gen (const, shallow) @@ -72,55 +70,53 @@ function caml_call_gen(f, args) { let argsLen = args.length; const d = n - argsLen; if (d === 0) return f(...args); - else if (d < 0) { + if (d < 0) { const rest = args.slice(n - 1); const k = args[argsLen - 1]; args = args.slice(0, n); - args[n - 1] = function (g) { + args[n - 1] = (g) => { if (typeof g !== "function") return k(g); const args = rest.slice(); args[args.length - 1] = k; return caml_call_gen(g, args); }; return f(...args); - } else { - argsLen--; - const k = args[argsLen]; - let g; - switch (d) { - case 1: { - g = function (x, y) { - const nargs = new Array(argsLen + 2); - for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; - nargs[argsLen] = x; - nargs[argsLen + 1] = y; - return f.apply(null, nargs); - }; - break; - } - case 2: { - g = function (x, y, z) { - const nargs = new Array(argsLen + 3); - for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; - nargs[argsLen] = x; - nargs[argsLen + 1] = y; - nargs[argsLen + 2] = z; - return f.apply(null, nargs); - }; - break; - } - default: { - g = function () { - const extra_args = arguments.length === 0 ? 1 : arguments.length; - const nargs = new Array(argsLen + extra_args); - for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; - for (let i = 0; i < arguments.length; i++) - nargs[argsLen + i] = arguments[i]; - return caml_call_gen(f, nargs); - }; - } + } + argsLen--; + const k = args[argsLen]; + let g; + switch (d) { + case 1: { + g = (x, y) => { + const nargs = new Array(argsLen + 2); + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; + nargs[argsLen] = x; + nargs[argsLen + 1] = y; + return f.apply(null, nargs); + }; + break; + } + case 2: { + g = (x, y, z) => { + const nargs = new Array(argsLen + 3); + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; + nargs[argsLen] = x; + nargs[argsLen + 1] = y; + nargs[argsLen + 2] = z; + return f.apply(null, nargs); + }; + break; + } + default: { + g = (...extra_args) => { + const nargs = new Array(argsLen + extra_args.length); + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; + for (let i = 0; i < extra_args.length; i++) + nargs[argsLen + i] = extra_args[i]; + return caml_call_gen(f, nargs); + }; } - g.l = d + 1; - return k(g); } + g.l = d + 1; + return k(g); } diff --git a/runtime/str.js b/runtime/str.js index b8079fa77e..25bdf020e7 100644 --- a/runtime/str.js +++ b/runtime/str.js @@ -121,7 +121,7 @@ const re_match = (() => { const prefix_match = () => { if (partial) return accept(); - else backtrack(); + backtrack(); }; /* Main DFA interpreter loop */ @@ -326,7 +326,7 @@ function re_string_match(re, s, pos) { caml_invalid_argument("Str.string_match"); const res = re_match(re, s, pos, 0); if (res) return res; - else return [0]; + return [0]; } //Provides: re_partial_match @@ -336,7 +336,7 @@ function re_partial_match(re, s, pos) { caml_invalid_argument("Str.partial_match"); const res = re_match(re, s, pos, 1); if (res) return res; - else return [0]; + return [0]; } //Provides: re_replacement_text diff --git a/runtime/sys.js b/runtime/sys.js index d5ce024d3c..64ac593b36 100644 --- a/runtime/sys.js +++ b/runtime/sys.js @@ -28,8 +28,7 @@ function caml_raise_sys_error(msg) { function caml_sys_exit(code) { if (globalThis.quit) globalThis.quit(code); //nodejs - if (globalThis.process && globalThis.process.exit) - globalThis.process.exit(code); + if (globalThis.process?.exit) globalThis.process.exit(code); caml_invalid_argument("Function 'exit' not implemented"); } @@ -71,12 +70,12 @@ function caml_format_exception(exn) { if (typeof v === "number") r += v.toString(); else if (v instanceof MlBytes) { r += `"${v.toString()}"`; - } else if (typeof v == "string") { + } else if (typeof v === "string") { r += `"${v.toString()}"`; } else r += "_"; } r += ")"; - } else if (exn[0] == 248) { + } else if (exn[0] === 248) { r += exn[1]; } return r; @@ -85,7 +84,7 @@ function caml_format_exception(exn) { //Provides: caml_fatal_uncaught_exception //Requires: caml_named_value, caml_format_exception, caml_callback function caml_fatal_uncaught_exception(err) { - if (Array.isArray(err) && (err[0] == 0 || err[0] == 248)) { + if (Array.isArray(err) && (err[0] === 0 || err[0] === 248)) { const handler = caml_named_value("Printexc.handle_uncaught_exception"); if (handler) caml_callback(handler, [err, false]); else { @@ -113,8 +112,7 @@ function jsoo_sys_getenv(n) { //nodejs env if (process && process.env && process.env[n] !== undefined) return process.env[n]; - if (globalThis.jsoo_static_env && globalThis.jsoo_static_env[n]) - return globalThis.jsoo_static_env[n]; + if (globalThis.jsoo_static_env?.[n]) return globalThis.jsoo_static_env[n]; } //Provides: caml_sys_getenv (const) @@ -141,7 +139,7 @@ let caml_argv = (() => { let main = "a.out"; let args = []; - if (process && process.argv && process.argv.length > 1) { + if (process?.argv && process.argv.length > 1) { const argv = process.argv; //nodejs main = argv[1]; @@ -190,7 +188,7 @@ function caml_sys_system_command(cmd) { const cmd_ = caml_jsstring_of_string(cmd); if (typeof require !== "undefined") { const child_process = require("node:child_process"); - if (child_process && child_process.execSync) + if (child_process?.execSync) try { child_process.execSync(cmd_, { stdio: "inherit" }); return 0; @@ -227,7 +225,8 @@ function caml_sys_random_seed() { if (globalThis.crypto.getRandomValues) { const a = globalThis.crypto.getRandomValues(new Int32Array(4)); return [0, a[0], a[1], a[2], a[3]]; - } else if (globalThis.crypto.randomBytes) { + } + if (globalThis.crypto.randomBytes) { const a = new Int32Array(globalThis.crypto.randomBytes(16).buffer); return [0, a[0], a[1], a[2], a[3]]; } @@ -283,9 +282,7 @@ function caml_sys_const_backend_type() { //Provides: os_type const os_type = - globalThis.process && - globalThis.process.platform && - globalThis.process.platform === "win32" + globalThis.process?.platform && globalThis.process.platform === "win32" ? "Win32" : "Unix"; @@ -370,7 +367,7 @@ function caml_sys_is_regular_file(name) { //If: !wasm function caml_setup_uncaught_exception_handler() { const process = globalThis.process; - if (process && process.on) { + if (process?.on) { process.on("uncaughtException", (err, origin) => { caml_fatal_uncaught_exception(err); process.exit(2); diff --git a/runtime/toplevel.js b/runtime/toplevel.js index ac6ffc2acc..452988d454 100644 --- a/runtime/toplevel.js +++ b/runtime/toplevel.js @@ -98,7 +98,8 @@ function caml_reify_bytecode(code, debug, _digest) { if (globalThis.toplevelCompile) { code = caml_string_of_array(caml_ba_to_typed_array(code)); return [0, 0, caml_callback(globalThis.toplevelCompile, [code, debug])]; - } else caml_failwith("Toplevel not initialized (toplevelCompile)"); + } + caml_failwith("Toplevel not initialized (toplevelCompile)"); } //Provides: caml_reify_bytecode @@ -121,7 +122,8 @@ function caml_reify_bytecode(code, debug, _digest) { } code = caml_string_of_array(code); return [0, 0, caml_callback(globalThis.toplevelCompile, [code, debug])]; - } else caml_failwith("Toplevel not initialized (toplevelCompile)"); + } + caml_failwith("Toplevel not initialized (toplevelCompile)"); } //Provides: caml_static_release_bytecode diff --git a/runtime/unix.js b/runtime/unix.js index 18e8a9a666..a513c9b951 100644 --- a/runtime/unix.js +++ b/runtime/unix.js @@ -90,9 +90,8 @@ function caml_unix_isatty(fileDescriptor) { if (fs_node_supported()) { const tty = require("node:tty"); return tty.isatty(fileDescriptor) ? 1 : 0; - } else { - return 0; } + return 0; } //Provides: caml_unix_isatty @@ -303,7 +302,7 @@ function caml_unix_unlink(name) { //Requires: caml_raise_not_found //Alias: unix_getuid function caml_unix_getuid(unit) { - if (globalThis.process && globalThis.process.getuid) { + if (globalThis.process?.getuid) { return globalThis.process.getuid(); } caml_raise_not_found(); diff --git a/runtime/weak.js b/runtime/weak.js index aa0e00d572..8320ca7734 100644 --- a/runtime/weak.js +++ b/runtime/weak.js @@ -121,7 +121,7 @@ function caml_ephe_check_key(x, i) { if (globalThis.WeakRef && weak instanceof globalThis.WeakRef) weak = weak.deref(); if (weak === undefined) return 0; - else return 1; + return 1; } //Provides: caml_ephe_blit_key @@ -153,7 +153,7 @@ function caml_ephe_blit_data(src, dst) { //Requires: caml_ephe_data_offset function caml_ephe_get_data(x) { if (x[caml_ephe_data_offset] === undefined) return 0; - else return [0, x[caml_ephe_data_offset]]; + return [0, x[caml_ephe_data_offset]]; } //Provides: caml_ephe_get_data_copy @@ -161,7 +161,7 @@ function caml_ephe_get_data(x) { //Requires: caml_obj_dup function caml_ephe_get_data_copy(x) { if (x[caml_ephe_data_offset] === undefined) return 0; - else return [0, caml_obj_dup(x[caml_ephe_data_offset])]; + return [0, caml_obj_dup(x[caml_ephe_data_offset])]; } //Provides: caml_ephe_set_data @@ -209,5 +209,5 @@ function caml_ephe_unset_data(x) { //Requires: caml_ephe_data_offset function caml_ephe_check_data(x) { if (x[caml_ephe_data_offset] === undefined) return 0; - else return 1; + return 1; } diff --git a/runtime/zstd.js b/runtime/zstd.js index 31dbfae162..eb5793b5d7 100644 --- a/runtime/zstd.js +++ b/runtime/zstd.js @@ -1,6 +1,6 @@ //Provides: zstd_decompress //Version: >= 5.1 -const zstd_decompress = (function () { +const zstd_decompress = (() => { // aliases for shorter compressed code (most minifers don't do this) const ab = ArrayBuffer; const u8 = Uint8Array; @@ -8,7 +8,7 @@ const zstd_decompress = (function () { const i16 = Int16Array; const u32 = Uint32Array; const i32 = Int32Array; - const slc = function (v, s, e) { + const slc = (v, s, e) => { if (u8.prototype.slice) return u8.prototype.slice.call(v, s, e); if (s == null || s < 0) s = 0; if (e == null || e > v.length) e = v.length; @@ -16,14 +16,14 @@ const zstd_decompress = (function () { n.set(v.subarray(s, e)); return n; }; - const fill = function (v, n, s, e) { + const fill = (v, n, s, e) => { if (u8.prototype.fill) return u8.prototype.fill.call(v, n, s, e); if (s == null || s < 0) s = 0; if (e == null || e > v.length) e = v.length; for (; s < e; ++s) v[s] = n; return v; }; - const cpw = function (v, t, s, e) { + const cpw = (v, t, s, e) => { if (u8.prototype.copyWithin) return u8.prototype.copyWithin.call(v, t, s, e); if (s == null || s < 0) s = 0; @@ -44,23 +44,23 @@ const zstd_decompress = (function () { "match distance too far back", "unexpected EOF", ]; - const err = function (ind, msg, nt) { + const err = (ind, msg, nt) => { const e = new Error(msg || ec[ind]); e.code = ind; if (!nt) throw e; return e; }; - const rb = function (d, b, n) { + const rb = (d, b, n) => { let i = 0; let o = 0; for (; i < n; ++i) o |= d[b++] << (i << 3); return o; }; - const b4 = function (d, b) { + const b4 = (d, b) => { return (d[b] | (d[b + 1] << 8) | (d[b + 2] << 16) | (d[b + 3] << 24)) >>> 0; }; // read Zstandard frame header - const rzfh = function (dat, w) { + const rzfh = (dat, w) => { const n3 = dat[0] | (dat[1] << 8) | (dat[2] << 16); if (n3 === 0x2fb528 && dat[3] === 253) { // Zstandard @@ -106,20 +106,21 @@ const zstd_decompress = (function () { c: cc, m: Math.min(131072, ws), }; - } else if (((n3 >> 4) | (dat[3] << 20)) === 0x184d2a5) { + } + if (((n3 >> 4) | (dat[3] << 20)) === 0x184d2a5) { // skippable return b4(dat, 4) + 8; } err(0); }; // most significant bit for nonzero - const msb = function (val) { + const msb = (val) => { let bits = 0; for (; 1 << bits <= val; ++bits); return bits - 1; }; // read finite state entropy - const rfse = function (dat, bt, mal) { + const rfse = (dat, bt, mal) => { // table pos let tpos = (bt << 3) + 4; // accuracy log @@ -219,7 +220,7 @@ const zstd_decompress = (function () { ]; }; // read huffman - const rhu = function (dat, bt) { + const rhu = (dat, bt) => { // index weight count let i = 0; let wc = -1; @@ -354,7 +355,7 @@ const zstd_decompress = (function () { 5, )[1]; // bits to baseline - const b2bl = function (b, s) { + const b2bl = (b, s) => { const len = b.length; const bl = new i32(len); for (let i = 0; i < len; ++i) { @@ -385,7 +386,7 @@ const zstd_decompress = (function () { // match length baseline const mlbl = /*#__PURE__ */ b2bl(mlb, 3); // decode huffman stream - const dhu = function (dat, out, hu) { + const dhu = (dat, out, hu) => { const len = dat.length; const ss = out.length; const lb = dat[len - 1]; @@ -408,7 +409,7 @@ const zstd_decompress = (function () { }; // decode huffman stream 4x // TODO: use workers to parallelize - const dhu4 = function (dat, out, hu) { + const dhu4 = (dat, out, hu) => { let bt = 6; const ss = out.length; const sz1 = (ss + 3) >> 2; @@ -432,7 +433,7 @@ const zstd_decompress = (function () { dhu(dat.subarray(bt), out.subarray(sz3), hu); }; // read Zstandard block - const rzb = function (dat, st, out) { + const rzb = (dat, st, out) => { let _a; let bt = st.b; // byte 0 block type @@ -526,7 +527,9 @@ const zstd_decompress = (function () { }; } else if (md === 2) { // accuracy log 8 for offsets, 9 for others - (_a = rfse(dat, bt, 9 - (i & 1))), (bt = _a[0]), (dts[i] = _a[1]); + _a = rfse(dat, bt, 9 - (i & 1)); + bt = _a[0]; + dts[i] = _a[1]; } else if (md === 3) { if (!st.t) err(0); dts[i] = st.t[i]; @@ -598,9 +601,9 @@ const zstd_decompress = (function () { st.o[1] = st.o[0]; st.o[0] = off -= 3; } else { - const idx = off - (ll != 0); + const idx = off - (ll !== 0); if (idx) { - off = idx == 3 ? st.o[0] - 1 : st.o[idx]; + off = idx === 3 ? st.o[0] - 1 : st.o[idx]; if (idx > 1) st.o[2] = st.o[1]; st.o[1] = st.o[0]; st.o[0] = off; @@ -651,7 +654,7 @@ const zstd_decompress = (function () { err(2); }; // concat - const cct = function (bufs, ol) { + const cct = (bufs, ol) => { if (bufs.length === 1) return bufs[0]; const buf = new u8(ol); for (let i = 0, b = 0; i < bufs.length; ++i) {