UNPKG

@tolokoban/tgd

Version:

ToloGameDev library for WebGL2

1,441 lines 9.44 MB
"use strict"; // Generated by js_of_ocaml //# buildInfo:effects=disabled, kind=unknown, use-js-string=true, version=6.0.1 //# 6 ".wast.eobjs/jsoo/wast.bc.runtime.js" ; (function (Object) { typeof globalThis !== "object" && (this ? get() : (Object.defineProperty(Object.prototype, "_T_", { configurable: true, get: get, }), _T_)); function get() { var global = this || self; global.globalThis = global; delete Object.prototype._T_; } })(Object); (function (globalThis) { "use strict"; function caml_sub_uint8_array_to_jsbytes(a, i, len) { var f = String.fromCharCode; if (i === 0 && len <= 4096 && len === a.length) return f.apply(null, a); var s = ""; for (; 0 < len; i += 1024, len -= 1024) s += f.apply(null, a.subarray(i, i + Math.min(len, 1024))); return s; } function caml_string_of_uint8_array(a) { return caml_sub_uint8_array_to_jsbytes(a, 0, a.length); } function caml_ba_get_1(ba, i0) { return ba.get(ba.offset(i0)); } function BigStringReader(bs, i) { this.s = bs; this.i = i; } BigStringReader.prototype = { read8u: function () { return caml_ba_get_1(this.s, this.i++); }, read8s: function () { return (caml_ba_get_1(this.s, this.i++) << 24) >> 24; }, read16u: function () { var s = this.s, i = this.i; this.i = i + 2; return (caml_ba_get_1(s, i) << 8) | caml_ba_get_1(s, i + 1); }, read16s: function () { var s = this.s, i = this.i; this.i = i + 2; return ((caml_ba_get_1(s, i) << 24) >> 16) | caml_ba_get_1(s, i + 1); }, read32u: function () { var s = this.s, i = this.i; this.i = i + 4; return (((caml_ba_get_1(s, i) << 24) | (caml_ba_get_1(s, i + 1) << 16) | (caml_ba_get_1(s, i + 2) << 8) | caml_ba_get_1(s, i + 3)) >>> 0); }, read32s: function () { var s = this.s, i = this.i; this.i = i + 4; return ((caml_ba_get_1(s, i) << 24) | (caml_ba_get_1(s, i + 1) << 16) | (caml_ba_get_1(s, i + 2) << 8) | caml_ba_get_1(s, i + 3)); }, readstr: function (len) { var i = this.i, offset = this.offset(i); this.i = i + len; return caml_string_of_uint8_array(this.s.data.subarray(offset, offset + len)); }, readuint8array: function (len) { var i = this.i, offset = this.offset(i); this.i = i + len; return this.s.data.subarray(offset, offset + len); }, }; function caml_str_repeat(n, s) { if (n === 0) return ""; if (s.repeat) return s.repeat(n); var r = "", l = 0; for (;;) { if (n & 1) r += s; n >>= 1; if (n === 0) return r; s += s; l++; if (l === 9) s.slice(0, 1); } } function caml_convert_string_to_bytes(s) { if (s.t === 2) s.c += caml_str_repeat(s.l - s.c.length, "\0"); else s.c = caml_sub_uint8_array_to_jsbytes(s.c, 0, s.c.length); s.t = 0; } function jsoo_is_ascii(s) { if (s.length < 24) { for (var i = 0; i < s.length; i++) if (s.charCodeAt(i) > 127) return false; return true; } else return !/[^\x00-\x7f]/.test(s); } function caml_utf16_of_utf8(s) { for (var b = "", t = "", c, c1, c2, v, i = 0, l = s.length; i < l; i++) { c1 = s.charCodeAt(i); if (c1 < 0x80) { for (var j = i + 1; j < l && (c1 = s.charCodeAt(j)) < 0x80; j++) ; if (j - i > 512) { t.slice(0, 1); b += t; t = ""; b += s.slice(i, j); } else t += s.slice(i, j); if (j === l) break; i = j; } v = 1; if (++i < l && ((c2 = s.charCodeAt(i)) & -64) === 128) { c = c2 + (c1 << 6); if (c1 < 0xe0) { v = c - 0x3080; if (v < 0x80) v = 1; } else { v = 2; if (++i < l && ((c2 = s.charCodeAt(i)) & -64) === 128) { c = c2 + (c << 6); if (c1 < 0xf0) { v = c - 0xe2080; if (v < 0x800 || (v >= 0xd7ff && v < 0xe000)) v = 2; } else { v = 3; if (++i < l && ((c2 = s.charCodeAt(i)) & -64) === 128 && c1 < 0xf5) { v = c2 - 0x3c82080 + (c << 6); if (v < 0x10000 || v > 0x10ffff) v = 3; } } } } } if (v < 4) { i -= v; t += "\ufffd"; } else if (v > 0xffff) t += String.fromCharCode(0xd7c0 + (v >> 10), 0xdc00 + (v & 0x3ff)); else t += String.fromCharCode(v); if (t.length > 1024) { t.slice(0, 1); b += t; t = ""; } } return b + t; } function MlBytes(tag, contents, length) { this.t = tag; this.c = contents; this.l = length; } MlBytes.prototype.toString = function () { switch (this.t) { case 9: case 8: return this.c; case 4: case 2: caml_convert_string_to_bytes(this); case 0: if (jsoo_is_ascii(this.c)) this.t = 9; else this.t = 8; return this.c; } }; MlBytes.prototype.toUtf16 = function () { var r = this.toString(); if (this.t === 9) return r; return caml_utf16_of_utf8(r); }; MlBytes.prototype.slice = function () { var content = this.t === 4 ? this.c.slice() : this.c; return new MlBytes(this.t, content, this.l); }; function MlChanid(id) { this.id = id; } var jsoo_static_env = {}; function jsoo_sys_getenv(n) { if (jsoo_static_env[n]) return jsoo_static_env[n]; var process = globalThis.process; if (process && process.env && process.env[n] !== undefined) return process.env[n]; if (globalThis.jsoo_env && typeof globalThis.jsoo_env[n] === "string") return globalThis.jsoo_env[n]; } var caml_record_backtrace_env_flag = 0; (function () { var r = jsoo_sys_getenv("OCAMLRUNPARAM"); if (r !== undefined) { var l = r.split(","); for (var i = 0; i < l.length; i++) if (l[i] === "b") { caml_record_backtrace_env_flag = 1; break; } else if (l[i].startsWith("b=")) caml_record_backtrace_env_flag = +l[i].slice(2); else continue; } })(); var caml_record_backtrace_runtime_flag = caml_record_backtrace_env_flag, caml_global_data = [0]; function caml_exn_with_js_backtrace(exn, force) { if (!exn.js_error || force || exn[0] === 248) exn.js_error = new globalThis.Error("Js exception containing backtrace"); return exn; } function caml_maybe_attach_backtrace(exn, force) { return caml_record_backtrace_env_flag && caml_record_backtrace_runtime_flag ? caml_exn_with_js_backtrace(exn, force) : exn; } function caml_raise_with_arg(tag, arg) { throw caml_maybe_attach_backtrace([0, tag, arg]); } function caml_utf8_of_utf16(s) { for (var b = "", t = b, c, d, i = 0, l = s.length; i < l; i++) { c = s.charCodeAt(i); if (c < 0x80) { for (var j = i + 1; j < l && (c = s.charCodeAt(j)) < 0x80; j++) ; if (j - i > 512) { t.slice(0, 1); b += t; t = ""; b += s.slice(i, j); } else t += s.slice(i, j); if (j === l) break; i = j; } if (c < 0x800) { t += String.fromCharCode(0xc0 | (c >> 6)); t += String.fromCharCode(0x80 | (c & 0x3f)); } else if (c < 0xd800 || c >= 0xdfff) t += String.fromCharCode(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f)); else if (c >= 0xdbff || i + 1 === l || (d = s.charCodeAt(i + 1)) < 0xdc00 || d > 0xdfff) t += "\xef\xbf\xbd"; else { i++; c = (c << 10) + d - 0x35fdc00; t += String.fromCharCode(0xf0 | (c >> 18), 0x80 | ((c >> 12) & 0x3f), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f)); } if (t.length > 1024) { t.slice(0, 1); b += t; t = ""; } } return b + t; } function caml_string_of_jsbytes(x) { return x; } function caml_string_of_jsstring(s) { return jsoo_is_ascii(s) ? caml_string_of_jsbytes(s) : caml_string_of_jsbytes(caml_utf8_of_utf16(s)); } function caml_raise_sys_error(msg) { caml_raise_with_arg(caml_global_data.Sys_error, caml_string_of_jsstring(msg)); } function caml_raise_with_args(tag, args) { throw caml_maybe_attach_backtrace([0, tag].concat(args)); } var unix_error = [ "E2BIG", "EACCES", "EAGAIN", "EBADF", "EBUSY", "ECHILD", "EDEADLK", "EDOM", "EEXIST", "EFAULT", "EFBIG", "EINTR", "EINVAL", "EIO", "EISDIR", "EMFILE", "EMLINK", "ENAMETOOLONG", "ENFILE", "ENODEV", "ENOENT", "ENOEXEC", "ENOLCK", "ENOMEM", "ENOSPC", "ENOSYS", "ENOTDIR", "ENOTEMPTY", "ENOTTY", "ENXIO", "EPERM", "EPIPE", "ERANGE", "EROFS", "ESPIPE", "ESRCH", "EXDEV", "EWOULDBLOCK", "EINPROGRESS", "EALREADY", "ENOTSOCK", "EDESTADDRREQ", "EMSGSIZE", "EPROTOTYPE", "ENOPROTOOPT", "EPROTONOSUPPORT", "ESOCKTNOSUPPORT", "EOPNOTSUPP", "EPFNOSUPPORT", "EAFNOSUPPORT", "EADDRINUSE", "EADDRNOTAVAIL", "ENETDOWN", "ENETUNREACH", "ENETRESET", "ECONNABORTED", "ECONNRESET", "ENOBUFS", "EISCONN", "ENOTCONN", "ESHUTDOWN", "ETOOMANYREFS", "ETIMEDOUT", "ECONNREFUSED", "EHOSTDOWN", "EHOSTUNREACH", "ELOOP", "EOVERFLOW", ]; function make_unix_err_args(code, syscall, path, errno) { var variant = unix_error.indexOf(code); if (variant < 0) { if (errno == null) errno = -9999; variant = [0, -errno]; } var args = [variant, caml_string_of_jsstring(syscall || ""), caml_string_of_jsstring(path || "")]; return args; } var caml_named_values = {}; function caml_named_value(nm) { return caml_named_values[nm]; } function caml_raise_system_error(raise_unix, code, cmd, msg, path) { var unix_error = caml_named_value("Unix.Unix_error"); if (raise_unix && unix_error) caml_raise_with_args(unix_error, make_unix_err_args(code, cmd, path)); else { var msg = code + ": " + msg + ", " + cmd; if (path !== undefined) msg += " '" + path + "'"; caml_raise_sys_error(msg); } } function caml_is_ml_bytes(s) { return s instanceof MlBytes; } function caml_is_ml_string(s) { return typeof s === "string" && !/[^\x00-\xff]/.test(s); } function caml_bytes_of_array(a) { if (!(a instanceof Uint8Array)) a = new Uint8Array(a); return new MlBytes(4, a, a.length); } function caml_bytes_of_jsbytes(s) { return new MlBytes(0, s, s.length); } function caml_jsbytes_of_string(x) { return x; } function caml_bytes_of_string(s) { return caml_bytes_of_jsbytes(caml_jsbytes_of_string(s)); } function caml_raise_no_such_file(name, raise_unix) { caml_raise_system_error(raise_unix, "ENOENT", "no such file or directory", name); } function caml_convert_bytes_to_array(s) { var a = new Uint8Array(s.l), b = s.c, l = b.length, i = 0; for (; i < l; i++) a[i] = b.charCodeAt(i); for (l = s.l; i < l; i++) a[i] = 0; s.c = a; s.t = 4; return a; } function caml_uint8_array_of_bytes(s) { if (s.t !== 4) caml_convert_bytes_to_array(s); return s.c; } function caml_bytes_of_uint8_array(a) { return new MlBytes(4, a, a.length); } function caml_raise_with_string(tag, msg) { caml_raise_with_arg(tag, caml_string_of_jsbytes(msg)); } function caml_invalid_argument(msg) { caml_raise_with_string(caml_global_data.Invalid_argument, msg); } function caml_create_bytes(len) { if (len < 0) caml_invalid_argument("Bytes.create"); return new MlBytes(len ? 2 : 9, "", len); } function caml_ml_bytes_length(s) { return s.l; } function caml_blit_bytes(s1, i1, s2, i2, len) { if (len === 0) return 0; if (i2 === 0 && (len >= s2.l || (s2.t === 2 && len >= s2.c.length))) { s2.c = s1.t === 4 ? caml_sub_uint8_array_to_jsbytes(s1.c, i1, len) : i1 === 0 && s1.c.length === len ? s1.c : s1.c.slice(i1, i1 + len); s2.t = s2.c.length === s2.l ? 0 : 2; } else if (s2.t === 2 && i2 === s2.c.length) { s2.c += s1.t === 4 ? caml_sub_uint8_array_to_jsbytes(s1.c, i1, len) : i1 === 0 && s1.c.length === len ? s1.c : s1.c.slice(i1, i1 + len); s2.t = s2.c.length === s2.l ? 0 : 2; } else { if (s2.t !== 4) caml_convert_bytes_to_array(s2); var c1 = s1.c, c2 = s2.c; if (s1.t === 4) if (i2 <= i1) for (var i = 0; i < len; i++) c2[i2 + i] = c1[i1 + i]; else for (var i = len - 1; i >= 0; i--) c2[i2 + i] = c1[i1 + i]; else { var l = Math.min(len, c1.length - i1); for (var i = 0; i < l; i++) c2[i2 + i] = c1.charCodeAt(i1 + i); for (; i < len; i++) c2[i2 + i] = 0; } } return 0; } function MlFile() { } function MlFakeFile(content) { this.data = content; } MlFakeFile.prototype = new MlFile(); MlFakeFile.prototype.constructor = MlFakeFile; MlFakeFile.prototype.truncate = function (len) { var old = this.data; this.data = caml_create_bytes(len | 0); caml_blit_bytes(old, 0, this.data, 0, len); }; MlFakeFile.prototype.length = function () { return caml_ml_bytes_length(this.data); }; MlFakeFile.prototype.write = function (offset, buf, pos, len) { var clen = this.length(); if (offset + len >= clen) { var new_str = caml_create_bytes(offset + len), old_data = this.data; this.data = new_str; caml_blit_bytes(old_data, 0, this.data, 0, clen); } caml_blit_bytes(caml_bytes_of_uint8_array(buf), pos, this.data, offset, len); return len; }; MlFakeFile.prototype.read = function (offset, buf, pos, len) { var clen = this.length(); if (offset + len >= clen) len = clen - offset; if (len) { var data = caml_create_bytes(len | 0); caml_blit_bytes(this.data, offset, data, 0, len); buf.set(caml_uint8_array_of_bytes(data), pos); } return len; }; function MlFakeFd(name, file, flags) { this.file = file; this.name = name; this.flags = flags; this.offset = 0; this.seeked = false; } MlFakeFd.prototype.err_closed = function (cmd, raise_unix) { caml_raise_system_error(raise_unix, "EBADF", cmd, "bad file descriptor"); }; MlFakeFd.prototype.length = function () { if (this.file) return this.file.length(); this.err_closed("length"); }; MlFakeFd.prototype.truncate = function (len, raise_unix) { if (this.file) { if (!(this.flags.wronly || this.flags.rdwr)) caml_raise_system_error(raise_unix, "EINVAL", "truncate", "invalid argument"); return this.file.truncate(len); } this.err_closed("truncate", raise_unix); }; MlFakeFd.prototype.write = function (buf, pos, len, raise_unix) { if (this.file && (this.flags.wronly || this.flags.rdwr)) { var offset = this.offset; this.offset += len; return this.file.write(offset, buf, pos, len); } this.err_closed("write", raise_unix); }; MlFakeFd.prototype.read = function (buf, pos, len, raise_unix) { if (this.file && !this.flags.wronly) { var offset = this.offset; this.offset += len; return this.file.read(offset, buf, pos, len); } this.err_closed("read", raise_unix); }; MlFakeFd.prototype.seek = function (offset, whence, raise_unix) { switch (whence) { case 0: break; case 1: offset += this.offset; break; case 2: offset += this.length(); break; } if (offset < 0) caml_raise_system_error(raise_unix, "EINVAL", "lseek", "invalid argument"); this.offset = offset; this.seeked = true; }; MlFakeFd.prototype.close = function () { if (!this.file) this.err_closed("close"); this.file = undefined; }; MlFakeFd.prototype.check_stream_semantics = function (cmd) { if (!this.file) return this.err_closed(cmd, 1); }; function MlFakeDevice(root, f) { this.content = {}; this.root = root; this.lookupFun = f; } MlFakeDevice.prototype.nm = function (name) { return this.root + name; }; MlFakeDevice.prototype.create_dir_if_needed = function (name) { var comp = name.split("/"), res = ""; for (var i = 0; i < comp.length - 1; i++) { res += comp[i] + "/"; if (this.content[res]) continue; this.content[res] = Symbol("directory"); } }; MlFakeDevice.prototype.slash = function (name) { return /\/$/.test(name) ? name : name + "/"; }; MlFakeDevice.prototype.lookup = function (name) { if (!this.content[name] && this.lookupFun) { var res = this.lookupFun(caml_string_of_jsstring(this.root), caml_string_of_jsstring(name)); if (res !== 0) { this.create_dir_if_needed(name); this.content[name] = new MlFakeFile(caml_bytes_of_string(res[1])); } } }; MlFakeDevice.prototype.exists = function (name, do_not_lookup) { if (name === "") return 1; var name_slash = this.slash(name); if (this.content[name_slash]) return 1; if (!do_not_lookup) this.lookup(name); return this.content[name] ? 1 : 0; }; MlFakeDevice.prototype.isFile = function (name) { return this.exists(name) && !this.is_dir(name) ? 1 : 0; }; MlFakeDevice.prototype.mkdir = function (name, mode, raise_unix) { if (this.exists(name)) caml_raise_system_error(raise_unix, "EEXIST", "mkdir", "file already exists", this.nm(name)); var parent = /^(.*)\/[^/]+/.exec(name); parent = (parent && parent[1]) || ""; if (!this.exists(parent)) caml_raise_system_error(raise_unix, "ENOENT", "mkdir", "no such file or directory", this.nm(name)); if (!this.is_dir(parent)) caml_raise_system_error(raise_unix, "ENOTDIR", "mkdir", "not a directory", this.nm(name)); this.create_dir_if_needed(this.slash(name)); }; MlFakeDevice.prototype.rmdir = function (name, raise_unix) { var name_slash = name === "" ? "" : this.slash(name), r = new RegExp("^" + name_slash + "([^/]+)"); if (!this.exists(name)) caml_raise_system_error(raise_unix, "ENOENT", "rmdir", "no such file or directory", this.nm(name)); if (!this.is_dir(name)) caml_raise_system_error(raise_unix, "ENOTDIR", "rmdir", "not a directory", this.nm(name)); for (var n in this.content) if (n.match(r)) caml_raise_system_error(raise_unix, "ENOTEMPTY", "rmdir", "directory not empty", this.nm(name)); delete this.content[name_slash]; }; MlFakeDevice.prototype.readdir = function (name) { var name_slash = name === "" ? "" : this.slash(name); if (!this.exists(name)) caml_raise_sys_error(name + ": No such file or directory"); if (!this.is_dir(name)) caml_raise_sys_error(name + ": Not a directory"); var r = new RegExp("^" + name_slash + "([^/]+)"), seen = {}, a = []; for (var n in this.content) { var m = n.match(r); if (m && !seen[m[1]]) { seen[m[1]] = true; a.push(m[1]); } } return a; }; MlFakeDevice.prototype.opendir = function (name, raise_unix) { var a = this.readdir(name), c = false, i = 0; return { readSync: function () { if (c) caml_raise_system_error(raise_unix, "EBADF", "readdir", "bad file descriptor"); if (i === a.length) return null; var entry = a[i]; i++; return { name: entry }; }, closeSync: function () { if (c) caml_raise_system_error(raise_unix, "EBADF", "readdir", "bad file descriptor"); c = true; a = []; }, }; }; MlFakeDevice.prototype.is_dir = function (name) { if (name === "") return true; var name_slash = this.slash(name); return this.content[name_slash] ? 1 : 0; }; MlFakeDevice.prototype.unlink = function (name, raise_unix) { if (!this.exists(name, true)) caml_raise_system_error(raise_unix, "ENOENT", "unlink", "no such file or directory", name); delete this.content[name]; return 0; }; MlFakeDevice.prototype.open = function (name, f, _perms, raise_unix) { var file; this.lookup(name); if (this.content[name]) { if (this.is_dir(name)) caml_raise_system_error(raise_unix, "EISDIR", "open", "illegal operation on a directory", this.nm(name)); if (f.create && f.excl) caml_raise_system_error(raise_unix, "EEXIST", "open", "file already exists", this.nm(name)); file = this.content[name]; if (f.truncate) file.truncate(); } else if (f.create) { this.create_dir_if_needed(name); this.content[name] = new MlFakeFile(caml_create_bytes(0)); file = this.content[name]; } else caml_raise_no_such_file(this.nm(name), raise_unix); return new MlFakeFd(this.nm(name), file, f); }; MlFakeDevice.prototype.truncate = function (name, len, raise_unix) { var file; this.lookup(name); if (this.content[name]) { if (this.is_dir(name)) caml_raise_system_error(raise_unix, "EISDIR", "open", "illegal operation on a directory", this.nm(name)); file = this.content[name]; file.truncate(len); } else caml_raise_no_such_file(this.nm(name), raise_unix); }; MlFakeDevice.prototype.register = function (name, content) { var file; if (this.content[name]) 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)); else if (Array.isArray(content)) file = new MlFakeFile(caml_bytes_of_array(content)); else if (typeof content === "string") file = new MlFakeFile(caml_bytes_of_jsbytes(content)); else if (content.toString) { var bytes = caml_bytes_of_string(caml_string_of_jsstring(content.toString())); file = new MlFakeFile(bytes); } if (file) { this.create_dir_if_needed(name); this.content[name] = file; } else caml_raise_sys_error(this.nm(name) + " : registering file with invalid content type"); }; MlFakeDevice.prototype.constructor = MlFakeDevice; function MlFakeFd_out(fd, flags) { MlFakeFile.call(this, caml_create_bytes(0)); this.log = function (s) { return 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.truncate = function (len, raise_unix) { caml_raise_system_error(raise_unix, "EINVAL", "ftruncate", "invalid argument"); }; MlFakeFd_out.prototype.write = function (buf, pos, len, raise_unix) { var written = len; if (this.log) { if (len > 0 && pos >= 0 && pos + len <= buf.length && buf[pos + len - 1] === 10) len--; var src = caml_create_bytes(len); caml_blit_bytes(caml_bytes_of_uint8_array(buf), pos, src, 0, len); this.log(src.toUtf16()); return written; } caml_raise_system_error(raise_unix, "EBADF", "write", "bad file descriptor"); }; MlFakeFd_out.prototype.read = function (buf, pos, len, raise_unix) { caml_raise_system_error(raise_unix, "EBADF", "read", "bad file descriptor"); }; MlFakeFd_out.prototype.seek = function (len, whence, raise_unix) { caml_raise_system_error(raise_unix, "ESPIPE", "lseek", "illegal seek"); }; MlFakeFd_out.prototype.close = function () { this.log = undefined; }; MlFakeFd_out.prototype.check_stream_semantics = function (cmd) { }; var caml_int64_offset = Math.pow(2, -24); function caml_raise_constant(tag) { throw tag; } function caml_raise_zero_divide() { caml_raise_constant(caml_global_data.Division_by_zero); } function MlInt64(lo, mi, hi) { this.lo = lo & 0xffffff; this.mi = mi & 0xffffff; this.hi = hi & 0xffff; } MlInt64.prototype.caml_custom = "_j"; MlInt64.prototype.copy = function () { return new MlInt64(this.lo, this.mi, this.hi); }; MlInt64.prototype.ucompare = function (x) { if (this.hi > x.hi) return 1; if (this.hi < x.hi) return -1; if (this.mi > x.mi) return 1; if (this.mi < x.mi) return -1; if (this.lo > x.lo) return 1; if (this.lo < x.lo) return -1; return 0; }; MlInt64.prototype.compare = function (x) { var hi = this.hi << 16, xhi = x.hi << 16; if (hi > xhi) return 1; if (hi < xhi) return -1; if (this.mi > x.mi) return 1; if (this.mi < x.mi) return -1; if (this.lo > x.lo) return 1; if (this.lo < x.lo) return -1; return 0; }; MlInt64.prototype.neg = function () { var lo = -this.lo, mi = -this.mi + (lo >> 24), hi = -this.hi + (mi >> 24); return new MlInt64(lo, mi, hi); }; MlInt64.prototype.add = function (x) { var lo = this.lo + x.lo, mi = this.mi + x.mi + (lo >> 24), hi = this.hi + x.hi + (mi >> 24); return new MlInt64(lo, mi, hi); }; MlInt64.prototype.sub = function (x) { var lo = this.lo - x.lo, mi = this.mi - x.mi + (lo >> 24), hi = this.hi - x.hi + (mi >> 24); return new MlInt64(lo, mi, hi); }; MlInt64.prototype.mul = function (x) { var lo = this.lo * x.lo, mi = ((lo * caml_int64_offset) | 0) + this.mi * x.lo + this.lo * x.mi, hi = ((mi * caml_int64_offset) | 0) + this.hi * x.lo + this.mi * x.mi + this.lo * x.hi; return new MlInt64(lo, mi, hi); }; MlInt64.prototype.isZero = function () { return (this.lo | this.mi | this.hi) === 0; }; MlInt64.prototype.isNeg = function () { return this.hi << 16 < 0; }; MlInt64.prototype.and = function (x) { return new MlInt64(this.lo & x.lo, this.mi & x.mi, this.hi & x.hi); }; MlInt64.prototype.or = function (x) { return new MlInt64(this.lo | x.lo, this.mi | x.mi, this.hi | x.hi); }; 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; if (s === 0) return this; if (s < 24) return new MlInt64(this.lo << s, (this.mi << s) | (this.lo >> (24 - s)), (this.hi << s) | (this.mi >> (24 - s))); if (s < 48) return new MlInt64(0, this.lo << (s - 24), (this.mi << (s - 24)) | (this.lo >> (48 - s))); return new MlInt64(0, 0, this.lo << (s - 48)); }; MlInt64.prototype.shift_right_unsigned = function (s) { s = s & 63; if (s === 0) return this; if (s < 24) return new MlInt64((this.lo >> s) | (this.mi << (24 - s)), (this.mi >> s) | (this.hi << (24 - s)), this.hi >> s); if (s < 48) return new MlInt64((this.mi >> (s - 24)) | (this.hi << (48 - s)), this.hi >> (s - 24), 0); return new MlInt64(this.hi >> (s - 48), 0, 0); }; MlInt64.prototype.shift_right = function (s) { s = s & 63; if (s === 0) return this; var h = (this.hi << 16) >> 16; if (s < 24) return new MlInt64((this.lo >> s) | (this.mi << (24 - s)), (this.mi >> s) | (h << (24 - s)), ((this.hi << 16) >> s) >>> 16); var sign = (this.hi << 16) >> 31; if (s < 48) return new MlInt64((this.mi >> (s - 24)) | (this.hi << (48 - s)), ((this.hi << 16) >> (s - 24)) >> 16, sign & 0xffff); return new MlInt64((this.hi << 16) >> (s - 32), sign, sign); }; MlInt64.prototype.lsl1 = function () { this.hi = (this.hi << 1) | (this.mi >> 23); this.mi = ((this.mi << 1) | (this.lo >> 23)) & 0xffffff; this.lo = (this.lo << 1) & 0xffffff; }; 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; }; MlInt64.prototype.udivmod = function (x) { var offset = 0, modulus = this.copy(), divisor = x.copy(), quotient = new MlInt64(0, 0, 0); while (modulus.ucompare(divisor) > 0) { offset++; divisor.lsl1(); } while (offset >= 0) { offset--; quotient.lsl1(); if (modulus.ucompare(divisor) >= 0) { quotient.lo++; modulus = modulus.sub(divisor); } divisor.lsr1(); } return { quotient: quotient, modulus: modulus }; }; MlInt64.prototype.div = function (y) { var x = this; if (y.isZero()) caml_raise_zero_divide(); var sign = x.hi ^ y.hi; if (x.hi & 0x8000) x = x.neg(); if (y.hi & 0x8000) y = y.neg(); var q = x.udivmod(y).quotient; if (sign & 0x8000) q = q.neg(); return q; }; MlInt64.prototype.mod = function (y) { var x = this; if (y.isZero()) caml_raise_zero_divide(); var sign = x.hi; if (x.hi & 0x8000) x = x.neg(); if (y.hi & 0x8000) y = y.neg(); var r = x.udivmod(y).modulus; if (sign & 0x8000) r = r.neg(); return r; }; MlInt64.prototype.toInt = function () { return this.lo | (this.mi << 24); }; MlInt64.prototype.toFloat = function () { return (this.hi << 16) * Math.pow(2, 32) + this.mi * Math.pow(2, 24) + this.lo; }; MlInt64.prototype.toArray = function () { return [ this.hi >> 8, this.hi & 0xff, this.mi >> 16, (this.mi >> 8) & 0xff, this.mi & 0xff, this.lo >> 16, (this.lo >> 8) & 0xff, this.lo & 0xff, ]; }; MlInt64.prototype.lo32 = function () { return this.lo | ((this.mi & 0xff) << 24); }; MlInt64.prototype.hi32 = function () { return ((this.mi >>> 8) & 0xffff) | (this.hi << 16); }; function MlMutex() { this.locked = false; } function MlNat(x) { this.data = new Int32Array(x); this.length = this.data.length + 2; } MlNat.prototype.caml_custom = "_nat"; function caml_raise_nodejs_error(err, raise_unix, cmd) { var unix_error = caml_named_value("Unix.Unix_error"); if (raise_unix && unix_error) { var args = make_unix_err_args(err.code, cmd || err.syscall, err.path, err.errno); caml_raise_with_args(unix_error, args); } else caml_raise_sys_error(err.toString()); } function caml_int64_of_float(x) { if (x < 0) x = Math.ceil(x); return new MlInt64(x & 0xffffff, Math.floor(x * caml_int64_offset) & 0xffffff, Math.floor(x * caml_int64_offset * caml_int64_offset) & 0xffff); } function fs_node_stats_from_js(js_stats, large) { var file_kind; if (js_stats.isFile()) file_kind = 0; else if (js_stats.isDirectory()) file_kind = 1; else if (js_stats.isCharacterDevice()) file_kind = 2; else if (js_stats.isBlockDevice()) file_kind = 3; else if (js_stats.isSymbolicLink()) file_kind = 4; else if (js_stats.isFIFO()) file_kind = 5; else if (js_stats.isSocket()) file_kind = 6; return [ 0, js_stats.dev, js_stats.ino | 0, file_kind, js_stats.mode, js_stats.nlink, js_stats.uid, js_stats.gid, js_stats.rdev, large ? caml_int64_of_float(js_stats.size) : js_stats.size | 0, js_stats.atimeMs / 1000, js_stats.mtimeMs / 1000, js_stats.ctimeMs / 1000, ]; } function caml_ml_string_length(s) { return s.length; } function caml_string_unsafe_get(s, i) { return s.charCodeAt(i); } function caml_uint8_array_of_string(s) { var l = caml_ml_string_length(s), a = new Uint8Array(l), i = 0; for (; i < l; i++) a[i] = caml_string_unsafe_get(s, i); return a; } function caml_bytes_bound_error() { caml_invalid_argument("index out of bounds"); } function caml_bytes_unsafe_set(s, i, c) { c &= 0xff; if (s.t !== 4) { if (i === s.c.length) { s.c += String.fromCharCode(c); if (i + 1 === s.l) s.t = 0; return 0; } caml_convert_bytes_to_array(s); } s.c[i] = c; return 0; } function caml_bytes_set(s, i, c) { if (i >>> 0 >= s.l) caml_bytes_bound_error(); return caml_bytes_unsafe_set(s, i, c); } function MlNodeFd(fd, flags) { // this.fs = require("node:fs") this.fd = fd; this.flags = flags; var stats = this.fs.fstatSync(fd); flags.noSeek = stats.isCharacterDevice() || stats.isFIFO() || stats.isSocket(); this.offset = this.flags.append ? stats.size : 0; this.seeked = false; } MlNodeFd.prototype = new MlFile(); MlNodeFd.prototype.constructor = MlNodeFd; MlNodeFd.prototype.truncate = function (len, raise_unix) { try { this.fs.ftruncateSync(this.fd, len | 0); if (this.offset > len) this.offset = len; } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeFd.prototype.length = function () { try { return this.fs.fstatSync(this.fd).size; } catch (err) { caml_raise_sys_error(err.toString()); } }; MlNodeFd.prototype.write = function (buf, buf_offset, len, raise_unix) { try { if (this.flags.noSeek || !this.seeked) var written = this.fs.writeSync(this.fd, buf, buf_offset, len); else var written = this.fs.writeSync(this.fd, buf, buf_offset, len, this.offset); this.offset += written; } catch (err) { caml_raise_nodejs_error(err, raise_unix); } return written; }; MlNodeFd.prototype.read = function (a, buf_offset, len, raise_unix) { try { if (this.flags.noSeek || !this.seeked) var read = this.fs.readSync(this.fd, a, buf_offset, len); else var read = this.fs.readSync(this.fd, a, buf_offset, len, this.offset); this.offset += read; return read; } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeFd.prototype.seek = function (offset, whence, raise_unix) { if (this.flags.noSeek) caml_raise_system_error(raise_unix, "ESPIPE", "lseek", "illegal seek"); switch (whence) { case 0: break; case 1: offset += this.offset; break; case 2: offset += this.length(); break; } if (offset < 0) caml_raise_system_error(raise_unix, "EINVAL", "lseek", "invalid argument"); this.offset = offset; this.seeked = true; return this.offset; }; MlNodeFd.prototype.stat = function (large) { try { var js_stats = this.fs.fstatSync(this.fd); return fs_node_stats_from_js(js_stats, large); } catch (err) { caml_raise_nodejs_error(err, 1); } }; MlNodeFd.prototype.close = function (raise_unix) { try { this.fs.closeSync(this.fd); return 0; } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeFd.prototype.check_stream_semantics = function (cmd) { try { var js_stats = this.fs.fstatSync(this.fd); } catch (err) { caml_raise_nodejs_error(err, 1, cmd); } if (!(js_stats.isFile() || js_stats.isCharacterDevice() || js_stats.isFIFO() || js_stats.isSocket())) caml_raise_system_error(1, "EINVAL", cmd, "invalid argument"); }; function MlNodeDevice(root) { // this.fs = require("node:fs") this.root = root; } MlNodeDevice.prototype.nm = function (name) { return this.root + name; }; MlNodeDevice.prototype.exists = function (name) { try { return this.fs.existsSync(this.nm(name)) ? 1 : 0; } catch (err) { return 0; } }; MlNodeDevice.prototype.isFile = function (name) { try { return this.fs.statSync(this.nm(name)).isFile() ? 1 : 0; } catch (err) { caml_raise_sys_error(err.toString()); } }; MlNodeDevice.prototype.mkdir = function (name, mode, raise_unix) { try { this.fs.mkdirSync(this.nm(name), { mode: mode }); return 0; } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeDevice.prototype.rmdir = function (name, raise_unix) { try { this.fs.rmdirSync(this.nm(name)); return 0; } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeDevice.prototype.readdir = function (name, raise_unix) { try { return this.fs.readdirSync(this.nm(name)); } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeDevice.prototype.is_dir = function (name) { try { return this.fs.statSync(this.nm(name)).isDirectory() ? 1 : 0; } catch (err) { caml_raise_sys_error(err.toString()); } }; MlNodeDevice.prototype.unlink = function (name, raise_unix) { try { this.fs.unlinkSync(this.nm(name)); return 0; } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeDevice.prototype.utimes = function (name, atime, mtime, raise_unix) { try { if (atime === 0 && mtime === 0) { atime = new Date().getTime() / 1000; mtime = atime; } this.fs.utimesSync(this.nm(name), atime, mtime); return 0; } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeDevice.prototype.truncate = function (name, len, raise_unix) { try { this.fs.truncateSync(this.nm(name), len | 0); return 0; } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeDevice.prototype.open = function (name, f, perms, raise_unix) { // var consts = require("node:constants"), // res = 0 for (var key in f) switch (key) { case "rdonly": res |= consts.O_RDONLY; break; case "wronly": res |= consts.O_WRONLY; break; case "rdwr": res |= consts.O_RDWR; break; case "append": res |= consts.O_APPEND; break; case "create": res |= consts.O_CREAT; break; case "truncate": res |= consts.O_TRUNC; break; case "excl": res |= consts.O_EXCL; break; case "binary": res |= consts.O_BINARY; break; case "text": res |= consts.O_TEXT; break; case "nonblock": res |= consts.O_NONBLOCK; break; case "noctty": res |= consts.O_NOCTTY; break; case "dsync": res |= consts.O_DSYNC; break; case "sync": res |= consts.O_SYNC; break; } try { var fd = this.fs.openSync(this.nm(name), res, perms); return new MlNodeFd(fd, f); } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; if (globalThis.process?.platform === "win32") MlNodeDevice.prototype.rename = function (o, n, raise_unix) { try { var target = this.nm(n), source = this.nm(o), target_stats, source_stats; if ((target_stats = this.fs.statSync(target, { throwIfNoEntry: false, })) && (source_stats = this.fs.statSync(source, { throwIfNoEntry: false, })) && source_stats.isDirectory()) if (target_stats.isDirectory()) { if (!target.startsWith(source)) try { this.fs.rmdirSync(target); } catch { } } else { var err = new Error(`ENOTDIR: not a directory, rename '${source}' -> '${target}'`); throw Object.assign(err, { errno: -20, code: "ENOTDIR", syscall: "rename", path: target, }); } this.fs.renameSync(this.nm(o), this.nm(n)); } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; else MlNodeDevice.prototype.rename = function (o, n, raise_unix) { try { this.fs.renameSync(this.nm(o), this.nm(n)); } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeDevice.prototype.stat = function (name, large, raise_unix) { try { var js_stats = this.fs.statSync(this.nm(name)); return fs_node_stats_from_js(js_stats, large); } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeDevice.prototype.lstat = function (name, large, raise_unix) { try { var js_stats = this.fs.lstatSync(this.nm(name)); return fs_node_stats_from_js(js_stats, large); } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeDevice.prototype.symlink = function (to_dir, target, path, raise_unix) { try { this.fs.symlinkSync(target, this.nm(path), to_dir === 0 ? null : to_dir[1] ? "dir" : "file"); return 0; } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeDevice.prototype.readlink = function (name, raise_unix) { try { var link = this.fs.readlinkSync(this.nm(name), "utf8"); return caml_string_of_jsstring(link); } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeDevice.prototype.opendir = function (name, raise_unix) { try { return this.fs.opendirSync(this.nm(name)); } catch (err) { caml_raise_nodejs_error(err, raise_unix); } }; MlNodeDevice.prototype.constructor = MlNodeDevice; var MlObjectTable; if (typeof globalThis.Map === "undefined") MlObjectTable = (function () { function NaiveLookup(objs) { this.objs = objs; } NaiveLookup.prototype.get = function (v) { for (var i = 0; i < this.objs.length; i++) if (this.objs[i] === v) return i; }; NaiveLookup.prototype.set = function () { }; return function () { this.objs = []; this.lookup = new NaiveLookup(this.objs); }; })(); else MlObjectTable = function () { this.objs = []; this.lookup = new globalThis.Map(); }; MlObjectTable.prototype.store = function (v) { this.lookup.set(v, this.objs.length); this.objs.push(