@tybys/wasm-util 0.4.0 → 0.5.1

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.
package/dist/wasm-util.js CHANGED
@@ -141,18 +141,20 @@
141
141
  }
142
142
  }
143
143
  wrapImportFunction(f) {
144
- return ((...args) => {
144
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
145
+ const _this = this;
146
+ return (function () {
145
147
  // eslint-disable-next-line no-unreachable-loop
146
- while (this.exports.asyncify_get_state() === 2 /* AsyncifyState.REWINDING */) {
147
- this.exports.asyncify_stop_rewind();
148
- return this.value;
148
+ while (_this.exports.asyncify_get_state() === 2 /* AsyncifyState.REWINDING */) {
149
+ _this.exports.asyncify_stop_rewind();
150
+ return _this.value;
149
151
  }
150
- this.assertState();
151
- const v = f(...args);
152
+ _this.assertState();
153
+ const v = f.apply(this, arguments);
152
154
  if (!isPromiseLike(v))
153
155
  return v;
154
- this.exports.asyncify_start_unwind(this.dataPtr);
155
- this.value = v;
156
+ _this.exports.asyncify_start_unwind(_this.dataPtr);
157
+ _this.value = v;
156
158
  });
157
159
  }
158
160
  wrapImports(imports) {
@@ -174,17 +176,19 @@
174
176
  return importObject;
175
177
  }
176
178
  wrapExportFunction(f) {
177
- return (async (...args) => {
178
- this.assertState();
179
- let ret = f(...args);
180
- while (this.exports.asyncify_get_state() === 1 /* AsyncifyState.UNWINDING */) {
181
- this.exports.asyncify_stop_unwind();
182
- this.value = await this.value;
183
- this.assertState();
184
- this.exports.asyncify_start_rewind(this.dataPtr);
185
- ret = f();
186
- }
187
- this.assertState();
179
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
180
+ const _this = this;
181
+ return (async function () {
182
+ _this.assertState();
183
+ let ret = f.apply(this, arguments);
184
+ while (_this.exports.asyncify_get_state() === 1 /* AsyncifyState.UNWINDING */) {
185
+ _this.exports.asyncify_stop_unwind();
186
+ _this.value = await _this.value;
187
+ _this.assertState();
188
+ _this.exports.asyncify_start_rewind(_this.dataPtr);
189
+ ret = f.call(this);
190
+ }
191
+ _this.assertState();
188
192
  return ret;
189
193
  });
190
194
  }
@@ -197,21 +201,13 @@
197
201
  return ignore ? exportValue : this.wrapExportFunction(exportValue);
198
202
  });
199
203
  }
200
- }
201
- // /** @public */
202
- // export class Instance extends WebAssembly.Instance {
203
- // constructor (options: AsyncifyOptions, module: WebAssembly.Module, importObject?: WebAssembly.Imports) {
204
- // importObject = importObject ?? {}
205
- // const asyncify = new Asyncify()
206
- // super(module, asyncify.wrapImports(importObject))
207
- // asyncify.init(importObject, this, options)
208
- // }
209
- // get exports (): WebAssembly.Exports {
210
- // return wrappedExports.get(super.exports)!
211
- // }
212
- // }
213
- // Object.defineProperty(Instance.prototype, 'exports', { enumerable: true })
204
+ }
214
205
 
206
+ function validateImports(imports) {
207
+ if (imports && typeof imports !== 'object') {
208
+ throw new TypeError('imports must be an object or undefined');
209
+ }
210
+ }
215
211
  async function fetchWasm(urlOrBuffer, imports) {
216
212
  if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
217
213
  return await _WebAssembly.instantiate(urlOrBuffer, imports);
@@ -222,24 +218,12 @@
222
218
  return source;
223
219
  }
224
220
  /** @public */
225
- async function load(urlOrBuffer, imports, asyncify) {
226
- var _a, _b;
227
- if (imports && typeof imports !== 'object') {
228
- throw new TypeError('imports must be an object or undefined');
229
- }
221
+ async function load(urlOrBuffer, imports) {
222
+ validateImports(imports);
230
223
  imports = imports !== null && imports !== void 0 ? imports : {};
231
- let asyncifyHelper;
232
224
  let source;
233
- if (asyncify) {
234
- asyncifyHelper = new Asyncify();
235
- imports = asyncifyHelper.wrapImports(imports);
236
- }
237
225
  if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
238
226
  source = await _WebAssembly.instantiate(urlOrBuffer, imports);
239
- if (asyncify) {
240
- const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
241
- return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
242
- }
243
227
  return source;
244
228
  }
245
229
  if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
@@ -256,35 +240,41 @@
256
240
  else {
257
241
  source = await fetchWasm(urlOrBuffer, imports);
258
242
  }
259
- if (asyncify) {
260
- const memory = source.instance.exports.memory || ((_b = imports.env) === null || _b === void 0 ? void 0 : _b.memory);
261
- return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
262
- }
263
243
  return source;
264
244
  }
265
245
  /** @public */
266
- function loadSync(buffer, imports, asyncify) {
246
+ async function asyncifyLoad(asyncify, urlOrBuffer, imports) {
267
247
  var _a;
248
+ validateImports(imports);
249
+ imports = imports !== null && imports !== void 0 ? imports : {};
250
+ const asyncifyHelper = new Asyncify();
251
+ imports = asyncifyHelper.wrapImports(imports);
252
+ const source = await load(urlOrBuffer, imports);
253
+ const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
254
+ return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
255
+ }
256
+ /** @public */
257
+ function loadSync(buffer, imports) {
268
258
  if ((buffer instanceof ArrayBuffer) && !ArrayBuffer.isView(buffer)) {
269
259
  throw new TypeError('Invalid source');
270
260
  }
271
- if (imports && typeof imports !== 'object') {
272
- throw new TypeError('imports must be an object or undefined');
273
- }
261
+ validateImports(imports);
274
262
  imports = imports !== null && imports !== void 0 ? imports : {};
275
- let asyncifyHelper;
276
- if (asyncify) {
277
- asyncifyHelper = new Asyncify();
278
- imports = asyncifyHelper.wrapImports(imports);
279
- }
280
263
  const module = new _WebAssembly.Module(buffer);
281
264
  const instance = new _WebAssembly.Instance(module, imports);
282
265
  const source = { instance, module };
283
- if (asyncify) {
284
- const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
285
- return { module: source.module, instance: asyncifyHelper.init(memory, instance, asyncify) };
286
- }
287
266
  return source;
267
+ }
268
+ /** @public */
269
+ function asyncifyLoadSync(asyncify, buffer, imports) {
270
+ var _a;
271
+ validateImports(imports);
272
+ imports = imports !== null && imports !== void 0 ? imports : {};
273
+ const asyncifyHelper = new Asyncify();
274
+ imports = asyncifyHelper.wrapImports(imports);
275
+ const source = loadSync(buffer, imports);
276
+ const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
277
+ return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
288
278
  }
289
279
 
290
280
  const CHAR_DOT = 46; /* . */
@@ -386,37 +376,67 @@
386
376
  return resolvedPath.length > 0 ? resolvedPath : '.';
387
377
  }
388
378
 
379
+ const FD_DATASYNC = /*#__PURE__*/ (BigInt(1) << BigInt(0));
380
+ const FD_READ = /*#__PURE__*/ (BigInt(1) << BigInt(1));
381
+ const FD_SEEK = /*#__PURE__*/ (BigInt(1) << BigInt(2));
382
+ const FD_FDSTAT_SET_FLAGS = /*#__PURE__*/ (BigInt(1) << BigInt(3));
383
+ const FD_SYNC = /*#__PURE__*/ (BigInt(1) << BigInt(4));
384
+ const FD_TELL = /*#__PURE__*/ (BigInt(1) << BigInt(5));
385
+ const FD_WRITE = /*#__PURE__*/ (BigInt(1) << BigInt(6));
386
+ const FD_ADVISE = /*#__PURE__*/ (BigInt(1) << BigInt(7));
387
+ const FD_ALLOCATE = /*#__PURE__*/ (BigInt(1) << BigInt(8));
388
+ const PATH_CREATE_DIRECTORY = /*#__PURE__*/ (BigInt(1) << BigInt(9));
389
+ const PATH_CREATE_FILE = /*#__PURE__*/ (BigInt(1) << BigInt(10));
390
+ const PATH_LINK_SOURCE = /*#__PURE__*/ (BigInt(1) << BigInt(11));
391
+ const PATH_LINK_TARGET = /*#__PURE__*/ (BigInt(1) << BigInt(12));
392
+ const PATH_OPEN = /*#__PURE__*/ (BigInt(1) << BigInt(13));
393
+ const FD_READDIR = /*#__PURE__*/ (BigInt(1) << BigInt(14));
394
+ const PATH_READLINK = /*#__PURE__*/ (BigInt(1) << BigInt(15));
395
+ const PATH_RENAME_SOURCE = /*#__PURE__*/ (BigInt(1) << BigInt(16));
396
+ const PATH_RENAME_TARGET = /*#__PURE__*/ (BigInt(1) << BigInt(17));
397
+ const PATH_FILESTAT_GET = /*#__PURE__*/ (BigInt(1) << BigInt(18));
398
+ const PATH_FILESTAT_SET_SIZE = /*#__PURE__*/ (BigInt(1) << BigInt(19));
399
+ const PATH_FILESTAT_SET_TIMES = /*#__PURE__*/ (BigInt(1) << BigInt(20));
400
+ const FD_FILESTAT_GET = /*#__PURE__*/ (BigInt(1) << BigInt(21));
401
+ const FD_FILESTAT_SET_SIZE = /*#__PURE__*/ (BigInt(1) << BigInt(22));
402
+ const FD_FILESTAT_SET_TIMES = /*#__PURE__*/ (BigInt(1) << BigInt(23));
403
+ const PATH_SYMLINK = /*#__PURE__*/ (BigInt(1) << BigInt(24));
404
+ const PATH_REMOVE_DIRECTORY = /*#__PURE__*/ (BigInt(1) << BigInt(25));
405
+ const PATH_UNLINK_FILE = /*#__PURE__*/ (BigInt(1) << BigInt(26));
406
+ const POLL_FD_READWRITE = /*#__PURE__*/ (BigInt(1) << BigInt(27));
407
+ const SOCK_SHUTDOWN = /*#__PURE__*/ (BigInt(1) << BigInt(28));
408
+ const SOCK_ACCEPT = /*#__PURE__*/ (BigInt(1) << BigInt(29));
389
409
  const WasiRights = {
390
- FD_DATASYNC: (BigInt(1) << BigInt(0)),
391
- FD_READ: (BigInt(1) << BigInt(1)),
392
- FD_SEEK: (BigInt(1) << BigInt(2)),
393
- FD_FDSTAT_SET_FLAGS: (BigInt(1) << BigInt(3)),
394
- FD_SYNC: (BigInt(1) << BigInt(4)),
395
- FD_TELL: (BigInt(1) << BigInt(5)),
396
- FD_WRITE: (BigInt(1) << BigInt(6)),
397
- FD_ADVISE: (BigInt(1) << BigInt(7)),
398
- FD_ALLOCATE: (BigInt(1) << BigInt(8)),
399
- PATH_CREATE_DIRECTORY: (BigInt(1) << BigInt(9)),
400
- PATH_CREATE_FILE: (BigInt(1) << BigInt(10)),
401
- PATH_LINK_SOURCE: (BigInt(1) << BigInt(11)),
402
- PATH_LINK_TARGET: (BigInt(1) << BigInt(12)),
403
- PATH_OPEN: (BigInt(1) << BigInt(13)),
404
- FD_READDIR: (BigInt(1) << BigInt(14)),
405
- PATH_READLINK: (BigInt(1) << BigInt(15)),
406
- PATH_RENAME_SOURCE: (BigInt(1) << BigInt(16)),
407
- PATH_RENAME_TARGET: (BigInt(1) << BigInt(17)),
408
- PATH_FILESTAT_GET: (BigInt(1) << BigInt(18)),
409
- PATH_FILESTAT_SET_SIZE: (BigInt(1) << BigInt(19)),
410
- PATH_FILESTAT_SET_TIMES: (BigInt(1) << BigInt(20)),
411
- FD_FILESTAT_GET: (BigInt(1) << BigInt(21)),
412
- FD_FILESTAT_SET_SIZE: (BigInt(1) << BigInt(22)),
413
- FD_FILESTAT_SET_TIMES: (BigInt(1) << BigInt(23)),
414
- PATH_SYMLINK: (BigInt(1) << BigInt(24)),
415
- PATH_REMOVE_DIRECTORY: (BigInt(1) << BigInt(25)),
416
- PATH_UNLINK_FILE: (BigInt(1) << BigInt(26)),
417
- POLL_FD_READWRITE: (BigInt(1) << BigInt(27)),
418
- SOCK_SHUTDOWN: (BigInt(1) << BigInt(28)),
419
- SOCK_ACCEPT: (BigInt(1) << BigInt(29))
410
+ FD_DATASYNC,
411
+ FD_READ,
412
+ FD_SEEK,
413
+ FD_FDSTAT_SET_FLAGS,
414
+ FD_SYNC,
415
+ FD_TELL,
416
+ FD_WRITE,
417
+ FD_ADVISE,
418
+ FD_ALLOCATE,
419
+ PATH_CREATE_DIRECTORY,
420
+ PATH_CREATE_FILE,
421
+ PATH_LINK_SOURCE,
422
+ PATH_LINK_TARGET,
423
+ PATH_OPEN,
424
+ FD_READDIR,
425
+ PATH_READLINK,
426
+ PATH_RENAME_SOURCE,
427
+ PATH_RENAME_TARGET,
428
+ PATH_FILESTAT_GET,
429
+ PATH_FILESTAT_SET_SIZE,
430
+ PATH_FILESTAT_SET_TIMES,
431
+ FD_FILESTAT_GET,
432
+ FD_FILESTAT_SET_SIZE,
433
+ FD_FILESTAT_SET_TIMES,
434
+ PATH_SYMLINK,
435
+ PATH_REMOVE_DIRECTORY,
436
+ PATH_UNLINK_FILE,
437
+ POLL_FD_READWRITE,
438
+ SOCK_SHUTDOWN,
439
+ SOCK_ACCEPT
420
440
  };
