@tybys/wasm-util 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- function t(t,e){if(null===t||"object"!=typeof t)throw new TypeError(`${e} must be an object. Received ${null===t?"null":typeof t}`)}function e(t,e){if("string"!=typeof t)throw new TypeError(`${e} must be a string. Received ${null===t?"null":typeof t}`)}function n(t,e){if("function"!=typeof t)throw new TypeError(`${e} must be a function. Received ${null===t?"null":typeof t}`)}function r(t,e){if(void 0!==t)throw new TypeError(`${e} must be undefined. Received ${null===t?"null":typeof t}`)}function i(t){return!(!t||"object"!=typeof t&&"function"!=typeof t||"function"!=typeof t.then)}const s=["asyncify_get_state","asyncify_start_rewind","asyncify_start_unwind","asyncify_stop_rewind","asyncify_stop_unwind"];function o(t,e,n,r){if("function"!=typeof t.exports[r]||n<=0)return{wasm64:e,dataPtr:16,start:e?32:24,end:1024};const i=t.exports[r],s=e?Number(i(BigInt(16)+BigInt(n))):i(8+n);if(0===s)throw new Error("Allocate asyncify data failed");return e?{wasm64:e,dataPtr:s,start:s+16,end:s+16+n}:{wasm64:e,dataPtr:s,start:s+8,end:s+8+n}}class a{constructor(){this.value=void 0,this.exports=void 0,this.dataPtr=0}init(t,e,n){var r,i;if(this.exports)throw new Error("Asyncify has been initialized");if(!(t instanceof WebAssembly.Memory))throw new TypeError("Require WebAssembly.Memory object");const a=e.exports;for(let t=0;t<s.length;++t)if("function"!=typeof a[s[t]])throw new TypeError("Invalid asyncify wasm");let c;const u=Boolean(n.wasm64);c=n.tryAllocate?!0===n.tryAllocate?o(e,u,4096,"malloc"):o(e,u,null!==(r=n.tryAllocate.size)&&void 0!==r?r:4096,null!==(i=n.tryAllocate.name)&&void 0!==i?i:"malloc"):{wasm64:u,dataPtr:16,start:u?32:24,end:1024},this.dataPtr=c.dataPtr,u?new BigInt64Array(t.buffer,this.dataPtr).set([BigInt(c.start),BigInt(c.end)]):new Int32Array(t.buffer,this.dataPtr).set([c.start,c.end]),this.exports=this.wrapExports(a,n.wrapExports);const f=Object.create(WebAssembly.Instance.prototype);return Object.defineProperty(f,"exports",{value:this.exports}),f}assertState(){if(0!==this.exports.asyncify_get_state())throw new Error("Asyncify state error")}wrapImportFunction(t){return(...e)=>{for(;2===this.exports.asyncify_get_state();)return this.exports.asyncify_stop_rewind(),this.value;this.assertState();const n=t(...e);if(!i(n))return n;this.exports.asyncify_start_unwind(this.dataPtr),this.value=n}}wrapImports(t){const e={};return Object.keys(t).forEach((n=>{const r=t[n],i={};Object.keys(r).forEach((t=>{const e=r[t];i[t]="function"==typeof e?this.wrapImportFunction(e):e})),e[n]=i})),e}wrapExportFunction(t){return async(...e)=>{this.assertState();let n=t(...e);for(;1===this.exports.asyncify_get_state();)this.exports.asyncify_stop_unwind(),this.value=await this.value,this.assertState(),this.exports.asyncify_start_rewind(this.dataPtr),n=t();return this.assertState(),n}}wrapExports(t,e){const n=Object.create(null);return Object.keys(t).forEach((r=>{const i=t[r];let o=-1!==s.indexOf(r)||"function"!=typeof i;Array.isArray(e)&&(o=o||-1===e.indexOf(r)),Object.defineProperty(n,r,{enumerable:!0,value:o?i:this.wrapExportFunction(i)})})),n}}async function c(t,e){const n=await fetch(t),r=await n.arrayBuffer();return await WebAssembly.instantiate(r,e)}async function u(t,e,n){var r,i;if(e&&"object"!=typeof e)throw new TypeError("imports must be an object or undefined");let s,o;if(e=null!=e?e:{},n&&(s=new a,e=s.wrapImports(e)),t instanceof ArrayBuffer||ArrayBuffer.isView(t)){if(o=await WebAssembly.instantiate(t,e),n){const t=o.instance.exports.memory||(null===(r=e.env)||void 0===r?void 0:r.memory);return{module:o.module,instance:s.init(t,o.instance,n)}}return o}if("string"!=typeof t&&!(t instanceof URL))throw new TypeError("Invalid source");if("function"==typeof WebAssembly.instantiateStreaming)try{o=await WebAssembly.instantiateStreaming(fetch(t),e)}catch(n){o=await c(t,e)}else o=await c(t,e);if(n){const t=o.instance.exports.memory||(null===(i=e.env)||void 0===i?void 0:i.memory);return{module:o.module,instance:s.init(t,o.instance,n)}}return o}function f(t,e,n){var r;if(t instanceof ArrayBuffer&&!ArrayBuffer.isView(t))throw new TypeError("Invalid source");if(e&&"object"!=typeof e)throw new TypeError("imports must be an object or undefined");let i;e=null!=e?e:{},n&&(i=new a,e=i.wrapImports(e));const s=new WebAssembly.Module(t),o=new WebAssembly.Instance(s,e),c={instance:o,module:s};if(n){const t=c.instance.exports.memory||(null===(r=e.env)||void 0===r?void 0:r.memory);return{module:c.module,instance:i.init(t,o,n)}}return c}function h(t){return 47===t}function l(...t){let n="",r=!1;for(let i=t.length-1;i>=-1&&!r;i--){const s=i>=0?t[i]:"/";e(s,"path"),0!==s.length&&(n=`${s}/${n}`,r=47===s.charCodeAt(0))}return n=function(t,e,n,r){let i="",s=0,o=-1,a=0,c=0;for(let u=0;u<=t.length;++u){if(u<t.length)c=t.charCodeAt(u);else{if(r(c))break;c=47}if(r(c)){if(o===u-1||1===a);else if(2===a){if(i.length<2||2!==s||46!==i.charCodeAt(i.length-1)||46!==i.charCodeAt(i.length-2)){if(i.length>2){const t=i.indexOf(n);-1===t?(i="",s=0):(i=i.slice(0,t),s=i.length-1-i.indexOf(n)),o=u,a=0;continue}if(0!==i.length){i="",s=0,o=u,a=0;continue}}e&&(i+=i.length>0?`${n}..`:"..",s=2)}else i.length>0?i+=`${n}${t.slice(o+1,u)}`:i=t.slice(o+1,u),s=u-o-1;o=u,a=0}else 46===c&&-1!==a?++a:a=-1}return i}(n,!r,"/",h),r?`/${n}`:n.length>0?n:"."}const _={FD_DATASYNC:BigInt(1)<<BigInt(0),FD_READ:BigInt(1)<<BigInt(1),FD_SEEK:BigInt(1)<<BigInt(2),FD_FDSTAT_SET_FLAGS:BigInt(1)<<BigInt(3),FD_SYNC:BigInt(1)<<BigInt(4),FD_TELL:BigInt(1)<<BigInt(5),FD_WRITE:BigInt(1)<<BigInt(6),FD_ADVISE:BigInt(1)<<BigInt(7),FD_ALLOCATE:BigInt(1)<<BigInt(8),PATH_CREATE_DIRECTORY:BigInt(1)<<BigInt(9),PATH_CREATE_FILE:BigInt(1)<<BigInt(10),PATH_LINK_SOURCE:BigInt(1)<<BigInt(11),PATH_LINK_TARGET:BigInt(1)<<BigInt(12),PATH_OPEN:BigInt(1)<<BigInt(13),FD_READDIR:BigInt(1)<<BigInt(14),PATH_READLINK:BigInt(1)<<BigInt(15),PATH_RENAME_SOURCE:BigInt(1)<<BigInt(16),PATH_RENAME_TARGET:BigInt(1)<<BigInt(17),PATH_FILESTAT_GET:BigInt(1)<<BigInt(18),PATH_FILESTAT_SET_SIZE:BigInt(1)<<BigInt(19),PATH_FILESTAT_SET_TIMES:BigInt(1)<<BigInt(20),FD_FILESTAT_GET:BigInt(1)<<BigInt(21),FD_FILESTAT_SET_SIZE:BigInt(1)<<BigInt(22),FD_FILESTAT_SET_TIMES:BigInt(1)<<BigInt(23),PATH_SYMLINK:BigInt(1)<<BigInt(24),PATH_REMOVE_DIRECTORY:BigInt(1)<<BigInt(25),PATH_UNLINK_FILE:BigInt(1)<<BigInt(26),POLL_FD_READWRITE:BigInt(1)<<BigInt(27),SOCK_SHUTDOWN:BigInt(1)<<BigInt(28),SOCK_ACCEPT:BigInt(1)<<BigInt(29)};class d extends Error{constructor(t,e){super(t),this.errno=e}getErrorMessage(){return function(t){switch(t){case 0:return"Success";case 1:return"Argument list too long";case 2:return"Permission denied";case 3:return"Address in use";case 4:return"Address not available";case 5:return"Address family not supported by protocol";case 6:return"Resource temporarily unavailable";case 7:return"Operation already in progress";case 8:return"Bad file descriptor";case 9:return"Bad message";case 10:return"Resource busy";case 11:return"Operation canceled";case 12:return"No child process";case 13:return"Connection aborted";case 14:return"Connection refused";case 15:return"Connection reset by peer";case 16:return"Resource deadlock would occur";case 17:return"Destination address required";case 18:return"Domain error";case 19:return"Quota exceeded";case 20:return"File exists";case 21:return"Bad address";case 22:return"File too large";case 23:return"Host is unreachable";case 24:return"Identifier removed";case 25:return"Illegal byte sequence";case 26:return"Operation in progress";case 27:return"Interrupted system call";case 28:return"Invalid argument";case 29:return"I/O error";case 30:return"Socket is connected";case 31:return"Is a directory";case 32:return"Symbolic link loop";case 33:return"No file descriptors available";case 34:return"Too many links";case 35:return"Message too large";case 36:return"Multihop attempted";case 37:return"Filename too long";case 38:return"Network is down";case 39:return"Connection reset by network";case 40:return"Network unreachable";case 41:return"Too many files open in system";case 42:return"No buffer space available";case 43:return"No such device";case 44:return"No such file or directory";case 45:return"Exec format error";case 46:return"No locks available";case 47:return"Link has been severed";case 48:return"Out of memory";case 49:return"No message of the desired type";case 50:return"Protocol not available";case 51:return"No space left on device";case 52:return"Function not implemented";case 53:return"Socket not connected";case 54:return"Not a directory";case 55:return"Directory not empty";case 56:return"State not recoverable";case 57:return"Not a socket";case 58:return"Not supported";case 59:return"Not a tty";case 60:return"No such device or address";case 61:return"Value too large for data type";case 62:return"Previous owner died";case 63:return"Operation not permitted";case 64:return"Broken pipe";case 65:return"Protocol error";case 66:return"Protocol not supported";case 67:return"Protocol wrong type for socket";case 68:return"Result not representable";case 69:return"Read-only file system";case 70:return"Invalid seek";case 71:return"No such process";case 72:return"Stale file handle";case 73:return"Operation timed out";case 74:return"Text file busy";case 75:return"Cross-device link";case 76:return"Capabilities insufficient";default:return"Unknown error"}}(this.errno)}}Object.defineProperty(d.prototype,"name",{configurable:!0,writable:!0,value:"WasiError"});const g=_.FD_DATASYNC|_.FD_READ|_.FD_SEEK|_.FD_FDSTAT_SET_FLAGS|_.FD_SYNC|_.FD_TELL|_.FD_WRITE|_.FD_ADVISE|_.FD_ALLOCATE|_.PATH_CREATE_DIRECTORY|_.PATH_CREATE_FILE|_.PATH_LINK_SOURCE|_.PATH_LINK_TARGET|_.PATH_OPEN|_.FD_READDIR|_.PATH_READLINK|_.PATH_RENAME_SOURCE|_.PATH_RENAME_TARGET|_.PATH_FILESTAT_GET|_.PATH_FILESTAT_SET_SIZE|_.PATH_FILESTAT_SET_TIMES|_.FD_FILESTAT_GET|_.FD_FILESTAT_SET_TIMES|_.FD_FILESTAT_SET_SIZE|_.PATH_SYMLINK|_.PATH_UNLINK_FILE|_.PATH_REMOVE_DIRECTORY|_.POLL_FD_READWRITE|_.SOCK_SHUTDOWN,E=g,y=g,p=g,I=g,T=_.FD_DATASYNC|_.FD_READ|_.FD_SEEK|_.FD_FDSTAT_SET_FLAGS|_.FD_SYNC|_.FD_TELL|_.FD_WRITE|_.FD_ADVISE|_.FD_ALLOCATE|_.FD_FILESTAT_GET|_.FD_FILESTAT_SET_SIZE|_.FD_FILESTAT_SET_TIMES|_.POLL_FD_READWRITE,A=BigInt(0),m=_.FD_FDSTAT_SET_FLAGS|_.FD_SYNC|_.FD_ADVISE|_.PATH_CREATE_DIRECTORY|_.PATH_CREATE_FILE|_.PATH_LINK_SOURCE|_.PATH_LINK_TARGET|_.PATH_OPEN|_.FD_READDIR|_.PATH_READLINK|_.PATH_RENAME_SOURCE|_.PATH_RENAME_TARGET|_.PATH_FILESTAT_GET|_.PATH_FILESTAT_SET_SIZE|_.PATH_FILESTAT_SET_TIMES|_.FD_FILESTAT_GET|_.FD_FILESTAT_SET_TIMES|_.PATH_SYMLINK|_.PATH_UNLINK_FILE|_.PATH_REMOVE_DIRECTORY|_.POLL_FD_READWRITE,b=m|T,w=_.FD_READ|_.FD_FDSTAT_SET_FLAGS|_.FD_WRITE|_.FD_FILESTAT_GET|_.POLL_FD_READWRITE|_.SOCK_SHUTDOWN,S=g,B=_.FD_READ|_.FD_FDSTAT_SET_FLAGS|_.FD_WRITE|_.FD_FILESTAT_GET|_.POLL_FD_READWRITE,D=BigInt(0);function N(t,e,n,r){const i={base:BigInt(0),inheriting:BigInt(0)};if(0===r)throw new d("Unknown file type",28);switch(r){case 4:i.base=T,i.inheriting=A;break;case 3:i.base=m,i.inheriting=b;break;case 6:case 5:i.base=w,i.inheriting=S;break;case 2:-1!==t.indexOf(e)?(i.base=B,i.inheriting=D):(i.base=p,i.inheriting=I);break;case 1:i.base=E,i.inheriting=y;break;default:i.base=BigInt(0),i.inheriting=BigInt(0)}const s=3&n;return 0===s?i.base&=~_.FD_WRITE:1===s&&(i.base&=~_.FD_READ),i}function F(t,e){let n=0;if("number"==typeof e&&e>=0)n=e;else for(let e=0;e<t.length;e++){n+=t[e].length}let r=0;const i=new Uint8Array(n);for(let e=0;e<t.length;e++){const n=t[e];i.set(n,r),r+=n.length}return i}class P{constructor(t,e,n,r,i,s,o,a){this.id=t,this.fd=e,this.path=n,this.realPath=r,this.type=i,this.rightsBase=s,this.rightsInheriting=o,this.preopen=a,this.pos=BigInt(0),this.size=BigInt(0)}seek(t,e){if(0===e)this.pos=BigInt(t);else if(1===e)this.pos+=BigInt(t);else{if(2!==e)throw new d("Unknown whence",29);this.pos=BigInt(this.size)-BigInt(t)}return this.pos}}class v extends P{constructor(t,e,n,r,i,s,o,a,c){super(e,n,r,i,s,o,a,c),this._log=t,this._buf=null}write(t){const e=t;if(this._buf&&(t=F([this._buf,t]),this._buf=null),-1===t.indexOf(10))return this._buf=t,e.byteLength;let n,r=0,i=0;for(;-1!==(n=t.indexOf(10,r));){const e=(new TextDecoder).decode(t.subarray(i,n));this._log(e),r+=n-i+1,i=n+1}return r<t.length&&(this._buf=t.slice(r)),e.byteLength}}function R(t){return t.isBlockDevice()?1:t.isCharacterDevice()?2:t.isDirectory()?3:t.isSocket()?6:t.isFile()?4:t.isSymbolicLink()?7:0}function L(t,e,n){t.setBigUint64(e,n.dev,!0),t.setBigUint64(e+8,n.ino,!0),t.setBigUint64(e+16,BigInt(R(n)),!0),t.setBigUint64(e+24,n.nlink,!0),t.setBigUint64(e+32,n.size,!0),t.setBigUint64(e+40,n.atimeMs*BigInt(1e6),!0),t.setBigUint64(e+48,n.mtimeMs*BigInt(1e6),!0),t.setBigUint64(e+56,n.ctimeMs*BigInt(1e6),!0)}class H{constructor(t){this.used=0,this.size=t.size,this.fds=Array(t.size),this.stdio=[t.in,t.out,t.err],this.fs=t.fs,this.print=t.print,this.printErr=t.printErr,this.insertStdio(t.in,0,"<stdin>"),this.insertStdio(t.out,1,"<stdout>"),this.insertStdio(t.err,2,"<stderr>")}insertStdio(t,e,n){const{base:r,inheriting:i}=N(this.stdio,t,2,2),s=this.insert(t,n,n,2,r,i,0);if(s.id!==e)throw new d(`id: ${s.id} !== expected: ${e}`,8);return s}insert(t,e,n,r,i,s,o){var a,c;let u,f=-1;if(this.used>=this.size){const t=2*this.size;this.fds.length=t,f=this.size,this.size=t}else for(let t=0;t<this.size;++t)if(null==this.fds[t]){f=t;break}return u="<stdout>"===e?new v(null!==(a=this.print)&&void 0!==a?a:console.log,f,t,e,n,r,i,s,o):"<stderr>"===e?new v(null!==(c=this.printErr)&&void 0!==c?c:console.error,f,t,e,n,r,i,s,o):new P(f,t,e,n,r,i,s,o),this.fds[f]=u,this.used++,u}getFileTypeByFd(t){return R(this.fs.fstatSync(t))}insertPreopen(t,e,n){const r=this.getFileTypeByFd(t);if(3!==r)throw new d(`Preopen not dir: ["${e}", "${n}"]`,54);const i=N(this.stdio,t,0,r);return this.insert(t,e,n,r,i.base,i.inheriting,1)}get(t,e,n){if(t>=this.size)throw new d("Invalid fd",8);const r=this.fds[t];if(!r||r.id!==t)throw new d("Bad file descriptor",8);if((~r.rightsBase&e)!==BigInt(0)||(~r.rightsInheriting&n)!==BigInt(0))throw new d("Capabilities insufficient",76);return r}remove(t){if(t>=this.size)throw new d("Invalid fd",8);const e=this.fds[t];if(!e||e.id!==t)throw new d("Bad file descriptor",8);this.fds[t]=void 0,this.used--}renumber(t,e){if(t===e)return;if(t>=this.size||e>=this.size)throw new d("Invalid fd",8);const n=this.fds[t],r=this.fds[e];if(!n||!r||n.id!==t||r.id!==e)throw new d("Invalid fd",8);this.fs.closeSync(n.fd),this.fds[t]=this.fds[e],this.fds[t].id=t,this.fds[e]=void 0,this.used--}}class U extends WebAssembly.Memory{constructor(t){super(t)}get HEAP8(){return new Int8Array(super.buffer)}get HEAPU8(){return new Uint8Array(super.buffer)}get HEAP16(){return new Int16Array(super.buffer)}get HEAPU16(){return new Uint16Array(super.buffer)}get HEAP32(){return new Int32Array(super.buffer)}get HEAPU32(){return new Uint32Array(super.buffer)}get HEAP64(){return new BigInt64Array(super.buffer)}get HEAPU64(){return new BigUint64Array(super.buffer)}get HEAPF32(){return new Float32Array(super.buffer)}get HEAPF64(){return new Float64Array(super.buffer)}get view(){return new DataView(super.buffer)}}function O(t){return Object.getPrototypeOf(t)===WebAssembly.Memory.prototype&&Object.setPrototypeOf(t,U.prototype),t}function k(t,e){if(0===t.length||0===e.length)return 0;let n=0,r=e.length-n;for(let i=0;i<t.length;++i){const s=t[i];if(r<s.length)return s.set(e.subarray(n,n+r),0),n+=r,r=0,n;s.set(e.subarray(n,n+s.length),0),n+=s.length,r-=s.length}return n}const C=new WeakMap,x=new WeakMap,M=new WeakMap;function W(t){return C.get(t)}function z(t){const e=M.get(t);if(!e)throw new Error("filesystem is unavailable");return e}function K(t){if(t instanceof d)return t.errno;switch(t.code){case"ENOENT":return 44;case"EBADF":return 8;case"EINVAL":return 28;case"EPERM":return 63;case"EPROTO":return 65;case"EEXIST":return 20;case"ENOTDIR":return 54;case"EMFILE":return 33;case"EACCES":return 2;case"EISDIR":return 31;case"ENOTEMPTY":return 55;case"ENOSYS":return 52}throw t}function G(t,e){return function(t,e){return Object.defineProperty(e,"name",{value:t}),e}(t,(function(){let t;try{t=e.apply(this,arguments)}catch(t){return K(t)}return i(t)?t.then((t=>t),K):t}))}function Y(t,e,n,r){let i=l(e.realPath,n);if(1==(1&r))try{i=t.readlinkSync(i)}catch(t){if("EINVAL"!==t.code&&"ENOENT"!==t.code)throw t}return i}const j=new TextEncoder,$=new TextDecoder;class V{constructor(t,e,n,r,i,s,o){this._setMemory=function(t){if(!(t instanceof WebAssembly.Memory))throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');C.set(this,O(t))},this.args_get=G("args_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{HEAPU8:n,view:r}=W(this),i=x.get(this).args;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=j.encode(o+"\0");n.set(a,e),e+=a.length}return 0})),this.args_sizes_get=G("args_sizes_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{view:n}=W(this),r=x.get(this).args;return n.setUint32(t,r.length,!0),n.setUint32(e,j.encode(r.join("\0")+"\0").length,!0),0})),this.environ_get=G("environ_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{HEAPU8:n,view:r}=W(this),i=x.get(this).env;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=j.encode(o+"\0");n.set(a,e),e+=a.length}return 0})),this.environ_sizes_get=G("environ_sizes_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{view:n}=W(this),r=x.get(this);return n.setUint32(t,r.env.length,!0),n.setUint32(e,j.encode(r.env.join("\0")+"\0").length,!0),0})),this.clock_res_get=G("clock_res_get",(function(t,e){if(0===(e=Number(e)))return 28;const{view:n}=W(this);switch(t){case 0:return n.setBigUint64(e,BigInt(1e6),!0),0;case 1:case 2:case 3:return n.setBigUint64(e,BigInt(1e3),!0),0;default:return 28}})),this.clock_time_get=G("clock_time_get",(function(t,e,n){if(0===(n=Number(n)))return 28;const{view:r}=W(this);switch(t){case 0:return r.setBigUint64(n,BigInt(Date.now())*BigInt(1e6),!0),0;case 1:case 2:case 3:{const t=performance.now(),e=Math.trunc(t),i=Math.floor(1e3*(t-e)),s=BigInt(e)*BigInt(1e9)+BigInt(i)*BigInt(1e6);return r.setBigUint64(n,s,!0),0}default:return 28}})),this.fd_advise=G("fd_advise",(function(t,e,n,r){return 52})),this.fd_allocate=G("fd_allocate",(function(t,e,n){const r=x.get(this),i=z(this),s=r.fds.get(t,_.FD_ALLOCATE,BigInt(0));return i.fstatSync(s.fd,{bigint:!0}).size<e+n&&i.truncateSync(s.fd,Number(e+n)),0})),this.fd_close=G("fd_close",(function(t){const e=x.get(this),n=e.fds.get(t,BigInt(0),BigInt(0));return z(this).closeSync(n.fd),e.fds.remove(t),0})),this.fd_datasync=G("fd_datasync",(function(t){const e=x.get(this).fds.get(t,_.FD_DATASYNC,BigInt(0));return z(this).fdatasyncSync(e.fd),0})),this.fd_fdstat_get=G("fd_fdstat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=x.get(this).fds.get(t,BigInt(0),BigInt(0)),{view:r}=W(this);return r.setUint16(e,n.type,!0),r.setUint16(e+2,0,!0),r.setBigUint64(e+8,n.rightsBase,!0),r.setBigUint64(e+16,n.rightsInheriting,!0),0})),this.fd_fdstat_set_flags=G("fd_fdstat_set_flags",(function(t,e){return 52})),this.fd_fdstat_set_rights=G("fd_fdstat_set_rights",(function(t,e,n){const r=x.get(this).fds.get(t,BigInt(0),BigInt(0));return(e|r.rightsBase)>r.rightsBase||(n|r.rightsInheriting)>r.rightsInheriting?76:(r.rightsBase=e,r.rightsInheriting=n,0)})),this.fd_filestat_get=G("fd_filestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=x.get(this).fds.get(t,_.FD_FILESTAT_GET,BigInt(0)),r=z(this).fstatSync(n.fd,{bigint:!0}),{view:i}=W(this);return L(i,e,r),0})),this.fd_filestat_set_size=G("fd_filestat_set_size",(function(t,e){const n=x.get(this).fds.get(t,_.FD_FILESTAT_SET_SIZE,BigInt(0));return z(this).ftruncateSync(n.fd,Number(e)),0})),this.fd_filestat_set_times=G("fd_filestat_set_times",(function(t,e,n,r){const i=x.get(this).fds.get(t,_.FD_FILESTAT_SET_TIMES,BigInt(0));2==(2&r)&&(e=BigInt(1e6*Date.now())),8==(8&r)&&(n=BigInt(1e6*Date.now()));return z(this).futimesSync(i.fd,Number(e),Number(n)),0})),this.fd_pread=G("fd_pread",(function(t,e,n,r,i){if(e=Number(e),i=Number(i),0===e||0===i)return 28;const{HEAPU8:s,view:o}=W(this),a=x.get(this).fds.get(t,_.FD_READ|_.FD_SEEK,BigInt(0));let c=0;const u=Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,i=o.getInt32(r,!0),a=o.getUint32(r+4,!0);return c+=a,s.subarray(i,i+a)}));let f=0;const h=new Uint8Array(c);h._isBuffer=!0;const l=z(this).readSync(a.fd,h,0,h.length,Number(r));return f=h?k(u,h.subarray(0,l)):0,o.setUint32(i,f,!0),0})),this.fd_prestat_get=G("fd_prestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=x.get(this);let r;try{r=n.fds.get(t,BigInt(0),BigInt(0))}catch(t){if(t instanceof d)return t.errno;throw t}if(1!==r.preopen)return 28;const{view:i}=W(this);return i.setUint32(e,0,!0),i.setUint32(e+4,j.encode(r.path).length+1,!0),0})),this.fd_prestat_dir_name=G("fd_prestat_dir_name",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const r=x.get(this).fds.get(t,BigInt(0),BigInt(0));if(1!==r.preopen)return 8;const i=j.encode(r.path+"\0");if(i.length>n)return 42;const{HEAPU8:s}=W(this);return s.set(i,e),0})),this.fd_pwrite=G("fd_pwrite",(function(t,e,n,r,i){if(e=Number(e),i=Number(i),0===e||0===i)return 28;const{HEAPU8:s,view:o}=W(this),a=x.get(this).fds.get(t,_.FD_WRITE|_.FD_SEEK,BigInt(0)),c=F(Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,i=o.getInt32(r,!0),a=o.getUint32(r+4,!0);return s.subarray(i,i+a)}))),u=z(this).writeSync(a.fd,c,0,c.length,Number(r));return o.setUint32(i,u,!0),0})),this.fd_read=G("fd_read",(function(t,e,n,r){if(e=Number(e),r=Number(r),0===e||0===r)return 28;const{HEAPU8:i,view:s}=W(this),o=x.get(this).fds.get(t,_.FD_READ,BigInt(0));let a=0;const c=Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,o=s.getInt32(r,!0),c=s.getUint32(r+4,!0);return a+=c,i.subarray(o,o+c)}));let u,f=0;if(0===t)u=function(){const t=window.prompt();return null===t?new Uint8Array:(new TextEncoder).encode(t+"\n")}(),f=u?k(c,u):0;else{u=new Uint8Array(a),u._isBuffer=!0;const t=z(this).readSync(o.fd,u,0,u.length,Number(o.pos));f=u?k(c,u.subarray(0,t)):0,o.pos+=BigInt(f)}return s.setUint32(r,f,!0),0})),this.fd_seek=G("fd_seek",(function(t,e,n,r){if(0===(r=Number(r)))return 28;if(0===t||1===t||2===t)return 0;const i=x.get(this).fds.get(t,_.FD_SEEK,BigInt(0)).seek(e,n),{view:s}=W(this);return s.setBigUint64(r,i,!0),0})),this.fd_readdir=G("fd_readdir",(function(t,e,n,r,i){if(e=Number(e),n=Number(n),i=Number(i),0===e||0===i)return 0;const s=x.get(this).fds.get(t,_.FD_READDIR,BigInt(0)),o=z(this),a=o.readdirSync(s.realPath,{withFileTypes:!0}),{HEAPU8:c,view:u}=W(this);let f=0;for(let t=Number(r);t<a.length;t++){const r=j.encode(a[t].name),i=o.statSync(l(s.realPath,a[t].name),{bigint:!0}),u=new Uint8Array(24+r.byteLength),h=new DataView(u.buffer);let _;h.setBigUint64(0,BigInt(t+1),!0),h.setBigUint64(8,BigInt(i.ino?i.ino:0),!0),h.setUint32(16,r.byteLength,!0),_=a[t].isFile()?4:a[t].isDirectory()?3:a[t].isSymbolicLink()?7:a[t].isCharacterDevice()?2:a[t].isBlockDevice()?1:a[t].isSocket()?6:0,h.setUint8(20,_),u.set(r,24);const d=u.slice(0,Math.min(u.length,n-f));c.set(d,e+f),f+=d.byteLength}return u.setUint32(i,f,!0),0})),this.fd_renumber=G("fd_renumber",(function(t,e){return x.get(this).fds.renumber(e,t),0})),this.fd_sync=G("fd_sync",(function(t){const e=x.get(this).fds.get(t,_.FD_SYNC,BigInt(0));return z(this).fsyncSync(e.fd),0})),this.fd_tell=G("fd_tell",(function(t,e){const n=x.get(this).fds.get(t,_.FD_TELL,BigInt(0)),r=BigInt(n.pos),{view:i}=W(this);return i.setBigUint64(Number(e),r,!0),0})),this.fd_write=G("fd_write",(function(t,e,n,r){if(e=Number(e),r=Number(r),0===e||0===r)return 28;const{HEAPU8:i,view:s}=W(this),o=x.get(this).fds.get(t,_.FD_WRITE,BigInt(0)),a=F(Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,o=s.getInt32(r,!0),a=s.getUint32(r+4,!0);return i.subarray(o,o+a)})));let c;if(1===t||2===t)c=o.write(a);else{c=z(this).writeSync(o.fd,a,0,a.length,Number(o.pos)),o.pos+=BigInt(c)}return s.setUint32(r,c,!0),0})),this.path_create_directory=G("path_create_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=W(this),i=x.get(this).fds.get(t,_.PATH_CREATE_DIRECTORY,BigInt(0));let s=$.decode(r.subarray(e,e+n));s=l(i.realPath,s);return z(this).mkdirSync(s),0})),this.path_filestat_get=G("path_filestat_get",(function(t,e,n,r,i){if(n=Number(n),r=Number(r),i=Number(i),0===n||0===i)return 28;const{HEAPU8:s,view:o}=W(this),a=x.get(this).fds.get(t,_.PATH_FILESTAT_GET,BigInt(0));let c=$.decode(s.subarray(n,n+r));const u=z(this);let f;return c=l(a.realPath,c),f=1==(1&e)?u.statSync(c,{bigint:!0}):u.lstatSync(c,{bigint:!0}),L(o,i,f),0})),this.path_filestat_set_times=G("path_filestat_set_times",(function(t,e,n,r,i,s,o){if(n=Number(n),r=Number(r),0===n)return 28;if(-16&o)return 28;const{HEAPU8:a}=W(this),c=x.get(this).fds.get(t,_.PATH_FILESTAT_SET_TIMES,BigInt(0)),u=z(this),f=Y(u,c,$.decode(a.subarray(n,n+r)),e);return 2==(2&o)&&(i=BigInt(1e6*Date.now())),8==(8&o)&&(s=BigInt(1e6*Date.now())),u.utimesSync(f,Number(i),Number(s)),0})),this.path_link=G("path_link",(function(t,e,n,r,i,s,o){if(n=Number(n),r=Number(r),s=Number(s),o=Number(o),0===n||0===s)return 28;const a=x.get(this);let c,u;t===i?c=u=a.fds.get(t,_.PATH_LINK_SOURCE|_.PATH_LINK_TARGET,BigInt(0)):(c=a.fds.get(t,_.PATH_LINK_SOURCE,BigInt(0)),u=a.fds.get(i,_.PATH_LINK_TARGET,BigInt(0)));const{HEAPU8:f}=W(this),h=z(this),d=Y(h,c,$.decode(f.subarray(n,n+r)),e),g=l(u.realPath,$.decode(f.subarray(s,s+o)));return h.linkSync(d,g),0})),this.path_open=G("path_open",(function(t,e,n,r,i,s,o,a,c){if(n=Number(n),c=Number(c),0===n||0===c)return 28;r=Number(r),s=BigInt(s);const u=((s=BigInt(s))&(_.FD_READ|_.FD_READDIR))!==BigInt(0),f=(s&(_.FD_DATASYNC|_.FD_WRITE|_.FD_ALLOCATE|_.FD_FILESTAT_SET_SIZE))!==BigInt(0);let h=f?u?2:1:0,l=_.PATH_OPEN,d=s|o;0!=(1&i)&&(h|=64,l|=_.PATH_CREATE_FILE),0!=(2&i)&&(h|=65536),0!=(4&i)&&(h|=128),0!=(8&i)&&(h|=512,l|=_.PATH_FILESTAT_SET_SIZE),0!=(1&a)&&(h|=1024),0!=(2&a)&&(d|=_.FD_DATASYNC),0!=(4&a)&&(h|=2048),0!=(8&a)&&(h|=1052672,d|=_.FD_SYNC),0!=(16&a)&&(h|=1052672,d|=_.FD_SYNC),f&&0==(1536&h)&&(d|=_.FD_SEEK);const g=x.get(this),E=g.fds.get(t,l,d),y=W(this),p=y.HEAPU8,I=$.decode(p.subarray(n,n+r)),T=z(this),A=Y(T,E,I,e),m=T.openSync(A,h,438),b=g.fds.getFileTypeByFd(m);if(0!=(2&i)&&3!==b)return 54;const{base:w,inheriting:S}=N(g.fds.stdio,m,h,b),B=g.fds.insert(m,A,A,b,s&w,o&S,0),D=T.fstatSync(m,{bigint:!0});D.isFile()&&(B.size=D.size,0!=(1024&h)&&(B.pos=D.size));return y.view.setInt32(c,B.id,!0),0})),this.path_readlink=G("path_readlink",(function(t,e,n,r,i,s){if(e=Number(e),n=Number(n),r=Number(r),i=Number(i),s=Number(s),0===e||0===r||0===s)return 28;const{HEAPU8:o,view:a}=W(this),c=x.get(this).fds.get(t,_.PATH_READLINK,BigInt(0));let u=$.decode(o.subarray(e,e+n));u=l(c.realPath,u);const f=z(this).readlinkSync(u),h=j.encode(f),d=Math.min(h.length,i);return d>=i?42:(o.set(h.subarray(0,d),r),o[r+d]=0,a.setUint32(s,d+1,!0),0)})),this.path_remove_directory=G("path_remove_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=W(this),i=x.get(this).fds.get(t,_.PATH_REMOVE_DIRECTORY,BigInt(0));let s=$.decode(r.subarray(e,e+n));s=l(i.realPath,s);return z(this).rmdirSync(s),0})),this.path_rename=G("path_rename",(function(t,e,n,r,i,s){if(e=Number(e),n=Number(n),i=Number(i),s=Number(s),0===e||0===i)return 28;const o=x.get(this);let a,c;t===r?a=c=o.fds.get(t,_.PATH_RENAME_SOURCE|_.PATH_RENAME_TARGET,BigInt(0)):(a=o.fds.get(t,_.PATH_RENAME_SOURCE,BigInt(0)),c=o.fds.get(r,_.PATH_RENAME_TARGET,BigInt(0)));const{HEAPU8:u}=W(this),f=l(a.realPath,$.decode(u.subarray(e,e+n))),h=l(c.realPath,$.decode(u.subarray(i,i+s)));return z(this).renameSync(f,h),0})),this.path_symlink=G("path_symlink",(function(t,e,n,r,i){if(t=Number(t),e=Number(e),r=Number(r),i=Number(i),0===t||0===r)return 28;const{HEAPU8:s}=W(this),o=x.get(this).fds.get(n,_.PATH_SYMLINK,BigInt(0)),a=$.decode(s.subarray(t,t+e));let c=$.decode(s.subarray(r,r+i));c=l(o.realPath,c);return z(this).symlinkSync(a,c),0})),this.path_unlink_file=G("path_unlink_file",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=W(this),i=x.get(this).fds.get(t,_.PATH_UNLINK_FILE,BigInt(0));let s=$.decode(r.subarray(e,e+n));s=l(i.realPath,s);return z(this).unlinkSync(s),0})),this.poll_oneoff=G("poll_oneoff",(function(t,e,n,r){return 52})),this.proc_exit=G("proc_exit",(function(t){return 0})),this.proc_raise=G("proc_raise",(function(t){return 52})),this.sched_yield=G("sched_yield",(function(){return 0})),this.random_get="undefined"!=typeof crypto&&"function"==typeof crypto.getRandomValues?G("random_get",(function(t,e){if(0===(t=Number(t)))return 28;e=Number(e);const{HEAPU8:n}=W(this);let r;const i=65536;for(r=0;r+i<e;r+=i)crypto.getRandomValues(n.subarray(t+r,t+r+i));return crypto.getRandomValues(n.subarray(t+r,t+e)),0})):G("random_get",(function(t,e){if(0===(t=Number(t)))return 28;e=Number(e);const{view:n}=W(this);for(let r=t;r<t+e;++r)n.setUint8(r,Math.floor(256*Math.random()));return 0})),this.sock_recv=G("sock_recv",(function(){return 58})),this.sock_send=G("sock_send",(function(){return 58})),this.sock_shutdown=G("sock_shutdown",(function(){return 58}));const a=i?i.fs:void 0,c=new H({size:3,in:r[0],out:r[1],err:r[2],fs:a,print:s,printErr:o});if(x.set(this,{fds:c,args:t,env:e}),a&&M.set(this,a),n.length>0)for(let t=0;t<n.length;++t){const e=a.realpathSync(n[t].realPath,"utf8"),r=a.openSync(e,"r",438);c.insertPreopen(r,n[t].mappedPath,e)}}}const Z=Object.freeze(Object.create(null)),q=Symbol("kExitCode"),Q=Symbol("kSetMemory"),X=Symbol("kStarted"),J=Symbol("kInstance");function tt(e,n){t(n,"instance"),t(n.exports,"instance.exports"),e[J]=n,e[Q](n.exports.memory)}class et{constructor(r=Z){var i;t(r,"options"),void 0!==r.args&&function(t,e){if(!Array.isArray(t))throw new TypeError(`${e} must be an array. Received ${null===t?"null":typeof t}`)}(r.args,"options.args");const s=(null!==(i=r.args)&&void 0!==i?i:[]).map(String),o=[];void 0!==r.env&&(t(r.env,"options.env"),Object.entries(r.env).forEach((({0:t,1:e})=>{void 0!==e&&o.push(`${t}=${e}`)})));const a=[];if(void 0!==r.preopens&&(t(r.preopens,"options.preopens"),Object.entries(r.preopens).forEach((({0:t,1:e})=>a.push({mappedPath:String(t),realPath:String(e)})))),a.length>0&&void 0===r.filesystem)throw new Error("filesystem is disabled, can not preopen directory");if(void 0!==r.filesystem){if(t(r.filesystem,"options.filesystem"),e(r.filesystem.type,"options.filesystem.type"),"memfs"!==r.filesystem.type)throw new Error(`Filesystem type ${r.filesystem.type} is not supported, only "memfs" is supported currently`);try{t(r.filesystem.fs,"options.filesystem.fs")}catch(t){throw new Error("Node.js fs like implementation is not provided")}}void 0!==r.print&&n(r.print,"options.print"),void 0!==r.printErr&&n(r.printErr,"options.printErr");const c=new V(s,o,a,[0,1,2],r.filesystem,r.print,r.printErr);for(const t in c)c[t]=c[t].bind(c);void 0!==r.returnOnExit&&(!function(t,e){if("boolean"!=typeof t)throw new TypeError(`${e} must be a boolean. Received ${null===t?"null":typeof t}`)}(r.returnOnExit,"options.returnOnExit"),r.returnOnExit&&(c.proc_exit=nt.bind(this))),this[Q]=c._setMemory,delete c._setMemory,this.wasiImport=c,this[X]=!1,this[q]=0,this[J]=void 0}start(t){if(this[X])throw new Error("WASI instance has already started");this[X]=!0,tt(this,t);const{_start:e,_initialize:i}=this[J].exports;let s;n(e,"instance.exports._start"),r(i,"instance.exports._initialize");try{s=e()}catch(t){if(t!==q)throw t}return s instanceof Promise?s.then((()=>this[q]),(t=>{if(t!==q)throw t;return this[q]})):this[q]}initialize(t){if(this[X])throw new Error("WASI instance has already started");this[X]=!0,tt(this,t);const{_start:e,_initialize:i}=this[J].exports;if(r(e,"instance.exports._start"),void 0!==i)return n(i,"instance.exports._initialize"),i()}}function nt(t){throw this[q]=t,q}export{a as Asyncify,U as Memory,et as WASI,O as extendMemory,u as load,f as loadSync};
1
+ const t="undefined"!=typeof WebAssembly?WebAssembly:"undefined"!=typeof WXWebAssembly?WXWebAssembly:void 0;if(!t)throw new Error("WebAssembly is not supported in this environment");function e(t,e){if(null===t||"object"!=typeof t)throw new TypeError(`${e} must be an object. Received ${null===t?"null":typeof t}`)}function n(t,e){if("string"!=typeof t)throw new TypeError(`${e} must be a string. Received ${null===t?"null":typeof t}`)}function r(t,e){if("function"!=typeof t)throw new TypeError(`${e} must be a function. Received ${null===t?"null":typeof t}`)}function i(t,e){if(void 0!==t)throw new TypeError(`${e} must be undefined. Received ${null===t?"null":typeof t}`)}function s(t){return!(!t||"object"!=typeof t&&"function"!=typeof t||"function"!=typeof t.then)}function o(t,e){const n=Object.create(null);return Object.keys(t).forEach((r=>{const i=t[r];Object.defineProperty(n,r,{enumerable:!0,value:e(i,r)})})),n}const a=["asyncify_get_state","asyncify_start_rewind","asyncify_start_unwind","asyncify_stop_rewind","asyncify_stop_unwind"];function u(t,e,n,r){if("function"!=typeof t.exports[r]||n<=0)return{wasm64:e,dataPtr:16,start:e?32:24,end:1024};const i=t.exports[r],s=e?Number(i(BigInt(16)+BigInt(n))):i(8+n);if(0===s)throw new Error("Allocate asyncify data failed");return e?{wasm64:e,dataPtr:s,start:s+16,end:s+16+n}:{wasm64:e,dataPtr:s,start:s+8,end:s+8+n}}class c{constructor(){this.value=void 0,this.exports=void 0,this.dataPtr=0}init(e,n,r){var i,s;if(this.exports)throw new Error("Asyncify has been initialized");if(!(e instanceof t.Memory))throw new TypeError("Require WebAssembly.Memory object");const o=n.exports;for(let t=0;t<a.length;++t)if("function"!=typeof o[a[t]])throw new TypeError("Invalid asyncify wasm");let c;const f=Boolean(r.wasm64);c=r.tryAllocate?!0===r.tryAllocate?u(n,f,4096,"malloc"):u(n,f,null!==(i=r.tryAllocate.size)&&void 0!==i?i:4096,null!==(s=r.tryAllocate.name)&&void 0!==s?s:"malloc"):{wasm64:f,dataPtr:16,start:f?32:24,end:1024},this.dataPtr=c.dataPtr,f?new BigInt64Array(e.buffer,this.dataPtr).set([BigInt(c.start),BigInt(c.end)]):new Int32Array(e.buffer,this.dataPtr).set([c.start,c.end]),this.exports=this.wrapExports(o,r.wrapExports);const h=Object.create(t.Instance.prototype);return Object.defineProperty(h,"exports",{value:this.exports}),h}assertState(){if(0!==this.exports.asyncify_get_state())throw new Error("Asyncify state error")}wrapImportFunction(t){return(...e)=>{for(;2===this.exports.asyncify_get_state();)return this.exports.asyncify_stop_rewind(),this.value;this.assertState();const n=t(...e);if(!s(n))return n;this.exports.asyncify_start_unwind(this.dataPtr),this.value=n}}wrapImports(t){const e={};return Object.keys(t).forEach((n=>{const r=t[n],i={};Object.keys(r).forEach((t=>{const e=r[t];i[t]="function"==typeof e?this.wrapImportFunction(e):e})),e[n]=i})),e}wrapExportFunction(t){return async(...e)=>{this.assertState();let n=t(...e);for(;1===this.exports.asyncify_get_state();)this.exports.asyncify_stop_unwind(),this.value=await this.value,this.assertState(),this.exports.asyncify_start_rewind(this.dataPtr),n=t();return this.assertState(),n}}wrapExports(t,e){return o(t,((t,n)=>{let r=-1!==a.indexOf(n)||"function"!=typeof t;return Array.isArray(e)&&(r=r||-1===e.indexOf(n)),r?t:this.wrapExportFunction(t)}))}}async function f(e,n){if("undefined"!=typeof wx&&"undefined"!=typeof __wxConfig)return await t.instantiate(e,n);const r=await fetch(e),i=await r.arrayBuffer();return await t.instantiate(i,n)}async function h(e,n,r){var i,s;if(n&&"object"!=typeof n)throw new TypeError("imports must be an object or undefined");let o,a;if(n=null!=n?n:{},r&&(o=new c,n=o.wrapImports(n)),e instanceof ArrayBuffer||ArrayBuffer.isView(e)){if(a=await t.instantiate(e,n),r){const t=a.instance.exports.memory||(null===(i=n.env)||void 0===i?void 0:i.memory);return{module:a.module,instance:o.init(t,a.instance,r)}}return a}if("string"!=typeof e&&!(e instanceof URL))throw new TypeError("Invalid source");if("function"==typeof t.instantiateStreaming)try{a=await t.instantiateStreaming(fetch(e),n)}catch(t){a=await f(e,n)}else a=await f(e,n);if(r){const t=a.instance.exports.memory||(null===(s=n.env)||void 0===s?void 0:s.memory);return{module:a.module,instance:o.init(t,a.instance,r)}}return a}function d(e,n,r){var i;if(e instanceof ArrayBuffer&&!ArrayBuffer.isView(e))throw new TypeError("Invalid source");if(n&&"object"!=typeof n)throw new TypeError("imports must be an object or undefined");let s;n=null!=n?n:{},r&&(s=new c,n=s.wrapImports(n));const o=new t.Module(e),a=new t.Instance(o,n),u={instance:a,module:o};if(r){const t=u.instance.exports.memory||(null===(i=n.env)||void 0===i?void 0:i.memory);return{module:u.module,instance:s.init(t,a,r)}}return u}function l(t){return 47===t}function _(...t){let e="",r=!1;for(let i=t.length-1;i>=-1&&!r;i--){const s=i>=0?t[i]:"/";n(s,"path"),0!==s.length&&(e=`${s}/${e}`,r=47===s.charCodeAt(0))}return e=function(t,e,n,r){let i="",s=0,o=-1,a=0,u=0;for(let c=0;c<=t.length;++c){if(c<t.length)u=t.charCodeAt(c);else{if(r(u))break;u=47}if(r(u)){if(o===c-1||1===a);else if(2===a){if(i.length<2||2!==s||46!==i.charCodeAt(i.length-1)||46!==i.charCodeAt(i.length-2)){if(i.length>2){const t=i.indexOf(n);-1===t?(i="",s=0):(i=i.slice(0,t),s=i.length-1-i.indexOf(n)),o=c,a=0;continue}if(0!==i.length){i="",s=0,o=c,a=0;continue}}e&&(i+=i.length>0?`${n}..`:"..",s=2)}else i.length>0?i+=`${n}${t.slice(o+1,c)}`:i=t.slice(o+1,c),s=c-o-1;o=c,a=0}else 46===u&&-1!==a?++a:a=-1}return i}(e,!r,"/",l),r?`/${e}`:e.length>0?e:"."}const g={FD_DATASYNC:BigInt(1)<<BigInt(0),FD_READ:BigInt(1)<<BigInt(1),FD_SEEK:BigInt(1)<<BigInt(2),FD_FDSTAT_SET_FLAGS:BigInt(1)<<BigInt(3),FD_SYNC:BigInt(1)<<BigInt(4),FD_TELL:BigInt(1)<<BigInt(5),FD_WRITE:BigInt(1)<<BigInt(6),FD_ADVISE:BigInt(1)<<BigInt(7),FD_ALLOCATE:BigInt(1)<<BigInt(8),PATH_CREATE_DIRECTORY:BigInt(1)<<BigInt(9),PATH_CREATE_FILE:BigInt(1)<<BigInt(10),PATH_LINK_SOURCE:BigInt(1)<<BigInt(11),PATH_LINK_TARGET:BigInt(1)<<BigInt(12),PATH_OPEN:BigInt(1)<<BigInt(13),FD_READDIR:BigInt(1)<<BigInt(14),PATH_READLINK:BigInt(1)<<BigInt(15),PATH_RENAME_SOURCE:BigInt(1)<<BigInt(16),PATH_RENAME_TARGET:BigInt(1)<<BigInt(17),PATH_FILESTAT_GET:BigInt(1)<<BigInt(18),PATH_FILESTAT_SET_SIZE:BigInt(1)<<BigInt(19),PATH_FILESTAT_SET_TIMES:BigInt(1)<<BigInt(20),FD_FILESTAT_GET:BigInt(1)<<BigInt(21),FD_FILESTAT_SET_SIZE:BigInt(1)<<BigInt(22),FD_FILESTAT_SET_TIMES:BigInt(1)<<BigInt(23),PATH_SYMLINK:BigInt(1)<<BigInt(24),PATH_REMOVE_DIRECTORY:BigInt(1)<<BigInt(25),PATH_UNLINK_FILE:BigInt(1)<<BigInt(26),POLL_FD_READWRITE:BigInt(1)<<BigInt(27),SOCK_SHUTDOWN:BigInt(1)<<BigInt(28),SOCK_ACCEPT:BigInt(1)<<BigInt(29)};class E extends Error{constructor(t,e){super(t),this.errno=e}getErrorMessage(){return function(t){switch(t){case 0:return"Success";case 1:return"Argument list too long";case 2:return"Permission denied";case 3:return"Address in use";case 4:return"Address not available";case 5:return"Address family not supported by protocol";case 6:return"Resource temporarily unavailable";case 7:return"Operation already in progress";case 8:return"Bad file descriptor";case 9:return"Bad message";case 10:return"Resource busy";case 11:return"Operation canceled";case 12:return"No child process";case 13:return"Connection aborted";case 14:return"Connection refused";case 15:return"Connection reset by peer";case 16:return"Resource deadlock would occur";case 17:return"Destination address required";case 18:return"Domain error";case 19:return"Quota exceeded";case 20:return"File exists";case 21:return"Bad address";case 22:return"File too large";case 23:return"Host is unreachable";case 24:return"Identifier removed";case 25:return"Illegal byte sequence";case 26:return"Operation in progress";case 27:return"Interrupted system call";case 28:return"Invalid argument";case 29:return"I/O error";case 30:return"Socket is connected";case 31:return"Is a directory";case 32:return"Symbolic link loop";case 33:return"No file descriptors available";case 34:return"Too many links";case 35:return"Message too large";case 36:return"Multihop attempted";case 37:return"Filename too long";case 38:return"Network is down";case 39:return"Connection reset by network";case 40:return"Network unreachable";case 41:return"Too many files open in system";case 42:return"No buffer space available";case 43:return"No such device";case 44:return"No such file or directory";case 45:return"Exec format error";case 46:return"No locks available";case 47:return"Link has been severed";case 48:return"Out of memory";case 49:return"No message of the desired type";case 50:return"Protocol not available";case 51:return"No space left on device";case 52:return"Function not implemented";case 53:return"Socket not connected";case 54:return"Not a directory";case 55:return"Directory not empty";case 56:return"State not recoverable";case 57:return"Not a socket";case 58:return"Not supported";case 59:return"Not a tty";case 60:return"No such device or address";case 61:return"Value too large for data type";case 62:return"Previous owner died";case 63:return"Operation not permitted";case 64:return"Broken pipe";case 65:return"Protocol error";case 66:return"Protocol not supported";case 67:return"Protocol wrong type for socket";case 68:return"Result not representable";case 69:return"Read-only file system";case 70:return"Invalid seek";case 71:return"No such process";case 72:return"Stale file handle";case 73:return"Operation timed out";case 74:return"Text file busy";case 75:return"Cross-device link";case 76:return"Capabilities insufficient";default:return"Unknown error"}}(this.errno)}}Object.defineProperty(E.prototype,"name",{configurable:!0,writable:!0,value:"WasiError"});const p=g.FD_DATASYNC|g.FD_READ|g.FD_SEEK|g.FD_FDSTAT_SET_FLAGS|g.FD_SYNC|g.FD_TELL|g.FD_WRITE|g.FD_ADVISE|g.FD_ALLOCATE|g.PATH_CREATE_DIRECTORY|g.PATH_CREATE_FILE|g.PATH_LINK_SOURCE|g.PATH_LINK_TARGET|g.PATH_OPEN|g.FD_READDIR|g.PATH_READLINK|g.PATH_RENAME_SOURCE|g.PATH_RENAME_TARGET|g.PATH_FILESTAT_GET|g.PATH_FILESTAT_SET_SIZE|g.PATH_FILESTAT_SET_TIMES|g.FD_FILESTAT_GET|g.FD_FILESTAT_SET_TIMES|g.FD_FILESTAT_SET_SIZE|g.PATH_SYMLINK|g.PATH_UNLINK_FILE|g.PATH_REMOVE_DIRECTORY|g.POLL_FD_READWRITE|g.SOCK_SHUTDOWN,y=p,I=p,T=p,A=p,m=g.FD_DATASYNC|g.FD_READ|g.FD_SEEK|g.FD_FDSTAT_SET_FLAGS|g.FD_SYNC|g.FD_TELL|g.FD_WRITE|g.FD_ADVISE|g.FD_ALLOCATE|g.FD_FILESTAT_GET|g.FD_FILESTAT_SET_SIZE|g.FD_FILESTAT_SET_TIMES|g.POLL_FD_READWRITE,b=BigInt(0),w=g.FD_FDSTAT_SET_FLAGS|g.FD_SYNC|g.FD_ADVISE|g.PATH_CREATE_DIRECTORY|g.PATH_CREATE_FILE|g.PATH_LINK_SOURCE|g.PATH_LINK_TARGET|g.PATH_OPEN|g.FD_READDIR|g.PATH_READLINK|g.PATH_RENAME_SOURCE|g.PATH_RENAME_TARGET|g.PATH_FILESTAT_GET|g.PATH_FILESTAT_SET_SIZE|g.PATH_FILESTAT_SET_TIMES|g.FD_FILESTAT_GET|g.FD_FILESTAT_SET_TIMES|g.PATH_SYMLINK|g.PATH_UNLINK_FILE|g.PATH_REMOVE_DIRECTORY|g.POLL_FD_READWRITE,S=w|m,B=g.FD_READ|g.FD_FDSTAT_SET_FLAGS|g.FD_WRITE|g.FD_FILESTAT_GET|g.POLL_FD_READWRITE|g.SOCK_SHUTDOWN,F=p,D=g.FD_READ|g.FD_FDSTAT_SET_FLAGS|g.FD_WRITE|g.FD_FILESTAT_GET|g.POLL_FD_READWRITE,N=BigInt(0);function P(t,e,n,r){const i={base:BigInt(0),inheriting:BigInt(0)};if(0===r)throw new E("Unknown file type",28);switch(r){case 4:i.base=m,i.inheriting=b;break;case 3:i.base=w,i.inheriting=S;break;case 6:case 5:i.base=B,i.inheriting=F;break;case 2:-1!==t.indexOf(e)?(i.base=D,i.inheriting=N):(i.base=T,i.inheriting=A);break;case 1:i.base=y,i.inheriting=I;break;default:i.base=BigInt(0),i.inheriting=BigInt(0)}const s=3&n;return 0===s?i.base&=~g.FD_WRITE:1===s&&(i.base&=~g.FD_READ),i}function v(t,e){let n=0;if("number"==typeof e&&e>=0)n=e;else for(let e=0;e<t.length;e++){n+=t[e].length}let r=0;const i=new Uint8Array(n);for(let e=0;e<t.length;e++){const n=t[e];i.set(n,r),r+=n.length}return i}class R{constructor(t,e,n,r,i,s,o,a){this.id=t,this.fd=e,this.path=n,this.realPath=r,this.type=i,this.rightsBase=s,this.rightsInheriting=o,this.preopen=a,this.pos=BigInt(0),this.size=BigInt(0)}seek(t,e){if(0===e)this.pos=BigInt(t);else if(1===e)this.pos+=BigInt(t);else{if(2!==e)throw new E("Unknown whence",29);this.pos=BigInt(this.size)-BigInt(t)}return this.pos}}class L extends R{constructor(t,e,n,r,i,s,o,a,u){super(e,n,r,i,s,o,a,u),this._log=t,this._buf=null}write(t){const e=t;if(this._buf&&(t=v([this._buf,t]),this._buf=null),-1===t.indexOf(10))return this._buf=t,e.byteLength;let n,r=0,i=0;for(;-1!==(n=t.indexOf(10,r));){const e=(new TextDecoder).decode(t.subarray(i,n));this._log(e),r+=n-i+1,i=n+1}return r<t.length&&(this._buf=t.slice(r)),e.byteLength}}function H(t){return t.isBlockDevice()?1:t.isCharacterDevice()?2:t.isDirectory()?3:t.isSocket()?6:t.isFile()?4:t.isSymbolicLink()?7:0}function U(t,e,n){t.setBigUint64(e,n.dev,!0),t.setBigUint64(e+8,n.ino,!0),t.setBigUint64(e+16,BigInt(H(n)),!0),t.setBigUint64(e+24,n.nlink,!0),t.setBigUint64(e+32,n.size,!0),t.setBigUint64(e+40,n.atimeMs*BigInt(1e6),!0),t.setBigUint64(e+48,n.mtimeMs*BigInt(1e6),!0),t.setBigUint64(e+56,n.ctimeMs*BigInt(1e6),!0)}class O{constructor(t){this.used=0,this.size=t.size,this.fds=Array(t.size),this.stdio=[t.in,t.out,t.err],this.fs=t.fs,this.print=t.print,this.printErr=t.printErr,this.insertStdio(t.in,0,"<stdin>"),this.insertStdio(t.out,1,"<stdout>"),this.insertStdio(t.err,2,"<stderr>")}insertStdio(t,e,n){const{base:r,inheriting:i}=P(this.stdio,t,2,2),s=this.insert(t,n,n,2,r,i,0);if(s.id!==e)throw new E(`id: ${s.id} !== expected: ${e}`,8);return s}insert(t,e,n,r,i,s,o){var a,u;let c,f=-1;if(this.used>=this.size){const t=2*this.size;this.fds.length=t,f=this.size,this.size=t}else for(let t=0;t<this.size;++t)if(null==this.fds[t]){f=t;break}return c="<stdout>"===e?new L(null!==(a=this.print)&&void 0!==a?a:console.log,f,t,e,n,r,i,s,o):"<stderr>"===e?new L(null!==(u=this.printErr)&&void 0!==u?u:console.error,f,t,e,n,r,i,s,o):new R(f,t,e,n,r,i,s,o),this.fds[f]=c,this.used++,c}getFileTypeByFd(t){return H(this.fs.fstatSync(t))}insertPreopen(t,e,n){const r=this.getFileTypeByFd(t);if(3!==r)throw new E(`Preopen not dir: ["${e}", "${n}"]`,54);const i=P(this.stdio,t,0,r);return this.insert(t,e,n,r,i.base,i.inheriting,1)}get(t,e,n){if(t>=this.size)throw new E("Invalid fd",8);const r=this.fds[t];if(!r||r.id!==t)throw new E("Bad file descriptor",8);if((~r.rightsBase&e)!==BigInt(0)||(~r.rightsInheriting&n)!==BigInt(0))throw new E("Capabilities insufficient",76);return r}remove(t){if(t>=this.size)throw new E("Invalid fd",8);const e=this.fds[t];if(!e||e.id!==t)throw new E("Bad file descriptor",8);this.fds[t]=void 0,this.used--}renumber(t,e){if(t===e)return;if(t>=this.size||e>=this.size)throw new E("Invalid fd",8);const n=this.fds[t],r=this.fds[e];if(!n||!r||n.id!==t||r.id!==e)throw new E("Invalid fd",8);this.fs.closeSync(n.fd),this.fds[t]=this.fds[e],this.fds[t].id=t,this.fds[e]=void 0,this.used--}}const k=t.Memory;class C extends k{constructor(t){super(t)}get HEAP8(){return new Int8Array(super.buffer)}get HEAPU8(){return new Uint8Array(super.buffer)}get HEAP16(){return new Int16Array(super.buffer)}get HEAPU16(){return new Uint16Array(super.buffer)}get HEAP32(){return new Int32Array(super.buffer)}get HEAPU32(){return new Uint32Array(super.buffer)}get HEAP64(){return new BigInt64Array(super.buffer)}get HEAPU64(){return new BigUint64Array(super.buffer)}get HEAPF32(){return new Float32Array(super.buffer)}get HEAPF64(){return new Float64Array(super.buffer)}get view(){return new DataView(super.buffer)}}function x(e){return Object.getPrototypeOf(e)===t.Memory.prototype&&Object.setPrototypeOf(e,C.prototype),e}function M(t,e){if(0===t.length||0===e.length)return 0;let n=0,r=e.length-n;for(let i=0;i<t.length;++i){const s=t[i];if(r<s.length)return s.set(e.subarray(n,n+r),0),n+=r,r=0,n;s.set(e.subarray(n,n+s.length),0),n+=s.length,r-=s.length}return n}const z=new WeakMap,W=new WeakMap,K=new WeakMap;function j(t){return z.get(t)}function G(t){const e=K.get(t);if(!e)throw new Error("filesystem is unavailable");return e}function Y(t){if(t instanceof E)return t.errno;switch(t.code){case"ENOENT":return 44;case"EBADF":return 8;case"EINVAL":return 28;case"EPERM":return 63;case"EPROTO":return 65;case"EEXIST":return 20;case"ENOTDIR":return 54;case"EMFILE":return 33;case"EACCES":return 2;case"EISDIR":return 31;case"ENOTEMPTY":return 55;case"ENOSYS":return 52}throw t}function $(t,e){return function(t,e){return Object.defineProperty(e,"name",{value:t}),e}(t,(function(){let t;try{t=e.apply(this,arguments)}catch(t){return Y(t)}return s(t)?t.then((t=>t),Y):t}))}function V(t,e,n,r){let i=_(e.realPath,n);if(1==(1&r))try{i=t.readlinkSync(i)}catch(t){if("EINVAL"!==t.code&&"ENOENT"!==t.code)throw t}return i}const Z=new TextEncoder,q=new TextDecoder;class X{constructor(e,n,r,i,s,o,a){this._setMemory=function(e){if(!(e instanceof t.Memory))throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');z.set(this,x(e))},this.args_get=$("args_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{HEAPU8:n,view:r}=j(this),i=W.get(this).args;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=Z.encode(o+"\0");n.set(a,e),e+=a.length}return 0})),this.args_sizes_get=$("args_sizes_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{view:n}=j(this),r=W.get(this).args;return n.setUint32(t,r.length,!0),n.setUint32(e,Z.encode(r.join("\0")+"\0").length,!0),0})),this.environ_get=$("environ_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{HEAPU8:n,view:r}=j(this),i=W.get(this).env;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=Z.encode(o+"\0");n.set(a,e),e+=a.length}return 0})),this.environ_sizes_get=$("environ_sizes_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{view:n}=j(this),r=W.get(this);return n.setUint32(t,r.env.length,!0),n.setUint32(e,Z.encode(r.env.join("\0")+"\0").length,!0),0})),this.clock_res_get=$("clock_res_get",(function(t,e){if(0===(e=Number(e)))return 28;const{view:n}=j(this);switch(t){case 0:return n.setBigUint64(e,BigInt(1e6),!0),0;case 1:case 2:case 3:return n.setBigUint64(e,BigInt(1e3),!0),0;default:return 28}})),this.clock_time_get=$("clock_time_get",(function(t,e,n){if(0===(n=Number(n)))return 28;const{view:r}=j(this);switch(t){case 0:return r.setBigUint64(n,BigInt(Date.now())*BigInt(1e6),!0),0;case 1:case 2:case 3:{const t=performance.now(),e=Math.trunc(t),i=Math.floor(1e3*(t-e)),s=BigInt(e)*BigInt(1e9)+BigInt(i)*BigInt(1e6);return r.setBigUint64(n,s,!0),0}default:return 28}})),this.fd_advise=$("fd_advise",(function(t,e,n,r){return 52})),this.fd_allocate=$("fd_allocate",(function(t,e,n){const r=W.get(this),i=G(this),s=r.fds.get(t,g.FD_ALLOCATE,BigInt(0));return i.fstatSync(s.fd,{bigint:!0}).size<e+n&&i.truncateSync(s.fd,Number(e+n)),0})),this.fd_close=$("fd_close",(function(t){const e=W.get(this),n=e.fds.get(t,BigInt(0),BigInt(0));return G(this).closeSync(n.fd),e.fds.remove(t),0})),this.fd_datasync=$("fd_datasync",(function(t){const e=W.get(this).fds.get(t,g.FD_DATASYNC,BigInt(0));return G(this).fdatasyncSync(e.fd),0})),this.fd_fdstat_get=$("fd_fdstat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=W.get(this).fds.get(t,BigInt(0),BigInt(0)),{view:r}=j(this);return r.setUint16(e,n.type,!0),r.setUint16(e+2,0,!0),r.setBigUint64(e+8,n.rightsBase,!0),r.setBigUint64(e+16,n.rightsInheriting,!0),0})),this.fd_fdstat_set_flags=$("fd_fdstat_set_flags",(function(t,e){return 52})),this.fd_fdstat_set_rights=$("fd_fdstat_set_rights",(function(t,e,n){const r=W.get(this).fds.get(t,BigInt(0),BigInt(0));return(e|r.rightsBase)>r.rightsBase||(n|r.rightsInheriting)>r.rightsInheriting?76:(r.rightsBase=e,r.rightsInheriting=n,0)})),this.fd_filestat_get=$("fd_filestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=W.get(this).fds.get(t,g.FD_FILESTAT_GET,BigInt(0)),r=G(this).fstatSync(n.fd,{bigint:!0}),{view:i}=j(this);return U(i,e,r),0})),this.fd_filestat_set_size=$("fd_filestat_set_size",(function(t,e){const n=W.get(this).fds.get(t,g.FD_FILESTAT_SET_SIZE,BigInt(0));return G(this).ftruncateSync(n.fd,Number(e)),0})),this.fd_filestat_set_times=$("fd_filestat_set_times",(function(t,e,n,r){const i=W.get(this).fds.get(t,g.FD_FILESTAT_SET_TIMES,BigInt(0));2==(2&r)&&(e=BigInt(1e6*Date.now())),8==(8&r)&&(n=BigInt(1e6*Date.now()));return G(this).futimesSync(i.fd,Number(e),Number(n)),0})),this.fd_pread=$("fd_pread",(function(t,e,n,r,i){if(e=Number(e),i=Number(i),0===e||0===i)return 28;const{HEAPU8:s,view:o}=j(this),a=W.get(this).fds.get(t,g.FD_READ|g.FD_SEEK,BigInt(0));let u=0;const c=Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,i=o.getInt32(r,!0),a=o.getUint32(r+4,!0);return u+=a,s.subarray(i,i+a)}));let f=0;const h=new Uint8Array(u);h._isBuffer=!0;const d=G(this).readSync(a.fd,h,0,h.length,Number(r));return f=h?M(c,h.subarray(0,d)):0,o.setUint32(i,f,!0),0})),this.fd_prestat_get=$("fd_prestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=W.get(this);let r;try{r=n.fds.get(t,BigInt(0),BigInt(0))}catch(t){if(t instanceof E)return t.errno;throw t}if(1!==r.preopen)return 28;const{view:i}=j(this);return i.setUint32(e,0,!0),i.setUint32(e+4,Z.encode(r.path).length+1,!0),0})),this.fd_prestat_dir_name=$("fd_prestat_dir_name",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const r=W.get(this).fds.get(t,BigInt(0),BigInt(0));if(1!==r.preopen)return 8;const i=Z.encode(r.path+"\0");if(i.length>n)return 42;const{HEAPU8:s}=j(this);return s.set(i,e),0})),this.fd_pwrite=$("fd_pwrite",(function(t,e,n,r,i){if(e=Number(e),i=Number(i),0===e||0===i)return 28;const{HEAPU8:s,view:o}=j(this),a=W.get(this).fds.get(t,g.FD_WRITE|g.FD_SEEK,BigInt(0)),u=v(Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,i=o.getInt32(r,!0),a=o.getUint32(r+4,!0);return s.subarray(i,i+a)}))),c=G(this).writeSync(a.fd,u,0,u.length,Number(r));return o.setUint32(i,c,!0),0})),this.fd_read=$("fd_read",(function(t,e,n,r){if(e=Number(e),r=Number(r),0===e||0===r)return 28;const{HEAPU8:i,view:s}=j(this),o=W.get(this).fds.get(t,g.FD_READ,BigInt(0));let a=0;const u=Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,o=s.getInt32(r,!0),u=s.getUint32(r+4,!0);return a+=u,i.subarray(o,o+u)}));let c,f=0;if(0===t){if("undefined"==typeof window||"function"!=typeof window.prompt)return 58;c=function(){const t=window.prompt();return null===t?new Uint8Array:(new TextEncoder).encode(t+"\n")}(),f=c?M(u,c):0}else{c=new Uint8Array(a),c._isBuffer=!0;const t=G(this).readSync(o.fd,c,0,c.length,Number(o.pos));f=c?M(u,c.subarray(0,t)):0,o.pos+=BigInt(f)}return s.setUint32(r,f,!0),0})),this.fd_seek=$("fd_seek",(function(t,e,n,r){if(0===(r=Number(r)))return 28;if(0===t||1===t||2===t)return 0;const i=W.get(this).fds.get(t,g.FD_SEEK,BigInt(0)).seek(e,n),{view:s}=j(this);return s.setBigUint64(r,i,!0),0})),this.fd_readdir=$("fd_readdir",(function(t,e,n,r,i){if(e=Number(e),n=Number(n),i=Number(i),0===e||0===i)return 0;const s=W.get(this).fds.get(t,g.FD_READDIR,BigInt(0)),o=G(this),a=o.readdirSync(s.realPath,{withFileTypes:!0}),{HEAPU8:u,view:c}=j(this);let f=0;for(let t=Number(r);t<a.length;t++){const r=Z.encode(a[t].name),i=o.statSync(_(s.realPath,a[t].name),{bigint:!0}),c=new Uint8Array(24+r.byteLength),h=new DataView(c.buffer);let d;h.setBigUint64(0,BigInt(t+1),!0),h.setBigUint64(8,BigInt(i.ino?i.ino:0),!0),h.setUint32(16,r.byteLength,!0),d=a[t].isFile()?4:a[t].isDirectory()?3:a[t].isSymbolicLink()?7:a[t].isCharacterDevice()?2:a[t].isBlockDevice()?1:a[t].isSocket()?6:0,h.setUint8(20,d),c.set(r,24);const l=c.slice(0,Math.min(c.length,n-f));u.set(l,e+f),f+=l.byteLength}return c.setUint32(i,f,!0),0})),this.fd_renumber=$("fd_renumber",(function(t,e){return W.get(this).fds.renumber(e,t),0})),this.fd_sync=$("fd_sync",(function(t){const e=W.get(this).fds.get(t,g.FD_SYNC,BigInt(0));return G(this).fsyncSync(e.fd),0})),this.fd_tell=$("fd_tell",(function(t,e){const n=W.get(this).fds.get(t,g.FD_TELL,BigInt(0)),r=BigInt(n.pos),{view:i}=j(this);return i.setBigUint64(Number(e),r,!0),0})),this.fd_write=$("fd_write",(function(t,e,n,r){if(e=Number(e),r=Number(r),0===e||0===r)return 28;const{HEAPU8:i,view:s}=j(this),o=W.get(this).fds.get(t,g.FD_WRITE,BigInt(0)),a=v(Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,o=s.getInt32(r,!0),a=s.getUint32(r+4,!0);return i.subarray(o,o+a)})));let u;if(1===t||2===t)u=o.write(a);else{u=G(this).writeSync(o.fd,a,0,a.length,Number(o.pos)),o.pos+=BigInt(u)}return s.setUint32(r,u,!0),0})),this.path_create_directory=$("path_create_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=j(this),i=W.get(this).fds.get(t,g.PATH_CREATE_DIRECTORY,BigInt(0));let s=q.decode(r.subarray(e,e+n));s=_(i.realPath,s);return G(this).mkdirSync(s),0})),this.path_filestat_get=$("path_filestat_get",(function(t,e,n,r,i){if(n=Number(n),r=Number(r),i=Number(i),0===n||0===i)return 28;const{HEAPU8:s,view:o}=j(this),a=W.get(this).fds.get(t,g.PATH_FILESTAT_GET,BigInt(0));let u=q.decode(s.subarray(n,n+r));const c=G(this);let f;return u=_(a.realPath,u),f=1==(1&e)?c.statSync(u,{bigint:!0}):c.lstatSync(u,{bigint:!0}),U(o,i,f),0})),this.path_filestat_set_times=$("path_filestat_set_times",(function(t,e,n,r,i,s,o){if(n=Number(n),r=Number(r),0===n)return 28;if(-16&o)return 28;const{HEAPU8:a}=j(this),u=W.get(this).fds.get(t,g.PATH_FILESTAT_SET_TIMES,BigInt(0)),c=G(this),f=V(c,u,q.decode(a.subarray(n,n+r)),e);return 2==(2&o)&&(i=BigInt(1e6*Date.now())),8==(8&o)&&(s=BigInt(1e6*Date.now())),c.utimesSync(f,Number(i),Number(s)),0})),this.path_link=$("path_link",(function(t,e,n,r,i,s,o){if(n=Number(n),r=Number(r),s=Number(s),o=Number(o),0===n||0===s)return 28;const a=W.get(this);let u,c;t===i?u=c=a.fds.get(t,g.PATH_LINK_SOURCE|g.PATH_LINK_TARGET,BigInt(0)):(u=a.fds.get(t,g.PATH_LINK_SOURCE,BigInt(0)),c=a.fds.get(i,g.PATH_LINK_TARGET,BigInt(0)));const{HEAPU8:f}=j(this),h=G(this),d=V(h,u,q.decode(f.subarray(n,n+r)),e),l=_(c.realPath,q.decode(f.subarray(s,s+o)));return h.linkSync(d,l),0})),this.path_open=$("path_open",(function(t,e,n,r,i,s,o,a,u){if(n=Number(n),u=Number(u),0===n||0===u)return 28;r=Number(r),s=BigInt(s);const c=((s=BigInt(s))&(g.FD_READ|g.FD_READDIR))!==BigInt(0),f=(s&(g.FD_DATASYNC|g.FD_WRITE|g.FD_ALLOCATE|g.FD_FILESTAT_SET_SIZE))!==BigInt(0);let h=f?c?2:1:0,d=g.PATH_OPEN,l=s|o;0!=(1&i)&&(h|=64,d|=g.PATH_CREATE_FILE),0!=(2&i)&&(h|=65536),0!=(4&i)&&(h|=128),0!=(8&i)&&(h|=512,d|=g.PATH_FILESTAT_SET_SIZE),0!=(1&a)&&(h|=1024),0!=(2&a)&&(l|=g.FD_DATASYNC),0!=(4&a)&&(h|=2048),0!=(8&a)&&(h|=1052672,l|=g.FD_SYNC),0!=(16&a)&&(h|=1052672,l|=g.FD_SYNC),f&&0==(1536&h)&&(l|=g.FD_SEEK);const _=W.get(this),E=_.fds.get(t,d,l),p=j(this),y=p.HEAPU8,I=q.decode(y.subarray(n,n+r)),T=G(this),A=V(T,E,I,e),m=T.openSync(A,h,438),b=_.fds.getFileTypeByFd(m);if(0!=(2&i)&&3!==b)return 54;const{base:w,inheriting:S}=P(_.fds.stdio,m,h,b),B=_.fds.insert(m,A,A,b,s&w,o&S,0),F=T.fstatSync(m,{bigint:!0});F.isFile()&&(B.size=F.size,0!=(1024&h)&&(B.pos=F.size));return p.view.setInt32(u,B.id,!0),0})),this.path_readlink=$("path_readlink",(function(t,e,n,r,i,s){if(e=Number(e),n=Number(n),r=Number(r),i=Number(i),s=Number(s),0===e||0===r||0===s)return 28;const{HEAPU8:o,view:a}=j(this),u=W.get(this).fds.get(t,g.PATH_READLINK,BigInt(0));let c=q.decode(o.subarray(e,e+n));c=_(u.realPath,c);const f=G(this).readlinkSync(c),h=Z.encode(f),d=Math.min(h.length,i);return d>=i?42:(o.set(h.subarray(0,d),r),o[r+d]=0,a.setUint32(s,d+1,!0),0)})),this.path_remove_directory=$("path_remove_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=j(this),i=W.get(this).fds.get(t,g.PATH_REMOVE_DIRECTORY,BigInt(0));let s=q.decode(r.subarray(e,e+n));s=_(i.realPath,s);return G(this).rmdirSync(s),0})),this.path_rename=$("path_rename",(function(t,e,n,r,i,s){if(e=Number(e),n=Number(n),i=Number(i),s=Number(s),0===e||0===i)return 28;const o=W.get(this);let a,u;t===r?a=u=o.fds.get(t,g.PATH_RENAME_SOURCE|g.PATH_RENAME_TARGET,BigInt(0)):(a=o.fds.get(t,g.PATH_RENAME_SOURCE,BigInt(0)),u=o.fds.get(r,g.PATH_RENAME_TARGET,BigInt(0)));const{HEAPU8:c}=j(this),f=_(a.realPath,q.decode(c.subarray(e,e+n))),h=_(u.realPath,q.decode(c.subarray(i,i+s)));return G(this).renameSync(f,h),0})),this.path_symlink=$("path_symlink",(function(t,e,n,r,i){if(t=Number(t),e=Number(e),r=Number(r),i=Number(i),0===t||0===r)return 28;const{HEAPU8:s}=j(this),o=W.get(this).fds.get(n,g.PATH_SYMLINK,BigInt(0)),a=q.decode(s.subarray(t,t+e));let u=q.decode(s.subarray(r,r+i));u=_(o.realPath,u);return G(this).symlinkSync(a,u),0})),this.path_unlink_file=$("path_unlink_file",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=j(this),i=W.get(this).fds.get(t,g.PATH_UNLINK_FILE,BigInt(0));let s=q.decode(r.subarray(e,e+n));s=_(i.realPath,s);return G(this).unlinkSync(s),0})),this.poll_oneoff=$("poll_oneoff",(function(t,e,n,r){return 52})),this.proc_exit=$("proc_exit",(function(t){return 0})),this.proc_raise=$("proc_raise",(function(t){return 52})),this.sched_yield=$("sched_yield",(function(){return 0})),this.random_get="undefined"!=typeof crypto&&"function"==typeof crypto.getRandomValues?$("random_get",(function(t,e){if(0===(t=Number(t)))return 28;e=Number(e);const{HEAPU8:n}=j(this);let r;const i=65536;for(r=0;r+i<e;r+=i)crypto.getRandomValues(n.subarray(t+r,t+r+i));return crypto.getRandomValues(n.subarray(t+r,t+e)),0})):$("random_get",(function(t,e){if(0===(t=Number(t)))return 28;e=Number(e);const{view:n}=j(this);for(let r=t;r<t+e;++r)n.setUint8(r,Math.floor(256*Math.random()));return 0})),this.sock_recv=$("sock_recv",(function(){return 58})),this.sock_send=$("sock_send",(function(){return 58})),this.sock_shutdown=$("sock_shutdown",(function(){return 58}));const u=s?s.fs:void 0,c=new O({size:3,in:i[0],out:i[1],err:i[2],fs:u,print:o,printErr:a});if(W.set(this,{fds:c,args:e,env:n}),u&&K.set(this,u),r.length>0)for(let t=0;t<r.length;++t){const e=u.realpathSync(r[t].realPath,"utf8"),n=u.openSync(e,"r",438);c.insertPreopen(n,r[t].mappedPath,e)}}}const Q=Object.freeze(Object.create(null)),J=Symbol("kExitCode"),tt=Symbol("kSetMemory"),et=Symbol("kStarted"),nt=Symbol("kInstance");function rt(t,n){e(n,"instance"),e(n.exports,"instance.exports"),t[nt]=n,t[tt](n.exports.memory)}class it{constructor(t=Q){var i;e(t,"options"),void 0!==t.args&&function(t,e){if(!Array.isArray(t))throw new TypeError(`${e} must be an array. Received ${null===t?"null":typeof t}`)}(t.args,"options.args");const s=(null!==(i=t.args)&&void 0!==i?i:[]).map(String),o=[];void 0!==t.env&&(e(t.env,"options.env"),Object.entries(t.env).forEach((({0:t,1:e})=>{void 0!==e&&o.push(`${t}=${e}`)})));const a=[];if(void 0!==t.preopens&&(e(t.preopens,"options.preopens"),Object.entries(t.preopens).forEach((({0:t,1:e})=>a.push({mappedPath:String(t),realPath:String(e)})))),a.length>0&&void 0===t.filesystem)throw new Error("filesystem is disabled, can not preopen directory");if(void 0!==t.filesystem){if(e(t.filesystem,"options.filesystem"),n(t.filesystem.type,"options.filesystem.type"),"memfs"!==t.filesystem.type)throw new Error(`Filesystem type ${t.filesystem.type} is not supported, only "memfs" is supported currently`);try{e(t.filesystem.fs,"options.filesystem.fs")}catch(t){throw new Error("Node.js fs like implementation is not provided")}}void 0!==t.print&&r(t.print,"options.print"),void 0!==t.printErr&&r(t.printErr,"options.printErr");const u=new X(s,o,a,[0,1,2],t.filesystem,t.print,t.printErr);for(const t in u)u[t]=u[t].bind(u);void 0!==t.returnOnExit&&(!function(t,e){if("boolean"!=typeof t)throw new TypeError(`${e} must be a boolean. Received ${null===t?"null":typeof t}`)}(t.returnOnExit,"options.returnOnExit"),t.returnOnExit&&(u.proc_exit=st.bind(this))),this[tt]=u._setMemory,delete u._setMemory,this.wasiImport=u,this[et]=!1,this[J]=0,this[nt]=void 0}start(t){if(this[et])throw new Error("WASI instance has already started");this[et]=!0,rt(this,t);const{_start:e,_initialize:n}=this[nt].exports;let s;r(e,"instance.exports._start"),i(n,"instance.exports._initialize");try{s=e()}catch(t){if(t!==J)throw t}return s instanceof Promise?s.then((()=>this[J]),(t=>{if(t!==J)throw t;return this[J]})):this[J]}initialize(t){if(this[et])throw new Error("WASI instance has already started");this[et]=!0,rt(this,t);const{_start:e,_initialize:n}=this[nt].exports;if(i(e,"instance.exports._start"),void 0!==n)return r(n,"instance.exports._initialize"),n()}}function st(t){throw this[J]=t,J}function ot(){const e=t.Function;if("function"!=typeof e)throw new Error('WebAssembly.Function is not supported in this environment. If you are using V8 based browser like Chrome, try to specify --js-flag="--wasm-staging --experimental-wasm-stack-switching"');return e}function at(t,e,n){const r=ot();if("function"!=typeof t)throw new TypeError("Function required");const i=e.slice(0);return i.unshift("externref"),new r({parameters:i,results:n},t,{suspending:"first"})}function ut(t){const e=ot();if("function"!=typeof t)throw new TypeError("Function required");return new e({parameters:[...e.type(t).parameters.slice(1)],results:["externref"]},t,{promising:"first"})}function ct(t,e){return o(t,((t,n)=>{let r="function"!=typeof t;return Array.isArray(e)&&(r=r||-1===e.indexOf(n)),r?t:ut(t)}))}export{c as Asyncify,C as Memory,it as WASI,k as WebAssemblyMemory,x as extendMemory,h as load,d as loadSync,ut as wrapAsyncExport,at as wrapAsyncImport,ct as wrapExports};
package/dist/wasm-util.js CHANGED
@@ -4,6 +4,15 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.wasmUtil = {}));
5
5
  })(this, (function (exports) { 'use strict';
6
6
 
7
+ const _WebAssembly = typeof WebAssembly !== 'undefined'
8
+ ? WebAssembly
9
+ : typeof WXWebAssembly !== 'undefined'
10
+ ? WXWebAssembly
11
+ : undefined;
12
+ if (!_WebAssembly) {
13
+ throw new Error('WebAssembly is not supported in this environment');
14
+ }
15
+
7
16
  function validateObject(value, name) {
8
17
  if (value === null || typeof value !== 'object') {
9
18
  throw new TypeError(`${name} must be an object. Received ${value === null ? 'null' : typeof value}`);
@@ -36,6 +45,17 @@
36
45
  }
37
46
  function isPromiseLike(obj) {
38
47
  return !!(obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function');
48
+ }
49
+ function wrapInstanceExports(exports, mapFn) {
50
+ const newExports = Object.create(null);
51
+ Object.keys(exports).forEach(name => {
52
+ const exportValue = exports[name];
53
+ Object.defineProperty(newExports, name, {
54
+ enumerable: true,
55
+ value: mapFn(exportValue, name)
56
+ });
57
+ });
58
+ return newExports;
39
59
  }
40
60
 
41
61
  const ignoreNames = [
@@ -75,7 +95,7 @@
75
95
  if (this.exports) {
76
96
  throw new Error('Asyncify has been initialized');
77
97
  }
78
- if (!(memory instanceof WebAssembly.Memory)) {
98
+ if (!(memory instanceof _WebAssembly.Memory)) {
79
99
  throw new TypeError('Require WebAssembly.Memory object');
80
100
  }
81
101
  const exports = instance.exports;
@@ -110,7 +130,7 @@
110
130
  new Int32Array(memory.buffer, this.dataPtr).set([address.start, address.end]);
111
131
  }
112
132
  this.exports = this.wrapExports(exports, options.wrapExports);
113
- const asyncifiedInstance = Object.create(WebAssembly.Instance.prototype);
133
+ const asyncifiedInstance = Object.create(_WebAssembly.Instance.prototype);
114
134
  Object.defineProperty(asyncifiedInstance, 'exports', { value: this.exports });
115
135
  // Object.setPrototypeOf(instance, Instance.prototype)
116
136
  return asyncifiedInstance;
@@ -169,20 +189,13 @@
169
189
  });
170
190
  }
171
191
  wrapExports(exports, needWrap) {
172
- const newExports = Object.create(null);
173
- Object.keys(exports).forEach(name => {
174
- const exportValue = exports[name];
192
+ return wrapInstanceExports(exports, (exportValue, name) => {
175
193
  let ignore = ignoreNames.indexOf(name) !== -1 || typeof exportValue !== 'function';
176
194
  if (Array.isArray(needWrap)) {
177
195
  ignore = ignore || (needWrap.indexOf(name) === -1);
178
196
  }
179
- Object.defineProperty(newExports, name, {
180
- enumerable: true,
181
- value: ignore ? exportValue : this.wrapExportFunction(exportValue)
182
- });
197
+ return ignore ? exportValue : this.wrapExportFunction(exportValue);
183
198
  });
184
- // wrappedExports.set(exports, newExports)
185
- return newExports;
186
199
  }
187
200
  }
188
201
  // /** @public */
@@ -200,9 +213,12 @@
200
213
  // Object.defineProperty(Instance.prototype, 'exports', { enumerable: true })
201
214
 
202
215
  async function fetchWasm(urlOrBuffer, imports) {
216
+ if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
217
+ return await _WebAssembly.instantiate(urlOrBuffer, imports);
218
+ }
203
219
  const response = await fetch(urlOrBuffer);
204
220
  const buffer = await response.arrayBuffer();
205
- const source = await WebAssembly.instantiate(buffer, imports);
221
+ const source = await _WebAssembly.instantiate(buffer, imports);
206
222
  return source;
207
223
  }
208
224
  /** @public */
@@ -219,7 +235,7 @@
219
235
  imports = asyncifyHelper.wrapImports(imports);
220
236
  }
221
237
  if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
222
- source = await WebAssembly.instantiate(urlOrBuffer, imports);
238
+ source = await _WebAssembly.instantiate(urlOrBuffer, imports);
223
239
  if (asyncify) {
224
240
  const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
225
241
  return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
@@ -229,9 +245,9 @@
229
245
  if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
230
246
  throw new TypeError('Invalid source');
231
247
  }
232
- if (typeof WebAssembly.instantiateStreaming === 'function') {
248
+ if (typeof _WebAssembly.instantiateStreaming === 'function') {
233
249
  try {
234
- source = await WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
250
+ source = await _WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
235
251
  }
236
252
  catch (_) {
237
253
  source = await fetchWasm(urlOrBuffer, imports);
@@ -261,8 +277,8 @@
261
277
  asyncifyHelper = new Asyncify();
262
278
  imports = asyncifyHelper.wrapImports(imports);
263
279
  }
264
- const module = new WebAssembly.Module(buffer);
265
- const instance = new WebAssembly.Instance(module, imports);
280
+ const module = new _WebAssembly.Module(buffer);
281
+ const instance = new _WebAssembly.Instance(module, imports);
266
282
  const source = { instance, module };
267
283
  if (asyncify) {
268
284
  const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
@@ -848,7 +864,9 @@
848
864
  }
849
865
 
850
866
  /** @public */
851
- class Memory extends WebAssembly.Memory {
867
+ const WebAssemblyMemory = _WebAssembly.Memory;
868
+ /** @public */
869
+ class Memory extends WebAssemblyMemory {
852
870
  // eslint-disable-next-line @typescript-eslint/no-useless-constructor
853
871
  constructor(descriptor) {
854
872
  super(descriptor);
@@ -867,7 +885,7 @@
867
885
  }
868
886
  /** @public */
869
887
  function extendMemory(memory) {
870
- if (Object.getPrototypeOf(memory) === WebAssembly.Memory.prototype) {
888
+ if (Object.getPrototypeOf(memory) === _WebAssembly.Memory.prototype) {
871
889
  Object.setPrototypeOf(memory, Memory.prototype);
872
890
  }
873
891
  return memory;
@@ -978,7 +996,7 @@
978
996
  class WASI$1 {
979
997
  constructor(args, env, preopens, stdio, filesystem, print, printErr) {
980
998
  this._setMemory = function _setMemory(m) {
981
- if (!(m instanceof WebAssembly.Memory)) {
999
+ if (!(m instanceof _WebAssembly.Memory)) {
982
1000
  throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
983
1001
  }
984
1002
  _memory.set(this, extendMemory(m));
@@ -1285,6 +1303,9 @@
1285
1303
  let buffer;
1286
1304
  let nread = 0;
1287
1305
  if (fd === 0) {
1306
+ if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
1307
+ return 58 /* WasiErrno.ENOTSUP */;
1308
+ }
1288
1309
  buffer = readStdin();
1289
1310
  nread = buffer ? copyMemory(ioVecs, buffer) : 0;
1290
1311
  }
@@ -1882,12 +1903,54 @@
1882
1903
  throw kExitCode;
1883
1904
  }
1884
1905
 
1906
+ function checkWebAssemblyFunction() {
1907
+ const WebAssemblyFunction = _WebAssembly.Function;
1908
+ if (typeof WebAssemblyFunction !== 'function') {
1909
+ throw new Error('WebAssembly.Function is not supported in this environment.' +
1910
+ ' If you are using V8 based browser like Chrome, try to specify' +
1911
+ ' --js-flag="--wasm-staging --experimental-wasm-stack-switching"');
1912
+ }
1913
+ return WebAssemblyFunction;
1914
+ }
1915
+ /** @public */
1916
+ function wrapAsyncImport(f, parameterType, returnType) {
1917
+ const WebAssemblyFunction = checkWebAssemblyFunction();
1918
+ if (typeof f !== 'function') {
1919
+ throw new TypeError('Function required');
1920
+ }
1921
+ const parameters = parameterType.slice(0);
1922
+ parameters.unshift('externref');
1923
+ return new WebAssemblyFunction({ parameters, results: returnType }, f, { suspending: 'first' });
1924
+ }
1925
+ /** @public */
1926
+ function wrapAsyncExport(f) {
1927
+ const WebAssemblyFunction = checkWebAssemblyFunction();
1928
+ if (typeof f !== 'function') {
1929
+ throw new TypeError('Function required');
1930
+ }
1931
+ return new WebAssemblyFunction({ parameters: [...WebAssemblyFunction.type(f).parameters.slice(1)], results: ['externref'] }, f, { promising: 'first' });
1932
+ }
1933
+ /** @public */
1934
+ function wrapExports(exports, needWrap) {
1935
+ return wrapInstanceExports(exports, (exportValue, name) => {
1936
+ let ignore = typeof exportValue !== 'function';
1937
+ if (Array.isArray(needWrap)) {
1938
+ ignore = ignore || (needWrap.indexOf(name) === -1);
1939
+ }
1940
+ return ignore ? exportValue : wrapAsyncExport(exportValue);
1941
+ });
1942
+ }
1943
+
1885
1944
  exports.Asyncify = Asyncify;
1886
1945
  exports.Memory = Memory;
1887
1946
  exports.WASI = WASI;
1947
+ exports.WebAssemblyMemory = WebAssemblyMemory;
1888
1948
  exports.extendMemory = extendMemory;
1889
1949
  exports.load = load;
1890
1950
  exports.loadSync = loadSync;
1951
+ exports.wrapAsyncExport = wrapAsyncExport;
1952
+ exports.wrapAsyncImport = wrapAsyncImport;
1953
+ exports.wrapExports = wrapExports;
1891
1954
 
1892
1955
  Object.defineProperty(exports, '__esModule', { value: true });
1893
1956
 
@@ -1 +1 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).wasmUtil={})}(this,(function(t){"use strict";function e(t,e){if(null===t||"object"!=typeof t)throw new TypeError(`${e} must be an object. Received ${null===t?"null":typeof t}`)}function n(t,e){if("string"!=typeof t)throw new TypeError(`${e} must be a string. Received ${null===t?"null":typeof t}`)}function r(t,e){if("function"!=typeof t)throw new TypeError(`${e} must be a function. Received ${null===t?"null":typeof t}`)}function i(t,e){if(void 0!==t)throw new TypeError(`${e} must be undefined. Received ${null===t?"null":typeof t}`)}function s(t){return!(!t||"object"!=typeof t&&"function"!=typeof t||"function"!=typeof t.then)}const o=["asyncify_get_state","asyncify_start_rewind","asyncify_start_unwind","asyncify_stop_rewind","asyncify_stop_unwind"];function a(t,e,n,r){if("function"!=typeof t.exports[r]||n<=0)return{wasm64:e,dataPtr:16,start:e?32:24,end:1024};const i=t.exports[r],s=e?Number(i(BigInt(16)+BigInt(n))):i(8+n);if(0===s)throw new Error("Allocate asyncify data failed");return e?{wasm64:e,dataPtr:s,start:s+16,end:s+16+n}:{wasm64:e,dataPtr:s,start:s+8,end:s+8+n}}class c{constructor(){this.value=void 0,this.exports=void 0,this.dataPtr=0}init(t,e,n){var r,i;if(this.exports)throw new Error("Asyncify has been initialized");if(!(t instanceof WebAssembly.Memory))throw new TypeError("Require WebAssembly.Memory object");const s=e.exports;for(let t=0;t<o.length;++t)if("function"!=typeof s[o[t]])throw new TypeError("Invalid asyncify wasm");let c;const u=Boolean(n.wasm64);c=n.tryAllocate?!0===n.tryAllocate?a(e,u,4096,"malloc"):a(e,u,null!==(r=n.tryAllocate.size)&&void 0!==r?r:4096,null!==(i=n.tryAllocate.name)&&void 0!==i?i:"malloc"):{wasm64:u,dataPtr:16,start:u?32:24,end:1024},this.dataPtr=c.dataPtr,u?new BigInt64Array(t.buffer,this.dataPtr).set([BigInt(c.start),BigInt(c.end)]):new Int32Array(t.buffer,this.dataPtr).set([c.start,c.end]),this.exports=this.wrapExports(s,n.wrapExports);const f=Object.create(WebAssembly.Instance.prototype);return Object.defineProperty(f,"exports",{value:this.exports}),f}assertState(){if(0!==this.exports.asyncify_get_state())throw new Error("Asyncify state error")}wrapImportFunction(t){return(...e)=>{for(;2===this.exports.asyncify_get_state();)return this.exports.asyncify_stop_rewind(),this.value;this.assertState();const n=t(...e);if(!s(n))return n;this.exports.asyncify_start_unwind(this.dataPtr),this.value=n}}wrapImports(t){const e={};return Object.keys(t).forEach((n=>{const r=t[n],i={};Object.keys(r).forEach((t=>{const e=r[t];i[t]="function"==typeof e?this.wrapImportFunction(e):e})),e[n]=i})),e}wrapExportFunction(t){return async(...e)=>{this.assertState();let n=t(...e);for(;1===this.exports.asyncify_get_state();)this.exports.asyncify_stop_unwind(),this.value=await this.value,this.assertState(),this.exports.asyncify_start_rewind(this.dataPtr),n=t();return this.assertState(),n}}wrapExports(t,e){const n=Object.create(null);return Object.keys(t).forEach((r=>{const i=t[r];let s=-1!==o.indexOf(r)||"function"!=typeof i;Array.isArray(e)&&(s=s||-1===e.indexOf(r)),Object.defineProperty(n,r,{enumerable:!0,value:s?i:this.wrapExportFunction(i)})})),n}}async function u(t,e){const n=await fetch(t),r=await n.arrayBuffer();return await WebAssembly.instantiate(r,e)}function f(t){return 47===t}function h(...t){let e="",r=!1;for(let i=t.length-1;i>=-1&&!r;i--){const s=i>=0?t[i]:"/";n(s,"path"),0!==s.length&&(e=`${s}/${e}`,r=47===s.charCodeAt(0))}return e=function(t,e,n,r){let i="",s=0,o=-1,a=0,c=0;for(let u=0;u<=t.length;++u){if(u<t.length)c=t.charCodeAt(u);else{if(r(c))break;c=47}if(r(c)){if(o===u-1||1===a);else if(2===a){if(i.length<2||2!==s||46!==i.charCodeAt(i.length-1)||46!==i.charCodeAt(i.length-2)){if(i.length>2){const t=i.indexOf(n);-1===t?(i="",s=0):(i=i.slice(0,t),s=i.length-1-i.indexOf(n)),o=u,a=0;continue}if(0!==i.length){i="",s=0,o=u,a=0;continue}}e&&(i+=i.length>0?`${n}..`:"..",s=2)}else i.length>0?i+=`${n}${t.slice(o+1,u)}`:i=t.slice(o+1,u),s=u-o-1;o=u,a=0}else 46===c&&-1!==a?++a:a=-1}return i}(e,!r,"/",f),r?`/${e}`:e.length>0?e:"."}const l={FD_DATASYNC:BigInt(1)<<BigInt(0),FD_READ:BigInt(1)<<BigInt(1),FD_SEEK:BigInt(1)<<BigInt(2),FD_FDSTAT_SET_FLAGS:BigInt(1)<<BigInt(3),FD_SYNC:BigInt(1)<<BigInt(4),FD_TELL:BigInt(1)<<BigInt(5),FD_WRITE:BigInt(1)<<BigInt(6),FD_ADVISE:BigInt(1)<<BigInt(7),FD_ALLOCATE:BigInt(1)<<BigInt(8),PATH_CREATE_DIRECTORY:BigInt(1)<<BigInt(9),PATH_CREATE_FILE:BigInt(1)<<BigInt(10),PATH_LINK_SOURCE:BigInt(1)<<BigInt(11),PATH_LINK_TARGET:BigInt(1)<<BigInt(12),PATH_OPEN:BigInt(1)<<BigInt(13),FD_READDIR:BigInt(1)<<BigInt(14),PATH_READLINK:BigInt(1)<<BigInt(15),PATH_RENAME_SOURCE:BigInt(1)<<BigInt(16),PATH_RENAME_TARGET:BigInt(1)<<BigInt(17),PATH_FILESTAT_GET:BigInt(1)<<BigInt(18),PATH_FILESTAT_SET_SIZE:BigInt(1)<<BigInt(19),PATH_FILESTAT_SET_TIMES:BigInt(1)<<BigInt(20),FD_FILESTAT_GET:BigInt(1)<<BigInt(21),FD_FILESTAT_SET_SIZE:BigInt(1)<<BigInt(22),FD_FILESTAT_SET_TIMES:BigInt(1)<<BigInt(23),PATH_SYMLINK:BigInt(1)<<BigInt(24),PATH_REMOVE_DIRECTORY:BigInt(1)<<BigInt(25),PATH_UNLINK_FILE:BigInt(1)<<BigInt(26),POLL_FD_READWRITE:BigInt(1)<<BigInt(27),SOCK_SHUTDOWN:BigInt(1)<<BigInt(28),SOCK_ACCEPT:BigInt(1)<<BigInt(29)};class d extends Error{constructor(t,e){super(t),this.errno=e}getErrorMessage(){return function(t){switch(t){case 0:return"Success";case 1:return"Argument list too long";case 2:return"Permission denied";case 3:return"Address in use";case 4:return"Address not available";case 5:return"Address family not supported by protocol";case 6:return"Resource temporarily unavailable";case 7:return"Operation already in progress";case 8:return"Bad file descriptor";case 9:return"Bad message";case 10:return"Resource busy";case 11:return"Operation canceled";case 12:return"No child process";case 13:return"Connection aborted";case 14:return"Connection refused";case 15:return"Connection reset by peer";case 16:return"Resource deadlock would occur";case 17:return"Destination address required";case 18:return"Domain error";case 19:return"Quota exceeded";case 20:return"File exists";case 21:return"Bad address";case 22:return"File too large";case 23:return"Host is unreachable";case 24:return"Identifier removed";case 25:return"Illegal byte sequence";case 26:return"Operation in progress";case 27:return"Interrupted system call";case 28:return"Invalid argument";case 29:return"I/O error";case 30:return"Socket is connected";case 31:return"Is a directory";case 32:return"Symbolic link loop";case 33:return"No file descriptors available";case 34:return"Too many links";case 35:return"Message too large";case 36:return"Multihop attempted";case 37:return"Filename too long";case 38:return"Network is down";case 39:return"Connection reset by network";case 40:return"Network unreachable";case 41:return"Too many files open in system";case 42:return"No buffer space available";case 43:return"No such device";case 44:return"No such file or directory";case 45:return"Exec format error";case 46:return"No locks available";case 47:return"Link has been severed";case 48:return"Out of memory";case 49:return"No message of the desired type";case 50:return"Protocol not available";case 51:return"No space left on device";case 52:return"Function not implemented";case 53:return"Socket not connected";case 54:return"Not a directory";case 55:return"Directory not empty";case 56:return"State not recoverable";case 57:return"Not a socket";case 58:return"Not supported";case 59:return"Not a tty";case 60:return"No such device or address";case 61:return"Value too large for data type";case 62:return"Previous owner died";case 63:return"Operation not permitted";case 64:return"Broken pipe";case 65:return"Protocol error";case 66:return"Protocol not supported";case 67:return"Protocol wrong type for socket";case 68:return"Result not representable";case 69:return"Read-only file system";case 70:return"Invalid seek";case 71:return"No such process";case 72:return"Stale file handle";case 73:return"Operation timed out";case 74:return"Text file busy";case 75:return"Cross-device link";case 76:return"Capabilities insufficient";default:return"Unknown error"}}(this.errno)}}Object.defineProperty(d.prototype,"name",{configurable:!0,writable:!0,value:"WasiError"});const _=l.FD_DATASYNC|l.FD_READ|l.FD_SEEK|l.FD_FDSTAT_SET_FLAGS|l.FD_SYNC|l.FD_TELL|l.FD_WRITE|l.FD_ADVISE|l.FD_ALLOCATE|l.PATH_CREATE_DIRECTORY|l.PATH_CREATE_FILE|l.PATH_LINK_SOURCE|l.PATH_LINK_TARGET|l.PATH_OPEN|l.FD_READDIR|l.PATH_READLINK|l.PATH_RENAME_SOURCE|l.PATH_RENAME_TARGET|l.PATH_FILESTAT_GET|l.PATH_FILESTAT_SET_SIZE|l.PATH_FILESTAT_SET_TIMES|l.FD_FILESTAT_GET|l.FD_FILESTAT_SET_TIMES|l.FD_FILESTAT_SET_SIZE|l.PATH_SYMLINK|l.PATH_UNLINK_FILE|l.PATH_REMOVE_DIRECTORY|l.POLL_FD_READWRITE|l.SOCK_SHUTDOWN,g=_,E=_,y=_,p=_,I=l.FD_DATASYNC|l.FD_READ|l.FD_SEEK|l.FD_FDSTAT_SET_FLAGS|l.FD_SYNC|l.FD_TELL|l.FD_WRITE|l.FD_ADVISE|l.FD_ALLOCATE|l.FD_FILESTAT_GET|l.FD_FILESTAT_SET_SIZE|l.FD_FILESTAT_SET_TIMES|l.POLL_FD_READWRITE,T=BigInt(0),A=l.FD_FDSTAT_SET_FLAGS|l.FD_SYNC|l.FD_ADVISE|l.PATH_CREATE_DIRECTORY|l.PATH_CREATE_FILE|l.PATH_LINK_SOURCE|l.PATH_LINK_TARGET|l.PATH_OPEN|l.FD_READDIR|l.PATH_READLINK|l.PATH_RENAME_SOURCE|l.PATH_RENAME_TARGET|l.PATH_FILESTAT_GET|l.PATH_FILESTAT_SET_SIZE|l.PATH_FILESTAT_SET_TIMES|l.FD_FILESTAT_GET|l.FD_FILESTAT_SET_TIMES|l.PATH_SYMLINK|l.PATH_UNLINK_FILE|l.PATH_REMOVE_DIRECTORY|l.POLL_FD_READWRITE,m=A|I,b=l.FD_READ|l.FD_FDSTAT_SET_FLAGS|l.FD_WRITE|l.FD_FILESTAT_GET|l.POLL_FD_READWRITE|l.SOCK_SHUTDOWN,w=_,S=l.FD_READ|l.FD_FDSTAT_SET_FLAGS|l.FD_WRITE|l.FD_FILESTAT_GET|l.POLL_FD_READWRITE,B=BigInt(0);function D(t,e,n,r){const i={base:BigInt(0),inheriting:BigInt(0)};if(0===r)throw new d("Unknown file type",28);switch(r){case 4:i.base=I,i.inheriting=T;break;case 3:i.base=A,i.inheriting=m;break;case 6:case 5:i.base=b,i.inheriting=w;break;case 2:-1!==t.indexOf(e)?(i.base=S,i.inheriting=B):(i.base=y,i.inheriting=p);break;case 1:i.base=g,i.inheriting=E;break;default:i.base=BigInt(0),i.inheriting=BigInt(0)}const s=3&n;return 0===s?i.base&=~l.FD_WRITE:1===s&&(i.base&=~l.FD_READ),i}function N(t,e){let n=0;if("number"==typeof e&&e>=0)n=e;else for(let e=0;e<t.length;e++){n+=t[e].length}let r=0;const i=new Uint8Array(n);for(let e=0;e<t.length;e++){const n=t[e];i.set(n,r),r+=n.length}return i}class F{constructor(t,e,n,r,i,s,o,a){this.id=t,this.fd=e,this.path=n,this.realPath=r,this.type=i,this.rightsBase=s,this.rightsInheriting=o,this.preopen=a,this.pos=BigInt(0),this.size=BigInt(0)}seek(t,e){if(0===e)this.pos=BigInt(t);else if(1===e)this.pos+=BigInt(t);else{if(2!==e)throw new d("Unknown whence",29);this.pos=BigInt(this.size)-BigInt(t)}return this.pos}}class P extends F{constructor(t,e,n,r,i,s,o,a,c){super(e,n,r,i,s,o,a,c),this._log=t,this._buf=null}write(t){const e=t;if(this._buf&&(t=N([this._buf,t]),this._buf=null),-1===t.indexOf(10))return this._buf=t,e.byteLength;let n,r=0,i=0;for(;-1!==(n=t.indexOf(10,r));){const e=(new TextDecoder).decode(t.subarray(i,n));this._log(e),r+=n-i+1,i=n+1}return r<t.length&&(this._buf=t.slice(r)),e.byteLength}}function v(t){return t.isBlockDevice()?1:t.isCharacterDevice()?2:t.isDirectory()?3:t.isSocket()?6:t.isFile()?4:t.isSymbolicLink()?7:0}function R(t,e,n){t.setBigUint64(e,n.dev,!0),t.setBigUint64(e+8,n.ino,!0),t.setBigUint64(e+16,BigInt(v(n)),!0),t.setBigUint64(e+24,n.nlink,!0),t.setBigUint64(e+32,n.size,!0),t.setBigUint64(e+40,n.atimeMs*BigInt(1e6),!0),t.setBigUint64(e+48,n.mtimeMs*BigInt(1e6),!0),t.setBigUint64(e+56,n.ctimeMs*BigInt(1e6),!0)}class L{constructor(t){this.used=0,this.size=t.size,this.fds=Array(t.size),this.stdio=[t.in,t.out,t.err],this.fs=t.fs,this.print=t.print,this.printErr=t.printErr,this.insertStdio(t.in,0,"<stdin>"),this.insertStdio(t.out,1,"<stdout>"),this.insertStdio(t.err,2,"<stderr>")}insertStdio(t,e,n){const{base:r,inheriting:i}=D(this.stdio,t,2,2),s=this.insert(t,n,n,2,r,i,0);if(s.id!==e)throw new d(`id: ${s.id} !== expected: ${e}`,8);return s}insert(t,e,n,r,i,s,o){var a,c;let u,f=-1;if(this.used>=this.size){const t=2*this.size;this.fds.length=t,f=this.size,this.size=t}else for(let t=0;t<this.size;++t)if(null==this.fds[t]){f=t;break}return u="<stdout>"===e?new P(null!==(a=this.print)&&void 0!==a?a:console.log,f,t,e,n,r,i,s,o):"<stderr>"===e?new P(null!==(c=this.printErr)&&void 0!==c?c:console.error,f,t,e,n,r,i,s,o):new F(f,t,e,n,r,i,s,o),this.fds[f]=u,this.used++,u}getFileTypeByFd(t){return v(this.fs.fstatSync(t))}insertPreopen(t,e,n){const r=this.getFileTypeByFd(t);if(3!==r)throw new d(`Preopen not dir: ["${e}", "${n}"]`,54);const i=D(this.stdio,t,0,r);return this.insert(t,e,n,r,i.base,i.inheriting,1)}get(t,e,n){if(t>=this.size)throw new d("Invalid fd",8);const r=this.fds[t];if(!r||r.id!==t)throw new d("Bad file descriptor",8);if((~r.rightsBase&e)!==BigInt(0)||(~r.rightsInheriting&n)!==BigInt(0))throw new d("Capabilities insufficient",76);return r}remove(t){if(t>=this.size)throw new d("Invalid fd",8);const e=this.fds[t];if(!e||e.id!==t)throw new d("Bad file descriptor",8);this.fds[t]=void 0,this.used--}renumber(t,e){if(t===e)return;if(t>=this.size||e>=this.size)throw new d("Invalid fd",8);const n=this.fds[t],r=this.fds[e];if(!n||!r||n.id!==t||r.id!==e)throw new d("Invalid fd",8);this.fs.closeSync(n.fd),this.fds[t]=this.fds[e],this.fds[t].id=t,this.fds[e]=void 0,this.used--}}class H extends WebAssembly.Memory{constructor(t){super(t)}get HEAP8(){return new Int8Array(super.buffer)}get HEAPU8(){return new Uint8Array(super.buffer)}get HEAP16(){return new Int16Array(super.buffer)}get HEAPU16(){return new Uint16Array(super.buffer)}get HEAP32(){return new Int32Array(super.buffer)}get HEAPU32(){return new Uint32Array(super.buffer)}get HEAP64(){return new BigInt64Array(super.buffer)}get HEAPU64(){return new BigUint64Array(super.buffer)}get HEAPF32(){return new Float32Array(super.buffer)}get HEAPF64(){return new Float64Array(super.buffer)}get view(){return new DataView(super.buffer)}}function U(t){return Object.getPrototypeOf(t)===WebAssembly.Memory.prototype&&Object.setPrototypeOf(t,H.prototype),t}function O(t,e){if(0===t.length||0===e.length)return 0;let n=0,r=e.length-n;for(let i=0;i<t.length;++i){const s=t[i];if(r<s.length)return s.set(e.subarray(n,n+r),0),n+=r,r=0,n;s.set(e.subarray(n,n+s.length),0),n+=s.length,r-=s.length}return n}const k=new WeakMap,C=new WeakMap,x=new WeakMap;function M(t){return k.get(t)}function W(t){const e=x.get(t);if(!e)throw new Error("filesystem is unavailable");return e}function z(t){if(t instanceof d)return t.errno;switch(t.code){case"ENOENT":return 44;case"EBADF":return 8;case"EINVAL":return 28;case"EPERM":return 63;case"EPROTO":return 65;case"EEXIST":return 20;case"ENOTDIR":return 54;case"EMFILE":return 33;case"EACCES":return 2;case"EISDIR":return 31;case"ENOTEMPTY":return 55;case"ENOSYS":return 52}throw t}function K(t,e){return function(t,e){return Object.defineProperty(e,"name",{value:t}),e}(t,(function(){let t;try{t=e.apply(this,arguments)}catch(t){return z(t)}return s(t)?t.then((t=>t),z):t}))}function j(t,e,n,r){let i=h(e.realPath,n);if(1==(1&r))try{i=t.readlinkSync(i)}catch(t){if("EINVAL"!==t.code&&"ENOENT"!==t.code)throw t}return i}const G=new TextEncoder,Y=new TextDecoder;class ${constructor(t,e,n,r,i,s,o){this._setMemory=function(t){if(!(t instanceof WebAssembly.Memory))throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');k.set(this,U(t))},this.args_get=K("args_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{HEAPU8:n,view:r}=M(this),i=C.get(this).args;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=G.encode(o+"\0");n.set(a,e),e+=a.length}return 0})),this.args_sizes_get=K("args_sizes_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{view:n}=M(this),r=C.get(this).args;return n.setUint32(t,r.length,!0),n.setUint32(e,G.encode(r.join("\0")+"\0").length,!0),0})),this.environ_get=K("environ_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{HEAPU8:n,view:r}=M(this),i=C.get(this).env;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=G.encode(o+"\0");n.set(a,e),e+=a.length}return 0})),this.environ_sizes_get=K("environ_sizes_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{view:n}=M(this),r=C.get(this);return n.setUint32(t,r.env.length,!0),n.setUint32(e,G.encode(r.env.join("\0")+"\0").length,!0),0})),this.clock_res_get=K("clock_res_get",(function(t,e){if(0===(e=Number(e)))return 28;const{view:n}=M(this);switch(t){case 0:return n.setBigUint64(e,BigInt(1e6),!0),0;case 1:case 2:case 3:return n.setBigUint64(e,BigInt(1e3),!0),0;default:return 28}})),this.clock_time_get=K("clock_time_get",(function(t,e,n){if(0===(n=Number(n)))return 28;const{view:r}=M(this);switch(t){case 0:return r.setBigUint64(n,BigInt(Date.now())*BigInt(1e6),!0),0;case 1:case 2:case 3:{const t=performance.now(),e=Math.trunc(t),i=Math.floor(1e3*(t-e)),s=BigInt(e)*BigInt(1e9)+BigInt(i)*BigInt(1e6);return r.setBigUint64(n,s,!0),0}default:return 28}})),this.fd_advise=K("fd_advise",(function(t,e,n,r){return 52})),this.fd_allocate=K("fd_allocate",(function(t,e,n){const r=C.get(this),i=W(this),s=r.fds.get(t,l.FD_ALLOCATE,BigInt(0));return i.fstatSync(s.fd,{bigint:!0}).size<e+n&&i.truncateSync(s.fd,Number(e+n)),0})),this.fd_close=K("fd_close",(function(t){const e=C.get(this),n=e.fds.get(t,BigInt(0),BigInt(0));return W(this).closeSync(n.fd),e.fds.remove(t),0})),this.fd_datasync=K("fd_datasync",(function(t){const e=C.get(this).fds.get(t,l.FD_DATASYNC,BigInt(0));return W(this).fdatasyncSync(e.fd),0})),this.fd_fdstat_get=K("fd_fdstat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=C.get(this).fds.get(t,BigInt(0),BigInt(0)),{view:r}=M(this);return r.setUint16(e,n.type,!0),r.setUint16(e+2,0,!0),r.setBigUint64(e+8,n.rightsBase,!0),r.setBigUint64(e+16,n.rightsInheriting,!0),0})),this.fd_fdstat_set_flags=K("fd_fdstat_set_flags",(function(t,e){return 52})),this.fd_fdstat_set_rights=K("fd_fdstat_set_rights",(function(t,e,n){const r=C.get(this).fds.get(t,BigInt(0),BigInt(0));return(e|r.rightsBase)>r.rightsBase||(n|r.rightsInheriting)>r.rightsInheriting?76:(r.rightsBase=e,r.rightsInheriting=n,0)})),this.fd_filestat_get=K("fd_filestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=C.get(this).fds.get(t,l.FD_FILESTAT_GET,BigInt(0)),r=W(this).fstatSync(n.fd,{bigint:!0}),{view:i}=M(this);return R(i,e,r),0})),this.fd_filestat_set_size=K("fd_filestat_set_size",(function(t,e){const n=C.get(this).fds.get(t,l.FD_FILESTAT_SET_SIZE,BigInt(0));return W(this).ftruncateSync(n.fd,Number(e)),0})),this.fd_filestat_set_times=K("fd_filestat_set_times",(function(t,e,n,r){const i=C.get(this).fds.get(t,l.FD_FILESTAT_SET_TIMES,BigInt(0));2==(2&r)&&(e=BigInt(1e6*Date.now())),8==(8&r)&&(n=BigInt(1e6*Date.now()));return W(this).futimesSync(i.fd,Number(e),Number(n)),0})),this.fd_pread=K("fd_pread",(function(t,e,n,r,i){if(e=Number(e),i=Number(i),0===e||0===i)return 28;const{HEAPU8:s,view:o}=M(this),a=C.get(this).fds.get(t,l.FD_READ|l.FD_SEEK,BigInt(0));let c=0;const u=Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,i=o.getInt32(r,!0),a=o.getUint32(r+4,!0);return c+=a,s.subarray(i,i+a)}));let f=0;const h=new Uint8Array(c);h._isBuffer=!0;const d=W(this).readSync(a.fd,h,0,h.length,Number(r));return f=h?O(u,h.subarray(0,d)):0,o.setUint32(i,f,!0),0})),this.fd_prestat_get=K("fd_prestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=C.get(this);let r;try{r=n.fds.get(t,BigInt(0),BigInt(0))}catch(t){if(t instanceof d)return t.errno;throw t}if(1!==r.preopen)return 28;const{view:i}=M(this);return i.setUint32(e,0,!0),i.setUint32(e+4,G.encode(r.path).length+1,!0),0})),this.fd_prestat_dir_name=K("fd_prestat_dir_name",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const r=C.get(this).fds.get(t,BigInt(0),BigInt(0));if(1!==r.preopen)return 8;const i=G.encode(r.path+"\0");if(i.length>n)return 42;const{HEAPU8:s}=M(this);return s.set(i,e),0})),this.fd_pwrite=K("fd_pwrite",(function(t,e,n,r,i){if(e=Number(e),i=Number(i),0===e||0===i)return 28;const{HEAPU8:s,view:o}=M(this),a=C.get(this).fds.get(t,l.FD_WRITE|l.FD_SEEK,BigInt(0)),c=N(Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,i=o.getInt32(r,!0),a=o.getUint32(r+4,!0);return s.subarray(i,i+a)}))),u=W(this).writeSync(a.fd,c,0,c.length,Number(r));return o.setUint32(i,u,!0),0})),this.fd_read=K("fd_read",(function(t,e,n,r){if(e=Number(e),r=Number(r),0===e||0===r)return 28;const{HEAPU8:i,view:s}=M(this),o=C.get(this).fds.get(t,l.FD_READ,BigInt(0));let a=0;const c=Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,o=s.getInt32(r,!0),c=s.getUint32(r+4,!0);return a+=c,i.subarray(o,o+c)}));let u,f=0;if(0===t)u=function(){const t=window.prompt();return null===t?new Uint8Array:(new TextEncoder).encode(t+"\n")}(),f=u?O(c,u):0;else{u=new Uint8Array(a),u._isBuffer=!0;const t=W(this).readSync(o.fd,u,0,u.length,Number(o.pos));f=u?O(c,u.subarray(0,t)):0,o.pos+=BigInt(f)}return s.setUint32(r,f,!0),0})),this.fd_seek=K("fd_seek",(function(t,e,n,r){if(0===(r=Number(r)))return 28;if(0===t||1===t||2===t)return 0;const i=C.get(this).fds.get(t,l.FD_SEEK,BigInt(0)).seek(e,n),{view:s}=M(this);return s.setBigUint64(r,i,!0),0})),this.fd_readdir=K("fd_readdir",(function(t,e,n,r,i){if(e=Number(e),n=Number(n),i=Number(i),0===e||0===i)return 0;const s=C.get(this).fds.get(t,l.FD_READDIR,BigInt(0)),o=W(this),a=o.readdirSync(s.realPath,{withFileTypes:!0}),{HEAPU8:c,view:u}=M(this);let f=0;for(let t=Number(r);t<a.length;t++){const r=G.encode(a[t].name),i=o.statSync(h(s.realPath,a[t].name),{bigint:!0}),u=new Uint8Array(24+r.byteLength),l=new DataView(u.buffer);let d;l.setBigUint64(0,BigInt(t+1),!0),l.setBigUint64(8,BigInt(i.ino?i.ino:0),!0),l.setUint32(16,r.byteLength,!0),d=a[t].isFile()?4:a[t].isDirectory()?3:a[t].isSymbolicLink()?7:a[t].isCharacterDevice()?2:a[t].isBlockDevice()?1:a[t].isSocket()?6:0,l.setUint8(20,d),u.set(r,24);const _=u.slice(0,Math.min(u.length,n-f));c.set(_,e+f),f+=_.byteLength}return u.setUint32(i,f,!0),0})),this.fd_renumber=K("fd_renumber",(function(t,e){return C.get(this).fds.renumber(e,t),0})),this.fd_sync=K("fd_sync",(function(t){const e=C.get(this).fds.get(t,l.FD_SYNC,BigInt(0));return W(this).fsyncSync(e.fd),0})),this.fd_tell=K("fd_tell",(function(t,e){const n=C.get(this).fds.get(t,l.FD_TELL,BigInt(0)),r=BigInt(n.pos),{view:i}=M(this);return i.setBigUint64(Number(e),r,!0),0})),this.fd_write=K("fd_write",(function(t,e,n,r){if(e=Number(e),r=Number(r),0===e||0===r)return 28;const{HEAPU8:i,view:s}=M(this),o=C.get(this).fds.get(t,l.FD_WRITE,BigInt(0)),a=N(Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,o=s.getInt32(r,!0),a=s.getUint32(r+4,!0);return i.subarray(o,o+a)})));let c;if(1===t||2===t)c=o.write(a);else{c=W(this).writeSync(o.fd,a,0,a.length,Number(o.pos)),o.pos+=BigInt(c)}return s.setUint32(r,c,!0),0})),this.path_create_directory=K("path_create_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=M(this),i=C.get(this).fds.get(t,l.PATH_CREATE_DIRECTORY,BigInt(0));let s=Y.decode(r.subarray(e,e+n));s=h(i.realPath,s);return W(this).mkdirSync(s),0})),this.path_filestat_get=K("path_filestat_get",(function(t,e,n,r,i){if(n=Number(n),r=Number(r),i=Number(i),0===n||0===i)return 28;const{HEAPU8:s,view:o}=M(this),a=C.get(this).fds.get(t,l.PATH_FILESTAT_GET,BigInt(0));let c=Y.decode(s.subarray(n,n+r));const u=W(this);let f;return c=h(a.realPath,c),f=1==(1&e)?u.statSync(c,{bigint:!0}):u.lstatSync(c,{bigint:!0}),R(o,i,f),0})),this.path_filestat_set_times=K("path_filestat_set_times",(function(t,e,n,r,i,s,o){if(n=Number(n),r=Number(r),0===n)return 28;if(-16&o)return 28;const{HEAPU8:a}=M(this),c=C.get(this).fds.get(t,l.PATH_FILESTAT_SET_TIMES,BigInt(0)),u=W(this),f=j(u,c,Y.decode(a.subarray(n,n+r)),e);return 2==(2&o)&&(i=BigInt(1e6*Date.now())),8==(8&o)&&(s=BigInt(1e6*Date.now())),u.utimesSync(f,Number(i),Number(s)),0})),this.path_link=K("path_link",(function(t,e,n,r,i,s,o){if(n=Number(n),r=Number(r),s=Number(s),o=Number(o),0===n||0===s)return 28;const a=C.get(this);let c,u;t===i?c=u=a.fds.get(t,l.PATH_LINK_SOURCE|l.PATH_LINK_TARGET,BigInt(0)):(c=a.fds.get(t,l.PATH_LINK_SOURCE,BigInt(0)),u=a.fds.get(i,l.PATH_LINK_TARGET,BigInt(0)));const{HEAPU8:f}=M(this),d=W(this),_=j(d,c,Y.decode(f.subarray(n,n+r)),e),g=h(u.realPath,Y.decode(f.subarray(s,s+o)));return d.linkSync(_,g),0})),this.path_open=K("path_open",(function(t,e,n,r,i,s,o,a,c){if(n=Number(n),c=Number(c),0===n||0===c)return 28;r=Number(r),s=BigInt(s);const u=((s=BigInt(s))&(l.FD_READ|l.FD_READDIR))!==BigInt(0),f=(s&(l.FD_DATASYNC|l.FD_WRITE|l.FD_ALLOCATE|l.FD_FILESTAT_SET_SIZE))!==BigInt(0);let h=f?u?2:1:0,d=l.PATH_OPEN,_=s|o;0!=(1&i)&&(h|=64,d|=l.PATH_CREATE_FILE),0!=(2&i)&&(h|=65536),0!=(4&i)&&(h|=128),0!=(8&i)&&(h|=512,d|=l.PATH_FILESTAT_SET_SIZE),0!=(1&a)&&(h|=1024),0!=(2&a)&&(_|=l.FD_DATASYNC),0!=(4&a)&&(h|=2048),0!=(8&a)&&(h|=1052672,_|=l.FD_SYNC),0!=(16&a)&&(h|=1052672,_|=l.FD_SYNC),f&&0==(1536&h)&&(_|=l.FD_SEEK);const g=C.get(this),E=g.fds.get(t,d,_),y=M(this),p=y.HEAPU8,I=Y.decode(p.subarray(n,n+r)),T=W(this),A=j(T,E,I,e),m=T.openSync(A,h,438),b=g.fds.getFileTypeByFd(m);if(0!=(2&i)&&3!==b)return 54;const{base:w,inheriting:S}=D(g.fds.stdio,m,h,b),B=g.fds.insert(m,A,A,b,s&w,o&S,0),N=T.fstatSync(m,{bigint:!0});N.isFile()&&(B.size=N.size,0!=(1024&h)&&(B.pos=N.size));return y.view.setInt32(c,B.id,!0),0})),this.path_readlink=K("path_readlink",(function(t,e,n,r,i,s){if(e=Number(e),n=Number(n),r=Number(r),i=Number(i),s=Number(s),0===e||0===r||0===s)return 28;const{HEAPU8:o,view:a}=M(this),c=C.get(this).fds.get(t,l.PATH_READLINK,BigInt(0));let u=Y.decode(o.subarray(e,e+n));u=h(c.realPath,u);const f=W(this).readlinkSync(u),d=G.encode(f),_=Math.min(d.length,i);return _>=i?42:(o.set(d.subarray(0,_),r),o[r+_]=0,a.setUint32(s,_+1,!0),0)})),this.path_remove_directory=K("path_remove_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=M(this),i=C.get(this).fds.get(t,l.PATH_REMOVE_DIRECTORY,BigInt(0));let s=Y.decode(r.subarray(e,e+n));s=h(i.realPath,s);return W(this).rmdirSync(s),0})),this.path_rename=K("path_rename",(function(t,e,n,r,i,s){if(e=Number(e),n=Number(n),i=Number(i),s=Number(s),0===e||0===i)return 28;const o=C.get(this);let a,c;t===r?a=c=o.fds.get(t,l.PATH_RENAME_SOURCE|l.PATH_RENAME_TARGET,BigInt(0)):(a=o.fds.get(t,l.PATH_RENAME_SOURCE,BigInt(0)),c=o.fds.get(r,l.PATH_RENAME_TARGET,BigInt(0)));const{HEAPU8:u}=M(this),f=h(a.realPath,Y.decode(u.subarray(e,e+n))),d=h(c.realPath,Y.decode(u.subarray(i,i+s)));return W(this).renameSync(f,d),0})),this.path_symlink=K("path_symlink",(function(t,e,n,r,i){if(t=Number(t),e=Number(e),r=Number(r),i=Number(i),0===t||0===r)return 28;const{HEAPU8:s}=M(this),o=C.get(this).fds.get(n,l.PATH_SYMLINK,BigInt(0)),a=Y.decode(s.subarray(t,t+e));let c=Y.decode(s.subarray(r,r+i));c=h(o.realPath,c);return W(this).symlinkSync(a,c),0})),this.path_unlink_file=K("path_unlink_file",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=M(this),i=C.get(this).fds.get(t,l.PATH_UNLINK_FILE,BigInt(0));let s=Y.decode(r.subarray(e,e+n));s=h(i.realPath,s);return W(this).unlinkSync(s),0})),this.poll_oneoff=K("poll_oneoff",(function(t,e,n,r){return 52})),this.proc_exit=K("proc_exit",(function(t){return 0})),this.proc_raise=K("proc_raise",(function(t){return 52})),this.sched_yield=K("sched_yield",(function(){return 0})),this.random_get="undefined"!=typeof crypto&&"function"==typeof crypto.getRandomValues?K("random_get",(function(t,e){if(0===(t=Number(t)))return 28;e=Number(e);const{HEAPU8:n}=M(this);let r;const i=65536;for(r=0;r+i<e;r+=i)crypto.getRandomValues(n.subarray(t+r,t+r+i));return crypto.getRandomValues(n.subarray(t+r,t+e)),0})):K("random_get",(function(t,e){if(0===(t=Number(t)))return 28;e=Number(e);const{view:n}=M(this);for(let r=t;r<t+e;++r)n.setUint8(r,Math.floor(256*Math.random()));return 0})),this.sock_recv=K("sock_recv",(function(){return 58})),this.sock_send=K("sock_send",(function(){return 58})),this.sock_shutdown=K("sock_shutdown",(function(){return 58}));const a=i?i.fs:void 0,c=new L({size:3,in:r[0],out:r[1],err:r[2],fs:a,print:s,printErr:o});if(C.set(this,{fds:c,args:t,env:e}),a&&x.set(this,a),n.length>0)for(let t=0;t<n.length;++t){const e=a.realpathSync(n[t].realPath,"utf8"),r=a.openSync(e,"r",438);c.insertPreopen(r,n[t].mappedPath,e)}}}const V=Object.freeze(Object.create(null)),Z=Symbol("kExitCode"),q=Symbol("kSetMemory"),Q=Symbol("kStarted"),X=Symbol("kInstance");function J(t,n){e(n,"instance"),e(n.exports,"instance.exports"),t[X]=n,t[q](n.exports.memory)}function tt(t){throw this[Z]=t,Z}t.Asyncify=c,t.Memory=H,t.WASI=class{constructor(t=V){var i;e(t,"options"),void 0!==t.args&&function(t,e){if(!Array.isArray(t))throw new TypeError(`${e} must be an array. Received ${null===t?"null":typeof t}`)}(t.args,"options.args");const s=(null!==(i=t.args)&&void 0!==i?i:[]).map(String),o=[];void 0!==t.env&&(e(t.env,"options.env"),Object.entries(t.env).forEach((({0:t,1:e})=>{void 0!==e&&o.push(`${t}=${e}`)})));const a=[];if(void 0!==t.preopens&&(e(t.preopens,"options.preopens"),Object.entries(t.preopens).forEach((({0:t,1:e})=>a.push({mappedPath:String(t),realPath:String(e)})))),a.length>0&&void 0===t.filesystem)throw new Error("filesystem is disabled, can not preopen directory");if(void 0!==t.filesystem){if(e(t.filesystem,"options.filesystem"),n(t.filesystem.type,"options.filesystem.type"),"memfs"!==t.filesystem.type)throw new Error(`Filesystem type ${t.filesystem.type} is not supported, only "memfs" is supported currently`);try{e(t.filesystem.fs,"options.filesystem.fs")}catch(t){throw new Error("Node.js fs like implementation is not provided")}}void 0!==t.print&&r(t.print,"options.print"),void 0!==t.printErr&&r(t.printErr,"options.printErr");const c=new $(s,o,a,[0,1,2],t.filesystem,t.print,t.printErr);for(const t in c)c[t]=c[t].bind(c);void 0!==t.returnOnExit&&(!function(t,e){if("boolean"!=typeof t)throw new TypeError(`${e} must be a boolean. Received ${null===t?"null":typeof t}`)}(t.returnOnExit,"options.returnOnExit"),t.returnOnExit&&(c.proc_exit=tt.bind(this))),this[q]=c._setMemory,delete c._setMemory,this.wasiImport=c,this[Q]=!1,this[Z]=0,this[X]=void 0}start(t){if(this[Q])throw new Error("WASI instance has already started");this[Q]=!0,J(this,t);const{_start:e,_initialize:n}=this[X].exports;let s;r(e,"instance.exports._start"),i(n,"instance.exports._initialize");try{s=e()}catch(t){if(t!==Z)throw t}return s instanceof Promise?s.then((()=>this[Z]),(t=>{if(t!==Z)throw t;return this[Z]})):this[Z]}initialize(t){if(this[Q])throw new Error("WASI instance has already started");this[Q]=!0,J(this,t);const{_start:e,_initialize:n}=this[X].exports;if(i(e,"instance.exports._start"),void 0!==n)return r(n,"instance.exports._initialize"),n()}},t.extendMemory=U,t.load=async function(t,e,n){var r,i;if(e&&"object"!=typeof e)throw new TypeError("imports must be an object or undefined");let s,o;if(e=null!=e?e:{},n&&(s=new c,e=s.wrapImports(e)),t instanceof ArrayBuffer||ArrayBuffer.isView(t)){if(o=await WebAssembly.instantiate(t,e),n){const t=o.instance.exports.memory||(null===(r=e.env)||void 0===r?void 0:r.memory);return{module:o.module,instance:s.init(t,o.instance,n)}}return o}if("string"!=typeof t&&!(t instanceof URL))throw new TypeError("Invalid source");if("function"==typeof WebAssembly.instantiateStreaming)try{o=await WebAssembly.instantiateStreaming(fetch(t),e)}catch(n){o=await u(t,e)}else o=await u(t,e);if(n){const t=o.instance.exports.memory||(null===(i=e.env)||void 0===i?void 0:i.memory);return{module:o.module,instance:s.init(t,o.instance,n)}}return o},t.loadSync=function(t,e,n){var r;if(t instanceof ArrayBuffer&&!ArrayBuffer.isView(t))throw new TypeError("Invalid source");if(e&&"object"!=typeof e)throw new TypeError("imports must be an object or undefined");let i;e=null!=e?e:{},n&&(i=new c,e=i.wrapImports(e));const s=new WebAssembly.Module(t),o=new WebAssembly.Instance(s,e),a={instance:o,module:s};if(n){const t=a.instance.exports.memory||(null===(r=e.env)||void 0===r?void 0:r.memory);return{module:a.module,instance:i.init(t,o,n)}}return a},Object.defineProperty(t,"__esModule",{value:!0})}));
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).wasmUtil={})}(this,(function(t){"use strict";const e="undefined"!=typeof WebAssembly?WebAssembly:"undefined"!=typeof WXWebAssembly?WXWebAssembly:void 0;if(!e)throw new Error("WebAssembly is not supported in this environment");function n(t,e){if(null===t||"object"!=typeof t)throw new TypeError(`${e} must be an object. Received ${null===t?"null":typeof t}`)}function r(t,e){if("string"!=typeof t)throw new TypeError(`${e} must be a string. Received ${null===t?"null":typeof t}`)}function i(t,e){if("function"!=typeof t)throw new TypeError(`${e} must be a function. Received ${null===t?"null":typeof t}`)}function s(t,e){if(void 0!==t)throw new TypeError(`${e} must be undefined. Received ${null===t?"null":typeof t}`)}function o(t){return!(!t||"object"!=typeof t&&"function"!=typeof t||"function"!=typeof t.then)}function a(t,e){const n=Object.create(null);return Object.keys(t).forEach((r=>{const i=t[r];Object.defineProperty(n,r,{enumerable:!0,value:e(i,r)})})),n}const u=["asyncify_get_state","asyncify_start_rewind","asyncify_start_unwind","asyncify_stop_rewind","asyncify_stop_unwind"];function c(t,e,n,r){if("function"!=typeof t.exports[r]||n<=0)return{wasm64:e,dataPtr:16,start:e?32:24,end:1024};const i=t.exports[r],s=e?Number(i(BigInt(16)+BigInt(n))):i(8+n);if(0===s)throw new Error("Allocate asyncify data failed");return e?{wasm64:e,dataPtr:s,start:s+16,end:s+16+n}:{wasm64:e,dataPtr:s,start:s+8,end:s+8+n}}class f{constructor(){this.value=void 0,this.exports=void 0,this.dataPtr=0}init(t,n,r){var i,s;if(this.exports)throw new Error("Asyncify has been initialized");if(!(t instanceof e.Memory))throw new TypeError("Require WebAssembly.Memory object");const o=n.exports;for(let t=0;t<u.length;++t)if("function"!=typeof o[u[t]])throw new TypeError("Invalid asyncify wasm");let a;const f=Boolean(r.wasm64);a=r.tryAllocate?!0===r.tryAllocate?c(n,f,4096,"malloc"):c(n,f,null!==(i=r.tryAllocate.size)&&void 0!==i?i:4096,null!==(s=r.tryAllocate.name)&&void 0!==s?s:"malloc"):{wasm64:f,dataPtr:16,start:f?32:24,end:1024},this.dataPtr=a.dataPtr,f?new BigInt64Array(t.buffer,this.dataPtr).set([BigInt(a.start),BigInt(a.end)]):new Int32Array(t.buffer,this.dataPtr).set([a.start,a.end]),this.exports=this.wrapExports(o,r.wrapExports);const h=Object.create(e.Instance.prototype);return Object.defineProperty(h,"exports",{value:this.exports}),h}assertState(){if(0!==this.exports.asyncify_get_state())throw new Error("Asyncify state error")}wrapImportFunction(t){return(...e)=>{for(;2===this.exports.asyncify_get_state();)return this.exports.asyncify_stop_rewind(),this.value;this.assertState();const n=t(...e);if(!o(n))return n;this.exports.asyncify_start_unwind(this.dataPtr),this.value=n}}wrapImports(t){const e={};return Object.keys(t).forEach((n=>{const r=t[n],i={};Object.keys(r).forEach((t=>{const e=r[t];i[t]="function"==typeof e?this.wrapImportFunction(e):e})),e[n]=i})),e}wrapExportFunction(t){return async(...e)=>{this.assertState();let n=t(...e);for(;1===this.exports.asyncify_get_state();)this.exports.asyncify_stop_unwind(),this.value=await this.value,this.assertState(),this.exports.asyncify_start_rewind(this.dataPtr),n=t();return this.assertState(),n}}wrapExports(t,e){return a(t,((t,n)=>{let r=-1!==u.indexOf(n)||"function"!=typeof t;return Array.isArray(e)&&(r=r||-1===e.indexOf(n)),r?t:this.wrapExportFunction(t)}))}}async function h(t,n){if("undefined"!=typeof wx&&"undefined"!=typeof __wxConfig)return await e.instantiate(t,n);const r=await fetch(t),i=await r.arrayBuffer();return await e.instantiate(i,n)}function d(t){return 47===t}function l(...t){let e="",n=!1;for(let i=t.length-1;i>=-1&&!n;i--){const s=i>=0?t[i]:"/";r(s,"path"),0!==s.length&&(e=`${s}/${e}`,n=47===s.charCodeAt(0))}return e=function(t,e,n,r){let i="",s=0,o=-1,a=0,u=0;for(let c=0;c<=t.length;++c){if(c<t.length)u=t.charCodeAt(c);else{if(r(u))break;u=47}if(r(u)){if(o===c-1||1===a);else if(2===a){if(i.length<2||2!==s||46!==i.charCodeAt(i.length-1)||46!==i.charCodeAt(i.length-2)){if(i.length>2){const t=i.indexOf(n);-1===t?(i="",s=0):(i=i.slice(0,t),s=i.length-1-i.indexOf(n)),o=c,a=0;continue}if(0!==i.length){i="",s=0,o=c,a=0;continue}}e&&(i+=i.length>0?`${n}..`:"..",s=2)}else i.length>0?i+=`${n}${t.slice(o+1,c)}`:i=t.slice(o+1,c),s=c-o-1;o=c,a=0}else 46===u&&-1!==a?++a:a=-1}return i}(e,!n,"/",d),n?`/${e}`:e.length>0?e:"."}const _={FD_DATASYNC:BigInt(1)<<BigInt(0),FD_READ:BigInt(1)<<BigInt(1),FD_SEEK:BigInt(1)<<BigInt(2),FD_FDSTAT_SET_FLAGS:BigInt(1)<<BigInt(3),FD_SYNC:BigInt(1)<<BigInt(4),FD_TELL:BigInt(1)<<BigInt(5),FD_WRITE:BigInt(1)<<BigInt(6),FD_ADVISE:BigInt(1)<<BigInt(7),FD_ALLOCATE:BigInt(1)<<BigInt(8),PATH_CREATE_DIRECTORY:BigInt(1)<<BigInt(9),PATH_CREATE_FILE:BigInt(1)<<BigInt(10),PATH_LINK_SOURCE:BigInt(1)<<BigInt(11),PATH_LINK_TARGET:BigInt(1)<<BigInt(12),PATH_OPEN:BigInt(1)<<BigInt(13),FD_READDIR:BigInt(1)<<BigInt(14),PATH_READLINK:BigInt(1)<<BigInt(15),PATH_RENAME_SOURCE:BigInt(1)<<BigInt(16),PATH_RENAME_TARGET:BigInt(1)<<BigInt(17),PATH_FILESTAT_GET:BigInt(1)<<BigInt(18),PATH_FILESTAT_SET_SIZE:BigInt(1)<<BigInt(19),PATH_FILESTAT_SET_TIMES:BigInt(1)<<BigInt(20),FD_FILESTAT_GET:BigInt(1)<<BigInt(21),FD_FILESTAT_SET_SIZE:BigInt(1)<<BigInt(22),FD_FILESTAT_SET_TIMES:BigInt(1)<<BigInt(23),PATH_SYMLINK:BigInt(1)<<BigInt(24),PATH_REMOVE_DIRECTORY:BigInt(1)<<BigInt(25),PATH_UNLINK_FILE:BigInt(1)<<BigInt(26),POLL_FD_READWRITE:BigInt(1)<<BigInt(27),SOCK_SHUTDOWN:BigInt(1)<<BigInt(28),SOCK_ACCEPT:BigInt(1)<<BigInt(29)};class g extends Error{constructor(t,e){super(t),this.errno=e}getErrorMessage(){return function(t){switch(t){case 0:return"Success";case 1:return"Argument list too long";case 2:return"Permission denied";case 3:return"Address in use";case 4:return"Address not available";case 5:return"Address family not supported by protocol";case 6:return"Resource temporarily unavailable";case 7:return"Operation already in progress";case 8:return"Bad file descriptor";case 9:return"Bad message";case 10:return"Resource busy";case 11:return"Operation canceled";case 12:return"No child process";case 13:return"Connection aborted";case 14:return"Connection refused";case 15:return"Connection reset by peer";case 16:return"Resource deadlock would occur";case 17:return"Destination address required";case 18:return"Domain error";case 19:return"Quota exceeded";case 20:return"File exists";case 21:return"Bad address";case 22:return"File too large";case 23:return"Host is unreachable";case 24:return"Identifier removed";case 25:return"Illegal byte sequence";case 26:return"Operation in progress";case 27:return"Interrupted system call";case 28:return"Invalid argument";case 29:return"I/O error";case 30:return"Socket is connected";case 31:return"Is a directory";case 32:return"Symbolic link loop";case 33:return"No file descriptors available";case 34:return"Too many links";case 35:return"Message too large";case 36:return"Multihop attempted";case 37:return"Filename too long";case 38:return"Network is down";case 39:return"Connection reset by network";case 40:return"Network unreachable";case 41:return"Too many files open in system";case 42:return"No buffer space available";case 43:return"No such device";case 44:return"No such file or directory";case 45:return"Exec format error";case 46:return"No locks available";case 47:return"Link has been severed";case 48:return"Out of memory";case 49:return"No message of the desired type";case 50:return"Protocol not available";case 51:return"No space left on device";case 52:return"Function not implemented";case 53:return"Socket not connected";case 54:return"Not a directory";case 55:return"Directory not empty";case 56:return"State not recoverable";case 57:return"Not a socket";case 58:return"Not supported";case 59:return"Not a tty";case 60:return"No such device or address";case 61:return"Value too large for data type";case 62:return"Previous owner died";case 63:return"Operation not permitted";case 64:return"Broken pipe";case 65:return"Protocol error";case 66:return"Protocol not supported";case 67:return"Protocol wrong type for socket";case 68:return"Result not representable";case 69:return"Read-only file system";case 70:return"Invalid seek";case 71:return"No such process";case 72:return"Stale file handle";case 73:return"Operation timed out";case 74:return"Text file busy";case 75:return"Cross-device link";case 76:return"Capabilities insufficient";default:return"Unknown error"}}(this.errno)}}Object.defineProperty(g.prototype,"name",{configurable:!0,writable:!0,value:"WasiError"});const p=_.FD_DATASYNC|_.FD_READ|_.FD_SEEK|_.FD_FDSTAT_SET_FLAGS|_.FD_SYNC|_.FD_TELL|_.FD_WRITE|_.FD_ADVISE|_.FD_ALLOCATE|_.PATH_CREATE_DIRECTORY|_.PATH_CREATE_FILE|_.PATH_LINK_SOURCE|_.PATH_LINK_TARGET|_.PATH_OPEN|_.FD_READDIR|_.PATH_READLINK|_.PATH_RENAME_SOURCE|_.PATH_RENAME_TARGET|_.PATH_FILESTAT_GET|_.PATH_FILESTAT_SET_SIZE|_.PATH_FILESTAT_SET_TIMES|_.FD_FILESTAT_GET|_.FD_FILESTAT_SET_TIMES|_.FD_FILESTAT_SET_SIZE|_.PATH_SYMLINK|_.PATH_UNLINK_FILE|_.PATH_REMOVE_DIRECTORY|_.POLL_FD_READWRITE|_.SOCK_SHUTDOWN,E=p,y=p,I=p,T=p,A=_.FD_DATASYNC|_.FD_READ|_.FD_SEEK|_.FD_FDSTAT_SET_FLAGS|_.FD_SYNC|_.FD_TELL|_.FD_WRITE|_.FD_ADVISE|_.FD_ALLOCATE|_.FD_FILESTAT_GET|_.FD_FILESTAT_SET_SIZE|_.FD_FILESTAT_SET_TIMES|_.POLL_FD_READWRITE,m=BigInt(0),b=_.FD_FDSTAT_SET_FLAGS|_.FD_SYNC|_.FD_ADVISE|_.PATH_CREATE_DIRECTORY|_.PATH_CREATE_FILE|_.PATH_LINK_SOURCE|_.PATH_LINK_TARGET|_.PATH_OPEN|_.FD_READDIR|_.PATH_READLINK|_.PATH_RENAME_SOURCE|_.PATH_RENAME_TARGET|_.PATH_FILESTAT_GET|_.PATH_FILESTAT_SET_SIZE|_.PATH_FILESTAT_SET_TIMES|_.FD_FILESTAT_GET|_.FD_FILESTAT_SET_TIMES|_.PATH_SYMLINK|_.PATH_UNLINK_FILE|_.PATH_REMOVE_DIRECTORY|_.POLL_FD_READWRITE,w=b|A,S=_.FD_READ|_.FD_FDSTAT_SET_FLAGS|_.FD_WRITE|_.FD_FILESTAT_GET|_.POLL_FD_READWRITE|_.SOCK_SHUTDOWN,B=p,F=_.FD_READ|_.FD_FDSTAT_SET_FLAGS|_.FD_WRITE|_.FD_FILESTAT_GET|_.POLL_FD_READWRITE,D=BigInt(0);function N(t,e,n,r){const i={base:BigInt(0),inheriting:BigInt(0)};if(0===r)throw new g("Unknown file type",28);switch(r){case 4:i.base=A,i.inheriting=m;break;case 3:i.base=b,i.inheriting=w;break;case 6:case 5:i.base=S,i.inheriting=B;break;case 2:-1!==t.indexOf(e)?(i.base=F,i.inheriting=D):(i.base=I,i.inheriting=T);break;case 1:i.base=E,i.inheriting=y;break;default:i.base=BigInt(0),i.inheriting=BigInt(0)}const s=3&n;return 0===s?i.base&=~_.FD_WRITE:1===s&&(i.base&=~_.FD_READ),i}function P(t,e){let n=0;if("number"==typeof e&&e>=0)n=e;else for(let e=0;e<t.length;e++){n+=t[e].length}let r=0;const i=new Uint8Array(n);for(let e=0;e<t.length;e++){const n=t[e];i.set(n,r),r+=n.length}return i}class v{constructor(t,e,n,r,i,s,o,a){this.id=t,this.fd=e,this.path=n,this.realPath=r,this.type=i,this.rightsBase=s,this.rightsInheriting=o,this.preopen=a,this.pos=BigInt(0),this.size=BigInt(0)}seek(t,e){if(0===e)this.pos=BigInt(t);else if(1===e)this.pos+=BigInt(t);else{if(2!==e)throw new g("Unknown whence",29);this.pos=BigInt(this.size)-BigInt(t)}return this.pos}}class R extends v{constructor(t,e,n,r,i,s,o,a,u){super(e,n,r,i,s,o,a,u),this._log=t,this._buf=null}write(t){const e=t;if(this._buf&&(t=P([this._buf,t]),this._buf=null),-1===t.indexOf(10))return this._buf=t,e.byteLength;let n,r=0,i=0;for(;-1!==(n=t.indexOf(10,r));){const e=(new TextDecoder).decode(t.subarray(i,n));this._log(e),r+=n-i+1,i=n+1}return r<t.length&&(this._buf=t.slice(r)),e.byteLength}}function L(t){return t.isBlockDevice()?1:t.isCharacterDevice()?2:t.isDirectory()?3:t.isSocket()?6:t.isFile()?4:t.isSymbolicLink()?7:0}function H(t,e,n){t.setBigUint64(e,n.dev,!0),t.setBigUint64(e+8,n.ino,!0),t.setBigUint64(e+16,BigInt(L(n)),!0),t.setBigUint64(e+24,n.nlink,!0),t.setBigUint64(e+32,n.size,!0),t.setBigUint64(e+40,n.atimeMs*BigInt(1e6),!0),t.setBigUint64(e+48,n.mtimeMs*BigInt(1e6),!0),t.setBigUint64(e+56,n.ctimeMs*BigInt(1e6),!0)}class U{constructor(t){this.used=0,this.size=t.size,this.fds=Array(t.size),this.stdio=[t.in,t.out,t.err],this.fs=t.fs,this.print=t.print,this.printErr=t.printErr,this.insertStdio(t.in,0,"<stdin>"),this.insertStdio(t.out,1,"<stdout>"),this.insertStdio(t.err,2,"<stderr>")}insertStdio(t,e,n){const{base:r,inheriting:i}=N(this.stdio,t,2,2),s=this.insert(t,n,n,2,r,i,0);if(s.id!==e)throw new g(`id: ${s.id} !== expected: ${e}`,8);return s}insert(t,e,n,r,i,s,o){var a,u;let c,f=-1;if(this.used>=this.size){const t=2*this.size;this.fds.length=t,f=this.size,this.size=t}else for(let t=0;t<this.size;++t)if(null==this.fds[t]){f=t;break}return c="<stdout>"===e?new R(null!==(a=this.print)&&void 0!==a?a:console.log,f,t,e,n,r,i,s,o):"<stderr>"===e?new R(null!==(u=this.printErr)&&void 0!==u?u:console.error,f,t,e,n,r,i,s,o):new v(f,t,e,n,r,i,s,o),this.fds[f]=c,this.used++,c}getFileTypeByFd(t){return L(this.fs.fstatSync(t))}insertPreopen(t,e,n){const r=this.getFileTypeByFd(t);if(3!==r)throw new g(`Preopen not dir: ["${e}", "${n}"]`,54);const i=N(this.stdio,t,0,r);return this.insert(t,e,n,r,i.base,i.inheriting,1)}get(t,e,n){if(t>=this.size)throw new g("Invalid fd",8);const r=this.fds[t];if(!r||r.id!==t)throw new g("Bad file descriptor",8);if((~r.rightsBase&e)!==BigInt(0)||(~r.rightsInheriting&n)!==BigInt(0))throw new g("Capabilities insufficient",76);return r}remove(t){if(t>=this.size)throw new g("Invalid fd",8);const e=this.fds[t];if(!e||e.id!==t)throw new g("Bad file descriptor",8);this.fds[t]=void 0,this.used--}renumber(t,e){if(t===e)return;if(t>=this.size||e>=this.size)throw new g("Invalid fd",8);const n=this.fds[t],r=this.fds[e];if(!n||!r||n.id!==t||r.id!==e)throw new g("Invalid fd",8);this.fs.closeSync(n.fd),this.fds[t]=this.fds[e],this.fds[t].id=t,this.fds[e]=void 0,this.used--}}const O=e.Memory;class k extends O{constructor(t){super(t)}get HEAP8(){return new Int8Array(super.buffer)}get HEAPU8(){return new Uint8Array(super.buffer)}get HEAP16(){return new Int16Array(super.buffer)}get HEAPU16(){return new Uint16Array(super.buffer)}get HEAP32(){return new Int32Array(super.buffer)}get HEAPU32(){return new Uint32Array(super.buffer)}get HEAP64(){return new BigInt64Array(super.buffer)}get HEAPU64(){return new BigUint64Array(super.buffer)}get HEAPF32(){return new Float32Array(super.buffer)}get HEAPF64(){return new Float64Array(super.buffer)}get view(){return new DataView(super.buffer)}}function x(t){return Object.getPrototypeOf(t)===e.Memory.prototype&&Object.setPrototypeOf(t,k.prototype),t}function C(t,e){if(0===t.length||0===e.length)return 0;let n=0,r=e.length-n;for(let i=0;i<t.length;++i){const s=t[i];if(r<s.length)return s.set(e.subarray(n,n+r),0),n+=r,r=0,n;s.set(e.subarray(n,n+s.length),0),n+=s.length,r-=s.length}return n}const M=new WeakMap,W=new WeakMap,z=new WeakMap;function K(t){return M.get(t)}function j(t){const e=z.get(t);if(!e)throw new Error("filesystem is unavailable");return e}function G(t){if(t instanceof g)return t.errno;switch(t.code){case"ENOENT":return 44;case"EBADF":return 8;case"EINVAL":return 28;case"EPERM":return 63;case"EPROTO":return 65;case"EEXIST":return 20;case"ENOTDIR":return 54;case"EMFILE":return 33;case"EACCES":return 2;case"EISDIR":return 31;case"ENOTEMPTY":return 55;case"ENOSYS":return 52}throw t}function Y(t,e){return function(t,e){return Object.defineProperty(e,"name",{value:t}),e}(t,(function(){let t;try{t=e.apply(this,arguments)}catch(t){return G(t)}return o(t)?t.then((t=>t),G):t}))}function $(t,e,n,r){let i=l(e.realPath,n);if(1==(1&r))try{i=t.readlinkSync(i)}catch(t){if("EINVAL"!==t.code&&"ENOENT"!==t.code)throw t}return i}const V=new TextEncoder,Z=new TextDecoder;class q{constructor(t,n,r,i,s,o,a){this._setMemory=function(t){if(!(t instanceof e.Memory))throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');M.set(this,x(t))},this.args_get=Y("args_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{HEAPU8:n,view:r}=K(this),i=W.get(this).args;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=V.encode(o+"\0");n.set(a,e),e+=a.length}return 0})),this.args_sizes_get=Y("args_sizes_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{view:n}=K(this),r=W.get(this).args;return n.setUint32(t,r.length,!0),n.setUint32(e,V.encode(r.join("\0")+"\0").length,!0),0})),this.environ_get=Y("environ_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{HEAPU8:n,view:r}=K(this),i=W.get(this).env;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=V.encode(o+"\0");n.set(a,e),e+=a.length}return 0})),this.environ_sizes_get=Y("environ_sizes_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{view:n}=K(this),r=W.get(this);return n.setUint32(t,r.env.length,!0),n.setUint32(e,V.encode(r.env.join("\0")+"\0").length,!0),0})),this.clock_res_get=Y("clock_res_get",(function(t,e){if(0===(e=Number(e)))return 28;const{view:n}=K(this);switch(t){case 0:return n.setBigUint64(e,BigInt(1e6),!0),0;case 1:case 2:case 3:return n.setBigUint64(e,BigInt(1e3),!0),0;default:return 28}})),this.clock_time_get=Y("clock_time_get",(function(t,e,n){if(0===(n=Number(n)))return 28;const{view:r}=K(this);switch(t){case 0:return r.setBigUint64(n,BigInt(Date.now())*BigInt(1e6),!0),0;case 1:case 2:case 3:{const t=performance.now(),e=Math.trunc(t),i=Math.floor(1e3*(t-e)),s=BigInt(e)*BigInt(1e9)+BigInt(i)*BigInt(1e6);return r.setBigUint64(n,s,!0),0}default:return 28}})),this.fd_advise=Y("fd_advise",(function(t,e,n,r){return 52})),this.fd_allocate=Y("fd_allocate",(function(t,e,n){const r=W.get(this),i=j(this),s=r.fds.get(t,_.FD_ALLOCATE,BigInt(0));return i.fstatSync(s.fd,{bigint:!0}).size<e+n&&i.truncateSync(s.fd,Number(e+n)),0})),this.fd_close=Y("fd_close",(function(t){const e=W.get(this),n=e.fds.get(t,BigInt(0),BigInt(0));return j(this).closeSync(n.fd),e.fds.remove(t),0})),this.fd_datasync=Y("fd_datasync",(function(t){const e=W.get(this).fds.get(t,_.FD_DATASYNC,BigInt(0));return j(this).fdatasyncSync(e.fd),0})),this.fd_fdstat_get=Y("fd_fdstat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=W.get(this).fds.get(t,BigInt(0),BigInt(0)),{view:r}=K(this);return r.setUint16(e,n.type,!0),r.setUint16(e+2,0,!0),r.setBigUint64(e+8,n.rightsBase,!0),r.setBigUint64(e+16,n.rightsInheriting,!0),0})),this.fd_fdstat_set_flags=Y("fd_fdstat_set_flags",(function(t,e){return 52})),this.fd_fdstat_set_rights=Y("fd_fdstat_set_rights",(function(t,e,n){const r=W.get(this).fds.get(t,BigInt(0),BigInt(0));return(e|r.rightsBase)>r.rightsBase||(n|r.rightsInheriting)>r.rightsInheriting?76:(r.rightsBase=e,r.rightsInheriting=n,0)})),this.fd_filestat_get=Y("fd_filestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=W.get(this).fds.get(t,_.FD_FILESTAT_GET,BigInt(0)),r=j(this).fstatSync(n.fd,{bigint:!0}),{view:i}=K(this);return H(i,e,r),0})),this.fd_filestat_set_size=Y("fd_filestat_set_size",(function(t,e){const n=W.get(this).fds.get(t,_.FD_FILESTAT_SET_SIZE,BigInt(0));return j(this).ftruncateSync(n.fd,Number(e)),0})),this.fd_filestat_set_times=Y("fd_filestat_set_times",(function(t,e,n,r){const i=W.get(this).fds.get(t,_.FD_FILESTAT_SET_TIMES,BigInt(0));2==(2&r)&&(e=BigInt(1e6*Date.now())),8==(8&r)&&(n=BigInt(1e6*Date.now()));return j(this).futimesSync(i.fd,Number(e),Number(n)),0})),this.fd_pread=Y("fd_pread",(function(t,e,n,r,i){if(e=Number(e),i=Number(i),0===e||0===i)return 28;const{HEAPU8:s,view:o}=K(this),a=W.get(this).fds.get(t,_.FD_READ|_.FD_SEEK,BigInt(0));let u=0;const c=Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,i=o.getInt32(r,!0),a=o.getUint32(r+4,!0);return u+=a,s.subarray(i,i+a)}));let f=0;const h=new Uint8Array(u);h._isBuffer=!0;const d=j(this).readSync(a.fd,h,0,h.length,Number(r));return f=h?C(c,h.subarray(0,d)):0,o.setUint32(i,f,!0),0})),this.fd_prestat_get=Y("fd_prestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=W.get(this);let r;try{r=n.fds.get(t,BigInt(0),BigInt(0))}catch(t){if(t instanceof g)return t.errno;throw t}if(1!==r.preopen)return 28;const{view:i}=K(this);return i.setUint32(e,0,!0),i.setUint32(e+4,V.encode(r.path).length+1,!0),0})),this.fd_prestat_dir_name=Y("fd_prestat_dir_name",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const r=W.get(this).fds.get(t,BigInt(0),BigInt(0));if(1!==r.preopen)return 8;const i=V.encode(r.path+"\0");if(i.length>n)return 42;const{HEAPU8:s}=K(this);return s.set(i,e),0})),this.fd_pwrite=Y("fd_pwrite",(function(t,e,n,r,i){if(e=Number(e),i=Number(i),0===e||0===i)return 28;const{HEAPU8:s,view:o}=K(this),a=W.get(this).fds.get(t,_.FD_WRITE|_.FD_SEEK,BigInt(0)),u=P(Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,i=o.getInt32(r,!0),a=o.getUint32(r+4,!0);return s.subarray(i,i+a)}))),c=j(this).writeSync(a.fd,u,0,u.length,Number(r));return o.setUint32(i,c,!0),0})),this.fd_read=Y("fd_read",(function(t,e,n,r){if(e=Number(e),r=Number(r),0===e||0===r)return 28;const{HEAPU8:i,view:s}=K(this),o=W.get(this).fds.get(t,_.FD_READ,BigInt(0));let a=0;const u=Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,o=s.getInt32(r,!0),u=s.getUint32(r+4,!0);return a+=u,i.subarray(o,o+u)}));let c,f=0;if(0===t){if("undefined"==typeof window||"function"!=typeof window.prompt)return 58;c=function(){const t=window.prompt();return null===t?new Uint8Array:(new TextEncoder).encode(t+"\n")}(),f=c?C(u,c):0}else{c=new Uint8Array(a),c._isBuffer=!0;const t=j(this).readSync(o.fd,c,0,c.length,Number(o.pos));f=c?C(u,c.subarray(0,t)):0,o.pos+=BigInt(f)}return s.setUint32(r,f,!0),0})),this.fd_seek=Y("fd_seek",(function(t,e,n,r){if(0===(r=Number(r)))return 28;if(0===t||1===t||2===t)return 0;const i=W.get(this).fds.get(t,_.FD_SEEK,BigInt(0)).seek(e,n),{view:s}=K(this);return s.setBigUint64(r,i,!0),0})),this.fd_readdir=Y("fd_readdir",(function(t,e,n,r,i){if(e=Number(e),n=Number(n),i=Number(i),0===e||0===i)return 0;const s=W.get(this).fds.get(t,_.FD_READDIR,BigInt(0)),o=j(this),a=o.readdirSync(s.realPath,{withFileTypes:!0}),{HEAPU8:u,view:c}=K(this);let f=0;for(let t=Number(r);t<a.length;t++){const r=V.encode(a[t].name),i=o.statSync(l(s.realPath,a[t].name),{bigint:!0}),c=new Uint8Array(24+r.byteLength),h=new DataView(c.buffer);let d;h.setBigUint64(0,BigInt(t+1),!0),h.setBigUint64(8,BigInt(i.ino?i.ino:0),!0),h.setUint32(16,r.byteLength,!0),d=a[t].isFile()?4:a[t].isDirectory()?3:a[t].isSymbolicLink()?7:a[t].isCharacterDevice()?2:a[t].isBlockDevice()?1:a[t].isSocket()?6:0,h.setUint8(20,d),c.set(r,24);const _=c.slice(0,Math.min(c.length,n-f));u.set(_,e+f),f+=_.byteLength}return c.setUint32(i,f,!0),0})),this.fd_renumber=Y("fd_renumber",(function(t,e){return W.get(this).fds.renumber(e,t),0})),this.fd_sync=Y("fd_sync",(function(t){const e=W.get(this).fds.get(t,_.FD_SYNC,BigInt(0));return j(this).fsyncSync(e.fd),0})),this.fd_tell=Y("fd_tell",(function(t,e){const n=W.get(this).fds.get(t,_.FD_TELL,BigInt(0)),r=BigInt(n.pos),{view:i}=K(this);return i.setBigUint64(Number(e),r,!0),0})),this.fd_write=Y("fd_write",(function(t,e,n,r){if(e=Number(e),r=Number(r),0===e||0===r)return 28;const{HEAPU8:i,view:s}=K(this),o=W.get(this).fds.get(t,_.FD_WRITE,BigInt(0)),a=P(Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,o=s.getInt32(r,!0),a=s.getUint32(r+4,!0);return i.subarray(o,o+a)})));let u;if(1===t||2===t)u=o.write(a);else{u=j(this).writeSync(o.fd,a,0,a.length,Number(o.pos)),o.pos+=BigInt(u)}return s.setUint32(r,u,!0),0})),this.path_create_directory=Y("path_create_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=K(this),i=W.get(this).fds.get(t,_.PATH_CREATE_DIRECTORY,BigInt(0));let s=Z.decode(r.subarray(e,e+n));s=l(i.realPath,s);return j(this).mkdirSync(s),0})),this.path_filestat_get=Y("path_filestat_get",(function(t,e,n,r,i){if(n=Number(n),r=Number(r),i=Number(i),0===n||0===i)return 28;const{HEAPU8:s,view:o}=K(this),a=W.get(this).fds.get(t,_.PATH_FILESTAT_GET,BigInt(0));let u=Z.decode(s.subarray(n,n+r));const c=j(this);let f;return u=l(a.realPath,u),f=1==(1&e)?c.statSync(u,{bigint:!0}):c.lstatSync(u,{bigint:!0}),H(o,i,f),0})),this.path_filestat_set_times=Y("path_filestat_set_times",(function(t,e,n,r,i,s,o){if(n=Number(n),r=Number(r),0===n)return 28;if(-16&o)return 28;const{HEAPU8:a}=K(this),u=W.get(this).fds.get(t,_.PATH_FILESTAT_SET_TIMES,BigInt(0)),c=j(this),f=$(c,u,Z.decode(a.subarray(n,n+r)),e);return 2==(2&o)&&(i=BigInt(1e6*Date.now())),8==(8&o)&&(s=BigInt(1e6*Date.now())),c.utimesSync(f,Number(i),Number(s)),0})),this.path_link=Y("path_link",(function(t,e,n,r,i,s,o){if(n=Number(n),r=Number(r),s=Number(s),o=Number(o),0===n||0===s)return 28;const a=W.get(this);let u,c;t===i?u=c=a.fds.get(t,_.PATH_LINK_SOURCE|_.PATH_LINK_TARGET,BigInt(0)):(u=a.fds.get(t,_.PATH_LINK_SOURCE,BigInt(0)),c=a.fds.get(i,_.PATH_LINK_TARGET,BigInt(0)));const{HEAPU8:f}=K(this),h=j(this),d=$(h,u,Z.decode(f.subarray(n,n+r)),e),g=l(c.realPath,Z.decode(f.subarray(s,s+o)));return h.linkSync(d,g),0})),this.path_open=Y("path_open",(function(t,e,n,r,i,s,o,a,u){if(n=Number(n),u=Number(u),0===n||0===u)return 28;r=Number(r),s=BigInt(s);const c=((s=BigInt(s))&(_.FD_READ|_.FD_READDIR))!==BigInt(0),f=(s&(_.FD_DATASYNC|_.FD_WRITE|_.FD_ALLOCATE|_.FD_FILESTAT_SET_SIZE))!==BigInt(0);let h=f?c?2:1:0,d=_.PATH_OPEN,l=s|o;0!=(1&i)&&(h|=64,d|=_.PATH_CREATE_FILE),0!=(2&i)&&(h|=65536),0!=(4&i)&&(h|=128),0!=(8&i)&&(h|=512,d|=_.PATH_FILESTAT_SET_SIZE),0!=(1&a)&&(h|=1024),0!=(2&a)&&(l|=_.FD_DATASYNC),0!=(4&a)&&(h|=2048),0!=(8&a)&&(h|=1052672,l|=_.FD_SYNC),0!=(16&a)&&(h|=1052672,l|=_.FD_SYNC),f&&0==(1536&h)&&(l|=_.FD_SEEK);const g=W.get(this),p=g.fds.get(t,d,l),E=K(this),y=E.HEAPU8,I=Z.decode(y.subarray(n,n+r)),T=j(this),A=$(T,p,I,e),m=T.openSync(A,h,438),b=g.fds.getFileTypeByFd(m);if(0!=(2&i)&&3!==b)return 54;const{base:w,inheriting:S}=N(g.fds.stdio,m,h,b),B=g.fds.insert(m,A,A,b,s&w,o&S,0),F=T.fstatSync(m,{bigint:!0});F.isFile()&&(B.size=F.size,0!=(1024&h)&&(B.pos=F.size));return E.view.setInt32(u,B.id,!0),0})),this.path_readlink=Y("path_readlink",(function(t,e,n,r,i,s){if(e=Number(e),n=Number(n),r=Number(r),i=Number(i),s=Number(s),0===e||0===r||0===s)return 28;const{HEAPU8:o,view:a}=K(this),u=W.get(this).fds.get(t,_.PATH_READLINK,BigInt(0));let c=Z.decode(o.subarray(e,e+n));c=l(u.realPath,c);const f=j(this).readlinkSync(c),h=V.encode(f),d=Math.min(h.length,i);return d>=i?42:(o.set(h.subarray(0,d),r),o[r+d]=0,a.setUint32(s,d+1,!0),0)})),this.path_remove_directory=Y("path_remove_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=K(this),i=W.get(this).fds.get(t,_.PATH_REMOVE_DIRECTORY,BigInt(0));let s=Z.decode(r.subarray(e,e+n));s=l(i.realPath,s);return j(this).rmdirSync(s),0})),this.path_rename=Y("path_rename",(function(t,e,n,r,i,s){if(e=Number(e),n=Number(n),i=Number(i),s=Number(s),0===e||0===i)return 28;const o=W.get(this);let a,u;t===r?a=u=o.fds.get(t,_.PATH_RENAME_SOURCE|_.PATH_RENAME_TARGET,BigInt(0)):(a=o.fds.get(t,_.PATH_RENAME_SOURCE,BigInt(0)),u=o.fds.get(r,_.PATH_RENAME_TARGET,BigInt(0)));const{HEAPU8:c}=K(this),f=l(a.realPath,Z.decode(c.subarray(e,e+n))),h=l(u.realPath,Z.decode(c.subarray(i,i+s)));return j(this).renameSync(f,h),0})),this.path_symlink=Y("path_symlink",(function(t,e,n,r,i){if(t=Number(t),e=Number(e),r=Number(r),i=Number(i),0===t||0===r)return 28;const{HEAPU8:s}=K(this),o=W.get(this).fds.get(n,_.PATH_SYMLINK,BigInt(0)),a=Z.decode(s.subarray(t,t+e));let u=Z.decode(s.subarray(r,r+i));u=l(o.realPath,u);return j(this).symlinkSync(a,u),0})),this.path_unlink_file=Y("path_unlink_file",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=K(this),i=W.get(this).fds.get(t,_.PATH_UNLINK_FILE,BigInt(0));let s=Z.decode(r.subarray(e,e+n));s=l(i.realPath,s);return j(this).unlinkSync(s),0})),this.poll_oneoff=Y("poll_oneoff",(function(t,e,n,r){return 52})),this.proc_exit=Y("proc_exit",(function(t){return 0})),this.proc_raise=Y("proc_raise",(function(t){return 52})),this.sched_yield=Y("sched_yield",(function(){return 0})),this.random_get="undefined"!=typeof crypto&&"function"==typeof crypto.getRandomValues?Y("random_get",(function(t,e){if(0===(t=Number(t)))return 28;e=Number(e);const{HEAPU8:n}=K(this);let r;const i=65536;for(r=0;r+i<e;r+=i)crypto.getRandomValues(n.subarray(t+r,t+r+i));return crypto.getRandomValues(n.subarray(t+r,t+e)),0})):Y("random_get",(function(t,e){if(0===(t=Number(t)))return 28;e=Number(e);const{view:n}=K(this);for(let r=t;r<t+e;++r)n.setUint8(r,Math.floor(256*Math.random()));return 0})),this.sock_recv=Y("sock_recv",(function(){return 58})),this.sock_send=Y("sock_send",(function(){return 58})),this.sock_shutdown=Y("sock_shutdown",(function(){return 58}));const u=s?s.fs:void 0,c=new U({size:3,in:i[0],out:i[1],err:i[2],fs:u,print:o,printErr:a});if(W.set(this,{fds:c,args:t,env:n}),u&&z.set(this,u),r.length>0)for(let t=0;t<r.length;++t){const e=u.realpathSync(r[t].realPath,"utf8"),n=u.openSync(e,"r",438);c.insertPreopen(n,r[t].mappedPath,e)}}}const X=Object.freeze(Object.create(null)),Q=Symbol("kExitCode"),J=Symbol("kSetMemory"),tt=Symbol("kStarted"),et=Symbol("kInstance");function nt(t,e){n(e,"instance"),n(e.exports,"instance.exports"),t[et]=e,t[J](e.exports.memory)}function rt(t){throw this[Q]=t,Q}function it(){const t=e.Function;if("function"!=typeof t)throw new Error('WebAssembly.Function is not supported in this environment. If you are using V8 based browser like Chrome, try to specify --js-flag="--wasm-staging --experimental-wasm-stack-switching"');return t}function st(t){const e=it();if("function"!=typeof t)throw new TypeError("Function required");return new e({parameters:[...e.type(t).parameters.slice(1)],results:["externref"]},t,{promising:"first"})}t.Asyncify=f,t.Memory=k,t.WASI=class{constructor(t=X){var e;n(t,"options"),void 0!==t.args&&function(t,e){if(!Array.isArray(t))throw new TypeError(`${e} must be an array. Received ${null===t?"null":typeof t}`)}(t.args,"options.args");const s=(null!==(e=t.args)&&void 0!==e?e:[]).map(String),o=[];void 0!==t.env&&(n(t.env,"options.env"),Object.entries(t.env).forEach((({0:t,1:e})=>{void 0!==e&&o.push(`${t}=${e}`)})));const a=[];if(void 0!==t.preopens&&(n(t.preopens,"options.preopens"),Object.entries(t.preopens).forEach((({0:t,1:e})=>a.push({mappedPath:String(t),realPath:String(e)})))),a.length>0&&void 0===t.filesystem)throw new Error("filesystem is disabled, can not preopen directory");if(void 0!==t.filesystem){if(n(t.filesystem,"options.filesystem"),r(t.filesystem.type,"options.filesystem.type"),"memfs"!==t.filesystem.type)throw new Error(`Filesystem type ${t.filesystem.type} is not supported, only "memfs" is supported currently`);try{n(t.filesystem.fs,"options.filesystem.fs")}catch(t){throw new Error("Node.js fs like implementation is not provided")}}void 0!==t.print&&i(t.print,"options.print"),void 0!==t.printErr&&i(t.printErr,"options.printErr");const u=new q(s,o,a,[0,1,2],t.filesystem,t.print,t.printErr);for(const t in u)u[t]=u[t].bind(u);void 0!==t.returnOnExit&&(!function(t,e){if("boolean"!=typeof t)throw new TypeError(`${e} must be a boolean. Received ${null===t?"null":typeof t}`)}(t.returnOnExit,"options.returnOnExit"),t.returnOnExit&&(u.proc_exit=rt.bind(this))),this[J]=u._setMemory,delete u._setMemory,this.wasiImport=u,this[tt]=!1,this[Q]=0,this[et]=void 0}start(t){if(this[tt])throw new Error("WASI instance has already started");this[tt]=!0,nt(this,t);const{_start:e,_initialize:n}=this[et].exports;let r;i(e,"instance.exports._start"),s(n,"instance.exports._initialize");try{r=e()}catch(t){if(t!==Q)throw t}return r instanceof Promise?r.then((()=>this[Q]),(t=>{if(t!==Q)throw t;return this[Q]})):this[Q]}initialize(t){if(this[tt])throw new Error("WASI instance has already started");this[tt]=!0,nt(this,t);const{_start:e,_initialize:n}=this[et].exports;if(s(e,"instance.exports._start"),void 0!==n)return i(n,"instance.exports._initialize"),n()}},t.WebAssemblyMemory=O,t.extendMemory=x,t.load=async function(t,n,r){var i,s;if(n&&"object"!=typeof n)throw new TypeError("imports must be an object or undefined");let o,a;if(n=null!=n?n:{},r&&(o=new f,n=o.wrapImports(n)),t instanceof ArrayBuffer||ArrayBuffer.isView(t)){if(a=await e.instantiate(t,n),r){const t=a.instance.exports.memory||(null===(i=n.env)||void 0===i?void 0:i.memory);return{module:a.module,instance:o.init(t,a.instance,r)}}return a}if("string"!=typeof t&&!(t instanceof URL))throw new TypeError("Invalid source");if("function"==typeof e.instantiateStreaming)try{a=await e.instantiateStreaming(fetch(t),n)}catch(e){a=await h(t,n)}else a=await h(t,n);if(r){const t=a.instance.exports.memory||(null===(s=n.env)||void 0===s?void 0:s.memory);return{module:a.module,instance:o.init(t,a.instance,r)}}return a},t.loadSync=function(t,n,r){var i;if(t instanceof ArrayBuffer&&!ArrayBuffer.isView(t))throw new TypeError("Invalid source");if(n&&"object"!=typeof n)throw new TypeError("imports must be an object or undefined");let s;n=null!=n?n:{},r&&(s=new f,n=s.wrapImports(n));const o=new e.Module(t),a=new e.Instance(o,n),u={instance:a,module:o};if(r){const t=u.instance.exports.memory||(null===(i=n.env)||void 0===i?void 0:i.memory);return{module:u.module,instance:s.init(t,a,r)}}return u},t.wrapAsyncExport=st,t.wrapAsyncImport=function(t,e,n){const r=it();if("function"!=typeof t)throw new TypeError("Function required");const i=e.slice(0);return i.unshift("externref"),new r({parameters:i,results:n},t,{suspending:"first"})},t.wrapExports=function(t,e){return a(t,((t,n)=>{let r="function"!=typeof t;return Array.isArray(e)&&(r=r||-1===e.indexOf(n)),r?t:st(t)}))},Object.defineProperty(t,"__esModule",{value:!0})}));
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Asyncify = void 0;
4
+ const webassembly_1 = require("./webassembly");
4
5
  const util_1 = require("./wasi/util");
5
6
  const ignoreNames = [
6
7
  'asyncify_get_state',
@@ -46,7 +47,7 @@ class Asyncify {
46
47
  if (this.exports) {
47
48
  throw new Error('Asyncify has been initialized');
48
49
  }
49
- if (!(memory instanceof WebAssembly.Memory)) {
50
+ if (!(memory instanceof webassembly_1._WebAssembly.Memory)) {
50
51
  throw new TypeError('Require WebAssembly.Memory object');
51
52
  }
52
53
  const exports = instance.exports;
@@ -81,7 +82,7 @@ class Asyncify {
81
82
  new Int32Array(memory.buffer, this.dataPtr).set([address.start, address.end]);
82
83
  }
83
84
  this.exports = this.wrapExports(exports, options.wrapExports);
84
- const asyncifiedInstance = Object.create(WebAssembly.Instance.prototype);
85
+ const asyncifiedInstance = Object.create(webassembly_1._WebAssembly.Instance.prototype);
85
86
  Object.defineProperty(asyncifiedInstance, 'exports', { value: this.exports });
86
87
  // Object.setPrototypeOf(instance, Instance.prototype)
87
88
  return asyncifiedInstance;
@@ -140,20 +141,13 @@ class Asyncify {
140
141
  });
141
142
  }
142
143
  wrapExports(exports, needWrap) {
143
- const newExports = Object.create(null);
144
- Object.keys(exports).forEach(name => {
145
- const exportValue = exports[name];
144
+ return (0, util_1.wrapInstanceExports)(exports, (exportValue, name) => {
146
145
  let ignore = ignoreNames.indexOf(name) !== -1 || typeof exportValue !== 'function';
147
146
  if (Array.isArray(needWrap)) {
148
147
  ignore = ignore || (needWrap.indexOf(name) === -1);
149
148
  }
150
- Object.defineProperty(newExports, name, {
151
- enumerable: true,
152
- value: ignore ? exportValue : this.wrapExportFunction(exportValue)
153
- });
149
+ return ignore ? exportValue : this.wrapExportFunction(exportValue);
154
150
  });
155
- // wrappedExports.set(exports, newExports)
156
- return newExports;
157
151
  }
158
152
  }
159
153
  exports.Asyncify = Asyncify;
package/lib/cjs/index.js CHANGED
@@ -8,4 +8,5 @@ tslib_1.__exportStar(require("./asyncify"), exports);
8
8
  tslib_1.__exportStar(require("./load"), exports);
9
9
  tslib_1.__exportStar(require("./wasi/index"), exports);
10
10
  tslib_1.__exportStar(require("./memory"), exports);
11
+ tslib_1.__exportStar(require("./jspi"), exports);
11
12
  //# sourceMappingURL=index.js.map