@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.
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extendMemory = exports.Memory = void 0;
4
+ /** @public */
5
+ class Memory extends WebAssembly.Memory {
6
+ // eslint-disable-next-line @typescript-eslint/no-useless-constructor
7
+ constructor(descriptor) {
8
+ super(descriptor);
9
+ }
10
+ get HEAP8() { return new Int8Array(super.buffer); }
11
+ get HEAPU8() { return new Uint8Array(super.buffer); }
12
+ get HEAP16() { return new Int16Array(super.buffer); }
13
+ get HEAPU16() { return new Uint16Array(super.buffer); }
14
+ get HEAP32() { return new Int32Array(super.buffer); }
15
+ get HEAPU32() { return new Uint32Array(super.buffer); }
16
+ get HEAP64() { return new BigInt64Array(super.buffer); }
17
+ get HEAPU64() { return new BigUint64Array(super.buffer); }
18
+ get HEAPF32() { return new Float32Array(super.buffer); }
19
+ get HEAPF64() { return new Float64Array(super.buffer); }
20
+ get view() { return new DataView(super.buffer); }
21
+ }
22
+ exports.Memory = Memory;
23
+ /** @public */
24
+ function extendMemory(memory) {
25
+ if (Object.getPrototypeOf(memory) === WebAssembly.Memory.prototype) {
26
+ Object.setPrototypeOf(memory, Memory.prototype);
27
+ }
28
+ return memory;
29
+ }
30
+ exports.extendMemory = extendMemory;
31
+ //# sourceMappingURL=memory.js.map
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WasiError = exports.strerror = void 0;
4
+ const types_1 = require("./types");
5
+ function strerror(errno) {
6
+ switch (errno) {
7
+ case types_1.WasiErrno.ESUCCESS: return 'Success';
8
+ case types_1.WasiErrno.E2BIG: return 'Argument list too long';
9
+ case types_1.WasiErrno.EACCES: return 'Permission denied';
10
+ case types_1.WasiErrno.EADDRINUSE: return 'Address in use';
11
+ case types_1.WasiErrno.EADDRNOTAVAIL: return 'Address not available';
12
+ case types_1.WasiErrno.EAFNOSUPPORT: return 'Address family not supported by protocol';
13
+ case types_1.WasiErrno.EAGAIN: return 'Resource temporarily unavailable';
14
+ case types_1.WasiErrno.EALREADY: return 'Operation already in progress';
15
+ case types_1.WasiErrno.EBADF: return 'Bad file descriptor';
16
+ case types_1.WasiErrno.EBADMSG: return 'Bad message';
17
+ case types_1.WasiErrno.EBUSY: return 'Resource busy';
18
+ case types_1.WasiErrno.ECANCELED: return 'Operation canceled';
19
+ case types_1.WasiErrno.ECHILD: return 'No child process';
20
+ case types_1.WasiErrno.ECONNABORTED: return 'Connection aborted';
21
+ case types_1.WasiErrno.ECONNREFUSED: return 'Connection refused';
22
+ case types_1.WasiErrno.ECONNRESET: return 'Connection reset by peer';
23
+ case types_1.WasiErrno.EDEADLK: return 'Resource deadlock would occur';
24
+ case types_1.WasiErrno.EDESTADDRREQ: return 'Destination address required';
25
+ case types_1.WasiErrno.EDOM: return 'Domain error';
26
+ case types_1.WasiErrno.EDQUOT: return 'Quota exceeded';
27
+ case types_1.WasiErrno.EEXIST: return 'File exists';
28
+ case types_1.WasiErrno.EFAULT: return 'Bad address';
29
+ case types_1.WasiErrno.EFBIG: return 'File too large';
30
+ case types_1.WasiErrno.EHOSTUNREACH: return 'Host is unreachable';
31
+ case types_1.WasiErrno.EIDRM: return 'Identifier removed';
32
+ case types_1.WasiErrno.EILSEQ: return 'Illegal byte sequence';
33
+ case types_1.WasiErrno.EINPROGRESS: return 'Operation in progress';
34
+ case types_1.WasiErrno.EINTR: return 'Interrupted system call';
35
+ case types_1.WasiErrno.EINVAL: return 'Invalid argument';
36
+ case types_1.WasiErrno.EIO: return 'I/O error';
37
+ case types_1.WasiErrno.EISCONN: return 'Socket is connected';
38
+ case types_1.WasiErrno.EISDIR: return 'Is a directory';
39
+ case types_1.WasiErrno.ELOOP: return 'Symbolic link loop';
40
+ case types_1.WasiErrno.EMFILE: return 'No file descriptors available';
41
+ case types_1.WasiErrno.EMLINK: return 'Too many links';
42
+ case types_1.WasiErrno.EMSGSIZE: return 'Message too large';
43
+ case types_1.WasiErrno.EMULTIHOP: return 'Multihop attempted';
44
+ case types_1.WasiErrno.ENAMETOOLONG: return 'Filename too long';
45
+ case types_1.WasiErrno.ENETDOWN: return 'Network is down';
46
+ case types_1.WasiErrno.ENETRESET: return 'Connection reset by network';
47
+ case types_1.WasiErrno.ENETUNREACH: return 'Network unreachable';
48
+ case types_1.WasiErrno.ENFILE: return 'Too many files open in system';
49
+ case types_1.WasiErrno.ENOBUFS: return 'No buffer space available';
50
+ case types_1.WasiErrno.ENODEV: return 'No such device';
51
+ case types_1.WasiErrno.ENOENT: return 'No such file or directory';
52
+ case types_1.WasiErrno.ENOEXEC: return 'Exec format error';
53
+ case types_1.WasiErrno.ENOLCK: return 'No locks available';
54
+ case types_1.WasiErrno.ENOLINK: return 'Link has been severed';
55
+ case types_1.WasiErrno.ENOMEM: return 'Out of memory';
56
+ case types_1.WasiErrno.ENOMSG: return 'No message of the desired type';
57
+ case types_1.WasiErrno.ENOPROTOOPT: return 'Protocol not available';
58
+ case types_1.WasiErrno.ENOSPC: return 'No space left on device';
59
+ case types_1.WasiErrno.ENOSYS: return 'Function not implemented';
60
+ case types_1.WasiErrno.ENOTCONN: return 'Socket not connected';
61
+ case types_1.WasiErrno.ENOTDIR: return 'Not a directory';
62
+ case types_1.WasiErrno.ENOTEMPTY: return 'Directory not empty';
63
+ case types_1.WasiErrno.ENOTRECOVERABLE: return 'State not recoverable';
64
+ case types_1.WasiErrno.ENOTSOCK: return 'Not a socket';
65
+ case types_1.WasiErrno.ENOTSUP: return 'Not supported';
66
+ case types_1.WasiErrno.ENOTTY: return 'Not a tty';
67
+ case types_1.WasiErrno.ENXIO: return 'No such device or address';
68
+ case types_1.WasiErrno.EOVERFLOW: return 'Value too large for data type';
69
+ case types_1.WasiErrno.EOWNERDEAD: return 'Previous owner died';
70
+ case types_1.WasiErrno.EPERM: return 'Operation not permitted';
71
+ case types_1.WasiErrno.EPIPE: return 'Broken pipe';
72
+ case types_1.WasiErrno.EPROTO: return 'Protocol error';
73
+ case types_1.WasiErrno.EPROTONOSUPPORT: return 'Protocol not supported';
74
+ case types_1.WasiErrno.EPROTOTYPE: return 'Protocol wrong type for socket';
75
+ case types_1.WasiErrno.ERANGE: return 'Result not representable';
76
+ case types_1.WasiErrno.EROFS: return 'Read-only file system';
77
+ case types_1.WasiErrno.ESPIPE: return 'Invalid seek';
78
+ case types_1.WasiErrno.ESRCH: return 'No such process';
79
+ case types_1.WasiErrno.ESTALE: return 'Stale file handle';
80
+ case types_1.WasiErrno.ETIMEDOUT: return 'Operation timed out';
81
+ case types_1.WasiErrno.ETXTBSY: return 'Text file busy';
82
+ case types_1.WasiErrno.EXDEV: return 'Cross-device link';
83
+ case types_1.WasiErrno.ENOTCAPABLE: return 'Capabilities insufficient';
84
+ default: return 'Unknown error';
85
+ }
86
+ }
87
+ exports.strerror = strerror;
88
+ class WasiError extends Error {
89
+ constructor(message, errno) {
90
+ super(message);
91
+ this.errno = errno;
92
+ }
93
+ getErrorMessage() {
94
+ return strerror(this.errno);
95
+ }
96
+ }
97
+ exports.WasiError = WasiError;
98
+ Object.defineProperty(WasiError.prototype, 'name', {
99
+ configurable: true,
100
+ writable: true,
101
+ value: 'WasiError'
102
+ });
103
+ //# sourceMappingURL=error.js.map
@@ -0,0 +1,226 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ 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.fs = options.fs;
124
+ this.print = options.print;
125
+ this.printErr = options.printErr;
126
+ this.insertStdio(options.in, 0, '<stdin>');
127
+ this.insertStdio(options.out, 1, '<stdout>');
128
+ this.insertStdio(options.err, 2, '<stderr>');
129
+ }
130
+ insertStdio(fd, expected, name) {
131
+ const type = types_1.WasiFileType.CHARACTER_DEVICE;
132
+ const { base, inheriting } = (0, rights_1.getRights)(this.stdio, fd, types_1.FileControlFlag.O_RDWR, type);
133
+ const wrap = this.insert(fd, name, name, type, base, inheriting, 0);
134
+ if (wrap.id !== expected) {
135
+ throw new error_1.WasiError(`id: ${wrap.id} !== expected: ${expected}`, types_1.WasiErrno.EBADF);
136
+ }
137
+ return wrap;
138
+ }
139
+ insert(fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen) {
140
+ var _a, _b;
141
+ let index = -1;
142
+ if (this.used >= this.size) {
143
+ const newSize = this.size * 2;
144
+ this.fds.length = newSize;
145
+ index = this.size;
146
+ this.size = newSize;
147
+ }
148
+ else {
149
+ for (let i = 0; i < this.size; ++i) {
150
+ if (this.fds[i] == null) {
151
+ index = i;
152
+ break;
153
+ }
154
+ }
155
+ }
156
+ let entry;
157
+ if (mappedPath === '<stdout>') {
158
+ entry = new StandardOutput((_a = this.print) !== null && _a !== void 0 ? _a : console.log, index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
159
+ }
160
+ else if (mappedPath === '<stderr>') {
161
+ entry = new StandardOutput((_b = this.printErr) !== null && _b !== void 0 ? _b : console.error, index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
162
+ }
163
+ else {
164
+ entry = new FileDescriptor(index, fd, mappedPath, realPath, type, rightsBase, rightsInheriting, preopen);
165
+ }
166
+ this.fds[index] = entry;
167
+ this.used++;
168
+ return entry;
169
+ }
170
+ getFileTypeByFd(fd) {
171
+ const stat = this.fs.fstatSync(fd);
172
+ return toFileType(stat);
173
+ }
174
+ insertPreopen(fd, mappedPath, realPath) {
175
+ const type = this.getFileTypeByFd(fd);
176
+ if (type !== types_1.WasiFileType.DIRECTORY) {
177
+ throw new error_1.WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, types_1.WasiErrno.ENOTDIR);
178
+ }
179
+ const result = (0, rights_1.getRights)(this.stdio, fd, 0, type);
180
+ return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
181
+ }
182
+ get(id, base, inheriting) {
183
+ if (id >= this.size) {
184
+ throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
185
+ }
186
+ const entry = this.fds[id];
187
+ if (!entry || entry.id !== id) {
188
+ throw new error_1.WasiError('Bad file descriptor', types_1.WasiErrno.EBADF);
189
+ }
190
+ /* Validate that the fd has the necessary rights. */
191
+ if ((~entry.rightsBase & base) !== BigInt(0) || (~entry.rightsInheriting & inheriting) !== BigInt(0)) {
192
+ throw new error_1.WasiError('Capabilities insufficient', types_1.WasiErrno.ENOTCAPABLE);
193
+ }
194
+ return entry;
195
+ }
196
+ remove(id) {
197
+ if (id >= this.size) {
198
+ throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
199
+ }
200
+ const entry = this.fds[id];
201
+ if (!entry || entry.id !== id) {
202
+ throw new error_1.WasiError('Bad file descriptor', types_1.WasiErrno.EBADF);
203
+ }
204
+ this.fds[id] = undefined;
205
+ this.used--;
206
+ }
207
+ renumber(dst, src) {
208
+ if (dst === src)
209
+ return;
210
+ if (dst >= this.size || src >= this.size) {
211
+ throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
212
+ }
213
+ const dstEntry = this.fds[dst];
214
+ const srcEntry = this.fds[src];
215
+ if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
216
+ throw new error_1.WasiError('Invalid fd', types_1.WasiErrno.EBADF);
217
+ }
218
+ this.fs.closeSync(dstEntry.fd);
219
+ this.fds[dst] = this.fds[src];
220
+ this.fds[dst].id = dst;
221
+ this.fds[src] = undefined;
222
+ this.used--;
223
+ }
224
+ }
225
+ exports.FileDescriptorTable = FileDescriptorTable;
226
+ //# sourceMappingURL=fd.js.map
@@ -0,0 +1,137 @@
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
+ const kEmptyObject = Object.freeze(Object.create(null));
7
+ const kExitCode = Symbol('kExitCode');
8
+ const kSetMemory = Symbol('kSetMemory');
9
+ const kStarted = Symbol('kStarted');
10
+ const kInstance = Symbol('kInstance');
11
+ function setupInstance(self, instance) {
12
+ (0, util_1.validateObject)(instance, 'instance');
13
+ (0, util_1.validateObject)(instance.exports, 'instance.exports');
14
+ self[kInstance] = instance;
15
+ self[kSetMemory](instance.exports.memory);
16
+ }
17
+ // /** @public */
18
+ // export type WasiSnapshotPreview1 = Omit<_WASI, '_setMemory'>
19
+ /** @public */
20
+ class WASI {
21
+ constructor(options = kEmptyObject) {
22
+ var _a;
23
+ (0, util_1.validateObject)(options, 'options');
24
+ if (options.args !== undefined) {
25
+ (0, util_1.validateArray)(options.args, 'options.args');
26
+ }
27
+ const args = ((_a = options.args) !== null && _a !== void 0 ? _a : []).map(String);
28
+ const env = [];
29
+ if (options.env !== undefined) {
30
+ (0, util_1.validateObject)(options.env, 'options.env');
31
+ Object.entries(options.env).forEach(({ 0: key, 1: value }) => {
32
+ if (value !== undefined) {
33
+ env.push(`${key}=${value}`);
34
+ }
35
+ });
36
+ }
37
+ const preopens = [];
38
+ if (options.preopens !== undefined) {
39
+ (0, util_1.validateObject)(options.preopens, 'options.preopens');
40
+ Object.entries(options.preopens).forEach(({ 0: key, 1: value }) => preopens.push({ mappedPath: String(key), realPath: String(value) }));
41
+ }
42
+ if (preopens.length > 0) {
43
+ if (options.filesystem === undefined) {
44
+ throw new Error('filesystem is disabled, can not preopen directory');
45
+ }
46
+ }
47
+ if (options.filesystem !== undefined) {
48
+ (0, util_1.validateObject)(options.filesystem, 'options.filesystem');
49
+ (0, util_1.validateString)(options.filesystem.type, 'options.filesystem.type');
50
+ if (options.filesystem.type !== 'memfs') {
51
+ throw new Error(`Filesystem type ${options.filesystem.type} is not supported, only "memfs" is supported currently`);
52
+ }
53
+ try {
54
+ (0, util_1.validateObject)(options.filesystem.fs, 'options.filesystem.fs');
55
+ }
56
+ catch (_) {
57
+ throw new Error('Node.js fs like implementation is not provided');
58
+ }
59
+ }
60
+ if (options.print !== undefined)
61
+ (0, util_1.validateFunction)(options.print, 'options.print');
62
+ if (options.printErr !== undefined)
63
+ (0, util_1.validateFunction)(options.printErr, 'options.printErr');
64
+ // const { stdin = 0, stdout = 1, stderr = 2 } = options
65
+ // validateInt32(stdin, 'options.stdin', 0)
66
+ // validateInt32(stdout, 'options.stdout', 0)
67
+ // validateInt32(stderr, 'options.stderr', 0)
68
+ // const stdio = [stdin, stdout, stderr] as const
69
+ const stdio = [0, 1, 2];
70
+ const wrap = new preview1_1.WASI(args, env, preopens, stdio, options.filesystem, options.print, options.printErr);
71
+ for (const prop in wrap) {
72
+ wrap[prop] = wrap[prop].bind(wrap);
73
+ }
74
+ if (options.returnOnExit !== undefined) {
75
+ (0, util_1.validateBoolean)(options.returnOnExit, 'options.returnOnExit');
76
+ if (options.returnOnExit) {
77
+ wrap.proc_exit = wasiReturnOnProcExit.bind(this);
78
+ }
79
+ }
80
+ this[kSetMemory] = wrap._setMemory;
81
+ delete wrap._setMemory;
82
+ this.wasiImport = wrap;
83
+ this[kStarted] = false;
84
+ this[kExitCode] = 0;
85
+ this[kInstance] = undefined;
86
+ }
87
+ // Must not export _initialize, must export _start
88
+ start(instance) {
89
+ if (this[kStarted]) {
90
+ throw new Error('WASI instance has already started');
91
+ }
92
+ this[kStarted] = true;
93
+ setupInstance(this, instance);
94
+ const { _start, _initialize } = this[kInstance].exports;
95
+ (0, util_1.validateFunction)(_start, 'instance.exports._start');
96
+ (0, util_1.validateUndefined)(_initialize, 'instance.exports._initialize');
97
+ let ret;
98
+ try {
99
+ ret = _start();
100
+ }
101
+ catch (err) {
102
+ if (err !== kExitCode) {
103
+ throw err;
104
+ }
105
+ }
106
+ if (ret instanceof Promise) {
107
+ return ret.then(() => this[kExitCode], (err) => {
108
+ if (err !== kExitCode) {
109
+ throw err;
110
+ }
111
+ return this[kExitCode];
112
+ });
113
+ }
114
+ return this[kExitCode];
115
+ }
116
+ // Must not export _start, may optionally export _initialize
117
+ initialize(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
+ (0, util_1.validateUndefined)(_start, 'instance.exports._start');
125
+ if (_initialize !== undefined) {
126
+ (0, util_1.validateFunction)(_initialize, 'instance.exports._initialize');
127
+ return _initialize();
128
+ }
129
+ }
130
+ }
131
+ exports.WASI = WASI;
132
+ function wasiReturnOnProcExit(rval) {
133
+ this[kExitCode] = rval;
134
+ // eslint-disable-next-line @typescript-eslint/no-throw-literal
135
+ throw kExitCode;
136
+ }
137
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.relative = exports.resolve = void 0;
4
+ const util_1 = require("./util");
5
+ const CHAR_DOT = 46; /* . */
6
+ const CHAR_FORWARD_SLASH = 47; /* / */
7
+ function isPosixPathSeparator(code) {
8
+ return code === CHAR_FORWARD_SLASH;
9
+ }
10
+ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
11
+ let res = '';
12
+ let lastSegmentLength = 0;
13
+ let lastSlash = -1;
14
+ let dots = 0;
15
+ let code = 0;
16
+ for (let i = 0; i <= path.length; ++i) {
17
+ if (i < path.length) {
18
+ code = path.charCodeAt(i);
19
+ }
20
+ else if (isPathSeparator(code)) {
21
+ break;
22
+ }
23
+ else {
24
+ code = CHAR_FORWARD_SLASH;
25
+ }
26
+ if (isPathSeparator(code)) {
27
+ if (lastSlash === i - 1 || dots === 1) {
28
+ // NOOP
29
+ }
30
+ else if (dots === 2) {
31
+ if (res.length < 2 || lastSegmentLength !== 2 ||
32
+ res.charCodeAt(res.length - 1) !== CHAR_DOT ||
33
+ res.charCodeAt(res.length - 2) !== CHAR_DOT) {
34
+ if (res.length > 2) {
35
+ const lastSlashIndex = res.indexOf(separator);
36
+ if (lastSlashIndex === -1) {
37
+ res = '';
38
+ lastSegmentLength = 0;
39
+ }
40
+ else {
41
+ res = res.slice(0, lastSlashIndex);
42
+ lastSegmentLength =
43
+ res.length - 1 - res.indexOf(separator);
44
+ }
45
+ lastSlash = i;
46
+ dots = 0;
47
+ continue;
48
+ }
49
+ else if (res.length !== 0) {
50
+ res = '';
51
+ lastSegmentLength = 0;
52
+ lastSlash = i;
53
+ dots = 0;
54
+ continue;
55
+ }
56
+ }
57
+ if (allowAboveRoot) {
58
+ res += res.length > 0 ? `${separator}..` : '..';
59
+ lastSegmentLength = 2;
60
+ }
61
+ }
62
+ else {
63
+ if (res.length > 0) {
64
+ res += `${separator}${path.slice(lastSlash + 1, i)}`;
65
+ }
66
+ else {
67
+ res = path.slice(lastSlash + 1, i);
68
+ }
69
+ lastSegmentLength = i - lastSlash - 1;
70
+ }
71
+ lastSlash = i;
72
+ dots = 0;
73
+ }
74
+ else if (code === CHAR_DOT && dots !== -1) {
75
+ ++dots;
76
+ }
77
+ else {
78
+ dots = -1;
79
+ }
80
+ }
81
+ return res;
82
+ }
83
+ function resolve(...args) {
84
+ let resolvedPath = '';
85
+ let resolvedAbsolute = false;
86
+ for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
87
+ const path = i >= 0 ? args[i] : '/';
88
+ (0, util_1.validateString)(path, 'path');
89
+ // Skip empty entries
90
+ if (path.length === 0) {
91
+ continue;
92
+ }
93
+ resolvedPath = `${path}/${resolvedPath}`;
94
+ resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
95
+ }
96
+ // At this point the path should be resolved to a full absolute path, but
97
+ // handle relative paths to be safe (might happen when process.cwd() fails)
98
+ // Normalize the path
99
+ resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/', isPosixPathSeparator);
100
+ if (resolvedAbsolute) {
101
+ return `/${resolvedPath}`;
102
+ }
103
+ return resolvedPath.length > 0 ? resolvedPath : '.';
104
+ }
105
+ exports.resolve = resolve;
106
+ function relative(from, to) {
107
+ (0, util_1.validateString)(from, 'from');
108
+ (0, util_1.validateString)(to, 'to');
109
+ if (from === to)
110
+ return '';
111
+ // Trim leading forward slashes.
112
+ from = resolve(from);
113
+ to = resolve(to);
114
+ if (from === to)
115
+ return '';
116
+ const fromStart = 1;
117
+ const fromEnd = from.length;
118
+ const fromLen = fromEnd - fromStart;
119
+ const toStart = 1;
120
+ const toLen = to.length - toStart;
121
+ // Compare paths to find the longest common path from root
122
+ const length = (fromLen < toLen ? fromLen : toLen);
123
+ let lastCommonSep = -1;
124
+ let i = 0;
125
+ for (; i < length; i++) {
126
+ const fromCode = from.charCodeAt(fromStart + i);
127
+ if (fromCode !== to.charCodeAt(toStart + i)) {
128
+ break;
129
+ }
130
+ else if (fromCode === CHAR_FORWARD_SLASH) {
131
+ lastCommonSep = i;
132
+ }
133
+ }
134
+ if (i === length) {
135
+ if (toLen > length) {
136
+ if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
137
+ // We get here if `from` is the exact base path for `to`.
138
+ // For example: from='/foo/bar'; to='/foo/bar/baz'
139
+ return to.slice(toStart + i + 1);
140
+ }
141
+ if (i === 0) {
142
+ // We get here if `from` is the root
143
+ // For example: from='/'; to='/foo'
144
+ return to.slice(toStart + i);
145
+ }
146
+ }
147
+ else if (fromLen > length) {
148
+ if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
149
+ // We get here if `to` is the exact base path for `from`.
150
+ // For example: from='/foo/bar/baz'; to='/foo/bar'
151
+ lastCommonSep = i;
152
+ }
153
+ else if (i === 0) {
154
+ // We get here if `to` is the root.
155
+ // For example: from='/foo/bar'; to='/'
156
+ lastCommonSep = 0;
157
+ }
158
+ }
159
+ }
160
+ let out = '';
161
+ // Generate the relative path based on the path difference between `to`
162
+ // and `from`.
163
+ for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
164
+ if (i === fromEnd ||
165
+ from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
166
+ out += out.length === 0 ? '..' : '/..';
167
+ }
168
+ }
169
+ // Lastly, append the rest of the destination (`to`) path that comes after
170
+ // the common path parts.
171
+ return `${out}${to.slice(toStart + lastCommonSep)}`;
172
+ }
173
+ exports.relative = relative;
174
+ //# sourceMappingURL=path.js.map