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