@tybys/wasm-util 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
  }
@@ -212,6 +216,11 @@
212
216
  // }
213
217
  // Object.defineProperty(Instance.prototype, 'exports', { enumerable: true })
214
218
 
219
+ function validateImports(imports) {
220
+ if (imports && typeof imports !== 'object') {
221
+ throw new TypeError('imports must be an object or undefined');
222
+ }
223
+ }
215
224
  async function fetchWasm(urlOrBuffer, imports) {
216
225
  if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
217
226
  return await _WebAssembly.instantiate(urlOrBuffer, imports);
@@ -222,24 +231,12 @@
222
231
  return source;
223
232
  }
224
233
  /** @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
- }
234
+ async function load(urlOrBuffer, imports) {
235
+ validateImports(imports);
230
236
  imports = imports !== null && imports !== void 0 ? imports : {};
231
- let asyncifyHelper;
232
237
  let source;
233
- if (asyncify) {
234
- asyncifyHelper = new Asyncify();
235
- imports = asyncifyHelper.wrapImports(imports);
236
- }
237
238
  if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
238
239
  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
240
  return source;
244
241
  }
245
242
  if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
@@ -256,35 +253,41 @@
256
253
  else {
257
254
  source = await fetchWasm(urlOrBuffer, imports);
258
255
  }
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
256
  return source;
264
257
  }
265
258
  /** @public */
266
- function loadSync(buffer, imports, asyncify) {
259
+ async function asyncifyLoad(asyncify, urlOrBuffer, imports) {
267
260
  var _a;
261
+ validateImports(imports);
262
+ imports = imports !== null && imports !== void 0 ? imports : {};
263
+ const asyncifyHelper = new Asyncify();
264
+ imports = asyncifyHelper.wrapImports(imports);
265
+ const source = await load(urlOrBuffer, imports);
266
+ const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
267
+ return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
268
+ }
269
+ /** @public */
270
+ function loadSync(buffer, imports) {
268
271
  if ((buffer instanceof ArrayBuffer) && !ArrayBuffer.isView(buffer)) {
269
272
  throw new TypeError('Invalid source');
270
273
  }
271
- if (imports && typeof imports !== 'object') {
272
- throw new TypeError('imports must be an object or undefined');
273
- }
274
+ validateImports(imports);
274
275
  imports = imports !== null && imports !== void 0 ? imports : {};
275
- let asyncifyHelper;
276
- if (asyncify) {
277
- asyncifyHelper = new Asyncify();
278
- imports = asyncifyHelper.wrapImports(imports);
279
- }
280
276
  const module = new _WebAssembly.Module(buffer);
281
277
  const instance = new _WebAssembly.Instance(module, imports);
282
278
  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
279
  return source;
280
+ }
281
+ /** @public */
282
+ function asyncifyLoadSync(asyncify, buffer, imports) {
283
+ var _a;
284
+ validateImports(imports);
285
+ imports = imports !== null && imports !== void 0 ? imports : {};
286
+ const asyncifyHelper = new Asyncify();
287
+ imports = asyncifyHelper.wrapImports(imports);
288
+ const source = loadSync(buffer, imports);
289
+ const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
290
+ return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
288
291
  }
289
292
 
290
293
  const CHAR_DOT = 46; /* . */
@@ -760,7 +763,6 @@
760
763
  this.size = options.size;
761
764
  this.fds = Array(options.size);
762
765
  this.stdio = [options.in, options.out, options.err];
763
- this.fs = options.fs;
764
766
  this.print = options.print;
765
767
  this.printErr = options.printErr;
766
768
  this.insertStdio(options.in, 0, '<stdin>');
@@ -807,18 +809,6 @@
807
809
  this.used++;
808
810
  return entry;
809
811
  }
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
812
  get(id, base, inheriting) {
823
813
  if (id >= this.size) {
824
814
  throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
@@ -844,6 +834,24 @@
844
834
  this.fds[id] = undefined;
845
835
  this.used--;
846
836
  }
837
+ }
838
+ class SyncTable extends FileDescriptorTable {
839
+ constructor(options) {
840
+ super(options);
841
+ this.fs = options.fs;
842
+ }
843
+ getFileTypeByFd(fd) {
844
+ const stats = this.fs.fstatSync(fd, { bigint: true });
845
+ return toFileType(stats);
846
+ }
847
+ insertPreopen(fd, mappedPath, realPath) {
848
+ const type = this.getFileTypeByFd(fd);
849
+ if (type !== 3 /* WasiFileType.DIRECTORY */) {
850
+ throw new WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, 54 /* WasiErrno.ENOTDIR */);
851
+ }
852
+ const result = getRights(this.stdio, fd, 0, type);
853
+ return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
854
+ }
847
855
  renumber(dst, src) {
848
856
  if (dst === src)
849
857
  return;
@@ -861,6 +869,41 @@
861
869
  this.fds[src] = undefined;
862
870
  this.used--;
863
871
  }
872
+ }
873
+ class AsyncTable extends FileDescriptorTable {
874
+ // eslint-disable-next-line @typescript-eslint/no-useless-constructor
875
+ constructor(options) {
876
+ super(options);
877
+ }
878
+ async getFileTypeByFd(fd) {
879
+ const stats = await fd.stat({ bigint: true });
880
+ return toFileType(stats);
881
+ }
882
+ async insertPreopen(fd, mappedPath, realPath) {
883
+ const type = await this.getFileTypeByFd(fd);
884
+ if (type !== 3 /* WasiFileType.DIRECTORY */) {
885
+ throw new WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, 54 /* WasiErrno.ENOTDIR */);
886
+ }
887
+ const result = getRights(this.stdio, fd.fd, 0, type);
888
+ return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
889
+ }
890
+ async renumber(dst, src) {
891
+ if (dst === src)
892
+ return;
893
+ if (dst >= this.size || src >= this.size) {
894
+ throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
895
+ }
896
+ const dstEntry = this.fds[dst];
897
+ const srcEntry = this.fds[src];
898
+ if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
899
+ throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
900
+ }
901
+ await dstEntry.fd.close();
902
+ this.fds[dst] = this.fds[src];
903
+ this.fds[dst].id = dst;
904
+ this.fds[src] = undefined;
905
+ this.used--;
906
+ }
864
907
  }
865
908
 
866
909
  /** @public */
@@ -891,6 +934,44 @@
891
934
  return memory;
892
935
  }
893
936
 
937
+ function checkWebAssemblyFunction() {
938
+ const WebAssemblyFunction = _WebAssembly.Function;
939
+ if (typeof WebAssemblyFunction !== 'function') {
940
+ throw new Error('WebAssembly.Function is not supported in this environment.' +
941
+ ' If you are using V8 based browser like Chrome, try to specify' +
942
+ ' --js-flag="--wasm-staging --experimental-wasm-stack-switching"');
943
+ }
944
+ return WebAssemblyFunction;
945
+ }
946
+ /** @public */
947
+ function wrapAsyncImport(f, parameterType, returnType) {
948
+ const WebAssemblyFunction = checkWebAssemblyFunction();
949
+ if (typeof f !== 'function') {
950
+ throw new TypeError('Function required');
951
+ }
952
+ const parameters = parameterType.slice(0);
953
+ parameters.unshift('externref');
954
+ return new WebAssemblyFunction({ parameters, results: returnType }, f, { suspending: 'first' });
955
+ }
956
+ /** @public */
957
+ function wrapAsyncExport(f) {
958
+ const WebAssemblyFunction = checkWebAssemblyFunction();
959
+ if (typeof f !== 'function') {
960
+ throw new TypeError('Function required');
961
+ }
962
+ return new WebAssemblyFunction({ parameters: [...WebAssemblyFunction.type(f).parameters.slice(1)], results: ['externref'] }, f, { promising: 'first' });
963
+ }
964
+ /** @public */
965
+ function wrapExports(exports, needWrap) {
966
+ return wrapInstanceExports(exports, (exportValue, name) => {
967
+ let ignore = typeof exportValue !== 'function';
968
+ if (Array.isArray(needWrap)) {
969
+ ignore = ignore || (needWrap.indexOf(name) === -1);
970
+ }
971
+ return ignore ? exportValue : wrapAsyncExport(exportValue);
972
+ });
973
+ }
974
+
894
975
  function copyMemory(targets, src) {
895
976
  if (targets.length === 0 || src.length === 0)
896
977
  return 0;
@@ -949,7 +1030,7 @@
949
1030
  Object.defineProperty(f, 'name', { value: name });
950
1031
  return f;
951
1032
  }
