@tybys/wasm-util 0.4.0 → 0.5.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 +2 -2
- package/dist/wasm-util.d.ts +25 -8
- package/dist/wasm-util.esm-bundler.js +1042 -476
- package/dist/wasm-util.esm.js +1042 -476
- package/dist/wasm-util.esm.min.js +1 -1
- package/dist/wasm-util.js +1043 -475
- package/dist/wasm-util.min.js +1 -1
- package/lib/cjs/asyncify.js +22 -31
- package/lib/cjs/load.js +33 -32
- package/lib/cjs/memory.js +1 -0
- package/lib/cjs/wasi/fd.js +57 -15
- package/lib/cjs/wasi/index.js +92 -60
- package/lib/cjs/wasi/preview1.js +684 -209
- package/lib/cjs/wasi/rights.js +1 -0
- package/lib/cjs/wasi/types.js +61 -30
- package/lib/mjs/asyncify.mjs +22 -31
- package/lib/mjs/load.mjs +30 -31
- package/lib/mjs/memory.mjs +1 -1
- package/lib/mjs/wasi/fd.mjs +53 -13
- package/lib/mjs/wasi/index.mjs +93 -61
- package/lib/mjs/wasi/preview1.mjs +685 -210
- package/lib/mjs/wasi/rights.mjs +2 -2
- package/lib/mjs/wasi/types.mjs +61 -30
- package/package.json +1 -1
package/lib/cjs/wasi/index.js
CHANGED
|
@@ -3,7 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.WASI = void 0;
|
|
4
4
|
const preview1_1 = require("./preview1");
|
|
5
5
|
const util_1 = require("./util");
|
|
6
|
-
|
|
6
|
+
// eslint-disable-next-line spaced-comment
|
|
7
|
+
const kEmptyObject = /*#__PURE__*/ Object.freeze(/*#__PURE__*/ Object.create(null));
|
|
7
8
|
const kExitCode = Symbol('kExitCode');
|
|
8
9
|
const kSetMemory = Symbol('kSetMemory');
|
|
9
10
|
const kStarted = Symbol('kStarted');
|
|
@@ -16,74 +17,105 @@ function setupInstance(self, instance) {
|
|
|
16
17
|
}
|
|
17
18
|
// /** @public */
|
|
18
19
|
// export type WasiSnapshotPreview1 = Omit<_WASI, '_setMemory'>
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
(0, util_1.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
(
|
|
31
|
-
|
|
32
|
-
if (value !== undefined) {
|
|
33
|
-
env.push(`${key}=${value}`);
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
const preopens = [];
|
|
38
|
-
if (options.preopens !== undefined) {
|
|
39
|
-
(0, util_1.validateObject)(options.preopens, 'options.preopens');
|
|
40
|
-
Object.entries(options.preopens).forEach(({ 0: key, 1: value }) => preopens.push({ mappedPath: String(key), realPath: String(value) }));
|
|
41
|
-
}
|
|
42
|
-
if (preopens.length > 0) {
|
|
43
|
-
if (options.filesystem === undefined) {
|
|
44
|
-
throw new Error('filesystem is disabled, can not preopen directory');
|
|
20
|
+
function validateOptions(options) {
|
|
21
|
+
var _a;
|
|
22
|
+
(0, util_1.validateObject)(options, 'options');
|
|
23
|
+
if (options.args !== undefined) {
|
|
24
|
+
(0, util_1.validateArray)(options.args, 'options.args');
|
|
25
|
+
}
|
|
26
|
+
const args = ((_a = options.args) !== null && _a !== void 0 ? _a : []).map(String);
|
|
27
|
+
const env = [];
|
|
28
|
+
if (options.env !== undefined) {
|
|
29
|
+
(0, util_1.validateObject)(options.env, 'options.env');
|
|
30
|
+
Object.entries(options.env).forEach(({ 0: key, 1: value }) => {
|
|
31
|
+
if (value !== undefined) {
|
|
32
|
+
env.push(`${key}=${value}`);
|
|
45
33
|
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const preopens = [];
|
|
37
|
+
if (options.preopens !== undefined) {
|
|
38
|
+
(0, util_1.validateObject)(options.preopens, 'options.preopens');
|
|
39
|
+
Object.entries(options.preopens).forEach(({ 0: key, 1: value }) => preopens.push({ mappedPath: String(key), realPath: String(value) }));
|
|
40
|
+
}
|
|
41
|
+
if (preopens.length > 0) {
|
|
42
|
+
if (options.fs === undefined) {
|
|
43
|
+
throw new Error('filesystem is disabled, can not preopen directory');
|
|
46
44
|
}
|
|
47
|
-
|
|
48
|
-
(0, util_1.validateObject)(options.
|
|
49
|
-
(0, util_1.validateString)(options.filesystem.type, 'options.filesystem.type');
|
|
50
|
-
if (options.filesystem.type !== 'memfs') {
|
|
51
|
-
throw new Error(`Filesystem type ${options.filesystem.type} is not supported, only "memfs" is supported currently`);
|
|
52
|
-
}
|
|
53
|
-
try {
|
|
54
|
-
(0, util_1.validateObject)(options.filesystem.fs, 'options.filesystem.fs');
|
|
55
|
-
}
|
|
56
|
-
catch (_) {
|
|
57
|
-
throw new Error('Node.js fs like implementation is not provided');
|
|
58
|
-
}
|
|
45
|
+
try {
|
|
46
|
+
(0, util_1.validateObject)(options.fs, 'options.fs');
|
|
59
47
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (options.printErr !== undefined)
|
|
63
|
-
(0, util_1.validateFunction)(options.printErr, 'options.printErr');
|
|
64
|
-
// const { stdin = 0, stdout = 1, stderr = 2 } = options
|
|
65
|
-
// validateInt32(stdin, 'options.stdin', 0)
|
|
66
|
-
// validateInt32(stdout, 'options.stdout', 0)
|
|
67
|
-
// validateInt32(stderr, 'options.stderr', 0)
|
|
68
|
-
// const stdio = [stdin, stdout, stderr] as const
|
|
69
|
-
const stdio = [0, 1, 2];
|
|
70
|
-
const wrap = new preview1_1.WASI(args, env, preopens, stdio, options.filesystem, options.print, options.printErr);
|
|
71
|
-
for (const prop in wrap) {
|
|
72
|
-
wrap[prop] = wrap[prop].bind(wrap);
|
|
48
|
+
catch (_) {
|
|
49
|
+
throw new TypeError('Node.js fs like implementation is not provided');
|
|
73
50
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
51
|
+
}
|
|
52
|
+
// if (options.filesystem !== undefined) {
|
|
53
|
+
// validateObject(options.filesystem, 'options.filesystem')
|
|
54
|
+
// validateString(options.filesystem.type, 'options.filesystem.type')
|
|
55
|
+
// if (options.filesystem.type !== 'memfs' && options.filesystem.type !== 'file-system-access-api') {
|
|
56
|
+
// throw new Error(`Filesystem type ${(options.filesystem as any).type as string} is not supported, only "memfs" and "file-system-access-api" is supported currently`)
|
|
57
|
+
// }
|
|
58
|
+
// try {
|
|
59
|
+
// validateObject(options.filesystem.fs, 'options.filesystem.fs')
|
|
60
|
+
// } catch (_) {
|
|
61
|
+
// throw new Error('Node.js fs like implementation is not provided')
|
|
62
|
+
// }
|
|
63
|
+
// }
|
|
64
|
+
if (options.print !== undefined)
|
|
65
|
+
(0, util_1.validateFunction)(options.print, 'options.print');
|
|
66
|
+
if (options.printErr !== undefined)
|
|
67
|
+
(0, util_1.validateFunction)(options.printErr, 'options.printErr');
|
|
68
|
+
if (options.returnOnExit !== undefined) {
|
|
69
|
+
(0, util_1.validateBoolean)(options.returnOnExit, 'options.returnOnExit');
|
|
70
|
+
}
|
|
71
|
+
// const { stdin = 0, stdout = 1, stderr = 2 } = options
|
|
72
|
+
// validateInt32(stdin, 'options.stdin', 0)
|
|
73
|
+
// validateInt32(stdout, 'options.stdout', 0)
|
|
74
|
+
// validateInt32(stderr, 'options.stderr', 0)
|
|
75
|
+
// const stdio = [stdin, stdout, stderr] as const
|
|
76
|
+
const stdio = [0, 1, 2];
|
|
77
|
+
return {
|
|
78
|
+
args,
|
|
79
|
+
env,
|
|
80
|
+
preopens,
|
|
81
|
+
stdio
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** @public */
|
|
85
|
+
class WASI {
|
|
86
|
+
constructor(setMemory, wrap) {
|
|
87
|
+
this[kSetMemory] = setMemory;
|
|
82
88
|
this.wasiImport = wrap;
|
|
83
89
|
this[kStarted] = false;
|
|
84
90
|
this[kExitCode] = 0;
|
|
85
91
|
this[kInstance] = undefined;
|
|
86
92
|
}
|
|
93
|
+
static createSync(options = kEmptyObject) {
|
|
94
|
+
const { args, env, preopens, stdio } = validateOptions(options);
|
|
95
|
+
const wrap = preview1_1.WASI.createSync(args, env, preopens, stdio, options.fs, options.print, options.printErr);
|
|
96
|
+
const setMemory = wrap._setMemory;
|
|
97
|
+
delete wrap._setMemory;
|
|
98
|
+
const _this = new WASI(setMemory, wrap);
|
|
99
|
+
if (options.returnOnExit) {
|
|
100
|
+
wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
|
|
101
|
+
}
|
|
102
|
+
return _this;
|
|
103
|
+
}
|
|
104
|
+
static async createAsync(options = kEmptyObject) {
|
|
105
|
+
const { args, env, preopens, stdio } = validateOptions(options);
|
|
106
|
+
if (options.asyncify !== undefined) {
|
|
107
|
+
(0, util_1.validateObject)(options.asyncify, 'options.asyncify');
|
|
108
|
+
(0, util_1.validateFunction)(options.asyncify.wrapImportFunction, 'options.asyncify.wrapImportFunction');
|
|
109
|
+
}
|
|
110
|
+
const wrap = await preview1_1.WASI.createAsync(args, env, preopens, stdio, options.fs, options.print, options.printErr, options.asyncify);
|
|
111
|
+
const setMemory = wrap._setMemory;
|
|
112
|
+
delete wrap._setMemory;
|
|
113
|
+
const _this = new WASI(setMemory, wrap);
|
|
114
|
+
if (options.returnOnExit) {
|
|
115
|
+
wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
|
|
116
|
+
}
|
|
117
|
+
return _this;
|
|
118
|
+
}
|
|
87
119
|
// Must not export _initialize, must export _start
|
|
88
120
|
start(instance) {
|
|
89
121
|
if (this[kStarted]) {
|