From b7225c1092cf8266186c17f4eb5dfe14e21b14d3 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Sat, 14 Sep 2024 10:47:24 +0900 Subject: [PATCH 01/12] runtime: wrap declarations in a block for switch clause Signed-off-by: Sora Morimoto --- runtime/bigarray.js | 39 +++++++++++++++++-------- runtime/compare.js | 20 +++++++++---- runtime/hash.js | 3 +- runtime/ieee_754.js | 6 ++-- runtime/internalMod.js | 8 ++++-- runtime/io.js | 4 ++- runtime/marshal.js | 64 +++++++++++++++++++++++++++++++----------- runtime/parsing.js | 8 ++++-- 8 files changed, 111 insertions(+), 41 deletions(-) diff --git a/runtime/bigarray.js b/runtime/bigarray.js index 9aa226c324..f997b945aa 100644 --- a/runtime/bigarray.js +++ b/runtime/bigarray.js @@ -149,17 +149,19 @@ Ml_Bigarray.prototype.offset = function (arg) { Ml_Bigarray.prototype.get = function (ofs) { switch (this.kind) { - case 7: + case 7: { // Int64 var l = this.data[ofs * 2 + 0]; var h = this.data[ofs * 2 + 1]; return caml_int64_create_lo_hi(l, h); + } case 10: - case 11: + case 11: { // Complex32, Complex64 var r = this.data[ofs * 2 + 0]; var i = this.data[ofs * 2 + 1]; return [254, r, i]; + } default: return this.data[ofs]; } @@ -187,7 +189,7 @@ Ml_Bigarray.prototype.set = function (ofs, v) { Ml_Bigarray.prototype.fill = function (v) { switch (this.kind) { - case 7: + case 7: { // Int64 var a = caml_int64_lo32(v); var b = caml_int64_hi32(v); @@ -199,8 +201,9 @@ Ml_Bigarray.prototype.fill = function (v) { } } break; + } case 10: - case 11: + case 11: { // Complex32, Complex64 var im = v[1]; var re = v[2]; @@ -212,6 +215,7 @@ Ml_Bigarray.prototype.fill = function (v) { } } break; + } default: this.data.fill(v); break; @@ -233,7 +237,7 @@ Ml_Bigarray.prototype.compare = function (b, total) { case 0: case 1: case 10: - case 11: + case 11: { // Floats var x, y; for (var i = 0; i < this.data.length; i++) { @@ -248,6 +252,7 @@ Ml_Bigarray.prototype.compare = function (b, total) { } } break; + } case 7: // Int64 for (var i = 0; i < this.data.length; i += 2) { @@ -749,7 +754,8 @@ function caml_ba_deserialize(reader, sz, name) { } break; case 8: // Int32Array (int) - case 9: // Int32Array (nativeint) + case 9: { + // Int32Array (nativeint) var sixty = reader.read8u(); if (sixty) caml_failwith( @@ -759,7 +765,9 @@ function caml_ba_deserialize(reader, sz, name) { data[i] = reader.read32s(); } break; - case 7: // (int64) + } + case 7: { + // (int64) var t = new Array(8); for (var i = 0; i < size; i++) { for (var j = 0; j < 8; j++) t[j] = reader.read8u(); @@ -767,7 +775,9 @@ function caml_ba_deserialize(reader, sz, name) { ba.set(i, int64); } break; - case 1: // Float64Array + } + case 1: { + // Float64Array var t = new Array(8); for (var i = 0; i < size; i++) { for (var j = 0; j < 8; j++) t[j] = reader.read8u(); @@ -775,6 +785,7 @@ function caml_ba_deserialize(reader, sz, name) { ba.set(i, f); } break; + } case 0: // Float32Array for (var i = 0; i < size; i++) { var f = caml_int32_float_of_bits(reader.read32s()); @@ -788,7 +799,8 @@ function caml_ba_deserialize(reader, sz, name) { ba.set(i, [254, re, im]); } break; - case 11: // Float64Array (complex64) + case 11: { + // Float64Array (complex64) var t = new Array(8); for (var i = 0; i < size; i++) { for (var j = 0; j < 8; j++) t[j] = reader.read8u(); @@ -798,6 +810,7 @@ function caml_ba_deserialize(reader, sz, name) { ba.set(i, [254, re, im]); } break; + } } sz[0] = (4 + num_dims) * 4; return caml_ba_create_unsafe(kind, layout, dims, data); @@ -823,7 +836,8 @@ function caml_ba_hash(ba) { switch (ba.kind) { case 2: //Int8Array case 3: //Uint8Array - case 12: //Uint8Array + case 12: { + //Uint8Array if (num_elts > 256) num_elts = 256; var w = 0, i = 0; @@ -846,8 +860,10 @@ function caml_ba_hash(ba) { h = caml_hash_mix_int(h, w); } break; + } case 4: // Int16Array - case 5: // Uint16Array + case 5: { + // Uint16Array if (num_elts > 128) num_elts = 128; var w = 0, i = 0; @@ -857,6 +873,7 @@ function caml_ba_hash(ba) { } if ((num_elts & 1) != 0) h = caml_hash_mix_int(h, ba.data[i]); break; + } case 6: // Int32Array (int32) if (num_elts > 64) num_elts = 64; for (var i = 0; i < num_elts; i++) h = caml_hash_mix_int(h, ba.data[i]); diff --git a/runtime/compare.js b/runtime/compare.js index 3f95d0f814..01964718eb 100644 --- a/runtime/compare.js +++ b/runtime/compare.js @@ -115,10 +115,12 @@ function caml_compare_val(a, b, total) { // Cannot happen caml_invalid_argument("compare: functional value"); break; - case 248: // Object + case 248: { + // Object var x = caml_int_compare(a[2], b[2]); if (x != 0) return x | 0; break; + } case 249: // Infix // Cannot happen caml_invalid_argument("compare: functional value"); @@ -152,7 +154,8 @@ function caml_compare_val(a, b, total) { case 1247: // Function caml_invalid_argument("compare: functional value"); break; - case 1255: // Custom + case 1255: { + // Custom var comp = caml_compare_val_get_custom(a); if (comp != caml_compare_val_get_custom(b)) { return a.caml_custom < b.caml_custom ? -1 : 1; @@ -169,7 +172,9 @@ function caml_compare_val(a, b, total) { } if (x != 0) return x | 0; break; - case 1256: // compare function + } + case 1256: { + // compare function var x = a.compare(b, total); if (x != x) { // Protect against invalid UNORDERED @@ -181,6 +186,7 @@ function caml_compare_val(a, b, total) { } if (x != 0) return x | 0; break; + } case 1000: // Number a = +a; b = +b; @@ -220,7 +226,8 @@ function caml_compare_val(a, b, total) { return 1; } break; - case 1252: // ocaml strings + case 1252: { + // ocaml strings var a = caml_jsbytes_of_string(a); var b = caml_jsbytes_of_string(b); if (a !== b) { @@ -228,7 +235,9 @@ function caml_compare_val(a, b, total) { if (a > b) return 1; } break; - case 12520: // javascript strings + } + case 12520: { + // javascript strings var a = a.toString(); var b = b.toString(); if (a !== b) { @@ -236,6 +245,7 @@ function caml_compare_val(a, b, total) { if (a > b) return 1; } break; + } case 246: // Lazy_tag case 254: // Double_array default: // Block with other tag diff --git a/runtime/hash.js b/runtime/hash.js index e0f6f7e7ce..b03278365b 100644 --- a/runtime/hash.js +++ b/runtime/hash.js @@ -230,7 +230,7 @@ function caml_hash(count, limit, seed, obj) { // Forward queue[--rd] = v[1]; break; - default: + default: { if (caml_is_continuation_tag(v[0])) { /* All continuations hash to the same value, since we have no idea how to distinguish them. */ @@ -243,6 +243,7 @@ function caml_hash(count, limit, seed, obj) { queue[wr++] = v[i]; } break; + } } } else if (caml_is_ml_bytes(v)) { h = caml_hash_mix_bytes(h, v); diff --git a/runtime/ieee_754.js b/runtime/ieee_754.js index e708b27719..a43215f889 100644 --- a/runtime/ieee_754.js +++ b/runtime/ieee_754.js @@ -517,17 +517,18 @@ function caml_format_float(fmt, x) { f.filler = " "; } else switch (f.conv) { - case "e": + case "e": { var s = x.toExponential(prec); // exponent should be at least two digits var i = s.length; if (s.charAt(i - 3) == "e") s = s.slice(0, i - 1) + "0" + s.slice(i - 1); break; + } case "f": s = toFixed(x, prec); break; - case "g": + case "g": { prec = prec ? prec : 1; s = x.toExponential(prec - 1); var j = s.indexOf("e"); @@ -557,6 +558,7 @@ function caml_format_float(fmt, x) { } } break; + } } return caml_finish_formatting(f, s); } diff --git a/runtime/internalMod.js b/runtime/internalMod.js index 1722b728cc..4c19cf7691 100644 --- a/runtime/internalMod.js +++ b/runtime/internalMod.js @@ -28,11 +28,13 @@ function caml_CamlinternalMod_init_mod(loc, shape) { function loop(shape, struct, idx) { if (typeof shape === "number") switch (shape) { - case 0: //function + case 0: { + //function var dummy = caml_alloc_dummy_infix(); dummy.fun = undef_module; struct[idx] = dummy; break; + } case 1: //lazy struct[idx] = [246, undef_module]; break; @@ -90,11 +92,13 @@ function caml_CamlinternalMod_init_mod(loc, shape, cont) { function loop(shape, struct, idx) { if (typeof shape === "number") switch (shape) { - case 0: //function + case 0: { + //function var dummy = caml_alloc_dummy_infix(); dummy.fun = undef_module; struct[idx] = dummy; break; + } case 1: //lazy struct[idx] = [246, undef_module]; break; diff --git a/runtime/io.js b/runtime/io.js index 2812b1bb96..f1792c70f8 100644 --- a/runtime/io.js +++ b/runtime/io.js @@ -557,7 +557,8 @@ function caml_ml_output_ta(chanid, buffer, offset, len) { chan.buffer_curr += buffer.length; if (chan.buffer_curr >= chan.buffer.length) caml_ml_flush(chanid); break; - case 2: // Buffered (only for stdout and stderr) + case 2: { + // Buffered (only for stdout and stderr) var id = buffer.lastIndexOf(10); if (id < 0) { chan.buffer.set(buffer, chan.buffer_curr); @@ -571,6 +572,7 @@ function caml_ml_output_ta(chanid, buffer, offset, len) { chan.buffer_curr += buffer.length - id - 1; } break; + } } return 0; } diff --git a/runtime/marshal.js b/runtime/marshal.js index f6eca871cc..14b5b1bf1f 100644 --- a/runtime/marshal.js +++ b/runtime/marshal.js @@ -343,7 +343,7 @@ function caml_input_value_from_reader(reader, ofs) { } var magic = reader.read32u(); switch (magic) { - case 0x8495a6be /* Intext_magic_number_small */: + case 0x8495a6be /* Intext_magic_number_small */: { var header_len = 20; var compressed = 0; var data_len = reader.read32u(); @@ -352,7 +352,8 @@ function caml_input_value_from_reader(reader, ofs) { var _size_32 = reader.read32u(); var _size_64 = reader.read32u(); break; - case 0x8495a6bd /* Intext_magic_number_compressed */: + } + case 0x8495a6bd /* Intext_magic_number_compressed */: { var header_len = reader.read8u() & 0x3f; var compressed = 1; var overflow = [false]; @@ -367,6 +368,7 @@ function caml_input_value_from_reader(reader, ofs) { ); } break; + } case 0x8495a6bf /* Intext_magic_number_big */: caml_failwith( "caml_input_value_from_reader: object too large to be read back on a 32-bit platform", @@ -408,19 +410,26 @@ function caml_input_value_from_reader(reader, ofs) { case 0x03: //cst.CODE_INT64: caml_failwith("input_value: integer too large"); break; - case 0x04: //cst.CODE_SHARED8: + case 0x04: { + //cst.CODE_SHARED8: var offset = reader.read8u(); if (compressed == 0) offset = obj_counter - offset; return intern_obj_table[offset]; - case 0x05: //cst.CODE_SHARED16: + } + case 0x05: { + //cst.CODE_SHARED16: var offset = reader.read16u(); if (compressed == 0) offset = obj_counter - offset; return intern_obj_table[offset]; - case 0x06: //cst.CODE_SHARED32: + } + case 0x06: { + //cst.CODE_SHARED32: var offset = reader.read32u(); if (compressed == 0) offset = obj_counter - offset; return intern_obj_table[offset]; - case 0x08: //cst.CODE_BLOCK32: + } + case 0x08: { + //cst.CODE_BLOCK32: var header = reader.read32u(); var tag = header & 0xff; var size = header >> 10; @@ -429,32 +438,42 @@ function caml_input_value_from_reader(reader, ofs) { 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: + case 0x09: { + //cst.CODE_STRING8: var len = reader.read8u(); var v = reader.readstr(len); if (intern_obj_table) intern_obj_table[obj_counter++] = v; return v; - case 0x0a: //cst.CODE_STRING32: + } + case 0x0a: { + //cst.CODE_STRING32: var len = reader.read32u(); var v = reader.readstr(len); if (intern_obj_table) intern_obj_table[obj_counter++] = v; return v; - case 0x0c: //cst.CODE_DOUBLE_LITTLE: + } + case 0x0c: { + //cst.CODE_DOUBLE_LITTLE: var t = new Array(8); for (var i = 0; i < 8; i++) t[7 - i] = reader.read8u(); var v = caml_float_of_bytes(t); if (intern_obj_table) intern_obj_table[obj_counter++] = v; return v; - case 0x0b: //cst.CODE_DOUBLE_BIG: + } + case 0x0b: { + //cst.CODE_DOUBLE_BIG: var t = new Array(8); for (var i = 0; i < 8; i++) t[i] = reader.read8u(); var 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: + } + case 0x0e: { + //cst.CODE_DOUBLE_ARRAY8_LITTLE: var len = reader.read8u(); var v = new Array(len + 1); v[0] = 254; @@ -465,7 +484,9 @@ function caml_input_value_from_reader(reader, ofs) { v[i] = caml_float_of_bytes(t); } return v; - case 0x0d: //cst.CODE_DOUBLE_ARRAY8_BIG: + } + case 0x0d: { + //cst.CODE_DOUBLE_ARRAY8_BIG: var len = reader.read8u(); var v = new Array(len + 1); v[0] = 254; @@ -476,7 +497,9 @@ function caml_input_value_from_reader(reader, ofs) { v[i] = caml_float_of_bytes(t); } return v; - case 0x07: //cst.CODE_DOUBLE_ARRAY32_LITTLE: + } + case 0x07: { + //cst.CODE_DOUBLE_ARRAY32_LITTLE: var len = reader.read32u(); var v = new Array(len + 1); v[0] = 254; @@ -487,7 +510,9 @@ function caml_input_value_from_reader(reader, ofs) { v[i] = caml_float_of_bytes(t); } return v; - case 0x0f: //cst.CODE_DOUBLE_ARRAY32_BIG: + } + case 0x0f: { + //cst.CODE_DOUBLE_ARRAY32_BIG: var len = reader.read32u(); var v = new Array(len + 1); v[0] = 254; @@ -497,13 +522,15 @@ function caml_input_value_from_reader(reader, ofs) { 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: + case 0x19: { + //cst.CODE_CUSTOM_FIXED: var c, s = ""; while ((c = reader.read8u()) != 0) s += String.fromCharCode(c); @@ -539,6 +566,7 @@ function caml_input_value_from_reader(reader, ofs) { } if (intern_obj_table) intern_obj_table[obj_counter++] = v; return v; + } default: caml_failwith("input_value: ill-formed message"); } @@ -594,11 +622,12 @@ function caml_marshal_data_size(s, ofs) { } switch (r.read32u()) { - case 0x8495a6be /* Intext_magic_number_small */: + case 0x8495a6be /* Intext_magic_number_small */: { var header_len = 20; var data_len = r.read32u(); break; - case 0x8495a6bd /* Intext_magic_number_compressed */: + } + case 0x8495a6bd /* Intext_magic_number_compressed */: { var header_len = r.read8u() & 0x3f; var overflow = [false]; var data_len = readvlq(overflow); @@ -608,6 +637,7 @@ function caml_marshal_data_size(s, ofs) { ); } break; + } case 0x8495a6bf: /* Intext_magic_number_big */ default: caml_failwith("Marshal.data_size: bad object"); diff --git a/runtime/parsing.js b/runtime/parsing.js index 74742a7be3..89cf81c96f 100644 --- a/runtime/parsing.js +++ b/runtime/parsing.js @@ -249,7 +249,8 @@ function caml_parse_engine(tables, env, cmd, arg) { cmd = loop; break; - case 10: //reduce: + case 10: { + //reduce: if (caml_parser_trace) log("State " + state + ": reduce by rule " + n); var m = tables.len[n]; env[env_asp] = sp; @@ -272,13 +273,15 @@ function caml_parse_engine(tables, env, cmd, arg) { res = GROW_STACKS_2; break exit; } + } // Fall through /* The ML code resizes the stacks */ case 3: //STACKS_GROWN_2: res = COMPUTE_SEMANTIC_ACTION; break exit; /* The ML code calls the semantic action */ - case 4: //SEMANTIC_ACTION_COMPUTED: + case 4: { + //SEMANTIC_ACTION_COMPUTED: env[env_s_stack][sp + 1] = state; env[env_v_stack][sp + 1] = arg; var asp = env[env_asp]; @@ -289,6 +292,7 @@ function caml_parse_engine(tables, env, cmd, arg) { } cmd = loop; break; + } /* Should not happen */ default: return RAISE_PARSE_ERROR; From 4ee34ac0151fb1a0fb3c4b84f6cfff3cfa82fef8 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Sat, 14 Sep 2024 11:36:49 +0900 Subject: [PATCH 02/12] runtime: fix no-inner-declarations Signed-off-by: Sora Morimoto --- runtime/array.js | 20 +++---- runtime/backtrace.js | 2 +- runtime/bigarray.js | 113 ++++++++++++++++++----------------- runtime/bigstring.js | 2 +- runtime/dynlink.js | 2 +- runtime/format.js | 8 +-- runtime/fs.js | 12 ++-- runtime/fs_fake.js | 6 +- runtime/fs_node.js | 2 +- runtime/graphics.js | 14 ++--- runtime/hash.js | 12 ++-- runtime/internalMod.js | 8 +-- runtime/io.js | 4 +- runtime/jslib.js | 22 +++---- runtime/jslib_js_of_ocaml.js | 2 +- runtime/lexing.js | 2 +- runtime/marshal.js | 48 +++++++-------- runtime/md5.js | 10 ++-- runtime/mlBytes.js | 29 +++++---- runtime/nat.js | 38 ++++++------ runtime/obj.js | 8 +-- runtime/stdlib.js | 18 +++--- runtime/stdlib_modern.js | 16 ++--- runtime/str.js | 6 +- runtime/sys.js | 4 +- runtime/toplevel.js | 4 +- runtime/weak.js | 6 +- runtime/zstd.js | 16 ++--- 28 files changed, 221 insertions(+), 213 deletions(-) diff --git a/runtime/array.js b/runtime/array.js index 95f44e78b2..c23114c412 100644 --- a/runtime/array.js +++ b/runtime/array.js @@ -21,7 +21,7 @@ function caml_array_sub(a, i, len) { var a2 = new Array(len + 1); a2[0] = 0; - for (var i2 = 1, i1 = i + 1; i2 <= len; i2++, i1++) { + for (let i2 = 1, i1 = i + 1; i2 <= len; i2++, i1++) { a2[i2] = a[i1]; } return a2; @@ -46,7 +46,7 @@ function caml_array_concat(l) { var a = [0]; while (l !== 0) { var b = l[1]; - for (var i = 1; i < b.length; i++) a.push(b[i]); + for (let i = 1; i < b.length; i++) a.push(b[i]); l = l[2]; } return a; @@ -55,9 +55,9 @@ function caml_array_concat(l) { //Provides: caml_array_blit function caml_array_blit(a1, i1, a2, i2, len) { if (i2 <= i1) { - for (var j = 1; j <= len; j++) a2[i2 + j] = a1[i1 + j]; + for (let j = 1; j <= len; j++) a2[i2 + j] = a1[i1 + j]; } else { - for (var j = len; j >= 1; j--) a2[i2 + j] = a1[i1 + j]; + for (let j = len; j >= 1; j--) a2[i2 + j] = a1[i1 + j]; } return 0; } @@ -65,9 +65,9 @@ function caml_array_blit(a1, i1, a2, i2, len) { //Provides: caml_floatarray_blit function caml_floatarray_blit(a1, i1, a2, i2, len) { if (i2 <= i1) { - for (var j = 1; j <= len; j++) a2[i2 + j] = a1[i1 + j]; + for (let j = 1; j <= len; j++) a2[i2 + j] = a1[i1 + j]; } else { - for (var j = len; j >= 1; j--) a2[i2 + j] = a1[i1 + j]; + for (let j = len; j >= 1; j--) a2[i2 + j] = a1[i1 + j]; } return 0; } @@ -90,7 +90,7 @@ function caml_array_get(array, index) { //Provides: caml_array_fill function caml_array_fill(array, ofs, len, v) { - for (var i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { array[ofs + i + 1] = v; } return 0; @@ -110,7 +110,7 @@ function caml_make_vect(len, init) { var len = (len + 1) | 0; var b = new Array(len); b[0] = 0; - for (var i = 1; i < len; i++) b[i] = init; + for (let i = 1; i < len; i++) b[i] = init; return b; } @@ -121,7 +121,7 @@ function caml_make_float_vect(len) { var len = (len + 1) | 0; var b = new Array(len); b[0] = 254; - for (var i = 1; i < len; i++) b[i] = 0; + for (let i = 1; i < len; i++) b[i] = 0; return b; } //Provides: caml_floatarray_create const (const) @@ -131,6 +131,6 @@ function caml_floatarray_create(len) { var len = (len + 1) | 0; var b = new Array(len); b[0] = 254; - for (var i = 1; i < len; i++) b[i] = 0; + for (let i = 1; i < len; i++) b[i] = 0; return b; } diff --git a/runtime/backtrace.js b/runtime/backtrace.js index af63260989..1a90355f7e 100644 --- a/runtime/backtrace.js +++ b/runtime/backtrace.js @@ -23,7 +23,7 @@ var caml_record_backtrace_env_flag = FLAG("with-js-error"); var r = jsoo_sys_getenv("OCAMLRUNPARAM"); if (r !== undefined) { var l = r.split(","); - for (var i = 0; i < l.length; i++) { + for (let i = 0; i < l.length; i++) { if (l[i] == "b") { caml_record_backtrace_env_flag = 1; break; diff --git a/runtime/bigarray.js b/runtime/bigarray.js index f997b945aa..904d4a34d9 100644 --- a/runtime/bigarray.js +++ b/runtime/bigarray.js @@ -34,7 +34,7 @@ function caml_ba_init() { function caml_ba_get_size(dims) { var n_dims = dims.length; var size = 1; - for (var i = 0; i < n_dims; i++) { + for (let i = 0; i < n_dims; i++) { if (dims[i] < 0) caml_invalid_argument("Bigarray.create: negative dimension"); size = size * dims[i]; @@ -132,12 +132,12 @@ Ml_Bigarray.prototype.offset = function (arg) { if (this.dims.length != arg.length) caml_invalid_argument("Bigarray.get/set: bad number of dimensions"); if (this.layout == 0 /* c_layout */) { - for (var i = 0; i < this.dims.length; i++) { + for (let i = 0; i < this.dims.length; i++) { if (arg[i] < 0 || arg[i] >= this.dims[i]) caml_array_bound_error(); ofs = ofs * this.dims[i] + arg[i]; } } else { - for (var i = this.dims.length - 1; i >= 0; i--) { + for (let i = this.dims.length - 1; i >= 0; i--) { if (arg[i] < 1 || arg[i] > this.dims[i]) { caml_array_bound_error(); } @@ -196,7 +196,7 @@ Ml_Bigarray.prototype.fill = function (v) { if (a == b) { this.data.fill(a); } else { - for (var i = 0; i < this.data.length; i++) { + for (let i = 0; i < this.data.length; i++) { this.data[i] = i % 2 == 0 ? a : b; } } @@ -210,7 +210,7 @@ Ml_Bigarray.prototype.fill = function (v) { if (im == re) { this.data.fill(im); } else { - for (var i = 0; i < this.data.length; i++) { + for (let i = 0; i < this.data.length; i++) { this.data[i] = i % 2 == 0 ? im : re; } } @@ -231,7 +231,7 @@ Ml_Bigarray.prototype.compare = function (b, total) { if (this.dims.length != b.dims.length) { return b.dims.length - this.dims.length; } - for (var i = 0; i < this.dims.length; i++) + for (let i = 0; i < this.dims.length; i++) if (this.dims[i] != b.dims[i]) return this.dims[i] < b.dims[i] ? -1 : 1; switch (this.kind) { case 0: @@ -240,7 +240,7 @@ Ml_Bigarray.prototype.compare = function (b, total) { case 11: { // Floats var x, y; - for (var i = 0; i < this.data.length; i++) { + for (let i = 0; i < this.data.length; i++) { x = this.data[i]; y = b.data[i]; if (x < y) return -1; @@ -255,7 +255,7 @@ Ml_Bigarray.prototype.compare = function (b, total) { } case 7: // Int64 - for (var i = 0; i < this.data.length; i += 2) { + for (let i = 0; i < this.data.length; i += 2) { // Check highest bits first if (this.data[i + 1] < b.data[i + 1]) return -1; if (this.data[i + 1] > b.data[i + 1]) return 1; @@ -271,7 +271,7 @@ Ml_Bigarray.prototype.compare = function (b, total) { case 8: case 9: case 12: - for (var i = 0; i < this.data.length; i++) { + for (let i = 0; i < this.data.length; i++) { if (this.data[i] < b.data[i]) return -1; if (this.data[i] > b.data[i]) return 1; } @@ -351,7 +351,7 @@ function caml_ba_create(kind, layout, dims_ml) { function caml_ba_change_layout(ba, layout) { if (ba.layout == layout) return ba; var new_dims = []; - for (var i = 0; i < ba.dims.length; i++) + for (let i = 0; i < ba.dims.length; i++) new_dims[i] = ba.dims[ba.dims.length - i - 1]; return caml_ba_create_unsafe(ba.kind, layout, new_dims, ba.data); } @@ -491,7 +491,7 @@ function caml_ba_uint8_set64(ba, i0, v) { var ofs = ba.offset(i0); if (ofs + 7 >= ba.data.length) caml_array_bound_error(); var v = caml_int64_to_bytes(v); - for (var i = 0; i < 8; i++) ba.set(ofs + i, v[7 - i]); + for (let i = 0; i < 8; i++) ba.set(ofs + i, v[7 - i]); return 0; } @@ -524,7 +524,7 @@ function caml_ba_fill(ba, v) { function caml_ba_blit(src, dst) { if (dst.dims.length != src.dims.length) caml_invalid_argument("Bigarray.blit: dimension mismatch"); - for (var i = 0; i < dst.dims.length; i++) + for (let i = 0; i < dst.dims.length; i++) if (dst.dims[i] != src.dims[i]) caml_invalid_argument("Bigarray.blit: dimension mismatch"); dst.data.set(src.data); @@ -538,10 +538,10 @@ function caml_ba_sub(ba, ofs, len) { var changed_dim; var mul = 1; if (ba.layout == 0) { - for (var i = 1; i < ba.dims.length; i++) mul = mul * ba.dims[i]; + for (let i = 1; i < ba.dims.length; i++) mul = mul * ba.dims[i]; changed_dim = 0; } else { - for (var i = 0; i < ba.dims.length - 1; i++) mul = mul * ba.dims[i]; + for (let i = 0; i < ba.dims.length - 1; i++) mul = mul * ba.dims[i]; changed_dim = ba.dims.length - 1; ofs = ofs - 1; } @@ -549,7 +549,7 @@ function caml_ba_sub(ba, ofs, len) { caml_invalid_argument("Bigarray.sub: bad sub-array"); } var new_dims = []; - for (var i = 0; i < ba.dims.length; i++) new_dims[i] = ba.dims[i]; + for (let i = 0; i < ba.dims.length; i++) new_dims[i] = ba.dims[i]; new_dims[changed_dim] = len; mul *= caml_ba_get_size_per_element(ba.kind); var new_data = ba.data.subarray(ofs * mul, (ofs + len) * mul); @@ -571,13 +571,14 @@ function caml_ba_slice(ba, vind) { // Compute offset and check bounds if (ba.layout == 0) { - for (var i = 0; i < num_inds; i++) index[i] = vind[i]; + let i = 0; + for (; i < num_inds; i++) index[i] = vind[i]; for (; i < ba.dims.length; i++) index[i] = 0; sub_dims = ba.dims.slice(num_inds); } else { - for (var i = 0; i < num_inds; i++) + for (let i = 0; i < num_inds; i++) index[ba.dims.length - num_inds + i] = vind[i]; - for (var i = 0; i < ba.dims.length - num_inds; i++) index[i] = 1; + for (let i = 0; i < ba.dims.length - num_inds; i++) index[i] = 1; sub_dims = ba.dims.slice(0, ba.dims.length - num_inds); } ofs = ba.offset(index); @@ -601,7 +602,7 @@ function caml_ba_reshape(ba, vind) { caml_invalid_argument("Bigarray.reshape: bad number of dimensions"); } var num_elts = 1; - for (var i = 0; i < num_dims; i++) { + for (let i = 0; i < num_dims; i++) { new_dim[i] = vind[i]; if (new_dim[i] < 0) caml_invalid_argument("Bigarray.reshape: negative dimension"); @@ -622,7 +623,7 @@ function caml_ba_serialize(writer, ba, sz) { writer.write(32, ba.dims.length); writer.write(32, ba.kind | (ba.layout << 8)); if (ba.caml_custom == "_bigarr02") - for (var i = 0; i < ba.dims.length; i++) { + for (let i = 0; i < ba.dims.length; i++) { if (ba.dims[i] < 0xffff) writer.write(16, ba.dims[i]); else { writer.write(16, 0xffff); @@ -630,65 +631,65 @@ function caml_ba_serialize(writer, ba, sz) { writer.write(32, ba.dims[i]); } } - else for (var i = 0; i < ba.dims.length; i++) writer.write(32, ba.dims[i]); + else for (let i = 0; i < ba.dims.length; i++) writer.write(32, ba.dims[i]); switch (ba.kind) { case 2: //Int8Array case 3: //Uint8Array case 12: //Uint8Array - for (var i = 0; i < ba.data.length; i++) { + for (let i = 0; i < ba.data.length; i++) { writer.write(8, ba.data[i]); } break; case 4: // Int16Array case 5: // Uint16Array - for (var i = 0; i < ba.data.length; i++) { + for (let i = 0; i < ba.data.length; i++) { writer.write(16, ba.data[i]); } break; case 6: // Int32Array (int32) - for (var i = 0; i < ba.data.length; i++) { + for (let i = 0; i < ba.data.length; i++) { writer.write(32, ba.data[i]); } break; case 8: // Int32Array (int) case 9: // Int32Array (nativeint) writer.write(8, 0); - for (var i = 0; i < ba.data.length; i++) { + for (let i = 0; i < ba.data.length; i++) { writer.write(32, ba.data[i]); } break; case 7: // Int32Array (int64) - for (var i = 0; i < ba.data.length / 2; i++) { + for (let i = 0; i < ba.data.length / 2; i++) { var b = caml_int64_to_bytes(ba.get(i)); - for (var j = 0; j < 8; j++) writer.write(8, b[j]); + for (let j = 0; j < 8; j++) writer.write(8, b[j]); } break; case 1: // Float64Array - for (var i = 0; i < ba.data.length; i++) { + for (let i = 0; i < ba.data.length; i++) { var b = caml_int64_to_bytes(caml_int64_bits_of_float(ba.get(i))); - for (var j = 0; j < 8; j++) writer.write(8, b[j]); + for (let j = 0; j < 8; j++) writer.write(8, b[j]); } break; case 0: // Float32Array - for (var i = 0; i < ba.data.length; i++) { + for (let i = 0; i < ba.data.length; i++) { var b = caml_int32_bits_of_float(ba.get(i)); writer.write(32, b); } break; case 10: // Float32Array (complex32) - for (var i = 0; i < ba.data.length / 2; i++) { + for (let i = 0; i < ba.data.length / 2; i++) { var j = ba.get(i); writer.write(32, caml_int32_bits_of_float(j[1])); writer.write(32, caml_int32_bits_of_float(j[2])); } break; case 11: // Float64Array (complex64) - for (var i = 0; i < ba.data.length / 2; i++) { + for (let i = 0; i < ba.data.length / 2; i++) { var complex = ba.get(i); var b = caml_int64_to_bytes(caml_int64_bits_of_float(complex[1])); - for (var j = 0; j < 8; j++) writer.write(8, b[j]); + for (let j = 0; j < 8; j++) writer.write(8, b[j]); var b = caml_int64_to_bytes(caml_int64_bits_of_float(complex[2])); - for (var j = 0; j < 8; j++) writer.write(8, b[j]); + for (let j = 0; j < 8; j++) writer.write(8, b[j]); } break; } @@ -711,7 +712,7 @@ function caml_ba_deserialize(reader, sz, name) { var layout = (tag >> 8) & 1; var dims = []; if (name == "_bigarr02") - for (var i = 0; i < num_dims; i++) { + for (let i = 0; i < num_dims; i++) { var size_dim = reader.read16u(); if (size_dim == 0xffff) { var size_dim_hi = reader.read32u(); @@ -722,34 +723,34 @@ function caml_ba_deserialize(reader, sz, name) { } dims.push(size_dim); } - else for (var i = 0; i < num_dims; i++) dims.push(reader.read32u()); + else for (let i = 0; i < num_dims; i++) dims.push(reader.read32u()); var size = caml_ba_get_size(dims); var data = caml_ba_create_buffer(kind, size); var ba = caml_ba_create_unsafe(kind, layout, dims, data); switch (kind) { case 2: //Int8Array - for (var i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { data[i] = reader.read8s(); } break; case 3: //Uint8Array case 12: //Uint8Array - for (var i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { data[i] = reader.read8u(); } break; case 4: // Int16Array - for (var i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { data[i] = reader.read16s(); } break; case 5: // Uint16Array - for (var i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { data[i] = reader.read16u(); } break; case 6: // Int32Array (int32) - for (var i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { data[i] = reader.read32s(); } break; @@ -761,7 +762,7 @@ function caml_ba_deserialize(reader, sz, name) { caml_failwith( "input_value: cannot read bigarray with 64-bit OCaml ints", ); - for (var i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { data[i] = reader.read32s(); } break; @@ -769,8 +770,8 @@ function caml_ba_deserialize(reader, sz, name) { case 7: { // (int64) var t = new Array(8); - for (var i = 0; i < size; i++) { - for (var j = 0; j < 8; j++) t[j] = reader.read8u(); + for (let i = 0; i < size; i++) { + for (let j = 0; j < 8; j++) t[j] = reader.read8u(); var int64 = caml_int64_of_bytes(t); ba.set(i, int64); } @@ -779,21 +780,21 @@ function caml_ba_deserialize(reader, sz, name) { case 1: { // Float64Array var t = new Array(8); - for (var i = 0; i < size; i++) { - for (var j = 0; j < 8; j++) t[j] = reader.read8u(); + for (let i = 0; i < size; i++) { + for (let j = 0; j < 8; j++) t[j] = reader.read8u(); var f = caml_int64_float_of_bits(caml_int64_of_bytes(t)); ba.set(i, f); } break; } case 0: // Float32Array - for (var i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { var f = caml_int32_float_of_bits(reader.read32s()); ba.set(i, f); } break; case 10: // Float32Array (complex32) - for (var i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { var re = caml_int32_float_of_bits(reader.read32s()); var im = caml_int32_float_of_bits(reader.read32s()); ba.set(i, [254, re, im]); @@ -802,10 +803,10 @@ function caml_ba_deserialize(reader, sz, name) { case 11: { // Float64Array (complex64) var t = new Array(8); - for (var i = 0; i < size; i++) { - for (var j = 0; j < 8; j++) t[j] = reader.read8u(); + for (let i = 0; i < size; i++) { + for (let j = 0; j < 8; j++) t[j] = reader.read8u(); var re = caml_int64_float_of_bits(caml_int64_of_bytes(t)); - for (var j = 0; j < 8; j++) t[j] = reader.read8u(); + for (let j = 0; j < 8; j++) t[j] = reader.read8u(); var im = caml_int64_float_of_bits(caml_int64_of_bytes(t)); ba.set(i, [254, re, im]); } @@ -876,17 +877,17 @@ function caml_ba_hash(ba) { } case 6: // Int32Array (int32) if (num_elts > 64) num_elts = 64; - for (var i = 0; i < num_elts; i++) h = caml_hash_mix_int(h, ba.data[i]); + for (let i = 0; i < num_elts; i++) h = caml_hash_mix_int(h, ba.data[i]); break; case 8: // Int32Array (int) case 9: // Int32Array (nativeint) if (num_elts > 64) num_elts = 64; - for (var i = 0; i < num_elts; i++) h = caml_hash_mix_int(h, ba.data[i]); + for (let i = 0; i < num_elts; i++) h = caml_hash_mix_int(h, ba.data[i]); break; case 7: // Int32Array (int64) if (num_elts > 32) num_elts = 32; num_elts *= 2; - for (var i = 0; i < num_elts; i++) { + for (let i = 0; i < num_elts; i++) { h = caml_hash_mix_int(h, ba.data[i]); } break; @@ -894,13 +895,13 @@ function caml_ba_hash(ba) { num_elts *= 2; /* fallthrough */ case 0: // Float32Array if (num_elts > 64) num_elts = 64; - for (var i = 0; i < num_elts; i++) h = caml_hash_mix_float(h, ba.data[i]); + for (let i = 0; i < num_elts; i++) h = caml_hash_mix_float(h, ba.data[i]); break; case 11: // Float64Array (complex64) num_elts *= 2; /* fallthrough */ case 1: // Float64Array if (num_elts > 32) num_elts = 32; - for (var i = 0; i < num_elts; i++) h = caml_hash_mix_float(h, ba.data[i]); + for (let i = 0; i < num_elts; i++) h = caml_hash_mix_float(h, ba.data[i]); break; } return h; diff --git a/runtime/bigstring.js b/runtime/bigstring.js index f6ae0b5db5..a54d70679f 100644 --- a/runtime/bigstring.js +++ b/runtime/bigstring.js @@ -37,7 +37,7 @@ function bigstring_of_typed_array(ba) { //Provides: caml_bigstring_memcmp //Requires: caml_ba_get_1 function caml_bigstring_memcmp(s1, pos1, s2, pos2, len) { - for (var i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { var a = caml_ba_get_1(s1, pos1 + i); var b = caml_ba_get_1(s2, pos2 + i); if (a < b) return -1; diff --git a/runtime/dynlink.js b/runtime/dynlink.js index 064827861e..c45e04171d 100644 --- a/runtime/dynlink.js +++ b/runtime/dynlink.js @@ -68,7 +68,7 @@ function caml_dynlink_get_current_libs() { var current_libs = get_current_libs(); var len = current_libs.length; var a = new Array(len); - for (var i = 0; i < len; i++) a[i] = i; + for (let i = 0; i < len; i++) a[i] = i; return a; } diff --git a/runtime/format.js b/runtime/format.js index 1371bc3cb9..25b0c35000 100644 --- a/runtime/format.js +++ b/runtime/format.js @@ -36,7 +36,7 @@ function caml_parse_format(fmt) { prec: -1, conv: "f", }; - for (var i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { var c = fmt.charAt(i); switch (c) { case "-": @@ -124,7 +124,7 @@ function caml_finish_formatting(f, rawbuffer) { /* Do the formatting */ var buffer = ""; if (f.justify == "+" && f.filler == " ") - for (var i = len; i < f.width; i++) buffer += " "; + for (let i = len; i < f.width; i++) buffer += " "; if (f.signedconv) { if (f.sign < 0) buffer += "-"; else if (f.signstyle != "-") buffer += f.signstyle; @@ -132,8 +132,8 @@ function caml_finish_formatting(f, rawbuffer) { if (f.alternate && f.base == 8) buffer += "0"; if (f.alternate && f.base == 16) buffer += f.uppercase ? "0X" : "0x"; if (f.justify == "+" && f.filler == "0") - for (var i = len; i < f.width; i++) buffer += "0"; + for (let i = len; i < f.width; i++) buffer += "0"; buffer += rawbuffer; - if (f.justify == "-") for (var i = len; i < f.width; i++) buffer += " "; + if (f.justify == "-") for (let i = len; i < f.width; i++) buffer += " "; return caml_string_of_jsbytes(buffer); } diff --git a/runtime/fs.js b/runtime/fs.js index a7529d7015..1fd1f7b734 100644 --- a/runtime/fs.js +++ b/runtime/fs.js @@ -91,7 +91,7 @@ function caml_make_path(name) { var comp0 = path_is_absolute(name); var comp = comp0[1].split(/[/\\]/); var ncomp = []; - for (var i = 0; i < comp.length; i++) { + for (let i = 0; i < comp.length; i++) { switch (comp[i]) { case "..": if (ncomp.length > 1) ncomp.pop(); @@ -133,7 +133,7 @@ jsoo_mount_point.push({ //Requires: jsoo_mount_point, caml_string_of_jsbytes function caml_list_mount_point() { var prev = 0; - for (var i = 0; i < jsoo_mount_point.length; i++) { + for (let i = 0; i < jsoo_mount_point.length; i++) { var old = prev; prev = [0, caml_string_of_jsbytes(jsoo_mount_point[i].path), old]; } @@ -147,7 +147,7 @@ function resolve_fs_device(name) { var name = path.join("/"); var name_slash = caml_trailing_slash(name); var res; - for (var i = 0; i < jsoo_mount_point.length; i++) { + for (let i = 0; i < jsoo_mount_point.length; i++) { var m = jsoo_mount_point[i]; if ( name_slash.search(m.path) == 0 && @@ -190,7 +190,7 @@ function caml_unmount(name) { var path = caml_make_path(name); var name = caml_trailing_slash(path.join("/")); var idx = -1; - for (var i = 0; i < jsoo_mount_point.length; i++) + for (let i = 0; i < jsoo_mount_point.length; i++) if (jsoo_mount_point[i].path == name) idx = i; if (idx > -1) jsoo_mount_point.splice(idx, 1); return 0; @@ -243,7 +243,7 @@ function caml_sys_read_directory(name) { var a = root.device.readdir(root.rest); var l = new Array(a.length + 1); l[0] = 0; - for (var i = 0; i < a.length; i++) l[i + 1] = caml_string_of_jsbytes(a[i]); + for (let i = 0; i < a.length; i++) l[i + 1] = caml_string_of_jsbytes(a[i]); return l; } @@ -319,7 +319,7 @@ function jsoo_create_file_extern(name, content) { function caml_fs_init() { var tmp = globalThis.caml_fs_tmp; if (tmp) { - for (var i = 0; i < tmp.length; i++) { + for (let i = 0; i < tmp.length; i++) { jsoo_create_file(tmp[i].name, tmp[i].content); } } diff --git a/runtime/fs_fake.js b/runtime/fs_fake.js index 1b88bc1496..0becff9195 100644 --- a/runtime/fs_fake.js +++ b/runtime/fs_fake.js @@ -36,7 +36,7 @@ MlFakeDevice.prototype.nm = function (name) { MlFakeDevice.prototype.create_dir_if_needed = function (name) { var comp = name.split("/"); var res = ""; - for (var i = 0; i < comp.length - 1; i++) { + for (let i = 0; i < comp.length - 1; i++) { res += comp[i] + "/"; if (this.content[res]) continue; this.content[res] = Symbol("directory"); @@ -134,7 +134,7 @@ MlFakeDevice.prototype.rmdir = function (name, raise_unix) { caml_raise_sys_error(name + ": Not a directory"); } } - for (var n in this.content) { + for (let n in this.content) { if (n.match(r)) { if (unix_error) { caml_raise_with_args( @@ -159,7 +159,7 @@ MlFakeDevice.prototype.readdir = function (name) { var r = new RegExp("^" + name_slash + "([^/]+)"); var seen = {}; var a = []; - for (var n in this.content) { + for (let n in this.content) { var m = n.match(r); if (m && !seen[m[1]]) { seen[m[1]] = true; diff --git a/runtime/fs_node.js b/runtime/fs_node.js index 8e6d6bf127..1ca01160fa 100644 --- a/runtime/fs_node.js +++ b/runtime/fs_node.js @@ -97,7 +97,7 @@ MlNodeDevice.prototype.unlink = function (name, raise_unix) { MlNodeDevice.prototype.open = function (name, f, raise_unix) { var consts = require("constants"); var res = 0; - for (var key in f) { + for (let key in f) { switch (key) { case "rdonly": res |= consts.O_RDONLY; diff --git a/runtime/graphics.js b/runtime/graphics.js index 9d23f885f1..1209c3a339 100644 --- a/runtime/graphics.js +++ b/runtime/graphics.js @@ -281,7 +281,7 @@ function caml_gr_arc_aux(ctx, cx, cy, ry, rx, a1, a2) { var num = (((a2 - a1) * Math.PI * ((rx + ry) / 2)) / space) | 0; var delta = ((a2 - a1) * Math.PI) / num; var i = a1 * Math.PI; - for (var j = 0; j <= num; j++) { + for (let j = 0; j <= num; j++) { xPos = cx - rx * Math.sin(i) * Math.sin(rot * Math.PI) + @@ -336,7 +336,7 @@ function caml_gr_fill_poly(ar) { var s = caml_gr_state_get(); s.context.beginPath(); s.context.moveTo(ar[1][1], s.height - ar[1][2]); - for (var i = 2; i < ar.length; i++) + for (let i = 2; i < ar.length; i++) s.context.lineTo(ar[i][1], s.height - ar[i][2]); s.context.lineTo(ar[1][1], s.height - ar[1][2]); s.context.fill(); @@ -415,8 +415,8 @@ function caml_gr_make_image(arr) { var h = arr.length - 1; var w = arr[1].length - 1; var im = s.context.createImageData(w, h); - for (var i = 0; i < h; i++) { - for (var j = 0; j < w; j++) { + for (let i = 0; i < h; i++) { + for (let j = 0; j < w; j++) { var c = arr[i + 1][j + 1]; var o = i * (w * 4) + j * 4; if (c == -1) { @@ -438,9 +438,9 @@ function caml_gr_make_image(arr) { //Requires: caml_gr_state_get function caml_gr_dump_image(im) { var data = [0]; - for (var i = 0; i < im.height; i++) { + for (let i = 0; i < im.height; i++) { data[i + 1] = [0]; - for (var j = 0; j < im.width; j++) { + for (let j = 0; j < im.width; j++) { var o = i * (im.width * 4) + j * 4, r = im.data[o + 0], g = im.data[o + 1], @@ -486,7 +486,7 @@ function caml_gr_blit_image(im, x, y) { im.width, im.height, ); - for (var i = 0; i < im2.data.length; i += 4) { + for (let i = 0; i < im2.data.length; i += 4) { im.data[i] = im2.data[i]; im.data[i + 1] = im2.data[i + 1]; im.data[i + 2] = im2.data[i + 2]; diff --git a/runtime/hash.js b/runtime/hash.js index b03278365b..315f617693 100644 --- a/runtime/hash.js +++ b/runtime/hash.js @@ -43,25 +43,25 @@ function caml_hash_univ_param(count, limit, obj) { default: count--; hash_accu = (hash_accu * 19 + obj[0]) | 0; - for (var i = obj.length - 1; i > 0; i--) hash_aux(obj[i]); + for (let i = obj.length - 1; i > 0; i--) hash_aux(obj[i]); } } else if (caml_is_ml_bytes(obj)) { count--; var content = caml_ml_bytes_content(obj); if (typeof content === "string") { - for (var b = content, l = b.length, i = 0; i < l; i++) + for (let b = content, l = b.length, i = 0; i < l; i++) hash_accu = (hash_accu * 19 + b.charCodeAt(i)) | 0; } else { /* ARRAY */ - for (var a = content, l = a.length, i = 0; i < l; i++) + for (let a = content, l = a.length, i = 0; i < l; i++) hash_accu = (hash_accu * 19 + a[i]) | 0; } } else if (caml_is_ml_string(obj)) { var jsbytes = caml_jsbytes_of_string(obj); - for (var b = jsbytes, l = jsbytes.length, i = 0; i < l; i++) + for (let b = jsbytes, l = jsbytes.length, i = 0; i < l; i++) hash_accu = (hash_accu * 19 + b.charCodeAt(i)) | 0; } else if (typeof obj === "string") { - for (var b = obj, l = obj.length, i = 0; i < l; i++) + for (let b = obj, l = obj.length, i = 0; i < l; i++) hash_accu = (hash_accu * 19 + b.charCodeAt(i)) | 0; } else if (obj === (obj | 0)) { // Integer @@ -71,7 +71,7 @@ function caml_hash_univ_param(count, limit, obj) { // Float count--; var p = caml_int64_to_bytes(caml_int64_bits_of_float(obj)); - for (var i = 7; i >= 0; i--) hash_accu = (hash_accu * 19 + p[i]) | 0; + for (let i = 7; i >= 0; i--) hash_accu = (hash_accu * 19 + p[i]) | 0; } else if (obj && obj.caml_custom) { if ( caml_custom_ops[obj.caml_custom] && diff --git a/runtime/internalMod.js b/runtime/internalMod.js index 4c19cf7691..4ece31318b 100644 --- a/runtime/internalMod.js +++ b/runtime/internalMod.js @@ -45,7 +45,7 @@ function caml_CamlinternalMod_init_mod(loc, shape) { switch (shape[0]) { case 0: //module struct[idx] = [0]; - for (var i = 1; i < shape[1].length; i++) + for (let i = 1; i < shape[1].length; i++) loop(shape[1][i], struct[idx], i); break; default: //case 1://Value @@ -72,7 +72,7 @@ function caml_CamlinternalMod_update_mod(shape, real, x) { else switch (shape[0]) { case 0: //module - for (var i = 1; i < shape[1].length; i++) + for (let i = 1; i < shape[1].length; i++) caml_CamlinternalMod_update_mod(shape[1][i], real[i], x[i]); break; //case 1://Value @@ -109,7 +109,7 @@ function caml_CamlinternalMod_init_mod(loc, shape, cont) { switch (shape[0]) { case 0: //module struct[idx] = [0]; - for (var i = 1; i < shape[1].length; i++) + for (let i = 1; i < shape[1].length; i++) loop(shape[1][i], struct[idx], i); break; default: //case 1://Value @@ -137,7 +137,7 @@ function caml_CamlinternalMod_update_mod(shape, real, x, cont) { else switch (shape[0]) { case 0: //module - for (var i = 1; i < shape[1].length; i++) + for (let i = 1; i < shape[1].length; i++) loop(shape[1][i], real[i], x[i]); break; //case 1://Value diff --git a/runtime/io.js b/runtime/io.js index f1792c70f8..eb4774f03e 100644 --- a/runtime/io.js +++ b/runtime/io.js @@ -153,7 +153,7 @@ function caml_ml_channel_get(id) { //Requires: caml_ml_channels function caml_ml_out_channels_list() { var l = 0; - for (var c = 0; c < caml_ml_channels.length; c++) { + for (let c = 0; c < caml_ml_channels.length; c++) { if ( caml_ml_channels[c] && caml_ml_channels[c].opened && @@ -431,7 +431,7 @@ function caml_ml_input_char(chanid) { function caml_ml_input_int(chanid) { var chan = caml_ml_channel_get(chanid); var res = 0; - for (var i = 0; i < 4; i++) { + for (let i = 0; i < 4; i++) { res = ((res << 8) + caml_ml_input_char(chanid)) | 0; } return res | 0; diff --git a/runtime/jslib.js b/runtime/jslib.js index 2d7887564f..d05143fadc 100644 --- a/runtime/jslib.js +++ b/runtime/jslib.js @@ -249,14 +249,14 @@ function caml_js_to_array(a) { var len = a.length; var b = new Array(len + 1); b[0] = 0; - for (var i = 0; i < len; i++) b[i + 1] = a[i]; + for (let i = 0; i < len; i++) b[i + 1] = a[i]; return b; } //Provides: caml_list_of_js_array const (mutable) function caml_list_of_js_array(a) { var l = 0; - for (var i = a.length - 1; i >= 0; i--) { + for (let i = a.length - 1; i >= 0; i--) { var e = a[i]; l = [0, e, l]; } @@ -382,7 +382,7 @@ function caml_js_wrap_callback(f) { var len = arguments.length; if (len > 0) { var args = new Array(len); - for (var i = 0; i < len; i++) args[i] = arguments[i]; + for (let i = 0; i < len; i++) args[i] = arguments[i]; } else { args = [undefined]; } @@ -397,7 +397,7 @@ 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]; + for (let i = 0; i < len; i++) args[i] = arguments[i]; return caml_callback(f, [args]); }; } @@ -408,7 +408,7 @@ function caml_js_wrap_callback_strict(arity, f) { 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]; + for (let i = 0; i < len; i++) args[i] = arguments[i]; return caml_callback(f, args); }; } @@ -418,7 +418,7 @@ function caml_js_wrap_callback_unsafe(f) { return function () { var len = caml_js_function_arity(f); var args = new Array(len); - for (var i = 0; i < len; i++) args[i] = arguments[i]; + for (let i = 0; i < len; i++) args[i] = arguments[i]; return caml_callback(f, args); }; } @@ -429,7 +429,7 @@ function caml_js_wrap_meth_callback(f) { 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]; + for (let i = 0; i < len; i++) args[i + 1] = arguments[i]; var res = caml_callback(f, args); return res instanceof Function ? caml_js_wrap_callback(res) : res; }; @@ -440,7 +440,7 @@ 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]; + for (let i = 0; i < len; i++) args[i] = arguments[i]; return caml_callback(f, [this, args]); }; } @@ -451,7 +451,7 @@ function caml_js_wrap_meth_callback_strict(arity, f) { 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]; + for (let i = 0; i < len; i++) args[i + 1] = arguments[i]; return caml_callback(f, args); }; } @@ -462,7 +462,7 @@ function caml_js_wrap_meth_callback_unsafe(f) { 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]; + for (let i = 0; i < len; i++) args[i + 1] = arguments[i]; return caml_callback(f, args); }; } @@ -515,7 +515,7 @@ function caml_pure_js_expr(s) { //Requires: caml_jsstring_of_string function caml_js_object(a) { var o = {}; - for (var i = 1; i < a.length; i++) { + for (let i = 1; i < a.length; i++) { var p = a[i]; o[caml_jsstring_of_string(p[1])] = p[2]; } diff --git a/runtime/jslib_js_of_ocaml.js b/runtime/jslib_js_of_ocaml.js index 6df6445d0c..f8782777f4 100644 --- a/runtime/jslib_js_of_ocaml.js +++ b/runtime/jslib_js_of_ocaml.js @@ -73,7 +73,7 @@ function caml_js_get_console() { "timeEnd", ]; function f() {} - for (var i = 0; i < m.length; i++) if (!c[m[i]]) c[m[i]] = f; + for (let i = 0; i < m.length; i++) if (!c[m[i]]) c[m[i]] = f; return c; } diff --git a/runtime/lexing.js b/runtime/lexing.js index 95710a72e6..1f2824592c 100644 --- a/runtime/lexing.js +++ b/runtime/lexing.js @@ -21,7 +21,7 @@ function caml_lex_array(s) { s = caml_jsbytes_of_string(s); var l = s.length / 2; var a = new Array(l); - for (var i = 0; i < l; i++) + for (let i = 0; i < l; i++) a[i] = ((s.charCodeAt(2 * i) | (s.charCodeAt(2 * i + 1) << 8)) << 16) >> 16; return a; } diff --git a/runtime/marshal.js b/runtime/marshal.js index 14b5b1bf1f..d004a8e5f9 100644 --- a/runtime/marshal.js +++ b/runtime/marshal.js @@ -152,7 +152,7 @@ MlStringReader.prototype = { var b = new Uint8Array(len); var s = this.s; var i = this.i; - for (var j = 0; j < len; j++) { + for (let j = 0; j < len; j++) { b[j] = s.charCodeAt(i + j); } this.i = i + len; @@ -211,7 +211,7 @@ BigStringReader.prototype = { readstr: function (len) { var i = this.i; var arr = new Array(len); - for (var j = 0; j < len; j++) { + for (let j = 0; j < len; j++) { arr[j] = caml_ba_get_1(this.s, i + j); } this.i = i + len; @@ -252,7 +252,7 @@ function caml_input_value_from_bytes(s, ofs) { //Requires: caml_int64_of_bytes function caml_int64_unmarshal(reader, size) { var t = new Array(8); - for (var j = 0; j < 8; j++) t[j] = reader.read8u(); + for (let j = 0; j < 8; j++) t[j] = reader.read8u(); size[0] = 8; return caml_int64_of_bytes(t); } @@ -261,7 +261,7 @@ function caml_int64_unmarshal(reader, size) { //Requires: caml_int64_to_bytes function caml_int64_marshal(writer, v, sizes) { var b = caml_int64_to_bytes(v); - for (var i = 0; i < 8; i++) writer.write(8, b[i]); + for (let i = 0; i < 8; i++) writer.write(8, b[i]); sizes[0] = 8; sizes[1] = 8; } @@ -459,7 +459,7 @@ function caml_input_value_from_reader(reader, ofs) { case 0x0c: { //cst.CODE_DOUBLE_LITTLE: var t = new Array(8); - for (var i = 0; i < 8; i++) t[7 - i] = reader.read8u(); + for (let i = 0; i < 8; i++) t[7 - i] = reader.read8u(); var v = caml_float_of_bytes(t); if (intern_obj_table) intern_obj_table[obj_counter++] = v; return v; @@ -467,7 +467,7 @@ function caml_input_value_from_reader(reader, ofs) { case 0x0b: { //cst.CODE_DOUBLE_BIG: var t = new Array(8); - for (var i = 0; i < 8; i++) t[i] = reader.read8u(); + for (let i = 0; i < 8; i++) t[i] = reader.read8u(); var v = caml_float_of_bytes(t); if (intern_obj_table) intern_obj_table[obj_counter++] = v; return v; @@ -479,8 +479,8 @@ function caml_input_value_from_reader(reader, ofs) { v[0] = 254; var t = new Array(8); if (intern_obj_table) intern_obj_table[obj_counter++] = v; - for (var i = 1; i <= len; i++) { - for (var j = 0; j < 8; j++) t[7 - j] = reader.read8u(); + 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; @@ -492,8 +492,8 @@ function caml_input_value_from_reader(reader, ofs) { v[0] = 254; var t = new Array(8); if (intern_obj_table) intern_obj_table[obj_counter++] = v; - for (var i = 1; i <= len; i++) { - for (var j = 0; j < 8; j++) t[j] = reader.read8u(); + 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; @@ -505,8 +505,8 @@ function caml_input_value_from_reader(reader, ofs) { v[0] = 254; if (intern_obj_table) intern_obj_table[obj_counter++] = v; var t = new Array(8); - for (var i = 1; i <= len; i++) { - for (var j = 0; j < 8; j++) t[7 - j] = reader.read8u(); + 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; @@ -517,8 +517,8 @@ function caml_input_value_from_reader(reader, ofs) { var v = new Array(len + 1); v[0] = 254; var t = new Array(8); - for (var i = 1; i <= len; i++) { - for (var j = 0; j < 8; j++) t[j] = reader.read8u(); + 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; @@ -655,7 +655,7 @@ if (typeof globalThis.Map === "undefined") { this.objs = objs; } NaiveLookup.prototype.get = function (v) { - for (var i = 0; i < this.objs.length; i++) { + for (let i = 0; i < this.objs.length; i++) { if (this.objs[i] === v) return i; } }; @@ -705,17 +705,17 @@ var caml_output_val = (function () { size_32: 0, size_64: 0, write: function (size, value) { - for (var i = size - 8; i >= 0; i -= 8) + for (let i = size - 8; i >= 0; i -= 8) this.chunk[this.chunk_idx++] = (value >> i) & 0xff; }, write_at: function (pos, size, value) { var pos = pos; - for (var i = size - 8; i >= 0; i -= 8) + for (let i = size - 8; i >= 0; i -= 8) this.chunk[pos++] = (value >> i) & 0xff; }, write_code: function (size, code, value) { this.chunk[this.chunk_idx++] = code; - for (var i = size - 8; i >= 0; i -= 8) + for (let i = size - 8; i >= 0; i -= 8) this.chunk[this.chunk_idx++] = (value >> i) & 0xff; }, write_shared: function (offset) { @@ -777,11 +777,11 @@ var caml_output_val = (function () { caml_invalid_argument("output_value: abstract value (Custom)"); if (ops.fixed_length == undefined) { writer.write(8, 0x18 /*cst.CODE_CUSTOM_LEN*/); - for (var i = 0; i < name.length; i++) + for (let i = 0; i < name.length; i++) writer.write(8, name.charCodeAt(i)); writer.write(8, 0); var header_pos = writer.pos(); - for (var i = 0; i < 12; i++) { + for (let i = 0; i < 12; i++) { writer.write(8, 0); } ops.serialize(writer, v, sz_32_64); @@ -790,7 +790,7 @@ var caml_output_val = (function () { writer.write_at(header_pos + 8, 32, sz_32_64[1]); } else { writer.write(8, 0x19 /*cst.CODE_CUSTOM_FIXED*/); - for (var i = 0; i < name.length; i++) + for (let i = 0; i < name.length; i++) writer.write(8, name.charCodeAt(i)); writer.write(8, 0); var old_pos = writer.pos(); @@ -835,7 +835,7 @@ var caml_output_val = (function () { else if (len < 0x100) writer.write_code(8, 0x09 /*cst.CODE_STRING8*/, len); else writer.write_code(32, 0x0a /*cst.CODE_STRING32*/, len); - for (var i = 0; i < len; i++) + for (let i = 0; i < len; i++) writer.write(8, caml_bytes_unsafe_get(v, i)); writer.size_32 += 1 + (((len + 4) / 4) | 0); writer.size_64 += 1 + (((len + 8) / 8) | 0); @@ -846,7 +846,7 @@ var caml_output_val = (function () { else if (len < 0x100) writer.write_code(8, 0x09 /*cst.CODE_STRING8*/, len); else writer.write_code(32, 0x0a /*cst.CODE_STRING32*/, len); - for (var i = 0; i < len; i++) + for (let i = 0; i < len; i++) writer.write(8, caml_string_unsafe_get(v, i)); writer.size_32 += 1 + (((len + 4) / 4) | 0); writer.size_64 += 1 + (((len + 8) / 8) | 0); @@ -862,7 +862,7 @@ var caml_output_val = (function () { if (memo(v)) return; var t = caml_int64_to_bytes(caml_int64_bits_of_float(v)); writer.write(8, 0x0c /*cst.CODE_DOUBLE_LITTLE*/); - for (var i = 0; i < 8; i++) { + for (let i = 0; i < 8; i++) { writer.write(8, t[7 - i]); } writer.size_32 += 3; diff --git a/runtime/md5.js b/runtime/md5.js index bf9a6f746c..1b6f6425ad 100644 --- a/runtime/md5.js +++ b/runtime/md5.js @@ -202,15 +202,15 @@ function caml_MD5Final(ctx) { ctx.b8[in_buf] = 0x80; in_buf++; if (in_buf > 56) { - for (var j = in_buf; j < 64; j++) { + for (let j = in_buf; j < 64; j++) { ctx.b8[j] = 0; } caml_MD5Transform(ctx.w, ctx.b32); - for (var j = 0; j < 56; j++) { + for (let j = 0; j < 56; j++) { ctx.b8[j] = 0; } } else { - for (var j = in_buf; j < 56; j++) { + for (let j = in_buf; j < 56; j++) { ctx.b8[j] = 0; } } @@ -218,8 +218,8 @@ function caml_MD5Final(ctx) { ctx.b32[15] = (ctx.len >> 29) & 0x1fffffff; caml_MD5Transform(ctx.w, ctx.b32); var t = new Uint8Array(16); - for (var i = 0; i < 4; i++) - for (var j = 0; j < 4; j++) t[i * 4 + j] = (ctx.w[i] >> (8 * j)) & 0xff; + for (let i = 0; i < 4; i++) + for (let j = 0; j < 4; j++) t[i * 4 + j] = (ctx.w[i] >> (8 * j)) & 0xff; return t; } diff --git a/runtime/mlBytes.js b/runtime/mlBytes.js index 577616952d..f99158c4a0 100644 --- a/runtime/mlBytes.js +++ b/runtime/mlBytes.js @@ -84,10 +84,13 @@ function caml_subarray_to_jsbytes(a, i, len) { //Provides: caml_utf8_of_utf16 function caml_utf8_of_utf16(s) { - for (var b = "", t = b, c, d, i = 0, l = s.length; i < l; i++) { + let b = ""; + let t = b; + for (let c, d, i = 0, l = s.length; i < l; i++) { c = s.charCodeAt(i); if (c < 0x80) { - for (var j = i + 1; j < l && (c = s.charCodeAt(j)) < 0x80; j++); + let j = i + 1; + for (; j < l && (c = s.charCodeAt(j)) < 0x80; j++); if (j - i > 512) { t.substr(0, 1); b += t; @@ -135,10 +138,13 @@ function caml_utf8_of_utf16(s) { //Provides: caml_utf16_of_utf8 function caml_utf16_of_utf8(s) { - for (var b = "", t = "", c, c1, c2, v, i = 0, l = s.length; i < l; i++) { + let b = ""; + let t = ""; + for (let c, c1, c2, v, i = 0, l = s.length; i < l; i++) { c1 = s.charCodeAt(i); if (c1 < 0x80) { - for (var j = i + 1; j < l && (c1 = s.charCodeAt(j)) < 0x80; j++); + let j = i + 1; + for (; j < l && (c1 = s.charCodeAt(j)) < 0x80; j++); if (j - i > 512) { t.substr(0, 1); b += t; @@ -192,7 +198,7 @@ function jsoo_is_ascii(s) { // The regular expression gets better at around this point for all browsers if (s.length < 24) { // Spidermonkey gets much slower when s.length >= 24 (on 64 bit archs) - for (var i = 0; i < s.length; i++) if (s.charCodeAt(i) > 127) return false; + for (let i = 0; i < s.length; i++) if (s.charCodeAt(i) > 127) return false; return true; } else return !/[^\x00-\x7f]/.test(s); } @@ -295,7 +301,7 @@ function caml_bytes_get32(s, i) { function caml_string_get64(s, i) { if (i >>> 0 >= caml_ml_string_length(s) - 7) caml_string_bound_error(); var a = new Array(8); - for (var j = 0; j < 8; j++) { + for (let j = 0; j < 8; j++) { a[7 - j] = caml_string_unsafe_get(s, i + j); } return caml_int64_of_bytes(a); @@ -307,7 +313,7 @@ function caml_string_get64(s, i) { function caml_bytes_get64(s, i) { if (i >>> 0 >= s.l - 7) caml_bytes_bound_error(); var a = new Array(8); - for (var j = 0; j < 8; j++) { + for (let j = 0; j < 8; j++) { a[7 - j] = caml_bytes_unsafe_get(s, i + j); } return caml_int64_of_bytes(a); @@ -395,7 +401,7 @@ function caml_string_set32(s, i, i32) { function caml_bytes_set64(s, i, i64) { if (i >>> 0 >= s.l - 7) caml_bytes_bound_error(); var a = caml_int64_to_bytes(i64); - for (var j = 0; j < 8; j++) { + for (let j = 0; j < 8; j++) { caml_bytes_unsafe_set(s, i + 7 - j, a[j]); } return 0; @@ -660,13 +666,14 @@ function caml_blit_bytes(s1, i1, s2, i2, len) { c2 = s2.c; if (s1.t == 4 /* ARRAY */) { if (i2 <= i1) { - for (var i = 0; i < len; i++) c2[i2 + i] = c1[i1 + i]; + for (let i = 0; i < len; i++) c2[i2 + i] = c1[i1 + i]; } else { - for (var i = len - 1; i >= 0; i--) c2[i2 + i] = c1[i1 + i]; + for (let i = len - 1; i >= 0; i--) c2[i2 + i] = c1[i1 + i]; } } else { var l = Math.min(len, c1.length - i1); - for (var i = 0; i < l; i++) c2[i2 + i] = c1.charCodeAt(i1 + i); + let i = 0; + for (; i < l; i++) c2[i2 + i] = c1.charCodeAt(i1 + i); for (; i < len; i++) c2[i2 + i] = 0; } } diff --git a/runtime/nat.js b/runtime/nat.js index 5f2738740a..2e6d4de982 100644 --- a/runtime/nat.js +++ b/runtime/nat.js @@ -28,7 +28,7 @@ MlNat.prototype.caml_custom = "_nat"; function caml_hash_nat(x) { var len = num_digits_nat(x, 0, x.data.length); var h = 0; - for (var i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { h = caml_hash_mix_int(h, x.data[i]); } return h; @@ -49,7 +49,7 @@ function nat_of_array(l) { //Requires: MlNat function create_nat(size) { var arr = new MlNat(size); - for (var i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { arr.data[i] = -1; } return arr; @@ -57,7 +57,7 @@ function create_nat(size) { //Provides: set_to_zero_nat function set_to_zero_nat(nat, ofs, len) { - for (var i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { nat.data[ofs + i] = 0; } return 0; @@ -65,7 +65,7 @@ function set_to_zero_nat(nat, ofs, len) { //Provides: blit_nat function blit_nat(nat1, ofs1, nat2, ofs2, len) { - for (var i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { nat1.data[ofs1 + i] = nat2.data[ofs2 + i]; } return 0; @@ -95,7 +95,7 @@ function nth_digit_nat_native(nat, ofs) { //Provides: num_digits_nat function num_digits_nat(nat, ofs, len) { - for (var i = len - 1; i >= 0; i--) { + for (let i = len - 1; i >= 0; i--) { if (nat.data[ofs + i] != 0) return i + 1; } return 1; // 0 counts as 1 digit @@ -152,7 +152,7 @@ function is_digit_odd(nat, ofs) { //Provides: incr_nat function incr_nat(nat, ofs, len, carry_in) { var carry = carry_in; - for (var i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { var x = (nat.data[ofs + i] >>> 0) + carry; nat.data[ofs + i] = x | 0; if (x == x >>> 0) { @@ -170,7 +170,7 @@ function incr_nat(nat, ofs, len, carry_in) { //Requires: incr_nat function add_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { var carry = carry_in; - for (var i = 0; i < len2; i++) { + for (let i = 0; i < len2; i++) { var x = (nat1.data[ofs1 + i] >>> 0) + (nat2.data[ofs2 + i] >>> 0) + carry; nat1.data[ofs1 + i] = x; if (x == x >>> 0) { @@ -184,7 +184,7 @@ function add_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { //Provides: complement_nat function complement_nat(nat, ofs, len) { - for (var i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { nat.data[ofs + i] = (-1 >>> 0) - (nat.data[ofs + i] >>> 0); } } @@ -193,7 +193,7 @@ function complement_nat(nat, ofs, len) { //Provides: decr_nat function decr_nat(nat, ofs, len, carry_in) { var borrow = carry_in == 1 ? 0 : 1; - for (var i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { var x = (nat.data[ofs + i] >>> 0) - borrow; nat.data[ofs + i] = x; if (x >= 0) { @@ -212,7 +212,7 @@ function decr_nat(nat, ofs, len, carry_in) { //Requires: decr_nat function sub_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { var borrow = carry_in == 1 ? 0 : 1; - for (var i = 0; i < len2; i++) { + for (let i = 0; i < len2; i++) { var x = (nat1.data[ofs1 + i] >>> 0) - (nat2.data[ofs2 + i] >>> 0) - borrow; nat1.data[ofs1 + i] = x; if (x >= 0) { @@ -231,7 +231,7 @@ function sub_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { function mult_digit_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3) { var carry = 0; var a = nat3.data[ofs3] >>> 0; - for (var i = 0; i < len2; i++) { + for (let i = 0; i < len2; i++) { var x1 = (nat1.data[ofs1 + i] >>> 0) + (nat2.data[ofs2 + i] >>> 0) * (a & 0x0000ffff) + @@ -264,7 +264,7 @@ function mult_digit_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3) { //Requires: mult_digit_nat function mult_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3, len3) { var carry = 0; - for (var i = 0; i < len3; i++) { + for (let i = 0; i < len3; i++) { carry += mult_digit_nat( nat1, ofs1 + i, @@ -298,7 +298,7 @@ function shift_left_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { return 0; } var wrap = 0; - for (var i = 0; i < len1; i++) { + for (let i = 0; i < len1; i++) { var a = nat1.data[ofs1 + i] >>> 0; nat1.data[ofs1 + i] = (a << nbits) | wrap; wrap = a >>> (32 - nbits); @@ -324,7 +324,7 @@ function div_digit_nat(natq, ofsq, natr, ofsr, nat1, ofs1, len, nat2, ofs2) { var rem = nat1.data[ofs1 + len - 1] >>> 0; // natq[ofsq+len-1] is guaranteed to be zero (due to the MSD requirement), // and should not be written to. - for (var i = len - 2; i >= 0; i--) { + for (let i = len - 2; i >= 0; i--) { var x = div_helper(rem, nat1.data[ofs1 + i] >>> 0, nat2.data[ofs2] >>> 0); natq.data[ofsq + i] = x[0]; rem = x[1]; @@ -350,7 +350,7 @@ function div_nat(nat1, ofs1, len1, nat2, ofs2, len2) { var d = (nat2.data[ofs2 + len2 - 1] >>> 0) + 1; var a = create_nat(len2 + 1); - for (var i = len1 - 1; i >= len2; i--) { + for (let i = len1 - 1; i >= len2; i--) { // Decent lower bound on quo var quo = d == 4294967296 @@ -388,7 +388,7 @@ function shift_right_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { return 0; } var wrap = 0; - for (var i = len1 - 1; i >= 0; i--) { + for (let i = len1 - 1; i >= 0; i--) { var a = nat1.data[ofs1 + i] >>> 0; nat1.data[ofs1 + i] = (a >>> nbits) | wrap; wrap = a << (32 - nbits); @@ -411,7 +411,7 @@ function compare_nat(nat1, ofs1, len1, nat2, ofs2, len2) { var b = num_digits_nat(nat2, ofs2, len2); if (a > b) return 1; if (a < b) return -1; - for (var i = len1 - 1; i >= 0; i--) { + for (let i = len1 - 1; i >= 0; i--) { if (nat1.data[ofs1 + i] >>> 0 > nat2.data[ofs2 + i] >>> 0) return 1; if (nat1.data[ofs1 + i] >>> 0 < nat2.data[ofs2 + i] >>> 0) return -1; } @@ -446,7 +446,7 @@ function lxor_digit_nat(nat1, ofs1, nat2, ofs2) { function serialize_nat(writer, nat, sz) { var len = nat.data.length; writer.write(32, len); - for (var i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { writer.write(32, nat.data[i]); } sz[0] = len * 4; @@ -458,7 +458,7 @@ function serialize_nat(writer, nat, sz) { function deserialize_nat(reader, sz) { var len = reader.read32s(); var nat = new MlNat(len); - for (var i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { nat.data[i] = reader.read32s(); } sz[0] = len * 4; diff --git a/runtime/obj.js b/runtime/obj.js index d4c7cb1522..012549c700 100644 --- a/runtime/obj.js +++ b/runtime/obj.js @@ -63,7 +63,7 @@ function caml_obj_set_tag(x, tag) { function caml_obj_block(tag, size) { var o = new Array(size + 1); o[0] = tag; - for (var i = 1; i <= size; i++) o[i] = 0; + for (let i = 1; i <= size; i++) o[i] = 0; return o; } @@ -72,7 +72,7 @@ function caml_obj_with_tag(tag, x) { var l = x.length; var a = new Array(l); a[0] = tag; - for (var i = 1; i < l; i++) a[i] = x[i]; + for (let i = 1; i < l; i++) a[i] = x[i]; return a; } @@ -80,7 +80,7 @@ function caml_obj_with_tag(tag, x) { function caml_obj_dup(x) { var l = x.length; var a = new Array(l); - for (var i = 0; i < l; i++) a[i] = x[i]; + for (let i = 0; i < l; i++) a[i] = x[i]; return a; } @@ -126,7 +126,7 @@ function caml_get_public_method(obj, tag, cacheid) { var ofs = caml_method_cache[cacheid]; if (ofs === undefined) { // Make sure the array is not sparse - for (var i = caml_method_cache.length; i < cacheid; i++) + for (let i = caml_method_cache.length; i < cacheid; i++) caml_method_cache[i] = 0; } else if (meths[ofs] === tag) { return meths[ofs - 1]; diff --git a/runtime/stdlib.js b/runtime/stdlib.js index 3e878681cf..957ec5407b 100644 --- a/runtime/stdlib.js +++ b/runtime/stdlib.js @@ -34,7 +34,7 @@ function caml_call_gen(f, args) { case 1: { var g = function (x) { var nargs = new Array(argsLen + 1); - for (var i = 0; i < argsLen; i++) nargs[i] = args[i]; + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; nargs[argsLen] = x; return f.apply(null, nargs); }; @@ -43,7 +43,7 @@ function caml_call_gen(f, args) { case 2: { var g = function (x, y) { var nargs = new Array(argsLen + 2); - for (var i = 0; i < argsLen; i++) nargs[i] = args[i]; + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; nargs[argsLen] = x; nargs[argsLen + 1] = y; return f.apply(null, nargs); @@ -54,8 +54,8 @@ function caml_call_gen(f, args) { 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++) + 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); }; @@ -93,7 +93,7 @@ function caml_call_gen(f, args) { case 1: { var g = function (x, y) { var nargs = new Array(argsLen + 2); - for (var i = 0; i < argsLen; i++) nargs[i] = args[i]; + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; nargs[argsLen] = x; nargs[argsLen + 1] = y; return f.apply(null, nargs); @@ -103,7 +103,7 @@ function caml_call_gen(f, args) { case 2: { var g = function (x, y, z) { var nargs = new Array(argsLen + 3); - for (var i = 0; i < argsLen; i++) nargs[i] = args[i]; + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; nargs[argsLen] = x; nargs[argsLen + 1] = y; nargs[argsLen + 2] = z; @@ -115,8 +115,8 @@ function caml_call_gen(f, args) { 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++) + 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); }; @@ -151,7 +151,7 @@ var caml_global_data = [0]; function caml_build_symbols(symb) { var r = {}; if (symb) { - for (var i = 1; i < symb.length; i++) { + for (let i = 1; i < symb.length; i++) { r[caml_jsstring_of_string(symb[i][1])] = symb[i][2]; } } diff --git a/runtime/stdlib_modern.js b/runtime/stdlib_modern.js index f942292d4f..fb4f789573 100644 --- a/runtime/stdlib_modern.js +++ b/runtime/stdlib_modern.js @@ -32,7 +32,7 @@ function caml_call_gen(f, args) { case 1: { var g = function (x) { var nargs = new Array(argsLen + 1); - for (var i = 0; i < argsLen; i++) nargs[i] = args[i]; + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; nargs[argsLen] = x; return f.apply(null, nargs); }; @@ -41,7 +41,7 @@ function caml_call_gen(f, args) { case 2: { var g = function (x, y) { var nargs = new Array(argsLen + 2); - for (var i = 0; i < argsLen; i++) nargs[i] = args[i]; + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; nargs[argsLen] = x; nargs[argsLen + 1] = y; return f.apply(null, nargs); @@ -52,8 +52,8 @@ function caml_call_gen(f, args) { 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++) + 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); }; @@ -89,7 +89,7 @@ function caml_call_gen(f, args) { case 1: { var g = function (x, y) { var nargs = new Array(argsLen + 2); - for (var i = 0; i < argsLen; i++) nargs[i] = args[i]; + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; nargs[argsLen] = x; nargs[argsLen + 1] = y; return f.apply(null, nargs); @@ -99,7 +99,7 @@ function caml_call_gen(f, args) { case 2: { var g = function (x, y, z) { var nargs = new Array(argsLen + 3); - for (var i = 0; i < argsLen; i++) nargs[i] = args[i]; + for (let i = 0; i < argsLen; i++) nargs[i] = args[i]; nargs[argsLen] = x; nargs[argsLen + 1] = y; nargs[argsLen + 2] = z; @@ -111,8 +111,8 @@ function caml_call_gen(f, args) { 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++) + 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); }; diff --git a/runtime/str.js b/runtime/str.js index a58190b805..1ec09c5929 100644 --- a/runtime/str.js +++ b/runtime/str.js @@ -81,7 +81,7 @@ var re_match = (function () { groups = new Array(numgroups), re_register = new Array(numregisters); - for (var i = 0; i < groups.length; i++) { + for (let i = 0; i < groups.length; i++) { groups[i] = { start: -1, end: -1 }; } groups[0].start = pos; @@ -108,7 +108,7 @@ var re_match = (function () { groups[0].end = pos; var result = new Array(1 + groups.length * 2); result[0] = 0; // tag - for (var i = 0; i < groups.length; i++) { + for (let i = 0; i < groups.length; i++) { var g = groups[i]; if (g.start < 0 || g.end < 0) { g.start = g.end = -1; @@ -235,7 +235,7 @@ var re_match = (function () { backtrack(); break; } - for (var i = group.start; i < group.end; i++) { + for (let i = group.start; i < group.end; i++) { if (pos === s.length) { prefix_match(); break; diff --git a/runtime/sys.js b/runtime/sys.js index 9e596f1781..a7044d1cc8 100644 --- a/runtime/sys.js +++ b/runtime/sys.js @@ -63,7 +63,7 @@ function caml_format_exception(exn) { var bucket = exn; } r += "("; - for (var i = start; i < bucket.length; i++) { + for (let i = start; i < bucket.length; i++) { if (i > start) r += ", "; var v = bucket[i]; if (typeof v == "number") r += v.toString(); @@ -148,7 +148,7 @@ var caml_argv = (function () { var p = caml_string_of_jsstring(main); var args2 = [0, p]; - for (var i = 0; i < args.length; i++) + for (let i = 0; i < args.length; i++) args2.push(caml_string_of_jsstring(args[i])); return args2; })(); diff --git a/runtime/toplevel.js b/runtime/toplevel.js index 385b00b80f..7487bd35bf 100644 --- a/runtime/toplevel.js +++ b/runtime/toplevel.js @@ -109,13 +109,13 @@ function caml_reify_bytecode(code, debug, _digest) { if (globalThis.toplevelCompile) { var len = 0; var all = []; - for (var i = 1; i < code.length; i++) { + for (let i = 1; i < code.length; i++) { var a = caml_uint8_array_of_bytes(code[i]); all.push(a); len += a.length; } code = new Uint8Array(len); - for (var i = 0, len = 0; i < all.length; i++) { + for (let i = 0, len = 0; i < all.length; i++) { code.set(all[i], len); len += all[i].length; } diff --git a/runtime/weak.js b/runtime/weak.js index ad5769abd4..607a6fc61e 100644 --- a/runtime/weak.js +++ b/runtime/weak.js @@ -50,7 +50,7 @@ function caml_ephe_unset_key(x, i) { var old = x[caml_ephe_key_offset + i].deref(); if (old !== undefined) { var count = 0; - for (var j = caml_ephe_key_offset; j < x.length; j++) { + for (let j = caml_ephe_key_offset; j < x.length; j++) { var key = x[j]; if (key instanceof globalThis.WeakRef) { key = key.deref(); @@ -173,7 +173,7 @@ function caml_ephe_set_data(x, data) { caml_ephe_unset_data(x); }); //register all keys - for (var j = caml_ephe_key_offset; j < x.length; j++) { + for (let j = caml_ephe_key_offset; j < x.length; j++) { var key = x[j]; if (key instanceof globalThis.WeakRef) { key = key.deref(); @@ -192,7 +192,7 @@ function caml_ephe_unset_data(x) { if (globalThis.FinalizationRegistry && globalThis.WeakRef) { if (x[1] instanceof globalThis.FinalizationRegistry) { //unregister all keys - for (var j = caml_ephe_key_offset; j < x.length; j++) { + for (let j = caml_ephe_key_offset; j < x.length; j++) { var key = x[j]; if (key instanceof globalThis.WeakRef) { key = key.deref(); diff --git a/runtime/zstd.js b/runtime/zstd.js index 3a100332bb..2c11b436a5 100644 --- a/runtime/zstd.js +++ b/runtime/zstd.js @@ -182,7 +182,7 @@ var zstd_decompress = (function () { var sstep = (sz >> 1) + (sz >> 3) + 3; // sym mask var smask = sz - 1; - for (var s = 0; s <= sym; ++s) { + for (let s = 0; s <= sym; ++s) { var sf = freq[s]; if (sf < 1) { dstate[s] = -sf; @@ -354,7 +354,7 @@ var zstd_decompress = (function () { var b2bl = function (b, s) { var len = b.length, bl = new i32(len); - for (var i = 0; i < len; ++i) { + for (let i = 0; i < len; ++i) { bl[i] = s; s += 1 << b[i]; } @@ -509,7 +509,7 @@ var zstd_decompress = (function () { var scm = dat[bt++]; if (scm & 3) err(0); var dts = [dmlt, doct, dllt]; - for (var i = 2; i > -1; --i) { + for (let i = 2; i > -1; --i) { var md = (scm >> ((i << 1) + 2)) & 3; if (md == 1) { // rle buf @@ -602,7 +602,7 @@ var zstd_decompress = (function () { st.o[0] = off; } else off = st.o[0]; } - for (var i = 0; i < ll; ++i) { + for (let i = 0; i < ll; ++i) { buf[oubt + i] = buf[spl + i]; } (oubt += ll), (spl += ll); @@ -611,12 +611,12 @@ var zstd_decompress = (function () { var len = -stin; var bs = st.e + stin; if (len > ml) len = ml; - for (var i = 0; i < len; ++i) { + for (let i = 0; i < len; ++i) { buf[oubt + i] = st.w[bs + i]; } (oubt += len), (ml -= len), (stin = 0); } - for (var i = 0; i < ml; ++i) { + for (let i = 0; i < ml; ++i) { buf[oubt + i] = buf[stin + i]; } oubt += ml; @@ -632,7 +632,7 @@ var zstd_decompress = (function () { if (out) { st.y += lss; if (spl) { - for (var i = 0; i < lss; ++i) { + for (let i = 0; i < lss; ++i) { buf[i] = buf[spl + i]; } } @@ -647,7 +647,7 @@ var zstd_decompress = (function () { var cct = function (bufs, ol) { if (bufs.length == 1) return bufs[0]; var buf = new u8(ol); - for (var i = 0, b = 0; i < bufs.length; ++i) { + for (let i = 0, b = 0; i < bufs.length; ++i) { var chk = bufs[i]; buf.set(chk, b); b += chk.length; From 6ebf3f845ae78de9542156ef73d87d68cc9aa35a Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Sat, 14 Sep 2024 14:03:20 +0900 Subject: [PATCH 03/12] runtime: use const/let instead of var Signed-off-by: Sora Morimoto --- runtime/array.js | 36 ++-- runtime/backtrace.js | 15 +- runtime/bigarray.js | 210 ++++++++++---------- runtime/bigstring.js | 26 +-- runtime/blake2.js | 14 +- runtime/compare.js | 38 ++-- runtime/domain.js | 26 +-- runtime/dynlink.js | 18 +- runtime/effect.js | 22 +- runtime/format.js | 10 +- runtime/fs.js | 115 +++++------ runtime/fs_fake.js | 80 ++++---- runtime/fs_node.js | 37 ++-- runtime/gc.js | 4 +- runtime/graphics.js | 170 ++++++++-------- runtime/hash.js | 44 ++-- runtime/ieee_754.js | 210 ++++++++++---------- runtime/int64.js | 86 ++++---- runtime/internalMod.js | 8 +- runtime/ints.js | 38 ++-- runtime/io.js | 147 +++++++------- runtime/jslib.js | 91 ++++----- runtime/jslib_js_of_ocaml.js | 15 +- runtime/lexing.js | 104 +++++----- runtime/marshal.js | 311 +++++++++++++++-------------- runtime/md5.js | 40 ++-- runtime/mlBytes.js | 76 +++---- runtime/nat.js | 79 ++++---- runtime/obj.js | 26 +-- runtime/parsing.js | 131 ++++++------ runtime/prng.js | 21 +- runtime/runtime_events.js | 2 +- runtime/stdlib.js | 62 +++--- runtime/stdlib_modern.js | 52 ++--- runtime/str.js | 126 ++++++------ runtime/sys.js | 66 +++--- runtime/toplevel.js | 18 +- runtime/unix.js | 77 +++---- runtime/weak.js | 30 +-- runtime/zstd.js | 375 +++++++++++++++++------------------ 40 files changed, 1538 insertions(+), 1518 deletions(-) diff --git a/runtime/array.js b/runtime/array.js index c23114c412..23b329eb50 100644 --- a/runtime/array.js +++ b/runtime/array.js @@ -19,7 +19,7 @@ //Provides: caml_array_sub mutable function caml_array_sub(a, i, len) { - var a2 = new Array(len + 1); + const a2 = new Array(len + 1); a2[0] = 0; for (let i2 = 1, i1 = i + 1; i2 <= len; i2++, i1++) { a2[i2] = a[i1]; @@ -29,13 +29,13 @@ function caml_array_sub(a, i, len) { //Provides: caml_array_append mutable function caml_array_append(a1, a2) { - var l1 = a1.length, - l2 = a2.length; - var l = l1 + l2 - 1; - var a = new Array(l); + const l1 = a1.length; + const l2 = a2.length; + const l = l1 + l2 - 1; + const a = new Array(l); a[0] = 0; - var i = 1, - j = 1; + 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; @@ -43,9 +43,9 @@ function caml_array_append(a1, a2) { //Provides: caml_array_concat mutable function caml_array_concat(l) { - var a = [0]; + const a = [0]; while (l !== 0) { - var b = l[1]; + const b = l[1]; for (let i = 1; i < b.length; i++) a.push(b[i]); l = l[2]; } @@ -107,10 +107,10 @@ function caml_check_bound(array, index) { //Requires: caml_array_bound_error function caml_make_vect(len, init) { if (len < 0) caml_array_bound_error(); - var len = (len + 1) | 0; - var b = new Array(len); + const len_ = (len + 1) | 0; + const b = new Array(len_); b[0] = 0; - for (let i = 1; i < len; i++) b[i] = init; + for (let i = 1; i < len_; i++) b[i] = init; return b; } @@ -118,19 +118,19 @@ function caml_make_vect(len, init) { //Requires: caml_array_bound_error function caml_make_float_vect(len) { if (len < 0) caml_array_bound_error(); - var len = (len + 1) | 0; - var b = new Array(len); + const len_ = (len + 1) | 0; + const b = new Array(len); b[0] = 254; - for (let i = 1; i < len; i++) b[i] = 0; + for (let i = 1; i < len_; i++) b[i] = 0; return b; } //Provides: caml_floatarray_create const (const) //Requires: caml_array_bound_error function caml_floatarray_create(len) { if (len < 0) caml_array_bound_error(); - var len = (len + 1) | 0; - var b = new Array(len); + const len_ = (len + 1) | 0; + const b = new Array(len); b[0] = 254; - for (let i = 1; i < len; i++) b[i] = 0; + for (let i = 1; i < len_; i++) b[i] = 0; return b; } diff --git a/runtime/backtrace.js b/runtime/backtrace.js index 1a90355f7e..f2895262cb 100644 --- a/runtime/backtrace.js +++ b/runtime/backtrace.js @@ -17,26 +17,27 @@ //Provides: caml_record_backtrace_env_flag //Requires: jsoo_sys_getenv -var caml_record_backtrace_env_flag = FLAG("with-js-error"); +let caml_record_backtrace_env_flag = FLAG("with-js-error"); -(function () { - var r = jsoo_sys_getenv("OCAMLRUNPARAM"); +(() => { + const r = jsoo_sys_getenv("OCAMLRUNPARAM"); if (r !== undefined) { - var l = r.split(","); + const l = r.split(","); for (let i = 0; i < l.length; i++) { if (l[i] == "b") { caml_record_backtrace_env_flag = 1; break; - } else if (l[i].startsWith("b=")) { + } + if (l[i].startsWith("b=")) { caml_record_backtrace_env_flag = +l[i].slice(2); - } else continue; + } } } })(); //Provides: caml_record_backtrace_runtime_flag //Requires: caml_record_backtrace_env_flag -var caml_record_backtrace_runtime_flag = caml_record_backtrace_env_flag; +let caml_record_backtrace_runtime_flag = caml_record_backtrace_env_flag; //Provides: caml_ml_debug_info_status const function caml_ml_debug_info_status() { diff --git a/runtime/bigarray.js b/runtime/bigarray.js index 904d4a34d9..d1d7f32939 100644 --- a/runtime/bigarray.js +++ b/runtime/bigarray.js @@ -32,8 +32,8 @@ function caml_ba_init() { //Provides: caml_ba_get_size //Requires: caml_invalid_argument function caml_ba_get_size(dims) { - var n_dims = dims.length; - var size = 1; + 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"); @@ -58,7 +58,7 @@ function caml_ba_get_size_per_element(kind) { //Requires: caml_ba_get_size_per_element //Requires: caml_invalid_argument function caml_ba_create_buffer(kind, size) { - var view; + let view; switch (kind) { case 0: view = Float32Array; @@ -101,17 +101,17 @@ function caml_ba_create_buffer(kind, size) { break; } if (!view) caml_invalid_argument("Bigarray.create: unsupported kind"); - var data = new view(size * caml_ba_get_size_per_element(kind)); + const data = new view(size * caml_ba_get_size_per_element(kind)); return data; } //Provides: caml_ba_custom_name //Version: < 4.11 -var caml_ba_custom_name = "_bigarray"; +const caml_ba_custom_name = "_bigarray"; //Provides: caml_ba_custom_name //Version: >= 4.11 -var caml_ba_custom_name = "_bigarr02"; +const caml_ba_custom_name = "_bigarr02"; //Provides: Ml_Bigarray //Requires: caml_array_bound_error, caml_invalid_argument, caml_ba_custom_name @@ -126,7 +126,7 @@ function Ml_Bigarray(kind, layout, dims, buffer) { Ml_Bigarray.prototype.caml_custom = caml_ba_custom_name; Ml_Bigarray.prototype.offset = function (arg) { - var ofs = 0; + let ofs = 0; if (typeof arg === "number") arg = [arg]; if (!Array.isArray(arg)) caml_invalid_argument("bigarray.js: invalid offset"); if (this.dims.length != arg.length) @@ -151,15 +151,15 @@ Ml_Bigarray.prototype.get = function (ofs) { switch (this.kind) { case 7: { // Int64 - var l = this.data[ofs * 2 + 0]; - var h = this.data[ofs * 2 + 1]; + const l = this.data[ofs * 2 + 0]; + const h = this.data[ofs * 2 + 1]; return caml_int64_create_lo_hi(l, h); } case 10: case 11: { // Complex32, Complex64 - var r = this.data[ofs * 2 + 0]; - var i = this.data[ofs * 2 + 1]; + const r = this.data[ofs * 2 + 0]; + const i = this.data[ofs * 2 + 1]; return [254, r, i]; } default: @@ -191,8 +191,8 @@ Ml_Bigarray.prototype.fill = function (v) { switch (this.kind) { case 7: { // Int64 - var a = caml_int64_lo32(v); - var b = caml_int64_hi32(v); + const a = caml_int64_lo32(v); + const b = caml_int64_hi32(v); if (a == b) { this.data.fill(a); } else { @@ -205,8 +205,8 @@ Ml_Bigarray.prototype.fill = function (v) { case 10: case 11: { // Complex32, Complex64 - var im = v[1]; - var re = v[2]; + const im = v[1]; + const re = v[2]; if (im == re) { this.data.fill(im); } else { @@ -224,8 +224,8 @@ Ml_Bigarray.prototype.fill = function (v) { Ml_Bigarray.prototype.compare = function (b, total) { if (this.layout != b.layout || this.kind != b.kind) { - var k1 = this.kind | (this.layout << 8); - var k2 = b.kind | (b.layout << 8); + const k1 = this.kind | (this.layout << 8); + const k2 = b.kind | (b.layout << 8); return k2 - k1; } if (this.dims.length != b.dims.length) { @@ -239,14 +239,15 @@ Ml_Bigarray.prototype.compare = function (b, total) { case 10: case 11: { // Floats - var x, y; + let x; + let y; for (let i = 0; i < this.data.length; i++) { x = this.data[i]; y = b.data[i]; if (x < y) return -1; if (x > y) return 1; if (x != y) { - if (!total) return NaN; + if (!total) return Number.NaN; if (x == x) return 1; if (y == y) return -1; } @@ -322,7 +323,7 @@ function caml_ba_compare(a, b, total) { //Requires: Ml_Bigarray, Ml_Bigarray_c_1_1, caml_ba_get_size, caml_ba_get_size_per_element //Requires: caml_invalid_argument function caml_ba_create_unsafe(kind, layout, dims, data) { - var size_per_element = caml_ba_get_size_per_element(kind); + const size_per_element = caml_ba_get_size_per_element(kind); if (caml_ba_get_size(dims) * size_per_element != data.length) { caml_invalid_argument("length doesn't match dims"); } @@ -341,8 +342,8 @@ function caml_ba_create_unsafe(kind, layout, dims, data) { //Requires: caml_ba_get_size, caml_ba_create_unsafe //Requires: caml_ba_create_buffer function caml_ba_create(kind, layout, dims_ml) { - var dims = caml_js_from_array(dims_ml); - var data = caml_ba_create_buffer(kind, caml_ba_get_size(dims)); + const dims = caml_js_from_array(dims_ml); + const data = caml_ba_create_buffer(kind, caml_ba_get_size(dims)); return caml_ba_create_unsafe(kind, layout, dims, data); } @@ -350,7 +351,7 @@ function caml_ba_create(kind, layout, dims_ml) { //Requires: caml_ba_create_unsafe function caml_ba_change_layout(ba, layout) { if (ba.layout == layout) return ba; - var new_dims = []; + const new_dims = []; for (let i = 0; i < ba.dims.length; i++) new_dims[i] = ba.dims[ba.dims.length - i - 1]; return caml_ba_create_unsafe(ba.kind, layout, new_dims, ba.data); @@ -399,45 +400,45 @@ function caml_ba_dim_3(ba) { //Provides: caml_ba_get_generic //Requires: caml_js_from_array function caml_ba_get_generic(ba, i) { - var ofs = ba.offset(caml_js_from_array(i)); + const ofs = ba.offset(caml_js_from_array(i)); return ba.get(ofs); } //Provides: caml_ba_uint8_get16 //Requires: caml_array_bound_error function caml_ba_uint8_get16(ba, i0) { - var ofs = ba.offset(i0); + const ofs = ba.offset(i0); if (ofs + 1 >= ba.data.length) caml_array_bound_error(); - var b1 = ba.get(ofs); - var b2 = ba.get(ofs + 1); + const b1 = ba.get(ofs); + const b2 = ba.get(ofs + 1); return b1 | (b2 << 8); } //Provides: caml_ba_uint8_get32 //Requires: caml_array_bound_error function caml_ba_uint8_get32(ba, i0) { - var ofs = ba.offset(i0); + const ofs = ba.offset(i0); if (ofs + 3 >= ba.data.length) caml_array_bound_error(); - var b1 = ba.get(ofs + 0); - var b2 = ba.get(ofs + 1); - var b3 = ba.get(ofs + 2); - var b4 = ba.get(ofs + 3); + const b1 = ba.get(ofs + 0); + const b2 = ba.get(ofs + 1); + const b3 = ba.get(ofs + 2); + const b4 = ba.get(ofs + 3); return (b1 << 0) | (b2 << 8) | (b3 << 16) | (b4 << 24); } //Provides: caml_ba_uint8_get64 //Requires: caml_array_bound_error, caml_int64_of_bytes function caml_ba_uint8_get64(ba, i0) { - var ofs = ba.offset(i0); + const ofs = ba.offset(i0); if (ofs + 7 >= ba.data.length) caml_array_bound_error(); - var b1 = ba.get(ofs + 0); - var b2 = ba.get(ofs + 1); - var b3 = ba.get(ofs + 2); - var b4 = ba.get(ofs + 3); - var b5 = ba.get(ofs + 4); - var b6 = ba.get(ofs + 5); - var b7 = ba.get(ofs + 6); - var b8 = ba.get(ofs + 7); + const b1 = ba.get(ofs + 0); + const b2 = ba.get(ofs + 1); + const b3 = ba.get(ofs + 2); + const b4 = ba.get(ofs + 3); + const b5 = ba.get(ofs + 4); + const b6 = ba.get(ofs + 5); + const b7 = ba.get(ofs + 6); + const b8 = ba.get(ofs + 7); return caml_int64_of_bytes([b8, b7, b6, b5, b4, b3, b2, b1]); } @@ -466,7 +467,7 @@ function caml_ba_set_generic(ba, i, v) { //Provides: caml_ba_uint8_set16 //Requires: caml_array_bound_error function caml_ba_uint8_set16(ba, i0, v) { - var ofs = ba.offset(i0); + const ofs = ba.offset(i0); if (ofs + 1 >= ba.data.length) caml_array_bound_error(); ba.set(ofs + 0, v & 0xff); ba.set(ofs + 1, (v >>> 8) & 0xff); @@ -476,7 +477,7 @@ function caml_ba_uint8_set16(ba, i0, v) { //Provides: caml_ba_uint8_set32 //Requires: caml_array_bound_error function caml_ba_uint8_set32(ba, i0, v) { - var ofs = ba.offset(i0); + const ofs = ba.offset(i0); if (ofs + 3 >= ba.data.length) caml_array_bound_error(); ba.set(ofs + 0, v & 0xff); ba.set(ofs + 1, (v >>> 8) & 0xff); @@ -488,10 +489,10 @@ function caml_ba_uint8_set32(ba, i0, v) { //Provides: caml_ba_uint8_set64 //Requires: caml_array_bound_error, caml_int64_to_bytes function caml_ba_uint8_set64(ba, i0, v) { - var ofs = ba.offset(i0); + const ofs = ba.offset(i0); if (ofs + 7 >= ba.data.length) caml_array_bound_error(); - var v = caml_int64_to_bytes(v); - for (let i = 0; i < 8; i++) ba.set(ofs + i, v[7 - i]); + const v_ = caml_int64_to_bytes(v); + for (let i = 0; i < 8; i++) ba.set(ofs + i, v_[7 - i]); return 0; } @@ -535,8 +536,8 @@ function caml_ba_blit(src, dst) { //Requires: caml_invalid_argument, caml_ba_create_unsafe, caml_ba_get_size //Requires: caml_ba_get_size_per_element function caml_ba_sub(ba, ofs, len) { - var changed_dim; - var mul = 1; + let changed_dim; + let mul = 1; if (ba.layout == 0) { for (let i = 1; i < ba.dims.length; i++) mul = mul * ba.dims[i]; changed_dim = 0; @@ -548,11 +549,11 @@ function caml_ba_sub(ba, ofs, len) { if (ofs < 0 || len < 0 || ofs + len > ba.dims[changed_dim]) { caml_invalid_argument("Bigarray.sub: bad sub-array"); } - var new_dims = []; + const new_dims = []; for (let i = 0; i < ba.dims.length; i++) new_dims[i] = ba.dims[i]; new_dims[changed_dim] = len; mul *= caml_ba_get_size_per_element(ba.kind); - var new_data = ba.data.subarray(ofs * mul, (ofs + len) * mul); + const new_data = ba.data.subarray(ofs * mul, (ofs + len) * mul); return caml_ba_create_unsafe(ba.kind, ba.layout, new_dims, new_data); } @@ -561,10 +562,9 @@ function caml_ba_sub(ba, ofs, len) { //Requires: caml_ba_get_size_per_element function caml_ba_slice(ba, vind) { vind = caml_js_from_array(vind); - var num_inds = vind.length; - var index = []; - var sub_dims = []; - var ofs; + const num_inds = vind.length; + const index = []; + let sub_dims = []; if (num_inds > ba.dims.length) caml_invalid_argument("Bigarray.slice: too many indices"); @@ -581,10 +581,10 @@ function caml_ba_slice(ba, vind) { for (let i = 0; i < ba.dims.length - num_inds; i++) index[i] = 1; sub_dims = ba.dims.slice(0, ba.dims.length - num_inds); } - ofs = ba.offset(index); - var size = caml_ba_get_size(sub_dims); - var size_per_element = caml_ba_get_size_per_element(ba.kind); - var new_data = ba.data.subarray( + const ofs = ba.offset(index); + const size = caml_ba_get_size(sub_dims); + const size_per_element = caml_ba_get_size_per_element(ba.kind); + const new_data = ba.data.subarray( ofs * size_per_element, (ofs + size) * size_per_element, ); @@ -595,13 +595,13 @@ function caml_ba_slice(ba, vind) { //Requires: caml_js_from_array, caml_invalid_argument, caml_ba_create_unsafe, caml_ba_get_size function caml_ba_reshape(ba, vind) { vind = caml_js_from_array(vind); - var new_dim = []; - var num_dims = vind.length; + const new_dim = []; + const num_dims = vind.length; if (num_dims < 0 || num_dims > 16) { caml_invalid_argument("Bigarray.reshape: bad number of dimensions"); } - var num_elts = 1; + let num_elts = 1; for (let i = 0; i < num_dims; i++) { new_dim[i] = vind[i]; if (new_dim[i] < 0) @@ -609,7 +609,7 @@ function caml_ba_reshape(ba, vind) { num_elts = num_elts * new_dim[i]; } - var size = caml_ba_get_size(ba.dims); + const size = caml_ba_get_size(ba.dims); // Check that sizes agree if (num_elts != size) caml_invalid_argument("Bigarray.reshape: size mismatch"); @@ -660,36 +660,40 @@ function caml_ba_serialize(writer, ba, sz) { break; case 7: // Int32Array (int64) for (let i = 0; i < ba.data.length / 2; i++) { - var b = caml_int64_to_bytes(ba.get(i)); + const b = caml_int64_to_bytes(ba.get(i)); for (let j = 0; j < 8; j++) writer.write(8, b[j]); } break; case 1: // Float64Array for (let i = 0; i < ba.data.length; i++) { - var b = caml_int64_to_bytes(caml_int64_bits_of_float(ba.get(i))); + const b = caml_int64_to_bytes(caml_int64_bits_of_float(ba.get(i))); for (let j = 0; j < 8; j++) writer.write(8, b[j]); } break; case 0: // Float32Array for (let i = 0; i < ba.data.length; i++) { - var b = caml_int32_bits_of_float(ba.get(i)); + const b = caml_int32_bits_of_float(ba.get(i)); writer.write(32, b); } break; case 10: // Float32Array (complex32) for (let i = 0; i < ba.data.length / 2; i++) { - var j = ba.get(i); + const j = ba.get(i); writer.write(32, caml_int32_bits_of_float(j[1])); writer.write(32, caml_int32_bits_of_float(j[2])); } break; case 11: // Float64Array (complex64) for (let i = 0; i < ba.data.length / 2; i++) { - var complex = ba.get(i); - var b = caml_int64_to_bytes(caml_int64_bits_of_float(complex[1])); - for (let j = 0; j < 8; j++) writer.write(8, b[j]); - var b = caml_int64_to_bytes(caml_int64_bits_of_float(complex[2])); - for (let j = 0; j < 8; j++) writer.write(8, b[j]); + const complex = ba.get(i); + for (let j = 0; j < 8; j++) { + const b = caml_int64_to_bytes(caml_int64_bits_of_float(complex[1])); + writer.write(8, b[j]); + } + for (let j = 0; j < 8; j++) { + const b = caml_int64_to_bytes(caml_int64_bits_of_float(complex[2])); + writer.write(8, b[j]); + } } break; } @@ -704,19 +708,19 @@ function caml_ba_serialize(writer, ba, sz) { //Requires: caml_int32_float_of_bits //Requires: caml_ba_create_buffer function caml_ba_deserialize(reader, sz, name) { - var num_dims = reader.read32s(); + const num_dims = reader.read32s(); if (num_dims < 0 || num_dims > 16) caml_failwith("input_value: wrong number of bigarray dimensions"); - var tag = reader.read32s(); - var kind = tag & 0xff; - var layout = (tag >> 8) & 1; - var dims = []; + const tag = reader.read32s(); + const kind = tag & 0xff; + const layout = (tag >> 8) & 1; + const dims = []; if (name == "_bigarr02") for (let i = 0; i < num_dims; i++) { - var size_dim = reader.read16u(); + let size_dim = reader.read16u(); if (size_dim == 0xffff) { - var size_dim_hi = reader.read32u(); - var size_dim_lo = reader.read32u(); + const size_dim_hi = reader.read32u(); + const size_dim_lo = reader.read32u(); if (size_dim_hi != 0) caml_failwith("input_value: bigarray dimension overflow in 32bit"); size_dim = size_dim_lo; @@ -724,9 +728,9 @@ function caml_ba_deserialize(reader, sz, name) { dims.push(size_dim); } else for (let i = 0; i < num_dims; i++) dims.push(reader.read32u()); - var size = caml_ba_get_size(dims); - var data = caml_ba_create_buffer(kind, size); - var ba = caml_ba_create_unsafe(kind, layout, dims, data); + const size = caml_ba_get_size(dims); + const data = caml_ba_create_buffer(kind, size); + const ba = caml_ba_create_unsafe(kind, layout, dims, data); switch (kind) { case 2: //Int8Array for (let i = 0; i < size; i++) { @@ -757,7 +761,7 @@ function caml_ba_deserialize(reader, sz, name) { case 8: // Int32Array (int) case 9: { // Int32Array (nativeint) - var sixty = reader.read8u(); + const sixty = reader.read8u(); if (sixty) caml_failwith( "input_value: cannot read bigarray with 64-bit OCaml ints", @@ -769,45 +773,45 @@ function caml_ba_deserialize(reader, sz, name) { } case 7: { // (int64) - var t = new Array(8); + const t = new Array(8); for (let i = 0; i < size; i++) { for (let j = 0; j < 8; j++) t[j] = reader.read8u(); - var int64 = caml_int64_of_bytes(t); + const int64 = caml_int64_of_bytes(t); ba.set(i, int64); } break; } case 1: { // Float64Array - var t = new Array(8); + const t = new Array(8); for (let i = 0; i < size; i++) { for (let j = 0; j < 8; j++) t[j] = reader.read8u(); - var f = caml_int64_float_of_bits(caml_int64_of_bytes(t)); + const f = caml_int64_float_of_bits(caml_int64_of_bytes(t)); ba.set(i, f); } break; } case 0: // Float32Array for (let i = 0; i < size; i++) { - var f = caml_int32_float_of_bits(reader.read32s()); + const f = caml_int32_float_of_bits(reader.read32s()); ba.set(i, f); } break; case 10: // Float32Array (complex32) for (let i = 0; i < size; i++) { - var re = caml_int32_float_of_bits(reader.read32s()); - var im = caml_int32_float_of_bits(reader.read32s()); + const re = caml_int32_float_of_bits(reader.read32s()); + const im = caml_int32_float_of_bits(reader.read32s()); ba.set(i, [254, re, im]); } break; case 11: { // Float64Array (complex64) - var t = new Array(8); + const t = new Array(8); for (let i = 0; i < size; i++) { for (let j = 0; j < 8; j++) t[j] = reader.read8u(); - var re = caml_int64_float_of_bits(caml_int64_of_bytes(t)); + const re = caml_int64_float_of_bits(caml_int64_of_bytes(t)); for (let j = 0; j < 8; j++) t[j] = reader.read8u(); - var im = caml_int64_float_of_bits(caml_int64_of_bytes(t)); + const im = caml_int64_float_of_bits(caml_int64_of_bytes(t)); ba.set(i, [254, re, im]); } break; @@ -832,16 +836,16 @@ function caml_ba_create_from(data1, data2, jstyp, kind, layout, dims) { //Provides: caml_ba_hash const //Requires: caml_ba_get_size, caml_hash_mix_int, caml_hash_mix_float function caml_ba_hash(ba) { - var num_elts = caml_ba_get_size(ba.dims); - var h = 0; + let num_elts = caml_ba_get_size(ba.dims); + let h = 0; switch (ba.kind) { case 2: //Int8Array case 3: //Uint8Array case 12: { //Uint8Array if (num_elts > 256) num_elts = 256; - var w = 0, - i = 0; + let w = 0; + let i = 0; for (i = 0; i + 4 <= ba.data.length; i += 4) { w = ba.data[i + 0] | @@ -866,8 +870,8 @@ function caml_ba_hash(ba) { case 5: { // Uint16Array if (num_elts > 128) num_elts = 128; - var w = 0, - i = 0; + let w = 0; + let i = 0; for (i = 0; i + 2 <= ba.data.length; i += 2) { w = ba.data[i + 0] | (ba.data[i + 1] << 16); h = caml_hash_mix_int(h, w); @@ -915,7 +919,7 @@ function caml_ba_to_typed_array(ba) { //Provides: caml_ba_kind_of_typed_array mutable //Requires: caml_invalid_argument function caml_ba_kind_of_typed_array(ta) { - var kind; + let kind; if (ta instanceof Float32Array) kind = 0; else if (ta instanceof Float64Array) kind = 1; else if (ta instanceof Int8Array) kind = 2; @@ -933,12 +937,12 @@ function caml_ba_kind_of_typed_array(ta) { //Requires: caml_ba_kind_of_typed_array //Requires: caml_ba_create_unsafe function caml_ba_from_typed_array(ta) { - var kind = caml_ba_kind_of_typed_array(ta); - var ta = + const kind = caml_ba_kind_of_typed_array(ta); + const ta_ = /* Needed to avoid unsigned setters overflowing the range of OCaml [int32] values. */ ta instanceof Uint32Array ? new Int32Array(ta.buffer, ta.byteOffset, ta.length) : ta; - return caml_ba_create_unsafe(kind, 0, [ta.length], ta); + return caml_ba_create_unsafe(kind, 0, [ta_.length], ta_); } diff --git a/runtime/bigstring.js b/runtime/bigstring.js index a54d70679f..a2c2b901fe 100644 --- a/runtime/bigstring.js +++ b/runtime/bigstring.js @@ -19,14 +19,14 @@ function bigstring_to_typed_array(bs) { //Provides: bigstring_of_array_buffer mutable //Requires: caml_ba_create_unsafe function bigstring_of_array_buffer(ab) { - var ta = new Uint8Array(ab); + const ta = new Uint8Array(ab); return caml_ba_create_unsafe(12, 0, [ta.length], ta); } //Provides: bigstring_of_typed_array mutable //Requires: caml_ba_create_unsafe function bigstring_of_typed_array(ba) { - var ta = new Uint8Array( + const ta = new Uint8Array( ba.buffer, ba.byteOffset, ba.length * ba.BYTES_PER_ELEMENT, @@ -38,8 +38,8 @@ function bigstring_of_typed_array(ba) { //Requires: caml_ba_get_1 function caml_bigstring_memcmp(s1, pos1, s2, pos2, len) { for (let i = 0; i < len; i++) { - var a = caml_ba_get_1(s1, pos1 + i); - var b = caml_ba_get_1(s2, pos2 + i); + const a = caml_ba_get_1(s1, pos1 + i); + const b = caml_ba_get_1(s2, pos2 + i); if (a < b) return -1; if (a > b) return 1; } @@ -54,15 +54,15 @@ function caml_bigstring_blit_ba_to_ba(ba1, pos1, ba2, pos2, len) { if (12 != ba2.kind) caml_invalid_argument("caml_bigstring_blit_ba_to_ba: kind mismatch"); if (len == 0) return 0; - var ofs1 = ba1.offset(pos1); - var ofs2 = ba2.offset(pos2); + const ofs1 = ba1.offset(pos1); + const ofs2 = ba2.offset(pos2); if (ofs1 + len > ba1.data.length) { caml_array_bound_error(); } if (ofs2 + len > ba2.data.length) { caml_array_bound_error(); } - var slice = ba1.data.subarray(ofs1, ofs1 + len); + const slice = ba1.data.subarray(ofs1, ofs1 + len); ba2.data.set(slice, pos2); return 0; } @@ -74,14 +74,14 @@ function caml_bigstring_blit_string_to_ba(str1, pos1, ba2, pos2, len) { if (12 != ba2.kind) caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch"); if (len == 0) return 0; - var ofs2 = ba2.offset(pos2); + const ofs2 = ba2.offset(pos2); if (pos1 + len > caml_ml_string_length(str1)) { caml_array_bound_error(); } if (ofs2 + len > ba2.data.length) { caml_array_bound_error(); } - var slice = caml_uint8_array_of_string(str1).slice(pos1, pos1 + len); + const slice = caml_uint8_array_of_string(str1).slice(pos1, pos1 + len); ba2.data.set(slice, ofs2); return 0; } @@ -93,14 +93,14 @@ function caml_bigstring_blit_bytes_to_ba(str1, pos1, ba2, pos2, len) { if (12 != ba2.kind) caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch"); if (len == 0) return 0; - var ofs2 = ba2.offset(pos2); + const ofs2 = ba2.offset(pos2); if (pos1 + len > caml_ml_bytes_length(str1)) { caml_array_bound_error(); } if (ofs2 + len > ba2.data.length) { caml_array_bound_error(); } - var slice = caml_uint8_array_of_bytes(str1).slice(pos1, pos1 + len); + const slice = caml_uint8_array_of_bytes(str1).slice(pos1, pos1 + len); ba2.data.set(slice, ofs2); return 0; } @@ -113,14 +113,14 @@ function caml_bigstring_blit_ba_to_bytes(ba1, pos1, bytes2, pos2, len) { if (12 != ba1.kind) caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch"); if (len == 0) return 0; - var ofs1 = ba1.offset(pos1); + const ofs1 = ba1.offset(pos1); if (ofs1 + len > ba1.data.length) { caml_array_bound_error(); } if (pos2 + len > caml_ml_bytes_length(bytes2)) { caml_array_bound_error(); } - var slice = ba1.data.slice(ofs1, ofs1 + len); + const slice = ba1.data.slice(ofs1, ofs1 + len); caml_blit_bytes(caml_bytes_of_array(slice), 0, bytes2, pos2, len); return 0; } diff --git a/runtime/blake2.js b/runtime/blake2.js index ca17a471b0..2203294e6e 100644 --- a/runtime/blake2.js +++ b/runtime/blake2.js @@ -1,6 +1,6 @@ //Provides: blake2b //Version: >= 5.2 -var blake2b = (function () { +const blake2b = (() => { // Blake2B in pure Javascript // Adapted from the reference implementation in RFC7693 // Ported to Javascript by DC - https://github.com/dcposch @@ -104,11 +104,7 @@ var blake2b = (function () { // These are offsets into a uint64 buffer. // Multiply them all by 2 to make them offsets into a uint32 buffer, // because this is Javascript and we don't have uint64s - const SIGMA82 = new Uint8Array( - SIGMA8.map(function (x) { - return x * 2; - }), - ); + const SIGMA82 = new Uint8Array(SIGMA8.map((x) => x * 2)); // Compression function. 'last' flag indicates last block. // Note we're representing 16 uint64s as 32 uint32s @@ -319,7 +315,7 @@ function caml_blake2_create(hashlen, key) { //Requires: blake2b //Version: >= 5.2 function caml_blake2_final(ctx, hashlen) { - var r = blake2b.Final(ctx); + const r = blake2b.Final(ctx); return caml_string_of_array(r); } @@ -328,7 +324,7 @@ function caml_blake2_final(ctx, hashlen) { //Requires: caml_uint8_array_of_string //Version: >= 5.2 function caml_blake2_update(ctx, buf, ofs, len) { - var input = caml_uint8_array_of_string(buf); + let input = caml_uint8_array_of_string(buf); input = input.subarray(ofs, ofs + len); blake2b.Update(ctx, input); return 0; @@ -340,7 +336,7 @@ function caml_blake2_update(ctx, buf, ofs, len) { //Requires: caml_blake2_final //Version: >= 5.2 function caml_blake2_string(hashlen, key, buf, ofs, len) { - var ctx = caml_blake2_create(hashlen, key); + const ctx = caml_blake2_create(hashlen, key); caml_blake2_update(ctx, buf, ofs, len); return caml_blake2_final(ctx, hashlen); } diff --git a/runtime/compare.js b/runtime/compare.js index 01964718eb..f72d0a36b8 100644 --- a/runtime/compare.js +++ b/runtime/compare.js @@ -26,7 +26,7 @@ function caml_compare_val_tag(a) { return 1252; // ocaml string (if different from bytes) else if (Array.isArray(a) && a[0] === a[0] >>> 0 && a[0] <= 255) { // Look like an ocaml block - var tag = a[0] | 0; + 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; @@ -57,9 +57,9 @@ function caml_compare_val_get_custom(a) { //Provides: caml_compare_val_number_custom //Requires: caml_compare_val_get_custom function caml_compare_val_number_custom(num, custom, swap, total) { - var comp = caml_compare_val_get_custom(custom); + const comp = caml_compare_val_get_custom(custom); if (comp) { - var x = swap > 0 ? comp(custom, num, total) : comp(num, custom, total); + const x = swap > 0 ? comp(custom, num, total) : comp(num, custom, total); if (total && x != x) return swap; // total && nan if (+x != +x) return +x; // nan if ((x | 0) != 0) return x | 0; // !nan @@ -74,17 +74,17 @@ function caml_compare_val_number_custom(num, custom, swap, total) { //Requires: caml_jsbytes_of_string //Requires: caml_is_continuation_tag function caml_compare_val(a, b, total) { - var stack = []; + const stack = []; for (;;) { if (!(total && a === b)) { - var tag_a = caml_compare_val_tag(a); + const tag_a = caml_compare_val_tag(a); // forward_tag ? if (tag_a == 250) { a = a[1]; continue; } - var tag_b = caml_compare_val_tag(b); + const tag_b = caml_compare_val_tag(b); // forward_tag ? if (tag_b == 250) { b = b[1]; @@ -117,7 +117,7 @@ function caml_compare_val(a, b, total) { break; case 248: { // Object - var x = caml_int_compare(a[2], b[2]); + const x = caml_int_compare(a[2], b[2]); if (x != 0) return x | 0; break; } @@ -134,7 +134,7 @@ function caml_compare_val(a, b, total) { break; case 252: // OCaml bytes if (a !== b) { - var x = caml_bytes_compare(a, b); + const x = caml_bytes_compare(a, b); if (x != 0) return x | 0; } break; @@ -156,12 +156,12 @@ function caml_compare_val(a, b, total) { break; case 1255: { // Custom - var comp = caml_compare_val_get_custom(a); + const comp = caml_compare_val_get_custom(a); if (comp != caml_compare_val_get_custom(b)) { return a.caml_custom < b.caml_custom ? -1 : 1; } if (!comp) caml_invalid_argument("compare: abstract value"); - var x = comp(a, b, total); + const x = comp(a, b, total); if (x != x) { // Protect against invalid UNORDERED return total ? -1 : x; @@ -175,7 +175,7 @@ function caml_compare_val(a, b, total) { } case 1256: { // compare function - var x = a.compare(b, total); + const x = a.compare(b, total); if (x != x) { // Protect against invalid UNORDERED return total ? -1 : x; @@ -193,7 +193,7 @@ function caml_compare_val(a, b, total) { if (a < b) return -1; if (a > b) return 1; if (a != b) { - if (!total) return NaN; + if (!total) return Number.NaN; if (a == a) return 1; if (b == b) return -1; } @@ -215,21 +215,21 @@ function caml_compare_val(a, b, total) { if (a < b) return -1; if (a > b) return 1; if (a != b) { - if (!total) return NaN; + if (!total) return Number.NaN; if (a == a) return 1; if (b == b) return -1; } break; case 1251: // JavaScript Symbol, no ordering. if (a !== b) { - if (!total) return NaN; + if (!total) return Number.NaN; return 1; } break; case 1252: { // ocaml strings - var a = caml_jsbytes_of_string(a); - var b = caml_jsbytes_of_string(b); + a = caml_jsbytes_of_string(a); + b = caml_jsbytes_of_string(b); if (a !== b) { if (a < b) return -1; if (a > b) return 1; @@ -238,8 +238,8 @@ function caml_compare_val(a, b, total) { } case 12520: { // javascript strings - var a = a.toString(); - var b = b.toString(); + a = a.toString(); + b = b.toString(); if (a !== b) { if (a < b) return -1; if (a > b) return 1; @@ -259,7 +259,7 @@ function caml_compare_val(a, b, total) { } } if (stack.length == 0) return 0; - var i = stack.pop(); + const i = stack.pop(); b = stack.pop(); a = stack.pop(); if (i + 1 < a.length) stack.push(a, b, i + 1); diff --git a/runtime/domain.js b/runtime/domain.js index 55c7638bba..bc4b08b4bc 100644 --- a/runtime/domain.js +++ b/runtime/domain.js @@ -1,5 +1,5 @@ //Provides: caml_domain_dls -var caml_domain_dls = [0]; +let caml_domain_dls = [0]; //Provides: caml_domain_dls_set //Requires: caml_domain_dls @@ -38,14 +38,14 @@ function caml_atomic_cas(ref, o, n) { //Provides: caml_atomic_fetch_add function caml_atomic_fetch_add(ref, i) { - var old = ref[1]; + const old = ref[1]; ref[1] += i; return old; } //Provides: caml_atomic_exchange function caml_atomic_exchange(ref, v) { - var r = ref[1]; + const r = ref[1]; ref[1] = v; return r; } @@ -57,7 +57,7 @@ function caml_atomic_make_contended(a) { //Provides: caml_ml_domain_unique_token //Version: < 5.2 -var caml_ml_domain_unique_token_ = [0]; +const caml_ml_domain_unique_token_ = [0]; function caml_ml_domain_unique_token(unit) { return caml_ml_domain_unique_token_; } @@ -73,19 +73,19 @@ function caml_recommended_domain_count(unit) { } //Provides: caml_domain_id -var caml_domain_id = 0; +let caml_domain_id = 0; //Provides: caml_domain_spawn //Requires: caml_ml_mutex_unlock //Requires: caml_domain_id //Requires: caml_callback //Version: >= 5.2 -var caml_domain_latest_idx = 1; +let caml_domain_latest_idx = 1; function caml_domain_spawn(f, term_sync) { - var id = caml_domain_latest_idx++; - var old = caml_domain_id; + const id = caml_domain_latest_idx++; + const old = caml_domain_id; caml_domain_id = id; - var res = caml_callback(f, [0]); + const res = caml_callback(f, [0]); caml_domain_id = old; caml_ml_mutex_unlock(term_sync[2]); //TODO: fix exn case @@ -98,12 +98,12 @@ function caml_domain_spawn(f, term_sync) { //Requires: caml_domain_id //Requires: caml_callback //Version: < 5.2 -var caml_domain_latest_idx = 1; +let caml_domain_latest_idx = 1; function caml_domain_spawn(f, mutex) { - var id = caml_domain_latest_idx++; - var old = caml_domain_id; + const id = caml_domain_latest_idx++; + const old = caml_domain_id; caml_domain_id = id; - var res = caml_callback(f, [0]); + const res = caml_callback(f, [0]); caml_domain_id = old; caml_ml_mutex_unlock(mutex); return id; diff --git a/runtime/dynlink.js b/runtime/dynlink.js index c45e04171d..e2316cfc70 100644 --- a/runtime/dynlink.js +++ b/runtime/dynlink.js @@ -17,7 +17,7 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. //Provides: get_current_libs -var current_libs; +let current_libs; function get_current_libs() { if (!current_libs) current_libs = [0, globalThis, globalThis.jsoo_runtime]; return current_libs; @@ -27,10 +27,10 @@ function get_current_libs() { //Requires: get_current_libs, caml_failwith //Requires: caml_jsstring_of_string function caml_dynlink_open_lib(_mode, file) { - var name = caml_jsstring_of_string(file); + const name = caml_jsstring_of_string(file); console.log("Dynlink: try to open ", name); //caml_failwith("file not found: "+name) - var current_libs = get_current_libs(); + const current_libs = get_current_libs(); current_libs.push({}); return current_libs.length; } @@ -38,7 +38,7 @@ function caml_dynlink_open_lib(_mode, file) { //Provides: caml_dynlink_close_lib //Requires: get_current_libs function caml_dynlink_close_lib(idx) { - var current_libs = get_current_libs(); + const current_libs = get_current_libs(); current_libs[idx] = null; return 0; } @@ -47,9 +47,9 @@ function caml_dynlink_close_lib(idx) { //Requires: get_current_libs //Requires: caml_jsstring_of_string function caml_dynlink_lookup_symbol(idx, fun_name) { - var name = caml_jsstring_of_string(fun_name); + const name = caml_jsstring_of_string(fun_name); console.log("Dynlink: looking for symbol", name); - var current_libs = get_current_libs(); + const current_libs = get_current_libs(); if (current_libs[idx] && current_libs[idx][name]) return { name: name, symbol: current_libs[idx][name] }; return 0; @@ -65,9 +65,9 @@ function caml_dynlink_add_primitive(dll_addr) { //Provides: caml_dynlink_get_current_libs //Requires: get_current_libs function caml_dynlink_get_current_libs() { - var current_libs = get_current_libs(); - var len = current_libs.length; - var a = new Array(len); + const current_libs = get_current_libs(); + const len = current_libs.length; + const a = new Array(len); for (let i = 0; i < len; i++) a[i] = i; return a; } diff --git a/runtime/effect.js b/runtime/effect.js index d80a444932..adce18aab0 100644 --- a/runtime/effect.js +++ b/runtime/effect.js @@ -46,7 +46,7 @@ additional parameter which is the current low-level continuation. //Provides: caml_exn_stack //If: effects // This is an OCaml list of exception handlers -var caml_exn_stack = 0; +let caml_exn_stack = 0; //Provides: caml_push_trap //Requires: caml_exn_stack @@ -60,10 +60,10 @@ function caml_push_trap(handler) { //If: effects function caml_pop_trap() { if (!caml_exn_stack) - return function (x) { + return (x) => { throw x; }; - var h = caml_exn_stack[1]; + const h = caml_exn_stack[1]; caml_exn_stack = caml_exn_stack[2]; return h; } @@ -73,7 +73,7 @@ function caml_pop_trap() { // This has the shape {h, r:{k, x, e}} where h is a triple of handlers // (see effect.js) and k, x and e are the saved continuation, // exception stack and fiber stack of the parent fiber. -var caml_fiber_stack; +let caml_fiber_stack; //Provides:caml_resume_stack //Requires: caml_named_value, caml_raise_constant, caml_exn_stack, caml_fiber_stack @@ -102,7 +102,7 @@ function caml_resume_stack(stack, k) { //If: effects function caml_pop_fiber() { // Move to the parent fiber, returning the parent's low-level continuation - var rem = caml_fiber_stack.r; + const rem = caml_fiber_stack.r; caml_exn_stack = rem.x; caml_fiber_stack = rem.e; return rem.k; @@ -115,13 +115,13 @@ function caml_perform_effect(eff, cont, k0) { // Allocate a continuation if we don't already have one if (!cont) cont = [245 /*continuation*/, 0]; // Get current effect handler - var handler = caml_fiber_stack.h[3]; + const handler = caml_fiber_stack.h[3]; // Cons the current fiber onto the continuation: // cont := Cons (k, exn_stack, handlers, !cont) cont[1] = [0, k0, caml_exn_stack, caml_fiber_stack.h, cont[1]]; // Move to parent fiber and execute the effect handler there // The handler is defined in Stdlib.Effect, so we know that the arity matches - var k1 = caml_pop_fiber(); + const k1 = caml_pop_fiber(); return caml_stack_check_depth() ? handler(eff, cont, k1, k1) : caml_trampoline_return(handler, [eff, cont, k1, k1]); @@ -132,8 +132,8 @@ function caml_perform_effect(eff, cont, k0) { //If: effects function caml_alloc_stack(hv, hx, hf) { function call(i, x) { - var f = caml_fiber_stack.h[i]; - var args = [x, caml_pop_fiber()]; + const f = caml_fiber_stack.h[i]; + const args = [x, caml_pop_fiber()]; return caml_stack_check_depth() ? caml_call_gen(f, args) : caml_trampoline_return(f, args); @@ -157,7 +157,7 @@ function caml_alloc_stack(hv, hx, hf) { //Provides: caml_continuation_use_noexc function caml_continuation_use_noexc(cont) { - var stack = cont[1]; + const stack = cont[1]; cont[1] = 0; return stack; } @@ -170,7 +170,7 @@ function caml_continuation_use_and_update_handler_noexc( hexn, heff, ) { - var stack = caml_continuation_use_noexc(cont); + const stack = caml_continuation_use_noexc(cont); stack[3] = [0, hval, hexn, heff]; return stack; } diff --git a/runtime/format.js b/runtime/format.js index 25b0c35000..6ae2a4c001 100644 --- a/runtime/format.js +++ b/runtime/format.js @@ -21,9 +21,9 @@ //Requires: caml_jsbytes_of_string, caml_invalid_argument function caml_parse_format(fmt) { fmt = caml_jsbytes_of_string(fmt); - var len = fmt.length; + const len = fmt.length; if (len > 31) caml_invalid_argument("format_int: format too long"); - var f = { + const f = { justify: "+", signstyle: "-", filler: " ", @@ -37,7 +37,7 @@ function caml_parse_format(fmt) { conv: "f", }; for (let i = 0; i < len; i++) { - var c = fmt.charAt(i); + let c = fmt.charAt(i); switch (c) { case "-": f.justify = "-"; @@ -114,7 +114,7 @@ function caml_parse_format(fmt) { //Requires: caml_string_of_jsbytes function caml_finish_formatting(f, rawbuffer) { if (f.uppercase) rawbuffer = rawbuffer.toUpperCase(); - var len = rawbuffer.length; + let len = rawbuffer.length; /* Adjust len to reflect additional chars (sign, etc) */ if (f.signedconv && (f.sign < 0 || f.signstyle != "-")) len++; if (f.alternate) { @@ -122,7 +122,7 @@ function caml_finish_formatting(f, rawbuffer) { if (f.base == 16) len += 2; } /* Do the formatting */ - var buffer = ""; + let buffer = ""; if (f.justify == "+" && f.filler == " ") for (let i = len; i < f.width; i++) buffer += " "; if (f.signedconv) { diff --git a/runtime/fs.js b/runtime/fs.js index 1fd1f7b734..60a2f0a258 100644 --- a/runtime/fs.js +++ b/runtime/fs.js @@ -26,22 +26,23 @@ function caml_trailing_slash(name) { //Provides: caml_current_dir //Requires: caml_trailing_slash, fs_node_supported +let caml_current_dir; if (fs_node_supported() && globalThis.process && globalThis.process.cwd) - var caml_current_dir = globalThis.process.cwd().replace(/\\/g, "/"); -else var caml_current_dir = "/static"; + caml_current_dir = globalThis.process.cwd().replace(/\\/g, "/"); +else caml_current_dir = "/static"; caml_current_dir = caml_trailing_slash(caml_current_dir); //Provides: caml_get_root //Requires: path_is_absolute function caml_get_root(path) { - var x = path_is_absolute(path); + const x = path_is_absolute(path); if (!x) return; return x[0] + "/"; } //Provides: caml_root //Requires: caml_get_root, caml_current_dir, caml_failwith -var caml_root = +const caml_root = caml_get_root(caml_current_dir) || caml_failwith("unable to compute caml_root"); @@ -58,16 +59,16 @@ function make_path_is_absolute() { function win32(path) { // https://github.com/nodejs/node/blob/b3fcc245fb25539909ef1d5eaa01dbf92e168633/lib/path.js#L56 - var splitDeviceRe = + const splitDeviceRe = /^([a-zA-Z]:|[\\/]{2}[^\\/]+[\\/]+[^\\/]+)?([\\/])?([\s\S]*?)$/; - var result = splitDeviceRe.exec(path); - var device = result[1] || ""; - var isUnc = Boolean(device && device.charAt(1) !== ":"); + const result = splitDeviceRe.exec(path); + const device = result[1] || ""; + const isUnc = Boolean(device && device.charAt(1) !== ":"); // UNC paths are always absolute if (Boolean(result[2] || isUnc)) { - var root = result[1] || ""; - var sep = result[2] || ""; + const root = result[1] || ""; + const sep = result[2] || ""; return [root, path.substring(root.length + sep.length)]; } return; @@ -80,7 +81,7 @@ function make_path_is_absolute() { return globalThis.process.platform === "win32" ? win32 : posix; } else return posix; } -var path_is_absolute = make_path_is_absolute(); +const path_is_absolute = make_path_is_absolute(); //Provides: caml_make_path //Requires: caml_current_dir @@ -88,9 +89,9 @@ var path_is_absolute = make_path_is_absolute(); function caml_make_path(name) { name = caml_jsstring_of_string(name); if (!path_is_absolute(name)) name = caml_current_dir + name; - var comp0 = path_is_absolute(name); - var comp = comp0[1].split(/[/\\]/); - var ncomp = []; + const comp0 = path_is_absolute(name); + const comp = comp0[1].split(/[/\\]/); + const ncomp = []; for (let i = 0; i < comp.length; i++) { switch (comp[i]) { case "..": @@ -112,7 +113,7 @@ function caml_make_path(name) { //Provides:jsoo_mount_point //Requires: MlFakeDevice, MlNodeDevice, caml_root, fs_node_supported -var jsoo_mount_point = []; +const jsoo_mount_point = []; if (fs_node_supported()) { jsoo_mount_point.push({ path: caml_root, @@ -132,9 +133,9 @@ jsoo_mount_point.push({ //Provides:caml_list_mount_point //Requires: jsoo_mount_point, caml_string_of_jsbytes function caml_list_mount_point() { - var prev = 0; + let prev = 0; for (let i = 0; i < jsoo_mount_point.length; i++) { - var old = prev; + const old = prev; prev = [0, caml_string_of_jsbytes(jsoo_mount_point[i].path), old]; } return prev; @@ -143,12 +144,12 @@ function caml_list_mount_point() { //Provides: resolve_fs_device //Requires: caml_make_path, jsoo_mount_point, caml_raise_sys_error, caml_get_root, MlNodeDevice, caml_trailing_slash, fs_node_supported function resolve_fs_device(name) { - var path = caml_make_path(name); - var name = path.join("/"); - var name_slash = caml_trailing_slash(name); - var res; + const path = caml_make_path(name); + const name_ = path.join("/"); + const name_slash = caml_trailing_slash(name_); + let res; for (let i = 0; i < jsoo_mount_point.length; i++) { - var m = jsoo_mount_point[i]; + const m = jsoo_mount_point[i]; if ( name_slash.search(m.path) == 0 && (!res || res.path.length < m.path.length) @@ -156,18 +157,18 @@ function resolve_fs_device(name) { res = { path: m.path, device: m.device, - rest: name.substring(m.path.length, name.length), + rest: name_.substring(m.path.length, name_.length), }; } if (!res && fs_node_supported()) { - var root = caml_get_root(name); + const root = caml_get_root(name_); if (root && root.match(/^[a-zA-Z]:\/$/)) { - var m = { path: root, device: new MlNodeDevice(root) }; + const m = { path: root, device: new MlNodeDevice(root) }; jsoo_mount_point.push(m); res = { path: m.path, device: m.device, - rest: name.substring(m.path.length, name.length), + rest: name_.substring(m.path.length, name_.length), }; } } @@ -178,20 +179,20 @@ function resolve_fs_device(name) { //Provides: caml_mount_autoload //Requires: MlFakeDevice, caml_make_path, jsoo_mount_point, caml_trailing_slash function caml_mount_autoload(name, f) { - var path = caml_make_path(name); - var name = caml_trailing_slash(path.join("/")); - jsoo_mount_point.push({ path: name, device: new MlFakeDevice(name, f) }); + const path = caml_make_path(name); + const name_ = caml_trailing_slash(path.join("/")); + jsoo_mount_point.push({ path: name_, device: new MlFakeDevice(name_, f) }); return 0; } //Provides: caml_unmount //Requires: jsoo_mount_point, caml_make_path, caml_trailing_slash function caml_unmount(name) { - var path = caml_make_path(name); - var name = caml_trailing_slash(path.join("/")); - var idx = -1; + const path = caml_make_path(name); + const name_ = caml_trailing_slash(path.join("/")); + let idx = -1; for (let i = 0; i < jsoo_mount_point.length; i++) - if (jsoo_mount_point[i].path == name) idx = i; + if (jsoo_mount_point[i].path == name_) idx = i; if (idx > -1) jsoo_mount_point.splice(idx, 1); return 0; } @@ -205,7 +206,7 @@ function caml_sys_getcwd() { //Provides: caml_sys_chdir //Requires: caml_current_dir, caml_raise_no_such_file, resolve_fs_device, caml_trailing_slash, caml_jsbytes_of_string function caml_sys_chdir(dir) { - var root = resolve_fs_device(dir); + const root = resolve_fs_device(dir); if (root.device.exists(root.rest)) { if (root.rest) caml_current_dir = caml_trailing_slash(root.path + root.rest); @@ -231,7 +232,7 @@ function caml_raise_not_a_dir(name) { //Provides: caml_sys_file_exists //Requires: resolve_fs_device function caml_sys_file_exists(name) { - var root = resolve_fs_device(name); + const root = resolve_fs_device(name); return root.device.exists(root.rest); } @@ -239,9 +240,9 @@ function caml_sys_file_exists(name) { //Requires: caml_string_of_jsbytes //Requires: caml_raise_not_a_dir, resolve_fs_device function caml_sys_read_directory(name) { - var root = resolve_fs_device(name); - var a = root.device.readdir(root.rest); - var l = new Array(a.length + 1); + const root = resolve_fs_device(name); + const a = root.device.readdir(root.rest); + const l = new Array(a.length + 1); l[0] = 0; for (let i = 0; i < a.length; i++) l[i + 1] = caml_string_of_jsbytes(a[i]); return l; @@ -250,8 +251,8 @@ function caml_sys_read_directory(name) { //Provides: caml_sys_remove //Requires: caml_raise_no_such_file, resolve_fs_device, caml_jsbytes_of_string function caml_sys_remove(name) { - var root = resolve_fs_device(name); - var ok = root.device.unlink(root.rest); + const root = resolve_fs_device(name); + const ok = root.device.unlink(root.rest); if (ok == 0) caml_raise_no_such_file(caml_jsbytes_of_string(name)); return 0; } @@ -259,16 +260,16 @@ function caml_sys_remove(name) { //Provides: caml_sys_is_directory //Requires: resolve_fs_device function caml_sys_is_directory(name) { - var root = resolve_fs_device(name); - var a = root.device.is_dir(root.rest); + const root = resolve_fs_device(name); + const a = root.device.is_dir(root.rest); return a ? 1 : 0; } //Provides: caml_sys_rename //Requires: caml_failwith, resolve_fs_device function caml_sys_rename(o, n) { - var o_root = resolve_fs_device(o); - var n_root = resolve_fs_device(n); + const o_root = resolve_fs_device(o); + const n_root = resolve_fs_device(n); if (o_root.device != n_root.device) caml_failwith("caml_sys_rename: cannot move file between two filesystem"); if (!o_root.device.rename) caml_failwith("caml_sys_rename: no implemented"); @@ -278,7 +279,7 @@ function caml_sys_rename(o, n) { //Provides: caml_sys_mkdir //Requires: resolve_fs_device, caml_raise_sys_error function caml_sys_mkdir(name, perm) { - var root = resolve_fs_device(name); + const root = resolve_fs_device(name); root.device.mkdir(root.rest, perm); return 0; } @@ -286,7 +287,7 @@ function caml_sys_mkdir(name, perm) { //Provides: caml_sys_rmdir //Requires: resolve_fs_device, caml_raise_sys_error, caml_raise_not_a_dir function caml_sys_rmdir(name) { - var root = resolve_fs_device(name); + const root = resolve_fs_device(name); root.device.rmdir(root.rest); return 0; } @@ -317,7 +318,7 @@ function jsoo_create_file_extern(name, content) { //Provides: caml_fs_init //Requires: jsoo_create_file function caml_fs_init() { - var tmp = globalThis.caml_fs_tmp; + const tmp = globalThis.caml_fs_tmp; if (tmp) { for (let i = 0; i < tmp.length; i++) { jsoo_create_file(tmp[i].name, tmp[i].content); @@ -331,7 +332,7 @@ function caml_fs_init() { //Provides: caml_create_file //Requires: caml_failwith, resolve_fs_device function caml_create_file(name, content) { - var root = resolve_fs_device(name); + const root = resolve_fs_device(name); if (!root.device.register) caml_failwith("cannot register file"); root.device.register(root.rest, content); return 0; @@ -340,23 +341,23 @@ function caml_create_file(name, content) { //Provides: jsoo_create_file //Requires: caml_create_file, caml_string_of_jsbytes function jsoo_create_file(name, content) { - var name = caml_string_of_jsbytes(name); - var content = caml_string_of_jsbytes(content); - return caml_create_file(name, content); + const name_ = caml_string_of_jsbytes(name); + const content_ = caml_string_of_jsbytes(content); + return caml_create_file(name_, content_); } //Provides: caml_read_file_content //Requires: resolve_fs_device, caml_raise_no_such_file, caml_string_of_array //Requires: caml_string_of_jsbytes, caml_jsbytes_of_string function caml_read_file_content(name) { - var name = typeof name == "string" ? caml_string_of_jsbytes(name) : name; - var root = resolve_fs_device(name); + const name_ = typeof name == "string" ? caml_string_of_jsbytes(name) : name; + const root = resolve_fs_device(name_); if (root.device.exists(root.rest)) { - var file = root.device.open(root.rest, { rdonly: 1 }); - var len = file.length(); - var buf = new Uint8Array(len); + const file = root.device.open(root.rest, { rdonly: 1 }); + const len = file.length(); + const buf = new Uint8Array(len); file.read(0, buf, 0, len); return caml_string_of_array(buf); } - caml_raise_no_such_file(caml_jsbytes_of_string(name)); + caml_raise_no_such_file(caml_jsbytes_of_string(name_)); } diff --git a/runtime/fs_fake.js b/runtime/fs_fake.js index 0becff9195..c437ecdb9d 100644 --- a/runtime/fs_fake.js +++ b/runtime/fs_fake.js @@ -34,20 +34,18 @@ MlFakeDevice.prototype.nm = function (name) { return this.root + name; }; MlFakeDevice.prototype.create_dir_if_needed = function (name) { - var comp = name.split("/"); - var res = ""; + const comp = name.split("/"); + let res = ""; for (let i = 0; i < comp.length - 1; i++) { res += comp[i] + "/"; if (this.content[res]) continue; this.content[res] = Symbol("directory"); } }; -MlFakeDevice.prototype.slash = function (name) { - return /\/$/.test(name) ? name : name + "/"; -}; +MlFakeDevice.prototype.slash = (name) => (/\/$/.test(name) ? name : name + "/"); MlFakeDevice.prototype.lookup = function (name) { if (!this.content[name] && this.lookupFun) { - var res = this.lookupFun( + const res = this.lookupFun( caml_string_of_jsbytes(this.root), caml_string_of_jsbytes(name), ); @@ -61,7 +59,7 @@ MlFakeDevice.prototype.exists = function (name) { // The root of the device exists if (name == "") return 1; // Check if a directory exists - var name_slash = this.slash(name); + const name_slash = this.slash(name); if (this.content[name_slash]) return 1; // Check if a file exists this.lookup(name); @@ -75,7 +73,7 @@ MlFakeDevice.prototype.isFile = function (name) { } }; MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) { - var unix_error = raise_unix && caml_named_value("Unix.Unix_error"); + const unix_error = raise_unix && caml_named_value("Unix.Unix_error"); if (this.exists(name)) { if (unix_error) { caml_raise_with_args( @@ -86,7 +84,7 @@ MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) { caml_raise_sys_error(name + ": File exists"); } } - var parent = /^(.*)\/[^/]+/.exec(name); + let parent = /^(.*)\/[^/]+/.exec(name); parent = (parent && parent[1]) || ""; if (!this.exists(parent)) { if (unix_error) { @@ -111,9 +109,9 @@ MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) { this.create_dir_if_needed(this.slash(name)); }; MlFakeDevice.prototype.rmdir = function (name, raise_unix) { - var unix_error = raise_unix && caml_named_value("Unix.Unix_error"); - var name_slash = name == "" ? "" : this.slash(name); - var r = new RegExp("^" + name_slash + "([^/]+)"); + const unix_error = raise_unix && caml_named_value("Unix.Unix_error"); + const name_slash = name == "" ? "" : this.slash(name); + const r = new RegExp("^" + name_slash + "([^/]+)"); if (!this.exists(name)) { if (unix_error) { caml_raise_with_args( @@ -134,7 +132,7 @@ MlFakeDevice.prototype.rmdir = function (name, raise_unix) { caml_raise_sys_error(name + ": Not a directory"); } } - for (let n in this.content) { + for (const n in this.content) { if (n.match(r)) { if (unix_error) { caml_raise_with_args( @@ -149,18 +147,18 @@ MlFakeDevice.prototype.rmdir = function (name, raise_unix) { delete this.content[name_slash]; }; MlFakeDevice.prototype.readdir = function (name) { - var name_slash = name == "" ? "" : this.slash(name); + const name_slash = name == "" ? "" : this.slash(name); if (!this.exists(name)) { caml_raise_sys_error(name + ": No such file or directory"); } if (!this.is_dir(name)) { caml_raise_sys_error(name + ": Not a directory"); } - var r = new RegExp("^" + name_slash + "([^/]+)"); - var seen = {}; - var a = []; - for (let n in this.content) { - var m = n.match(r); + const r = new RegExp("^" + name_slash + "([^/]+)"); + const seen = {}; + const a = []; + for (const n in this.content) { + const m = n.match(r); if (m && !seen[m[1]]) { seen[m[1]] = true; a.push(m[1]); @@ -169,11 +167,11 @@ MlFakeDevice.prototype.readdir = function (name) { return a; }; MlFakeDevice.prototype.opendir = function (name, raise_unix) { - var unix_error = raise_unix && caml_named_value("Unix.Unix_error"); + const unix_error = raise_unix && caml_named_value("Unix.Unix_error"); - var a = this.readdir(name); - var c = false; - var i = 0; + let a = this.readdir(name); + let c = false; + let i = 0; return { readSync: function () { if (c) { @@ -187,7 +185,7 @@ MlFakeDevice.prototype.opendir = function (name, raise_unix) { } } if (i == a.length) return null; - var entry = a[i]; + const entry = a[i]; i++; return { name: entry }; }, @@ -209,16 +207,16 @@ MlFakeDevice.prototype.opendir = function (name, raise_unix) { }; MlFakeDevice.prototype.is_dir = function (name) { if (name == "") return true; - var name_slash = this.slash(name); + const name_slash = this.slash(name); return this.content[name_slash] ? 1 : 0; }; MlFakeDevice.prototype.unlink = function (name) { - var ok = this.content[name] ? true : false; + const ok = this.content[name] ? true : false; delete this.content[name]; return ok; }; MlFakeDevice.prototype.open = function (name, f) { - var file; + let file; if (f.rdonly && f.wronly) caml_raise_sys_error( this.nm(name) + " : flags Open_rdonly and Open_wronly are not compatible", @@ -246,7 +244,7 @@ MlFakeDevice.prototype.open = function (name, f) { }; MlFakeDevice.prototype.open = function (name, f) { - var file; + let file; if (f.rdonly && f.wronly) caml_raise_sys_error( this.nm(name) + " : flags Open_rdonly and Open_wronly are not compatible", @@ -274,7 +272,7 @@ MlFakeDevice.prototype.open = function (name, f) { }; MlFakeDevice.prototype.register = function (name, content) { - var file; + let file; if (this.content[name]) caml_raise_sys_error(this.nm(name) + " : file already exists"); if (caml_is_ml_bytes(content)) file = new MlFakeFile(content); @@ -285,7 +283,7 @@ MlFakeDevice.prototype.register = function (name, content) { else if (typeof content === "string") file = new MlFakeFile(caml_bytes_of_jsbytes(content)); else if (content.toString) { - var bytes = caml_bytes_of_string( + const bytes = caml_bytes_of_string( caml_string_of_jsstring(content.toString()), ); file = new MlFakeFile(bytes); @@ -311,7 +309,7 @@ function MlFakeFile(content) { MlFakeFile.prototype = new MlFile(); MlFakeFile.prototype.constructor = MlFakeFile; MlFakeFile.prototype.truncate = function (len) { - var old = this.data; + const old = this.data; this.data = caml_create_bytes(len | 0); caml_blit_bytes(old, 0, this.data, 0, len); }; @@ -319,10 +317,10 @@ MlFakeFile.prototype.length = function () { return caml_ml_bytes_length(this.data); }; MlFakeFile.prototype.write = function (offset, buf, pos, len) { - var clen = this.length(); + const clen = this.length(); if (offset + len >= clen) { - var new_str = caml_create_bytes(offset + len); - var old_data = this.data; + const new_str = caml_create_bytes(offset + len); + const old_data = this.data; this.data = new_str; caml_blit_bytes(old_data, 0, this.data, 0, clen); } @@ -330,12 +328,12 @@ MlFakeFile.prototype.write = function (offset, buf, pos, len) { return 0; }; MlFakeFile.prototype.read = function (offset, buf, pos, len) { - var clen = this.length(); + const clen = this.length(); if (offset + len >= clen) { len = clen - offset; } if (len) { - var data = caml_create_bytes(len | 0); + const data = caml_create_bytes(len | 0); caml_blit_bytes(this.data, offset, data, 0, len); buf.set(caml_uint8_array_of_bytes(data), pos); } @@ -347,18 +345,14 @@ MlFakeFile.prototype.read = function (offset, buf, pos, len) { //Requires: caml_raise_sys_error function MlFakeFd_out(fd, flags) { MlFakeFile.call(this, caml_create_bytes(0)); - this.log = function (s) { - return 0; - }; + this.log = (s) => 0; if (fd == 1 && typeof console.log == "function") this.log = console.log; else if (fd == 2 && typeof console.error == "function") this.log = console.error; else if (typeof console.log == "function") this.log = console.log; this.flags = flags; } -MlFakeFd_out.prototype.length = function () { - return 0; -}; +MlFakeFd_out.prototype.length = () => 0; MlFakeFd_out.prototype.write = function (offset, buf, pos, len) { if (this.log) { if ( @@ -370,7 +364,7 @@ MlFakeFd_out.prototype.write = function (offset, buf, pos, len) { len--; // Do not output the last \n if present // as console logging display a newline at the end - var src = caml_create_bytes(len); + const src = caml_create_bytes(len); caml_blit_bytes(caml_bytes_of_array(buf), pos, src, 0, len); this.log(src.toUtf16()); return 0; diff --git a/runtime/fs_node.js b/runtime/fs_node.js index 1ca01160fa..f95dd52048 100644 --- a/runtime/fs_node.js +++ b/runtime/fs_node.js @@ -87,7 +87,7 @@ MlNodeDevice.prototype.is_dir = function (name) { }; MlNodeDevice.prototype.unlink = function (name, raise_unix) { try { - var b = this.fs.existsSync(this.nm(name)) ? 1 : 0; + const b = this.fs.existsSync(this.nm(name)) ? 1 : 0; this.fs.unlinkSync(this.nm(name)); return b; } catch (err) { @@ -95,9 +95,9 @@ MlNodeDevice.prototype.unlink = function (name, raise_unix) { } }; MlNodeDevice.prototype.open = function (name, f, raise_unix) { - var consts = require("constants"); - var res = 0; - for (let key in f) { + const consts = require("constants"); + let res = 0; + for (const key in f) { switch (key) { case "rdonly": res |= consts.O_RDONLY; @@ -129,8 +129,8 @@ MlNodeDevice.prototype.open = function (name, f, raise_unix) { } } try { - var fd = this.fs.openSync(this.nm(name), res); - var isCharacterDevice = this.fs + const fd = this.fs.openSync(this.nm(name), res); + const isCharacterDevice = this.fs .lstatSync(this.nm(name)) .isCharacterDevice(); f.isCharacterDevice = isCharacterDevice; @@ -149,7 +149,7 @@ MlNodeDevice.prototype.rename = function (o, n, raise_unix) { }; MlNodeDevice.prototype.stat = function (name, raise_unix) { try { - var js_stats = this.fs.statSync(this.nm(name)); + const js_stats = this.fs.statSync(this.nm(name)); return this.stats_from_js(js_stats); } catch (err) { this.raise_nodejs_error(err, raise_unix); @@ -157,7 +157,7 @@ MlNodeDevice.prototype.stat = function (name, raise_unix) { }; MlNodeDevice.prototype.lstat = function (name, raise_unix) { try { - var js_stats = this.fs.lstatSync(this.nm(name)); + const js_stats = this.fs.lstatSync(this.nm(name)); return this.stats_from_js(js_stats); } catch (err) { this.raise_nodejs_error(err, raise_unix); @@ -177,7 +177,7 @@ MlNodeDevice.prototype.symlink = function (to_dir, target, path, raise_unix) { }; MlNodeDevice.prototype.readlink = function (name, raise_unix) { try { - var link = this.fs.readlinkSync(this.nm(name), "utf8"); + const link = this.fs.readlinkSync(this.nm(name), "utf8"); return caml_string_of_jsstring(link); } catch (err) { this.raise_nodejs_error(err, raise_unix); @@ -190,16 +190,16 @@ MlNodeDevice.prototype.opendir = function (name, raise_unix) { this.raise_nodejs_error(err, raise_unix); } }; -MlNodeDevice.prototype.raise_nodejs_error = function (err, raise_unix) { - var unix_error = caml_named_value("Unix.Unix_error"); +MlNodeDevice.prototype.raise_nodejs_error = (err, raise_unix) => { + const unix_error = caml_named_value("Unix.Unix_error"); if (raise_unix && unix_error) { - var args = make_unix_err_args(err.code, err.syscall, err.path, err.errno); + const args = make_unix_err_args(err.code, err.syscall, err.path, err.errno); caml_raise_with_args(unix_error, args); } else { caml_raise_sys_error(err.toString()); } }; -MlNodeDevice.prototype.stats_from_js = function (js_stats) { +MlNodeDevice.prototype.stats_from_js = (js_stats) => { /* ===Unix.file_kind=== * type file_kind = * S_REG (** Regular file *) @@ -210,7 +210,7 @@ MlNodeDevice.prototype.stats_from_js = function (js_stats) { * | S_FIFO (** Named pipe *) * | S_SOCK (** Socket *) */ - var file_kind; + let file_kind; if (js_stats.isFile()) { file_kind = 0; } else if (js_stats.isDirectory()) { @@ -302,9 +302,8 @@ MlNodeFd.prototype.write = function (offset, buf, buf_offset, len) { MlNodeFd.prototype.read = function (offset, a, buf_offset, len) { try { if (this.flags.isCharacterDevice) - var read = this.fs.readSync(this.fd, a, buf_offset, len); - else var read = this.fs.readSync(this.fd, a, buf_offset, len, offset); - return read; + return this.fs.readSync(this.fd, a, buf_offset, len); + else return this.fs.readSync(this.fd, a, buf_offset, len, offset); } catch (err) { caml_raise_sys_error(err.toString()); } @@ -327,8 +326,8 @@ function MlNodeFd() {} function caml_sys_open_for_node(fd, flags) { if (flags.name) { try { - var fs = require("fs"); - var fd2 = fs.openSync(flags.name, "rs"); + const fs = require("fs"); + const fd2 = fs.openSync(flags.name, "rs"); return new MlNodeFd(fd2, flags); } catch (e) {} } diff --git a/runtime/gc.js b/runtime/gc.js index 8855240497..31486b80c5 100644 --- a/runtime/gc.js +++ b/runtime/gc.js @@ -62,10 +62,10 @@ function caml_final_register() { } //Provides: caml_final_register_called_without_value -var all_finalizers = new globalThis.Set(); +const all_finalizers = new globalThis.Set(); function caml_final_register_called_without_value(cb, a) { if (globalThis.FinalizationRegistry && a instanceof Object) { - var x = new globalThis.FinalizationRegistry(function (x) { + const x = new globalThis.FinalizationRegistry((x) => { all_finalizers.delete(x); cb(0); return; diff --git a/runtime/graphics.js b/runtime/graphics.js index 1209c3a339..6b7374cc64 100644 --- a/runtime/graphics.js +++ b/runtime/graphics.js @@ -17,7 +17,7 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. //Provides: caml_gr_state -var caml_gr_state; +let caml_gr_state; //Provides: caml_gr_state_get //Requires: caml_gr_state @@ -47,40 +47,40 @@ function caml_gr_state_set(ctx) { //Requires: caml_failwith //Requires: caml_jsstring_of_string function caml_gr_open_graph(info) { - var info = caml_jsstring_of_string(info); + const info_ = caml_jsstring_of_string(info); function get(name) { - var res = info.match("(^|,) *" + name + " *= *([a-zA-Z0-9_]+) *(,|$)"); + const res = info.match("(^|,) *" + name + " *= *([a-zA-Z0-9_]+) *(,|$)"); if (res) return res[2]; } - var specs = []; - if (!(info == "")) specs.push(info); - var target = get("target"); + const specs = []; + if (!(info_ == "")) specs.push(info_); + let target = get("target"); if (!target) target = ""; - var status = get("status"); + const status = get("status"); if (!status) specs.push("status=1"); - var w = get("width"); - w = w ? parseInt(w) : 200; + let w = get("width"); + w = w ? Number.parseInt(w) : 200; specs.push("width=" + w); - var h = get("height"); - h = h ? parseInt(h) : 200; + let h = get("height"); + h = h ? Number.parseInt(h) : 200; specs.push("height=" + h); - var win = globalThis.open("about:blank", target, specs.join(",")); + const win = globalThis.open("about:blank", target, specs.join(",")); if (!win) { caml_failwith("Graphics.open_graph: cannot open the window"); } - var doc = win.document; - var canvas = doc.createElement("canvas"); + const doc = win.document; + const canvas = doc.createElement("canvas"); canvas.width = w; canvas.height = h; - var ctx = caml_gr_state_create(canvas, w, h); - ctx.set_title = function (title) { + const ctx = caml_gr_state_create(canvas, w, h); + ctx.set_title = (title) => { doc.title = title; }; caml_gr_state_set(ctx); - var body = doc.body; + const body = doc.body; body.style.margin = "0px"; body.appendChild(canvas); return 0; @@ -106,7 +106,7 @@ function caml_gr_state_init() { //Provides: caml_gr_state_create //Requires: caml_string_of_jsbytes function caml_gr_state_create(canvas, w, h) { - var context = canvas.getContext("2d"); + const context = canvas.getContext("2d"); return { context: context, canvas: canvas, @@ -130,7 +130,7 @@ function caml_gr_doc_of_state(state) { //Provides: caml_gr_close_graph //Requires: caml_gr_state_get function caml_gr_close_graph() { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.canvas.width = 0; s.canvas.height = 0; return 0; @@ -140,9 +140,9 @@ function caml_gr_close_graph() { //Requires: caml_gr_state_get //Requires: caml_jsstring_of_string function caml_gr_set_window_title(name) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.title = name; - var jsname = caml_jsstring_of_string(name); + const jsname = caml_jsstring_of_string(name); if (s.set_title) s.set_title(jsname); return 0; } @@ -150,7 +150,7 @@ function caml_gr_set_window_title(name) { //Provides: caml_gr_resize_window //Requires: caml_gr_state_get function caml_gr_resize_window(w, h) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.width = w; s.height = h; s.canvas.width = w; @@ -161,7 +161,7 @@ function caml_gr_resize_window(w, h) { //Provides: caml_gr_clear_graph //Requires: caml_gr_state_get function caml_gr_clear_graph() { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.canvas.width = s.width; s.canvas.height = s.height; // s.context.strokeRect (0., 0., s.width, s.height); @@ -171,30 +171,30 @@ function caml_gr_clear_graph() { //Provides: caml_gr_size_x //Requires: caml_gr_state_get function caml_gr_size_x() { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); return s.width; } //Provides: caml_gr_size_y //Requires: caml_gr_state_get function caml_gr_size_y() { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); return s.height; } //Provides: caml_gr_set_color //Requires: caml_gr_state_get function caml_gr_set_color(color) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); function convert(number) { - var str = "" + number.toString(16); + let str = "" + number.toString(16); while (str.length < 2) str = "0" + str; return str; } - var r = (color >> 16) & 0xff, - g = (color >> 8) & 0xff, - b = (color >> 0) & 0xff; + const r = (color >> 16) & 0xff; + const g = (color >> 8) & 0xff; + const b = (color >> 0) & 0xff; s.color = color; - var c_str = "#" + convert(r) + convert(g) + convert(b); + const c_str = "#" + convert(r) + convert(g) + convert(b); s.context.fillStyle = c_str; s.context.strokeStyle = c_str; return 0; @@ -202,15 +202,13 @@ function caml_gr_set_color(color) { //Provides: caml_gr_plot //Requires: caml_gr_state_get function caml_gr_plot(x, y) { - var s = caml_gr_state_get(); - var im = s.context.createImageData(1, 1); - var d = im.data; - var color = s.color; + const s = caml_gr_state_get(); + const im = s.context.createImageData(1, 1); + const d = im.data; + const color = s.color; d[0] = (color >> 16) & 0xff; //r - (d[1] = - (color >> 8) & - 0xff), //g - (d[2] = (color >> 0) & 0xff); //b + d[1] = (color >> 8) & 0xff; //g + d[2] = (color >> 0) & 0xff; //b d[3] = 0xff; //a s.x = x; s.y = y; @@ -221,15 +219,15 @@ function caml_gr_plot(x, y) { //Provides: caml_gr_point_color //Requires: caml_gr_state_get function caml_gr_point_color(x, y) { - var s = caml_gr_state_get(); - var im = s.context.getImageData(x, s.height - y, 1, 1); - var d = im.data; + const s = caml_gr_state_get(); + const im = s.context.getImageData(x, s.height - y, 1, 1); + const d = im.data; return (d[0] << 16) + (d[1] << 8) + d[2]; } //Provides: caml_gr_moveto //Requires: caml_gr_state_get function caml_gr_moveto(x, y) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.x = x; s.y = y; return 0; @@ -238,19 +236,19 @@ function caml_gr_moveto(x, y) { //Provides: caml_gr_current_x //Requires: caml_gr_state_get function caml_gr_current_x() { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); return s.x; } //Provides: caml_gr_current_y //Requires: caml_gr_state_get function caml_gr_current_y() { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); return s.y; } //Provides: caml_gr_lineto //Requires: caml_gr_state_get function caml_gr_lineto(x, y) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.context.beginPath(); s.context.moveTo(s.x, s.height - s.y); s.context.lineTo(x, s.height - y); @@ -262,7 +260,7 @@ function caml_gr_lineto(x, y) { //Provides: caml_gr_draw_rect //Requires: caml_gr_state_get function caml_gr_draw_rect(x, y, w, h) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.context.strokeRect(x, s.height - y, w, -h); return 0; } @@ -272,15 +270,15 @@ function caml_gr_arc_aux(ctx, cx, cy, ry, rx, a1, a2) { while (a1 > a2) a2 += 360; a1 /= 180; a2 /= 180; - var rot = 0, - xPos, - yPos, - xPos_prev, - yPos_prev; - var space = 2; - var num = (((a2 - a1) * Math.PI * ((rx + ry) / 2)) / space) | 0; - var delta = ((a2 - a1) * Math.PI) / num; - var i = a1 * Math.PI; + const rot = 0; + let xPos; + let yPos; + let xPos_prev; + let yPos_prev; + const space = 2; + const num = (((a2 - a1) * Math.PI * ((rx + ry) / 2)) / space) | 0; + const delta = ((a2 - a1) * Math.PI) / num; + let i = a1 * Math.PI; for (let j = 0; j <= num; j++) { xPos = cx - @@ -307,7 +305,7 @@ function caml_gr_arc_aux(ctx, cx, cy, ry, rx, a1, a2) { //Provides: caml_gr_draw_arc //Requires: caml_gr_state_get, caml_gr_arc_aux function caml_gr_draw_arc(x, y, rx, ry, a1, a2) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.context.beginPath(); caml_gr_arc_aux(s.context, x, s.height - y, rx, ry, a1, a2); s.context.stroke(); @@ -317,7 +315,7 @@ function caml_gr_draw_arc(x, y, rx, ry, a1, a2) { //Provides: caml_gr_set_line_width //Requires: caml_gr_state_get function caml_gr_set_line_width(w) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.line_width = w; s.context.lineWidth = w; return 0; @@ -326,14 +324,14 @@ function caml_gr_set_line_width(w) { //Provides: caml_gr_fill_rect //Requires: caml_gr_state_get function caml_gr_fill_rect(x, y, w, h) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.context.fillRect(x, s.height - y, w, -h); return 0; } //Provides: caml_gr_fill_poly //Requires: caml_gr_state_get function caml_gr_fill_poly(ar) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.context.beginPath(); s.context.moveTo(ar[1][1], s.height - ar[1][2]); for (let i = 2; i < ar.length; i++) @@ -346,7 +344,7 @@ function caml_gr_fill_poly(ar) { //Provides: caml_gr_fill_arc //Requires: caml_gr_state_get, caml_gr_arc_aux function caml_gr_fill_arc(x, y, rx, ry, a1, a2) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.context.beginPath(); caml_gr_arc_aux(s.context, x, s.height - y, rx, ry, a1, a2); s.context.fill(); @@ -356,9 +354,9 @@ function caml_gr_fill_arc(x, y, rx, ry, a1, a2) { //Provides: caml_gr_draw_str //Requires: caml_gr_state_get function caml_gr_draw_str(str) { - var s = caml_gr_state_get(); - var m = s.context.measureText(str); - var dx = m.width; + const s = caml_gr_state_get(); + const m = s.context.measureText(str); + const dx = m.width; s.context.fillText(str, s.x, s.height - s.y); s.x += dx | 0; return 0; @@ -383,7 +381,7 @@ function caml_gr_draw_string(str) { //Requires: caml_gr_state_get //Requires: caml_jsstring_of_string function caml_gr_set_font(f) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.font = f; s.context.font = s.text_size + "px " + caml_jsstring_of_string(s.font); return 0; @@ -393,7 +391,7 @@ function caml_gr_set_font(f) { //Requires: caml_gr_state_get //Requires: caml_jsstring_of_string function caml_gr_set_text_size(size) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); s.text_size = size; s.context.font = s.text_size + "px " + caml_jsstring_of_string(s.font); return 0; @@ -403,22 +401,22 @@ function caml_gr_set_text_size(size) { //Requires: caml_gr_state_get //Requires: caml_jsstring_of_string function caml_gr_text_size(txt) { - var s = caml_gr_state_get(); - var w = s.context.measureText(caml_jsstring_of_string(txt)).width; + const s = caml_gr_state_get(); + const w = s.context.measureText(caml_jsstring_of_string(txt)).width; return [0, w, s.text_size]; } //Provides: caml_gr_make_image //Requires: caml_gr_state_get function caml_gr_make_image(arr) { - var s = caml_gr_state_get(); - var h = arr.length - 1; - var w = arr[1].length - 1; - var im = s.context.createImageData(w, h); + const s = caml_gr_state_get(); + const h = arr.length - 1; + const w = arr[1].length - 1; + const im = s.context.createImageData(w, h); for (let i = 0; i < h; i++) { for (let j = 0; j < w; j++) { - var c = arr[i + 1][j + 1]; - var o = i * (w * 4) + j * 4; + const c = arr[i + 1][j + 1]; + const o = i * (w * 4) + j * 4; if (c == -1) { im.data[o + 0] = 0; im.data[o + 1] = 0; @@ -437,14 +435,14 @@ function caml_gr_make_image(arr) { //Provides: caml_gr_dump_image //Requires: caml_gr_state_get function caml_gr_dump_image(im) { - var data = [0]; + const data = [0]; for (let i = 0; i < im.height; i++) { data[i + 1] = [0]; for (let j = 0; j < im.width; j++) { - var o = i * (im.width * 4) + j * 4, - r = im.data[o + 0], - g = im.data[o + 1], - b = im.data[o + 2]; + const o = i * (im.width * 4) + j * 4; + const r = im.data[o + 0]; + const g = im.data[o + 1]; + const b = im.data[o + 2]; data[i + 1][j + 1] = (r << 16) + (g << 8) + b; } } @@ -453,14 +451,14 @@ function caml_gr_dump_image(im) { //Provides: caml_gr_draw_image //Requires: caml_gr_state_get function caml_gr_draw_image(im, x, y) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); if (!im.image) { - var canvas = document.createElement("canvas"); + const canvas = document.createElement("canvas"); canvas.width = s.width; canvas.height = s.height; canvas.getContext("2d").putImageData(im, 0, 0); - var image = new globalThis.Image(); - image.onload = function () { + const image = new globalThis.Image(); + image.onload = () => { s.context.drawImage(image, x, s.height - im.height - y); im.image = image; }; @@ -473,14 +471,14 @@ function caml_gr_draw_image(im, x, y) { //Provides: caml_gr_create_image //Requires: caml_gr_state_get function caml_gr_create_image(x, y) { - var s = caml_gr_state_get(); + const s = caml_gr_state_get(); return s.context.createImageData(x, y); } //Provides: caml_gr_blit_image //Requires: caml_gr_state_get function caml_gr_blit_image(im, x, y) { - var s = caml_gr_state_get(); - var im2 = s.context.getImageData( + const s = caml_gr_state_get(); + const im2 = s.context.getImageData( x, s.height - im.height - y, im.width, diff --git a/runtime/hash.js b/runtime/hash.js index 315f617693..f0f2655ea9 100644 --- a/runtime/hash.js +++ b/runtime/hash.js @@ -24,7 +24,7 @@ //Requires: caml_ml_bytes_length, caml_jsbytes_of_string //Version: < 4.12 function caml_hash_univ_param(count, limit, obj) { - var hash_accu = 0; + let hash_accu = 0; function hash_aux(obj) { limit--; if (count < 0 || limit < 0) return; @@ -47,7 +47,7 @@ function caml_hash_univ_param(count, limit, obj) { } } else if (caml_is_ml_bytes(obj)) { count--; - var content = caml_ml_bytes_content(obj); + const content = caml_ml_bytes_content(obj); if (typeof content === "string") { for (let b = content, l = b.length, i = 0; i < l; i++) hash_accu = (hash_accu * 19 + b.charCodeAt(i)) | 0; @@ -57,7 +57,7 @@ function caml_hash_univ_param(count, limit, obj) { hash_accu = (hash_accu * 19 + a[i]) | 0; } } else if (caml_is_ml_string(obj)) { - var jsbytes = caml_jsbytes_of_string(obj); + const jsbytes = caml_jsbytes_of_string(obj); for (let b = jsbytes, l = jsbytes.length, i = 0; i < l; i++) hash_accu = (hash_accu * 19 + b.charCodeAt(i)) | 0; } else if (typeof obj === "string") { @@ -70,14 +70,14 @@ function caml_hash_univ_param(count, limit, obj) { } else if (obj === +obj) { // Float count--; - var p = caml_int64_to_bytes(caml_int64_bits_of_float(obj)); + 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) { if ( caml_custom_ops[obj.caml_custom] && caml_custom_ops[obj.caml_custom].hash ) { - var h = caml_custom_ops[obj.caml_custom].hash(obj) | 0; + const h = caml_custom_ops[obj.caml_custom].hash(obj) | 0; hash_accu = (hash_accu * 65599 + h) | 0; } } @@ -126,9 +126,9 @@ function caml_hash_mix_int64(h, v) { //Provides: caml_hash_mix_jsbytes //Requires: caml_hash_mix_int function caml_hash_mix_jsbytes(h, s) { - var len = s.length, - i, - w; + const len = s.length; + let i; + let w; for (i = 0; i + 4 <= len; i += 4) { w = s.charCodeAt(i) | @@ -155,9 +155,9 @@ function caml_hash_mix_jsbytes(h, s) { //Provides: caml_hash_mix_bytes_arr //Requires: caml_hash_mix_int function caml_hash_mix_bytes_arr(h, s) { - var len = s.length, - i, - w; + const len = s.length; + let i; + let w; for (i = 0; i + 4 <= len; i += 4) { w = s[i] | (s[i + 1] << 8) | (s[i + 2] << 16) | (s[i + 3] << 24); h = caml_hash_mix_int(h, w); @@ -182,7 +182,7 @@ function caml_hash_mix_bytes_arr(h, s) { //Requires: caml_hash_mix_jsbytes //Requires: caml_hash_mix_bytes_arr function caml_hash_mix_bytes(h, v) { - var content = caml_ml_bytes_content(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); } @@ -200,7 +200,15 @@ function caml_hash_mix_string(h, v) { //Requires: caml_hash_mix_jsbytes //Requires: caml_is_continuation_tag function caml_hash(count, limit, seed, obj) { - var queue, rd, wr, sz, num, h, v, i, len; + let queue; + let rd; + let wr; + let sz; + let num; + let h; + let v; + let i; + let len; sz = limit; if (sz < 0 || sz > 256) sz = 256; num = count; @@ -215,7 +223,7 @@ function caml_hash(count, limit, seed, obj) { caml_custom_ops[v.caml_custom] && caml_custom_ops[v.caml_custom].hash ) { - var hh = caml_custom_ops[v.caml_custom].hash(v); + const hh = caml_custom_ops[v.caml_custom].hash(v); h = caml_hash_mix_int(h, hh); num--; } @@ -236,7 +244,7 @@ function caml_hash(count, limit, seed, obj) { since we have no idea how to distinguish them. */ break; } - var tag = ((v.length - 1) << 10) | v[0]; + const tag = ((v.length - 1) << 10) | v[0]; h = caml_hash_mix_int(h, tag); for (i = 1, len = v.length; i < len; i++) { if (wr >= sz) break; @@ -271,7 +279,7 @@ function caml_hash(count, limit, seed, obj) { //Provides: caml_string_hash //Requires: caml_hash_mix_final, caml_hash_mix_string function caml_string_hash(h, v) { - var h = caml_hash_mix_string(h, v); - var h = caml_hash_mix_final(h); - return h & 0x3fffffff; + const h_ = caml_hash_mix_string(h, v); + const h__ = caml_hash_mix_final(h_); + return h__ & 0x3fffffff; } diff --git a/runtime/ieee_754.js b/runtime/ieee_754.js index a43215f889..d7643ac299 100644 --- a/runtime/ieee_754.js +++ b/runtime/ieee_754.js @@ -18,11 +18,11 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. //Provides: jsoo_floor_log2 -var log2_ok = Math.log2 && Math.log2(1.1235582092889474e307) == 1020; +const log2_ok = Math.log2 && Math.log2(1.1235582092889474e307) == 1020; function jsoo_floor_log2(x) { if (log2_ok) return Math.floor(Math.log2(x)); - var i = 0; - if (x == 0) return -Infinity; + let i = 0; + if (x == 0) return Number.NEGATIVE_INFINITY; if (x >= 1) { while (x >= 2) { x /= 2; @@ -40,16 +40,17 @@ function jsoo_floor_log2(x) { //Provides: caml_int64_bits_of_float const //Requires: jsoo_floor_log2, caml_int64_create_lo_mi_hi function caml_int64_bits_of_float(x) { - if (!isFinite(x)) { - if (isNaN(x)) return caml_int64_create_lo_mi_hi(1, 0, 0x7ff0); + 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); } - var sign = x == 0 && 1 / x == -Infinity ? 0x8000 : x >= 0 ? 0 : 0x8000; + const sign = + x == 0 && 1 / x == Number.NEGATIVE_INFINITY ? 0x8000 : x >= 0 ? 0 : 0x8000; if (sign) x = -x; // Int64.bits_of_float 1.1235582092889474E+307 = 0x7fb0000000000000L // using Math.LOG2E*Math.log(x) in place of Math.log2 result in precision lost - var exp = jsoo_floor_log2(x) + 1023; + let exp = jsoo_floor_log2(x) + 1023; if (exp <= 0) { exp = 0; x /= Math.pow(2, -1026); @@ -63,12 +64,12 @@ function caml_int64_bits_of_float(x) { x /= 2; } } - var k = Math.pow(2, 24); - var r3 = x | 0; + const k = Math.pow(2, 24); + let r3 = x | 0; x = (x - r3) * k; - var r2 = x | 0; + const r2 = x | 0; x = (x - r2) * k; - var r1 = x | 0; + const r1 = x | 0; r3 = (r3 & 0xf) | sign | (exp << 4); return caml_int64_create_lo_mi_hi(r1, r2, r3); } @@ -76,9 +77,9 @@ function caml_int64_bits_of_float(x) { //Provides: caml_int32_bits_of_float const //Requires: jsoo_floor_log2 function caml_int32_bits_of_float(x) { - var float32a = new Float32Array(1); + const float32a = new Float32Array(1); float32a[0] = x; - var int32a = new Int32Array(float32a.buffer); + const int32a = new Int32Array(float32a.buffer); return int32a[0] | 0; } @@ -88,13 +89,13 @@ function caml_int32_bits_of_float(x) { //Provides: caml_hexstring_of_float const //Requires: caml_string_of_jsstring, caml_str_repeat function caml_hexstring_of_float(x, prec, style) { - if (!isFinite(x)) { - if (isNaN(x)) return caml_string_of_jsstring("nan"); + if (!Number.isFinite(x)) { + if (Number.isNaN(x)) return caml_string_of_jsstring("nan"); return caml_string_of_jsstring(x > 0 ? "infinity" : "-infinity"); } - var sign = x == 0 && 1 / x == -Infinity ? 1 : x >= 0 ? 0 : 1; + const sign = x == 0 && 1 / x == Number.NEGATIVE_INFINITY ? 1 : x >= 0 ? 0 : 1; if (sign) x = -x; - var exp = 0; + let exp = 0; if (x == 0) { } else if (x < 1) { while (x < 1 && exp > -1022) { @@ -107,8 +108,8 @@ function caml_hexstring_of_float(x, prec, style) { exp++; } } - var exp_sign = exp < 0 ? "" : "+"; - var sign_str = ""; + const exp_sign = exp < 0 ? "" : "+"; + let sign_str = ""; if (sign) sign_str = "-"; else { switch (style) { @@ -124,16 +125,16 @@ function caml_hexstring_of_float(x, prec, style) { } if (prec >= 0 && prec < 13) { /* If a precision is given, and is small, round mantissa accordingly */ - var cst = Math.pow(2, prec * 4); + const cst = Math.pow(2, prec * 4); x = Math.round(x * cst) / cst; } - var x_str = x.toString(16); + let x_str = x.toString(16); if (prec >= 0) { - var idx = x_str.indexOf("."); + const idx = x_str.indexOf("."); if (idx < 0) { x_str += "." + caml_str_repeat(prec, "0"); } else { - var size = idx + 1 + prec; + const size = idx + 1 + prec; if (x_str.length < size) x_str += caml_str_repeat(size - x_str.length, "0"); else x_str = x_str.substr(0, size); @@ -146,16 +147,17 @@ function caml_hexstring_of_float(x, prec, style) { //Provides: caml_int64_float_of_bits const function caml_int64_float_of_bits(x) { - var lo = x.lo; - var mi = x.mi; - var hi = x.hi; - var exp = (hi & 0x7fff) >> 4; + const lo = x.lo; + const mi = x.mi; + const hi = x.hi; + const exp = (hi & 0x7fff) >> 4; if (exp == 2047) { - if ((lo | mi | (hi & 0xf)) == 0) return hi & 0x8000 ? -Infinity : Infinity; - else return NaN; + if ((lo | mi | (hi & 0xf)) == 0) + return hi & 0x8000 ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY; + else return Number.NaN; } - var k = Math.pow(2, -24); - var res = (lo * k + mi) * k + (hi & 0xf); + const k = Math.pow(2, -24); + let res = (lo * k + mi) * k + (hi & 0xf); if (exp > 0) { res += 16; res *= Math.pow(2, exp - 1027); @@ -167,14 +169,14 @@ function caml_int64_float_of_bits(x) { //Provides: caml_nextafter_float const //Requires: caml_int64_float_of_bits, caml_int64_bits_of_float, caml_int64_add, caml_int64_sub,caml_int64_of_int32 function caml_nextafter_float(x, y) { - if (isNaN(x) || isNaN(y)) return NaN; + if (Number.isNaN(x) || Number.isNaN(y)) return Number.NaN; if (x == y) return y; if (x == 0) { if (y < 0) return -Math.pow(2, -1074); else return Math.pow(2, -1074); } - var bits = caml_int64_bits_of_float(x); - var one = caml_int64_of_int32(1); + let bits = caml_int64_bits_of_float(x); + const one = caml_int64_of_int32(1); if (x < y == x > 0) bits = caml_int64_add(bits, one); else bits = caml_int64_sub(bits, one); return caml_int64_float_of_bits(bits); @@ -187,35 +189,35 @@ function caml_trunc_float(x) { //Provides: caml_int32_float_of_bits const function caml_int32_float_of_bits(x) { - var int32a = new Int32Array(1); + const int32a = new Int32Array(1); int32a[0] = x; - var float32a = new Float32Array(int32a.buffer); + const float32a = new Float32Array(int32a.buffer); return float32a[0]; } //Provides: caml_classify_float const function caml_classify_float(x) { - if (isFinite(x)) { + if (Number.isFinite(x)) { if (Math.abs(x) >= 2.2250738585072014e-308) return 0; if (x != 0) return 1; return 2; } - return isNaN(x) ? 4 : 3; + return Number.isNaN(x) ? 4 : 3; } //Provides: caml_modf_float const function caml_modf_float(x) { - if (isFinite(x)) { - var neg = 1 / x < 0; + if (Number.isFinite(x)) { + const neg = 1 / x < 0; x = Math.abs(x); - var i = Math.floor(x); - var f = x - i; + let i = Math.floor(x); + let f = x - i; if (neg) { i = -i; f = -f; } return [0, f, i]; } - if (isNaN(x)) return [0, NaN, NaN]; + if (Number.isNaN(x)) return [0, Number.NaN, Number.NaN]; return [0, 1 / x, x]; } //Provides: caml_ldexp_float const @@ -241,9 +243,9 @@ function caml_ldexp_float(x, exp) { //Requires: jsoo_floor_log2 function caml_frexp_float(x) { if (x == 0 || !isFinite(x)) return [0, x, 0]; - var neg = x < 0; + const neg = x < 0; if (neg) x = -x; - var exp = Math.max(-1023, jsoo_floor_log2(x) + 1); + let exp = Math.max(-1023, jsoo_floor_log2(x) + 1); x *= Math.pow(2, -exp); while (x < 0.5) { x *= 2; @@ -331,10 +333,10 @@ function caml_atanh_float(x) { //Provides: caml_round_float const function caml_round_float(x) { if (x >= 0) { - var y = Math.floor(x); + const y = Math.floor(x); return x - y >= 0.5 ? y + 1 : y; } else { - var y = Math.ceil(x); + const y = Math.ceil(x); return y - x >= 0.5 ? y - 1 : y; } } @@ -345,20 +347,20 @@ function caml_cbrt_float(x) { //Provides: caml_erf_float const function caml_erf_float(x) { - var a1 = 0.254829592; - var a2 = -0.284496736; - var a3 = 1.421413741; - var a4 = -1.453152027; - var a5 = 1.061405429; - var p = 0.3275911; + const a1 = 0.254829592; + const a2 = -0.284496736; + const a3 = 1.421413741; + const a4 = -1.453152027; + const a5 = 1.061405429; + const p = 0.3275911; - var sign = 1; + let sign = 1; if (x < 0) { sign = -1; } x = Math.abs(x); - var t = 1.0 / (1.0 + p * x); - var y = + const t = 1.0 / (1.0 + p * x); + const y = 1.0 - ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * Math.exp(-x * x); return sign * y; @@ -372,22 +374,22 @@ function caml_erfc_float(x) { //Provides: caml_fma_float const function caml_fma_float(x, y, z) { - var SPLIT = Math.pow(2, 27) + 1; - var MIN_VALUE = Math.pow(2, -1022); - var EPSILON = Math.pow(2, -52); - var C = 416; - var A = Math.pow(2, +C); - var B = Math.pow(2, -C); + const SPLIT = Math.pow(2, 27) + 1; + const MIN_VALUE = Math.pow(2, -1022); + const EPSILON = Math.pow(2, -52); + const C = 416; + const A = Math.pow(2, +C); + const B = Math.pow(2, -C); function multiply(a, b) { - var at = SPLIT * a; - var ahi = at - (at - a); - var alo = a - ahi; - var bt = SPLIT * b; - var bhi = bt - (bt - b); - var blo = b - bhi; - var p = a * b; - var e = ahi * bhi - p + ahi * blo + alo * bhi + alo * blo; + const at = SPLIT * a; + const ahi = at - (at - a); + const alo = a - ahi; + const bt = SPLIT * b; + const bhi = bt - (bt - b); + const blo = b - bhi; + const p = a * b; + const e = ahi * bhi - p + ahi * blo + alo * bhi + alo * blo; return { p: p, e: e, @@ -395,9 +397,9 @@ function caml_fma_float(x, y, z) { } function add(a, b) { - var s = a + b; - var v = s - a; - var e = a - (s - v) + (b - v); + const s = a + b; + const v = s - a; + const e = a - (s - v) + (b - v); return { s: s, e: e, @@ -429,7 +431,7 @@ function caml_fma_float(x, y, z) { return z; } - var scale = 1; + let scale = 1; while (Math.abs(x) > A) { scale *= A; x *= B; @@ -453,9 +455,9 @@ function caml_fma_float(x, y, z) { return z; } - var xs = x; - var ys = y; - var zs = z / scale; + const xs = x; + const ys = y; + let zs = z / scale; if (Math.abs(zs) > (Math.abs(xs * ys) * 4) / EPSILON) { return z; @@ -464,17 +466,17 @@ function caml_fma_float(x, y, z) { zs = (z < 0 ? -1 : +1) * MIN_VALUE; } - var xy = multiply(xs, ys); - var s = add(xy.p, zs); - var u = add(xy.e, s.e); - var i = add(s.s, u.s); + const xy = multiply(xs, ys); + const s = add(xy.p, zs); + const u = add(xy.e, s.e); + const i = add(s.s, u.s); - var f = i.s + adjust(i.e, u.e); + const f = i.s + adjust(i.e, u.e); if (f === 0) { return f; } - var fs = f * scale; + const fs = f * scale; if (Math.abs(fs) > MIN_VALUE) { return fs; } @@ -490,7 +492,7 @@ function caml_format_float(fmt, x) { if (Math.abs(x) < 1.0) { return x.toFixed(dp); } else { - var e = parseInt(x.toString().split("+")[1]); + let e = Number.parseInt(x.toString().split("+")[1]); if (e > 20) { e -= 20; x /= Math.pow(10, e); @@ -502,25 +504,25 @@ function caml_format_float(fmt, x) { } else return x.toFixed(dp); } } - var s, - f = caml_parse_format(fmt); - var prec = f.prec < 0 ? 6 : f.prec; - if (x < 0 || (x == 0 && 1 / x == -Infinity)) { + let s; + const f = caml_parse_format(fmt); + let prec = f.prec < 0 ? 6 : f.prec; + if (x < 0 || (x == 0 && 1 / x == Number.NEGATIVE_INFINITY)) { f.sign = -1; x = -x; } - if (isNaN(x)) { + if (Number.isNaN(x)) { s = "nan"; f.filler = " "; - } else if (!isFinite(x)) { + } else if (!Number.isFinite(x)) { s = "inf"; f.filler = " "; } else switch (f.conv) { case "e": { - var s = x.toExponential(prec); + s = x.toExponential(prec); // exponent should be at least two digits - var i = s.length; + const i = s.length; if (s.charAt(i - 3) == "e") s = s.slice(0, i - 1) + "0" + s.slice(i - 1); break; @@ -531,11 +533,11 @@ function caml_format_float(fmt, x) { case "g": { prec = prec ? prec : 1; s = x.toExponential(prec - 1); - var j = s.indexOf("e"); - var exp = +s.slice(j + 1); + const j = s.indexOf("e"); + const exp = +s.slice(j + 1); if (exp < -4 || x >= 1e21 || x.toFixed(0).length > prec) { // remove trailing zeroes - var i = j - 1; + let i = j - 1; while (s.charAt(i) == "0") i--; if (s.charAt(i) == ".") i--; s = s.slice(0, i + 1) + s.slice(j); @@ -544,14 +546,14 @@ function caml_format_float(fmt, x) { s = s.slice(0, i - 1) + "0" + s.slice(i - 1); break; } else { - var p = prec; + 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 - var i = s.length - 1; + let i = s.length - 1; while (s.charAt(i) == "0") i--; if (s.charAt(i) == ".") i--; s = s.slice(0, i + 1); @@ -566,23 +568,23 @@ function caml_format_float(fmt, x) { //Provides: caml_float_of_string (const) //Requires: caml_failwith, caml_jsbytes_of_string function caml_float_of_string(s) { - var res; + let res; s = caml_jsbytes_of_string(s); res = +s; if (s.length > 0 && res === res) return res; s = s.replace(/_/g, ""); res = +s; if ((s.length > 0 && res === res) || /^[+-]?nan$/i.test(s)) return res; - var m = /^ *([+-]?)0x([0-9a-f]+)\.?([0-9a-f]*)(p([+-]?[0-9]+))?/i.exec(s); + const m = /^ *([+-]?)0x([0-9a-f]+)\.?([0-9a-f]*)(p([+-]?[0-9]+))?/i.exec(s); // 1 2 3 5 if (m) { - var m3 = m[3].replace(/0+$/, ""); - var mantissa = parseInt(m[1] + m[2] + m3, 16); - var exponent = (m[5] | 0) - 4 * m3.length; + const m3 = m[3].replace(/0+$/, ""); + const mantissa = Number.parseInt(m[1] + m[2] + m3, 16); + const exponent = (m[5] | 0) - 4 * m3.length; res = mantissa * Math.pow(2, exponent); return res; } - if (/^\+?inf(inity)?$/i.test(s)) return Infinity; - if (/^-inf(inity)?$/i.test(s)) return -Infinity; + if (/^\+?inf(inity)?$/i.test(s)) return Number.POSITIVE_INFINITY; + if (/^-inf(inity)?$/i.test(s)) return Number.NEGATIVE_INFINITY; caml_failwith("float_of_string"); } diff --git a/runtime/int64.js b/runtime/int64.js index 9681954052..f5c7894d4c 100644 --- a/runtime/int64.js +++ b/runtime/int64.js @@ -18,7 +18,7 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. //Provides: caml_int64_offset -var caml_int64_offset = Math.pow(2, -24); +const caml_int64_offset = Math.pow(2, -24); //Provides: MlInt64 //Requires: caml_int64_offset, caml_raise_zero_divide @@ -42,8 +42,8 @@ MlInt64.prototype.ucompare = function (x) { return 0; }; MlInt64.prototype.compare = function (x) { - var hi = this.hi << 16; - var xhi = x.hi << 16; + const hi = this.hi << 16; + const xhi = x.hi << 16; if (hi > xhi) return 1; if (hi < xhi) return -1; if (this.mi > x.mi) return 1; @@ -53,27 +53,27 @@ MlInt64.prototype.compare = function (x) { return 0; }; MlInt64.prototype.neg = function () { - var lo = -this.lo; - var mi = -this.mi + (lo >> 24); - var hi = -this.hi + (mi >> 24); + const lo = -this.lo; + const mi = -this.mi + (lo >> 24); + const hi = -this.hi + (mi >> 24); return new MlInt64(lo, mi, hi); }; MlInt64.prototype.add = function (x) { - var lo = this.lo + x.lo; - var mi = this.mi + x.mi + (lo >> 24); - var hi = this.hi + x.hi + (mi >> 24); + const lo = this.lo + x.lo; + const mi = this.mi + x.mi + (lo >> 24); + const hi = this.hi + x.hi + (mi >> 24); return new MlInt64(lo, mi, hi); }; MlInt64.prototype.sub = function (x) { - var lo = this.lo - x.lo; - var mi = this.mi - x.mi + (lo >> 24); - var hi = this.hi - x.hi + (mi >> 24); + const lo = this.lo - x.lo; + const mi = this.mi - x.mi + (lo >> 24); + const hi = this.hi - x.hi + (mi >> 24); return new MlInt64(lo, mi, hi); }; MlInt64.prototype.mul = function (x) { - var lo = this.lo * x.lo; - var mi = ((lo * caml_int64_offset) | 0) + this.mi * x.lo + this.lo * x.mi; - var hi = + const lo = this.lo * x.lo; + const mi = ((lo * caml_int64_offset) | 0) + this.mi * x.lo + this.lo * x.mi; + const hi = ((mi * caml_int64_offset) | 0) + this.hi * x.lo + this.mi * x.mi + @@ -133,14 +133,14 @@ MlInt64.prototype.shift_right_unsigned = function (s) { MlInt64.prototype.shift_right = function (s) { s = s & 63; if (s == 0) return this; - var h = (this.hi << 16) >> 16; + const h = (this.hi << 16) >> 16; if (s < 24) return new MlInt64( (this.lo >> s) | (this.mi << (24 - s)), (this.mi >> s) | (h << (24 - s)), ((this.hi << 16) >> s) >>> 16, ); - var sign = (this.hi << 16) >> 31; + const sign = (this.hi << 16) >> 31; if (s < 48) return new MlInt64( (this.mi >> (s - 24)) | (this.hi << (48 - s)), @@ -160,10 +160,10 @@ MlInt64.prototype.lsr1 = function () { this.hi = this.hi >>> 1; }; MlInt64.prototype.udivmod = function (x) { - var offset = 0; - var modulus = this.copy(); - var divisor = x.copy(); - var quotient = new MlInt64(0, 0, 0); + let offset = 0; + let modulus = this.copy(); + const divisor = x.copy(); + const quotient = new MlInt64(0, 0, 0); while (modulus.ucompare(divisor) > 0) { offset++; divisor.lsl1(); @@ -180,22 +180,22 @@ MlInt64.prototype.udivmod = function (x) { return { quotient: quotient, modulus: modulus }; }; MlInt64.prototype.div = function (y) { - var x = this; + let x = this; if (y.isZero()) caml_raise_zero_divide(); - var sign = x.hi ^ y.hi; + const sign = x.hi ^ y.hi; if (x.hi & 0x8000) x = x.neg(); if (y.hi & 0x8000) y = y.neg(); - var q = x.udivmod(y).quotient; + let q = x.udivmod(y).quotient; if (sign & 0x8000) q = q.neg(); return q; }; MlInt64.prototype.mod = function (y) { - var x = this; + let x = this; if (y.isZero()) caml_raise_zero_divide(); - var sign = x.hi; + const sign = x.hi; if (x.hi & 0x8000) x = x.neg(); if (y.hi & 0x8000) y = y.neg(); - var r = x.udivmod(y).modulus; + let r = x.udivmod(y).modulus; if (sign & 0x8000) r = r.neg(); return r; }; @@ -340,22 +340,22 @@ function caml_int64_of_float(x) { //Requires: caml_int64_of_int32, caml_int64_to_int32 //Requires: caml_int64_is_zero, caml_str_repeat function caml_int64_format(fmt, x) { - var f = caml_parse_format(fmt); + const f = caml_parse_format(fmt); if (f.signedconv && caml_int64_is_negative(x)) { f.sign = -1; x = caml_int64_neg(x); } - var buffer = ""; - var wbase = caml_int64_of_int32(f.base); - var cvtbl = "0123456789abcdef"; + let buffer = ""; + const wbase = caml_int64_of_int32(f.base); + const cvtbl = "0123456789abcdef"; do { - var p = x.udivmod(wbase); + const p = x.udivmod(wbase); x = p.quotient; buffer = cvtbl.charAt(caml_int64_to_int32(p.modulus)) + buffer; } while (!caml_int64_is_zero(x)); if (f.prec >= 0) { f.filler = " "; - var n = f.prec - buffer.length; + const n = f.prec - buffer.length; if (n > 0) buffer = caml_str_repeat(n, "0") + buffer; } return caml_finish_formatting(f, buffer); @@ -367,19 +367,19 @@ function caml_int64_format(fmt, x) { //Requires: caml_int64_add, caml_int64_mul, caml_int64_neg //Requires: caml_ml_string_length,caml_string_unsafe_get, MlInt64 function caml_int64_of_string(s) { - var r = caml_parse_sign_and_base(s); - var i = r[0], - sign = r[1], - base = r[2], - signedness = r[3]; - var base64 = caml_int64_of_int32(base); - var threshold = new MlInt64(0xffffff, 0xfffffff, 0xffff).udivmod( + const r = caml_parse_sign_and_base(s); + let i = r[0]; + const sign = r[1]; + const base = r[2]; + const signedness = r[3]; + const base64 = caml_int64_of_int32(base); + const threshold = new MlInt64(0xffffff, 0xfffffff, 0xffff).udivmod( base64, ).quotient; - var c = caml_string_unsafe_get(s, i); - var d = caml_parse_digit(c); + let c = caml_string_unsafe_get(s, i); + let d = caml_parse_digit(c); if (d < 0 || d >= base) caml_failwith("int_of_string"); - var res = caml_int64_of_int32(d); + let res = caml_int64_of_int32(d); for (;;) { i++; c = caml_string_unsafe_get(s, i); diff --git a/runtime/internalMod.js b/runtime/internalMod.js index 4ece31318b..102606fff6 100644 --- a/runtime/internalMod.js +++ b/runtime/internalMod.js @@ -30,7 +30,7 @@ function caml_CamlinternalMod_init_mod(loc, shape) { switch (shape) { case 0: { //function - var dummy = caml_alloc_dummy_infix(); + const dummy = caml_alloc_dummy_infix(); dummy.fun = undef_module; struct[idx] = dummy; break; @@ -52,7 +52,7 @@ function caml_CamlinternalMod_init_mod(loc, shape) { struct[idx] = shape[1]; } } - var res = []; + const res = []; loop(shape, res, 0); return res[0]; } @@ -94,7 +94,7 @@ function caml_CamlinternalMod_init_mod(loc, shape, cont) { switch (shape) { case 0: { //function - var dummy = caml_alloc_dummy_infix(); + const dummy = caml_alloc_dummy_infix(); dummy.fun = undef_module; struct[idx] = dummy; break; @@ -116,7 +116,7 @@ function caml_CamlinternalMod_init_mod(loc, shape, cont) { struct[idx] = shape[1]; } } - var res = []; + const res = []; loop(shape, res, 0); return cont(res[0]); } diff --git a/runtime/ints.js b/runtime/ints.js index 91f633ae22..5aa7990202 100644 --- a/runtime/ints.js +++ b/runtime/ints.js @@ -21,17 +21,17 @@ function caml_format_int(fmt, i) { if (caml_jsbytes_of_string(fmt) == "%d") return caml_string_of_jsbytes("" + i); - var f = caml_parse_format(fmt); + const f = caml_parse_format(fmt); if (i < 0) { if (f.signedconv) { f.sign = -1; i = -i; } else i >>>= 0; } - var s = i.toString(f.base); + let s = i.toString(f.base); if (f.prec >= 0) { f.filler = " "; - var n = f.prec - s.length; + const n = f.prec - s.length; if (n > 0) s = caml_str_repeat(n, "0") + s; } return caml_finish_formatting(f, s); @@ -40,11 +40,11 @@ function caml_format_int(fmt, i) { //Provides: caml_parse_sign_and_base //Requires: caml_string_unsafe_get, caml_ml_string_length function caml_parse_sign_and_base(s) { - var i = 0, - len = caml_ml_string_length(s), - base = 10, - sign = 1, - signedness = 1; + let i = 0; + const len = caml_ml_string_length(s); + let base = 10; + let sign = 1; + let signedness = 1; if (len > 0) { switch (caml_string_unsafe_get(s, i)) { case 45: @@ -98,17 +98,17 @@ function caml_parse_digit(c) { //Requires: caml_ml_string_length, caml_string_unsafe_get //Requires: caml_parse_sign_and_base, caml_parse_digit, caml_failwith function caml_int_of_string(s) { - var r = caml_parse_sign_and_base(s); - var i = r[0], - sign = r[1], - base = r[2], - signedness = r[3]; - var len = caml_ml_string_length(s); - var threshold = -1 >>> 0; - var c = i < len ? caml_string_unsafe_get(s, i) : 0; - var d = caml_parse_digit(c); + const r = caml_parse_sign_and_base(s); + let i = r[0]; + const sign = r[1]; + const base = r[2]; + const signedness = r[3]; + const len = caml_ml_string_length(s); + const threshold = -1 >>> 0; + let c = i < len ? caml_string_unsafe_get(s, i) : 0; + let d = caml_parse_digit(c); if (d < 0 || d >= base) caml_failwith("int_of_string"); - var res = d; + let res = d; for (i++; i < len; i++) { c = caml_string_unsafe_get(s, i); if (c == 95) continue; @@ -163,6 +163,6 @@ function caml_int32_bswap(x) { //Provides: caml_int64_bswap //Requires: caml_int64_to_bytes, caml_int64_of_bytes function caml_int64_bswap(x) { - var y = caml_int64_to_bytes(x); + const y = caml_int64_to_bytes(x); return caml_int64_of_bytes([y[7], y[6], y[5], y[4], y[3], y[2], y[1], y[0]]); } diff --git a/runtime/io.js b/runtime/io.js index eb4774f03e..bfa176ef1a 100644 --- a/runtime/io.js +++ b/runtime/io.js @@ -20,12 +20,12 @@ ///////////// Io //Provides: caml_sys_fds -var caml_sys_fds = new Array(3); +const caml_sys_fds = new Array(3); //Provides: caml_sys_close //Requires: caml_sys_fds function caml_sys_close(fd) { - var file = caml_sys_fds[fd]; + const file = caml_sys_fds[fd]; if (file) file.close(); delete caml_sys_fds[fd]; return 0; @@ -47,7 +47,7 @@ function caml_sys_open_internal(file, idx) { return idx | 0; } function caml_sys_open(name, flags, _perms) { - var f = {}; + const f = {}; while (flags) { switch (flags[1]) { case 0: @@ -90,11 +90,11 @@ function caml_sys_open(name, flags, _perms) { caml_jsbytes_of_string(name) + " : flags Open_text and Open_binary are not compatible", ); - var root = resolve_fs_device(name); - var file = root.device.open(root.rest, f); + const root = resolve_fs_device(name); + const file = root.device.open(root.rest, f); return caml_sys_open_internal(file, undefined); } -(function () { +(() => { function file(fd, flags) { if (fs_node_supported()) { return caml_sys_open_for_node(fd, flags); @@ -119,19 +119,19 @@ function caml_sys_open(name, flags, _perms) { //Provides: caml_ml_set_channel_name //Requires: caml_ml_channel_get function caml_ml_set_channel_name(chanid, name) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); chan.name = name; return 0; } //Provides: caml_ml_channels -var caml_ml_channels = new Array(); +const caml_ml_channels = new Array(); //Provides: caml_ml_channel_redirect //Requires: caml_ml_channel_get, caml_ml_channels function caml_ml_channel_redirect(captured, into) { - var to_restore = caml_ml_channel_get(captured); - var new_ = caml_ml_channel_get(into); + const to_restore = caml_ml_channel_get(captured); + const new_ = caml_ml_channel_get(into); caml_ml_channels[captured] = new_; // XXX return to_restore; } @@ -152,7 +152,7 @@ function caml_ml_channel_get(id) { //Provides: caml_ml_out_channels_list //Requires: caml_ml_channels function caml_ml_out_channels_list() { - var l = 0; + let l = 0; for (let c = 0; c < caml_ml_channels.length; c++) { if ( caml_ml_channels[c] && @@ -169,10 +169,10 @@ function caml_ml_out_channels_list() { //Requires: caml_raise_sys_error //Requires: caml_sys_open function caml_ml_open_descriptor_out(fd) { - var file = caml_sys_fds[fd]; + const file = caml_sys_fds[fd]; if (file.flags.rdonly) caml_raise_sys_error("fd " + fd + " is readonly"); - var buffered = file.flags.buffered !== undefined ? file.flags.buffered : 1; - var channel = { + const buffered = file.flags.buffered !== undefined ? file.flags.buffered : 1; + const channel = { file: file, offset: file.flags.append ? file.length() : 0, fd: fd, @@ -191,10 +191,10 @@ function caml_ml_open_descriptor_out(fd) { //Requires: caml_raise_sys_error //Requires: caml_sys_open function caml_ml_open_descriptor_in(fd) { - var file = caml_sys_fds[fd]; + const file = caml_sys_fds[fd]; if (file.flags.wronly) caml_raise_sys_error("fd " + fd + " is writeonly"); - var refill = null; - var channel = { + const refill = null; + const channel = { file: file, offset: file.flags.append ? file.length() : 0, fd: fd, @@ -227,14 +227,14 @@ function caml_ml_open_descriptor_out_with_flags(fd, flags) { //Requires: caml_ml_channel_get //Alias: win_filedescr_of_channel function caml_channel_descriptor(chanid) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); return chan.fd; } //Provides: caml_ml_set_binary_mode //Requires: caml_ml_channel_get function caml_ml_set_binary_mode(chanid, mode) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); chan.file.flags.text = !mode; chan.file.flags.binary = mode; return 0; @@ -244,7 +244,7 @@ function caml_ml_set_binary_mode(chanid, mode) { //Requires: caml_ml_channel_get //Version: >= 5.2 function caml_ml_is_binary_mode(chanid) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); return chan.file.flags.binary; } @@ -254,7 +254,7 @@ function caml_ml_is_binary_mode(chanid) { //Requires: caml_ml_flush, caml_ml_channel_get //Requires: caml_sys_close function caml_ml_close_channel(chanid) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); if (chan.opened) { chan.opened = false; caml_sys_close(chan.fd); @@ -269,22 +269,22 @@ function caml_ml_close_channel(chanid) { //Provides: caml_ml_channel_size //Requires: caml_ml_channel_get function caml_ml_channel_size(chanid) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); return chan.file.length(); } //Provides: caml_ml_channel_size_64 //Requires: caml_int64_of_float,caml_ml_channel_get function caml_ml_channel_size_64(chanid) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); return caml_int64_of_float(chan.file.length()); } //Provides: caml_ml_set_channel_output //Requires: caml_ml_channel_get function caml_ml_set_channel_output(chanid, f) { - var chan = caml_ml_channel_get(chanid); - chan.output = function (s) { + const chan = caml_ml_channel_get(chanid); + chan.output = (s) => { f(s); }; return 0; @@ -301,13 +301,13 @@ function caml_ml_set_channel_refill(chanid, f) { //Requires: caml_ml_string_length, caml_uint8_array_of_string function caml_refill(chan) { if (chan.refill != null) { - var str = chan.refill(); - var str_a = caml_uint8_array_of_string(str); + const str = chan.refill(); + const str_a = caml_uint8_array_of_string(str); if (str_a.length == 0) { chan.refill = null; } else { if (chan.buffer.length < chan.buffer_max + str_a.length) { - var b = new Uint8Array(chan.buffer_max + str_a.length); + const b = new Uint8Array(chan.buffer_max + str_a.length); b.set(chan.buffer); chan.buffer = b; } @@ -316,7 +316,7 @@ function caml_refill(chan) { chan.buffer_max += str_a.length; } } else { - var nread = chan.file.read( + const nread = chan.file.read( chan.offset, chan.buffer, chan.buffer_max, @@ -331,7 +331,7 @@ function caml_refill(chan) { //Requires: caml_ml_input_block //Requires: caml_uint8_array_of_bytes function caml_ml_input(chanid, b, i, l) { - var ba = caml_uint8_array_of_bytes(b); + const ba = caml_uint8_array_of_bytes(b); return caml_ml_input_block(chanid, ba, i, l); } @@ -339,16 +339,16 @@ function caml_ml_input(chanid, b, i, l) { //Requires: caml_ml_input_block //Requires: caml_ba_to_typed_array function caml_ml_input_bigarray(chanid, b, i, l) { - var ba = caml_ba_to_typed_array(b); + const ba = caml_ba_to_typed_array(b); return caml_ml_input_block(chanid, ba, i, l); } //Provides: caml_ml_input_block //Requires: caml_refill, caml_ml_channel_get function caml_ml_input_block(chanid, ba, i, l) { - var chan = caml_ml_channel_get(chanid); - var n = l; - var avail = chan.buffer_max - chan.buffer_curr; + const chan = caml_ml_channel_get(chanid); + let n = l; + const avail = chan.buffer_max - chan.buffer_curr; if (l <= avail) { ba.set(chan.buffer.subarray(chan.buffer_curr, chan.buffer_curr + l), i); chan.buffer_curr += l; @@ -360,7 +360,7 @@ function caml_ml_input_block(chanid, ba, i, l) { chan.buffer_curr = 0; chan.buffer_max = 0; caml_refill(chan); - var avail = chan.buffer_max - chan.buffer_curr; + const avail = chan.buffer_max - chan.buffer_curr; if (n > avail) n = avail; ba.set(chan.buffer.subarray(chan.buffer_curr, chan.buffer_curr + n), i); chan.buffer_curr += n; @@ -373,10 +373,10 @@ function caml_ml_input_block(chanid, ba, i, l) { //Requires: caml_refill, caml_failwith, caml_raise_end_of_file //Requires: caml_marshal_header_size function caml_input_value(chanid) { - var chan = caml_ml_channel_get(chanid); - var header = new Uint8Array(caml_marshal_header_size); + const chan = caml_ml_channel_get(chanid); + const header = new Uint8Array(caml_marshal_header_size); function block(buffer, offset, n) { - var r = 0; + let r = 0; while (r < n) { if (chan.buffer_curr >= chan.buffer_max) { chan.buffer_curr = 0; @@ -390,16 +390,17 @@ function caml_input_value(chanid) { } return r; } - var r = block(header, 0, caml_marshal_header_size); - if (r == 0) caml_raise_end_of_file(); - else if (r < caml_marshal_header_size) + const r1 = block(header, 0, caml_marshal_header_size); + if (r1 == 0) caml_raise_end_of_file(); + else if (r1 < caml_marshal_header_size) caml_failwith("input_value: truncated object"); - var len = caml_marshal_data_size(caml_bytes_of_array(header), 0); - var buf = new Uint8Array(len + caml_marshal_header_size); + const len = caml_marshal_data_size(caml_bytes_of_array(header), 0); + const buf = new Uint8Array(len + caml_marshal_header_size); buf.set(header, 0); - var r = block(buf, caml_marshal_header_size, len); - if (r < len) caml_failwith("input_value: truncated object " + r + " " + len); - var res = caml_input_value_from_bytes(caml_bytes_of_array(buf), 0); + const r2 = block(buf, caml_marshal_header_size, len); + if (r2 < len) + caml_failwith("input_value: truncated object " + r2 + " " + len); + const res = caml_input_value_from_bytes(caml_bytes_of_array(buf), 0); return res; } @@ -413,14 +414,14 @@ function caml_input_value_to_outside_heap(c) { //Requires: caml_raise_end_of_file, caml_array_bound_error //Requires: caml_ml_channel_get, caml_refill function caml_ml_input_char(chanid) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); if (chan.buffer_curr >= chan.buffer_max) { chan.buffer_curr = 0; chan.buffer_max = 0; caml_refill(chan); } if (chan.buffer_curr >= chan.buffer_max) caml_raise_end_of_file(); - var res = chan.buffer[chan.buffer_curr]; + const res = chan.buffer[chan.buffer_curr]; chan.buffer_curr++; return res; } @@ -429,8 +430,8 @@ function caml_ml_input_char(chanid) { //Requires: caml_raise_end_of_file //Requires: caml_ml_input_char, caml_ml_channel_get function caml_ml_input_int(chanid) { - var chan = caml_ml_channel_get(chanid); - var res = 0; + const chan = caml_ml_channel_get(chanid); + let res = 0; for (let i = 0; i < 4; i++) { res = ((res << 8) + caml_ml_input_char(chanid)) | 0; } @@ -440,7 +441,7 @@ function caml_ml_input_int(chanid) { //Provides: caml_seek_in //Requires: caml_raise_sys_error, caml_ml_channel_get function caml_seek_in(chanid, pos) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); if (chan.refill != null) caml_raise_sys_error("Illegal seek"); if ( pos >= chan.offset - chan.buffer_max && @@ -465,14 +466,14 @@ function caml_ml_seek_in(chanid, pos) { //Provides: caml_ml_seek_in_64 //Requires: caml_int64_to_float, caml_seek_in function caml_ml_seek_in_64(chanid, pos) { - var pos = caml_int64_to_float(pos); - return caml_seek_in(chanid, pos); + const pos_ = caml_int64_to_float(pos); + return caml_seek_in(chanid, pos_); } //Provides: caml_pos_in //Requires: caml_ml_channel_get function caml_pos_in(chanid) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); return (chan.offset - (chan.buffer_max - chan.buffer_curr)) | 0; } @@ -492,8 +493,8 @@ function caml_ml_pos_in_64(chanid) { //Requires: caml_array_bound_error //Requires: caml_ml_channel_get, caml_refill function caml_ml_input_scan_line(chanid) { - var chan = caml_ml_channel_get(chanid); - var p = chan.buffer_curr; + const chan = caml_ml_channel_get(chanid); + let p = chan.buffer_curr; do { if (p >= chan.buffer_max) { if (chan.buffer_curr > 0) { @@ -505,7 +506,7 @@ function caml_ml_input_scan_line(chanid) { if (chan.buffer_max >= chan.buffer.length) { return -chan.buffer_max | 0; } - var prev_max = chan.buffer_max; + const prev_max = chan.buffer_max; caml_refill(chan); if (prev_max == chan.buffer_max) { return -chan.buffer_max | 0; @@ -519,7 +520,7 @@ function caml_ml_input_scan_line(chanid) { //Requires: caml_raise_sys_error, caml_ml_channel_get //Requires: caml_subarray_to_jsbytes function caml_ml_flush(chanid) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); if (!chan.opened) caml_raise_sys_error("Cannot flush a closed channel"); if (!chan.buffer || chan.buffer_curr == 0) return 0; if (chan.output) { @@ -538,11 +539,11 @@ function caml_ml_flush(chanid) { //Requires: caml_ml_flush,caml_ml_bytes_length //Requires: caml_raise_sys_error, caml_ml_channel_get function caml_ml_output_ta(chanid, buffer, offset, len) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); if (!chan.opened) caml_raise_sys_error("Cannot output to a closed channel"); buffer = buffer.subarray(offset, offset + len); if (chan.buffer_curr + buffer.length > chan.buffer.length) { - var b = new Uint8Array(chan.buffer_curr + buffer.length); + const b = new Uint8Array(chan.buffer_curr + buffer.length); b.set(chan.buffer); chan.buffer = b; } @@ -559,7 +560,7 @@ function caml_ml_output_ta(chanid, buffer, offset, len) { break; case 2: { // Buffered (only for stdout and stderr) - var id = buffer.lastIndexOf(10); + const id = buffer.lastIndexOf(10); if (id < 0) { chan.buffer.set(buffer, chan.buffer_curr); chan.buffer_curr += buffer.length; @@ -580,15 +581,15 @@ function caml_ml_output_ta(chanid, buffer, offset, len) { //Provides: caml_ml_output_bytes //Requires: caml_uint8_array_of_bytes, caml_ml_output_ta function caml_ml_output_bytes(chanid, buffer, offset, len) { - var buffer = caml_uint8_array_of_bytes(buffer); - return caml_ml_output_ta(chanid, buffer, offset, len); + const buffer_ = caml_uint8_array_of_bytes(buffer); + return caml_ml_output_ta(chanid, buffer_, offset, len); } //Provides: caml_ml_output_bigarray //Requires: caml_ba_to_typed_array, caml_ml_output_ta function caml_ml_output_bigarray(chanid, buffer, offset, len) { - var buffer = caml_ba_to_typed_array(buffer); - return caml_ml_output_ta(chanid, buffer, offset, len); + const buffer_ = caml_ba_to_typed_array(buffer); + return caml_ml_output_ta(chanid, buffer_, offset, len); } //Provides: caml_ml_output @@ -606,7 +607,7 @@ function caml_ml_output(chanid, buffer, offset, len) { //Requires: caml_ml_output //Requires: caml_string_of_jsbytes function caml_ml_output_char(chanid, c) { - var s = caml_string_of_jsbytes(String.fromCharCode(c)); + const s = caml_string_of_jsbytes(String.fromCharCode(c)); caml_ml_output(chanid, s, 0, 1); return 0; } @@ -614,7 +615,7 @@ function caml_ml_output_char(chanid, c) { //Provides: caml_output_value //Requires: caml_output_value_to_string, caml_ml_output,caml_ml_string_length function caml_output_value(chanid, v, flags) { - var s = caml_output_value_to_string(v, flags); + const s = caml_output_value_to_string(v, flags); caml_ml_output(chanid, s, 0, caml_ml_string_length(s)); return 0; } @@ -623,7 +624,7 @@ function caml_output_value(chanid, v, flags) { //Requires: caml_ml_channel_get, caml_ml_flush function caml_seek_out(chanid, pos) { caml_ml_flush(chanid); - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); chan.offset = pos; return 0; } @@ -636,14 +637,14 @@ function caml_ml_seek_out(chanid, pos) { //Provides: caml_ml_seek_out_64 //Requires: caml_int64_to_float, caml_seek_out function caml_ml_seek_out_64(chanid, pos) { - var pos = caml_int64_to_float(pos); - return caml_seek_out(chanid, pos); + const pos_ = caml_int64_to_float(pos); + return caml_seek_out(chanid, pos_); } //Provides: caml_pos_out //Requires: caml_ml_channel_get, caml_ml_flush function caml_pos_out(chanid) { - var chan = caml_ml_channel_get(chanid); + const chan = caml_ml_channel_get(chanid); return chan.offset + chan.buffer_curr; } @@ -663,8 +664,8 @@ function caml_ml_pos_out_64(chanid) { //Requires: caml_ml_output //Requires: caml_string_of_array function caml_ml_output_int(chanid, i) { - var arr = [(i >> 24) & 0xff, (i >> 16) & 0xff, (i >> 8) & 0xff, i & 0xff]; - var s = caml_string_of_array(arr); + const arr = [(i >> 24) & 0xff, (i >> 16) & 0xff, (i >> 8) & 0xff, i & 0xff]; + const s = caml_string_of_array(arr); caml_ml_output(chanid, s, 0, 4); return 0; } diff --git a/runtime/jslib.js b/runtime/jslib.js index d05143fadc..9555d47574 100644 --- a/runtime/jslib.js +++ b/runtime/jslib.js @@ -52,7 +52,7 @@ function caml_js_typeof(o) { //Provides:caml_trampoline function caml_trampoline(res) { - var c = 1; + let c = 1; while (res && res.joo_tramp) { res = res.joo_tramp.apply(null, res.joo_args); c++; @@ -67,7 +67,7 @@ function caml_trampoline_return(f, args) { //Provides:caml_stack_depth //If: effects -var caml_stack_depth = 0; +let caml_stack_depth = 0; //Provides:caml_stack_check_depth //If: effects @@ -79,7 +79,7 @@ function caml_stack_check_depth() { //Provides: caml_callback //If: !effects //Requires:caml_call_gen -var caml_callback = caml_call_gen; +const caml_callback = caml_call_gen; //Provides: caml_callback //If: effects @@ -89,7 +89,7 @@ function caml_callback(f, args) { function uncaught_effect_handler(eff, k, ms) { // Resumes the continuation k by raising exception Unhandled. caml_resume_stack(k[1], ms); - var exn = caml_named_value("Effect.Unhandled"); + let exn = caml_named_value("Effect.Unhandled"); if (exn) caml_raise_with_arg(exn, eff); else { exn = [ @@ -100,21 +100,21 @@ function caml_callback(f, args) { caml_raise_constant(exn); } } - var saved_stack_depth = caml_stack_depth; - var saved_exn_stack = caml_exn_stack; - var saved_fiber_stack = caml_fiber_stack; + const saved_stack_depth = caml_stack_depth; + const saved_exn_stack = caml_exn_stack; + const saved_fiber_stack = caml_fiber_stack; + let res = { + joo_tramp: f, + joo_args: args.concat((x) => { + return x; + }), + }; try { caml_exn_stack = 0; caml_fiber_stack = { h: [0, 0, 0, uncaught_effect_handler], r: { k: 0, x: 0, e: 0 }, }; - var res = { - joo_tramp: f, - joo_args: args.concat(function (x) { - return x; - }), - }; do { caml_stack_depth = 40; try { @@ -122,7 +122,7 @@ function caml_callback(f, args) { } catch (e) { /* Handle exception coming from JavaScript or from the runtime. */ if (!caml_exn_stack) throw e; - var handler = caml_exn_stack[1]; + const handler = caml_exn_stack[1]; caml_exn_stack = caml_exn_stack[2]; res = { joo_tramp: handler, joo_args: [caml_wrap_exception(e)] }; } @@ -155,7 +155,7 @@ function caml_jsoo_flags_effects(unit) { function caml_wrap_exception(e) { if (FLAG("excwrap")) { if (Array.isArray(e)) return e; - var exn; + let exn; //Stack_overflow: chrome, safari if ( globalThis.RangeError && @@ -246,8 +246,8 @@ function caml_js_from_array(a) { } //Provides: caml_js_to_array mutable (shallow) function caml_js_to_array(a) { - var len = a.length; - var b = new Array(len + 1); + const len = a.length; + const b = new Array(len + 1); b[0] = 0; for (let i = 0; i < len; i++) b[i + 1] = a[i]; return b; @@ -255,9 +255,9 @@ function caml_js_to_array(a) { //Provides: caml_list_of_js_array const (mutable) function caml_list_of_js_array(a) { - var l = 0; + let l = 0; for (let i = a.length - 1; i >= 0; i--) { - var e = a[i]; + const e = a[i]; l = [0, e, l]; } return l; @@ -265,7 +265,7 @@ function caml_list_of_js_array(a) { //Provides: caml_list_to_js_array const (mutable) function caml_list_to_js_array(l) { - var a = []; + const a = []; for (; l !== 0; l = l[2]) { a.push(l[1]); } @@ -275,17 +275,17 @@ function caml_list_to_js_array(l) { //Provides: caml_js_var mutable //Requires: caml_jsstring_of_string function caml_js_var(x) { - var x = caml_jsstring_of_string(x); + const x_ = caml_jsstring_of_string(x); //Checks that x has the form ident[.ident]* - if (!x.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*(\.[a-zA-Z_$][a-zA-Z_$0-9]*)*$/)) { + if (!x_.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*(\.[a-zA-Z_$][a-zA-Z_$0-9]*)*$/)) { console.error( 'caml_js_var: "' + - x + + x_ + '" is not a valid JavaScript variable. continuing ..', ); //console.error("Js.Unsafe.eval_string") } - return eval(x); + return eval(x_); } //Provides: caml_js_call (const, mutable, shallow) //Requires: caml_js_from_array @@ -379,14 +379,15 @@ function caml_ojs_new_arr(c, a) { //Requires: caml_callback function caml_js_wrap_callback(f) { return function () { - var len = arguments.length; + const len = arguments.length; + let args; if (len > 0) { - var args = new Array(len); + args = new Array(len); for (let i = 0; i < len; i++) args[i] = arguments[i]; } else { args = [undefined]; } - var res = caml_callback(f, args); + const res = caml_callback(f, args); return res instanceof Function ? caml_js_wrap_callback(res) : res; }; } @@ -395,8 +396,8 @@ function caml_js_wrap_callback(f) { //Requires: caml_callback function caml_js_wrap_callback_arguments(f) { return function () { - var len = arguments.length; - var args = new Array(len); + const len = arguments.length; + const args = new Array(len); for (let i = 0; i < len; i++) args[i] = arguments[i]; return caml_callback(f, [args]); }; @@ -405,9 +406,9 @@ function caml_js_wrap_callback_arguments(f) { //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); + 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 caml_callback(f, args); }; @@ -416,8 +417,8 @@ function caml_js_wrap_callback_strict(arity, f) { //Requires: caml_callback, caml_js_function_arity function caml_js_wrap_callback_unsafe(f) { return function () { - var len = caml_js_function_arity(f); - var args = new Array(len); + const len = caml_js_function_arity(f); + const args = new Array(len); for (let i = 0; i < len; i++) args[i] = arguments[i]; return caml_callback(f, args); }; @@ -426,11 +427,11 @@ function caml_js_wrap_callback_unsafe(f) { //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); + 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]; - var res = caml_callback(f, args); + const res = caml_callback(f, args); return res instanceof Function ? caml_js_wrap_callback(res) : res; }; } @@ -438,8 +439,8 @@ function caml_js_wrap_meth_callback(f) { //Requires: caml_callback function caml_js_wrap_meth_callback_arguments(f) { return function () { - var len = arguments.length; - var args = new Array(len); + const len = arguments.length; + const args = new Array(len); for (let i = 0; i < len; i++) args[i] = arguments[i]; return caml_callback(f, [this, args]); }; @@ -448,8 +449,8 @@ function caml_js_wrap_meth_callback_arguments(f) { //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); + 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); @@ -459,8 +460,8 @@ function caml_js_wrap_meth_callback_strict(arity, f) { //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); + 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); @@ -514,9 +515,9 @@ function caml_pure_js_expr(s) { //Provides: caml_js_object (object_literal) //Requires: caml_jsstring_of_string function caml_js_object(a) { - var o = {}; + const o = {}; for (let i = 1; i < a.length; i++) { - var p = a[i]; + const p = a[i]; o[caml_jsstring_of_string(p[1])] = p[2]; } return o; diff --git a/runtime/jslib_js_of_ocaml.js b/runtime/jslib_js_of_ocaml.js index f8782777f4..861354bc3a 100644 --- a/runtime/jslib_js_of_ocaml.js +++ b/runtime/jslib_js_of_ocaml.js @@ -21,7 +21,7 @@ //Provides: caml_js_on_ie const function caml_js_on_ie() { - var ua = + const ua = globalThis.navigator && globalThis.navigator.userAgent ? globalThis.navigator.userAgent : ""; @@ -29,7 +29,7 @@ function caml_js_on_ie() { } //Provides: caml_js_html_escape const (const) -var caml_js_regexps = { amp: /&/g, lt: /> 16; return a; @@ -29,18 +29,18 @@ function caml_lex_array(s) { //Provides: caml_lex_engine //Requires: caml_failwith, caml_lex_array, caml_uint8_array_of_bytes function caml_lex_engine(tbl, start_state, lexbuf) { - var lex_buffer = 2; - var lex_buffer_len = 3; - var lex_start_pos = 5; - var lex_curr_pos = 6; - var lex_last_pos = 7; - var lex_last_action = 8; - var lex_eof_reached = 9; - var lex_base = 1; - var lex_backtrk = 2; - var lex_default = 3; - var lex_trans = 4; - var lex_check = 5; + const lex_buffer = 2; + const lex_buffer_len = 3; + const lex_start_pos = 5; + const lex_curr_pos = 6; + const lex_last_pos = 7; + const lex_last_action = 8; + const lex_eof_reached = 9; + const lex_base = 1; + const lex_backtrk = 2; + const lex_default = 3; + const lex_trans = 4; + const lex_check = 5; if (!tbl.lex_default) { tbl.lex_base = caml_lex_array(tbl[lex_base]); @@ -50,10 +50,10 @@ function caml_lex_engine(tbl, start_state, lexbuf) { tbl.lex_default = caml_lex_array(tbl[lex_default]); } - var c, - state = start_state; + let c; + let state = start_state; - var buffer = caml_uint8_array_of_bytes(lexbuf[lex_buffer]); + const buffer = caml_uint8_array_of_bytes(lexbuf[lex_buffer]); if (state >= 0) { /* First entry */ @@ -65,10 +65,10 @@ function caml_lex_engine(tbl, start_state, lexbuf) { } for (;;) { /* Lookup base address or action number for current state */ - var base = tbl.lex_base[state]; + const base = tbl.lex_base[state]; if (base < 0) return -base - 1; /* See if it's a backtrack point */ - var backtrk = tbl.lex_backtrk[state]; + const backtrk = tbl.lex_backtrk[state]; if (backtrk >= 0) { lexbuf[lex_last_pos] = lexbuf[lex_curr_pos]; lexbuf[lex_last_action] = backtrk; @@ -108,10 +108,10 @@ function caml_lex_engine(tbl, start_state, lexbuf) { //Requires: caml_jsbytes_of_string, caml_uint8_array_of_bytes function caml_lex_run_mem(s, i, mem, curr_pos) { for (;;) { - var dst = s.charCodeAt(i); + const dst = s.charCodeAt(i); i++; if (dst == 0xff) return; - var src = s.charCodeAt(i); + const src = s.charCodeAt(i); i++; if (src == 0xff) mem[dst + 1] = curr_pos; else mem[dst + 1] = mem[src + 1]; @@ -120,10 +120,10 @@ function caml_lex_run_mem(s, i, mem, curr_pos) { function caml_lex_run_tag(s, i, mem) { for (;;) { - var dst = s.charCodeAt(i); + const dst = s.charCodeAt(i); i++; if (dst == 0xff) return; - var src = s.charCodeAt(i); + const src = s.charCodeAt(i); i++; if (src == 0xff) mem[dst + 1] = -1; else mem[dst + 1] = mem[src + 1]; @@ -131,25 +131,25 @@ function caml_lex_run_tag(s, i, mem) { } function caml_new_lex_engine(tbl, start_state, lexbuf) { - var lex_buffer = 2; - var lex_buffer_len = 3; - var lex_start_pos = 5; - var lex_curr_pos = 6; - var lex_last_pos = 7; - var lex_last_action = 8; - var lex_eof_reached = 9; - var lex_mem = 10; - var lex_base = 1; - var lex_backtrk = 2; - var lex_default = 3; - var lex_trans = 4; - var lex_check = 5; - var lex_base_code = 6; - var lex_backtrk_code = 7; - var lex_default_code = 8; - var lex_trans_code = 9; - var lex_check_code = 10; - var lex_code = 11; + const lex_buffer = 2; + const lex_buffer_len = 3; + const lex_start_pos = 5; + const lex_curr_pos = 6; + const lex_last_pos = 7; + const lex_last_action = 8; + const lex_eof_reached = 9; + const lex_mem = 10; + const lex_base = 1; + const lex_backtrk = 2; + const lex_default = 3; + const lex_trans = 4; + const lex_check = 5; + const lex_base_code = 6; + const lex_backtrk_code = 7; + const lex_default_code = 8; + const lex_trans_code = 9; + const lex_check_code = 10; + const lex_code = 11; if (!tbl.lex_default) { tbl.lex_base = caml_lex_array(tbl[lex_base]); @@ -168,10 +168,10 @@ function caml_new_lex_engine(tbl, start_state, lexbuf) { if (tbl.lex_code == null) tbl.lex_code = caml_jsbytes_of_string(tbl[lex_code]); - var c, - state = start_state; + let c; + let state = start_state; - var buffer = caml_uint8_array_of_bytes(lexbuf[lex_buffer]); + const buffer = caml_uint8_array_of_bytes(lexbuf[lex_buffer]); if (state >= 0) { /* First entry */ @@ -183,16 +183,16 @@ function caml_new_lex_engine(tbl, start_state, lexbuf) { } for (;;) { /* Lookup base address or action number for current state */ - var base = tbl.lex_base[state]; + const base = tbl.lex_base[state]; if (base < 0) { - var pc_off = tbl.lex_base_code[state]; + const pc_off = tbl.lex_base_code[state]; caml_lex_run_tag(tbl.lex_code, pc_off, lexbuf[lex_mem]); return -base - 1; } /* See if it's a backtrack point */ - var backtrk = tbl.lex_backtrk[state]; + const backtrk = tbl.lex_backtrk[state]; if (backtrk >= 0) { - var pc_off = tbl.lex_backtrk_code[state]; + const pc_off = tbl.lex_backtrk_code[state]; caml_lex_run_tag(tbl.lex_code, pc_off, lexbuf[lex_mem]); lexbuf[lex_last_pos] = lexbuf[lex_curr_pos]; lexbuf[lex_last_action] = backtrk; @@ -207,7 +207,7 @@ function caml_new_lex_engine(tbl, start_state, lexbuf) { lexbuf[lex_curr_pos]++; } /* Determine next state */ - var pstate = state; + const pstate = state; if (tbl.lex_check[base + c] == state) state = tbl.lex_trans[base + c]; else state = tbl.lex_default[state]; /* If no transition on this char, return to last backtrack point */ @@ -217,8 +217,8 @@ function caml_new_lex_engine(tbl, start_state, lexbuf) { else return lexbuf[lex_last_action]; } else { /* If some transition, get and perform memory moves */ - var base_code = tbl.lex_base_code[pstate], - pc_off; + const base_code = tbl.lex_base_code[pstate]; + let pc_off; if (tbl.lex_check_code[base_code + c] == pstate) pc_off = tbl.lex_trans_code[base_code + c]; else pc_off = tbl.lex_default_code[pstate]; diff --git a/runtime/marshal.js b/runtime/marshal.js index d004a8e5f9..d1344a381c 100644 --- a/runtime/marshal.js +++ b/runtime/marshal.js @@ -18,7 +18,7 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. //Provides: caml_marshal_constants -var caml_marshal_constants = { +const caml_marshal_constants = { PREFIX_SMALL_BLOCK: 0x80, PREFIX_SMALL_INT: 0x40, PREFIX_SMALL_STRING: 0x20, @@ -60,36 +60,36 @@ UInt8ArrayReader.prototype = { return (this.s[this.i++] << 24) >> 24; }, read16u: function () { - var s = this.s, - i = this.i; + const s = this.s; + const i = this.i; this.i = i + 2; return (s[i] << 8) | s[i + 1]; }, read16s: function () { - var s = this.s, - i = this.i; + const s = this.s; + const i = this.i; this.i = i + 2; return ((s[i] << 24) >> 16) | s[i + 1]; }, read32u: function () { - var s = this.s, - i = this.i; + const s = this.s; + const i = this.i; this.i = i + 4; return ((s[i] << 24) | (s[i + 1] << 16) | (s[i + 2] << 8) | s[i + 3]) >>> 0; }, read32s: function () { - var s = this.s, - i = this.i; + const s = this.s; + const i = this.i; this.i = i + 4; return (s[i] << 24) | (s[i + 1] << 16) | (s[i + 2] << 8) | s[i + 3]; }, readstr: function (len) { - var i = this.i; + const i = this.i; this.i = i + len; return caml_string_of_array(this.s.subarray(i, i + len)); }, readuint8array: function (len) { - var i = this.i; + const i = this.i; this.i = i + len; return this.s.subarray(i, i + len); }, @@ -109,20 +109,20 @@ MlStringReader.prototype = { return (this.s.charCodeAt(this.i++) << 24) >> 24; }, read16u: function () { - var s = this.s, - i = this.i; + const s = this.s; + const i = this.i; this.i = i + 2; return (s.charCodeAt(i) << 8) | s.charCodeAt(i + 1); }, read16s: function () { - var s = this.s, - i = this.i; + const s = this.s; + const i = this.i; this.i = i + 2; return ((s.charCodeAt(i) << 24) >> 16) | s.charCodeAt(i + 1); }, read32u: function () { - var s = this.s, - i = this.i; + const s = this.s; + const i = this.i; this.i = i + 4; return ( ((s.charCodeAt(i) << 24) | @@ -133,8 +133,8 @@ MlStringReader.prototype = { ); }, read32s: function () { - var s = this.s, - i = this.i; + const s = this.s; + const i = this.i; this.i = i + 4; return ( (s.charCodeAt(i) << 24) | @@ -144,14 +144,14 @@ MlStringReader.prototype = { ); }, readstr: function (len) { - var i = this.i; + const i = this.i; this.i = i + len; return caml_string_of_jsbytes(this.s.substring(i, i + len)); }, readuint8array: function (len) { - var b = new Uint8Array(len); - var s = this.s; - var i = this.i; + const b = new Uint8Array(len); + const s = this.s; + const i = this.i; for (let j = 0; j < len; j++) { b[j] = s.charCodeAt(i + j); } @@ -174,20 +174,20 @@ BigStringReader.prototype = { return (caml_ba_get_1(this.s, this.i++) << 24) >> 24; }, read16u: function () { - var s = this.s, - i = this.i; + const s = this.s; + const i = this.i; this.i = i + 2; return (caml_ba_get_1(s, i) << 8) | caml_ba_get_1(s, i + 1); }, read16s: function () { - var s = this.s, - i = this.i; + const s = this.s; + const i = this.i; this.i = i + 2; return ((caml_ba_get_1(s, i) << 24) >> 16) | caml_ba_get_1(s, i + 1); }, read32u: function () { - var s = this.s, - i = this.i; + const s = this.s; + const i = this.i; this.i = i + 4; return ( ((caml_ba_get_1(s, i) << 24) | @@ -198,8 +198,8 @@ BigStringReader.prototype = { ); }, read32s: function () { - var s = this.s, - i = this.i; + const s = this.s; + const i = this.i; this.i = i + 4; return ( (caml_ba_get_1(s, i) << 24) | @@ -209,8 +209,8 @@ BigStringReader.prototype = { ); }, readstr: function (len) { - var i = this.i; - var arr = new Array(len); + const i = this.i; + const arr = new Array(len); for (let j = 0; j < len; j++) { arr[j] = caml_ba_get_1(this.s, i + j); } @@ -218,8 +218,8 @@ BigStringReader.prototype = { return caml_string_of_array(arr); }, readuint8array: function (len) { - var i = this.i; - var offset = this.offset(i); + const i = this.i; + const offset = this.offset(i); this.i = i + len; return this.s.data.subarray(offset, offset + len); }, @@ -234,14 +234,14 @@ function caml_float_of_bytes(a) { //Provides: caml_input_value_from_string mutable //Requires: MlStringReader, caml_input_value_from_reader function caml_input_value_from_string(s, ofs) { - var reader = new MlStringReader(s, typeof ofs == "number" ? ofs : ofs[0]); + const reader = new MlStringReader(s, typeof ofs == "number" ? ofs : ofs[0]); return caml_input_value_from_reader(reader, ofs); } //Provides: caml_input_value_from_bytes mutable //Requires: MlStringReader, caml_input_value_from_reader, caml_string_of_bytes function caml_input_value_from_bytes(s, ofs) { - var reader = new MlStringReader( + const reader = new MlStringReader( caml_string_of_bytes(s), typeof ofs == "number" ? ofs : ofs[0], ); @@ -251,7 +251,7 @@ function caml_input_value_from_bytes(s, ofs) { //Provides: caml_int64_unmarshal //Requires: caml_int64_of_bytes function caml_int64_unmarshal(reader, size) { - var t = new Array(8); + const t = new Array(8); for (let j = 0; j < 8; j++) t[j] = reader.read8u(); size[0] = 8; return caml_int64_of_bytes(t); @@ -260,7 +260,7 @@ function caml_int64_unmarshal(reader, size) { //Provides: caml_int64_marshal //Requires: caml_int64_to_bytes function caml_int64_marshal(writer, v, sizes) { - var b = caml_int64_to_bytes(v); + const b = caml_int64_to_bytes(v); for (let i = 0; i < 8; i++) writer.write(8, b[i]); sizes[0] = 8; sizes[1] = 8; @@ -290,7 +290,7 @@ function caml_nativeint_unmarshal(reader, size) { //Requires: caml_int64_unmarshal, caml_int64_marshal, caml_int64_compare, caml_int64_hash //Requires: caml_int32_unmarshal, caml_nativeint_unmarshal //Requires: caml_ba_serialize, caml_ba_deserialize, caml_ba_compare, caml_ba_hash -var caml_custom_ops = { +const caml_custom_ops = { _j: { deserialize: caml_int64_unmarshal, serialize: caml_int64_marshal, @@ -307,17 +307,13 @@ var caml_custom_ops = { fixed_length: 4, }, _bigarray: { - deserialize: function (reader, sz) { - return caml_ba_deserialize(reader, sz, "_bigarray"); - }, + deserialize: (reader, sz) => caml_ba_deserialize(reader, sz, "_bigarray"), serialize: caml_ba_serialize, compare: caml_ba_compare, hash: caml_ba_hash, }, _bigarr02: { - deserialize: function (reader, sz) { - return caml_ba_deserialize(reader, sz, "_bigarr02"); - }, + deserialize: (reader, sz) => caml_ba_deserialize(reader, sz, "_bigarr02"), serialize: caml_ba_serialize, compare: caml_ba_compare, hash: caml_ba_hash, @@ -331,37 +327,44 @@ var caml_custom_ops = { //Requires: caml_decompress_input function caml_input_value_from_reader(reader, ofs) { function readvlq(overflow) { - var c = reader.read8u(); - var n = c & 0x7f; + let c = reader.read8u(); + let n = c & 0x7f; while ((c & 0x80) != 0) { c = reader.read8u(); - var n7 = n << 7; + const n7 = n << 7; if (n != n7 >> 7) overflow[0] = true; n = n7 | (c & 0x7f); } return n; } - var magic = reader.read32u(); + const magic = reader.read32u(); + let header_len; + let compressed; + let data_len; + let uncompressed_data_len; + let num_objects; + let _size_32; + let _size_64; switch (magic) { case 0x8495a6be /* Intext_magic_number_small */: { - var header_len = 20; - var compressed = 0; - var data_len = reader.read32u(); - var uncompressed_data_len = data_len; - var num_objects = reader.read32u(); - var _size_32 = reader.read32u(); - var _size_64 = reader.read32u(); + header_len = 20; + compressed = 0; + data_len = reader.read32u(); + uncompressed_data_len = data_len; + num_objects = reader.read32u(); + _size_32 = reader.read32u(); + _size_64 = reader.read32u(); break; } case 0x8495a6bd /* Intext_magic_number_compressed */: { - var header_len = reader.read8u() & 0x3f; - var compressed = 1; - var overflow = [false]; - var data_len = readvlq(overflow); - var uncompressed_data_len = readvlq(overflow); - var num_objects = readvlq(overflow); - var _size_32 = readvlq(overflow); - var _size_64 = readvlq(overflow); + header_len = reader.read8u() & 0x3f; + compressed = 1; + const overflow = [false]; + data_len = readvlq(overflow); + uncompressed_data_len = readvlq(overflow); + num_objects = readvlq(overflow); + _size_32 = readvlq(overflow); + _size_64 = readvlq(overflow); if (overflow[0]) { caml_failwith( "caml_input_value_from_reader: object too large to be read back on this platform", @@ -378,16 +381,16 @@ function caml_input_value_from_reader(reader, ofs) { caml_failwith("caml_input_value_from_reader: bad object"); break; } - var stack = []; - var intern_obj_table = num_objects > 0 ? [] : null; - var obj_counter = 0; + const stack = []; + const intern_obj_table = num_objects > 0 ? [] : null; + let obj_counter = 0; function intern_rec(reader) { - var code = reader.read8u(); + const code = reader.read8u(); if (code >= 0x40 /*cst.PREFIX_SMALL_INT*/) { if (code >= 0x80 /*cst.PREFIX_SMALL_BLOCK*/) { - var tag = code & 0xf; - var size = (code >> 4) & 0x7; - var v = [tag]; + const tag = code & 0xf; + const size = (code >> 4) & 0x7; + const v = [tag]; if (size == 0) return v; if (intern_obj_table) intern_obj_table[obj_counter++] = v; stack.push(v, size); @@ -395,8 +398,8 @@ function caml_input_value_from_reader(reader, ofs) { } else return code & 0x3f; } else { if (code >= 0x20 /*cst.PREFIX_SMALL_STRING */) { - var len = code & 0x1f; - var v = reader.readstr(len); + const len = code & 0x1f; + const v = reader.readstr(len); if (intern_obj_table) intern_obj_table[obj_counter++] = v; return v; } else { @@ -412,28 +415,28 @@ function caml_input_value_from_reader(reader, ofs) { break; case 0x04: { //cst.CODE_SHARED8: - var offset = reader.read8u(); + let offset = reader.read8u(); if (compressed == 0) offset = obj_counter - offset; return intern_obj_table[offset]; } case 0x05: { //cst.CODE_SHARED16: - var offset = reader.read16u(); + let offset = reader.read16u(); if (compressed == 0) offset = obj_counter - offset; return intern_obj_table[offset]; } case 0x06: { //cst.CODE_SHARED32: - var offset = reader.read32u(); + let offset = reader.read32u(); if (compressed == 0) offset = obj_counter - offset; return intern_obj_table[offset]; } case 0x08: { //cst.CODE_BLOCK32: - var header = reader.read32u(); - var tag = header & 0xff; - var size = header >> 10; - var v = [tag]; + 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); @@ -444,40 +447,40 @@ function caml_input_value_from_reader(reader, ofs) { break; case 0x09: { //cst.CODE_STRING8: - var len = reader.read8u(); - var v = reader.readstr(len); + 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: - var len = reader.read32u(); - var v = reader.readstr(len); + 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: - var t = new Array(8); + const t = new Array(8); for (let i = 0; i < 8; i++) t[7 - i] = reader.read8u(); - var v = caml_float_of_bytes(t); + 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: - var t = new Array(8); + const t = new Array(8); for (let i = 0; i < 8; i++) t[i] = reader.read8u(); - var v = caml_float_of_bytes(t); + 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: - var len = reader.read8u(); - var v = new Array(len + 1); + const len = reader.read8u(); + const v = new Array(len + 1); v[0] = 254; - var t = new Array(8); + 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(); @@ -487,10 +490,10 @@ function caml_input_value_from_reader(reader, ofs) { } case 0x0d: { //cst.CODE_DOUBLE_ARRAY8_BIG: - var len = reader.read8u(); - var v = new Array(len + 1); + const len = reader.read8u(); + const v = new Array(len + 1); v[0] = 254; - var t = new Array(8); + 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(); @@ -500,11 +503,11 @@ function caml_input_value_from_reader(reader, ofs) { } case 0x07: { //cst.CODE_DOUBLE_ARRAY32_LITTLE: - var len = reader.read32u(); - var v = new Array(len + 1); + const len = reader.read32u(); + const v = new Array(len + 1); v[0] = 254; if (intern_obj_table) intern_obj_table[obj_counter++] = v; - var t = new Array(8); + 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); @@ -513,10 +516,10 @@ function caml_input_value_from_reader(reader, ofs) { } case 0x0f: { //cst.CODE_DOUBLE_ARRAY32_BIG: - var len = reader.read32u(); - var v = new Array(len + 1); + const len = reader.read32u(); + const v = new Array(len + 1); v[0] = 254; - var t = new Array(8); + 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); @@ -531,11 +534,11 @@ function caml_input_value_from_reader(reader, ofs) { case 0x18: //cst.CODE_CUSTOM_LEN: case 0x19: { //cst.CODE_CUSTOM_FIXED: - var c, - s = ""; + let c; + let s = ""; while ((c = reader.read8u()) != 0) s += String.fromCharCode(c); - var ops = caml_custom_ops[s]; - var expected_size; + const ops = caml_custom_ops[s]; + let expected_size; if (!ops) caml_failwith("input_value: unknown custom block identifier"); switch (code) { @@ -555,9 +558,9 @@ function caml_input_value_from_reader(reader, ofs) { reader.read32s(); break; } - var old_pos = reader.i; - var size = [0]; - var v = ops.deserialize(reader, size); + 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( @@ -575,19 +578,19 @@ function caml_input_value_from_reader(reader, ofs) { } if (compressed) { if (caml_decompress_input) { - var data = reader.readuint8array(data_len); - var res = new Uint8Array(uncompressed_data_len); - var res = caml_decompress_input(data, res); - var reader = new UInt8ArrayReader(res, 0); + const data = reader.readuint8array(data_len); + let res = new Uint8Array(uncompressed_data_len); + res = caml_decompress_input(data, res); + reader = new UInt8ArrayReader(res, 0); } else { caml_failwith("input_value: compressed object, cannot decompress"); } } - var res = intern_rec(reader); + const res = intern_rec(reader); while (stack.length > 0) { - var size = stack.pop(); - var v = stack.pop(); - var d = v.length; + const size = stack.pop(); + const v = stack.pop(); + const d = v.length; if (d < size) stack.push(v, size); v[d] = intern_rec(reader); } @@ -596,11 +599,11 @@ function caml_input_value_from_reader(reader, ofs) { //Provides: caml_marshal_header_size //Version: < 5.1.0 -var caml_marshal_header_size = 20; +const caml_marshal_header_size = 20; //Provides: caml_marshal_header_size //Version: >= 5.1.0 -var caml_marshal_header_size = 16; +const caml_marshal_header_size = 16; //Provides: caml_marshal_data_size mutable //Requires: caml_failwith, caml_bytes_unsafe_get @@ -608,29 +611,31 @@ var caml_marshal_header_size = 16; //Requires: UInt8ArrayReader //Requires: caml_marshal_header_size function caml_marshal_data_size(s, ofs) { - var r = new UInt8ArrayReader(caml_uint8_array_of_bytes(s), ofs); + const r = new UInt8ArrayReader(caml_uint8_array_of_bytes(s), ofs); function readvlq(overflow) { - var c = r.read8u(); - var n = c & 0x7f; + let c = r.read8u(); + let n = c & 0x7f; while ((c & 0x80) != 0) { c = r.read8u(); - var n7 = n << 7; + const n7 = n << 7; if (n != n7 >> 7) overflow[0] = true; n = n7 | (c & 0x7f); } return n; } + let header_len; + let data_len; switch (r.read32u()) { case 0x8495a6be /* Intext_magic_number_small */: { - var header_len = 20; - var data_len = r.read32u(); + header_len = 20; + data_len = r.read32u(); break; } case 0x8495a6bd /* Intext_magic_number_compressed */: { - var header_len = r.read8u() & 0x3f; - var overflow = [false]; - var data_len = readvlq(overflow); + header_len = r.read8u() & 0x3f; + const overflow = [false]; + data_len = readvlq(overflow); if (overflow[0]) { caml_failwith( "Marshal.data_size: object too large to be read back on this platform", @@ -647,9 +652,9 @@ function caml_marshal_data_size(s, ofs) { } //Provides: MlObjectTable -var MlObjectTable; +let MlObjectTable; if (typeof globalThis.Map === "undefined") { - MlObjectTable = (function () { + MlObjectTable = (() => { /* polyfill (using linear search) */ function NaiveLookup(objs) { this.objs = objs; @@ -659,7 +664,7 @@ if (typeof globalThis.Map === "undefined") { if (this.objs[i] === v) return i; } }; - NaiveLookup.prototype.set = function () { + NaiveLookup.prototype.set = () => { // Do nothing here. [MlObjectTable.store] will push to [this.objs] directly. }; @@ -681,7 +686,7 @@ MlObjectTable.prototype.store = function (v) { }; MlObjectTable.prototype.recall = function (v) { - var i = this.lookup.get(v); + const i = this.lookup.get(v); return i === undefined ? undefined : this.objs.length - i; /* index is relative */ @@ -694,7 +699,7 @@ MlObjectTable.prototype.recall = function (v) { //Requires: caml_is_ml_string, caml_ml_string_length, caml_string_unsafe_get //Requires: MlObjectTable, caml_list_to_js_array, caml_custom_ops //Requires: caml_invalid_argument,caml_string_of_jsbytes, caml_is_continuation_tag -var caml_output_val = (function () { +const caml_output_val = (() => { function Writer() { this.chunk = []; } @@ -709,9 +714,9 @@ var caml_output_val = (function () { this.chunk[this.chunk_idx++] = (value >> i) & 0xff; }, write_at: function (pos, size, value) { - var pos = pos; + let pos_ = pos; for (let i = size - 8; i >= 0; i -= 8) - this.chunk[pos++] = (value >> i) & 0xff; + this.chunk[pos_++] = (value >> i) & 0xff; }, write_code: function (size, code, value) { this.chunk[this.chunk_idx++] = code; @@ -739,11 +744,11 @@ var caml_output_val = (function () { return this.chunk; }, }; - return function (v, flags) { + return (v, flags) => { flags = caml_list_to_js_array(flags); - var no_sharing = flags.indexOf(0 /*Marshal.No_sharing*/) !== -1, - closures = flags.indexOf(1 /*Marshal.Closures*/) !== -1; + const no_sharing = flags.indexOf(0 /*Marshal.No_sharing*/) !== -1; + const closures = flags.indexOf(1 /*Marshal.Closures*/) !== -1; /* Marshal.Compat_32 is redundant since integers are 32-bit anyway */ if (closures) @@ -751,13 +756,13 @@ var caml_output_val = (function () { "in caml_output_val: flag Marshal.Closures is not supported.", ); - var writer = new Writer(); - var stack = []; - var intern_obj_table = no_sharing ? null : new MlObjectTable(); + const writer = new Writer(); + const stack = []; + const intern_obj_table = no_sharing ? null : new MlObjectTable(); function memo(v) { if (no_sharing) return false; - var existing_offset = intern_obj_table.recall(v); + const existing_offset = intern_obj_table.recall(v); if (existing_offset) { writer.write_shared(existing_offset); return true; @@ -770,9 +775,9 @@ var caml_output_val = (function () { function extern_rec(v) { if (v.caml_custom) { if (memo(v)) return; - var name = v.caml_custom; - var ops = caml_custom_ops[name]; - var sz_32_64 = [0, 0]; + const name = v.caml_custom; + const ops = caml_custom_ops[name]; + const sz_32_64 = [0, 0]; if (!ops.serialize) caml_invalid_argument("output_value: abstract value (Custom)"); if (ops.fixed_length == undefined) { @@ -780,7 +785,7 @@ var caml_output_val = (function () { for (let i = 0; i < name.length; i++) writer.write(8, name.charCodeAt(i)); writer.write(8, 0); - var header_pos = writer.pos(); + const header_pos = writer.pos(); for (let i = 0; i < 12; i++) { writer.write(8, 0); } @@ -793,7 +798,7 @@ var caml_output_val = (function () { for (let i = 0; i < name.length; i++) writer.write(8, name.charCodeAt(i)); writer.write(8, 0); - var old_pos = writer.pos(); + const old_pos = writer.pos(); ops.serialize(writer, v, sz_32_64); if (ops.fixed_length != writer.pos() - old_pos) caml_failwith( @@ -830,7 +835,7 @@ var caml_output_val = (function () { ); } if (memo(v)) return; - var len = caml_ml_bytes_length(v); + const len = caml_ml_bytes_length(v); if (len < 0x20) writer.write(8, 0x20 /*cst.PREFIX_SMALL_STRING*/ + len); else if (len < 0x100) writer.write_code(8, 0x09 /*cst.CODE_STRING8*/, len); @@ -841,7 +846,7 @@ var caml_output_val = (function () { writer.size_64 += 1 + (((len + 8) / 8) | 0); } else if (caml_is_ml_string(v)) { if (memo(v)) return; - var len = caml_ml_string_length(v); + const len = caml_ml_string_length(v); if (len < 0x20) writer.write(8, 0x20 /*cst.PREFIX_SMALL_STRING*/ + len); else if (len < 0x100) writer.write_code(8, 0x09 /*cst.CODE_STRING8*/, len); @@ -852,7 +857,7 @@ var caml_output_val = (function () { writer.size_64 += 1 + (((len + 8) / 8) | 0); } else { if (v != (v | 0)) { - var type_of_v = typeof v; + const type_of_v = typeof v; if (type_of_v != "number") caml_failwith("output_value: abstract value (" + type_of_v + ")"); // If a float happens to be an integer it is serialized as an integer @@ -860,7 +865,7 @@ var caml_output_val = (function () { // float or integer.) This can result in unexpected crashes when // unmarshalling using the standard runtime. if (memo(v)) return; - var t = caml_int64_to_bytes(caml_int64_bits_of_float(v)); + const t = caml_int64_to_bytes(caml_int64_bits_of_float(v)); writer.write(8, 0x0c /*cst.CODE_DOUBLE_LITTLE*/); for (let i = 0; i < 8; i++) { writer.write(8, t[7 - i]); @@ -880,8 +885,8 @@ var caml_output_val = (function () { } extern_rec(v); while (stack.length > 0) { - var i = stack.pop(); - var v = stack.pop(); + const i = stack.pop(); + const v = stack.pop(); if (i + 1 < v.length) stack.push(v, i + 1); extern_rec(v[i]); } @@ -906,7 +911,7 @@ function caml_output_value_to_bytes(v, flags) { //Provides: caml_output_value_to_buffer //Requires: caml_output_val, caml_failwith, caml_blit_bytes function caml_output_value_to_buffer(s, ofs, len, v, flags) { - var t = caml_output_val(v, flags); + const t = caml_output_val(v, flags); if (t.length > len) caml_failwith("Marshal.to_buffer: buffer overflow"); caml_blit_bytes(t, 0, s, ofs, t.length); return 0; diff --git a/runtime/md5.js b/runtime/md5.js index 1b6f6425ad..10360a2444 100644 --- a/runtime/md5.js +++ b/runtime/md5.js @@ -22,17 +22,17 @@ //Requires: caml_raise_end_of_file, caml_ml_input_block //Requires: caml_MD5Init, caml_MD5Update, caml_MD5Final function caml_md5_chan(chanid, toread) { - var ctx = caml_MD5Init(); - var buffer = new Uint8Array(4096); + const ctx = caml_MD5Init(); + const buffer = new Uint8Array(4096); if (toread < 0) { while (true) { - var read = caml_ml_input_block(chanid, buffer, 0, buffer.length); + const read = caml_ml_input_block(chanid, buffer, 0, buffer.length); if (read == 0) break; caml_MD5Update(ctx, buffer.subarray(0, read), read); } } else { while (toread > 0) { - var read = caml_ml_input_block( + const read = caml_ml_input_block( chanid, buffer, 0, @@ -53,7 +53,7 @@ function caml_md5_string(s, ofs, len) { } //Provides: caml_MD5Transform -var caml_MD5Transform = (function () { +const caml_MD5Transform = (() => { function add(x, y) { return (x + y) | 0; } @@ -74,11 +74,11 @@ var caml_MD5Transform = (function () { return xx(c ^ (b | ~d), a, b, x, s, t); } - return function (w, buffer) { - var a = w[0], - b = w[1], - c = w[2], - d = w[3]; + return (w, buffer) => { + let a = w[0]; + let b = w[1]; + let c = w[2]; + let d = w[3]; a = ff(a, b, c, d, buffer[0], 7, 0xd76aa478); d = ff(d, a, b, c, buffer[1], 12, 0xe8c7b756); @@ -157,9 +157,9 @@ var caml_MD5Transform = (function () { //Provides: caml_MD5Init function caml_MD5Init() { - var buffer = new ArrayBuffer(64); - var b32 = new Uint32Array(buffer); - var b8 = new Uint8Array(buffer); + const buffer = new ArrayBuffer(64); + const b32 = new Uint32Array(buffer); + const b8 = new Uint8Array(buffer); return { len: 0, w: new Uint32Array([0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]), @@ -171,11 +171,11 @@ function caml_MD5Init() { //Provides: caml_MD5Update //Requires: caml_MD5Transform function caml_MD5Update(ctx, input, input_len) { - var in_buf = ctx.len & 0x3f; - var input_pos = 0; + const in_buf = ctx.len & 0x3f; + let input_pos = 0; ctx.len += input_len; if (in_buf) { - var missing = 64 - in_buf; + const missing = 64 - in_buf; if (input_len < missing) { ctx.b8.set(input.subarray(0, input_len), in_buf); return; @@ -198,7 +198,7 @@ function caml_MD5Update(ctx, input, input_len) { //Provides: caml_MD5Final //Requires: caml_MD5Transform function caml_MD5Final(ctx) { - var in_buf = ctx.len & 0x3f; + let in_buf = ctx.len & 0x3f; ctx.b8[in_buf] = 0x80; in_buf++; if (in_buf > 56) { @@ -217,7 +217,7 @@ function caml_MD5Final(ctx) { ctx.b32[14] = ctx.len << 3; ctx.b32[15] = (ctx.len >> 29) & 0x1fffffff; caml_MD5Transform(ctx.w, ctx.b32); - var t = new Uint8Array(16); + const t = new Uint8Array(16); for (let i = 0; i < 4; i++) for (let j = 0; j < 4; j++) t[i * 4 + j] = (ctx.w[i] >> (8 * j)) & 0xff; return t; @@ -227,8 +227,8 @@ function caml_MD5Final(ctx) { //Requires: caml_uint8_array_of_bytes, caml_string_of_array //Requires: caml_MD5Init, caml_MD5Update, caml_MD5Final function caml_md5_bytes(s, ofs, len) { - var ctx = caml_MD5Init(); - var a = caml_uint8_array_of_bytes(s); + const ctx = caml_MD5Init(); + const a = caml_uint8_array_of_bytes(s); caml_MD5Update(ctx, a.subarray(ofs, ofs + len), len); return caml_string_of_array(caml_MD5Final(ctx)); } diff --git a/runtime/mlBytes.js b/runtime/mlBytes.js index f99158c4a0..7fba868667 100644 --- a/runtime/mlBytes.js +++ b/runtime/mlBytes.js @@ -52,8 +52,8 @@ function caml_str_repeat(n, s) { if (s.repeat) { return s.repeat(n); } // ECMAscript 6 and Firefox 24+ - var r = "", - l = 0; + let r = ""; + let l = 0; for (;;) { if (n & 1) r += s; n >>= 1; @@ -74,9 +74,9 @@ function caml_str_repeat(n, s) { // In such setup, Typed_array would be implemented as polyfill, and [f.apply] would // fail here. Mark the primitive as Weakdef, so that people can override it easily. function caml_subarray_to_jsbytes(a, i, len) { - var f = String.fromCharCode; + const f = String.fromCharCode; if (i == 0 && len <= 4096 && len == a.length) return f.apply(null, a); - var s = ""; + let s = ""; for (; 0 < len; i += 1024, len -= 1024) s += f.apply(null, a.slice(i, i + Math.min(len, 1024))); return s; @@ -257,8 +257,8 @@ function caml_string_get(s, i) { //Requires: caml_ml_string_length function caml_string_get16(s, i) { if (i >>> 0 >= caml_ml_string_length(s) - 1) caml_string_bound_error(); - var b1 = caml_string_unsafe_get(s, i), - b2 = caml_string_unsafe_get(s, i + 1); + const b1 = caml_string_unsafe_get(s, i); + const b2 = caml_string_unsafe_get(s, i + 1); return (b2 << 8) | b1; } @@ -266,8 +266,8 @@ function caml_string_get16(s, i) { //Requires: caml_bytes_unsafe_get, caml_bytes_bound_error function caml_bytes_get16(s, i) { if (i >>> 0 >= s.l - 1) caml_bytes_bound_error(); - var b1 = caml_bytes_unsafe_get(s, i), - b2 = caml_bytes_unsafe_get(s, i + 1); + const b1 = caml_bytes_unsafe_get(s, i); + const b2 = caml_bytes_unsafe_get(s, i + 1); return (b2 << 8) | b1; } @@ -276,10 +276,10 @@ function caml_bytes_get16(s, i) { //Requires: caml_ml_string_length function caml_string_get32(s, i) { if (i >>> 0 >= caml_ml_string_length(s) - 3) caml_string_bound_error(); - var b1 = caml_string_unsafe_get(s, i), - b2 = caml_string_unsafe_get(s, i + 1), - b3 = caml_string_unsafe_get(s, i + 2), - b4 = caml_string_unsafe_get(s, i + 3); + const b1 = caml_string_unsafe_get(s, i); + const b2 = caml_string_unsafe_get(s, i + 1); + const b3 = caml_string_unsafe_get(s, i + 2); + const b4 = caml_string_unsafe_get(s, i + 3); return (b4 << 24) | (b3 << 16) | (b2 << 8) | b1; } @@ -287,10 +287,10 @@ function caml_string_get32(s, i) { //Requires: caml_bytes_unsafe_get, caml_bytes_bound_error function caml_bytes_get32(s, i) { if (i >>> 0 >= s.l - 3) caml_bytes_bound_error(); - var b1 = caml_bytes_unsafe_get(s, i), - b2 = caml_bytes_unsafe_get(s, i + 1), - b3 = caml_bytes_unsafe_get(s, i + 2), - b4 = caml_bytes_unsafe_get(s, i + 3); + const b1 = caml_bytes_unsafe_get(s, i); + const b2 = caml_bytes_unsafe_get(s, i + 1); + const b3 = caml_bytes_unsafe_get(s, i + 2); + const b4 = caml_bytes_unsafe_get(s, i + 3); return (b4 << 24) | (b3 << 16) | (b2 << 8) | b1; } @@ -300,7 +300,7 @@ function caml_bytes_get32(s, i) { //Requires: caml_ml_string_length function caml_string_get64(s, i) { if (i >>> 0 >= caml_ml_string_length(s) - 7) caml_string_bound_error(); - var a = new Array(8); + const a = new Array(8); for (let j = 0; j < 8; j++) { a[7 - j] = caml_string_unsafe_get(s, i + j); } @@ -312,7 +312,7 @@ function caml_string_get64(s, i) { //Requires: caml_int64_of_bytes function caml_bytes_get64(s, i) { if (i >>> 0 >= s.l - 7) caml_bytes_bound_error(); - var a = new Array(8); + const a = new Array(8); for (let j = 0; j < 8; j++) { a[7 - j] = caml_bytes_unsafe_get(s, i + j); } @@ -345,8 +345,8 @@ function caml_string_set(s, i, c) { //Requires: caml_bytes_bound_error, caml_bytes_unsafe_set function caml_bytes_set16(s, i, i16) { if (i >>> 0 >= s.l - 1) caml_bytes_bound_error(); - var b2 = 0xff & (i16 >> 8), - b1 = 0xff & i16; + const b2 = 0xff & (i16 >> 8); + const b1 = 0xff & i16; caml_bytes_unsafe_set(s, i + 0, b1); caml_bytes_unsafe_set(s, i + 1, b2); return 0; @@ -370,10 +370,10 @@ function caml_string_set16(s, i, i16) { //Requires: caml_bytes_bound_error, caml_bytes_unsafe_set function caml_bytes_set32(s, i, i32) { if (i >>> 0 >= s.l - 3) caml_bytes_bound_error(); - var b4 = 0xff & (i32 >> 24), - b3 = 0xff & (i32 >> 16), - b2 = 0xff & (i32 >> 8), - b1 = 0xff & i32; + const b4 = 0xff & (i32 >> 24); + const b3 = 0xff & (i32 >> 16); + const b2 = 0xff & (i32 >> 8); + const b1 = 0xff & i32; caml_bytes_unsafe_set(s, i + 0, b1); caml_bytes_unsafe_set(s, i + 1, b2); caml_bytes_unsafe_set(s, i + 2, b3); @@ -400,7 +400,7 @@ function caml_string_set32(s, i, i32) { //Requires: caml_int64_to_bytes function caml_bytes_set64(s, i, i64) { if (i >>> 0 >= s.l - 7) caml_bytes_bound_error(); - var a = caml_int64_to_bytes(i64); + const a = caml_int64_to_bytes(i64); for (let j = 0; j < 8; j++) { caml_bytes_unsafe_set(s, i + 7 - j, a[j]); } @@ -431,7 +431,7 @@ 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) { - var tag = 9 /* BYTES | ASCII */; + 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); @@ -461,12 +461,12 @@ MlBytes.prototype.toString = function () { } }; MlBytes.prototype.toUtf16 = function () { - var r = this.toString(); + const r = this.toString(); if (this.t == 9) return r; return caml_utf16_of_utf8(r); }; MlBytes.prototype.slice = function () { - var content = this.t == 4 ? this.c.slice() : this.c; + const content = this.t == 4 ? this.c.slice() : this.c; return new MlBytes(this.t, content, this.l); }; @@ -482,10 +482,10 @@ function caml_convert_string_to_bytes(s) { //Provides: caml_convert_bytes_to_array function caml_convert_bytes_to_array(s) { /* Assumes not ARRAY */ - var a = new Uint8Array(s.l); - var b = s.c, - l = b.length, - i = 0; + const a = new Uint8Array(s.l); + const b = s.c; + let l = b.length; + let i = 0; for (; i < l; i++) a[i] = b.charCodeAt(i); for (l = s.l; i < l; i++) a[i] = 0; s.c = a; @@ -504,9 +504,9 @@ function caml_uint8_array_of_bytes(s) { //Requires: caml_convert_bytes_to_array //Requires: caml_ml_string_length, caml_string_unsafe_get function caml_uint8_array_of_string(s) { - var l = caml_ml_string_length(s); - var a = new Uint8Array(l); - var i = 0; + const l = caml_ml_string_length(s); + const a = new Uint8Array(l); + let i = 0; for (; i < l; i++) a[i] = caml_string_unsafe_get(s, i); return a; } @@ -662,8 +662,8 @@ function caml_blit_bytes(s1, i1, s2, i2, len) { s2.t = s2.c.length == s2.l ? 0 /* BYTES | UNKOWN */ : 2; /* PARTIAL */ } else { if (s2.t != 4 /* ARRAY */) caml_convert_bytes_to_array(s2); - var c1 = s1.c, - c2 = s2.c; + const c1 = s1.c; + const c2 = s2.c; if (s1.t == 4 /* ARRAY */) { if (i2 <= i1) { for (let i = 0; i < len; i++) c2[i2 + i] = c1[i1 + i]; @@ -671,7 +671,7 @@ function caml_blit_bytes(s1, i1, s2, i2, len) { for (let i = len - 1; i >= 0; i--) c2[i2 + i] = c1[i1 + i]; } } else { - var l = Math.min(len, c1.length - i1); + const l = Math.min(len, c1.length - i1); let i = 0; for (; i < l; i++) c2[i2 + i] = c1.charCodeAt(i1 + i); for (; i < len; i++) c2[i2 + i] = 0; diff --git a/runtime/nat.js b/runtime/nat.js index 2e6d4de982..f0ad229e0d 100644 --- a/runtime/nat.js +++ b/runtime/nat.js @@ -26,8 +26,8 @@ MlNat.prototype.caml_custom = "_nat"; //Provides: caml_hash_nat //Requires: caml_hash_mix_int, num_digits_nat function caml_hash_nat(x) { - var len = num_digits_nat(x, 0, x.data.length); - var h = 0; + const len = num_digits_nat(x, 0, x.data.length); + let h = 0; for (let i = 0; i < len; i++) { h = caml_hash_mix_int(h, x.data[i]); } @@ -48,7 +48,7 @@ function nat_of_array(l) { //Provides: create_nat //Requires: MlNat function create_nat(size) { - var arr = new MlNat(size); + const arr = new MlNat(size); for (let i = 0; i < size; i++) { arr.data[i] = -1; } @@ -103,8 +103,8 @@ function num_digits_nat(nat, ofs, len) { //Provides: num_leading_zero_bits_in_digit function num_leading_zero_bits_in_digit(nat, ofs) { - var a = nat.data[ofs]; - var b = 0; + let a = nat.data[ofs]; + let b = 0; if (a & 0xffff0000) { b += 16; a >>>= 16; @@ -151,9 +151,9 @@ function is_digit_odd(nat, ofs) { //Provides: incr_nat function incr_nat(nat, ofs, len, carry_in) { - var carry = carry_in; + let carry = carry_in; for (let i = 0; i < len; i++) { - var x = (nat.data[ofs + i] >>> 0) + carry; + const x = (nat.data[ofs + i] >>> 0) + carry; nat.data[ofs + i] = x | 0; if (x == x >>> 0) { carry = 0; @@ -169,9 +169,9 @@ function incr_nat(nat, ofs, len, carry_in) { //Provides: add_nat //Requires: incr_nat function add_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { - var carry = carry_in; + let carry = carry_in; for (let i = 0; i < len2; i++) { - var x = (nat1.data[ofs1 + i] >>> 0) + (nat2.data[ofs2 + i] >>> 0) + carry; + const x = (nat1.data[ofs1 + i] >>> 0) + (nat2.data[ofs2 + i] >>> 0) + carry; nat1.data[ofs1 + i] = x; if (x == x >>> 0) { carry = 0; @@ -192,9 +192,9 @@ function complement_nat(nat, ofs, len) { // ocaml flips carry_in //Provides: decr_nat function decr_nat(nat, ofs, len, carry_in) { - var borrow = carry_in == 1 ? 0 : 1; + let borrow = carry_in == 1 ? 0 : 1; for (let i = 0; i < len; i++) { - var x = (nat.data[ofs + i] >>> 0) - borrow; + const x = (nat.data[ofs + i] >>> 0) - borrow; nat.data[ofs + i] = x; if (x >= 0) { borrow = 0; @@ -211,9 +211,10 @@ function decr_nat(nat, ofs, len, carry_in) { //Provides: sub_nat //Requires: decr_nat function sub_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { - var borrow = carry_in == 1 ? 0 : 1; + let borrow = carry_in == 1 ? 0 : 1; for (let i = 0; i < len2; i++) { - var x = (nat1.data[ofs1 + i] >>> 0) - (nat2.data[ofs2 + i] >>> 0) - borrow; + const x = + (nat1.data[ofs1 + i] >>> 0) - (nat2.data[ofs2 + i] >>> 0) - borrow; nat1.data[ofs1 + i] = x; if (x >= 0) { borrow = 0; @@ -229,16 +230,16 @@ function sub_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { //Provides: mult_digit_nat //Requires: add_nat, nat_of_array function mult_digit_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3) { - var carry = 0; - var a = nat3.data[ofs3] >>> 0; + let carry = 0; + const a = nat3.data[ofs3] >>> 0; for (let i = 0; i < len2; i++) { - var x1 = + const x1 = (nat1.data[ofs1 + i] >>> 0) + (nat2.data[ofs2 + i] >>> 0) * (a & 0x0000ffff) + carry; - var x2 = (nat2.data[ofs2 + i] >>> 0) * (a >>> 16); + const x2 = (nat2.data[ofs2 + i] >>> 0) * (a >>> 16); carry = Math.floor(x2 / 65536); - var x3 = x1 + (x2 % 65536) * 65536; + const x3 = x1 + (x2 % 65536) * 65536; nat1.data[ofs1 + i] = x3; carry += Math.floor(x3 / 4294967296); } @@ -263,7 +264,7 @@ function mult_digit_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3) { //Provides: mult_nat //Requires: mult_digit_nat function mult_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3, len3) { - var carry = 0; + let carry = 0; for (let i = 0; i < len3; i++) { carry += mult_digit_nat( nat1, @@ -284,7 +285,7 @@ function mult_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3, len3) { //Provides: square_nat //Requires: mult_nat, add_nat function square_nat(nat1, ofs1, len1, nat2, ofs2, len2) { - var carry = 0; + let carry = 0; carry += add_nat(nat1, ofs1, len1, nat1, ofs1, len1, 0); carry += mult_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat2, ofs2, len2); return carry; @@ -297,9 +298,9 @@ function shift_left_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { nat2.data[ofs2] = 0; return 0; } - var wrap = 0; + let wrap = 0; for (let i = 0; i < len1; i++) { - var a = nat1.data[ofs1 + i] >>> 0; + const a = nat1.data[ofs1 + i] >>> 0; nat1.data[ofs1 + i] = (a << nbits) | wrap; wrap = a >>> (32 - nbits); } @@ -310,10 +311,10 @@ function shift_left_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { // Assuming c > a, returns [quotient, remainder] of (a<<32 + b)/c //Provides: div_helper function div_helper(a, b, c) { - var x = a * 65536 + (b >>> 16); - var y = Math.floor(x / c) * 65536; - var z = (x % c) * 65536; - var w = z + (b & 0x0000ffff); + const x = a * 65536 + (b >>> 16); + const y = Math.floor(x / c) * 65536; + const z = (x % c) * 65536; + const w = z + (b & 0x0000ffff); return [y + Math.floor(w / c), w % c]; } @@ -321,11 +322,11 @@ function div_helper(a, b, c) { //Provides: div_digit_nat //Requires: div_helper function div_digit_nat(natq, ofsq, natr, ofsr, nat1, ofs1, len, nat2, ofs2) { - var rem = nat1.data[ofs1 + len - 1] >>> 0; + let rem = nat1.data[ofs1 + len - 1] >>> 0; // natq[ofsq+len-1] is guaranteed to be zero (due to the MSD requirement), // and should not be written to. for (let i = len - 2; i >= 0; i--) { - var x = div_helper(rem, nat1.data[ofs1 + i] >>> 0, nat2.data[ofs2] >>> 0); + const x = div_helper(rem, nat1.data[ofs1 + i] >>> 0, nat2.data[ofs2] >>> 0); natq.data[ofsq + i] = x[0]; rem = x[1]; } @@ -344,15 +345,15 @@ function div_nat(nat1, ofs1, len1, nat2, ofs2, len2) { return 0; } - var s = num_leading_zero_bits_in_digit(nat2, ofs2 + len2 - 1); + const s = num_leading_zero_bits_in_digit(nat2, ofs2 + len2 - 1); shift_left_nat(nat2, ofs2, len2, nat_of_array([0]), 0, s); shift_left_nat(nat1, ofs1, len1, nat_of_array([0]), 0, s); - var d = (nat2.data[ofs2 + len2 - 1] >>> 0) + 1; - var a = create_nat(len2 + 1); + const d = (nat2.data[ofs2 + len2 - 1] >>> 0) + 1; + const a = create_nat(len2 + 1); for (let i = len1 - 1; i >= len2; i--) { // Decent lower bound on quo - var quo = + let quo = d == 4294967296 ? nat1.data[ofs1 + i] >>> 0 : div_helper( @@ -387,9 +388,9 @@ function shift_right_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { nat2.data[ofs2] = 0; return 0; } - var wrap = 0; + let wrap = 0; for (let i = len1 - 1; i >= 0; i--) { - var a = nat1.data[ofs1 + i] >>> 0; + const a = nat1.data[ofs1 + i] >>> 0; nat1.data[ofs1 + i] = (a >>> nbits) | wrap; wrap = a << (32 - nbits); } @@ -407,8 +408,8 @@ function compare_digits_nat(nat1, ofs1, nat2, ofs2) { //Provides: compare_nat //Requires: num_digits_nat function compare_nat(nat1, ofs1, len1, nat2, ofs2, len2) { - var a = num_digits_nat(nat1, ofs1, len1); - var b = num_digits_nat(nat2, ofs2, len2); + const a = num_digits_nat(nat1, ofs1, len1); + const b = num_digits_nat(nat2, ofs2, len2); if (a > b) return 1; if (a < b) return -1; for (let i = len1 - 1; i >= 0; i--) { @@ -444,7 +445,7 @@ function lxor_digit_nat(nat1, ofs1, nat2, ofs2) { //Provides: serialize_nat function serialize_nat(writer, nat, sz) { - var len = nat.data.length; + const len = nat.data.length; writer.write(32, len); for (let i = 0; i < len; i++) { writer.write(32, nat.data[i]); @@ -456,8 +457,8 @@ function serialize_nat(writer, nat, sz) { //Provides: deserialize_nat //Requires: MlNat function deserialize_nat(reader, sz) { - var len = reader.read32s(); - var nat = new MlNat(len); + const len = reader.read32s(); + const nat = new MlNat(len); for (let i = 0; i < len; i++) { nat.data[i] = reader.read32s(); } diff --git a/runtime/obj.js b/runtime/obj.js index 012549c700..eb2b23e5b0 100644 --- a/runtime/obj.js +++ b/runtime/obj.js @@ -25,7 +25,7 @@ function caml_update_dummy(x, y) { x.fun = y; return 0; } - var i = y.length; + let i = y.length; while (i--) x[i] = y[i]; return 0; } @@ -61,7 +61,7 @@ function caml_obj_set_tag(x, tag) { } //Provides: caml_obj_block const (const,const) function caml_obj_block(tag, size) { - var o = new Array(size + 1); + const o = new Array(size + 1); o[0] = tag; for (let i = 1; i <= size; i++) o[i] = 0; return o; @@ -69,8 +69,8 @@ function caml_obj_block(tag, size) { //Provides: caml_obj_with_tag function caml_obj_with_tag(tag, x) { - var l = x.length; - var a = new Array(l); + const l = x.length; + const a = new Array(l); a[0] = tag; for (let i = 1; i < l; i++) a[i] = x[i]; return a; @@ -78,8 +78,8 @@ function caml_obj_with_tag(tag, x) { //Provides: caml_obj_dup mutable (mutable) function caml_obj_dup(x) { - var l = x.length; - var a = new Array(l); + const l = x.length; + const a = new Array(l); for (let i = 0; i < l; i++) a[i] = x[i]; return a; } @@ -120,10 +120,10 @@ function caml_lazy_make_forward(v) { ///////////// CamlinternalOO //Provides: caml_get_public_method const -var caml_method_cache = []; +const caml_method_cache = []; function caml_get_public_method(obj, tag, cacheid) { - var meths = obj[1]; - var ofs = caml_method_cache[cacheid]; + const meths = obj[1]; + const ofs = caml_method_cache[cacheid]; if (ofs === undefined) { // Make sure the array is not sparse for (let i = caml_method_cache.length; i < cacheid; i++) @@ -131,9 +131,9 @@ function caml_get_public_method(obj, tag, cacheid) { } else if (meths[ofs] === tag) { return meths[ofs - 1]; } - var li = 3, - hi = meths[1] * 2 + 1, - mi; + let li = 3; + let hi = meths[1] * 2 + 1; + let mi; while (li < hi) { mi = ((li + hi) >> 1) | 1; if (tag < meths[mi + 1]) hi = mi - 2; @@ -145,7 +145,7 @@ function caml_get_public_method(obj, tag, cacheid) { } //Provides: caml_oo_last_id -var caml_oo_last_id = 0; +let caml_oo_last_id = 0; //Provides: caml_set_oo_id //Requires: caml_oo_last_id diff --git a/runtime/parsing.js b/runtime/parsing.js index 89cf81c96f..18d2498ab4 100644 --- a/runtime/parsing.js +++ b/runtime/parsing.js @@ -18,81 +18,82 @@ /* The pushdown automata */ //Provides: caml_parser_trace -var caml_parser_trace = 0; +let caml_parser_trace = 0; //Provides: caml_parse_engine //Requires: caml_lex_array, caml_parser_trace,caml_jsstring_of_string //Requires: caml_ml_output, caml_ml_string_length, caml_string_of_jsbytes //Requires: caml_jsbytes_of_string, MlBytes function caml_parse_engine(tables, env, cmd, arg) { - var ERRCODE = 256; + const ERRCODE = 256; - //var START = 0; - //var TOKEN_READ = 1; - //var STACKS_GROWN_1 = 2; - //var STACKS_GROWN_2 = 3; - //var SEMANTIC_ACTION_COMPUTED = 4; - //var ERROR_DETECTED = 5; - var loop = 6; - var testshift = 7; - var shift = 8; - var shift_recover = 9; - var reduce = 10; + //const START = 0; + //const TOKEN_READ = 1; + //const STACKS_GROWN_1 = 2; + //const STACKS_GROWN_2 = 3; + //const SEMANTIC_ACTION_COMPUTED = 4; + //const ERROR_DETECTED = 5; + const loop = 6; + const testshift = 7; + const shift = 8; + const shift_recover = 9; + const reduce = 10; - var READ_TOKEN = 0; - var RAISE_PARSE_ERROR = 1; - var GROW_STACKS_1 = 2; - var GROW_STACKS_2 = 3; - var COMPUTE_SEMANTIC_ACTION = 4; - var CALL_ERROR_FUNCTION = 5; + const READ_TOKEN = 0; + const RAISE_PARSE_ERROR = 1; + const GROW_STACKS_1 = 2; + const GROW_STACKS_2 = 3; + const COMPUTE_SEMANTIC_ACTION = 4; + const CALL_ERROR_FUNCTION = 5; - var env_s_stack = 1; - var env_v_stack = 2; - var env_symb_start_stack = 3; - var env_symb_end_stack = 4; - var env_stacksize = 5; - var env_stackbase = 6; - var env_curr_char = 7; - var env_lval = 8; - var env_symb_start = 9; - var env_symb_end = 10; - var env_asp = 11; - var env_rule_len = 12; - var env_rule_number = 13; - var env_sp = 14; - var env_state = 15; - var env_errflag = 16; + const env_s_stack = 1; + const env_v_stack = 2; + const env_symb_start_stack = 3; + const env_symb_end_stack = 4; + const env_stacksize = 5; + const env_stackbase = 6; + const env_curr_char = 7; + const env_lval = 8; + const env_symb_start = 9; + const env_symb_end = 10; + const env_asp = 11; + const env_rule_len = 12; + const env_rule_number = 13; + const env_sp = 14; + const env_state = 15; + const env_errflag = 16; - // var _tbl_actions = 1; - var tbl_transl_const = 2; - var tbl_transl_block = 3; - var tbl_lhs = 4; - var tbl_len = 5; - var tbl_defred = 6; - var tbl_dgoto = 7; - var tbl_sindex = 8; - var tbl_rindex = 9; - var tbl_gindex = 10; - var tbl_tablesize = 11; - var tbl_table = 12; - var tbl_check = 13; - // var _tbl_error_function = 14; - var tbl_names_const = 15; - var tbl_names_block = 16; + // const _tbl_actions = 1; + const tbl_transl_const = 2; + const tbl_transl_block = 3; + const tbl_lhs = 4; + const tbl_len = 5; + const tbl_defred = 6; + const tbl_dgoto = 7; + const tbl_sindex = 8; + const tbl_rindex = 9; + const tbl_gindex = 10; + const tbl_tablesize = 11; + const tbl_table = 12; + const tbl_check = 13; + // const _tbl_error_function = 14; + const tbl_names_const = 15; + const tbl_names_block = 16; function log(x) { - var s = caml_string_of_jsbytes(x + "\n"); + const s = caml_string_of_jsbytes(x + "\n"); caml_ml_output(2, s, 0, caml_ml_string_length(s)); } function token_name(names, number) { - var str = caml_jsstring_of_string(names); + const str = caml_jsstring_of_string(names); if (str[0] == "\x00") return ""; return str.split("\x00")[number]; } function print_token(state, tok) { - var token, kind; + let token; + let kind; if (Array.isArray(tok)) { token = token_name(tables[tbl_names_block], tok[0]); if (typeof tok[1] == "number") kind = "" + tok[1]; @@ -118,16 +119,16 @@ function caml_parse_engine(tables, env, cmd, arg) { tables.dgoto = caml_lex_array(tables[tbl_dgoto]); } - var res = 0, - n, - n1, - n2, - state1; + let res = 0; + let n; + let n1; + let n2; + let state1; // RESTORE - var sp = env[env_sp]; - var state = env[env_state]; - var errflag = env[env_errflag]; + let sp = env[env_sp]; + let state = env[env_state]; + let errflag = env[env_errflag]; exit: for (;;) { next: switch (cmd) { @@ -252,7 +253,7 @@ function caml_parse_engine(tables, env, cmd, arg) { case 10: { //reduce: if (caml_parser_trace) log("State " + state + ": reduce by rule " + n); - var m = tables.len[n]; + let m = tables.len[n]; env[env_asp] = sp; env[env_rule_number] = n; env[env_rule_len] = m; @@ -284,7 +285,7 @@ function caml_parse_engine(tables, env, cmd, arg) { //SEMANTIC_ACTION_COMPUTED: env[env_s_stack][sp + 1] = state; env[env_v_stack][sp + 1] = arg; - var asp = env[env_asp]; + const asp = env[env_asp]; env[env_symb_end_stack][sp + 1] = env[env_symb_end_stack][asp + 1]; if (sp > asp) { /* This is an epsilon production. Take symb_start equal to symb_end. */ @@ -308,7 +309,7 @@ function caml_parse_engine(tables, env, cmd, arg) { //Provides: caml_set_parser_trace //Requires: caml_parser_trace function caml_set_parser_trace(bool) { - var oldflag = caml_parser_trace; + const oldflag = caml_parser_trace; caml_parser_trace = bool; return oldflag; } diff --git a/runtime/prng.js b/runtime/prng.js index 9bce1eafbd..27080d29a9 100644 --- a/runtime/prng.js +++ b/runtime/prng.js @@ -37,16 +37,15 @@ function caml_lxm_next(v) { function set(a, i, x) { return caml_ba_set_1(a, i, x); } - var M = caml_int64_of_string(caml_new_string("0xd1342543de82ef95")); - var daba = caml_int64_of_string(caml_new_string("0xdaba0b6eb09322e3")); - var z, q0, q1; - var st = v; - var a = get(st, 0); - var s = get(st, 1); - var x0 = get(st, 2); - var x1 = get(st, 3); + const M = caml_int64_of_string(caml_new_string("0xd1342543de82ef95")); + const daba = caml_int64_of_string(caml_new_string("0xdaba0b6eb09322e3")); + const st = v; + const a = get(st, 0); + const s = get(st, 1); + const x0 = get(st, 2); + const x1 = get(st, 3); /* Combining operation */ - z = add(s, x0); + let z = add(s, x0); /* Mixing function */ z = mul(xor(z, shift_r(z, 32)), daba); z = mul(xor(z, shift_r(z, 32)), daba); @@ -54,8 +53,8 @@ function caml_lxm_next(v) { /* LCG update */ set(st, 1, add(mul(s, M), a)); /* XBG update */ - var q0 = x0; - var q1 = x1; + let q0 = x0; + let q1 = x1; q1 = xor(q1, q0); q0 = rotl(q0, 24); q0 = xor(xor(q0, q1), shift_l(q1, 16)); diff --git a/runtime/runtime_events.js b/runtime/runtime_events.js index 1811ec9d17..0807ed2980 100644 --- a/runtime/runtime_events.js +++ b/runtime/runtime_events.js @@ -1,5 +1,5 @@ //Provides: caml_custom_event_index -var caml_custom_event_index = 0; +let caml_custom_event_index = 0; //Provides: caml_runtime_events_user_register //Requires: caml_custom_event_index diff --git a/runtime/stdlib.js b/runtime/stdlib.js index 957ec5407b..f4668fc54c 100644 --- a/runtime/stdlib.js +++ b/runtime/stdlib.js @@ -21,19 +21,20 @@ //If: !effects //Weakdef function caml_call_gen(f, args) { - var n = f.l >= 0 ? f.l : (f.l = f.length); - var argsLen = args.length; - var d = n - argsLen; + const n = f.l >= 0 ? f.l : (f.l = f.length); + const argsLen = args.length; + const d = n - argsLen; if (d == 0) return f.apply(null, args); else if (d < 0) { - var g = f.apply(null, args.slice(0, n)); + 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: { - var g = function (x) { - var nargs = new Array(argsLen + 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); @@ -41,8 +42,8 @@ function caml_call_gen(f, args) { break; } case 2: { - var g = function (x, y) { - var nargs = new Array(argsLen + 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; @@ -51,9 +52,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); + 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]; @@ -70,29 +71,30 @@ function caml_call_gen(f, args) { //If: effects //Weakdef function caml_call_gen(f, args) { - var n = f.l >= 0 ? f.l : (f.l = f.length); - var argsLen = args.length; - var d = n - argsLen; + const n = f.l >= 0 ? f.l : (f.l = f.length); + let argsLen = args.length; + const d = n - argsLen; if (d == 0) { return f.apply(null, args); } else if (d < 0) { - var rest = args.slice(n - 1); - var k = args[argsLen - 1]; + const rest = args.slice(n - 1); + const k = args[argsLen - 1]; args = args.slice(0, n); args[n - 1] = function (g) { if (typeof g !== "function") return k(g); - var args = rest.slice(); + const args = rest.slice(); args[args.length - 1] = k; return caml_call_gen(g, args); }; return f.apply(null, args); } else { argsLen--; - var k = args[argsLen]; + const k = args[argsLen]; + let g; switch (d) { case 1: { - var g = function (x, y) { - var nargs = new Array(argsLen + 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; @@ -101,8 +103,8 @@ function caml_call_gen(f, args) { break; } case 2: { - var g = function (x, y, z) { - var nargs = new Array(argsLen + 3); + 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; @@ -112,9 +114,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(argsLen + extra_args); + 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]; @@ -128,7 +130,7 @@ function caml_call_gen(f, args) { } //Provides: caml_named_values -var caml_named_values = {}; +const caml_named_values = {}; //Provides: caml_register_named_value (const,mutable) //Requires: caml_named_values, caml_jsbytes_of_string @@ -144,12 +146,12 @@ function caml_named_value(nm) { } //Provides: caml_global_data -var caml_global_data = [0]; +const caml_global_data = [0]; //Provides: caml_build_symbols //Requires: caml_jsstring_of_string function caml_build_symbols(symb) { - var r = {}; + const r = {}; if (symb) { for (let i = 1; i < symb.length; i++) { r[caml_jsstring_of_string(symb[i][1])] = symb[i][2]; @@ -163,14 +165,14 @@ function caml_build_symbols(symb) { //Requires: caml_failwith function caml_register_global(n, v, name_opt) { if (name_opt) { - var name = name_opt; + const name = name_opt; if (globalThis.toplevelReloc) { n = caml_callback(globalThis.toplevelReloc, [name]); } else if (caml_global_data.symbols) { if (!caml_global_data.symidx) { caml_global_data.symidx = caml_build_symbols(caml_global_data.symbols); } - var nid = caml_global_data.symidx[name]; + const nid = caml_global_data.symidx[name]; if (nid >= 0) n = nid; else { caml_failwith("caml_register_global: cannot locate " + name); diff --git a/runtime/stdlib_modern.js b/runtime/stdlib_modern.js index fb4f789573..9bb6910f27 100644 --- a/runtime/stdlib_modern.js +++ b/runtime/stdlib_modern.js @@ -19,19 +19,20 @@ //Provides: caml_call_gen (const, shallow) //If: !effects function caml_call_gen(f, args) { - var n = f.l >= 0 ? f.l : (f.l = f.length); - var argsLen = args.length; - var d = n - argsLen; + const n = f.l >= 0 ? f.l : (f.l = f.length); + const argsLen = args.length; + const d = n - argsLen; if (d == 0) return f(...args); else if (d < 0) { - var g = f(...args.slice(0, n)); + 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: { - var g = function (x) { - var nargs = new Array(argsLen + 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); @@ -39,8 +40,8 @@ function caml_call_gen(f, args) { break; } case 2: { - var g = function (x, y) { - var nargs = new Array(argsLen + 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; @@ -49,9 +50,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); + 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]; @@ -67,28 +68,29 @@ function caml_call_gen(f, args) { //Provides: caml_call_gen (const, shallow) //If: effects function caml_call_gen(f, args) { - var n = f.l >= 0 ? f.l : (f.l = f.length); - var argsLen = args.length; - var d = n - argsLen; + const n = f.l >= 0 ? f.l : (f.l = f.length); + let argsLen = args.length; + const d = n - argsLen; if (d == 0) return f(...args); else if (d < 0) { - var rest = args.slice(n - 1); - var k = args[argsLen - 1]; + const rest = args.slice(n - 1); + const k = args[argsLen - 1]; args = args.slice(0, n); args[n - 1] = function (g) { if (typeof g !== "function") return k(g); - var args = rest.slice(); + const args = rest.slice(); args[args.length - 1] = k; return caml_call_gen(g, args); }; return f(...args); } else { argsLen--; - var k = args[argsLen]; + const k = args[argsLen]; + let g; switch (d) { case 1: { - var g = function (x, y) { - var nargs = new Array(argsLen + 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; @@ -97,8 +99,8 @@ function caml_call_gen(f, args) { break; } case 2: { - var g = function (x, y, z) { - var nargs = new Array(argsLen + 3); + 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; @@ -108,9 +110,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(argsLen + extra_args); + 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]; diff --git a/runtime/str.js b/runtime/str.js index 1ec09c5929..9f2c1969f9 100644 --- a/runtime/str.js +++ b/runtime/str.js @@ -24,8 +24,8 @@ //Requires: caml_jsbytes_of_string, caml_js_from_array, caml_uint8_array_of_string //Requires: caml_string_get -var re_match = (function () { - var re_word_letters = [ +const re_match = (() => { + const re_word_letters = [ 0x00, 0x00, 0x00, 0x00 /* 0x00-0x1F: none */, 0x00, 0x00, 0xff, 0x03 /* 0x20-0x3F: digits 0-9 */, 0xfe, 0xff, 0xff, 0x87 /* 0x40-0x5F: A to Z, _ */, 0xfe, 0xff, 0xff, @@ -35,7 +35,7 @@ var re_match = (function () { 0xff /* 0xE0-0xFF: Latin-1 accented lowercase */, ]; - var opcodes = { + const opcodes = { CHAR: 0, CHARNORM: 1, STRING: 2, @@ -66,29 +66,29 @@ var re_match = (function () { } function re_match_impl(re, s, pos, partial) { - var prog = caml_js_from_array(re[1]), - cpool = caml_js_from_array(re[2]), - normtable = caml_jsbytes_of_string(re[3]), - numgroups = re[4] | 0, - numregisters = re[5] | 0, - startchars = re[6] | 0; + const prog = caml_js_from_array(re[1]); + const cpool = caml_js_from_array(re[2]); + const normtable = caml_jsbytes_of_string(re[3]); + const numgroups = re[4] | 0; + const numregisters = re[5] | 0; + const startchars = re[6] | 0; - var s = caml_uint8_array_of_string(s); + const s_ = caml_uint8_array_of_string(s); - var pc = 0, - quit = false, - stack = [], - groups = new Array(numgroups), - re_register = new Array(numregisters); + let pc = 0; + let quit = false; + const stack = []; + const groups = new Array(numgroups); + const re_register = new Array(numregisters); for (let i = 0; i < groups.length; i++) { groups[i] = { start: -1, end: -1 }; } groups[0].start = pos; - var backtrack = function () { + const backtrack = () => { while (stack.length) { - var item = stack.pop(); + const item = stack.pop(); if (item.undo) { item.undo.obj[item.undo.prop] = item.undo.value; } else if (item.pos) { @@ -100,16 +100,16 @@ var re_match = (function () { quit = true; }; - var push = function (item) { + const push = (item) => { stack.push(item); }; - var accept = function () { + const accept = () => { groups[0].end = pos; - var result = new Array(1 + groups.length * 2); + const result = new Array(1 + groups.length * 2); result[0] = 0; // tag for (let i = 0; i < groups.length; i++) { - var g = groups[i]; + const g = groups[i]; if (g.start < 0 || g.end < 0) { g.start = g.end = -1; } @@ -119,24 +119,24 @@ var re_match = (function () { return result; }; - var prefix_match = function () { + const prefix_match = () => { if (partial) return accept(); else backtrack(); }; /* Main DFA interpreter loop */ while (!quit) { - var op = prog[pc] & 0xff, - sarg = prog[pc] >> 8, - uarg = sarg & 0xff, - c = s[pos], - group; + const op = prog[pc] & 0xff; + const sarg = prog[pc] >> 8; + const uarg = sarg & 0xff; + let c = s_[pos]; + let group; pc++; switch (op) { case opcodes.CHAR: - if (pos === s.length) { + if (pos === s_.length) { prefix_match(); break; } @@ -144,7 +144,7 @@ var re_match = (function () { else backtrack(); break; case opcodes.CHARNORM: - if (pos === s.length) { + if (pos === s_.length) { prefix_match(); break; } @@ -153,15 +153,15 @@ var re_match = (function () { break; case opcodes.STRING: for ( - var arg = caml_jsbytes_of_string(cpool[uarg]), i = 0; + let arg = caml_jsbytes_of_string(cpool[uarg]), i = 0; i < arg.length; i++ ) { - if (pos === s.length) { + if (pos === s_.length) { prefix_match(); break; } - if (c === arg.charCodeAt(i)) c = s[++pos]; + if (c === arg.charCodeAt(i)) c = s_[++pos]; else { backtrack(); break; @@ -170,15 +170,15 @@ var re_match = (function () { break; case opcodes.STRINGNORM: for ( - var arg = caml_jsbytes_of_string(cpool[uarg]), i = 0; + let arg = caml_jsbytes_of_string(cpool[uarg]), i = 0; i < arg.length; i++ ) { - if (pos === s.length) { + if (pos === s_.length) { prefix_match(); break; } - if (normtable.charCodeAt(c) === arg.charCodeAt(i)) c = s[++pos]; + if (normtable.charCodeAt(c) === arg.charCodeAt(i)) c = s_[++pos]; else { backtrack(); break; @@ -186,7 +186,7 @@ var re_match = (function () { } break; case opcodes.CHARCLASS: - if (pos === s.length) { + if (pos === s_.length) { prefix_match(); break; } @@ -194,28 +194,28 @@ var re_match = (function () { else backtrack(); break; case opcodes.BOL: - if (pos > 0 && s[pos - 1] != 10 /* \n */) { + if (pos > 0 && s_[pos - 1] != 10 /* \n */) { backtrack(); } break; case opcodes.EOL: - if (pos < s.length && s[pos] != 10 /* \n */) { + if (pos < s_.length && s_[pos] != 10 /* \n */) { backtrack(); } break; case opcodes.WORDBOUNDARY: if (pos == 0) { - if (pos === s.length) { + if (pos === s_.length) { prefix_match(); break; } - if (is_word_letter(s[0])) break; + if (is_word_letter(s_[0])) break; backtrack(); - } else if (pos === s.length) { - if (is_word_letter(s[pos - 1])) break; + } else if (pos === s_.length) { + if (is_word_letter(s_[pos - 1])) break; backtrack(); } else { - if (is_word_letter(s[pos - 1]) != is_word_letter(s[pos])) break; + if (is_word_letter(s_[pos - 1]) != is_word_letter(s_[pos])) break; backtrack(); } break; @@ -236,11 +236,11 @@ var re_match = (function () { break; } for (let i = group.start; i < group.end; i++) { - if (pos === s.length) { + if (pos === s_.length) { prefix_match(); break; } - if (s[i] != s[pos]) { + if (s_[i] != s_[pos]) { backtrack(); break; } @@ -251,16 +251,16 @@ var re_match = (function () { if (in_bitset(cpool[uarg], c)) pos++; break; case opcodes.SIMPLESTAR: - while (in_bitset(cpool[uarg], c)) c = s[++pos]; + while (in_bitset(cpool[uarg], c)) c = s_[++pos]; break; case opcodes.SIMPLEPLUS: - if (pos === s.length) { + if (pos === s_.length) { prefix_match(); break; } if (in_bitset(cpool[uarg], c)) { do { - c = s[++pos]; + c = s_[++pos]; } while (in_bitset(cpool[uarg], c)); } else backtrack(); break; @@ -297,7 +297,7 @@ function re_search_forward(re, s, pos) { if (pos < 0 || pos > caml_ml_string_length(s)) caml_invalid_argument("Str.search_forward"); while (pos <= caml_ml_string_length(s)) { - var res = re_match(re, s, pos, 0); + const res = re_match(re, s, pos, 0); if (res) return res; pos++; } @@ -311,7 +311,7 @@ function re_search_backward(re, s, pos) { if (pos < 0 || pos > caml_ml_string_length(s)) caml_invalid_argument("Str.search_backward"); while (pos >= 0) { - var res = re_match(re, s, pos, 0); + const res = re_match(re, s, pos, 0); if (res) return res; pos--; } @@ -324,7 +324,7 @@ function re_search_backward(re, s, pos) { function re_string_match(re, s, pos) { if (pos < 0 || pos > caml_ml_string_length(s)) caml_invalid_argument("Str.string_match"); - var res = re_match(re, s, pos, 0); + const res = re_match(re, s, pos, 0); if (res) return res; else return [0]; } @@ -334,7 +334,7 @@ function re_string_match(re, s, pos) { function re_partial_match(re, s, pos) { if (pos < 0 || pos > caml_ml_string_length(s)) caml_invalid_argument("Str.partial_match"); - var res = re_match(re, s, pos, 1); + const res = re_match(re, s, pos, 1); if (res) return res; else return [0]; } @@ -345,20 +345,22 @@ function re_partial_match(re, s, pos) { //Requires: caml_failwith // external re_replacement_text: string -> int array -> string -> string function re_replacement_text(repl, groups, orig) { - var repl = caml_jsbytes_of_string(repl); - var len = repl.length; - var orig = caml_jsbytes_of_string(orig); - var res = ""; //result - var n = 0; // current position - var cur; //current char - var start, end, c; + const repl_ = caml_jsbytes_of_string(repl); + const len = repl_.length; + const orig_ = caml_jsbytes_of_string(orig); + let res = ""; //result + let n = 0; // current position + let cur; //current char + let start; + let end; + let c; while (n < len) { - cur = repl.charAt(n++); + cur = repl_.charAt(n++); if (cur != "\\") { res += cur; } else { if (n == len) caml_failwith("Str.replace: illegal backslash sequence"); - cur = repl.charAt(n++); + cur = repl_.charAt(n++); switch (cur) { case "\\": res += cur; @@ -380,7 +382,7 @@ function re_replacement_text(repl, groups, orig) { end = caml_array_get(groups, c * 2 + 1); if (start == -1) caml_failwith("Str.replace: reference to unmatched group"); - res += orig.slice(start, end); + res += orig_.slice(start, end); break; default: res += "\\" + cur; diff --git a/runtime/sys.js b/runtime/sys.js index a7044d1cc8..9830ef3214 100644 --- a/runtime/sys.js +++ b/runtime/sys.js @@ -48,24 +48,26 @@ function caml_is_special_exception(exn) { //Provides: caml_format_exception //Requires: MlBytes, caml_is_special_exception function caml_format_exception(exn) { - var r = ""; + let r = ""; if (exn[0] == 0) { + let bucket; + let start; r += exn[1][1]; if ( exn.length == 3 && exn[2][0] == 0 && caml_is_special_exception(exn[1]) ) { - var bucket = exn[2]; - var start = 1; + bucket = exn[2]; + start = 1; } else { - var start = 2; - var bucket = exn; + bucket = exn; + start = 2; } r += "("; for (let i = start; i < bucket.length; i++) { if (i > start) r += ", "; - var v = bucket[i]; + const v = bucket[i]; if (typeof v == "number") r += v.toString(); else if (v instanceof MlBytes) { r += '"' + v.toString() + '"'; @@ -84,11 +86,11 @@ function caml_format_exception(exn) { //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)) { - var handler = caml_named_value("Printexc.handle_uncaught_exception"); + const handler = caml_named_value("Printexc.handle_uncaught_exception"); if (handler) caml_callback(handler, [err, false]); else { - var msg = caml_format_exception(err); - var at_exit = caml_named_value("Pervasives.do_at_exit"); + const msg = caml_format_exception(err); + const at_exit = caml_named_value("Pervasives.do_at_exit"); if (at_exit) caml_callback(at_exit, [0]); console.error("Fatal error: exception " + msg); if (err.js_error) throw err.js_error; @@ -107,7 +109,7 @@ function caml_set_static_env(k, v) { //Provides: jsoo_sys_getenv (const) function jsoo_sys_getenv(n) { - var process = globalThis.process; + const process = globalThis.process; //nodejs env if (process && process.env && process.env[n] != undefined) return process.env[n]; @@ -121,7 +123,7 @@ function jsoo_sys_getenv(n) { //Requires: caml_jsstring_of_string //Requires: jsoo_sys_getenv function caml_sys_getenv(name) { - var r = jsoo_sys_getenv(caml_jsstring_of_string(name)); + const r = jsoo_sys_getenv(caml_jsstring_of_string(name)); if (r === undefined) caml_raise_not_found(); return caml_string_of_jsstring(r); } @@ -134,20 +136,20 @@ function caml_sys_unsafe_getenv(name) { //Provides: caml_argv //Requires: caml_string_of_jsstring -var caml_argv = (function () { - var process = globalThis.process; - var main = "a.out"; - var args = []; +let caml_argv = (() => { + const process = globalThis.process; + let main = "a.out"; + let args = []; if (process && process.argv && process.argv.length > 1) { - var argv = process.argv; + const argv = process.argv; //nodejs main = argv[1]; args = argv.slice(2); } - var p = caml_string_of_jsstring(main); - var args2 = [0, p]; + const p = caml_string_of_jsstring(main); + const args2 = [0, p]; for (let i = 0; i < args.length; i++) args2.push(caml_string_of_jsstring(args[i])); return args2; @@ -155,7 +157,7 @@ var caml_argv = (function () { //Provides: caml_executable_name //Requires: caml_argv -var caml_executable_name = caml_argv[1]; +const caml_executable_name = caml_argv[1]; //Provides: caml_sys_get_argv //Requires: caml_argv @@ -185,12 +187,12 @@ function caml_sys_executable_name(a) { //Provides: caml_sys_system_command //Requires: caml_jsstring_of_string function caml_sys_system_command(cmd) { - var cmd = caml_jsstring_of_string(cmd); + const cmd_ = caml_jsstring_of_string(cmd); if (typeof require != "undefined") { - var child_process = require("child_process"); + const child_process = require("child_process"); if (child_process && child_process.execSync) try { - child_process.execSync(cmd, { stdio: "inherit" }); + child_process.execSync(cmd_, { stdio: "inherit" }); return 0; } catch (e) { return 1; @@ -206,9 +208,9 @@ function caml_sys_system_command(cmd) { } //Provides: caml_sys_time mutable -var caml_initial_time = new Date().getTime() * 0.001; +const caml_initial_time = new Date().getTime() * 0.001; function caml_sys_time() { - var now = new Date().getTime(); + const now = new Date().getTime(); return now * 0.001 - caml_initial_time; } @@ -223,10 +225,10 @@ function caml_sys_time_include_children(b) { function caml_sys_random_seed() { if (globalThis.crypto) { if (globalThis.crypto.getRandomValues) { - var a = globalThis.crypto.getRandomValues(new Int32Array(4)); + const a = globalThis.crypto.getRandomValues(new Int32Array(4)); return [0, a[0], a[1], a[2], a[3]]; } else if (globalThis.crypto.randomBytes) { - var a = new Int32Array(globalThis.crypto.randomBytes(16).buffer); + const a = new Int32Array(globalThis.crypto.randomBytes(16).buffer); return [0, a[0], a[1], a[2], a[3]]; } } @@ -280,7 +282,7 @@ function caml_sys_const_backend_type() { } //Provides: os_type -var os_type = +const os_type = globalThis.process && globalThis.process.platform && globalThis.process.platform == "win32" @@ -315,7 +317,7 @@ function caml_install_signal_handler() { } //Provides: caml_runtime_warnings -var caml_runtime_warnings = 0; +let caml_runtime_warnings = 0; //Provides: caml_ml_enable_runtime_warnings //Requires: caml_runtime_warnings @@ -360,21 +362,21 @@ function caml_xdg_defaults(_unit) { //Provides: caml_sys_is_regular_file //Requires: resolve_fs_device function caml_sys_is_regular_file(name) { - var root = resolve_fs_device(name); + const root = resolve_fs_device(name); return root.device.isFile(root.rest); } //Always //Requires: caml_fatal_uncaught_exception //If: !wasm function caml_setup_uncaught_exception_handler() { - var process = globalThis.process; + const process = globalThis.process; if (process && process.on) { - process.on("uncaughtException", function (err, origin) { + process.on("uncaughtException", (err, origin) => { caml_fatal_uncaught_exception(err); process.exit(2); }); } else if (globalThis.addEventListener) { - globalThis.addEventListener("error", function (event) { + globalThis.addEventListener("error", (event) => { if (event.error) { caml_fatal_uncaught_exception(event.error); } diff --git a/runtime/toplevel.js b/runtime/toplevel.js index 7487bd35bf..ac6ffc2acc 100644 --- a/runtime/toplevel.js +++ b/runtime/toplevel.js @@ -58,12 +58,12 @@ function caml_get_section_table() { if (!caml_global_data.sections) { caml_failwith("Program not compiled with --toplevel"); } - var symb = caml_global_data.sections[1]; - var crcs = caml_global_data.sections[2]; - var prim = caml_global_data.sections[3]; - var dlpt = caml_global_data.sections[4]; + const symb = caml_global_data.sections[1]; + const crcs = caml_global_data.sections[2]; + const prim = caml_global_data.sections[3]; + const dlpt = caml_global_data.sections[4]; function sl(l) { - var x = ""; + let x = ""; while (l) { x += caml_jsbytes_of_string(l[1]); x += "\0"; @@ -71,7 +71,7 @@ function caml_get_section_table() { } return caml_string_of_jsbytes(x); } - var res = caml_list_of_js_array([ + const res = caml_list_of_js_array([ [0, caml_string_of_jsbytes("SYMB"), symb], [0, caml_string_of_jsbytes("CRCS"), crcs], [0, caml_string_of_jsbytes("PRIM"), sl(prim)], @@ -107,10 +107,10 @@ function caml_reify_bytecode(code, debug, _digest) { //Version: < 5.2 function caml_reify_bytecode(code, debug, _digest) { if (globalThis.toplevelCompile) { - var len = 0; - var all = []; + let len = 0; + const all = []; for (let i = 1; i < code.length; i++) { - var a = caml_uint8_array_of_bytes(code[i]); + const a = caml_uint8_array_of_bytes(code[i]); all.push(a); len += a.length; } diff --git a/runtime/unix.js b/runtime/unix.js index 916dae7951..03fc4f70e0 100644 --- a/runtime/unix.js +++ b/runtime/unix.js @@ -14,10 +14,10 @@ function caml_unix_time() { //Provides: caml_unix_gmtime //Alias: unix_gmtime function caml_unix_gmtime(t) { - var d = new Date(t * 1000); - var d_num = d.getTime(); - var januaryfirst = new Date(Date.UTC(d.getUTCFullYear(), 0, 1)).getTime(); - var doy = Math.floor((d_num - januaryfirst) / 86400000); + const d = new Date(t * 1000); + const d_num = d.getTime(); + const januaryfirst = new Date(Date.UTC(d.getUTCFullYear(), 0, 1)).getTime(); + const doy = Math.floor((d_num - januaryfirst) / 86400000); return BLOCK( 0, d.getUTCSeconds(), @@ -35,13 +35,13 @@ function caml_unix_gmtime(t) { //Provides: caml_unix_localtime //Alias: unix_localtime function caml_unix_localtime(t) { - var d = new Date(t * 1000); - var d_num = d.getTime(); - var januaryfirst = new Date(d.getFullYear(), 0, 1).getTime(); - var doy = Math.floor((d_num - januaryfirst) / 86400000); - var jan = new Date(d.getFullYear(), 0, 1); - var jul = new Date(d.getFullYear(), 6, 1); - var stdTimezoneOffset = Math.max( + const d = new Date(t * 1000); + const d_num = d.getTime(); + const januaryfirst = new Date(d.getFullYear(), 0, 1).getTime(); + const doy = Math.floor((d_num - januaryfirst) / 86400000); + const jan = new Date(d.getFullYear(), 0, 1); + const jul = new Date(d.getFullYear(), 6, 1); + const stdTimezoneOffset = Math.max( jan.getTimezoneOffset(), jul.getTimezoneOffset(), ); @@ -64,9 +64,9 @@ function caml_unix_localtime(t) { //Requires: caml_unix_localtime //Alias: unix_mktime function caml_unix_mktime(tm) { - var d = new Date(tm[6] + 1900, tm[5], tm[4], tm[3], tm[2], tm[1]).getTime(); - var t = Math.floor(d / 1000); - var tm2 = caml_unix_localtime(t); + const d = new Date(tm[6] + 1900, tm[5], tm[4], tm[3], tm[2], tm[1]).getTime(); + const t = Math.floor(d / 1000); + const tm2 = caml_unix_localtime(t); return BLOCK(0, t, tm2); } //Provides: caml_unix_startup const @@ -88,7 +88,7 @@ function caml_unix_filedescr_of_fd(x) { //Alias: unix_isatty function caml_unix_isatty(fileDescriptor) { if (fs_node_supported()) { - var tty = require("tty"); + const tty = require("tty"); return tty.isatty(fileDescriptor) ? 1 : 0; } else { return 0; @@ -104,7 +104,7 @@ function caml_unix_isatty(fileDescriptor) { //Provides: make_unix_err_args //Requires: caml_string_of_jsstring -var unix_error = [ +const unix_error = [ /* ===Unix.error=== * * This array is in order of the variant in OCaml @@ -179,7 +179,7 @@ var unix_error = [ "EOVERFLOW", ]; function make_unix_err_args(code, syscall, path, errno) { - var variant = unix_error.indexOf(code); + let variant = unix_error.indexOf(code); if (variant < 0) { // Default if undefined if (errno == null) { @@ -188,7 +188,7 @@ function make_unix_err_args(code, syscall, path, errno) { // If none of the above variants, fallback to EUNKNOWNERR(int) variant = BLOCK(0, errno); } - var args = [ + const args = [ variant, caml_string_of_jsstring(syscall || ""), caml_string_of_jsstring(path || ""), @@ -200,7 +200,7 @@ function make_unix_err_args(code, syscall, path, errno) { //Requires: resolve_fs_device, caml_failwith //Alias: unix_stat function caml_unix_stat(name) { - var root = resolve_fs_device(name); + const root = resolve_fs_device(name); if (!root.device.stat) { caml_failwith("caml_unix_stat: not implemented"); } @@ -211,7 +211,7 @@ function caml_unix_stat(name) { //Requires: caml_unix_stat, caml_int64_of_int32 //Alias: unix_stat_64 function caml_unix_stat_64(name) { - var r = caml_unix_stat(name); + const r = caml_unix_stat(name); r[9] = caml_int64_of_int32(r[9]); return r; } @@ -220,7 +220,7 @@ function caml_unix_stat_64(name) { //Requires: resolve_fs_device, caml_failwith //Alias: unix_lstat function caml_unix_lstat(name) { - var root = resolve_fs_device(name); + const root = resolve_fs_device(name); if (!root.device.lstat) { caml_failwith("caml_unix_lstat: not implemented"); } @@ -231,7 +231,7 @@ function caml_unix_lstat(name) { //Requires: caml_unix_lstat, caml_int64_of_int32 //Alias: unix_lstat_64 function caml_unix_lstat_64(name) { - var r = caml_unix_lstat(name); + const r = caml_unix_lstat(name); r[9] = caml_int64_of_int32(r[9]); return r; } @@ -240,7 +240,7 @@ function caml_unix_lstat_64(name) { //Requires: resolve_fs_device, caml_failwith //Alias: unix_mkdir function caml_unix_mkdir(name, perm) { - var root = resolve_fs_device(name); + const root = resolve_fs_device(name); if (!root.device.mkdir) { caml_failwith("caml_unix_mkdir: not implemented"); } @@ -251,7 +251,7 @@ function caml_unix_mkdir(name, perm) { //Requires: resolve_fs_device, caml_failwith //Alias: unix_rmdir function caml_unix_rmdir(name) { - var root = resolve_fs_device(name); + const root = resolve_fs_device(name); if (!root.device.rmdir) { caml_failwith("caml_unix_rmdir: not implemented"); } @@ -262,8 +262,8 @@ function caml_unix_rmdir(name) { //Requires: resolve_fs_device, caml_failwith //Alias: unix_symlink function caml_unix_symlink(to_dir, src, dst) { - var src_root = resolve_fs_device(src); - var dst_root = resolve_fs_device(dst); + const src_root = resolve_fs_device(src); + const dst_root = resolve_fs_device(dst); if (src_root.device != dst_root.device) caml_failwith("caml_unix_symlink: cannot symlink between two filesystems"); if (!src_root.device.symlink) { @@ -281,7 +281,7 @@ function caml_unix_symlink(to_dir, src, dst) { //Requires: resolve_fs_device, caml_failwith //Alias: unix_readlink function caml_unix_readlink(name) { - var root = resolve_fs_device(name); + const root = resolve_fs_device(name); if (!root.device.readlink) { caml_failwith("caml_unix_readlink: not implemented"); } @@ -292,7 +292,7 @@ function caml_unix_readlink(name) { //Requires: resolve_fs_device, caml_failwith //Alias: unix_unlink function caml_unix_unlink(name) { - var root = resolve_fs_device(name); + const root = resolve_fs_device(name); if (!root.device.unlink) { caml_failwith("caml_unix_unlink: not implemented"); } @@ -327,11 +327,14 @@ function caml_unix_has_symlink(unit) { //Requires: resolve_fs_device, caml_failwith //Alias: unix_opendir function caml_unix_opendir(path) { - var root = resolve_fs_device(path); + const root = resolve_fs_device(path); if (!root.device.opendir) { caml_failwith("caml_unix_opendir: not implemented"); } - var dir_handle = root.device.opendir(root.rest, /* raise Unix_error */ true); + const dir_handle = root.device.opendir( + root.rest, + /* raise Unix_error */ true, + ); return { pointer: dir_handle, path: path }; } @@ -341,11 +344,11 @@ function caml_unix_opendir(path) { //Requires: make_unix_err_args, caml_raise_with_args, caml_named_value //Alias: unix_readdir function caml_unix_readdir(dir_handle) { - var entry; + let entry; try { entry = dir_handle.pointer.readSync(); } catch (e) { - var unix_error = caml_named_value("Unix.Unix_error"); + const unix_error = caml_named_value("Unix.Unix_error"); caml_raise_with_args( unix_error, make_unix_err_args("EBADF", "readdir", dir_handle.path), @@ -365,7 +368,7 @@ function caml_unix_closedir(dir_handle) { try { dir_handle.pointer.closeSync(); } catch (e) { - var unix_error = caml_named_value("Unix.Unix_error"); + const unix_error = caml_named_value("Unix.Unix_error"); caml_raise_with_args( unix_error, make_unix_err_args("EBADF", "closedir", dir_handle.path), @@ -378,7 +381,7 @@ function caml_unix_closedir(dir_handle) { //Alias: unix_rewinddir function caml_unix_rewinddir(dir_handle) { caml_unix_closedir(dir_handle); - var new_dir_handle = caml_unix_opendir(dir_handle.path); + const new_dir_handle = caml_unix_opendir(dir_handle.path); dir_handle.pointer = new_dir_handle.pointer; return 0; } @@ -389,12 +392,12 @@ function caml_unix_rewinddir(dir_handle) { //Alias: win_findfirst function caml_unix_findfirst(path) { // The Windows code adds this glob to the path, so we need to remove it - var path_js = caml_jsstring_of_string(path); + let path_js = caml_jsstring_of_string(path); path_js = path_js.replace(/(^|[\\\/])\*\.\*$/, ""); path = caml_string_of_jsstring(path_js); // *.* is now stripped - var dir_handle = caml_unix_opendir(path); - var first_entry = caml_unix_readdir(dir_handle); + const dir_handle = caml_unix_opendir(path); + const first_entry = caml_unix_readdir(dir_handle); // The Windows bindings type dir_handle as an `int` but it's not in JS return [0, first_entry, dir_handle]; } diff --git a/runtime/weak.js b/runtime/weak.js index 607a6fc61e..fd3b0c10cc 100644 --- a/runtime/weak.js +++ b/runtime/weak.js @@ -20,10 +20,10 @@ // Weak API //Provides: caml_ephe_key_offset -var caml_ephe_key_offset = 3; +const caml_ephe_key_offset = 3; //Provides: caml_ephe_data_offset -var caml_ephe_data_offset = 2; +const caml_ephe_data_offset = 2; //Provides: caml_ephe_set_key //Requires: caml_invalid_argument, caml_ephe_key_offset @@ -47,11 +47,11 @@ function caml_ephe_unset_key(x, i) { x[caml_ephe_key_offset + i] instanceof globalThis.WeakRef && x[1].unregister ) { - var old = x[caml_ephe_key_offset + i].deref(); + const old = x[caml_ephe_key_offset + i].deref(); if (old !== undefined) { - var count = 0; + let count = 0; for (let j = caml_ephe_key_offset; j < x.length; j++) { - var key = x[j]; + let key = x[j]; if (key instanceof globalThis.WeakRef) { key = key.deref(); if (key === old) count++; @@ -67,7 +67,7 @@ function caml_ephe_unset_key(x, i) { //Provides: caml_ephe_create //Requires: caml_weak_create, caml_ephe_data_offset function caml_ephe_create(n) { - var x = caml_weak_create(n); + const x = caml_weak_create(n); return x; } @@ -75,7 +75,7 @@ function caml_ephe_create(n) { //Requires: caml_ephe_key_offset, caml_invalid_argument,caml_ephe_data_offset function caml_weak_create(n) { if (n < 0) caml_invalid_argument("Weak.create"); - var x = [251, "caml_ephe_list_head"]; + const x = [251, "caml_ephe_list_head"]; x.length = caml_ephe_key_offset + n; return x; } @@ -94,7 +94,7 @@ function caml_weak_set(x, i, v) { function caml_ephe_get_key(x, i) { if (i < 0 || caml_ephe_key_offset + i >= x.length) caml_invalid_argument("Weak.get_key"); - var weak = x[caml_ephe_key_offset + i]; + let weak = x[caml_ephe_key_offset + i]; if (globalThis.WeakRef && weak instanceof globalThis.WeakRef) weak = weak.deref(); return weak === undefined ? 0 : [0, weak]; @@ -106,9 +106,9 @@ function caml_ephe_get_key(x, i) { function caml_ephe_get_key_copy(x, i) { if (i < 0 || caml_ephe_key_offset + i >= x.length) caml_invalid_argument("Weak.get_copy"); - var y = caml_ephe_get_key(x, i); + const y = caml_ephe_get_key(x, i); if (y === 0) return y; - var z = y[1]; + const z = y[1]; if (Array.isArray(z)) return [0, caml_obj_dup(z)]; return y; } @@ -117,7 +117,7 @@ function caml_ephe_get_key_copy(x, i) { //Requires: caml_ephe_key_offset //Alias: caml_weak_check function caml_ephe_check_key(x, i) { - var weak = x[caml_ephe_key_offset + i]; + let weak = x[caml_ephe_key_offset + i]; if (globalThis.WeakRef && weak instanceof globalThis.WeakRef) weak = weak.deref(); if (weak === undefined) return 0; @@ -143,7 +143,7 @@ function caml_ephe_blit_key(a1, i1, a2, i2, len) { //Provides: caml_ephe_blit_data //Requires: caml_ephe_data_offset, caml_ephe_set_data, caml_ephe_unset_data function caml_ephe_blit_data(src, dst) { - var n = src[caml_ephe_data_offset]; + const n = src[caml_ephe_data_offset]; if (n === undefined) caml_ephe_unset_data(dst); else caml_ephe_set_data(dst, n); return 0; @@ -169,12 +169,12 @@ function caml_ephe_get_data_copy(x) { function caml_ephe_set_data(x, data) { if (globalThis.FinalizationRegistry && globalThis.WeakRef) { if (!(x[1] instanceof globalThis.FinalizationRegistry)) { - x[1] = new globalThis.FinalizationRegistry(function () { + x[1] = new globalThis.FinalizationRegistry(() => { caml_ephe_unset_data(x); }); //register all keys for (let j = caml_ephe_key_offset; j < x.length; j++) { - var key = x[j]; + let key = x[j]; if (key instanceof globalThis.WeakRef) { key = key.deref(); if (key) x[1].register(key, undefined, key); @@ -193,7 +193,7 @@ function caml_ephe_unset_data(x) { if (x[1] instanceof globalThis.FinalizationRegistry) { //unregister all keys for (let j = caml_ephe_key_offset; j < x.length; j++) { - var key = x[j]; + let key = x[j]; if (key instanceof globalThis.WeakRef) { key = key.deref(); if (key) x[1].unregister(key); diff --git a/runtime/zstd.js b/runtime/zstd.js index 2c11b436a5..5fcb225c15 100644 --- a/runtime/zstd.js +++ b/runtime/zstd.js @@ -1,30 +1,29 @@ //Provides: zstd_decompress //Version: >= 5.1 -var zstd_decompress = (function () { - "use strict"; +const zstd_decompress = (function () { // aliases for shorter compressed code (most minifers don't do this) - var ab = ArrayBuffer, - u8 = Uint8Array, - u16 = Uint16Array, - i16 = Int16Array, - u32 = Uint32Array, - i32 = Int32Array; - var slc = function (v, s, e) { + const ab = ArrayBuffer; + const u8 = Uint8Array; + const u16 = Uint16Array; + const i16 = Int16Array; + const u32 = Uint32Array; + const i32 = Int32Array; + const slc = function (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; - var n = new u8(e - s); + const n = new u8(e - s); n.set(v.subarray(s, e)); return n; }; - var fill = function (v, n, s, e) { + const fill = function (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; }; - var cpw = function (v, t, s, e) { + const cpw = function (v, t, s, e) { if (u8.prototype.copyWithin) return u8.prototype.copyWithin.call(v, t, s, e); if (s == null || s < 0) s = 0; @@ -37,7 +36,7 @@ var zstd_decompress = (function () { * Codes for errors generated within this library */ // error codes - var ec = [ + const ec = [ "invalid zstd data", "window size too large (>2046MB)", "invalid block type", @@ -45,53 +44,53 @@ var zstd_decompress = (function () { "match distance too far back", "unexpected EOF", ]; - var err = function (ind, msg, nt) { - var e = new Error(msg || ec[ind]); + const err = function (ind, msg, nt) { + const e = new Error(msg || ec[ind]); e.code = ind; if (!nt) throw e; return e; }; - var rb = function (d, b, n) { - var i = 0, - o = 0; + const rb = function (d, b, n) { + let i = 0; + let o = 0; for (; i < n; ++i) o |= d[b++] << (i << 3); return o; }; - var b4 = function (d, b) { + const b4 = function (d, b) { return (d[b] | (d[b + 1] << 8) | (d[b + 2] << 16) | (d[b + 3] << 24)) >>> 0; }; // read Zstandard frame header - var rzfh = function (dat, w) { - var n3 = dat[0] | (dat[1] << 8) | (dat[2] << 16); + const rzfh = function (dat, w) { + const n3 = dat[0] | (dat[1] << 8) | (dat[2] << 16); if (n3 == 0x2fb528 && dat[3] == 253) { // Zstandard - var flg = dat[4]; + const flg = dat[4]; // single segment checksum dict flag frame content flag - var ss = (flg >> 5) & 1, - cc = (flg >> 2) & 1, - df = flg & 3, - fcf = flg >> 6; + const ss = (flg >> 5) & 1; + const cc = (flg >> 2) & 1; + const df = flg & 3; + const fcf = flg >> 6; if (flg & 8) err(0); // byte - var bt = 6 - ss; + let bt = 6 - ss; // dict bytes - var db = df == 3 ? 4 : df; + const db = df == 3 ? 4 : df; // dictionary id - var di = rb(dat, bt, db); + const di = rb(dat, bt, db); bt += db; // frame size bytes - var fsb = fcf ? 1 << fcf : ss; + const fsb = fcf ? 1 << fcf : ss; // frame source size - var fss = rb(dat, bt, fsb) + (fcf == 1 && 256); + const fss = rb(dat, bt, fsb) + (fcf == 1 && 256); // window size - var ws = fss; + let ws = fss; if (!ss) { // window descriptor - var wb = 1 << (10 + (dat[5] >> 3)); + const wb = 1 << (10 + (dat[5] >> 3)); ws = wb + (wb >> 3) * (dat[5] & 7); } if (ws > 2145386496) err(1); - var buf = new u8((w == 1 ? fss || ws : w ? 0 : ws) + 12); + const buf = new u8((w == 1 ? fss || ws : w ? 0 : ws) + 12); (buf[0] = 1), (buf[4] = 4), (buf[8] = 8); return { b: bt + fsb, @@ -112,50 +111,50 @@ var zstd_decompress = (function () { err(0); }; // most significant bit for nonzero - var msb = function (val) { - var bits = 0; + const msb = function (val) { + let bits = 0; for (; 1 << bits <= val; ++bits); return bits - 1; }; // read finite state entropy - var rfse = function (dat, bt, mal) { + const rfse = function (dat, bt, mal) { // table pos - var tpos = (bt << 3) + 4; + let tpos = (bt << 3) + 4; // accuracy log - var al = (dat[bt] & 15) + 5; + const al = (dat[bt] & 15) + 5; if (al > mal) err(3); // size - var sz = 1 << al; + const sz = 1 << al; // probabilities symbols repeat index high threshold - var probs = sz, - sym = -1, - re = -1, - i = -1, - ht = sz; + let probs = sz; + let sym = -1; + let re = -1; + let i = -1; + let ht = sz; // optimization: single allocation is much faster - var buf = new ab(512 + (sz << 2)); - var freq = new i16(buf, 0, 256); + const buf = new ab(512 + (sz << 2)); + const freq = new i16(buf, 0, 256); // same view as freq - var dstate = new u16(buf, 0, 256); - var nstate = new u16(buf, 512, sz); - var bb1 = 512 + (sz << 1); - var syms = new u8(buf, bb1, sz); - var nbits = new u8(buf, bb1 + sz); + const dstate = new u16(buf, 0, 256); + const nstate = new u16(buf, 512, sz); + const bb1 = 512 + (sz << 1); + const syms = new u8(buf, bb1, sz); + const nbits = new u8(buf, bb1 + sz); while (sym < 255 && probs > 0) { - var bits = msb(probs + 1); - var cbt = tpos >> 3; + const bits = msb(probs + 1); + const cbt = tpos >> 3; // mask - var msk = (1 << (bits + 1)) - 1; - var val = + const msk = (1 << (bits + 1)) - 1; + let val = ((dat[cbt] | (dat[cbt + 1] << 8) | (dat[cbt + 2] << 16)) >> (tpos & 7)) & msk; // mask (1 fewer bit) - var msk1fb = (1 << bits) - 1; + const msk1fb = (1 << bits) - 1; // max small value - var msv = msk - probs - 1; + const msv = msk - probs - 1; // small value - var sval = val & msk1fb; + const sval = val & msk1fb; if (sval < msv) (tpos += bits), (val = sval); else { tpos += bits + 1; @@ -169,7 +168,7 @@ var zstd_decompress = (function () { if (!val) { do { // repeat byte - var rbt = tpos >> 3; + const rbt = tpos >> 3; re = ((dat[rbt] | (dat[rbt + 1] << 8)) >> (tpos & 7)) & 3; tpos += 2; sym += re; @@ -177,13 +176,13 @@ var zstd_decompress = (function () { } } if (sym > 255 || probs) err(0); - var sympos = 0; + let sympos = 0; // sym step (coprime with sz - formula from zstd source) - var sstep = (sz >> 1) + (sz >> 3) + 3; + const sstep = (sz >> 1) + (sz >> 3) + 3; // sym mask - var smask = sz - 1; + const smask = sz - 1; for (let s = 0; s <= sym; ++s) { - var sf = freq[s]; + const sf = freq[s]; if (sf < 1) { dstate[s] = -sf; continue; @@ -200,9 +199,9 @@ var zstd_decompress = (function () { if (sympos) err(0); for (i = 0; i < sz; ++i) { // next state - var ns = dstate[syms[i]]++; + const ns = dstate[syms[i]]++; // num bits - var nb = (nbits[i] = al - msb(ns)); + const nb = (nbits[i] = al - msb(ns)); nstate[i] = (ns << nb) - sz; } return [ @@ -216,42 +215,42 @@ var zstd_decompress = (function () { ]; }; // read huffman - var rhu = function (dat, bt) { + const rhu = function (dat, bt) { // index weight count - var i = 0, + let i = 0, wc = -1; // buffer header byte - var buf = new u8(292), - hb = dat[bt]; + const buf = new u8(292); + const hb = dat[bt]; // huffman weights - var hw = buf.subarray(0, 256); + const hw = buf.subarray(0, 256); // rank count - var rc = buf.subarray(256, 268); + const rc = buf.subarray(256, 268); // rank index - var ri = new u16(buf.buffer, 268); + const ri = new u16(buf.buffer, 268); // NOTE: at this point bt is 1 less than expected if (hb < 128) { // end byte, fse decode table - var _a = rfse(dat, bt + 1, 6), - ebt = _a[0], - fdt = _a[1]; + const _a = rfse(dat, bt + 1, 6); + const ebt = _a[0]; + const fdt = _a[1]; bt += hb; - var epos = ebt << 3; + const epos = ebt << 3; // last byte - var lb = dat[bt]; + const lb = dat[bt]; if (!lb) err(0); // state1 state2 state1 bits state2 bits - var st1 = 0, - st2 = 0, - btr1 = fdt.b, - btr2 = btr1; + let st1 = 0; + let st2 = 0; + let btr1 = fdt.b; + let btr2 = btr1; // fse pos // pre-increment to account for original deficit of 1 - var fpos = (++bt << 3) - 8 + msb(lb); + let fpos = (++bt << 3) - 8 + msb(lb); for (;;) { fpos -= btr1; if (fpos < epos) break; - var cbt = fpos >> 3; + let cbt = fpos >> 3; st1 += ((dat[cbt] | (dat[cbt + 1] << 8)) >> (fpos & 7)) & ((1 << btr1) - 1); hw[++wc] = fdt.s[st1]; @@ -270,48 +269,48 @@ var zstd_decompress = (function () { } else { wc = hb - 127; for (; i < wc; i += 2) { - var byte = dat[++bt]; + const byte = dat[++bt]; hw[i] = byte >> 4; hw[i + 1] = byte & 15; } ++bt; } // weight exponential sum - var wes = 0; + let wes = 0; for (i = 0; i < wc; ++i) { - var wt = hw[i]; + const wt = hw[i]; // bits must be at most 11, same as weight if (wt > 11) err(0); wes += wt && 1 << (wt - 1); } // max bits - var mb = msb(wes) + 1; + const mb = msb(wes) + 1; // table size - var ts = 1 << mb; + const ts = 1 << mb; // remaining sum - var rem = ts - wes; + const rem = ts - wes; // must be power of 2 if (rem & (rem - 1)) err(0); hw[wc++] = msb(rem) + 1; for (i = 0; i < wc; ++i) { - var wt = hw[i]; + const wt = hw[i]; ++rc[(hw[i] = wt && mb + 1 - wt)]; } // huf buf - var hbuf = new u8(ts << 1); + const hbuf = new u8(ts << 1); // symbols num bits - var syms = hbuf.subarray(0, ts), - nb = hbuf.subarray(ts); + const syms = hbuf.subarray(0, ts); + const nb = hbuf.subarray(ts); ri[mb] = 0; for (i = mb; i > 0; --i) { - var pv = ri[i]; + const pv = ri[i]; fill(nb, i, pv, (ri[i - 1] = pv + rc[i] * (1 << (mb - i)))); } if (ri[0] != ts) err(0); for (i = 0; i < wc; ++i) { - var bits = hw[i]; + const bits = hw[i]; if (bits) { - var code = ri[bits]; + const code = ri[bits]; fill(syms, i, code, (ri[bits] = code + (1 << (mb - bits)))); } } @@ -327,7 +326,7 @@ var zstd_decompress = (function () { // Tables generated using this: // https://gist.github.com/101arrowz/a979452d4355992cbf8f257cbffc9edd // default literal length table - var dllt = /*#__PURE__*/ rfse( + const dllt = /*#__PURE__*/ rfse( /*#__PURE__*/ new u8([ 81, 16, 99, 140, 49, 198, 24, 99, 12, 33, 196, 24, 99, 102, 102, 134, 70, 146, 4, @@ -336,7 +335,7 @@ var zstd_decompress = (function () { 6, )[1]; // default match length table - var dmlt = /*#__PURE__*/ rfse( + const dmlt = /*#__PURE__*/ rfse( /*#__PURE__*/ new u8([ 33, 20, 196, 24, 99, 140, 33, 132, 16, 66, 8, 33, 132, 16, 66, 8, 33, 68, 68, 68, 68, 68, 68, 68, 68, 36, 9, @@ -345,15 +344,15 @@ var zstd_decompress = (function () { 6, )[1]; // default offset code table - var doct = /*#__PURE__ */ rfse( + const doct = /*#__PURE__ */ rfse( /*#__PURE__*/ new u8([32, 132, 16, 66, 102, 70, 68, 68, 68, 68, 36, 73, 2]), 0, 5, )[1]; // bits to baseline - var b2bl = function (b, s) { - var len = b.length, - bl = new i32(len); + const b2bl = function (b, s) { + const len = b.length; + const bl = new i32(len); for (let i = 0; i < len; ++i) { bl[i] = s; s += 1 << b[i]; @@ -361,7 +360,7 @@ var zstd_decompress = (function () { return bl; }; // literal length bits - var llb = /*#__PURE__ */ new u8( + const llb = /*#__PURE__ */ new u8( /*#__PURE__ */ new i32([ 0, 0, 0, 0, 16843009, 50528770, 134678020, 202050057, 269422093, ]).buffer, @@ -369,9 +368,9 @@ var zstd_decompress = (function () { 36, ); // literal length baseline - var llbl = /*#__PURE__ */ b2bl(llb, 0); + const llbl = /*#__PURE__ */ b2bl(llb, 0); // match length bits - var mlb = /*#__PURE__ */ new u8( + const mlb = /*#__PURE__ */ new u8( /*#__PURE__ */ new i32([ 0, 0, 0, 0, 0, 0, 0, 0, 16843009, 50528770, 117769220, 185207048, 252579084, 16, @@ -380,22 +379,22 @@ var zstd_decompress = (function () { 53, ); // match length baseline - var mlbl = /*#__PURE__ */ b2bl(mlb, 3); + const mlbl = /*#__PURE__ */ b2bl(mlb, 3); // decode huffman stream - var dhu = function (dat, out, hu) { - var len = dat.length, - ss = out.length, - lb = dat[len - 1], - msk = (1 << hu.b) - 1, - eb = -hu.b; + const dhu = function (dat, out, hu) { + const len = dat.length; + const ss = out.length; + const lb = dat[len - 1]; + const msk = (1 << hu.b) - 1; + const eb = -hu.b; if (!lb) err(0); - var st = 0, - btr = hu.b, - pos = (len << 3) - 8 + msb(lb) - btr, - i = -1; - for (; pos > eb && i < ss; ) { - var cbt = pos >> 3; - var val = + let st = 0; + let btr = hu.b; + let pos = (len << 3) - 8 + msb(lb) - btr; + let i = -1; + while (pos > eb && i < ss) { + const cbt = pos >> 3; + const val = (dat[cbt] | (dat[cbt + 1] << 8) | (dat[cbt + 2] << 16)) >> (pos & 7); st = ((st << btr) | val) & msk; out[++i] = hu.s[st]; @@ -405,12 +404,12 @@ var zstd_decompress = (function () { }; // decode huffman stream 4x // TODO: use workers to parallelize - var dhu4 = function (dat, out, hu) { - var bt = 6; - var ss = out.length, - sz1 = (ss + 3) >> 2, - sz2 = sz1 << 1, - sz3 = sz1 + sz2; + const dhu4 = function (dat, out, hu) { + let bt = 6; + const ss = out.length; + const sz1 = (ss + 3) >> 2; + const sz2 = sz1 << 1; + const sz3 = sz1 + sz2; dhu( dat.subarray(bt, (bt += dat[0] | (dat[1] << 8))), out.subarray(0, sz1), @@ -429,16 +428,16 @@ var zstd_decompress = (function () { dhu(dat.subarray(bt), out.subarray(sz3), hu); }; // read Zstandard block - var rzb = function (dat, st, out) { - var _a; - var bt = st.b; + const rzb = function (dat, st, out) { + let _a; + let bt = st.b; // byte 0 block type - var b0 = dat[bt], - btype = (b0 >> 1) & 3; + const b0 = dat[bt]; + const btype = (b0 >> 1) & 3; st.l = b0 & 1; - var sz = (b0 >> 3) | (dat[bt + 1] << 5) | (dat[bt + 2] << 13); + const sz = (b0 >> 3) | (dat[bt + 1] << 5) | (dat[bt + 2] << 13); // end byte for block - var ebt = (bt += 3) + sz; + const ebt = (bt += 3) + sz; if (btype == 1) { if (bt >= dat.length) return; st.b = bt + 1; @@ -460,13 +459,13 @@ var zstd_decompress = (function () { } if (btype == 2) { // byte 3 lit btype size format - var b3 = dat[bt], - lbt = b3 & 3, - sf = (b3 >> 2) & 3; + const b3 = dat[bt]; + const lbt = b3 & 3; + const sf = (b3 >> 2) & 3; // lit src size lit cmp sz 4 streams - var lss = b3 >> 4, - lcs = 0, - s4 = 0; + let lss = b3 >> 4; + let lcs = 0; + let s4 = 0; if (lbt < 2) { if (sf & 1) lss |= (dat[++bt] << 4) | (sf & 2 && dat[++bt] << 12); else lss = b3 >> 3; @@ -484,16 +483,16 @@ var zstd_decompress = (function () { } ++bt; // add literals to end - can never overlap with backreferences because unused literals always appended - var buf = out ? out.subarray(st.y, st.y + st.m) : new u8(st.m); + let buf = out ? out.subarray(st.y, st.y + st.m) : new u8(st.m); // starting point for literals - var spl = buf.length - lss; + let spl = buf.length - lss; if (lbt == 0) buf.set(dat.subarray(bt, (bt += lss)), spl); else if (lbt == 1) fill(buf, dat[bt++], spl); else { // huffman table - var hu = st.h; + let hu = st.h; if (lbt == 2) { - var hud = rhu(dat, bt); + const hud = rhu(dat, bt); // subtract description length lcs += bt - (bt = hud[0]); st.h = hu = hud[1]; @@ -501,19 +500,19 @@ var zstd_decompress = (function () { (s4 ? dhu4 : dhu)(dat.subarray(bt, (bt += lcs)), buf.subarray(spl), hu); } // num sequences - var ns = dat[bt++]; + let ns = dat[bt++]; if (ns) { if (ns == 255) ns = (dat[bt++] | (dat[bt++] << 8)) + 0x7f00; else if (ns > 127) ns = ((ns - 128) << 8) | dat[bt++]; // symbol compression modes - var scm = dat[bt++]; + const scm = dat[bt++]; if (scm & 3) err(0); - var dts = [dmlt, doct, dllt]; + const dts = [dmlt, doct, dllt]; for (let i = 2; i > -1; --i) { - var md = (scm >> ((i << 1) + 2)) & 3; + const md = (scm >> ((i << 1) + 2)) & 3; if (md == 1) { // rle buf - var rbuf = new u8([0, 0, dat[bt++]]); + const rbuf = new u8([0, 0, dat[bt++]]); dts[i] = { s: rbuf.subarray(2, 3), n: rbuf.subarray(0, 1), @@ -528,33 +527,33 @@ var zstd_decompress = (function () { dts[i] = st.t[i]; } } - var _b = (st.t = dts), - mlt = _b[0], - oct = _b[1], - llt = _b[2]; - var lb = dat[ebt - 1]; + const _b = (st.t = dts); + const mlt = _b[0]; + const oct = _b[1]; + const llt = _b[2]; + const lb = dat[ebt - 1]; if (!lb) err(0); - var spos = (ebt << 3) - 8 + msb(lb) - llt.b, - cbt = spos >> 3, - oubt = 0; - var lst = + let spos = (ebt << 3) - 8 + msb(lb) - llt.b; + let cbt = spos >> 3; + let oubt = 0; + let lst = ((dat[cbt] | (dat[cbt + 1] << 8)) >> (spos & 7)) & ((1 << llt.b) - 1); cbt = (spos -= oct.b) >> 3; - var ost = + let ost = ((dat[cbt] | (dat[cbt + 1] << 8)) >> (spos & 7)) & ((1 << oct.b) - 1); cbt = (spos -= mlt.b) >> 3; - var mst = + let mst = ((dat[cbt] | (dat[cbt + 1] << 8)) >> (spos & 7)) & ((1 << mlt.b) - 1); for (++ns; --ns; ) { - var llc = llt.s[lst]; - var lbtr = llt.n[lst]; - var mlc = mlt.s[mst]; - var mbtr = mlt.n[mst]; - var ofc = oct.s[ost]; - var obtr = oct.n[ost]; + const llc = llt.s[lst]; + const lbtr = llt.n[lst]; + const mlc = mlt.s[mst]; + const mbtr = mlt.n[mst]; + const ofc = oct.s[ost]; + const obtr = oct.n[ost]; cbt = (spos -= ofc) >> 3; - var ofp = 1 << ofc; - var off = + const ofp = 1 << ofc; + let off = ofp + (((dat[cbt] | (dat[cbt + 1] << 8) | @@ -563,13 +562,13 @@ var zstd_decompress = (function () { (spos & 7)) & (ofp - 1)); cbt = (spos -= mlb[mlc]) >> 3; - var ml = + let ml = mlbl[mlc] + (((dat[cbt] | (dat[cbt + 1] << 8) | (dat[cbt + 2] << 16)) >> (spos & 7)) & ((1 << mlb[mlc]) - 1)); cbt = (spos -= llb[llc]) >> 3; - var ll = + const ll = llbl[llc] + (((dat[cbt] | (dat[cbt + 1] << 8) | (dat[cbt + 2] << 16)) >> (spos & 7)) & @@ -594,7 +593,7 @@ var zstd_decompress = (function () { st.o[1] = st.o[0]; st.o[0] = off -= 3; } else { - var idx = off - (ll != 0); + const idx = off - (ll != 0); if (idx) { off = idx == 3 ? st.o[0] - 1 : st.o[idx]; if (idx > 1) st.o[2] = st.o[1]; @@ -606,10 +605,10 @@ var zstd_decompress = (function () { buf[oubt + i] = buf[spl + i]; } (oubt += ll), (spl += ll); - var stin = oubt - off; + let stin = oubt - off; if (stin < 0) { - var len = -stin; - var bs = st.e + stin; + let len = -stin; + const bs = st.e + stin; if (len > ml) len = ml; for (let i = 0; i < len; ++i) { buf[oubt + i] = st.w[bs + i]; @@ -644,11 +643,11 @@ var zstd_decompress = (function () { err(2); }; // concat - var cct = function (bufs, ol) { + const cct = function (bufs, ol) { if (bufs.length == 1) return bufs[0]; - var buf = new u8(ol); + const buf = new u8(ol); for (let i = 0, b = 0; i < bufs.length; ++i) { - var chk = bufs[i]; + const chk = bufs[i]; buf.set(chk, b); b += chk.length; } @@ -664,12 +663,12 @@ var zstd_decompress = (function () { * @returns The decompressed data */ return function decompress(dat, buf) { - var bt = 0, - bufs = [], - nb = +!buf, - ol = 0; - for (; dat.length; ) { - var st = rzfh(dat, nb || buf); + let bt = 0; + const bufs = []; + const nb = +!buf; + let ol = 0; + while (dat.length) { + const st = rzfh(dat, nb || buf); if (typeof st == "object") { if (nb) { buf = null; @@ -681,8 +680,8 @@ var zstd_decompress = (function () { bufs.push(buf); st.e = 0; } - for (; !st.l; ) { - var blk = rzb(dat, st, buf); + while (!st.l) { + const blk = rzb(dat, st, buf); if (!blk) err(5); if (buf) st.e = st.y; else { @@ -702,23 +701,23 @@ var zstd_decompress = (function () { //Provides: caml_decompress_input //Version: < 5.1.0 -var caml_decompress_input = null; +const caml_decompress_input = null; //Provides: caml_decompress_input //Version: >= 5.1.0 //Version: < 5.1.1 //Requires: zstd_decompress -var caml_decompress_input = zstd_decompress; +const caml_decompress_input = zstd_decompress; //Provides: caml_decompress_input //Version: >= 5.1.1 //Version: < 5.2.0 -var caml_decompress_input = null; +const caml_decompress_input = null; //Provides: caml_decompress_input //Version: >= 5.2 //Requires: zstd_decompress -var caml_decompress_input = zstd_decompress; +let caml_decompress_input = zstd_decompress; //Provides: caml_zstd_initialize //Requires: caml_decompress_input From 6ab943978d61e9c93082aaacbb7d41b7fbf55bf3 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Sat, 14 Sep 2024 14:29:02 +0900 Subject: [PATCH 04/12] runtime: fix eqeqeq Signed-off-by: Sora Morimoto --- runtime/backtrace.js | 2 +- runtime/bigarray.js | 58 ++++++++++++------------ runtime/bigstring.js | 18 ++++---- runtime/compare.js | 60 ++++++++++++------------ runtime/format.js | 18 ++++---- runtime/fs.js | 10 ++-- runtime/fs_fake.js | 18 ++++---- runtime/gc.js | 6 +-- runtime/graphics.js | 8 ++-- runtime/ieee_754.js | 49 +++++++++++--------- runtime/int64.js | 12 ++--- runtime/ints.js | 14 +++--- runtime/io.js | 12 ++--- runtime/jslib.js | 4 +- runtime/jslib_js_of_ocaml.js | 2 +- runtime/lexing.js | 26 +++++------ runtime/marshal.js | 38 ++++++++-------- runtime/md5.js | 4 +- runtime/mlBytes.js | 72 +++++++++++++++-------------- runtime/nat.js | 26 +++++------ runtime/obj.js | 18 ++++---- runtime/parsing.js | 26 +++++------ runtime/stdlib.js | 4 +- runtime/stdlib_modern.js | 8 ++-- runtime/str.js | 16 +++---- runtime/sys.js | 24 +++++----- runtime/unix.js | 2 +- runtime/weak.js | 4 +- runtime/zstd.js | 88 ++++++++++++++++++++---------------- 29 files changed, 332 insertions(+), 315 deletions(-) diff --git a/runtime/backtrace.js b/runtime/backtrace.js index f2895262cb..b2a44f8002 100644 --- a/runtime/backtrace.js +++ b/runtime/backtrace.js @@ -24,7 +24,7 @@ let caml_record_backtrace_env_flag = FLAG("with-js-error"); if (r !== undefined) { const l = r.split(","); for (let i = 0; i < l.length; i++) { - if (l[i] == "b") { + if (l[i] === "b") { caml_record_backtrace_env_flag = 1; break; } diff --git a/runtime/bigarray.js b/runtime/bigarray.js index d1d7f32939..c45620c0e2 100644 --- a/runtime/bigarray.js +++ b/runtime/bigarray.js @@ -129,9 +129,9 @@ Ml_Bigarray.prototype.offset = function (arg) { let ofs = 0; if (typeof arg === "number") arg = [arg]; if (!Array.isArray(arg)) caml_invalid_argument("bigarray.js: invalid offset"); - if (this.dims.length != arg.length) + if (this.dims.length !== arg.length) caml_invalid_argument("Bigarray.get/set: bad number of dimensions"); - if (this.layout == 0 /* c_layout */) { + if (this.layout === 0 /* c_layout */) { for (let i = 0; i < this.dims.length; i++) { if (arg[i] < 0 || arg[i] >= this.dims[i]) caml_array_bound_error(); ofs = ofs * this.dims[i] + arg[i]; @@ -193,11 +193,11 @@ Ml_Bigarray.prototype.fill = function (v) { // Int64 const a = caml_int64_lo32(v); const b = caml_int64_hi32(v); - if (a == b) { + if (a === b) { this.data.fill(a); } else { for (let i = 0; i < this.data.length; i++) { - this.data[i] = i % 2 == 0 ? a : b; + this.data[i] = i % 2 === 0 ? a : b; } } break; @@ -207,11 +207,11 @@ Ml_Bigarray.prototype.fill = function (v) { // Complex32, Complex64 const im = v[1]; const re = v[2]; - if (im == re) { + if (im === re) { this.data.fill(im); } else { for (let i = 0; i < this.data.length; i++) { - this.data[i] = i % 2 == 0 ? im : re; + this.data[i] = i % 2 === 0 ? im : re; } } break; @@ -223,16 +223,16 @@ Ml_Bigarray.prototype.fill = function (v) { }; Ml_Bigarray.prototype.compare = function (b, total) { - if (this.layout != b.layout || this.kind != b.kind) { + if (this.layout !== b.layout || this.kind !== b.kind) { const k1 = this.kind | (this.layout << 8); const k2 = b.kind | (b.layout << 8); return k2 - k1; } - if (this.dims.length != b.dims.length) { + if (this.dims.length !== b.dims.length) { return b.dims.length - this.dims.length; } for (let i = 0; i < this.dims.length; i++) - if (this.dims[i] != b.dims[i]) return this.dims[i] < b.dims[i] ? -1 : 1; + if (this.dims[i] !== b.dims[i]) return this.dims[i] < b.dims[i] ? -1 : 1; switch (this.kind) { case 0: case 1: @@ -246,10 +246,10 @@ Ml_Bigarray.prototype.compare = function (b, total) { y = b.data[i]; if (x < y) return -1; if (x > y) return 1; - if (x != y) { + if (x !== y) { if (!total) return Number.NaN; - if (x == x) return 1; - if (y == y) return -1; + if (x === x) return 1; + if (y === y) return -1; } } break; @@ -293,7 +293,7 @@ function Ml_Bigarray_c_1_1(kind, layout, dims, buffer) { Ml_Bigarray_c_1_1.prototype = new Ml_Bigarray(); Ml_Bigarray_c_1_1.prototype.offset = function (arg) { if (typeof arg !== "number") { - if (Array.isArray(arg) && arg.length == 1) arg = arg[0]; + if (Array.isArray(arg) && arg.length === 1) arg = arg[0]; else caml_invalid_argument("Ml_Bigarray_c_1_1.offset"); } if (arg < 0 || arg >= this.dims[0]) caml_array_bound_error(); @@ -324,13 +324,13 @@ function caml_ba_compare(a, b, total) { //Requires: caml_invalid_argument function caml_ba_create_unsafe(kind, layout, dims, data) { const size_per_element = caml_ba_get_size_per_element(kind); - if (caml_ba_get_size(dims) * size_per_element != data.length) { + if (caml_ba_get_size(dims) * size_per_element !== data.length) { caml_invalid_argument("length doesn't match dims"); } if ( - layout == 0 && // c_layout - dims.length == 1 && // Array1 - size_per_element == 1 + layout === 0 && // c_layout + dims.length === 1 && // Array1 + size_per_element === 1 ) // 1-to-1 mapping return new Ml_Bigarray_c_1_1(kind, layout, dims, data); @@ -350,7 +350,7 @@ function caml_ba_create(kind, layout, dims_ml) { //Provides: caml_ba_change_layout //Requires: caml_ba_create_unsafe function caml_ba_change_layout(ba, layout) { - if (ba.layout == layout) return ba; + if (ba.layout === layout) return ba; const new_dims = []; for (let i = 0; i < ba.dims.length; i++) new_dims[i] = ba.dims[ba.dims.length - i - 1]; @@ -523,10 +523,10 @@ function caml_ba_fill(ba, v) { //Provides: caml_ba_blit //Requires: caml_invalid_argument function caml_ba_blit(src, dst) { - if (dst.dims.length != src.dims.length) + if (dst.dims.length !== src.dims.length) caml_invalid_argument("Bigarray.blit: dimension mismatch"); for (let i = 0; i < dst.dims.length; i++) - if (dst.dims[i] != src.dims[i]) + if (dst.dims[i] !== src.dims[i]) caml_invalid_argument("Bigarray.blit: dimension mismatch"); dst.data.set(src.data); return 0; @@ -538,7 +538,7 @@ function caml_ba_blit(src, dst) { function caml_ba_sub(ba, ofs, len) { let changed_dim; let mul = 1; - if (ba.layout == 0) { + if (ba.layout === 0) { for (let i = 1; i < ba.dims.length; i++) mul = mul * ba.dims[i]; changed_dim = 0; } else { @@ -570,7 +570,7 @@ function caml_ba_slice(ba, vind) { caml_invalid_argument("Bigarray.slice: too many indices"); // Compute offset and check bounds - if (ba.layout == 0) { + if (ba.layout === 0) { let i = 0; for (; i < num_inds; i++) index[i] = vind[i]; for (; i < ba.dims.length; i++) index[i] = 0; @@ -611,7 +611,7 @@ function caml_ba_reshape(ba, vind) { const size = caml_ba_get_size(ba.dims); // Check that sizes agree - if (num_elts != size) + if (num_elts !== size) caml_invalid_argument("Bigarray.reshape: size mismatch"); return caml_ba_create_unsafe(ba.kind, ba.layout, new_dim, ba.data); } @@ -622,7 +622,7 @@ function caml_ba_reshape(ba, vind) { function caml_ba_serialize(writer, ba, sz) { writer.write(32, ba.dims.length); writer.write(32, ba.kind | (ba.layout << 8)); - if (ba.caml_custom == "_bigarr02") + if (ba.caml_custom === "_bigarr02") for (let i = 0; i < ba.dims.length; i++) { if (ba.dims[i] < 0xffff) writer.write(16, ba.dims[i]); else { @@ -715,13 +715,13 @@ function caml_ba_deserialize(reader, sz, name) { const kind = tag & 0xff; const layout = (tag >> 8) & 1; const dims = []; - if (name == "_bigarr02") + if (name === "_bigarr02") for (let i = 0; i < num_dims; i++) { let size_dim = reader.read16u(); - if (size_dim == 0xffff) { + if (size_dim === 0xffff) { const size_dim_hi = reader.read32u(); const size_dim_lo = reader.read32u(); - if (size_dim_hi != 0) + if (size_dim_hi !== 0) caml_failwith("input_value: bigarray dimension overflow in 32bit"); size_dim = size_dim_lo; } @@ -825,7 +825,7 @@ function caml_ba_deserialize(reader, sz, name) { //Provides: caml_ba_create_from //Requires: caml_ba_create_unsafe, caml_invalid_argument, caml_ba_get_size_per_element function caml_ba_create_from(data1, data2, jstyp, kind, layout, dims) { - if (data2 || caml_ba_get_size_per_element(kind) == 2) { + if (data2 || caml_ba_get_size_per_element(kind) === 2) { caml_invalid_argument( "caml_ba_create_from: use return caml_ba_create_unsafe", ); @@ -876,7 +876,7 @@ function caml_ba_hash(ba) { w = ba.data[i + 0] | (ba.data[i + 1] << 16); h = caml_hash_mix_int(h, w); } - if ((num_elts & 1) != 0) h = caml_hash_mix_int(h, ba.data[i]); + if ((num_elts & 1) !== 0) h = caml_hash_mix_int(h, ba.data[i]); break; } case 6: // Int32Array (int32) diff --git a/runtime/bigstring.js b/runtime/bigstring.js index a2c2b901fe..ed9e55682a 100644 --- a/runtime/bigstring.js +++ b/runtime/bigstring.js @@ -49,11 +49,11 @@ 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 (12 !== ba1.kind) caml_invalid_argument("caml_bigstring_blit_ba_to_ba: kind mismatch"); - if (12 != ba2.kind) + if (12 !== ba2.kind) caml_invalid_argument("caml_bigstring_blit_ba_to_ba: kind mismatch"); - if (len == 0) return 0; + if (len === 0) return 0; const ofs1 = ba1.offset(pos1); const ofs2 = ba2.offset(pos2); if (ofs1 + len > ba1.data.length) { @@ -71,9 +71,9 @@ 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 (12 !== ba2.kind) caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch"); - if (len == 0) return 0; + if (len === 0) return 0; const ofs2 = ba2.offset(pos2); if (pos1 + len > caml_ml_string_length(str1)) { caml_array_bound_error(); @@ -90,9 +90,9 @@ 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 (12 !== ba2.kind) caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch"); - if (len == 0) return 0; + if (len === 0) return 0; const ofs2 = ba2.offset(pos2); if (pos1 + len > caml_ml_bytes_length(str1)) { caml_array_bound_error(); @@ -110,9 +110,9 @@ 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 (12 !== ba1.kind) caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch"); - if (len == 0) return 0; + if (len === 0) return 0; const ofs1 = ba1.offset(pos1); if (ofs1 + len > ba1.data.length) { caml_array_bound_error(); diff --git a/runtime/compare.js b/runtime/compare.js index f72d0a36b8..a6a2098132 100644 --- a/runtime/compare.js +++ b/runtime/compare.js @@ -29,10 +29,10 @@ function caml_compare_val_tag(a) { 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; + return tag === 254 ? 0 : tag; } else if (a instanceof String) return 12520; // javascript string, like string_tag (252) - else if (typeof a == "string") + 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) @@ -40,9 +40,9 @@ function caml_compare_val_tag(a) { return 1255; // like custom_tag (255) else if (a && a.compare) return 1256; // like custom_tag (255) - else if (typeof a == "function") + else if (typeof a === "function") return 1247; // like closure_tag (247) - else if (typeof a == "symbol") return 1251; + else if (typeof a === "symbol") return 1251; return 1001; //out_of_heap_tag } @@ -60,9 +60,9 @@ function caml_compare_val_number_custom(num, custom, swap, total) { const comp = caml_compare_val_get_custom(custom); if (comp) { const x = swap > 0 ? comp(custom, num, total) : comp(num, custom, total); - if (total && x != x) return swap; // total && nan - if (+x != +x) return +x; // nan - if ((x | 0) != 0) return x | 0; // !nan + if (total && x !== x) return swap; // total && nan + if (+x !== +x) return +x; // nan + if ((x | 0) !== 0) return x | 0; // !nan } return swap; } @@ -79,29 +79,29 @@ function caml_compare_val(a, b, total) { if (!(total && a === b)) { const tag_a = caml_compare_val_tag(a); // forward_tag ? - if (tag_a == 250) { + if (tag_a === 250) { a = a[1]; continue; } const tag_b = caml_compare_val_tag(b); // forward_tag ? - if (tag_b == 250) { + if (tag_b === 250) { b = b[1]; continue; } // tags are different if (tag_a !== tag_b) { - if (tag_a == 1000) { - if (tag_b == 1255) { + if (tag_a === 1000) { + if (tag_b === 1255) { //immediate can compare against custom return caml_compare_val_number_custom(a, b, -1, total); } return -1; } - if (tag_b == 1000) { - if (tag_a == 1255) { + if (tag_b === 1000) { + if (tag_a === 1255) { //immediate can compare against custom return caml_compare_val_number_custom(b, a, 1, total); } @@ -118,7 +118,7 @@ function caml_compare_val(a, b, total) { case 248: { // Object const x = caml_int_compare(a[2], b[2]); - if (x != 0) return x | 0; + if (x !== 0) return x | 0; break; } case 249: // Infix @@ -135,7 +135,7 @@ function caml_compare_val(a, b, total) { case 252: // OCaml bytes if (a !== b) { const x = caml_bytes_compare(a, b); - if (x != 0) return x | 0; + if (x !== 0) return x | 0; } break; case 253: // Double_tag @@ -157,12 +157,12 @@ function caml_compare_val(a, b, total) { case 1255: { // Custom const comp = caml_compare_val_get_custom(a); - if (comp != caml_compare_val_get_custom(b)) { + if (comp !== caml_compare_val_get_custom(b)) { return a.caml_custom < b.caml_custom ? -1 : 1; } if (!comp) caml_invalid_argument("compare: abstract value"); const x = comp(a, b, total); - if (x != x) { + if (x !== x) { // Protect against invalid UNORDERED return total ? -1 : x; } @@ -170,13 +170,13 @@ function caml_compare_val(a, b, total) { // Protect against invalid return value return -1; } - if (x != 0) return x | 0; + if (x !== 0) return x | 0; break; } case 1256: { // compare function const x = a.compare(b, total); - if (x != x) { + if (x !== x) { // Protect against invalid UNORDERED return total ? -1 : x; } @@ -184,7 +184,7 @@ function caml_compare_val(a, b, total) { // Protect against invalid return value return -1; } - if (x != 0) return x | 0; + if (x !== 0) return x | 0; break; } case 1000: // Number @@ -192,10 +192,10 @@ function caml_compare_val(a, b, total) { b = +b; if (a < b) return -1; if (a > b) return 1; - if (a != b) { + if (a !== b) { if (!total) return Number.NaN; - if (a == a) return 1; - if (b == b) return -1; + if (a === a) return 1; + if (b === b) return -1; } break; case 1001: // The rest @@ -216,8 +216,8 @@ function caml_compare_val(a, b, total) { if (a > b) return 1; if (a != b) { if (!total) return Number.NaN; - if (a == a) return 1; - if (b == b) return -1; + if (a === a) return 1; + if (b === b) return -1; } break; case 1251: // JavaScript Symbol, no ordering. @@ -253,12 +253,12 @@ function caml_compare_val(a, b, total) { caml_invalid_argument("compare: continuation value"); break; } - if (a.length != b.length) return a.length < b.length ? -1 : 1; + if (a.length !== b.length) return a.length < b.length ? -1 : 1; if (a.length > 1) stack.push(a, b, 1); break; } } - if (stack.length == 0) return 0; + if (stack.length === 0) return 0; const i = stack.pop(); b = stack.pop(); a = stack.pop(); @@ -275,18 +275,18 @@ function caml_compare(a, b) { //Provides: caml_int_compare mutable (const, const) function caml_int_compare(a, b) { if (a < b) return -1; - if (a == b) return 0; + if (a === b) return 0; return 1; } //Provides: caml_equal mutable (const, const) //Requires: caml_compare_val function caml_equal(x, y) { - return +(caml_compare_val(x, y, false) == 0); + return +(caml_compare_val(x, y, false) === 0); } //Provides: caml_notequal mutable (const, const) //Requires: caml_compare_val function caml_notequal(x, y) { - return +(caml_compare_val(x, y, false) != 0); + return +(caml_compare_val(x, y, false) !== 0); } //Provides: caml_greaterequal mutable (const, const) //Requires: caml_compare_val diff --git a/runtime/format.js b/runtime/format.js index 6ae2a4c001..fc7f55512d 100644 --- a/runtime/format.js +++ b/runtime/format.js @@ -116,24 +116,24 @@ function caml_finish_formatting(f, rawbuffer) { if (f.uppercase) rawbuffer = rawbuffer.toUpperCase(); let len = rawbuffer.length; /* Adjust len to reflect additional chars (sign, etc) */ - if (f.signedconv && (f.sign < 0 || f.signstyle != "-")) len++; + if (f.signedconv && (f.sign < 0 || f.signstyle !== "-")) len++; if (f.alternate) { - if (f.base == 8) len += 1; - if (f.base == 16) len += 2; + if (f.base === 8) len += 1; + if (f.base === 16) len += 2; } /* Do the formatting */ let buffer = ""; - if (f.justify == "+" && f.filler == " ") + if (f.justify === "+" && f.filler === " ") for (let i = len; i < f.width; i++) buffer += " "; if (f.signedconv) { if (f.sign < 0) buffer += "-"; - else if (f.signstyle != "-") buffer += f.signstyle; + else if (f.signstyle !== "-") buffer += f.signstyle; } - if (f.alternate && f.base == 8) buffer += "0"; - if (f.alternate && f.base == 16) buffer += f.uppercase ? "0X" : "0x"; - if (f.justify == "+" && f.filler == "0") + if (f.alternate && f.base === 8) buffer += "0"; + if (f.alternate && f.base === 16) buffer += f.uppercase ? "0X" : "0x"; + if (f.justify === "+" && f.filler === "0") for (let i = len; i < f.width; i++) buffer += "0"; buffer += rawbuffer; - if (f.justify == "-") for (let i = len; i < f.width; i++) buffer += " "; + if (f.justify === "-") for (let i = len; i < f.width; i++) buffer += " "; return caml_string_of_jsbytes(buffer); } diff --git a/runtime/fs.js b/runtime/fs.js index 60a2f0a258..3c0446c587 100644 --- a/runtime/fs.js +++ b/runtime/fs.js @@ -151,7 +151,7 @@ function resolve_fs_device(name) { for (let i = 0; i < jsoo_mount_point.length; i++) { const m = jsoo_mount_point[i]; if ( - name_slash.search(m.path) == 0 && + name_slash.search(m.path) === 0 && (!res || res.path.length < m.path.length) ) res = { @@ -192,7 +192,7 @@ function caml_unmount(name) { const name_ = caml_trailing_slash(path.join("/")); let idx = -1; for (let i = 0; i < jsoo_mount_point.length; i++) - if (jsoo_mount_point[i].path == name_) idx = i; + if (jsoo_mount_point[i].path === name_) idx = i; if (idx > -1) jsoo_mount_point.splice(idx, 1); return 0; } @@ -253,7 +253,7 @@ function caml_sys_read_directory(name) { function caml_sys_remove(name) { const root = resolve_fs_device(name); const ok = root.device.unlink(root.rest); - if (ok == 0) caml_raise_no_such_file(caml_jsbytes_of_string(name)); + if (ok === 0) caml_raise_no_such_file(caml_jsbytes_of_string(name)); return 0; } @@ -270,7 +270,7 @@ function caml_sys_is_directory(name) { function caml_sys_rename(o, n) { const o_root = resolve_fs_device(o); const n_root = resolve_fs_device(n); - if (o_root.device != n_root.device) + if (o_root.device !== n_root.device) caml_failwith("caml_sys_rename: cannot move file between two filesystem"); if (!o_root.device.rename) caml_failwith("caml_sys_rename: no implemented"); o_root.device.rename(o_root.rest, n_root.rest); @@ -350,7 +350,7 @@ function jsoo_create_file(name, content) { //Requires: resolve_fs_device, caml_raise_no_such_file, caml_string_of_array //Requires: caml_string_of_jsbytes, caml_jsbytes_of_string function caml_read_file_content(name) { - const name_ = typeof name == "string" ? caml_string_of_jsbytes(name) : name; + const name_ = typeof name === "string" ? caml_string_of_jsbytes(name) : name; const root = resolve_fs_device(name_); if (root.device.exists(root.rest)) { const file = root.device.open(root.rest, { rdonly: 1 }); diff --git a/runtime/fs_fake.js b/runtime/fs_fake.js index c437ecdb9d..11be670378 100644 --- a/runtime/fs_fake.js +++ b/runtime/fs_fake.js @@ -57,7 +57,7 @@ MlFakeDevice.prototype.lookup = function (name) { }; MlFakeDevice.prototype.exists = function (name) { // The root of the device exists - if (name == "") return 1; + if (name === "") return 1; // Check if a directory exists const name_slash = this.slash(name); if (this.content[name_slash]) return 1; @@ -110,7 +110,7 @@ MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) { }; MlFakeDevice.prototype.rmdir = function (name, raise_unix) { const unix_error = raise_unix && caml_named_value("Unix.Unix_error"); - const name_slash = name == "" ? "" : this.slash(name); + const name_slash = name === "" ? "" : this.slash(name); const r = new RegExp("^" + name_slash + "([^/]+)"); if (!this.exists(name)) { if (unix_error) { @@ -147,7 +147,7 @@ MlFakeDevice.prototype.rmdir = function (name, raise_unix) { delete this.content[name_slash]; }; MlFakeDevice.prototype.readdir = function (name) { - const name_slash = name == "" ? "" : this.slash(name); + const name_slash = name === "" ? "" : this.slash(name); if (!this.exists(name)) { caml_raise_sys_error(name + ": No such file or directory"); } @@ -184,7 +184,7 @@ MlFakeDevice.prototype.opendir = function (name, raise_unix) { caml_raise_sys_error(name + ": closedir failed"); } } - if (i == a.length) return null; + if (i === a.length) return null; const entry = a[i]; i++; return { name: entry }; @@ -206,7 +206,7 @@ MlFakeDevice.prototype.opendir = function (name, raise_unix) { }; }; MlFakeDevice.prototype.is_dir = function (name) { - if (name == "") return true; + if (name === "") return true; const name_slash = this.slash(name); return this.content[name_slash] ? 1 : 0; }; @@ -346,10 +346,10 @@ MlFakeFile.prototype.read = function (offset, buf, pos, len) { function MlFakeFd_out(fd, flags) { MlFakeFile.call(this, caml_create_bytes(0)); this.log = (s) => 0; - if (fd == 1 && typeof console.log == "function") this.log = console.log; - else if (fd == 2 && typeof console.error == "function") + if (fd === 1 && typeof console.log === "function") this.log = console.log; + else if (fd === 2 && typeof console.error === "function") this.log = console.error; - else if (typeof console.log == "function") this.log = console.log; + else if (typeof console.log === "function") this.log = console.log; this.flags = flags; } MlFakeFd_out.prototype.length = () => 0; @@ -359,7 +359,7 @@ MlFakeFd_out.prototype.write = function (offset, buf, pos, len) { len > 0 && pos >= 0 && pos + len <= buf.length && - buf[pos + len - 1] == 10 + buf[pos + len - 1] === 10 ) len--; // Do not output the last \n if present diff --git a/runtime/gc.js b/runtime/gc.js index 31486b80c5..6bacac5a97 100644 --- a/runtime/gc.js +++ b/runtime/gc.js @@ -1,19 +1,19 @@ //Provides: caml_gc_minor function caml_gc_minor(unit) { //available with [node --expose-gc] - if (typeof globalThis.gc == "function") globalThis.gc(true); + if (typeof globalThis.gc === "function") globalThis.gc(true); return 0; } //Provides: caml_gc_major function caml_gc_major(unit) { //available with [node --expose-gc] - if (typeof globalThis.gc == "function") globalThis.gc(); + if (typeof globalThis.gc === "function") globalThis.gc(); return 0; } //Provides: caml_gc_full_major function caml_gc_full_major(unit) { //available with [node --expose-gc] - if (typeof globalThis.gc == "function") globalThis.gc(); + if (typeof globalThis.gc === "function") globalThis.gc(); return 0; } //Provides: caml_gc_compaction diff --git a/runtime/graphics.js b/runtime/graphics.js index 6b7374cc64..10f8698545 100644 --- a/runtime/graphics.js +++ b/runtime/graphics.js @@ -53,7 +53,7 @@ function caml_gr_open_graph(info) { if (res) return res[2]; } const specs = []; - if (!(info_ == "")) specs.push(info_); + if (!(info_ === "")) specs.push(info_); let target = get("target"); if (!target) target = ""; const status = get("status"); @@ -290,9 +290,9 @@ function caml_gr_arc_aux(ctx, cx, cy, ry, rx, a1, a2) { ry * Math.cos(i) * Math.sin(rot * Math.PI) + rx * Math.sin(i) * Math.cos(rot * Math.PI); yPos = yPos.toFixed(2); - if (j == 0) { + if (j === 0) { ctx.moveTo(xPos, yPos); - } else if (xPos_prev != xPos || yPos_prev != yPos) { + } else if (xPos_prev !== xPos || yPos_prev !== yPos) { ctx.lineTo(xPos, yPos); } xPos_prev = xPos; @@ -417,7 +417,7 @@ function caml_gr_make_image(arr) { for (let j = 0; j < w; j++) { const c = arr[i + 1][j + 1]; const o = i * (w * 4) + j * 4; - if (c == -1) { + if (c === -1) { im.data[o + 0] = 0; im.data[o + 1] = 0; im.data[o + 2] = 0; diff --git a/runtime/ieee_754.js b/runtime/ieee_754.js index d7643ac299..9cd1ab0930 100644 --- a/runtime/ieee_754.js +++ b/runtime/ieee_754.js @@ -18,11 +18,11 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. //Provides: jsoo_floor_log2 -const log2_ok = Math.log2 && Math.log2(1.1235582092889474e307) == 1020; +const log2_ok = Math.log2 && Math.log2(1.1235582092889474e307) === 1020; function jsoo_floor_log2(x) { if (log2_ok) return Math.floor(Math.log2(x)); let i = 0; - if (x == 0) return Number.NEGATIVE_INFINITY; + if (x === 0) return Number.NEGATIVE_INFINITY; if (x >= 1) { while (x >= 2) { x /= 2; @@ -46,7 +46,11 @@ function caml_int64_bits_of_float(x) { else return caml_int64_create_lo_mi_hi(0, 0, 0xfff0); } const sign = - x == 0 && 1 / x == Number.NEGATIVE_INFINITY ? 0x8000 : x >= 0 ? 0 : 0x8000; + x === 0 && 1 / x === Number.NEGATIVE_INFINITY + ? 0x8000 + : x >= 0 + ? 0 + : 0x8000; if (sign) x = -x; // Int64.bits_of_float 1.1235582092889474E+307 = 0x7fb0000000000000L // using Math.LOG2E*Math.log(x) in place of Math.log2 result in precision lost @@ -60,7 +64,7 @@ function caml_int64_bits_of_float(x) { x *= 2; exp -= 1; } - if (exp == 0) { + if (exp === 0) { x /= 2; } } @@ -93,10 +97,11 @@ function caml_hexstring_of_float(x, prec, style) { if (Number.isNaN(x)) return caml_string_of_jsstring("nan"); return caml_string_of_jsstring(x > 0 ? "infinity" : "-infinity"); } - const sign = x == 0 && 1 / x == Number.NEGATIVE_INFINITY ? 1 : x >= 0 ? 0 : 1; + const sign = + x === 0 && 1 / x === Number.NEGATIVE_INFINITY ? 1 : x >= 0 ? 0 : 1; if (sign) x = -x; let exp = 0; - if (x == 0) { + if (x === 0) { } else if (x < 1) { while (x < 1 && exp > -1022) { x *= 2; @@ -151,8 +156,8 @@ function caml_int64_float_of_bits(x) { const mi = x.mi; const hi = x.hi; const exp = (hi & 0x7fff) >> 4; - if (exp == 2047) { - if ((lo | mi | (hi & 0xf)) == 0) + if (exp === 2047) { + if ((lo | mi | (hi & 0xf)) === 0) return hi & 0x8000 ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY; else return Number.NaN; } @@ -170,14 +175,14 @@ function caml_int64_float_of_bits(x) { //Requires: caml_int64_float_of_bits, caml_int64_bits_of_float, caml_int64_add, caml_int64_sub,caml_int64_of_int32 function caml_nextafter_float(x, y) { if (Number.isNaN(x) || Number.isNaN(y)) return Number.NaN; - if (x == y) return y; - if (x == 0) { + if (x === y) return y; + if (x === 0) { if (y < 0) return -Math.pow(2, -1074); else return Math.pow(2, -1074); } let bits = caml_int64_bits_of_float(x); const one = caml_int64_of_int32(1); - if (x < y == x > 0) bits = caml_int64_add(bits, one); + if (x < y === x > 0) bits = caml_int64_add(bits, one); else bits = caml_int64_sub(bits, one); return caml_int64_float_of_bits(bits); } @@ -199,7 +204,7 @@ function caml_int32_float_of_bits(x) { function caml_classify_float(x) { if (Number.isFinite(x)) { if (Math.abs(x) >= 2.2250738585072014e-308) return 0; - if (x != 0) return 1; + if (x !== 0) return 1; return 2; } return Number.isNaN(x) ? 4 : 3; @@ -242,7 +247,7 @@ function caml_ldexp_float(x, exp) { //Provides: caml_frexp_float const //Requires: jsoo_floor_log2 function caml_frexp_float(x) { - if (x == 0 || !isFinite(x)) return [0, x, 0]; + if (x === 0 || !Number.isFinite(x)) return [0, x, 0]; const neg = x < 0; if (neg) x = -x; let exp = Math.max(-1023, jsoo_floor_log2(x) + 1); @@ -271,14 +276,14 @@ function caml_float_compare(x, y) { //Provides: caml_copysign_float const function caml_copysign_float(x, y) { - if (y == 0) y = 1 / y; + if (y === 0) y = 1 / y; x = Math.abs(x); return y < 0 ? -x : x; } //Provides: caml_signbit_float const function caml_signbit_float(x) { - if (x == 0) x = 1 / x; + if (x === 0) x = 1 / x; return x < 0 ? 1 : 0; } @@ -507,7 +512,7 @@ function caml_format_float(fmt, x) { let s; const f = caml_parse_format(fmt); let prec = f.prec < 0 ? 6 : f.prec; - if (x < 0 || (x == 0 && 1 / x == Number.NEGATIVE_INFINITY)) { + if (x < 0 || (x === 0 && 1 / x === Number.NEGATIVE_INFINITY)) { f.sign = -1; x = -x; } @@ -523,7 +528,7 @@ function caml_format_float(fmt, x) { s = x.toExponential(prec); // exponent should be at least two digits const i = s.length; - if (s.charAt(i - 3) == "e") + if (s.charAt(i - 3) === "e") s = s.slice(0, i - 1) + "0" + s.slice(i - 1); break; } @@ -538,11 +543,11 @@ function caml_format_float(fmt, x) { if (exp < -4 || x >= 1e21 || x.toFixed(0).length > prec) { // remove trailing zeroes let i = j - 1; - while (s.charAt(i) == "0") i--; - if (s.charAt(i) == ".") i--; + while (s.charAt(i) === "0") i--; + if (s.charAt(i) === ".") i--; s = s.slice(0, i + 1) + s.slice(j); i = s.length; - if (s.charAt(i - 3) == "e") + if (s.charAt(i - 3) === "e") s = s.slice(0, i - 1) + "0" + s.slice(i - 1); break; } else { @@ -554,8 +559,8 @@ function caml_format_float(fmt, x) { if (p) { // remove trailing zeroes let i = s.length - 1; - while (s.charAt(i) == "0") i--; - if (s.charAt(i) == ".") i--; + while (s.charAt(i) === "0") i--; + if (s.charAt(i) === ".") i--; s = s.slice(0, i + 1); } } diff --git a/runtime/int64.js b/runtime/int64.js index f5c7894d4c..be1d5df4fb 100644 --- a/runtime/int64.js +++ b/runtime/int64.js @@ -81,7 +81,7 @@ MlInt64.prototype.mul = function (x) { return new MlInt64(lo, mi, hi); }; MlInt64.prototype.isZero = function () { - return (this.lo | this.mi | this.hi) == 0; + return (this.lo | this.mi | this.hi) === 0; }; MlInt64.prototype.isNeg = function () { return this.hi << 16 < 0; @@ -97,7 +97,7 @@ MlInt64.prototype.xor = function (x) { }; MlInt64.prototype.shift_left = function (s) { s = s & 63; - if (s == 0) return this; + if (s === 0) return this; if (s < 24) { return new MlInt64( this.lo << s, @@ -115,7 +115,7 @@ MlInt64.prototype.shift_left = function (s) { }; MlInt64.prototype.shift_right_unsigned = function (s) { s = s & 63; - if (s == 0) return this; + if (s === 0) return this; if (s < 24) return new MlInt64( (this.lo >> s) | (this.mi << (24 - s)), @@ -132,7 +132,7 @@ MlInt64.prototype.shift_right_unsigned = function (s) { }; MlInt64.prototype.shift_right = function (s) { s = s & 63; - if (s == 0) return this; + if (s === 0) return this; const h = (this.hi << 16) >> 16; if (s < 24) return new MlInt64( @@ -383,7 +383,7 @@ function caml_int64_of_string(s) { for (;;) { i++; c = caml_string_unsafe_get(s, i); - if (c == 95) continue; + if (c === 95) continue; d = caml_parse_digit(c); if (d < 0 || d >= base) break; /* Detect overflow in multiplication base * res */ @@ -393,7 +393,7 @@ function caml_int64_of_string(s) { /* Detect overflow in addition (base * res) + d */ if (caml_int64_ult(res, d)) caml_failwith("int_of_string"); } - if (i != caml_ml_string_length(s)) caml_failwith("int_of_string"); + if (i !== caml_ml_string_length(s)) caml_failwith("int_of_string"); if (signedness && caml_int64_ult(new MlInt64(0, 0, 0x8000), res)) caml_failwith("int_of_string"); if (sign < 0) res = caml_int64_neg(res); diff --git a/runtime/ints.js b/runtime/ints.js index 5aa7990202..1854d142c3 100644 --- a/runtime/ints.js +++ b/runtime/ints.js @@ -19,7 +19,7 @@ //Requires: caml_parse_format, caml_finish_formatting, caml_str_repeat //Requires: caml_string_of_jsbytes, caml_jsbytes_of_string function caml_format_int(fmt, i) { - if (caml_jsbytes_of_string(fmt) == "%d") + if (caml_jsbytes_of_string(fmt) === "%d") return caml_string_of_jsbytes("" + i); const f = caml_parse_format(fmt); if (i < 0) { @@ -57,7 +57,7 @@ function caml_parse_sign_and_base(s) { break; } } - if (i + 1 < len && caml_string_unsafe_get(s, i) == 48) + if (i + 1 < len && caml_string_unsafe_get(s, i) === 48) switch (caml_string_unsafe_get(s, i + 1)) { case 120: case 88: @@ -111,18 +111,18 @@ function caml_int_of_string(s) { let res = d; for (i++; i < len; i++) { c = caml_string_unsafe_get(s, i); - if (c == 95) continue; + if (c === 95) continue; d = caml_parse_digit(c); if (d < 0 || d >= base) break; res = base * res + d; if (res > threshold) caml_failwith("int_of_string"); } - if (i != len) caml_failwith("int_of_string"); + if (i !== len) caml_failwith("int_of_string"); // 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; - if (signedness && (res | 0) != res) + if (signedness && (res | 0) !== res) /* Signed representation expected, allow -2^(nbits-1) to 2^(nbits-1) - 1 */ caml_failwith("int_of_string"); return res | 0; @@ -136,14 +136,14 @@ function caml_mul(a, b) { //Provides: caml_div //Requires: caml_raise_zero_divide function caml_div(x, y) { - if (y == 0) caml_raise_zero_divide(); + if (y === 0) caml_raise_zero_divide(); return (x / y) | 0; } //Provides: caml_mod //Requires: caml_raise_zero_divide function caml_mod(x, y) { - if (y == 0) caml_raise_zero_divide(); + if (y === 0) caml_raise_zero_divide(); return x % y; } diff --git a/runtime/io.js b/runtime/io.js index bfa176ef1a..232d61ad61 100644 --- a/runtime/io.js +++ b/runtime/io.js @@ -40,7 +40,7 @@ function caml_sys_close(fd) { //Requires: caml_sys_fds //Requires: caml_sys_open_for_node function caml_sys_open_internal(file, idx) { - if (idx == undefined) { + if (idx === undefined) { idx = caml_sys_fds.length; } caml_sys_fds[idx] = file; @@ -303,7 +303,7 @@ function caml_refill(chan) { if (chan.refill != null) { const str = chan.refill(); const str_a = caml_uint8_array_of_string(str); - if (str_a.length == 0) { + if (str_a.length === 0) { chan.refill = null; } else { if (chan.buffer.length < chan.buffer_max + str_a.length) { @@ -391,7 +391,7 @@ function caml_input_value(chanid) { return r; } const r1 = block(header, 0, caml_marshal_header_size); - if (r1 == 0) caml_raise_end_of_file(); + if (r1 === 0) caml_raise_end_of_file(); else if (r1 < caml_marshal_header_size) caml_failwith("input_value: truncated object"); const len = caml_marshal_data_size(caml_bytes_of_array(header), 0); @@ -508,11 +508,11 @@ function caml_ml_input_scan_line(chanid) { } const prev_max = chan.buffer_max; caml_refill(chan); - if (prev_max == chan.buffer_max) { + if (prev_max === chan.buffer_max) { return -chan.buffer_max | 0; } } - } while (chan.buffer[p++] != 10); + } while (chan.buffer[p++] !== 10); return (p - chan.buffer_curr) | 0; } @@ -522,7 +522,7 @@ function caml_ml_input_scan_line(chanid) { function caml_ml_flush(chanid) { const chan = caml_ml_channel_get(chanid); if (!chan.opened) caml_raise_sys_error("Cannot flush a closed channel"); - if (!chan.buffer || chan.buffer_curr == 0) return 0; + if (!chan.buffer || chan.buffer_curr === 0) return 0; if (chan.output) { chan.output(caml_subarray_to_jsbytes(chan.buffer, 0, chan.buffer_curr)); } else { diff --git a/runtime/jslib.js b/runtime/jslib.js index 9555d47574..816e250090 100644 --- a/runtime/jslib.js +++ b/runtime/jslib.js @@ -203,7 +203,7 @@ function caml_maybe_attach_backtrace(exn, force) { //Requires: caml_global_data function caml_exn_with_js_backtrace(exn, force) { //never reraise for constant exn - if (!exn.js_error || force || exn[0] == 248) + if (!exn.js_error || force || exn[0] === 248) exn.js_error = new globalThis.Error("Js exception containing backtrace"); return exn; } @@ -484,7 +484,7 @@ function caml_js_function_arity(f) { //Provides: caml_js_equals mutable (const, const) function caml_js_equals(x, y) { - return +(x == y); + return +(x === y); } //Provides: caml_js_strict_equals mutable (const, const) diff --git a/runtime/jslib_js_of_ocaml.js b/runtime/jslib_js_of_ocaml.js index 861354bc3a..ab50cb19be 100644 --- a/runtime/jslib_js_of_ocaml.js +++ b/runtime/jslib_js_of_ocaml.js @@ -25,7 +25,7 @@ function caml_js_on_ie() { globalThis.navigator && globalThis.navigator.userAgent ? globalThis.navigator.userAgent : ""; - return ua.indexOf("MSIE") != -1 && ua.indexOf("Opera") != 0; + return ua.indexOf("MSIE") !== -1 && ua.indexOf("Opera") !== 0; } //Provides: caml_js_html_escape const (const) diff --git a/runtime/lexing.js b/runtime/lexing.js index 6012e059f7..c723fb16e9 100644 --- a/runtime/lexing.js +++ b/runtime/lexing.js @@ -75,7 +75,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; + if (lexbuf[lex_eof_reached] === 0) return -state - 1; else c = 256; } else { /* Read next input char */ @@ -83,18 +83,18 @@ function caml_lex_engine(tbl, start_state, lexbuf) { lexbuf[lex_curr_pos]++; } /* Determine next state */ - if (tbl.lex_check[base + c] == state) state = tbl.lex_trans[base + c]; + if (tbl.lex_check[base + c] === state) state = tbl.lex_trans[base + c]; else state = tbl.lex_default[state]; /* If no transition on this char, return to last backtrack point */ if (state < 0) { lexbuf[lex_curr_pos] = lexbuf[lex_last_pos]; - if (lexbuf[lex_last_action] == -1) caml_failwith("lexing: empty token"); + if (lexbuf[lex_last_action] === -1) caml_failwith("lexing: empty token"); else return lexbuf[lex_last_action]; } else { /* Erase the EOF condition only if the EOF pseudo-character was consumed by the automaton (i.e. there was no backtrack above) */ - if (c == 256) lexbuf[lex_eof_reached] = 0; + if (c === 256) lexbuf[lex_eof_reached] = 0; } } } @@ -110,10 +110,10 @@ function caml_lex_run_mem(s, i, mem, curr_pos) { for (;;) { const dst = s.charCodeAt(i); i++; - if (dst == 0xff) return; + if (dst === 0xff) return; const src = s.charCodeAt(i); i++; - if (src == 0xff) mem[dst + 1] = curr_pos; + if (src === 0xff) mem[dst + 1] = curr_pos; else mem[dst + 1] = mem[src + 1]; } } @@ -122,10 +122,10 @@ function caml_lex_run_tag(s, i, mem) { for (;;) { const dst = s.charCodeAt(i); i++; - if (dst == 0xff) return; + if (dst === 0xff) return; const src = s.charCodeAt(i); i++; - if (src == 0xff) mem[dst + 1] = -1; + if (src === 0xff) mem[dst + 1] = -1; else mem[dst + 1] = mem[src + 1]; } } @@ -199,7 +199,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; + if (lexbuf[lex_eof_reached] === 0) return -state - 1; else c = 256; } else { /* Read next input char */ @@ -208,18 +208,18 @@ function caml_new_lex_engine(tbl, start_state, lexbuf) { } /* Determine next state */ const pstate = state; - if (tbl.lex_check[base + c] == state) state = tbl.lex_trans[base + c]; + if (tbl.lex_check[base + c] === state) state = tbl.lex_trans[base + c]; else state = tbl.lex_default[state]; /* If no transition on this char, return to last backtrack point */ if (state < 0) { lexbuf[lex_curr_pos] = lexbuf[lex_last_pos]; - if (lexbuf[lex_last_action] == -1) caml_failwith("lexing: empty token"); + if (lexbuf[lex_last_action] === -1) caml_failwith("lexing: empty token"); else return lexbuf[lex_last_action]; } else { /* If some transition, get and perform memory moves */ const base_code = tbl.lex_base_code[pstate]; let pc_off; - if (tbl.lex_check_code[base_code + c] == pstate) + if (tbl.lex_check_code[base_code + c] === pstate) pc_off = tbl.lex_trans_code[base_code + c]; else pc_off = tbl.lex_default_code[pstate]; if (pc_off > 0) @@ -232,7 +232,7 @@ function caml_new_lex_engine(tbl, start_state, lexbuf) { /* Erase the EOF condition only if the EOF pseudo-character was consumed by the automaton (i.e. there was no backtrack above) */ - if (c == 256) lexbuf[lex_eof_reached] = 0; + if (c === 256) lexbuf[lex_eof_reached] = 0; } } } diff --git a/runtime/marshal.js b/runtime/marshal.js index d1344a381c..4b82e527a7 100644 --- a/runtime/marshal.js +++ b/runtime/marshal.js @@ -234,7 +234,7 @@ function caml_float_of_bytes(a) { //Provides: caml_input_value_from_string mutable //Requires: MlStringReader, caml_input_value_from_reader function caml_input_value_from_string(s, ofs) { - const reader = new MlStringReader(s, typeof ofs == "number" ? ofs : ofs[0]); + const reader = new MlStringReader(s, typeof ofs === "number" ? ofs : ofs[0]); return caml_input_value_from_reader(reader, ofs); } @@ -243,7 +243,7 @@ function caml_input_value_from_string(s, ofs) { function caml_input_value_from_bytes(s, ofs) { const reader = new MlStringReader( caml_string_of_bytes(s), - typeof ofs == "number" ? ofs : ofs[0], + typeof ofs === "number" ? ofs : ofs[0], ); return caml_input_value_from_reader(reader, ofs); } @@ -329,10 +329,10 @@ function caml_input_value_from_reader(reader, ofs) { function readvlq(overflow) { let c = reader.read8u(); let n = c & 0x7f; - while ((c & 0x80) != 0) { + while ((c & 0x80) !== 0) { c = reader.read8u(); const n7 = n << 7; - if (n != n7 >> 7) overflow[0] = true; + if (n !== n7 >> 7) overflow[0] = true; n = n7 | (c & 0x7f); } return n; @@ -391,7 +391,7 @@ function caml_input_value_from_reader(reader, ofs) { const tag = code & 0xf; const size = (code >> 4) & 0x7; const v = [tag]; - if (size == 0) return v; + if (size === 0) return v; if (intern_obj_table) intern_obj_table[obj_counter++] = v; stack.push(v, size); return v; @@ -416,19 +416,19 @@ function caml_input_value_from_reader(reader, ofs) { case 0x04: { //cst.CODE_SHARED8: let offset = reader.read8u(); - if (compressed == 0) offset = obj_counter - offset; + 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; + 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; + if (compressed === 0) offset = obj_counter - offset; return intern_obj_table[offset]; } case 0x08: { @@ -437,7 +437,7 @@ function caml_input_value_from_reader(reader, ofs) { const tag = header & 0xff; const size = header >> 10; const v = [tag]; - if (size == 0) return v; + if (size === 0) return v; if (intern_obj_table) intern_obj_table[obj_counter++] = v; stack.push(v, size); return v; @@ -536,7 +536,7 @@ function caml_input_value_from_reader(reader, ofs) { //cst.CODE_CUSTOM_FIXED: let c; let s = ""; - while ((c = reader.read8u()) != 0) s += String.fromCharCode(c); + while ((c = reader.read8u()) !== 0) s += String.fromCharCode(c); const ops = caml_custom_ops[s]; let expected_size; if (!ops) @@ -561,8 +561,8 @@ function caml_input_value_from_reader(reader, ofs) { const old_pos = reader.i; const size = [0]; const v = ops.deserialize(reader, size); - if (expected_size != undefined) { - if (expected_size != size[0]) + if (expected_size !== undefined) { + if (expected_size !== size[0]) caml_failwith( "input_value: incorrect length of serialized custom block", ); @@ -615,10 +615,10 @@ function caml_marshal_data_size(s, ofs) { function readvlq(overflow) { let c = r.read8u(); let n = c & 0x7f; - while ((c & 0x80) != 0) { + while ((c & 0x80) !== 0) { c = r.read8u(); const n7 = n << 7; - if (n != n7 >> 7) overflow[0] = true; + if (n !== n7 >> 7) overflow[0] = true; n = n7 | (c & 0x7f); } return n; @@ -780,7 +780,7 @@ const caml_output_val = (() => { const sz_32_64 = [0, 0]; if (!ops.serialize) caml_invalid_argument("output_value: abstract value (Custom)"); - if (ops.fixed_length == undefined) { + if (ops.fixed_length === undefined) { writer.write(8, 0x18 /*cst.CODE_CUSTOM_LEN*/); for (let i = 0; i < name.length; i++) writer.write(8, name.charCodeAt(i)); @@ -800,7 +800,7 @@ const caml_output_val = (() => { writer.write(8, 0); const old_pos = writer.pos(); ops.serialize(writer, v, sz_32_64); - if (ops.fixed_length != writer.pos() - old_pos) + if (ops.fixed_length !== writer.pos() - old_pos) caml_failwith( "output_value: incorrect fixed sizes specified by " + name, ); @@ -808,7 +808,7 @@ const caml_output_val = (() => { writer.size_32 += 2 + ((sz_32_64[0] + 3) >> 2); writer.size_64 += 2 + ((sz_32_64[1] + 7) >> 3); } else if (Array.isArray(v) && v[0] === (v[0] | 0)) { - if (v[0] == 251) { + if (v[0] === 251) { caml_failwith("output_value: abstract value (Abstract)"); } if (caml_is_continuation_tag(v[0])) @@ -856,9 +856,9 @@ const caml_output_val = (() => { writer.size_32 += 1 + (((len + 4) / 4) | 0); writer.size_64 += 1 + (((len + 8) / 8) | 0); } else { - if (v != (v | 0)) { + if (v !== (v | 0)) { const type_of_v = typeof v; - if (type_of_v != "number") + if (type_of_v !== "number") caml_failwith("output_value: abstract value (" + type_of_v + ")"); // If a float happens to be an integer it is serialized as an integer // (Js_of_ocaml cannot tell whether the type of an integer number is diff --git a/runtime/md5.js b/runtime/md5.js index 10360a2444..be66d29889 100644 --- a/runtime/md5.js +++ b/runtime/md5.js @@ -27,7 +27,7 @@ function caml_md5_chan(chanid, toread) { if (toread < 0) { while (true) { const read = caml_ml_input_block(chanid, buffer, 0, buffer.length); - if (read == 0) break; + if (read === 0) break; caml_MD5Update(ctx, buffer.subarray(0, read), read); } } else { @@ -38,7 +38,7 @@ function caml_md5_chan(chanid, toread) { 0, toread > buffer.length ? buffer.length : toread, ); - if (read == 0) caml_raise_end_of_file(); + if (read === 0) caml_raise_end_of_file(); caml_MD5Update(ctx, buffer.subarray(0, read), read); toread -= read; } diff --git a/runtime/mlBytes.js b/runtime/mlBytes.js index 7fba868667..81d95062f1 100644 --- a/runtime/mlBytes.js +++ b/runtime/mlBytes.js @@ -48,7 +48,7 @@ //Provides: caml_str_repeat function caml_str_repeat(n, s) { - if (n == 0) return ""; + if (n === 0) return ""; if (s.repeat) { return s.repeat(n); } // ECMAscript 6 and Firefox 24+ @@ -57,10 +57,10 @@ function caml_str_repeat(n, s) { for (;;) { if (n & 1) r += s; n >>= 1; - if (n == 0) return r; + if (n === 0) return r; s += s; l++; - if (l == 9) { + if (l === 9) { s.slice(0, 1); // flatten the string // then, the flattening of the whole string will be faster, // as it will be composed of larger pieces @@ -75,7 +75,7 @@ function caml_str_repeat(n, s) { // fail here. Mark the primitive as Weakdef, so that people can override it easily. function caml_subarray_to_jsbytes(a, i, len) { const f = String.fromCharCode; - if (i == 0 && len <= 4096 && len == a.length) return f.apply(null, a); + if (i === 0 && len <= 4096 && len === a.length) return f.apply(null, a); let s = ""; for (; 0 < len; i += 1024, len -= 1024) s += f.apply(null, a.slice(i, i + Math.min(len, 1024))); @@ -97,7 +97,7 @@ function caml_utf8_of_utf16(s) { t = ""; b += s.slice(i, j); } else t += s.slice(i, j); - if (j == l) break; + if (j === l) break; i = j; } if (c < 0x800) { @@ -111,7 +111,7 @@ function caml_utf8_of_utf16(s) { ); } else if ( c >= 0xdbff || - i + 1 == l || + i + 1 === l || (d = s.charCodeAt(i + 1)) < 0xdc00 || d > 0xdfff ) { @@ -151,25 +151,29 @@ function caml_utf16_of_utf8(s) { t = ""; b += s.slice(i, j); } else t += s.slice(i, j); - if (j == l) break; + if (j === l) break; i = j; } v = 1; - if (++i < l && ((c2 = s.charCodeAt(i)) & -64) == 128) { + if (++i < l && ((c2 = s.charCodeAt(i)) & -64) === 128) { c = c2 + (c1 << 6); if (c1 < 0xe0) { v = c - 0x3080; if (v < 0x80) v = 1; } else { v = 2; - if (++i < l && ((c2 = s.charCodeAt(i)) & -64) == 128) { + if (++i < l && ((c2 = s.charCodeAt(i)) & -64) === 128) { c = c2 + (c << 6); if (c1 < 0xf0) { v = c - 0xe2080; if (v < 0x800 || (v >= 0xd7ff && v < 0xe000)) v = 2; } else { v = 3; - if (++i < l && ((c2 = s.charCodeAt(i)) & -64) == 128 && c1 < 0xf5) { + if ( + ++i < l && + ((c2 = s.charCodeAt(i)) & -64) === 128 && + c1 < 0xf5 + ) { v = c2 - 0x3c82080 + (c << 6); if (v < 0x10000 || v > 0x10ffff) v = 3; } @@ -220,10 +224,10 @@ function caml_bytes_unsafe_get(s, i) { function caml_bytes_unsafe_set(s, i, c) { // The OCaml compiler uses Char.unsafe_chr on integers larger than 255! c &= 0xff; - if (s.t != 4 /* ARRAY */) { - if (i == s.c.length) { + if (s.t !== 4 /* ARRAY */) { + if (i === s.c.length) { s.c += String.fromCharCode(c); - if (i + 1 == s.l) s.t = 0; /*BYTES | UNKOWN*/ + if (i + 1 === s.l) s.t = 0; /*BYTES | UNKOWN*/ return 0; } caml_convert_bytes_to_array(s); @@ -462,11 +466,11 @@ MlBytes.prototype.toString = function () { }; MlBytes.prototype.toUtf16 = function () { const r = this.toString(); - if (this.t == 9) return r; + if (this.t === 9) return r; return caml_utf16_of_utf8(r); }; MlBytes.prototype.slice = function () { - const content = this.t == 4 ? this.c.slice() : this.c; + const content = this.t === 4 ? this.c.slice() : this.c; return new MlBytes(this.t, content, this.l); }; @@ -474,7 +478,7 @@ MlBytes.prototype.slice = function () { //Requires: caml_str_repeat, caml_subarray_to_jsbytes function caml_convert_string_to_bytes(s) { /* Assumes not BYTES */ - if (s.t == 2 /* PARTIAL */) s.c += caml_str_repeat(s.l - s.c.length, "\0"); + if (s.t === 2 /* PARTIAL */) s.c += caml_str_repeat(s.l - s.c.length, "\0"); else s.c = caml_subarray_to_jsbytes(s.c, 0, s.c.length); s.t = 0; /*BYTES | UNKOWN*/ } @@ -496,7 +500,7 @@ function caml_convert_bytes_to_array(s) { //Provides: caml_uint8_array_of_bytes mutable //Requires: caml_convert_bytes_to_array function caml_uint8_array_of_bytes(s) { - if (s.t != 4 /* ARRAY */) caml_convert_bytes_to_array(s); + if (s.t !== 4 /* ARRAY */) caml_convert_bytes_to_array(s); return s.c; } @@ -562,7 +566,7 @@ function caml_bytes_equal(s1, s2) { if (s1 === s2) return 1; s1.t & 6 && caml_convert_string_to_bytes(s1); s2.t & 6 && caml_convert_string_to_bytes(s2); - return s1.c == s2.c ? 1 : 0; + return s1.c === s2.c ? 1 : 0; } //Provides: caml_string_notequal mutable (const, const) @@ -621,16 +625,16 @@ function caml_bytes_greaterthan(s1, s2) { //Alias: caml_fill_string function caml_fill_bytes(s, i, l, c) { if (l > 0) { - if (i == 0 && (l >= s.l || (s.t == 2 /* PARTIAL */ && l >= s.c.length))) { - if (c == 0) { + if (i === 0 && (l >= s.l || (s.t === 2 /* PARTIAL */ && l >= s.c.length))) { + if (c === 0) { s.c = ""; s.t = 2; /* PARTIAL */ } else { s.c = caml_str_repeat(l, String.fromCharCode(c)); - s.t = l == s.l ? 0 /* BYTES | UNKOWN */ : 2; /* PARTIAL */ + s.t = l === s.l ? 0 /* BYTES | UNKOWN */ : 2; /* PARTIAL */ } } else { - if (s.t != 4 /* ARRAY */) caml_convert_bytes_to_array(s); + if (s.t !== 4 /* ARRAY */) caml_convert_bytes_to_array(s); for (l += i; i < l; i++) s.c[i] = c; } } @@ -640,31 +644,31 @@ function caml_fill_bytes(s, i, l, c) { //Provides: caml_blit_bytes //Requires: caml_subarray_to_jsbytes, caml_convert_bytes_to_array function caml_blit_bytes(s1, i1, s2, i2, len) { - if (len == 0) return 0; + if (len === 0) return 0; if ( - i2 == 0 && - (len >= s2.l || (s2.t == 2 /* PARTIAL */ && len >= s2.c.length)) + i2 === 0 && + (len >= s2.l || (s2.t === 2 /* PARTIAL */ && len >= s2.c.length)) ) { s2.c = - s1.t == 4 /* ARRAY */ + s1.t === 4 /* ARRAY */ ? caml_subarray_to_jsbytes(s1.c, i1, len) - : i1 == 0 && s1.c.length == len + : i1 === 0 && s1.c.length === len ? s1.c : s1.c.substr(i1, len); - s2.t = s2.c.length == s2.l ? 0 /* BYTES | UNKOWN */ : 2; /* PARTIAL */ - } else if (s2.t == 2 /* PARTIAL */ && i2 == s2.c.length) { + s2.t = s2.c.length === s2.l ? 0 /* BYTES | UNKOWN */ : 2; /* PARTIAL */ + } else if (s2.t === 2 /* PARTIAL */ && i2 === s2.c.length) { s2.c += - s1.t == 4 /* ARRAY */ + s1.t === 4 /* ARRAY */ ? caml_subarray_to_jsbytes(s1.c, i1, len) - : i1 == 0 && s1.c.length == len + : i1 === 0 && s1.c.length === len ? s1.c : s1.c.substr(i1, len); - s2.t = s2.c.length == s2.l ? 0 /* BYTES | UNKOWN */ : 2; /* PARTIAL */ + s2.t = s2.c.length === s2.l ? 0 /* BYTES | UNKOWN */ : 2; /* PARTIAL */ } else { - if (s2.t != 4 /* ARRAY */) caml_convert_bytes_to_array(s2); + if (s2.t !== 4 /* ARRAY */) caml_convert_bytes_to_array(s2); const c1 = s1.c; const c2 = s2.c; - if (s1.t == 4 /* ARRAY */) { + if (s1.t === 4 /* ARRAY */) { if (i2 <= i1) { for (let i = 0; i < len; i++) c2[i2 + i] = c1[i1 + i]; } else { diff --git a/runtime/nat.js b/runtime/nat.js index f0ad229e0d..20a0701b5f 100644 --- a/runtime/nat.js +++ b/runtime/nat.js @@ -96,7 +96,7 @@ function nth_digit_nat_native(nat, ofs) { //Provides: num_digits_nat function num_digits_nat(nat, ofs, len) { for (let i = len - 1; i >= 0; i--) { - if (nat.data[ofs + i] != 0) return i + 1; + if (nat.data[ofs + i] !== 0) return i + 1; } return 1; // 0 counts as 1 digit } @@ -139,7 +139,7 @@ function is_digit_int(nat, ofs) { //Provides: is_digit_zero function is_digit_zero(nat, ofs) { - if (nat.data[ofs] == 0) return 1; + if (nat.data[ofs] === 0) return 1; return 0; } @@ -155,7 +155,7 @@ function incr_nat(nat, ofs, len, carry_in) { for (let i = 0; i < len; i++) { const x = (nat.data[ofs + i] >>> 0) + carry; nat.data[ofs + i] = x | 0; - if (x == x >>> 0) { + if (x === x >>> 0) { carry = 0; break; } else { @@ -173,7 +173,7 @@ function add_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { for (let i = 0; i < len2; i++) { const x = (nat1.data[ofs1 + i] >>> 0) + (nat2.data[ofs2 + i] >>> 0) + carry; nat1.data[ofs1 + i] = x; - if (x == x >>> 0) { + if (x === x >>> 0) { carry = 0; } else { carry = 1; @@ -192,7 +192,7 @@ function complement_nat(nat, ofs, len) { // ocaml flips carry_in //Provides: decr_nat function decr_nat(nat, ofs, len, carry_in) { - let borrow = carry_in == 1 ? 0 : 1; + let borrow = carry_in === 1 ? 0 : 1; for (let i = 0; i < len; i++) { const x = (nat.data[ofs + i] >>> 0) - borrow; nat.data[ofs + i] = x; @@ -203,7 +203,7 @@ function decr_nat(nat, ofs, len, carry_in) { borrow = 1; } } - return borrow == 1 ? 0 : 1; + return borrow === 1 ? 0 : 1; } // ocaml flips carry_in @@ -211,7 +211,7 @@ function decr_nat(nat, ofs, len, carry_in) { //Provides: sub_nat //Requires: decr_nat function sub_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { - let borrow = carry_in == 1 ? 0 : 1; + let borrow = carry_in === 1 ? 0 : 1; for (let i = 0; i < len2; i++) { const x = (nat1.data[ofs1 + i] >>> 0) - (nat2.data[ofs2 + i] >>> 0) - borrow; @@ -222,7 +222,7 @@ function sub_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { borrow = 1; } } - return decr_nat(nat1, ofs1 + len2, len1 - len2, borrow == 1 ? 0 : 1); + return decr_nat(nat1, ofs1 + len2, len1 - len2, borrow === 1 ? 0 : 1); } // nat1 += nat2 * nat3[ofs3] @@ -294,7 +294,7 @@ function square_nat(nat1, ofs1, len1, nat2, ofs2, len2) { // 0 <= shift < 32 //Provides: shift_left_nat function shift_left_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { - if (nbits == 0) { + if (nbits === 0) { nat2.data[ofs2] = 0; return 0; } @@ -340,7 +340,7 @@ function div_digit_nat(natq, ofsq, natr, ofsr, nat1, ofs1, len, nat2, ofs2) { //Provides: div_nat //Requires: div_digit_nat, div_helper, num_leading_zero_bits_in_digit, shift_left_nat, shift_right_nat, create_nat, set_to_zero_nat, mult_digit_nat, sub_nat, compare_nat, nat_of_array function div_nat(nat1, ofs1, len1, nat2, ofs2, len2) { - if (len2 == 1) { + if (len2 === 1) { div_digit_nat(nat1, ofs1 + 1, nat1, ofs1, nat1, ofs1, len1, nat2, ofs2); return 0; } @@ -354,7 +354,7 @@ function div_nat(nat1, ofs1, len1, nat2, ofs2, len2) { for (let i = len1 - 1; i >= len2; i--) { // Decent lower bound on quo let quo = - d == 4294967296 + d === 4294967296 ? nat1.data[ofs1 + i] >>> 0 : div_helper( nat1.data[ofs1 + i] >>> 0, @@ -366,7 +366,7 @@ function div_nat(nat1, ofs1, len1, nat2, ofs2, len2) { sub_nat(nat1, ofs1 + i - len2, len2 + 1, a, 0, len2 + 1, 1); while ( - nat1.data[ofs1 + i] != 0 || + nat1.data[ofs1 + i] !== 0 || compare_nat(nat1, ofs1 + i - len2, len2, nat2, ofs2, len2) >= 0 ) { quo = quo + 1; @@ -384,7 +384,7 @@ function div_nat(nat1, ofs1, len1, nat2, ofs2, len2) { // 0 <= shift < 32 //Provides: shift_right_nat function shift_right_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { - if (nbits == 0) { + if (nbits === 0) { nat2.data[ofs2] = 0; return 0; } diff --git a/runtime/obj.js b/runtime/obj.js index eb2b23e5b0..e099d0ca69 100644 --- a/runtime/obj.js +++ b/runtime/obj.js @@ -46,10 +46,10 @@ function caml_obj_is_block(x) { //Provides: caml_obj_tag //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]; + 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 instanceof Function || typeof x === "function") return 247; else if (x && x.caml_custom) return 255; else return 1000; } @@ -88,7 +88,7 @@ function caml_obj_dup(x) { //Requires: caml_invalid_argument function caml_obj_truncate(x, s) { if (s <= 0 || s + 1 > x.length) caml_invalid_argument("Obj.truncate"); - if (x.length != s + 1) x.length = s + 1; + if (x.length !== s + 1) x.length = s + 1; return 0; } @@ -101,7 +101,7 @@ function caml_obj_make_forward(b, v) { //Provides: caml_obj_compare_and_swap function caml_obj_compare_and_swap(x, i, old, n) { - if (x[i + 1] == old) { + if (x[i + 1] === old) { x[i + 1] = n; return 1; } @@ -141,7 +141,7 @@ function caml_get_public_method(obj, tag, cacheid) { } caml_method_cache[cacheid] = li + 1; /* return 0 if tag is not there */ - return tag == meths[li + 1] ? meths[li] : 0; + return tag === meths[li + 1] ? meths[li] : 0; } //Provides: caml_oo_last_id @@ -183,7 +183,7 @@ function caml_obj_add_offset(v, offset) { //Provides: caml_obj_update_tag function caml_obj_update_tag(b, o, n) { - if (b[0] == o) { + if (b[0] === o) { b[0] = n; return 1; } @@ -195,7 +195,7 @@ function caml_obj_update_tag(b, o, n) { function caml_lazy_update_to_forcing(o) { if ( Array.isArray(o) && - o[0] == o[0] >>> 0 && + o[0] === o[0] >>> 0 && caml_obj_update_tag(o, 246, 244) ) { return 0; @@ -221,7 +221,7 @@ function caml_lazy_reset_to_lazy(o) { //Provides: caml_lazy_read_result //Requires: caml_obj_tag function caml_lazy_read_result(o) { - return caml_obj_tag(o) == 250 ? o[1] : o; + return caml_obj_tag(o) === 250 ? o[1] : o; } //Provides: caml_is_continuation_tag @@ -233,7 +233,7 @@ function caml_is_continuation_tag(t) { //Provides: caml_is_continuation_tag //Version: >= 5 function caml_is_continuation_tag(t) { - return t == 245 ? 1 : 0; + return t === 245 ? 1 : 0; } //Provides: caml_custom_identifier diff --git a/runtime/parsing.js b/runtime/parsing.js index 18d2498ab4..cf290f8890 100644 --- a/runtime/parsing.js +++ b/runtime/parsing.js @@ -87,7 +87,7 @@ function caml_parse_engine(tables, env, cmd, arg) { function token_name(names, number) { const str = caml_jsstring_of_string(names); - if (str[0] == "\x00") return ""; + if (str[0] === "\x00") return ""; return str.split("\x00")[number]; } @@ -96,8 +96,8 @@ function caml_parse_engine(tables, env, cmd, arg) { let kind; if (Array.isArray(tok)) { token = token_name(tables[tbl_names_block], tok[0]); - if (typeof tok[1] == "number") kind = "" + tok[1]; - else if (typeof tok[1] == "string") kind = tok[1]; + if (typeof tok[1] === "number") kind = "" + tok[1]; + else if (typeof tok[1] === "string") kind = tok[1]; else if (tok[1] instanceof MlBytes) kind = caml_jsbytes_of_string(tok[1]); else kind = "_"; log("State " + state + ": read token " + token + "(" + kind + ")"); @@ -139,7 +139,7 @@ function caml_parse_engine(tables, env, cmd, arg) { case 6: //loop: n = tables.defred[state]; - if (n != 0) { + if (n !== 0) { cmd = reduce; break; } @@ -166,10 +166,10 @@ function caml_parse_engine(tables, env, cmd, arg) { n1 = tables.sindex[state]; n2 = n1 + env[env_curr_char]; if ( - n1 != 0 && + n1 !== 0 && n2 >= 0 && n2 <= tables[tbl_tablesize] && - tables.check[n2] == env[env_curr_char] + tables.check[n2] === env[env_curr_char] ) { cmd = shift; break; @@ -177,10 +177,10 @@ function caml_parse_engine(tables, env, cmd, arg) { n1 = tables.rindex[state]; n2 = n1 + env[env_curr_char]; if ( - n1 != 0 && + n1 !== 0 && n2 >= 0 && n2 <= tables[tbl_tablesize] && - tables.check[n2] == env[env_curr_char] + tables.check[n2] === env[env_curr_char] ) { n = tables.table[n2]; cmd = reduce; @@ -200,10 +200,10 @@ function caml_parse_engine(tables, env, cmd, arg) { n1 = tables.sindex[state1]; n2 = n1 + ERRCODE; if ( - n1 != 0 && + n1 !== 0 && n2 >= 0 && n2 <= tables[tbl_tablesize] && - tables.check[n2] == ERRCODE + tables.check[n2] === ERRCODE ) { if (caml_parser_trace) log("Recovering in state " + state1); cmd = shift_recover; @@ -219,7 +219,7 @@ function caml_parse_engine(tables, env, cmd, arg) { } } } else { - if (env[env_curr_char] == 0) + if (env[env_curr_char] === 0) return RAISE_PARSE_ERROR; /* The ML code raises Parse_error */ if (caml_parser_trace) log("Discarding last token read"); env[env_curr_char] = -1; @@ -263,10 +263,10 @@ function caml_parse_engine(tables, env, cmd, arg) { n1 = tables.gindex[m]; n2 = n1 + state1; if ( - n1 != 0 && + n1 !== 0 && n2 >= 0 && n2 <= tables[tbl_tablesize] && - tables.check[n2] == state1 + tables.check[n2] === state1 ) state = tables.table[n2]; else state = tables.dgoto[m]; diff --git a/runtime/stdlib.js b/runtime/stdlib.js index f4668fc54c..de08655cf5 100644 --- a/runtime/stdlib.js +++ b/runtime/stdlib.js @@ -24,7 +24,7 @@ function caml_call_gen(f, args) { const n = f.l >= 0 ? f.l : (f.l = f.length); const argsLen = args.length; const d = n - argsLen; - if (d == 0) return f.apply(null, args); + if (d === 0) return f.apply(null, args); else if (d < 0) { const g = f.apply(null, args.slice(0, n)); if (typeof g !== "function") return g; @@ -74,7 +74,7 @@ function caml_call_gen(f, args) { const n = f.l >= 0 ? f.l : (f.l = f.length); let argsLen = args.length; const d = n - argsLen; - if (d == 0) { + if (d === 0) { return f.apply(null, args); } else if (d < 0) { const rest = args.slice(n - 1); diff --git a/runtime/stdlib_modern.js b/runtime/stdlib_modern.js index 9bb6910f27..eabb36a714 100644 --- a/runtime/stdlib_modern.js +++ b/runtime/stdlib_modern.js @@ -22,7 +22,7 @@ function caml_call_gen(f, args) { const n = f.l >= 0 ? f.l : (f.l = f.length); const argsLen = args.length; const d = n - argsLen; - if (d == 0) return f(...args); + if (d === 0) return f(...args); else if (d < 0) { const g = f(...args.slice(0, n)); if (typeof g !== "function") return g; @@ -51,7 +51,7 @@ function caml_call_gen(f, args) { } default: { g = function () { - const extra_args = arguments.length == 0 ? 1 : arguments.length; + 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++) @@ -71,7 +71,7 @@ function caml_call_gen(f, args) { const n = f.l >= 0 ? f.l : (f.l = f.length); let argsLen = args.length; const d = n - argsLen; - if (d == 0) return f(...args); + if (d === 0) return f(...args); else if (d < 0) { const rest = args.slice(n - 1); const k = args[argsLen - 1]; @@ -111,7 +111,7 @@ function caml_call_gen(f, args) { } default: { g = function () { - const extra_args = arguments.length == 0 ? 1 : arguments.length; + 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++) diff --git a/runtime/str.js b/runtime/str.js index 9f2c1969f9..6f40ce1c4c 100644 --- a/runtime/str.js +++ b/runtime/str.js @@ -194,17 +194,17 @@ const re_match = (() => { else backtrack(); break; case opcodes.BOL: - if (pos > 0 && s_[pos - 1] != 10 /* \n */) { + if (pos > 0 && s_[pos - 1] !== 10 /* \n */) { backtrack(); } break; case opcodes.EOL: - if (pos < s_.length && s_[pos] != 10 /* \n */) { + if (pos < s_.length && s_[pos] !== 10 /* \n */) { backtrack(); } break; case opcodes.WORDBOUNDARY: - if (pos == 0) { + if (pos === 0) { if (pos === s_.length) { prefix_match(); break; @@ -215,7 +215,7 @@ const re_match = (() => { if (is_word_letter(s_[pos - 1])) break; backtrack(); } else { - if (is_word_letter(s_[pos - 1]) != is_word_letter(s_[pos])) break; + if (is_word_letter(s_[pos - 1]) !== is_word_letter(s_[pos])) break; backtrack(); } break; @@ -240,7 +240,7 @@ const re_match = (() => { prefix_match(); break; } - if (s_[i] != s_[pos]) { + if (s_[i] !== s_[pos]) { backtrack(); break; } @@ -356,10 +356,10 @@ function re_replacement_text(repl, groups, orig) { let c; while (n < len) { cur = repl_.charAt(n++); - if (cur != "\\") { + if (cur !== "\\") { res += cur; } else { - if (n == len) caml_failwith("Str.replace: illegal backslash sequence"); + if (n === len) caml_failwith("Str.replace: illegal backslash sequence"); cur = repl_.charAt(n++); switch (cur) { case "\\": @@ -380,7 +380,7 @@ function re_replacement_text(repl, groups, orig) { caml_failwith("Str.replace: reference to unmatched group"); start = caml_array_get(groups, c * 2); end = caml_array_get(groups, c * 2 + 1); - if (start == -1) + if (start === -1) caml_failwith("Str.replace: reference to unmatched group"); res += orig_.slice(start, end); break; diff --git a/runtime/sys.js b/runtime/sys.js index 9830ef3214..bfda600f57 100644 --- a/runtime/sys.js +++ b/runtime/sys.js @@ -49,13 +49,13 @@ function caml_is_special_exception(exn) { //Requires: MlBytes, caml_is_special_exception function caml_format_exception(exn) { let r = ""; - if (exn[0] == 0) { + if (exn[0] === 0) { let bucket; let start; r += exn[1][1]; if ( - exn.length == 3 && - exn[2][0] == 0 && + exn.length === 3 && + exn[2][0] === 0 && caml_is_special_exception(exn[1]) ) { bucket = exn[2]; @@ -68,7 +68,7 @@ function caml_format_exception(exn) { for (let i = start; i < bucket.length; i++) { if (i > start) r += ", "; const v = bucket[i]; - if (typeof v == "number") r += v.toString(); + if (typeof v === "number") r += v.toString(); else if (v instanceof MlBytes) { r += '"' + v.toString() + '"'; } else if (typeof v == "string") { @@ -111,7 +111,7 @@ function caml_set_static_env(k, v) { function jsoo_sys_getenv(n) { const process = globalThis.process; //nodejs env - if (process && process.env && process.env[n] != undefined) + 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]; @@ -188,7 +188,7 @@ function caml_sys_executable_name(a) { //Requires: caml_jsstring_of_string function caml_sys_system_command(cmd) { const cmd_ = caml_jsstring_of_string(cmd); - if (typeof require != "undefined") { + if (typeof require !== "undefined") { const child_process = require("child_process"); if (child_process && child_process.execSync) try { @@ -232,8 +232,8 @@ function caml_sys_random_seed() { return [0, a[0], a[1], a[2], a[3]]; } } - var now = new Date().getTime(); - var x = now ^ (0xffffffff * Math.random()); + const now = new Date().getTime(); + const x = now ^ (0xffffffff * Math.random()); return [0, x]; } @@ -262,17 +262,17 @@ function caml_sys_const_max_wosize() { //Provides: caml_sys_const_ostype_unix const //Requires: os_type function caml_sys_const_ostype_unix() { - return os_type == "Unix" ? 1 : 0; + return os_type === "Unix" ? 1 : 0; } //Provides: caml_sys_const_ostype_win32 const //Requires: os_type function caml_sys_const_ostype_win32() { - return os_type == "Win32" ? 1 : 0; + return os_type === "Win32" ? 1 : 0; } //Provides: caml_sys_const_ostype_cygwin const //Requires: os_type function caml_sys_const_ostype_cygwin() { - return os_type == "Cygwin" ? 1 : 0; + return os_type === "Cygwin" ? 1 : 0; } //Provides: caml_sys_const_backend_type const @@ -285,7 +285,7 @@ function caml_sys_const_backend_type() { const os_type = globalThis.process && globalThis.process.platform && - globalThis.process.platform == "win32" + globalThis.process.platform === "win32" ? "Win32" : "Unix"; diff --git a/runtime/unix.js b/runtime/unix.js index 03fc4f70e0..676dc3409e 100644 --- a/runtime/unix.js +++ b/runtime/unix.js @@ -264,7 +264,7 @@ function caml_unix_rmdir(name) { function caml_unix_symlink(to_dir, src, dst) { const src_root = resolve_fs_device(src); const dst_root = resolve_fs_device(dst); - if (src_root.device != dst_root.device) + if (src_root.device !== dst_root.device) caml_failwith("caml_unix_symlink: cannot symlink between two filesystems"); if (!src_root.device.symlink) { caml_failwith("caml_unix_symlink: not implemented"); diff --git a/runtime/weak.js b/runtime/weak.js index fd3b0c10cc..aa0e00d572 100644 --- a/runtime/weak.js +++ b/runtime/weak.js @@ -57,7 +57,7 @@ function caml_ephe_unset_key(x, i) { if (key === old) count++; } } - if (count == 1) x[1].unregister(old); + if (count === 1) x[1].unregister(old); } } x[caml_ephe_key_offset + i] = undefined; @@ -84,7 +84,7 @@ function caml_weak_create(n) { //Requires: caml_invalid_argument //Requires: caml_ephe_set_key, caml_ephe_unset_key function caml_weak_set(x, i, v) { - if (v == 0) caml_ephe_unset_key(x, i); + if (v === 0) caml_ephe_unset_key(x, i); else caml_ephe_set_key(x, i, v[1]); return 0; } diff --git a/runtime/zstd.js b/runtime/zstd.js index 5fcb225c15..31dbfae162 100644 --- a/runtime/zstd.js +++ b/runtime/zstd.js @@ -62,7 +62,7 @@ const zstd_decompress = (function () { // read Zstandard frame header const rzfh = function (dat, w) { const n3 = dat[0] | (dat[1] << 8) | (dat[2] << 16); - if (n3 == 0x2fb528 && dat[3] == 253) { + if (n3 === 0x2fb528 && dat[3] === 253) { // Zstandard const flg = dat[4]; // single segment checksum dict flag frame content flag @@ -74,14 +74,14 @@ const zstd_decompress = (function () { // byte let bt = 6 - ss; // dict bytes - const db = df == 3 ? 4 : df; + const db = df === 3 ? 4 : df; // dictionary id const di = rb(dat, bt, db); bt += db; // frame size bytes const fsb = fcf ? 1 << fcf : ss; // frame source size - const fss = rb(dat, bt, fsb) + (fcf == 1 && 256); + const fss = rb(dat, bt, fsb) + (fcf === 1 && 256); // window size let ws = fss; if (!ss) { @@ -90,21 +90,23 @@ const zstd_decompress = (function () { ws = wb + (wb >> 3) * (dat[5] & 7); } if (ws > 2145386496) err(1); - const buf = new u8((w == 1 ? fss || ws : w ? 0 : ws) + 12); - (buf[0] = 1), (buf[4] = 4), (buf[8] = 8); + const buf = new u8((w === 1 ? fss || ws : w ? 0 : ws) + 12); + buf[0] = 1; + buf[4] = 4; + buf[8] = 8; return { b: bt + fsb, y: 0, l: 0, d: di, - w: w && w != 1 ? w : buf.subarray(12), + w: w && w !== 1 ? w : buf.subarray(12), e: ws, o: new i32(buf.buffer, 0, 3), u: fss, c: cc, m: Math.min(131072, ws), }; - } else if (((n3 >> 4) | (dat[3] << 20)) == 0x184d2a5) { + } else if (((n3 >> 4) | (dat[3] << 20)) === 0x184d2a5) { // skippable return b4(dat, 4) + 8; } @@ -155,13 +157,15 @@ const zstd_decompress = (function () { const msv = msk - probs - 1; // small value const sval = val & msk1fb; - if (sval < msv) (tpos += bits), (val = sval); - else { + if (sval < msv) { + tpos += bits; + val = sval; + } else { tpos += bits + 1; if (val > msk1fb) val -= msv; } freq[++sym] = --val; - if (val == -1) { + if (val === -1) { probs += val; syms[--ht] = sym; } else probs -= val; @@ -172,7 +176,7 @@ const zstd_decompress = (function () { re = ((dat[rbt] | (dat[rbt + 1] << 8)) >> (tpos & 7)) & 3; tpos += 2; sym += re; - } while (re == 3); + } while (re === 3); } } if (sym > 255 || probs) err(0); @@ -217,8 +221,8 @@ const zstd_decompress = (function () { // read huffman const rhu = function (dat, bt) { // index weight count - let i = 0, - wc = -1; + let i = 0; + let wc = -1; // buffer header byte const buf = new u8(292); const hb = dat[bt]; @@ -306,7 +310,7 @@ const zstd_decompress = (function () { const pv = ri[i]; fill(nb, i, pv, (ri[i - 1] = pv + rc[i] * (1 << (mb - i)))); } - if (ri[0] != ts) err(0); + if (ri[0] !== ts) err(0); for (i = 0; i < wc; ++i) { const bits = hw[i]; if (bits) { @@ -400,7 +404,7 @@ const zstd_decompress = (function () { out[++i] = hu.s[st]; pos -= btr = hu.n[st]; } - if (pos != eb || i + 1 != ss) err(0); + if (pos !== eb || i + 1 !== ss) err(0); }; // decode huffman stream 4x // TODO: use workers to parallelize @@ -438,7 +442,7 @@ const zstd_decompress = (function () { const sz = (b0 >> 3) | (dat[bt + 1] << 5) | (dat[bt + 2] << 13); // end byte for block const ebt = (bt += 3) + sz; - if (btype == 1) { + if (btype === 1) { if (bt >= dat.length) return; st.b = bt + 1; if (out) { @@ -448,7 +452,7 @@ const zstd_decompress = (function () { return fill(new u8(sz), dat[bt]); } if (ebt > dat.length) return; - if (btype == 0) { + if (btype === 0) { st.b = ebt; if (out) { out.set(dat.subarray(bt, ebt), st.y); @@ -457,7 +461,7 @@ const zstd_decompress = (function () { } return slc(dat, bt, ebt); } - if (btype == 2) { + if (btype === 2) { // byte 3 lit btype size format const b3 = dat[bt]; const lbt = b3 & 3; @@ -471,27 +475,28 @@ const zstd_decompress = (function () { else lss = b3 >> 3; } else { s4 = sf; - if (sf < 2) - (lss |= (dat[++bt] & 63) << 4), - (lcs = (dat[bt] >> 6) | (dat[++bt] << 2)); - else if (sf == 2) - (lss |= (dat[++bt] << 4) | ((dat[++bt] & 3) << 12)), - (lcs = (dat[bt] >> 2) | (dat[++bt] << 6)); - else - (lss |= (dat[++bt] << 4) | ((dat[++bt] & 63) << 12)), - (lcs = (dat[bt] >> 6) | (dat[++bt] << 2) | (dat[++bt] << 10)); + if (sf < 2) { + lss |= (dat[++bt] & 63) << 4; + lcs = (dat[bt] >> 6) | (dat[++bt] << 2); + } else if (sf === 2) { + lss |= (dat[++bt] << 4) | ((dat[++bt] & 3) << 12); + lcs = (dat[bt] >> 2) | (dat[++bt] << 6); + } else { + lss |= (dat[++bt] << 4) | ((dat[++bt] & 63) << 12); + lcs = (dat[bt] >> 6) | (dat[++bt] << 2) | (dat[++bt] << 10); + } } ++bt; // add literals to end - can never overlap with backreferences because unused literals always appended let buf = out ? out.subarray(st.y, st.y + st.m) : new u8(st.m); // starting point for literals let spl = buf.length - lss; - if (lbt == 0) buf.set(dat.subarray(bt, (bt += lss)), spl); - else if (lbt == 1) fill(buf, dat[bt++], spl); + if (lbt === 0) buf.set(dat.subarray(bt, (bt += lss)), spl); + else if (lbt === 1) fill(buf, dat[bt++], spl); else { // huffman table let hu = st.h; - if (lbt == 2) { + if (lbt === 2) { const hud = rhu(dat, bt); // subtract description length lcs += bt - (bt = hud[0]); @@ -502,7 +507,7 @@ const zstd_decompress = (function () { // num sequences let ns = dat[bt++]; if (ns) { - if (ns == 255) ns = (dat[bt++] | (dat[bt++] << 8)) + 0x7f00; + if (ns === 255) ns = (dat[bt++] | (dat[bt++] << 8)) + 0x7f00; else if (ns > 127) ns = ((ns - 128) << 8) | dat[bt++]; // symbol compression modes const scm = dat[bt++]; @@ -510,7 +515,7 @@ const zstd_decompress = (function () { const dts = [dmlt, doct, dllt]; for (let i = 2; i > -1; --i) { const md = (scm >> ((i << 1) + 2)) & 3; - if (md == 1) { + if (md === 1) { // rle buf const rbuf = new u8([0, 0, dat[bt++]]); dts[i] = { @@ -519,10 +524,10 @@ const zstd_decompress = (function () { t: new u16(rbuf.buffer, 0, 1), b: 0, }; - } else if (md == 2) { + } 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]); - } else if (md == 3) { + } else if (md === 3) { if (!st.t) err(0); dts[i] = st.t[i]; } @@ -604,7 +609,8 @@ const zstd_decompress = (function () { for (let i = 0; i < ll; ++i) { buf[oubt + i] = buf[spl + i]; } - (oubt += ll), (spl += ll); + oubt += ll; + spl += ll; let stin = oubt - off; if (stin < 0) { let len = -stin; @@ -613,14 +619,16 @@ const zstd_decompress = (function () { for (let i = 0; i < len; ++i) { buf[oubt + i] = st.w[bs + i]; } - (oubt += len), (ml -= len), (stin = 0); + oubt += len; + ml -= len; + stin = 0; } for (let i = 0; i < ml; ++i) { buf[oubt + i] = buf[stin + i]; } oubt += ml; } - if (oubt != spl) { + if (oubt !== spl) { while (spl < buf.length) { buf[oubt++] = buf[spl++]; } @@ -644,7 +652,7 @@ const zstd_decompress = (function () { }; // concat const cct = function (bufs, ol) { - if (bufs.length == 1) return bufs[0]; + if (bufs.length === 1) return bufs[0]; const buf = new u8(ol); for (let i = 0, b = 0; i < bufs.length; ++i) { const chk = bufs[i]; @@ -669,10 +677,10 @@ const zstd_decompress = (function () { let ol = 0; while (dat.length) { const st = rzfh(dat, nb || buf); - if (typeof st == "object") { + if (typeof st === "object") { if (nb) { buf = null; - if (st.w.length == st.u) { + if (st.w.length === st.u) { bufs.push((buf = st.w)); ol += st.u; } From ecf3c78c602f41a7636c7d425d74fdfeb17c879c Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Sat, 14 Sep 2024 14:35:16 +0900 Subject: [PATCH 05/12] runtime: fix no-extra-boolean-cast Signed-off-by: Sora Morimoto --- runtime/fs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/fs.js b/runtime/fs.js index 3c0446c587..c108a6aaa4 100644 --- a/runtime/fs.js +++ b/runtime/fs.js @@ -66,7 +66,7 @@ function make_path_is_absolute() { const isUnc = Boolean(device && device.charAt(1) !== ":"); // UNC paths are always absolute - if (Boolean(result[2] || isUnc)) { + if (result[2] || isUnc) { const root = result[1] || ""; const sep = result[2] || ""; return [root, path.substring(root.length + sep.length)]; From 9f7f1183c6fa812112094f56564bc2bf57fc3f0a Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Sat, 14 Sep 2024 14:47:25 +0900 Subject: [PATCH 06/12] runtime: fix use-nodejs-import-protocol Signed-off-by: Sora Morimoto --- runtime/fs_node.js | 8 ++++---- runtime/sys.js | 2 +- runtime/unix.js | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/runtime/fs_node.js b/runtime/fs_node.js index f95dd52048..eecfbdef4d 100644 --- a/runtime/fs_node.js +++ b/runtime/fs_node.js @@ -35,7 +35,7 @@ function fs_node_supported() { //Requires: MlNodeFd, caml_raise_sys_error, caml_raise_with_args //Requires: make_unix_err_args, caml_named_value, caml_string_of_jsstring function MlNodeDevice(root) { - this.fs = require("fs"); + this.fs = require("node:fs"); this.root = root; } MlNodeDevice.prototype.nm = function (name) { @@ -95,7 +95,7 @@ MlNodeDevice.prototype.unlink = function (name, raise_unix) { } }; MlNodeDevice.prototype.open = function (name, f, raise_unix) { - const consts = require("constants"); + const consts = require("node:constants"); let res = 0; for (const key in f) { switch (key) { @@ -268,7 +268,7 @@ function MlNodeDevice() {} //Provides: MlNodeFd //Requires: MlFile, caml_uint8_array_of_string, caml_uint8_array_of_bytes, caml_bytes_set, caml_raise_sys_error function MlNodeFd(fd, flags) { - this.fs = require("fs"); + this.fs = require("node:fs"); this.fd = fd; this.flags = flags; } @@ -326,7 +326,7 @@ function MlNodeFd() {} function caml_sys_open_for_node(fd, flags) { if (flags.name) { try { - const fs = require("fs"); + const fs = require("node:fs"); const fd2 = fs.openSync(flags.name, "rs"); return new MlNodeFd(fd2, flags); } catch (e) {} diff --git a/runtime/sys.js b/runtime/sys.js index bfda600f57..39dbf340a1 100644 --- a/runtime/sys.js +++ b/runtime/sys.js @@ -189,7 +189,7 @@ function caml_sys_executable_name(a) { function caml_sys_system_command(cmd) { const cmd_ = caml_jsstring_of_string(cmd); if (typeof require !== "undefined") { - const child_process = require("child_process"); + const child_process = require("node:child_process"); if (child_process && child_process.execSync) try { child_process.execSync(cmd_, { stdio: "inherit" }); diff --git a/runtime/unix.js b/runtime/unix.js index 676dc3409e..18e8a9a666 100644 --- a/runtime/unix.js +++ b/runtime/unix.js @@ -88,7 +88,7 @@ function caml_unix_filedescr_of_fd(x) { //Alias: unix_isatty function caml_unix_isatty(fileDescriptor) { if (fs_node_supported()) { - const tty = require("tty"); + const tty = require("node:tty"); return tty.isatty(fileDescriptor) ? 1 : 0; } else { return 0; From 0e351b2a65f4f270a385ba31a563c60e646bd6fe Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Sat, 14 Sep 2024 14:48:11 +0900 Subject: [PATCH 07/12] runtime: use literal keys Signed-off-by: Sora Morimoto --- runtime/nat.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/nat.js b/runtime/nat.js index 20a0701b5f..7712301188 100644 --- a/runtime/nat.js +++ b/runtime/nat.js @@ -2,7 +2,7 @@ //Requires: caml_custom_ops //Requires: serialize_nat, deserialize_nat, caml_hash_nat function initialize_nat() { - caml_custom_ops["_nat"] = { + caml_custom_ops._nat = { deserialize: deserialize_nat, serialize: serialize_nat, hash: caml_hash_nat, From 724396eb51f2ec96a5470551e696b02efd5b5e64 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Sat, 14 Sep 2024 14:48:57 +0900 Subject: [PATCH 08/12] runtime: use exponentiation operator Signed-off-by: Sora Morimoto --- runtime/ieee_754.js | 44 ++++++++++++++++++++++---------------------- runtime/int64.js | 6 ++---- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/runtime/ieee_754.js b/runtime/ieee_754.js index 9cd1ab0930..6995514002 100644 --- a/runtime/ieee_754.js +++ b/runtime/ieee_754.js @@ -57,9 +57,9 @@ function caml_int64_bits_of_float(x) { let exp = jsoo_floor_log2(x) + 1023; if (exp <= 0) { exp = 0; - x /= Math.pow(2, -1026); + x /= 2 ** -1026; } else { - x /= Math.pow(2, exp - 1027); + x /= 2 ** (exp - 1027); if (x < 16) { x *= 2; exp -= 1; @@ -68,7 +68,7 @@ function caml_int64_bits_of_float(x) { x /= 2; } } - const k = Math.pow(2, 24); + const k = 2 ** 24; let r3 = x | 0; x = (x - r3) * k; const r2 = x | 0; @@ -130,7 +130,7 @@ function caml_hexstring_of_float(x, prec, style) { } if (prec >= 0 && prec < 13) { /* If a precision is given, and is small, round mantissa accordingly */ - const cst = Math.pow(2, prec * 4); + const cst = 2 ** (prec * 4); x = Math.round(x * cst) / cst; } let x_str = x.toString(16); @@ -161,12 +161,12 @@ function caml_int64_float_of_bits(x) { return hi & 0x8000 ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY; else return Number.NaN; } - const k = Math.pow(2, -24); + const k = 2 ** -24; let res = (lo * k + mi) * k + (hi & 0xf); if (exp > 0) { res += 16; - res *= Math.pow(2, exp - 1027); - } else res *= Math.pow(2, -1026); + res *= 2 ** (exp - 1027); + } else res *= 2 ** -1026; if (hi & 0x8000) res = -res; return res; } @@ -177,8 +177,8 @@ function caml_nextafter_float(x, y) { if (Number.isNaN(x) || Number.isNaN(y)) return Number.NaN; if (x === y) return y; if (x === 0) { - if (y < 0) return -Math.pow(2, -1074); - else return Math.pow(2, -1074); + if (y < 0) return -(2 ** -1074); + else return 2 ** -1074; } let bits = caml_int64_bits_of_float(x); const one = caml_int64_of_int32(1); @@ -230,18 +230,18 @@ function caml_ldexp_float(x, exp) { exp |= 0; if (exp > 1023) { exp -= 1023; - x *= Math.pow(2, 1023); + x *= 2 ** 1023; if (exp > 1023) { // in case x is subnormal exp -= 1023; - x *= Math.pow(2, 1023); + x *= 2 ** 1023; } } if (exp < -1023) { exp += 1023; - x *= Math.pow(2, -1023); + x *= 2 ** -1023; } - x *= Math.pow(2, exp); + x *= 2 ** exp; return x; } //Provides: caml_frexp_float const @@ -251,7 +251,7 @@ function caml_frexp_float(x) { const neg = x < 0; if (neg) x = -x; let exp = Math.max(-1023, jsoo_floor_log2(x) + 1); - x *= Math.pow(2, -exp); + x *= 2 ** -exp; while (x < 0.5) { x *= 2; exp--; @@ -293,7 +293,7 @@ function caml_expm1_float(x) { } //Provides: caml_exp2_float const function caml_exp2_float(x) { - return Math.pow(2, x); + return 2 ** x; } //Provides: caml_log1p_float const function caml_log1p_float(x) { @@ -379,12 +379,12 @@ function caml_erfc_float(x) { //Provides: caml_fma_float const function caml_fma_float(x, y, z) { - const SPLIT = Math.pow(2, 27) + 1; - const MIN_VALUE = Math.pow(2, -1022); - const EPSILON = Math.pow(2, -52); + const SPLIT = 2 ** 27 + 1; + const MIN_VALUE = 2 ** -1022; + const EPSILON = 2 ** -52; const C = 416; - const A = Math.pow(2, +C); - const B = Math.pow(2, -C); + const A = 2 ** +C; + const B = 2 ** -C; function multiply(a, b) { const at = SPLIT * a; @@ -500,7 +500,7 @@ function caml_format_float(fmt, x) { let e = Number.parseInt(x.toString().split("+")[1]); if (e > 20) { e -= 20; - x /= Math.pow(10, e); + x /= 10 ** e; x += new Array(e + 1).join("0"); if (dp > 0) { x = x + "." + new Array(dp + 1).join("0"); @@ -586,7 +586,7 @@ function caml_float_of_string(s) { const m3 = m[3].replace(/0+$/, ""); const mantissa = Number.parseInt(m[1] + m[2] + m3, 16); const exponent = (m[5] | 0) - 4 * m3.length; - res = mantissa * Math.pow(2, exponent); + res = mantissa * 2 ** exponent; return res; } if (/^\+?inf(inity)?$/i.test(s)) return Number.POSITIVE_INFINITY; diff --git a/runtime/int64.js b/runtime/int64.js index be1d5df4fb..282ebb1764 100644 --- a/runtime/int64.js +++ b/runtime/int64.js @@ -18,7 +18,7 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. //Provides: caml_int64_offset -const caml_int64_offset = Math.pow(2, -24); +const caml_int64_offset = 2 ** -24; //Provides: MlInt64 //Requires: caml_int64_offset, caml_raise_zero_divide @@ -203,9 +203,7 @@ MlInt64.prototype.toInt = function () { return this.lo | (this.mi << 24); }; MlInt64.prototype.toFloat = function () { - return ( - (this.hi << 16) * Math.pow(2, 32) + this.mi * Math.pow(2, 24) + this.lo - ); + return (this.hi << 16) * 2 ** 32 + this.mi * 2 ** 24 + this.lo; }; MlInt64.prototype.toArray = function () { return [ From f392e192e0c9828aa12b0849560fa9e61462447f Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Sat, 14 Sep 2024 15:06:16 +0900 Subject: [PATCH 09/12] runtime: use template literals Signed-off-by: Sora Morimoto --- runtime/fs.js | 10 ++++----- runtime/fs_fake.js | 54 ++++++++++++++++++++++----------------------- runtime/graphics.js | 16 +++++++------- runtime/ieee_754.js | 10 ++++----- runtime/ints.js | 2 +- runtime/io.js | 13 +++++------ runtime/jslib.js | 4 +--- runtime/marshal.js | 4 ++-- runtime/parsing.js | 16 +++++++------- runtime/stdlib.js | 2 +- runtime/str.js | 2 +- runtime/sys.js | 6 ++--- 12 files changed, 67 insertions(+), 72 deletions(-) diff --git a/runtime/fs.js b/runtime/fs.js index c108a6aaa4..8c2a83f396 100644 --- a/runtime/fs.js +++ b/runtime/fs.js @@ -21,7 +21,7 @@ //Provides: caml_trailing_slash function caml_trailing_slash(name) { - return name.slice(-1) !== "/" ? name + "/" : name; + return name.slice(-1) !== "/" ? `${name}/` : name; } //Provides: caml_current_dir @@ -37,7 +37,7 @@ caml_current_dir = caml_trailing_slash(caml_current_dir); function caml_get_root(path) { const x = path_is_absolute(path); if (!x) return; - return x[0] + "/"; + return `${x[0]}/`; } //Provides: caml_root @@ -173,7 +173,7 @@ function resolve_fs_device(name) { } } if (res) return res; - caml_raise_sys_error("no device found for " + name_slash); + caml_raise_sys_error(`no device found for ${name_slash}`); } //Provides: caml_mount_autoload @@ -220,13 +220,13 @@ function caml_sys_chdir(dir) { //Provides: caml_raise_no_such_file //Requires: caml_raise_sys_error function caml_raise_no_such_file(name) { - caml_raise_sys_error(name + ": No such file or directory"); + caml_raise_sys_error(`${name}: No such file or directory`); } //Provides: caml_raise_not_a_dir //Requires: caml_raise_sys_error function caml_raise_not_a_dir(name) { - caml_raise_sys_error(name + ": Not a directory"); + caml_raise_sys_error(`${name}: Not a directory`); } //Provides: caml_sys_file_exists diff --git a/runtime/fs_fake.js b/runtime/fs_fake.js index 11be670378..090599a9f5 100644 --- a/runtime/fs_fake.js +++ b/runtime/fs_fake.js @@ -37,12 +37,12 @@ MlFakeDevice.prototype.create_dir_if_needed = function (name) { const comp = name.split("/"); let res = ""; for (let i = 0; i < comp.length - 1; i++) { - res += comp[i] + "/"; + res += `${comp[i]}/`; if (this.content[res]) continue; this.content[res] = Symbol("directory"); } }; -MlFakeDevice.prototype.slash = (name) => (/\/$/.test(name) ? name : name + "/"); +MlFakeDevice.prototype.slash = (name) => (/\/$/.test(name) ? name : `${name}/`); MlFakeDevice.prototype.lookup = function (name) { if (!this.content[name] && this.lookupFun) { const res = this.lookupFun( @@ -81,7 +81,7 @@ MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) { make_unix_err_args("EEXIST", "mkdir", this.nm(name)), ); } else { - caml_raise_sys_error(name + ": File exists"); + caml_raise_sys_error(`${name}: File exists`); } } let parent = /^(.*)\/[^/]+/.exec(name); @@ -93,7 +93,7 @@ MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) { make_unix_err_args("ENOENT", "mkdir", this.nm(parent)), ); } else { - caml_raise_sys_error(parent + ": No such file or directory"); + caml_raise_sys_error(`${parent}: No such file or directory`); } } if (!this.is_dir(parent)) { @@ -103,7 +103,7 @@ MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) { make_unix_err_args("ENOTDIR", "mkdir", this.nm(parent)), ); } else { - caml_raise_sys_error(parent + ": Not a directory"); + caml_raise_sys_error(`${parent}: Not a directory`); } } this.create_dir_if_needed(this.slash(name)); @@ -111,7 +111,7 @@ MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) { MlFakeDevice.prototype.rmdir = function (name, raise_unix) { const unix_error = raise_unix && caml_named_value("Unix.Unix_error"); const name_slash = name === "" ? "" : this.slash(name); - const r = new RegExp("^" + name_slash + "([^/]+)"); + const r = new RegExp(`^${name_slash}([^/]+)`); if (!this.exists(name)) { if (unix_error) { caml_raise_with_args( @@ -119,7 +119,7 @@ MlFakeDevice.prototype.rmdir = function (name, raise_unix) { make_unix_err_args("ENOENT", "rmdir", this.nm(name)), ); } else { - caml_raise_sys_error(name + ": No such file or directory"); + caml_raise_sys_error(`${name}: No such file or directory`); } } if (!this.is_dir(name)) { @@ -129,7 +129,7 @@ MlFakeDevice.prototype.rmdir = function (name, raise_unix) { make_unix_err_args("ENOTDIR", "rmdir", this.nm(name)), ); } else { - caml_raise_sys_error(name + ": Not a directory"); + caml_raise_sys_error(`${name}: Not a directory`); } } for (const n in this.content) { @@ -140,7 +140,7 @@ MlFakeDevice.prototype.rmdir = function (name, raise_unix) { make_unix_err_args("ENOTEMPTY", "rmdir", this.nm(name)), ); } else { - caml_raise_sys_error(this.nm(name) + ": Directory not empty"); + caml_raise_sys_error(`${this.nm(name)}: Directory not empty`); } } } @@ -149,12 +149,12 @@ MlFakeDevice.prototype.rmdir = function (name, raise_unix) { MlFakeDevice.prototype.readdir = function (name) { const name_slash = name === "" ? "" : this.slash(name); if (!this.exists(name)) { - caml_raise_sys_error(name + ": No such file or directory"); + caml_raise_sys_error(`${name}: No such file or directory`); } if (!this.is_dir(name)) { - caml_raise_sys_error(name + ": Not a directory"); + caml_raise_sys_error(`${name}: Not a directory`); } - const r = new RegExp("^" + name_slash + "([^/]+)"); + const r = new RegExp(`^${name_slash}([^/]+)`); const seen = {}; const a = []; for (const n in this.content) { @@ -181,7 +181,7 @@ MlFakeDevice.prototype.opendir = function (name, raise_unix) { make_unix_err_args("EBADF", "closedir", this.nm(name)), ); } else { - caml_raise_sys_error(name + ": closedir failed"); + caml_raise_sys_error(`${name}: closedir failed`); } } if (i === a.length) return null; @@ -197,7 +197,7 @@ MlFakeDevice.prototype.opendir = function (name, raise_unix) { make_unix_err_args("EBADF", "closedir", this.nm(name)), ); } else { - caml_raise_sys_error(name + ": closedir failed"); + caml_raise_sys_error(`${name}: closedir failed`); } } c = true; @@ -219,18 +219,18 @@ MlFakeDevice.prototype.open = function (name, f) { let file; if (f.rdonly && f.wronly) caml_raise_sys_error( - this.nm(name) + " : flags Open_rdonly and Open_wronly are not compatible", + `${this.nm(name)}: flags Open_rdonly and Open_wronly are not compatible`, ); if (f.text && f.binary) caml_raise_sys_error( - this.nm(name) + " : flags Open_text and Open_binary are not compatible", + `${this.nm(name)}: flags Open_text and Open_binary are not compatible`, ); this.lookup(name); if (this.content[name]) { if (this.is_dir(name)) - caml_raise_sys_error(this.nm(name) + " : is a directory"); + caml_raise_sys_error(`${this.nm(name)}: is a directory`); if (f.create && f.excl) - caml_raise_sys_error(this.nm(name) + " : file already exists"); + caml_raise_sys_error(`${this.nm(name)}: file already exists`); file = this.content[name]; if (f.truncate) file.truncate(); } else if (f.create) { @@ -247,18 +247,18 @@ MlFakeDevice.prototype.open = function (name, f) { let file; if (f.rdonly && f.wronly) caml_raise_sys_error( - this.nm(name) + " : flags Open_rdonly and Open_wronly are not compatible", + `${this.nm(name)}: flags Open_rdonly and Open_wronly are not compatible`, ); if (f.text && f.binary) caml_raise_sys_error( - this.nm(name) + " : flags Open_text and Open_binary are not compatible", + `${this.nm(name)}: flags Open_text and Open_binary are not compatible`, ); this.lookup(name); if (this.content[name]) { if (this.is_dir(name)) - caml_raise_sys_error(this.nm(name) + " : is a directory"); + caml_raise_sys_error(`${this.nm(name)}: is a directory`); if (f.create && f.excl) - caml_raise_sys_error(this.nm(name) + " : file already exists"); + caml_raise_sys_error(`${this.nm(name)}: file already exists`); file = this.content[name]; if (f.truncate) file.truncate(); } else if (f.create) { @@ -274,7 +274,7 @@ MlFakeDevice.prototype.open = function (name, f) { MlFakeDevice.prototype.register = function (name, content) { let file; if (this.content[name]) - caml_raise_sys_error(this.nm(name) + " : file already exists"); + caml_raise_sys_error(`${this.nm(name)}: file already exists`); if (caml_is_ml_bytes(content)) file = new MlFakeFile(content); if (caml_is_ml_string(content)) file = new MlFakeFile(caml_bytes_of_string(content)); @@ -293,7 +293,7 @@ MlFakeDevice.prototype.register = function (name, content) { this.content[name] = file; } else caml_raise_sys_error( - this.nm(name) + " : registering file with invalid content type", + `${this.nm(name)}: registering file with invalid content type`, ); }; @@ -369,10 +369,10 @@ MlFakeFd_out.prototype.write = function (offset, buf, pos, len) { this.log(src.toUtf16()); return 0; } - caml_raise_sys_error(this.fd + ": file descriptor already closed"); + caml_raise_sys_error(`${this.fd}: file descriptor already closed`); }; MlFakeFd_out.prototype.read = function (offset, buf, pos, len) { - caml_raise_sys_error(this.fd + ": file descriptor is write only"); + caml_raise_sys_error(`${this.fd}: file descriptor is write only`); }; MlFakeFd_out.prototype.close = function () { this.log = undefined; @@ -388,7 +388,7 @@ function MlFakeFd(name, file, flags) { } MlFakeFd.prototype.err_closed = function () { - caml_raise_sys_error(this.name + ": file descriptor already closed"); + caml_raise_sys_error(`${this.name}: file descriptor already closed`); }; MlFakeFd.prototype.length = function () { if (this.file) return this.file.length(); diff --git a/runtime/graphics.js b/runtime/graphics.js index 10f8698545..6ea87f4911 100644 --- a/runtime/graphics.js +++ b/runtime/graphics.js @@ -49,7 +49,7 @@ function caml_gr_state_set(ctx) { function caml_gr_open_graph(info) { const info_ = caml_jsstring_of_string(info); function get(name) { - const res = info.match("(^|,) *" + name + " *= *([a-zA-Z0-9_]+) *(,|$)"); + const res = info.match(`(^|,) *${name} *= *([a-zA-Z0-9_]+) *(,|$)`); if (res) return res[2]; } const specs = []; @@ -61,11 +61,11 @@ function caml_gr_open_graph(info) { let w = get("width"); w = w ? Number.parseInt(w) : 200; - specs.push("width=" + w); + specs.push(`width=${w}`); let h = get("height"); h = h ? Number.parseInt(h) : 200; - specs.push("height=" + h); + specs.push(`height=${h}`); const win = globalThis.open("about:blank", target, specs.join(",")); if (!win) { @@ -186,15 +186,15 @@ function caml_gr_size_y() { function caml_gr_set_color(color) { const s = caml_gr_state_get(); function convert(number) { - let str = "" + number.toString(16); - while (str.length < 2) str = "0" + str; + let str = `${number.toString(16)}`; + while (str.length < 2) str = `0${str}`; return str; } const r = (color >> 16) & 0xff; const g = (color >> 8) & 0xff; const b = (color >> 0) & 0xff; s.color = color; - const c_str = "#" + convert(r) + convert(g) + convert(b); + const c_str = `#${convert(r)}${convert(g)}${convert(b)}`; s.context.fillStyle = c_str; s.context.strokeStyle = c_str; return 0; @@ -383,7 +383,7 @@ function caml_gr_draw_string(str) { function caml_gr_set_font(f) { const s = caml_gr_state_get(); s.font = f; - s.context.font = s.text_size + "px " + caml_jsstring_of_string(s.font); + s.context.font = `${s.text_size}px ${caml_jsstring_of_string(s.font)}`; return 0; } @@ -393,7 +393,7 @@ function caml_gr_set_font(f) { function caml_gr_set_text_size(size) { const s = caml_gr_state_get(); s.text_size = size; - s.context.font = s.text_size + "px " + caml_jsstring_of_string(s.font); + s.context.font = `${s.text_size}px ${caml_jsstring_of_string(s.font)}`; return 0; } diff --git a/runtime/ieee_754.js b/runtime/ieee_754.js index 6995514002..b174f20ce9 100644 --- a/runtime/ieee_754.js +++ b/runtime/ieee_754.js @@ -137,7 +137,7 @@ function caml_hexstring_of_float(x, prec, style) { if (prec >= 0) { const idx = x_str.indexOf("."); if (idx < 0) { - x_str += "." + caml_str_repeat(prec, "0"); + x_str += `.${caml_str_repeat(prec, "0")}`; } else { const size = idx + 1 + prec; if (x_str.length < size) @@ -146,7 +146,7 @@ function caml_hexstring_of_float(x, prec, style) { } } return caml_string_of_jsstring( - sign_str + "0x" + x_str + "p" + exp_sign + exp.toString(10), + `${sign_str}0x${x_str}p${exp_sign}${exp.toString(10)}`, ); } @@ -503,7 +503,7 @@ function caml_format_float(fmt, x) { x /= 10 ** e; x += new Array(e + 1).join("0"); if (dp > 0) { - x = x + "." + new Array(dp + 1).join("0"); + x = `${x}.${new Array(dp + 1).join("0")}`; } return x; } else return x.toFixed(dp); @@ -529,7 +529,7 @@ function caml_format_float(fmt, x) { // exponent should be at least two digits const i = s.length; if (s.charAt(i - 3) === "e") - s = s.slice(0, i - 1) + "0" + s.slice(i - 1); + s = `${s.slice(0, i - 1)}0${s.slice(i - 1)}`; break; } case "f": @@ -548,7 +548,7 @@ function caml_format_float(fmt, x) { s = s.slice(0, i + 1) + s.slice(j); i = s.length; if (s.charAt(i - 3) === "e") - s = s.slice(0, i - 1) + "0" + s.slice(i - 1); + s = `${s.slice(0, i - 1)}0${s.slice(i - 1)}`; break; } else { let p = prec; diff --git a/runtime/ints.js b/runtime/ints.js index 1854d142c3..3e8335faa3 100644 --- a/runtime/ints.js +++ b/runtime/ints.js @@ -20,7 +20,7 @@ //Requires: caml_string_of_jsbytes, caml_jsbytes_of_string function caml_format_int(fmt, i) { if (caml_jsbytes_of_string(fmt) === "%d") - return caml_string_of_jsbytes("" + i); + return caml_string_of_jsbytes(`${i}`); const f = caml_parse_format(fmt); if (i < 0) { if (f.signedconv) { diff --git a/runtime/io.js b/runtime/io.js index 232d61ad61..358d28946f 100644 --- a/runtime/io.js +++ b/runtime/io.js @@ -82,13 +82,11 @@ function caml_sys_open(name, flags, _perms) { } if (f.rdonly && f.wronly) caml_raise_sys_error( - caml_jsbytes_of_string(name) + - " : flags Open_rdonly and Open_wronly are not compatible", + `${caml_jsbytes_of_string(name)}: flags Open_rdonly and Open_wronly are not compatible`, ); if (f.text && f.binary) caml_raise_sys_error( - caml_jsbytes_of_string(name) + - " : flags Open_text and Open_binary are not compatible", + `${caml_jsbytes_of_string(name)}: flags Open_text and Open_binary are not compatible`, ); const root = resolve_fs_device(name); const file = root.device.open(root.rest, f); @@ -170,7 +168,7 @@ function caml_ml_out_channels_list() { //Requires: caml_sys_open function caml_ml_open_descriptor_out(fd) { const file = caml_sys_fds[fd]; - if (file.flags.rdonly) caml_raise_sys_error("fd " + fd + " is readonly"); + if (file.flags.rdonly) caml_raise_sys_error(`fd ${fd} is readonly`); const buffered = file.flags.buffered !== undefined ? file.flags.buffered : 1; const channel = { file: file, @@ -192,7 +190,7 @@ function caml_ml_open_descriptor_out(fd) { //Requires: caml_sys_open function caml_ml_open_descriptor_in(fd) { const file = caml_sys_fds[fd]; - if (file.flags.wronly) caml_raise_sys_error("fd " + fd + " is writeonly"); + if (file.flags.wronly) caml_raise_sys_error(`fd ${fd} is writeonly`); const refill = null; const channel = { file: file, @@ -398,8 +396,7 @@ function caml_input_value(chanid) { const buf = new Uint8Array(len + caml_marshal_header_size); buf.set(header, 0); const r2 = block(buf, caml_marshal_header_size, len); - if (r2 < len) - caml_failwith("input_value: truncated object " + r2 + " " + len); + if (r2 < len) caml_failwith(`input_value: truncated object ${r2} ${len}`); const res = caml_input_value_from_bytes(caml_bytes_of_array(buf), 0); return res; } diff --git a/runtime/jslib.js b/runtime/jslib.js index 816e250090..2d9d526048 100644 --- a/runtime/jslib.js +++ b/runtime/jslib.js @@ -279,9 +279,7 @@ function caml_js_var(x) { //Checks that x has the form ident[.ident]* if (!x_.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*(\.[a-zA-Z_$][a-zA-Z_$0-9]*)*$/)) { console.error( - 'caml_js_var: "' + - x_ + - '" is not a valid JavaScript variable. continuing ..', + `caml_js_var: "${x_}" is not a valid JavaScript variable. continuing ..`, ); //console.error("Js.Unsafe.eval_string") } diff --git a/runtime/marshal.js b/runtime/marshal.js index 4b82e527a7..c3dde6c132 100644 --- a/runtime/marshal.js +++ b/runtime/marshal.js @@ -802,7 +802,7 @@ const caml_output_val = (() => { ops.serialize(writer, v, sz_32_64); if (ops.fixed_length !== writer.pos() - old_pos) caml_failwith( - "output_value: incorrect fixed sizes specified by " + name, + `output_value: incorrect fixed sizes specified by ${name}`, ); } writer.size_32 += 2 + ((sz_32_64[0] + 3) >> 2); @@ -859,7 +859,7 @@ const caml_output_val = (() => { if (v !== (v | 0)) { const type_of_v = typeof v; if (type_of_v !== "number") - caml_failwith("output_value: abstract value (" + type_of_v + ")"); + caml_failwith(`output_value: abstract value (${type_of_v})`); // If a float happens to be an integer it is serialized as an integer // (Js_of_ocaml cannot tell whether the type of an integer number is // float or integer.) This can result in unexpected crashes when diff --git a/runtime/parsing.js b/runtime/parsing.js index cf290f8890..281d86a9a4 100644 --- a/runtime/parsing.js +++ b/runtime/parsing.js @@ -81,7 +81,7 @@ function caml_parse_engine(tables, env, cmd, arg) { const tbl_names_block = 16; function log(x) { - const s = caml_string_of_jsbytes(x + "\n"); + const s = caml_string_of_jsbytes(`${x}\n`); caml_ml_output(2, s, 0, caml_ml_string_length(s)); } @@ -96,14 +96,14 @@ function caml_parse_engine(tables, env, cmd, arg) { let kind; if (Array.isArray(tok)) { token = token_name(tables[tbl_names_block], tok[0]); - if (typeof tok[1] === "number") kind = "" + tok[1]; + if (typeof tok[1] === "number") kind = `${tok[1]}`; else if (typeof tok[1] === "string") kind = tok[1]; else if (tok[1] instanceof MlBytes) kind = caml_jsbytes_of_string(tok[1]); else kind = "_"; - log("State " + state + ": read token " + token + "(" + kind + ")"); + log(`State ${state}: read token ${token}(${kind})`); } else { token = token_name(tables[tbl_names_const], tok); - log("State " + state + ": read token " + token); + log(`State ${state}: read token ${token}`); } } @@ -205,11 +205,11 @@ function caml_parse_engine(tables, env, cmd, arg) { n2 <= tables[tbl_tablesize] && tables.check[n2] === ERRCODE ) { - if (caml_parser_trace) log("Recovering in state " + state1); + if (caml_parser_trace) log(`Recovering in state ${state1}`); cmd = shift_recover; break next; } else { - if (caml_parser_trace) log("Discarding state " + state1); + 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; @@ -233,7 +233,7 @@ function caml_parse_engine(tables, env, cmd, arg) { // Fall through case 9: //shift_recover: if (caml_parser_trace) - log("State " + state + ": shift to state " + tables.table[n2]); + log(`State ${state}: shift to state ${tables.table[n2]}`); state = tables.table[n2]; sp++; if (sp >= env[env_stacksize]) { @@ -252,7 +252,7 @@ function caml_parse_engine(tables, env, cmd, arg) { case 10: { //reduce: - if (caml_parser_trace) log("State " + state + ": reduce by rule " + n); + if (caml_parser_trace) log(`State ${state}: reduce by rule ${n}`); let m = tables.len[n]; env[env_asp] = sp; env[env_rule_number] = n; diff --git a/runtime/stdlib.js b/runtime/stdlib.js index de08655cf5..ee856a6430 100644 --- a/runtime/stdlib.js +++ b/runtime/stdlib.js @@ -175,7 +175,7 @@ function caml_register_global(n, v, name_opt) { const nid = caml_global_data.symidx[name]; if (nid >= 0) n = nid; else { - caml_failwith("caml_register_global: cannot locate " + name); + caml_failwith(`caml_register_global: cannot locate ${name}`); } } } diff --git a/runtime/str.js b/runtime/str.js index 6f40ce1c4c..b8079fa77e 100644 --- a/runtime/str.js +++ b/runtime/str.js @@ -385,7 +385,7 @@ function re_replacement_text(repl, groups, orig) { res += orig_.slice(start, end); break; default: - res += "\\" + cur; + res += `\\${cur}`; } } } diff --git a/runtime/sys.js b/runtime/sys.js index 39dbf340a1..d5ce024d3c 100644 --- a/runtime/sys.js +++ b/runtime/sys.js @@ -70,9 +70,9 @@ function caml_format_exception(exn) { const v = bucket[i]; if (typeof v === "number") r += v.toString(); else if (v instanceof MlBytes) { - r += '"' + v.toString() + '"'; + r += `"${v.toString()}"`; } else if (typeof v == "string") { - r += '"' + v.toString() + '"'; + r += `"${v.toString()}"`; } else r += "_"; } r += ")"; @@ -92,7 +92,7 @@ function caml_fatal_uncaught_exception(err) { const msg = caml_format_exception(err); const at_exit = caml_named_value("Pervasives.do_at_exit"); if (at_exit) caml_callback(at_exit, [0]); - console.error("Fatal error: exception " + msg); + console.error(`Fatal error: exception ${msg}`); if (err.js_error) throw err.js_error; } } else { From 7f29a35fc8264f45452bd68793efabc5963f152e Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Sat, 14 Sep 2024 15:07:33 +0900 Subject: [PATCH 10/12] runtime: fix no-useless-ternary Signed-off-by: Sora Morimoto --- runtime/fs_fake.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/fs_fake.js b/runtime/fs_fake.js index 090599a9f5..21a0a0e60c 100644 --- a/runtime/fs_fake.js +++ b/runtime/fs_fake.js @@ -211,7 +211,7 @@ MlFakeDevice.prototype.is_dir = function (name) { return this.content[name_slash] ? 1 : 0; }; MlFakeDevice.prototype.unlink = function (name) { - const ok = this.content[name] ? true : false; + const ok = !!this.content[name]; delete this.content[name]; return ok; }; From dda4aa2f28fa1671c76b196a48207e284f3e7f27 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Fri, 20 Sep 2024 00:56:29 +0900 Subject: [PATCH 11/12] 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..39e0cd5e47 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": { + "noAssignInExpressions": "off", + "noFallthroughSwitchClause": "off", + "noRedeclare": "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) { From 6bfaf0b8253e392751e0dffcb40aa9bd8746cbf1 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Fri, 20 Sep 2024 02:31:30 +0900 Subject: [PATCH 12/12] 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) {