Skip to content

Commit

Permalink
runtime: cleanup
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 dda4aa2 commit 6bfaf0b
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 75 deletions.
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"rules": {
"recommended": true,
"style": {
"noParameterAssign": "off"
"noParameterAssign": "off",
"useNamingConvention": "off"
},
"suspicious": {
"noAssignInExpressions": "off",
Expand Down
37 changes: 6 additions & 31 deletions runtime/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)];
}
10 changes: 5 additions & 5 deletions runtime/bigarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions runtime/bigstring.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions runtime/blake2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
Expand Down
2 changes: 1 addition & 1 deletion runtime/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions runtime/graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
9 changes: 5 additions & 4 deletions runtime/ieee_754.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -565,6 +562,10 @@ function caml_format_float(fmt, x) {
break;
}
}
else {
s = "inf";
f.filler = " ";
}
return caml_finish_formatting(f, s);
}

Expand Down
8 changes: 4 additions & 4 deletions runtime/int64.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion runtime/ints.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 1 addition & 3 deletions runtime/jslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion runtime/jslib_js_of_ocaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function caml_js_on_ie() {
}

//Provides: caml_js_html_escape const (const)
const caml_js_regexps = { amp: /&/g, lt: /</g, quot: /\"/g, all: /[&<\"]/ };
const caml_js_regexps = { amp: /&/g, lt: /</g, quot: /"/g, all: /[&<"]/ };
function caml_js_html_escape(s) {
if (!caml_js_regexps.all.test(s)) return s;
return s
Expand Down
14 changes: 5 additions & 9 deletions runtime/mlBytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ 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);
let s = "";
for (; 0 < len; i += 1024, len -= 1024)
for (; len > 0; i += 1024, len -= 1024)
s += f.apply(null, a.slice(i, i + Math.min(len, 1024)));
return s;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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 = "";
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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 = "";
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/nat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/str.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 } });
Expand Down
2 changes: 1 addition & 1 deletion runtime/unix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion runtime/zstd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6bfaf0b

Please sign in to comment.