421
441
 
422
442
  function strerror(errno) {
@@ -562,7 +582,7 @@
562
582
  WasiRights.FD_FILESTAT_SET_SIZE |
563
583
  WasiRights.FD_FILESTAT_SET_TIMES |
564
584
  WasiRights.POLL_FD_READWRITE;
565
- const REGULAR_FILE_INHERITING = BigInt(0);
585
+ const REGULAR_FILE_INHERITING = /*#__PURE__*/ BigInt(0);
566
586
  const DIRECTORY_BASE = WasiRights.FD_FDSTAT_SET_FLAGS |
567
587
  WasiRights.FD_SYNC |
568
588
  WasiRights.FD_ADVISE |
@@ -597,7 +617,7 @@
597
617
  WasiRights.FD_WRITE |
598
618
  WasiRights.FD_FILESTAT_GET |
599
619
  WasiRights.POLL_FD_READWRITE;
600
- const TTY_INHERITING = BigInt(0);
620
+ const TTY_INHERITING = /*#__PURE__*/ BigInt(0);
601
621
  function getRights(stdio, fd, flags, type) {
602
622
  const ret = {
603
623
  base: BigInt(0),
@@ -760,7 +780,6 @@
760
780
  this.size = options.size;
761
781
  this.fds = Array(options.size);
762
782
  this.stdio = [options.in, options.out, options.err];
763
- this.fs = options.fs;
764
783
  this.print = options.print;
765
784
  this.printErr = options.printErr;
766
785
  this.insertStdio(options.in, 0, '<stdin>');
@@ -807,18 +826,6 @@
807
826
  this.used++;
808
827
  return entry;
809
828
  }
810
- getFileTypeByFd(fd) {
811
- const stat = this.fs.fstatSync(fd);
812
- return toFileType(stat);
813
- }
814
- insertPreopen(fd, mappedPath, realPath) {
815
- const type = this.getFileTypeByFd(fd);
816
- if (type !== 3 /* WasiFileType.DIRECTORY */) {
817
- throw new WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, 54 /* WasiErrno.ENOTDIR */);
818
- }
819
- const result = getRights(this.stdio, fd, 0, type);
820
- return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
821
- }
822
829
  get(id, base, inheriting) {
823
830
  if (id >= this.size) {
824
831
  throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
@@ -844,6 +851,24 @@
844
851
  this.fds[id] = undefined;
845
852
  this.used--;
846
853
  }
854
+ }
855
+ class SyncTable extends FileDescriptorTable {
856
+ constructor(options) {
857
+ super(options);
858
+ this.fs = options.fs;
859
+ }
860
+ getFileTypeByFd(fd) {
861
+ const stats = this.fs.fstatSync(fd, { bigint: true });
862
+ return toFileType(stats);
863
+ }
864
+ insertPreopen(fd, mappedPath, realPath) {
865
+ const type = this.getFileTypeByFd(fd);
866
+ if (type !== 3 /* WasiFileType.DIRECTORY */) {
867
+ throw new WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, 54 /* WasiErrno.ENOTDIR */);
868
+ }
869
+ const result = getRights(this.stdio, fd, 0, type);
870
+ return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
871
+ }
847
872
  renumber(dst, src) {
848
873
  if (dst === src)
849
874
  return;
@@ -861,10 +886,45 @@
861
886
  this.fds[src] = undefined;
862
887
  this.used--;
863
888
  }
889
+ }
890
+ class AsyncTable extends FileDescriptorTable {
891
+ // eslint-disable-next-line @typescript-eslint/no-useless-constructor
892
+ constructor(options) {
893
+ super(options);
894
+ }
895
+ async getFileTypeByFd(fd) {
896
+ const stats = await fd.stat({ bigint: true });
897
+ return toFileType(stats);
898
+ }
899
+ async insertPreopen(fd, mappedPath, realPath) {
900
+ const type = await this.getFileTypeByFd(fd);
901
+ if (type !== 3 /* WasiFileType.DIRECTORY */) {
902
+ throw new WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, 54 /* WasiErrno.ENOTDIR */);
903
+ }
904
+ const result = getRights(this.stdio, fd.fd, 0, type);
905
+ return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
906
+ }
907
+ async renumber(dst, src) {
908
+ if (dst === src)
909
+ return;
910
+ if (dst >= this.size || src >= this.size) {
911
+ throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
912
+ }
913
+ const dstEntry = this.fds[dst];
914
+ const srcEntry = this.fds[src];
915
+ if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
916
+ throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
917
+ }
918
+ await dstEntry.fd.close();
919
+ this.fds[dst] = this.fds[src];
920
+ this.fds[dst].id = dst;
921
+ this.fds[src] = undefined;
922
+ this.used--;
923
+ }
864
924
  }
865
925
 
866
926
  /** @public */
867
- const WebAssemblyMemory = _WebAssembly.Memory;
927
+ const WebAssemblyMemory = _WebAssembly.Memory;
868
928
  /** @public */
869
929
  class Memory extends WebAssemblyMemory {
870
930
  // eslint-disable-next-line @typescript-eslint/no-useless-constructor
@@ -891,6 +951,44 @@
891
951
  return memory;
892
952
  }
893
953
 
954
+ function checkWebAssemblyFunction() {
955
+ const WebAssemblyFunction = _WebAssembly.Function;
956
+ if (typeof WebAssemblyFunction !== 'function') {
957
+ throw new Error('WebAssembly.Function is not supported in this environment.' +
958
+ ' If you are using V8 based browser like Chrome, try to specify' +
959
+ ' --js-flag="--wasm-staging --experimental-wasm-stack-switching"');
960
+ }
961
+ return WebAssemblyFunction;
962
+ }
963
+ /** @public */
964
+ function wrapAsyncImport(f, parameterType, returnType) {
965
+ const WebAssemblyFunction = checkWebAssemblyFunction();
966
+ if (typeof f !== 'function') {
967
+ throw new TypeError('Function required');
968
+ }
969
+ const parameters = parameterType.slice(0);
970
+ parameters.unshift('externref');
971
+ return new WebAssemblyFunction({ parameters, results: returnType }, f, { suspending: 'first' });
972
+ }
973
+ /** @public */
974
+ function wrapAsyncExport(f) {
975
+ const WebAssemblyFunction = checkWebAssemblyFunction();
976
+ if (typeof f !== 'function') {
977
+ throw new TypeError('Function required');
978
+ }
979
+ return new WebAssemblyFunction({ parameters: [...WebAssemblyFunction.type(f).parameters.slice(1)], results: ['externref'] }, f, { promising: 'first' });
980
+ }
981
+ /** @public */
982
+ function wrapExports(exports, needWrap) {
983
+ return wrapInstanceExports(exports, (exportValue, name) => {
984
+ let ignore = typeof exportValue !== 'function';
985
+ if (Array.isArray(needWrap)) {
986
+ ignore = ignore || (needWrap.indexOf(name) === -1);
987
+ }
988
+ return ignore ? exportValue : wrapAsyncExport(exportValue);
989
+ });
990
+ }
991
+
894
992
  function copyMemory(targets, src) {
895
993
  if (targets.length === 0 || src.length === 0)
896
994
  return 0;
@@ -949,7 +1047,7 @@
949
1047
  Object.defineProperty(f, 'name', { value: name });
950
1048
  return f;
951
1049
  }
