@tybys/wasm-util 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +12 -6
  4. package/dist/wasm-util.esm-bundler.js +2639 -2607
  5. package/dist/wasm-util.esm.js +2639 -2607
  6. package/dist/wasm-util.esm.min.js +1 -1
  7. package/dist/wasm-util.js +2639 -2606
  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 +96 -85
  13. package/lib/cjs/memfs.js +2689 -0
  14. package/lib/cjs/memory.js +34 -34
  15. package/lib/cjs/wasi/error.js +102 -102
  16. package/lib/cjs/wasi/fd.js +267 -267
  17. package/lib/cjs/wasi/fs.js +2 -2
  18. package/lib/cjs/wasi/index.js +193 -171
  19. package/lib/cjs/wasi/path.js +173 -173
  20. package/lib/cjs/wasi/preview1.js +1478 -1478
  21. package/lib/cjs/wasi/rights.js +139 -139
  22. package/lib/cjs/wasi/types.js +219 -219
  23. package/lib/cjs/wasi/util.js +121 -121
  24. package/lib/cjs/webassembly.js +12 -12
  25. package/lib/mjs/asyncify.mjs +153 -153
  26. package/lib/mjs/index.mjs +9 -9
  27. package/lib/mjs/jspi.mjs +39 -39
  28. package/lib/mjs/load.mjs +89 -78
  29. package/lib/mjs/memfs.mjs +2688 -0
  30. package/lib/mjs/memory.mjs +28 -28
  31. package/lib/mjs/wasi/error.mjs +97 -97
  32. package/lib/mjs/wasi/fd.mjs +256 -256
  33. package/lib/mjs/wasi/fs.mjs +1 -1
  34. package/lib/mjs/wasi/index.mjs +188 -167
  35. package/lib/mjs/wasi/path.mjs +168 -168
  36. package/lib/mjs/wasi/preview1.mjs +1474 -1474
  37. package/lib/mjs/wasi/rights.mjs +134 -134
  38. package/lib/mjs/wasi/types.mjs +216 -216
  39. package/lib/mjs/wasi/util.mjs +108 -108
  40. package/lib/mjs/webassembly.mjs +9 -9
  41. package/package.json +58 -58
