Skip to content

Commit

Permalink
runtime: more...
Browse files Browse the repository at this point in the history
Signed-off-by: Sora Morimoto <[email protected]>
  • Loading branch information
smorimoto committed Sep 19, 2024
1 parent 7f29a35 commit 46f25d3
Show file tree
Hide file tree
Showing 27 changed files with 491 additions and 539 deletions.
15 changes: 14 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,20 @@
"useEditorconfig": true
},
"linter": {
"enabled": false
"enabled": true,
"rules": {
"recommended": true,
"style": {
"noParameterAssign": "off"
},
"suspicious": {
"noFallthroughSwitchClause": "off",
"noRedeclare": "off",
"noAssignInExpressions": "off",
"noSelfCompare": "off",
"useDefaultSwitchClauseLast": "off"
}
}
},
"organizeImports": {
"enabled": true
Expand Down
35 changes: 13 additions & 22 deletions runtime/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion runtime/dynlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions runtime/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
8 changes: 4 additions & 4 deletions runtime/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions runtime/fs_fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion runtime/fs_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
58 changes: 28 additions & 30 deletions runtime/ieee_754.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 2 additions & 16 deletions runtime/internalMod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions runtime/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 46f25d3

Please sign in to comment.