Skip to content

Commit

Permalink
runtime: use template literals
Browse files Browse the repository at this point in the history
Signed-off-by: Sora Morimoto <[email protected]>
  • Loading branch information
smorimoto committed Sep 14, 2024
1 parent a9ec353 commit 59b0b4f
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 72 deletions.
10 changes: 5 additions & 5 deletions runtime/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
54 changes: 27 additions & 27 deletions runtime/fs_fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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)) {
Expand All @@ -103,23 +103,23 @@ 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));
};
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(
unix_error,
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)) {
Expand All @@ -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) {
Expand All @@ -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`);
}
}
}
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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));
Expand All @@ -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`,
);
};

Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions runtime/graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions runtime/ieee_754.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)}`,
);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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":
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion runtime/ints.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 5 additions & 8 deletions runtime/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 59b0b4f

Please sign in to comment.