Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: fix useArrowFunction and noArguments #1682

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"rules": {
"recommended": true,
"complexity": {
"useArrowFunction": "off",
"useOptionalChain": "off"
},
"correctness": {
Expand All @@ -28,7 +27,6 @@
"noGlobalEval": "off"
},
"style": {
"noArguments": "off",
"noCommaOperator": "off",
"noParameterAssign": "off",
"noUselessElse": "off",
Expand Down
2 changes: 1 addition & 1 deletion runtime/backtrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//Requires: jsoo_sys_getenv
var caml_record_backtrace_env_flag = FLAG("with-js-error");

(function () {
(() => {
var r = jsoo_sys_getenv("OCAMLRUNPARAM");
if (r !== undefined) {
var l = r.split(",");
Expand Down
8 changes: 2 additions & 6 deletions runtime/blake2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//Provides: blake2b
//Version: >= 5.2
var blake2b = (function () {
var blake2b = (() => {
// Blake2B in pure Javascript
// Adapted from the reference implementation in RFC7693
// Ported to Javascript by DC - https://github.com/dcposch
Expand Down Expand Up @@ -104,11 +104,7 @@ var blake2b = (function () {
// These are offsets into a uint64 buffer.
// Multiply them all by 2 to make them offsets into a uint32 buffer,
// because this is Javascript and we don't have uint64s
const SIGMA82 = new Uint8Array(
SIGMA8.map(function (x) {
return x * 2;
}),
);
const SIGMA82 = new Uint8Array(SIGMA8.map((x) => x * 2));

// Compression function. 'last' flag indicates last block.
// Note we're representing 16 uint64s as 32 uint32s
Expand Down
2 changes: 1 addition & 1 deletion runtime/effect.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function caml_push_trap(handler) {
//If: effects
function caml_pop_trap() {
if (!caml_exn_stack)
return function (x) {
return (x) => {
throw x;
};
var h = caml_exn_stack[1];
Expand Down
12 changes: 3 additions & 9 deletions runtime/fs_fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ MlFakeDevice.prototype.create_dir_if_needed = function (name) {
this.content[res] = Symbol("directory");
}
};
MlFakeDevice.prototype.slash = function (name) {
return /\/$/.test(name) ? name : name + "/";
};
MlFakeDevice.prototype.slash = (name) => (/\/$/.test(name) ? name : name + "/");
MlFakeDevice.prototype.lookup = function (name) {
if (!this.content[name] && this.lookupFun) {
var res = this.lookupFun(
Expand Down Expand Up @@ -350,18 +348,14 @@ MlFakeFile.prototype.read = function (offset, buf, pos, len) {
//Requires: caml_raise_sys_error
function MlFakeFd_out(fd, flags) {
MlFakeFile.call(this, caml_create_bytes(0));
this.log = function (s) {
return 0;
};
this.log = (s) => 0;
if (fd == 1 && typeof console.log == "function") this.log = console.log;
else if (fd == 2 && typeof console.error == "function")
this.log = console.error;
else if (typeof console.log == "function") this.log = console.log;
this.flags = flags;
}
MlFakeFd_out.prototype.length = function () {
return 0;
};
MlFakeFd_out.prototype.length = () => 0;
MlFakeFd_out.prototype.write = function (offset, buf, pos, len) {
if (this.log) {
if (
Expand Down
4 changes: 2 additions & 2 deletions runtime/fs_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ MlNodeDevice.prototype.opendir = function (name, raise_unix) {
this.raise_nodejs_error(err, raise_unix);
}
};
MlNodeDevice.prototype.raise_nodejs_error = function (err, raise_unix) {
MlNodeDevice.prototype.raise_nodejs_error = (err, raise_unix) => {
var unix_error = caml_named_value("Unix.Unix_error");
if (raise_unix && unix_error) {
var args = make_unix_err_args(err.code, err.syscall, err.path, err.errno);
Expand All @@ -198,7 +198,7 @@ MlNodeDevice.prototype.raise_nodejs_error = function (err, raise_unix) {
caml_raise_sys_error(err.toString());
}
};
MlNodeDevice.prototype.stats_from_js = function (js_stats) {
MlNodeDevice.prototype.stats_from_js = (js_stats) => {
/* ===Unix.file_kind===
* type file_kind =
* S_REG (** Regular file *)
Expand Down
2 changes: 1 addition & 1 deletion runtime/gc.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function caml_final_register() {
var all_finalizers = new globalThis.Set();
function caml_final_register_called_without_value(cb, a) {
if (globalThis.FinalizationRegistry && a instanceof Object) {
var x = new globalThis.FinalizationRegistry(function (x) {
var x = new globalThis.FinalizationRegistry((x) => {
all_finalizers.delete(x);
cb(0);
return;
Expand Down
4 changes: 2 additions & 2 deletions runtime/graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function caml_gr_open_graph(info) {
canvas.width = w;
canvas.height = h;
var ctx = caml_gr_state_create(canvas, w, h);
ctx.set_title = function (title) {
ctx.set_title = (title) => {
doc.title = title;
};
caml_gr_state_set(ctx);
Expand Down Expand Up @@ -460,7 +460,7 @@ function caml_gr_draw_image(im, x, y) {
canvas.height = s.height;
canvas.getContext("2d").putImageData(im, 0, 0);
var image = new globalThis.Image();
image.onload = function () {
image.onload = () => {
s.context.drawImage(image, x, s.height - im.height - y);
im.image = image;
};
Expand Down
4 changes: 2 additions & 2 deletions runtime/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function caml_sys_open(name, flags, _perms) {
var file = root.device.open(root.rest, f);
return caml_sys_open_internal(file, undefined);
}
(function () {
(() => {
function file(fd, flags) {
if (fs_node_supported()) {
return caml_sys_open_for_node(fd, flags);
Expand Down Expand Up @@ -284,7 +284,7 @@ function caml_ml_channel_size_64(chanid) {
//Requires: caml_ml_channel_get
function caml_ml_set_channel_output(chanid, f) {
var chan = caml_ml_channel_get(chanid);
chan.output = function (s) {
chan.output = (s) => {
f(s);
};
return 0;
Expand Down
63 changes: 18 additions & 45 deletions runtime/jslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ function caml_callback(f, args) {
};
var res = {
joo_tramp: f,
joo_args: args.concat(function (x) {
return x;
}),
joo_args: args.concat((x) => x),
};
do {
caml_stack_depth = 40;
Expand Down Expand Up @@ -378,12 +376,8 @@ function caml_ojs_new_arr(c, a) {
//Provides: caml_js_wrap_callback const (const)
//Requires: caml_callback
function caml_js_wrap_callback(f) {
return function () {
var len = arguments.length;
if (len > 0) {
var args = new Array(len);
for (var i = 0; i < len; i++) args[i] = arguments[i];
} else {
return (...args) => {
if (args.length <= 0) {
args = [undefined];
}
var res = caml_callback(f, args);
Expand All @@ -394,76 +388,55 @@ function caml_js_wrap_callback(f) {
//Provides: caml_js_wrap_callback_arguments
//Requires: caml_callback
function caml_js_wrap_callback_arguments(f) {
return function () {
var len = arguments.length;
var args = new Array(len);
for (var i = 0; i < len; i++) args[i] = arguments[i];
return caml_callback(f, [args]);
};
return (...args) => caml_callback(f, [args]);
}
//Provides: caml_js_wrap_callback_strict const
//Requires: caml_callback
function caml_js_wrap_callback_strict(arity, f) {
return function () {
var n = arguments.length;
var args = new Array(arity);
var len = Math.min(arguments.length, arity);
for (var i = 0; i < len; i++) args[i] = arguments[i];
return (...args) => {
args.length = arity;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use this trick to change the length of an array. I will comment the benchmark later.

Copy link
Member Author

@smorimoto smorimoto Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return caml_callback(f, args);
};
}
//Provides: caml_js_wrap_callback_unsafe const (const)
//Requires: caml_callback, caml_js_function_arity
function caml_js_wrap_callback_unsafe(f) {
return function () {
return (...args) => {
var len = caml_js_function_arity(f);
var args = new Array(len);
for (var i = 0; i < len; i++) args[i] = arguments[i];
args.length = len;
return caml_callback(f, args);
};
}
//Provides: caml_js_wrap_meth_callback const (const)
//Requires: caml_callback, caml_js_wrap_callback
function caml_js_wrap_meth_callback(f) {
return function () {
var len = arguments.length;
var args = new Array(len + 1);
args[0] = this;
for (var i = 0; i < len; i++) args[i + 1] = arguments[i];
var res = caml_callback(f, args);
return function (...args) {
const res = caml_callback(f, [this].concat(args));
return res instanceof Function ? caml_js_wrap_callback(res) : res;
};
}
//Provides: caml_js_wrap_meth_callback_arguments const (const)
//Requires: caml_callback
function caml_js_wrap_meth_callback_arguments(f) {
return function () {
var len = arguments.length;
var args = new Array(len);
for (var i = 0; i < len; i++) args[i] = arguments[i];
return function (...args) {
return caml_callback(f, [this, args]);
};
}
//Provides: caml_js_wrap_meth_callback_strict const
//Requires: caml_callback
function caml_js_wrap_meth_callback_strict(arity, f) {
return function () {
var args = new Array(arity + 1);
var len = Math.min(arguments.length, arity);
args[0] = this;
for (var i = 0; i < len; i++) args[i + 1] = arguments[i];
return caml_callback(f, args);
return function (...args) {
args.length = arity;
return caml_callback(f, [this].concat(args));
};
}
//Provides: caml_js_wrap_meth_callback_unsafe const (const)
//Requires: caml_callback, caml_js_function_arity
function caml_js_wrap_meth_callback_unsafe(f) {
return function () {
var len = caml_js_function_arity(f) - 1;
var args = new Array(len + 1);
args[0] = this;
for (var i = 0; i < len; i++) args[i + 1] = arguments[i];
return caml_callback(f, args);
return function (...args) {
const len = caml_js_function_arity(f) - 1;
args.length = len;
return caml_callback(f, [this].concat(args));
};
}

Expand Down
16 changes: 6 additions & 10 deletions runtime/marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,13 @@ var caml_custom_ops = {
fixed_length: 4,
},
_bigarray: {
deserialize: function (reader, sz) {
return caml_ba_deserialize(reader, sz, "_bigarray");
},
deserialize: (reader, sz) => caml_ba_deserialize(reader, sz, "_bigarray"),
serialize: caml_ba_serialize,
compare: caml_ba_compare,
hash: caml_ba_hash,
},
_bigarr02: {
deserialize: function (reader, sz) {
return caml_ba_deserialize(reader, sz, "_bigarr02");
},
deserialize: (reader, sz) => caml_ba_deserialize(reader, sz, "_bigarr02"),
serialize: caml_ba_serialize,
compare: caml_ba_compare,
hash: caml_ba_hash,
Expand Down Expand Up @@ -623,7 +619,7 @@ function caml_marshal_data_size(s, ofs) {
//Provides: MlObjectTable
var MlObjectTable;
if (typeof globalThis.Map === "undefined") {
MlObjectTable = (function () {
MlObjectTable = (() => {
/* polyfill (using linear search) */
function NaiveLookup(objs) {
this.objs = objs;
Expand All @@ -633,7 +629,7 @@ if (typeof globalThis.Map === "undefined") {
if (this.objs[i] === v) return i;
}
};
NaiveLookup.prototype.set = function () {
NaiveLookup.prototype.set = () => {
// Do nothing here. [MlObjectTable.store] will push to [this.objs] directly.
};

Expand Down Expand Up @@ -668,7 +664,7 @@ MlObjectTable.prototype.recall = function (v) {
//Requires: caml_is_ml_string, caml_ml_string_length, caml_string_unsafe_get
//Requires: MlObjectTable, caml_list_to_js_array, caml_custom_ops
//Requires: caml_invalid_argument,caml_string_of_jsbytes, caml_is_continuation_tag
var caml_output_val = (function () {
var caml_output_val = (() => {
function Writer() {
this.chunk = [];
}
Expand Down Expand Up @@ -713,7 +709,7 @@ var caml_output_val = (function () {
return this.chunk;
},
};
return function (v, flags) {
return (v, flags) => {
flags = caml_list_to_js_array(flags);

var no_sharing = flags.indexOf(0 /*Marshal.No_sharing*/) !== -1,
Expand Down
4 changes: 2 additions & 2 deletions runtime/md5.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function caml_md5_string(s, ofs, len) {
}

//Provides: caml_MD5Transform
var caml_MD5Transform = (function () {
var caml_MD5Transform = (() => {
function add(x, y) {
return (x + y) | 0;
}
Expand All @@ -74,7 +74,7 @@ var caml_MD5Transform = (function () {
return xx(c ^ (b | ~d), a, b, x, s, t);
}

return function (w, buffer) {
return (w, buffer) => {
var a = w[0],
b = w[1],
c = w[2],
Expand Down
Loading