952
- function syscallWrap(name, f) {
1050
+ function syscallWrap(self, name, f) {
953
1051
  return defineName(name, function () {
954
1052
  {
955
1053
  const args = Array.prototype.slice.call(arguments);
@@ -959,7 +1057,7 @@
959
1057
  }
960
1058
  let r;
961
1059
  try {
962
- r = f.apply(this, arguments);
1060
+ r = f.apply(self, arguments);
963
1061
  }
964
1062
  catch (err) {
965
1063
  return handleError(err);
@@ -970,7 +1068,7 @@
970
1068
  return r;
971
1069
  });
972
1070
  }
973
- function resolvePath(fs, fileDescriptor, path, flags) {
1071
+ function resolvePathSync(fs, fileDescriptor, path, flags) {
974
1072
  let resolvedPath = resolve(fileDescriptor.realPath, path);
975
1073
  if ((flags & 1) === 1) {
976
1074
  try {
@@ -984,6 +1082,20 @@
984
1082
  }
985
1083
  return resolvedPath;
986
1084
  }
1085
+ async function resolvePathAsync(fs, fileDescriptor, path, flags) {
1086
+ let resolvedPath = resolve(fileDescriptor.realPath, path);
1087
+ if ((flags & 1) === 1) {
1088
+ try {
1089
+ resolvedPath = await fs.promises.readlink(resolvedPath);
1090
+ }
1091
+ catch (err) {
1092
+ if (err.code !== 'EINVAL' && err.code !== 'ENOENT') {
1093
+ throw err;
1094
+ }
1095
+ }
1096
+ }
1097
+ return resolvedPath;
1098
+ }
987
1099
  const encoder = new TextEncoder();
988
1100
  const decoder = new TextDecoder();
989
1101
  function readStdin() {
@@ -994,14 +1106,8 @@
994
1106
  return buffer;
995
1107
  }
996
1108
  class WASI$1 {
997
- constructor(args, env, preopens, stdio, filesystem, print, printErr) {
998
- this._setMemory = function _setMemory(m) {
999
- if (!(m instanceof _WebAssembly.Memory)) {
1000
- throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
1001
- }
1002
- _memory.set(this, extendMemory(m));
1003
- };
1004
- this.args_get = syscallWrap('args_get', function (argv, argv_buf) {
1109
+ constructor(args, env, fds, asyncFs, fs, asyncify) {
1110
+ this.args_get = syscallWrap(this, 'args_get', function (argv, argv_buf) {
1005
1111
  argv = Number(argv);
1006
1112
  argv_buf = Number(argv_buf);
1007
1113
  if (argv === 0 || argv_buf === 0) {
@@ -1020,7 +1126,7 @@
1020
1126
  }
1021
1127
  return 0 /* WasiErrno.ESUCCESS */;
1022
1128
  });
1023
- this.args_sizes_get = syscallWrap('args_sizes_get', function (argc, argv_buf_size) {
1129
+ this.args_sizes_get = syscallWrap(this, 'args_sizes_get', function (argc, argv_buf_size) {
1024
1130
  argc = Number(argc);
1025
1131
  argv_buf_size = Number(argv_buf_size);
1026
1132
  if (argc === 0 || argv_buf_size === 0) {
@@ -1033,7 +1139,7 @@
1033
1139
  view.setUint32(argv_buf_size, encoder.encode(args.join('\0') + '\0').length, true);
1034
1140
  return 0 /* WasiErrno.ESUCCESS */;
1035
1141
  });
1036
- this.environ_get = syscallWrap('environ_get', function (environ, environ_buf) {
1142
+ this.environ_get = syscallWrap(this, 'environ_get', function (environ, environ_buf) {
1037
1143
  environ = Number(environ);
1038
1144
  environ_buf = Number(environ_buf);
1039
1145
  if (environ === 0 || environ_buf === 0) {
@@ -1052,7 +1158,7 @@
1052
1158
  }
1053
1159
  return 0 /* WasiErrno.ESUCCESS */;
1054
1160
  });
1055
- this.environ_sizes_get = syscallWrap('environ_sizes_get', function (len, buflen) {
1161
+ this.environ_sizes_get = syscallWrap(this, 'environ_sizes_get', function (len, buflen) {
1056
1162
  len = Number(len);
1057
1163
  buflen = Number(buflen);
1058
1164
  if (len === 0 || buflen === 0) {
@@ -1064,7 +1170,7 @@
1064
1170
  view.setUint32(buflen, encoder.encode(wasi.env.join('\0') + '\0').length, true);
1065
1171
  return 0 /* WasiErrno.ESUCCESS */;
1066
1172
  });
1067
- this.clock_res_get = syscallWrap('clock_res_get', function (id, resolution) {
1173
+ this.clock_res_get = syscallWrap(this, 'clock_res_get', function (id, resolution) {
1068
1174
  resolution = Number(resolution);
1069
1175
  if (resolution === 0) {
1070
1176
  return 28 /* WasiErrno.EINVAL */;
@@ -1082,7 +1188,7 @@
1082
1188
  default: return 28 /* WasiErrno.EINVAL */;
1083
1189
  }
1084
1190
  });
1085
- this.clock_time_get = syscallWrap('clock_time_get', function (id, _percision, time) {
1191
+ this.clock_time_get = syscallWrap(this, 'clock_time_get', function (id, _percision, time) {
1086
1192
  time = Number(time);
1087
1193
  if (time === 0) {
1088
1194
  return 28 /* WasiErrno.EINVAL */;
@@ -1105,35 +1211,10 @@
1105
1211
  default: return 28 /* WasiErrno.EINVAL */;
1106
1212
  }
1107
1213
  });
1108
- this.fd_advise = syscallWrap('fd_advise', function (_fd, _offset, _len, _advice) {
1214
+ this.fd_advise = syscallWrap(this, 'fd_advise', function (_fd, _offset, _len, _advice) {
1109
1215
  return 52 /* WasiErrno.ENOSYS */;
1110
1216
  });
1111
- this.fd_allocate = syscallWrap('fd_allocate', function (fd, offset, len) {
1112
- const wasi = _wasi.get(this);
1113
- const fs = getFs(this);
1114
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_ALLOCATE, BigInt(0));
1115
- const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
1116
- if (stat.size < offset + len) {
1117
- fs.ftruncateSync(fileDescriptor.fd, Number(offset + len));
1118
- }
1119
- return 0 /* WasiErrno.ESUCCESS */;
1120
- });
1121
- this.fd_close = syscallWrap('fd_close', function (fd) {
1122
- const wasi = _wasi.get(this);
1123
- const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
1124
- const fs = getFs(this);
1125
- fs.closeSync(fileDescriptor.fd);
1126
- wasi.fds.remove(fd);
1127
- return 0 /* WasiErrno.ESUCCESS */;
1128
- });
1129
- this.fd_datasync = syscallWrap('fd_datasync', function (fd) {
1130
- const wasi = _wasi.get(this);
1131
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_DATASYNC, BigInt(0));
1132
- const fs = getFs(this);
1133
- fs.fdatasyncSync(fileDescriptor.fd);
1134
- return 0 /* WasiErrno.ESUCCESS */;
1135
- });
1136
- this.fd_fdstat_get = syscallWrap('fd_fdstat_get', function (fd, fdstat) {
1217
+ this.fd_fdstat_get = syscallWrap(this, 'fd_fdstat_get', function (fd, fdstat) {
1137
1218
  fdstat = Number(fdstat);
1138
1219
  if (fdstat === 0) {
1139
1220
  return 28 /* WasiErrno.EINVAL */;
@@ -1147,10 +1228,10 @@
1147
1228
  view.setBigUint64(fdstat + 16, fileDescriptor.rightsInheriting, true);
1148
1229
  return 0 /* WasiErrno.ESUCCESS */;
1149
1230
  });
1150
- this.fd_fdstat_set_flags = syscallWrap('fd_fdstat_set_flags', function (_fd, _flags) {
1231
+ this.fd_fdstat_set_flags = syscallWrap(this, 'fd_fdstat_set_flags', function (_fd, _flags) {
1151
1232
  return 52 /* WasiErrno.ENOSYS */;
1152
1233
  });
1153
- this.fd_fdstat_set_rights = syscallWrap('fd_fdstat_set_rights', function (fd, rightsBase, rightsInheriting) {
1234
+ this.fd_fdstat_set_rights = syscallWrap(this, 'fd_fdstat_set_rights', function (fd, rightsBase, rightsInheriting) {
1154
1235
  const wasi = _wasi.get(this);
1155
1236
  const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
1156
1237
  if ((rightsBase | fileDescriptor.rightsBase) > fileDescriptor.rightsBase) {
@@ -1164,65 +1245,7 @@
1164
1245
  fileDescriptor.rightsInheriting = rightsInheriting;
1165
1246
  return 0 /* WasiErrno.ESUCCESS */;
1166
1247
  });
1167
- this.fd_filestat_get = syscallWrap('fd_filestat_get', function (fd, buf) {
1168
- buf = Number(buf);
1169
- if (buf === 0)
1170
- return 28 /* WasiErrno.EINVAL */;
1171
- const wasi = _wasi.get(this);
1172
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
1173
- const fs = getFs(this);
1174
- const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
1175
- const { view } = getMemory(this);
1176
- toFileStat(view, buf, stat);
1177
- return 0 /* WasiErrno.ESUCCESS */;
1178
- });
1179
- this.fd_filestat_set_size = syscallWrap('fd_filestat_set_size', function (fd, size) {
1180
- const wasi = _wasi.get(this);
1181
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_SIZE, BigInt(0));
1182
- const fs = getFs(this);
1183
- fs.ftruncateSync(fileDescriptor.fd, Number(size));
1184
- return 0 /* WasiErrno.ESUCCESS */;
1185
- });
1186
- this.fd_filestat_set_times = syscallWrap('fd_filestat_set_times', function (fd, atim, mtim, flags) {
1187
- const wasi = _wasi.get(this);
1188
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_TIMES, BigInt(0));
1189
- if ((flags & 2 /* WasiFstFlag.SET_ATIM_NOW */) === 2 /* WasiFstFlag.SET_ATIM_NOW */) {
1190
- atim = BigInt(Date.now() * 1000000);
1191
- }
1192
- if ((flags & 8 /* WasiFstFlag.SET_MTIM_NOW */) === 8 /* WasiFstFlag.SET_MTIM_NOW */) {
1193
- mtim = BigInt(Date.now() * 1000000);
1194
- }
1195
- const fs = getFs(this);
1196
- fs.futimesSync(fileDescriptor.fd, Number(atim), Number(mtim));
1197
- return 0 /* WasiErrno.ESUCCESS */;
1198
- });
1199
- this.fd_pread = syscallWrap('fd_pread', function (fd, iovs, iovslen, offset, size) {
1200
- iovs = Number(iovs);
1201
- size = Number(size);
1202
- if (iovs === 0 || size === 0) {
1203
- return 28 /* WasiErrno.EINVAL */;
1204
- }
1205
- const { HEAPU8, view } = getMemory(this);
1206
- const wasi = _wasi.get(this);
1207
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
1208
- let totalSize = 0;
1209
- const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1210
- const offset = iovs + (i * 8);
1211
- const buf = view.getInt32(offset, true);
1212
- const bufLen = view.getUint32(offset + 4, true);
1213
- totalSize += bufLen;
1214
- return HEAPU8.subarray(buf, buf + bufLen);
1215
- });
1216
- let nread = 0;
1217
- const buffer = new Uint8Array(totalSize);
1218
- buffer._isBuffer = true;
1219
- const fs = getFs(this);
1220
- const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
1221
- nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1222
- view.setUint32(size, nread, true);
1223
- return 0 /* WasiErrno.ESUCCESS */;
1224
- });
1225
- this.fd_prestat_get = syscallWrap('fd_prestat_get', function (fd, prestat) {
1248
+ this.fd_prestat_get = syscallWrap(this, 'fd_prestat_get', function (fd, prestat) {
1226
1249
  prestat = Number(prestat);
1227
1250
  if (prestat === 0) {
1228
1251
  return 28 /* WasiErrno.EINVAL */;
@@ -1245,7 +1268,7 @@
1245
1268
  view.setUint32(prestat + 4, encoder.encode(fileDescriptor.path).length + 1, true);
1246
1269
  return 0 /* WasiErrno.ESUCCESS */;
1247
1270
  });
1248
- this.fd_prestat_dir_name = syscallWrap('fd_prestat_dir_name', function (fd, path, path_len) {
1271
+ this.fd_prestat_dir_name = syscallWrap(this, 'fd_prestat_dir_name', function (fd, path, path_len) {
1249
1272
  path = Number(path);
1250
1273
  path_len = Number(path_len);
1251
1274
  if (path === 0) {
@@ -1263,78 +1286,363 @@
1263
1286
  HEAPU8.set(buffer, path);
1264
1287
  return 0 /* WasiErrno.ESUCCESS */;
1265
1288
  });
1266
- this.fd_pwrite = syscallWrap('fd_pwrite', function (fd, iovs, iovslen, offset, size) {
1267
- iovs = Number(iovs);
1268
- size = Number(size);
1269
- if (iovs === 0 || size === 0) {
1289
+ this.fd_seek = syscallWrap(this, 'fd_seek', function (fd, offset, whence, newOffset) {
1290
+ newOffset = Number(newOffset);
1291
+ if (newOffset === 0) {
1270
1292
  return 28 /* WasiErrno.EINVAL */;
1271
1293
  }
1272
- const { HEAPU8, view } = getMemory(this);
1294
+ if (fd === 0 || fd === 1 || fd === 2)
1295
+ return 0 /* WasiErrno.ESUCCESS */;
1273
1296
  const wasi = _wasi.get(this);
1274
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
1275
- const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
1276
- const offset = iovs + (i * 8);
1277
- const buf = view.getInt32(offset, true);
1278
- const bufLen = view.getUint32(offset + 4, true);
1279
- return HEAPU8.subarray(buf, buf + bufLen);
1280
- }));
1281
- const fs = getFs(this);
1282
- const nwritten = fs.writeSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
1283
- view.setUint32(size, nwritten, true);
1297
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SEEK, BigInt(0));
1298
+ const r = fileDescriptor.seek(offset, whence);
1299
+ const { view } = getMemory(this);
1300
+ view.setBigUint64(newOffset, r, true);
1284
1301
  return 0 /* WasiErrno.ESUCCESS */;
1285
1302
  });
1286
- this.fd_read = syscallWrap('fd_read', function (fd, iovs, iovslen, size) {
1287
- iovs = Number(iovs);
1288
- size = Number(size);
1289
- if (iovs === 0 || size === 0) {
1290
- return 28 /* WasiErrno.EINVAL */;
1291
- }
1292
- const { HEAPU8, view } = getMemory(this);
1303
+ this.fd_tell = syscallWrap(this, 'fd_tell', function (fd, offset) {
1293
1304
  const wasi = _wasi.get(this);
1294
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
1295
- let totalSize = 0;
1296
- const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1297
- const offset = iovs + (i * 8);
1298
- const buf = view.getInt32(offset, true);
1299
- const bufLen = view.getUint32(offset + 4, true);
1300
- totalSize += bufLen;
1301
- return HEAPU8.subarray(buf, buf + bufLen);
1302
- });
1303
- let buffer;
1304
- let nread = 0;
1305
- if (fd === 0) {
1306
- if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
1307
- return 58 /* WasiErrno.ENOTSUP */;
1308
- }
1309
- buffer = readStdin();
1310
- nread = buffer ? copyMemory(ioVecs, buffer) : 0;
1311
- }
1312
- else {
1313
- buffer = new Uint8Array(totalSize);
1314
- buffer._isBuffer = true;
1315
- const fs = getFs(this);
1316
- const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(fileDescriptor.pos));
1317
- nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1305
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_TELL, BigInt(0));
1306
+ const pos = BigInt(fileDescriptor.pos);
1307
+ const { view } = getMemory(this);
1308
+ view.setBigUint64(Number(offset), pos, true);
1309
+ return 0 /* WasiErrno.ESUCCESS */;
1310
+ });
1311
+ this.poll_oneoff = syscallWrap(this, 'poll_oneoff', function (_in, _out, _sub, _nevents) {
1312
+ return 52 /* WasiErrno.ENOSYS */;
1313
+ });
1314
+ this.proc_exit = syscallWrap(this, 'proc_exit', function (_rval) {
1315
+ return 0 /* WasiErrno.ESUCCESS */;
1316
+ });
1317
+ this.proc_raise = syscallWrap(this, 'proc_raise', function (_sig) {
1318
+ return 52 /* WasiErrno.ENOSYS */;
1319
+ });
1320
+ this.sched_yield = syscallWrap(this, 'sched_yield', function () {
1321
+ return 0 /* WasiErrno.ESUCCESS */;
1322
+ });
1323
+ this.random_get = typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function'
1324
+ ? syscallWrap(this, 'random_get', function (buf, buf_len) {
1325
+ buf = Number(buf);
1326
+ if (buf === 0) {
1327
+ return 28 /* WasiErrno.EINVAL */;
1328
+ }
1329
+ buf_len = Number(buf_len);
1330
+ const { HEAPU8 } = getMemory(this);
1331
+ let pos;
1332
+ const stride = 65536;
1333
+ for (pos = 0; pos + stride < buf_len; pos += stride) {
1334
+ crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + pos + stride));
1335
+ }
1336
+ crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + buf_len));
1337
+ return 0 /* WasiErrno.ESUCCESS */;
1338
+ })
1339
+ : syscallWrap(this, 'random_get', function (buf, buf_len) {
1340
+ buf = Number(buf);
1341
+ if (buf === 0) {
1342
+ return 28 /* WasiErrno.EINVAL */;
1343
+ }
1344
+ buf_len = Number(buf_len);
1345
+ const { view } = getMemory(this);
1346
+ for (let i = buf; i < buf + buf_len; ++i) {
1347
+ view.setUint8(i, Math.floor(Math.random() * 256));
1348
+ }
1349
+ return 0 /* WasiErrno.ESUCCESS */;
1350
+ });
1351
+ this.sock_recv = syscallWrap(this, 'sock_recv', function () {
1352
+ return 58 /* WasiErrno.ENOTSUP */;
1353
+ });
1354
+ this.sock_send = syscallWrap(this, 'sock_send', function () {
1355
+ return 58 /* WasiErrno.ENOTSUP */;
1356
+ });
1357
+ this.sock_shutdown = syscallWrap(this, 'sock_shutdown', function () {
1358
+ return 58 /* WasiErrno.ENOTSUP */;
1359
+ });
1360
+ _wasi.set(this, {
1361
+ fds,
1362
+ args,
1363
+ env
1364
+ });
1365
+ if (fs)
1366
+ _fs.set(this, fs);
1367
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
1368
+ const _this = this;
1369
+ function defineImport(name, syncVersion, asyncVersion, parameterType, returnType) {
1370
+ if (asyncFs) {
1371
+ if (asyncify) {
1372
+ _this[name] = asyncify.wrapImportFunction(syscallWrap(_this, name, asyncVersion));
1373
+ }
1374
+ else {
1375
+ _this[name] = wrapAsyncImport(syscallWrap(_this, name, asyncVersion), parameterType, returnType);
1376
+ }
1377
+ }
1378
+ else {
1379
+ _this[name] = syscallWrap(_this, name, syncVersion);
1380
+ }
1381
+ }
1382
+ defineImport('fd_allocate', function fd_allocate(fd, offset, len) {
1383
+ const wasi = _wasi.get(this);
1384
+ const fs = getFs(this);
1385
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_ALLOCATE, BigInt(0));
1386
+ const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
1387
+ if (stat.size < offset + len) {
1388
+ fs.ftruncateSync(fileDescriptor.fd, Number(offset + len));
1389
+ }
1390
+ return 0 /* WasiErrno.ESUCCESS */;
1391
+ }, async function fd_allocate(fd, offset, len) {
1392
+ const wasi = _wasi.get(this);
1393
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_ALLOCATE, BigInt(0));
1394
+ const h = fileDescriptor.fd;
1395
+ const stat = await h.stat({ bigint: true });
1396
+ if (stat.size < offset + len) {
1397
+ await h.truncate(Number(offset + len));
1398
+ }
1399
+ return 0 /* WasiErrno.ESUCCESS */;
1400
+ }, ['i32', 'i64', 'f64'], ['i32']);
1401
+ defineImport('fd_close', function fd_close(fd) {
1402
+ const wasi = _wasi.get(this);
1403
+ const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
1404
+ const fs = getFs(this);
1405
+ fs.closeSync(fileDescriptor.fd);
1406
+ wasi.fds.remove(fd);
1407
+ return 0 /* WasiErrno.ESUCCESS */;
1408
+ }, async function fd_close(fd) {
1409
+ const wasi = _wasi.get(this);
1410
+ const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
1411
+ await fileDescriptor.fd.close();
1412
+ wasi.fds.remove(fd);
1413
+ return 0 /* WasiErrno.ESUCCESS */;
1414
+ }, ['i32'], ['i32']);
1415
+ defineImport('fd_datasync', function fd_datasync(fd) {
1416
+ const wasi = _wasi.get(this);
1417
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_DATASYNC, BigInt(0));
1418
+ const fs = getFs(this);
1419
+ fs.fdatasyncSync(fileDescriptor.fd);
1420
+ return 0 /* WasiErrno.ESUCCESS */;
1421
+ }, async function fd_datasync(fd) {
1422
+ const wasi = _wasi.get(this);
1423
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_DATASYNC, BigInt(0));
1424
+ await fileDescriptor.fd.datasync();
1425
+ return 0 /* WasiErrno.ESUCCESS */;
1426
+ }, ['i32'], ['i32']);
1427
+ defineImport('fd_filestat_get', function fd_filestat_get(fd, buf) {
1428
+ buf = Number(buf);
1429
+ if (buf === 0)
1430
+ return 28 /* WasiErrno.EINVAL */;
1431
+ const wasi = _wasi.get(this);
1432
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
1433
+ const fs = getFs(this);
1434
+ const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
1435
+ const { view } = getMemory(this);
1436
+ toFileStat(view, buf, stat);
1437
+ return 0 /* WasiErrno.ESUCCESS */;
1438
+ }, async function fd_filestat_get(fd, buf) {
1439
+ buf = Number(buf);
1440
+ if (buf === 0)
1441
+ return 28 /* WasiErrno.EINVAL */;
1442
+ const wasi = _wasi.get(this);
1443
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
1444
+ const h = fileDescriptor.fd;
1445
+ const stat = await h.stat({ bigint: true });
1446
+ const { view } = getMemory(this);
1447
+ toFileStat(view, buf, stat);
1448
+ return 0 /* WasiErrno.ESUCCESS */;
1449
+ }, ['i32', 'i32'], ['i32']);
1450
+ defineImport('fd_filestat_set_size', function fd_filestat_set_size(fd, size) {
1451
+ const wasi = _wasi.get(this);
1452
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_SIZE, BigInt(0));
1453
+ const fs = getFs(this);
1454
+ fs.ftruncateSync(fileDescriptor.fd, Number(size));
1455
+ return 0 /* WasiErrno.ESUCCESS */;
1456
+ }, async function fd_filestat_set_size(fd, size) {
1457
+ const wasi = _wasi.get(this);
1458
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_SIZE, BigInt(0));
1459
+ const h = fileDescriptor.fd;
1460
+ await h.truncate(Number(size));
1461
+ return 0 /* WasiErrno.ESUCCESS */;
1462
+ }, ['i32', 'i64'], ['i32']);
1463
+ function fdFilestatGetTimes(fd, atim, mtim, flags) {
1464
+ const wasi = _wasi.get(this);
1465
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_TIMES, BigInt(0));
1466
+ if ((flags & 2 /* WasiFstFlag.SET_ATIM_NOW */) === 2 /* WasiFstFlag.SET_ATIM_NOW */) {
1467
+ atim = BigInt(Date.now() * 1000000);
1468
+ }
1469
+ if ((flags & 8 /* WasiFstFlag.SET_MTIM_NOW */) === 8 /* WasiFstFlag.SET_MTIM_NOW */) {
1470
+ mtim = BigInt(Date.now() * 1000000);
1471
+ }
1472
+ return { fileDescriptor, atim, mtim };
1473
+ }
1474
+ defineImport('fd_filestat_set_times', function fd_filestat_set_times(fd, atim, mtim, flags) {
1475
+ const { fileDescriptor, atim: atimRes, mtim: mtimRes } = fdFilestatGetTimes.call(this, fd, atim, mtim, flags);
1476
+ const fs = getFs(this);
1477
+ fs.futimesSync(fileDescriptor.fd, Number(atimRes), Number(mtimRes));
1478
+ return 0 /* WasiErrno.ESUCCESS */;
1479
+ }, async function fd_filestat_set_times(fd, atim, mtim, flags) {
1480
+ const { fileDescriptor, atim: atimRes, mtim: mtimRes } = fdFilestatGetTimes.call(this, fd, atim, mtim, flags);
1481
+ const h = fileDescriptor.fd;
1482
+ await h.utimes(Number(atimRes), Number(mtimRes));
1483
+ return 0 /* WasiErrno.ESUCCESS */;
1484
+ }, ['i32', 'i64', 'i64', 'i32'], ['i32']);
1485
+ defineImport('fd_pread', function fd_pread(fd, iovs, iovslen, offset, size) {
1486
+ iovs = Number(iovs);
1487
+ size = Number(size);
1488
+ if (iovs === 0 || size === 0) {
1489
+ return 28 /* WasiErrno.EINVAL */;
1490
+ }
1491
+ const { HEAPU8, view } = getMemory(this);
1492
+ const wasi = _wasi.get(this);
1493
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
1494
+ let totalSize = 0;
1495
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1496
+ const offset = iovs + (i * 8);
1497
+ const buf = view.getInt32(offset, true);
1498
+ const bufLen = view.getUint32(offset + 4, true);
1499
+ totalSize += bufLen;
1500
+ return HEAPU8.subarray(buf, buf + bufLen);
1501
+ });
1502
+ let nread = 0;
1503
+ const buffer = new Uint8Array(totalSize);
1504
+ buffer._isBuffer = true;
1505
+ const fs = getFs(this);
1506
+ const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
1507
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1508
+ view.setUint32(size, nread, true);
1509
+ return 0 /* WasiErrno.ESUCCESS */;
1510
+ }, async function (fd, iovs, iovslen, offset, size) {
1511
+ iovs = Number(iovs);
1512
+ size = Number(size);
1513
+ if (iovs === 0 || size === 0) {
1514
+ return 28 /* WasiErrno.EINVAL */;
1515
+ }
1516
+ const { HEAPU8, view } = getMemory(this);
1517
+ const wasi = _wasi.get(this);
1518
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
1519
+ let totalSize = 0;
1520
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1521
+ const offset = iovs + (i * 8);
1522
+ const buf = view.getInt32(offset, true);
1523
+ const bufLen = view.getUint32(offset + 4, true);
1524
+ totalSize += bufLen;
1525
+ return HEAPU8.subarray(buf, buf + bufLen);
1526
+ });
1527
+ let nread = 0;
1528
+ const buffer = new Uint8Array(totalSize);
1529
+ buffer._isBuffer = true;
1530
+ const { bytesRead } = await fileDescriptor.fd.read(buffer, 0, buffer.length, Number(offset));
1531
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1532
+ view.setUint32(size, nread, true);
1533
+ return 0 /* WasiErrno.ESUCCESS */;
1534
+ }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
1535
+ defineImport('fd_pwrite', function fd_pwrite(fd, iovs, iovslen, offset, size) {
1536
+ iovs = Number(iovs);
1537
+ size = Number(size);
1538
+ if (iovs === 0 || size === 0) {
1539
+ return 28 /* WasiErrno.EINVAL */;
1540
+ }
1541
+ const { HEAPU8, view } = getMemory(this);
1542
+ const wasi = _wasi.get(this);
1543
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
1544
+ const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
1545
+ const offset = iovs + (i * 8);
1546
+ const buf = view.getInt32(offset, true);
1547
+ const bufLen = view.getUint32(offset + 4, true);
1548
+ return HEAPU8.subarray(buf, buf + bufLen);
1549
+ }));
1550
+ const fs = getFs(this);
1551
+ const nwritten = fs.writeSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
1552
+ view.setUint32(size, nwritten, true);
1553
+ return 0 /* WasiErrno.ESUCCESS */;
1554
+ }, async function fd_pwrite(fd, iovs, iovslen, offset, size) {
1555
+ iovs = Number(iovs);
1556
+ size = Number(size);
1557
+ if (iovs === 0 || size === 0) {
1558
+ return 28 /* WasiErrno.EINVAL */;
1559
+ }
1560
+ const { HEAPU8, view } = getMemory(this);
1561
+ const wasi = _wasi.get(this);
1562
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
1563
+ const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
1564
+ const offset = iovs + (i * 8);
1565
+ const buf = view.getInt32(offset, true);
1566
+ const bufLen = view.getUint32(offset + 4, true);
1567
+ return HEAPU8.subarray(buf, buf + bufLen);
1568
+ }));
1569
+ const { bytesWritten } = await fileDescriptor.fd.write(buffer, 0, buffer.length, Number(offset));
1570
+ view.setUint32(size, bytesWritten, true);
1571
+ return 0 /* WasiErrno.ESUCCESS */;
1572
+ }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
1573
+ defineImport('fd_read', function fd_read(fd, iovs, iovslen, size) {
1574
+ iovs = Number(iovs);
1575
+ size = Number(size);
1576
+ if (iovs === 0 || size === 0) {
1577
+ return 28 /* WasiErrno.EINVAL */;
1578
+ }
1579
+ const { HEAPU8, view } = getMemory(this);
1580
+ const wasi = _wasi.get(this);
1581
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
1582
+ let totalSize = 0;
1583
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1584
+ const offset = iovs + (i * 8);
1585
+ const buf = view.getInt32(offset, true);
1586
+ const bufLen = view.getUint32(offset + 4, true);
1587
+ totalSize += bufLen;
1588
+ return HEAPU8.subarray(buf, buf + bufLen);
1589
+ });
1590
+ let buffer;
1591
+ let nread = 0;
1592
+ if (fd === 0) {
1593
+ if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
1594
+ return 58 /* WasiErrno.ENOTSUP */;
1595
+ }
1596
+ buffer = readStdin();
1597
+ nread = buffer ? copyMemory(ioVecs, buffer) : 0;
1598
+ }
1599
+ else {
1600
+ buffer = new Uint8Array(totalSize);
1601
+ buffer._isBuffer = true;
1602
+ const fs = getFs(this);
1603
+ const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(fileDescriptor.pos));
1604
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1318
1605
  fileDescriptor.pos += BigInt(nread);
1319
1606
  }
1320
1607
  view.setUint32(size, nread, true);
1321
1608
  return 0 /* WasiErrno.ESUCCESS */;
1322
- });
1323
- this.fd_seek = syscallWrap('fd_seek', function (fd, offset, whence, newOffset) {
1324
- newOffset = Number(newOffset);
1325
- if (newOffset === 0) {
1609
+ }, async function fd_read(fd, iovs, iovslen, size) {
1610
+ iovs = Number(iovs);
1611
+ size = Number(size);
1612
+ if (iovs === 0 || size === 0) {
1326
1613
  return 28 /* WasiErrno.EINVAL */;
1327
1614
  }
1328
- if (fd === 0 || fd === 1 || fd === 2)
1329
- return 0 /* WasiErrno.ESUCCESS */;
1615
+ const { HEAPU8, view } = getMemory(this);
1330
1616
  const wasi = _wasi.get(this);
1331
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SEEK, BigInt(0));
1332
- const r = fileDescriptor.seek(offset, whence);
1333
- const { view } = getMemory(this);
1334
- view.setBigUint64(newOffset, r, true);
1617
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
1618
+ let totalSize = 0;
1619
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1620
+ const offset = iovs + (i * 8);
1621
+ const buf = view.getInt32(offset, true);
1622
+ const bufLen = view.getUint32(offset + 4, true);
1623
+ totalSize += bufLen;
1624
+ return HEAPU8.subarray(buf, buf + bufLen);
1625
+ });
1626
+ let buffer;
1627
+ let nread = 0;
1628
+ if (fd === 0) {
1629
+ if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
1630
+ return 58 /* WasiErrno.ENOTSUP */;
1631
+ }
1632
+ buffer = readStdin();
1633
+ nread = buffer ? copyMemory(ioVecs, buffer) : 0;
1634
+ }
1635
+ else {
1636
+ buffer = new Uint8Array(totalSize);
1637
+ buffer._isBuffer = true;
1638
+ const { bytesRead } = await fileDescriptor.fd.read(buffer, 0, buffer.length, Number(fileDescriptor.pos));
1639
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1640
+ fileDescriptor.pos += BigInt(nread);
1641
+ }
1642
+ view.setUint32(size, nread, true);
1335
1643
  return 0 /* WasiErrno.ESUCCESS */;
1336
- });
1337
- this.fd_readdir = syscallWrap('fd_readdir', function (fd, buf, buf_len, cookie, bufused) {
1644
+ }, ['i32', 'i32', 'i32', 'i32'], ['i32']);
1645
+ defineImport('fd_readdir', function fd_readdir(fd, buf, buf_len, cookie, bufused) {
1338
1646
  buf = Number(buf);
1339
1647
  buf_len = Number(buf_len);
1340
1648
  bufused = Number(bufused);
@@ -1384,28 +1692,79 @@
1384
1692
  }
1385
1693
  view.setUint32(bufused, bufferUsed, true);
1386
1694
  return 0 /* WasiErrno.ESUCCESS */;
1387
- });
1388
- this.fd_renumber = syscallWrap('fd_renumber', function (from, to) {
1695
+ }, async function fd_readdir(fd, buf, buf_len, cookie, bufused) {
1696
+ buf = Number(buf);
1697
+ buf_len = Number(buf_len);
1698
+ bufused = Number(bufused);
1699
+ if (buf === 0 || bufused === 0)
1700
+ return 0 /* WasiErrno.ESUCCESS */;
1701
+ const wasi = _wasi.get(this);
1702
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READDIR, BigInt(0));
1703
+ const fs = getFs(this);
1704
+ const entries = await fs.promises.readdir(fileDescriptor.realPath, { withFileTypes: true });
1705
+ const { HEAPU8, view } = getMemory(this);
1706
+ let bufferUsed = 0;
1707
+ for (let i = Number(cookie); i < entries.length; i++) {
1708
+ const nameData = encoder.encode(entries[i].name);
1709
+ const entryInfo = await fs.promises.stat(resolve(fileDescriptor.realPath, entries[i].name), { bigint: true });
1710
+ const entryData = new Uint8Array(24 + nameData.byteLength);
1711
+ const entryView = new DataView(entryData.buffer);
1712
+ entryView.setBigUint64(0, BigInt(i + 1), true);
1713
+ entryView.setBigUint64(8, BigInt(entryInfo.ino ? entryInfo.ino : 0), true);
1714
+ entryView.setUint32(16, nameData.byteLength, true);
1715
+ let type;
1716
+ if (entries[i].isFile()) {
1717
+ type = 4 /* WasiFileType.REGULAR_FILE */;
1718
+ }
1719
+ else if (entries[i].isDirectory()) {
1720
+ type = 3 /* WasiFileType.DIRECTORY */;
1721
+ }
1722
+ else if (entries[i].isSymbolicLink()) {
1723
+ type = 7 /* WasiFileType.SYMBOLIC_LINK */;
1724
+ }
1725
+ else if (entries[i].isCharacterDevice()) {
1726
+ type = 2 /* WasiFileType.CHARACTER_DEVICE */;
1727
+ }
1728
+ else if (entries[i].isBlockDevice()) {
1729
+ type = 1 /* WasiFileType.BLOCK_DEVICE */;
1730
+ }
1731
+ else if (entries[i].isSocket()) {
1732
+ type = 6 /* WasiFileType.SOCKET_STREAM */;
1733
+ }
1734
+ else {
1735
+ type = 0 /* WasiFileType.UNKNOWN */;
1736
+ }
1737
+ entryView.setUint8(20, type);
1738
+ entryData.set(nameData, 24);
1739
+ const data = entryData.slice(0, Math.min(entryData.length, buf_len - bufferUsed));
1740
+ HEAPU8.set(data, buf + bufferUsed);
1741
+ bufferUsed += data.byteLength;
1742
+ }
1743
+ view.setUint32(bufused, bufferUsed, true);
1744
+ return 0 /* WasiErrno.ESUCCESS */;
1745
+ }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
1746
+ defineImport('fd_renumber', function fd_renumber(from, to) {
1389
1747
  const wasi = _wasi.get(this);
1390
1748
  wasi.fds.renumber(to, from);
1391
1749
  return 0 /* WasiErrno.ESUCCESS */;
1392
- });
1393
- this.fd_sync = syscallWrap('fd_sync', function (fd) {
1750
+ }, async function fd_renumber(from, to) {
1751
+ const wasi = _wasi.get(this);
1752
+ await wasi.fds.renumber(to, from);
1753
+ return 0 /* WasiErrno.ESUCCESS */;
1754
+ }, ['i32', 'i32'], ['i32']);
1755
+ defineImport('fd_sync', function fd_sync(fd) {
1394
1756
  const wasi = _wasi.get(this);
1395
1757
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SYNC, BigInt(0));
1396
1758
  const fs = getFs(this);
1397
1759
  fs.fsyncSync(fileDescriptor.fd);
1398
1760
  return 0 /* WasiErrno.ESUCCESS */;
1399
- });
1400
- this.fd_tell = syscallWrap('fd_tell', function (fd, offset) {
1761
+ }, async function fd_sync(fd) {
1401
1762
  const wasi = _wasi.get(this);
1402
- const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_TELL, BigInt(0));
1403
- const pos = BigInt(fileDescriptor.pos);
1404
- const { view } = getMemory(this);
1405
- view.setBigUint64(Number(offset), pos, true);
1763
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SYNC, BigInt(0));
1764
+ await fileDescriptor.fd.sync();
1406
1765
  return 0 /* WasiErrno.ESUCCESS */;
