@tailwindcss/oxide-wasm32-wasi 0.0.0-insiders.d801d8d

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 (113) hide show
  1. package/README.md +3 -0
  2. package/node_modules/@emnapi/core/LICENSE +21 -0
  3. package/node_modules/@emnapi/core/README.md +1 -0
  4. package/node_modules/@emnapi/core/dist/emnapi-core.cjs.js +6661 -0
  5. package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.d.ts +363 -0
  6. package/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.js +1 -0
  7. package/node_modules/@emnapi/core/dist/emnapi-core.d.mts +363 -0
  8. package/node_modules/@emnapi/core/dist/emnapi-core.d.ts +365 -0
  9. package/node_modules/@emnapi/core/dist/emnapi-core.esm-bundler.js +7411 -0
  10. package/node_modules/@emnapi/core/dist/emnapi-core.js +8390 -0
  11. package/node_modules/@emnapi/core/dist/emnapi-core.min.d.mts +363 -0
  12. package/node_modules/@emnapi/core/dist/emnapi-core.min.js +1 -0
  13. package/node_modules/@emnapi/core/dist/emnapi-core.min.mjs +1 -0
  14. package/node_modules/@emnapi/core/dist/emnapi-core.mjs +6650 -0
  15. package/node_modules/@emnapi/core/index.js +5 -0
  16. package/node_modules/@emnapi/core/package.json +49 -0
  17. package/node_modules/@emnapi/runtime/LICENSE +21 -0
  18. package/node_modules/@emnapi/runtime/README.md +1 -0
  19. package/node_modules/@emnapi/runtime/dist/emnapi.cjs.js +1347 -0
  20. package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.d.ts +663 -0
  21. package/node_modules/@emnapi/runtime/dist/emnapi.cjs.min.js +1 -0
  22. package/node_modules/@emnapi/runtime/dist/emnapi.d.mts +663 -0
  23. package/node_modules/@emnapi/runtime/dist/emnapi.d.ts +665 -0
  24. package/node_modules/@emnapi/runtime/dist/emnapi.esm-bundler.js +1403 -0
  25. package/node_modules/@emnapi/runtime/dist/emnapi.iife.d.ts +420 -0
  26. package/node_modules/@emnapi/runtime/dist/emnapi.iife.js +1474 -0
  27. package/node_modules/@emnapi/runtime/dist/emnapi.js +1475 -0
  28. package/node_modules/@emnapi/runtime/dist/emnapi.min.d.mts +663 -0
  29. package/node_modules/@emnapi/runtime/dist/emnapi.min.js +1 -0
  30. package/node_modules/@emnapi/runtime/dist/emnapi.min.mjs +1 -0
  31. package/node_modules/@emnapi/runtime/dist/emnapi.mjs +1316 -0
  32. package/node_modules/@emnapi/runtime/index.js +5 -0
  33. package/node_modules/@emnapi/runtime/package.json +48 -0
  34. package/node_modules/@emnapi/wasi-threads/LICENSE +21 -0
  35. package/node_modules/@emnapi/wasi-threads/README.md +203 -0
  36. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.js +881 -0
  37. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.d.ts +214 -0
  38. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.cjs.min.js +1 -0
  39. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.mts +214 -0
  40. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.ts +216 -0
  41. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.esm-bundler.js +929 -0
  42. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.js +941 -0
  43. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.d.mts +214 -0
  44. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.js +1 -0
  45. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.min.mjs +1 -0
  46. package/node_modules/@emnapi/wasi-threads/dist/wasi-threads.mjs +876 -0
  47. package/node_modules/@emnapi/wasi-threads/index.js +5 -0
  48. package/node_modules/@emnapi/wasi-threads/package.json +50 -0
  49. package/node_modules/@napi-rs/wasm-runtime/LICENSE +43 -0
  50. package/node_modules/@napi-rs/wasm-runtime/dist/fs.js +23712 -0
  51. package/node_modules/@napi-rs/wasm-runtime/dist/runtime.js +12269 -0
  52. package/node_modules/@napi-rs/wasm-runtime/fs-proxy.cjs +193 -0
  53. package/node_modules/@napi-rs/wasm-runtime/package.json +60 -0
  54. package/node_modules/@napi-rs/wasm-runtime/runtime.cjs +15 -0
  55. package/node_modules/@tybys/wasm-util/README.md +193 -0
  56. package/node_modules/@tybys/wasm-util/dist/tsdoc-metadata.json +11 -0
  57. package/node_modules/@tybys/wasm-util/dist/wasm-util.d.ts +417 -0
  58. package/node_modules/@tybys/wasm-util/dist/wasm-util.esm-bundler.js +2738 -0
  59. package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.js +2738 -0
  60. package/node_modules/@tybys/wasm-util/dist/wasm-util.esm.min.js +1 -0
  61. package/node_modules/@tybys/wasm-util/dist/wasm-util.js +2760 -0
  62. package/node_modules/@tybys/wasm-util/dist/wasm-util.min.js +1 -0
  63. package/node_modules/@tybys/wasm-util/lib/cjs/asyncify.js +158 -0
  64. package/node_modules/@tybys/wasm-util/lib/cjs/index.js +13 -0
  65. package/node_modules/@tybys/wasm-util/lib/cjs/jspi.js +46 -0
  66. package/node_modules/@tybys/wasm-util/lib/cjs/load.js +97 -0
  67. package/node_modules/@tybys/wasm-util/lib/cjs/memory.js +35 -0
  68. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/error.js +103 -0
  69. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/fd.js +268 -0
  70. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/fs.js +3 -0
  71. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/index.js +194 -0
  72. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/path.js +174 -0
  73. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/preview1.js +1544 -0
  74. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/rights.js +141 -0
  75. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/types.js +220 -0
  76. package/node_modules/@tybys/wasm-util/lib/cjs/wasi/util.js +128 -0
  77. package/node_modules/@tybys/wasm-util/lib/cjs/webassembly.js +13 -0
  78. package/node_modules/@tybys/wasm-util/lib/mjs/asyncify.mjs +153 -0
  79. package/node_modules/@tybys/wasm-util/lib/mjs/index.mjs +9 -0
  80. package/node_modules/@tybys/wasm-util/lib/mjs/jspi.mjs +39 -0
  81. package/node_modules/@tybys/wasm-util/lib/mjs/load.mjs +89 -0
  82. package/node_modules/@tybys/wasm-util/lib/mjs/memory.mjs +28 -0
  83. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/error.mjs +97 -0
  84. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/fd.mjs +256 -0
  85. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/fs.mjs +1 -0
  86. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/index.mjs +188 -0
  87. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/path.mjs +168 -0
  88. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/preview1.mjs +1539 -0
  89. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/rights.mjs +135 -0
  90. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/types.mjs +216 -0
  91. package/node_modules/@tybys/wasm-util/lib/mjs/wasi/util.mjs +113 -0
  92. package/node_modules/@tybys/wasm-util/lib/mjs/webassembly.mjs +9 -0
  93. package/node_modules/@tybys/wasm-util/package.json +58 -0
  94. package/node_modules/tslib/CopyrightNotice.txt +15 -0
  95. package/node_modules/tslib/LICENSE.txt +12 -0
  96. package/node_modules/tslib/README.md +164 -0
  97. package/node_modules/tslib/SECURITY.md +41 -0
  98. package/node_modules/tslib/modules/index.d.ts +38 -0
  99. package/node_modules/tslib/modules/index.js +70 -0
  100. package/node_modules/tslib/modules/package.json +3 -0
  101. package/node_modules/tslib/package.json +47 -0
  102. package/node_modules/tslib/tslib.d.ts +460 -0
  103. package/node_modules/tslib/tslib.es6.html +1 -0
  104. package/node_modules/tslib/tslib.es6.js +393 -0
  105. package/node_modules/tslib/tslib.es6.mjs +392 -0
  106. package/node_modules/tslib/tslib.html +1 -0
  107. package/node_modules/tslib/tslib.js +475 -0
  108. package/package.json +45 -0
  109. package/tailwindcss-oxide.wasi-browser.js +63 -0
  110. package/tailwindcss-oxide.wasi.cjs +88 -0
  111. package/tailwindcss-oxide.wasm32-wasi.wasm +0 -0
  112. package/wasi-worker-browser.mjs +39 -0
  113. package/wasi-worker.mjs +63 -0