952
- function syscallWrap(name, f) {
1033
+ function syscallWrap(self, name, f) {
953
1034
  return defineName(name, function () {
954
1035
  {
955
1036
  const args = Array.prototype.slice.call(arguments);
@@ -959,7 +1040,7 @@
959
1040
  }
960
1041
  let r;
961
1042
  try {
962
- r = f.apply(this, arguments);
1043
+ r = f.apply(self, arguments);
963
1044
  }
964
1045
  catch (err) {
965
1046
  return handleError(err);
@@ -970,7 +1051,7 @@
970
1051
  return r;
971
1052
  });
972
1053
  }
973
- function resolvePath(fs, fileDescriptor, path, flags) {
1054
+ function resolvePathSync(fs, fileDescriptor, path, flags) {
974
1055
  let resolvedPath = resolve(fileDescriptor.realPath, path);
975
1056
  if ((flags & 1) === 1) {
976
1057
  try {
@@ -984,6 +1065,20 @@
984
1065
  }
985
1066
  return resolvedPath;
986
1067
  }
1068
+ async function resolvePathAsync(fs, fileDescriptor, path, flags) {
1069
+ let resolvedPath = resolve(fileDescriptor.realPath, path);
1070
+ if ((flags & 1) === 1) {
1071
+ try {
1072
+ resolvedPath = await fs.promises.readlink(resolvedPath);
1073
+ }
1074
+ catch (err) {
1075
+ if (err.code !== 'EINVAL' && err.code !== 'ENOENT') {
1076
+ throw err;
1077
+ }
1078
+ }
1079
+ }
1080
+ return resolvedPath;
1081
+ }
987
1082
  const encoder = new TextEncoder();
988
1083
  const decoder = new TextDecoder();
989
1084
  function readStdin() {
@@ -994,14 +1089,8 @@
994
1089
  return buffer;
995
1090
  }
996
1091
  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) {
1092
+ constructor(args, env, fds, asyncFs, fs, asyncify) {
1093
+ this.args_get = syscallWrap(this, 'args_get', function (argv, argv_buf) {
1005
1094
  argv = Number(argv);
1006
1095
  argv_buf = Number(argv_buf);
1007
1096
  if (argv === 0 || argv_buf === 0) {
@@ -1020,7 +1109,7 @@
1020
1109
  }
1021
1110
  return 0 /* WasiErrno.ESUCCESS */;
1022
1111
  });
1023
- this.args_sizes_get = syscallWrap('args_sizes_get', function (argc, argv_buf_size) {
1112
+ this.args_sizes_get = syscallWrap(this, 'args_sizes_get', function (argc, argv_buf_size) {
1024
1113
  argc = Number(argc);
1025
1114
  argv_buf_size = Number(argv_buf_size);
1026
1115
  if (argc === 0 || argv_buf_size === 0) {
@@ -1033,7 +1122,7 @@
1033
1122
  view.setUint32(argv_buf_size, encoder.encode(args.join('\0') + '\0').length, true);
1034
1123
  return 0 /* WasiErrno.ESUCCESS */;
1035
1124
  });
1036
- this.environ_get = syscallWrap('environ_get', function (environ, environ_buf) {
1125
+ this.environ_get = syscallWrap(this, 'environ_get', function (environ, environ_buf) {
1037
1126
  environ = Number(environ);
1038
1127
  environ_buf = Number(environ_buf);
1039
1128
  if (environ === 0 || environ_buf === 0) {
@@ -1052,7 +1141,7 @@
1052
1141
  }
1053
1142
  return 0 /* WasiErrno.ESUCCESS */;
1054
1143
  });
1055
- this.environ_sizes_get = syscallWrap('environ_sizes_get', function (len, buflen) {
1144
+ this.environ_sizes_get = syscallWrap(this, 'environ_sizes_get', function (len, buflen) {
1056
1145
  len = Number(len);
1057
1146
  buflen = Number(buflen);
1058
1147
  if (len === 0 || buflen === 0) {
@@ -1064,7 +1153,7 @@
1064
1153
  view.setUint32(buflen, encoder.encode(wasi.env.join('\0') + '\0').length, true);
1065
1154
  return 0 /* WasiErrno.ESUCCESS */;
1066
1155
  });
1067
- this.clock_res_get = syscallWrap('clock_res_get', function (id, resolution) {
1156
+ this.clock_res_get = syscallWrap(this, 'clock_res_get', function (id, resolution) {
1068
1157
  resolution = Number(resolution);
1069
1158
  if (resolution === 0) {
1070
1159
  return 28 /* WasiErrno.EINVAL */;
@@ -1082,7 +1171,7 @@
1082
1171
  default: return 28 /* WasiErrno.EINVAL */;
1083
1172
  }
1084
1173
  });
1085
- this.clock_time_get = syscallWrap('clock_time_get', function (id, _percision, time) {
1174
+ this.clock_time_get = syscallWrap(this, 'clock_time_get', function (id, _percision, time) {
1086
1175
  time = Number(time);
1087
1176
  if (time === 0) {
1088
1177
  return 28 /* WasiErrno.EINVAL */;
@@ -1105,35 +1194,10 @@
1105
1194
  default: return 28 /* WasiErrno.EINVAL */;
1106
1195
  }
1107
1196
  });
1108
- this.fd_advise = syscallWrap('fd_advise', function (_fd, _offset, _len, _advice) {
1197
+ this.fd_advise = syscallWrap(this, 'fd_advise', function (_fd, _offset, _len, _advice) {
1109
1198
  return 52 /* WasiErrno.ENOSYS */;
1110
1199
  });
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) {
1200
+ this.fd_fdstat_get = syscallWrap(this, 'fd_fdstat_get', function (fd, fdstat) {
1137
1201
  fdstat = Number(fdstat);
1138
1202
  if (fdstat === 0) {
1139
1203
  return 28 /* WasiErrno.EINVAL */;
@@ -1147,10 +1211,10 @@
1147
1211
  view.setBigUint64(fdstat + 16, fileDescriptor.rightsInheriting, true);
1148
1212
  return 0 /* WasiErrno.ESUCCESS */;
1149
1213
  });
1150
- this.fd_fdstat_set_flags = syscallWrap('fd_fdstat_set_flags', function (_fd, _flags) {
1214
+ this.fd_fdstat_set_flags = syscallWrap(this, 'fd_fdstat_set_flags', function (_fd, _flags) {
1151
1215
  return 52 /* WasiErrno.ENOSYS */;
1152
1216
  });
1153
- this.fd_fdstat_set_rights = syscallWrap('fd_fdstat_set_rights', function (fd, rightsBase, rightsInheriting) {
1217
+ this.fd_fdstat_set_rights = syscallWrap(this, 'fd_fdstat_set_rights', function (fd, rightsBase, rightsInheriting) {
1154
1218
  const wasi = _wasi.get(this);
1155
1219
  const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
1156
1220
  if ((rightsBase | fileDescriptor.rightsBase) > fileDescriptor.rightsBase) {
@@ -1164,65 +1228,7 @@
1164
1228
  fileDescriptor.rightsInheriting = rightsInheriting;
1165
1229
  return 0 /* WasiErrno.ESUCCESS */;
1166
1230
  });
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) {
1231
+ this.fd_prestat_get = syscallWrap(this, 'fd_prestat_get', function (fd, prestat) {
1226
1232
  prestat = Number(prestat);
1227
1233
  if (prestat === 0) {
1228
1234
  return 28 /* WasiErrno.EINVAL */;
@@ -1245,7 +1251,7 @@
1245
1251
  view.setUint32(prestat + 4, encoder.encode(fileDescriptor.path).length + 1, true);
1246
1252
  return 0 /* WasiErrno.ESUCCESS */;
1247
1253
  });
1248
- this.fd_prestat_dir_name = syscallWrap('fd_prestat_dir_name', function (fd, path, path_len) {
1254
+ this.fd_prestat_dir_name = syscallWrap(this, 'fd_prestat_dir_name', function (fd, path, path_len) {
1249
1255
  path = Number(path);
1250
1256
  path_len = Number(path_len);
1251
1257
  if (path === 0) {
@@ -1263,64 +1269,7 @@
1263
1269
  HEAPU8.set(buffer, path);
1264
1270
  return 0 /* WasiErrno.ESUCCESS */;
1265
1271
  });
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) {
1270
- return 28 /* WasiErrno.EINVAL */;
1271
- }
1272
- const { HEAPU8, view } = getMemory(this);
1273
- 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);
1284
- return 0 /* WasiErrno.ESUCCESS */;
1285
- });
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);
1293
- 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;
1318
- fileDescriptor.pos += BigInt(nread);
1319
- }
1320
- view.setUint32(size, nread, true);
1321
- return 0 /* WasiErrno.ESUCCESS */;
1322
- });
1323
- this.fd_seek = syscallWrap('fd_seek', function (fd, offset, whence, newOffset) {
1272
+ this.fd_seek = syscallWrap(this, 'fd_seek', function (fd, offset, whence, newOffset) {
1324
1273
  newOffset = Number(newOffset);
1325
1274
  if (newOffset === 0) {
1326
1275
  return 28 /* WasiErrno.EINVAL */;
@@ -1334,7 +1283,349 @@
1334
1283
  view.setBigUint64(newOffset, r, true);
1335
1284
  return 0 /* WasiErrno.ESUCCESS */;
1336
1285
  });
1337
- this.fd_readdir = syscallWrap('fd_readdir', function (fd, buf, buf_len, cookie, bufused) {
1286
+ this.fd_tell = syscallWrap(this, 'fd_tell', function (fd, offset) {
1287
+ const wasi = _wasi.get(this);
1288
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_TELL, BigInt(0));
1289
+ const pos = BigInt(fileDescriptor.pos);
1290
+ const { view } = getMemory(this);
1291
+ view.setBigUint64(Number(offset), pos, true);
1292
+ return 0 /* WasiErrno.ESUCCESS */;
1293
+ });
1294
+ this.poll_oneoff = syscallWrap(this, 'poll_oneoff', function (_in, _out, _sub, _nevents) {
1295
+ return 52 /* WasiErrno.ENOSYS */;
1296
+ });
1297
+ this.proc_exit = syscallWrap(this, 'proc_exit', function (_rval) {
1298
+ return 0 /* WasiErrno.ESUCCESS */;
1299
+ });
1300
+ this.proc_raise = syscallWrap(this, 'proc_raise', function (_sig) {
1301
+ return 52 /* WasiErrno.ENOSYS */;
1302
+ });
1303
+ this.sched_yield = syscallWrap(this, 'sched_yield', function () {
1304
+ return 0 /* WasiErrno.ESUCCESS */;
1305
+ });
1306
+ this.random_get = typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function'
1307
+ ? syscallWrap(this, 'random_get', function (buf, buf_len) {
1308
+ buf = Number(buf);
1309
+ if (buf === 0) {
1310
+ return 28 /* WasiErrno.EINVAL */;
1311
+ }
1312
+ buf_len = Number(buf_len);
1313
+ const { HEAPU8 } = getMemory(this);
1314
+ let pos;
1315
+ const stride = 65536;
1316
+ for (pos = 0; pos + stride < buf_len; pos += stride) {
1317
+ crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + pos + stride));
1318
+ }
1319
+ crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + buf_len));
1320
+ return 0 /* WasiErrno.ESUCCESS */;
1321
+ })
1322
+ : syscallWrap(this, 'random_get', function (buf, buf_len) {
1323
+ buf = Number(buf);
1324
+ if (buf === 0) {
1325
+ return 28 /* WasiErrno.EINVAL */;
1326
+ }
1327
+ buf_len = Number(buf_len);
1328
+ const { view } = getMemory(this);
1329
+ for (let i = buf; i < buf + buf_len; ++i) {
1330
+ view.setUint8(i, Math.floor(Math.random() * 256));
1331
+ }
1332
+ return 0 /* WasiErrno.ESUCCESS */;
1333
+ });
1334
+ this.sock_recv = syscallWrap(this, 'sock_recv', function () {
1335
+ return 58 /* WasiErrno.ENOTSUP */;
1336
+ });
1337
+ this.sock_send = syscallWrap(this, 'sock_send', function () {
1338
+ return 58 /* WasiErrno.ENOTSUP */;
1339
+ });
1340
+ this.sock_shutdown = syscallWrap(this, 'sock_shutdown', function () {
1341
+ return 58 /* WasiErrno.ENOTSUP */;
1342
+ });
1343
+ _wasi.set(this, {
1344
+ fds,
1345
+ args,
1346
+ env
1347
+ });
1348
+ if (fs)
1349
+ _fs.set(this, fs);
1350
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
1351
+ const _this = this;
1352
+ function defineImport(name, syncVersion, asyncVersion, parameterType, returnType) {
1353
+ if (asyncFs) {
1354
+ if (asyncify) {
1355
+ _this[name] = asyncify.wrapImportFunction(syscallWrap(_this, name, asyncVersion));
1356
+ }
1357
+ else {
1358
+ _this[name] = wrapAsyncImport(syscallWrap(_this, name, asyncVersion), parameterType, returnType);
1359
+ }
1360
+ }
1361
+ else {
1362
+ _this[name] = syscallWrap(_this, name, syncVersion);
1363
+ }
1364
+ }
1365
+ defineImport('fd_allocate', function fd_allocate(fd, offset, len) {
1366
+ const wasi = _wasi.get(this);
1367
+ const fs = getFs(this);
1368
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_ALLOCATE, BigInt(0));
1369
+ const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
1370
+ if (stat.size < offset + len) {
1371
+ fs.ftruncateSync(fileDescriptor.fd, Number(offset + len));
1372
+ }
1373
+ return 0 /* WasiErrno.ESUCCESS */;
1374
+ }, async function fd_allocate(fd, offset, len) {
1375
+ const wasi = _wasi.get(this);
1376
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_ALLOCATE, BigInt(0));
1377
+ const h = fileDescriptor.fd;
1378
+ const stat = await h.stat({ bigint: true });
1379
+ if (stat.size < offset + len) {
1380
+ await h.truncate(Number(offset + len));
1381
+ }
1382
+ return 0 /* WasiErrno.ESUCCESS */;
1383
+ }, ['i32', 'i64', 'f64'], ['i32']);
1384
+ defineImport('fd_close', function fd_close(fd) {
1385
+ const wasi = _wasi.get(this);
1386
+ const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
1387
+ const fs = getFs(this);
1388
+ fs.closeSync(fileDescriptor.fd);
1389
+ wasi.fds.remove(fd);
1390
+ return 0 /* WasiErrno.ESUCCESS */;
1391
+ }, async function fd_close(fd) {
1392
+ const wasi = _wasi.get(this);
1393
+ const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
1394
+ await fileDescriptor.fd.close();
1395
+ wasi.fds.remove(fd);
1396
+ return 0 /* WasiErrno.ESUCCESS */;
1397
+ }, ['i32'], ['i32']);
1398
+ defineImport('fd_datasync', function fd_datasync(fd) {
1399
+ const wasi = _wasi.get(this);
1400
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_DATASYNC, BigInt(0));
1401
+ const fs = getFs(this);
1402
+ fs.fdatasyncSync(fileDescriptor.fd);
1403
+ return 0 /* WasiErrno.ESUCCESS */;
1404
+ }, async function fd_datasync(fd) {
1405
+ const wasi = _wasi.get(this);
1406
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_DATASYNC, BigInt(0));
1407
+ await fileDescriptor.fd.datasync();
1408
+ return 0 /* WasiErrno.ESUCCESS */;
1409
+ }, ['i32'], ['i32']);
1410
+ defineImport('fd_filestat_get', function fd_filestat_get(fd, buf) {
1411
+ buf = Number(buf);
1412
+ if (buf === 0)
1413
+ return 28 /* WasiErrno.EINVAL */;
1414
+ const wasi = _wasi.get(this);
1415
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
1416
+ const fs = getFs(this);
1417
+ const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
1418
+ const { view } = getMemory(this);
1419
+ toFileStat(view, buf, stat);
1420
+ return 0 /* WasiErrno.ESUCCESS */;
1421
+ }, async 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 h = fileDescriptor.fd;
1428
+ const stat = await h.stat({ bigint: true });
1429
+ const { view } = getMemory(this);
1430
+ toFileStat(view, buf, stat);
1431
+ return 0 /* WasiErrno.ESUCCESS */;
1432
+ }, ['i32', 'i32'], ['i32']);
1433
+ defineImport('fd_filestat_set_size', function fd_filestat_set_size(fd, size) {
1434
+ const wasi = _wasi.get(this);
1435
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_SIZE, BigInt(0));
1436
+ const fs = getFs(this);
1437
+ fs.ftruncateSync(fileDescriptor.fd, Number(size));
1438
+ return 0 /* WasiErrno.ESUCCESS */;
1439
+ }, async function fd_filestat_set_size(fd, size) {
1440
+ const wasi = _wasi.get(this);
1441
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_SIZE, BigInt(0));
1442
+ const h = fileDescriptor.fd;
1443
+ await h.truncate(Number(size));
1444
+ return 0 /* WasiErrno.ESUCCESS */;
1445
+ }, ['i32', 'i64'], ['i32']);
1446
+ function fdFilestatGetTimes(fd, atim, mtim, flags) {
1447
+ const wasi = _wasi.get(this);
1448
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_TIMES, BigInt(0));
1449
+ if ((flags & 2 /* WasiFstFlag.SET_ATIM_NOW */) === 2 /* WasiFstFlag.SET_ATIM_NOW */) {
1450
+ atim = BigInt(Date.now() * 1000000);
1451
+ }
1452
+ if ((flags & 8 /* WasiFstFlag.SET_MTIM_NOW */) === 8 /* WasiFstFlag.SET_MTIM_NOW */) {
1453
+ mtim = BigInt(Date.now() * 1000000);
1454
+ }
1455
+ return { fileDescriptor, atim, mtim };
1456
+ }
1457
+ defineImport('fd_filestat_set_times', function fd_filestat_set_times(fd, atim, mtim, flags) {
1458
+ const { fileDescriptor, atim: atimRes, mtim: mtimRes } = fdFilestatGetTimes.call(this, fd, atim, mtim, flags);
1459
+ const fs = getFs(this);
1460
+ fs.futimesSync(fileDescriptor.fd, Number(atimRes), Number(mtimRes));
1461
+ return 0 /* WasiErrno.ESUCCESS */;
1462
+ }, async function fd_filestat_set_times(fd, atim, mtim, flags) {
1463
+ const { fileDescriptor, atim: atimRes, mtim: mtimRes } = fdFilestatGetTimes.call(this, fd, atim, mtim, flags);
1464
+ const h = fileDescriptor.fd;
1465
+ await h.utimes(Number(atimRes), Number(mtimRes));
1466
+ return 0 /* WasiErrno.ESUCCESS */;
1467
+ }, ['i32', 'i64', 'i64', 'i32'], ['i32']);
1468
+ defineImport('fd_pread', function fd_pread(fd, iovs, iovslen, offset, size) {
1469
+ iovs = Number(iovs);
1470
+ size = Number(size);
1471
+ if (iovs === 0 || size === 0) {
1472
+ return 28 /* WasiErrno.EINVAL */;
1473
+ }
1474
+ const { HEAPU8, view } = getMemory(this);
1475
+ const wasi = _wasi.get(this);
1476
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
1477
+ let totalSize = 0;
1478
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1479
+ const offset = iovs + (i * 8);
1480
+ const buf = view.getInt32(offset, true);
1481
+ const bufLen = view.getUint32(offset + 4, true);
1482
+ totalSize += bufLen;
1483
+ return HEAPU8.subarray(buf, buf + bufLen);
1484
+ });
1485
+ let nread = 0;
1486
+ const buffer = new Uint8Array(totalSize);
1487
+ buffer._isBuffer = true;
1488
+ const fs = getFs(this);
1489
+ const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
1490
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1491
+ view.setUint32(size, nread, true);
1492
+ return 0 /* WasiErrno.ESUCCESS */;
1493
+ }, async function (fd, iovs, iovslen, offset, size) {
1494
+ iovs = Number(iovs);
1495
+ size = Number(size);
1496
+ if (iovs === 0 || size === 0) {
1497
+ return 28 /* WasiErrno.EINVAL */;
1498
+ }
1499
+ const { HEAPU8, view } = getMemory(this);
1500
+ const wasi = _wasi.get(this);
1501
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
1502
+ let totalSize = 0;
1503
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1504
+ const offset = iovs + (i * 8);
1505
+ const buf = view.getInt32(offset, true);
1506
+ const bufLen = view.getUint32(offset + 4, true);
1507
+ totalSize += bufLen;
1508
+ return HEAPU8.subarray(buf, buf + bufLen);
1509
+ });
1510
+ let nread = 0;
1511
+ const buffer = new Uint8Array(totalSize);
1512
+ buffer._isBuffer = true;
1513
+ const { bytesRead } = await fileDescriptor.fd.read(buffer, 0, buffer.length, Number(offset));
1514
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1515
+ view.setUint32(size, nread, true);
1516
+ return 0 /* WasiErrno.ESUCCESS */;
1517
+ }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
1518
+ defineImport('fd_pwrite', function fd_pwrite(fd, iovs, iovslen, offset, size) {
1519
+ iovs = Number(iovs);
1520
+ size = Number(size);
1521
+ if (iovs === 0 || size === 0) {
1522
+ return 28 /* WasiErrno.EINVAL */;
1523
+ }
1524
+ const { HEAPU8, view } = getMemory(this);
1525
+ const wasi = _wasi.get(this);
1526
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
1527
+ const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
1528
+ const offset = iovs + (i * 8);
1529
+ const buf = view.getInt32(offset, true);
1530
+ const bufLen = view.getUint32(offset + 4, true);
1531
+ return HEAPU8.subarray(buf, buf + bufLen);
1532
+ }));
1533
+ const fs = getFs(this);
1534
+ const nwritten = fs.writeSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
1535
+ view.setUint32(size, nwritten, true);
1536
+ return 0 /* WasiErrno.ESUCCESS */;
1537
+ }, async function fd_pwrite(fd, iovs, iovslen, offset, size) {
1538
+ iovs = Number(iovs);
1539
+ size = Number(size);
1540
+ if (iovs === 0 || size === 0) {
1541
+ return 28 /* WasiErrno.EINVAL */;
1542
+ }
1543
+ const { HEAPU8, view } = getMemory(this);
1544
+ const wasi = _wasi.get(this);
1545
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
1546
+ const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
1547
+ const offset = iovs + (i * 8);
1548
+ const buf = view.getInt32(offset, true);
1549
+ const bufLen = view.getUint32(offset + 4, true);
1550
+ return HEAPU8.subarray(buf, buf + bufLen);
1551
+ }));
1552
+ const { bytesWritten } = await fileDescriptor.fd.write(buffer, 0, buffer.length, Number(offset));
1553
+ view.setUint32(size, bytesWritten, true);
1554
+ return 0 /* WasiErrno.ESUCCESS */;
1555
+ }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
1556
+ defineImport('fd_read', function fd_read(fd, iovs, iovslen, size) {
1557
+ iovs = Number(iovs);
1558
+ size = Number(size);
1559
+ if (iovs === 0 || size === 0) {
1560
+ return 28 /* WasiErrno.EINVAL */;
1561
+ }
1562
+ const { HEAPU8, view } = getMemory(this);
1563
+ const wasi = _wasi.get(this);
1564
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
1565
+ let totalSize = 0;
1566
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1567
+ const offset = iovs + (i * 8);
1568
+ const buf = view.getInt32(offset, true);
1569
+ const bufLen = view.getUint32(offset + 4, true);
1570
+ totalSize += bufLen;
1571
+ return HEAPU8.subarray(buf, buf + bufLen);
1572
+ });
1573
+ let buffer;
1574
+ let nread = 0;
1575
+ if (fd === 0) {
1576
+ if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
1577
+ return 58 /* WasiErrno.ENOTSUP */;
1578
+ }
1579
+ buffer = readStdin();
1580
+ nread = buffer ? copyMemory(ioVecs, buffer) : 0;
1581
+ }
1582
+ else {
1583
+ buffer = new Uint8Array(totalSize);
1584
+ buffer._isBuffer = true;
1585
+ const fs = getFs(this);
1586
+ const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(fileDescriptor.pos));
1587
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1588
+ fileDescriptor.pos += BigInt(nread);
1589
+ }
1590
+ view.setUint32(size, nread, true);
1591
+ return 0 /* WasiErrno.ESUCCESS */;
1592
+ }, async function fd_read(fd, iovs, iovslen, size) {
1593
+ iovs = Number(iovs);
1594
+ size = Number(size);
1595
+ if (iovs === 0 || size === 0) {
1596
+ return 28 /* WasiErrno.EINVAL */;
1597
+ }
1598
+ const { HEAPU8, view } = getMemory(this);
1599
+ const wasi = _wasi.get(this);
1600
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
1601
+ let totalSize = 0;
1602
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1603
+ const offset = iovs + (i * 8);
1604
+ const buf = view.getInt32(offset, true);
1605
+ const bufLen = view.getUint32(offset + 4, true);
1606
+ totalSize += bufLen;
1607
+ return HEAPU8.subarray(buf, buf + bufLen);
1608
+ });
1609
+ let buffer;
1610
+ let nread = 0;
1611
+ if (fd === 0) {
1612
+ if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
1613
+ return 58 /* WasiErrno.ENOTSUP */;
1614
+ }
1615
+ buffer = readStdin();
1616
+ nread = buffer ? copyMemory(ioVecs, buffer) : 0;
1617
+ }
1618
+ else {
1619
+ buffer = new Uint8Array(totalSize);
1620
+ buffer._isBuffer = true;
1621
+ const { bytesRead } = await fileDescriptor.fd.read(buffer, 0, buffer.length, Number(fileDescriptor.pos));
1622
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1623
+ fileDescriptor.pos += BigInt(nread);
1624
+ }
1625
+ view.setUint32(size, nread, true);
1626
+ return 0 /* WasiErrno.ESUCCESS */;
1627
+ }, ['i32', 'i32', 'i32', 'i32'], ['i32']);
1628
+ defineImport('fd_readdir', function fd_readdir(fd, buf, buf_len, cookie, bufused) {
1338
1629
  buf = Number(buf);
1339
1630
  buf_len = Number(buf_len);
1340
1631
  bufused = Number(bufused);
@@ -1384,28 +1675,79 @@
1384
1675
  }
1385
1676
  view.setUint32(bufused, bufferUsed, true);
1386
1677
  return 0 /* WasiErrno.ESUCCESS */;
1387
- });
1388
- this.fd_renumber = syscallWrap('fd_renumber', function (from, to) {
1678
+ }, async function fd_readdir(fd, buf, buf_len, cookie, bufused) {
1679
+ buf = Number(buf);
1680
+ buf_len = Number(buf_len);
1681
+ bufused = Number(bufused);
1682
+ if (buf === 0 || bufused === 0)
1683
+ return 0 /* WasiErrno.ESUCCESS */;
1684
+ const wasi = _wasi.get(this);
1685
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READDIR, BigInt(0));
1686
+ const fs = getFs(this);
1687
+ const entries = await fs.promises.readdir(fileDescriptor.realPath, { withFileTypes: true });
1688
+ const { HEAPU8, view } = getMemory(this);
1689
+ let bufferUsed = 0;
1690
+ for (let i = Number(cookie); i < entries.length; i++) {
1691
+ const nameData = encoder.encode(entries[i].name);
1692
+ const entryInfo = await fs.promises.stat(resolve(fileDescriptor.realPath, entries[i].name), { bigint: true });
1693
+ const entryData = new Uint8Array(24 + nameData.byteLength);
1694
+ const entryView = new DataView(entryData.buffer);
1695
+ entryView.setBigUint64(0, BigInt(i + 1), true);
1696
+ entryView.setBigUint64(8, BigInt(entryInfo.ino ? entryInfo.ino : 0), true);
1697
+ entryView.setUint32(16, nameData.byteLength, true);
1698
+ let type;
1699
+ if (entries[i].isFile()) {
1700
+ type = 4 /* WasiFileType.REGULAR_FILE */;
1701
+ }
1702
+ else if (entries[i].isDirectory()) {
1703
+ type = 3 /* WasiFileType.DIRECTORY */;
1704
+ }
1705
+ else if (entries[i].isSymbolicLink()) {
1706
+ type = 7 /* WasiFileType.SYMBOLIC_LINK */;
1707
+ }
1708
+ else if (entries[i].isCharacterDevice()) {
1709
+ type = 2 /* WasiFileType.CHARACTER_DEVICE */;
1710
+ }
1711
+ else if (entries[i].isBlockDevice()) {
1712
+ type = 1 /* WasiFileType.BLOCK_DEVICE */;
1713
+ }
1714
+ else if (entries[i].isSocket()) {
1715
+ type = 6 /* WasiFileType.SOCKET_STREAM */;
1716
+ }
1717
+ else {
1718
+ type = 0 /* WasiFileType.UNKNOWN */;
1719
+ }
1720
+ entryView.setUint8(20, type);
1721
+ entryData.set(nameData, 24);
1722
+ const data = entryData.slice(0, Math.min(entryData.length, buf_len - bufferUsed));
1723
+ HEAPU8.set(data, buf + bufferUsed);
1724
+ bufferUsed += data.byteLength;
1725
+ }
1726
+ view.setUint32(bufused, bufferUsed, true);
1727
+ return 0 /* WasiErrno.ESUCCESS */;
1728
+ }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
1729
+ defineImport('fd_renumber', function fd_renumber(from, to) {
1389
1730
  const wasi = _wasi.get(this);
1390
1731
  wasi.fds.renumber(to, from);
1391
1732
  return 0 /* WasiErrno.ESUCCESS */;
1392
- });
1393
- this.fd_sync = syscallWrap('fd_sync', function (fd) {
1733
+ }, async function fd_renumber(from, to) {
1734
+ const wasi = _wasi.get(this);
1735
+ await wasi.fds.renumber(to, from);
1736
+ return 0 /* WasiErrno.ESUCCESS */;
1737
+ }, ['i32', 'i32'], ['i32']);
1738
+ defineImport('fd_sync', function fd_sync(fd) {
1394
1739
  const wasi = _wasi.get(this);
1395
1740
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SYNC, BigInt(0));
1396
1741
  const fs = getFs(this);
1397
1742
  fs.fsyncSync(fileDescriptor.fd);
1398
1743
  return 0 /* WasiErrno.ESUCCESS */;
1399
- });
1400
- this.fd_tell = syscallWrap('fd_tell', function (fd, offset) {
1744
+ }, async function fd_sync(fd) {
1401
1745
  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);
1746
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SYNC, BigInt(0));
1747
+ await fileDescriptor.fd.sync();
1406
1748
  return 0 /* WasiErrno.ESUCCESS */;
