@tybys/wasm-util 0.6.0 → 0.8.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 +193 -193
- package/dist/tsdoc-metadata.json +1 -1
- package/dist/wasm-util.d.ts +13 -7
- package/dist/wasm-util.esm-bundler.js +2639 -2604
- package/dist/wasm-util.esm.js +2639 -2604
- package/dist/wasm-util.esm.min.js +1 -1
- package/dist/wasm-util.js +2639 -2603
- package/dist/wasm-util.min.js +1 -1
- package/lib/cjs/asyncify.js +157 -157
- package/lib/cjs/index.js +12 -12
- package/lib/cjs/jspi.js +45 -45
- package/lib/cjs/load.js +96 -85
- package/lib/cjs/memfs.js +2689 -0
- package/lib/cjs/memory.js +34 -34
- package/lib/cjs/wasi/error.js +102 -102
- package/lib/cjs/wasi/fd.js +267 -267
- package/lib/cjs/wasi/fs.js +2 -2
- package/lib/cjs/wasi/index.js +193 -168
- package/lib/cjs/wasi/path.js +173 -173
- package/lib/cjs/wasi/preview1.js +1478 -1478
- package/lib/cjs/wasi/rights.js +139 -139
- package/lib/cjs/wasi/types.js +219 -219
- package/lib/cjs/wasi/util.js +121 -121
- package/lib/cjs/webassembly.js +12 -12
- package/lib/mjs/asyncify.mjs +153 -153
- package/lib/mjs/index.mjs +9 -9
- package/lib/mjs/jspi.mjs +39 -39
- package/lib/mjs/load.mjs +89 -78
- package/lib/mjs/memfs.mjs +2688 -0
- package/lib/mjs/memory.mjs +28 -28
- package/lib/mjs/wasi/error.mjs +97 -97
- package/lib/mjs/wasi/fd.mjs +256 -256
- package/lib/mjs/wasi/fs.mjs +1 -1
- package/lib/mjs/wasi/index.mjs +188 -164
- package/lib/mjs/wasi/path.mjs +168 -168
- package/lib/mjs/wasi/preview1.mjs +1474 -1474
- package/lib/mjs/wasi/rights.mjs +134 -134
- package/lib/mjs/wasi/types.mjs +216 -216
- package/lib/mjs/wasi/util.mjs +108 -108
- package/lib/mjs/webassembly.mjs +9 -9
- package/package.json +58 -58
package/lib/cjs/wasi/index.js
CHANGED
|
@@ -1,169 +1,194 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WASI = void 0;
|
|
4
|
-
const preview1_1 = require("./preview1");
|
|
5
|
-
const util_1 = require("./util");
|
|
6
|
-
// eslint-disable-next-line spaced-comment
|
|
7
|
-
const kEmptyObject = /*#__PURE__*/ Object.freeze(/*#__PURE__*/ Object.create(null));
|
|
8
|
-
const kExitCode = Symbol('kExitCode');
|
|
9
|
-
const kSetMemory = Symbol('kSetMemory');
|
|
10
|
-
const kStarted = Symbol('kStarted');
|
|
11
|
-
const kInstance = Symbol('kInstance');
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
(0, util_1.validateObject)(instance
|
|
15
|
-
|
|
16
|
-
self[
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (options.
|
|
24
|
-
(0, util_1.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
//
|
|
72
|
-
//
|
|
73
|
-
//
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAsyncWASI = exports.WASI = void 0;
|
|
4
|
+
const preview1_1 = require("./preview1");
|
|
5
|
+
const util_1 = require("./util");
|
|
6
|
+
// eslint-disable-next-line spaced-comment
|
|
7
|
+
const kEmptyObject = /*#__PURE__*/ Object.freeze(/*#__PURE__*/ Object.create(null));
|
|
8
|
+
const kExitCode = Symbol('kExitCode');
|
|
9
|
+
const kSetMemory = Symbol('kSetMemory');
|
|
10
|
+
const kStarted = Symbol('kStarted');
|
|
11
|
+
const kInstance = Symbol('kInstance');
|
|
12
|
+
const kBindingName = Symbol('kBindingName');
|
|
13
|
+
function setupInstance(self, instance) {
|
|
14
|
+
(0, util_1.validateObject)(instance, 'instance');
|
|
15
|
+
(0, util_1.validateObject)(instance.exports, 'instance.exports');
|
|
16
|
+
self[kInstance] = instance;
|
|
17
|
+
self[kSetMemory](instance.exports.memory);
|
|
18
|
+
}
|
|
19
|
+
function validateOptions(options) {
|
|
20
|
+
var _a;
|
|
21
|
+
(0, util_1.validateObject)(options, 'options');
|
|
22
|
+
let _WASI;
|
|
23
|
+
if (options.version !== undefined) {
|
|
24
|
+
(0, util_1.validateString)(options.version, 'options.version');
|
|
25
|
+
switch (options.version) {
|
|
26
|
+
case 'unstable':
|
|
27
|
+
_WASI = preview1_1.WASI;
|
|
28
|
+
this[kBindingName] = 'wasi_unstable';
|
|
29
|
+
break;
|
|
30
|
+
case 'preview1':
|
|
31
|
+
_WASI = preview1_1.WASI;
|
|
32
|
+
this[kBindingName] = 'wasi_snapshot_preview1';
|
|
33
|
+
break;
|
|
34
|
+
default:
|
|
35
|
+
throw new TypeError(`unsupported WASI version "${options.version}"`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
_WASI = preview1_1.WASI;
|
|
40
|
+
this[kBindingName] = 'wasi_snapshot_preview1';
|
|
41
|
+
}
|
|
42
|
+
if (options.args !== undefined) {
|
|
43
|
+
(0, util_1.validateArray)(options.args, 'options.args');
|
|
44
|
+
}
|
|
45
|
+
const args = ((_a = options.args) !== null && _a !== void 0 ? _a : []).map(String);
|
|
46
|
+
const env = [];
|
|
47
|
+
if (options.env !== undefined) {
|
|
48
|
+
(0, util_1.validateObject)(options.env, 'options.env');
|
|
49
|
+
Object.entries(options.env).forEach(({ 0: key, 1: value }) => {
|
|
50
|
+
if (value !== undefined) {
|
|
51
|
+
env.push(`${key}=${value}`);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const preopens = [];
|
|
56
|
+
if (options.preopens !== undefined) {
|
|
57
|
+
(0, util_1.validateObject)(options.preopens, 'options.preopens');
|
|
58
|
+
Object.entries(options.preopens).forEach(({ 0: key, 1: value }) => preopens.push({ mappedPath: String(key), realPath: String(value) }));
|
|
59
|
+
}
|
|
60
|
+
if (preopens.length > 0) {
|
|
61
|
+
if (options.fs === undefined) {
|
|
62
|
+
throw new Error('filesystem is disabled, can not preopen directory');
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
(0, util_1.validateObject)(options.fs, 'options.fs');
|
|
66
|
+
}
|
|
67
|
+
catch (_) {
|
|
68
|
+
throw new TypeError('Node.js fs like implementation is not provided');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// if (options.filesystem !== undefined) {
|
|
72
|
+
// validateObject(options.filesystem, 'options.filesystem')
|
|
73
|
+
// validateString(options.filesystem.type, 'options.filesystem.type')
|
|
74
|
+
// if (options.filesystem.type !== 'memfs' && options.filesystem.type !== 'file-system-access-api') {
|
|
75
|
+
// 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`)
|
|
76
|
+
// }
|
|
77
|
+
// try {
|
|
78
|
+
// validateObject(options.filesystem.fs, 'options.filesystem.fs')
|
|
79
|
+
// } catch (_) {
|
|
80
|
+
// throw new Error('Node.js fs like implementation is not provided')
|
|
81
|
+
// }
|
|
82
|
+
// }
|
|
83
|
+
if (options.print !== undefined)
|
|
84
|
+
(0, util_1.validateFunction)(options.print, 'options.print');
|
|
85
|
+
if (options.printErr !== undefined)
|
|
86
|
+
(0, util_1.validateFunction)(options.printErr, 'options.printErr');
|
|
87
|
+
if (options.returnOnExit !== undefined) {
|
|
88
|
+
(0, util_1.validateBoolean)(options.returnOnExit, 'options.returnOnExit');
|
|
89
|
+
}
|
|
90
|
+
// const { stdin = 0, stdout = 1, stderr = 2 } = options
|
|
91
|
+
// validateInt32(stdin, 'options.stdin', 0)
|
|
92
|
+
// validateInt32(stdout, 'options.stdout', 0)
|
|
93
|
+
// validateInt32(stderr, 'options.stderr', 0)
|
|
94
|
+
// const stdio = [stdin, stdout, stderr] as const
|
|
95
|
+
const stdio = [0, 1, 2];
|
|
96
|
+
return {
|
|
97
|
+
args,
|
|
98
|
+
env,
|
|
99
|
+
preopens,
|
|
100
|
+
stdio,
|
|
101
|
+
_WASI
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function initWASI(setMemory, wrap) {
|
|
105
|
+
this[kSetMemory] = setMemory;
|
|
106
|
+
this.wasiImport = wrap;
|
|
107
|
+
this[kStarted] = false;
|
|
108
|
+
this[kExitCode] = 0;
|
|
109
|
+
this[kInstance] = undefined;
|
|
110
|
+
}
|
|
111
|
+
/** @public */
|
|
112
|
+
class WASI {
|
|
113
|
+
constructor(options = kEmptyObject) {
|
|
114
|
+
const { args, env, preopens, stdio, _WASI } = validateOptions.call(this, options);
|
|
115
|
+
const wrap = _WASI.createSync(args, env, preopens, stdio, options.fs, options.print, options.printErr);
|
|
116
|
+
const setMemory = wrap._setMemory;
|
|
117
|
+
delete wrap._setMemory;
|
|
118
|
+
initWASI.call(this, setMemory, wrap);
|
|
119
|
+
if (options.returnOnExit) {
|
|
120
|
+
wrap.proc_exit = wasiReturnOnProcExit.bind(this);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Must not export _initialize, must export _start
|
|
124
|
+
start(instance) {
|
|
125
|
+
if (this[kStarted]) {
|
|
126
|
+
throw new Error('WASI instance has already started');
|
|
127
|
+
}
|
|
128
|
+
this[kStarted] = true;
|
|
129
|
+
setupInstance(this, instance);
|
|
130
|
+
const { _start, _initialize } = this[kInstance].exports;
|
|
131
|
+
(0, util_1.validateFunction)(_start, 'instance.exports._start');
|
|
132
|
+
(0, util_1.validateUndefined)(_initialize, 'instance.exports._initialize');
|
|
133
|
+
let ret;
|
|
134
|
+
try {
|
|
135
|
+
ret = _start();
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
if (err !== kExitCode) {
|
|
139
|
+
throw err;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (ret instanceof Promise) {
|
|
143
|
+
return ret.then(() => this[kExitCode], (err) => {
|
|
144
|
+
if (err !== kExitCode) {
|
|
145
|
+
throw err;
|
|
146
|
+
}
|
|
147
|
+
return this[kExitCode];
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
return this[kExitCode];
|
|
151
|
+
}
|
|
152
|
+
// Must not export _start, may optionally export _initialize
|
|
153
|
+
initialize(instance) {
|
|
154
|
+
if (this[kStarted]) {
|
|
155
|
+
throw new Error('WASI instance has already started');
|
|
156
|
+
}
|
|
157
|
+
this[kStarted] = true;
|
|
158
|
+
setupInstance(this, instance);
|
|
159
|
+
const { _start, _initialize } = this[kInstance].exports;
|
|
160
|
+
(0, util_1.validateUndefined)(_start, 'instance.exports._start');
|
|
161
|
+
if (_initialize !== undefined) {
|
|
162
|
+
(0, util_1.validateFunction)(_initialize, 'instance.exports._initialize');
|
|
163
|
+
return _initialize();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
getImportObject() {
|
|
167
|
+
return { [this[kBindingName]]: this.wasiImport };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
exports.WASI = WASI;
|
|
171
|
+
function wasiReturnOnProcExit(rval) {
|
|
172
|
+
this[kExitCode] = rval;
|
|
173
|
+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
174
|
+
throw kExitCode;
|
|
175
|
+
}
|
|
176
|
+
/** @public */
|
|
177
|
+
async function createAsyncWASI(options = kEmptyObject) {
|
|
178
|
+
const _this = Object.create(WASI.prototype);
|
|
179
|
+
const { args, env, preopens, stdio, _WASI } = validateOptions.call(_this, options);
|
|
180
|
+
if (options.asyncify !== undefined) {
|
|
181
|
+
(0, util_1.validateObject)(options.asyncify, 'options.asyncify');
|
|
182
|
+
(0, util_1.validateFunction)(options.asyncify.wrapImportFunction, 'options.asyncify.wrapImportFunction');
|
|
183
|
+
}
|
|
184
|
+
const wrap = await _WASI.createAsync(args, env, preopens, stdio, options.fs, options.print, options.printErr, options.asyncify);
|
|
185
|
+
const setMemory = wrap._setMemory;
|
|
186
|
+
delete wrap._setMemory;
|
|
187
|
+
initWASI.call(_this, setMemory, wrap);
|
|
188
|
+
if (options.returnOnExit) {
|
|
189
|
+
wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
|
|
190
|
+
}
|
|
191
|
+
return _this;
|
|
192
|
+
}
|
|
193
|
+
exports.createAsyncWASI = createAsyncWASI;
|
|
169
194
|
//# sourceMappingURL=index.js.map
|