@tybys/wasm-util 0.7.0 → 0.8.1
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 +3 -3
- package/dist/wasm-util.d.ts +12 -6
- package/dist/wasm-util.esm-bundler.js +78 -39
- package/dist/wasm-util.esm.js +78 -39
- package/dist/wasm-util.esm.min.js +1 -1
- package/dist/wasm-util.js +78 -38
- package/dist/wasm-util.min.js +1 -1
- package/lib/cjs/load.js +24 -13
- package/lib/cjs/wasi/index.js +47 -25
- package/lib/cjs/wasi/preview1.js +8 -1
- package/lib/mjs/load.mjs +24 -13
- package/lib/mjs/wasi/index.mjs +46 -25
- package/lib/mjs/wasi/preview1.mjs +8 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,13 +19,13 @@ You can use `memfs-browser` to provide filesystem capability.
|
|
|
19
19
|
|
|
20
20
|
```js
|
|
21
21
|
import { load, WASI } from '@tybys/wasm-util'
|
|
22
|
-
import {
|
|
22
|
+
import { Volume, createFsFromVolume } from 'memfs-browser'
|
|
23
23
|
|
|
24
|
-
const fs = createFsFromVolume(Volume.
|
|
24
|
+
const fs = createFsFromVolume(Volume.fromJSON({
|
|
25
25
|
'/home/wasi': null
|
|
26
26
|
}))
|
|
27
27
|
|
|
28
|
-
const wasi = WASI
|
|
28
|
+
const wasi = new WASI({
|
|
29
29
|
args: ['chrome', 'file.wasm'],
|
|
30
30
|
env: {
|
|
31
31
|
NODE_ENV: 'development',
|
package/dist/wasm-util.d.ts
CHANGED
|
@@ -32,10 +32,10 @@ export declare type AsyncifyExports<T, U> = T extends Record<string, any> ? {
|
|
|
32
32
|
} : T;
|
|
33
33
|
|
|
34
34
|
/** @public */
|
|
35
|
-
export declare function asyncifyLoad(asyncify: AsyncifyOptions, urlOrBuffer: string | URL | BufferSource, imports?: WebAssembly.Imports): Promise<WebAssembly.WebAssemblyInstantiatedSource>;
|
|
35
|
+
export declare function asyncifyLoad(asyncify: AsyncifyOptions, urlOrBuffer: string | URL | BufferSource | WebAssembly.Module, imports?: WebAssembly.Imports): Promise<WebAssembly.WebAssemblyInstantiatedSource>;
|
|
36
36
|
|
|
37
37
|
/** @public */
|
|
38
|
-
export declare function asyncifyLoadSync(asyncify: AsyncifyOptions, buffer: BufferSource, imports?: WebAssembly.Imports): WebAssembly.WebAssemblyInstantiatedSource;
|
|
38
|
+
export declare function asyncifyLoadSync(asyncify: AsyncifyOptions, buffer: BufferSource | WebAssembly.Module, imports?: WebAssembly.Imports): WebAssembly.WebAssemblyInstantiatedSource;
|
|
39
39
|
|
|
40
40
|
/** @public */
|
|
41
41
|
export declare interface AsyncifyOptions {
|
|
@@ -76,6 +76,9 @@ export declare type BufferEncodingOption = 'buffer' | {
|
|
|
76
76
|
/** @public */
|
|
77
77
|
export declare type Callable = (...args: any[]) => any;
|
|
78
78
|
|
|
79
|
+
/** @public */
|
|
80
|
+
export declare function createAsyncWASI(options?: AsyncWASIOptions): Promise<WASI>;
|
|
81
|
+
|
|
79
82
|
/** @public */
|
|
80
83
|
export declare function extendMemory(memory: WebAssembly.Memory): Memory;
|
|
81
84
|
|
|
@@ -254,6 +257,8 @@ export declare interface IFsPromises {
|
|
|
254
257
|
symlink(target: PathLike, path: PathLike, type?: 'dir' | 'file' | 'junction'): Promise<void>;
|
|
255
258
|
}
|
|
256
259
|
|
|
260
|
+
declare const kBindingName: unique symbol;
|
|
261
|
+
|
|
257
262
|
declare const kExitCode: unique symbol;
|
|
258
263
|
|
|
259
264
|
declare const kInstance: unique symbol;
|
|
@@ -263,10 +268,10 @@ declare const kSetMemory: unique symbol;
|
|
|
263
268
|
declare const kStarted: unique symbol;
|
|
264
269
|
|
|
265
270
|
/** @public */
|
|
266
|
-
export declare function load(
|
|
271
|
+
export declare function load(wasmInput: string | URL | BufferSource | WebAssembly.Module, imports?: WebAssembly.Imports): Promise<WebAssembly.WebAssemblyInstantiatedSource>;
|
|
267
272
|
|
|
268
273
|
/** @public */
|
|
269
|
-
export declare function loadSync(
|
|
274
|
+
export declare function loadSync(wasmInput: BufferSource | WebAssembly.Module, imports?: WebAssembly.Imports): WebAssembly.WebAssemblyInstantiatedSource;
|
|
270
275
|
|
|
271
276
|
/** @public */
|
|
272
277
|
export declare interface MakeDirectoryOptions {
|
|
@@ -367,16 +372,17 @@ export declare class WASI {
|
|
|
367
372
|
private [kStarted];
|
|
368
373
|
private [kExitCode];
|
|
369
374
|
private [kInstance];
|
|
375
|
+
private [kBindingName];
|
|
370
376
|
readonly wasiImport: Record<string, any>;
|
|
371
|
-
static createSync(options?: SyncWASIOptions): WASI;
|
|
372
|
-
static createAsync(options?: AsyncWASIOptions): Promise<WASI>;
|
|
373
377
|
constructor(options?: SyncWASIOptions);
|
|
374
378
|
start(instance: WebAssembly.Instance): number | undefined | Promise<number> | Promise<undefined>;
|
|
375
379
|
initialize(instance: WebAssembly.Instance): void | Promise<void>;
|
|
380
|
+
getImportObject(): Record<string, Record<string, any>>;
|
|
376
381
|
}
|
|
377
382
|
|
|
378
383
|
/** @public */
|
|
379
384
|
export declare interface WASIOptions {
|
|
385
|
+
version?: 'unstable' | 'preview1';
|
|
380
386
|
args?: string[] | undefined;
|
|
381
387
|
env?: Record<string, string> | undefined;
|
|
382
388
|
preopens?: Record<string, string> | undefined;
|
|
@@ -225,30 +225,35 @@ function fetchWasm(urlOrBuffer, imports) {
|
|
|
225
225
|
.then(buffer => _WebAssembly.instantiate(buffer, imports));
|
|
226
226
|
}
|
|
227
227
|
/** @public */
|
|
228
|
-
function load(
|
|
228
|
+
function load(wasmInput, imports) {
|
|
229
229
|
validateImports(imports);
|
|
230
230
|
imports = imports !== null && imports !== void 0 ? imports : {};
|
|
231
231
|
let source;
|
|
232
|
-
if (
|
|
233
|
-
return _WebAssembly.instantiate(
|
|
232
|
+
if (wasmInput instanceof ArrayBuffer || ArrayBuffer.isView(wasmInput)) {
|
|
233
|
+
return _WebAssembly.instantiate(wasmInput, imports);
|
|
234
|
+
}
|
|
235
|
+
if (wasmInput instanceof _WebAssembly.Module) {
|
|
236
|
+
return _WebAssembly.instantiate(wasmInput, imports).then((instance) => {
|
|
237
|
+
return { instance, module: wasmInput };
|
|
238
|
+
});
|
|
234
239
|
}
|
|
235
|
-
if (typeof
|
|
240
|
+
if (typeof wasmInput !== 'string' && !(wasmInput instanceof URL)) {
|
|
236
241
|
throw new TypeError('Invalid source');
|
|
237
242
|
}
|
|
238
243
|
if (typeof _WebAssembly.instantiateStreaming === 'function') {
|
|
239
244
|
let responsePromise;
|
|
240
245
|
try {
|
|
241
|
-
responsePromise = fetch(
|
|
246
|
+
responsePromise = fetch(wasmInput);
|
|
242
247
|
source = _WebAssembly.instantiateStreaming(responsePromise, imports).catch(() => {
|
|
243
|
-
return fetchWasm(
|
|
248
|
+
return fetchWasm(wasmInput, imports);
|
|
244
249
|
});
|
|
245
250
|
}
|
|
246
251
|
catch (_) {
|
|
247
|
-
source = fetchWasm(
|
|
252
|
+
source = fetchWasm(wasmInput, imports);
|
|
248
253
|
}
|
|
249
254
|
}
|
|
250
255
|
else {
|
|
251
|
-
source = fetchWasm(
|
|
256
|
+
source = fetchWasm(wasmInput, imports);
|
|
252
257
|
}
|
|
253
258
|
return source;
|
|
254
259
|
}
|
|
@@ -265,13 +270,19 @@ function asyncifyLoad(asyncify, urlOrBuffer, imports) {
|
|
|
265
270
|
});
|
|
266
271
|
}
|
|
267
272
|
/** @public */
|
|
268
|
-
function loadSync(
|
|
269
|
-
if ((buffer instanceof ArrayBuffer) && !ArrayBuffer.isView(buffer)) {
|
|
270
|
-
throw new TypeError('Invalid source');
|
|
271
|
-
}
|
|
273
|
+
function loadSync(wasmInput, imports) {
|
|
272
274
|
validateImports(imports);
|
|
273
275
|
imports = imports !== null && imports !== void 0 ? imports : {};
|
|
274
|
-
|
|
276
|
+
let module;
|
|
277
|
+
if ((wasmInput instanceof ArrayBuffer) || ArrayBuffer.isView(wasmInput)) {
|
|
278
|
+
module = new _WebAssembly.Module(wasmInput);
|
|
279
|
+
}
|
|
280
|
+
else if (wasmInput instanceof WebAssembly.Module) {
|
|
281
|
+
module = wasmInput;
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
throw new TypeError('Invalid source');
|
|
285
|
+
}
|
|
275
286
|
const instance = new _WebAssembly.Instance(module, imports);
|
|
276
287
|
const source = { instance, module };
|
|
277
288
|
return source;
|
|
@@ -1449,7 +1460,14 @@ class WASI$1 {
|
|
|
1449
1460
|
return 28 /* WasiErrno.EINVAL */;
|
|
1450
1461
|
}
|
|
1451
1462
|
buf_len = Number(buf_len);
|
|
1452
|
-
const { HEAPU8 } = getMemory(this);
|
|
1463
|
+
const { HEAPU8, view } = getMemory(this);
|
|
1464
|
+
if ((typeof SharedArrayBuffer === 'function' && HEAPU8.buffer instanceof SharedArrayBuffer) ||
|
|
1465
|
+
(Object.prototype.toString.call(HEAPU8.buffer) === '[object SharedArrayBuffer]')) {
|
|
1466
|
+
for (let i = buf; i < buf + buf_len; ++i) {
|
|
1467
|
+
view.setUint8(i, Math.floor(Math.random() * 256));
|
|
1468
|
+
}
|
|
1469
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1470
|
+
}
|
|
1453
1471
|
let pos;
|
|
1454
1472
|
const stride = 65536;
|
|
1455
1473
|
for (pos = 0; pos + stride < buf_len; pos += stride) {
|
|
@@ -2472,17 +2490,36 @@ const kExitCode = Symbol('kExitCode');
|
|
|
2472
2490
|
const kSetMemory = Symbol('kSetMemory');
|
|
2473
2491
|
const kStarted = Symbol('kStarted');
|
|
2474
2492
|
const kInstance = Symbol('kInstance');
|
|
2493
|
+
const kBindingName = Symbol('kBindingName');
|
|
2475
2494
|
function setupInstance(self, instance) {
|
|
2476
2495
|
validateObject(instance, 'instance');
|
|
2477
2496
|
validateObject(instance.exports, 'instance.exports');
|
|
2478
2497
|
self[kInstance] = instance;
|
|
2479
2498
|
self[kSetMemory](instance.exports.memory);
|
|
2480
2499
|
}
|
|
2481
|
-
// /** @public */
|
|
2482
|
-
// export type WasiSnapshotPreview1 = Omit<_WASI, '_setMemory'>
|
|
2483
2500
|
function validateOptions(options) {
|
|
2484
2501
|
var _a;
|
|
2485
2502
|
validateObject(options, 'options');
|
|
2503
|
+
let _WASI;
|
|
2504
|
+
if (options.version !== undefined) {
|
|
2505
|
+
validateString(options.version, 'options.version');
|
|
2506
|
+
switch (options.version) {
|
|
2507
|
+
case 'unstable':
|
|
2508
|
+
_WASI = WASI$1;
|
|
2509
|
+
this[kBindingName] = 'wasi_unstable';
|
|
2510
|
+
break;
|
|
2511
|
+
case 'preview1':
|
|
2512
|
+
_WASI = WASI$1;
|
|
2513
|
+
this[kBindingName] = 'wasi_snapshot_preview1';
|
|
2514
|
+
break;
|
|
2515
|
+
default:
|
|
2516
|
+
throw new TypeError(`unsupported WASI version "${options.version}"`);
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
else {
|
|
2520
|
+
_WASI = WASI$1;
|
|
2521
|
+
this[kBindingName] = 'wasi_snapshot_preview1';
|
|
2522
|
+
}
|
|
2486
2523
|
if (options.args !== undefined) {
|
|
2487
2524
|
validateArray(options.args, 'options.args');
|
|
2488
2525
|
}
|
|
@@ -2541,7 +2578,8 @@ function validateOptions(options) {
|
|
|
2541
2578
|
args,
|
|
2542
2579
|
env,
|
|
2543
2580
|
preopens,
|
|
2544
|
-
stdio
|
|
2581
|
+
stdio,
|
|
2582
|
+
_WASI
|
|
2545
2583
|
};
|
|
2546
2584
|
}
|
|
2547
2585
|
function initWASI(setMemory, wrap) {
|
|
@@ -2554,8 +2592,8 @@ function initWASI(setMemory, wrap) {
|
|
|
2554
2592
|
/** @public */
|
|
2555
2593
|
class WASI {
|
|
2556
2594
|
constructor(options = kEmptyObject) {
|
|
2557
|
-
const { args, env, preopens, stdio } = validateOptions(options);
|
|
2558
|
-
const wrap =
|
|
2595
|
+
const { args, env, preopens, stdio, _WASI } = validateOptions.call(this, options);
|
|
2596
|
+
const wrap = _WASI.createSync(args, env, preopens, stdio, options.fs, options.print, options.printErr);
|
|
2559
2597
|
const setMemory = wrap._setMemory;
|
|
2560
2598
|
delete wrap._setMemory;
|
|
2561
2599
|
initWASI.call(this, setMemory, wrap);
|
|
@@ -2563,25 +2601,6 @@ class WASI {
|
|
|
2563
2601
|
wrap.proc_exit = wasiReturnOnProcExit.bind(this);
|
|
2564
2602
|
}
|
|
2565
2603
|
}
|
|
2566
|
-
static createSync(options = kEmptyObject) {
|
|
2567
|
-
return new WASI(options);
|
|
2568
|
-
}
|
|
2569
|
-
static async createAsync(options = kEmptyObject) {
|
|
2570
|
-
const { args, env, preopens, stdio } = validateOptions(options);
|
|
2571
|
-
if (options.asyncify !== undefined) {
|
|
2572
|
-
validateObject(options.asyncify, 'options.asyncify');
|
|
2573
|
-
validateFunction(options.asyncify.wrapImportFunction, 'options.asyncify.wrapImportFunction');
|
|
2574
|
-
}
|
|
2575
|
-
const wrap = await WASI$1.createAsync(args, env, preopens, stdio, options.fs, options.print, options.printErr, options.asyncify);
|
|
2576
|
-
const setMemory = wrap._setMemory;
|
|
2577
|
-
delete wrap._setMemory;
|
|
2578
|
-
const _this = Object.create(WASI.prototype);
|
|
2579
|
-
initWASI.call(_this, setMemory, wrap);
|
|
2580
|
-
if (options.returnOnExit) {
|
|
2581
|
-
wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
|
|
2582
|
-
}
|
|
2583
|
-
return _this;
|
|
2584
|
-
}
|
|
2585
2604
|
// Must not export _initialize, must export _start
|
|
2586
2605
|
start(instance) {
|
|
2587
2606
|
if (this[kStarted]) {
|
|
@@ -2625,11 +2644,31 @@ class WASI {
|
|
|
2625
2644
|
return _initialize();
|
|
2626
2645
|
}
|
|
2627
2646
|
}
|
|
2647
|
+
getImportObject() {
|
|
2648
|
+
return { [this[kBindingName]]: this.wasiImport };
|
|
2649
|
+
}
|
|
2628
2650
|
}
|
|
2629
2651
|
function wasiReturnOnProcExit(rval) {
|
|
2630
2652
|
this[kExitCode] = rval;
|
|
2631
2653
|
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
2632
2654
|
throw kExitCode;
|
|
2633
2655
|
}
|
|
2656
|
+
/** @public */
|
|
2657
|
+
async function createAsyncWASI(options = kEmptyObject) {
|
|
2658
|
+
const _this = Object.create(WASI.prototype);
|
|
2659
|
+
const { args, env, preopens, stdio, _WASI } = validateOptions.call(_this, options);
|
|
2660
|
+
if (options.asyncify !== undefined) {
|
|
2661
|
+
validateObject(options.asyncify, 'options.asyncify');
|
|
2662
|
+
validateFunction(options.asyncify.wrapImportFunction, 'options.asyncify.wrapImportFunction');
|
|
2663
|
+
}
|
|
2664
|
+
const wrap = await _WASI.createAsync(args, env, preopens, stdio, options.fs, options.print, options.printErr, options.asyncify);
|
|
2665
|
+
const setMemory = wrap._setMemory;
|
|
2666
|
+
delete wrap._setMemory;
|
|
2667
|
+
initWASI.call(_this, setMemory, wrap);
|
|
2668
|
+
if (options.returnOnExit) {
|
|
2669
|
+
wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
|
|
2670
|
+
}
|
|
2671
|
+
return _this;
|
|
2672
|
+
}
|
|
2634
2673
|
|
|
2635
|
-
export { Asyncify, Memory, WASI, WebAssemblyMemory, asyncifyLoad, asyncifyLoadSync, extendMemory, load, loadSync, wrapAsyncExport, wrapAsyncImport, wrapExports };
|
|
2674
|
+
export { Asyncify, Memory, WASI, WebAssemblyMemory, asyncifyLoad, asyncifyLoadSync, createAsyncWASI, extendMemory, load, loadSync, wrapAsyncExport, wrapAsyncImport, wrapExports };
|
package/dist/wasm-util.esm.js
CHANGED
|
@@ -225,30 +225,35 @@ function fetchWasm(urlOrBuffer, imports) {
|
|
|
225
225
|
.then(buffer => _WebAssembly.instantiate(buffer, imports));
|
|
226
226
|
}
|
|
227
227
|
/** @public */
|
|
228
|
-
function load(
|
|
228
|
+
function load(wasmInput, imports) {
|
|
229
229
|
validateImports(imports);
|
|
230
230
|
imports = imports !== null && imports !== void 0 ? imports : {};
|
|
231
231
|
let source;
|
|
232
|
-
if (
|
|
233
|
-
return _WebAssembly.instantiate(
|
|
232
|
+
if (wasmInput instanceof ArrayBuffer || ArrayBuffer.isView(wasmInput)) {
|
|
233
|
+
return _WebAssembly.instantiate(wasmInput, imports);
|
|
234
|
+
}
|
|
235
|
+
if (wasmInput instanceof _WebAssembly.Module) {
|
|
236
|
+
return _WebAssembly.instantiate(wasmInput, imports).then((instance) => {
|
|
237
|
+
return { instance, module: wasmInput };
|
|
238
|
+
});
|
|
234
239
|
}
|
|
235
|
-
if (typeof
|
|
240
|
+
if (typeof wasmInput !== 'string' && !(wasmInput instanceof URL)) {
|
|
236
241
|
throw new TypeError('Invalid source');
|
|
237
242
|
}
|
|
238
243
|
if (typeof _WebAssembly.instantiateStreaming === 'function') {
|
|
239
244
|
let responsePromise;
|
|
240
245
|
try {
|
|
241
|
-
responsePromise = fetch(
|
|
246
|
+
responsePromise = fetch(wasmInput);
|
|
242
247
|
source = _WebAssembly.instantiateStreaming(responsePromise, imports).catch(() => {
|
|
243
|
-
return fetchWasm(
|
|
248
|
+
return fetchWasm(wasmInput, imports);
|
|
244
249
|
});
|
|
245
250
|
}
|
|
246
251
|
catch (_) {
|
|
247
|
-
source = fetchWasm(
|
|
252
|
+
source = fetchWasm(wasmInput, imports);
|
|
248
253
|
}
|
|
249
254
|
}
|
|
250
255
|
else {
|
|
251
|
-
source = fetchWasm(
|
|
256
|
+
source = fetchWasm(wasmInput, imports);
|
|
252
257
|
}
|
|
253
258
|
return source;
|
|
254
259
|
}
|
|
@@ -265,13 +270,19 @@ function asyncifyLoad(asyncify, urlOrBuffer, imports) {
|
|
|
265
270
|
});
|
|
266
271
|
}
|
|
267
272
|
/** @public */
|
|
268
|
-
function loadSync(
|
|
269
|
-
if ((buffer instanceof ArrayBuffer) && !ArrayBuffer.isView(buffer)) {
|
|
270
|
-
throw new TypeError('Invalid source');
|
|
271
|
-
}
|
|
273
|
+
function loadSync(wasmInput, imports) {
|
|
272
274
|
validateImports(imports);
|
|
273
275
|
imports = imports !== null && imports !== void 0 ? imports : {};
|
|
274
|
-
|
|
276
|
+
let module;
|
|
277
|
+
if ((wasmInput instanceof ArrayBuffer) || ArrayBuffer.isView(wasmInput)) {
|
|
278
|
+
module = new _WebAssembly.Module(wasmInput);
|
|
279
|
+
}
|
|
280
|
+
else if (wasmInput instanceof WebAssembly.Module) {
|
|
281
|
+
module = wasmInput;
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
throw new TypeError('Invalid source');
|
|
285
|
+
}
|
|
275
286
|
const instance = new _WebAssembly.Instance(module, imports);
|
|
276
287
|
const source = { instance, module };
|
|
277
288
|
return source;
|
|
@@ -1449,7 +1460,14 @@ class WASI$1 {
|
|
|
1449
1460
|
return 28 /* WasiErrno.EINVAL */;
|
|
1450
1461
|
}
|
|
1451
1462
|
buf_len = Number(buf_len);
|
|
1452
|
-
const { HEAPU8 } = getMemory(this);
|
|
1463
|
+
const { HEAPU8, view } = getMemory(this);
|
|
1464
|
+
if ((typeof SharedArrayBuffer === 'function' && HEAPU8.buffer instanceof SharedArrayBuffer) ||
|
|
1465
|
+
(Object.prototype.toString.call(HEAPU8.buffer) === '[object SharedArrayBuffer]')) {
|
|
1466
|
+
for (let i = buf; i < buf + buf_len; ++i) {
|
|
1467
|
+
view.setUint8(i, Math.floor(Math.random() * 256));
|
|
1468
|
+
}
|
|
1469
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1470
|
+
}
|
|
1453
1471
|
let pos;
|
|
1454
1472
|
const stride = 65536;
|
|
1455
1473
|
for (pos = 0; pos + stride < buf_len; pos += stride) {
|
|
@@ -2472,17 +2490,36 @@ const kExitCode = Symbol('kExitCode');
|
|
|
2472
2490
|
const kSetMemory = Symbol('kSetMemory');
|
|
2473
2491
|
const kStarted = Symbol('kStarted');
|
|
2474
2492
|
const kInstance = Symbol('kInstance');
|
|
2493
|
+
const kBindingName = Symbol('kBindingName');
|
|
2475
2494
|
function setupInstance(self, instance) {
|
|
2476
2495
|
validateObject(instance, 'instance');
|
|
2477
2496
|
validateObject(instance.exports, 'instance.exports');
|
|
2478
2497
|
self[kInstance] = instance;
|
|
2479
2498
|
self[kSetMemory](instance.exports.memory);
|
|
2480
2499
|
}
|
|
2481
|
-
// /** @public */
|
|
2482
|
-
// export type WasiSnapshotPreview1 = Omit<_WASI, '_setMemory'>
|
|
2483
2500
|
function validateOptions(options) {
|
|
2484
2501
|
var _a;
|
|
2485
2502
|
validateObject(options, 'options');
|
|
2503
|
+
let _WASI;
|
|
2504
|
+
if (options.version !== undefined) {
|
|
2505
|
+
validateString(options.version, 'options.version');
|
|
2506
|
+
switch (options.version) {
|
|
2507
|
+
case 'unstable':
|
|
2508
|
+
_WASI = WASI$1;
|
|
2509
|
+
this[kBindingName] = 'wasi_unstable';
|
|
2510
|
+
break;
|
|
2511
|
+
case 'preview1':
|
|
2512
|
+
_WASI = WASI$1;
|
|
2513
|
+
this[kBindingName] = 'wasi_snapshot_preview1';
|
|
2514
|
+
break;
|
|
2515
|
+
default:
|
|
2516
|
+
throw new TypeError(`unsupported WASI version "${options.version}"`);
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
else {
|
|
2520
|
+
_WASI = WASI$1;
|
|
2521
|
+
this[kBindingName] = 'wasi_snapshot_preview1';
|
|
2522
|
+
}
|
|
2486
2523
|
if (options.args !== undefined) {
|
|
2487
2524
|
validateArray(options.args, 'options.args');
|
|
2488
2525
|
}
|
|
@@ -2541,7 +2578,8 @@ function validateOptions(options) {
|
|
|
2541
2578
|
args,
|
|
2542
2579
|
env,
|
|
2543
2580
|
preopens,
|
|
2544
|
-
stdio
|
|
2581
|
+
stdio,
|
|
2582
|
+
_WASI
|
|
2545
2583
|
};
|
|
2546
2584
|
}
|
|
2547
2585
|
function initWASI(setMemory, wrap) {
|
|
@@ -2554,8 +2592,8 @@ function initWASI(setMemory, wrap) {
|
|
|
2554
2592
|
/** @public */
|
|
2555
2593
|
class WASI {
|
|
2556
2594
|
constructor(options = kEmptyObject) {
|
|
2557
|
-
const { args, env, preopens, stdio } = validateOptions(options);
|
|
2558
|
-
const wrap =
|
|
2595
|
+
const { args, env, preopens, stdio, _WASI } = validateOptions.call(this, options);
|
|
2596
|
+
const wrap = _WASI.createSync(args, env, preopens, stdio, options.fs, options.print, options.printErr);
|
|
2559
2597
|
const setMemory = wrap._setMemory;
|
|
2560
2598
|
delete wrap._setMemory;
|
|
2561
2599
|
initWASI.call(this, setMemory, wrap);
|
|
@@ -2563,25 +2601,6 @@ class WASI {
|
|
|
2563
2601
|
wrap.proc_exit = wasiReturnOnProcExit.bind(this);
|
|
2564
2602
|
}
|
|
2565
2603
|
}
|
|
2566
|
-
static createSync(options = kEmptyObject) {
|
|
2567
|
-
return new WASI(options);
|
|
2568
|
-
}
|
|
2569
|
-
static async createAsync(options = kEmptyObject) {
|
|
2570
|
-
const { args, env, preopens, stdio } = validateOptions(options);
|
|
2571
|
-
if (options.asyncify !== undefined) {
|
|
2572
|
-
validateObject(options.asyncify, 'options.asyncify');
|
|
2573
|
-
validateFunction(options.asyncify.wrapImportFunction, 'options.asyncify.wrapImportFunction');
|
|
2574
|
-
}
|
|
2575
|
-
const wrap = await WASI$1.createAsync(args, env, preopens, stdio, options.fs, options.print, options.printErr, options.asyncify);
|
|
2576
|
-
const setMemory = wrap._setMemory;
|
|
2577
|
-
delete wrap._setMemory;
|
|
2578
|
-
const _this = Object.create(WASI.prototype);
|
|
2579
|
-
initWASI.call(_this, setMemory, wrap);
|
|
2580
|
-
if (options.returnOnExit) {
|
|
2581
|
-
wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
|
|
2582
|
-
}
|
|
2583
|
-
return _this;
|
|
2584
|
-
}
|
|
2585
2604
|
// Must not export _initialize, must export _start
|
|
2586
2605
|
start(instance) {
|
|
2587
2606
|
if (this[kStarted]) {
|
|
@@ -2625,11 +2644,31 @@ class WASI {
|
|
|
2625
2644
|
return _initialize();
|
|
2626
2645
|
}
|
|
2627
2646
|
}
|
|
2647
|
+
getImportObject() {
|
|
2648
|
+
return { [this[kBindingName]]: this.wasiImport };
|
|
2649
|
+
}
|
|
2628
2650
|
}
|
|
2629
2651
|
function wasiReturnOnProcExit(rval) {
|
|
2630
2652
|
this[kExitCode] = rval;
|
|
2631
2653
|
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
2632
2654
|
throw kExitCode;
|
|
2633
2655
|
}
|
|
2656
|
+
/** @public */
|
|
2657
|
+
async function createAsyncWASI(options = kEmptyObject) {
|
|
2658
|
+
const _this = Object.create(WASI.prototype);
|
|
2659
|
+
const { args, env, preopens, stdio, _WASI } = validateOptions.call(_this, options);
|
|
2660
|
+
if (options.asyncify !== undefined) {
|
|
2661
|
+
validateObject(options.asyncify, 'options.asyncify');
|
|
2662
|
+
validateFunction(options.asyncify.wrapImportFunction, 'options.asyncify.wrapImportFunction');
|
|
2663
|
+
}
|
|
2664
|
+
const wrap = await _WASI.createAsync(args, env, preopens, stdio, options.fs, options.print, options.printErr, options.asyncify);
|
|
2665
|
+
const setMemory = wrap._setMemory;
|
|
2666
|
+
delete wrap._setMemory;
|
|
2667
|
+
initWASI.call(_this, setMemory, wrap);
|
|
2668
|
+
if (options.returnOnExit) {
|
|
2669
|
+
wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
|
|
2670
|
+
}
|
|
2671
|
+
return _this;
|
|
2672
|
+
}
|
|
2634
2673
|
|
|
2635
|
-
export { Asyncify, Memory, WASI, WebAssemblyMemory, asyncifyLoad, asyncifyLoadSync, extendMemory, load, loadSync, wrapAsyncExport, wrapAsyncImport, wrapExports };
|
|
2674
|
+
export { Asyncify, Memory, WASI, WebAssemblyMemory, asyncifyLoad, asyncifyLoadSync, createAsyncWASI, extendMemory, load, loadSync, wrapAsyncExport, wrapAsyncImport, wrapExports };
|