1407
- });
1408
- this.fd_write = syscallWrap('fd_write', function (fd, iovs, iovslen, size) {
1749
+ }, ['i32'], ['i32']);
1750
+ defineImport('fd_write', function fd_write(fd, iovs, iovslen, size) {
1409
1751
  iovs = Number(iovs);
1410
1752
  size = Number(size);
1411
1753
  if (iovs === 0 || size === 0) {
@@ -1431,8 +1773,33 @@
1431
1773
  }
1432
1774
  view.setUint32(size, nwritten, true);
1433
1775
  return 0 /* WasiErrno.ESUCCESS */;
1434
- });
1435
- this.path_create_directory = syscallWrap('path_create_directory', function (fd, path, path_len) {
1776
+ }, async function fd_write(fd, iovs, iovslen, size) {
1777
+ iovs = Number(iovs);
1778
+ size = Number(size);
1779
+ if (iovs === 0 || size === 0) {
1780
+ return 28 /* WasiErrno.EINVAL */;
1781
+ }
1782
+ const { HEAPU8, view } = getMemory(this);
1783
+ const wasi = _wasi.get(this);
1784
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
1785
+ const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
1786
+ const offset = iovs + (i * 8);
1787
+ const buf = view.getInt32(offset, true);
1788
+ const bufLen = view.getUint32(offset + 4, true);
1789
+ return HEAPU8.subarray(buf, buf + bufLen);
1790
+ }));
1791
+ let nwritten;
1792
+ if (fd === 1 || fd === 2) {
1793
+ nwritten = fileDescriptor.write(buffer);
1794
+ }
1795
+ else {
1796
+ nwritten = await (await (fileDescriptor.fd.write(buffer, 0, buffer.length, Number(fileDescriptor.pos)))).bytesWritten;
1797
+ fileDescriptor.pos += BigInt(nwritten);
1798
+ }
1799
+ view.setUint32(size, nwritten, true);
1800
+ return 0 /* WasiErrno.ESUCCESS */;
1801
+ }, ['i32', 'i32', 'i32', 'i32'], ['i32']);
1802
+ defineImport('path_create_directory', function path_create_directory(fd, path, path_len) {
1436
1803
  path = Number(path);
1437
1804
  path_len = Number(path_len);
1438
1805
  if (path === 0) {
@@ -1446,8 +1813,22 @@
1446
1813
  const fs = getFs(this);
1447
1814
  fs.mkdirSync(pathString);
1448
1815
  return 0 /* WasiErrno.ESUCCESS */;
1449
- });
1450
- this.path_filestat_get = syscallWrap('path_filestat_get', function (fd, flags, path, path_len, filestat) {
1816
+ }, async function path_create_directory(fd, path, path_len) {
1817
+ path = Number(path);
1818
+ path_len = Number(path_len);
1819
+ if (path === 0) {
1820
+ return 28 /* WasiErrno.EINVAL */;
1821
+ }
1822
+ const { HEAPU8 } = getMemory(this);
1823
+ const wasi = _wasi.get(this);
1824
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
1825
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1826
+ pathString = resolve(fileDescriptor.realPath, pathString);
1827
+ const fs = getFs(this);
1828
+ await fs.promises.mkdir(pathString);
1829
+ return 0 /* WasiErrno.ESUCCESS */;
1830
+ }, ['i32', 'i32', 'i32'], ['i32']);
1831
+ defineImport('path_filestat_get', function path_filestat_get(fd, flags, path, path_len, filestat) {
1451
1832
  path = Number(path);
1452
1833
  path_len = Number(path_len);
1453
1834
  filestat = Number(filestat);
@@ -1469,8 +1850,30 @@
1469
1850
  }
1470
1851
  toFileStat(view, filestat, stat);
1471
1852
  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) {
1853
+ }, async function path_filestat_get(fd, flags, path, path_len, filestat) {
1854
+ path = Number(path);
1855
+ path_len = Number(path_len);
1856
+ filestat = Number(filestat);
1857
+ if (path === 0 || filestat === 0) {
1858
+ return 28 /* WasiErrno.EINVAL */;
1859
+ }
1860
+ const { HEAPU8, view } = getMemory(this);
1861
+ const wasi = _wasi.get(this);
1862
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
1863
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1864
+ const fs = getFs(this);
1865
+ pathString = resolve(fileDescriptor.realPath, pathString);
1866
+ let stat;
1867
+ if ((flags & 1) === 1) {
1868
+ stat = await fs.promises.stat(pathString, { bigint: true });
1869
+ }
1870
+ else {
1871
+ stat = await fs.promises.lstat(pathString, { bigint: true });
1872
+ }
1873
+ toFileStat(view, filestat, stat);
1874
+ return 0 /* WasiErrno.ESUCCESS */;
1875
+ }, ['i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1876
+ defineImport('path_filestat_set_times', function path_filestat_set_times(fd, flags, path, path_len, atim, mtim, fst_flags) {
1474
1877
  path = Number(path);
1475
1878
  path_len = Number(path_len);
1476
1879
  if (path === 0)
@@ -1485,7 +1888,7 @@
1485
1888
  const wasi = _wasi.get(this);
1486
1889
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1487
1890
  const fs = getFs(this);
1488
- const resolvedPath = resolvePath(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1891
+ const resolvedPath = resolvePathSync(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1489
1892
  if ((fst_flags & 2 /* WasiFstFlag.SET_ATIM_NOW */) === 2 /* WasiFstFlag.SET_ATIM_NOW */) {
1490
1893
  atim = BigInt(Date.now() * 1000000);
1491
1894
  }
@@ -1494,8 +1897,32 @@
1494
1897
  }
1495
1898
  fs.utimesSync(resolvedPath, Number(atim), Number(mtim));
1496
1899
  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) {
1900
+ }, async function path_filestat_set_times(fd, flags, path, path_len, atim, mtim, fst_flags) {
1901
+ path = Number(path);
1902
+ path_len = Number(path_len);
1903
+ if (path === 0)
1904
+ return 28 /* WasiErrno.EINVAL */;
1905
+ if ((fst_flags) & ~(1 /* WasiFstFlag.SET_ATIM */ |
1906
+ 2 /* WasiFstFlag.SET_ATIM_NOW */ |
1907
+ 4 /* WasiFstFlag.SET_MTIM */ |
1908
+ 8 /* WasiFstFlag.SET_MTIM_NOW */)) {
1909
+ return 28 /* WasiErrno.EINVAL */;
1910
+ }
1911
+ const { HEAPU8 } = getMemory(this);
1912
+ const wasi = _wasi.get(this);
1913
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1914
+ const fs = getFs(this);
1915
+ const resolvedPath = await resolvePathAsync(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1916
+ if ((fst_flags & 2 /* WasiFstFlag.SET_ATIM_NOW */) === 2 /* WasiFstFlag.SET_ATIM_NOW */) {
1917
+ atim = BigInt(Date.now() * 1000000);
1918
+ }
1919
+ if ((fst_flags & 8 /* WasiFstFlag.SET_MTIM_NOW */) === 8 /* WasiFstFlag.SET_MTIM_NOW */) {
1920
+ mtim = BigInt(Date.now() * 1000000);
1921
+ }
1922
+ await fs.promises.utimes(resolvedPath, Number(atim), Number(mtim));
1923
+ return 0 /* WasiErrno.ESUCCESS */;
1924
+ }, ['i32', 'i32', 'i32', 'i32', 'i64', 'i64', 'i32'], ['i32']);
1925
+ defineImport('path_link', function path_link(old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
1499
1926
  old_path = Number(old_path);
1500
1927
  old_path_len = Number(old_path_len);
1501
1928
  new_path = Number(new_path);
@@ -1515,20 +1942,36 @@
1515
1942
  }
1516
1943
  const { HEAPU8 } = getMemory(this);
1517
1944
  const fs = getFs(this);
1518
- const resolvedOldPath = resolvePath(fs, oldWrap, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)), old_flags);
1945
+ const resolvedOldPath = resolvePathSync(fs, oldWrap, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)), old_flags);
1519
1946
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
1520
1947
  fs.linkSync(resolvedOldPath, resolvedNewPath);
1521
1948
  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) {
1949
+ }, async function path_link(old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
1950
+ old_path = Number(old_path);
1951
+ old_path_len = Number(old_path_len);
1952
+ new_path = Number(new_path);
1953
+ new_path_len = Number(new_path_len);
1954
+ if (old_path === 0 || new_path === 0) {
1527
1955
  return 28 /* WasiErrno.EINVAL */;
1528
1956
  }
1529
- path_len = Number(path_len);
1530
- fs_rights_base = BigInt(fs_rights_base);
1531
- fs_rights_base = BigInt(fs_rights_base);
1957
+ const wasi = _wasi.get(this);
1958
+ let oldWrap;
1959
+ let newWrap;
1960
+ if (old_fd === new_fd) {
1961
+ oldWrap = newWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE | WasiRights.PATH_LINK_TARGET, BigInt(0));
1962
+ }
1963
+ else {
1964
+ oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE, BigInt(0));
1965
+ newWrap = wasi.fds.get(new_fd, WasiRights.PATH_LINK_TARGET, BigInt(0));
1966
+ }
1967
+ const { HEAPU8 } = getMemory(this);
1968
+ const fs = getFs(this);
1969
+ const resolvedOldPath = await resolvePathAsync(fs, oldWrap, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)), old_flags);
1970
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
1971
+ await fs.promises.link(resolvedOldPath, resolvedNewPath);
1972
+ return 0 /* WasiErrno.ESUCCESS */;
1973
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1974
+ function pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags) {
1532
1975
  const read = (fs_rights_base & (WasiRights.FD_READ |
1533
1976
  WasiRights.FD_READDIR)) !== BigInt(0);
1534
1977
  const write = (fs_rights_base & (WasiRights.FD_DATASYNC |
@@ -1573,32 +2016,78 @@
1573
2016
  if (write && (flags & (1024 /* FileControlFlag.O_APPEND */ | 512 /* FileControlFlag.O_TRUNC */)) === 0) {
1574
2017
  needed_inheriting |= WasiRights.FD_SEEK;
1575
2018
  }
2019
+ return { flags, needed_base, needed_inheriting };
2020
+ }
2021
+ defineImport('path_open', function path_open(dirfd, dirflags, path, path_len, o_flags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) {
2022
+ path = Number(path);
2023
+ fd = Number(fd);
2024
+ if (path === 0 || fd === 0) {
2025
+ return 28 /* WasiErrno.EINVAL */;
2026
+ }
2027
+ path_len = Number(path_len);
2028
+ fs_rights_base = BigInt(fs_rights_base);
2029
+ fs_rights_inheriting = BigInt(fs_rights_inheriting);
2030
+ const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
1576
2031
  const wasi = _wasi.get(this);
1577
- const fileDescriptor = wasi.fds.get(dirfd, needed_base, needed_inheriting);
2032
+ const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1578
2033
  const memory = getMemory(this);
1579
2034
  const HEAPU8 = memory.HEAPU8;
1580
2035
  const pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1581
2036
  const fs = getFs(this);
1582
- const resolved_path = resolvePath(fs, fileDescriptor, pathString, dirflags);
1583
- const r = fs.openSync(resolved_path, flags, 0o666);
2037
+ const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
2038
+ const r = fs.openSync(resolved_path, flagsRes, 0o666);
1584
2039
  const filetype = wasi.fds.getFileTypeByFd(r);
1585
2040
  if ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 && filetype !== 3 /* WasiFileType.DIRECTORY */) {
1586
2041
  return 54 /* WasiErrno.ENOTDIR */;
1587
2042
  }
1588
- const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r, flags, filetype);
2043
+ const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r, flagsRes, filetype);
1589
2044
  const wrap = wasi.fds.insert(r, resolved_path, resolved_path, filetype, fs_rights_base & max_base, fs_rights_inheriting & max_inheriting, 0);
1590
2045
  const stat = fs.fstatSync(r, { bigint: true });
1591
2046
  if (stat.isFile()) {
1592
2047
  wrap.size = stat.size;
1593
- if ((flags & 1024 /* FileControlFlag.O_APPEND */) !== 0) {
2048
+ if ((flagsRes & 1024 /* FileControlFlag.O_APPEND */) !== 0) {
1594
2049
  wrap.pos = stat.size;
1595
2050
  }
1596
2051
  }
1597
2052
  const view = memory.view;
1598
2053
  view.setInt32(fd, wrap.id, true);
1599
2054
  return 0 /* WasiErrno.ESUCCESS */;
1600
- });
1601
- this.path_readlink = syscallWrap('path_readlink', function (fd, path, path_len, buf, buf_len, bufused) {
2055
+ }, async function path_open(dirfd, dirflags, path, path_len, o_flags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) {
2056
+ path = Number(path);
2057
+ fd = Number(fd);
2058
+ if (path === 0 || fd === 0) {
2059
+ return 28 /* WasiErrno.EINVAL */;
2060
+ }
2061
+ path_len = Number(path_len);
2062
+ fs_rights_base = BigInt(fs_rights_base);
2063
+ fs_rights_inheriting = BigInt(fs_rights_inheriting);
2064
+ const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
2065
+ const wasi = _wasi.get(this);
2066
+ const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
2067
+ const memory = getMemory(this);
2068
+ const HEAPU8 = memory.HEAPU8;
2069
+ const pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
2070
+ const fs = getFs(this);
2071
+ const resolved_path = await resolvePathAsync(fs, fileDescriptor, pathString, dirflags);
2072
+ const r = await fs.promises.open(resolved_path, flagsRes, 0o666);
2073
+ const filetype = await wasi.fds.getFileTypeByFd(r);
2074
+ if ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 && filetype !== 3 /* WasiFileType.DIRECTORY */) {
2075
+ return 54 /* WasiErrno.ENOTDIR */;
2076
+ }
2077
+ const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r.fd, flagsRes, filetype);
2078
+ const wrap = wasi.fds.insert(r, resolved_path, resolved_path, filetype, fs_rights_base & max_base, fs_rights_inheriting & max_inheriting, 0);
2079
+ const stat = await r.stat({ bigint: true });
2080
+ if (stat.isFile()) {
2081
+ wrap.size = stat.size;
2082
+ if ((flagsRes & 1024 /* FileControlFlag.O_APPEND */) !== 0) {
2083
+ wrap.pos = stat.size;
2084
+ }
2085
+ }
2086
+ const view = memory.view;
2087
+ view.setInt32(fd, wrap.id, true);
2088
+ return 0 /* WasiErrno.ESUCCESS */;
2089
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i64', 'i64', 'i32', 'i32'], ['i32']);
2090
+ defineImport('path_readlink', function path_readlink(fd, path, path_len, buf, buf_len, bufused) {
1602
2091
  path = Number(path);
1603
2092
  path_len = Number(path_len);
1604
2093
  buf = Number(buf);
@@ -1622,8 +2111,32 @@
1622
2111
  HEAPU8[buf + len] = 0;
1623
2112
  view.setUint32(bufused, len + 1, true);
1624
2113
  return 0 /* WasiErrno.ESUCCESS */;
1625
- });
1626
- this.path_remove_directory = syscallWrap('path_remove_directory', function (fd, path, path_len) {
2114
+ }, async function path_readlink(fd, path, path_len, buf, buf_len, bufused) {
2115
+ path = Number(path);
2116
+ path_len = Number(path_len);
2117
+ buf = Number(buf);
2118
+ buf_len = Number(buf_len);
2119
+ bufused = Number(bufused);
2120
+ if (path === 0 || buf === 0 || bufused === 0) {
2121
+ return 28 /* WasiErrno.EINVAL */;
2122
+ }
2123
+ const { HEAPU8, view } = getMemory(this);
2124
+ const wasi = _wasi.get(this);
2125
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
2126
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
2127
+ pathString = resolve(fileDescriptor.realPath, pathString);
2128
+ const fs = getFs(this);
2129
+ const link = await fs.promises.readlink(pathString);
2130
+ const linkData = encoder.encode(link);
2131
+ const len = Math.min(linkData.length, buf_len);
2132
+ if (len >= buf_len)
2133
+ return 42 /* WasiErrno.ENOBUFS */;
2134
+ HEAPU8.set(linkData.subarray(0, len), buf);
2135
+ HEAPU8[buf + len] = 0;
2136
+ view.setUint32(bufused, len + 1, true);
2137
+ return 0 /* WasiErrno.ESUCCESS */;
2138
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
2139
+ defineImport('path_remove_directory', function path_remove_directory(fd, path, path_len) {
1627
2140
  path = Number(path);
1628
2141
  path_len = Number(path_len);
1629
2142
  if (path === 0) {
@@ -1637,8 +2150,22 @@
1637
2150
  const fs = getFs(this);
1638
2151
  fs.rmdirSync(pathString);
1639
2152
  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) {
2153
+ }, async function path_remove_directory(fd, path, path_len) {
2154
+ path = Number(path);
2155
+ path_len = Number(path_len);
2156
+ if (path === 0) {
2157
+ return 28 /* WasiErrno.EINVAL */;
2158
+ }
2159
+ const { HEAPU8 } = getMemory(this);
2160
+ const wasi = _wasi.get(this);
2161
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
2162
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
2163
+ pathString = resolve(fileDescriptor.realPath, pathString);
2164
+ const fs = getFs(this);
2165
+ await fs.promises.rmdir(pathString);
2166
+ return 0 /* WasiErrno.ESUCCESS */;
2167
+ }, ['i32', 'i32', 'i32'], ['i32']);
2168
+ defineImport('path_rename', function path_rename(old_fd, old_path, old_path_len, new_fd, new_path, new_path_len) {
1642
2169
  old_path = Number(old_path);
1643
2170
  old_path_len = Number(old_path_len);
1644
2171
  new_path = Number(new_path);
@@ -1662,8 +2189,32 @@
1662
2189
  const fs = getFs(this);
1663
2190
  fs.renameSync(resolvedOldPath, resolvedNewPath);
1664
2191
  return 0 /* WasiErrno.ESUCCESS */;
1665
- });
1666
- this.path_symlink = syscallWrap('path_symlink', function (old_path, old_path_len, fd, new_path, new_path_len) {
2192
+ }, async function path_rename(old_fd, old_path, old_path_len, new_fd, new_path, new_path_len) {
2193
+ old_path = Number(old_path);
2194
+ old_path_len = Number(old_path_len);
2195
+ new_path = Number(new_path);
2196
+ new_path_len = Number(new_path_len);
2197
+ if (old_path === 0 || new_path === 0) {
2198
+ return 28 /* WasiErrno.EINVAL */;
2199
+ }
2200
+ const wasi = _wasi.get(this);
2201
+ let oldWrap;
2202
+ let newWrap;
2203
+ if (old_fd === new_fd) {
2204
+ oldWrap = newWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE | WasiRights.PATH_RENAME_TARGET, BigInt(0));
2205
+ }
2206
+ else {
2207
+ oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE, BigInt(0));
2208
+ newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
2209
+ }
2210
+ const { HEAPU8 } = getMemory(this);
2211
+ const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)));
2212
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
2213
+ const fs = getFs(this);
2214
+ await fs.promises.rename(resolvedOldPath, resolvedNewPath);
2215
+ return 0 /* WasiErrno.ESUCCESS */;
2216
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
2217
+ defineImport('path_symlink', function path_symlink(old_path, old_path_len, fd, new_path, new_path_len) {
1667
2218
  old_path = Number(old_path);
1668
2219
  old_path_len = Number(old_path_len);
1669
2220
  new_path = Number(new_path);
@@ -1680,8 +2231,25 @@
1680
2231
  const fs = getFs(this);
1681
2232
  fs.symlinkSync(oldPath, newPath);
1682
2233
  return 0 /* WasiErrno.ESUCCESS */;
1683
- });
1684
- this.path_unlink_file = syscallWrap('path_unlink_file', function (fd, path, path_len) {
2234
+ }, async function path_symlink(old_path, old_path_len, fd, new_path, new_path_len) {
2235
+ old_path = Number(old_path);
2236
+ old_path_len = Number(old_path_len);
2237
+ new_path = Number(new_path);
2238
+ new_path_len = Number(new_path_len);
2239
+ if (old_path === 0 || new_path === 0) {
2240
+ return 28 /* WasiErrno.EINVAL */;
2241
+ }
2242
+ const { HEAPU8 } = getMemory(this);
2243
+ const wasi = _wasi.get(this);
2244
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
2245
+ const oldPath = decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len));
2246
+ let newPath = decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len));
2247
+ newPath = resolve(fileDescriptor.realPath, newPath);
2248
+ const fs = getFs(this);
2249
+ await fs.promises.symlink(oldPath, newPath);
2250
+ return 0 /* WasiErrno.ESUCCESS */;
2251
+ }, ['i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
2252
+ defineImport('path_unlink_file', function path_unlink_file(fd, path, path_len) {
1685
2253
  path = Number(path);
1686
2254
  path_len = Number(path_len);
1687
2255
  if (path === 0) {
@@ -1695,58 +2263,30 @@
1695
2263
  const fs = getFs(this);
1696
2264
  fs.unlinkSync(pathString);
1697
2265
  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 () {
2266
+ }, async function path_unlink_file(fd, path, path_len) {
2267
+ path = Number(path);
2268
+ path_len = Number(path_len);
2269
+ if (path === 0) {
2270
+ return 28 /* WasiErrno.EINVAL */;
2271
+ }
2272
+ const { HEAPU8 } = getMemory(this);
2273
+ const wasi = _wasi.get(this);
2274
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
2275
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
2276
+ pathString = resolve(fileDescriptor.realPath, pathString);
2277
+ const fs = getFs(this);
2278
+ await fs.promises.unlink(pathString);
1709
2279
  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({
2280
+ }, ['i32', 'i32', 'i32'], ['i32']);
2281
+ this._setMemory = function setMemory(m) {
2282
+ if (!(m instanceof _WebAssembly.Memory)) {
2283
+ throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
2284
+ }
2285
+ _memory.set(_this, extendMemory(m));
2286
+ };
2287
+ }
2288
+ static createSync(args, env, preopens, stdio, fs, print, printErr) {
2289
+ const fds = new SyncTable({
1750
2290
  size: 3,
1751
2291
  in: stdio[0],
1752
2292
  out: stdio[1],
@@ -1755,13 +2295,7 @@
1755
2295
  print,
1756
2296
  printErr
1757
2297
  });
1758
- _wasi.set(this, {
1759
- fds,
1760
- args,
1761
- env
1762
- });
1763
- if (fs)
1764
- _fs.set(this, fs);
2298
+ const _this = new WASI$1(args, env, fds, false, fs);
1765
2299
  if (preopens.length > 0) {
1766
2300
  for (let i = 0; i < preopens.length; ++i) {
1767
2301
  const realPath = fs.realpathSync(preopens[i].realPath, 'utf8');
@@ -1769,6 +2303,27 @@
1769
2303
  fds.insertPreopen(fd, preopens[i].mappedPath, realPath);
1770
2304
  }
1771
2305
  }
2306
+ return _this;
2307
+ }
2308
+ static async createAsync(args, env, preopens, stdio, fs, print, printErr, asyncify) {
2309
+ const fds = new AsyncTable({
2310
+ size: 3,
2311
+ in: stdio[0],
2312
+ out: stdio[1],
2313
+ err: stdio[2],
2314
+ print,
2315
+ printErr
2316
+ });
2317
+ const _this = new WASI$1(args, env, fds, true, fs, asyncify);
2318
+ if (preopens.length > 0) {
2319
+ for (let i = 0; i < preopens.length; ++i) {
2320
+ const entry = preopens[i];
2321
+ const realPath = await fs.promises.realpath(entry.realPath);
2322
+ const fd = await fs.promises.open(realPath, 'r', 0o666);
2323
+ await fds.insertPreopen(fd, entry.mappedPath, realPath);
2324
+ }
2325
+ }
2326
+ return _this;
1772
2327
  }
1773
2328
  }
1774
2329
 
@@ -1785,74 +2340,105 @@
1785
2340
  }
1786
2341
  // /** @public */
1787
2342
  // 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');
2343
+ function validateOptions(options) {
2344
+ var _a;
2345
+ validateObject(options, 'options');
2346
+ if (options.args !== undefined) {
2347
+ validateArray(options.args, 'options.args');
2348
+ }
2349
+ const args = ((_a = options.args) !== null && _a !== void 0 ? _a : []).map(String);
2350
+ const env = [];
2351
+ if (options.env !== undefined) {
2352
+ validateObject(options.env, 'options.env');
2353
+ Object.entries(options.env).forEach(({ 0: key, 1: value }) => {
2354
+ if (value !== undefined) {
2355
+ env.push(`${key}=${value}`);
1827
2356
  }
2357
+ });
2358
+ }
2359
+ const preopens = [];
2360
+ if (options.preopens !== undefined) {
2361
+ validateObject(options.preopens, 'options.preopens');
2362
+ Object.entries(options.preopens).forEach(({ 0: key, 1: value }) => preopens.push({ mappedPath: String(key), realPath: String(value) }));
2363
+ }
2364
+ if (preopens.length > 0) {
2365
+ if (options.fs === undefined) {
2366
+ throw new Error('filesystem is disabled, can not preopen directory');
1828
2367
  }
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
- }
2368
+ try {
2369
+ validateObject(options.fs, 'options.fs');
1848
2370
  }
1849
- this[kSetMemory] = wrap._setMemory;
1850
- delete wrap._setMemory;
2371
+ catch (_) {
2372
+ throw new TypeError('Node.js fs like implementation is not provided');
2373
+ }
2374
+ }
2375
+ // if (options.filesystem !== undefined) {
2376
+ // validateObject(options.filesystem, 'options.filesystem')
2377
+ // validateString(options.filesystem.type, 'options.filesystem.type')
2378
+ // if (options.filesystem.type !== 'memfs' && options.filesystem.type !== 'file-system-access-api') {
2379
+ // 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`)
2380
+ // }
2381
+ // try {
2382
+ // validateObject(options.filesystem.fs, 'options.filesystem.fs')
2383
+ // } catch (_) {
2384
+ // throw new Error('Node.js fs like implementation is not provided')
2385
+ // }
2386
+ // }
2387
+ if (options.print !== undefined)
2388
+ validateFunction(options.print, 'options.print');
2389
+ if (options.printErr !== undefined)
2390
+ validateFunction(options.printErr, 'options.printErr');
2391
+ if (options.returnOnExit !== undefined) {
2392
+ validateBoolean(options.returnOnExit, 'options.returnOnExit');
2393
+ }
2394
+ // const { stdin = 0, stdout = 1, stderr = 2 } = options
2395
+ // validateInt32(stdin, 'options.stdin', 0)
2396
+ // validateInt32(stdout, 'options.stdout', 0)
2397
+ // validateInt32(stderr, 'options.stderr', 0)
2398
+ // const stdio = [stdin, stdout, stderr] as const
2399
+ const stdio = [0, 1, 2];
2400
+ return {
2401
+ args,
2402
+ env,
2403
+ preopens,
2404
+ stdio
2405
+ };
2406
+ }
2407
+ /** @public */
2408
+ class WASI {
2409
+ constructor(setMemory, wrap) {
2410
+ this[kSetMemory] = setMemory;
1851
2411
  this.wasiImport = wrap;
1852
2412
  this[kStarted] = false;
1853
2413
  this[kExitCode] = 0;
1854
2414
  this[kInstance] = undefined;
1855
2415
  }
2416
+ static createSync(options = kEmptyObject) {
2417
+ const { args, env, preopens, stdio } = validateOptions(options);
2418
+ const wrap = WASI$1.createSync(args, env, preopens, stdio, options.fs, options.print, options.printErr);
2419
+ const setMemory = wrap._setMemory;
2420
+ delete wrap._setMemory;
2421
+ const _this = new WASI(setMemory, wrap);
2422
+ if (options.returnOnExit) {
2423
+ wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
2424
+ }
2425
+ return _this;
2426
+ }
2427
+ static async createAsync(options = kEmptyObject) {
2428
+ const { args, env, preopens, stdio } = validateOptions(options);
2429
+ if (options.asyncify !== undefined) {
2430
+ validateObject(options.asyncify, 'options.asyncify');
2431
+ validateFunction(options.asyncify.wrapImportFunction, 'options.asyncify.wrapImportFunction');
2432
+ }
2433
+ const wrap = await WASI$1.createAsync(args, env, preopens, stdio, options.fs, options.print, options.printErr, options.asyncify);
2434
+ const setMemory = wrap._setMemory;
2435
+ delete wrap._setMemory;
2436
+ const _this = new WASI(setMemory, wrap);
2437
+ if (options.returnOnExit) {
2438
+ wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
2439
+ }
2440
+ return _this;
2441
+ }
1856
2442
  // Must not export _initialize, must export _start
1857
2443
  start(instance) {
1858
2444
  if (this[kStarted]) {
@@ -1903,48 +2489,12 @@
1903
2489
  throw kExitCode;
1904
2490
  }
1905
2491
 
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
2492
  exports.Asyncify = Asyncify;
1945
2493
  exports.Memory = Memory;
1946
2494
  exports.WASI = WASI;
1947
2495
  exports.WebAssemblyMemory = WebAssemblyMemory;
2496
+ exports.asyncifyLoad = asyncifyLoad;
2497
+ exports.asyncifyLoadSync = asyncifyLoadSync;
1948
2498
  exports.extendMemory = extendMemory;
1949
2499
  exports.load = load;
1950
2500
  exports.loadSync = loadSync;