@@ -1,268 +1,268 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AsyncTable = exports.SyncTable = exports.FileDescriptorTable = exports.toFileStat = exports.toFileType = exports.StandardOutput = exports.FileDescriptor = exports.concatBuffer = void 0;
4
- const types_1 = require("./types");
5
- const rights_1 = require("./rights");
6
- const error_1 = require("./error");
7
- function concatBuffer(buffers, size) {
8
- let total = 0;
9
- if (typeof size === 'number' && size >= 0) {
10
- total = size;
11
- }
12
- else {
13
- for (let i = 0; i < buffers.length; i++) {
14
- const buffer = buffers[i];
15
- total += buffer.length;
16
- }
17
- }
18
- let pos = 0;
19
- const ret = new Uint8Array(total);
20
- for (let i = 0; i < buffers.length; i++) {
21
- const buffer = buffers[i];
22
- ret.set(buffer, pos);
23
- pos += buffer.length;
24
- }
25
- return ret;
26
- }
27
- exports.concatBuffer = concatBuffer;
28
- class FileDescriptor {
29
- constructor(id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen) {
30
- this.id = id;
31
- this.fd = fd;
32
- this.path = path;
33
- this.realPath = realPath;
34
- this.type = type;
35
- this.rightsBase = rightsBase;
36
- this.rightsInheriting = rightsInheriting;
37
- this.preopen = preopen;
38
- this.pos = BigInt(0);
39
- this.size = BigInt(0);
40
- }
41
- seek(offset, whence) {
42
- if (whence === types_1.WasiWhence.SET) {
43
- this.pos = BigInt(offset);
44
- }
45
- else if (whence === types_1.WasiWhence.CUR) {
46
- this.pos += BigInt(offset);
47
- }
48
- else if (whence === types_1.WasiWhence.END) {
49
- this.pos = BigInt(this.size) - BigInt(offset);
50
- }
51
- else {
52
- throw new error_1.WasiError('Unknown whence', types_1.WasiErrno.EIO);
53
- }
54
- return this.pos;
55
- }
56
- }
57
- exports.FileDescriptor = FileDescriptor;
58
- class StandardOutput extends FileDescriptor {
59
- constructor(log, id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen) {
60
- super(id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen);
61
- this._log = log;
62
- this._buf = null;
63
- }
64
- write(buffer) {
65
- const originalBuffer = buffer;
66
- if (this._buf) {
67
- buffer = concatBuffer([this._buf, buffer]);
68
- this._buf = null;
69
- }
70
- if (buffer.indexOf(10) === -1) {
71
- this._buf = buffer;
72
- return originalBuffer.byteLength;
73
- }
74
- let written = 0;
75
- let lastBegin = 0;
76
- let index;
77
- while ((index = buffer.indexOf(10, written)) !== -1) {
78
- const str = new TextDecoder().decode(buffer.subarray(lastBegin, index));
79
- this._log(str);
80
- written += index - lastBegin + 1;
81
- lastBegin = index + 1;
82
- }
83
- if (written < buffer.length) {
84
- this._buf = buffer.slice(written);
85
- }
86
- return originalBuffer.byteLength;
87
- }
88
- }
89
- exports.StandardOutput = StandardOutput;
90
- function toFileType(stat) {
91
- if (stat.isBlockDevice())
92
- return types_1.WasiFileType.BLOCK_DEVICE;
93
- if (stat.isCharacterDevice())
94
- return types_1.WasiFileType.CHARACTER_DEVICE;
95
- if (stat.isDirectory())
96
- return types_1.WasiFileType.DIRECTORY;
97
- if (stat.isSocket())
98
- return types_1.WasiFileType.SOCKET_STREAM;
99
- if (stat.isFile())
100
- return types_1.WasiFileType.REGULAR_FILE;
101
- if (stat.isSymbolicLink())
102
- return types_1.WasiFileType.SYMBOLIC_LINK;
103
- return types_1.WasiFileType.UNKNOWN;
104
- }
105
- exports.toFileType = toFileType;
106
- function toFileStat(view, buf, stat) {
107
- view.setBigUint64(buf, stat.dev, true);
108
- view.setBigUint64(buf + 8, stat.ino, true);
109
- view.setBigUint64(buf + 16, BigInt(toFileType(stat)), true);
110
- view.setBigUint64(buf + 24, stat.nlink, true);
111
- view.setBigUint64(buf + 32, stat.size, true);
112
- view.setBigUint64(buf + 40, stat.atimeMs * BigInt(1000000), true);
113
- view.setBigUint64(buf + 48, stat.mtimeMs * BigInt(1000000), true);
114
- view.setBigUint64(buf + 56, stat.ctimeMs * BigInt(1000000), true);
115
- }
116
- exports.toFileStat = toFileStat;
117
- class FileDescriptorTable {
118
- constructor(options) {
119
- this.used = 0;
120
- this.size = options.size;
121
- this.fds = Array(options.size);
122
- this.stdio = [options.in, options.out, options.err];
123
- this.print = options.print;
124
- this.printErr = options.printErr;
125
- this.insertStdio(options.in, 0, '<stdin>');
126
- this.insertStdio(options.out, 1, '<stdout>');
127
- this.insertStdio(options.err, 2, '<stderr>');
128
- }
129
- insertStdio(fd, expected, name) {
130
- const type = types_1.WasiFileType.CHARACTER_DEVICE;
131
- const { base, inheriting } = (0, rights_1.getRights)(this.stdio, fd, types_1.FileControlFlag.O_RDWR, type);
132
- const wrap = this.insert(fd, name, name, type, base, inheriting, 0);
133
- if (wrap.id !== expected) {
134
- throw new error_1.WasiError(`id: ${wrap.id} !== expected: ${expected}`, types_1.WasiErrno.EBADF);
135
- }
136
- return wrap;
137
- }
138
- insert(fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen) {
139
- var _a, _b;
140
- let index = -1;
141
- if (this.used >= this.size) {
142
- const newSize = this.size * 2;
143
- this.fds.length = newSize;
144
- index = this.size;
145
- this.size = newSize;
146
- }
147
- else {
148
- for (let i = 0; i < this.size; ++i) {
149
- if (this.fds[i] == null) {
150
- index = i;
151
- break;
152
- }
153
- }
154
- }
155
- let entry;
156
- if (mappedPath === '<stdout>') {
157
- entry = new StandardOutput((_a = this.print) !== null && _a !== void 0 ? _a : console.log, index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
158
- }
159
- else if (mappedPath === '<stderr>') {
160
- entry = new StandardOutput((_b = this.printErr) !== null && _b !== void 0 ? _b : console.error, index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
161
- }
162
- else {
163
- entry = new FileDescriptor(index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
164
- }
165
- this.fds[index] = entry;
166
- this.used++;
167
- return entry;
168
- }
169
- get(id, base, inheriting) {
170
- if (id >= this.size) {
171
- throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
172
- }
173
- const entry = this.fds[id];
174
- if (!entry || entry.id !== id) {
175
- throw new error_1.WasiError('Bad file descriptor', types_1.WasiErrno.EBADF);
176
- }
177
- /* Validate that the fd has the necessary rights. */
178
- if ((~entry.rightsBase & base) !== BigInt(0) || (~entry.rightsInheriting & inheriting) !== BigInt(0)) {
179
- throw new error_1.WasiError('Capabilities insufficient', types_1.WasiErrno.ENOTCAPABLE);
180
- }
181
- return entry;
182
- }
183
- remove(id) {
184
- if (id >= this.size) {
185
- throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
186
- }
187
- const entry = this.fds[id];
188
- if (!entry || entry.id !== id) {
189
- throw new error_1.WasiError('Bad file descriptor', types_1.WasiErrno.EBADF);
190
- }
191
- this.fds[id] = undefined;
192
- this.used--;
193
- }
194
- }
195
- exports.FileDescriptorTable = FileDescriptorTable;
196
- class SyncTable extends FileDescriptorTable {
197
- constructor(options) {
198
- super(options);
199
- this.fs = options.fs;
200
- }
201
- getFileTypeByFd(fd) {
202
- const stats = this.fs.fstatSync(fd, { bigint: true });
203
- return toFileType(stats);
204
- }
205
- insertPreopen(fd, mappedPath, realPath) {
206
- const type = this.getFileTypeByFd(fd);
207
- if (type !== types_1.WasiFileType.DIRECTORY) {
208
- throw new error_1.WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, types_1.WasiErrno.ENOTDIR);
209
- }
210
- const result = (0, rights_1.getRights)(this.stdio, fd, 0, type);
211
- return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
212
- }
213
- renumber(dst, src) {
214
- if (dst === src)
215
- return;
216
- if (dst >= this.size || src >= this.size) {
217
- throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
218
- }
219
- const dstEntry = this.fds[dst];
220
- const srcEntry = this.fds[src];
221
- if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
222
- throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
223
- }
224
- this.fs.closeSync(dstEntry.fd);
225
- this.fds[dst] = this.fds[src];
226
- this.fds[dst].id = dst;
227
- this.fds[src] = undefined;
228
- this.used--;
229
- }
230
- }
231
- exports.SyncTable = SyncTable;
232
- class AsyncTable extends FileDescriptorTable {
233
- // eslint-disable-next-line @typescript-eslint/no-useless-constructor
234
- constructor(options) {
235
- super(options);
236
- }
237
- async getFileTypeByFd(fd) {
238
- const stats = await fd.stat({ bigint: true });
239
- return toFileType(stats);
240
- }
241
- async insertPreopen(fd, mappedPath, realPath) {
242
- const type = await this.getFileTypeByFd(fd);
243
- if (type !== types_1.WasiFileType.DIRECTORY) {
244
- throw new error_1.WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, types_1.WasiErrno.ENOTDIR);
245
- }
246
- const result = (0, rights_1.getRights)(this.stdio, fd.fd, 0, type);
247
- return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
248
- }
249
- async renumber(dst, src) {
250
- if (dst === src)
251
- return;
252
- if (dst >= this.size || src >= this.size) {
253
- throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
254
- }
255
- const dstEntry = this.fds[dst];
256
- const srcEntry = this.fds[src];
257
- if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
258
- throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
259
- }
260
- await dstEntry.fd.close();
261
- this.fds[dst] = this.fds[src];
262
- this.fds[dst].id = dst;
263
- this.fds[src] = undefined;
264
- this.used--;
265
- }
266
- }
267
- exports.AsyncTable = AsyncTable;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AsyncTable = exports.SyncTable = exports.FileDescriptorTable = exports.toFileStat = exports.toFileType = exports.StandardOutput = exports.FileDescriptor = exports.concatBuffer = void 0;
4
+ const types_1 = require("./types");
5
+ const rights_1 = require("./rights");
6
+ const error_1 = require("./error");
7
+ function concatBuffer(buffers, size) {
8
+ let total = 0;
9
+ if (typeof size === 'number' && size >= 0) {
10
+ total = size;
11
+ }
12
+ else {
13
+ for (let i = 0; i < buffers.length; i++) {
14
+ const buffer = buffers[i];
15
+ total += buffer.length;
16
+ }
17
+ }
18
+ let pos = 0;
19
+ const ret = new Uint8Array(total);
20
+ for (let i = 0; i < buffers.length; i++) {
21
+ const buffer = buffers[i];
22
+ ret.set(buffer, pos);
23
+ pos += buffer.length;
24
+ }
25
+ return ret;
26
+ }
27
+ exports.concatBuffer = concatBuffer;
28
+ class FileDescriptor {
29
+ constructor(id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen) {
30
+ this.id = id;
31
+ this.fd = fd;
32
+ this.path = path;
33
+ this.realPath = realPath;
34
+ this.type = type;
35
+ this.rightsBase = rightsBase;
36
+ this.rightsInheriting = rightsInheriting;
37
+ this.preopen = preopen;
38
+ this.pos = BigInt(0);
39
+ this.size = BigInt(0);
40
+ }
41
+ seek(offset, whence) {
42
+ if (whence === types_1.WasiWhence.SET) {
43
+ this.pos = BigInt(offset);
44
+ }
45
+ else if (whence === types_1.WasiWhence.CUR) {
46
+ this.pos += BigInt(offset);
47
+ }
48
+ else if (whence === types_1.WasiWhence.END) {
49
+ this.pos = BigInt(this.size) - BigInt(offset);
50
+ }
51
+ else {
52
+ throw new error_1.WasiError('Unknown whence', types_1.WasiErrno.EIO);
53
+ }
54
+ return this.pos;
55
+ }
56
+ }
57
+ exports.FileDescriptor = FileDescriptor;
58
+ class StandardOutput extends FileDescriptor {
59
+ constructor(log, id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen) {
60
+ super(id, fd, path, realPath, type, rightsBase, rightsInheriting, preopen);
61
+ this._log = log;
62
+ this._buf = null;
63
+ }
64
+ write(buffer) {
65
+ const originalBuffer = buffer;
66
+ if (this._buf) {
67
+ buffer = concatBuffer([this._buf, buffer]);
68
+ this._buf = null;
69
+ }
70
+ if (buffer.indexOf(10) === -1) {
71
+ this._buf = buffer;
72
+ return originalBuffer.byteLength;
73
+ }
74
+ let written = 0;
75
+ let lastBegin = 0;
76
+ let index;
77
+ while ((index = buffer.indexOf(10, written)) !== -1) {
78
+ const str = new TextDecoder().decode(buffer.subarray(lastBegin, index));
79
+ this._log(str);
80
+ written += index - lastBegin + 1;
81
+ lastBegin = index + 1;
82
+ }
83
+ if (written < buffer.length) {
84
+ this._buf = buffer.slice(written);
85
+ }
86
+ return originalBuffer.byteLength;
87
+ }
88
+ }
89
+ exports.StandardOutput = StandardOutput;
90
+ function toFileType(stat) {
91
+ if (stat.isBlockDevice())
92
+ return types_1.WasiFileType.BLOCK_DEVICE;
93
+ if (stat.isCharacterDevice())
94
+ return types_1.WasiFileType.CHARACTER_DEVICE;
95
+ if (stat.isDirectory())
96
+ return types_1.WasiFileType.DIRECTORY;
97
+ if (stat.isSocket())
98
+ return types_1.WasiFileType.SOCKET_STREAM;
99
+ if (stat.isFile())
100
+ return types_1.WasiFileType.REGULAR_FILE;
101
+ if (stat.isSymbolicLink())
102
+ return types_1.WasiFileType.SYMBOLIC_LINK;
103
+ return types_1.WasiFileType.UNKNOWN;
104
+ }
105
+ exports.toFileType = toFileType;
106
+ function toFileStat(view, buf, stat) {
107
+ view.setBigUint64(buf, stat.dev, true);
108
+ view.setBigUint64(buf + 8, stat.ino, true);
109
+ view.setBigUint64(buf + 16, BigInt(toFileType(stat)), true);
110
+ view.setBigUint64(buf + 24, stat.nlink, true);
111
+ view.setBigUint64(buf + 32, stat.size, true);
112
+ view.setBigUint64(buf + 40, stat.atimeMs * BigInt(1000000), true);
113
+ view.setBigUint64(buf + 48, stat.mtimeMs * BigInt(1000000), true);
114
+ view.setBigUint64(buf + 56, stat.ctimeMs * BigInt(1000000), true);
115
+ }
116
+ exports.toFileStat = toFileStat;
117
+ class FileDescriptorTable {
118
+ constructor(options) {
119
+ this.used = 0;
120
+ this.size = options.size;
121
+ this.fds = Array(options.size);
122
+ this.stdio = [options.in, options.out, options.err];
123
+ this.print = options.print;
124
+ this.printErr = options.printErr;
125
+ this.insertStdio(options.in, 0, '<stdin>');
126
+ this.insertStdio(options.out, 1, '<stdout>');
127
+ this.insertStdio(options.err, 2, '<stderr>');
128
+ }
129
+ insertStdio(fd, expected, name) {
130
+ const type = types_1.WasiFileType.CHARACTER_DEVICE;
131
+ const { base, inheriting } = (0, rights_1.getRights)(this.stdio, fd, types_1.FileControlFlag.O_RDWR, type);
132
+ const wrap = this.insert(fd, name, name, type, base, inheriting, 0);
133
+ if (wrap.id !== expected) {
134
+ throw new error_1.WasiError(`id: ${wrap.id} !== expected: ${expected}`, types_1.WasiErrno.EBADF);
135
+ }
136
+ return wrap;
137
+ }
138
+ insert(fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen) {
139
+ var _a, _b;
140
+ let index = -1;
141
+ if (this.used >= this.size) {
142
+ const newSize = this.size * 2;
143
+ this.fds.length = newSize;
144
+ index = this.size;
145
+ this.size = newSize;
146
+ }
147
+ else {
148
+ for (let i = 0; i < this.size; ++i) {
149
+ if (this.fds[i] == null) {
150
+ index = i;
151
+ break;
152
+ }
153
+ }
154
+ }
155
+ let entry;
156
+ if (mappedPath === '<stdout>') {
157
+ entry = new StandardOutput((_a = this.print) !== null && _a !== void 0 ? _a : console.log, index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
158
+ }
159
+ else if (mappedPath === '<stderr>') {
160
+ entry = new StandardOutput((_b = this.printErr) !== null && _b !== void 0 ? _b : console.error, index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
161
+ }
162
+ else {
163
+ entry = new FileDescriptor(index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
164
+ }
165
+ this.fds[index] = entry;
166
+ this.used++;
167
+ return entry;
168
+ }
169
+ get(id, base, inheriting) {
170
+ if (id >= this.size) {
171
+ throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
172
+ }
173
+ const entry = this.fds[id];
174
+ if (!entry || entry.id !== id) {
175
+ throw new error_1.WasiError('Bad file descriptor', types_1.WasiErrno.EBADF);
176
+ }
177
+ /* Validate that the fd has the necessary rights. */
178
+ if ((~entry.rightsBase & base) !== BigInt(0) || (~entry.rightsInheriting & inheriting) !== BigInt(0)) {
179
+ throw new error_1.WasiError('Capabilities insufficient', types_1.WasiErrno.ENOTCAPABLE);
180
+ }
181
+ return entry;
182
+ }
183
+ remove(id) {
184
+ if (id >= this.size) {
185
+ throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
186
+ }
187
+ const entry = this.fds[id];
188
+ if (!entry || entry.id !== id) {
189
+ throw new error_1.WasiError('Bad file descriptor', types_1.WasiErrno.EBADF);
190
+ }
191
+ this.fds[id] = undefined;
192
+ this.used--;
193
+ }
194
+ }
195
+ exports.FileDescriptorTable = FileDescriptorTable;
196
+ class SyncTable extends FileDescriptorTable {
197
+ constructor(options) {
198
+ super(options);
199
+ this.fs = options.fs;
200
+ }
201
+ getFileTypeByFd(fd) {
202
+ const stats = this.fs.fstatSync(fd, { bigint: true });
203
+ return toFileType(stats);
204
+ }
205
+ insertPreopen(fd, mappedPath, realPath) {
206
+ const type = this.getFileTypeByFd(fd);
207
+ if (type !== types_1.WasiFileType.DIRECTORY) {
208
+ throw new error_1.WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, types_1.WasiErrno.ENOTDIR);
209
+ }
210
+ const result = (0, rights_1.getRights)(this.stdio, fd, 0, type);
211
+ return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
212
+ }
213
+ renumber(dst, src) {
214
+ if (dst === src)
215
+ return;
216
+ if (dst >= this.size || src >= this.size) {
217
+ throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
218
+ }
219
+ const dstEntry = this.fds[dst];
220
+ const srcEntry = this.fds[src];
221
+ if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
222
+ throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
223
+ }
224
+ this.fs.closeSync(dstEntry.fd);
225
+ this.fds[dst] = this.fds[src];
226
+ this.fds[dst].id = dst;
227
+ this.fds[src] = undefined;
228
+ this.used--;
229
+ }
230
+ }
231
+ exports.SyncTable = SyncTable;
232
+ class AsyncTable extends FileDescriptorTable {
233
+ // eslint-disable-next-line @typescript-eslint/no-useless-constructor
234
+ constructor(options) {
235
+ super(options);
236
+ }
237
+ async getFileTypeByFd(fd) {
238
+ const stats = await fd.stat({ bigint: true });
239
+ return toFileType(stats);
240
+ }
241
+ async insertPreopen(fd, mappedPath, realPath) {
242
+ const type = await this.getFileTypeByFd(fd);
243
+ if (type !== types_1.WasiFileType.DIRECTORY) {
244
+ throw new error_1.WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, types_1.WasiErrno.ENOTDIR);
245
+ }
246
+ const result = (0, rights_1.getRights)(this.stdio, fd.fd, 0, type);
247
+ return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
248
+ }
249
+ async renumber(dst, src) {
250
+ if (dst === src)
251
+ return;
252
+ if (dst >= this.size || src >= this.size) {
253
+ throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
254
+ }
255
+ const dstEntry = this.fds[dst];
256
+ const srcEntry = this.fds[src];
257
+ if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
258
+ throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
259
+ }
260
+ await dstEntry.fd.close();
261
+ this.fds[dst] = this.fds[src];
262
+ this.fds[dst].id = dst;
263
+ this.fds[src] = undefined;
264
+ this.used--;
265
+ }
266
+ }
267
+ exports.AsyncTable = AsyncTable;
268
268
  //# sourceMappingURL=fd.js.map
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=fs.js.map