@tybys/wasm-util 0.8.0 → 0.8.1

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