1407
- });
1408
- this.fd_write = syscallWrap('fd_write', function (fd, iovs, iovslen, size) {
1766
+ }, ['i32'], ['i32']);
1767
+ defineImport('fd_write', function fd_write(fd, iovs, iovslen, size) {
1409
1768
  iovs = Number(iovs);
1410
1769
  size = Number(size);
1411
1770
  if (iovs === 0 || size === 0) {
@@ -1431,8 +1790,33 @@
1431
1790
  }
1432
1791
  view.setUint32(size, nwritten, true);
1433
1792
  return 0 /* WasiErrno.ESUCCESS */;
1434
- });
1435
- this.path_create_directory = syscallWrap('path_create_directory', function (fd, path, path_len) {
1793
+ }, async function fd_write(fd, iovs, iovslen, size) {
1794
+ iovs = Number(iovs);
1795
+ size = Number(size);
1796
+ if (iovs === 0 || size === 0) {
1797
+ return 28 /* WasiErrno.EINVAL */;
1798
+ }
1799
+ const { HEAPU8, view } = getMemory(this);
1800
+ const wasi = _wasi.get(this);
1801
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
1802
+ const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
1803
+ const offset = iovs + (i * 8);
1804
+ const buf = view.getInt32(offset, true);
1805
+ const bufLen = view.getUint32(offset + 4, true);
1806
+ return HEAPU8.subarray(buf, buf + bufLen);
1807
+ }));
1808
+ let nwritten;
1809
+ if (fd === 1 || fd === 2) {
1810
+ nwritten = fileDescriptor.write(buffer);
1811
+ }
1812
+ else {
1813
+ nwritten = await (await (fileDescriptor.fd.write(buffer, 0, buffer.length, Number(fileDescriptor.pos)))).bytesWritten;
1814
+ fileDescriptor.pos += BigInt(nwritten);
1815
+ }
1816
+ view.setUint32(size, nwritten, true);
1817
+ return 0 /* WasiErrno.ESUCCESS */;
1818
+ }, ['i32', 'i32', 'i32', 'i32'], ['i32']);
1819
+ defineImport('path_create_directory', function path_create_directory(fd, path, path_len) {
1436
1820
  path = Number(path);
1437
1821
  path_len = Number(path_len);
1438
1822
  if (path === 0) {
@@ -1446,8 +1830,22 @@
1446
1830
  const fs = getFs(this);
1447
1831
  fs.mkdirSync(pathString);
1448
1832
  return 0 /* WasiErrno.ESUCCESS */;
1449
- });
1450
- this.path_filestat_get = syscallWrap('path_filestat_get', function (fd, flags, path, path_len, filestat) {
1833
+ }, async function path_create_directory(fd, path, path_len) {
1834
+ path = Number(path);
1835
+ path_len = Number(path_len);
1836
+ if (path === 0) {
1837
+ return 28 /* WasiErrno.EINVAL */;
1838
+ }
1839
+ const { HEAPU8 } = getMemory(this);
1840
+ const wasi = _wasi.get(this);
1841
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
1842
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1843
+ pathString = resolve(fileDescriptor.realPath, pathString);
1844
+ const fs = getFs(this);
1845
+ await fs.promises.mkdir(pathString);
1846
+ return 0 /* WasiErrno.ESUCCESS */;
1847
+ }, ['i32', 'i32', 'i32'], ['i32']);
1848
+ defineImport('path_filestat_get', function path_filestat_get(fd, flags, path, path_len, filestat) {
1451
1849
  path = Number(path);
1452
1850
  path_len = Number(path_len);
1453
1851
  filestat = Number(filestat);
@@ -1469,8 +1867,30 @@
1469
1867
  }
1470
1868
  toFileStat(view, filestat, stat);
1471
1869
  return 0 /* WasiErrno.ESUCCESS */;
1472
- });
1473
- this.path_filestat_set_times = syscallWrap('path_filestat_set_times', function (fd, flags, path, path_len, atim, mtim, fst_flags) {
1870
+ }, async function path_filestat_get(fd, flags, path, path_len, filestat) {
1871
+ path = Number(path);
1872
+ path_len = Number(path_len);
1873
+ filestat = Number(filestat);
1874
+ if (path === 0 || filestat === 0) {
1875
+ return 28 /* WasiErrno.EINVAL */;
1876
+ }
1877
+ const { HEAPU8, view } = getMemory(this);
1878
+ const wasi = _wasi.get(this);
1879
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
1880
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1881
+ const fs = getFs(this);
1882
+ pathString = resolve(fileDescriptor.realPath, pathString);
1883
+ let stat;
1884
+ if ((flags & 1) === 1) {
1885
+ stat = await fs.promises.stat(pathString, { bigint: true });
1886
+ }
1887
+ else {
1888
+ stat = await fs.promises.lstat(pathString, { bigint: true });
1889
+ }
1890
+ toFileStat(view, filestat, stat);
1891
+ return 0 /* WasiErrno.ESUCCESS */;
1892
+ }, ['i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1893
+ defineImport('path_filestat_set_times', function path_filestat_set_times(fd, flags, path, path_len, atim, mtim, fst_flags) {
1474
1894
  path = Number(path);
1475
1895
  path_len = Number(path_len);
1476
1896
  if (path === 0)
@@ -1485,7 +1905,7 @@
1485
1905
  const wasi = _wasi.get(this);
1486
1906
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1487
1907
  const fs = getFs(this);
1488
- const resolvedPath = resolvePath(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1908
+ const resolvedPath = resolvePathSync(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1489
1909
  if ((fst_flags & 2 /* WasiFstFlag.SET_ATIM_NOW */) === 2 /* WasiFstFlag.SET_ATIM_NOW */) {
1490
1910
  atim = BigInt(Date.now() * 1000000);
1491
1911
  }
@@ -1494,8 +1914,32 @@
1494
1914
  }
1495
1915
  fs.utimesSync(resolvedPath, Number(atim), Number(mtim));
1496
1916
  return 0 /* WasiErrno.ESUCCESS */;
1497
- });
1498
- this.path_link = syscallWrap('path_link', function (old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
1917
+ }, async function path_filestat_set_times(fd, flags, path, path_len, atim, mtim, fst_flags) {
1918
+ path = Number(path);
1919
+ path_len = Number(path_len);
1920
+ if (path === 0)
1921
+ return 28 /* WasiErrno.EINVAL */;
1922
+ if ((fst_flags) & ~(1 /* WasiFstFlag.SET_ATIM */ |
1923
+ 2 /* WasiFstFlag.SET_ATIM_NOW */ |
1924
+ 4 /* WasiFstFlag.SET_MTIM */ |
1925
+ 8 /* WasiFstFlag.SET_MTIM_NOW */)) {
1926
+ return 28 /* WasiErrno.EINVAL */;
1927
+ }
1928
+ const { HEAPU8 } = getMemory(this);
1929
+ const wasi = _wasi.get(this);
1930
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1931
+ const fs = getFs(this);
1932
+ const resolvedPath = await resolvePathAsync(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1933
+ if ((fst_flags & 2 /* WasiFstFlag.SET_ATIM_NOW */) === 2 /* WasiFstFlag.SET_ATIM_NOW */) {
1934
+ atim = BigInt(Date.now() * 1000000);
1935
+ }
1936
+ if ((fst_flags & 8 /* WasiFstFlag.SET_MTIM_NOW */) === 8 /* WasiFstFlag.SET_MTIM_NOW */) {
1937
+ mtim = BigInt(Date.now() * 1000000);
1938
+ }
1939
+ await fs.promises.utimes(resolvedPath, Number(atim), Number(mtim));
1940
+ return 0 /* WasiErrno.ESUCCESS */;
1941
+ }, ['i32', 'i32', 'i32', 'i32', 'i64', 'i64', 'i32'], ['i32']);
1942
+ defineImport('path_link', function path_link(old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
1499
1943
  old_path = Number(old_path);
1500
1944
  old_path_len = Number(old_path_len);
1501
1945
  new_path = Number(new_path);
@@ -1515,20 +1959,36 @@
1515
1959
  }
1516
1960
  const { HEAPU8 } = getMemory(this);
1517
1961
  const fs = getFs(this);
1518
- const resolvedOldPath = resolvePath(fs, oldWrap, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)), old_flags);
1962
+ const resolvedOldPath = resolvePathSync(fs, oldWrap, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)), old_flags);
1519
1963
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
1520
1964
  fs.linkSync(resolvedOldPath, resolvedNewPath);
1521
1965
  return 0 /* WasiErrno.ESUCCESS */;
1522
- });
1523
- this.path_open = syscallWrap('path_open', function (dirfd, dirflags, path, path_len, o_flags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) {
1524
- path = Number(path);
1525
- fd = Number(fd);
1526
- if (path === 0 || fd === 0) {
1966
+ }, async function path_link(old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
1967
+ old_path = Number(old_path);
1968
+ old_path_len = Number(old_path_len);
1969
+ new_path = Number(new_path);
1970
+ new_path_len = Number(new_path_len);
1971
+ if (old_path === 0 || new_path === 0) {
1527
1972
  return 28 /* WasiErrno.EINVAL */;
1528
1973
  }
1529
- path_len = Number(path_len);
1530
- fs_rights_base = BigInt(fs_rights_base);
1531
- fs_rights_base = BigInt(fs_rights_base);
1974
+ const wasi = _wasi.get(this);
1975
+ let oldWrap;
1976
+ let newWrap;
1977
+ if (old_fd === new_fd) {
1978
+ oldWrap = newWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE | WasiRights.PATH_LINK_TARGET, BigInt(0));
1979
+ }
1980
+ else {
1981
+ oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE, BigInt(0));
1982
+ newWrap = wasi.fds.get(new_fd, WasiRights.PATH_LINK_TARGET, BigInt(0));
1983
+ }
1984
+ const { HEAPU8 } = getMemory(this);
1985
+ const fs = getFs(this);
1986
+ const resolvedOldPath = await resolvePathAsync(fs, oldWrap, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)), old_flags);
1987
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
1988
+ await fs.promises.link(resolvedOldPath, resolvedNewPath);
1989
+ return 0 /* WasiErrno.ESUCCESS */;
1990
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1991
+ function pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags) {
1532
1992
  const read = (fs_rights_base & (WasiRights.FD_READ |
1533
1993
  WasiRights.FD_READDIR)) !== BigInt(0);
1534
1994
  const write = (fs_rights_base & (WasiRights.FD_DATASYNC |
@@ -1573,32 +2033,78 @@
1573
2033
  if (write && (flags & (1024 /* FileControlFlag.O_APPEND */ | 512 /* FileControlFlag.O_TRUNC */)) === 0) {
1574
2034
  needed_inheriting |= WasiRights.FD_SEEK;
1575
2035
  }
2036
+ return { flags, needed_base, needed_inheriting };
2037
+ }
2038
+ defineImport('path_open', function path_open(dirfd, dirflags, path, path_len, o_flags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) {
2039
+ path = Number(path);
2040
+ fd = Number(fd);
2041
+ if (path === 0 || fd === 0) {
2042
+ return 28 /* WasiErrno.EINVAL */;
2043
+ }
2044
+ path_len = Number(path_len);
2045
+ fs_rights_base = BigInt(fs_rights_base);
2046
+ fs_rights_inheriting = BigInt(fs_rights_inheriting);
2047
+ const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
1576
2048
  const wasi = _wasi.get(this);
1577
- const fileDescriptor = wasi.fds.get(dirfd, needed_base, needed_inheriting);
2049
+ const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1578
2050
  const memory = getMemory(this);
1579
2051
  const HEAPU8 = memory.HEAPU8;
1580
2052
  const pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1581
2053
  const fs = getFs(this);
1582
- const resolved_path = resolvePath(fs, fileDescriptor, pathString, dirflags);
1583
- const r = fs.openSync(resolved_path, flags, 0o666);
2054
+ const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
2055
+ const r = fs.openSync(resolved_path, flagsRes, 0o666);
1584
2056
  const filetype = wasi.fds.getFileTypeByFd(r);
1585
2057
  if ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 && filetype !== 3 /* WasiFileType.DIRECTORY */) {
1586
2058
  return 54 /* WasiErrno.ENOTDIR */;
1587
2059
  }
1588
- const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r, flags, filetype);
2060
+ const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r, flagsRes, filetype);
1589
2061
  const wrap = wasi.fds.insert(r, resolved_path, resolved_path, filetype, fs_rights_base & max_base, fs_rights_inheriting & max_inheriting, 0);
1590
2062
  const stat = fs.fstatSync(r, { bigint: true });
1591
2063
  if (stat.isFile()) {
1592
2064
  wrap.size = stat.size;
1593
- if ((flags & 1024 /* FileControlFlag.O_APPEND */) !== 0) {
2065
+ if ((flagsRes & 1024 /* FileControlFlag.O_APPEND */) !== 0) {
1594
2066
  wrap.pos = stat.size;
1595
2067
  }
1596
2068
  }
1597
2069
  const view = memory.view;
1598
2070
  view.setInt32(fd, wrap.id, true);
1599
2071
  return 0 /* WasiErrno.ESUCCESS */;
1600
- });
1601
- this.path_readlink = syscallWrap('path_readlink', function (fd, path, path_len, buf, buf_len, bufused) {
2072
+ }, async function path_open(dirfd, dirflags, path, path_len, o_flags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) {
2073
+ path = Number(path);
2074
+ fd = Number(fd);
2075
+ if (path === 0 || fd === 0) {
2076
+ return 28 /* WasiErrno.EINVAL */;
2077
+ }
2078
+ path_len = Number(path_len);
2079
+ fs_rights_base = BigInt(fs_rights_base);
2080
+ fs_rights_inheriting = BigInt(fs_rights_inheriting);
2081
+ const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
2082
+ const wasi = _wasi.get(this);
2083
+ const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
2084
+ const memory = getMemory(this);
2085
+ const HEAPU8 = memory.HEAPU8;
2086
+ const pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
2087
+ const fs = getFs(this);
2088
+ const resolved_path = await resolvePathAsync(fs, fileDescriptor, pathString, dirflags);
2089
+ const r = await fs.promises.open(resolved_path, flagsRes, 0o666);
2090
+ const filetype = await wasi.fds.getFileTypeByFd(r);
2091
+ if ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 && filetype !== 3 /* WasiFileType.DIRECTORY */) {
2092
+ return 54 /* WasiErrno.ENOTDIR */;
2093
+ }
2094
+ const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r.fd, flagsRes, filetype);
2095
+ const wrap = wasi.fds.insert(r, resolved_path, resolved_path, filetype, fs_rights_base & max_base, fs_rights_inheriting & max_inheriting, 0);
2096
+ const stat = await r.stat({ bigint: true });
2097
+ if (stat.isFile()) {
2098
+ wrap.size = stat.size;
2099
+ if ((flagsRes & 1024 /* FileControlFlag.O_APPEND */) !== 0) {
2100
+ wrap.pos = stat.size;
2101
+ }
2102
+ }
2103
+ const view = memory.view;
2104
+ view.setInt32(fd, wrap.id, true);
2105
+ return 0 /* WasiErrno.ESUCCESS */;
2106
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i64', 'i64', 'i32', 'i32'], ['i32']);
2107
+ defineImport('path_readlink', function path_readlink(fd, path, path_len, buf, buf_len, bufused) {
1602
2108
  path = Number(path);
1603
2109
  path_len = Number(path_len);
1604
2110
  buf = Number(buf);
@@ -1622,8 +2128,32 @@
1622
2128
  HEAPU8[buf + len] = 0;
1623
2129
  view.setUint32(bufused, len + 1, true);
1624
2130
  return 0 /* WasiErrno.ESUCCESS */;
1625
- });
1626
- this.path_remove_directory = syscallWrap('path_remove_directory', function (fd, path, path_len) {
2131
+ }, async function path_readlink(fd, path, path_len, buf, buf_len, bufused) {
2132
+ path = Number(path);
2133
+ path_len = Number(path_len);
2134
+ buf = Number(buf);
2135
+ buf_len = Number(buf_len);
2136
+ bufused = Number(bufused);
2137
+ if (path === 0 || buf === 0 || bufused === 0) {
2138
+ return 28 /* WasiErrno.EINVAL */;
2139
+ }
2140
+ const { HEAPU8, view } = getMemory(this);
2141
+ const wasi = _wasi.get(this);
2142
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
2143
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
2144
+ pathString = resolve(fileDescriptor.realPath, pathString);
2145
+ const fs = getFs(this);
2146
+ const link = await fs.promises.readlink(pathString);
2147
+ const linkData = encoder.encode(link);
2148
+ const len = Math.min(linkData.length, buf_len);
2149
+ if (len >= buf_len)
2150
+ return 42 /* WasiErrno.ENOBUFS */;
2151
+ HEAPU8.set(linkData.subarray(0, len), buf);
2152
+ HEAPU8[buf + len] = 0;
2153
+ view.setUint32(bufused, len + 1, true);
2154
+ return 0 /* WasiErrno.ESUCCESS */;
2155
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
2156
+ defineImport('path_remove_directory', function path_remove_directory(fd, path, path_len) {
1627
2157
  path = Number(path);
1628
2158
  path_len = Number(path_len);
1629
2159
  if (path === 0) {
@@ -1637,8 +2167,22 @@
1637
2167
  const fs = getFs(this);
1638
2168
  fs.rmdirSync(pathString);
1639
2169
  return 0 /* WasiErrno.ESUCCESS */;
1640
- });
1641
- this.path_rename = syscallWrap('path_rename', function (old_fd, old_path, old_path_len, new_fd, new_path, new_path_len) {
2170
+ }, async function path_remove_directory(fd, path, path_len) {
2171
+ path = Number(path);
2172
+ path_len = Number(path_len);
2173
+ if (path === 0) {
2174
+ return 28 /* WasiErrno.EINVAL */;
2175
+ }
2176
+ const { HEAPU8 } = getMemory(this);
2177
+ const wasi = _wasi.get(this);
2178
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
2179
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
2180
+ pathString = resolve(fileDescriptor.realPath, pathString);
2181
+ const fs = getFs(this);
2182
+ await fs.promises.rmdir(pathString);
2183
+ return 0 /* WasiErrno.ESUCCESS */;
2184
+ }, ['i32', 'i32', 'i32'], ['i32']);
2185
+ defineImport('path_rename', function path_rename(old_fd, old_path, old_path_len, new_fd, new_path, new_path_len) {
1642
2186
  old_path = Number(old_path);
1643
2187
  old_path_len = Number(old_path_len);
1644
2188
  new_path = Number(new_path);
@@ -1662,8 +2206,32 @@
1662
2206
  const fs = getFs(this);
1663
2207
  fs.renameSync(resolvedOldPath, resolvedNewPath);
1664
2208
  return 0 /* WasiErrno.ESUCCESS */;
1665
- });
1666
- this.path_symlink = syscallWrap('path_symlink', function (old_path, old_path_len, fd, new_path, new_path_len) {
2209
+ }, async function path_rename(old_fd, old_path, old_path_len, new_fd, new_path, new_path_len) {
2210
+ old_path = Number(old_path);
2211
+ old_path_len = Number(old_path_len);
2212
+ new_path = Number(new_path);
2213
+ new_path_len = Number(new_path_len);
2214
+ if (old_path === 0 || new_path === 0) {
2215
+ return 28 /* WasiErrno.EINVAL */;
2216
+ }
2217
+ const wasi = _wasi.get(this);
2218
+ let oldWrap;
2219
+ let newWrap;
2220
+ if (old_fd === new_fd) {
2221
+ oldWrap = newWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE | WasiRights.PATH_RENAME_TARGET, BigInt(0));
2222
+ }
2223
+ else {
2224
+ oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE, BigInt(0));
2225
+ newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
2226
+ }
2227
+ const { HEAPU8 } = getMemory(this);
2228
+ const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)));
2229
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
2230
+ const fs = getFs(this);
2231
+ await fs.promises.rename(resolvedOldPath, resolvedNewPath);
2232
+ return 0 /* WasiErrno.ESUCCESS */;
2233
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
2234
+ defineImport('path_symlink', function path_symlink(old_path, old_path_len, fd, new_path, new_path_len) {
1667
2235
  old_path = Number(old_path);
1668
2236
  old_path_len = Number(old_path_len);
1669
2237
  new_path = Number(new_path);
@@ -1680,8 +2248,25 @@
1680
2248
  const fs = getFs(this);
1681
2249
  fs.symlinkSync(oldPath, newPath);
1682
2250
  return 0 /* WasiErrno.ESUCCESS */;
1683
- });
1684
- this.path_unlink_file = syscallWrap('path_unlink_file', function (fd, path, path_len) {
2251
+ }, async function path_symlink(old_path, old_path_len, fd, new_path, new_path_len) {
2252
+ old_path = Number(old_path);
2253
+ old_path_len = Number(old_path_len);
2254
+ new_path = Number(new_path);
2255
+ new_path_len = Number(new_path_len);
2256
+ if (old_path === 0 || new_path === 0) {
2257
+ return 28 /* WasiErrno.EINVAL */;
2258
+ }
2259
+ const { HEAPU8 } = getMemory(this);
2260
+ const wasi = _wasi.get(this);
2261
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
2262
+ const oldPath = decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len));
2263
+ let newPath = decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len));
2264
+ newPath = resolve(fileDescriptor.realPath, newPath);
2265
+ const fs = getFs(this);
2266
+ await fs.promises.symlink(oldPath, newPath);
2267
+ return 0 /* WasiErrno.ESUCCESS */;
2268
+ }, ['i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
2269
+ defineImport('path_unlink_file', function path_unlink_file(fd, path, path_len) {
1685
2270
  path = Number(path);
1686
2271
  path_len = Number(path_len);
1687
2272
  if (path === 0) {
@@ -1695,58 +2280,30 @@
1695
2280
  const fs = getFs(this);
1696
2281
  fs.unlinkSync(pathString);
1697
2282
  return 0 /* WasiErrno.ESUCCESS */;
1698
- });
1699
- this.poll_oneoff = syscallWrap('poll_oneoff', function (_in, _out, _sub, _nevents) {
1700
- return 52 /* WasiErrno.ENOSYS */;
1701
- });
1702
- this.proc_exit = syscallWrap('proc_exit', function (_rval) {
1703
- return 0 /* WasiErrno.ESUCCESS */;
1704
- });
1705
- this.proc_raise = syscallWrap('proc_raise', function (_sig) {
1706
- return 52 /* WasiErrno.ENOSYS */;
1707
- });
1708
- this.sched_yield = syscallWrap('sched_yield', function () {
2283
+ }, async function path_unlink_file(fd, path, path_len) {
2284
+ path = Number(path);
2285
+ path_len = Number(path_len);
2286
+ if (path === 0) {
2287
+ return 28 /* WasiErrno.EINVAL */;
2288
+ }
2289
+ const { HEAPU8 } = getMemory(this);
2290
+ const wasi = _wasi.get(this);
2291
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
2292
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
2293
+ pathString = resolve(fileDescriptor.realPath, pathString);
2294
+ const fs = getFs(this);
2295
+ await fs.promises.unlink(pathString);
1709
2296
  return 0 /* WasiErrno.ESUCCESS */;
1710
- });
1711
- this.random_get = typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function'
1712
- ? syscallWrap('random_get', function (buf, buf_len) {
1713
- buf = Number(buf);
1714
- if (buf === 0) {
1715
- return 28 /* WasiErrno.EINVAL */;
1716
- }
1717
- buf_len = Number(buf_len);
1718
- const { HEAPU8 } = getMemory(this);
1719
- let pos;
1720
- const stride = 65536;
1721
- for (pos = 0; pos + stride < buf_len; pos += stride) {
1722
- crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + pos + stride));
1723
- }
1724
- crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + buf_len));
1725
- return 0 /* WasiErrno.ESUCCESS */;
1726
- })
1727
- : syscallWrap('random_get', function (buf, buf_len) {
1728
- buf = Number(buf);
1729
- if (buf === 0) {
1730
- return 28 /* WasiErrno.EINVAL */;
1731
- }
1732
- buf_len = Number(buf_len);
1733
- const { view } = getMemory(this);
1734
- for (let i = buf; i < buf + buf_len; ++i) {
1735
- view.setUint8(i, Math.floor(Math.random() * 256));
1736
- }
1737
- return 0 /* WasiErrno.ESUCCESS */;
1738
- });
1739
- this.sock_recv = syscallWrap('sock_recv', function () {
1740
- return 58 /* WasiErrno.ENOTSUP */;
1741
- });
1742
- this.sock_send = syscallWrap('sock_send', function () {
1743
- return 58 /* WasiErrno.ENOTSUP */;
1744
- });
1745
- this.sock_shutdown = syscallWrap('sock_shutdown', function () {
1746
- return 58 /* WasiErrno.ENOTSUP */;
1747
- });
1748
- const fs = filesystem ? filesystem.fs : undefined;
1749
- const fds = new FileDescriptorTable({
2297
+ }, ['i32', 'i32', 'i32'], ['i32']);
2298
+ this._setMemory = function setMemory(m) {
2299
+ if (!(m instanceof _WebAssembly.Memory)) {
2300
+ throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
2301
+ }
2302
+ _memory.set(_this, extendMemory(m));
2303
+ };
2304
+ }
2305
+ static createSync(args, env, preopens, stdio, fs, print, printErr) {
2306
+ const fds = new SyncTable({
1750
2307
  size: 3,
1751
2308
  in: stdio[0],
1752
2309
  out: stdio[1],
@@ -1755,13 +2312,7 @@
1755
2312
  print,
1756
2313
  printErr
1757
2314
  });
1758
- _wasi.set(this, {
1759
- fds,
1760
- args,
1761
- env
1762
- });
1763
- if (fs)
1764
- _fs.set(this, fs);
2315
+ const _this = new WASI$1(args, env, fds, false, fs);
1765
2316
  if (preopens.length > 0) {
1766
2317
  for (let i = 0; i < preopens.length; ++i) {
1767
2318
  const realPath = fs.realpathSync(preopens[i].realPath, 'utf8');
@@ -1769,10 +2320,32 @@
1769
2320
  fds.insertPreopen(fd, preopens[i].mappedPath, realPath);
1770
2321
  }
1771
2322
  }
2323
+ return _this;
2324
+ }
2325
+ static async createAsync(args, env, preopens, stdio, fs, print, printErr, asyncify) {
2326
+ const fds = new AsyncTable({
2327
+ size: 3,
2328
+ in: stdio[0],
2329
+ out: stdio[1],
2330
+ err: stdio[2],
2331
+ print,
2332
+ printErr
2333
+ });
2334
+ const _this = new WASI$1(args, env, fds, true, fs, asyncify);
2335
+ if (preopens.length > 0) {
2336
+ for (let i = 0; i < preopens.length; ++i) {
2337
+ const entry = preopens[i];
2338
+ const realPath = await fs.promises.realpath(entry.realPath);
2339
+ const fd = await fs.promises.open(realPath, 'r', 0o666);
2340
+ await fds.insertPreopen(fd, entry.mappedPath, realPath);
2341
+ }
2342
+ }
2343
+ return _this;
1772
2344
  }
