Skip to content

Commit 6bfaf0b

Browse files
committed
runtime: cleanup
Signed-off-by: Sora Morimoto <[email protected]>
1 parent dda4aa2 commit 6bfaf0b

File tree

17 files changed

+46
-75
lines changed

17 files changed

+46
-75
lines changed

biome.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"rules": {
1414
"recommended": true,
1515
"style": {
16-
"noParameterAssign": "off"
16+
"noParameterAssign": "off",
17+
"useNamingConvention": "off"
1718
},
1819
"suspicious": {
1920
"noAssignInExpressions": "off",

runtime/array.js

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,12 @@
1919

2020
//Provides: caml_array_sub mutable
2121
function caml_array_sub(a, i, len) {
22-
const a2 = new Array(len + 1);
23-
a2[0] = 0;
24-
for (let i2 = 1, i1 = i + 1; i2 <= len; i2++, i1++) {
25-
a2[i2] = a[i1];
26-
}
27-
return a2;
22+
return [0, ...a.slice(i + 1, i + len + 1)];
2823
}
2924

3025
//Provides: caml_array_append mutable
3126
function caml_array_append(a1, a2) {
32-
const l1 = a1.length;
33-
const l2 = a2.length;
34-
const l = l1 + l2 - 1;
35-
const a = new Array(l);
36-
a[0] = 0;
37-
let i = 1;
38-
let j = 1;
39-
for (; i < l1; i++) a[i] = a1[i];
40-
for (; i < l; i++, j++) a[i] = a2[j];
41-
return a;
27+
return [0, ...a1.slice(1), ...a2.slice(1)];
4228
}
4329

4430
//Provides: caml_array_concat mutable
@@ -107,30 +93,19 @@ function caml_check_bound(array, index) {
10793
//Requires: caml_array_bound_error
10894
function caml_make_vect(len, init) {
10995
if (len < 0) caml_array_bound_error();
110-
const len_ = (len + 1) | 0;
111-
const b = new Array(len_);
112-
b[0] = 0;
113-
for (let i = 1; i < len_; i++) b[i] = init;
114-
return b;
96+
return [0, ...new Array(len | 0).fill(init)];
11597
}
11698

11799
//Provides: caml_make_float_vect const (const)
118100
//Requires: caml_array_bound_error
119101
function caml_make_float_vect(len) {
120102
if (len < 0) caml_array_bound_error();
121-
const len_ = (len + 1) | 0;
122-
const b = new Array(len);
123-
b[0] = 254;
124-
for (let i = 1; i < len_; i++) b[i] = 0;
125-
return b;
103+
return [254, ...new Array(len).fill(0)];
126104
}
105+
127106
//Provides: caml_floatarray_create const (const)
128107
//Requires: caml_array_bound_error
129108
function caml_floatarray_create(len) {
130109
if (len < 0) caml_array_bound_error();
131-
const len_ = (len + 1) | 0;
132-
const b = new Array(len);
133-
b[0] = 254;
134-
for (let i = 1; i < len_; i++) b[i] = 0;
135-
return b;
110+
return [254, ...new Array(len).fill(0)];
136111
}

runtime/bigarray.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function caml_ba_get_size(dims) {
3737
for (let i = 0; i < n_dims; i++) {
3838
if (dims[i] < 0)
3939
caml_invalid_argument("Bigarray.create: negative dimension");
40-
size = size * dims[i];
40+
size *= dims[i];
4141
}
4242
return size;
4343
}
@@ -539,12 +539,12 @@ function caml_ba_sub(ba, ofs, len) {
539539
let changed_dim;
540540
let mul = 1;
541541
if (ba.layout === 0) {
542-
for (let i = 1; i < ba.dims.length; i++) mul = mul * ba.dims[i];
542+
for (let i = 1; i < ba.dims.length; i++) mul *= ba.dims[i];
543543
changed_dim = 0;
544544
} else {
545-
for (let i = 0; i < ba.dims.length - 1; i++) mul = mul * ba.dims[i];
545+
for (let i = 0; i < ba.dims.length - 1; i++) mul *= ba.dims[i];
546546
changed_dim = ba.dims.length - 1;
547-
ofs = ofs - 1;
547+
ofs -= 1;
548548
}
549549
if (ofs < 0 || len < 0 || ofs + len > ba.dims[changed_dim]) {
550550
caml_invalid_argument("Bigarray.sub: bad sub-array");
@@ -606,7 +606,7 @@ function caml_ba_reshape(ba, vind) {
606606
new_dim[i] = vind[i];
607607
if (new_dim[i] < 0)
608608
caml_invalid_argument("Bigarray.reshape: negative dimension");
609-
num_elts = num_elts * new_dim[i];
609+
num_elts *= new_dim[i];
610610
}
611611

612612
const size = caml_ba_get_size(ba.dims);

runtime/bigstring.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ function caml_bigstring_memcmp(s1, pos1, s2, pos2, len) {
4949
//Provides: caml_bigstring_blit_ba_to_ba
5050
//Requires: caml_invalid_argument, caml_array_bound_error
5151
function caml_bigstring_blit_ba_to_ba(ba1, pos1, ba2, pos2, len) {
52-
if (12 !== ba1.kind)
52+
if (ba1.kind !== 12)
5353
caml_invalid_argument("caml_bigstring_blit_ba_to_ba: kind mismatch");
54-
if (12 !== ba2.kind)
54+
if (ba2.kind !== 12)
5555
caml_invalid_argument("caml_bigstring_blit_ba_to_ba: kind mismatch");
5656
if (len === 0) return 0;
5757
const ofs1 = ba1.offset(pos1);
@@ -71,7 +71,7 @@ function caml_bigstring_blit_ba_to_ba(ba1, pos1, ba2, pos2, len) {
7171
//Requires: caml_invalid_argument, caml_array_bound_error, caml_uint8_array_of_string
7272
//Requires: caml_ml_string_length
7373
function caml_bigstring_blit_string_to_ba(str1, pos1, ba2, pos2, len) {
74-
if (12 !== ba2.kind)
74+
if (ba2.kind !== 12)
7575
caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch");
7676
if (len === 0) return 0;
7777
const ofs2 = ba2.offset(pos2);
@@ -90,7 +90,7 @@ function caml_bigstring_blit_string_to_ba(str1, pos1, ba2, pos2, len) {
9090
//Requires: caml_invalid_argument, caml_array_bound_error, caml_uint8_array_of_bytes
9191
//Requires: caml_ml_bytes_length
9292
function caml_bigstring_blit_bytes_to_ba(str1, pos1, ba2, pos2, len) {
93-
if (12 !== ba2.kind)
93+
if (ba2.kind !== 12)
9494
caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch");
9595
if (len === 0) return 0;
9696
const ofs2 = ba2.offset(pos2);
@@ -110,7 +110,7 @@ function caml_bigstring_blit_bytes_to_ba(str1, pos1, ba2, pos2, len) {
110110
//Requires: caml_blit_bytes, caml_bytes_of_array
111111
//Requires: caml_ml_bytes_length
112112
function caml_bigstring_blit_ba_to_bytes(ba1, pos1, bytes2, pos2, len) {
113-
if (12 !== ba1.kind)
113+
if (ba1.kind !== 12)
114114
caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch");
115115
if (len === 0) return 0;
116116
const ofs1 = ba1.offset(pos1);

runtime/blake2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ const blake2b = (() => {
120120
}
121121

122122
// low 64 bits of offset
123-
v[24] = v[24] ^ ctx.t;
124-
v[25] = v[25] ^ (ctx.t / 0x100000000);
123+
v[24] ^= ctx.t;
124+
v[25] ^= ctx.t / 0x100000000;
125125
// high 64 bits not supported, offset may not be higher than 2**53-1
126126

127127
// last block flag set ?

runtime/fail.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function caml_raise_with_arg(tag, arg) {
3131
//Provides: caml_raise_with_args (const, mutable)
3232
//Requires: caml_maybe_attach_backtrace
3333
function caml_raise_with_args(tag, args) {
34-
throw caml_maybe_attach_backtrace([0, tag].concat(args));
34+
throw caml_maybe_attach_backtrace([0, tag, ...args]);
3535
}
3636

3737
//Provides: caml_raise_with_string (const, const)

runtime/graphics.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,9 @@ function caml_gr_dump_image(im) {
452452
//Requires: caml_gr_state_get
453453
function caml_gr_draw_image(im, x, y) {
454454
const s = caml_gr_state_get();
455-
if (!im.image) {
455+
if (im.image) {
456+
s.context.drawImage(im.image, x, s.height - im.height - y);
457+
} else {
456458
const canvas = document.createElement("canvas");
457459
canvas.width = s.width;
458460
canvas.height = s.height;
@@ -463,8 +465,6 @@ function caml_gr_draw_image(im, x, y) {
463465
im.image = image;
464466
};
465467
image.src = canvas.toDataURL("image/png");
466-
} else {
467-
s.context.drawImage(im.image, x, s.height - im.height - y);
468468
}
469469
return 0;
470470
}

runtime/ieee_754.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,7 @@ function caml_format_float(fmt, x) {
518518
if (Number.isNaN(x)) {
519519
s = "nan";
520520
f.filler = " ";
521-
} else if (!Number.isFinite(x)) {
522-
s = "inf";
523-
f.filler = " ";
524-
} else
521+
} else if (Number.isFinite(x))
525522
switch (f.conv) {
526523
case "e": {
527524
s = x.toExponential(prec);
@@ -565,6 +562,10 @@ function caml_format_float(fmt, x) {
565562
break;
566563
}
567564
}
565+
else {
566+
s = "inf";
567+
f.filler = " ";
568+
}
568569
return caml_finish_formatting(f, s);
569570
}
570571

runtime/int64.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ MlInt64.prototype.xor = function (x) {
9696
return new MlInt64(this.lo ^ x.lo, this.mi ^ x.mi, this.hi ^ x.hi);
9797
};
9898
MlInt64.prototype.shift_left = function (s) {
99-
s = s & 63;
99+
s &= 63;
100100
if (s === 0) return this;
101101
if (s < 24) {
102102
return new MlInt64(
@@ -114,7 +114,7 @@ MlInt64.prototype.shift_left = function (s) {
114114
return new MlInt64(0, 0, this.lo << (s - 48));
115115
};
116116
MlInt64.prototype.shift_right_unsigned = function (s) {
117-
s = s & 63;
117+
s &= 63;
118118
if (s === 0) return this;
119119
if (s < 24)
120120
return new MlInt64(
@@ -131,7 +131,7 @@ MlInt64.prototype.shift_right_unsigned = function (s) {
131131
return new MlInt64(this.hi >> (s - 48), 0, 0);
132132
};
133133
MlInt64.prototype.shift_right = function (s) {
134-
s = s & 63;
134+
s &= 63;
135135
if (s === 0) return this;
136136
const h = (this.hi << 16) >> 16;
137137
if (s < 24)
@@ -157,7 +157,7 @@ MlInt64.prototype.lsl1 = function () {
157157
MlInt64.prototype.lsr1 = function () {
158158
this.lo = ((this.lo >>> 1) | (this.mi << 23)) & 0xffffff;
159159
this.mi = ((this.mi >>> 1) | (this.hi << 23)) & 0xffffff;
160-
this.hi = this.hi >>> 1;
160+
this.hi >>>= 1;
161161
};
162162
MlInt64.prototype.udivmod = function (x) {
163163
let offset = 0;

runtime/ints.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function caml_int_of_string(s) {
121121
// For base different from 10, we expect an unsigned representation,
122122
// hence any value of 'res' (less than 'threshold') is acceptable.
123123
// But we have to convert the result back to a signed integer.
124-
res = sign * res;
124+
res *= sign;
125125
if (signedness && (res | 0) !== res)
126126
/* Signed representation expected, allow -2^(nbits-1) to 2^(nbits-1) - 1 */
127127
caml_failwith("int_of_string");

0 commit comments

Comments
 (0)