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