@@ -0,0 +1,1544 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WASI = void 0;
4
+ const webassembly_1 = require("../webassembly");
5
+ const path_1 = require("./path");
6
+ const types_1 = require("./types");
7
+ const fd_1 = require("./fd");
8
+ const error_1 = require("./error");
9
+ const util_1 = require("./util");
10
+ const rights_1 = require("./rights");
11
+ const memory_1 = require("../memory");
12
+ const jspi_1 = require("../jspi");
13
+ function copyMemory(targets, src) {
14
+ if (targets.length === 0 || src.length === 0)
15
+ return 0;
16
+ let copied = 0;
17
+ let left = src.length - copied;
18
+ for (let i = 0; i < targets.length; ++i) {
19
+ const target = targets[i];
20
+ if (left < target.length) {
21
+ target.set(src.subarray(copied, copied + left), 0);
22
+ copied += left;
23
+ left = 0;
24
+ return copied;
25
+ }
26
+ target.set(src.subarray(copied, copied + target.length), 0);
27
+ copied += target.length;
28
+ left -= target.length;
29
+ }
30
+ return copied;
31
+ }
32
+ const _memory = new WeakMap();
33
+ const _wasi = new WeakMap();
34
+ const _fs = new WeakMap();
35
+ function getMemory(wasi) {
36
+ return _memory.get(wasi);
37
+ }
38
+ function getFs(wasi) {
39
+ const fs = _fs.get(wasi);
40
+ if (!fs)
41
+ throw new Error('filesystem is unavailable');
42
+ return fs;
43
+ }
44
+ function handleError(err) {
45
+ if (err instanceof error_1.WasiError) {
46
+ if (process.env.NODE_ENV !== 'production') {
47
+ console.warn(err);
48
+ }
49
+ return err.errno;
50
+ }
51
+ switch (err.code) {
52
+ case 'ENOENT': return types_1.WasiErrno.ENOENT;
53
+ case 'EBADF': return types_1.WasiErrno.EBADF;
54
+ case 'EINVAL': return types_1.WasiErrno.EINVAL;
55
+ case 'EPERM': return types_1.WasiErrno.EPERM;
56
+ case 'EPROTO': return types_1.WasiErrno.EPROTO;
57
+ case 'EEXIST': return types_1.WasiErrno.EEXIST;
58
+ case 'ENOTDIR': return types_1.WasiErrno.ENOTDIR;
59
+ case 'EMFILE': return types_1.WasiErrno.EMFILE;
60
+ case 'EACCES': return types_1.WasiErrno.EACCES;
61
+ case 'EISDIR': return types_1.WasiErrno.EISDIR;
62
+ case 'ENOTEMPTY': return types_1.WasiErrno.ENOTEMPTY;
63
+ case 'ENOSYS': return types_1.WasiErrno.ENOSYS;
64
+ }
65
+ throw err;
66
+ }
67
+ function defineName(name, f) {
68
+ Object.defineProperty(f, 'name', { value: name });
69
+ return f;
70
+ }
71
+ function syscallWrap(self, name, f) {
72
+ return defineName(name, function () {
73
+ if (process.env.NODE_DEBUG_NATIVE === 'wasi') {
74
+ const args = Array.prototype.slice.call(arguments);
75
+ let debugArgs = [`${name}(${Array.from({ length: arguments.length }).map(() => '%d').join(', ')})`];
76
+ debugArgs = debugArgs.concat(args);
77
+ console.debug.apply(console, debugArgs);
78
+ }
79
+ let r;
80
+ try {
81
+ r = f.apply(self, arguments);
82
+ }
83
+ catch (err) {
84
+ return handleError(err);
85
+ }
86
+ if ((0, util_1.isPromiseLike)(r)) {
87
+ return r.then(_ => _, handleError);
88
+ }
89
+ return r;
90
+ });
91
+ }
92
+ function resolvePathSync(fs, fileDescriptor, path, flags) {
93
+ let resolvedPath = (0, path_1.resolve)(fileDescriptor.realPath, path);
94
+ if ((flags & 1) === 1) {
95
+ try {
96
+ resolvedPath = fs.readlinkSync(resolvedPath);
97
+ }
98
+ catch (err) {
99
+ if (err.code !== 'EINVAL' && err.code !== 'ENOENT') {
100
+ throw err;
101
+ }
102
+ }
103
+ }
104
+ return resolvedPath;
105
+ }
106
+ async function resolvePathAsync(fs, fileDescriptor, path, flags) {
107
+ let resolvedPath = (0, path_1.resolve)(fileDescriptor.realPath, path);
108
+ if ((flags & 1) === 1) {
109
+ try {
110
+ resolvedPath = await fs.promises.readlink(resolvedPath);
111
+ }
112
+ catch (err) {
113
+ if (err.code !== 'EINVAL' && err.code !== 'ENOENT') {
114
+ throw err;
115
+ }
116
+ }
117
+ }
118
+ return resolvedPath;
119
+ }
120
+ // eslint-disable-next-line spaced-comment
121
+ const encoder = /*#__PURE__*/ new TextEncoder();
122
+ // eslint-disable-next-line spaced-comment
123
+ const decoder = /*#__PURE__*/ new TextDecoder();
124
+ const INT64_MAX = (BigInt(1) << BigInt(63)) - BigInt(1);
125
+ function readStdin() {
126
+ const value = window.prompt();
127
+ if (value === null)
128
+ return new Uint8Array();
129
+ const buffer = new TextEncoder().encode(value + '\n');
130
+ return buffer;
131
+ }
132
+ function validateFstFlagsOrReturn(flags) {
133
+ return (Boolean((flags) & ~(types_1.WasiFstFlag.SET_ATIM | types_1.WasiFstFlag.SET_ATIM_NOW |
134
+ types_1.WasiFstFlag.SET_MTIM | types_1.WasiFstFlag.SET_MTIM_NOW)) ||
135
+ ((flags) & (types_1.WasiFstFlag.SET_ATIM | types_1.WasiFstFlag.SET_ATIM_NOW)) ===
136
+ (types_1.WasiFstFlag.SET_ATIM | types_1.WasiFstFlag.SET_ATIM_NOW) ||
137
+ ((flags) & (types_1.WasiFstFlag.SET_MTIM | types_1.WasiFstFlag.SET_MTIM_NOW)) ===
138
+ (types_1.WasiFstFlag.SET_MTIM | types_1.WasiFstFlag.SET_MTIM_NOW));
139
+ }
140
+ class WASI {
141
+ constructor(args, env, fds, asyncFs, fs, asyncify) {
142
+ this.args_get = syscallWrap(this, 'args_get', function (argv, argv_buf) {
143
+ argv = Number(argv);
144
+ argv_buf = Number(argv_buf);
145
+ if (argv === 0 || argv_buf === 0) {
146
+ return types_1.WasiErrno.EINVAL;
147
+ }
148
+ const { HEAPU8, view } = getMemory(this);
149
+ const wasi = _wasi.get(this);
150
+ const args = wasi.args;
151
+ for (let i = 0; i < args.length; ++i) {
152
+ const arg = args[i];
153
+ view.setInt32(argv, argv_buf, true);
154
+ argv += 4;
155
+ const data = encoder.encode(arg + '\0');
156
+ HEAPU8.set(data, argv_buf);
157
+ argv_buf += data.length;
158
+ }
159
+ return types_1.WasiErrno.ESUCCESS;
160
+ });
161
+ this.args_sizes_get = syscallWrap(this, 'args_sizes_get', function (argc, argv_buf_size) {
162
+ argc = Number(argc);
163
+ argv_buf_size = Number(argv_buf_size);
164
+ if (argc === 0 || argv_buf_size === 0) {
165
+ return types_1.WasiErrno.EINVAL;
166
+ }
167
+ const { view } = getMemory(this);
168
+ const wasi = _wasi.get(this);
169
+ const args = wasi.args;
170
+ view.setUint32(argc, args.length, true);
171
+ view.setUint32(argv_buf_size, encoder.encode(args.join('\0') + '\0').length, true);
172
+ return types_1.WasiErrno.ESUCCESS;
173
+ });
174
+ this.environ_get = syscallWrap(this, 'environ_get', function (environ, environ_buf) {
175
+ environ = Number(environ);
176
+ environ_buf = Number(environ_buf);
177
+ if (environ === 0 || environ_buf === 0) {
178
+ return types_1.WasiErrno.EINVAL;
179
+ }
180
+ const { HEAPU8, view } = getMemory(this);
181
+ const wasi = _wasi.get(this);
182
+ const env = wasi.env;
183
+ for (let i = 0; i < env.length; ++i) {
184
+ const pair = env[i];
185
+ view.setInt32(environ, environ_buf, true);
186
+ environ += 4;
187
+ const data = encoder.encode(pair + '\0');
188
+ HEAPU8.set(data, environ_buf);
189
+ environ_buf += data.length;
190
+ }
191
+ return types_1.WasiErrno.ESUCCESS;
192
+ });
193
+ this.environ_sizes_get = syscallWrap(this, 'environ_sizes_get', function (len, buflen) {
194
+ len = Number(len);
195
+ buflen = Number(buflen);
196
+ if (len === 0 || buflen === 0) {
197
+ return types_1.WasiErrno.EINVAL;
198
+ }
199
+ const { view } = getMemory(this);
200
+ const wasi = _wasi.get(this);
201
+ view.setUint32(len, wasi.env.length, true);
202
+ view.setUint32(buflen, encoder.encode(wasi.env.join('\0') + '\0').length, true);
203
+ return types_1.WasiErrno.ESUCCESS;
204
+ });
205
+ this.clock_res_get = syscallWrap(this, 'clock_res_get', function (id, resolution) {
206
+ resolution = Number(resolution);
207
+ if (resolution === 0) {
208
+ return types_1.WasiErrno.EINVAL;
209
+ }
210
+ const { view } = getMemory(this);
211
+ switch (id) {
212
+ case types_1.WasiClockid.REALTIME:
213
+ view.setBigUint64(resolution, BigInt(1000000), true);
214
+ return types_1.WasiErrno.ESUCCESS;
215
+ case types_1.WasiClockid.MONOTONIC:
216
+ case types_1.WasiClockid.PROCESS_CPUTIME_ID:
217
+ case types_1.WasiClockid.THREAD_CPUTIME_ID:
218
+ view.setBigUint64(resolution, BigInt(1000), true);
219
+ return types_1.WasiErrno.ESUCCESS;
220
+ default: return types_1.WasiErrno.EINVAL;
221
+ }
222
+ });
223
+ this.clock_time_get = syscallWrap(this, 'clock_time_get', function (id, _percision, time) {
224
+ time = Number(time);
225
+ if (time === 0) {
226
+ return types_1.WasiErrno.EINVAL;
227
+ }
228
+ const { view } = getMemory(this);
229
+ switch (id) {
230
+ case types_1.WasiClockid.REALTIME:
231
+ view.setBigUint64(time, BigInt(Date.now()) * BigInt(1000000), true);
232
+ return types_1.WasiErrno.ESUCCESS;
233
+ case types_1.WasiClockid.MONOTONIC:
234
+ case types_1.WasiClockid.PROCESS_CPUTIME_ID:
235
+ case types_1.WasiClockid.THREAD_CPUTIME_ID: {
236
+ const t = performance.now();
237
+ const s = Math.trunc(t);
238
+ const ms = Math.floor((t - s) * 1000);
239
+ const result = BigInt(s) * BigInt(1000000000) + BigInt(ms) * BigInt(1000000);
240
+ view.setBigUint64(time, result, true);
241
+ return types_1.WasiErrno.ESUCCESS;
242
+ }
243
+ default: return types_1.WasiErrno.EINVAL;
244
+ }
245
+ });
246
+ this.fd_advise = syscallWrap(this, 'fd_advise', function (_fd, _offset, _len, _advice) {
247
+ return types_1.WasiErrno.ENOSYS;
248
+ });
249
+ this.fd_fdstat_get = syscallWrap(this, 'fd_fdstat_get', function (fd, fdstat) {
250
+ fdstat = Number(fdstat);
251
+ if (fdstat === 0) {
252
+ return types_1.WasiErrno.EINVAL;
253
+ }
254
+ const wasi = _wasi.get(this);
255
+ const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
256
+ const { view } = getMemory(this);
257
+ view.setUint16(fdstat, fileDescriptor.type, true);
258
+ view.setUint16(fdstat + 2, 0, true);
259
+ view.setBigUint64(fdstat + 8, fileDescriptor.rightsBase, true);
260
+ view.setBigUint64(fdstat + 16, fileDescriptor.rightsInheriting, true);
261
+ return types_1.WasiErrno.ESUCCESS;
262
+ });
263
+ this.fd_fdstat_set_flags = syscallWrap(this, 'fd_fdstat_set_flags', function (_fd, _flags) {
264
+ return types_1.WasiErrno.ENOSYS;
265
+ });
266
+ this.fd_fdstat_set_rights = syscallWrap(this, 'fd_fdstat_set_rights', function (fd, rightsBase, rightsInheriting) {
267
+ const wasi = _wasi.get(this);
268
+ const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
269
+ if ((rightsBase | fileDescriptor.rightsBase) > fileDescriptor.rightsBase) {
270
+ return types_1.WasiErrno.ENOTCAPABLE;
271
+ }
272
+ if ((rightsInheriting | fileDescriptor.rightsInheriting) >
273
+ fileDescriptor.rightsInheriting) {
274
+ return types_1.WasiErrno.ENOTCAPABLE;
275
+ }
276
+ fileDescriptor.rightsBase = rightsBase;
277
+ fileDescriptor.rightsInheriting = rightsInheriting;
278
+ return types_1.WasiErrno.ESUCCESS;
279
+ });
280
+ this.fd_prestat_get = syscallWrap(this, 'fd_prestat_get', function (fd, prestat) {
281
+ prestat = Number(prestat);
282
+ if (prestat === 0) {
283
+ return types_1.WasiErrno.EINVAL;
284
+ }
285
+ const wasi = _wasi.get(this);
286
+ let fileDescriptor;
287
+ try {
288
+ fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
289
+ }
290
+ catch (err) {
291
+ if (err instanceof error_1.WasiError)
292
+ return err.errno;
293
+ throw err;
294
+ }
295
+ if (fileDescriptor.preopen !== 1)
296
+ return types_1.WasiErrno.EINVAL;
297
+ const { view } = getMemory(this);
298
+ // preopen type is dir(0)
299
+ view.setUint32(prestat, 0, true);
300
+ view.setUint32(prestat + 4, encoder.encode(fileDescriptor.path).length, true);
301
+ return types_1.WasiErrno.ESUCCESS;
302
+ });
303
+ this.fd_prestat_dir_name = syscallWrap(this, 'fd_prestat_dir_name', function (fd, path, path_len) {
304
+ path = Number(path);
305
+ path_len = Number(path_len);
306
+ if (path === 0) {
307
+ return types_1.WasiErrno.EINVAL;
308
+ }
309
+ const wasi = _wasi.get(this);
310
+ const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
311
+ if (fileDescriptor.preopen !== 1)
312
+ return types_1.WasiErrno.EBADF;
313
+ const buffer = encoder.encode(fileDescriptor.path);
314
+ const size = buffer.length;
315
+ if (size > path_len)
316
+ return types_1.WasiErrno.ENOBUFS;
317
+ const { HEAPU8 } = getMemory(this);
318
+ HEAPU8.set(buffer, path);
319
+ return types_1.WasiErrno.ESUCCESS;
320
+ });
321
+ this.fd_seek = syscallWrap(this, 'fd_seek', function (fd, offset, whence, newOffset) {
322
+ newOffset = Number(newOffset);
323
+ if (newOffset === 0) {
324
+ return types_1.WasiErrno.EINVAL;
325
+ }
326
+ if (fd === 0 || fd === 1 || fd === 2)
327
+ return types_1.WasiErrno.ESUCCESS;
328
+ const wasi = _wasi.get(this);
329
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_SEEK, BigInt(0));
330
+ const r = fileDescriptor.seek(offset, whence);
331
+ const { view } = getMemory(this);
332
+ view.setBigUint64(newOffset, r, true);
333
+ return types_1.WasiErrno.ESUCCESS;
334
+ });
335
+ this.fd_tell = syscallWrap(this, 'fd_tell', function (fd, offset) {
336
+ const wasi = _wasi.get(this);
337
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_TELL, BigInt(0));
338
+ const pos = BigInt(fileDescriptor.pos);
339
+ const { view } = getMemory(this);
340
+ view.setBigUint64(Number(offset), pos, true);
341
+ return types_1.WasiErrno.ESUCCESS;
342
+ });
343
+ this.poll_oneoff = syscallWrap(this, 'poll_oneoff', function (in_ptr, out_ptr, nsubscriptions, nevents) {
344
+ in_ptr = Number(in_ptr);
345
+ out_ptr = Number(out_ptr);
346
+ nevents = Number(nevents);
347
+ nsubscriptions = Number(nsubscriptions);
348
+ nsubscriptions = nsubscriptions >>> 0;
349
+ if (in_ptr === 0 || out_ptr === 0 || nsubscriptions === 0 || nevents === 0) {
350
+ return types_1.WasiErrno.EINVAL;
351
+ }
352
+ const { view } = getMemory(this);
353
+ view.setUint32(nevents, 0, true);
354
+ let i = 0;
355
+ let timer_userdata = BigInt(0);
356
+ let cur_timeout = BigInt(0);
357
+ let has_timeout = 0;
358
+ let min_timeout = BigInt(0);
359
+ let sub;
360
+ const subscriptions = Array(nsubscriptions);
361
+ for (i = 0; i < nsubscriptions; i++) {
362
+ sub = in_ptr + i * 48;
363
+ const userdata = view.getBigUint64(sub, true);
364
+ const type = view.getUint8(sub + 8);
365
+ const clockIdOrFd = view.getUint32(sub + 16, true);
366
+ const timeout = view.getBigUint64(sub + 24, true);
367
+ const precision = view.getBigUint64(sub + 32, true);
368
+ const flags = view.getUint16(sub + 40, true);
369
+ subscriptions[i] = {
370
+ userdata,
371
+ type,
372
+ u: {
373
+ clock: {
374
+ clock_id: clockIdOrFd,
375
+ timeout,
376
+ precision,
377
+ flags
378
+ },
379
+ fd_readwrite: {
380
+ fd: clockIdOrFd
381
+ }
382
+ }
383
+ };
384
+ }
385
+ const fdevents = [];
386
+ for (i = 0; i < nsubscriptions; i++) {
387
+ sub = subscriptions[i];
388
+ switch (sub.type) {
389
+ case types_1.WasiEventType.CLOCK: {
390
+ if (sub.u.clock.flags === types_1.WasiSubclockflags.ABSTIME) {
391
+ /* Convert absolute time to relative delay. */
392
+ const now = BigInt(Date.now()) * BigInt(1000000);
393
+ cur_timeout = sub.u.clock.timeout - now;
394
+ }
395
+ else {
396
+ cur_timeout = sub.u.clock.timeout;
397
+ }
398
+ if (has_timeout === 0 || cur_timeout < min_timeout) {
399
+ min_timeout = cur_timeout;
400
+ timer_userdata = sub.userdata;
401
+ has_timeout = 1;
402
+ }
403
+ break;
404
+ }
405
+ case types_1.WasiEventType.FD_READ:
406
+ case types_1.WasiEventType.FD_WRITE:
407
+ fdevents.push(sub);
408
+ break;
409
+ default: return types_1.WasiErrno.EINVAL;
410
+ }
411
+ }
412
+ if (fdevents.length > 0) {
413
+ for (i = 0; i < fdevents.length; i++) {
414
+ const fdevent = fdevents[i];
415
+ const event = out_ptr + 32 * i;
416
+ view.setBigUint64(event, fdevent.userdata, true);
417
+ view.setUint32(event + 8, types_1.WasiErrno.ENOSYS, true);
418
+ view.setUint32(event + 12, fdevent.type, true);
419
+ view.setBigUint64(event + 16, BigInt(0), true);
420
+ view.setUint16(event + 24, 0, true);
421
+ view.setUint32(nevents, 1, true);
422
+ }
423
+ view.setUint32(nevents, fdevents.length, true);
424
+ return types_1.WasiErrno.ESUCCESS;
425
+ }
426
+ if (has_timeout) {
427
+ const delay = Number(min_timeout / BigInt(1000000));
428
+ // if (isMainThread || typeof SharedArrayBuffer !== 'function') {
429
+ (0, util_1.sleepBreakIf)(delay, () => false);
430
+ // } else {
431
+ // const buf = new SharedArrayBuffer(4)
432
+ // const arr = new Int32Array(buf)
433
+ // postMsg({
434
+ // __tybys_wasm_util_wasi__: {
435
+ // type: 'set-timeout',
436
+ // payload: {
437
+ // buffer: buf,
438
+ // delay
439
+ // }
440
+ // }
441
+ // })
442
+ // Atomics.wait(arr, 0, 0)
443
+ // }
444
+ const event = out_ptr;
445
+ view.setBigUint64(event, timer_userdata, true);
446
+ view.setUint32(event + 8, types_1.WasiErrno.ESUCCESS, true);
447
+ view.setUint32(event + 12, types_1.WasiEventType.CLOCK, true);
448
+ view.setUint32(nevents, 1, true);
449
+ }
450
+ return types_1.WasiErrno.ESUCCESS;
451
+ });
452
+ this.proc_exit = syscallWrap(this, 'proc_exit', function (rval) {
453
+ if ((typeof process === 'object') && (process !== null) && (typeof process.exit === 'function')) {
454
+ process.exit(rval);
455
+ }
456
+ return types_1.WasiErrno.ESUCCESS;
457
+ });
458
+ this.proc_raise = syscallWrap(this, 'proc_raise', function (_sig) {
459
+ return types_1.WasiErrno.ENOSYS;
460
+ });
461
+ this.sched_yield = syscallWrap(this, 'sched_yield', function () {
462
+ return types_1.WasiErrno.ESUCCESS;
463
+ });
464
+ this.random_get = typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function'
465
+ ? syscallWrap(this, 'random_get', function (buf, buf_len) {
466
+ buf = Number(buf);
467
+ if (buf === 0) {
468
+ return types_1.WasiErrno.EINVAL;
469
+ }
470
+ buf_len = Number(buf_len);
471
+ const { HEAPU8, view } = getMemory(this);
472
+ if ((typeof SharedArrayBuffer === 'function' && HEAPU8.buffer instanceof SharedArrayBuffer) ||
473
+ (Object.prototype.toString.call(HEAPU8.buffer) === '[object SharedArrayBuffer]')) {
474
+ for (let i = buf; i < buf + buf_len; ++i) {
475
+ view.setUint8(i, Math.floor(Math.random() * 256));
476
+ }
477
+ return types_1.WasiErrno.ESUCCESS;
478
+ }
479
+ let pos;
480
+ const stride = 65536;
481
+ for (pos = 0; pos + stride < buf_len; pos += stride) {
482
+ crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + pos + stride));
483
+ }
484
+ crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + buf_len));
485
+ return types_1.WasiErrno.ESUCCESS;
486
+ })
487
+ : syscallWrap(this, 'random_get', function (buf, buf_len) {
488
+ buf = Number(buf);
489
+ if (buf === 0) {
490
+ return types_1.WasiErrno.EINVAL;
491
+ }
492
+ buf_len = Number(buf_len);
493
+ const { view } = getMemory(this);
494
+ for (let i = buf; i < buf + buf_len; ++i) {
495
+ view.setUint8(i, Math.floor(Math.random() * 256));
496
+ }
497
+ return types_1.WasiErrno.ESUCCESS;
498
+ });
499
+ this.sock_recv = syscallWrap(this, 'sock_recv', function () {
500
+ return types_1.WasiErrno.ENOTSUP;
501
+ });
502
+ this.sock_send = syscallWrap(this, 'sock_send', function () {
503
+ return types_1.WasiErrno.ENOTSUP;
504
+ });
505
+ this.sock_shutdown = syscallWrap(this, 'sock_shutdown', function () {
506
+ return types_1.WasiErrno.ENOTSUP;
507
+ });
508
+ this.sock_accept = syscallWrap(this, 'sock_accept', function () {
509
+ return types_1.WasiErrno.ENOTSUP;
510
+ });
511
+ _wasi.set(this, {
512
+ fds,
513
+ args,
514
+ env
515
+ });
516
+ if (fs)
517
+ _fs.set(this, fs);
518
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
519
+ const _this = this;
520
+ function defineImport(name, syncVersion, asyncVersion, parameterType, returnType) {
521
+ if (asyncFs) {
522
+ if (asyncify) {
523
+ _this[name] = asyncify.wrapImportFunction(syscallWrap(_this, name, asyncVersion));
524
+ }
525
+ else {
526
+ _this[name] = (0, jspi_1.wrapAsyncImport)(syscallWrap(_this, name, asyncVersion), parameterType, returnType);
527
+ }
528
+ }
529
+ else {
530
+ _this[name] = syscallWrap(_this, name, syncVersion);
531
+ }
532
+ }
533
+ defineImport('fd_allocate', function fd_allocate(fd, offset, len) {
534
+ const wasi = _wasi.get(this);
535
+ const fs = getFs(this);
536
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_ALLOCATE, BigInt(0));
537
+ const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
538
+ if (stat.size < offset + len) {
539
+ fs.ftruncateSync(fileDescriptor.fd, Number(offset + len));
540
+ }
541
+ return types_1.WasiErrno.ESUCCESS;
542
+ }, async function fd_allocate(fd, offset, len) {
543
+ const wasi = _wasi.get(this);
544
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_ALLOCATE, BigInt(0));
545
+ const h = fileDescriptor.fd;
546
+ const stat = await h.stat({ bigint: true });
547
+ if (stat.size < offset + len) {
548
+ await h.truncate(Number(offset + len));
549
+ }
550
+ return types_1.WasiErrno.ESUCCESS;
551
+ }, ['i32', 'i64', 'f64'], ['i32']);
552
+ defineImport('fd_close', function fd_close(fd) {
553
+ const wasi = _wasi.get(this);
554
+ const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
555
+ const fs = getFs(this);
556
+ fs.closeSync(fileDescriptor.fd);
557
+ wasi.fds.remove(fd);
558
+ return types_1.WasiErrno.ESUCCESS;
559
+ }, async function fd_close(fd) {
560
+ const wasi = _wasi.get(this);
561
+ const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
562
+ await fileDescriptor.fd.close();
563
+ wasi.fds.remove(fd);
564
+ return types_1.WasiErrno.ESUCCESS;
565
+ }, ['i32'], ['i32']);
566
+ defineImport('fd_datasync', function fd_datasync(fd) {
567
+ const wasi = _wasi.get(this);
568
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_DATASYNC, BigInt(0));
569
+ const fs = getFs(this);
570
+ fs.fdatasyncSync(fileDescriptor.fd);
571
+ return types_1.WasiErrno.ESUCCESS;
572
+ }, async function fd_datasync(fd) {
573
+ const wasi = _wasi.get(this);
574
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_DATASYNC, BigInt(0));
575
+ await fileDescriptor.fd.datasync();
576
+ return types_1.WasiErrno.ESUCCESS;
577
+ }, ['i32'], ['i32']);
578
+ defineImport('fd_filestat_get', function fd_filestat_get(fd, buf) {
579
+ buf = Number(buf);
580
+ if (buf === 0)
581
+ return types_1.WasiErrno.EINVAL;
582
+ const wasi = _wasi.get(this);
583
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_FILESTAT_GET, BigInt(0));
584
+ const fs = getFs(this);
585
+ const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
586
+ const { view } = getMemory(this);
587
+ (0, fd_1.toFileStat)(view, buf, stat);
588
+ return types_1.WasiErrno.ESUCCESS;
589
+ }, async function fd_filestat_get(fd, buf) {
590
+ buf = Number(buf);
591
+ if (buf === 0)
592
+ return types_1.WasiErrno.EINVAL;
593
+ const wasi = _wasi.get(this);
594
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_FILESTAT_GET, BigInt(0));
595
+ const h = fileDescriptor.fd;
596
+ const stat = await h.stat({ bigint: true });
597
+ const { view } = getMemory(this);
598
+ (0, fd_1.toFileStat)(view, buf, stat);
599
+ return types_1.WasiErrno.ESUCCESS;
600
+ }, ['i32', 'i32'], ['i32']);
601
+ defineImport('fd_filestat_set_size', function fd_filestat_set_size(fd, size) {
602
+ const wasi = _wasi.get(this);
603
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_FILESTAT_SET_SIZE, BigInt(0));
604
+ const fs = getFs(this);
605
+ fs.ftruncateSync(fileDescriptor.fd, Number(size));
606
+ return types_1.WasiErrno.ESUCCESS;
607
+ }, async function fd_filestat_set_size(fd, size) {
608
+ const wasi = _wasi.get(this);
609
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_FILESTAT_SET_SIZE, BigInt(0));
610
+ const h = fileDescriptor.fd;
611
+ await h.truncate(Number(size));
612
+ return types_1.WasiErrno.ESUCCESS;
613
+ }, ['i32', 'i64'], ['i32']);
614
+ function fdFilestatGetTimes(fd, atim, mtim, flags) {
615
+ const wasi = _wasi.get(this);
616
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_FILESTAT_SET_TIMES, BigInt(0));
617
+ if ((flags & types_1.WasiFstFlag.SET_ATIM_NOW) === types_1.WasiFstFlag.SET_ATIM_NOW) {
618
+ atim = BigInt(Date.now() * 1000000);
619
+ }
620
+ if ((flags & types_1.WasiFstFlag.SET_MTIM_NOW) === types_1.WasiFstFlag.SET_MTIM_NOW) {
621
+ mtim = BigInt(Date.now() * 1000000);
622
+ }
623
+ return { fileDescriptor, atim, mtim };
624
+ }
625
+ defineImport('fd_filestat_set_times', function fd_filestat_set_times(fd, atim, mtim, flags) {
626
+ if (validateFstFlagsOrReturn(flags)) {
627
+ return types_1.WasiErrno.EINVAL;
628
+ }
629
+ const { fileDescriptor, atim: atimRes, mtim: mtimRes } = fdFilestatGetTimes.call(this, fd, atim, mtim, flags);
630
+ const fs = getFs(this);
631
+ fs.futimesSync(fileDescriptor.fd, Number(atimRes), Number(mtimRes));
632
+ return types_1.WasiErrno.ESUCCESS;
633
+ }, async function fd_filestat_set_times(fd, atim, mtim, flags) {
634
+ if (validateFstFlagsOrReturn(flags)) {
635
+ return types_1.WasiErrno.EINVAL;
636
+ }
637
+ const { fileDescriptor, atim: atimRes, mtim: mtimRes } = fdFilestatGetTimes.call(this, fd, atim, mtim, flags);
638
+ const h = fileDescriptor.fd;
639
+ await h.utimes(Number(atimRes), Number(mtimRes));
640
+ return types_1.WasiErrno.ESUCCESS;
641
+ }, ['i32', 'i64', 'i64', 'i32'], ['i32']);
642
+ defineImport('fd_pread', function fd_pread(fd, iovs, iovslen, offset, size) {
643
+ iovs = Number(iovs);
644
+ size = Number(size);
645
+ if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
646
+ return types_1.WasiErrno.EINVAL;
647
+ }
648
+ const { HEAPU8, view } = getMemory(this);
649
+ const wasi = _wasi.get(this);
650
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_READ | types_1.WasiRights.FD_SEEK, BigInt(0));
651
+ if (!iovslen) {
652
+ view.setUint32(size, 0, true);
653
+ return types_1.WasiErrno.ESUCCESS;
654
+ }
655
+ let totalSize = 0;
656
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
657
+ const offset = iovs + (i * 8);
658
+ const buf = view.getInt32(offset, true);
659
+ const bufLen = view.getUint32(offset + 4, true);
660
+ totalSize += bufLen;
661
+ return HEAPU8.subarray(buf, buf + bufLen);
662
+ });
663
+ let nread = 0;
664
+ const buffer = (() => {
665
+ try {
666
+ return new Uint8Array(new SharedArrayBuffer(totalSize));
667
+ }
668
+ catch (_) {
669
+ return new Uint8Array(totalSize);
670
+ }
671
+ })();
672
+ buffer._isBuffer = true;
673
+ const fs = getFs(this);
674
+ const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
675
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
676
+ view.setUint32(size, nread, true);
677
+ return types_1.WasiErrno.ESUCCESS;
678
+ }, async function (fd, iovs, iovslen, offset, size) {
679
+ iovs = Number(iovs);
680
+ size = Number(size);
681
+ if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
682
+ return types_1.WasiErrno.EINVAL;
683
+ }
684
+ const { HEAPU8, view } = getMemory(this);
685
+ const wasi = _wasi.get(this);
686
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_READ | types_1.WasiRights.FD_SEEK, BigInt(0));
687
+ if (!iovslen) {
688
+ view.setUint32(size, 0, true);
689
+ return types_1.WasiErrno.ESUCCESS;
690
+ }
691
+ let totalSize = 0;
692
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
693
+ const offset = iovs + (i * 8);
694
+ const buf = view.getInt32(offset, true);
695
+ const bufLen = view.getUint32(offset + 4, true);
696
+ totalSize += bufLen;
697
+ return HEAPU8.subarray(buf, buf + bufLen);
698
+ });
699
+ let nread = 0;
700
+ const buffer = new Uint8Array(totalSize);
701
+ buffer._isBuffer = true;
702
+ const { bytesRead } = await fileDescriptor.fd.read(buffer, 0, buffer.length, Number(offset));
703
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
704
+ view.setUint32(size, nread, true);
705
+ return types_1.WasiErrno.ESUCCESS;
706
+ }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
707
+ defineImport('fd_pwrite', function fd_pwrite(fd, iovs, iovslen, offset, size) {
708
+ iovs = Number(iovs);
709
+ size = Number(size);
710
+ if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
711
+ return types_1.WasiErrno.EINVAL;
712
+ }
713
+ const { HEAPU8, view } = getMemory(this);
714
+ const wasi = _wasi.get(this);
715
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_WRITE | types_1.WasiRights.FD_SEEK, BigInt(0));
716
+ if (!iovslen) {
717
+ view.setUint32(size, 0, true);
718
+ return types_1.WasiErrno.ESUCCESS;
719
+ }
720
+ const buffer = (0, fd_1.concatBuffer)(Array.from({ length: Number(iovslen) }, (_, i) => {
721
+ const offset = iovs + (i * 8);
722
+ const buf = view.getInt32(offset, true);
723
+ const bufLen = view.getUint32(offset + 4, true);
724
+ return HEAPU8.subarray(buf, buf + bufLen);
725
+ }));
726
+ const fs = getFs(this);
727
+ const nwritten = fs.writeSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
728
+ view.setUint32(size, nwritten, true);
729
+ return types_1.WasiErrno.ESUCCESS;
730
+ }, async function fd_pwrite(fd, iovs, iovslen, offset, size) {
731
+ iovs = Number(iovs);
732
+ size = Number(size);
733
+ if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
734
+ return types_1.WasiErrno.EINVAL;
735
+ }
736
+ const { HEAPU8, view } = getMemory(this);
737
+ const wasi = _wasi.get(this);
738
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_WRITE | types_1.WasiRights.FD_SEEK, BigInt(0));
739
+ if (!iovslen) {
740
+ view.setUint32(size, 0, true);
741
+ return types_1.WasiErrno.ESUCCESS;
742
+ }
743
+ const buffer = (0, fd_1.concatBuffer)(Array.from({ length: Number(iovslen) }, (_, i) => {
744
+ const offset = iovs + (i * 8);
745
+ const buf = view.getInt32(offset, true);
746
+ const bufLen = view.getUint32(offset + 4, true);
747
+ return HEAPU8.subarray(buf, buf + bufLen);
748
+ }));
749
+ const { bytesWritten } = await fileDescriptor.fd.write(buffer, 0, buffer.length, Number(offset));
750
+ view.setUint32(size, bytesWritten, true);
751
+ return types_1.WasiErrno.ESUCCESS;
752
+ }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
753
+ defineImport('fd_read', function fd_read(fd, iovs, iovslen, size) {
754
+ iovs = Number(iovs);
755
+ size = Number(size);
756
+ if ((iovs === 0 && iovslen) || size === 0) {
757
+ return types_1.WasiErrno.EINVAL;
758
+ }
759
+ const { HEAPU8, view } = getMemory(this);
760
+ const wasi = _wasi.get(this);
761
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_READ, BigInt(0));
762
+ if (!iovslen) {
763
+ view.setUint32(size, 0, true);
764
+ return types_1.WasiErrno.ESUCCESS;
765
+ }
766
+ let totalSize = 0;
767
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
768
+ const offset = iovs + (i * 8);
769
+ const buf = view.getInt32(offset, true);
770
+ const bufLen = view.getUint32(offset + 4, true);
771
+ totalSize += bufLen;
772
+ return HEAPU8.subarray(buf, buf + bufLen);
773
+ });
774
+ let buffer;
775
+ let nread = 0;
776
+ if (fd === 0) {
777
+ if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
778
+ return types_1.WasiErrno.ENOTSUP;
779
+ }
780
+ buffer = readStdin();
781
+ nread = buffer ? copyMemory(ioVecs, buffer) : 0;
782
+ }
783
+ else {
784
+ buffer = (() => {
785
+ try {
786
+ return new Uint8Array(new SharedArrayBuffer(totalSize));
787
+ }
788
+ catch (_) {
789
+ return new Uint8Array(totalSize);
790
+ }
791
+ })();
792
+ buffer._isBuffer = true;
793
+ const fs = getFs(this);
794
+ const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(fileDescriptor.pos));
795
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
796
+ fileDescriptor.pos += BigInt(nread);
797
+ }
798
+ view.setUint32(size, nread, true);
799
+ return types_1.WasiErrno.ESUCCESS;
800
+ }, async function fd_read(fd, iovs, iovslen, size) {
801
+ iovs = Number(iovs);
802
+ size = Number(size);
803
+ if ((iovs === 0 && iovslen) || size === 0) {
804
+ return types_1.WasiErrno.EINVAL;
805
+ }
806
+ const { HEAPU8, view } = getMemory(this);
807
+ const wasi = _wasi.get(this);
808
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_READ, BigInt(0));
809
+ if (!iovslen) {
810
+ view.setUint32(size, 0, true);
811
+ return types_1.WasiErrno.ESUCCESS;
812
+ }
813
+ let totalSize = 0;
814
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
815
+ const offset = iovs + (i * 8);
816
+ const buf = view.getInt32(offset, true);
817
+ const bufLen = view.getUint32(offset + 4, true);
818
+ totalSize += bufLen;
819
+ return HEAPU8.subarray(buf, buf + bufLen);
820
+ });
821
+ let buffer;
822
+ let nread = 0;
823
+ if (fd === 0) {
824
+ if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
825
+ return types_1.WasiErrno.ENOTSUP;
826
+ }
827
+ buffer = readStdin();
828
+ nread = buffer ? copyMemory(ioVecs, buffer) : 0;
829
+ }
830
+ else {
831
+ buffer = new Uint8Array(totalSize);
832
+ buffer._isBuffer = true;
833
+ const { bytesRead } = await fileDescriptor.fd.read(buffer, 0, buffer.length, Number(fileDescriptor.pos));
834
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
835
+ fileDescriptor.pos += BigInt(nread);
836
+ }
837
+ view.setUint32(size, nread, true);
838
+ return types_1.WasiErrno.ESUCCESS;
839
+ }, ['i32', 'i32', 'i32', 'i32'], ['i32']);
840
+ defineImport('fd_readdir', function fd_readdir(fd, buf, buf_len, cookie, bufused) {
841
+ buf = Number(buf);
842
+ buf_len = Number(buf_len);
843
+ bufused = Number(bufused);
844
+ if (buf === 0 || bufused === 0)
845
+ return types_1.WasiErrno.ESUCCESS;
846
+ const wasi = _wasi.get(this);
847
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_READDIR, BigInt(0));
848
+ const fs = getFs(this);
849
+ const entries = fs.readdirSync(fileDescriptor.realPath, { withFileTypes: true });
850
+ const { HEAPU8, view } = getMemory(this);
851
+ let bufferUsed = 0;
852
+ for (let i = Number(cookie); i < entries.length; i++) {
853
+ const nameData = encoder.encode(entries[i].name);
854
+ const entryInfo = fs.statSync((0, path_1.resolve)(fileDescriptor.realPath, entries[i].name), { bigint: true });
855
+ const entryData = new Uint8Array(24 + nameData.byteLength);
856
+ const entryView = new DataView(entryData.buffer);
857
+ entryView.setBigUint64(0, BigInt(i + 1), true);
858
+ entryView.setBigUint64(8, BigInt(entryInfo.ino ? entryInfo.ino : 0), true);
859
+ entryView.setUint32(16, nameData.byteLength, true);
860
+ let type;
861
+ if (entries[i].isFile()) {
862
+ type = types_1.WasiFileType.REGULAR_FILE;
863
+ }
864
+ else if (entries[i].isDirectory()) {
865
+ type = types_1.WasiFileType.DIRECTORY;
866
+ }
867
+ else if (entries[i].isSymbolicLink()) {
868
+ type = types_1.WasiFileType.SYMBOLIC_LINK;
869
+ }
870
+ else if (entries[i].isCharacterDevice()) {
871
+ type = types_1.WasiFileType.CHARACTER_DEVICE;
872
+ }
873
+ else if (entries[i].isBlockDevice()) {
874
+ type = types_1.WasiFileType.BLOCK_DEVICE;
875
+ }
876
+ else if (entries[i].isSocket()) {
877
+ type = types_1.WasiFileType.SOCKET_STREAM;
878
+ }
879
+ else {
880
+ type = types_1.WasiFileType.UNKNOWN;
881
+ }
882
+ entryView.setUint8(20, type);
883
+ entryData.set(nameData, 24);
884
+ const data = entryData.slice(0, Math.min(entryData.length, buf_len - bufferUsed));
885
+ HEAPU8.set(data, buf + bufferUsed);
886
+ bufferUsed += data.byteLength;
887
+ }
888
+ view.setUint32(bufused, bufferUsed, true);
889
+ return types_1.WasiErrno.ESUCCESS;
890
+ }, async function fd_readdir(fd, buf, buf_len, cookie, bufused) {
891
+ buf = Number(buf);
892
+ buf_len = Number(buf_len);
893
+ bufused = Number(bufused);
894
+ if (buf === 0 || bufused === 0)
895
+ return types_1.WasiErrno.ESUCCESS;
896
+ const wasi = _wasi.get(this);
897
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_READDIR, BigInt(0));
898
+ const fs = getFs(this);
899
+ const entries = await fs.promises.readdir(fileDescriptor.realPath, { withFileTypes: true });
900
+ const { HEAPU8, view } = getMemory(this);
901
+ let bufferUsed = 0;
902
+ for (let i = Number(cookie); i < entries.length; i++) {
903
+ const nameData = encoder.encode(entries[i].name);
904
+ const entryInfo = await fs.promises.stat((0, path_1.resolve)(fileDescriptor.realPath, entries[i].name), { bigint: true });
905
+ const entryData = new Uint8Array(24 + nameData.byteLength);
906
+ const entryView = new DataView(entryData.buffer);
907
+ entryView.setBigUint64(0, BigInt(i + 1), true);
908
+ entryView.setBigUint64(8, BigInt(entryInfo.ino ? entryInfo.ino : 0), true);
909
+ entryView.setUint32(16, nameData.byteLength, true);
910
+ let type;
911
+ if (entries[i].isFile()) {
912
+ type = types_1.WasiFileType.REGULAR_FILE;
913
+ }
914
+ else if (entries[i].isDirectory()) {
915
+ type = types_1.WasiFileType.DIRECTORY;
916
+ }
917
+ else if (entries[i].isSymbolicLink()) {
918
+ type = types_1.WasiFileType.SYMBOLIC_LINK;
919
+ }
920
+ else if (entries[i].isCharacterDevice()) {
921
+ type = types_1.WasiFileType.CHARACTER_DEVICE;
922
+ }
923
+ else if (entries[i].isBlockDevice()) {
924
+ type = types_1.WasiFileType.BLOCK_DEVICE;
925
+ }
926
+ else if (entries[i].isSocket()) {
927
+ type = types_1.WasiFileType.SOCKET_STREAM;
928
+ }
929
+ else {
930
+ type = types_1.WasiFileType.UNKNOWN;
931
+ }
932
+ entryView.setUint8(20, type);
933
+ entryData.set(nameData, 24);
934
+ const data = entryData.slice(0, Math.min(entryData.length, buf_len - bufferUsed));
935
+ HEAPU8.set(data, buf + bufferUsed);
936
+ bufferUsed += data.byteLength;
937
+ }
938
+ view.setUint32(bufused, bufferUsed, true);
939
+ return types_1.WasiErrno.ESUCCESS;
940
+ }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
941
+ defineImport('fd_renumber', function fd_renumber(from, to) {
942
+ const wasi = _wasi.get(this);
943
+ wasi.fds.renumber(to, from);
944
+ return types_1.WasiErrno.ESUCCESS;
945
+ }, async function fd_renumber(from, to) {
946
+ const wasi = _wasi.get(this);
947
+ await wasi.fds.renumber(to, from);
948
+ return types_1.WasiErrno.ESUCCESS;
949
+ }, ['i32', 'i32'], ['i32']);
950
+ defineImport('fd_sync', function fd_sync(fd) {
951
+ const wasi = _wasi.get(this);
952
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_SYNC, BigInt(0));
953
+ const fs = getFs(this);
954
+ fs.fsyncSync(fileDescriptor.fd);
955
+ return types_1.WasiErrno.ESUCCESS;
956
+ }, async function fd_sync(fd) {
957
+ const wasi = _wasi.get(this);
958
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_SYNC, BigInt(0));
959
+ await fileDescriptor.fd.sync();
960
+ return types_1.WasiErrno.ESUCCESS;
961
+ }, ['i32'], ['i32']);
962
+ defineImport('fd_write', function fd_write(fd, iovs, iovslen, size) {
963
+ iovs = Number(iovs);
964
+ size = Number(size);
965
+ if ((iovs === 0 && iovslen) || size === 0) {
966
+ return types_1.WasiErrno.EINVAL;
967
+ }
968
+ const { HEAPU8, view } = getMemory(this);
969
+ const wasi = _wasi.get(this);
970
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_WRITE, BigInt(0));
971
+ if (!iovslen) {
972
+ view.setUint32(size, 0, true);
973
+ return types_1.WasiErrno.ESUCCESS;
974
+ }
975
+ const buffer = (0, fd_1.concatBuffer)(Array.from({ length: Number(iovslen) }, (_, i) => {
976
+ const offset = iovs + (i * 8);
977
+ const buf = view.getInt32(offset, true);
978
+ const bufLen = view.getUint32(offset + 4, true);
979
+ return HEAPU8.subarray(buf, buf + bufLen);
980
+ }));
981
+ let nwritten;
982
+ if (fd === 1 || fd === 2) {
983
+ nwritten = fileDescriptor.write(buffer);
984
+ }
985
+ else {
986
+ const fs = getFs(this);
987
+ nwritten = fs.writeSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(fileDescriptor.pos));
988
+ fileDescriptor.pos += BigInt(nwritten);
989
+ }
990
+ view.setUint32(size, nwritten, true);
991
+ return types_1.WasiErrno.ESUCCESS;
992
+ }, async function fd_write(fd, iovs, iovslen, size) {
993
+ iovs = Number(iovs);
994
+ size = Number(size);
995
+ if ((iovs === 0 && iovslen) || size === 0) {
996
+ return types_1.WasiErrno.EINVAL;
997
+ }
998
+ const { HEAPU8, view } = getMemory(this);
999
+ const wasi = _wasi.get(this);
1000
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.FD_WRITE, BigInt(0));
1001
+ if (!iovslen) {
1002
+ view.setUint32(size, 0, true);
1003
+ return types_1.WasiErrno.ESUCCESS;
1004
+ }
1005
+ const buffer = (0, fd_1.concatBuffer)(Array.from({ length: Number(iovslen) }, (_, i) => {
1006
+ const offset = iovs + (i * 8);
1007
+ const buf = view.getInt32(offset, true);
1008
+ const bufLen = view.getUint32(offset + 4, true);
1009
+ return HEAPU8.subarray(buf, buf + bufLen);
1010
+ }));
1011
+ let nwritten;
1012
+ if (fd === 1 || fd === 2) {
1013
+ nwritten = fileDescriptor.write(buffer);
1014
+ }
1015
+ else {
1016
+ nwritten = await (await (fileDescriptor.fd.write(buffer, 0, buffer.length, Number(fileDescriptor.pos)))).bytesWritten;
1017
+ fileDescriptor.pos += BigInt(nwritten);
1018
+ }
1019
+ view.setUint32(size, nwritten, true);
1020
+ return types_1.WasiErrno.ESUCCESS;
1021
+ }, ['i32', 'i32', 'i32', 'i32'], ['i32']);
1022
+ defineImport('path_create_directory', function path_create_directory(fd, path, path_len) {
1023
+ path = Number(path);
1024
+ path_len = Number(path_len);
1025
+ if (path === 0) {
1026
+ return types_1.WasiErrno.EINVAL;
1027
+ }
1028
+ const { HEAPU8 } = getMemory(this);
1029
+ const wasi = _wasi.get(this);
1030
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
1031
+ let pathString = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len));
1032
+ pathString = (0, path_1.resolve)(fileDescriptor.realPath, pathString);
1033
+ const fs = getFs(this);
1034
+ fs.mkdirSync(pathString);
1035
+ return types_1.WasiErrno.ESUCCESS;
1036
+ }, async function path_create_directory(fd, path, path_len) {
1037
+ path = Number(path);
1038
+ path_len = Number(path_len);
1039
+ if (path === 0) {
1040
+ return types_1.WasiErrno.EINVAL;
1041
+ }
1042
+ const { HEAPU8 } = getMemory(this);
1043
+ const wasi = _wasi.get(this);
1044
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
1045
+ let pathString = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len));
1046
+ pathString = (0, path_1.resolve)(fileDescriptor.realPath, pathString);
1047
+ const fs = getFs(this);
1048
+ await fs.promises.mkdir(pathString);
1049
+ return types_1.WasiErrno.ESUCCESS;
1050
+ }, ['i32', 'i32', 'i32'], ['i32']);
1051
+ defineImport('path_filestat_get', function path_filestat_get(fd, flags, path, path_len, filestat) {
1052
+ path = Number(path);
1053
+ path_len = Number(path_len);
1054
+ filestat = Number(filestat);
1055
+ if (path === 0 || filestat === 0) {
1056
+ return types_1.WasiErrno.EINVAL;
1057
+ }
1058
+ const { HEAPU8, view } = getMemory(this);
1059
+ const wasi = _wasi.get(this);
1060
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_FILESTAT_GET, BigInt(0));
1061
+ let pathString = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len));
1062
+ const fs = getFs(this);
1063
+ pathString = (0, path_1.resolve)(fileDescriptor.realPath, pathString);
1064
+ let stat;
1065
+ if ((flags & 1) === 1) {
1066
+ stat = fs.statSync(pathString, { bigint: true });
1067
+ }
1068
+ else {
1069
+ stat = fs.lstatSync(pathString, { bigint: true });
1070
+ }
1071
+ (0, fd_1.toFileStat)(view, filestat, stat);
1072
+ return types_1.WasiErrno.ESUCCESS;
1073
+ }, async function path_filestat_get(fd, flags, path, path_len, filestat) {
1074
+ path = Number(path);
1075
+ path_len = Number(path_len);
1076
+ filestat = Number(filestat);
1077
+ if (path === 0 || filestat === 0) {
1078
+ return types_1.WasiErrno.EINVAL;
1079
+ }
1080
+ const { HEAPU8, view } = getMemory(this);
1081
+ const wasi = _wasi.get(this);
1082
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_FILESTAT_GET, BigInt(0));
1083
+ let pathString = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len));
1084
+ const fs = getFs(this);
1085
+ pathString = (0, path_1.resolve)(fileDescriptor.realPath, pathString);
1086
+ let stat;
1087
+ if ((flags & 1) === 1) {
1088
+ stat = await fs.promises.stat(pathString, { bigint: true });
1089
+ }
1090
+ else {
1091
+ stat = await fs.promises.lstat(pathString, { bigint: true });
1092
+ }
1093
+ (0, fd_1.toFileStat)(view, filestat, stat);
1094
+ return types_1.WasiErrno.ESUCCESS;
1095
+ }, ['i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1096
+ defineImport('path_filestat_set_times', function path_filestat_set_times(fd, flags, path, path_len, atim, mtim, fst_flags) {
1097
+ path = Number(path);
1098
+ path_len = Number(path_len);
1099
+ if (path === 0)
1100
+ return types_1.WasiErrno.EINVAL;
1101
+ const { HEAPU8 } = getMemory(this);
1102
+ const wasi = _wasi.get(this);
1103
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1104
+ if (validateFstFlagsOrReturn(fst_flags)) {
1105
+ return types_1.WasiErrno.EINVAL;
1106
+ }
1107
+ const fs = getFs(this);
1108
+ const resolvedPath = resolvePathSync(fs, fileDescriptor, decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len)), flags);
1109
+ if ((fst_flags & types_1.WasiFstFlag.SET_ATIM_NOW) === types_1.WasiFstFlag.SET_ATIM_NOW) {
1110
+ atim = BigInt(Date.now() * 1000000);
1111
+ }
1112
+ if ((fst_flags & types_1.WasiFstFlag.SET_MTIM_NOW) === types_1.WasiFstFlag.SET_MTIM_NOW) {
1113
+ mtim = BigInt(Date.now() * 1000000);
1114
+ }
1115
+ fs.utimesSync(resolvedPath, Number(atim), Number(mtim));
1116
+ return types_1.WasiErrno.ESUCCESS;
1117
+ }, async function path_filestat_set_times(fd, flags, path, path_len, atim, mtim, fst_flags) {
1118
+ path = Number(path);
1119
+ path_len = Number(path_len);
1120
+ if (path === 0)
1121
+ return types_1.WasiErrno.EINVAL;
1122
+ const { HEAPU8 } = getMemory(this);
1123
+ const wasi = _wasi.get(this);
1124
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1125
+ if (validateFstFlagsOrReturn(fst_flags)) {
1126
+ return types_1.WasiErrno.EINVAL;
1127
+ }
1128
+ const fs = getFs(this);
1129
+ const resolvedPath = await resolvePathAsync(fs, fileDescriptor, decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len)), flags);
1130
+ if ((fst_flags & types_1.WasiFstFlag.SET_ATIM_NOW) === types_1.WasiFstFlag.SET_ATIM_NOW) {
1131
+ atim = BigInt(Date.now() * 1000000);
1132
+ }
1133
+ if ((fst_flags & types_1.WasiFstFlag.SET_MTIM_NOW) === types_1.WasiFstFlag.SET_MTIM_NOW) {
1134
+ mtim = BigInt(Date.now() * 1000000);
1135
+ }
1136
+ await fs.promises.utimes(resolvedPath, Number(atim), Number(mtim));
1137
+ return types_1.WasiErrno.ESUCCESS;
1138
+ }, ['i32', 'i32', 'i32', 'i32', 'i64', 'i64', 'i32'], ['i32']);
1139
+ defineImport('path_link', function path_link(old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
1140
+ old_path = Number(old_path);
1141
+ old_path_len = Number(old_path_len);
1142
+ new_path = Number(new_path);
1143
+ new_path_len = Number(new_path_len);
1144
+ if (old_path === 0 || new_path === 0) {
1145
+ return types_1.WasiErrno.EINVAL;
1146
+ }
1147
+ const wasi = _wasi.get(this);
1148
+ let oldWrap;
1149
+ let newWrap;
1150
+ if (old_fd === new_fd) {
1151
+ oldWrap = newWrap = wasi.fds.get(old_fd, types_1.WasiRights.PATH_LINK_SOURCE | types_1.WasiRights.PATH_LINK_TARGET, BigInt(0));
1152
+ }
1153
+ else {
1154
+ oldWrap = wasi.fds.get(old_fd, types_1.WasiRights.PATH_LINK_SOURCE, BigInt(0));
1155
+ newWrap = wasi.fds.get(new_fd, types_1.WasiRights.PATH_LINK_TARGET, BigInt(0));
1156
+ }
1157
+ const { HEAPU8 } = getMemory(this);
1158
+ const fs = getFs(this);
1159
+ const resolvedOldPath = resolvePathSync(fs, oldWrap, decoder.decode((0, util_1.unsharedSlice)(HEAPU8, old_path, old_path + old_path_len)), old_flags);
1160
+ const resolvedNewPath = (0, path_1.resolve)(newWrap.realPath, decoder.decode((0, util_1.unsharedSlice)(HEAPU8, new_path, new_path + new_path_len)));
1161
+ fs.linkSync(resolvedOldPath, resolvedNewPath);
1162
+ return types_1.WasiErrno.ESUCCESS;
1163
+ }, async function path_link(old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
1164
+ old_path = Number(old_path);
1165
+ old_path_len = Number(old_path_len);
1166
+ new_path = Number(new_path);
1167
+ new_path_len = Number(new_path_len);
1168
+ if (old_path === 0 || new_path === 0) {
1169
+ return types_1.WasiErrno.EINVAL;
1170
+ }
1171
+ const wasi = _wasi.get(this);
1172
+ let oldWrap;
1173
+ let newWrap;
1174
+ if (old_fd === new_fd) {
1175
+ oldWrap = newWrap = wasi.fds.get(old_fd, types_1.WasiRights.PATH_LINK_SOURCE | types_1.WasiRights.PATH_LINK_TARGET, BigInt(0));
1176
+ }
1177
+ else {
1178
+ oldWrap = wasi.fds.get(old_fd, types_1.WasiRights.PATH_LINK_SOURCE, BigInt(0));
1179
+ newWrap = wasi.fds.get(new_fd, types_1.WasiRights.PATH_LINK_TARGET, BigInt(0));
1180
+ }
1181
+ const { HEAPU8 } = getMemory(this);
1182
+ const fs = getFs(this);
1183
+ const resolvedOldPath = await resolvePathAsync(fs, oldWrap, decoder.decode((0, util_1.unsharedSlice)(HEAPU8, old_path, old_path + old_path_len)), old_flags);
1184
+ const resolvedNewPath = (0, path_1.resolve)(newWrap.realPath, decoder.decode((0, util_1.unsharedSlice)(HEAPU8, new_path, new_path + new_path_len)));
1185
+ await fs.promises.link(resolvedOldPath, resolvedNewPath);
1186
+ return types_1.WasiErrno.ESUCCESS;
1187
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1188
+ function pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags) {
1189
+ const read = (fs_rights_base & (types_1.WasiRights.FD_READ |
1190
+ types_1.WasiRights.FD_READDIR)) !== BigInt(0);
1191
+ const write = (fs_rights_base & (types_1.WasiRights.FD_DATASYNC |
1192
+ types_1.WasiRights.FD_WRITE |
1193
+ types_1.WasiRights.FD_ALLOCATE |
1194
+ types_1.WasiRights.FD_FILESTAT_SET_SIZE)) !== BigInt(0);
1195
+ let flags = write ? read ? types_1.FileControlFlag.O_RDWR : types_1.FileControlFlag.O_WRONLY : types_1.FileControlFlag.O_RDONLY;
1196
+ let needed_base = types_1.WasiRights.PATH_OPEN;
1197
+ let needed_inheriting = fs_rights_base | fs_rights_inheriting;
1198
+ if ((o_flags & types_1.WasiFileControlFlag.O_CREAT) !== 0) {
1199
+ flags |= types_1.FileControlFlag.O_CREAT;
1200
+ needed_base |= types_1.WasiRights.PATH_CREATE_FILE;
1201
+ }
1202
+ if ((o_flags & types_1.WasiFileControlFlag.O_DIRECTORY) !== 0) {
1203
+ flags |= types_1.FileControlFlag.O_DIRECTORY;
1204
+ }
1205
+ if ((o_flags & types_1.WasiFileControlFlag.O_EXCL) !== 0) {
1206
+ flags |= types_1.FileControlFlag.O_EXCL;
1207
+ }
1208
+ if ((o_flags & types_1.WasiFileControlFlag.O_TRUNC) !== 0) {
1209
+ flags |= types_1.FileControlFlag.O_TRUNC;
1210
+ needed_base |= types_1.WasiRights.PATH_FILESTAT_SET_SIZE;
1211
+ }
1212
+ if ((fs_flags & types_1.WasiFdFlag.APPEND) !== 0) {
1213
+ flags |= types_1.FileControlFlag.O_APPEND;
1214
+ }
1215
+ if ((fs_flags & types_1.WasiFdFlag.DSYNC) !== 0) {
1216
+ // flags |= FileControlFlag.O_DSYNC;
1217
+ needed_inheriting |= types_1.WasiRights.FD_DATASYNC;
1218
+ }
1219
+ if ((fs_flags & types_1.WasiFdFlag.NONBLOCK) !== 0) {
1220
+ flags |= types_1.FileControlFlag.O_NONBLOCK;
1221
+ }
1222
+ if ((fs_flags & types_1.WasiFdFlag.RSYNC) !== 0) {
1223
+ flags |= types_1.FileControlFlag.O_SYNC;
1224
+ needed_inheriting |= types_1.WasiRights.FD_SYNC;
1225
+ }
1226
+ if ((fs_flags & types_1.WasiFdFlag.SYNC) !== 0) {
1227
+ flags |= types_1.FileControlFlag.O_SYNC;
1228
+ needed_inheriting |= types_1.WasiRights.FD_SYNC;
1229
+ }
1230
+ if (write && (flags & (types_1.FileControlFlag.O_APPEND | types_1.FileControlFlag.O_TRUNC)) === 0) {
1231
+ needed_inheriting |= types_1.WasiRights.FD_SEEK;
1232
+ }
1233
+ return { flags, needed_base, needed_inheriting };
1234
+ }
1235
+ defineImport('path_open', function path_open(dirfd, dirflags, path, path_len, o_flags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) {
1236
+ path = Number(path);
1237
+ fd = Number(fd);
1238
+ if (path === 0 || fd === 0) {
1239
+ return types_1.WasiErrno.EINVAL;
1240
+ }
1241
+ path_len = Number(path_len);
1242
+ fs_rights_base = BigInt(fs_rights_base);
1243
+ fs_rights_inheriting = BigInt(fs_rights_inheriting);
1244
+ const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
1245
+ const wasi = _wasi.get(this);
1246
+ const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1247
+ const memory = getMemory(this);
1248
+ const HEAPU8 = memory.HEAPU8;
1249
+ const pathString = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len));
1250
+ const fs = getFs(this);
1251
+ const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
1252
+ const r = fs.openSync(resolved_path, flagsRes, 0o666);
1253
+ const filetype = wasi.fds.getFileTypeByFd(r);
1254
+ if ((o_flags & types_1.WasiFileControlFlag.O_DIRECTORY) !== 0 && filetype !== types_1.WasiFileType.DIRECTORY) {
1255
+ return types_1.WasiErrno.ENOTDIR;
1256
+ }
1257
+ const { base: max_base, inheriting: max_inheriting } = (0, rights_1.getRights)(wasi.fds.stdio, r, flagsRes, filetype);
1258
+ const wrap = wasi.fds.insert(r, resolved_path, resolved_path, filetype, fs_rights_base & max_base, fs_rights_inheriting & max_inheriting, 0);
1259
+ const stat = fs.fstatSync(r, { bigint: true });
1260
+ if (stat.isFile()) {
1261
+ wrap.size = stat.size;
1262
+ if ((flagsRes & types_1.FileControlFlag.O_APPEND) !== 0) {
1263
+ wrap.pos = stat.size;
1264
+ }
1265
+ }
1266
+ const view = memory.view;
1267
+ view.setInt32(fd, wrap.id, true);
1268
+ return types_1.WasiErrno.ESUCCESS;
1269
+ }, async function path_open(dirfd, dirflags, path, path_len, o_flags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) {
1270
+ path = Number(path);
1271
+ fd = Number(fd);
1272
+ if (path === 0 || fd === 0) {
1273
+ return types_1.WasiErrno.EINVAL;
1274
+ }
1275
+ path_len = Number(path_len);
1276
+ fs_rights_base = BigInt(fs_rights_base);
1277
+ fs_rights_inheriting = BigInt(fs_rights_inheriting);
1278
+ const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
1279
+ const wasi = _wasi.get(this);
1280
+ const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1281
+ const memory = getMemory(this);
1282
+ const HEAPU8 = memory.HEAPU8;
1283
+ const pathString = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len));
1284
+ const fs = getFs(this);
1285
+ const resolved_path = await resolvePathAsync(fs, fileDescriptor, pathString, dirflags);
1286
+ const r = await fs.promises.open(resolved_path, flagsRes, 0o666);
1287
+ const filetype = await wasi.fds.getFileTypeByFd(r);
1288
+ if ((o_flags & types_1.WasiFileControlFlag.O_DIRECTORY) !== 0 && filetype !== types_1.WasiFileType.DIRECTORY) {
1289
+ return types_1.WasiErrno.ENOTDIR;
1290
+ }
1291
+ const { base: max_base, inheriting: max_inheriting } = (0, rights_1.getRights)(wasi.fds.stdio, r.fd, flagsRes, filetype);
1292
+ const wrap = wasi.fds.insert(r, resolved_path, resolved_path, filetype, fs_rights_base & max_base, fs_rights_inheriting & max_inheriting, 0);
1293
+ const stat = await r.stat({ bigint: true });
1294
+ if (stat.isFile()) {
1295
+ wrap.size = stat.size;
1296
+ if ((flagsRes & types_1.FileControlFlag.O_APPEND) !== 0) {
1297
+ wrap.pos = stat.size;
1298
+ }
1299
+ }
1300
+ const view = memory.view;
1301
+ view.setInt32(fd, wrap.id, true);
1302
+ return types_1.WasiErrno.ESUCCESS;
1303
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i64', 'i64', 'i32', 'i32'], ['i32']);
1304
+ defineImport('path_readlink', function path_readlink(fd, path, path_len, buf, buf_len, bufused) {
1305
+ path = Number(path);
1306
+ path_len = Number(path_len);
1307
+ buf = Number(buf);
1308
+ buf_len = Number(buf_len);
1309
+ bufused = Number(bufused);
1310
+ if (path === 0 || buf === 0 || bufused === 0) {
1311
+ return types_1.WasiErrno.EINVAL;
1312
+ }
1313
+ const { HEAPU8, view } = getMemory(this);
1314
+ const wasi = _wasi.get(this);
1315
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_READLINK, BigInt(0));
1316
+ let pathString = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len));
1317
+ pathString = (0, path_1.resolve)(fileDescriptor.realPath, pathString);
1318
+ const fs = getFs(this);
1319
+ const link = fs.readlinkSync(pathString);
1320
+ const linkData = encoder.encode(link);
1321
+ const len = Math.min(linkData.length, buf_len);
1322
+ if (len >= buf_len)
1323
+ return types_1.WasiErrno.ENOBUFS;
1324
+ HEAPU8.set(linkData.subarray(0, len), buf);
1325
+ HEAPU8[buf + len] = 0;
1326
+ view.setUint32(bufused, len, true);
1327
+ return types_1.WasiErrno.ESUCCESS;
1328
+ }, async function path_readlink(fd, path, path_len, buf, buf_len, bufused) {
1329
+ path = Number(path);
1330
+ path_len = Number(path_len);
1331
+ buf = Number(buf);
1332
+ buf_len = Number(buf_len);
1333
+ bufused = Number(bufused);
1334
+ if (path === 0 || buf === 0 || bufused === 0) {
1335
+ return types_1.WasiErrno.EINVAL;
1336
+ }
1337
+ const { HEAPU8, view } = getMemory(this);
1338
+ const wasi = _wasi.get(this);
1339
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_READLINK, BigInt(0));
1340
+ let pathString = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len));
1341
+ pathString = (0, path_1.resolve)(fileDescriptor.realPath, pathString);
1342
+ const fs = getFs(this);
1343
+ const link = await fs.promises.readlink(pathString);
1344
+ const linkData = encoder.encode(link);
1345
+ const len = Math.min(linkData.length, buf_len);
1346
+ if (len >= buf_len)
1347
+ return types_1.WasiErrno.ENOBUFS;
1348
+ HEAPU8.set(linkData.subarray(0, len), buf);
1349
+ HEAPU8[buf + len] = 0;
1350
+ view.setUint32(bufused, len, true);
1351
+ return types_1.WasiErrno.ESUCCESS;
1352
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1353
+ defineImport('path_remove_directory', function path_remove_directory(fd, path, path_len) {
1354
+ path = Number(path);
1355
+ path_len = Number(path_len);
1356
+ if (path === 0) {
1357
+ return types_1.WasiErrno.EINVAL;
1358
+ }
1359
+ const { HEAPU8 } = getMemory(this);
1360
+ const wasi = _wasi.get(this);
1361
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
1362
+ let pathString = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len));
1363
+ pathString = (0, path_1.resolve)(fileDescriptor.realPath, pathString);
1364
+ const fs = getFs(this);
1365
+ fs.rmdirSync(pathString);
1366
+ return types_1.WasiErrno.ESUCCESS;
1367
+ }, async function path_remove_directory(fd, path, path_len) {
1368
+ path = Number(path);
1369
+ path_len = Number(path_len);
1370
+ if (path === 0) {
1371
+ return types_1.WasiErrno.EINVAL;
1372
+ }
1373
+ const { HEAPU8 } = getMemory(this);
1374
+ const wasi = _wasi.get(this);
1375
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
1376
+ let pathString = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len));
1377
+ pathString = (0, path_1.resolve)(fileDescriptor.realPath, pathString);
1378
+ const fs = getFs(this);
1379
+ await fs.promises.rmdir(pathString);
1380
+ return types_1.WasiErrno.ESUCCESS;
1381
+ }, ['i32', 'i32', 'i32'], ['i32']);
1382
+ defineImport('path_rename', function path_rename(old_fd, old_path, old_path_len, new_fd, new_path, new_path_len) {
1383
+ old_path = Number(old_path);
1384
+ old_path_len = Number(old_path_len);
1385
+ new_path = Number(new_path);
1386
+ new_path_len = Number(new_path_len);
1387
+ if (old_path === 0 || new_path === 0) {
1388
+ return types_1.WasiErrno.EINVAL;
1389
+ }
1390
+ const wasi = _wasi.get(this);
1391
+ let oldWrap;
1392
+ let newWrap;
1393
+ if (old_fd === new_fd) {
1394
+ oldWrap = newWrap = wasi.fds.get(old_fd, types_1.WasiRights.PATH_RENAME_SOURCE | types_1.WasiRights.PATH_RENAME_TARGET, BigInt(0));
1395
+ }
1396
+ else {
1397
+ oldWrap = wasi.fds.get(old_fd, types_1.WasiRights.PATH_RENAME_SOURCE, BigInt(0));
1398
+ newWrap = wasi.fds.get(new_fd, types_1.WasiRights.PATH_RENAME_TARGET, BigInt(0));
1399
+ }
1400
+ const { HEAPU8 } = getMemory(this);
1401
+ const resolvedOldPath = (0, path_1.resolve)(oldWrap.realPath, decoder.decode((0, util_1.unsharedSlice)(HEAPU8, old_path, old_path + old_path_len)));
1402
+ const resolvedNewPath = (0, path_1.resolve)(newWrap.realPath, decoder.decode((0, util_1.unsharedSlice)(HEAPU8, new_path, new_path + new_path_len)));
1403
+ const fs = getFs(this);
1404
+ fs.renameSync(resolvedOldPath, resolvedNewPath);
1405
+ return types_1.WasiErrno.ESUCCESS;
1406
+ }, async function path_rename(old_fd, old_path, old_path_len, new_fd, new_path, new_path_len) {
1407
+ old_path = Number(old_path);
1408
+ old_path_len = Number(old_path_len);
1409
+ new_path = Number(new_path);
1410
+ new_path_len = Number(new_path_len);
1411
+ if (old_path === 0 || new_path === 0) {
1412
+ return types_1.WasiErrno.EINVAL;
1413
+ }
1414
+ const wasi = _wasi.get(this);
1415
+ let oldWrap;
1416
+ let newWrap;
1417
+ if (old_fd === new_fd) {
1418
+ oldWrap = newWrap = wasi.fds.get(old_fd, types_1.WasiRights.PATH_RENAME_SOURCE | types_1.WasiRights.PATH_RENAME_TARGET, BigInt(0));
1419
+ }
1420
+ else {
1421
+ oldWrap = wasi.fds.get(old_fd, types_1.WasiRights.PATH_RENAME_SOURCE, BigInt(0));
1422
+ newWrap = wasi.fds.get(new_fd, types_1.WasiRights.PATH_RENAME_TARGET, BigInt(0));
1423
+ }
1424
+ const { HEAPU8 } = getMemory(this);
1425
+ const resolvedOldPath = (0, path_1.resolve)(oldWrap.realPath, decoder.decode((0, util_1.unsharedSlice)(HEAPU8, old_path, old_path + old_path_len)));
1426
+ const resolvedNewPath = (0, path_1.resolve)(newWrap.realPath, decoder.decode((0, util_1.unsharedSlice)(HEAPU8, new_path, new_path + new_path_len)));
1427
+ const fs = getFs(this);
1428
+ await fs.promises.rename(resolvedOldPath, resolvedNewPath);
1429
+ return types_1.WasiErrno.ESUCCESS;
1430
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1431
+ defineImport('path_symlink', function path_symlink(old_path, old_path_len, fd, new_path, new_path_len) {
1432
+ old_path = Number(old_path);
1433
+ old_path_len = Number(old_path_len);
1434
+ new_path = Number(new_path);
1435
+ new_path_len = Number(new_path_len);
1436
+ if (old_path === 0 || new_path === 0) {
1437
+ return types_1.WasiErrno.EINVAL;
1438
+ }
1439
+ const { HEAPU8 } = getMemory(this);
1440
+ const wasi = _wasi.get(this);
1441
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_SYMLINK, BigInt(0));
1442
+ const oldPath = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, old_path, old_path + old_path_len));
1443
+ let newPath = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, new_path, new_path + new_path_len));
1444
+ newPath = (0, path_1.resolve)(fileDescriptor.realPath, newPath);
1445
+ const fs = getFs(this);
1446
+ fs.symlinkSync(oldPath, newPath);
1447
+ return types_1.WasiErrno.ESUCCESS;
1448
+ }, async function path_symlink(old_path, old_path_len, fd, new_path, new_path_len) {
1449
+ old_path = Number(old_path);
1450
+ old_path_len = Number(old_path_len);
1451
+ new_path = Number(new_path);
1452
+ new_path_len = Number(new_path_len);
1453
+ if (old_path === 0 || new_path === 0) {
1454
+ return types_1.WasiErrno.EINVAL;
1455
+ }
1456
+ const { HEAPU8 } = getMemory(this);
1457
+ const wasi = _wasi.get(this);
1458
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_SYMLINK, BigInt(0));
1459
+ const oldPath = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, old_path, old_path + old_path_len));
1460
+ let newPath = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, new_path, new_path + new_path_len));
1461
+ newPath = (0, path_1.resolve)(fileDescriptor.realPath, newPath);
1462
+ const fs = getFs(this);
1463
+ await fs.promises.symlink(oldPath, newPath);
1464
+ return types_1.WasiErrno.ESUCCESS;
1465
+ }, ['i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1466
+ defineImport('path_unlink_file', function path_unlink_file(fd, path, path_len) {
1467
+ path = Number(path);
1468
+ path_len = Number(path_len);
1469
+ if (path === 0) {
1470
+ return types_1.WasiErrno.EINVAL;
1471
+ }
1472
+ const { HEAPU8 } = getMemory(this);
1473
+ const wasi = _wasi.get(this);
1474
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_UNLINK_FILE, BigInt(0));
1475
+ let pathString = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len));
1476
+ pathString = (0, path_1.resolve)(fileDescriptor.realPath, pathString);
1477
+ const fs = getFs(this);
1478
+ fs.unlinkSync(pathString);
1479
+ return types_1.WasiErrno.ESUCCESS;
1480
+ }, async function path_unlink_file(fd, path, path_len) {
1481
+ path = Number(path);
1482
+ path_len = Number(path_len);
1483
+ if (path === 0) {
1484
+ return types_1.WasiErrno.EINVAL;
1485
+ }
1486
+ const { HEAPU8 } = getMemory(this);
1487
+ const wasi = _wasi.get(this);
1488
+ const fileDescriptor = wasi.fds.get(fd, types_1.WasiRights.PATH_UNLINK_FILE, BigInt(0));
1489
+ let pathString = decoder.decode((0, util_1.unsharedSlice)(HEAPU8, path, path + path_len));
1490
+ pathString = (0, path_1.resolve)(fileDescriptor.realPath, pathString);
1491
+ const fs = getFs(this);
1492
+ await fs.promises.unlink(pathString);
1493
+ return types_1.WasiErrno.ESUCCESS;
1494
+ }, ['i32', 'i32', 'i32'], ['i32']);
1495
+ this._setMemory = function setMemory(m) {
1496
+ if (!(m instanceof webassembly_1._WebAssembly.Memory)) {
1497
+ throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
1498
+ }
1499
+ _memory.set(_this, (0, memory_1.extendMemory)(m));
1500
+ };
1501
+ }
1502
+ static createSync(args, env, preopens, stdio, fs, print, printErr) {
1503
+ const fds = new fd_1.SyncTable({
1504
+ size: 3,
1505
+ in: stdio[0],
1506
+ out: stdio[1],
1507
+ err: stdio[2],
1508
+ fs,
1509
+ print,
1510
+ printErr
1511
+ });
1512
+ const _this = new WASI(args, env, fds, false, fs);
1513
+ if (preopens.length > 0) {
1514
+ for (let i = 0; i < preopens.length; ++i) {
1515
+ const realPath = fs.realpathSync(preopens[i].realPath, 'utf8');
1516
+ const fd = fs.openSync(realPath, 'r', 0o666);
1517
+ fds.insertPreopen(fd, preopens[i].mappedPath, realPath);
1518
+ }
1519
+ }
1520
+ return _this;
1521
+ }
1522
+ static async createAsync(args, env, preopens, stdio, fs, print, printErr, asyncify) {
1523
+ const fds = new fd_1.AsyncTable({
1524
+ size: 3,
1525
+ in: stdio[0],
1526
+ out: stdio[1],
1527
+ err: stdio[2],
1528
+ print,
1529
+ printErr
1530
+ });
1531
+ const _this = new WASI(args, env, fds, true, fs, asyncify);
1532
+ if (preopens.length > 0) {
1533
+ for (let i = 0; i < preopens.length; ++i) {
1534
+ const entry = preopens[i];
1535
+ const realPath = await fs.promises.realpath(entry.realPath);
1536
+ const fd = await fs.promises.open(realPath, 'r', 0o666);
1537
+ await fds.insertPreopen(fd, entry.mappedPath, realPath);
1538
+ }
1539
+ }
1540
+ return _this;
1541
+ }
1542
+ }
1543
+ exports.WASI = WASI;
1544
+ //# sourceMappingURL=preview1.js.map