1773
2345
  }
1774
2346
 
1775
- const kEmptyObject = Object.freeze(Object.create(null));
2347
+ // eslint-disable-next-line spaced-comment
2348
+ const kEmptyObject = /*#__PURE__*/ Object.freeze(/*#__PURE__*/ Object.create(null));
1776
2349
  const kExitCode = Symbol('kExitCode');
1777
2350
  const kSetMemory = Symbol('kSetMemory');
1778
2351
  const kStarted = Symbol('kStarted');
@@ -1785,74 +2358,105 @@
1785
2358
  }
1786
2359
  // /** @public */
1787
2360
  // export type WasiSnapshotPreview1 = Omit<_WASI, '_setMemory'>
1788
- /** @public */
1789
- class WASI {
1790
- constructor(options = kEmptyObject) {
1791
- var _a;
1792
- validateObject(options, 'options');
1793
- if (options.args !== undefined) {
1794
- validateArray(options.args, 'options.args');
1795
- }
1796
- const args = ((_a = options.args) !== null && _a !== void 0 ? _a : []).map(String);
1797
- const env = [];
1798
- if (options.env !== undefined) {
1799
- validateObject(options.env, 'options.env');
1800
- Object.entries(options.env).forEach(({ 0: key, 1: value }) => {
1801
- if (value !== undefined) {
1802
- env.push(`${key}=${value}`);
1803
- }
1804
- });
1805
- }
1806
- const preopens = [];
1807
- if (options.preopens !== undefined) {
1808
- validateObject(options.preopens, 'options.preopens');
1809
- Object.entries(options.preopens).forEach(({ 0: key, 1: value }) => preopens.push({ mappedPath: String(key), realPath: String(value) }));
1810
- }
1811
- if (preopens.length > 0) {
1812
- if (options.filesystem === undefined) {
1813
- throw new Error('filesystem is disabled, can not preopen directory');
1814
- }
1815
- }
1816
- if (options.filesystem !== undefined) {
1817
- validateObject(options.filesystem, 'options.filesystem');
1818
- validateString(options.filesystem.type, 'options.filesystem.type');
1819
- if (options.filesystem.type !== 'memfs') {
1820
- throw new Error(`Filesystem type ${options.filesystem.type} is not supported, only "memfs" is supported currently`);
1821
- }
1822
- try {
1823
- validateObject(options.filesystem.fs, 'options.filesystem.fs');
1824
- }
1825
- catch (_) {
1826
- throw new Error('Node.js fs like implementation is not provided');
2361
+ function validateOptions(options) {
2362
+ var _a;
2363
+ validateObject(options, 'options');
2364
+ if (options.args !== undefined) {
2365
+ validateArray(options.args, 'options.args');
2366
+ }
2367
+ const args = ((_a = options.args) !== null && _a !== void 0 ? _a : []).map(String);
2368
+ const env = [];
2369
+ if (options.env !== undefined) {
2370
+ validateObject(options.env, 'options.env');
2371
+ Object.entries(options.env).forEach(({ 0: key, 1: value }) => {
2372
+ if (value !== undefined) {
2373
+ env.push(`${key}=${value}`);
1827
2374
  }
2375
+ });
2376
+ }
2377
+ const preopens = [];
2378
+ if (options.preopens !== undefined) {
2379
+ validateObject(options.preopens, 'options.preopens');
2380
+ Object.entries(options.preopens).forEach(({ 0: key, 1: value }) => preopens.push({ mappedPath: String(key), realPath: String(value) }));
2381
+ }
2382
+ if (preopens.length > 0) {
2383
+ if (options.fs === undefined) {
2384
+ throw new Error('filesystem is disabled, can not preopen directory');
1828
2385
  }
1829
- if (options.print !== undefined)
1830
- validateFunction(options.print, 'options.print');
1831
- if (options.printErr !== undefined)
1832
- validateFunction(options.printErr, 'options.printErr');
1833
- // const { stdin = 0, stdout = 1, stderr = 2 } = options
1834
- // validateInt32(stdin, 'options.stdin', 0)
1835
- // validateInt32(stdout, 'options.stdout', 0)
1836
- // validateInt32(stderr, 'options.stderr', 0)
1837
- // const stdio = [stdin, stdout, stderr] as const
1838
- const stdio = [0, 1, 2];
1839
- const wrap = new WASI$1(args, env, preopens, stdio, options.filesystem, options.print, options.printErr);
1840
- for (const prop in wrap) {
1841
- wrap[prop] = wrap[prop].bind(wrap);
1842
- }
1843
- if (options.returnOnExit !== undefined) {
1844
- validateBoolean(options.returnOnExit, 'options.returnOnExit');
1845
- if (options.returnOnExit) {
1846
- wrap.proc_exit = wasiReturnOnProcExit.bind(this);
1847
- }
2386
+ try {
2387
+ validateObject(options.fs, 'options.fs');
1848
2388
  }
1849
- this[kSetMemory] = wrap._setMemory;
1850
- delete wrap._setMemory;
2389
+ catch (_) {
2390
+ throw new TypeError('Node.js fs like implementation is not provided');
2391
+ }
2392
+ }
2393
+ // if (options.filesystem !== undefined) {
2394
+ // validateObject(options.filesystem, 'options.filesystem')
2395
+ // validateString(options.filesystem.type, 'options.filesystem.type')
2396
+ // if (options.filesystem.type !== 'memfs' && options.filesystem.type !== 'file-system-access-api') {
2397
+ // throw new Error(`Filesystem type ${(options.filesystem as any).type as string} is not supported, only "memfs" and "file-system-access-api" is supported currently`)
2398
+ // }
2399
+ // try {
2400
+ // validateObject(options.filesystem.fs, 'options.filesystem.fs')
2401
+ // } catch (_) {
2402
+ // throw new Error('Node.js fs like implementation is not provided')
2403
+ // }
2404
+ // }
2405
+ if (options.print !== undefined)
2406
+ validateFunction(options.print, 'options.print');
2407
+ if (options.printErr !== undefined)
2408
+ validateFunction(options.printErr, 'options.printErr');
2409
+ if (options.returnOnExit !== undefined) {
2410
+ validateBoolean(options.returnOnExit, 'options.returnOnExit');
2411
+ }
2412
+ // const { stdin = 0, stdout = 1, stderr = 2 } = options
2413
+ // validateInt32(stdin, 'options.stdin', 0)
2414
+ // validateInt32(stdout, 'options.stdout', 0)
2415
+ // validateInt32(stderr, 'options.stderr', 0)
2416
+ // const stdio = [stdin, stdout, stderr] as const
2417
+ const stdio = [0, 1, 2];
2418
+ return {
2419
+ args,
2420
+ env,
2421
+ preopens,
2422
+ stdio
2423
+ };
2424
+ }
2425
+ /** @public */
2426
+ class WASI {
2427
+ constructor(setMemory, wrap) {
2428
+ this[kSetMemory] = setMemory;
1851
2429
  this.wasiImport = wrap;
1852
2430
  this[kStarted] = false;
1853
2431
  this[kExitCode] = 0;
1854
2432
  this[kInstance] = undefined;
1855
2433
  }
2434
+ static createSync(options = kEmptyObject) {
2435
+ const { args, env, preopens, stdio } = validateOptions(options);
2436
+ const wrap = WASI$1.createSync(args, env, preopens, stdio, options.fs, options.print, options.printErr);
2437
+ const setMemory = wrap._setMemory;
2438
+ delete wrap._setMemory;
2439
+ const _this = new WASI(setMemory, wrap);
2440
+ if (options.returnOnExit) {
2441
+ wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
2442
+ }
2443
+ return _this;
2444
+ }
2445
+ static async createAsync(options = kEmptyObject) {
2446
+ const { args, env, preopens, stdio } = validateOptions(options);
2447
+ if (options.asyncify !== undefined) {
2448
+ validateObject(options.asyncify, 'options.asyncify');
2449
+ validateFunction(options.asyncify.wrapImportFunction, 'options.asyncify.wrapImportFunction');
2450
+ }
2451
+ const wrap = await WASI$1.createAsync(args, env, preopens, stdio, options.fs, options.print, options.printErr, options.asyncify);
2452
+ const setMemory = wrap._setMemory;
2453
+ delete wrap._setMemory;
2454
+ const _this = new WASI(setMemory, wrap);
2455
+ if (options.returnOnExit) {
2456
+ wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
2457
+ }
2458
+ return _this;
2459
+ }
1856
2460
  // Must not export _initialize, must export _start
1857
2461
  start(instance) {
1858
2462
  if (this[kStarted]) {
@@ -1903,48 +2507,12 @@
1903
2507
  throw kExitCode;
1904
2508
  }
1905
2509
 
1906
- function checkWebAssemblyFunction() {
1907
- const WebAssemblyFunction = _WebAssembly.Function;
1908
- if (typeof WebAssemblyFunction !== 'function') {
1909
- throw new Error('WebAssembly.Function is not supported in this environment.' +
1910
- ' If you are using V8 based browser like Chrome, try to specify' +
1911
- ' --js-flag="--wasm-staging --experimental-wasm-stack-switching"');
1912
- }
1913
- return WebAssemblyFunction;
1914
- }
1915
- /** @public */
1916
- function wrapAsyncImport(f, parameterType, returnType) {
1917
- const WebAssemblyFunction = checkWebAssemblyFunction();
1918
- if (typeof f !== 'function') {
1919
- throw new TypeError('Function required');
1920
- }
1921
- const parameters = parameterType.slice(0);
1922
- parameters.unshift('externref');
1923
- return new WebAssemblyFunction({ parameters, results: returnType }, f, { suspending: 'first' });
1924
- }
1925
- /** @public */
1926
- function wrapAsyncExport(f) {
1927
- const WebAssemblyFunction = checkWebAssemblyFunction();
1928
- if (typeof f !== 'function') {
1929
- throw new TypeError('Function required');
1930
- }
1931
- return new WebAssemblyFunction({ parameters: [...WebAssemblyFunction.type(f).parameters.slice(1)], results: ['externref'] }, f, { promising: 'first' });
1932
- }
1933
- /** @public */
1934
- function wrapExports(exports, needWrap) {
1935
- return wrapInstanceExports(exports, (exportValue, name) => {
1936
- let ignore = typeof exportValue !== 'function';
1937
- if (Array.isArray(needWrap)) {
1938
- ignore = ignore || (needWrap.indexOf(name) === -1);
1939
- }
1940
- return ignore ? exportValue : wrapAsyncExport(exportValue);
1941
- });
1942
- }
1943
-
1944
2510
  exports.Asyncify = Asyncify;
1945
2511
  exports.Memory = Memory;
1946
2512
  exports.WASI = WASI;
1947
2513
  exports.WebAssemblyMemory = WebAssemblyMemory;
2514
+ exports.asyncifyLoad = asyncifyLoad;
2515
+ exports.asyncifyLoadSync = asyncifyLoadSync;
1948
2516
  exports.extendMemory = extendMemory;
1949
2517
  exports.load = load;
1950
2518
  exports.loadSync = loadSync;