@tybys/wasm-util 0.1.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.
@@ -0,0 +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})}));
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Asyncify = void 0;
4
+ const util_1 = require("./wasi/util");
5
+ const ignoreNames = [
6
+ 'asyncify_get_state',
7
+ 'asyncify_start_rewind',
8
+ 'asyncify_start_unwind',
9
+ 'asyncify_stop_rewind',
10
+ 'asyncify_stop_unwind'
11
+ ];
12
+ // const wrappedExports = new WeakMap<WebAssembly.Exports, WebAssembly.Exports>()
13
+ var AsyncifyState;
14
+ (function (AsyncifyState) {
15
+ AsyncifyState[AsyncifyState["NONE"] = 0] = "NONE";
16
+ AsyncifyState[AsyncifyState["UNWINDING"] = 1] = "UNWINDING";
17
+ AsyncifyState[AsyncifyState["REWINDING"] = 2] = "REWINDING";
18
+ })(AsyncifyState || (AsyncifyState = {}));
19
+ function tryAllocate(instance, wasm64, size, mallocName) {
20
+ if (typeof instance.exports[mallocName] !== 'function' || size <= 0) {
21
+ return {
22
+ wasm64,
23
+ dataPtr: 16,
24
+ start: wasm64 ? 32 : 24,
25
+ end: 1024
26
+ };
27
+ }
28
+ const malloc = instance.exports[mallocName];
29
+ const dataPtr = wasm64 ? Number(malloc(BigInt(16) + BigInt(size))) : malloc(8 + size);
30
+ if (dataPtr === 0) {
31
+ throw new Error('Allocate asyncify data failed');
32
+ }
33
+ return wasm64
34
+ ? { wasm64, dataPtr, start: dataPtr + 16, end: dataPtr + 16 + size }
35
+ : { wasm64, dataPtr, start: dataPtr + 8, end: dataPtr + 8 + size };
36
+ }
37
+ /** @public */
38
+ class Asyncify {
39
+ constructor() {
40
+ this.value = undefined;
41
+ this.exports = undefined;
42
+ this.dataPtr = 0;
43
+ }
44
+ init(memory, instance, options) {
45
+ var _a, _b;
46
+ if (this.exports) {
47
+ throw new Error('Asyncify has been initialized');
48
+ }
49
+ if (!(memory instanceof WebAssembly.Memory)) {
50
+ throw new TypeError('Require WebAssembly.Memory object');
51
+ }
52
+ const exports = instance.exports;
53
+ for (let i = 0; i < ignoreNames.length; ++i) {
54
+ if (typeof exports[ignoreNames[i]] !== 'function') {
55
+ throw new TypeError('Invalid asyncify wasm');
56
+ }
57
+ }
58
+ let address;
59
+ const wasm64 = Boolean(options.wasm64);
60
+ if (!options.tryAllocate) {
61
+ address = {
62
+ wasm64,
63
+ dataPtr: 16,
64
+ start: wasm64 ? 32 : 24,
65
+ end: 1024
66
+ };
67
+ }
68
+ else {
69
+ if (options.tryAllocate === true) {
70
+ address = tryAllocate(instance, wasm64, 4096, 'malloc');
71
+ }
72
+ else {
73
+ address = tryAllocate(instance, wasm64, (_a = options.tryAllocate.size) !== null && _a !== void 0 ? _a : 4096, (_b = options.tryAllocate.name) !== null && _b !== void 0 ? _b : 'malloc');
74
+ }
75
+ }
76
+ this.dataPtr = address.dataPtr;
77
+ if (wasm64) {
78
+ new BigInt64Array(memory.buffer, this.dataPtr).set([BigInt(address.start), BigInt(address.end)]);
79
+ }
80
+ else {
81
+ new Int32Array(memory.buffer, this.dataPtr).set([address.start, address.end]);
82
+ }
83
+ this.exports = this.wrapExports(exports, options.wrapExports);
84
+ const asyncifiedInstance = Object.create(WebAssembly.Instance.prototype);
85
+ Object.defineProperty(asyncifiedInstance, 'exports', { value: this.exports });
86
+ // Object.setPrototypeOf(instance, Instance.prototype)
87
+ return asyncifiedInstance;
88
+ }
89
+ assertState() {
90
+ if (this.exports.asyncify_get_state() !== AsyncifyState.NONE) {
91
+ throw new Error('Asyncify state error');
92
+ }
93
+ }
94
+ wrapImportFunction(f) {
95
+ return ((...args) => {
96
+ // eslint-disable-next-line no-unreachable-loop
97
+ while (this.exports.asyncify_get_state() === AsyncifyState.REWINDING) {
98
+ this.exports.asyncify_stop_rewind();
99
+ return this.value;
100
+ }
101
+ this.assertState();
102
+ const v = f(...args);
103
+ if (!(0, util_1.isPromiseLike)(v))
104
+ return v;
105
+ this.exports.asyncify_start_unwind(this.dataPtr);
106
+ this.value = v;
107
+ });
108
+ }
109
+ wrapImports(imports) {
110
+ const importObject = {};
111
+ Object.keys(imports).forEach(k => {
112
+ const mod = imports[k];
113
+ const newModule = {};
114
+ Object.keys(mod).forEach(name => {
115
+ const importValue = mod[name];
116
+ if (typeof importValue === 'function') {
117
+ newModule[name] = this.wrapImportFunction(importValue);
118
+ }
119
+ else {
120
+ newModule[name] = importValue;
121
+ }
122
+ });
123
+ importObject[k] = newModule;
124
+ });
125
+ return importObject;
126
+ }
127
+ wrapExportFunction(f) {
128
+ return (async (...args) => {
129
+ this.assertState();
130
+ let ret = f(...args);
131
+ while (this.exports.asyncify_get_state() === AsyncifyState.UNWINDING) {
132
+ this.exports.asyncify_stop_unwind();
133
+ this.value = await this.value;
134
+ this.assertState();
135
+ this.exports.asyncify_start_rewind(this.dataPtr);
136
+ ret = f();
137
+ }
138
+ this.assertState();
139
+ return ret;
140
+ });
141
+ }
142
+ wrapExports(exports, needWrap) {
143
+ const newExports = Object.create(null);
144
+ Object.keys(exports).forEach(name => {
145
+ const exportValue = exports[name];
146
+ let ignore = ignoreNames.indexOf(name) !== -1 || typeof exportValue !== 'function';
147
+ if (Array.isArray(needWrap)) {
148
+ ignore = ignore || (needWrap.indexOf(name) === -1);
149
+ }
150
+ Object.defineProperty(newExports, name, {
151
+ enumerable: true,
152
+ value: ignore ? exportValue : this.wrapExportFunction(exportValue)
153
+ });
154
+ });
155
+ // wrappedExports.set(exports, newExports)
156
+ return newExports;
157
+ }
158
+ }
159
+ exports.Asyncify = Asyncify;
160
+ // /** @public */
161
+ // export class Instance extends WebAssembly.Instance {
162
+ // constructor (options: AsyncifyOptions, module: WebAssembly.Module, importObject?: WebAssembly.Imports) {
163
+ // importObject = importObject ?? {}
164
+ // const asyncify = new Asyncify()
165
+ // super(module, asyncify.wrapImports(importObject))
166
+ // asyncify.init(importObject, this, options)
167
+ // }
168
+ // get exports (): WebAssembly.Exports {
169
+ // return wrappedExports.get(super.exports)!
170
+ // }
171
+ // }
172
+ // Object.defineProperty(Instance.prototype, 'exports', { enumerable: true })
173
+ //# sourceMappingURL=asyncify.js.map
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ /**
3
+ * @packageDocumentation
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const tslib_1 = require("tslib");
7
+ tslib_1.__exportStar(require("./asyncify"), exports);
8
+ tslib_1.__exportStar(require("./load"), exports);
9
+ tslib_1.__exportStar(require("./wasi/index"), exports);
10
+ tslib_1.__exportStar(require("./memory"), exports);
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadSync = exports.load = void 0;
4
+ const asyncify_1 = require("./asyncify");
5
+ async function fetchWasm(urlOrBuffer, imports) {
6
+ const response = await fetch(urlOrBuffer);
7
+ const buffer = await response.arrayBuffer();
8
+ const source = await WebAssembly.instantiate(buffer, imports);
9
+ return source;
10
+ }
11
+ /** @public */
12
+ async function load(urlOrBuffer, imports, asyncify) {
13
+ var _a, _b;
14
+ if (imports && typeof imports !== 'object') {
15
+ throw new TypeError('imports must be an object or undefined');
16
+ }
17
+ imports = imports !== null && imports !== void 0 ? imports : {};
18
+ let asyncifyHelper;
19
+ let source;
20
+ if (asyncify) {
21
+ asyncifyHelper = new asyncify_1.Asyncify();
22
+ imports = asyncifyHelper.wrapImports(imports);
23
+ }
24
+ if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
25
+ source = await WebAssembly.instantiate(urlOrBuffer, imports);
26
+ if (asyncify) {
27
+ const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
28
+ return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
29
+ }
30
+ return source;
31
+ }
32
+ if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
33
+ throw new TypeError('Invalid source');
34
+ }
35
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
36
+ try {
37
+ source = await WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
38
+ }
39
+ catch (_) {
40
+ source = await fetchWasm(urlOrBuffer, imports);
41
+ }
42
+ }
43
+ else {
44
+ source = await fetchWasm(urlOrBuffer, imports);
45
+ }
46
+ if (asyncify) {
47
+ const memory = source.instance.exports.memory || ((_b = imports.env) === null || _b === void 0 ? void 0 : _b.memory);
48
+ return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
49
+ }
50
+ return source;
51
+ }
52
+ exports.load = load;
53
+ /** @public */
54
+ function loadSync(buffer, imports, asyncify) {
55
+ var _a;
56
+ if ((buffer instanceof ArrayBuffer) && !ArrayBuffer.isView(buffer)) {
57
+ throw new TypeError('Invalid source');
58
+ }
59
+ if (imports && typeof imports !== 'object') {
60
+ throw new TypeError('imports must be an object or undefined');
61
+ }
62
+ imports = imports !== null && imports !== void 0 ? imports : {};
63
+ let asyncifyHelper;
64
+ if (asyncify) {
65
+ asyncifyHelper = new asyncify_1.Asyncify();
66
+ imports = asyncifyHelper.wrapImports(imports);
67
+ }
68
+ const module = new WebAssembly.Module(buffer);
69
+ const instance = new WebAssembly.Instance(module, imports);
70
+ const source = { instance, module };
71
+ if (asyncify) {
72
+ const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
73
+ return { module: source.module, instance: asyncifyHelper.init(memory, instance, asyncify) };
74
+ }
75
+ return source;
76
+ }
77
+ exports.loadSync = loadSync;
78
+ //# sourceMappingURL=load.js.map