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