@tybys/wasm-util 0.2.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.
- package/dist/wasm-util.d.ts +17 -0
- package/dist/wasm-util.esm-bundler.js +52 -10
- package/dist/wasm-util.esm.js +52 -10
- package/dist/wasm-util.esm.min.js +1 -1
- package/dist/wasm-util.js +54 -9
- package/dist/wasm-util.min.js +1 -1
- package/lib/cjs/asyncify.js +2 -9
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/jspi.js +46 -0
- package/lib/cjs/wasi/util.js +13 -1
- package/lib/mjs/asyncify.mjs +3 -10
- package/lib/mjs/index.mjs +1 -0
- package/lib/mjs/jspi.mjs +39 -0
- package/lib/mjs/wasi/util.mjs +11 -0
- package/package.json +1 -1
package/dist/wasm-util.d.ts
CHANGED
|
@@ -79,6 +79,11 @@ export declare class Memory extends WebAssemblyMemory {
|
|
|
79
79
|
get view(): DataView;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
/** @public */
|
|
83
|
+
export declare type PromisifyExports<T, U> = T extends Record<string, any> ? {
|
|
84
|
+
[P in keyof T]: T[P] extends Callable ? U extends Array<keyof T> ? P extends U[number] ? AsyncifyExportFunction<T[P]> : T[P] : AsyncifyExportFunction<T[P]> : T[P];
|
|
85
|
+
} : T;
|
|
86
|
+
|
|
82
87
|
/** @public */
|
|
83
88
|
export declare class WASI {
|
|
84
89
|
private [kSetMemory];
|
|
@@ -114,6 +119,18 @@ export declare const WebAssemblyMemory: {
|
|
|
114
119
|
prototype: WebAssembly.Memory;
|
|
115
120
|
};
|
|
116
121
|
|
|
122
|
+
/** @public */
|
|
123
|
+
export declare function wrapAsyncExport<T extends Function = any>(f: Function): T;
|
|
124
|
+
|
|
125
|
+
/** @public */
|
|
126
|
+
export declare function wrapAsyncImport(f: Function, parameterType: WebAssembly.ValueType[], returnType: WebAssembly.ValueType[]): Function;
|
|
127
|
+
|
|
128
|
+
/** @public */
|
|
129
|
+
export declare function wrapExports<T extends WebAssembly.Exports>(exports: T): PromisifyExports<T, void>;
|
|
130
|
+
|
|
131
|
+
/** @public */
|
|
132
|
+
export declare function wrapExports<T extends WebAssembly.Exports, U extends Array<keyof T>>(exports: T, needWrap: U): PromisifyExports<T, U>;
|
|
133
|
+
|
|
117
134
|
export { }
|
|
118
135
|
|
|
119
136
|
export as namespace wasmUtil;
|
|
@@ -39,6 +39,17 @@ function validateUndefined(value, name) {
|
|
|
39
39
|
}
|
|
40
40
|
function isPromiseLike(obj) {
|
|
41
41
|
return !!(obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function');
|
|
42
|
+
}
|
|
43
|
+
function wrapInstanceExports(exports, mapFn) {
|
|
44
|
+
const newExports = Object.create(null);
|
|
45
|
+
Object.keys(exports).forEach(name => {
|
|
46
|
+
const exportValue = exports[name];
|
|
47
|
+
Object.defineProperty(newExports, name, {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
value: mapFn(exportValue, name)
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
return newExports;
|
|
42
53
|
}
|
|
43
54
|
|
|
44
55
|
const ignoreNames = [
|
|
@@ -172,20 +183,13 @@ class Asyncify {
|
|
|
172
183
|
});
|
|
173
184
|
}
|
|
174
185
|
wrapExports(exports, needWrap) {
|
|
175
|
-
|
|
176
|
-
Object.keys(exports).forEach(name => {
|
|
177
|
-
const exportValue = exports[name];
|
|
186
|
+
return wrapInstanceExports(exports, (exportValue, name) => {
|
|
178
187
|
let ignore = ignoreNames.indexOf(name) !== -1 || typeof exportValue !== 'function';
|
|
179
188
|
if (Array.isArray(needWrap)) {
|
|
180
189
|
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
181
190
|
}
|
|
182
|
-
|
|
183
|
-
enumerable: true,
|
|
184
|
-
value: ignore ? exportValue : this.wrapExportFunction(exportValue)
|
|
185
|
-
});
|
|
191
|
+
return ignore ? exportValue : this.wrapExportFunction(exportValue);
|
|
186
192
|
});
|
|
187
|
-
// wrappedExports.set(exports, newExports)
|
|
188
|
-
return newExports;
|
|
189
193
|
}
|
|
190
194
|
}
|
|
191
195
|
// /** @public */
|
|
@@ -1893,4 +1897,42 @@ function wasiReturnOnProcExit(rval) {
|
|
|
1893
1897
|
throw kExitCode;
|
|
1894
1898
|
}
|
|
1895
1899
|
|
|
1896
|
-
|
|
1900
|
+
function checkWebAssemblyFunction() {
|
|
1901
|
+
const WebAssemblyFunction = _WebAssembly.Function;
|
|
1902
|
+
if (typeof WebAssemblyFunction !== 'function') {
|
|
1903
|
+
throw new Error('WebAssembly.Function is not supported in this environment.' +
|
|
1904
|
+
' If you are using V8 based browser like Chrome, try to specify' +
|
|
1905
|
+
' --js-flag="--wasm-staging --experimental-wasm-stack-switching"');
|
|
1906
|
+
}
|
|
1907
|
+
return WebAssemblyFunction;
|
|
1908
|
+
}
|
|
1909
|
+
/** @public */
|
|
1910
|
+
function wrapAsyncImport(f, parameterType, returnType) {
|
|
1911
|
+
const WebAssemblyFunction = checkWebAssemblyFunction();
|
|
1912
|
+
if (typeof f !== 'function') {
|
|
1913
|
+
throw new TypeError('Function required');
|
|
1914
|
+
}
|
|
1915
|
+
const parameters = parameterType.slice(0);
|
|
1916
|
+
parameters.unshift('externref');
|
|
1917
|
+
return new WebAssemblyFunction({ parameters, results: returnType }, f, { suspending: 'first' });
|
|
1918
|
+
}
|
|
1919
|
+
/** @public */
|
|
1920
|
+
function wrapAsyncExport(f) {
|
|
1921
|
+
const WebAssemblyFunction = checkWebAssemblyFunction();
|
|
1922
|
+
if (typeof f !== 'function') {
|
|
1923
|
+
throw new TypeError('Function required');
|
|
1924
|
+
}
|
|
1925
|
+
return new WebAssemblyFunction({ parameters: [...WebAssemblyFunction.type(f).parameters.slice(1)], results: ['externref'] }, f, { promising: 'first' });
|
|
1926
|
+
}
|
|
1927
|
+
/** @public */
|
|
1928
|
+
function wrapExports(exports, needWrap) {
|
|
1929
|
+
return wrapInstanceExports(exports, (exportValue, name) => {
|
|
1930
|
+
let ignore = typeof exportValue !== 'function';
|
|
1931
|
+
if (Array.isArray(needWrap)) {
|
|
1932
|
+
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
1933
|
+
}
|
|
1934
|
+
return ignore ? exportValue : wrapAsyncExport(exportValue);
|
|
1935
|
+
});
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
export { Asyncify, Memory, WASI, WebAssemblyMemory, extendMemory, load, loadSync, wrapAsyncExport, wrapAsyncImport, wrapExports };
|
package/dist/wasm-util.esm.js
CHANGED
|
@@ -39,6 +39,17 @@ function validateUndefined(value, name) {
|
|
|
39
39
|
}
|
|
40
40
|
function isPromiseLike(obj) {
|
|
41
41
|
return !!(obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function');
|
|
42
|
+
}
|
|
43
|
+
function wrapInstanceExports(exports, mapFn) {
|
|
44
|
+
const newExports = Object.create(null);
|
|
45
|
+
Object.keys(exports).forEach(name => {
|
|
46
|
+
const exportValue = exports[name];
|
|
47
|
+
Object.defineProperty(newExports, name, {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
value: mapFn(exportValue, name)
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
return newExports;
|
|
42
53
|
}
|
|
43
54
|
|
|
44
55
|
const ignoreNames = [
|
|
@@ -172,20 +183,13 @@ class Asyncify {
|
|
|
172
183
|
});
|
|
173
184
|
}
|
|
174
185
|
wrapExports(exports, needWrap) {
|
|
175
|
-
|
|
176
|
-
Object.keys(exports).forEach(name => {
|
|
177
|
-
const exportValue = exports[name];
|
|
186
|
+
return wrapInstanceExports(exports, (exportValue, name) => {
|
|
178
187
|
let ignore = ignoreNames.indexOf(name) !== -1 || typeof exportValue !== 'function';
|
|
179
188
|
if (Array.isArray(needWrap)) {
|
|
180
189
|
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
181
190
|
}
|
|
182
|
-
|
|
183
|
-
enumerable: true,
|
|
184
|
-
value: ignore ? exportValue : this.wrapExportFunction(exportValue)
|
|
185
|
-
});
|
|
191
|
+
return ignore ? exportValue : this.wrapExportFunction(exportValue);
|
|
186
192
|
});
|
|
187
|
-
// wrappedExports.set(exports, newExports)
|
|
188
|
-
return newExports;
|
|
189
193
|
}
|
|
190
194
|
}
|
|
191
195
|
// /** @public */
|
|
@@ -1893,4 +1897,42 @@ function wasiReturnOnProcExit(rval) {
|
|
|
1893
1897
|
throw kExitCode;
|
|
1894
1898
|
}
|
|
1895
1899
|
|
|
1896
|
-
|
|
1900
|
+
function checkWebAssemblyFunction() {
|
|
1901
|
+
const WebAssemblyFunction = _WebAssembly.Function;
|
|
1902
|
+
if (typeof WebAssemblyFunction !== 'function') {
|
|
1903
|
+
throw new Error('WebAssembly.Function is not supported in this environment.' +
|
|
1904
|
+
' If you are using V8 based browser like Chrome, try to specify' +
|
|
1905
|
+
' --js-flag="--wasm-staging --experimental-wasm-stack-switching"');
|
|
1906
|
+
}
|
|
1907
|
+
return WebAssemblyFunction;
|
|
1908
|
+
}
|
|
1909
|
+
/** @public */
|
|
1910
|
+
function wrapAsyncImport(f, parameterType, returnType) {
|
|
1911
|
+
const WebAssemblyFunction = checkWebAssemblyFunction();
|
|
1912
|
+
if (typeof f !== 'function') {
|
|
1913
|
+
throw new TypeError('Function required');
|
|
1914
|
+
}
|
|
1915
|
+
const parameters = parameterType.slice(0);
|
|
1916
|
+
parameters.unshift('externref');
|
|
1917
|
+
return new WebAssemblyFunction({ parameters, results: returnType }, f, { suspending: 'first' });
|
|
1918
|
+
}
|
|
1919
|
+
/** @public */
|
|
1920
|
+
function wrapAsyncExport(f) {
|
|
1921
|
+
const WebAssemblyFunction = checkWebAssemblyFunction();
|
|
1922
|
+
if (typeof f !== 'function') {
|
|
1923
|
+
throw new TypeError('Function required');
|
|
1924
|
+
}
|
|
1925
|
+
return new WebAssemblyFunction({ parameters: [...WebAssemblyFunction.type(f).parameters.slice(1)], results: ['externref'] }, f, { promising: 'first' });
|
|
1926
|
+
}
|
|
1927
|
+
/** @public */
|
|
1928
|
+
function wrapExports(exports, needWrap) {
|
|
1929
|
+
return wrapInstanceExports(exports, (exportValue, name) => {
|
|
1930
|
+
let ignore = typeof exportValue !== 'function';
|
|
1931
|
+
if (Array.isArray(needWrap)) {
|
|
1932
|
+
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
1933
|
+
}
|
|
1934
|
+
return ignore ? exportValue : wrapAsyncExport(exportValue);
|
|
1935
|
+
});
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
export { Asyncify, Memory, WASI, WebAssemblyMemory, extendMemory, load, loadSync, wrapAsyncExport, wrapAsyncImport, wrapExports };
|
|
@@ -1 +1 @@
|
|
|
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)}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(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 c=n.exports;for(let t=0;t<o.length;++t)if("function"!=typeof c[o[t]])throw new TypeError("Invalid asyncify wasm");let u;const f=Boolean(r.wasm64);u=r.tryAllocate?!0===r.tryAllocate?a(n,f,4096,"malloc"):a(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=u.dataPtr,f?new BigInt64Array(e.buffer,this.dataPtr).set([BigInt(u.start),BigInt(u.end)]):new Int32Array(e.buffer,this.dataPtr).set([u.start,u.end]),this.exports=this.wrapExports(c,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){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(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 f(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 u(e,n)}else a=await u(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 h(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 d(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,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,"/",d),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 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 E=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,p=E,y=E,I=E,T=E,A=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,m=BigInt(0),b=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,w=b|A,S=l.FD_READ|l.FD_FDSTAT_SET_FLAGS|l.FD_WRITE|l.FD_FILESTAT_GET|l.POLL_FD_READWRITE|l.SOCK_SHUTDOWN,B=E,D=l.FD_READ|l.FD_FDSTAT_SET_FLAGS|l.FD_WRITE|l.FD_FILESTAT_GET|l.POLL_FD_READWRITE,N=BigInt(0);function F(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=D,i.inheriting=N):(i.base=I,i.inheriting=T);break;case 1:i.base=p,i.inheriting=y;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 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,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=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}=F(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,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 R(null!==(a=this.print)&&void 0!==a?a:console.log,f,t,e,n,r,i,s,o):"<stderr>"===e?new R(null!==(c=this.printErr)&&void 0!==c?c:console.error,f,t,e,n,r,i,s,o):new v(f,t,e,n,r,i,s,o),this.fds[f]=u,this.used++,u}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=F(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=t.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 C(e){return Object.getPrototypeOf(e)===t.Memory.prototype&&Object.setPrototypeOf(e,k.prototype),e}function x(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,z=new WeakMap,K=new WeakMap;function W(t){return M.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 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 j(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 $(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 V=new TextEncoder,Z=new TextDecoder;class q{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');M.set(this,C(e))},this.args_get=j("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=z.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=j("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=z.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=j("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=z.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=j("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=z.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=j("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=j("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=j("fd_advise",(function(t,e,n,r){return 52})),this.fd_allocate=j("fd_allocate",(function(t,e,n){const r=z.get(this),i=G(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=j("fd_close",(function(t){const e=z.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=j("fd_datasync",(function(t){const e=z.get(this).fds.get(t,l.FD_DATASYNC,BigInt(0));return G(this).fdatasyncSync(e.fd),0})),this.fd_fdstat_get=j("fd_fdstat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=z.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=j("fd_fdstat_set_flags",(function(t,e){return 52})),this.fd_fdstat_set_rights=j("fd_fdstat_set_rights",(function(t,e,n){const r=z.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=j("fd_filestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=z.get(this).fds.get(t,l.FD_FILESTAT_GET,BigInt(0)),r=G(this).fstatSync(n.fd,{bigint:!0}),{view:i}=W(this);return H(i,e,r),0})),this.fd_filestat_set_size=j("fd_filestat_set_size",(function(t,e){const n=z.get(this).fds.get(t,l.FD_FILESTAT_SET_SIZE,BigInt(0));return G(this).ftruncateSync(n.fd,Number(e)),0})),this.fd_filestat_set_times=j("fd_filestat_set_times",(function(t,e,n,r){const i=z.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 G(this).futimesSync(i.fd,Number(e),Number(n)),0})),this.fd_pread=j("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=z.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=G(this).readSync(a.fd,h,0,h.length,Number(r));return f=h?x(u,h.subarray(0,d)):0,o.setUint32(i,f,!0),0})),this.fd_prestat_get=j("fd_prestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=z.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}=W(this);return i.setUint32(e,0,!0),i.setUint32(e+4,V.encode(r.path).length+1,!0),0})),this.fd_prestat_dir_name=j("fd_prestat_dir_name",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const r=z.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}=W(this);return s.set(i,e),0})),this.fd_pwrite=j("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=z.get(this).fds.get(t,l.FD_WRITE|l.FD_SEEK,BigInt(0)),c=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)}))),u=G(this).writeSync(a.fd,c,0,c.length,Number(r));return o.setUint32(i,u,!0),0})),this.fd_read=j("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=z.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){if("undefined"==typeof window||"function"!=typeof window.prompt)return 58;u=function(){const t=window.prompt();return null===t?new Uint8Array:(new TextEncoder).encode(t+"\n")}(),f=u?x(c,u):0}else{u=new Uint8Array(a),u._isBuffer=!0;const t=G(this).readSync(o.fd,u,0,u.length,Number(o.pos));f=u?x(c,u.subarray(0,t)):0,o.pos+=BigInt(f)}return s.setUint32(r,f,!0),0})),this.fd_seek=j("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=z.get(this).fds.get(t,l.FD_SEEK,BigInt(0)).seek(e,n),{view:s}=W(this);return s.setBigUint64(r,i,!0),0})),this.fd_readdir=j("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=z.get(this).fds.get(t,l.FD_READDIR,BigInt(0)),o=G(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=V.encode(a[t].name),i=o.statSync(_(s.realPath,a[t].name),{bigint:!0}),u=new Uint8Array(24+r.byteLength),h=new DataView(u.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),u.set(r,24);const l=u.slice(0,Math.min(u.length,n-f));c.set(l,e+f),f+=l.byteLength}return u.setUint32(i,f,!0),0})),this.fd_renumber=j("fd_renumber",(function(t,e){return z.get(this).fds.renumber(e,t),0})),this.fd_sync=j("fd_sync",(function(t){const e=z.get(this).fds.get(t,l.FD_SYNC,BigInt(0));return G(this).fsyncSync(e.fd),0})),this.fd_tell=j("fd_tell",(function(t,e){const n=z.get(this).fds.get(t,l.FD_TELL,BigInt(0)),r=BigInt(n.pos),{view:i}=W(this);return i.setBigUint64(Number(e),r,!0),0})),this.fd_write=j("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=z.get(this).fds.get(t,l.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 c;if(1===t||2===t)c=o.write(a);else{c=G(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=j("path_create_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=W(this),i=z.get(this).fds.get(t,l.PATH_CREATE_DIRECTORY,BigInt(0));let s=Z.decode(r.subarray(e,e+n));s=_(i.realPath,s);return G(this).mkdirSync(s),0})),this.path_filestat_get=j("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=z.get(this).fds.get(t,l.PATH_FILESTAT_GET,BigInt(0));let c=Z.decode(s.subarray(n,n+r));const u=G(this);let f;return c=_(a.realPath,c),f=1==(1&e)?u.statSync(c,{bigint:!0}):u.lstatSync(c,{bigint:!0}),H(o,i,f),0})),this.path_filestat_set_times=j("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=z.get(this).fds.get(t,l.PATH_FILESTAT_SET_TIMES,BigInt(0)),u=G(this),f=$(u,c,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())),u.utimesSync(f,Number(i),Number(s)),0})),this.path_link=j("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=z.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}=W(this),h=G(this),d=$(h,c,Z.decode(f.subarray(n,n+r)),e),g=_(u.realPath,Z.decode(f.subarray(s,s+o)));return h.linkSync(d,g),0})),this.path_open=j("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=z.get(this),E=g.fds.get(t,d,_),p=W(this),y=p.HEAPU8,I=Z.decode(y.subarray(n,n+r)),T=G(this),A=$(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}=F(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 p.view.setInt32(c,B.id,!0),0})),this.path_readlink=j("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=z.get(this).fds.get(t,l.PATH_READLINK,BigInt(0));let u=Z.decode(o.subarray(e,e+n));u=_(c.realPath,u);const f=G(this).readlinkSync(u),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=j("path_remove_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=W(this),i=z.get(this).fds.get(t,l.PATH_REMOVE_DIRECTORY,BigInt(0));let s=Z.decode(r.subarray(e,e+n));s=_(i.realPath,s);return G(this).rmdirSync(s),0})),this.path_rename=j("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=z.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}=W(this),f=_(a.realPath,Z.decode(u.subarray(e,e+n))),h=_(c.realPath,Z.decode(u.subarray(i,i+s)));return G(this).renameSync(f,h),0})),this.path_symlink=j("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=z.get(this).fds.get(n,l.PATH_SYMLINK,BigInt(0)),a=Z.decode(s.subarray(t,t+e));let c=Z.decode(s.subarray(r,r+i));c=_(o.realPath,c);return G(this).symlinkSync(a,c),0})),this.path_unlink_file=j("path_unlink_file",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=W(this),i=z.get(this).fds.get(t,l.PATH_UNLINK_FILE,BigInt(0));let s=Z.decode(r.subarray(e,e+n));s=_(i.realPath,s);return G(this).unlinkSync(s),0})),this.poll_oneoff=j("poll_oneoff",(function(t,e,n,r){return 52})),this.proc_exit=j("proc_exit",(function(t){return 0})),this.proc_raise=j("proc_raise",(function(t){return 52})),this.sched_yield=j("sched_yield",(function(){return 0})),this.random_get="undefined"!=typeof crypto&&"function"==typeof crypto.getRandomValues?j("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})):j("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=j("sock_recv",(function(){return 58})),this.sock_send=j("sock_send",(function(){return 58})),this.sock_shutdown=j("sock_shutdown",(function(){return 58}));const c=s?s.fs:void 0,u=new U({size:3,in:i[0],out:i[1],err:i[2],fs:c,print:o,printErr:a});if(z.set(this,{fds:u,args:e,env:n}),c&&K.set(this,c),r.length>0)for(let t=0;t<r.length;++t){const e=c.realpathSync(r[t].realPath,"utf8"),n=c.openSync(e,"r",438);u.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,n){e(n,"instance"),e(n.exports,"instance.exports"),t[et]=n,t[J](n.exports.memory)}class rt{constructor(t=X){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 q(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=it.bind(this))),this[J]=c._setMemory,delete c._setMemory,this.wasiImport=c,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 s;r(e,"instance.exports._start"),i(n,"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[tt])throw new Error("WASI instance has already started");this[tt]=!0,nt(this,t);const{_start:e,_initialize:n}=this[et].exports;if(i(e,"instance.exports._start"),void 0!==n)return r(n,"instance.exports._initialize"),n()}}function it(t){throw this[Q]=t,Q}export{c as Asyncify,k as Memory,rt as WASI,O as WebAssemblyMemory,C as extendMemory,f as load,h 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
|
@@ -45,6 +45,17 @@
|
|
|
45
45
|
}
|
|
46
46
|
function isPromiseLike(obj) {
|
|
47
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;
|
|
48
59
|
}
|
|
49
60
|
|
|
50
61
|
const ignoreNames = [
|
|
@@ -178,20 +189,13 @@
|
|
|
178
189
|
});
|
|
179
190
|
}
|
|
180
191
|
wrapExports(exports, needWrap) {
|
|
181
|
-
|
|
182
|
-
Object.keys(exports).forEach(name => {
|
|
183
|
-
const exportValue = exports[name];
|
|
192
|
+
return wrapInstanceExports(exports, (exportValue, name) => {
|
|
184
193
|
let ignore = ignoreNames.indexOf(name) !== -1 || typeof exportValue !== 'function';
|
|
185
194
|
if (Array.isArray(needWrap)) {
|
|
186
195
|
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
187
196
|
}
|
|
188
|
-
|
|
189
|
-
enumerable: true,
|
|
190
|
-
value: ignore ? exportValue : this.wrapExportFunction(exportValue)
|
|
191
|
-
});
|
|
197
|
+
return ignore ? exportValue : this.wrapExportFunction(exportValue);
|
|
192
198
|
});
|
|
193
|
-
// wrappedExports.set(exports, newExports)
|
|
194
|
-
return newExports;
|
|
195
199
|
}
|
|
196
200
|
}
|
|
197
201
|
// /** @public */
|
|
@@ -1899,6 +1903,44 @@
|
|
|
1899
1903
|
throw kExitCode;
|
|
1900
1904
|
}
|
|
1901
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
|
+
|
|
1902
1944
|
exports.Asyncify = Asyncify;
|
|
1903
1945
|
exports.Memory = Memory;
|
|
1904
1946
|
exports.WASI = WASI;
|
|
@@ -1906,6 +1948,9 @@
|
|
|
1906
1948
|
exports.extendMemory = extendMemory;
|
|
1907
1949
|
exports.load = load;
|
|
1908
1950
|
exports.loadSync = loadSync;
|
|
1951
|
+
exports.wrapAsyncExport = wrapAsyncExport;
|
|
1952
|
+
exports.wrapAsyncImport = wrapAsyncImport;
|
|
1953
|
+
exports.wrapExports = wrapExports;
|
|
1909
1954
|
|
|
1910
1955
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
1911
1956
|
|
package/dist/wasm-util.min.js
CHANGED
|
@@ -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";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)}const a=["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 u{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<a.length;++t)if("function"!=typeof o[a[t]])throw new TypeError("Invalid asyncify wasm");let u;const f=Boolean(r.wasm64);u=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=u.dataPtr,f?new BigInt64Array(t.buffer,this.dataPtr).set([BigInt(u.start),BigInt(u.end)]):new Int32Array(t.buffer,this.dataPtr).set([u.start,u.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){const n=Object.create(null);return Object.keys(t).forEach((r=>{const i=t[r];let s=-1!==a.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 f(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 h(t){return 47===t}function d(...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,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,!n,"/",h),n?`/${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 _ 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(_.prototype,"name",{configurable:!0,writable:!0,value:"WasiError"});const g=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,E=g,y=g,p=g,I=g,T=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,A=BigInt(0),m=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,b=m|T,w=l.FD_READ|l.FD_FDSTAT_SET_FLAGS|l.FD_WRITE|l.FD_FILESTAT_GET|l.POLL_FD_READWRITE|l.SOCK_SHUTDOWN,S=g,B=l.FD_READ|l.FD_FDSTAT_SET_FLAGS|l.FD_WRITE|l.FD_FILESTAT_GET|l.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 _("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&=~l.FD_WRITE:1===s&&(i.base&=~l.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 _("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 _(`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 _(`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 _("Invalid fd",8);const r=this.fds[t];if(!r||r.id!==t)throw new _("Bad file descriptor",8);if((~r.rightsBase&e)!==BigInt(0)||(~r.rightsInheriting&n)!==BigInt(0))throw new _("Capabilities insufficient",76);return r}remove(t){if(t>=this.size)throw new _("Invalid fd",8);const e=this.fds[t];if(!e||e.id!==t)throw new _("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 _("Invalid fd",8);const n=this.fds[t],r=this.fds[e];if(!n||!r||n.id!==t||r.id!==e)throw new _("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 U=e.Memory;class O extends U{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 k(t){return Object.getPrototypeOf(t)===e.Memory.prototype&&Object.setPrototypeOf(t,O.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 x=new WeakMap,M=new WeakMap,W=new WeakMap;function z(t){return x.get(t)}function K(t){const e=W.get(t);if(!e)throw new Error("filesystem is unavailable");return e}function j(t){if(t instanceof _)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 j(t)}return o(t)?t.then((t=>t),j):t}))}function Y(t,e,n,r){let i=d(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 $=new TextEncoder,V=new TextDecoder;class Z{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');x.set(this,k(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}=z(this),i=M.get(this).args;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=$.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}=z(this),r=M.get(this).args;return n.setUint32(t,r.length,!0),n.setUint32(e,$.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}=z(this),i=M.get(this).env;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=$.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}=z(this),r=M.get(this);return n.setUint32(t,r.env.length,!0),n.setUint32(e,$.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}=z(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}=z(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=M.get(this),i=K(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=G("fd_close",(function(t){const e=M.get(this),n=e.fds.get(t,BigInt(0),BigInt(0));return K(this).closeSync(n.fd),e.fds.remove(t),0})),this.fd_datasync=G("fd_datasync",(function(t){const e=M.get(this).fds.get(t,l.FD_DATASYNC,BigInt(0));return K(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=M.get(this).fds.get(t,BigInt(0),BigInt(0)),{view:r}=z(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=M.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=M.get(this).fds.get(t,l.FD_FILESTAT_GET,BigInt(0)),r=K(this).fstatSync(n.fd,{bigint:!0}),{view:i}=z(this);return L(i,e,r),0})),this.fd_filestat_set_size=G("fd_filestat_set_size",(function(t,e){const n=M.get(this).fds.get(t,l.FD_FILESTAT_SET_SIZE,BigInt(0));return K(this).ftruncateSync(n.fd,Number(e)),0})),this.fd_filestat_set_times=G("fd_filestat_set_times",(function(t,e,n,r){const i=M.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 K(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}=z(this),a=M.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=K(this).readSync(a.fd,h,0,h.length,Number(r));return f=h?C(u,h.subarray(0,d)):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=M.get(this);let r;try{r=n.fds.get(t,BigInt(0),BigInt(0))}catch(t){if(t instanceof _)return t.errno;throw t}if(1!==r.preopen)return 28;const{view:i}=z(this);return i.setUint32(e,0,!0),i.setUint32(e+4,$.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=M.get(this).fds.get(t,BigInt(0),BigInt(0));if(1!==r.preopen)return 8;const i=$.encode(r.path+"\0");if(i.length>n)return 42;const{HEAPU8:s}=z(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}=z(this),a=M.get(this).fds.get(t,l.FD_WRITE|l.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=K(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}=z(this),o=M.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){if("undefined"==typeof window||"function"!=typeof window.prompt)return 58;u=function(){const t=window.prompt();return null===t?new Uint8Array:(new TextEncoder).encode(t+"\n")}(),f=u?C(c,u):0}else{u=new Uint8Array(a),u._isBuffer=!0;const t=K(this).readSync(o.fd,u,0,u.length,Number(o.pos));f=u?C(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=M.get(this).fds.get(t,l.FD_SEEK,BigInt(0)).seek(e,n),{view:s}=z(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=M.get(this).fds.get(t,l.FD_READDIR,BigInt(0)),o=K(this),a=o.readdirSync(s.realPath,{withFileTypes:!0}),{HEAPU8:c,view:u}=z(this);let f=0;for(let t=Number(r);t<a.length;t++){const r=$.encode(a[t].name),i=o.statSync(d(s.realPath,a[t].name),{bigint:!0}),u=new Uint8Array(24+r.byteLength),h=new DataView(u.buffer);let l;h.setBigUint64(0,BigInt(t+1),!0),h.setBigUint64(8,BigInt(i.ino?i.ino:0),!0),h.setUint32(16,r.byteLength,!0),l=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,l),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=G("fd_renumber",(function(t,e){return M.get(this).fds.renumber(e,t),0})),this.fd_sync=G("fd_sync",(function(t){const e=M.get(this).fds.get(t,l.FD_SYNC,BigInt(0));return K(this).fsyncSync(e.fd),0})),this.fd_tell=G("fd_tell",(function(t,e){const n=M.get(this).fds.get(t,l.FD_TELL,BigInt(0)),r=BigInt(n.pos),{view:i}=z(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}=z(this),o=M.get(this).fds.get(t,l.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=K(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}=z(this),i=M.get(this).fds.get(t,l.PATH_CREATE_DIRECTORY,BigInt(0));let s=V.decode(r.subarray(e,e+n));s=d(i.realPath,s);return K(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}=z(this),a=M.get(this).fds.get(t,l.PATH_FILESTAT_GET,BigInt(0));let c=V.decode(s.subarray(n,n+r));const u=K(this);let f;return c=d(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}=z(this),c=M.get(this).fds.get(t,l.PATH_FILESTAT_SET_TIMES,BigInt(0)),u=K(this),f=Y(u,c,V.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=M.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}=z(this),h=K(this),_=Y(h,c,V.decode(f.subarray(n,n+r)),e),g=d(u.realPath,V.decode(f.subarray(s,s+o)));return h.linkSync(_,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))&(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=M.get(this),E=g.fds.get(t,d,_),y=z(this),p=y.HEAPU8,I=V.decode(p.subarray(n,n+r)),T=K(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}=z(this),c=M.get(this).fds.get(t,l.PATH_READLINK,BigInt(0));let u=V.decode(o.subarray(e,e+n));u=d(c.realPath,u);const f=K(this).readlinkSync(u),h=$.encode(f),_=Math.min(h.length,i);return _>=i?42:(o.set(h.subarray(0,_),r),o[r+_]=0,a.setUint32(s,_+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}=z(this),i=M.get(this).fds.get(t,l.PATH_REMOVE_DIRECTORY,BigInt(0));let s=V.decode(r.subarray(e,e+n));s=d(i.realPath,s);return K(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=M.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}=z(this),f=d(a.realPath,V.decode(u.subarray(e,e+n))),h=d(c.realPath,V.decode(u.subarray(i,i+s)));return K(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}=z(this),o=M.get(this).fds.get(n,l.PATH_SYMLINK,BigInt(0)),a=V.decode(s.subarray(t,t+e));let c=V.decode(s.subarray(r,r+i));c=d(o.realPath,c);return K(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}=z(this),i=M.get(this).fds.get(t,l.PATH_UNLINK_FILE,BigInt(0));let s=V.decode(r.subarray(e,e+n));s=d(i.realPath,s);return K(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}=z(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}=z(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 c=s?s.fs:void 0,u=new H({size:3,in:i[0],out:i[1],err:i[2],fs:c,print:o,printErr:a});if(M.set(this,{fds:u,args:t,env:n}),c&&W.set(this,c),r.length>0)for(let t=0;t<r.length;++t){const e=c.realpathSync(r[t].realPath,"utf8"),n=c.openSync(e,"r",438);u.insertPreopen(n,r[t].mappedPath,e)}}}const q=Object.freeze(Object.create(null)),X=Symbol("kExitCode"),Q=Symbol("kSetMemory"),J=Symbol("kStarted"),tt=Symbol("kInstance");function et(t,e){n(e,"instance"),n(e.exports,"instance.exports"),t[tt]=e,t[Q](e.exports.memory)}function nt(t){throw this[X]=t,X}t.Asyncify=u,t.Memory=O,t.WASI=class{constructor(t=q){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 c=new Z(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=nt.bind(this))),this[Q]=c._setMemory,delete c._setMemory,this.wasiImport=c,this[J]=!1,this[X]=0,this[tt]=void 0}start(t){if(this[J])throw new Error("WASI instance has already started");this[J]=!0,et(this,t);const{_start:e,_initialize:n}=this[tt].exports;let r;i(e,"instance.exports._start"),s(n,"instance.exports._initialize");try{r=e()}catch(t){if(t!==X)throw t}return r instanceof Promise?r.then((()=>this[X]),(t=>{if(t!==X)throw t;return this[X]})):this[X]}initialize(t){if(this[J])throw new Error("WASI instance has already started");this[J]=!0,et(this,t);const{_start:e,_initialize:n}=this[tt].exports;if(s(e,"instance.exports._start"),void 0!==n)return i(n,"instance.exports._initialize"),n()}},t.WebAssemblyMemory=U,t.extendMemory=k,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 u,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 f(t,n)}else a=await f(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 u,n=s.wrapImports(n));const o=new e.Module(t),a=new e.Instance(o,n),c={instance:a,module:o};if(r){const t=c.instance.exports.memory||(null===(i=n.env)||void 0===i?void 0:i.memory);return{module:c.module,instance:s.init(t,a,r)}}return c},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})}));
|
package/lib/cjs/asyncify.js
CHANGED
|
@@ -141,20 +141,13 @@ class Asyncify {
|
|
|
141
141
|
});
|
|
142
142
|
}
|
|
143
143
|
wrapExports(exports, needWrap) {
|
|
144
|
-
|
|
145
|
-
Object.keys(exports).forEach(name => {
|
|
146
|
-
const exportValue = exports[name];
|
|
144
|
+
return (0, util_1.wrapInstanceExports)(exports, (exportValue, name) => {
|
|
147
145
|
let ignore = ignoreNames.indexOf(name) !== -1 || typeof exportValue !== 'function';
|
|
148
146
|
if (Array.isArray(needWrap)) {
|
|
149
147
|
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
150
148
|
}
|
|
151
|
-
|
|
152
|
-
enumerable: true,
|
|
153
|
-
value: ignore ? exportValue : this.wrapExportFunction(exportValue)
|
|
154
|
-
});
|
|
149
|
+
return ignore ? exportValue : this.wrapExportFunction(exportValue);
|
|
155
150
|
});
|
|
156
|
-
// wrappedExports.set(exports, newExports)
|
|
157
|
-
return newExports;
|
|
158
151
|
}
|
|
159
152
|
}
|
|
160
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
|
package/lib/cjs/jspi.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wrapExports = exports.wrapAsyncExport = exports.wrapAsyncImport = void 0;
|
|
4
|
+
const util_1 = require("./wasi/util");
|
|
5
|
+
const webassembly_1 = require("./webassembly");
|
|
6
|
+
function checkWebAssemblyFunction() {
|
|
7
|
+
const WebAssemblyFunction = webassembly_1._WebAssembly.Function;
|
|
8
|
+
if (typeof WebAssemblyFunction !== 'function') {
|
|
9
|
+
throw new Error('WebAssembly.Function is not supported in this environment.' +
|
|
10
|
+
' If you are using V8 based browser like Chrome, try to specify' +
|
|
11
|
+
' --js-flag="--wasm-staging --experimental-wasm-stack-switching"');
|
|
12
|
+
}
|
|
13
|
+
return WebAssemblyFunction;
|
|
14
|
+
}
|
|
15
|
+
/** @public */
|
|
16
|
+
function wrapAsyncImport(f, parameterType, returnType) {
|
|
17
|
+
const WebAssemblyFunction = checkWebAssemblyFunction();
|
|
18
|
+
if (typeof f !== 'function') {
|
|
19
|
+
throw new TypeError('Function required');
|
|
20
|
+
}
|
|
21
|
+
const parameters = parameterType.slice(0);
|
|
22
|
+
parameters.unshift('externref');
|
|
23
|
+
return new WebAssemblyFunction({ parameters, results: returnType }, f, { suspending: 'first' });
|
|
24
|
+
}
|
|
25
|
+
exports.wrapAsyncImport = wrapAsyncImport;
|
|
26
|
+
/** @public */
|
|
27
|
+
function wrapAsyncExport(f) {
|
|
28
|
+
const WebAssemblyFunction = checkWebAssemblyFunction();
|
|
29
|
+
if (typeof f !== 'function') {
|
|
30
|
+
throw new TypeError('Function required');
|
|
31
|
+
}
|
|
32
|
+
return new WebAssemblyFunction({ parameters: [...WebAssemblyFunction.type(f).parameters.slice(1)], results: ['externref'] }, f, { promising: 'first' });
|
|
33
|
+
}
|
|
34
|
+
exports.wrapAsyncExport = wrapAsyncExport;
|
|
35
|
+
/** @public */
|
|
36
|
+
function wrapExports(exports, needWrap) {
|
|
37
|
+
return (0, util_1.wrapInstanceExports)(exports, (exportValue, name) => {
|
|
38
|
+
let ignore = typeof exportValue !== 'function';
|
|
39
|
+
if (Array.isArray(needWrap)) {
|
|
40
|
+
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
41
|
+
}
|
|
42
|
+
return ignore ? exportValue : wrapAsyncExport(exportValue);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
exports.wrapExports = wrapExports;
|
|
46
|
+
//# sourceMappingURL=jspi.js.map
|
package/lib/cjs/wasi/util.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isPromiseLike = exports.validateInt32 = exports.validateUndefined = exports.validateFunction = exports.validateString = exports.validateBoolean = exports.validateArray = exports.validateObject = void 0;
|
|
3
|
+
exports.wrapInstanceExports = exports.isPromiseLike = exports.validateInt32 = exports.validateUndefined = exports.validateFunction = exports.validateString = exports.validateBoolean = exports.validateArray = exports.validateObject = void 0;
|
|
4
4
|
function validateObject(value, name) {
|
|
5
5
|
if (value === null || typeof value !== 'object') {
|
|
6
6
|
throw new TypeError(`${name} must be an object. Received ${value === null ? 'null' : typeof value}`);
|
|
@@ -53,4 +53,16 @@ function isPromiseLike(obj) {
|
|
|
53
53
|
return !!(obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function');
|
|
54
54
|
}
|
|
55
55
|
exports.isPromiseLike = isPromiseLike;
|
|
56
|
+
function wrapInstanceExports(exports, mapFn) {
|
|
57
|
+
const newExports = Object.create(null);
|
|
58
|
+
Object.keys(exports).forEach(name => {
|
|
59
|
+
const exportValue = exports[name];
|
|
60
|
+
Object.defineProperty(newExports, name, {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
value: mapFn(exportValue, name)
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
return newExports;
|
|
66
|
+
}
|
|
67
|
+
exports.wrapInstanceExports = wrapInstanceExports;
|
|
56
68
|
//# sourceMappingURL=util.js.map
|
package/lib/mjs/asyncify.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { _WebAssembly } from "./webassembly.mjs";
|
|
2
|
-
import { isPromiseLike } from "./wasi/util.mjs";
|
|
2
|
+
import { isPromiseLike, wrapInstanceExports } from "./wasi/util.mjs";
|
|
3
3
|
const ignoreNames = [
|
|
4
4
|
'asyncify_get_state',
|
|
5
5
|
'asyncify_start_rewind',
|
|
@@ -138,20 +138,13 @@ export class Asyncify {
|
|
|
138
138
|
});
|
|
139
139
|
}
|
|
140
140
|
wrapExports(exports, needWrap) {
|
|
141
|
-
|
|
142
|
-
Object.keys(exports).forEach(name => {
|
|
143
|
-
const exportValue = exports[name];
|
|
141
|
+
return wrapInstanceExports(exports, (exportValue, name) => {
|
|
144
142
|
let ignore = ignoreNames.indexOf(name) !== -1 || typeof exportValue !== 'function';
|
|
145
143
|
if (Array.isArray(needWrap)) {
|
|
146
144
|
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
147
145
|
}
|
|
148
|
-
|
|
149
|
-
enumerable: true,
|
|
150
|
-
value: ignore ? exportValue : this.wrapExportFunction(exportValue)
|
|
151
|
-
});
|
|
146
|
+
return ignore ? exportValue : this.wrapExportFunction(exportValue);
|
|
152
147
|
});
|
|
153
|
-
// wrappedExports.set(exports, newExports)
|
|
154
|
-
return newExports;
|
|
155
148
|
}
|
|
156
149
|
}
|
|
157
150
|
// /** @public */
|
package/lib/mjs/index.mjs
CHANGED
package/lib/mjs/jspi.mjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { wrapInstanceExports } from "./wasi/util.mjs";
|
|
2
|
+
import { _WebAssembly } from "./webassembly.mjs";
|
|
3
|
+
function checkWebAssemblyFunction() {
|
|
4
|
+
const WebAssemblyFunction = _WebAssembly.Function;
|
|
5
|
+
if (typeof WebAssemblyFunction !== 'function') {
|
|
6
|
+
throw new Error('WebAssembly.Function is not supported in this environment.' +
|
|
7
|
+
' If you are using V8 based browser like Chrome, try to specify' +
|
|
8
|
+
' --js-flag="--wasm-staging --experimental-wasm-stack-switching"');
|
|
9
|
+
}
|
|
10
|
+
return WebAssemblyFunction;
|
|
11
|
+
}
|
|
12
|
+
/** @public */
|
|
13
|
+
export function wrapAsyncImport(f, parameterType, returnType) {
|
|
14
|
+
const WebAssemblyFunction = checkWebAssemblyFunction();
|
|
15
|
+
if (typeof f !== 'function') {
|
|
16
|
+
throw new TypeError('Function required');
|
|
17
|
+
}
|
|
18
|
+
const parameters = parameterType.slice(0);
|
|
19
|
+
parameters.unshift('externref');
|
|
20
|
+
return new WebAssemblyFunction({ parameters, results: returnType }, f, { suspending: 'first' });
|
|
21
|
+
}
|
|
22
|
+
/** @public */
|
|
23
|
+
export function wrapAsyncExport(f) {
|
|
24
|
+
const WebAssemblyFunction = checkWebAssemblyFunction();
|
|
25
|
+
if (typeof f !== 'function') {
|
|
26
|
+
throw new TypeError('Function required');
|
|
27
|
+
}
|
|
28
|
+
return new WebAssemblyFunction({ parameters: [...WebAssemblyFunction.type(f).parameters.slice(1)], results: ['externref'] }, f, { promising: 'first' });
|
|
29
|
+
}
|
|
30
|
+
/** @public */
|
|
31
|
+
export function wrapExports(exports, needWrap) {
|
|
32
|
+
return wrapInstanceExports(exports, (exportValue, name) => {
|
|
33
|
+
let ignore = typeof exportValue !== 'function';
|
|
34
|
+
if (Array.isArray(needWrap)) {
|
|
35
|
+
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
36
|
+
}
|
|
37
|
+
return ignore ? exportValue : wrapAsyncExport(exportValue);
|
|
38
|
+
});
|
|
39
|
+
}
|
package/lib/mjs/wasi/util.mjs
CHANGED
|
@@ -42,3 +42,14 @@ export function validateInt32(value, name, min = -2147483648, max = 2147483647)
|
|
|
42
42
|
export function isPromiseLike(obj) {
|
|
43
43
|
return !!(obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function');
|
|
44
44
|
}
|
|
45
|
+
export function wrapInstanceExports(exports, mapFn) {
|
|
46
|
+
const newExports = Object.create(null);
|
|
47
|
+
Object.keys(exports).forEach(name => {
|
|
48
|
+
const exportValue = exports[name];
|
|
49
|
+
Object.defineProperty(newExports, name, {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
value: mapFn(exportValue, name)
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
return newExports;
|
|
55
|
+
}
|