@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.
@@ -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
  }
@@ -206,6 +210,11 @@ class Asyncify {
206
210
  // }
207
211
  // Object.defineProperty(Instance.prototype, 'exports', { enumerable: true })
208
212
 
213
+ function validateImports(imports) {
214
+ if (imports && typeof imports !== 'object') {
215
+ throw new TypeError('imports must be an object or undefined');
216
+ }
217
+ }
209
218
  async function fetchWasm(urlOrBuffer, imports) {
210
219
  if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
211
220
  return await _WebAssembly.instantiate(urlOrBuffer, imports);
@@ -216,24 +225,12 @@ async function fetchWasm(urlOrBuffer, imports) {
216
225
  return source;
217
226
  }
218
227
  /** @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
- }
228
+ async function load(urlOrBuffer, imports) {
229
+ validateImports(imports);
224
230
  imports = imports !== null && imports !== void 0 ? imports : {};
225
- let asyncifyHelper;
226
231
  let source;
227
- if (asyncify) {
228
- asyncifyHelper = new Asyncify();
229
- imports = asyncifyHelper.wrapImports(imports);
230
- }
231
232
  if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
232
233
  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
234
  return source;
238
235
  }
239
236
  if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
@@ -250,35 +247,41 @@ async function load(urlOrBuffer, imports, asyncify) {
250
247
  else {
251
248
  source = await fetchWasm(urlOrBuffer, imports);
252
249
  }
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
250
  return source;
258
251
  }
259
252
  /** @public */
260
- function loadSync(buffer, imports, asyncify) {
253
+ async function asyncifyLoad(asyncify, urlOrBuffer, imports) {
261
254
  var _a;
255
+ validateImports(imports);
256
+ imports = imports !== null && imports !== void 0 ? imports : {};
257
+ const asyncifyHelper = new Asyncify();
258
+ imports = asyncifyHelper.wrapImports(imports);
259
+ const source = await load(urlOrBuffer, imports);
260
+ const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
261
+ return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
262
+ }
263
+ /** @public */
264
+ function loadSync(buffer, imports) {
262
265
  if ((buffer instanceof ArrayBuffer) && !ArrayBuffer.isView(buffer)) {
263
266
  throw new TypeError('Invalid source');
264
267
  }
265
- if (imports && typeof imports !== 'object') {
266
- throw new TypeError('imports must be an object or undefined');
267
- }
268
+ validateImports(imports);
268
269
  imports = imports !== null && imports !== void 0 ? imports : {};
269
- let asyncifyHelper;
270
- if (asyncify) {
271
- asyncifyHelper = new Asyncify();
272
- imports = asyncifyHelper.wrapImports(imports);
273
- }
274
270
  const module = new _WebAssembly.Module(buffer);
275
271
  const instance = new _WebAssembly.Instance(module, imports);
276
272
  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
273
  return source;
274
+ }
275
+ /** @public */
276
+ function asyncifyLoadSync(asyncify, buffer, imports) {
277
+ var _a;
278
+ validateImports(imports);
279
+ imports = imports !== null && imports !== void 0 ? imports : {};
280
+ const asyncifyHelper = new Asyncify();
281
+ imports = asyncifyHelper.wrapImports(imports);
282
+ const source = loadSync(buffer, imports);
283
+ const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
284
+ return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
282
285
  }
283
286
 
284
287
  const CHAR_DOT = 46; /* . */
@@ -754,7 +757,6 @@ class FileDescriptorTable {
754
757
  this.size = options.size;
755
758
  this.fds = Array(options.size);
756
759
  this.stdio = [options.in, options.out, options.err];
757
- this.fs = options.fs;
758
760
  this.print = options.print;
759
761
  this.printErr = options.printErr;
760
762
  this.insertStdio(options.in, 0, '<stdin>');
@@ -801,18 +803,6 @@ class FileDescriptorTable {
801
803
  this.used++;
802
804
  return entry;
803
805
  }
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
806
  get(id, base, inheriting) {
817
807
  if (id >= this.size) {
818
808
  throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
@@ -838,6 +828,24 @@ class FileDescriptorTable {
838
828
  this.fds[id] = undefined;
839
829
  this.used--;
840
830
  }
831
+ }
832
+ class SyncTable extends FileDescriptorTable {
833
+ constructor(options) {
834
+ super(options);
835
+ this.fs = options.fs;
836
+ }
837
+ getFileTypeByFd(fd) {
838
+ const stats = this.fs.fstatSync(fd, { bigint: true });
839
+ return toFileType(stats);
840
+ }
841
+ insertPreopen(fd, mappedPath, realPath) {
842
+ const type = this.getFileTypeByFd(fd);
843
+ if (type !== 3 /* WasiFileType.DIRECTORY */) {
844
+ throw new WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, 54 /* WasiErrno.ENOTDIR */);
845
+ }
846
+ const result = getRights(this.stdio, fd, 0, type);
847
+ return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
848
+ }
841
849
  renumber(dst, src) {
842
850
  if (dst === src)
843
851
  return;
@@ -855,6 +863,41 @@ class FileDescriptorTable {
855
863
  this.fds[src] = undefined;
856
864
  this.used--;
857
865
  }
866
+ }
867
+ class AsyncTable extends FileDescriptorTable {
868
+ // eslint-disable-next-line @typescript-eslint/no-useless-constructor
869
+ constructor(options) {
870
+ super(options);
871
+ }
872
+ async getFileTypeByFd(fd) {
873
+ const stats = await fd.stat({ bigint: true });
874
+ return toFileType(stats);
875
+ }
876
+ async insertPreopen(fd, mappedPath, realPath) {
877
+ const type = await this.getFileTypeByFd(fd);
878
+ if (type !== 3 /* WasiFileType.DIRECTORY */) {
879
+ throw new WasiError(`Preopen not dir: ["${mappedPath}", "${realPath}"]`, 54 /* WasiErrno.ENOTDIR */);
880
+ }
881
+ const result = getRights(this.stdio, fd.fd, 0, type);
882
+ return this.insert(fd, mappedPath, realPath, type, result.base, result.inheriting, 1);
883
+ }
884
+ async renumber(dst, src) {
885
+ if (dst === src)
886
+ return;
887
+ if (dst >= this.size || src >= this.size) {
888
+ throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
889
+ }
890
+ const dstEntry = this.fds[dst];
891
+ const srcEntry = this.fds[src];
892
+ if (!dstEntry || !srcEntry || dstEntry.id !== dst || srcEntry.id !== src) {
893
+ throw new WasiError('Invalid fd', 8 /* WasiErrno.EBADF */);
894
+ }
895
+ await dstEntry.fd.close();
896
+ this.fds[dst] = this.fds[src];
897
+ this.fds[dst].id = dst;
898
+ this.fds[src] = undefined;
899
+ this.used--;
900
+ }
858
901
  }
859
902
 
860
903
  /** @public */
@@ -885,6 +928,44 @@ function extendMemory(memory) {
885
928
  return memory;
886
929
  }
887
930
 
931
+ function checkWebAssemblyFunction() {
932
+ const WebAssemblyFunction = _WebAssembly.Function;
933
+ if (typeof WebAssemblyFunction !== 'function') {
934
+ throw new Error('WebAssembly.Function is not supported in this environment.' +
935
+ ' If you are using V8 based browser like Chrome, try to specify' +
936
+ ' --js-flag="--wasm-staging --experimental-wasm-stack-switching"');
937
+ }
938
+ return WebAssemblyFunction;
939
+ }
940
+ /** @public */
941
+ function wrapAsyncImport(f, parameterType, returnType) {
942
+ const WebAssemblyFunction = checkWebAssemblyFunction();
943
+ if (typeof f !== 'function') {
944
+ throw new TypeError('Function required');
945
+ }
946
+ const parameters = parameterType.slice(0);
947
+ parameters.unshift('externref');
948
+ return new WebAssemblyFunction({ parameters, results: returnType }, f, { suspending: 'first' });
949
+ }
950
+ /** @public */
951
+ function wrapAsyncExport(f) {
952
+ const WebAssemblyFunction = checkWebAssemblyFunction();
953
+ if (typeof f !== 'function') {
954
+ throw new TypeError('Function required');
955
+ }
956
+ return new WebAssemblyFunction({ parameters: [...WebAssemblyFunction.type(f).parameters.slice(1)], results: ['externref'] }, f, { promising: 'first' });
957
+ }
958
+ /** @public */
959
+ function wrapExports(exports, needWrap) {
960
+ return wrapInstanceExports(exports, (exportValue, name) => {
961
+ let ignore = typeof exportValue !== 'function';
962
+ if (Array.isArray(needWrap)) {
963
+ ignore = ignore || (needWrap.indexOf(name) === -1);
964
+ }
965
+ return ignore ? exportValue : wrapAsyncExport(exportValue);
966
+ });
967
+ }
968
+
888
969
  function copyMemory(targets, src) {
889
970
  if (targets.length === 0 || src.length === 0)
890
971
  return 0;
@@ -943,7 +1024,7 @@ function defineName(name, f) {
943
1024
  Object.defineProperty(f, 'name', { value: name });
944
1025
  return f;
945
1026
  }
946
- function syscallWrap(name, f) {
1027
+ function syscallWrap(self, name, f) {
947
1028
  return defineName(name, function () {
948
1029
  {
949
1030
  const args = Array.prototype.slice.call(arguments);
@@ -953,7 +1034,7 @@ function syscallWrap(name, f) {
953
1034
  }
954
1035
  let r;
955
1036
  try {
956
- r = f.apply(this, arguments);
1037
+ r = f.apply(self, arguments);
957
1038
  }
958
1039
  catch (err) {
959
1040
  return handleError(err);
@@ -964,7 +1045,7 @@ function syscallWrap(name, f) {
964
1045
  return r;
965
1046
  });
966
1047
  }
967
- function resolvePath(fs, fileDescriptor, path, flags) {
1048
+ function resolvePathSync(fs, fileDescriptor, path, flags) {
968
1049
  let resolvedPath = resolve(fileDescriptor.realPath, path);
969
1050
  if ((flags & 1) === 1) {
970
1051
  try {
@@ -978,6 +1059,20 @@ function resolvePath(fs, fileDescriptor, path, flags) {
978
1059
  }
979
1060
  return resolvedPath;
980
1061
  }
1062
+ async function resolvePathAsync(fs, fileDescriptor, path, flags) {
1063
+ let resolvedPath = resolve(fileDescriptor.realPath, path);
1064
+ if ((flags & 1) === 1) {
1065
+ try {
1066
+ resolvedPath = await fs.promises.readlink(resolvedPath);
1067
+ }
1068
+ catch (err) {
1069
+ if (err.code !== 'EINVAL' && err.code !== 'ENOENT') {
1070
+ throw err;
1071
+ }
1072
+ }
1073
+ }
1074
+ return resolvedPath;
1075
+ }
981
1076
  const encoder = new TextEncoder();
982
1077
  const decoder = new TextDecoder();
983
1078
  function readStdin() {
@@ -988,14 +1083,8 @@ function readStdin() {
988
1083
  return buffer;
989
1084
  }
990
1085
  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) {
1086
+ constructor(args, env, fds, asyncFs, fs, asyncify) {
1087
+ this.args_get = syscallWrap(this, 'args_get', function (argv, argv_buf) {
999
1088
  argv = Number(argv);
1000
1089
  argv_buf = Number(argv_buf);
1001
1090
  if (argv === 0 || argv_buf === 0) {
@@ -1014,7 +1103,7 @@ class WASI$1 {
1014
1103
  }
1015
1104
  return 0 /* WasiErrno.ESUCCESS */;
1016
1105
  });
1017
- this.args_sizes_get = syscallWrap('args_sizes_get', function (argc, argv_buf_size) {
1106
+ this.args_sizes_get = syscallWrap(this, 'args_sizes_get', function (argc, argv_buf_size) {
1018
1107
  argc = Number(argc);
1019
1108
  argv_buf_size = Number(argv_buf_size);
1020
1109
  if (argc === 0 || argv_buf_size === 0) {
@@ -1027,7 +1116,7 @@ class WASI$1 {
1027
1116
  view.setUint32(argv_buf_size, encoder.encode(args.join('\0') + '\0').length, true);
1028
1117
  return 0 /* WasiErrno.ESUCCESS */;
1029
1118
  });
1030
- this.environ_get = syscallWrap('environ_get', function (environ, environ_buf) {
1119
+ this.environ_get = syscallWrap(this, 'environ_get', function (environ, environ_buf) {
1031
1120
  environ = Number(environ);
1032
1121
  environ_buf = Number(environ_buf);
1033
1122
  if (environ === 0 || environ_buf === 0) {
@@ -1046,7 +1135,7 @@ class WASI$1 {
1046
1135
  }
1047
1136
  return 0 /* WasiErrno.ESUCCESS */;
1048
1137
  });
1049
- this.environ_sizes_get = syscallWrap('environ_sizes_get', function (len, buflen) {
1138
+ this.environ_sizes_get = syscallWrap(this, 'environ_sizes_get', function (len, buflen) {
1050
1139
  len = Number(len);
1051
1140
  buflen = Number(buflen);
1052
1141
  if (len === 0 || buflen === 0) {
@@ -1058,7 +1147,7 @@ class WASI$1 {
1058
1147
  view.setUint32(buflen, encoder.encode(wasi.env.join('\0') + '\0').length, true);
1059
1148
  return 0 /* WasiErrno.ESUCCESS */;
1060
1149
  });
1061
- this.clock_res_get = syscallWrap('clock_res_get', function (id, resolution) {
1150
+ this.clock_res_get = syscallWrap(this, 'clock_res_get', function (id, resolution) {
1062
1151
  resolution = Number(resolution);
1063
1152
  if (resolution === 0) {
1064
1153
  return 28 /* WasiErrno.EINVAL */;
@@ -1076,7 +1165,7 @@ class WASI$1 {
1076
1165
  default: return 28 /* WasiErrno.EINVAL */;
1077
1166
  }
1078
1167
  });
1079
- this.clock_time_get = syscallWrap('clock_time_get', function (id, _percision, time) {
1168
+ this.clock_time_get = syscallWrap(this, 'clock_time_get', function (id, _percision, time) {
1080
1169
  time = Number(time);
1081
1170
  if (time === 0) {
1082
1171
  return 28 /* WasiErrno.EINVAL */;
@@ -1099,35 +1188,10 @@ class WASI$1 {
1099
1188
  default: return 28 /* WasiErrno.EINVAL */;
1100
1189
  }
1101
1190
  });
1102
- this.fd_advise = syscallWrap('fd_advise', function (_fd, _offset, _len, _advice) {
1191
+ this.fd_advise = syscallWrap(this, 'fd_advise', function (_fd, _offset, _len, _advice) {
1103
1192
  return 52 /* WasiErrno.ENOSYS */;
1104
1193
  });
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) {
1194
+ this.fd_fdstat_get = syscallWrap(this, 'fd_fdstat_get', function (fd, fdstat) {
1131
1195
  fdstat = Number(fdstat);
1132
1196
  if (fdstat === 0) {
1133
1197
  return 28 /* WasiErrno.EINVAL */;
@@ -1141,10 +1205,10 @@ class WASI$1 {
1141
1205
  view.setBigUint64(fdstat + 16, fileDescriptor.rightsInheriting, true);
1142
1206
  return 0 /* WasiErrno.ESUCCESS */;
1143
1207
  });
1144
- this.fd_fdstat_set_flags = syscallWrap('fd_fdstat_set_flags', function (_fd, _flags) {
1208
+ this.fd_fdstat_set_flags = syscallWrap(this, 'fd_fdstat_set_flags', function (_fd, _flags) {
1145
1209
  return 52 /* WasiErrno.ENOSYS */;
1146
1210
  });
1147
- this.fd_fdstat_set_rights = syscallWrap('fd_fdstat_set_rights', function (fd, rightsBase, rightsInheriting) {
1211
+ this.fd_fdstat_set_rights = syscallWrap(this, 'fd_fdstat_set_rights', function (fd, rightsBase, rightsInheriting) {
1148
1212
  const wasi = _wasi.get(this);
1149
1213
  const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
1150
1214
  if ((rightsBase | fileDescriptor.rightsBase) > fileDescriptor.rightsBase) {
@@ -1158,65 +1222,7 @@ class WASI$1 {
1158
1222
  fileDescriptor.rightsInheriting = rightsInheriting;
1159
1223
  return 0 /* WasiErrno.ESUCCESS */;
1160
1224
  });
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) {
1225
+ this.fd_prestat_get = syscallWrap(this, 'fd_prestat_get', function (fd, prestat) {
1220
1226
  prestat = Number(prestat);
1221
1227
  if (prestat === 0) {
1222
1228
  return 28 /* WasiErrno.EINVAL */;
@@ -1239,7 +1245,7 @@ class WASI$1 {
1239
1245
  view.setUint32(prestat + 4, encoder.encode(fileDescriptor.path).length + 1, true);
1240
1246
  return 0 /* WasiErrno.ESUCCESS */;
1241
1247
  });
1242
- this.fd_prestat_dir_name = syscallWrap('fd_prestat_dir_name', function (fd, path, path_len) {
1248
+ this.fd_prestat_dir_name = syscallWrap(this, 'fd_prestat_dir_name', function (fd, path, path_len) {
1243
1249
  path = Number(path);
1244
1250
  path_len = Number(path_len);
1245
1251
  if (path === 0) {
@@ -1257,64 +1263,7 @@ class WASI$1 {
1257
1263
  HEAPU8.set(buffer, path);
1258
1264
  return 0 /* WasiErrno.ESUCCESS */;
1259
1265
  });
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) {
1264
- return 28 /* WasiErrno.EINVAL */;
1265
- }
1266
- const { HEAPU8, view } = getMemory(this);
1267
- 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);
1278
- return 0 /* WasiErrno.ESUCCESS */;
1279
- });
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);
1287
- 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;
1312
- fileDescriptor.pos += BigInt(nread);
1313
- }
1314
- view.setUint32(size, nread, true);
1315
- return 0 /* WasiErrno.ESUCCESS */;
1316
- });
1317
- this.fd_seek = syscallWrap('fd_seek', function (fd, offset, whence, newOffset) {
1266
+ this.fd_seek = syscallWrap(this, 'fd_seek', function (fd, offset, whence, newOffset) {
1318
1267
  newOffset = Number(newOffset);
1319
1268
  if (newOffset === 0) {
1320
1269
  return 28 /* WasiErrno.EINVAL */;
@@ -1328,7 +1277,349 @@ class WASI$1 {
1328
1277
  view.setBigUint64(newOffset, r, true);
1329
1278
  return 0 /* WasiErrno.ESUCCESS */;
1330
1279
  });
1331
- this.fd_readdir = syscallWrap('fd_readdir', function (fd, buf, buf_len, cookie, bufused) {
1280
+ this.fd_tell = syscallWrap(this, 'fd_tell', function (fd, offset) {
1281
+ const wasi = _wasi.get(this);
1282
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_TELL, BigInt(0));
1283
+ const pos = BigInt(fileDescriptor.pos);
1284
+ const { view } = getMemory(this);
1285
+ view.setBigUint64(Number(offset), pos, true);
1286
+ return 0 /* WasiErrno.ESUCCESS */;
1287
+ });
1288
+ this.poll_oneoff = syscallWrap(this, 'poll_oneoff', function (_in, _out, _sub, _nevents) {
1289
+ return 52 /* WasiErrno.ENOSYS */;
1290
+ });
1291
+ this.proc_exit = syscallWrap(this, 'proc_exit', function (_rval) {
1292
+ return 0 /* WasiErrno.ESUCCESS */;
1293
+ });
1294
+ this.proc_raise = syscallWrap(this, 'proc_raise', function (_sig) {
1295
+ return 52 /* WasiErrno.ENOSYS */;
1296
+ });
1297
+ this.sched_yield = syscallWrap(this, 'sched_yield', function () {
1298
+ return 0 /* WasiErrno.ESUCCESS */;
1299
+ });
1300
+ this.random_get = typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function'
1301
+ ? syscallWrap(this, 'random_get', function (buf, buf_len) {
1302
+ buf = Number(buf);
1303
+ if (buf === 0) {
1304
+ return 28 /* WasiErrno.EINVAL */;
1305
+ }
1306
+ buf_len = Number(buf_len);
1307
+ const { HEAPU8 } = getMemory(this);
1308
+ let pos;
1309
+ const stride = 65536;
1310
+ for (pos = 0; pos + stride < buf_len; pos += stride) {
1311
+ crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + pos + stride));
1312
+ }
1313
+ crypto.getRandomValues(HEAPU8.subarray(buf + pos, buf + buf_len));
1314
+ return 0 /* WasiErrno.ESUCCESS */;
1315
+ })
1316
+ : syscallWrap(this, 'random_get', function (buf, buf_len) {
1317
+ buf = Number(buf);
1318
+ if (buf === 0) {
1319
+ return 28 /* WasiErrno.EINVAL */;
1320
+ }
1321
+ buf_len = Number(buf_len);
1322
+ const { view } = getMemory(this);
1323
+ for (let i = buf; i < buf + buf_len; ++i) {
1324
+ view.setUint8(i, Math.floor(Math.random() * 256));
1325
+ }
1326
+ return 0 /* WasiErrno.ESUCCESS */;
1327
+ });
1328
+ this.sock_recv = syscallWrap(this, 'sock_recv', function () {
1329
+ return 58 /* WasiErrno.ENOTSUP */;
1330
+ });
1331
+ this.sock_send = syscallWrap(this, 'sock_send', function () {
1332
+ return 58 /* WasiErrno.ENOTSUP */;
1333
+ });
1334
+ this.sock_shutdown = syscallWrap(this, 'sock_shutdown', function () {
1335
+ return 58 /* WasiErrno.ENOTSUP */;
1336
+ });
1337
+ _wasi.set(this, {
1338
+ fds,
1339
+ args,
1340
+ env
1341
+ });
1342
+ if (fs)
1343
+ _fs.set(this, fs);
1344
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
1345
+ const _this = this;
1346
+ function defineImport(name, syncVersion, asyncVersion, parameterType, returnType) {
1347
+ if (asyncFs) {
1348
+ if (asyncify) {
1349
+ _this[name] = asyncify.wrapImportFunction(syscallWrap(_this, name, asyncVersion));
1350
+ }
1351
+ else {
1352
+ _this[name] = wrapAsyncImport(syscallWrap(_this, name, asyncVersion), parameterType, returnType);
1353
+ }
1354
+ }
1355
+ else {
1356
+ _this[name] = syscallWrap(_this, name, syncVersion);
1357
+ }
1358
+ }
1359
+ defineImport('fd_allocate', function fd_allocate(fd, offset, len) {
1360
+ const wasi = _wasi.get(this);
1361
+ const fs = getFs(this);
1362
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_ALLOCATE, BigInt(0));
1363
+ const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
1364
+ if (stat.size < offset + len) {
1365
+ fs.ftruncateSync(fileDescriptor.fd, Number(offset + len));
1366
+ }
1367
+ return 0 /* WasiErrno.ESUCCESS */;
1368
+ }, async function fd_allocate(fd, offset, len) {
1369
+ const wasi = _wasi.get(this);
1370
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_ALLOCATE, BigInt(0));
1371
+ const h = fileDescriptor.fd;
1372
+ const stat = await h.stat({ bigint: true });
1373
+ if (stat.size < offset + len) {
1374
+ await h.truncate(Number(offset + len));
1375
+ }
1376
+ return 0 /* WasiErrno.ESUCCESS */;
1377
+ }, ['i32', 'i64', 'f64'], ['i32']);
1378
+ defineImport('fd_close', function fd_close(fd) {
1379
+ const wasi = _wasi.get(this);
1380
+ const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
1381
+ const fs = getFs(this);
1382
+ fs.closeSync(fileDescriptor.fd);
1383
+ wasi.fds.remove(fd);
1384
+ return 0 /* WasiErrno.ESUCCESS */;
1385
+ }, async function fd_close(fd) {
1386
+ const wasi = _wasi.get(this);
1387
+ const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
1388
+ await fileDescriptor.fd.close();
1389
+ wasi.fds.remove(fd);
1390
+ return 0 /* WasiErrno.ESUCCESS */;
1391
+ }, ['i32'], ['i32']);
1392
+ defineImport('fd_datasync', function fd_datasync(fd) {
1393
+ const wasi = _wasi.get(this);
1394
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_DATASYNC, BigInt(0));
1395
+ const fs = getFs(this);
1396
+ fs.fdatasyncSync(fileDescriptor.fd);
1397
+ return 0 /* WasiErrno.ESUCCESS */;
1398
+ }, async function fd_datasync(fd) {
1399
+ const wasi = _wasi.get(this);
1400
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_DATASYNC, BigInt(0));
1401
+ await fileDescriptor.fd.datasync();
1402
+ return 0 /* WasiErrno.ESUCCESS */;
1403
+ }, ['i32'], ['i32']);
1404
+ defineImport('fd_filestat_get', function fd_filestat_get(fd, buf) {
1405
+ buf = Number(buf);
1406
+ if (buf === 0)
1407
+ return 28 /* WasiErrno.EINVAL */;
1408
+ const wasi = _wasi.get(this);
1409
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
1410
+ const fs = getFs(this);
1411
+ const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
1412
+ const { view } = getMemory(this);
1413
+ toFileStat(view, buf, stat);
1414
+ return 0 /* WasiErrno.ESUCCESS */;
1415
+ }, async function fd_filestat_get(fd, buf) {
1416
+ buf = Number(buf);
1417
+ if (buf === 0)
1418
+ return 28 /* WasiErrno.EINVAL */;
1419
+ const wasi = _wasi.get(this);
1420
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
1421
+ const h = fileDescriptor.fd;
1422
+ const stat = await h.stat({ bigint: true });
1423
+ const { view } = getMemory(this);
1424
+ toFileStat(view, buf, stat);
1425
+ return 0 /* WasiErrno.ESUCCESS */;
1426
+ }, ['i32', 'i32'], ['i32']);
1427
+ defineImport('fd_filestat_set_size', function fd_filestat_set_size(fd, size) {
1428
+ const wasi = _wasi.get(this);
1429
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_SIZE, BigInt(0));
1430
+ const fs = getFs(this);
1431
+ fs.ftruncateSync(fileDescriptor.fd, Number(size));
1432
+ return 0 /* WasiErrno.ESUCCESS */;
1433
+ }, async 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 h = fileDescriptor.fd;
1437
+ await h.truncate(Number(size));
1438
+ return 0 /* WasiErrno.ESUCCESS */;
1439
+ }, ['i32', 'i64'], ['i32']);
1440
+ function fdFilestatGetTimes(fd, atim, mtim, flags) {
1441
+ const wasi = _wasi.get(this);
1442
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_SET_TIMES, BigInt(0));
1443
+ if ((flags & 2 /* WasiFstFlag.SET_ATIM_NOW */) === 2 /* WasiFstFlag.SET_ATIM_NOW */) {
1444
+ atim = BigInt(Date.now() * 1000000);
1445
+ }
1446
+ if ((flags & 8 /* WasiFstFlag.SET_MTIM_NOW */) === 8 /* WasiFstFlag.SET_MTIM_NOW */) {
1447
+ mtim = BigInt(Date.now() * 1000000);
1448
+ }
1449
+ return { fileDescriptor, atim, mtim };
1450
+ }
1451
+ defineImport('fd_filestat_set_times', function fd_filestat_set_times(fd, atim, mtim, flags) {
1452
+ const { fileDescriptor, atim: atimRes, mtim: mtimRes } = fdFilestatGetTimes.call(this, fd, atim, mtim, flags);
1453
+ const fs = getFs(this);
1454
+ fs.futimesSync(fileDescriptor.fd, Number(atimRes), Number(mtimRes));
1455
+ return 0 /* WasiErrno.ESUCCESS */;
1456
+ }, async function fd_filestat_set_times(fd, atim, mtim, flags) {
1457
+ const { fileDescriptor, atim: atimRes, mtim: mtimRes } = fdFilestatGetTimes.call(this, fd, atim, mtim, flags);
1458
+ const h = fileDescriptor.fd;
1459
+ await h.utimes(Number(atimRes), Number(mtimRes));
1460
+ return 0 /* WasiErrno.ESUCCESS */;
1461
+ }, ['i32', 'i64', 'i64', 'i32'], ['i32']);
1462
+ defineImport('fd_pread', function fd_pread(fd, iovs, iovslen, offset, size) {
1463
+ iovs = Number(iovs);
1464
+ size = Number(size);
1465
+ if (iovs === 0 || size === 0) {
1466
+ return 28 /* WasiErrno.EINVAL */;
1467
+ }
1468
+ const { HEAPU8, view } = getMemory(this);
1469
+ const wasi = _wasi.get(this);
1470
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
1471
+ let totalSize = 0;
1472
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1473
+ const offset = iovs + (i * 8);
1474
+ const buf = view.getInt32(offset, true);
1475
+ const bufLen = view.getUint32(offset + 4, true);
1476
+ totalSize += bufLen;
1477
+ return HEAPU8.subarray(buf, buf + bufLen);
1478
+ });
1479
+ let nread = 0;
1480
+ const buffer = new Uint8Array(totalSize);
1481
+ buffer._isBuffer = true;
1482
+ const fs = getFs(this);
1483
+ const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
1484
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1485
+ view.setUint32(size, nread, true);
1486
+ return 0 /* WasiErrno.ESUCCESS */;
1487
+ }, async function (fd, iovs, iovslen, offset, size) {
1488
+ iovs = Number(iovs);
1489
+ size = Number(size);
1490
+ if (iovs === 0 || size === 0) {
1491
+ return 28 /* WasiErrno.EINVAL */;
1492
+ }
1493
+ const { HEAPU8, view } = getMemory(this);
1494
+ const wasi = _wasi.get(this);
1495
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
1496
+ let totalSize = 0;
1497
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1498
+ const offset = iovs + (i * 8);
1499
+ const buf = view.getInt32(offset, true);
1500
+ const bufLen = view.getUint32(offset + 4, true);
1501
+ totalSize += bufLen;
1502
+ return HEAPU8.subarray(buf, buf + bufLen);
1503
+ });
1504
+ let nread = 0;
1505
+ const buffer = new Uint8Array(totalSize);
1506
+ buffer._isBuffer = true;
1507
+ const { bytesRead } = await fileDescriptor.fd.read(buffer, 0, buffer.length, Number(offset));
1508
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1509
+ view.setUint32(size, nread, true);
1510
+ return 0 /* WasiErrno.ESUCCESS */;
1511
+ }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
1512
+ defineImport('fd_pwrite', function fd_pwrite(fd, iovs, iovslen, offset, size) {
1513
+ iovs = Number(iovs);
1514
+ size = Number(size);
1515
+ if (iovs === 0 || size === 0) {
1516
+ return 28 /* WasiErrno.EINVAL */;
1517
+ }
1518
+ const { HEAPU8, view } = getMemory(this);
1519
+ const wasi = _wasi.get(this);
1520
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
1521
+ const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
1522
+ const offset = iovs + (i * 8);
1523
+ const buf = view.getInt32(offset, true);
1524
+ const bufLen = view.getUint32(offset + 4, true);
1525
+ return HEAPU8.subarray(buf, buf + bufLen);
1526
+ }));
1527
+ const fs = getFs(this);
1528
+ const nwritten = fs.writeSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
1529
+ view.setUint32(size, nwritten, true);
1530
+ return 0 /* WasiErrno.ESUCCESS */;
1531
+ }, async function fd_pwrite(fd, iovs, iovslen, offset, size) {
1532
+ iovs = Number(iovs);
1533
+ size = Number(size);
1534
+ if (iovs === 0 || size === 0) {
1535
+ return 28 /* WasiErrno.EINVAL */;
1536
+ }
1537
+ const { HEAPU8, view } = getMemory(this);
1538
+ const wasi = _wasi.get(this);
1539
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
1540
+ const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
1541
+ const offset = iovs + (i * 8);
1542
+ const buf = view.getInt32(offset, true);
1543
+ const bufLen = view.getUint32(offset + 4, true);
1544
+ return HEAPU8.subarray(buf, buf + bufLen);
1545
+ }));
1546
+ const { bytesWritten } = await fileDescriptor.fd.write(buffer, 0, buffer.length, Number(offset));
1547
+ view.setUint32(size, bytesWritten, true);
1548
+ return 0 /* WasiErrno.ESUCCESS */;
1549
+ }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
1550
+ defineImport('fd_read', function fd_read(fd, iovs, iovslen, size) {
1551
+ iovs = Number(iovs);
1552
+ size = Number(size);
1553
+ if (iovs === 0 || size === 0) {
1554
+ return 28 /* WasiErrno.EINVAL */;
1555
+ }
1556
+ const { HEAPU8, view } = getMemory(this);
1557
+ const wasi = _wasi.get(this);
1558
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
1559
+ let totalSize = 0;
1560
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1561
+ const offset = iovs + (i * 8);
1562
+ const buf = view.getInt32(offset, true);
1563
+ const bufLen = view.getUint32(offset + 4, true);
1564
+ totalSize += bufLen;
1565
+ return HEAPU8.subarray(buf, buf + bufLen);
1566
+ });
1567
+ let buffer;
1568
+ let nread = 0;
1569
+ if (fd === 0) {
1570
+ if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
1571
+ return 58 /* WasiErrno.ENOTSUP */;
1572
+ }
1573
+ buffer = readStdin();
1574
+ nread = buffer ? copyMemory(ioVecs, buffer) : 0;
1575
+ }
1576
+ else {
1577
+ buffer = new Uint8Array(totalSize);
1578
+ buffer._isBuffer = true;
1579
+ const fs = getFs(this);
1580
+ const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(fileDescriptor.pos));
1581
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1582
+ fileDescriptor.pos += BigInt(nread);
1583
+ }
1584
+ view.setUint32(size, nread, true);
1585
+ return 0 /* WasiErrno.ESUCCESS */;
1586
+ }, async function fd_read(fd, iovs, iovslen, size) {
1587
+ iovs = Number(iovs);
1588
+ size = Number(size);
1589
+ if (iovs === 0 || size === 0) {
1590
+ return 28 /* WasiErrno.EINVAL */;
1591
+ }
1592
+ const { HEAPU8, view } = getMemory(this);
1593
+ const wasi = _wasi.get(this);
1594
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
1595
+ let totalSize = 0;
1596
+ const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
1597
+ const offset = iovs + (i * 8);
1598
+ const buf = view.getInt32(offset, true);
1599
+ const bufLen = view.getUint32(offset + 4, true);
1600
+ totalSize += bufLen;
1601
+ return HEAPU8.subarray(buf, buf + bufLen);
1602
+ });
1603
+ let buffer;
1604
+ let nread = 0;
1605
+ if (fd === 0) {
1606
+ if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
1607
+ return 58 /* WasiErrno.ENOTSUP */;
1608
+ }
1609
+ buffer = readStdin();
1610
+ nread = buffer ? copyMemory(ioVecs, buffer) : 0;
1611
+ }
1612
+ else {
1613
+ buffer = new Uint8Array(totalSize);
1614
+ buffer._isBuffer = true;
1615
+ const { bytesRead } = await fileDescriptor.fd.read(buffer, 0, buffer.length, Number(fileDescriptor.pos));
1616
+ nread = buffer ? copyMemory(ioVecs, buffer.subarray(0, bytesRead)) : 0;
1617
+ fileDescriptor.pos += BigInt(nread);
1618
+ }
1619
+ view.setUint32(size, nread, true);
1620
+ return 0 /* WasiErrno.ESUCCESS */;
1621
+ }, ['i32', 'i32', 'i32', 'i32'], ['i32']);
1622
+ defineImport('fd_readdir', function fd_readdir(fd, buf, buf_len, cookie, bufused) {
1332
1623
  buf = Number(buf);
1333
1624
  buf_len = Number(buf_len);
1334
1625
  bufused = Number(bufused);
@@ -1378,28 +1669,79 @@ class WASI$1 {
1378
1669
  }
1379
1670
  view.setUint32(bufused, bufferUsed, true);
1380
1671
  return 0 /* WasiErrno.ESUCCESS */;
1381
- });
1382
- this.fd_renumber = syscallWrap('fd_renumber', function (from, to) {
1672
+ }, async function fd_readdir(fd, buf, buf_len, cookie, bufused) {
1673
+ buf = Number(buf);
1674
+ buf_len = Number(buf_len);
1675
+ bufused = Number(bufused);
1676
+ if (buf === 0 || bufused === 0)
1677
+ return 0 /* WasiErrno.ESUCCESS */;
1678
+ const wasi = _wasi.get(this);
1679
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READDIR, BigInt(0));
1680
+ const fs = getFs(this);
1681
+ const entries = await fs.promises.readdir(fileDescriptor.realPath, { withFileTypes: true });
1682
+ const { HEAPU8, view } = getMemory(this);
1683
+ let bufferUsed = 0;
1684
+ for (let i = Number(cookie); i < entries.length; i++) {
1685
+ const nameData = encoder.encode(entries[i].name);
1686
+ const entryInfo = await fs.promises.stat(resolve(fileDescriptor.realPath, entries[i].name), { bigint: true });
1687
+ const entryData = new Uint8Array(24 + nameData.byteLength);
1688
+ const entryView = new DataView(entryData.buffer);
1689
+ entryView.setBigUint64(0, BigInt(i + 1), true);
1690
+ entryView.setBigUint64(8, BigInt(entryInfo.ino ? entryInfo.ino : 0), true);
1691
+ entryView.setUint32(16, nameData.byteLength, true);
1692
+ let type;
1693
+ if (entries[i].isFile()) {
1694
+ type = 4 /* WasiFileType.REGULAR_FILE */;
1695
+ }
1696
+ else if (entries[i].isDirectory()) {
1697
+ type = 3 /* WasiFileType.DIRECTORY */;
1698
+ }
1699
+ else if (entries[i].isSymbolicLink()) {
1700
+ type = 7 /* WasiFileType.SYMBOLIC_LINK */;
1701
+ }
1702
+ else if (entries[i].isCharacterDevice()) {
1703
+ type = 2 /* WasiFileType.CHARACTER_DEVICE */;
1704
+ }
1705
+ else if (entries[i].isBlockDevice()) {
1706
+ type = 1 /* WasiFileType.BLOCK_DEVICE */;
1707
+ }
1708
+ else if (entries[i].isSocket()) {
1709
+ type = 6 /* WasiFileType.SOCKET_STREAM */;
1710
+ }
1711
+ else {
1712
+ type = 0 /* WasiFileType.UNKNOWN */;
1713
+ }
1714
+ entryView.setUint8(20, type);
1715
+ entryData.set(nameData, 24);
1716
+ const data = entryData.slice(0, Math.min(entryData.length, buf_len - bufferUsed));
1717
+ HEAPU8.set(data, buf + bufferUsed);
1718
+ bufferUsed += data.byteLength;
1719
+ }
1720
+ view.setUint32(bufused, bufferUsed, true);
1721
+ return 0 /* WasiErrno.ESUCCESS */;
1722
+ }, ['i32', 'i32', 'i32', 'i64', 'i32'], ['i32']);
1723
+ defineImport('fd_renumber', function fd_renumber(from, to) {
1383
1724
  const wasi = _wasi.get(this);
1384
1725
  wasi.fds.renumber(to, from);
1385
1726
  return 0 /* WasiErrno.ESUCCESS */;
1386
- });
1387
- this.fd_sync = syscallWrap('fd_sync', function (fd) {
1727
+ }, async function fd_renumber(from, to) {
1728
+ const wasi = _wasi.get(this);
1729
+ await wasi.fds.renumber(to, from);
1730
+ return 0 /* WasiErrno.ESUCCESS */;
1731
+ }, ['i32', 'i32'], ['i32']);
1732
+ defineImport('fd_sync', function fd_sync(fd) {
1388
1733
  const wasi = _wasi.get(this);
1389
1734
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SYNC, BigInt(0));
1390
1735
  const fs = getFs(this);
1391
1736
  fs.fsyncSync(fileDescriptor.fd);
1392
1737
  return 0 /* WasiErrno.ESUCCESS */;
1393
- });
1394
- this.fd_tell = syscallWrap('fd_tell', function (fd, offset) {
1738
+ }, async function fd_sync(fd) {
1395
1739
  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);
1740
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SYNC, BigInt(0));
1741
+ await fileDescriptor.fd.sync();
1400
1742
  return 0 /* WasiErrno.ESUCCESS */;
1401
- });
1402
- this.fd_write = syscallWrap('fd_write', function (fd, iovs, iovslen, size) {
1743
+ }, ['i32'], ['i32']);
1744
+ defineImport('fd_write', function fd_write(fd, iovs, iovslen, size) {
1403
1745
  iovs = Number(iovs);
1404
1746
  size = Number(size);
1405
1747
  if (iovs === 0 || size === 0) {
@@ -1425,8 +1767,33 @@ class WASI$1 {
1425
1767
  }
1426
1768
  view.setUint32(size, nwritten, true);
1427
1769
  return 0 /* WasiErrno.ESUCCESS */;
1428
- });
1429
- this.path_create_directory = syscallWrap('path_create_directory', function (fd, path, path_len) {
1770
+ }, async function fd_write(fd, iovs, iovslen, size) {
1771
+ iovs = Number(iovs);
1772
+ size = Number(size);
1773
+ if (iovs === 0 || size === 0) {
1774
+ return 28 /* WasiErrno.EINVAL */;
1775
+ }
1776
+ const { HEAPU8, view } = getMemory(this);
1777
+ const wasi = _wasi.get(this);
1778
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
1779
+ const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
1780
+ const offset = iovs + (i * 8);
1781
+ const buf = view.getInt32(offset, true);
1782
+ const bufLen = view.getUint32(offset + 4, true);
1783
+ return HEAPU8.subarray(buf, buf + bufLen);
1784
+ }));
1785
+ let nwritten;
1786
+ if (fd === 1 || fd === 2) {
1787
+ nwritten = fileDescriptor.write(buffer);
1788
+ }
1789
+ else {
1790
+ nwritten = await (await (fileDescriptor.fd.write(buffer, 0, buffer.length, Number(fileDescriptor.pos)))).bytesWritten;
1791
+ fileDescriptor.pos += BigInt(nwritten);
1792
+ }
1793
+ view.setUint32(size, nwritten, true);
1794
+ return 0 /* WasiErrno.ESUCCESS */;
1795
+ }, ['i32', 'i32', 'i32', 'i32'], ['i32']);
1796
+ defineImport('path_create_directory', function path_create_directory(fd, path, path_len) {
1430
1797
  path = Number(path);
1431
1798
  path_len = Number(path_len);
1432
1799
  if (path === 0) {
@@ -1440,8 +1807,22 @@ class WASI$1 {
1440
1807
  const fs = getFs(this);
1441
1808
  fs.mkdirSync(pathString);
1442
1809
  return 0 /* WasiErrno.ESUCCESS */;
1443
- });
1444
- this.path_filestat_get = syscallWrap('path_filestat_get', function (fd, flags, path, path_len, filestat) {
1810
+ }, async function path_create_directory(fd, path, path_len) {
1811
+ path = Number(path);
1812
+ path_len = Number(path_len);
1813
+ if (path === 0) {
1814
+ return 28 /* WasiErrno.EINVAL */;
1815
+ }
1816
+ const { HEAPU8 } = getMemory(this);
1817
+ const wasi = _wasi.get(this);
1818
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
1819
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1820
+ pathString = resolve(fileDescriptor.realPath, pathString);
1821
+ const fs = getFs(this);
1822
+ await fs.promises.mkdir(pathString);
1823
+ return 0 /* WasiErrno.ESUCCESS */;
1824
+ }, ['i32', 'i32', 'i32'], ['i32']);
1825
+ defineImport('path_filestat_get', function path_filestat_get(fd, flags, path, path_len, filestat) {
1445
1826
  path = Number(path);
1446
1827
  path_len = Number(path_len);
1447
1828
  filestat = Number(filestat);
@@ -1463,8 +1844,30 @@ class WASI$1 {
1463
1844
  }
1464
1845
  toFileStat(view, filestat, stat);
1465
1846
  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) {
1847
+ }, async function path_filestat_get(fd, flags, path, path_len, filestat) {
1848
+ path = Number(path);
1849
+ path_len = Number(path_len);
1850
+ filestat = Number(filestat);
1851
+ if (path === 0 || filestat === 0) {
1852
+ return 28 /* WasiErrno.EINVAL */;
1853
+ }
1854
+ const { HEAPU8, view } = getMemory(this);
1855
+ const wasi = _wasi.get(this);
1856
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
1857
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1858
+ const fs = getFs(this);
1859
+ pathString = resolve(fileDescriptor.realPath, pathString);
1860
+ let stat;
1861
+ if ((flags & 1) === 1) {
1862
+ stat = await fs.promises.stat(pathString, { bigint: true });
1863
+ }
1864
+ else {
1865
+ stat = await fs.promises.lstat(pathString, { bigint: true });
1866
+ }
1867
+ toFileStat(view, filestat, stat);
1868
+ return 0 /* WasiErrno.ESUCCESS */;
1869
+ }, ['i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1870
+ defineImport('path_filestat_set_times', function path_filestat_set_times(fd, flags, path, path_len, atim, mtim, fst_flags) {
1468
1871
  path = Number(path);
1469
1872
  path_len = Number(path_len);
1470
1873
  if (path === 0)
@@ -1479,7 +1882,7 @@ class WASI$1 {
1479
1882
  const wasi = _wasi.get(this);
1480
1883
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1481
1884
  const fs = getFs(this);
1482
- const resolvedPath = resolvePath(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1885
+ const resolvedPath = resolvePathSync(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1483
1886
  if ((fst_flags & 2 /* WasiFstFlag.SET_ATIM_NOW */) === 2 /* WasiFstFlag.SET_ATIM_NOW */) {
1484
1887
  atim = BigInt(Date.now() * 1000000);
1485
1888
  }
@@ -1488,8 +1891,32 @@ class WASI$1 {
1488
1891
  }
1489
1892
  fs.utimesSync(resolvedPath, Number(atim), Number(mtim));
1490
1893
  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) {
1894
+ }, async function path_filestat_set_times(fd, flags, path, path_len, atim, mtim, fst_flags) {
1895
+ path = Number(path);
1896
+ path_len = Number(path_len);
1897
+ if (path === 0)
1898
+ return 28 /* WasiErrno.EINVAL */;
1899
+ if ((fst_flags) & ~(1 /* WasiFstFlag.SET_ATIM */ |
1900
+ 2 /* WasiFstFlag.SET_ATIM_NOW */ |
1901
+ 4 /* WasiFstFlag.SET_MTIM */ |
1902
+ 8 /* WasiFstFlag.SET_MTIM_NOW */)) {
1903
+ return 28 /* WasiErrno.EINVAL */;
1904
+ }
1905
+ const { HEAPU8 } = getMemory(this);
1906
+ const wasi = _wasi.get(this);
1907
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1908
+ const fs = getFs(this);
1909
+ const resolvedPath = await resolvePathAsync(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1910
+ if ((fst_flags & 2 /* WasiFstFlag.SET_ATIM_NOW */) === 2 /* WasiFstFlag.SET_ATIM_NOW */) {
1911
+ atim = BigInt(Date.now() * 1000000);
1912
+ }
1913
+ if ((fst_flags & 8 /* WasiFstFlag.SET_MTIM_NOW */) === 8 /* WasiFstFlag.SET_MTIM_NOW */) {
1914
+ mtim = BigInt(Date.now() * 1000000);
1915
+ }
1916
+ await fs.promises.utimes(resolvedPath, Number(atim), Number(mtim));
1917
+ return 0 /* WasiErrno.ESUCCESS */;
1918
+ }, ['i32', 'i32', 'i32', 'i32', 'i64', 'i64', 'i32'], ['i32']);
1919
+ defineImport('path_link', function path_link(old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
1493
1920
  old_path = Number(old_path);
1494
1921
  old_path_len = Number(old_path_len);
1495
1922
  new_path = Number(new_path);
@@ -1509,20 +1936,36 @@ class WASI$1 {
1509
1936
  }
1510
1937
  const { HEAPU8 } = getMemory(this);
1511
1938
  const fs = getFs(this);
1512
- const resolvedOldPath = resolvePath(fs, oldWrap, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)), old_flags);
1939
+ const resolvedOldPath = resolvePathSync(fs, oldWrap, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)), old_flags);
1513
1940
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
1514
1941
  fs.linkSync(resolvedOldPath, resolvedNewPath);
1515
1942
  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) {
1943
+ }, async function path_link(old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
1944
+ old_path = Number(old_path);
1945
+ old_path_len = Number(old_path_len);
1946
+ new_path = Number(new_path);
1947
+ new_path_len = Number(new_path_len);
1948
+ if (old_path === 0 || new_path === 0) {
1521
1949
  return 28 /* WasiErrno.EINVAL */;
1522
1950
  }
1523
- path_len = Number(path_len);
1524
- fs_rights_base = BigInt(fs_rights_base);
1525
- fs_rights_base = BigInt(fs_rights_base);
1951
+ const wasi = _wasi.get(this);
1952
+ let oldWrap;
1953
+ let newWrap;
1954
+ if (old_fd === new_fd) {
1955
+ oldWrap = newWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE | WasiRights.PATH_LINK_TARGET, BigInt(0));
1956
+ }
1957
+ else {
1958
+ oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE, BigInt(0));
1959
+ newWrap = wasi.fds.get(new_fd, WasiRights.PATH_LINK_TARGET, BigInt(0));
1960
+ }
1961
+ const { HEAPU8 } = getMemory(this);
1962
+ const fs = getFs(this);
1963
+ const resolvedOldPath = await resolvePathAsync(fs, oldWrap, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)), old_flags);
1964
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
1965
+ await fs.promises.link(resolvedOldPath, resolvedNewPath);
1966
+ return 0 /* WasiErrno.ESUCCESS */;
1967
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1968
+ function pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags) {
1526
1969
  const read = (fs_rights_base & (WasiRights.FD_READ |
1527
1970
  WasiRights.FD_READDIR)) !== BigInt(0);
1528
1971
  const write = (fs_rights_base & (WasiRights.FD_DATASYNC |
@@ -1567,32 +2010,78 @@ class WASI$1 {
1567
2010
  if (write && (flags & (1024 /* FileControlFlag.O_APPEND */ | 512 /* FileControlFlag.O_TRUNC */)) === 0) {
1568
2011
  needed_inheriting |= WasiRights.FD_SEEK;
1569
2012
  }
2013
+ return { flags, needed_base, needed_inheriting };
2014
+ }
2015
+ defineImport('path_open', function path_open(dirfd, dirflags, path, path_len, o_flags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) {
2016
+ path = Number(path);
2017
+ fd = Number(fd);
2018
+ if (path === 0 || fd === 0) {
2019
+ return 28 /* WasiErrno.EINVAL */;
2020
+ }
2021
+ path_len = Number(path_len);
2022
+ fs_rights_base = BigInt(fs_rights_base);
2023
+ fs_rights_inheriting = BigInt(fs_rights_inheriting);
2024
+ const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
1570
2025
  const wasi = _wasi.get(this);
1571
- const fileDescriptor = wasi.fds.get(dirfd, needed_base, needed_inheriting);
2026
+ const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1572
2027
  const memory = getMemory(this);
1573
2028
  const HEAPU8 = memory.HEAPU8;
1574
2029
  const pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1575
2030
  const fs = getFs(this);
1576
- const resolved_path = resolvePath(fs, fileDescriptor, pathString, dirflags);
1577
- const r = fs.openSync(resolved_path, flags, 0o666);
2031
+ const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
2032
+ const r = fs.openSync(resolved_path, flagsRes, 0o666);
1578
2033
  const filetype = wasi.fds.getFileTypeByFd(r);
1579
2034
  if ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 && filetype !== 3 /* WasiFileType.DIRECTORY */) {
1580
2035
  return 54 /* WasiErrno.ENOTDIR */;
1581
2036
  }
1582
- const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r, flags, filetype);
2037
+ const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r, flagsRes, filetype);
1583
2038
  const wrap = wasi.fds.insert(r, resolved_path, resolved_path, filetype, fs_rights_base & max_base, fs_rights_inheriting & max_inheriting, 0);
1584
2039
  const stat = fs.fstatSync(r, { bigint: true });
1585
2040
  if (stat.isFile()) {
1586
2041
  wrap.size = stat.size;
1587
- if ((flags & 1024 /* FileControlFlag.O_APPEND */) !== 0) {
2042
+ if ((flagsRes & 1024 /* FileControlFlag.O_APPEND */) !== 0) {
1588
2043
  wrap.pos = stat.size;
1589
2044
  }
1590
2045
  }
1591
2046
  const view = memory.view;
1592
2047
  view.setInt32(fd, wrap.id, true);
1593
2048
  return 0 /* WasiErrno.ESUCCESS */;
1594
- });
1595
- this.path_readlink = syscallWrap('path_readlink', function (fd, path, path_len, buf, buf_len, bufused) {
2049
+ }, async function path_open(dirfd, dirflags, path, path_len, o_flags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) {
2050
+ path = Number(path);
2051
+ fd = Number(fd);
2052
+ if (path === 0 || fd === 0) {
2053
+ return 28 /* WasiErrno.EINVAL */;
2054
+ }
2055
+ path_len = Number(path_len);
2056
+ fs_rights_base = BigInt(fs_rights_base);
2057
+ fs_rights_inheriting = BigInt(fs_rights_inheriting);
2058
+ const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
2059
+ const wasi = _wasi.get(this);
2060
+ const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
2061
+ const memory = getMemory(this);
2062
+ const HEAPU8 = memory.HEAPU8;
2063
+ const pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
2064
+ const fs = getFs(this);
2065
+ const resolved_path = await resolvePathAsync(fs, fileDescriptor, pathString, dirflags);
2066
+ const r = await fs.promises.open(resolved_path, flagsRes, 0o666);
2067
+ const filetype = await wasi.fds.getFileTypeByFd(r);
2068
+ if ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 && filetype !== 3 /* WasiFileType.DIRECTORY */) {
2069
+ return 54 /* WasiErrno.ENOTDIR */;
2070
+ }
2071
+ const { base: max_base, inheriting: max_inheriting } = getRights(wasi.fds.stdio, r.fd, flagsRes, filetype);
2072
+ const wrap = wasi.fds.insert(r, resolved_path, resolved_path, filetype, fs_rights_base & max_base, fs_rights_inheriting & max_inheriting, 0);
2073
+ const stat = await r.stat({ bigint: true });
2074
+ if (stat.isFile()) {
2075
+ wrap.size = stat.size;
2076
+ if ((flagsRes & 1024 /* FileControlFlag.O_APPEND */) !== 0) {
2077
+ wrap.pos = stat.size;
2078
+ }
2079
+ }
2080
+ const view = memory.view;
2081
+ view.setInt32(fd, wrap.id, true);
2082
+ return 0 /* WasiErrno.ESUCCESS */;
2083
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i64', 'i64', 'i32', 'i32'], ['i32']);
2084
+ defineImport('path_readlink', function path_readlink(fd, path, path_len, buf, buf_len, bufused) {
1596
2085
  path = Number(path);
1597
2086
  path_len = Number(path_len);
1598
2087
  buf = Number(buf);
@@ -1616,8 +2105,32 @@ class WASI$1 {
1616
2105
  HEAPU8[buf + len] = 0;
1617
2106
  view.setUint32(bufused, len + 1, true);
1618
2107
  return 0 /* WasiErrno.ESUCCESS */;
1619
- });
1620
- this.path_remove_directory = syscallWrap('path_remove_directory', function (fd, path, path_len) {
2108
+ }, async function path_readlink(fd, path, path_len, buf, buf_len, bufused) {
2109
+ path = Number(path);
2110
+ path_len = Number(path_len);
2111
+ buf = Number(buf);
2112
+ buf_len = Number(buf_len);
2113
+ bufused = Number(bufused);
2114
+ if (path === 0 || buf === 0 || bufused === 0) {
2115
+ return 28 /* WasiErrno.EINVAL */;
2116
+ }
2117
+ const { HEAPU8, view } = getMemory(this);
2118
+ const wasi = _wasi.get(this);
2119
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
2120
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
2121
+ pathString = resolve(fileDescriptor.realPath, pathString);
2122
+ const fs = getFs(this);
2123
+ const link = await fs.promises.readlink(pathString);
2124
+ const linkData = encoder.encode(link);
2125
+ const len = Math.min(linkData.length, buf_len);
2126
+ if (len >= buf_len)
2127
+ return 42 /* WasiErrno.ENOBUFS */;
2128
+ HEAPU8.set(linkData.subarray(0, len), buf);
2129
+ HEAPU8[buf + len] = 0;
2130
+ view.setUint32(bufused, len + 1, true);
2131
+ return 0 /* WasiErrno.ESUCCESS */;
2132
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
2133
+ defineImport('path_remove_directory', function path_remove_directory(fd, path, path_len) {
1621
2134
  path = Number(path);
1622
2135
  path_len = Number(path_len);
1623
2136
  if (path === 0) {
@@ -1631,8 +2144,22 @@ class WASI$1 {
1631
2144
  const fs = getFs(this);
1632
2145
  fs.rmdirSync(pathString);
1633
2146
  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) {
2147
+ }, async function path_remove_directory(fd, path, path_len) {
2148
+ path = Number(path);
2149
+ path_len = Number(path_len);
2150
+ if (path === 0) {
2151
+ return 28 /* WasiErrno.EINVAL */;
2152
+ }
2153
+ const { HEAPU8 } = getMemory(this);
2154
+ const wasi = _wasi.get(this);
2155
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
2156
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
2157
+ pathString = resolve(fileDescriptor.realPath, pathString);
2158
+ const fs = getFs(this);
2159
+ await fs.promises.rmdir(pathString);
2160
+ return 0 /* WasiErrno.ESUCCESS */;
2161
+ }, ['i32', 'i32', 'i32'], ['i32']);
2162
+ defineImport('path_rename', function path_rename(old_fd, old_path, old_path_len, new_fd, new_path, new_path_len) {
1636
2163
  old_path = Number(old_path);
1637
2164
  old_path_len = Number(old_path_len);
1638
2165
  new_path = Number(new_path);
@@ -1656,8 +2183,32 @@ class WASI$1 {
1656
2183
  const fs = getFs(this);
1657
2184
  fs.renameSync(resolvedOldPath, resolvedNewPath);
1658
2185
  return 0 /* WasiErrno.ESUCCESS */;
1659
- });
1660
- this.path_symlink = syscallWrap('path_symlink', function (old_path, old_path_len, fd, new_path, new_path_len) {
2186
+ }, async function path_rename(old_fd, old_path, old_path_len, new_fd, new_path, new_path_len) {
2187
+ old_path = Number(old_path);
2188
+ old_path_len = Number(old_path_len);
2189
+ new_path = Number(new_path);
2190
+ new_path_len = Number(new_path_len);
2191
+ if (old_path === 0 || new_path === 0) {
2192
+ return 28 /* WasiErrno.EINVAL */;
2193
+ }
2194
+ const wasi = _wasi.get(this);
2195
+ let oldWrap;
2196
+ let newWrap;
2197
+ if (old_fd === new_fd) {
2198
+ oldWrap = newWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE | WasiRights.PATH_RENAME_TARGET, BigInt(0));
2199
+ }
2200
+ else {
2201
+ oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE, BigInt(0));
2202
+ newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
2203
+ }
2204
+ const { HEAPU8 } = getMemory(this);
2205
+ const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)));
2206
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
2207
+ const fs = getFs(this);
2208
+ await fs.promises.rename(resolvedOldPath, resolvedNewPath);
2209
+ return 0 /* WasiErrno.ESUCCESS */;
2210
+ }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
2211
+ defineImport('path_symlink', function path_symlink(old_path, old_path_len, fd, new_path, new_path_len) {
1661
2212
  old_path = Number(old_path);
1662
2213
  old_path_len = Number(old_path_len);
1663
2214
  new_path = Number(new_path);
@@ -1674,8 +2225,25 @@ class WASI$1 {
1674
2225
  const fs = getFs(this);
1675
2226
  fs.symlinkSync(oldPath, newPath);
1676
2227
  return 0 /* WasiErrno.ESUCCESS */;
1677
- });
1678
- this.path_unlink_file = syscallWrap('path_unlink_file', function (fd, path, path_len) {
2228
+ }, async function path_symlink(old_path, old_path_len, fd, new_path, new_path_len) {
2229
+ old_path = Number(old_path);
2230
+ old_path_len = Number(old_path_len);
2231
+ new_path = Number(new_path);
2232
+ new_path_len = Number(new_path_len);
2233
+ if (old_path === 0 || new_path === 0) {
2234
+ return 28 /* WasiErrno.EINVAL */;
2235
+ }
2236
+ const { HEAPU8 } = getMemory(this);
2237
+ const wasi = _wasi.get(this);
2238
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
2239
+ const oldPath = decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len));
2240
+ let newPath = decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len));
2241
+ newPath = resolve(fileDescriptor.realPath, newPath);
2242
+ const fs = getFs(this);
2243
+ await fs.promises.symlink(oldPath, newPath);
2244
+ return 0 /* WasiErrno.ESUCCESS */;
2245
+ }, ['i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
2246
+ defineImport('path_unlink_file', function path_unlink_file(fd, path, path_len) {
1679
2247
  path = Number(path);
1680
2248
  path_len = Number(path_len);
1681
2249
  if (path === 0) {
@@ -1689,58 +2257,30 @@ class WASI$1 {
1689
2257
  const fs = getFs(this);
1690
2258
  fs.unlinkSync(pathString);
1691
2259
  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 () {
2260
+ }, async function path_unlink_file(fd, path, path_len) {
2261
+ path = Number(path);
2262
+ path_len = Number(path_len);
2263
+ if (path === 0) {
2264
+ return 28 /* WasiErrno.EINVAL */;
2265
+ }
2266
+ const { HEAPU8 } = getMemory(this);
2267
+ const wasi = _wasi.get(this);
2268
+ const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
2269
+ let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
2270
+ pathString = resolve(fileDescriptor.realPath, pathString);
2271
+ const fs = getFs(this);
2272
+ await fs.promises.unlink(pathString);
1703
2273
  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({
2274
+ }, ['i32', 'i32', 'i32'], ['i32']);
2275
+ this._setMemory = function setMemory(m) {
2276
+ if (!(m instanceof _WebAssembly.Memory)) {
2277
+ throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
2278
+ }
2279
+ _memory.set(_this, extendMemory(m));
2280
+ };
2281
+ }
2282
+ static createSync(args, env, preopens, stdio, fs, print, printErr) {
2283
+ const fds = new SyncTable({
1744
2284
  size: 3,
1745
2285
  in: stdio[0],
1746
2286
  out: stdio[1],
@@ -1749,13 +2289,7 @@ class WASI$1 {
1749
2289
  print,
1750
2290
  printErr
1751
2291
  });
1752
- _wasi.set(this, {
1753
- fds,
1754
- args,
1755
- env
1756
- });
1757
- if (fs)
1758
- _fs.set(this, fs);
2292
+ const _this = new WASI$1(args, env, fds, false, fs);
1759
2293
  if (preopens.length > 0) {
1760
2294
  for (let i = 0; i < preopens.length; ++i) {
1761
2295
  const realPath = fs.realpathSync(preopens[i].realPath, 'utf8');
@@ -1763,6 +2297,27 @@ class WASI$1 {
1763
2297
  fds.insertPreopen(fd, preopens[i].mappedPath, realPath);
1764
2298
  }
1765
2299
  }
2300
+ return _this;
2301
+ }
2302
+ static async createAsync(args, env, preopens, stdio, fs, print, printErr, asyncify) {
2303
+ const fds = new AsyncTable({
2304
+ size: 3,
2305
+ in: stdio[0],
2306
+ out: stdio[1],
2307
+ err: stdio[2],
2308
+ print,
2309
+ printErr
2310
+ });
2311
+ const _this = new WASI$1(args, env, fds, true, fs, asyncify);
2312
+ if (preopens.length > 0) {
2313
+ for (let i = 0; i < preopens.length; ++i) {
2314
+ const entry = preopens[i];
2315
+ const realPath = await fs.promises.realpath(entry.realPath);
2316
+ const fd = await fs.promises.open(realPath, 'r', 0o666);
2317
+ await fds.insertPreopen(fd, entry.mappedPath, realPath);
2318
+ }
2319
+ }
2320
+ return _this;
1766
2321
  }
1767
2322
  }
1768
2323
 
@@ -1779,74 +2334,105 @@ function setupInstance(self, instance) {
1779
2334
  }
1780
2335
  // /** @public */
1781
2336
  // 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');
2337
+ function validateOptions(options) {
2338
+ var _a;
2339
+ validateObject(options, 'options');
2340
+ if (options.args !== undefined) {
2341
+ validateArray(options.args, 'options.args');
2342
+ }
2343
+ const args = ((_a = options.args) !== null && _a !== void 0 ? _a : []).map(String);
2344
+ const env = [];
2345
+ if (options.env !== undefined) {
2346
+ validateObject(options.env, 'options.env');
2347
+ Object.entries(options.env).forEach(({ 0: key, 1: value }) => {
2348
+ if (value !== undefined) {
2349
+ env.push(`${key}=${value}`);
1821
2350
  }
2351
+ });
2352
+ }
2353
+ const preopens = [];
2354
+ if (options.preopens !== undefined) {
2355
+ validateObject(options.preopens, 'options.preopens');
2356
+ Object.entries(options.preopens).forEach(({ 0: key, 1: value }) => preopens.push({ mappedPath: String(key), realPath: String(value) }));
2357
+ }
2358
+ if (preopens.length > 0) {
2359
+ if (options.fs === undefined) {
2360
+ throw new Error('filesystem is disabled, can not preopen directory');
1822
2361
  }
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
- }
2362
+ try {
2363
+ validateObject(options.fs, 'options.fs');
1842
2364
  }
1843
- this[kSetMemory] = wrap._setMemory;
1844
- delete wrap._setMemory;
2365
+ catch (_) {
2366
+ throw new TypeError('Node.js fs like implementation is not provided');
2367
+ }
2368
+ }
2369
+ // if (options.filesystem !== undefined) {
2370
+ // validateObject(options.filesystem, 'options.filesystem')
2371
+ // validateString(options.filesystem.type, 'options.filesystem.type')
2372
+ // if (options.filesystem.type !== 'memfs' && options.filesystem.type !== 'file-system-access-api') {
2373
+ // 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`)
2374
+ // }
2375
+ // try {
2376
+ // validateObject(options.filesystem.fs, 'options.filesystem.fs')
2377
+ // } catch (_) {
2378
+ // throw new Error('Node.js fs like implementation is not provided')
2379
+ // }
2380
+ // }
2381
+ if (options.print !== undefined)
2382
+ validateFunction(options.print, 'options.print');
2383
+ if (options.printErr !== undefined)
2384
+ validateFunction(options.printErr, 'options.printErr');
2385
+ if (options.returnOnExit !== undefined) {
2386
+ validateBoolean(options.returnOnExit, 'options.returnOnExit');
2387
+ }
2388
+ // const { stdin = 0, stdout = 1, stderr = 2 } = options
2389
+ // validateInt32(stdin, 'options.stdin', 0)
2390
+ // validateInt32(stdout, 'options.stdout', 0)
2391
+ // validateInt32(stderr, 'options.stderr', 0)
2392
+ // const stdio = [stdin, stdout, stderr] as const
2393
+ const stdio = [0, 1, 2];
2394
+ return {
2395
+ args,
2396
+ env,
2397
+ preopens,
2398
+ stdio
2399
+ };
2400
+ }
2401
+ /** @public */
2402
+ class WASI {
2403
+ constructor(setMemory, wrap) {
2404
+ this[kSetMemory] = setMemory;
1845
2405
  this.wasiImport = wrap;
1846
2406
  this[kStarted] = false;
1847
2407
  this[kExitCode] = 0;
1848
2408
  this[kInstance] = undefined;
1849
2409
  }
2410
+ static createSync(options = kEmptyObject) {
2411
+ const { args, env, preopens, stdio } = validateOptions(options);
2412
+ const wrap = WASI$1.createSync(args, env, preopens, stdio, options.fs, options.print, options.printErr);
2413
+ const setMemory = wrap._setMemory;
2414
+ delete wrap._setMemory;
2415
+ const _this = new WASI(setMemory, wrap);
2416
+ if (options.returnOnExit) {
2417
+ wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
2418
+ }
2419
+ return _this;
2420
+ }
2421
+ static async createAsync(options = kEmptyObject) {
2422
+ const { args, env, preopens, stdio } = validateOptions(options);
2423
+ if (options.asyncify !== undefined) {
2424
+ validateObject(options.asyncify, 'options.asyncify');
2425
+ validateFunction(options.asyncify.wrapImportFunction, 'options.asyncify.wrapImportFunction');
2426
+ }
2427
+ const wrap = await WASI$1.createAsync(args, env, preopens, stdio, options.fs, options.print, options.printErr, options.asyncify);
2428
+ const setMemory = wrap._setMemory;
2429
+ delete wrap._setMemory;
2430
+ const _this = new WASI(setMemory, wrap);
2431
+ if (options.returnOnExit) {
2432
+ wrap.proc_exit = wasiReturnOnProcExit.bind(_this);
2433
+ }
2434
+ return _this;
2435
+ }
1850
2436
  // Must not export _initialize, must export _start
1851
2437
  start(instance) {
1852
2438
  if (this[kStarted]) {
@@ -1897,42 +2483,4 @@ function wasiReturnOnProcExit(rval) {
1897
2483
  throw kExitCode;
1898
2484
  }
1899
2485
 
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 };
2486
+ export { Asyncify, Memory, WASI, WebAssemblyMemory, asyncifyLoad, asyncifyLoadSync, extendMemory, load, loadSync, wrapAsyncExport, wrapAsyncImport, wrapExports };