@tybys/wasm-util 0.1.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 +190 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/dist/wasm-util.d.ts +113 -0
- package/dist/wasm-util.esm-bundler.js +1879 -0
- package/dist/wasm-util.esm.js +1879 -0
- package/dist/wasm-util.esm.min.js +1 -0
- package/dist/wasm-util.js +1894 -0
- package/dist/wasm-util.min.js +1 -0
- package/lib/cjs/asyncify.js +173 -0
- package/lib/cjs/index.js +11 -0
- package/lib/cjs/load.js +78 -0
- package/lib/cjs/memfs.js +2689 -0
- package/lib/cjs/memory.js +31 -0
- package/lib/cjs/wasi/error.js +103 -0
- package/lib/cjs/wasi/fd.js +226 -0
- package/lib/cjs/wasi/index.js +137 -0
- package/lib/cjs/wasi/path.js +174 -0
- package/lib/cjs/wasi/preview1.js +889 -0
- package/lib/cjs/wasi/rights.js +139 -0
- package/lib/cjs/wasi/types.js +179 -0
- package/lib/cjs/wasi/util.js +56 -0
- package/lib/mjs/asyncify.mjs +168 -0
- package/lib/mjs/index.mjs +7 -0
- package/lib/mjs/load.mjs +72 -0
- package/lib/mjs/memfs.mjs +2688 -0
- package/lib/mjs/memory.mjs +25 -0
- package/lib/mjs/wasi/error.mjs +97 -0
- package/lib/mjs/wasi/fd.mjs +216 -0
- package/lib/mjs/wasi/index.mjs +132 -0
- package/lib/mjs/wasi/path.mjs +168 -0
- package/lib/mjs/wasi/preview1.mjs +884 -0
- package/lib/mjs/wasi/rights.mjs +134 -0
- package/lib/mjs/wasi/types.mjs +175 -0
- package/lib/mjs/wasi/util.mjs +44 -0
- package/package.json +57 -0
|
@@ -0,0 +1,1894 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.wasmUtil = {}));
|
|
5
|
+
})(this, (function (exports) { 'use strict';
|
|
6
|
+
|
|
7
|
+
function validateObject(value, name) {
|
|
8
|
+
if (value === null || typeof value !== 'object') {
|
|
9
|
+
throw new TypeError(`${name} must be an object. Received ${value === null ? 'null' : typeof value}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function validateArray(value, name) {
|
|
13
|
+
if (!Array.isArray(value)) {
|
|
14
|
+
throw new TypeError(`${name} must be an array. Received ${value === null ? 'null' : typeof value}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function validateBoolean(value, name) {
|
|
18
|
+
if (typeof value !== 'boolean') {
|
|
19
|
+
throw new TypeError(`${name} must be a boolean. Received ${value === null ? 'null' : typeof value}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function validateString(value, name) {
|
|
23
|
+
if (typeof value !== 'string') {
|
|
24
|
+
throw new TypeError(`${name} must be a string. Received ${value === null ? 'null' : typeof value}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function validateFunction(value, name) {
|
|
28
|
+
if (typeof value !== 'function') {
|
|
29
|
+
throw new TypeError(`${name} must be a function. Received ${value === null ? 'null' : typeof value}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function validateUndefined(value, name) {
|
|
33
|
+
if (value !== undefined) {
|
|
34
|
+
throw new TypeError(`${name} must be undefined. Received ${value === null ? 'null' : typeof value}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function isPromiseLike(obj) {
|
|
38
|
+
return !!(obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const ignoreNames = [
|
|
42
|
+
'asyncify_get_state',
|
|
43
|
+
'asyncify_start_rewind',
|
|
44
|
+
'asyncify_start_unwind',
|
|
45
|
+
'asyncify_stop_rewind',
|
|
46
|
+
'asyncify_stop_unwind'
|
|
47
|
+
];
|
|
48
|
+
function tryAllocate(instance, wasm64, size, mallocName) {
|
|
49
|
+
if (typeof instance.exports[mallocName] !== 'function' || size <= 0) {
|
|
50
|
+
return {
|
|
51
|
+
wasm64,
|
|
52
|
+
dataPtr: 16,
|
|
53
|
+
start: wasm64 ? 32 : 24,
|
|
54
|
+
end: 1024
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const malloc = instance.exports[mallocName];
|
|
58
|
+
const dataPtr = wasm64 ? Number(malloc(BigInt(16) + BigInt(size))) : malloc(8 + size);
|
|
59
|
+
if (dataPtr === 0) {
|
|
60
|
+
throw new Error('Allocate asyncify data failed');
|
|
61
|
+
}
|
|
62
|
+
return wasm64
|
|
63
|
+
? { wasm64, dataPtr, start: dataPtr + 16, end: dataPtr + 16 + size }
|
|
64
|
+
: { wasm64, dataPtr, start: dataPtr + 8, end: dataPtr + 8 + size };
|
|
65
|
+
}
|
|
66
|
+
/** @public */
|
|
67
|
+
class Asyncify {
|
|
68
|
+
constructor() {
|
|
69
|
+
this.value = undefined;
|
|
70
|
+
this.exports = undefined;
|
|
71
|
+
this.dataPtr = 0;
|
|
72
|
+
}
|
|
73
|
+
init(memory, instance, options) {
|
|
74
|
+
var _a, _b;
|
|
75
|
+
if (this.exports) {
|
|
76
|
+
throw new Error('Asyncify has been initialized');
|
|
77
|
+
}
|
|
78
|
+
if (!(memory instanceof WebAssembly.Memory)) {
|
|
79
|
+
throw new TypeError('Require WebAssembly.Memory object');
|
|
80
|
+
}
|
|
81
|
+
const exports = instance.exports;
|
|
82
|
+
for (let i = 0; i < ignoreNames.length; ++i) {
|
|
83
|
+
if (typeof exports[ignoreNames[i]] !== 'function') {
|
|
84
|
+
throw new TypeError('Invalid asyncify wasm');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
let address;
|
|
88
|
+
const wasm64 = Boolean(options.wasm64);
|
|
89
|
+
if (!options.tryAllocate) {
|
|
90
|
+
address = {
|
|
91
|
+
wasm64,
|
|
92
|
+
dataPtr: 16,
|
|
93
|
+
start: wasm64 ? 32 : 24,
|
|
94
|
+
end: 1024
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
if (options.tryAllocate === true) {
|
|
99
|
+
address = tryAllocate(instance, wasm64, 4096, 'malloc');
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
address = tryAllocate(instance, wasm64, (_a = options.tryAllocate.size) !== null && _a !== void 0 ? _a : 4096, (_b = options.tryAllocate.name) !== null && _b !== void 0 ? _b : 'malloc');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
this.dataPtr = address.dataPtr;
|
|
106
|
+
if (wasm64) {
|
|
107
|
+
new BigInt64Array(memory.buffer, this.dataPtr).set([BigInt(address.start), BigInt(address.end)]);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
new Int32Array(memory.buffer, this.dataPtr).set([address.start, address.end]);
|
|
111
|
+
}
|
|
112
|
+
this.exports = this.wrapExports(exports, options.wrapExports);
|
|
113
|
+
const asyncifiedInstance = Object.create(WebAssembly.Instance.prototype);
|
|
114
|
+
Object.defineProperty(asyncifiedInstance, 'exports', { value: this.exports });
|
|
115
|
+
// Object.setPrototypeOf(instance, Instance.prototype)
|
|
116
|
+
return asyncifiedInstance;
|
|
117
|
+
}
|
|
118
|
+
assertState() {
|
|
119
|
+
if (this.exports.asyncify_get_state() !== 0 /* AsyncifyState.NONE */) {
|
|
120
|
+
throw new Error('Asyncify state error');
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
wrapImportFunction(f) {
|
|
124
|
+
return ((...args) => {
|
|
125
|
+
// eslint-disable-next-line no-unreachable-loop
|
|
126
|
+
while (this.exports.asyncify_get_state() === 2 /* AsyncifyState.REWINDING */) {
|
|
127
|
+
this.exports.asyncify_stop_rewind();
|
|
128
|
+
return this.value;
|
|
129
|
+
}
|
|
130
|
+
this.assertState();
|
|
131
|
+
const v = f(...args);
|
|
132
|
+
if (!isPromiseLike(v))
|
|
133
|
+
return v;
|
|
134
|
+
this.exports.asyncify_start_unwind(this.dataPtr);
|
|
135
|
+
this.value = v;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
wrapImports(imports) {
|
|
139
|
+
const importObject = {};
|
|
140
|
+
Object.keys(imports).forEach(k => {
|
|
141
|
+
const mod = imports[k];
|
|
142
|
+
const newModule = {};
|
|
143
|
+
Object.keys(mod).forEach(name => {
|
|
144
|
+
const importValue = mod[name];
|
|
145
|
+
if (typeof importValue === 'function') {
|
|
146
|
+
newModule[name] = this.wrapImportFunction(importValue);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
newModule[name] = importValue;
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
importObject[k] = newModule;
|
|
153
|
+
});
|
|
154
|
+
return importObject;
|
|
155
|
+
}
|
|
156
|
+
wrapExportFunction(f) {
|
|
157
|
+
return (async (...args) => {
|
|
158
|
+
this.assertState();
|
|
159
|
+
let ret = f(...args);
|
|
160
|
+
while (this.exports.asyncify_get_state() === 1 /* AsyncifyState.UNWINDING */) {
|
|
161
|
+
this.exports.asyncify_stop_unwind();
|
|
162
|
+
this.value = await this.value;
|
|
163
|
+
this.assertState();
|
|
164
|
+
this.exports.asyncify_start_rewind(this.dataPtr);
|
|
165
|
+
ret = f();
|
|
166
|
+
}
|
|
167
|
+
this.assertState();
|
|
168
|
+
return ret;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
wrapExports(exports, needWrap) {
|
|
172
|
+
const newExports = Object.create(null);
|
|
173
|
+
Object.keys(exports).forEach(name => {
|
|
174
|
+
const exportValue = exports[name];
|
|
175
|
+
let ignore = ignoreNames.indexOf(name) !== -1 || typeof exportValue !== 'function';
|
|
176
|
+
if (Array.isArray(needWrap)) {
|
|
177
|
+
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
178
|
+
}
|
|
179
|
+
Object.defineProperty(newExports, name, {
|
|
180
|
+
enumerable: true,
|
|
181
|
+
value: ignore ? exportValue : this.wrapExportFunction(exportValue)
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
// wrappedExports.set(exports, newExports)
|
|
185
|
+
return newExports;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// /** @public */
|
|
189
|
+
// export class Instance extends WebAssembly.Instance {
|
|
190
|
+
// constructor (options: AsyncifyOptions, module: WebAssembly.Module, importObject?: WebAssembly.Imports) {
|
|
191
|
+
// importObject = importObject ?? {}
|
|
192
|
+
// const asyncify = new Asyncify()
|
|
193
|
+
// super(module, asyncify.wrapImports(importObject))
|
|
194
|
+
// asyncify.init(importObject, this, options)
|
|
195
|
+
// }
|
|
196
|
+
// get exports (): WebAssembly.Exports {
|
|
197
|
+
// return wrappedExports.get(super.exports)!
|
|
198
|
+
// }
|
|
199
|
+
// }
|
|
200
|
+
// Object.defineProperty(Instance.prototype, 'exports', { enumerable: true })
|
|
201
|
+
|
|
202
|
+
async function fetchWasm(urlOrBuffer, imports) {
|
|
203
|
+
const response = await fetch(urlOrBuffer);
|
|
204
|
+
const buffer = await response.arrayBuffer();
|
|
205
|
+
const source = await WebAssembly.instantiate(buffer, imports);
|
|
206
|
+
return source;
|
|
207
|
+
}
|
|
208
|
+
/** @public */
|
|
209
|
+
async function load(urlOrBuffer, imports, asyncify) {
|
|
210
|
+
var _a, _b;
|
|
211
|
+
if (imports && typeof imports !== 'object') {
|
|
212
|
+
throw new TypeError('imports must be an object or undefined');
|
|
213
|
+
}
|
|
214
|
+
imports = imports !== null && imports !== void 0 ? imports : {};
|
|
215
|
+
let asyncifyHelper;
|
|
216
|
+
let source;
|
|
217
|
+
if (asyncify) {
|
|
218
|
+
asyncifyHelper = new Asyncify();
|
|
219
|
+
imports = asyncifyHelper.wrapImports(imports);
|
|
220
|
+
}
|
|
221
|
+
if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
|
|
222
|
+
source = await WebAssembly.instantiate(urlOrBuffer, imports);
|
|
223
|
+
if (asyncify) {
|
|
224
|
+
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
|
225
|
+
return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
|
|
226
|
+
}
|
|
227
|
+
return source;
|
|
228
|
+
}
|
|
229
|
+
if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
|
|
230
|
+
throw new TypeError('Invalid source');
|
|
231
|
+
}
|
|
232
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
233
|
+
try {
|
|
234
|
+
source = await WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
|
|
235
|
+
}
|
|
236
|
+
catch (_) {
|
|
237
|
+
source = await fetchWasm(urlOrBuffer, imports);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
source = await fetchWasm(urlOrBuffer, imports);
|
|
242
|
+
}
|
|
243
|
+
if (asyncify) {
|
|
244
|
+
const memory = source.instance.exports.memory || ((_b = imports.env) === null || _b === void 0 ? void 0 : _b.memory);
|
|
245
|
+
return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
|
|
246
|
+
}
|
|
247
|
+
return source;
|
|
248
|
+
}
|
|
249
|
+
/** @public */
|
|
250
|
+
function loadSync(buffer, imports, asyncify) {
|
|
251
|
+
var _a;
|
|
252
|
+
if ((buffer instanceof ArrayBuffer) && !ArrayBuffer.isView(buffer)) {
|
|
253
|
+
throw new TypeError('Invalid source');
|
|
254
|
+
}
|
|
255
|
+
if (imports && typeof imports !== 'object') {
|
|
256
|
+
throw new TypeError('imports must be an object or undefined');
|
|
257
|
+
}
|
|
258
|
+
imports = imports !== null && imports !== void 0 ? imports : {};
|
|
259
|
+
let asyncifyHelper;
|
|
260
|
+
if (asyncify) {
|
|
261
|
+
asyncifyHelper = new Asyncify();
|
|
262
|
+
imports = asyncifyHelper.wrapImports(imports);
|
|
263
|
+
}
|
|
264
|
+
const module = new WebAssembly.Module(buffer);
|
|
265
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
266
|
+
const source = { instance, module };
|
|
267
|
+
if (asyncify) {
|
|
268
|
+
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
|
269
|
+
return { module: source.module, instance: asyncifyHelper.init(memory, instance, asyncify) };
|
|
270
|
+
}
|
|
271
|
+
return source;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const CHAR_DOT = 46; /* . */
|
|
275
|
+
const CHAR_FORWARD_SLASH = 47; /* / */
|
|
276
|
+
function isPosixPathSeparator(code) {
|
|
277
|
+
return code === CHAR_FORWARD_SLASH;
|
|
278
|
+
}
|
|
279
|
+
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
|
|
280
|
+
let res = '';
|
|
281
|
+
let lastSegmentLength = 0;
|
|
282
|
+
let lastSlash = -1;
|
|
283
|
+
let dots = 0;
|
|
284
|
+
let code = 0;
|
|
285
|
+
for (let i = 0; i <= path.length; ++i) {
|
|
286
|
+
if (i < path.length) {
|
|
287
|
+
code = path.charCodeAt(i);
|
|
288
|
+
}
|
|
289
|
+
else if (isPathSeparator(code)) {
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
code = CHAR_FORWARD_SLASH;
|
|
294
|
+
}
|
|
295
|
+
if (isPathSeparator(code)) {
|
|
296
|
+
if (lastSlash === i - 1 || dots === 1) ;
|
|
297
|
+
else if (dots === 2) {
|
|
298
|
+
if (res.length < 2 || lastSegmentLength !== 2 ||
|
|
299
|
+
res.charCodeAt(res.length - 1) !== CHAR_DOT ||
|
|
300
|
+
res.charCodeAt(res.length - 2) !== CHAR_DOT) {
|
|
301
|
+
if (res.length > 2) {
|
|
302
|
+
const lastSlashIndex = res.indexOf(separator);
|
|
303
|
+
if (lastSlashIndex === -1) {
|
|
304
|
+
res = '';
|
|
305
|
+
lastSegmentLength = 0;
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
res = res.slice(0, lastSlashIndex);
|
|
309
|
+
lastSegmentLength =
|
|
310
|
+
res.length - 1 - res.indexOf(separator);
|
|
311
|
+
}
|
|
312
|
+
lastSlash = i;
|
|
313
|
+
dots = 0;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
else if (res.length !== 0) {
|
|
317
|
+
res = '';
|
|
318
|
+
lastSegmentLength = 0;
|
|
319
|
+
lastSlash = i;
|
|
320
|
+
dots = 0;
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (allowAboveRoot) {
|
|
325
|
+
res += res.length > 0 ? `${separator}..` : '..';
|
|
326
|
+
lastSegmentLength = 2;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
if (res.length > 0) {
|
|
331
|
+
res += `${separator}${path.slice(lastSlash + 1, i)}`;
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
res = path.slice(lastSlash + 1, i);
|
|
335
|
+
}
|
|
336
|
+
lastSegmentLength = i - lastSlash - 1;
|
|
337
|
+
}
|
|
338
|
+
lastSlash = i;
|
|
339
|
+
dots = 0;
|
|
340
|
+
}
|
|
341
|
+
else if (code === CHAR_DOT && dots !== -1) {
|
|
342
|
+
++dots;
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
dots = -1;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return res;
|
|
349
|
+
}
|
|
350
|
+
function resolve(...args) {
|
|
351
|
+
let resolvedPath = '';
|
|
352
|
+
let resolvedAbsolute = false;
|
|
353
|
+
for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
|
|
354
|
+
const path = i >= 0 ? args[i] : '/';
|
|
355
|
+
validateString(path, 'path');
|
|
356
|
+
// Skip empty entries
|
|
357
|
+
if (path.length === 0) {
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
resolvedPath = `${path}/${resolvedPath}`;
|
|
361
|
+
resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
|
362
|
+
}
|
|
363
|
+
// At this point the path should be resolved to a full absolute path, but
|
|
364
|
+
// handle relative paths to be safe (might happen when process.cwd() fails)
|
|
365
|
+
// Normalize the path
|
|
366
|
+
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/', isPosixPathSeparator);
|
|
367
|
+
if (resolvedAbsolute) {
|
|
368
|
+
return `/${resolvedPath}`;
|
|
369
|
+
}
|
|
370
|
+
return resolvedPath.length > 0 ? resolvedPath : '.';
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const WasiRights = {
|
|
374
|
+
FD_DATASYNC: (BigInt(1) << BigInt(0)),
|
|
375
|
+
FD_READ: (BigInt(1) << BigInt(1)),
|
|
376
|
+
FD_SEEK: (BigInt(1) << BigInt(2)),
|
|
377
|
+
FD_FDSTAT_SET_FLAGS: (BigInt(1) << BigInt(3)),
|
|
378
|
+
FD_SYNC: (BigInt(1) << BigInt(4)),
|
|
379
|
+
FD_TELL: (BigInt(1) << BigInt(5)),
|
|
380
|
+
FD_WRITE: (BigInt(1) << BigInt(6)),
|
|
381
|
+
FD_ADVISE: (BigInt(1) << BigInt(7)),
|
|
382
|
+
FD_ALLOCATE: (BigInt(1) << BigInt(8)),
|
|
383
|
+
PATH_CREATE_DIRECTORY: (BigInt(1) << BigInt(9)),
|
|
384
|
+
PATH_CREATE_FILE: (BigInt(1) << BigInt(10)),
|
|
385
|
+
PATH_LINK_SOURCE: (BigInt(1) << BigInt(11)),
|
|
386
|
+
PATH_LINK_TARGET: (BigInt(1) << BigInt(12)),
|
|
387
|
+
PATH_OPEN: (BigInt(1) << BigInt(13)),
|
|
388
|
+
FD_READDIR: (BigInt(1) << BigInt(14)),
|
|
389
|
+
PATH_READLINK: (BigInt(1) << BigInt(15)),
|
|
390
|
+
PATH_RENAME_SOURCE: (BigInt(1) << BigInt(16)),
|
|
391
|
+
PATH_RENAME_TARGET: (BigInt(1) << BigInt(17)),
|
|
392
|
+
PATH_FILESTAT_GET: (BigInt(1) << BigInt(18)),
|
|
393
|
+
PATH_FILESTAT_SET_SIZE: (BigInt(1) << BigInt(19)),
|
|
394
|
+
PATH_FILESTAT_SET_TIMES: (BigInt(1) << BigInt(20)),
|
|
395
|
+
FD_FILESTAT_GET: (BigInt(1) << BigInt(21)),
|
|
396
|
+
FD_FILESTAT_SET_SIZE: (BigInt(1) << BigInt(22)),
|
|
397
|
+
FD_FILESTAT_SET_TIMES: (BigInt(1) << BigInt(23)),
|
|
398
|
+
PATH_SYMLINK: (BigInt(1) << BigInt(24)),
|
|
399
|
+
PATH_REMOVE_DIRECTORY: (BigInt(1) << BigInt(25)),
|
|
400
|
+
PATH_UNLINK_FILE: (BigInt(1) << BigInt(26)),
|
|
401
|
+
POLL_FD_READWRITE: (BigInt(1) << BigInt(27)),
|
|
402
|
+
SOCK_SHUTDOWN: (BigInt(1) << BigInt(28)),
|
|
403
|
+
SOCK_ACCEPT: (BigInt(1) << BigInt(29))
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
function strerror(errno) {
|
|
407
|
+
switch (errno) {
|
|
408
|
+
case 0 /* WasiErrno.ESUCCESS */: return 'Success';
|
|
409
|
+
case 1 /* WasiErrno.E2BIG */: return 'Argument list too long';
|
|
410
|
+
case 2 /* WasiErrno.EACCES */: return 'Permission denied';
|
|
411
|
+
case 3 /* WasiErrno.EADDRINUSE */: return 'Address in use';
|
|
412
|
+
case 4 /* WasiErrno.EADDRNOTAVAIL */: return 'Address not available';
|
|
413
|
+
case 5 /* WasiErrno.EAFNOSUPPORT */: return 'Address family not supported by protocol';
|
|
414
|
+
case 6 /* WasiErrno.EAGAIN */: return 'Resource temporarily unavailable';
|
|
415
|
+
case 7 /* WasiErrno.EALREADY */: return 'Operation already in progress';
|
|
416
|
+
case 8 /* WasiErrno.EBADF */: return 'Bad file descriptor';
|
|
417
|
+
case 9 /* WasiErrno.EBADMSG */: return 'Bad message';
|
|
418
|
+
case 10 /* WasiErrno.EBUSY */: return 'Resource busy';
|
|
419
|
+
case 11 /* WasiErrno.ECANCELED */: return 'Operation canceled';
|
|
420
|
+
case 12 /* WasiErrno.ECHILD */: return 'No child process';
|
|
421
|
+
case 13 /* WasiErrno.ECONNABORTED */: return 'Connection aborted';
|
|
422
|
+
case 14 /* WasiErrno.ECONNREFUSED */: return 'Connection refused';
|
|
423
|
+
case 15 /* WasiErrno.ECONNRESET */: return 'Connection reset by peer';
|
|
424
|
+
case 16 /* WasiErrno.EDEADLK */: return 'Resource deadlock would occur';
|
|
425
|
+
case 17 /* WasiErrno.EDESTADDRREQ */: return 'Destination address required';
|
|
426
|
+
case 18 /* WasiErrno.EDOM */: return 'Domain error';
|
|
427
|
+
case 19 /* WasiErrno.EDQUOT */: return 'Quota exceeded';
|
|
428
|
+
case 20 /* WasiErrno.EEXIST */: return 'File exists';
|
|
429
|
+
case 21 /* WasiErrno.EFAULT */: return 'Bad address';
|
|
430
|
+
case 22 /* WasiErrno.EFBIG */: return 'File too large';
|
|
431
|
+
case 23 /* WasiErrno.EHOSTUNREACH */: return 'Host is unreachable';
|
|
432
|
+
case 24 /* WasiErrno.EIDRM */: return 'Identifier removed';
|
|
433
|
+
case 25 /* WasiErrno.EILSEQ */: return 'Illegal byte sequence';
|
|
434
|
+
case 26 /* WasiErrno.EINPROGRESS */: return 'Operation in progress';
|
|
435
|
+
case 27 /* WasiErrno.EINTR */: return 'Interrupted system call';
|
|
436
|
+
case 28 /* WasiErrno.EINVAL */: return 'Invalid argument';
|
|
437
|
+
case 29 /* WasiErrno.EIO */: return 'I/O error';
|
|
438
|
+
case 30 /* WasiErrno.EISCONN */: return 'Socket is connected';
|
|
439
|
+
case 31 /* WasiErrno.EISDIR */: return 'Is a directory';
|
|
440
|
+
case 32 /* WasiErrno.ELOOP */: return 'Symbolic link loop';
|
|
441
|
+
case 33 /* WasiErrno.EMFILE */: return 'No file descriptors available';
|
|
442
|
+
case 34 /* WasiErrno.EMLINK */: return 'Too many links';
|
|
443
|
+
case 35 /* WasiErrno.EMSGSIZE */: return 'Message too large';
|
|
444
|
+
case 36 /* WasiErrno.EMULTIHOP */: return 'Multihop attempted';
|
|
445
|
+
case 37 /* WasiErrno.ENAMETOOLONG */: return 'Filename too long';
|
|
446
|
+
case 38 /* WasiErrno.ENETDOWN */: return 'Network is down';
|
|
447
|
+
case 39 /* WasiErrno.ENETRESET */: return 'Connection reset by network';
|
|
448
|
+
case 40 /* WasiErrno.ENETUNREACH */: return 'Network unreachable';
|
|
449
|
+
case 41 /* WasiErrno.ENFILE */: return 'Too many files open in system';
|
|
450
|
+
case 42 /* WasiErrno.ENOBUFS */: return 'No buffer space available';
|
|
451
|
+
case 43 /* WasiErrno.ENODEV */: return 'No such device';
|
|
452
|
+
case 44 /* WasiErrno.ENOENT */: return 'No such file or directory';
|
|
453
|
+
case 45 /* WasiErrno.ENOEXEC */: return 'Exec format error';
|
|
454
|
+
case 46 /* WasiErrno.ENOLCK */: return 'No locks available';
|
|
455
|
+
case 47 /* WasiErrno.ENOLINK */: return 'Link has been severed';
|
|
456
|
+
case 48 /* WasiErrno.ENOMEM */: return 'Out of memory';
|
|
457
|
+
case 49 /* WasiErrno.ENOMSG */: return 'No message of the desired type';
|
|
458
|
+
case 50 /* WasiErrno.ENOPROTOOPT */: return 'Protocol not available';
|
|
459
|
+
case 51 /* WasiErrno.ENOSPC */: return 'No space left on device';
|
|
460
|
+
case 52 /* WasiErrno.ENOSYS */: return 'Function not implemented';
|
|
461
|
+
case 53 /* WasiErrno.ENOTCONN */: return 'Socket not connected';
|
|
462
|
+
case 54 /* WasiErrno.ENOTDIR */: return 'Not a directory';
|
|
463
|
+
case 55 /* WasiErrno.ENOTEMPTY */: return 'Directory not empty';
|
|
464
|
+
case 56 /* WasiErrno.ENOTRECOVERABLE */: return 'State not recoverable';
|
|
465
|
+
case 57 /* WasiErrno.ENOTSOCK */: return 'Not a socket';
|
|
466
|
+
case 58 /* WasiErrno.ENOTSUP */: return 'Not supported';
|
|
467
|
+
case 59 /* WasiErrno.ENOTTY */: return 'Not a tty';
|
|
468
|
+
case 60 /* WasiErrno.ENXIO */: return 'No such device or address';
|
|
469
|
+
case 61 /* WasiErrno.EOVERFLOW */: return 'Value too large for data type';
|
|
470
|
+
case 62 /* WasiErrno.EOWNERDEAD */: return 'Previous owner died';
|
|
471
|
+
case 63 /* WasiErrno.EPERM */: return 'Operation not permitted';
|
|
472
|
+
case 64 /* WasiErrno.EPIPE */: return 'Broken pipe';
|
|
473
|
+
case 65 /* WasiErrno.EPROTO */: return 'Protocol error';
|
|
474
|
+
case 66 /* WasiErrno.EPROTONOSUPPORT */: return 'Protocol not supported';
|
|
475
|
+
case 67 /* WasiErrno.EPROTOTYPE */: return 'Protocol wrong type for socket';
|
|
476
|
+
case 68 /* WasiErrno.ERANGE */: return 'Result not representable';
|
|
477
|
+
case 69 /* WasiErrno.EROFS */: return 'Read-only file system';
|
|
478
|
+
case 70 /* WasiErrno.ESPIPE */: return 'Invalid seek';
|
|
479
|
+
case 71 /* WasiErrno.ESRCH */: return 'No such process';
|
|
480
|
+
case 72 /* WasiErrno.ESTALE */: return 'Stale file handle';
|
|
481
|
+
case 73 /* WasiErrno.ETIMEDOUT */: return 'Operation timed out';
|
|
482
|
+
case 74 /* WasiErrno.ETXTBSY */: return 'Text file busy';
|
|
483
|
+
case 75 /* WasiErrno.EXDEV */: return 'Cross-device link';
|
|
484
|
+
case 76 /* WasiErrno.ENOTCAPABLE */: return 'Capabilities insufficient';
|
|
485
|
+
default: return 'Unknown error';
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
class WasiError extends Error {
|
|
489
|
+
constructor(message, errno) {
|
|
490
|
+
super(message);
|
|
491
|
+
this.errno = errno;
|
|
492
|
+
}
|
|
493
|
+
getErrorMessage() {
|
|
494
|
+
return strerror(this.errno);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
Object.defineProperty(WasiError.prototype, 'name', {
|
|
498
|
+
configurable: true,
|
|
499
|
+
writable: true,
|
|
500
|
+
value: 'WasiError'
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
const RIGHTS_ALL = WasiRights.FD_DATASYNC |
|
|
504
|
+
WasiRights.FD_READ |
|
|
505
|
+
WasiRights.FD_SEEK |
|
|
506
|
+
WasiRights.FD_FDSTAT_SET_FLAGS |
|
|
507
|
+
WasiRights.FD_SYNC |
|
|
508
|
+
WasiRights.FD_TELL |
|
|
509
|
+
WasiRights.FD_WRITE |
|
|
510
|
+
WasiRights.FD_ADVISE |
|
|
511
|
+
WasiRights.FD_ALLOCATE |
|
|
512
|
+
WasiRights.PATH_CREATE_DIRECTORY |
|
|
513
|
+
WasiRights.PATH_CREATE_FILE |
|
|
514
|
+
WasiRights.PATH_LINK_SOURCE |
|
|
515
|
+
WasiRights.PATH_LINK_TARGET |
|
|
516
|
+
WasiRights.PATH_OPEN |
|
|
517
|
+
WasiRights.FD_READDIR |
|
|
518
|
+
WasiRights.PATH_READLINK |
|
|
519
|
+
WasiRights.PATH_RENAME_SOURCE |
|
|
520
|
+
WasiRights.PATH_RENAME_TARGET |
|
|
521
|
+
WasiRights.PATH_FILESTAT_GET |
|
|
522
|
+
WasiRights.PATH_FILESTAT_SET_SIZE |
|
|
523
|
+
WasiRights.PATH_FILESTAT_SET_TIMES |
|
|
524
|
+
WasiRights.FD_FILESTAT_GET |
|
|
525
|
+
WasiRights.FD_FILESTAT_SET_TIMES |
|
|
526
|
+
WasiRights.FD_FILESTAT_SET_SIZE |
|
|
527
|
+
WasiRights.PATH_SYMLINK |
|
|
528
|
+
WasiRights.PATH_UNLINK_FILE |
|
|
529
|
+
WasiRights.PATH_REMOVE_DIRECTORY |
|
|
530
|
+
WasiRights.POLL_FD_READWRITE |
|
|
531
|
+
WasiRights.SOCK_SHUTDOWN;
|
|
532
|
+
const BLOCK_DEVICE_BASE = RIGHTS_ALL;
|
|
533
|
+
const BLOCK_DEVICE_INHERITING = RIGHTS_ALL;
|
|
534
|
+
const CHARACTER_DEVICE_BASE = RIGHTS_ALL;
|
|
535
|
+
const CHARACTER_DEVICE_INHERITING = RIGHTS_ALL;
|
|
536
|
+
const REGULAR_FILE_BASE = WasiRights.FD_DATASYNC |
|
|
537
|
+
WasiRights.FD_READ |
|
|
538
|
+
WasiRights.FD_SEEK |
|
|
539
|
+
WasiRights.FD_FDSTAT_SET_FLAGS |
|
|
540
|
+
WasiRights.FD_SYNC |
|
|
541
|
+
WasiRights.FD_TELL |
|
|
542
|
+
WasiRights.FD_WRITE |
|
|
543
|
+
WasiRights.FD_ADVISE |
|
|
544
|
+
WasiRights.FD_ALLOCATE |
|
|
545
|
+
WasiRights.FD_FILESTAT_GET |
|
|
546
|
+
WasiRights.FD_FILESTAT_SET_SIZE |
|
|
547
|
+
WasiRights.FD_FILESTAT_SET_TIMES |
|
|
548
|
+
WasiRights.POLL_FD_READWRITE;
|
|
549
|
+
const REGULAR_FILE_INHERITING = BigInt(0);
|
|
550
|
+
const DIRECTORY_BASE = WasiRights.FD_FDSTAT_SET_FLAGS |
|
|
551
|
+
WasiRights.FD_SYNC |
|
|
552
|
+
WasiRights.FD_ADVISE |
|
|
553
|
+
WasiRights.PATH_CREATE_DIRECTORY |
|
|
554
|
+
WasiRights.PATH_CREATE_FILE |
|
|
555
|
+
WasiRights.PATH_LINK_SOURCE |
|
|
556
|
+
WasiRights.PATH_LINK_TARGET |
|
|
557
|
+
WasiRights.PATH_OPEN |
|
|
558
|
+
WasiRights.FD_READDIR |
|
|
559
|
+
WasiRights.PATH_READLINK |
|
|
560
|
+
WasiRights.PATH_RENAME_SOURCE |
|
|
561
|
+
WasiRights.PATH_RENAME_TARGET |
|
|
562
|
+
WasiRights.PATH_FILESTAT_GET |
|
|
563
|
+
WasiRights.PATH_FILESTAT_SET_SIZE |
|
|
564
|
+
WasiRights.PATH_FILESTAT_SET_TIMES |
|
|
565
|
+
WasiRights.FD_FILESTAT_GET |
|
|
566
|
+
WasiRights.FD_FILESTAT_SET_TIMES |
|
|
567
|
+
WasiRights.PATH_SYMLINK |
|
|
568
|
+
WasiRights.PATH_UNLINK_FILE |
|
|
569
|
+
WasiRights.PATH_REMOVE_DIRECTORY |
|
|
570
|
+
WasiRights.POLL_FD_READWRITE;
|
|
571
|
+
const DIRECTORY_INHERITING = DIRECTORY_BASE | REGULAR_FILE_BASE;
|
|
572
|
+
const SOCKET_BASE = (WasiRights.FD_READ |
|
|
573
|
+
WasiRights.FD_FDSTAT_SET_FLAGS |
|
|
574
|
+
WasiRights.FD_WRITE |
|
|
575
|
+
WasiRights.FD_FILESTAT_GET |
|
|
576
|
+
WasiRights.POLL_FD_READWRITE |
|
|
577
|
+
WasiRights.SOCK_SHUTDOWN);
|
|
578
|
+
const SOCKET_INHERITING = RIGHTS_ALL;
|
|
579
|
+
const TTY_BASE = WasiRights.FD_READ |
|
|
580
|
+
WasiRights.FD_FDSTAT_SET_FLAGS |
|
|
581
|
+
WasiRights.FD_WRITE |
|
|
582
|
+
WasiRights.FD_FILESTAT_GET |
|
|
583
|
+
WasiRights.POLL_FD_READWRITE;
|
|
584
|
+
const TTY_INHERITING = BigInt(0);
|
|
585
|
+
function getRights(stdio, fd, flags, type) {
|
|
586
|
+
const ret = {
|
|
587
|
+
base: BigInt(0),
|
|
588
|
+
inheriting: BigInt(0)
|
|
589
|
+
};
|
|
590
|
+
if (type === 0 /* WasiFileType.UNKNOWN */) {
|
|
591
|
+
throw new WasiError('Unknown file type', 28 /* WasiErrno.EINVAL */);
|
|
592
|
+
}
|
|
593
|
+
switch (type) {
|
|
594
|
+
case 4 /* WasiFileType.REGULAR_FILE */:
|
|
595
|
+
ret.base = REGULAR_FILE_BASE;
|
|
596
|
+
ret.inheriting = REGULAR_FILE_INHERITING;
|
|
597
|
+
break;
|
|
598
|
+
case 3 /* WasiFileType.DIRECTORY */:
|
|
599
|
+
ret.base = DIRECTORY_BASE;
|
|
600
|
+
ret.inheriting = DIRECTORY_INHERITING;
|
|
601
|
+
break;
|
|
602
|
+
case 6 /* WasiFileType.SOCKET_STREAM */:
|
|
603
|
+
case 5 /* WasiFileType.SOCKET_DGRAM */:
|
|
604
|
+
ret.base = SOCKET_BASE;
|
|
605
|
+
ret.inheriting = SOCKET_INHERITING;
|
|
606
|
+
break;
|
|
607
|
+
case 2 /* WasiFileType.CHARACTER_DEVICE */:
|
|
608
|
+
if (stdio.indexOf(fd) !== -1) {
|
|
609
|
+
ret.base = TTY_BASE;
|
|
610
|
+
ret.inheriting = TTY_INHERITING;
|
|
611
|
+
}
|
|
612
|
+
else {
|
|
613
|
+
ret.base = CHARACTER_DEVICE_BASE;
|
|
614
|
+
ret.inheriting = CHARACTER_DEVICE_INHERITING;
|
|
615
|
+
}
|
|
616
|
+
break;
|
|
617
|
+
case 1 /* WasiFileType.BLOCK_DEVICE */:
|
|
618
|
+
ret.base = BLOCK_DEVICE_BASE;
|
|
619
|
+
ret.inheriting = BLOCK_DEVICE_INHERITING;
|
|
620
|
+
break;
|
|
621
|
+
default:
|
|
622
|
+
ret.base = BigInt(0);
|
|
623
|
+
ret.inheriting = BigInt(0);
|
|
624
|
+
}
|
|
625
|
+
/* Disable read/write bits depending on access mode. */
|
|
626
|
+
const read_or_write_only = flags & (0 | 1 | 2);
|
|
627
|
+
if (read_or_write_only === 0) {
|
|
628
|
+
ret.base &= ~WasiRights.FD_WRITE;
|
|
629
|
+
}
|
|
630
|
+
else if (read_or_write_only === 1) {
|
|
631
|
+
ret.base &= ~WasiRights.FD_READ;
|
|
632
|
+
}
|
|
633
|
+
return ret;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
function concatBuffer(buffers, size) {
|
|
637
|
+
let total = 0;
|
|
638
|
+
if (typeof size === 'number' && size >= 0) {
|
|
639
|
+
total = size;
|
|
640
|
+
}
|
|
641
|
+
else {
|
|
642
|
+
for (let i = 0; i < buffers.length; i++) {
|
|
643
|
+
const buffer = buffers[i];
|
|
644
|
+
total += buffer.length;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
let pos = 0;
|
|
648
|
+
const ret = new Uint8Array(total);
|
|
649
|
+
for (let i = 0; i < buffers.length; i++) {
|
|
650
|
+
const buffer = buffers[i];
|
|
651
|
+
ret.set(buffer, pos);
|
|
652
|
+
pos += buffer.length;
|
|
653
|
+
}
|
|
654
|
+
return ret;
|
|
655
|
+
}
|
|
656
|
+
class FileDescriptor {
|
|
657
|
+
constructor(id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen) {
|
|
658
|
+
this.id = id;
|
|
659
|
+
this.fd = fd;
|
|
660
|
+
this.path = path;
|
|
661
|
+
this.realPath = realPath;
|
|
662
|
+
this.type = type;
|
|
663
|
+
this.rightsBase = rightsBase;
|
|
664
|
+
this.rightsInheriting = rightsInheriting;
|
|
665
|
+
this.preopen = preopen;
|
|
666
|
+
this.pos = BigInt(0);
|
|
667
|
+
this.size = BigInt(0);
|
|
668
|
+
}
|
|
669
|
+
seek(offset, whence) {
|
|
670
|
+
if (whence === 0 /* WasiWhence.SET */) {
|
|
671
|
+
this.pos = BigInt(offset);
|
|
672
|
+
}
|
|
673
|
+
else if (whence === 1 /* WasiWhence.CUR */) {
|
|
674
|
+
this.pos += BigInt(offset);
|
|
675
|
+
}
|
|
676
|
+
else if (whence === 2 /* WasiWhence.END */) {
|
|
677
|
+
this.pos = BigInt(this.size) - BigInt(offset);
|
|
678
|
+
}
|
|
679
|
+
else {
|
|
680
|
+
throw new WasiError('Unknown whence', 29 /* WasiErrno.EIO */);
|
|
681
|
+
}
|
|
682
|
+
return this.pos;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
class StandardOutput extends FileDescriptor {
|
|
686
|
+
constructor(log, id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen) {
|
|
687
|
+
super(id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen);
|
|
688
|
+
this._log = log;
|
|
689
|
+
this._buf = null;
|
|
690
|
+
}
|
|
691
|
+
write(buffer) {
|
|
692
|
+
const originalBuffer = buffer;
|
|
693
|
+
if (this._buf) {
|
|
694
|
+
buffer = concatBuffer([this._buf, buffer]);
|
|
695
|
+
this._buf = null;
|
|
696
|
+
}
|
|
697
|
+
if (buffer.indexOf(10) === -1) {
|
|
698
|
+
this._buf = buffer;
|
|
699
|
+
return originalBuffer.byteLength;
|
|
700
|
+
}
|
|
701
|
+
let written = 0;
|
|
702
|
+
let lastBegin = 0;
|
|
703
|
+
let index;
|
|
704
|
+
while ((index = buffer.indexOf(10, written)) !== -1) {
|
|
705
|
+
const str = new TextDecoder().decode(buffer.subarray(lastBegin, index));
|
|
706
|
+
this._log(str);
|
|
707
|
+
written += index - lastBegin + 1;
|
|
708
|
+
lastBegin = index + 1;
|
|
709
|
+
}
|
|
710
|
+
if (written < buffer.length) {
|
|
711
|
+
this._buf = buffer.slice(written);
|
|
712
|
+
}
|
|
713
|
+
return originalBuffer.byteLength;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
function toFileType(stat) {
|
|
717
|
+
if (stat.isBlockDevice())
|
|
718
|
+
return 1 /* WasiFileType.BLOCK_DEVICE */;
|
|
719
|
+
if (stat.isCharacterDevice())
|
|
720
|
+
return 2 /* WasiFileType.CHARACTER_DEVICE */;
|
|
721
|
+
if (stat.isDirectory())
|
|
722
|
+
return 3 /* WasiFileType.DIRECTORY */;
|
|
723
|
+
if (stat.isSocket())
|
|
724
|
+
return 6 /* WasiFileType.SOCKET_STREAM */;
|
|
725
|
+
if (stat.isFile())
|
|
726
|
+
return 4 /* WasiFileType.REGULAR_FILE */;
|
|
727
|
+
if (stat.isSymbolicLink())
|
|
728
|
+
return 7 /* WasiFileType.SYMBOLIC_LINK */;
|
|
729
|
+
return 0 /* WasiFileType.UNKNOWN */;
|
|
730
|
+
}
|
|
731
|
+
function toFileStat(view, buf, stat) {
|
|
732
|
+
view.setBigUint64(buf, stat.dev, true);
|
|
733
|
+
view.setBigUint64(buf + 8, stat.ino, true);
|
|
734
|
+
view.setBigUint64(buf + 16, BigInt(toFileType(stat)), true);
|
|
735
|
+
view.setBigUint64(buf + 24, stat.nlink, true);
|
|
736
|
+
view.setBigUint64(buf + 32, stat.size, true);
|
|
737
|
+
view.setBigUint64(buf + 40, stat.atimeMs * BigInt(1000000), true);
|
|
738
|
+
view.setBigUint64(buf + 48, stat.mtimeMs * BigInt(1000000), true);
|
|
739
|
+
view.setBigUint64(buf + 56, stat.ctimeMs * BigInt(1000000), true);
|
|
740
|
+
}
|
|
741
|
+
class FileDescriptorTable {
|
|
742
|
+
constructor(options) {
|
|
743
|
+
this.used = 0;
|
|
744
|
+
this.size = options.size;
|
|
745
|
+
this.fds = Array(options.size);
|
|
746
|
+
this.stdio = [options.in, options.out, options.err];
|
|
747
|
+
this.fs = options.fs;
|
|
748
|
+
this.print = options.print;
|
|
749
|
+
this.printErr = options.printErr;
|
|
750
|
+
this.insertStdio(options.in, 0, '<stdin>');
|
|
751
|
+
this.insertStdio(options.out, 1, '<stdout>');
|
|
752
|
+
this.insertStdio(options.err, 2, '<stderr>');
|
|
753
|
+
}
|
|
754
|
+
insertStdio(fd, expected, name) {
|
|
755
|
+
const type = 2 /* WasiFileType.CHARACTER_DEVICE */;
|
|
756
|
+
const { base, inheriting } = getRights(this.stdio, fd, 2 /* FileControlFlag.O_RDWR */, type);
|
|
757
|
+
const wrap = this.insert(fd, name, name, type, base, inheriting, 0);
|
|
758
|
+
if (wrap.id !== expected) {
|
|
759
|
+
throw new WasiError(`id: ${wrap.id} !== expected: ${expected}`, 8 /* WasiErrno.EBADF */);
|
|
760
|
+
}
|
|
761
|
+
return wrap;
|
|
762
|
+
}
|
|
763
|
+
insert(fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen) {
|
|
764
|
+
var _a, _b;
|
|
765
|
+
let index = -1;
|
|
766
|
+
if (this.used >= this.size) {
|
|
767
|
+
const newSize = this.size * 2;
|
|
768
|
+
this.fds.length = newSize;
|
|
769
|
+
index = this.size;
|
|
770
|
+
this.size = newSize;
|
|
771
|
+
}
|
|
772
|
+
else {
|
|
773
|
+
for (let i = 0; i < this.size; ++i) {
|
|
774
|
+
if (this.fds[i] == null) {
|
|
775
|
+
index = i;
|
|
776
|
+
break;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
let entry;
|
|
781
|
+
if (mappedPath === '<stdout>') {
|
|
782
|
+
entry = new StandardOutput((_a = this.print) !== null && _a !== void 0 ? _a : console.log, index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
|
|
783
|
+
}
|
|
784
|
+
else if (mappedPath === '<stderr>') {
|
|
785
|
+
entry = new StandardOutput((_b = this.printErr) !== null && _b !== void 0 ? _b : console.error, index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
|
|
786
|
+
}
|
|
787
|
+
else {
|
|
788
|
+
entry = new FileDescriptor(index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
|
|
789
|
+
}
|
|
790
|
+
this.fds[index] = entry;
|
|
791
|
+
this.used++;
|
|
792
|
+
return entry;
|
|
793
|
+
}
|
|
794
|
+
getFileTypeByFd(fd) {
|
|
795
|
+
const stat = this.fs.fstatSync(fd);
|
|
796
|
+
return toFileType(stat);
|
|
797
|
+
}
|
|
798
|
+
insertPreopen(fd, mappedPath, realPath) {
|
|
799
|
+
const type = this.getFileTypeByFd(fd);
|
|
800
|
+
if (type !== 3 /* WasiFileType.DIRECTORY */) {
|
|
801
|
+
throw new WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, 54 /* WasiErrno.ENOTDIR */);
|
|
802
|
+
}
|
|
803
|
+
const result = getRights(this.stdio, fd, 0, type);
|
|
804
|
+
return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
|
|
805
|
+
}
|
|
806
|
+
get(id, base, inheriting) {
|
|
807
|
+
if (id >= this.size) {
|
|
808
|
+
throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
|
|
809
|
+
}
|
|
810
|
+
const entry = this.fds[id];
|
|
811
|
+
if (!entry || entry.id !== id) {
|
|
812
|
+
throw new WasiError('Bad file descriptor', 8 /* WasiErrno.EBADF */);
|
|
813
|
+
}
|
|
814
|
+
/* Validate that the fd has the necessary rights. */
|
|
815
|
+
if ((~entry.rightsBase & base) !== BigInt(0) || (~entry.rightsInheriting & inheriting) !== BigInt(0)) {
|
|
816
|
+
throw new WasiError('Capabilities insufficient', 76 /* WasiErrno.ENOTCAPABLE */);
|
|
817
|
+
}
|
|
818
|
+
return entry;
|
|
819
|
+
}
|
|
820
|
+
remove(id) {
|
|
821
|
+
if (id >= this.size) {
|
|
822
|
+
throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
|
|
823
|
+
}
|
|
824
|
+
const entry = this.fds[id];
|
|
825
|
+
if (!entry || entry.id !== id) {
|
|
826
|
+
throw new WasiError('Bad file descriptor', 8 /* WasiErrno.EBADF */);
|
|
827
|
+
}
|
|
828
|
+
this.fds[id] = undefined;
|
|
829
|
+
this.used--;
|
|
830
|
+
}
|
|
831
|
+
renumber(dst, src) {
|
|
832
|
+
if (dst === src)
|
|
833
|
+
return;
|
|
834
|
+
if (dst >= this.size || src >= this.size) {
|
|
835
|
+
throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
|
|
836
|
+
}
|
|
837
|
+
const dstEntry = this.fds[dst];
|
|
838
|
+
const srcEntry = this.fds[src];
|
|
839
|
+
if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
|
|
840
|
+
throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
|
|
841
|
+
}
|
|
842
|
+
this.fs.closeSync(dstEntry.fd);
|
|
843
|
+
this.fds[dst] = this.fds[src];
|
|
844
|
+
this.fds[dst].id = dst;
|
|
845
|
+
this.fds[src] = undefined;
|
|
846
|
+
this.used--;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
/** @public */
|
|
851
|
+
class Memory extends WebAssembly.Memory {
|
|
852
|
+
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
853
|
+
constructor(descriptor) {
|
|
854
|
+
super(descriptor);
|
|
855
|
+
}
|
|
856
|
+
get HEAP8() { return new Int8Array(super.buffer); }
|
|
857
|
+
get HEAPU8() { return new Uint8Array(super.buffer); }
|
|
858
|
+
get HEAP16() { return new Int16Array(super.buffer); }
|
|
859
|
+
get HEAPU16() { return new Uint16Array(super.buffer); }
|
|
860
|
+
get HEAP32() { return new Int32Array(super.buffer); }
|
|
861
|
+
get HEAPU32() { return new Uint32Array(super.buffer); }
|
|
862
|
+
get HEAP64() { return new BigInt64Array(super.buffer); }
|
|
863
|
+
get HEAPU64() { return new BigUint64Array(super.buffer); }
|
|
864
|
+
get HEAPF32() { return new Float32Array(super.buffer); }
|
|
865
|
+
get HEAPF64() { return new Float64Array(super.buffer); }
|
|
866
|
+
get view() { return new DataView(super.buffer); }
|
|
867
|
+
}
|
|
868
|
+
/** @public */
|
|
869
|
+
function extendMemory(memory) {
|
|
870
|
+
if (Object.getPrototypeOf(memory) === WebAssembly.Memory.prototype) {
|
|
871
|
+
Object.setPrototypeOf(memory, Memory.prototype);
|
|
872
|
+
}
|
|
873
|
+
return memory;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
function copyMemory(targets, src) {
|
|
877
|
+
if (targets.length === 0 || src.length === 0)
|
|
878
|
+
return 0;
|
|
879
|
+
let copied = 0;
|
|
880
|
+
let left = src.length - copied;
|
|
881
|
+
for (let i = 0; i < targets.length; ++i) {
|
|
882
|
+
const target = targets[i];
|
|
883
|
+
if (left < target.length) {
|
|
884
|
+
target.set(src.subarray(copied, copied + left), 0);
|
|
885
|
+
copied += left;
|
|
886
|
+
left = 0;
|
|
887
|
+
return copied;
|
|
888
|
+
}
|
|
889
|
+
target.set(src.subarray(copied, copied + target.length), 0);
|
|
890
|
+
copied += target.length;
|
|
891
|
+
left -= target.length;
|
|
892
|
+
}
|
|
893
|
+
return copied;
|
|
894
|
+
}
|
|
895
|
+
const _memory = new WeakMap();
|
|
896
|
+
const _wasi = new WeakMap();
|
|
897
|
+
const _fs = new WeakMap();
|
|
898
|
+
function getMemory(wasi) {
|
|
899
|
+
return _memory.get(wasi);
|
|
900
|
+
}
|
|
901
|
+
function getFs(wasi) {
|
|
902
|
+
const fs = _fs.get(wasi);
|
|
903
|
+
if (!fs)
|
|
904
|
+
throw new Error('filesystem is unavailable');
|
|
905
|
+
return fs;
|
|
906
|
+
}
|
|
907
|
+
function handleError(err) {
|
|
908
|
+
if (err instanceof WasiError) {
|
|
909
|
+
{
|
|
910
|
+
console.warn(err);
|
|
911
|
+
}
|
|
912
|
+
return err.errno;
|
|
913
|
+
}
|
|
914
|
+
switch (err.code) {
|
|
915
|
+
case 'ENOENT': return 44 /* WasiErrno.ENOENT */;
|
|
916
|
+
case 'EBADF': return 8 /* WasiErrno.EBADF */;
|
|
917
|
+
case 'EINVAL': return 28 /* WasiErrno.EINVAL */;
|
|
918
|
+
case 'EPERM': return 63 /* WasiErrno.EPERM */;
|
|
919
|
+
case 'EPROTO': return 65 /* WasiErrno.EPROTO */;
|
|
920
|
+
case 'EEXIST': return 20 /* WasiErrno.EEXIST */;
|
|
921
|
+
case 'ENOTDIR': return 54 /* WasiErrno.ENOTDIR */;
|
|
922
|
+
case 'EMFILE': return 33 /* WasiErrno.EMFILE */;
|
|
923
|
+
case 'EACCES': return 2 /* WasiErrno.EACCES */;
|
|
924
|
+
case 'EISDIR': return 31 /* WasiErrno.EISDIR */;
|
|
925
|
+
case 'ENOTEMPTY': return 55 /* WasiErrno.ENOTEMPTY */;
|
|
926
|
+
case 'ENOSYS': return 52 /* WasiErrno.ENOSYS */;
|
|
927
|
+
}
|
|
928
|
+
throw err;
|
|
929
|
+
}
|
|
930
|
+
function defineName(name, f) {
|
|
931
|
+
Object.defineProperty(f, 'name', { value: name });
|
|
932
|
+
return f;
|
|
933
|
+
}
|
|
934
|
+
function syscallWrap(name, f) {
|
|
935
|
+
return defineName(name, function () {
|
|
936
|
+
{
|
|
937
|
+
const args = Array.prototype.slice.call(arguments);
|
|
938
|
+
let debugArgs = [`${name}(${Array.from({ length: arguments.length }).map(() => '%d').join(', ')})`];
|
|
939
|
+
debugArgs = debugArgs.concat(args);
|
|
940
|
+
console.debug.apply(console, debugArgs);
|
|
941
|
+
}
|
|
942
|
+
let r;
|
|
943
|
+
try {
|
|
944
|
+
r = f.apply(this, arguments);
|
|
945
|
+
}
|
|
946
|
+
catch (err) {
|
|
947
|
+
return handleError(err);
|
|
948
|
+
}
|
|
949
|
+
if (isPromiseLike(r)) {
|
|
950
|
+
return r.then(_ => _, handleError);
|
|
951
|
+
}
|
|
952
|
+
return r;
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
function resolvePath(fs, fileDescriptor, path, flags) {
|
|
956
|
+
let resolvedPath = resolve(fileDescriptor.realPath, path);
|
|
957
|
+
if ((flags & 1) === 1) {
|
|
958
|
+
try {
|
|
959
|
+
resolvedPath = fs.readlinkSync(resolvedPath);
|
|
960
|
+
}
|
|
961
|
+
catch (err) {
|
|
962
|
+
if (err.code !== 'EINVAL' && err.code !== 'ENOENT') {
|
|
963
|
+
throw err;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
return resolvedPath;
|
|
968
|
+
}
|
|
969
|
+
const encoder = new TextEncoder();
|
|
970
|
+
const decoder = new TextDecoder();
|
|
971
|
+
function readStdin() {
|
|
972
|
+
const value = window.prompt();
|
|
973
|
+
if (value === null)
|
|
974
|
+
return new Uint8Array();
|
|
975
|
+
const buffer = new TextEncoder().encode(value + '\n');
|
|
976
|
+
return buffer;
|
|
977
|
+
}
|
|
978
|
+
class WASI$1 {
|
|
979
|
+
constructor(args, env, preopens, stdio, filesystem, print, printErr) {
|
|
980
|
+
this._setMemory = function _setMemory(m) {
|
|
981
|
+
if (!(m instanceof WebAssembly.Memory)) {
|
|
982
|
+
throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
|
|
983
|
+
}
|
|
984
|
+
_memory.set(this, extendMemory(m));
|
|
985
|
+
};
|
|
986
|
+
this.args_get = syscallWrap('args_get', function (argv, argv_buf) {
|
|
987
|
+
argv = Number(argv);
|
|
988
|
+
argv_buf = Number(argv_buf);
|
|
989
|
+
if (argv === 0 || argv_buf === 0) {
|
|
990
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
991
|
+
}
|
|
992
|
+
const { HEAPU8, view } = getMemory(this);
|
|
993
|
+
const wasi = _wasi.get(this);
|
|
994
|
+
const args = wasi.args;
|
|
995
|
+
for (let i = 0; i < args.length; ++i) {
|
|
996
|
+
const arg = args[i];
|
|
997
|
+
view.setInt32(argv, argv_buf, true);
|
|
998
|
+
argv += 4;
|
|
999
|
+
const data = encoder.encode(arg + '\0');
|
|
1000
|
+
HEAPU8.set(data, argv_buf);
|
|
1001
|
+
argv_buf += data.length;
|
|
1002
|
+
}
|
|
1003
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1004
|
+
});
|
|
1005
|
+
this.args_sizes_get = syscallWrap('args_sizes_get', function (argc, argv_buf_size) {
|
|
1006
|
+
argc = Number(argc);
|
|
1007
|
+
argv_buf_size = Number(argv_buf_size);
|
|
1008
|
+
if (argc === 0 || argv_buf_size === 0) {
|
|
1009
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1010
|
+
}
|
|
1011
|
+
const { view } = getMemory(this);
|
|
1012
|
+
const wasi = _wasi.get(this);
|
|
1013
|
+
const args = wasi.args;
|
|
1014
|
+
view.setUint32(argc, args.length, true);
|
|
1015
|
+
view.setUint32(argv_buf_size, encoder.encode(args.join('\0') + '\0').length, true);
|
|
1016
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1017
|
+
});
|
|
1018
|
+
this.environ_get = syscallWrap('environ_get', function (environ, environ_buf) {
|
|
1019
|
+
environ = Number(environ);
|
|
1020
|
+
environ_buf = Number(environ_buf);
|
|
1021
|
+
if (environ === 0 || environ_buf === 0) {
|
|
1022
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1023
|
+
}
|
|
1024
|
+
const { HEAPU8, view } = getMemory(this);
|
|
1025
|
+
const wasi = _wasi.get(this);
|
|
1026
|
+
const env = wasi.env;
|
|
1027
|
+
for (let i = 0; i < env.length; ++i) {
|
|
1028
|
+
const pair = env[i];
|
|
1029
|
+
view.setInt32(environ, environ_buf, true);
|
|
1030
|
+
environ += 4;
|
|
1031
|
+
const data = encoder.encode(pair + '\0');
|
|
1032
|
+
HEAPU8.set(data, environ_buf);
|
|
1033
|
+
environ_buf += data.length;
|
|
1034
|
+
}
|
|
1035
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1036
|
+
});
|
|
1037
|
+
this.environ_sizes_get = syscallWrap('environ_sizes_get', function (len, buflen) {
|
|
1038
|
+
len = Number(len);
|
|
1039
|
+
buflen = Number(buflen);
|
|
1040
|
+
if (len === 0 || buflen === 0) {
|
|
1041
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1042
|
+
}
|
|
1043
|
+
const { view } = getMemory(this);
|
|
1044
|
+
const wasi = _wasi.get(this);
|
|
1045
|
+
view.setUint32(len, wasi.env.length, true);
|
|
1046
|
+
view.setUint32(buflen, encoder.encode(wasi.env.join('\0') + '\0').length, true);
|
|
1047
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1048
|
+
});
|
|
1049
|
+
this.clock_res_get = syscallWrap('clock_res_get', function (id, resolution) {
|
|
1050
|
+
resolution = Number(resolution);
|
|
1051
|
+
if (resolution === 0) {
|
|
1052
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1053
|
+
}
|
|
1054
|
+
const { view } = getMemory(this);
|
|
1055
|
+
switch (id) {
|
|
1056
|
+
case 0 /* WasiClockid.REALTIME */:
|
|
1057
|
+
view.setBigUint64(resolution, BigInt(1000000), true);
|
|
1058
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1059
|
+
case 1 /* WasiClockid.MONOTONIC */:
|
|
1060
|
+
case 2 /* WasiClockid.PROCESS_CPUTIME_ID */:
|
|
1061
|
+
case 3 /* WasiClockid.THREAD_CPUTIME_ID */:
|
|
1062
|
+
view.setBigUint64(resolution, BigInt(1000), true);
|
|
1063
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1064
|
+
default: return 28 /* WasiErrno.EINVAL */;
|
|
1065
|
+
}
|
|
1066
|
+
});
|
|
1067
|
+
this.clock_time_get = syscallWrap('clock_time_get', function (id, _percision, time) {
|
|
1068
|
+
time = Number(time);
|
|
1069
|
+
if (time === 0) {
|
|
1070
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1071
|
+
}
|
|
1072
|
+
const { view } = getMemory(this);
|
|
1073
|
+
switch (id) {
|
|
1074
|
+
case 0 /* WasiClockid.REALTIME */:
|
|
1075
|
+
view.setBigUint64(time, BigInt(Date.now()) * BigInt(1000000), true);
|
|
1076
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1077
|
+
case 1 /* WasiClockid.MONOTONIC */:
|
|
1078
|
+
case 2 /* WasiClockid.PROCESS_CPUTIME_ID */:
|
|
1079
|
+
case 3 /* WasiClockid.THREAD_CPUTIME_ID */: {
|
|
1080
|
+
const t = performance.now();
|
|
1081
|
+
const s = Math.trunc(t);
|
|
1082
|
+
const ms = Math.floor((t - s) * 1000);
|
|
1083
|
+
const result = BigInt(s) * BigInt(1000000000) + BigInt(ms) * BigInt(1000000);
|
|
1084
|
+
view.setBigUint64(time, result, true);
|
|
1085
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1086
|
+
}
|
|
1087
|
+
default: return 28 /* WasiErrno.EINVAL */;
|
|
1088
|
+
}
|
|
1089
|
+
});
|
|
1090
|
+
this.fd_advise = syscallWrap('fd_advise', function (_fd, _offset, _len, _advice) {
|
|
1091
|
+
return 52 /* WasiErrno.ENOSYS */;
|
|
1092
|
+
});
|
|
1093
|
+
this.fd_allocate = syscallWrap('fd_allocate', function (fd, offset, len) {
|
|
1094
|
+
const wasi = _wasi.get(this);
|
|
1095
|
+
const fs = getFs(this);
|
|
1096
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_ALLOCATE, BigInt(0));
|
|
1097
|
+
const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
|
|
1098
|
+
if (stat.size < offset + len) {
|
|
1099
|
+
fs.truncateSync(fileDescriptor.fd, Number(offset + len));
|
|
1100
|
+
}
|
|
1101
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1102
|
+
});
|
|
1103
|
+
this.fd_close = syscallWrap('fd_close', function (fd) {
|
|
1104
|
+
const wasi = _wasi.get(this);
|
|
1105
|
+
const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
|
|
1106
|
+
const fs = getFs(this);
|
|
1107
|
+
fs.closeSync(fileDescriptor.fd);
|
|
1108
|
+
wasi.fds.remove(fd);
|
|
1109
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1110
|
+
});
|
|
1111
|
+
this.fd_datasync = syscallWrap('fd_datasync', function (fd) {
|
|
1112
|
+
const wasi = _wasi.get(this);
|
|
1113
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_DATASYNC, BigInt(0));
|
|
1114
|
+
const fs = getFs(this);
|
|
1115
|
+
fs.fdatasyncSync(fileDescriptor.fd);
|
|
1116
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1117
|
+
});
|
|
1118
|
+
this.fd_fdstat_get = syscallWrap('fd_fdstat_get', function (fd, fdstat) {
|
|
1119
|
+
fdstat = Number(fdstat);
|
|
1120
|
+
if (fdstat === 0) {
|
|
1121
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1122
|
+
}
|
|
1123
|
+
const wasi = _wasi.get(this);
|
|
1124
|
+
const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
|
|
1125
|
+
const { view } = getMemory(this);
|
|
1126
|
+
view.setUint16(fdstat, fileDescriptor.type, true);
|
|
1127
|
+
view.setUint16(fdstat + 2, 0, true);
|
|
1128
|
+
view.setBigUint64(fdstat + 8, fileDescriptor.rightsBase, true);
|
|
1129
|
+
view.setBigUint64(fdstat + 16, fileDescriptor.rightsInheriting, true);
|
|
1130
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1131
|
+
});
|
|
1132
|
+
this.fd_fdstat_set_flags = syscallWrap('fd_fdstat_set_flags', function (_fd, _flags) {
|
|
1133
|
+
return 52 /* WasiErrno.ENOSYS */;
|
|
1134
|
+
});
|
|
1135
|
+
this.fd_fdstat_set_rights = syscallWrap('fd_fdstat_set_rights', function (fd, rightsBase, rightsInheriting) {
|
|
1136
|
+
const wasi = _wasi.get(this);
|
|
1137
|
+
const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
|
|
1138
|
+
if ((rightsBase | fileDescriptor.rightsBase) > fileDescriptor.rightsBase) {
|
|
1139
|
+
return 76 /* WasiErrno.ENOTCAPABLE */;
|
|
1140
|
+
}
|
|
1141
|
+
if ((rightsInheriting | fileDescriptor.rightsInheriting) >
|
|
1142
|
+
fileDescriptor.rightsInheriting) {
|
|
1143
|
+
return 76 /* WasiErrno.ENOTCAPABLE */;
|
|
1144
|
+
}
|
|
1145
|
+
fileDescriptor.rightsBase = rightsBase;
|
|
1146
|
+
fileDescriptor.rightsInheriting = rightsInheriting;
|
|
1147
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1148
|
+
});
|
|
1149
|
+
this.fd_filestat_get = syscallWrap('fd_filestat_get', function (fd, buf) {
|
|
1150
|
+
buf = Number(buf);
|
|
1151
|
+
if (buf === 0)
|
|
1152
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1153
|
+
const wasi = _wasi.get(this);
|
|
1154
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
|
|
1155
|
+
const fs = getFs(this);
|
|
1156
|
+
const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
|
|
1157
|
+
const { view } = getMemory(this);
|
|
1158
|
+
toFileStat(view, buf, stat);
|
|
1159
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1160
|
+
});
|
|
1161
|
+
this.fd_filestat_set_size = syscallWrap('fd_filestat_set_size', function (fd, size) {
|
|
1162
|
+
const wasi = _wasi.get(this);
|
|
1163
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_SIZE, BigInt(0));
|
|
1164
|
+
const fs = getFs(this);
|
|
1165
|
+
fs.ftruncateSync(fileDescriptor.fd, Number(size));
|
|
1166
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1167
|
+
});
|
|
1168
|
+
this.fd_filestat_set_times = syscallWrap('fd_filestat_set_times', function (fd, atim, mtim, flags) {
|
|
1169
|
+
const wasi = _wasi.get(this);
|
|
1170
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_TIMES, BigInt(0));
|
|
1171
|
+
if ((flags & 2 /* WasiFstFlag.SET_ATIM_NOW */) === 2 /* WasiFstFlag.SET_ATIM_NOW */) {
|
|
1172
|
+
atim = BigInt(Date.now() * 1000000);
|
|
1173
|
+
}
|
|
1174
|
+
if ((flags & 8 /* WasiFstFlag.SET_MTIM_NOW */) === 8 /* WasiFstFlag.SET_MTIM_NOW */) {
|
|
1175
|
+
mtim = BigInt(Date.now() * 1000000);
|
|
1176
|
+
}
|
|
1177
|
+
const fs = getFs(this);
|
|
1178
|
+
fs.futimesSync(fileDescriptor.fd, Number(atim), Number(mtim));
|
|
1179
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1180
|
+
});
|
|
1181
|
+
this.fd_pread = syscallWrap('fd_pread', function (fd, iovs, iovslen, offset, size) {
|
|
1182
|
+
iovs = Number(iovs);
|
|
1183
|
+
size = Number(size);
|
|
1184
|
+
if (iovs === 0 || size === 0) {
|
|
1185
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1186
|
+
}
|
|
1187
|
+
const { HEAPU8, view } = getMemory(this);
|
|
1188
|
+
const wasi = _wasi.get(this);
|
|
1189
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
|
|
1190
|
+
let totalSize = 0;
|
|
1191
|
+
const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
|
|
1192
|
+
const offset = iovs + (i * 8);
|
|
1193
|
+
const buf = view.getInt32(offset, true);
|
|
1194
|
+
const bufLen = view.getUint32(offset + 4, true);
|
|
1195
|
+
totalSize += bufLen;
|
|
1196
|
+
return HEAPU8.subarray(buf, buf + bufLen);
|
|
1197
|
+
});
|
|
1198
|
+
let nread = 0;
|
|
1199
|
+
const buffer = new Uint8Array(totalSize);
|
|
1200
|
+
buffer._isBuffer = true;
|
|
1201
|
+
const fs = getFs(this);
|
|
1202
|
+
const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
|
|
1203
|
+
nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
|
|
1204
|
+
view.setUint32(size, nread, true);
|
|
1205
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1206
|
+
});
|
|
1207
|
+
this.fd_prestat_get = syscallWrap('fd_prestat_get', function (fd, prestat) {
|
|
1208
|
+
prestat = Number(prestat);
|
|
1209
|
+
if (prestat === 0) {
|
|
1210
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1211
|
+
}
|
|
1212
|
+
const wasi = _wasi.get(this);
|
|
1213
|
+
let fileDescriptor;
|
|
1214
|
+
try {
|
|
1215
|
+
fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
|
|
1216
|
+
}
|
|
1217
|
+
catch (err) {
|
|
1218
|
+
if (err instanceof WasiError)
|
|
1219
|
+
return err.errno;
|
|
1220
|
+
throw err;
|
|
1221
|
+
}
|
|
1222
|
+
if (fileDescriptor.preopen !== 1)
|
|
1223
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1224
|
+
const { view } = getMemory(this);
|
|
1225
|
+
// preopen type is dir(0)
|
|
1226
|
+
view.setUint32(prestat, 0, true);
|
|
1227
|
+
view.setUint32(prestat + 4, encoder.encode(fileDescriptor.path).length + 1, true);
|
|
1228
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1229
|
+
});
|
|
1230
|
+
this.fd_prestat_dir_name = syscallWrap('fd_prestat_dir_name', function (fd, path, path_len) {
|
|
1231
|
+
path = Number(path);
|
|
1232
|
+
path_len = Number(path_len);
|
|
1233
|
+
if (path === 0) {
|
|
1234
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1235
|
+
}
|
|
1236
|
+
const wasi = _wasi.get(this);
|
|
1237
|
+
const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
|
|
1238
|
+
if (fileDescriptor.preopen !== 1)
|
|
1239
|
+
return 8 /* WasiErrno.EBADF */;
|
|
1240
|
+
const buffer = encoder.encode(fileDescriptor.path + '\0');
|
|
1241
|
+
const size = buffer.length;
|
|
1242
|
+
if (size > path_len)
|
|
1243
|
+
return 42 /* WasiErrno.ENOBUFS */;
|
|
1244
|
+
const { HEAPU8 } = getMemory(this);
|
|
1245
|
+
HEAPU8.set(buffer, path);
|
|
1246
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1247
|
+
});
|
|
1248
|
+
this.fd_pwrite = syscallWrap('fd_pwrite', function (fd, iovs, iovslen, offset, size) {
|
|
1249
|
+
iovs = Number(iovs);
|
|
1250
|
+
size = Number(size);
|
|
1251
|
+
if (iovs === 0 || size === 0) {
|
|
1252
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1253
|
+
}
|
|
1254
|
+
const { HEAPU8, view } = getMemory(this);
|
|
1255
|
+
const wasi = _wasi.get(this);
|
|
1256
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
|
|
1257
|
+
const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
|
|
1258
|
+
const offset = iovs + (i * 8);
|
|
1259
|
+
const buf = view.getInt32(offset, true);
|
|
1260
|
+
const bufLen = view.getUint32(offset + 4, true);
|
|
1261
|
+
return HEAPU8.subarray(buf, buf + bufLen);
|
|
1262
|
+
}));
|
|
1263
|
+
const fs = getFs(this);
|
|
1264
|
+
const nwritten = fs.writeSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
|
|
1265
|
+
view.setUint32(size, nwritten, true);
|
|
1266
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1267
|
+
});
|
|
1268
|
+
this.fd_read = syscallWrap('fd_read', function (fd, iovs, iovslen, size) {
|
|
1269
|
+
iovs = Number(iovs);
|
|
1270
|
+
size = Number(size);
|
|
1271
|
+
if (iovs === 0 || size === 0) {
|
|
1272
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1273
|
+
}
|
|
1274
|
+
const { HEAPU8, view } = getMemory(this);
|
|
1275
|
+
const wasi = _wasi.get(this);
|
|
1276
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
|
|
1277
|
+
let totalSize = 0;
|
|
1278
|
+
const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
|
|
1279
|
+
const offset = iovs + (i * 8);
|
|
1280
|
+
const buf = view.getInt32(offset, true);
|
|
1281
|
+
const bufLen = view.getUint32(offset + 4, true);
|
|
1282
|
+
totalSize += bufLen;
|
|
1283
|
+
return HEAPU8.subarray(buf, buf + bufLen);
|
|
1284
|
+
});
|
|
1285
|
+
let buffer;
|
|
1286
|
+
let nread = 0;
|
|
1287
|
+
if (fd === 0) {
|
|
1288
|
+
buffer = readStdin();
|
|
1289
|
+
nread = buffer ? copyMemory(ioVecs, buffer) : 0;
|
|
1290
|
+
}
|
|
1291
|
+
else {
|
|
1292
|
+
buffer = new Uint8Array(totalSize);
|
|
1293
|
+
buffer._isBuffer = true;
|
|
1294
|
+
const fs = getFs(this);
|
|
1295
|
+
const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(fileDescriptor.pos));
|
|
1296
|
+
nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
|
|
1297
|
+
fileDescriptor.pos += BigInt(nread);
|
|
1298
|
+
}
|
|
1299
|
+
view.setUint32(size, nread, true);
|
|
1300
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1301
|
+
});
|
|
1302
|
+
this.fd_seek = syscallWrap('fd_seek', function (fd, offset, whence, newOffset) {
|
|
1303
|
+
newOffset = Number(newOffset);
|
|
1304
|
+
if (newOffset === 0) {
|
|
1305
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1306
|
+
}
|
|
1307
|
+
if (fd === 0 || fd === 1 || fd === 2)
|
|
1308
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1309
|
+
const wasi = _wasi.get(this);
|
|
1310
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SEEK, BigInt(0));
|
|
1311
|
+
const r = fileDescriptor.seek(offset, whence);
|
|
1312
|
+
const { view } = getMemory(this);
|
|
1313
|
+
view.setBigUint64(newOffset, r, true);
|
|
1314
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1315
|
+
});
|
|
1316
|
+
this.fd_readdir = syscallWrap('fd_readdir', function (fd, buf, buf_len, cookie, bufused) {
|
|
1317
|
+
buf = Number(buf);
|
|
1318
|
+
buf_len = Number(buf_len);
|
|
1319
|
+
bufused = Number(bufused);
|
|
1320
|
+
if (buf === 0 || bufused === 0)
|
|
1321
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1322
|
+
const wasi = _wasi.get(this);
|
|
1323
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READDIR, BigInt(0));
|
|
1324
|
+
const fs = getFs(this);
|
|
1325
|
+
const entries = fs.readdirSync(fileDescriptor.realPath, { withFileTypes: true });
|
|
1326
|
+
const { HEAPU8, view } = getMemory(this);
|
|
1327
|
+
let bufferUsed = 0;
|
|
1328
|
+
for (let i = Number(cookie); i < entries.length; i++) {
|
|
1329
|
+
const nameData = encoder.encode(entries[i].name);
|
|
1330
|
+
const entryInfo = fs.statSync(resolve(fileDescriptor.realPath, entries[i].name), { bigint: true });
|
|
1331
|
+
const entryData = new Uint8Array(24 + nameData.byteLength);
|
|
1332
|
+
const entryView = new DataView(entryData.buffer);
|
|
1333
|
+
entryView.setBigUint64(0, BigInt(i + 1), true);
|
|
1334
|
+
entryView.setBigUint64(8, BigInt(entryInfo.ino ? entryInfo.ino : 0), true);
|
|
1335
|
+
entryView.setUint32(16, nameData.byteLength, true);
|
|
1336
|
+
let type;
|
|
1337
|
+
if (entries[i].isFile()) {
|
|
1338
|
+
type = 4 /* WasiFileType.REGULAR_FILE */;
|
|
1339
|
+
}
|
|
1340
|
+
else if (entries[i].isDirectory()) {
|
|
1341
|
+
type = 3 /* WasiFileType.DIRECTORY */;
|
|
1342
|
+
}
|
|
1343
|
+
else if (entries[i].isSymbolicLink()) {
|
|
1344
|
+
type = 7 /* WasiFileType.SYMBOLIC_LINK */;
|
|
1345
|
+
}
|
|
1346
|
+
else if (entries[i].isCharacterDevice()) {
|
|
1347
|
+
type = 2 /* WasiFileType.CHARACTER_DEVICE */;
|
|
1348
|
+
}
|
|
1349
|
+
else if (entries[i].isBlockDevice()) {
|
|
1350
|
+
type = 1 /* WasiFileType.BLOCK_DEVICE */;
|
|
1351
|
+
}
|
|
1352
|
+
else if (entries[i].isSocket()) {
|
|
1353
|
+
type = 6 /* WasiFileType.SOCKET_STREAM */;
|
|
1354
|
+
}
|
|
1355
|
+
else {
|
|
1356
|
+
type = 0 /* WasiFileType.UNKNOWN */;
|
|
1357
|
+
}
|
|
1358
|
+
entryView.setUint8(20, type);
|
|
1359
|
+
entryData.set(nameData, 24);
|
|
1360
|
+
const data = entryData.slice(0, Math.min(entryData.length, buf_len - bufferUsed));
|
|
1361
|
+
HEAPU8.set(data, buf + bufferUsed);
|
|
1362
|
+
bufferUsed += data.byteLength;
|
|
1363
|
+
}
|
|
1364
|
+
view.setUint32(bufused, bufferUsed, true);
|
|
1365
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1366
|
+
});
|
|
1367
|
+
this.fd_renumber = syscallWrap('fd_renumber', function (from, to) {
|
|
1368
|
+
const wasi = _wasi.get(this);
|
|
1369
|
+
wasi.fds.renumber(to, from);
|
|
1370
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1371
|
+
});
|
|
1372
|
+
this.fd_sync = syscallWrap('fd_sync', function (fd) {
|
|
1373
|
+
const wasi = _wasi.get(this);
|
|
1374
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SYNC, BigInt(0));
|
|
1375
|
+
const fs = getFs(this);
|
|
1376
|
+
fs.fsyncSync(fileDescriptor.fd);
|
|
1377
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1378
|
+
});
|
|
1379
|
+
this.fd_tell = syscallWrap('fd_tell', function (fd, offset) {
|
|
1380
|
+
const wasi = _wasi.get(this);
|
|
1381
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_TELL, BigInt(0));
|
|
1382
|
+
const pos = BigInt(fileDescriptor.pos);
|
|
1383
|
+
const { view } = getMemory(this);
|
|
1384
|
+
view.setBigUint64(Number(offset), pos, true);
|
|
1385
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1386
|
+
});
|
|
1387
|
+
this.fd_write = syscallWrap('fd_write', function (fd, iovs, iovslen, size) {
|
|
1388
|
+
iovs = Number(iovs);
|
|
1389
|
+
size = Number(size);
|
|
1390
|
+
if (iovs === 0 || size === 0) {
|
|
1391
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1392
|
+
}
|
|
1393
|
+
const { HEAPU8, view } = getMemory(this);
|
|
1394
|
+
const wasi = _wasi.get(this);
|
|
1395
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
|
|
1396
|
+
const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
|
|
1397
|
+
const offset = iovs + (i * 8);
|
|
1398
|
+
const buf = view.getInt32(offset, true);
|
|
1399
|
+
const bufLen = view.getUint32(offset + 4, true);
|
|
1400
|
+
return HEAPU8.subarray(buf, buf + bufLen);
|
|
1401
|
+
}));
|
|
1402
|
+
let nwritten;
|
|
1403
|
+
if (fd === 1 || fd === 2) {
|
|
1404
|
+
nwritten = fileDescriptor.write(buffer);
|
|
1405
|
+
}
|
|
1406
|
+
else {
|
|
1407
|
+
const fs = getFs(this);
|
|
1408
|
+
nwritten = fs.writeSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(fileDescriptor.pos));
|
|
1409
|
+
fileDescriptor.pos += BigInt(nwritten);
|
|
1410
|
+
}
|
|
1411
|
+
view.setUint32(size, nwritten, true);
|
|
1412
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1413
|
+
});
|
|
1414
|
+
this.path_create_directory = syscallWrap('path_create_directory', function (fd, path, path_len) {
|
|
1415
|
+
path = Number(path);
|
|
1416
|
+
path_len = Number(path_len);
|
|
1417
|
+
if (path === 0) {
|
|
1418
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1419
|
+
}
|
|
1420
|
+
const { HEAPU8 } = getMemory(this);
|
|
1421
|
+
const wasi = _wasi.get(this);
|
|
1422
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
|
|
1423
|
+
let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
|
|
1424
|
+
pathString = resolve(fileDescriptor.realPath, pathString);
|
|
1425
|
+
const fs = getFs(this);
|
|
1426
|
+
fs.mkdirSync(pathString);
|
|
1427
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1428
|
+
});
|
|
1429
|
+
this.path_filestat_get = syscallWrap('path_filestat_get', function (fd, flags, path, path_len, filestat) {
|
|
1430
|
+
path = Number(path);
|
|
1431
|
+
path_len = Number(path_len);
|
|
1432
|
+
filestat = Number(filestat);
|
|
1433
|
+
if (path === 0 || filestat === 0) {
|
|
1434
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1435
|
+
}
|
|
1436
|
+
const { HEAPU8, view } = getMemory(this);
|
|
1437
|
+
const wasi = _wasi.get(this);
|
|
1438
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
|
|
1439
|
+
let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
|
|
1440
|
+
const fs = getFs(this);
|
|
1441
|
+
pathString = resolve(fileDescriptor.realPath, pathString);
|
|
1442
|
+
let stat;
|
|
1443
|
+
if ((flags & 1) === 1) {
|
|
1444
|
+
stat = fs.statSync(pathString, { bigint: true });
|
|
1445
|
+
}
|
|
1446
|
+
else {
|
|
1447
|
+
stat = fs.lstatSync(pathString, { bigint: true });
|
|
1448
|
+
}
|
|
1449
|
+
toFileStat(view, filestat, stat);
|
|
1450
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1451
|
+
});
|
|
1452
|
+
this.path_filestat_set_times = syscallWrap('path_filestat_set_times', function (fd, flags, path, path_len, atim, mtim, fst_flags) {
|
|
1453
|
+
path = Number(path);
|
|
1454
|
+
path_len = Number(path_len);
|
|
1455
|
+
if (path === 0)
|
|
1456
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1457
|
+
if ((fst_flags) & ~(1 /* WasiFstFlag.SET_ATIM */ |
|
|
1458
|
+
2 /* WasiFstFlag.SET_ATIM_NOW */ |
|
|
1459
|
+
4 /* WasiFstFlag.SET_MTIM */ |
|
|
1460
|
+
8 /* WasiFstFlag.SET_MTIM_NOW */)) {
|
|
1461
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1462
|
+
}
|
|
1463
|
+
const { HEAPU8 } = getMemory(this);
|
|
1464
|
+
const wasi = _wasi.get(this);
|
|
1465
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
|
|
1466
|
+
const fs = getFs(this);
|
|
1467
|
+
const resolvedPath = resolvePath(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
|
|
1468
|
+
if ((fst_flags & 2 /* WasiFstFlag.SET_ATIM_NOW */) === 2 /* WasiFstFlag.SET_ATIM_NOW */) {
|
|
1469
|
+
atim = BigInt(Date.now() * 1000000);
|
|
1470
|
+
}
|
|
1471
|
+
if ((fst_flags & 8 /* WasiFstFlag.SET_MTIM_NOW */) === 8 /* WasiFstFlag.SET_MTIM_NOW */) {
|
|
1472
|
+
mtim = BigInt(Date.now() * 1000000);
|
|
1473
|
+
}
|
|
1474
|
+
fs.utimesSync(resolvedPath, Number(atim), Number(mtim));
|
|
1475
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1476
|
+
});
|
|
1477
|
+
this.path_link = syscallWrap('path_link', function (old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
|
|
1478
|
+
old_path = Number(old_path);
|
|
1479
|
+
old_path_len = Number(old_path_len);
|
|
1480
|
+
new_path = Number(new_path);
|
|
1481
|
+
new_path_len = Number(new_path_len);
|
|
1482
|
+
if (old_path === 0 || new_path === 0) {
|
|
1483
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1484
|
+
}
|
|
1485
|
+
const wasi = _wasi.get(this);
|
|
1486
|
+
let oldWrap;
|
|
1487
|
+
let newWrap;
|
|
1488
|
+
if (old_fd === new_fd) {
|
|
1489
|
+
oldWrap = newWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE | WasiRights.PATH_LINK_TARGET, BigInt(0));
|
|
1490
|
+
}
|
|
1491
|
+
else {
|
|
1492
|
+
oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE, BigInt(0));
|
|
1493
|
+
newWrap = wasi.fds.get(new_fd, WasiRights.PATH_LINK_TARGET, BigInt(0));
|
|
1494
|
+
}
|
|
1495
|
+
const { HEAPU8 } = getMemory(this);
|
|
1496
|
+
const fs = getFs(this);
|
|
1497
|
+
const resolvedOldPath = resolvePath(fs, oldWrap, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)), old_flags);
|
|
1498
|
+
const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
|
|
1499
|
+
fs.linkSync(resolvedOldPath, resolvedNewPath);
|
|
1500
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1501
|
+
});
|
|
1502
|
+
this.path_open = syscallWrap('path_open', function (dirfd, dirflags, path, path_len, o_flags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) {
|
|
1503
|
+
path = Number(path);
|
|
1504
|
+
fd = Number(fd);
|
|
1505
|
+
if (path === 0 || fd === 0) {
|
|
1506
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1507
|
+
}
|
|
1508
|
+
path_len = Number(path_len);
|
|
1509
|
+
fs_rights_base = BigInt(fs_rights_base);
|
|
1510
|
+
fs_rights_base = BigInt(fs_rights_base);
|
|
1511
|
+
const read = (fs_rights_base & (WasiRights.FD_READ |
|
|
1512
|
+
WasiRights.FD_READDIR)) !== BigInt(0);
|
|
1513
|
+
const write = (fs_rights_base & (WasiRights.FD_DATASYNC |
|
|
1514
|
+
WasiRights.FD_WRITE |
|
|
1515
|
+
WasiRights.FD_ALLOCATE |
|
|
1516
|
+
WasiRights.FD_FILESTAT_SET_SIZE)) !== BigInt(0);
|
|
1517
|
+
let flags = write ? read ? 2 /* FileControlFlag.O_RDWR */ : 1 /* FileControlFlag.O_WRONLY */ : 0 /* FileControlFlag.O_RDONLY */;
|
|
1518
|
+
let needed_base = WasiRights.PATH_OPEN;
|
|
1519
|
+
let needed_inheriting = fs_rights_base | fs_rights_inheriting;
|
|
1520
|
+
if ((o_flags & 1 /* WasiFileControlFlag.O_CREAT */) !== 0) {
|
|
1521
|
+
flags |= 64 /* FileControlFlag.O_CREAT */;
|
|
1522
|
+
needed_base |= WasiRights.PATH_CREATE_FILE;
|
|
1523
|
+
}
|
|
1524
|
+
if ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0) {
|
|
1525
|
+
flags |= 65536 /* FileControlFlag.O_DIRECTORY */;
|
|
1526
|
+
}
|
|
1527
|
+
if ((o_flags & 4 /* WasiFileControlFlag.O_EXCL */) !== 0) {
|
|
1528
|
+
flags |= 128 /* FileControlFlag.O_EXCL */;
|
|
1529
|
+
}
|
|
1530
|
+
if ((o_flags & 8 /* WasiFileControlFlag.O_TRUNC */) !== 0) {
|
|
1531
|
+
flags |= 512 /* FileControlFlag.O_TRUNC */;
|
|
1532
|
+
needed_base |= WasiRights.PATH_FILESTAT_SET_SIZE;
|
|
1533
|
+
}
|
|
1534
|
+
if ((fs_flags & 1 /* WasiFdFlag.APPEND */) !== 0) {
|
|
1535
|
+
flags |= 1024 /* FileControlFlag.O_APPEND */;
|
|
1536
|
+
}
|
|
1537
|
+
if ((fs_flags & 2 /* WasiFdFlag.DSYNC */) !== 0) {
|
|
1538
|
+
// flags |= FileControlFlag.O_DSYNC;
|
|
1539
|
+
needed_inheriting |= WasiRights.FD_DATASYNC;
|
|
1540
|
+
}
|
|
1541
|
+
if ((fs_flags & 4 /* WasiFdFlag.NONBLOCK */) !== 0) {
|
|
1542
|
+
flags |= 2048 /* FileControlFlag.O_NONBLOCK */;
|
|
1543
|
+
}
|
|
1544
|
+
if ((fs_flags & 8 /* WasiFdFlag.RSYNC */) !== 0) {
|
|
1545
|
+
flags |= 1052672 /* FileControlFlag.O_SYNC */;
|
|
1546
|
+
needed_inheriting |= WasiRights.FD_SYNC;
|
|
1547
|
+
}
|
|
1548
|
+
if ((fs_flags & 16 /* WasiFdFlag.SYNC */) !== 0) {
|
|
1549
|
+
flags |= 1052672 /* FileControlFlag.O_SYNC */;
|
|
1550
|
+
needed_inheriting |= WasiRights.FD_SYNC;
|
|
1551
|
+
}
|
|
1552
|
+
if (write && (flags & (1024 /* FileControlFlag.O_APPEND */ | 512 /* FileControlFlag.O_TRUNC */)) === 0) {
|
|
1553
|
+
needed_inheriting |= WasiRights.FD_SEEK;
|
|
1554
|
+
}
|
|
1555
|
+
const wasi = _wasi.get(this);
|
|
1556
|
+
const fileDescriptor = wasi.fds.get(dirfd, needed_base, needed_inheriting);
|
|
1557
|
+
const memory = getMemory(this);
|
|
1558
|
+
const HEAPU8 = memory.HEAPU8;
|
|
1559
|
+
const pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
|
|
1560
|
+
const fs = getFs(this);
|
|
1561
|
+
const resolved_path = resolvePath(fs, fileDescriptor, pathString, dirflags);
|
|
1562
|
+
const r = fs.openSync(resolved_path, flags, 0o666);
|
|
1563
|
+
const filetype = wasi.fds.getFileTypeByFd(r);
|
|
1564
|
+
if ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 && filetype !== 3 /* WasiFileType.DIRECTORY */) {
|
|
1565
|
+
return 54 /* WasiErrno.ENOTDIR */;
|
|
1566
|
+
}
|
|
1567
|
+
const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r, flags, filetype);
|
|
1568
|
+
const wrap = wasi.fds.insert(r, resolved_path, resolved_path, filetype, fs_rights_base & max_base, fs_rights_inheriting & max_inheriting, 0);
|
|
1569
|
+
const stat = fs.fstatSync(r, { bigint: true });
|
|
1570
|
+
if (stat.isFile()) {
|
|
1571
|
+
wrap.size = stat.size;
|
|
1572
|
+
if ((flags & 1024 /* FileControlFlag.O_APPEND */) !== 0) {
|
|
1573
|
+
wrap.pos = stat.size;
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
const view = memory.view;
|
|
1577
|
+
view.setInt32(fd, wrap.id, true);
|
|
1578
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1579
|
+
});
|
|
1580
|
+
this.path_readlink = syscallWrap('path_readlink', function (fd, path, path_len, buf, buf_len, bufused) {
|
|
1581
|
+
path = Number(path);
|
|
1582
|
+
path_len = Number(path_len);
|
|
1583
|
+
buf = Number(buf);
|
|
1584
|
+
buf_len = Number(buf_len);
|
|
1585
|
+
bufused = Number(bufused);
|
|
1586
|
+
if (path === 0 || buf === 0 || bufused === 0) {
|
|
1587
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1588
|
+
}
|
|
1589
|
+
const { HEAPU8, view } = getMemory(this);
|
|
1590
|
+
const wasi = _wasi.get(this);
|
|
1591
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
|
|
1592
|
+
let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
|
|
1593
|
+
pathString = resolve(fileDescriptor.realPath, pathString);
|
|
1594
|
+
const fs = getFs(this);
|
|
1595
|
+
const link = fs.readlinkSync(pathString);
|
|
1596
|
+
const linkData = encoder.encode(link);
|
|
1597
|
+
const len = Math.min(linkData.length, buf_len);
|
|
1598
|
+
if (len >= buf_len)
|
|
1599
|
+
return 42 /* WasiErrno.ENOBUFS */;
|
|
1600
|
+
HEAPU8.set(linkData.subarray(0, len), buf);
|
|
1601
|
+
HEAPU8[buf + len] = 0;
|
|
1602
|
+
view.setUint32(bufused, len + 1, true);
|
|
1603
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1604
|
+
});
|
|
1605
|
+
this.path_remove_directory = syscallWrap('path_remove_directory', function (fd, path, path_len) {
|
|
1606
|
+
path = Number(path);
|
|
1607
|
+
path_len = Number(path_len);
|
|
1608
|
+
if (path === 0) {
|
|
1609
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1610
|
+
}
|
|
1611
|
+
const { HEAPU8 } = getMemory(this);
|
|
1612
|
+
const wasi = _wasi.get(this);
|
|
1613
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
|
|
1614
|
+
let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
|
|
1615
|
+
pathString = resolve(fileDescriptor.realPath, pathString);
|
|
1616
|
+
const fs = getFs(this);
|
|
1617
|
+
fs.rmdirSync(pathString);
|
|
1618
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1619
|
+
});
|
|
1620
|
+
this.path_rename = syscallWrap('path_rename', function (old_fd, old_path, old_path_len, new_fd, new_path, new_path_len) {
|
|
1621
|
+
old_path = Number(old_path);
|
|
1622
|
+
old_path_len = Number(old_path_len);
|
|
1623
|
+
new_path = Number(new_path);
|
|
1624
|
+
new_path_len = Number(new_path_len);
|
|
1625
|
+
if (old_path === 0 || new_path === 0) {
|
|
1626
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1627
|
+
}
|
|
1628
|
+
const wasi = _wasi.get(this);
|
|
1629
|
+
let oldWrap;
|
|
1630
|
+
let newWrap;
|
|
1631
|
+
if (old_fd === new_fd) {
|
|
1632
|
+
oldWrap = newWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE | WasiRights.PATH_RENAME_TARGET, BigInt(0));
|
|
1633
|
+
}
|
|
1634
|
+
else {
|
|
1635
|
+
oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE, BigInt(0));
|
|
1636
|
+
newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
|
|
1637
|
+
}
|
|
1638
|
+
const { HEAPU8 } = getMemory(this);
|
|
1639
|
+
const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)));
|
|
1640
|
+
const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
|
|
1641
|
+
const fs = getFs(this);
|
|
1642
|
+
fs.renameSync(resolvedOldPath, resolvedNewPath);
|
|
1643
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1644
|
+
});
|
|
1645
|
+
this.path_symlink = syscallWrap('path_symlink', function (old_path, old_path_len, fd, new_path, new_path_len) {
|
|
1646
|
+
old_path = Number(old_path);
|
|
1647
|
+
old_path_len = Number(old_path_len);
|
|
1648
|
+
new_path = Number(new_path);
|
|
1649
|
+
new_path_len = Number(new_path_len);
|
|
1650
|
+
if (old_path === 0 || new_path === 0) {
|
|
1651
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1652
|
+
}
|
|
1653
|
+
const { HEAPU8 } = getMemory(this);
|
|
1654
|
+
const wasi = _wasi.get(this);
|
|
1655
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
|
|
1656
|
+
const oldPath = decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len));
|
|
1657
|
+
let newPath = decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len));
|
|
1658
|
+
newPath = resolve(fileDescriptor.realPath, newPath);
|
|
1659
|
+
const fs = getFs(this);
|
|
1660
|
+
fs.symlinkSync(oldPath, newPath);
|
|
1661
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1662
|
+
});
|
|
1663
|
+
this.path_unlink_file = syscallWrap('path_unlink_file', function (fd, path, path_len) {
|
|
1664
|
+
path = Number(path);
|
|
1665
|
+
path_len = Number(path_len);
|
|
1666
|
+
if (path === 0) {
|
|
1667
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1668
|
+
}
|
|
1669
|
+
const { HEAPU8 } = getMemory(this);
|
|
1670
|
+
const wasi = _wasi.get(this);
|
|
1671
|
+
const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
|
|
1672
|
+
let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
|
|
1673
|
+
pathString = resolve(fileDescriptor.realPath, pathString);
|
|
1674
|
+
const fs = getFs(this);
|
|
1675
|
+
fs.unlinkSync(pathString);
|
|
1676
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1677
|
+
});
|
|
1678
|
+
this.poll_oneoff = syscallWrap('poll_oneoff', function (_in, _out, _sub, _nevents) {
|
|
1679
|
+
return 52 /* WasiErrno.ENOSYS */;
|
|
1680
|
+
});
|
|
1681
|
+
this.proc_exit = syscallWrap('proc_exit', function (_rval) {
|
|
1682
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1683
|
+
});
|
|
1684
|
+
this.proc_raise = syscallWrap('proc_raise', function (_sig) {
|
|
1685
|
+
return 52 /* WasiErrno.ENOSYS */;
|
|
1686
|
+
});
|
|
1687
|
+
this.sched_yield = syscallWrap('sched_yield', function () {
|
|
1688
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1689
|
+
});
|
|
1690
|
+
this.random_get = typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function'
|
|
1691
|
+
? syscallWrap('random_get', function (buf, buf_len) {
|
|
1692
|
+
buf = Number(buf);
|
|
1693
|
+
if (buf === 0) {
|
|
1694
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1695
|
+
}
|
|
1696
|
+
buf_len = Number(buf_len);
|
|
1697
|
+
const { HEAPU8 } = getMemory(this);
|
|
1698
|
+
let pos;
|
|
1699
|
+
const stride = 65536;
|
|
1700
|
+
for (pos = 0; pos + stride < buf_len; pos += stride) {
|
|
1701
|
+
crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + pos + stride));
|
|
1702
|
+
}
|
|
1703
|
+
crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + buf_len));
|
|
1704
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1705
|
+
})
|
|
1706
|
+
: syscallWrap('random_get', function (buf, buf_len) {
|
|
1707
|
+
buf = Number(buf);
|
|
1708
|
+
if (buf === 0) {
|
|
1709
|
+
return 28 /* WasiErrno.EINVAL */;
|
|
1710
|
+
}
|
|
1711
|
+
buf_len = Number(buf_len);
|
|
1712
|
+
const { view } = getMemory(this);
|
|
1713
|
+
for (let i = buf; i < buf + buf_len; ++i) {
|
|
1714
|
+
view.setUint8(i, Math.floor(Math.random() * 256));
|
|
1715
|
+
}
|
|
1716
|
+
return 0 /* WasiErrno.ESUCCESS */;
|
|
1717
|
+
});
|
|
1718
|
+
this.sock_recv = syscallWrap('sock_recv', function () {
|
|
1719
|
+
return 58 /* WasiErrno.ENOTSUP */;
|
|
1720
|
+
});
|
|
1721
|
+
this.sock_send = syscallWrap('sock_send', function () {
|
|
1722
|
+
return 58 /* WasiErrno.ENOTSUP */;
|
|
1723
|
+
});
|
|
1724
|
+
this.sock_shutdown = syscallWrap('sock_shutdown', function () {
|
|
1725
|
+
return 58 /* WasiErrno.ENOTSUP */;
|
|
1726
|
+
});
|
|
1727
|
+
const fs = filesystem ? filesystem.fs : undefined;
|
|
1728
|
+
const fds = new FileDescriptorTable({
|
|
1729
|
+
size: 3,
|
|
1730
|
+
in: stdio[0],
|
|
1731
|
+
out: stdio[1],
|
|
1732
|
+
err: stdio[2],
|
|
1733
|
+
fs,
|
|
1734
|
+
print,
|
|
1735
|
+
printErr
|
|
1736
|
+
});
|
|
1737
|
+
_wasi.set(this, {
|
|
1738
|
+
fds,
|
|
1739
|
+
args,
|
|
1740
|
+
env
|
|
1741
|
+
});
|
|
1742
|
+
if (fs)
|
|
1743
|
+
_fs.set(this, fs);
|
|
1744
|
+
if (preopens.length > 0) {
|
|
1745
|
+
for (let i = 0; i < preopens.length; ++i) {
|
|
1746
|
+
const realPath = fs.realpathSync(preopens[i].realPath, 'utf8');
|
|
1747
|
+
const fd = fs.openSync(realPath, 'r', 0o666);
|
|
1748
|
+
fds.insertPreopen(fd, preopens[i].mappedPath, realPath);
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
const kEmptyObject = Object.freeze(Object.create(null));
|
|
1755
|
+
const kExitCode = Symbol('kExitCode');
|
|
1756
|
+
const kSetMemory = Symbol('kSetMemory');
|
|
1757
|
+
const kStarted = Symbol('kStarted');
|
|
1758
|
+
const kInstance = Symbol('kInstance');
|
|
1759
|
+
function setupInstance(self, instance) {
|
|
1760
|
+
validateObject(instance, 'instance');
|
|
1761
|
+
validateObject(instance.exports, 'instance.exports');
|
|
1762
|
+
self[kInstance] = instance;
|
|
1763
|
+
self[kSetMemory](instance.exports.memory);
|
|
1764
|
+
}
|
|
1765
|
+
// /** @public */
|
|
1766
|
+
// export type WasiSnapshotPreview1 = Omit<_WASI, '_setMemory'>
|
|
1767
|
+
/** @public */
|
|
1768
|
+
class WASI {
|
|
1769
|
+
constructor(options = kEmptyObject) {
|
|
1770
|
+
var _a;
|
|
1771
|
+
validateObject(options, 'options');
|
|
1772
|
+
if (options.args !== undefined) {
|
|
1773
|
+
validateArray(options.args, 'options.args');
|
|
1774
|
+
}
|
|
1775
|
+
const args = ((_a = options.args) !== null && _a !== void 0 ? _a : []).map(String);
|
|
1776
|
+
const env = [];
|
|
1777
|
+
if (options.env !== undefined) {
|
|
1778
|
+
validateObject(options.env, 'options.env');
|
|
1779
|
+
Object.entries(options.env).forEach(({ 0: key, 1: value }) => {
|
|
1780
|
+
if (value !== undefined) {
|
|
1781
|
+
env.push(`${key}=${value}`);
|
|
1782
|
+
}
|
|
1783
|
+
});
|
|
1784
|
+
}
|
|
1785
|
+
const preopens = [];
|
|
1786
|
+
if (options.preopens !== undefined) {
|
|
1787
|
+
validateObject(options.preopens, 'options.preopens');
|
|
1788
|
+
Object.entries(options.preopens).forEach(({ 0: key, 1: value }) => preopens.push({ mappedPath: String(key), realPath: String(value) }));
|
|
1789
|
+
}
|
|
1790
|
+
if (preopens.length > 0) {
|
|
1791
|
+
if (options.filesystem === undefined) {
|
|
1792
|
+
throw new Error('filesystem is disabled, can not preopen directory');
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
if (options.filesystem !== undefined) {
|
|
1796
|
+
validateObject(options.filesystem, 'options.filesystem');
|
|
1797
|
+
validateString(options.filesystem.type, 'options.filesystem.type');
|
|
1798
|
+
if (options.filesystem.type !== 'memfs') {
|
|
1799
|
+
throw new Error(`Filesystem type ${options.filesystem.type} is not supported, only "memfs" is supported currently`);
|
|
1800
|
+
}
|
|
1801
|
+
try {
|
|
1802
|
+
validateObject(options.filesystem.fs, 'options.filesystem.fs');
|
|
1803
|
+
}
|
|
1804
|
+
catch (_) {
|
|
1805
|
+
throw new Error('Node.js fs like implementation is not provided');
|
|
1806
|
+
}
|
|
1807
|
+
}
|
|
1808
|
+
if (options.print !== undefined)
|
|
1809
|
+
validateFunction(options.print, 'options.print');
|
|
1810
|
+
if (options.printErr !== undefined)
|
|
1811
|
+
validateFunction(options.printErr, 'options.printErr');
|
|
1812
|
+
// const { stdin = 0, stdout = 1, stderr = 2 } = options
|
|
1813
|
+
// validateInt32(stdin, 'options.stdin', 0)
|
|
1814
|
+
// validateInt32(stdout, 'options.stdout', 0)
|
|
1815
|
+
// validateInt32(stderr, 'options.stderr', 0)
|
|
1816
|
+
// const stdio = [stdin, stdout, stderr] as const
|
|
1817
|
+
const stdio = [0, 1, 2];
|
|
1818
|
+
const wrap = new WASI$1(args, env, preopens, stdio, options.filesystem, options.print, options.printErr);
|
|
1819
|
+
for (const prop in wrap) {
|
|
1820
|
+
wrap[prop] = wrap[prop].bind(wrap);
|
|
1821
|
+
}
|
|
1822
|
+
if (options.returnOnExit !== undefined) {
|
|
1823
|
+
validateBoolean(options.returnOnExit, 'options.returnOnExit');
|
|
1824
|
+
if (options.returnOnExit) {
|
|
1825
|
+
wrap.proc_exit = wasiReturnOnProcExit.bind(this);
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
this[kSetMemory] = wrap._setMemory;
|
|
1829
|
+
delete wrap._setMemory;
|
|
1830
|
+
this.wasiImport = wrap;
|
|
1831
|
+
this[kStarted] = false;
|
|
1832
|
+
this[kExitCode] = 0;
|
|
1833
|
+
this[kInstance] = undefined;
|
|
1834
|
+
}
|
|
1835
|
+
// Must not export _initialize, must export _start
|
|
1836
|
+
start(instance) {
|
|
1837
|
+
if (this[kStarted]) {
|
|
1838
|
+
throw new Error('WASI instance has already started');
|
|
1839
|
+
}
|
|
1840
|
+
this[kStarted] = true;
|
|
1841
|
+
setupInstance(this, instance);
|
|
1842
|
+
const { _start, _initialize } = this[kInstance].exports;
|
|
1843
|
+
validateFunction(_start, 'instance.exports._start');
|
|
1844
|
+
validateUndefined(_initialize, 'instance.exports._initialize');
|
|
1845
|
+
let ret;
|
|
1846
|
+
try {
|
|
1847
|
+
ret = _start();
|
|
1848
|
+
}
|
|
1849
|
+
catch (err) {
|
|
1850
|
+
if (err !== kExitCode) {
|
|
1851
|
+
throw err;
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
if (ret instanceof Promise) {
|
|
1855
|
+
return ret.then(() => this[kExitCode], (err) => {
|
|
1856
|
+
if (err !== kExitCode) {
|
|
1857
|
+
throw err;
|
|
1858
|
+
}
|
|
1859
|
+
return this[kExitCode];
|
|
1860
|
+
});
|
|
1861
|
+
}
|
|
1862
|
+
return this[kExitCode];
|
|
1863
|
+
}
|
|
1864
|
+
// Must not export _start, may optionally export _initialize
|
|
1865
|
+
initialize(instance) {
|
|
1866
|
+
if (this[kStarted]) {
|
|
1867
|
+
throw new Error('WASI instance has already started');
|
|
1868
|
+
}
|
|
1869
|
+
this[kStarted] = true;
|
|
1870
|
+
setupInstance(this, instance);
|
|
1871
|
+
const { _start, _initialize } = this[kInstance].exports;
|
|
1872
|
+
validateUndefined(_start, 'instance.exports._start');
|
|
1873
|
+
if (_initialize !== undefined) {
|
|
1874
|
+
validateFunction(_initialize, 'instance.exports._initialize');
|
|
1875
|
+
return _initialize();
|
|
1876
|
+
}
|
|
1877
|
+
}
|
|
1878
|
+
}
|
|
1879
|
+
function wasiReturnOnProcExit(rval) {
|
|
1880
|
+
this[kExitCode] = rval;
|
|
1881
|
+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
1882
|
+
throw kExitCode;
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
exports.Asyncify = Asyncify;
|
|
1886
|
+
exports.Memory = Memory;
|
|
1887
|
+
exports.WASI = WASI;
|
|
1888
|
+
exports.extendMemory = extendMemory;
|
|
1889
|
+
exports.load = load;
|
|
1890
|
+
exports.loadSync = loadSync;
|
|
1891
|
+
|
|
1892
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
1893
|
+
|
|
1894
|
+
}));
|