Skip to content

Commit 46f25d3

Browse files
committed
runtime: more...
Signed-off-by: Sora Morimoto <[email protected]>
1 parent 7f29a35 commit 46f25d3

27 files changed

+491
-539
lines changed

biome.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,20 @@
99
"useEditorconfig": true
1010
},
1111
"linter": {
12-
"enabled": false
12+
"enabled": true,
13+
"rules": {
14+
"recommended": true,
15+
"style": {
16+
"noParameterAssign": "off"
17+
},
18+
"suspicious": {
19+
"noFallthroughSwitchClause": "off",
20+
"noRedeclare": "off",
21+
"noAssignInExpressions": "off",
22+
"noSelfCompare": "off",
23+
"useDefaultSwitchClauseLast": "off"
24+
}
25+
}
1326
},
1427
"organizeImports": {
1528
"enabled": true

runtime/compare.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,23 @@
1818
//Provides: caml_compare_val_tag
1919
//Requires: caml_is_ml_string, caml_is_ml_bytes
2020
function caml_compare_val_tag(a) {
21-
if (typeof a === "number")
22-
return 1000; // int_tag (we use it for all numbers)
23-
else if (caml_is_ml_bytes(a))
24-
return 252; // string_tag
25-
else if (caml_is_ml_string(a))
26-
return 1252; // ocaml string (if different from bytes)
27-
else if (Array.isArray(a) && a[0] === a[0] >>> 0 && a[0] <= 255) {
21+
if (typeof a === "number") return 1000;
22+
if (caml_is_ml_bytes(a)) return 252;
23+
if (caml_is_ml_string(a)) return 1252;
24+
if (Array.isArray(a) && a[0] === a[0] >>> 0 && a[0] <= 255) {
2825
// Look like an ocaml block
2926
const tag = a[0] | 0;
3027
// ignore double_array_tag because we cannot accurately set
3128
// this tag when we create an array of float.
3229
return tag === 254 ? 0 : tag;
33-
} else if (a instanceof String)
34-
return 12520; // javascript string, like string_tag (252)
35-
else if (typeof a === "string")
36-
return 12520; // javascript string, like string_tag (252)
37-
else if (a instanceof Number)
38-
return 1000; // int_tag (we use it for all numbers)
39-
else if (a && a.caml_custom)
40-
return 1255; // like custom_tag (255)
41-
else if (a && a.compare)
42-
return 1256; // like custom_tag (255)
43-
else if (typeof a === "function")
44-
return 1247; // like closure_tag (247)
45-
else if (typeof a === "symbol") return 1251;
30+
}
31+
if (a instanceof String) return 12520;
32+
if (typeof a === "string") return 12520;
33+
if (a instanceof Number) return 1000;
34+
if (a?.caml_custom) return 1255;
35+
if (a?.compare) return 1256;
36+
if (typeof a === "function") return 1247;
37+
if (typeof a === "symbol") return 1251;
4638
return 1001; //out_of_heap_tag
4739
}
4840

@@ -214,6 +206,7 @@ function caml_compare_val(a, b, total) {
214206
// Exception: `!=` will not coerce/convert if both a and b are objects
215207
if (a < b) return -1;
216208
if (a > b) return 1;
209+
// biome-ignore lint/suspicious/noDoubleEquals: type-coercive comparison
217210
if (a != b) {
218211
if (!total) return Number.NaN;
219212
if (a === a) return 1;
@@ -246,8 +239,6 @@ function caml_compare_val(a, b, total) {
246239
}
247240
break;
248241
}
249-
case 246: // Lazy_tag
250-
case 254: // Double_array
251242
default: // Block with other tag
252243
if (caml_is_continuation_tag(tag_a)) {
253244
caml_invalid_argument("compare: continuation value");

runtime/dynlink.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function caml_dynlink_lookup_symbol(idx, fun_name) {
5050
const name = caml_jsstring_of_string(fun_name);
5151
console.log("Dynlink: looking for symbol", name);
5252
const current_libs = get_current_libs();
53-
if (current_libs[idx] && current_libs[idx][name])
53+
if (current_libs[idx]?.[name])
5454
return { name: name, symbol: current_libs[idx][name] };
5555
return 0;
5656
}

runtime/format.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,19 @@ function caml_parse_format(fmt) {
6060
case "6":
6161
case "7":
6262
case "8":
63-
case "9":
63+
case "9": {
6464
f.width = 0;
65-
while (((c = fmt.charCodeAt(i) - 48), c >= 0 && c <= 9)) {
65+
while ((c = fmt.charCodeAt(i) - 48) && c >= 0 && c <= 9) {
6666
f.width = f.width * 10 + c;
6767
i++;
6868
}
6969
i--;
7070
break;
71+
}
7172
case ".":
7273
f.prec = 0;
7374
i++;
74-
while (((c = fmt.charCodeAt(i) - 48), c >= 0 && c <= 9)) {
75+
while ((c = fmt.charCodeAt(i) - 48) && c >= 0 && c <= 9) {
7576
f.prec = f.prec * 10 + c;
7677
i++;
7778
}

runtime/fs.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ function make_path_is_absolute() {
7979
globalThis.process.platform
8080
) {
8181
return globalThis.process.platform === "win32" ? win32 : posix;
82-
} else return posix;
82+
}
83+
return posix;
8384
}
8485
const path_is_absolute = make_path_is_absolute();
8586

@@ -162,7 +163,7 @@ function resolve_fs_device(name) {
162163
}
163164
if (!res && fs_node_supported()) {
164165
const root = caml_get_root(name_);
165-
if (root && root.match(/^[a-zA-Z]:\/$/)) {
166+
if (root?.match(/^[a-zA-Z]:\/$/)) {
166167
const m = { path: root, device: new MlNodeDevice(root) };
167168
jsoo_mount_point.push(m);
168169
res = {
@@ -212,9 +213,8 @@ function caml_sys_chdir(dir) {
212213
caml_current_dir = caml_trailing_slash(root.path + root.rest);
213214
else caml_current_dir = root.path;
214215
return 0;
215-
} else {
216-
caml_raise_no_such_file(caml_jsbytes_of_string(dir));
217216
}
217+
caml_raise_no_such_file(caml_jsbytes_of_string(dir));
218218
}
219219

220220
//Provides: caml_raise_no_such_file

runtime/fs_fake.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ MlFakeDevice.prototype.exists = function (name) {
6868
MlFakeDevice.prototype.isFile = function (name) {
6969
if (this.exists(name) && !this.is_dir(name)) {
7070
return 1;
71-
} else {
72-
return 0;
7371
}
72+
return 0;
7473
};
7574
MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) {
7675
const unix_error = raise_unix && caml_named_value("Unix.Unix_error");
@@ -85,7 +84,7 @@ MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) {
8584
}
8685
}
8786
let parent = /^(.*)\/[^/]+/.exec(name);
88-
parent = (parent && parent[1]) || "";
87+
parent = parent?.[1] || "";
8988
if (!this.exists(parent)) {
9089
if (unix_error) {
9190
caml_raise_with_args(

runtime/fs_node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ MlNodeFd.prototype.read = function (offset, a, buf_offset, len) {
303303
try {
304304
if (this.flags.isCharacterDevice)
305305
return this.fs.readSync(this.fd, a, buf_offset, len);
306-
else return this.fs.readSync(this.fd, a, buf_offset, len, offset);
306+
return this.fs.readSync(this.fd, a, buf_offset, len, offset);
307307
} catch (err) {
308308
caml_raise_sys_error(err.toString());
309309
}

runtime/hash.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function caml_hash_univ_param(count, limit, obj) {
7272
count--;
7373
const p = caml_int64_to_bytes(caml_int64_bits_of_float(obj));
7474
for (let i = 7; i >= 0; i--) hash_accu = (hash_accu * 19 + p[i]) | 0;
75-
} else if (obj && obj.caml_custom) {
75+
} else if (obj?.caml_custom) {
7676
if (
7777
caml_custom_ops[obj.caml_custom] &&
7878
caml_custom_ops[obj.caml_custom].hash
@@ -184,7 +184,7 @@ function caml_hash_mix_bytes_arr(h, s) {
184184
function caml_hash_mix_bytes(h, v) {
185185
const content = caml_ml_bytes_content(v);
186186
if (typeof content === "string") return caml_hash_mix_jsbytes(h, content);
187-
/* ARRAY */ else return caml_hash_mix_bytes_arr(h, content);
187+
/* ARRAY */ return caml_hash_mix_bytes_arr(h, content);
188188
}
189189

190190
//Provides: caml_hash_mix_string
@@ -218,7 +218,7 @@ function caml_hash(count, limit, seed, obj) {
218218
wr = 1;
219219
while (rd < wr && num > 0) {
220220
v = queue[rd++];
221-
if (v && v.caml_custom) {
221+
if (v?.caml_custom) {
222222
if (
223223
caml_custom_ops[v.caml_custom] &&
224224
caml_custom_ops[v.caml_custom].hash

runtime/ieee_754.js

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function caml_int64_bits_of_float(x) {
4343
if (!Number.isFinite(x)) {
4444
if (Number.isNaN(x)) return caml_int64_create_lo_mi_hi(1, 0, 0x7ff0);
4545
if (x > 0) return caml_int64_create_lo_mi_hi(0, 0, 0x7ff0);
46-
else return caml_int64_create_lo_mi_hi(0, 0, 0xfff0);
46+
return caml_int64_create_lo_mi_hi(0, 0, 0xfff0);
4747
}
4848
const sign =
4949
x === 0 && 1 / x === Number.NEGATIVE_INFINITY
@@ -159,7 +159,7 @@ function caml_int64_float_of_bits(x) {
159159
if (exp === 2047) {
160160
if ((lo | mi | (hi & 0xf)) === 0)
161161
return hi & 0x8000 ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
162-
else return Number.NaN;
162+
return Number.NaN;
163163
}
164164
const k = 2 ** -24;
165165
let res = (lo * k + mi) * k + (hi & 0xf);
@@ -178,7 +178,7 @@ function caml_nextafter_float(x, y) {
178178
if (x === y) return y;
179179
if (x === 0) {
180180
if (y < 0) return -(2 ** -1074);
181-
else return 2 ** -1074;
181+
return 2 ** -1074;
182182
}
183183
let bits = caml_int64_bits_of_float(x);
184184
const one = caml_int64_of_int32(1);
@@ -340,10 +340,9 @@ function caml_round_float(x) {
340340
if (x >= 0) {
341341
const y = Math.floor(x);
342342
return x - y >= 0.5 ? y + 1 : y;
343-
} else {
344-
const y = Math.ceil(x);
345-
return y - x >= 0.5 ? y - 1 : y;
346343
}
344+
const y = Math.ceil(x);
345+
return y - x >= 0.5 ? y - 1 : y;
347346
}
348347
//Provides: caml_cbrt_float const
349348
function caml_cbrt_float(x) {
@@ -496,18 +495,18 @@ function caml_format_float(fmt, x) {
496495
function toFixed(x, dp) {
497496
if (Math.abs(x) < 1.0) {
498497
return x.toFixed(dp);
499-
} else {
500-
let e = Number.parseInt(x.toString().split("+")[1]);
501-
if (e > 20) {
502-
e -= 20;
503-
x /= 10 ** e;
504-
x += new Array(e + 1).join("0");
505-
if (dp > 0) {
506-
x = `${x}.${new Array(dp + 1).join("0")}`;
507-
}
508-
return x;
509-
} else return x.toFixed(dp);
510498
}
499+
let e = Number.parseInt(x.toString().split("+")[1]);
500+
if (e > 20) {
501+
e -= 20;
502+
x /= 10 ** e;
503+
x += new Array(e + 1).join("0");
504+
if (dp > 0) {
505+
x = `${x}.${new Array(dp + 1).join("0")}`;
506+
}
507+
return x;
508+
}
509+
return x.toFixed(dp);
511510
}
512511
let s;
513512
const f = caml_parse_format(fmt);
@@ -550,19 +549,18 @@ function caml_format_float(fmt, x) {
550549
if (s.charAt(i - 3) === "e")
551550
s = `${s.slice(0, i - 1)}0${s.slice(i - 1)}`;
552551
break;
553-
} else {
554-
let p = prec;
555-
if (exp < 0) {
556-
p -= exp + 1;
557-
s = x.toFixed(p);
558-
} else while (((s = x.toFixed(p)), s.length > prec + 1)) p--;
559-
if (p) {
560-
// remove trailing zeroes
561-
let i = s.length - 1;
562-
while (s.charAt(i) === "0") i--;
563-
if (s.charAt(i) === ".") i--;
564-
s = s.slice(0, i + 1);
565-
}
552+
}
553+
let p = prec;
554+
if (exp < 0) {
555+
p -= exp + 1;
556+
s = x.toFixed(p);
557+
} else while ((s = x.toFixed(p)) && s.length > prec + 1) p--;
558+
if (p) {
559+
// remove trailing zeroes
560+
let i = s.length - 1;
561+
while (s.charAt(i) === "0") i--;
562+
if (s.charAt(i) === ".") i--;
563+
s = s.slice(0, i + 1);
566564
}
567565
break;
568566
}

runtime/internalMod.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,7 @@ function caml_CamlinternalMod_init_mod(loc, shape) {
6161
//If: !effects
6262
//Version: < 4.13
6363
function caml_CamlinternalMod_update_mod(shape, real, x) {
64-
if (typeof shape === "number")
65-
switch (shape) {
66-
case 0: //function
67-
case 1: //lazy
68-
case 2: //class
69-
default:
70-
caml_update_dummy(real, x);
71-
}
64+
if (typeof shape === "number") caml_update_dummy(real, x);
7265
else
7366
switch (shape[0]) {
7467
case 0: //module
@@ -126,14 +119,7 @@ function caml_CamlinternalMod_init_mod(loc, shape, cont) {
126119
//Version: < 4.13
127120
function caml_CamlinternalMod_update_mod(shape, real, x, cont) {
128121
function loop(shape, real, x) {
129-
if (typeof shape === "number")
130-
switch (shape) {
131-
case 0: //function
132-
case 1: //lazy
133-
case 2: //class
134-
default:
135-
caml_update_dummy(real, x);
136-
}
122+
if (typeof shape === "number") caml_update_dummy(real, x);
137123
else
138124
switch (shape[0]) {
139125
case 0: //module

0 commit comments

Comments
 (0)