@tybys/wasm-util 0.1.0 → 0.2.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/README.md +46 -43
- package/dist/wasm-util.d.ts +7 -1
- package/dist/wasm-util.esm-bundler.js +29 -12
- package/dist/wasm-util.esm.js +29 -12
- package/dist/wasm-util.esm.min.js +1 -1
- package/dist/wasm-util.js +29 -11
- package/dist/wasm-util.min.js +1 -1
- package/lib/cjs/asyncify.js +3 -2
- package/lib/cjs/load.js +10 -6
- package/lib/cjs/memory.js +6 -3
- package/lib/cjs/wasi/preview1.js +6 -1
- package/lib/cjs/webassembly.js +13 -0
- package/lib/mjs/asyncify.mjs +3 -2
- package/lib/mjs/load.mjs +10 -6
- package/lib/mjs/memory.mjs +5 -2
- package/lib/mjs/wasi/preview1.mjs +5 -1
- package/lib/mjs/webassembly.mjs +9 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,6 +8,51 @@ WebAssembly related utils for browser environment
|
|
|
8
8
|
|
|
9
9
|
All example code below need to be bundled by ES module bundlers like `webpack` / `rollup`, or specify import map in browser native ES module runtime.
|
|
10
10
|
|
|
11
|
+
### WASI polyfill for browser
|
|
12
|
+
|
|
13
|
+
The API is similar to the `require('wasi').WASI` in Node.js.
|
|
14
|
+
|
|
15
|
+
You can use `memfs-browser` to provide filesystem capability.
|
|
16
|
+
|
|
17
|
+
- Example: [https://github.com/toyobayashi/wasi-wabt](https://github.com/toyobayashi/wasi-wabt)
|
|
18
|
+
- Demo: [https://toyobayashi.github.io/wasi-wabt/](https://toyobayashi.github.io/wasi-wabt/)
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import { load, WASI } from '@tybys/wasm-util'
|
|
22
|
+
import { Volumn, createFsFromVolume } from 'memfs-browser'
|
|
23
|
+
|
|
24
|
+
const fs = createFsFromVolume(Volume.from({
|
|
25
|
+
'/home/wasi': null
|
|
26
|
+
}))
|
|
27
|
+
|
|
28
|
+
const wasi = new WASI({
|
|
29
|
+
args: ['chrome', 'file.wasm'],
|
|
30
|
+
env: {
|
|
31
|
+
NODE_ENV: 'development',
|
|
32
|
+
WASI_SDK_PATH: '/opt/wasi-sdk'
|
|
33
|
+
},
|
|
34
|
+
preopens: {
|
|
35
|
+
'/': '/'
|
|
36
|
+
},
|
|
37
|
+
filesystem: { type: 'memfs', fs },
|
|
38
|
+
|
|
39
|
+
// redirect stdout / stderr
|
|
40
|
+
|
|
41
|
+
// print (text) { console.log(text) },
|
|
42
|
+
// printErr (text) { console.error(text) }
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
const imports = {
|
|
46
|
+
wasi_snapshot_preview1: wasi.wasiImport
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const { module, instance } = await load('/path/to/file.wasm', imports)
|
|
50
|
+
wasi.start(instance)
|
|
51
|
+
// wasi.initialize(instance)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Implemented syscalls: [wasi_snapshot_preview1](#wasi_snapshot_preview1)
|
|
55
|
+
|
|
11
56
|
### `load` / `loadSync`
|
|
12
57
|
|
|
13
58
|
`loadSync` has 4KB wasm size limit in browser.
|
|
@@ -99,49 +144,7 @@ await p
|
|
|
99
144
|
console.log(Date.now() - now >= 200)
|
|
100
145
|
```
|
|
101
146
|
|
|
102
|
-
###
|
|
103
|
-
|
|
104
|
-
The API is similar to the `require('wasi').WASI` in Node.js.
|
|
105
|
-
|
|
106
|
-
You can use `memfs-browser` to provide filesystem capability.
|
|
107
|
-
|
|
108
|
-
```js
|
|
109
|
-
import { load, WASI } from '@tybys/wasm-util'
|
|
110
|
-
import { Volumn, createFsFromVolume } from 'memfs-browser'
|
|
111
|
-
|
|
112
|
-
const fs = createFsFromVolume(Volume.from({
|
|
113
|
-
'/home/wasi': null
|
|
114
|
-
}))
|
|
115
|
-
|
|
116
|
-
const wasi = new WASI({
|
|
117
|
-
args: ['chrome', 'file.wasm'],
|
|
118
|
-
env: {
|
|
119
|
-
NODE_ENV: 'development',
|
|
120
|
-
WASI_SDK_PATH: '/opt/wasi-sdk'
|
|
121
|
-
},
|
|
122
|
-
preopens: {
|
|
123
|
-
'/': '/'
|
|
124
|
-
},
|
|
125
|
-
filesystem: { type: 'memfs', fs },
|
|
126
|
-
|
|
127
|
-
// redirect stdout / stderr
|
|
128
|
-
|
|
129
|
-
// print (text) { console.log(text) },
|
|
130
|
-
// printErr (text) { console.error(text) }
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
const imports = {
|
|
134
|
-
wasi_snapshot_preview1: wasi.wasiImport
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const { module, instance } = await load('/path/to/file.wasm', imports)
|
|
138
|
-
wasi.start(instance)
|
|
139
|
-
// wasi.initialize(instance)
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
Implemented syscalls:
|
|
143
|
-
|
|
144
|
-
#### wasi_snapshot_preview1
|
|
147
|
+
### wasi_snapshot_preview1
|
|
145
148
|
|
|
146
149
|
- [x] args_get
|
|
147
150
|
- [x] args_sizes_get
|
package/dist/wasm-util.d.ts
CHANGED
|
@@ -64,7 +64,7 @@ export declare function load(urlOrBuffer: string | URL | BufferSource, imports?:
|
|
|
64
64
|
export declare function loadSync(buffer: BufferSource, imports?: WebAssembly.Imports, asyncify?: AsyncifyOptions): WebAssembly.WebAssemblyInstantiatedSource;
|
|
65
65
|
|
|
66
66
|
/** @public */
|
|
67
|
-
export declare class Memory extends
|
|
67
|
+
export declare class Memory extends WebAssemblyMemory {
|
|
68
68
|
constructor(descriptor: WebAssembly.MemoryDescriptor);
|
|
69
69
|
get HEAP8(): Int8Array;
|
|
70
70
|
get HEAPU8(): Uint8Array;
|
|
@@ -108,6 +108,12 @@ export declare interface WASIOptions {
|
|
|
108
108
|
};
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
/** @public */
|
|
112
|
+
export declare const WebAssemblyMemory: {
|
|
113
|
+
new (descriptor: WebAssembly.MemoryDescriptor): WebAssembly.Memory;
|
|
114
|
+
prototype: WebAssembly.Memory;
|
|
115
|
+
};
|
|
116
|
+
|
|
111
117
|
export { }
|
|
112
118
|
|
|
113
119
|
export as namespace wasmUtil;
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
const _WebAssembly = typeof WebAssembly !== 'undefined'
|
|
2
|
+
? WebAssembly
|
|
3
|
+
: typeof WXWebAssembly !== 'undefined'
|
|
4
|
+
? WXWebAssembly
|
|
5
|
+
: undefined;
|
|
6
|
+
if (!_WebAssembly) {
|
|
7
|
+
throw new Error('WebAssembly is not supported in this environment');
|
|
8
|
+
}
|
|
9
|
+
|
|
1
10
|
function validateObject(value, name) {
|
|
2
11
|
if (value === null || typeof value !== 'object') {
|
|
3
12
|
throw new TypeError(`${name} must be an object. Received ${value === null ? 'null' : typeof value}`);
|
|
@@ -69,7 +78,7 @@ class Asyncify {
|
|
|
69
78
|
if (this.exports) {
|
|
70
79
|
throw new Error('Asyncify has been initialized');
|
|
71
80
|
}
|
|
72
|
-
if (!(memory instanceof
|
|
81
|
+
if (!(memory instanceof _WebAssembly.Memory)) {
|
|
73
82
|
throw new TypeError('Require WebAssembly.Memory object');
|
|
74
83
|
}
|
|
75
84
|
const exports = instance.exports;
|
|
@@ -104,7 +113,7 @@ class Asyncify {
|
|
|
104
113
|
new Int32Array(memory.buffer, this.dataPtr).set([address.start, address.end]);
|
|
105
114
|
}
|
|
106
115
|
this.exports = this.wrapExports(exports, options.wrapExports);
|
|
107
|
-
const asyncifiedInstance = Object.create(
|
|
116
|
+
const asyncifiedInstance = Object.create(_WebAssembly.Instance.prototype);
|
|
108
117
|
Object.defineProperty(asyncifiedInstance, 'exports', { value: this.exports });
|
|
109
118
|
// Object.setPrototypeOf(instance, Instance.prototype)
|
|
110
119
|
return asyncifiedInstance;
|
|
@@ -194,9 +203,12 @@ class Asyncify {
|
|
|
194
203
|
// Object.defineProperty(Instance.prototype, 'exports', { enumerable: true })
|
|
195
204
|
|
|
196
205
|
async function fetchWasm(urlOrBuffer, imports) {
|
|
206
|
+
if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
|
|
207
|
+
return await _WebAssembly.instantiate(urlOrBuffer, imports);
|
|
208
|
+
}
|
|
197
209
|
const response = await fetch(urlOrBuffer);
|
|
198
210
|
const buffer = await response.arrayBuffer();
|
|
199
|
-
const source = await
|
|
211
|
+
const source = await _WebAssembly.instantiate(buffer, imports);
|
|
200
212
|
return source;
|
|
201
213
|
}
|
|
202
214
|
/** @public */
|
|
@@ -213,7 +225,7 @@ async function load(urlOrBuffer, imports, asyncify) {
|
|
|
213
225
|
imports = asyncifyHelper.wrapImports(imports);
|
|
214
226
|
}
|
|
215
227
|
if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
|
|
216
|
-
source = await
|
|
228
|
+
source = await _WebAssembly.instantiate(urlOrBuffer, imports);
|
|
217
229
|
if (asyncify) {
|
|
218
230
|
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
|
219
231
|
return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
|
|
@@ -223,9 +235,9 @@ async function load(urlOrBuffer, imports, asyncify) {
|
|
|
223
235
|
if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
|
|
224
236
|
throw new TypeError('Invalid source');
|
|
225
237
|
}
|
|
226
|
-
if (typeof
|
|
238
|
+
if (typeof _WebAssembly.instantiateStreaming === 'function') {
|
|
227
239
|
try {
|
|
228
|
-
source = await
|
|
240
|
+
source = await _WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
|
|
229
241
|
}
|
|
230
242
|
catch (_) {
|
|
231
243
|
source = await fetchWasm(urlOrBuffer, imports);
|
|
@@ -255,8 +267,8 @@ function loadSync(buffer, imports, asyncify) {
|
|
|
255
267
|
asyncifyHelper = new Asyncify();
|
|
256
268
|
imports = asyncifyHelper.wrapImports(imports);
|
|
257
269
|
}
|
|
258
|
-
const module = new
|
|
259
|
-
const instance = new
|
|
270
|
+
const module = new _WebAssembly.Module(buffer);
|
|
271
|
+
const instance = new _WebAssembly.Instance(module, imports);
|
|
260
272
|
const source = { instance, module };
|
|
261
273
|
if (asyncify) {
|
|
262
274
|
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
|
@@ -842,7 +854,9 @@ class FileDescriptorTable {
|
|
|
842
854
|
}
|
|
843
855
|
|
|
844
856
|
/** @public */
|
|
845
|
-
|
|
857
|
+
const WebAssemblyMemory = _WebAssembly.Memory;
|
|
858
|
+
/** @public */
|
|
859
|
+
class Memory extends WebAssemblyMemory {
|
|
846
860
|
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
847
861
|
constructor(descriptor) {
|
|
848
862
|
super(descriptor);
|
|
@@ -861,7 +875,7 @@ class Memory extends WebAssembly.Memory {
|
|
|
861
875
|
}
|
|
862
876
|
/** @public */
|
|
863
877
|
function extendMemory(memory) {
|
|
864
|
-
if (Object.getPrototypeOf(memory) ===
|
|
878
|
+
if (Object.getPrototypeOf(memory) === _WebAssembly.Memory.prototype) {
|
|
865
879
|
Object.setPrototypeOf(memory, Memory.prototype);
|
|
866
880
|
}
|
|
867
881
|
return memory;
|
|
@@ -972,7 +986,7 @@ function readStdin() {
|
|
|
972
986
|
class WASI$1 {
|
|
973
987
|
constructor(args, env, preopens, stdio, filesystem, print, printErr) {
|
|
974
988
|
this._setMemory = function _setMemory(m) {
|
|
975
|
-
if (!(m instanceof
|
|
989
|
+
if (!(m instanceof _WebAssembly.Memory)) {
|
|
976
990
|
throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
|
|
977
991
|
}
|
|
978
992
|
_memory.set(this, extendMemory(m));
|
|
@@ -1279,6 +1293,9 @@ class WASI$1 {
|
|
|
1279
1293
|
let buffer;
|
|
1280
1294
|
let nread = 0;
|
|
1281
1295
|
if (fd === 0) {
|
|
1296
|
+
if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
|
|
1297
|
+
return 58 /* WasiErrno.ENOTSUP */;
|
|
1298
|
+
}
|
|
1282
1299
|
buffer = readStdin();
|
|
1283
1300
|
nread = buffer ? copyMemory(ioVecs, buffer) : 0;
|
|
1284
1301
|
}
|
|
@@ -1876,4 +1893,4 @@ function wasiReturnOnProcExit(rval) {
|
|
|
1876
1893
|
throw kExitCode;
|
|
1877
1894
|
}
|
|
1878
1895
|
|
|
1879
|
-
export { Asyncify, Memory, WASI, extendMemory, load, loadSync };
|
|
1896
|
+
export { Asyncify, Memory, WASI, WebAssemblyMemory, extendMemory, load, loadSync };
|
package/dist/wasm-util.esm.js
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
const _WebAssembly = typeof WebAssembly !== 'undefined'
|
|
2
|
+
? WebAssembly
|
|
3
|
+
: typeof WXWebAssembly !== 'undefined'
|
|
4
|
+
? WXWebAssembly
|
|
5
|
+
: undefined;
|
|
6
|
+
if (!_WebAssembly) {
|
|
7
|
+
throw new Error('WebAssembly is not supported in this environment');
|
|
8
|
+
}
|
|
9
|
+
|
|
1
10
|
function validateObject(value, name) {
|
|
2
11
|
if (value === null || typeof value !== 'object') {
|
|
3
12
|
throw new TypeError(`${name} must be an object. Received ${value === null ? 'null' : typeof value}`);
|
|
@@ -69,7 +78,7 @@ class Asyncify {
|
|
|
69
78
|
if (this.exports) {
|
|
70
79
|
throw new Error('Asyncify has been initialized');
|
|
71
80
|
}
|
|
72
|
-
if (!(memory instanceof
|
|
81
|
+
if (!(memory instanceof _WebAssembly.Memory)) {
|
|
73
82
|
throw new TypeError('Require WebAssembly.Memory object');
|
|
74
83
|
}
|
|
75
84
|
const exports = instance.exports;
|
|
@@ -104,7 +113,7 @@ class Asyncify {
|
|
|
104
113
|
new Int32Array(memory.buffer, this.dataPtr).set([address.start, address.end]);
|
|
105
114
|
}
|
|
106
115
|
this.exports = this.wrapExports(exports, options.wrapExports);
|
|
107
|
-
const asyncifiedInstance = Object.create(
|
|
116
|
+
const asyncifiedInstance = Object.create(_WebAssembly.Instance.prototype);
|
|
108
117
|
Object.defineProperty(asyncifiedInstance, 'exports', { value: this.exports });
|
|
109
118
|
// Object.setPrototypeOf(instance, Instance.prototype)
|
|
110
119
|
return asyncifiedInstance;
|
|
@@ -194,9 +203,12 @@ class Asyncify {
|
|
|
194
203
|
// Object.defineProperty(Instance.prototype, 'exports', { enumerable: true })
|
|
195
204
|
|
|
196
205
|
async function fetchWasm(urlOrBuffer, imports) {
|
|
206
|
+
if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
|
|
207
|
+
return await _WebAssembly.instantiate(urlOrBuffer, imports);
|
|
208
|
+
}
|
|
197
209
|
const response = await fetch(urlOrBuffer);
|
|
198
210
|
const buffer = await response.arrayBuffer();
|
|
199
|
-
const source = await
|
|
211
|
+
const source = await _WebAssembly.instantiate(buffer, imports);
|
|
200
212
|
return source;
|
|
201
213
|
}
|
|
202
214
|
/** @public */
|
|
@@ -213,7 +225,7 @@ async function load(urlOrBuffer, imports, asyncify) {
|
|
|
213
225
|
imports = asyncifyHelper.wrapImports(imports);
|
|
214
226
|
}
|
|
215
227
|
if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
|
|
216
|
-
source = await
|
|
228
|
+
source = await _WebAssembly.instantiate(urlOrBuffer, imports);
|
|
217
229
|
if (asyncify) {
|
|
218
230
|
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
|
219
231
|
return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
|
|
@@ -223,9 +235,9 @@ async function load(urlOrBuffer, imports, asyncify) {
|
|
|
223
235
|
if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
|
|
224
236
|
throw new TypeError('Invalid source');
|
|
225
237
|
}
|
|
226
|
-
if (typeof
|
|
238
|
+
if (typeof _WebAssembly.instantiateStreaming === 'function') {
|
|
227
239
|
try {
|
|
228
|
-
source = await
|
|
240
|
+
source = await _WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
|
|
229
241
|
}
|
|
230
242
|
catch (_) {
|
|
231
243
|
source = await fetchWasm(urlOrBuffer, imports);
|
|
@@ -255,8 +267,8 @@ function loadSync(buffer, imports, asyncify) {
|
|
|
255
267
|
asyncifyHelper = new Asyncify();
|
|
256
268
|
imports = asyncifyHelper.wrapImports(imports);
|
|
257
269
|
}
|
|
258
|
-
const module = new
|
|
259
|
-
const instance = new
|
|
270
|
+
const module = new _WebAssembly.Module(buffer);
|
|
271
|
+
const instance = new _WebAssembly.Instance(module, imports);
|
|
260
272
|
const source = { instance, module };
|
|
261
273
|
if (asyncify) {
|
|
262
274
|
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
|
@@ -842,7 +854,9 @@ class FileDescriptorTable {
|
|
|
842
854
|
}
|
|
843
855
|
|
|
844
856
|
/** @public */
|
|
845
|
-
|
|
857
|
+
const WebAssemblyMemory = _WebAssembly.Memory;
|
|
858
|
+
/** @public */
|
|
859
|
+
class Memory extends WebAssemblyMemory {
|
|
846
860
|
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
847
861
|
constructor(descriptor) {
|
|
848
862
|
super(descriptor);
|
|
@@ -861,7 +875,7 @@ class Memory extends WebAssembly.Memory {
|
|
|
861
875
|
}
|
|
862
876
|
/** @public */
|
|
863
877
|
function extendMemory(memory) {
|
|
864
|
-
if (Object.getPrototypeOf(memory) ===
|
|
878
|
+
if (Object.getPrototypeOf(memory) === _WebAssembly.Memory.prototype) {
|
|
865
879
|
Object.setPrototypeOf(memory, Memory.prototype);
|
|
866
880
|
}
|
|
867
881
|
return memory;
|
|
@@ -972,7 +986,7 @@ function readStdin() {
|
|
|
972
986
|
class WASI$1 {
|
|
973
987
|
constructor(args, env, preopens, stdio, filesystem, print, printErr) {
|
|
974
988
|
this._setMemory = function _setMemory(m) {
|
|
975
|
-
if (!(m instanceof
|
|
989
|
+
if (!(m instanceof _WebAssembly.Memory)) {
|
|
976
990
|
throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
|
|
977
991
|
}
|
|
978
992
|
_memory.set(this, extendMemory(m));
|
|
@@ -1279,6 +1293,9 @@ class WASI$1 {
|
|
|
1279
1293
|
let buffer;
|
|
1280
1294
|
let nread = 0;
|
|
1281
1295
|
if (fd === 0) {
|
|
1296
|
+
if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
|
|
1297
|
+
return 58 /* WasiErrno.ENOTSUP */;
|
|
1298
|
+
}
|
|
1282
1299
|
buffer = readStdin();
|
|
1283
1300
|
nread = buffer ? copyMemory(ioVecs, buffer) : 0;
|
|
1284
1301
|
}
|
|
@@ -1876,4 +1893,4 @@ function wasiReturnOnProcExit(rval) {
|
|
|
1876
1893
|
throw kExitCode;
|
|
1877
1894
|
}
|
|
1878
1895
|
|
|
1879
|
-
export { Asyncify, Memory, WASI, extendMemory, load, loadSync };
|
|
1896
|
+
export { Asyncify, Memory, WASI, WebAssemblyMemory, extendMemory, load, loadSync };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
function t(t,e){if(null===t||"object"!=typeof t)throw new TypeError(`${e} must be an object. Received ${null===t?"null":typeof t}`)}function e(t,e){if("string"!=typeof t)throw new TypeError(`${e} must be a string. Received ${null===t?"null":typeof t}`)}function n(t,e){if("function"!=typeof t)throw new TypeError(`${e} must be a function. Received ${null===t?"null":typeof t}`)}function r(t,e){if(void 0!==t)throw new TypeError(`${e} must be undefined. Received ${null===t?"null":typeof t}`)}function i(t){return!(!t||"object"!=typeof t&&"function"!=typeof t||"function"!=typeof t.then)}const s=["asyncify_get_state","asyncify_start_rewind","asyncify_start_unwind","asyncify_stop_rewind","asyncify_stop_unwind"];function o(t,e,n,r){if("function"!=typeof t.exports[r]||n<=0)return{wasm64:e,dataPtr:16,start:e?32:24,end:1024};const i=t.exports[r],s=e?Number(i(BigInt(16)+BigInt(n))):i(8+n);if(0===s)throw new Error("Allocate asyncify data failed");return e?{wasm64:e,dataPtr:s,start:s+16,end:s+16+n}:{wasm64:e,dataPtr:s,start:s+8,end:s+8+n}}class a{constructor(){this.value=void 0,this.exports=void 0,this.dataPtr=0}init(t,e,n){var r,i;if(this.exports)throw new Error("Asyncify has been initialized");if(!(t instanceof WebAssembly.Memory))throw new TypeError("Require WebAssembly.Memory object");const a=e.exports;for(let t=0;t<s.length;++t)if("function"!=typeof a[s[t]])throw new TypeError("Invalid asyncify wasm");let c;const u=Boolean(n.wasm64);c=n.tryAllocate?!0===n.tryAllocate?o(e,u,4096,"malloc"):o(e,u,null!==(r=n.tryAllocate.size)&&void 0!==r?r:4096,null!==(i=n.tryAllocate.name)&&void 0!==i?i:"malloc"):{wasm64:u,dataPtr:16,start:u?32:24,end:1024},this.dataPtr=c.dataPtr,u?new BigInt64Array(t.buffer,this.dataPtr).set([BigInt(c.start),BigInt(c.end)]):new Int32Array(t.buffer,this.dataPtr).set([c.start,c.end]),this.exports=this.wrapExports(a,n.wrapExports);const f=Object.create(WebAssembly.Instance.prototype);return Object.defineProperty(f,"exports",{value:this.exports}),f}assertState(){if(0!==this.exports.asyncify_get_state())throw new Error("Asyncify state error")}wrapImportFunction(t){return(...e)=>{for(;2===this.exports.asyncify_get_state();)return this.exports.asyncify_stop_rewind(),this.value;this.assertState();const n=t(...e);if(!i(n))return n;this.exports.asyncify_start_unwind(this.dataPtr),this.value=n}}wrapImports(t){const e={};return Object.keys(t).forEach((n=>{const r=t[n],i={};Object.keys(r).forEach((t=>{const e=r[t];i[t]="function"==typeof e?this.wrapImportFunction(e):e})),e[n]=i})),e}wrapExportFunction(t){return async(...e)=>{this.assertState();let n=t(...e);for(;1===this.exports.asyncify_get_state();)this.exports.asyncify_stop_unwind(),this.value=await this.value,this.assertState(),this.exports.asyncify_start_rewind(this.dataPtr),n=t();return this.assertState(),n}}wrapExports(t,e){const n=Object.create(null);return Object.keys(t).forEach((r=>{const i=t[r];let o=-1!==s.indexOf(r)||"function"!=typeof i;Array.isArray(e)&&(o=o||-1===e.indexOf(r)),Object.defineProperty(n,r,{enumerable:!0,value:o?i:this.wrapExportFunction(i)})})),n}}async function c(t,e){const n=await fetch(t),r=await n.arrayBuffer();return await WebAssembly.instantiate(r,e)}async function u(t,e,n){var r,i;if(e&&"object"!=typeof e)throw new TypeError("imports must be an object or undefined");let s,o;if(e=null!=e?e:{},n&&(s=new a,e=s.wrapImports(e)),t instanceof ArrayBuffer||ArrayBuffer.isView(t)){if(o=await WebAssembly.instantiate(t,e),n){const t=o.instance.exports.memory||(null===(r=e.env)||void 0===r?void 0:r.memory);return{module:o.module,instance:s.init(t,o.instance,n)}}return o}if("string"!=typeof t&&!(t instanceof URL))throw new TypeError("Invalid source");if("function"==typeof WebAssembly.instantiateStreaming)try{o=await WebAssembly.instantiateStreaming(fetch(t),e)}catch(n){o=await c(t,e)}else o=await c(t,e);if(n){const t=o.instance.exports.memory||(null===(i=e.env)||void 0===i?void 0:i.memory);return{module:o.module,instance:s.init(t,o.instance,n)}}return o}function f(t,e,n){var r;if(t instanceof ArrayBuffer&&!ArrayBuffer.isView(t))throw new TypeError("Invalid source");if(e&&"object"!=typeof e)throw new TypeError("imports must be an object or undefined");let i;e=null!=e?e:{},n&&(i=new a,e=i.wrapImports(e));const s=new WebAssembly.Module(t),o=new WebAssembly.Instance(s,e),c={instance:o,module:s};if(n){const t=c.instance.exports.memory||(null===(r=e.env)||void 0===r?void 0:r.memory);return{module:c.module,instance:i.init(t,o,n)}}return c}function h(t){return 47===t}function l(...t){let n="",r=!1;for(let i=t.length-1;i>=-1&&!r;i--){const s=i>=0?t[i]:"/";e(s,"path"),0!==s.length&&(n=`${s}/${n}`,r=47===s.charCodeAt(0))}return n=function(t,e,n,r){let i="",s=0,o=-1,a=0,c=0;for(let u=0;u<=t.length;++u){if(u<t.length)c=t.charCodeAt(u);else{if(r(c))break;c=47}if(r(c)){if(o===u-1||1===a);else if(2===a){if(i.length<2||2!==s||46!==i.charCodeAt(i.length-1)||46!==i.charCodeAt(i.length-2)){if(i.length>2){const t=i.indexOf(n);-1===t?(i="",s=0):(i=i.slice(0,t),s=i.length-1-i.indexOf(n)),o=u,a=0;continue}if(0!==i.length){i="",s=0,o=u,a=0;continue}}e&&(i+=i.length>0?`${n}..`:"..",s=2)}else i.length>0?i+=`${n}${t.slice(o+1,u)}`:i=t.slice(o+1,u),s=u-o-1;o=u,a=0}else 46===c&&-1!==a?++a:a=-1}return i}(n,!r,"/",h),r?`/${n}`:n.length>0?n:"."}const _={FD_DATASYNC:BigInt(1)<<BigInt(0),FD_READ:BigInt(1)<<BigInt(1),FD_SEEK:BigInt(1)<<BigInt(2),FD_FDSTAT_SET_FLAGS:BigInt(1)<<BigInt(3),FD_SYNC:BigInt(1)<<BigInt(4),FD_TELL:BigInt(1)<<BigInt(5),FD_WRITE:BigInt(1)<<BigInt(6),FD_ADVISE:BigInt(1)<<BigInt(7),FD_ALLOCATE:BigInt(1)<<BigInt(8),PATH_CREATE_DIRECTORY:BigInt(1)<<BigInt(9),PATH_CREATE_FILE:BigInt(1)<<BigInt(10),PATH_LINK_SOURCE:BigInt(1)<<BigInt(11),PATH_LINK_TARGET:BigInt(1)<<BigInt(12),PATH_OPEN:BigInt(1)<<BigInt(13),FD_READDIR:BigInt(1)<<BigInt(14),PATH_READLINK:BigInt(1)<<BigInt(15),PATH_RENAME_SOURCE:BigInt(1)<<BigInt(16),PATH_RENAME_TARGET:BigInt(1)<<BigInt(17),PATH_FILESTAT_GET:BigInt(1)<<BigInt(18),PATH_FILESTAT_SET_SIZE:BigInt(1)<<BigInt(19),PATH_FILESTAT_SET_TIMES:BigInt(1)<<BigInt(20),FD_FILESTAT_GET:BigInt(1)<<BigInt(21),FD_FILESTAT_SET_SIZE:BigInt(1)<<BigInt(22),FD_FILESTAT_SET_TIMES:BigInt(1)<<BigInt(23),PATH_SYMLINK:BigInt(1)<<BigInt(24),PATH_REMOVE_DIRECTORY:BigInt(1)<<BigInt(25),PATH_UNLINK_FILE:BigInt(1)<<BigInt(26),POLL_FD_READWRITE:BigInt(1)<<BigInt(27),SOCK_SHUTDOWN:BigInt(1)<<BigInt(28),SOCK_ACCEPT:BigInt(1)<<BigInt(29)};class d extends Error{constructor(t,e){super(t),this.errno=e}getErrorMessage(){return function(t){switch(t){case 0:return"Success";case 1:return"Argument list too long";case 2:return"Permission denied";case 3:return"Address in use";case 4:return"Address not available";case 5:return"Address family not supported by protocol";case 6:return"Resource temporarily unavailable";case 7:return"Operation already in progress";case 8:return"Bad file descriptor";case 9:return"Bad message";case 10:return"Resource busy";case 11:return"Operation canceled";case 12:return"No child process";case 13:return"Connection aborted";case 14:return"Connection refused";case 15:return"Connection reset by peer";case 16:return"Resource deadlock would occur";case 17:return"Destination address required";case 18:return"Domain error";case 19:return"Quota exceeded";case 20:return"File exists";case 21:return"Bad address";case 22:return"File too large";case 23:return"Host is unreachable";case 24:return"Identifier removed";case 25:return"Illegal byte sequence";case 26:return"Operation in progress";case 27:return"Interrupted system call";case 28:return"Invalid argument";case 29:return"I/O error";case 30:return"Socket is connected";case 31:return"Is a directory";case 32:return"Symbolic link loop";case 33:return"No file descriptors available";case 34:return"Too many links";case 35:return"Message too large";case 36:return"Multihop attempted";case 37:return"Filename too long";case 38:return"Network is down";case 39:return"Connection reset by network";case 40:return"Network unreachable";case 41:return"Too many files open in system";case 42:return"No buffer space available";case 43:return"No such device";case 44:return"No such file or directory";case 45:return"Exec format error";case 46:return"No locks available";case 47:return"Link has been severed";case 48:return"Out of memory";case 49:return"No message of the desired type";case 50:return"Protocol not available";case 51:return"No space left on device";case 52:return"Function not implemented";case 53:return"Socket not connected";case 54:return"Not a directory";case 55:return"Directory not empty";case 56:return"State not recoverable";case 57:return"Not a socket";case 58:return"Not supported";case 59:return"Not a tty";case 60:return"No such device or address";case 61:return"Value too large for data type";case 62:return"Previous owner died";case 63:return"Operation not permitted";case 64:return"Broken pipe";case 65:return"Protocol error";case 66:return"Protocol not supported";case 67:return"Protocol wrong type for socket";case 68:return"Result not representable";case 69:return"Read-only file system";case 70:return"Invalid seek";case 71:return"No such process";case 72:return"Stale file handle";case 73:return"Operation timed out";case 74:return"Text file busy";case 75:return"Cross-device link";case 76:return"Capabilities insufficient";default:return"Unknown error"}}(this.errno)}}Object.defineProperty(d.prototype,"name",{configurable:!0,writable:!0,value:"WasiError"});const g=_.FD_DATASYNC|_.FD_READ|_.FD_SEEK|_.FD_FDSTAT_SET_FLAGS|_.FD_SYNC|_.FD_TELL|_.FD_WRITE|_.FD_ADVISE|_.FD_ALLOCATE|_.PATH_CREATE_DIRECTORY|_.PATH_CREATE_FILE|_.PATH_LINK_SOURCE|_.PATH_LINK_TARGET|_.PATH_OPEN|_.FD_READDIR|_.PATH_READLINK|_.PATH_RENAME_SOURCE|_.PATH_RENAME_TARGET|_.PATH_FILESTAT_GET|_.PATH_FILESTAT_SET_SIZE|_.PATH_FILESTAT_SET_TIMES|_.FD_FILESTAT_GET|_.FD_FILESTAT_SET_TIMES|_.FD_FILESTAT_SET_SIZE|_.PATH_SYMLINK|_.PATH_UNLINK_FILE|_.PATH_REMOVE_DIRECTORY|_.POLL_FD_READWRITE|_.SOCK_SHUTDOWN,E=g,y=g,p=g,I=g,T=_.FD_DATASYNC|_.FD_READ|_.FD_SEEK|_.FD_FDSTAT_SET_FLAGS|_.FD_SYNC|_.FD_TELL|_.FD_WRITE|_.FD_ADVISE|_.FD_ALLOCATE|_.FD_FILESTAT_GET|_.FD_FILESTAT_SET_SIZE|_.FD_FILESTAT_SET_TIMES|_.POLL_FD_READWRITE,A=BigInt(0),m=_.FD_FDSTAT_SET_FLAGS|_.FD_SYNC|_.FD_ADVISE|_.PATH_CREATE_DIRECTORY|_.PATH_CREATE_FILE|_.PATH_LINK_SOURCE|_.PATH_LINK_TARGET|_.PATH_OPEN|_.FD_READDIR|_.PATH_READLINK|_.PATH_RENAME_SOURCE|_.PATH_RENAME_TARGET|_.PATH_FILESTAT_GET|_.PATH_FILESTAT_SET_SIZE|_.PATH_FILESTAT_SET_TIMES|_.FD_FILESTAT_GET|_.FD_FILESTAT_SET_TIMES|_.PATH_SYMLINK|_.PATH_UNLINK_FILE|_.PATH_REMOVE_DIRECTORY|_.POLL_FD_READWRITE,b=m|T,w=_.FD_READ|_.FD_FDSTAT_SET_FLAGS|_.FD_WRITE|_.FD_FILESTAT_GET|_.POLL_FD_READWRITE|_.SOCK_SHUTDOWN,S=g,B=_.FD_READ|_.FD_FDSTAT_SET_FLAGS|_.FD_WRITE|_.FD_FILESTAT_GET|_.POLL_FD_READWRITE,D=BigInt(0);function N(t,e,n,r){const i={base:BigInt(0),inheriting:BigInt(0)};if(0===r)throw new d("Unknown file type",28);switch(r){case 4:i.base=T,i.inheriting=A;break;case 3:i.base=m,i.inheriting=b;break;case 6:case 5:i.base=w,i.inheriting=S;break;case 2:-1!==t.indexOf(e)?(i.base=B,i.inheriting=D):(i.base=p,i.inheriting=I);break;case 1:i.base=E,i.inheriting=y;break;default:i.base=BigInt(0),i.inheriting=BigInt(0)}const s=3&n;return 0===s?i.base&=~_.FD_WRITE:1===s&&(i.base&=~_.FD_READ),i}function F(t,e){let n=0;if("number"==typeof e&&e>=0)n=e;else for(let e=0;e<t.length;e++){n+=t[e].length}let r=0;const i=new Uint8Array(n);for(let e=0;e<t.length;e++){const n=t[e];i.set(n,r),r+=n.length}return i}class P{constructor(t,e,n,r,i,s,o,a){this.id=t,this.fd=e,this.path=n,this.realPath=r,this.type=i,this.rightsBase=s,this.rightsInheriting=o,this.preopen=a,this.pos=BigInt(0),this.size=BigInt(0)}seek(t,e){if(0===e)this.pos=BigInt(t);else if(1===e)this.pos+=BigInt(t);else{if(2!==e)throw new d("Unknown whence",29);this.pos=BigInt(this.size)-BigInt(t)}return this.pos}}class v extends P{constructor(t,e,n,r,i,s,o,a,c){super(e,n,r,i,s,o,a,c),this._log=t,this._buf=null}write(t){const e=t;if(this._buf&&(t=F([this._buf,t]),this._buf=null),-1===t.indexOf(10))return this._buf=t,e.byteLength;let n,r=0,i=0;for(;-1!==(n=t.indexOf(10,r));){const e=(new TextDecoder).decode(t.subarray(i,n));this._log(e),r+=n-i+1,i=n+1}return r<t.length&&(this._buf=t.slice(r)),e.byteLength}}function R(t){return t.isBlockDevice()?1:t.isCharacterDevice()?2:t.isDirectory()?3:t.isSocket()?6:t.isFile()?4:t.isSymbolicLink()?7:0}function L(t,e,n){t.setBigUint64(e,n.dev,!0),t.setBigUint64(e+8,n.ino,!0),t.setBigUint64(e+16,BigInt(R(n)),!0),t.setBigUint64(e+24,n.nlink,!0),t.setBigUint64(e+32,n.size,!0),t.setBigUint64(e+40,n.atimeMs*BigInt(1e6),!0),t.setBigUint64(e+48,n.mtimeMs*BigInt(1e6),!0),t.setBigUint64(e+56,n.ctimeMs*BigInt(1e6),!0)}class H{constructor(t){this.used=0,this.size=t.size,this.fds=Array(t.size),this.stdio=[t.in,t.out,t.err],this.fs=t.fs,this.print=t.print,this.printErr=t.printErr,this.insertStdio(t.in,0,"<stdin>"),this.insertStdio(t.out,1,"<stdout>"),this.insertStdio(t.err,2,"<stderr>")}insertStdio(t,e,n){const{base:r,inheriting:i}=N(this.stdio,t,2,2),s=this.insert(t,n,n,2,r,i,0);if(s.id!==e)throw new d(`id: ${s.id} !== expected: ${e}`,8);return s}insert(t,e,n,r,i,s,o){var a,c;let u,f=-1;if(this.used>=this.size){const t=2*this.size;this.fds.length=t,f=this.size,this.size=t}else for(let t=0;t<this.size;++t)if(null==this.fds[t]){f=t;break}return u="<stdout>"===e?new v(null!==(a=this.print)&&void 0!==a?a:console.log,f,t,e,n,r,i,s,o):"<stderr>"===e?new v(null!==(c=this.printErr)&&void 0!==c?c:console.error,f,t,e,n,r,i,s,o):new P(f,t,e,n,r,i,s,o),this.fds[f]=u,this.used++,u}getFileTypeByFd(t){return R(this.fs.fstatSync(t))}insertPreopen(t,e,n){const r=this.getFileTypeByFd(t);if(3!==r)throw new d(`Preopen not dir: ["${e}", "${n}"]`,54);const i=N(this.stdio,t,0,r);return this.insert(t,e,n,r,i.base,i.inheriting,1)}get(t,e,n){if(t>=this.size)throw new d("Invalid fd",8);const r=this.fds[t];if(!r||r.id!==t)throw new d("Bad file descriptor",8);if((~r.rightsBase&e)!==BigInt(0)||(~r.rightsInheriting&n)!==BigInt(0))throw new d("Capabilities insufficient",76);return r}remove(t){if(t>=this.size)throw new d("Invalid fd",8);const e=this.fds[t];if(!e||e.id!==t)throw new d("Bad file descriptor",8);this.fds[t]=void 0,this.used--}renumber(t,e){if(t===e)return;if(t>=this.size||e>=this.size)throw new d("Invalid fd",8);const n=this.fds[t],r=this.fds[e];if(!n||!r||n.id!==t||r.id!==e)throw new d("Invalid fd",8);this.fs.closeSync(n.fd),this.fds[t]=this.fds[e],this.fds[t].id=t,this.fds[e]=void 0,this.used--}}class U extends WebAssembly.Memory{constructor(t){super(t)}get HEAP8(){return new Int8Array(super.buffer)}get HEAPU8(){return new Uint8Array(super.buffer)}get HEAP16(){return new Int16Array(super.buffer)}get HEAPU16(){return new Uint16Array(super.buffer)}get HEAP32(){return new Int32Array(super.buffer)}get HEAPU32(){return new Uint32Array(super.buffer)}get HEAP64(){return new BigInt64Array(super.buffer)}get HEAPU64(){return new BigUint64Array(super.buffer)}get HEAPF32(){return new Float32Array(super.buffer)}get HEAPF64(){return new Float64Array(super.buffer)}get view(){return new DataView(super.buffer)}}function O(t){return Object.getPrototypeOf(t)===WebAssembly.Memory.prototype&&Object.setPrototypeOf(t,U.prototype),t}function k(t,e){if(0===t.length||0===e.length)return 0;let n=0,r=e.length-n;for(let i=0;i<t.length;++i){const s=t[i];if(r<s.length)return s.set(e.subarray(n,n+r),0),n+=r,r=0,n;s.set(e.subarray(n,n+s.length),0),n+=s.length,r-=s.length}return n}const C=new WeakMap,x=new WeakMap,M=new WeakMap;function W(t){return C.get(t)}function z(t){const e=M.get(t);if(!e)throw new Error("filesystem is unavailable");return e}function K(t){if(t instanceof d)return t.errno;switch(t.code){case"ENOENT":return 44;case"EBADF":return 8;case"EINVAL":return 28;case"EPERM":return 63;case"EPROTO":return 65;case"EEXIST":return 20;case"ENOTDIR":return 54;case"EMFILE":return 33;case"EACCES":return 2;case"EISDIR":return 31;case"ENOTEMPTY":return 55;case"ENOSYS":return 52}throw t}function G(t,e){return function(t,e){return Object.defineProperty(e,"name",{value:t}),e}(t,(function(){let t;try{t=e.apply(this,arguments)}catch(t){return K(t)}return i(t)?t.then((t=>t),K):t}))}function Y(t,e,n,r){let i=l(e.realPath,n);if(1==(1&r))try{i=t.readlinkSync(i)}catch(t){if("EINVAL"!==t.code&&"ENOENT"!==t.code)throw t}return i}const j=new TextEncoder,$=new TextDecoder;class V{constructor(t,e,n,r,i,s,o){this._setMemory=function(t){if(!(t instanceof WebAssembly.Memory))throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');C.set(this,O(t))},this.args_get=G("args_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{HEAPU8:n,view:r}=W(this),i=x.get(this).args;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=j.encode(o+"\0");n.set(a,e),e+=a.length}return 0})),this.args_sizes_get=G("args_sizes_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{view:n}=W(this),r=x.get(this).args;return n.setUint32(t,r.length,!0),n.setUint32(e,j.encode(r.join("\0")+"\0").length,!0),0})),this.environ_get=G("environ_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{HEAPU8:n,view:r}=W(this),i=x.get(this).env;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=j.encode(o+"\0");n.set(a,e),e+=a.length}return 0})),this.environ_sizes_get=G("environ_sizes_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{view:n}=W(this),r=x.get(this);return n.setUint32(t,r.env.length,!0),n.setUint32(e,j.encode(r.env.join("\0")+"\0").length,!0),0})),this.clock_res_get=G("clock_res_get",(function(t,e){if(0===(e=Number(e)))return 28;const{view:n}=W(this);switch(t){case 0:return n.setBigUint64(e,BigInt(1e6),!0),0;case 1:case 2:case 3:return n.setBigUint64(e,BigInt(1e3),!0),0;default:return 28}})),this.clock_time_get=G("clock_time_get",(function(t,e,n){if(0===(n=Number(n)))return 28;const{view:r}=W(this);switch(t){case 0:return r.setBigUint64(n,BigInt(Date.now())*BigInt(1e6),!0),0;case 1:case 2:case 3:{const t=performance.now(),e=Math.trunc(t),i=Math.floor(1e3*(t-e)),s=BigInt(e)*BigInt(1e9)+BigInt(i)*BigInt(1e6);return r.setBigUint64(n,s,!0),0}default:return 28}})),this.fd_advise=G("fd_advise",(function(t,e,n,r){return 52})),this.fd_allocate=G("fd_allocate",(function(t,e,n){const r=x.get(this),i=z(this),s=r.fds.get(t,_.FD_ALLOCATE,BigInt(0));return i.fstatSync(s.fd,{bigint:!0}).size<e+n&&i.truncateSync(s.fd,Number(e+n)),0})),this.fd_close=G("fd_close",(function(t){const e=x.get(this),n=e.fds.get(t,BigInt(0),BigInt(0));return z(this).closeSync(n.fd),e.fds.remove(t),0})),this.fd_datasync=G("fd_datasync",(function(t){const e=x.get(this).fds.get(t,_.FD_DATASYNC,BigInt(0));return z(this).fdatasyncSync(e.fd),0})),this.fd_fdstat_get=G("fd_fdstat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=x.get(this).fds.get(t,BigInt(0),BigInt(0)),{view:r}=W(this);return r.setUint16(e,n.type,!0),r.setUint16(e+2,0,!0),r.setBigUint64(e+8,n.rightsBase,!0),r.setBigUint64(e+16,n.rightsInheriting,!0),0})),this.fd_fdstat_set_flags=G("fd_fdstat_set_flags",(function(t,e){return 52})),this.fd_fdstat_set_rights=G("fd_fdstat_set_rights",(function(t,e,n){const r=x.get(this).fds.get(t,BigInt(0),BigInt(0));return(e|r.rightsBase)>r.rightsBase||(n|r.rightsInheriting)>r.rightsInheriting?76:(r.rightsBase=e,r.rightsInheriting=n,0)})),this.fd_filestat_get=G("fd_filestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=x.get(this).fds.get(t,_.FD_FILESTAT_GET,BigInt(0)),r=z(this).fstatSync(n.fd,{bigint:!0}),{view:i}=W(this);return L(i,e,r),0})),this.fd_filestat_set_size=G("fd_filestat_set_size",(function(t,e){const n=x.get(this).fds.get(t,_.FD_FILESTAT_SET_SIZE,BigInt(0));return z(this).ftruncateSync(n.fd,Number(e)),0})),this.fd_filestat_set_times=G("fd_filestat_set_times",(function(t,e,n,r){const i=x.get(this).fds.get(t,_.FD_FILESTAT_SET_TIMES,BigInt(0));2==(2&r)&&(e=BigInt(1e6*Date.now())),8==(8&r)&&(n=BigInt(1e6*Date.now()));return z(this).futimesSync(i.fd,Number(e),Number(n)),0})),this.fd_pread=G("fd_pread",(function(t,e,n,r,i){if(e=Number(e),i=Number(i),0===e||0===i)return 28;const{HEAPU8:s,view:o}=W(this),a=x.get(this).fds.get(t,_.FD_READ|_.FD_SEEK,BigInt(0));let c=0;const u=Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,i=o.getInt32(r,!0),a=o.getUint32(r+4,!0);return c+=a,s.subarray(i,i+a)}));let f=0;const h=new Uint8Array(c);h._isBuffer=!0;const l=z(this).readSync(a.fd,h,0,h.length,Number(r));return f=h?k(u,h.subarray(0,l)):0,o.setUint32(i,f,!0),0})),this.fd_prestat_get=G("fd_prestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=x.get(this);let r;try{r=n.fds.get(t,BigInt(0),BigInt(0))}catch(t){if(t instanceof d)return t.errno;throw t}if(1!==r.preopen)return 28;const{view:i}=W(this);return i.setUint32(e,0,!0),i.setUint32(e+4,j.encode(r.path).length+1,!0),0})),this.fd_prestat_dir_name=G("fd_prestat_dir_name",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const r=x.get(this).fds.get(t,BigInt(0),BigInt(0));if(1!==r.preopen)return 8;const i=j.encode(r.path+"\0");if(i.length>n)return 42;const{HEAPU8:s}=W(this);return s.set(i,e),0})),this.fd_pwrite=G("fd_pwrite",(function(t,e,n,r,i){if(e=Number(e),i=Number(i),0===e||0===i)return 28;const{HEAPU8:s,view:o}=W(this),a=x.get(this).fds.get(t,_.FD_WRITE|_.FD_SEEK,BigInt(0)),c=F(Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,i=o.getInt32(r,!0),a=o.getUint32(r+4,!0);return s.subarray(i,i+a)}))),u=z(this).writeSync(a.fd,c,0,c.length,Number(r));return o.setUint32(i,u,!0),0})),this.fd_read=G("fd_read",(function(t,e,n,r){if(e=Number(e),r=Number(r),0===e||0===r)return 28;const{HEAPU8:i,view:s}=W(this),o=x.get(this).fds.get(t,_.FD_READ,BigInt(0));let a=0;const c=Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,o=s.getInt32(r,!0),c=s.getUint32(r+4,!0);return a+=c,i.subarray(o,o+c)}));let u,f=0;if(0===t)u=function(){const t=window.prompt();return null===t?new Uint8Array:(new TextEncoder).encode(t+"\n")}(),f=u?k(c,u):0;else{u=new Uint8Array(a),u._isBuffer=!0;const t=z(this).readSync(o.fd,u,0,u.length,Number(o.pos));f=u?k(c,u.subarray(0,t)):0,o.pos+=BigInt(f)}return s.setUint32(r,f,!0),0})),this.fd_seek=G("fd_seek",(function(t,e,n,r){if(0===(r=Number(r)))return 28;if(0===t||1===t||2===t)return 0;const i=x.get(this).fds.get(t,_.FD_SEEK,BigInt(0)).seek(e,n),{view:s}=W(this);return s.setBigUint64(r,i,!0),0})),this.fd_readdir=G("fd_readdir",(function(t,e,n,r,i){if(e=Number(e),n=Number(n),i=Number(i),0===e||0===i)return 0;const s=x.get(this).fds.get(t,_.FD_READDIR,BigInt(0)),o=z(this),a=o.readdirSync(s.realPath,{withFileTypes:!0}),{HEAPU8:c,view:u}=W(this);let f=0;for(let t=Number(r);t<a.length;t++){const r=j.encode(a[t].name),i=o.statSync(l(s.realPath,a[t].name),{bigint:!0}),u=new Uint8Array(24+r.byteLength),h=new DataView(u.buffer);let _;h.setBigUint64(0,BigInt(t+1),!0),h.setBigUint64(8,BigInt(i.ino?i.ino:0),!0),h.setUint32(16,r.byteLength,!0),_=a[t].isFile()?4:a[t].isDirectory()?3:a[t].isSymbolicLink()?7:a[t].isCharacterDevice()?2:a[t].isBlockDevice()?1:a[t].isSocket()?6:0,h.setUint8(20,_),u.set(r,24);const d=u.slice(0,Math.min(u.length,n-f));c.set(d,e+f),f+=d.byteLength}return u.setUint32(i,f,!0),0})),this.fd_renumber=G("fd_renumber",(function(t,e){return x.get(this).fds.renumber(e,t),0})),this.fd_sync=G("fd_sync",(function(t){const e=x.get(this).fds.get(t,_.FD_SYNC,BigInt(0));return z(this).fsyncSync(e.fd),0})),this.fd_tell=G("fd_tell",(function(t,e){const n=x.get(this).fds.get(t,_.FD_TELL,BigInt(0)),r=BigInt(n.pos),{view:i}=W(this);return i.setBigUint64(Number(e),r,!0),0})),this.fd_write=G("fd_write",(function(t,e,n,r){if(e=Number(e),r=Number(r),0===e||0===r)return 28;const{HEAPU8:i,view:s}=W(this),o=x.get(this).fds.get(t,_.FD_WRITE,BigInt(0)),a=F(Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,o=s.getInt32(r,!0),a=s.getUint32(r+4,!0);return i.subarray(o,o+a)})));let c;if(1===t||2===t)c=o.write(a);else{c=z(this).writeSync(o.fd,a,0,a.length,Number(o.pos)),o.pos+=BigInt(c)}return s.setUint32(r,c,!0),0})),this.path_create_directory=G("path_create_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=W(this),i=x.get(this).fds.get(t,_.PATH_CREATE_DIRECTORY,BigInt(0));let s=$.decode(r.subarray(e,e+n));s=l(i.realPath,s);return z(this).mkdirSync(s),0})),this.path_filestat_get=G("path_filestat_get",(function(t,e,n,r,i){if(n=Number(n),r=Number(r),i=Number(i),0===n||0===i)return 28;const{HEAPU8:s,view:o}=W(this),a=x.get(this).fds.get(t,_.PATH_FILESTAT_GET,BigInt(0));let c=$.decode(s.subarray(n,n+r));const u=z(this);let f;return c=l(a.realPath,c),f=1==(1&e)?u.statSync(c,{bigint:!0}):u.lstatSync(c,{bigint:!0}),L(o,i,f),0})),this.path_filestat_set_times=G("path_filestat_set_times",(function(t,e,n,r,i,s,o){if(n=Number(n),r=Number(r),0===n)return 28;if(-16&o)return 28;const{HEAPU8:a}=W(this),c=x.get(this).fds.get(t,_.PATH_FILESTAT_SET_TIMES,BigInt(0)),u=z(this),f=Y(u,c,$.decode(a.subarray(n,n+r)),e);return 2==(2&o)&&(i=BigInt(1e6*Date.now())),8==(8&o)&&(s=BigInt(1e6*Date.now())),u.utimesSync(f,Number(i),Number(s)),0})),this.path_link=G("path_link",(function(t,e,n,r,i,s,o){if(n=Number(n),r=Number(r),s=Number(s),o=Number(o),0===n||0===s)return 28;const a=x.get(this);let c,u;t===i?c=u=a.fds.get(t,_.PATH_LINK_SOURCE|_.PATH_LINK_TARGET,BigInt(0)):(c=a.fds.get(t,_.PATH_LINK_SOURCE,BigInt(0)),u=a.fds.get(i,_.PATH_LINK_TARGET,BigInt(0)));const{HEAPU8:f}=W(this),h=z(this),d=Y(h,c,$.decode(f.subarray(n,n+r)),e),g=l(u.realPath,$.decode(f.subarray(s,s+o)));return h.linkSync(d,g),0})),this.path_open=G("path_open",(function(t,e,n,r,i,s,o,a,c){if(n=Number(n),c=Number(c),0===n||0===c)return 28;r=Number(r),s=BigInt(s);const u=((s=BigInt(s))&(_.FD_READ|_.FD_READDIR))!==BigInt(0),f=(s&(_.FD_DATASYNC|_.FD_WRITE|_.FD_ALLOCATE|_.FD_FILESTAT_SET_SIZE))!==BigInt(0);let h=f?u?2:1:0,l=_.PATH_OPEN,d=s|o;0!=(1&i)&&(h|=64,l|=_.PATH_CREATE_FILE),0!=(2&i)&&(h|=65536),0!=(4&i)&&(h|=128),0!=(8&i)&&(h|=512,l|=_.PATH_FILESTAT_SET_SIZE),0!=(1&a)&&(h|=1024),0!=(2&a)&&(d|=_.FD_DATASYNC),0!=(4&a)&&(h|=2048),0!=(8&a)&&(h|=1052672,d|=_.FD_SYNC),0!=(16&a)&&(h|=1052672,d|=_.FD_SYNC),f&&0==(1536&h)&&(d|=_.FD_SEEK);const g=x.get(this),E=g.fds.get(t,l,d),y=W(this),p=y.HEAPU8,I=$.decode(p.subarray(n,n+r)),T=z(this),A=Y(T,E,I,e),m=T.openSync(A,h,438),b=g.fds.getFileTypeByFd(m);if(0!=(2&i)&&3!==b)return 54;const{base:w,inheriting:S}=N(g.fds.stdio,m,h,b),B=g.fds.insert(m,A,A,b,s&w,o&S,0),D=T.fstatSync(m,{bigint:!0});D.isFile()&&(B.size=D.size,0!=(1024&h)&&(B.pos=D.size));return y.view.setInt32(c,B.id,!0),0})),this.path_readlink=G("path_readlink",(function(t,e,n,r,i,s){if(e=Number(e),n=Number(n),r=Number(r),i=Number(i),s=Number(s),0===e||0===r||0===s)return 28;const{HEAPU8:o,view:a}=W(this),c=x.get(this).fds.get(t,_.PATH_READLINK,BigInt(0));let u=$.decode(o.subarray(e,e+n));u=l(c.realPath,u);const f=z(this).readlinkSync(u),h=j.encode(f),d=Math.min(h.length,i);return d>=i?42:(o.set(h.subarray(0,d),r),o[r+d]=0,a.setUint32(s,d+1,!0),0)})),this.path_remove_directory=G("path_remove_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=W(this),i=x.get(this).fds.get(t,_.PATH_REMOVE_DIRECTORY,BigInt(0));let s=$.decode(r.subarray(e,e+n));s=l(i.realPath,s);return z(this).rmdirSync(s),0})),this.path_rename=G("path_rename",(function(t,e,n,r,i,s){if(e=Number(e),n=Number(n),i=Number(i),s=Number(s),0===e||0===i)return 28;const o=x.get(this);let a,c;t===r?a=c=o.fds.get(t,_.PATH_RENAME_SOURCE|_.PATH_RENAME_TARGET,BigInt(0)):(a=o.fds.get(t,_.PATH_RENAME_SOURCE,BigInt(0)),c=o.fds.get(r,_.PATH_RENAME_TARGET,BigInt(0)));const{HEAPU8:u}=W(this),f=l(a.realPath,$.decode(u.subarray(e,e+n))),h=l(c.realPath,$.decode(u.subarray(i,i+s)));return z(this).renameSync(f,h),0})),this.path_symlink=G("path_symlink",(function(t,e,n,r,i){if(t=Number(t),e=Number(e),r=Number(r),i=Number(i),0===t||0===r)return 28;const{HEAPU8:s}=W(this),o=x.get(this).fds.get(n,_.PATH_SYMLINK,BigInt(0)),a=$.decode(s.subarray(t,t+e));let c=$.decode(s.subarray(r,r+i));c=l(o.realPath,c);return z(this).symlinkSync(a,c),0})),this.path_unlink_file=G("path_unlink_file",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=W(this),i=x.get(this).fds.get(t,_.PATH_UNLINK_FILE,BigInt(0));let s=$.decode(r.subarray(e,e+n));s=l(i.realPath,s);return z(this).unlinkSync(s),0})),this.poll_oneoff=G("poll_oneoff",(function(t,e,n,r){return 52})),this.proc_exit=G("proc_exit",(function(t){return 0})),this.proc_raise=G("proc_raise",(function(t){return 52})),this.sched_yield=G("sched_yield",(function(){return 0})),this.random_get="undefined"!=typeof crypto&&"function"==typeof crypto.getRandomValues?G("random_get",(function(t,e){if(0===(t=Number(t)))return 28;e=Number(e);const{HEAPU8:n}=W(this);let r;const i=65536;for(r=0;r+i<e;r+=i)crypto.getRandomValues(n.subarray(t+r,t+r+i));return crypto.getRandomValues(n.subarray(t+r,t+e)),0})):G("random_get",(function(t,e){if(0===(t=Number(t)))return 28;e=Number(e);const{view:n}=W(this);for(let r=t;r<t+e;++r)n.setUint8(r,Math.floor(256*Math.random()));return 0})),this.sock_recv=G("sock_recv",(function(){return 58})),this.sock_send=G("sock_send",(function(){return 58})),this.sock_shutdown=G("sock_shutdown",(function(){return 58}));const a=i?i.fs:void 0,c=new H({size:3,in:r[0],out:r[1],err:r[2],fs:a,print:s,printErr:o});if(x.set(this,{fds:c,args:t,env:e}),a&&M.set(this,a),n.length>0)for(let t=0;t<n.length;++t){const e=a.realpathSync(n[t].realPath,"utf8"),r=a.openSync(e,"r",438);c.insertPreopen(r,n[t].mappedPath,e)}}}const Z=Object.freeze(Object.create(null)),q=Symbol("kExitCode"),Q=Symbol("kSetMemory"),X=Symbol("kStarted"),J=Symbol("kInstance");function tt(e,n){t(n,"instance"),t(n.exports,"instance.exports"),e[J]=n,e[Q](n.exports.memory)}class et{constructor(r=Z){var i;t(r,"options"),void 0!==r.args&&function(t,e){if(!Array.isArray(t))throw new TypeError(`${e} must be an array. Received ${null===t?"null":typeof t}`)}(r.args,"options.args");const s=(null!==(i=r.args)&&void 0!==i?i:[]).map(String),o=[];void 0!==r.env&&(t(r.env,"options.env"),Object.entries(r.env).forEach((({0:t,1:e})=>{void 0!==e&&o.push(`${t}=${e}`)})));const a=[];if(void 0!==r.preopens&&(t(r.preopens,"options.preopens"),Object.entries(r.preopens).forEach((({0:t,1:e})=>a.push({mappedPath:String(t),realPath:String(e)})))),a.length>0&&void 0===r.filesystem)throw new Error("filesystem is disabled, can not preopen directory");if(void 0!==r.filesystem){if(t(r.filesystem,"options.filesystem"),e(r.filesystem.type,"options.filesystem.type"),"memfs"!==r.filesystem.type)throw new Error(`Filesystem type ${r.filesystem.type} is not supported, only "memfs" is supported currently`);try{t(r.filesystem.fs,"options.filesystem.fs")}catch(t){throw new Error("Node.js fs like implementation is not provided")}}void 0!==r.print&&n(r.print,"options.print"),void 0!==r.printErr&&n(r.printErr,"options.printErr");const c=new V(s,o,a,[0,1,2],r.filesystem,r.print,r.printErr);for(const t in c)c[t]=c[t].bind(c);void 0!==r.returnOnExit&&(!function(t,e){if("boolean"!=typeof t)throw new TypeError(`${e} must be a boolean. Received ${null===t?"null":typeof t}`)}(r.returnOnExit,"options.returnOnExit"),r.returnOnExit&&(c.proc_exit=nt.bind(this))),this[Q]=c._setMemory,delete c._setMemory,this.wasiImport=c,this[X]=!1,this[q]=0,this[J]=void 0}start(t){if(this[X])throw new Error("WASI instance has already started");this[X]=!0,tt(this,t);const{_start:e,_initialize:i}=this[J].exports;let s;n(e,"instance.exports._start"),r(i,"instance.exports._initialize");try{s=e()}catch(t){if(t!==q)throw t}return s instanceof Promise?s.then((()=>this[q]),(t=>{if(t!==q)throw t;return this[q]})):this[q]}initialize(t){if(this[X])throw new Error("WASI instance has already started");this[X]=!0,tt(this,t);const{_start:e,_initialize:i}=this[J].exports;if(r(e,"instance.exports._start"),void 0!==i)return n(i,"instance.exports._initialize"),i()}}function nt(t){throw this[q]=t,q}export{a as Asyncify,U as Memory,et as WASI,O as extendMemory,u as load,f as loadSync};
|
|
1
|
+
const t="undefined"!=typeof WebAssembly?WebAssembly:"undefined"!=typeof WXWebAssembly?WXWebAssembly:void 0;if(!t)throw new Error("WebAssembly is not supported in this environment");function e(t,e){if(null===t||"object"!=typeof t)throw new TypeError(`${e} must be an object. Received ${null===t?"null":typeof t}`)}function n(t,e){if("string"!=typeof t)throw new TypeError(`${e} must be a string. Received ${null===t?"null":typeof t}`)}function r(t,e){if("function"!=typeof t)throw new TypeError(`${e} must be a function. Received ${null===t?"null":typeof t}`)}function i(t,e){if(void 0!==t)throw new TypeError(`${e} must be undefined. Received ${null===t?"null":typeof t}`)}function s(t){return!(!t||"object"!=typeof t&&"function"!=typeof t||"function"!=typeof t.then)}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};
|
package/dist/wasm-util.js
CHANGED
|
@@ -4,6 +4,15 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.wasmUtil = {}));
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
+
const _WebAssembly = typeof WebAssembly !== 'undefined'
|
|
8
|
+
? WebAssembly
|
|
9
|
+
: typeof WXWebAssembly !== 'undefined'
|
|
10
|
+
? WXWebAssembly
|
|
11
|
+
: undefined;
|
|
12
|
+
if (!_WebAssembly) {
|
|
13
|
+
throw new Error('WebAssembly is not supported in this environment');
|
|
14
|
+
}
|
|
15
|
+
|
|
7
16
|
function validateObject(value, name) {
|
|
8
17
|
if (value === null || typeof value !== 'object') {
|
|
9
18
|
throw new TypeError(`${name} must be an object. Received ${value === null ? 'null' : typeof value}`);
|
|
@@ -75,7 +84,7 @@
|
|
|
75
84
|
if (this.exports) {
|
|
76
85
|
throw new Error('Asyncify has been initialized');
|
|
77
86
|
}
|
|
78
|
-
if (!(memory instanceof
|
|
87
|
+
if (!(memory instanceof _WebAssembly.Memory)) {
|
|
79
88
|
throw new TypeError('Require WebAssembly.Memory object');
|
|
80
89
|
}
|
|
81
90
|
const exports = instance.exports;
|
|
@@ -110,7 +119,7 @@
|
|
|
110
119
|
new Int32Array(memory.buffer, this.dataPtr).set([address.start, address.end]);
|
|
111
120
|
}
|
|
112
121
|
this.exports = this.wrapExports(exports, options.wrapExports);
|
|
113
|
-
const asyncifiedInstance = Object.create(
|
|
122
|
+
const asyncifiedInstance = Object.create(_WebAssembly.Instance.prototype);
|
|
114
123
|
Object.defineProperty(asyncifiedInstance, 'exports', { value: this.exports });
|
|
115
124
|
// Object.setPrototypeOf(instance, Instance.prototype)
|
|
116
125
|
return asyncifiedInstance;
|
|
@@ -200,9 +209,12 @@
|
|
|
200
209
|
// Object.defineProperty(Instance.prototype, 'exports', { enumerable: true })
|
|
201
210
|
|
|
202
211
|
async function fetchWasm(urlOrBuffer, imports) {
|
|
212
|
+
if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
|
|
213
|
+
return await _WebAssembly.instantiate(urlOrBuffer, imports);
|
|
214
|
+
}
|
|
203
215
|
const response = await fetch(urlOrBuffer);
|
|
204
216
|
const buffer = await response.arrayBuffer();
|
|
205
|
-
const source = await
|
|
217
|
+
const source = await _WebAssembly.instantiate(buffer, imports);
|
|
206
218
|
return source;
|
|
207
219
|
}
|
|
208
220
|
/** @public */
|
|
@@ -219,7 +231,7 @@
|
|
|
219
231
|
imports = asyncifyHelper.wrapImports(imports);
|
|
220
232
|
}
|
|
221
233
|
if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
|
|
222
|
-
source = await
|
|
234
|
+
source = await _WebAssembly.instantiate(urlOrBuffer, imports);
|
|
223
235
|
if (asyncify) {
|
|
224
236
|
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
|
225
237
|
return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
|
|
@@ -229,9 +241,9 @@
|
|
|
229
241
|
if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
|
|
230
242
|
throw new TypeError('Invalid source');
|
|
231
243
|
}
|
|
232
|
-
if (typeof
|
|
244
|
+
if (typeof _WebAssembly.instantiateStreaming === 'function') {
|
|
233
245
|
try {
|
|
234
|
-
source = await
|
|
246
|
+
source = await _WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
|
|
235
247
|
}
|
|
236
248
|
catch (_) {
|
|
237
249
|
source = await fetchWasm(urlOrBuffer, imports);
|
|
@@ -261,8 +273,8 @@
|
|
|
261
273
|
asyncifyHelper = new Asyncify();
|
|
262
274
|
imports = asyncifyHelper.wrapImports(imports);
|
|
263
275
|
}
|
|
264
|
-
const module = new
|
|
265
|
-
const instance = new
|
|
276
|
+
const module = new _WebAssembly.Module(buffer);
|
|
277
|
+
const instance = new _WebAssembly.Instance(module, imports);
|
|
266
278
|
const source = { instance, module };
|
|
267
279
|
if (asyncify) {
|
|
268
280
|
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
|
@@ -848,7 +860,9 @@
|
|
|
848
860
|
}
|
|
849
861
|
|
|
850
862
|
/** @public */
|
|
851
|
-
|
|
863
|
+
const WebAssemblyMemory = _WebAssembly.Memory;
|
|
864
|
+
/** @public */
|
|
865
|
+
class Memory extends WebAssemblyMemory {
|
|
852
866
|
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
853
867
|
constructor(descriptor) {
|
|
854
868
|
super(descriptor);
|
|
@@ -867,7 +881,7 @@
|
|
|
867
881
|
}
|
|
868
882
|
/** @public */
|
|
869
883
|
function extendMemory(memory) {
|
|
870
|
-
if (Object.getPrototypeOf(memory) ===
|
|
884
|
+
if (Object.getPrototypeOf(memory) === _WebAssembly.Memory.prototype) {
|
|
871
885
|
Object.setPrototypeOf(memory, Memory.prototype);
|
|
872
886
|
}
|
|
873
887
|
return memory;
|
|
@@ -978,7 +992,7 @@
|
|
|
978
992
|
class WASI$1 {
|
|
979
993
|
constructor(args, env, preopens, stdio, filesystem, print, printErr) {
|
|
980
994
|
this._setMemory = function _setMemory(m) {
|
|
981
|
-
if (!(m instanceof
|
|
995
|
+
if (!(m instanceof _WebAssembly.Memory)) {
|
|
982
996
|
throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
|
|
983
997
|
}
|
|
984
998
|
_memory.set(this, extendMemory(m));
|
|
@@ -1285,6 +1299,9 @@
|
|
|
1285
1299
|
let buffer;
|
|
1286
1300
|
let nread = 0;
|
|
1287
1301
|
if (fd === 0) {
|
|
1302
|
+
if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
|
|
1303
|
+
return 58 /* WasiErrno.ENOTSUP */;
|
|
1304
|
+
}
|
|
1288
1305
|
buffer = readStdin();
|
|
1289
1306
|
nread = buffer ? copyMemory(ioVecs, buffer) : 0;
|
|
1290
1307
|
}
|
|
@@ -1885,6 +1902,7 @@
|
|
|
1885
1902
|
exports.Asyncify = Asyncify;
|
|
1886
1903
|
exports.Memory = Memory;
|
|
1887
1904
|
exports.WASI = WASI;
|
|
1905
|
+
exports.WebAssemblyMemory = WebAssemblyMemory;
|
|
1888
1906
|
exports.extendMemory = extendMemory;
|
|
1889
1907
|
exports.load = load;
|
|
1890
1908
|
exports.loadSync = loadSync;
|
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";function e(t,e){if(null===t||"object"!=typeof t)throw new TypeError(`${e} must be an object. Received ${null===t?"null":typeof t}`)}function n(t,e){if("string"!=typeof t)throw new TypeError(`${e} must be a string. Received ${null===t?"null":typeof t}`)}function r(t,e){if("function"!=typeof t)throw new TypeError(`${e} must be a function. Received ${null===t?"null":typeof t}`)}function i(t,e){if(void 0!==t)throw new TypeError(`${e} must be undefined. Received ${null===t?"null":typeof t}`)}function s(t){return!(!t||"object"!=typeof t&&"function"!=typeof t||"function"!=typeof t.then)}const o=["asyncify_get_state","asyncify_start_rewind","asyncify_start_unwind","asyncify_stop_rewind","asyncify_stop_unwind"];function a(t,e,n,r){if("function"!=typeof t.exports[r]||n<=0)return{wasm64:e,dataPtr:16,start:e?32:24,end:1024};const i=t.exports[r],s=e?Number(i(BigInt(16)+BigInt(n))):i(8+n);if(0===s)throw new Error("Allocate asyncify data failed");return e?{wasm64:e,dataPtr:s,start:s+16,end:s+16+n}:{wasm64:e,dataPtr:s,start:s+8,end:s+8+n}}class c{constructor(){this.value=void 0,this.exports=void 0,this.dataPtr=0}init(t,e,n){var r,i;if(this.exports)throw new Error("Asyncify has been initialized");if(!(t instanceof WebAssembly.Memory))throw new TypeError("Require WebAssembly.Memory object");const s=e.exports;for(let t=0;t<o.length;++t)if("function"!=typeof s[o[t]])throw new TypeError("Invalid asyncify wasm");let c;const u=Boolean(n.wasm64);c=n.tryAllocate?!0===n.tryAllocate?a(e,u,4096,"malloc"):a(e,u,null!==(r=n.tryAllocate.size)&&void 0!==r?r:4096,null!==(i=n.tryAllocate.name)&&void 0!==i?i:"malloc"):{wasm64:u,dataPtr:16,start:u?32:24,end:1024},this.dataPtr=c.dataPtr,u?new BigInt64Array(t.buffer,this.dataPtr).set([BigInt(c.start),BigInt(c.end)]):new Int32Array(t.buffer,this.dataPtr).set([c.start,c.end]),this.exports=this.wrapExports(s,n.wrapExports);const f=Object.create(WebAssembly.Instance.prototype);return Object.defineProperty(f,"exports",{value:this.exports}),f}assertState(){if(0!==this.exports.asyncify_get_state())throw new Error("Asyncify state error")}wrapImportFunction(t){return(...e)=>{for(;2===this.exports.asyncify_get_state();)return this.exports.asyncify_stop_rewind(),this.value;this.assertState();const n=t(...e);if(!s(n))return n;this.exports.asyncify_start_unwind(this.dataPtr),this.value=n}}wrapImports(t){const e={};return Object.keys(t).forEach((n=>{const r=t[n],i={};Object.keys(r).forEach((t=>{const e=r[t];i[t]="function"==typeof e?this.wrapImportFunction(e):e})),e[n]=i})),e}wrapExportFunction(t){return async(...e)=>{this.assertState();let n=t(...e);for(;1===this.exports.asyncify_get_state();)this.exports.asyncify_stop_unwind(),this.value=await this.value,this.assertState(),this.exports.asyncify_start_rewind(this.dataPtr),n=t();return this.assertState(),n}}wrapExports(t,e){const n=Object.create(null);return Object.keys(t).forEach((r=>{const i=t[r];let s=-1!==o.indexOf(r)||"function"!=typeof i;Array.isArray(e)&&(s=s||-1===e.indexOf(r)),Object.defineProperty(n,r,{enumerable:!0,value:s?i:this.wrapExportFunction(i)})})),n}}async function u(t,e){const n=await fetch(t),r=await n.arrayBuffer();return await WebAssembly.instantiate(r,e)}function f(t){return 47===t}function h(...t){let e="",r=!1;for(let i=t.length-1;i>=-1&&!r;i--){const s=i>=0?t[i]:"/";n(s,"path"),0!==s.length&&(e=`${s}/${e}`,r=47===s.charCodeAt(0))}return e=function(t,e,n,r){let i="",s=0,o=-1,a=0,c=0;for(let u=0;u<=t.length;++u){if(u<t.length)c=t.charCodeAt(u);else{if(r(c))break;c=47}if(r(c)){if(o===u-1||1===a);else if(2===a){if(i.length<2||2!==s||46!==i.charCodeAt(i.length-1)||46!==i.charCodeAt(i.length-2)){if(i.length>2){const t=i.indexOf(n);-1===t?(i="",s=0):(i=i.slice(0,t),s=i.length-1-i.indexOf(n)),o=u,a=0;continue}if(0!==i.length){i="",s=0,o=u,a=0;continue}}e&&(i+=i.length>0?`${n}..`:"..",s=2)}else i.length>0?i+=`${n}${t.slice(o+1,u)}`:i=t.slice(o+1,u),s=u-o-1;o=u,a=0}else 46===c&&-1!==a?++a:a=-1}return i}(e,!r,"/",f),r?`/${e}`:e.length>0?e:"."}const l={FD_DATASYNC:BigInt(1)<<BigInt(0),FD_READ:BigInt(1)<<BigInt(1),FD_SEEK:BigInt(1)<<BigInt(2),FD_FDSTAT_SET_FLAGS:BigInt(1)<<BigInt(3),FD_SYNC:BigInt(1)<<BigInt(4),FD_TELL:BigInt(1)<<BigInt(5),FD_WRITE:BigInt(1)<<BigInt(6),FD_ADVISE:BigInt(1)<<BigInt(7),FD_ALLOCATE:BigInt(1)<<BigInt(8),PATH_CREATE_DIRECTORY:BigInt(1)<<BigInt(9),PATH_CREATE_FILE:BigInt(1)<<BigInt(10),PATH_LINK_SOURCE:BigInt(1)<<BigInt(11),PATH_LINK_TARGET:BigInt(1)<<BigInt(12),PATH_OPEN:BigInt(1)<<BigInt(13),FD_READDIR:BigInt(1)<<BigInt(14),PATH_READLINK:BigInt(1)<<BigInt(15),PATH_RENAME_SOURCE:BigInt(1)<<BigInt(16),PATH_RENAME_TARGET:BigInt(1)<<BigInt(17),PATH_FILESTAT_GET:BigInt(1)<<BigInt(18),PATH_FILESTAT_SET_SIZE:BigInt(1)<<BigInt(19),PATH_FILESTAT_SET_TIMES:BigInt(1)<<BigInt(20),FD_FILESTAT_GET:BigInt(1)<<BigInt(21),FD_FILESTAT_SET_SIZE:BigInt(1)<<BigInt(22),FD_FILESTAT_SET_TIMES:BigInt(1)<<BigInt(23),PATH_SYMLINK:BigInt(1)<<BigInt(24),PATH_REMOVE_DIRECTORY:BigInt(1)<<BigInt(25),PATH_UNLINK_FILE:BigInt(1)<<BigInt(26),POLL_FD_READWRITE:BigInt(1)<<BigInt(27),SOCK_SHUTDOWN:BigInt(1)<<BigInt(28),SOCK_ACCEPT:BigInt(1)<<BigInt(29)};class d extends Error{constructor(t,e){super(t),this.errno=e}getErrorMessage(){return function(t){switch(t){case 0:return"Success";case 1:return"Argument list too long";case 2:return"Permission denied";case 3:return"Address in use";case 4:return"Address not available";case 5:return"Address family not supported by protocol";case 6:return"Resource temporarily unavailable";case 7:return"Operation already in progress";case 8:return"Bad file descriptor";case 9:return"Bad message";case 10:return"Resource busy";case 11:return"Operation canceled";case 12:return"No child process";case 13:return"Connection aborted";case 14:return"Connection refused";case 15:return"Connection reset by peer";case 16:return"Resource deadlock would occur";case 17:return"Destination address required";case 18:return"Domain error";case 19:return"Quota exceeded";case 20:return"File exists";case 21:return"Bad address";case 22:return"File too large";case 23:return"Host is unreachable";case 24:return"Identifier removed";case 25:return"Illegal byte sequence";case 26:return"Operation in progress";case 27:return"Interrupted system call";case 28:return"Invalid argument";case 29:return"I/O error";case 30:return"Socket is connected";case 31:return"Is a directory";case 32:return"Symbolic link loop";case 33:return"No file descriptors available";case 34:return"Too many links";case 35:return"Message too large";case 36:return"Multihop attempted";case 37:return"Filename too long";case 38:return"Network is down";case 39:return"Connection reset by network";case 40:return"Network unreachable";case 41:return"Too many files open in system";case 42:return"No buffer space available";case 43:return"No such device";case 44:return"No such file or directory";case 45:return"Exec format error";case 46:return"No locks available";case 47:return"Link has been severed";case 48:return"Out of memory";case 49:return"No message of the desired type";case 50:return"Protocol not available";case 51:return"No space left on device";case 52:return"Function not implemented";case 53:return"Socket not connected";case 54:return"Not a directory";case 55:return"Directory not empty";case 56:return"State not recoverable";case 57:return"Not a socket";case 58:return"Not supported";case 59:return"Not a tty";case 60:return"No such device or address";case 61:return"Value too large for data type";case 62:return"Previous owner died";case 63:return"Operation not permitted";case 64:return"Broken pipe";case 65:return"Protocol error";case 66:return"Protocol not supported";case 67:return"Protocol wrong type for socket";case 68:return"Result not representable";case 69:return"Read-only file system";case 70:return"Invalid seek";case 71:return"No such process";case 72:return"Stale file handle";case 73:return"Operation timed out";case 74:return"Text file busy";case 75:return"Cross-device link";case 76:return"Capabilities insufficient";default:return"Unknown error"}}(this.errno)}}Object.defineProperty(d.prototype,"name",{configurable:!0,writable:!0,value:"WasiError"});const _=l.FD_DATASYNC|l.FD_READ|l.FD_SEEK|l.FD_FDSTAT_SET_FLAGS|l.FD_SYNC|l.FD_TELL|l.FD_WRITE|l.FD_ADVISE|l.FD_ALLOCATE|l.PATH_CREATE_DIRECTORY|l.PATH_CREATE_FILE|l.PATH_LINK_SOURCE|l.PATH_LINK_TARGET|l.PATH_OPEN|l.FD_READDIR|l.PATH_READLINK|l.PATH_RENAME_SOURCE|l.PATH_RENAME_TARGET|l.PATH_FILESTAT_GET|l.PATH_FILESTAT_SET_SIZE|l.PATH_FILESTAT_SET_TIMES|l.FD_FILESTAT_GET|l.FD_FILESTAT_SET_TIMES|l.FD_FILESTAT_SET_SIZE|l.PATH_SYMLINK|l.PATH_UNLINK_FILE|l.PATH_REMOVE_DIRECTORY|l.POLL_FD_READWRITE|l.SOCK_SHUTDOWN,g=_,E=_,y=_,p=_,I=l.FD_DATASYNC|l.FD_READ|l.FD_SEEK|l.FD_FDSTAT_SET_FLAGS|l.FD_SYNC|l.FD_TELL|l.FD_WRITE|l.FD_ADVISE|l.FD_ALLOCATE|l.FD_FILESTAT_GET|l.FD_FILESTAT_SET_SIZE|l.FD_FILESTAT_SET_TIMES|l.POLL_FD_READWRITE,T=BigInt(0),A=l.FD_FDSTAT_SET_FLAGS|l.FD_SYNC|l.FD_ADVISE|l.PATH_CREATE_DIRECTORY|l.PATH_CREATE_FILE|l.PATH_LINK_SOURCE|l.PATH_LINK_TARGET|l.PATH_OPEN|l.FD_READDIR|l.PATH_READLINK|l.PATH_RENAME_SOURCE|l.PATH_RENAME_TARGET|l.PATH_FILESTAT_GET|l.PATH_FILESTAT_SET_SIZE|l.PATH_FILESTAT_SET_TIMES|l.FD_FILESTAT_GET|l.FD_FILESTAT_SET_TIMES|l.PATH_SYMLINK|l.PATH_UNLINK_FILE|l.PATH_REMOVE_DIRECTORY|l.POLL_FD_READWRITE,m=A|I,b=l.FD_READ|l.FD_FDSTAT_SET_FLAGS|l.FD_WRITE|l.FD_FILESTAT_GET|l.POLL_FD_READWRITE|l.SOCK_SHUTDOWN,w=_,S=l.FD_READ|l.FD_FDSTAT_SET_FLAGS|l.FD_WRITE|l.FD_FILESTAT_GET|l.POLL_FD_READWRITE,B=BigInt(0);function D(t,e,n,r){const i={base:BigInt(0),inheriting:BigInt(0)};if(0===r)throw new d("Unknown file type",28);switch(r){case 4:i.base=I,i.inheriting=T;break;case 3:i.base=A,i.inheriting=m;break;case 6:case 5:i.base=b,i.inheriting=w;break;case 2:-1!==t.indexOf(e)?(i.base=S,i.inheriting=B):(i.base=y,i.inheriting=p);break;case 1:i.base=g,i.inheriting=E;break;default:i.base=BigInt(0),i.inheriting=BigInt(0)}const s=3&n;return 0===s?i.base&=~l.FD_WRITE:1===s&&(i.base&=~l.FD_READ),i}function N(t,e){let n=0;if("number"==typeof e&&e>=0)n=e;else for(let e=0;e<t.length;e++){n+=t[e].length}let r=0;const i=new Uint8Array(n);for(let e=0;e<t.length;e++){const n=t[e];i.set(n,r),r+=n.length}return i}class F{constructor(t,e,n,r,i,s,o,a){this.id=t,this.fd=e,this.path=n,this.realPath=r,this.type=i,this.rightsBase=s,this.rightsInheriting=o,this.preopen=a,this.pos=BigInt(0),this.size=BigInt(0)}seek(t,e){if(0===e)this.pos=BigInt(t);else if(1===e)this.pos+=BigInt(t);else{if(2!==e)throw new d("Unknown whence",29);this.pos=BigInt(this.size)-BigInt(t)}return this.pos}}class P extends F{constructor(t,e,n,r,i,s,o,a,c){super(e,n,r,i,s,o,a,c),this._log=t,this._buf=null}write(t){const e=t;if(this._buf&&(t=N([this._buf,t]),this._buf=null),-1===t.indexOf(10))return this._buf=t,e.byteLength;let n,r=0,i=0;for(;-1!==(n=t.indexOf(10,r));){const e=(new TextDecoder).decode(t.subarray(i,n));this._log(e),r+=n-i+1,i=n+1}return r<t.length&&(this._buf=t.slice(r)),e.byteLength}}function v(t){return t.isBlockDevice()?1:t.isCharacterDevice()?2:t.isDirectory()?3:t.isSocket()?6:t.isFile()?4:t.isSymbolicLink()?7:0}function R(t,e,n){t.setBigUint64(e,n.dev,!0),t.setBigUint64(e+8,n.ino,!0),t.setBigUint64(e+16,BigInt(v(n)),!0),t.setBigUint64(e+24,n.nlink,!0),t.setBigUint64(e+32,n.size,!0),t.setBigUint64(e+40,n.atimeMs*BigInt(1e6),!0),t.setBigUint64(e+48,n.mtimeMs*BigInt(1e6),!0),t.setBigUint64(e+56,n.ctimeMs*BigInt(1e6),!0)}class L{constructor(t){this.used=0,this.size=t.size,this.fds=Array(t.size),this.stdio=[t.in,t.out,t.err],this.fs=t.fs,this.print=t.print,this.printErr=t.printErr,this.insertStdio(t.in,0,"<stdin>"),this.insertStdio(t.out,1,"<stdout>"),this.insertStdio(t.err,2,"<stderr>")}insertStdio(t,e,n){const{base:r,inheriting:i}=D(this.stdio,t,2,2),s=this.insert(t,n,n,2,r,i,0);if(s.id!==e)throw new d(`id: ${s.id} !== expected: ${e}`,8);return s}insert(t,e,n,r,i,s,o){var a,c;let u,f=-1;if(this.used>=this.size){const t=2*this.size;this.fds.length=t,f=this.size,this.size=t}else for(let t=0;t<this.size;++t)if(null==this.fds[t]){f=t;break}return u="<stdout>"===e?new P(null!==(a=this.print)&&void 0!==a?a:console.log,f,t,e,n,r,i,s,o):"<stderr>"===e?new P(null!==(c=this.printErr)&&void 0!==c?c:console.error,f,t,e,n,r,i,s,o):new F(f,t,e,n,r,i,s,o),this.fds[f]=u,this.used++,u}getFileTypeByFd(t){return v(this.fs.fstatSync(t))}insertPreopen(t,e,n){const r=this.getFileTypeByFd(t);if(3!==r)throw new d(`Preopen not dir: ["${e}", "${n}"]`,54);const i=D(this.stdio,t,0,r);return this.insert(t,e,n,r,i.base,i.inheriting,1)}get(t,e,n){if(t>=this.size)throw new d("Invalid fd",8);const r=this.fds[t];if(!r||r.id!==t)throw new d("Bad file descriptor",8);if((~r.rightsBase&e)!==BigInt(0)||(~r.rightsInheriting&n)!==BigInt(0))throw new d("Capabilities insufficient",76);return r}remove(t){if(t>=this.size)throw new d("Invalid fd",8);const e=this.fds[t];if(!e||e.id!==t)throw new d("Bad file descriptor",8);this.fds[t]=void 0,this.used--}renumber(t,e){if(t===e)return;if(t>=this.size||e>=this.size)throw new d("Invalid fd",8);const n=this.fds[t],r=this.fds[e];if(!n||!r||n.id!==t||r.id!==e)throw new d("Invalid fd",8);this.fs.closeSync(n.fd),this.fds[t]=this.fds[e],this.fds[t].id=t,this.fds[e]=void 0,this.used--}}class H extends WebAssembly.Memory{constructor(t){super(t)}get HEAP8(){return new Int8Array(super.buffer)}get HEAPU8(){return new Uint8Array(super.buffer)}get HEAP16(){return new Int16Array(super.buffer)}get HEAPU16(){return new Uint16Array(super.buffer)}get HEAP32(){return new Int32Array(super.buffer)}get HEAPU32(){return new Uint32Array(super.buffer)}get HEAP64(){return new BigInt64Array(super.buffer)}get HEAPU64(){return new BigUint64Array(super.buffer)}get HEAPF32(){return new Float32Array(super.buffer)}get HEAPF64(){return new Float64Array(super.buffer)}get view(){return new DataView(super.buffer)}}function U(t){return Object.getPrototypeOf(t)===WebAssembly.Memory.prototype&&Object.setPrototypeOf(t,H.prototype),t}function O(t,e){if(0===t.length||0===e.length)return 0;let n=0,r=e.length-n;for(let i=0;i<t.length;++i){const s=t[i];if(r<s.length)return s.set(e.subarray(n,n+r),0),n+=r,r=0,n;s.set(e.subarray(n,n+s.length),0),n+=s.length,r-=s.length}return n}const k=new WeakMap,C=new WeakMap,x=new WeakMap;function M(t){return k.get(t)}function W(t){const e=x.get(t);if(!e)throw new Error("filesystem is unavailable");return e}function z(t){if(t instanceof d)return t.errno;switch(t.code){case"ENOENT":return 44;case"EBADF":return 8;case"EINVAL":return 28;case"EPERM":return 63;case"EPROTO":return 65;case"EEXIST":return 20;case"ENOTDIR":return 54;case"EMFILE":return 33;case"EACCES":return 2;case"EISDIR":return 31;case"ENOTEMPTY":return 55;case"ENOSYS":return 52}throw t}function K(t,e){return function(t,e){return Object.defineProperty(e,"name",{value:t}),e}(t,(function(){let t;try{t=e.apply(this,arguments)}catch(t){return z(t)}return s(t)?t.then((t=>t),z):t}))}function j(t,e,n,r){let i=h(e.realPath,n);if(1==(1&r))try{i=t.readlinkSync(i)}catch(t){if("EINVAL"!==t.code&&"ENOENT"!==t.code)throw t}return i}const G=new TextEncoder,Y=new TextDecoder;class ${constructor(t,e,n,r,i,s,o){this._setMemory=function(t){if(!(t instanceof WebAssembly.Memory))throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');k.set(this,U(t))},this.args_get=K("args_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{HEAPU8:n,view:r}=M(this),i=C.get(this).args;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=G.encode(o+"\0");n.set(a,e),e+=a.length}return 0})),this.args_sizes_get=K("args_sizes_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{view:n}=M(this),r=C.get(this).args;return n.setUint32(t,r.length,!0),n.setUint32(e,G.encode(r.join("\0")+"\0").length,!0),0})),this.environ_get=K("environ_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{HEAPU8:n,view:r}=M(this),i=C.get(this).env;for(let s=0;s<i.length;++s){const o=i[s];r.setInt32(t,e,!0),t+=4;const a=G.encode(o+"\0");n.set(a,e),e+=a.length}return 0})),this.environ_sizes_get=K("environ_sizes_get",(function(t,e){if(t=Number(t),e=Number(e),0===t||0===e)return 28;const{view:n}=M(this),r=C.get(this);return n.setUint32(t,r.env.length,!0),n.setUint32(e,G.encode(r.env.join("\0")+"\0").length,!0),0})),this.clock_res_get=K("clock_res_get",(function(t,e){if(0===(e=Number(e)))return 28;const{view:n}=M(this);switch(t){case 0:return n.setBigUint64(e,BigInt(1e6),!0),0;case 1:case 2:case 3:return n.setBigUint64(e,BigInt(1e3),!0),0;default:return 28}})),this.clock_time_get=K("clock_time_get",(function(t,e,n){if(0===(n=Number(n)))return 28;const{view:r}=M(this);switch(t){case 0:return r.setBigUint64(n,BigInt(Date.now())*BigInt(1e6),!0),0;case 1:case 2:case 3:{const t=performance.now(),e=Math.trunc(t),i=Math.floor(1e3*(t-e)),s=BigInt(e)*BigInt(1e9)+BigInt(i)*BigInt(1e6);return r.setBigUint64(n,s,!0),0}default:return 28}})),this.fd_advise=K("fd_advise",(function(t,e,n,r){return 52})),this.fd_allocate=K("fd_allocate",(function(t,e,n){const r=C.get(this),i=W(this),s=r.fds.get(t,l.FD_ALLOCATE,BigInt(0));return i.fstatSync(s.fd,{bigint:!0}).size<e+n&&i.truncateSync(s.fd,Number(e+n)),0})),this.fd_close=K("fd_close",(function(t){const e=C.get(this),n=e.fds.get(t,BigInt(0),BigInt(0));return W(this).closeSync(n.fd),e.fds.remove(t),0})),this.fd_datasync=K("fd_datasync",(function(t){const e=C.get(this).fds.get(t,l.FD_DATASYNC,BigInt(0));return W(this).fdatasyncSync(e.fd),0})),this.fd_fdstat_get=K("fd_fdstat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=C.get(this).fds.get(t,BigInt(0),BigInt(0)),{view:r}=M(this);return r.setUint16(e,n.type,!0),r.setUint16(e+2,0,!0),r.setBigUint64(e+8,n.rightsBase,!0),r.setBigUint64(e+16,n.rightsInheriting,!0),0})),this.fd_fdstat_set_flags=K("fd_fdstat_set_flags",(function(t,e){return 52})),this.fd_fdstat_set_rights=K("fd_fdstat_set_rights",(function(t,e,n){const r=C.get(this).fds.get(t,BigInt(0),BigInt(0));return(e|r.rightsBase)>r.rightsBase||(n|r.rightsInheriting)>r.rightsInheriting?76:(r.rightsBase=e,r.rightsInheriting=n,0)})),this.fd_filestat_get=K("fd_filestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=C.get(this).fds.get(t,l.FD_FILESTAT_GET,BigInt(0)),r=W(this).fstatSync(n.fd,{bigint:!0}),{view:i}=M(this);return R(i,e,r),0})),this.fd_filestat_set_size=K("fd_filestat_set_size",(function(t,e){const n=C.get(this).fds.get(t,l.FD_FILESTAT_SET_SIZE,BigInt(0));return W(this).ftruncateSync(n.fd,Number(e)),0})),this.fd_filestat_set_times=K("fd_filestat_set_times",(function(t,e,n,r){const i=C.get(this).fds.get(t,l.FD_FILESTAT_SET_TIMES,BigInt(0));2==(2&r)&&(e=BigInt(1e6*Date.now())),8==(8&r)&&(n=BigInt(1e6*Date.now()));return W(this).futimesSync(i.fd,Number(e),Number(n)),0})),this.fd_pread=K("fd_pread",(function(t,e,n,r,i){if(e=Number(e),i=Number(i),0===e||0===i)return 28;const{HEAPU8:s,view:o}=M(this),a=C.get(this).fds.get(t,l.FD_READ|l.FD_SEEK,BigInt(0));let c=0;const u=Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,i=o.getInt32(r,!0),a=o.getUint32(r+4,!0);return c+=a,s.subarray(i,i+a)}));let f=0;const h=new Uint8Array(c);h._isBuffer=!0;const d=W(this).readSync(a.fd,h,0,h.length,Number(r));return f=h?O(u,h.subarray(0,d)):0,o.setUint32(i,f,!0),0})),this.fd_prestat_get=K("fd_prestat_get",(function(t,e){if(0===(e=Number(e)))return 28;const n=C.get(this);let r;try{r=n.fds.get(t,BigInt(0),BigInt(0))}catch(t){if(t instanceof d)return t.errno;throw t}if(1!==r.preopen)return 28;const{view:i}=M(this);return i.setUint32(e,0,!0),i.setUint32(e+4,G.encode(r.path).length+1,!0),0})),this.fd_prestat_dir_name=K("fd_prestat_dir_name",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const r=C.get(this).fds.get(t,BigInt(0),BigInt(0));if(1!==r.preopen)return 8;const i=G.encode(r.path+"\0");if(i.length>n)return 42;const{HEAPU8:s}=M(this);return s.set(i,e),0})),this.fd_pwrite=K("fd_pwrite",(function(t,e,n,r,i){if(e=Number(e),i=Number(i),0===e||0===i)return 28;const{HEAPU8:s,view:o}=M(this),a=C.get(this).fds.get(t,l.FD_WRITE|l.FD_SEEK,BigInt(0)),c=N(Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,i=o.getInt32(r,!0),a=o.getUint32(r+4,!0);return s.subarray(i,i+a)}))),u=W(this).writeSync(a.fd,c,0,c.length,Number(r));return o.setUint32(i,u,!0),0})),this.fd_read=K("fd_read",(function(t,e,n,r){if(e=Number(e),r=Number(r),0===e||0===r)return 28;const{HEAPU8:i,view:s}=M(this),o=C.get(this).fds.get(t,l.FD_READ,BigInt(0));let a=0;const c=Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,o=s.getInt32(r,!0),c=s.getUint32(r+4,!0);return a+=c,i.subarray(o,o+c)}));let u,f=0;if(0===t)u=function(){const t=window.prompt();return null===t?new Uint8Array:(new TextEncoder).encode(t+"\n")}(),f=u?O(c,u):0;else{u=new Uint8Array(a),u._isBuffer=!0;const t=W(this).readSync(o.fd,u,0,u.length,Number(o.pos));f=u?O(c,u.subarray(0,t)):0,o.pos+=BigInt(f)}return s.setUint32(r,f,!0),0})),this.fd_seek=K("fd_seek",(function(t,e,n,r){if(0===(r=Number(r)))return 28;if(0===t||1===t||2===t)return 0;const i=C.get(this).fds.get(t,l.FD_SEEK,BigInt(0)).seek(e,n),{view:s}=M(this);return s.setBigUint64(r,i,!0),0})),this.fd_readdir=K("fd_readdir",(function(t,e,n,r,i){if(e=Number(e),n=Number(n),i=Number(i),0===e||0===i)return 0;const s=C.get(this).fds.get(t,l.FD_READDIR,BigInt(0)),o=W(this),a=o.readdirSync(s.realPath,{withFileTypes:!0}),{HEAPU8:c,view:u}=M(this);let f=0;for(let t=Number(r);t<a.length;t++){const r=G.encode(a[t].name),i=o.statSync(h(s.realPath,a[t].name),{bigint:!0}),u=new Uint8Array(24+r.byteLength),l=new DataView(u.buffer);let d;l.setBigUint64(0,BigInt(t+1),!0),l.setBigUint64(8,BigInt(i.ino?i.ino:0),!0),l.setUint32(16,r.byteLength,!0),d=a[t].isFile()?4:a[t].isDirectory()?3:a[t].isSymbolicLink()?7:a[t].isCharacterDevice()?2:a[t].isBlockDevice()?1:a[t].isSocket()?6:0,l.setUint8(20,d),u.set(r,24);const _=u.slice(0,Math.min(u.length,n-f));c.set(_,e+f),f+=_.byteLength}return u.setUint32(i,f,!0),0})),this.fd_renumber=K("fd_renumber",(function(t,e){return C.get(this).fds.renumber(e,t),0})),this.fd_sync=K("fd_sync",(function(t){const e=C.get(this).fds.get(t,l.FD_SYNC,BigInt(0));return W(this).fsyncSync(e.fd),0})),this.fd_tell=K("fd_tell",(function(t,e){const n=C.get(this).fds.get(t,l.FD_TELL,BigInt(0)),r=BigInt(n.pos),{view:i}=M(this);return i.setBigUint64(Number(e),r,!0),0})),this.fd_write=K("fd_write",(function(t,e,n,r){if(e=Number(e),r=Number(r),0===e||0===r)return 28;const{HEAPU8:i,view:s}=M(this),o=C.get(this).fds.get(t,l.FD_WRITE,BigInt(0)),a=N(Array.from({length:Number(n)},((t,n)=>{const r=e+8*n,o=s.getInt32(r,!0),a=s.getUint32(r+4,!0);return i.subarray(o,o+a)})));let c;if(1===t||2===t)c=o.write(a);else{c=W(this).writeSync(o.fd,a,0,a.length,Number(o.pos)),o.pos+=BigInt(c)}return s.setUint32(r,c,!0),0})),this.path_create_directory=K("path_create_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=M(this),i=C.get(this).fds.get(t,l.PATH_CREATE_DIRECTORY,BigInt(0));let s=Y.decode(r.subarray(e,e+n));s=h(i.realPath,s);return W(this).mkdirSync(s),0})),this.path_filestat_get=K("path_filestat_get",(function(t,e,n,r,i){if(n=Number(n),r=Number(r),i=Number(i),0===n||0===i)return 28;const{HEAPU8:s,view:o}=M(this),a=C.get(this).fds.get(t,l.PATH_FILESTAT_GET,BigInt(0));let c=Y.decode(s.subarray(n,n+r));const u=W(this);let f;return c=h(a.realPath,c),f=1==(1&e)?u.statSync(c,{bigint:!0}):u.lstatSync(c,{bigint:!0}),R(o,i,f),0})),this.path_filestat_set_times=K("path_filestat_set_times",(function(t,e,n,r,i,s,o){if(n=Number(n),r=Number(r),0===n)return 28;if(-16&o)return 28;const{HEAPU8:a}=M(this),c=C.get(this).fds.get(t,l.PATH_FILESTAT_SET_TIMES,BigInt(0)),u=W(this),f=j(u,c,Y.decode(a.subarray(n,n+r)),e);return 2==(2&o)&&(i=BigInt(1e6*Date.now())),8==(8&o)&&(s=BigInt(1e6*Date.now())),u.utimesSync(f,Number(i),Number(s)),0})),this.path_link=K("path_link",(function(t,e,n,r,i,s,o){if(n=Number(n),r=Number(r),s=Number(s),o=Number(o),0===n||0===s)return 28;const a=C.get(this);let c,u;t===i?c=u=a.fds.get(t,l.PATH_LINK_SOURCE|l.PATH_LINK_TARGET,BigInt(0)):(c=a.fds.get(t,l.PATH_LINK_SOURCE,BigInt(0)),u=a.fds.get(i,l.PATH_LINK_TARGET,BigInt(0)));const{HEAPU8:f}=M(this),d=W(this),_=j(d,c,Y.decode(f.subarray(n,n+r)),e),g=h(u.realPath,Y.decode(f.subarray(s,s+o)));return d.linkSync(_,g),0})),this.path_open=K("path_open",(function(t,e,n,r,i,s,o,a,c){if(n=Number(n),c=Number(c),0===n||0===c)return 28;r=Number(r),s=BigInt(s);const u=((s=BigInt(s))&(l.FD_READ|l.FD_READDIR))!==BigInt(0),f=(s&(l.FD_DATASYNC|l.FD_WRITE|l.FD_ALLOCATE|l.FD_FILESTAT_SET_SIZE))!==BigInt(0);let h=f?u?2:1:0,d=l.PATH_OPEN,_=s|o;0!=(1&i)&&(h|=64,d|=l.PATH_CREATE_FILE),0!=(2&i)&&(h|=65536),0!=(4&i)&&(h|=128),0!=(8&i)&&(h|=512,d|=l.PATH_FILESTAT_SET_SIZE),0!=(1&a)&&(h|=1024),0!=(2&a)&&(_|=l.FD_DATASYNC),0!=(4&a)&&(h|=2048),0!=(8&a)&&(h|=1052672,_|=l.FD_SYNC),0!=(16&a)&&(h|=1052672,_|=l.FD_SYNC),f&&0==(1536&h)&&(_|=l.FD_SEEK);const g=C.get(this),E=g.fds.get(t,d,_),y=M(this),p=y.HEAPU8,I=Y.decode(p.subarray(n,n+r)),T=W(this),A=j(T,E,I,e),m=T.openSync(A,h,438),b=g.fds.getFileTypeByFd(m);if(0!=(2&i)&&3!==b)return 54;const{base:w,inheriting:S}=D(g.fds.stdio,m,h,b),B=g.fds.insert(m,A,A,b,s&w,o&S,0),N=T.fstatSync(m,{bigint:!0});N.isFile()&&(B.size=N.size,0!=(1024&h)&&(B.pos=N.size));return y.view.setInt32(c,B.id,!0),0})),this.path_readlink=K("path_readlink",(function(t,e,n,r,i,s){if(e=Number(e),n=Number(n),r=Number(r),i=Number(i),s=Number(s),0===e||0===r||0===s)return 28;const{HEAPU8:o,view:a}=M(this),c=C.get(this).fds.get(t,l.PATH_READLINK,BigInt(0));let u=Y.decode(o.subarray(e,e+n));u=h(c.realPath,u);const f=W(this).readlinkSync(u),d=G.encode(f),_=Math.min(d.length,i);return _>=i?42:(o.set(d.subarray(0,_),r),o[r+_]=0,a.setUint32(s,_+1,!0),0)})),this.path_remove_directory=K("path_remove_directory",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=M(this),i=C.get(this).fds.get(t,l.PATH_REMOVE_DIRECTORY,BigInt(0));let s=Y.decode(r.subarray(e,e+n));s=h(i.realPath,s);return W(this).rmdirSync(s),0})),this.path_rename=K("path_rename",(function(t,e,n,r,i,s){if(e=Number(e),n=Number(n),i=Number(i),s=Number(s),0===e||0===i)return 28;const o=C.get(this);let a,c;t===r?a=c=o.fds.get(t,l.PATH_RENAME_SOURCE|l.PATH_RENAME_TARGET,BigInt(0)):(a=o.fds.get(t,l.PATH_RENAME_SOURCE,BigInt(0)),c=o.fds.get(r,l.PATH_RENAME_TARGET,BigInt(0)));const{HEAPU8:u}=M(this),f=h(a.realPath,Y.decode(u.subarray(e,e+n))),d=h(c.realPath,Y.decode(u.subarray(i,i+s)));return W(this).renameSync(f,d),0})),this.path_symlink=K("path_symlink",(function(t,e,n,r,i){if(t=Number(t),e=Number(e),r=Number(r),i=Number(i),0===t||0===r)return 28;const{HEAPU8:s}=M(this),o=C.get(this).fds.get(n,l.PATH_SYMLINK,BigInt(0)),a=Y.decode(s.subarray(t,t+e));let c=Y.decode(s.subarray(r,r+i));c=h(o.realPath,c);return W(this).symlinkSync(a,c),0})),this.path_unlink_file=K("path_unlink_file",(function(t,e,n){if(e=Number(e),n=Number(n),0===e)return 28;const{HEAPU8:r}=M(this),i=C.get(this).fds.get(t,l.PATH_UNLINK_FILE,BigInt(0));let s=Y.decode(r.subarray(e,e+n));s=h(i.realPath,s);return W(this).unlinkSync(s),0})),this.poll_oneoff=K("poll_oneoff",(function(t,e,n,r){return 52})),this.proc_exit=K("proc_exit",(function(t){return 0})),this.proc_raise=K("proc_raise",(function(t){return 52})),this.sched_yield=K("sched_yield",(function(){return 0})),this.random_get="undefined"!=typeof crypto&&"function"==typeof crypto.getRandomValues?K("random_get",(function(t,e){if(0===(t=Number(t)))return 28;e=Number(e);const{HEAPU8:n}=M(this);let r;const i=65536;for(r=0;r+i<e;r+=i)crypto.getRandomValues(n.subarray(t+r,t+r+i));return crypto.getRandomValues(n.subarray(t+r,t+e)),0})):K("random_get",(function(t,e){if(0===(t=Number(t)))return 28;e=Number(e);const{view:n}=M(this);for(let r=t;r<t+e;++r)n.setUint8(r,Math.floor(256*Math.random()));return 0})),this.sock_recv=K("sock_recv",(function(){return 58})),this.sock_send=K("sock_send",(function(){return 58})),this.sock_shutdown=K("sock_shutdown",(function(){return 58}));const a=i?i.fs:void 0,c=new L({size:3,in:r[0],out:r[1],err:r[2],fs:a,print:s,printErr:o});if(C.set(this,{fds:c,args:t,env:e}),a&&x.set(this,a),n.length>0)for(let t=0;t<n.length;++t){const e=a.realpathSync(n[t].realPath,"utf8"),r=a.openSync(e,"r",438);c.insertPreopen(r,n[t].mappedPath,e)}}}const V=Object.freeze(Object.create(null)),Z=Symbol("kExitCode"),q=Symbol("kSetMemory"),Q=Symbol("kStarted"),X=Symbol("kInstance");function J(t,n){e(n,"instance"),e(n.exports,"instance.exports"),t[X]=n,t[q](n.exports.memory)}function tt(t){throw this[Z]=t,Z}t.Asyncify=c,t.Memory=H,t.WASI=class{constructor(t=V){var i;e(t,"options"),void 0!==t.args&&function(t,e){if(!Array.isArray(t))throw new TypeError(`${e} must be an array. Received ${null===t?"null":typeof t}`)}(t.args,"options.args");const s=(null!==(i=t.args)&&void 0!==i?i:[]).map(String),o=[];void 0!==t.env&&(e(t.env,"options.env"),Object.entries(t.env).forEach((({0:t,1:e})=>{void 0!==e&&o.push(`${t}=${e}`)})));const a=[];if(void 0!==t.preopens&&(e(t.preopens,"options.preopens"),Object.entries(t.preopens).forEach((({0:t,1:e})=>a.push({mappedPath:String(t),realPath:String(e)})))),a.length>0&&void 0===t.filesystem)throw new Error("filesystem is disabled, can not preopen directory");if(void 0!==t.filesystem){if(e(t.filesystem,"options.filesystem"),n(t.filesystem.type,"options.filesystem.type"),"memfs"!==t.filesystem.type)throw new Error(`Filesystem type ${t.filesystem.type} is not supported, only "memfs" is supported currently`);try{e(t.filesystem.fs,"options.filesystem.fs")}catch(t){throw new Error("Node.js fs like implementation is not provided")}}void 0!==t.print&&r(t.print,"options.print"),void 0!==t.printErr&&r(t.printErr,"options.printErr");const c=new $(s,o,a,[0,1,2],t.filesystem,t.print,t.printErr);for(const t in c)c[t]=c[t].bind(c);void 0!==t.returnOnExit&&(!function(t,e){if("boolean"!=typeof t)throw new TypeError(`${e} must be a boolean. Received ${null===t?"null":typeof t}`)}(t.returnOnExit,"options.returnOnExit"),t.returnOnExit&&(c.proc_exit=tt.bind(this))),this[q]=c._setMemory,delete c._setMemory,this.wasiImport=c,this[Q]=!1,this[Z]=0,this[X]=void 0}start(t){if(this[Q])throw new Error("WASI instance has already started");this[Q]=!0,J(this,t);const{_start:e,_initialize:n}=this[X].exports;let s;r(e,"instance.exports._start"),i(n,"instance.exports._initialize");try{s=e()}catch(t){if(t!==Z)throw t}return s instanceof Promise?s.then((()=>this[Z]),(t=>{if(t!==Z)throw t;return this[Z]})):this[Z]}initialize(t){if(this[Q])throw new Error("WASI instance has already started");this[Q]=!0,J(this,t);const{_start:e,_initialize:n}=this[X].exports;if(i(e,"instance.exports._start"),void 0!==n)return r(n,"instance.exports._initialize"),n()}},t.extendMemory=U,t.load=async function(t,e,n){var r,i;if(e&&"object"!=typeof e)throw new TypeError("imports must be an object or undefined");let s,o;if(e=null!=e?e:{},n&&(s=new c,e=s.wrapImports(e)),t instanceof ArrayBuffer||ArrayBuffer.isView(t)){if(o=await WebAssembly.instantiate(t,e),n){const t=o.instance.exports.memory||(null===(r=e.env)||void 0===r?void 0:r.memory);return{module:o.module,instance:s.init(t,o.instance,n)}}return o}if("string"!=typeof t&&!(t instanceof URL))throw new TypeError("Invalid source");if("function"==typeof WebAssembly.instantiateStreaming)try{o=await WebAssembly.instantiateStreaming(fetch(t),e)}catch(n){o=await u(t,e)}else o=await u(t,e);if(n){const t=o.instance.exports.memory||(null===(i=e.env)||void 0===i?void 0:i.memory);return{module:o.module,instance:s.init(t,o.instance,n)}}return o},t.loadSync=function(t,e,n){var r;if(t instanceof ArrayBuffer&&!ArrayBuffer.isView(t))throw new TypeError("Invalid source");if(e&&"object"!=typeof e)throw new TypeError("imports must be an object or undefined");let i;e=null!=e?e:{},n&&(i=new c,e=i.wrapImports(e));const s=new WebAssembly.Module(t),o=new WebAssembly.Instance(s,e),a={instance:o,module:s};if(n){const t=a.instance.exports.memory||(null===(r=e.env)||void 0===r?void 0:r.memory);return{module:a.module,instance:i.init(t,o,n)}}return a},Object.defineProperty(t,"__esModule",{value:!0})}));
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).wasmUtil={})}(this,(function(t){"use strict";const e="undefined"!=typeof WebAssembly?WebAssembly:"undefined"!=typeof WXWebAssembly?WXWebAssembly:void 0;if(!e)throw new Error("WebAssembly is not supported in this environment");function n(t,e){if(null===t||"object"!=typeof t)throw new TypeError(`${e} must be an object. Received ${null===t?"null":typeof t}`)}function r(t,e){if("string"!=typeof t)throw new TypeError(`${e} must be a string. Received ${null===t?"null":typeof t}`)}function i(t,e){if("function"!=typeof t)throw new TypeError(`${e} must be a function. Received ${null===t?"null":typeof t}`)}function s(t,e){if(void 0!==t)throw new TypeError(`${e} must be undefined. Received ${null===t?"null":typeof t}`)}function o(t){return!(!t||"object"!=typeof t&&"function"!=typeof t||"function"!=typeof t.then)}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})}));
|
package/lib/cjs/asyncify.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Asyncify = void 0;
|
|
4
|
+
const webassembly_1 = require("./webassembly");
|
|
4
5
|
const util_1 = require("./wasi/util");
|
|
5
6
|
const ignoreNames = [
|
|
6
7
|
'asyncify_get_state',
|
|
@@ -46,7 +47,7 @@ class Asyncify {
|
|
|
46
47
|
if (this.exports) {
|
|
47
48
|
throw new Error('Asyncify has been initialized');
|
|
48
49
|
}
|
|
49
|
-
if (!(memory instanceof
|
|
50
|
+
if (!(memory instanceof webassembly_1._WebAssembly.Memory)) {
|
|
50
51
|
throw new TypeError('Require WebAssembly.Memory object');
|
|
51
52
|
}
|
|
52
53
|
const exports = instance.exports;
|
|
@@ -81,7 +82,7 @@ class Asyncify {
|
|
|
81
82
|
new Int32Array(memory.buffer, this.dataPtr).set([address.start, address.end]);
|
|
82
83
|
}
|
|
83
84
|
this.exports = this.wrapExports(exports, options.wrapExports);
|
|
84
|
-
const asyncifiedInstance = Object.create(
|
|
85
|
+
const asyncifiedInstance = Object.create(webassembly_1._WebAssembly.Instance.prototype);
|
|
85
86
|
Object.defineProperty(asyncifiedInstance, 'exports', { value: this.exports });
|
|
86
87
|
// Object.setPrototypeOf(instance, Instance.prototype)
|
|
87
88
|
return asyncifiedInstance;
|
package/lib/cjs/load.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.loadSync = exports.load = void 0;
|
|
4
|
+
const webassembly_1 = require("./webassembly");
|
|
4
5
|
const asyncify_1 = require("./asyncify");
|
|
5
6
|
async function fetchWasm(urlOrBuffer, imports) {
|
|
7
|
+
if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
|
|
8
|
+
return await webassembly_1._WebAssembly.instantiate(urlOrBuffer, imports);
|
|
9
|
+
}
|
|
6
10
|
const response = await fetch(urlOrBuffer);
|
|
7
11
|
const buffer = await response.arrayBuffer();
|
|
8
|
-
const source = await
|
|
12
|
+
const source = await webassembly_1._WebAssembly.instantiate(buffer, imports);
|
|
9
13
|
return source;
|
|
10
14
|
}
|
|
11
15
|
/** @public */
|
|
@@ -22,7 +26,7 @@ async function load(urlOrBuffer, imports, asyncify) {
|
|
|
22
26
|
imports = asyncifyHelper.wrapImports(imports);
|
|
23
27
|
}
|
|
24
28
|
if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
|
|
25
|
-
source = await
|
|
29
|
+
source = await webassembly_1._WebAssembly.instantiate(urlOrBuffer, imports);
|
|
26
30
|
if (asyncify) {
|
|
27
31
|
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
|
28
32
|
return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
|
|
@@ -32,9 +36,9 @@ async function load(urlOrBuffer, imports, asyncify) {
|
|
|
32
36
|
if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
|
|
33
37
|
throw new TypeError('Invalid source');
|
|
34
38
|
}
|
|
35
|
-
if (typeof
|
|
39
|
+
if (typeof webassembly_1._WebAssembly.instantiateStreaming === 'function') {
|
|
36
40
|
try {
|
|
37
|
-
source = await
|
|
41
|
+
source = await webassembly_1._WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
|
|
38
42
|
}
|
|
39
43
|
catch (_) {
|
|
40
44
|
source = await fetchWasm(urlOrBuffer, imports);
|
|
@@ -65,8 +69,8 @@ function loadSync(buffer, imports, asyncify) {
|
|
|
65
69
|
asyncifyHelper = new asyncify_1.Asyncify();
|
|
66
70
|
imports = asyncifyHelper.wrapImports(imports);
|
|
67
71
|
}
|
|
68
|
-
const module = new
|
|
69
|
-
const instance = new
|
|
72
|
+
const module = new webassembly_1._WebAssembly.Module(buffer);
|
|
73
|
+
const instance = new webassembly_1._WebAssembly.Instance(module, imports);
|
|
70
74
|
const source = { instance, module };
|
|
71
75
|
if (asyncify) {
|
|
72
76
|
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
package/lib/cjs/memory.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extendMemory = exports.Memory = void 0;
|
|
3
|
+
exports.extendMemory = exports.Memory = exports.WebAssemblyMemory = void 0;
|
|
4
|
+
const webassembly_1 = require("./webassembly");
|
|
4
5
|
/** @public */
|
|
5
|
-
|
|
6
|
+
exports.WebAssemblyMemory = webassembly_1._WebAssembly.Memory;
|
|
7
|
+
/** @public */
|
|
8
|
+
class Memory extends exports.WebAssemblyMemory {
|
|
6
9
|
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
7
10
|
constructor(descriptor) {
|
|
8
11
|
super(descriptor);
|
|
@@ -22,7 +25,7 @@ class Memory extends WebAssembly.Memory {
|
|
|
22
25
|
exports.Memory = Memory;
|
|
23
26
|
/** @public */
|
|
24
27
|
function extendMemory(memory) {
|
|
25
|
-
if (Object.getPrototypeOf(memory) ===
|
|
28
|
+
if (Object.getPrototypeOf(memory) === webassembly_1._WebAssembly.Memory.prototype) {
|
|
26
29
|
Object.setPrototypeOf(memory, Memory.prototype);
|
|
27
30
|
}
|
|
28
31
|
return memory;
|
package/lib/cjs/wasi/preview1.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.WASI = void 0;
|
|
4
|
+
// import { vol } from 'memfs-browser'
|
|
5
|
+
const webassembly_1 = require("../webassembly");
|
|
4
6
|
const path_1 = require("./path");
|
|
5
7
|
const types_1 = require("./types");
|
|
6
8
|
const fd_1 = require("./fd");
|
|
@@ -113,7 +115,7 @@ function readStdin() {
|
|
|
113
115
|
class WASI {
|
|
114
116
|
constructor(args, env, preopens, stdio, filesystem, print, printErr) {
|
|
115
117
|
this._setMemory = function _setMemory(m) {
|
|
116
|
-
if (!(m instanceof
|
|
118
|
+
if (!(m instanceof webassembly_1._WebAssembly.Memory)) {
|
|
117
119
|
throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
|
|
118
120
|
}
|
|
119
121
|
_memory.set(this, (0, memory_1.extendMemory)(m));
|
|
@@ -420,6 +422,9 @@ class WASI {
|
|
|
420
422
|
let buffer;
|
|
421
423
|
let nread = 0;
|
|
422
424
|
if (fd === 0) {
|
|
425
|
+
if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
|
|
426
|
+
return types_1.WasiErrno.ENOTSUP;
|
|
427
|
+
}
|
|
423
428
|
buffer = readStdin();
|
|
424
429
|
nread = buffer ? copyMemory(ioVecs, buffer) : 0;
|
|
425
430
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports._WebAssembly = void 0;
|
|
4
|
+
const _WebAssembly = typeof WebAssembly !== 'undefined'
|
|
5
|
+
? WebAssembly
|
|
6
|
+
: typeof WXWebAssembly !== 'undefined'
|
|
7
|
+
? WXWebAssembly
|
|
8
|
+
: undefined;
|
|
9
|
+
exports._WebAssembly = _WebAssembly;
|
|
10
|
+
if (!_WebAssembly) {
|
|
11
|
+
throw new Error('WebAssembly is not supported in this environment');
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=webassembly.js.map
|
package/lib/mjs/asyncify.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { _WebAssembly } from "./webassembly.mjs";
|
|
1
2
|
import { isPromiseLike } from "./wasi/util.mjs";
|
|
2
3
|
const ignoreNames = [
|
|
3
4
|
'asyncify_get_state',
|
|
@@ -43,7 +44,7 @@ export class Asyncify {
|
|
|
43
44
|
if (this.exports) {
|
|
44
45
|
throw new Error('Asyncify has been initialized');
|
|
45
46
|
}
|
|
46
|
-
if (!(memory instanceof
|
|
47
|
+
if (!(memory instanceof _WebAssembly.Memory)) {
|
|
47
48
|
throw new TypeError('Require WebAssembly.Memory object');
|
|
48
49
|
}
|
|
49
50
|
const exports = instance.exports;
|
|
@@ -78,7 +79,7 @@ export class Asyncify {
|
|
|
78
79
|
new Int32Array(memory.buffer, this.dataPtr).set([address.start, address.end]);
|
|
79
80
|
}
|
|
80
81
|
this.exports = this.wrapExports(exports, options.wrapExports);
|
|
81
|
-
const asyncifiedInstance = Object.create(
|
|
82
|
+
const asyncifiedInstance = Object.create(_WebAssembly.Instance.prototype);
|
|
82
83
|
Object.defineProperty(asyncifiedInstance, 'exports', { value: this.exports });
|
|
83
84
|
// Object.setPrototypeOf(instance, Instance.prototype)
|
|
84
85
|
return asyncifiedInstance;
|
package/lib/mjs/load.mjs
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
+
import { _WebAssembly } from "./webassembly.mjs";
|
|
1
2
|
import { Asyncify } from "./asyncify.mjs";
|
|
2
3
|
async function fetchWasm(urlOrBuffer, imports) {
|
|
4
|
+
if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
|
|
5
|
+
return await _WebAssembly.instantiate(urlOrBuffer, imports);
|
|
6
|
+
}
|
|
3
7
|
const response = await fetch(urlOrBuffer);
|
|
4
8
|
const buffer = await response.arrayBuffer();
|
|
5
|
-
const source = await
|
|
9
|
+
const source = await _WebAssembly.instantiate(buffer, imports);
|
|
6
10
|
return source;
|
|
7
11
|
}
|
|
8
12
|
/** @public */
|
|
@@ -19,7 +23,7 @@ export async function load(urlOrBuffer, imports, asyncify) {
|
|
|
19
23
|
imports = asyncifyHelper.wrapImports(imports);
|
|
20
24
|
}
|
|
21
25
|
if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
|
|
22
|
-
source = await
|
|
26
|
+
source = await _WebAssembly.instantiate(urlOrBuffer, imports);
|
|
23
27
|
if (asyncify) {
|
|
24
28
|
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
|
25
29
|
return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
|
|
@@ -29,9 +33,9 @@ export async function load(urlOrBuffer, imports, asyncify) {
|
|
|
29
33
|
if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
|
|
30
34
|
throw new TypeError('Invalid source');
|
|
31
35
|
}
|
|
32
|
-
if (typeof
|
|
36
|
+
if (typeof _WebAssembly.instantiateStreaming === 'function') {
|
|
33
37
|
try {
|
|
34
|
-
source = await
|
|
38
|
+
source = await _WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
|
|
35
39
|
}
|
|
36
40
|
catch (_) {
|
|
37
41
|
source = await fetchWasm(urlOrBuffer, imports);
|
|
@@ -61,8 +65,8 @@ export function loadSync(buffer, imports, asyncify) {
|
|
|
61
65
|
asyncifyHelper = new Asyncify();
|
|
62
66
|
imports = asyncifyHelper.wrapImports(imports);
|
|
63
67
|
}
|
|
64
|
-
const module = new
|
|
65
|
-
const instance = new
|
|
68
|
+
const module = new _WebAssembly.Module(buffer);
|
|
69
|
+
const instance = new _WebAssembly.Instance(module, imports);
|
|
66
70
|
const source = { instance, module };
|
|
67
71
|
if (asyncify) {
|
|
68
72
|
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
package/lib/mjs/memory.mjs
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { _WebAssembly } from "./webassembly.mjs";
|
|
1
2
|
/** @public */
|
|
2
|
-
export
|
|
3
|
+
export const WebAssemblyMemory = _WebAssembly.Memory;
|
|
4
|
+
/** @public */
|
|
5
|
+
export class Memory extends WebAssemblyMemory {
|
|
3
6
|
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
4
7
|
constructor(descriptor) {
|
|
5
8
|
super(descriptor);
|
|
@@ -18,7 +21,7 @@ export class Memory extends WebAssembly.Memory {
|
|
|
18
21
|
}
|
|
19
22
|
/** @public */
|
|
20
23
|
export function extendMemory(memory) {
|
|
21
|
-
if (Object.getPrototypeOf(memory) ===
|
|
24
|
+
if (Object.getPrototypeOf(memory) === _WebAssembly.Memory.prototype) {
|
|
22
25
|
Object.setPrototypeOf(memory, Memory.prototype);
|
|
23
26
|
}
|
|
24
27
|
return memory;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { _WebAssembly } from "../webassembly.mjs";
|
|
1
2
|
import { resolve } from "./path.mjs";
|
|
2
3
|
import { WasiErrno, WasiRights, FileControlFlag, WasiFileControlFlag, WasiFdFlag, WasiFileType, WasiClockid, WasiFstFlag } from "./types.mjs";
|
|
3
4
|
import { FileDescriptorTable, concatBuffer, toFileStat } from "./fd.mjs";
|
|
@@ -110,7 +111,7 @@ function readStdin() {
|
|
|
110
111
|
export class WASI {
|
|
111
112
|
constructor(args, env, preopens, stdio, filesystem, print, printErr) {
|
|
112
113
|
this._setMemory = function _setMemory(m) {
|
|
113
|
-
if (!(m instanceof
|
|
114
|
+
if (!(m instanceof _WebAssembly.Memory)) {
|
|
114
115
|
throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
|
|
115
116
|
}
|
|
116
117
|
_memory.set(this, extendMemory(m));
|
|
@@ -417,6 +418,9 @@ export class WASI {
|
|
|
417
418
|
let buffer;
|
|
418
419
|
let nread = 0;
|
|
419
420
|
if (fd === 0) {
|
|
421
|
+
if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
|
|
422
|
+
return WasiErrno.ENOTSUP;
|
|
423
|
+
}
|
|
420
424
|
buffer = readStdin();
|
|
421
425
|
nread = buffer ? copyMemory(ioVecs, buffer) : 0;
|
|
422
426
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const _WebAssembly = typeof WebAssembly !== 'undefined'
|
|
2
|
+
? WebAssembly
|
|
3
|
+
: typeof WXWebAssembly !== 'undefined'
|
|
4
|
+
? WXWebAssembly
|
|
5
|
+
: undefined;
|
|
6
|
+
if (!_WebAssembly) {
|
|
7
|
+
throw new Error('WebAssembly is not supported in this environment');
|
|
8
|
+
}
|
|
9
|
+
export { _WebAssembly };
|