@tybys/wasm-util 0.10.1 → 0.10.3

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.
@@ -306,6 +306,141 @@ function asyncifyLoadSync(asyncify, buffer, imports) {
306
306
 
307
307
  const CHAR_DOT = 46; /* . */
308
308
  const CHAR_FORWARD_SLASH = 47; /* / */
309
+ const CHAR_BACKWARD_SLASH = 92; /* \ */
310
+ const CHAR_COLON = 58; /* : */
311
+ const CHAR_UPPERCASE_A = 65; /* A */
312
+ const CHAR_UPPERCASE_Z = 90; /* Z */
313
+ const CHAR_LOWERCASE_A = 97; /* a */
314
+ const CHAR_LOWERCASE_Z = 122; /* z */
315
+ function isPathSeparatorWin(code) {
316
+ return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
317
+ }
318
+ function isWindowsDeviceRoot(code) {
319
+ return (code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) ||
320
+ (code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z);
321
+ }
322
+ const _isWin32 = typeof process !== 'undefined' && process.platform === 'win32';
323
+ /**
324
+ * Windows variant of `resolve()`. Mirrors Node's `path.win32.resolve`
325
+ * semantics enough for the WASI shim's needs (drive-letter absolutes
326
+ * + UNC paths + per-drive cwd lookups via `process.env`/`process.cwd`).
327
+ *
328
+ * Why this is needed: on Windows, `FileDescriptor.realPath` is the
329
+ * host realpath returned by `fs.realpathSync(realPath, 'utf8')` — the
330
+ * backslash form `D:\…`. The POSIX-only `resolveImpl` below only
331
+ * treats `/` as a separator, reads `D` as non-`/`, decides realPath
332
+ * is "relative", and produces a garbage joined path. Downstream
333
+ * `fs.openSync(garbage)` then returns `EINVAL` and the WASI caller
334
+ * sees a permanent failure. This function gives us the correct
335
+ * Windows-style resolution without taking a Node-only `path` import
336
+ * (which would break browser bundles of this package).
337
+ *
338
+ * Cribbed from Node's `lib/path.js` `win32.resolve()` (MIT-licensed),
339
+ * trimmed to what WASI realpath resolution actually exercises.
340
+ */
341
+ function resolveWin32(args) {
342
+ let resolvedDevice = '';
343
+ let resolvedTail = '';
344
+ let resolvedAbsolute = false;
345
+ for (let i = args.length - 1; i >= -1; i--) {
346
+ let path;
347
+ if (i >= 0) {
348
+ path = args[i];
349
+ validateString(path, 'path');
350
+ if (path.length === 0)
351
+ continue;
352
+ }
353
+ else if (resolvedDevice.length === 0) {
354
+ path = (typeof process !== 'undefined' && typeof process.cwd === 'function')
355
+ ? process.cwd()
356
+ : '';
357
+ }
358
+ else {
359
+ // Look up per-drive cwd via the `=X:` env var convention; fall back
360
+ // to the global cwd if absent.
361
+ const envKey = `=${resolvedDevice}`;
362
+ const env = (typeof process !== 'undefined') ? process.env : undefined;
363
+ path = (env && typeof env[envKey] === 'string')
364
+ ? env[envKey]
365
+ : (typeof process !== 'undefined' && typeof process.cwd === 'function')
366
+ ? process.cwd()
367
+ : '';
368
+ if (path === undefined ||
369
+ (path.slice(0, 2).toLowerCase() !== resolvedDevice.toLowerCase() &&
370
+ path.charCodeAt(2) === CHAR_BACKWARD_SLASH)) {
371
+ path = `${resolvedDevice}\\`;
372
+ }
373
+ }
374
+ const len = path.length;
375
+ let rootEnd = 0;
376
+ let device = '';
377
+ let isAbsolute = false;
378
+ const code = path.charCodeAt(0);
379
+ if (len === 1) {
380
+ if (isPathSeparatorWin(code)) {
381
+ rootEnd = 1;
382
+ isAbsolute = true;
383
+ }
384
+ }
385
+ else if (isPathSeparatorWin(code)) {
386
+ isAbsolute = true;
387
+ if (isPathSeparatorWin(path.charCodeAt(1))) {
388
+ // UNC path: `\\server\share\…`
389
+ let j = 2;
390
+ let last = j;
391
+ while (j < len && !isPathSeparatorWin(path.charCodeAt(j)))
392
+ j++;
393
+ if (j < len && j !== last) {
394
+ const firstPart = path.slice(last, j);
395
+ last = j;
396
+ while (j < len && isPathSeparatorWin(path.charCodeAt(j)))
397
+ j++;
398
+ if (j < len && j !== last) {
399
+ last = j;
400
+ while (j < len && !isPathSeparatorWin(path.charCodeAt(j)))
401
+ j++;
402
+ if (j === len || j !== last) {
403
+ device = `\\\\${firstPart}\\${path.slice(last, j)}`;
404
+ rootEnd = j;
405
+ }
406
+ }
407
+ }
408
+ }
409
+ else {
410
+ rootEnd = 1;
411
+ }
412
+ }
413
+ else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) {
414
+ device = path.slice(0, 2);
415
+ rootEnd = 2;
416
+ if (len > 2 && isPathSeparatorWin(path.charCodeAt(2))) {
417
+ isAbsolute = true;
418
+ rootEnd = 3;
419
+ }
420
+ }
421
+ if (device.length > 0) {
422
+ if (resolvedDevice.length > 0) {
423
+ if (device.toLowerCase() !== resolvedDevice.toLowerCase())
424
+ continue;
425
+ }
426
+ else {
427
+ resolvedDevice = device;
428
+ }
429
+ }
430
+ if (resolvedAbsolute) {
431
+ if (resolvedDevice.length > 0)
432
+ break;
433
+ }
434
+ else {
435
+ resolvedTail = `${path.slice(rootEnd)}\\${resolvedTail}`;
436
+ resolvedAbsolute = isAbsolute;
437
+ if (isAbsolute && resolvedDevice.length > 0)
438
+ break;
439
+ }
440
+ }
441
+ resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\', isPathSeparatorWin);
442
+ return resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail || '.';
443
+ }
309
444
  function isPosixPathSeparator(code) {
310
445
  return code === CHAR_FORWARD_SLASH;
311
446
  }
@@ -381,6 +516,13 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
381
516
  return res;
382
517
  }
383
518
  function resolve(...args) {
519
+ // On Windows, host paths are `D:\…` style. The POSIX-only resolver
520
+ // below treats `D` as a non-`/` character and produces garbage; route
521
+ // to the Windows-aware variant. POSIX hosts (Linux/macOS/browser)
522
+ // run the original code path unchanged — `_isWin32` is constant-folded
523
+ // away on those targets.
524
+ if (_isWin32)
525
+ return resolveWin32(args);
384
526
  let resolvedPath = '';
385
527
  let resolvedAbsolute = false;
386
528
  for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
@@ -1017,6 +1159,26 @@ function wrapExports(exports, needWrap) {
1017
1159
  });
1018
1160
  }
1019
1161
 
1162
+ // Linux fcntl flag bits ↔ Windows libuv flag bits. `pathOpen()` builds
1163
+ // `flagsRes` using Linux constants (O_CREAT=0x40, O_EXCL=0x80,
1164
+ // O_APPEND=0x400). Windows libuv expects different bits (O_CREAT=0x100,
1165
+ // O_EXCL=0x400, O_APPEND=0x8). Without translation,
1166
+ // `fs.openSync(path, 0x241 /* L:WRONLY|CREAT|TRUNC */)` is rejected
1167
+ // with EINVAL because Linux's 0x40 happens to mean O_TEMPORARY on
1168
+ // Windows — and O_TEMPORARY without O_CREAT is invalid.
1169
+ const _isWin32Flags = typeof process !== 'undefined' && process.platform === 'win32';
1170
+ function _toWinOpenFlags(f) {
1171
+ let r = f & 3; // RDONLY/WRONLY/RDWR — same on both
1172
+ if ((f & 0x40) !== 0)
1173
+ r |= 0x100; // O_CREAT: Linux 0x40 -> Windows 0x100
1174
+ if ((f & 0x80) !== 0)
1175
+ r |= 0x400; // O_EXCL: Linux 0x80 -> Windows 0x400
1176
+ if ((f & 0x200) !== 0)
1177
+ r |= 0x200; // O_TRUNC: same value on both
1178
+ if ((f & 0x400) !== 0)
1179
+ r |= 0x8; // O_APPEND: Linux 0x400 -> Windows 0x8
1180
+ return r;
1181
+ }
1020
1182
  function copyMemory(targets, src) {
1021
1183
  if (targets.length === 0 || src.length === 0)
1022
1184
  return 0;
@@ -1039,8 +1201,18 @@ function copyMemory(targets, src) {
1039
1201
  const _memory = new WeakMap();
1040
1202
  const _wasi = new WeakMap();
1041
1203
  const _fs = new WeakMap();
1042
- function getMemory(wasi) {
1043
- return _memory.get(wasi);
1204
+ function ensureMemoryFor(memory, end) {
1205
+ let buffer = memory.buffer;
1206
+ if (end > buffer.byteLength) {
1207
+ memory.grow(0);
1208
+ buffer = memory.buffer;
1209
+ }
1210
+ return buffer;
1211
+ }
1212
+ function getMemory(wasi, end = 0) {
1213
+ const memory = _memory.get(wasi);
1214
+ ensureMemoryFor(memory, end);
1215
+ return memory;
1044
1216
  }
1045
1217
  function getFs(wasi) {
1046
1218
  const fs = _fs.get(wasi);
@@ -1169,9 +1341,10 @@ class WASI$1 {
1169
1341
  if (argv === 0 || argv_buf === 0) {
1170
1342
  return 28 /* WasiErrno.EINVAL */;
1171
1343
  }
1172
- const { HEAPU8, view } = getMemory(this);
1173
1344
  const wasi = _wasi.get(this);
1174
1345
  const args = wasi.args;
1346
+ const argsSize = encoder.encode(args.join('\0') + '\0').length;
1347
+ const { HEAPU8, view } = getMemory(this, Math.max(argv + args.length * 4, argv_buf + argsSize));
1175
1348
  for (let i = 0; i < args.length; ++i) {
1176
1349
  const arg = args[i];
1177
1350
  view.setInt32(argv, argv_buf, true);
@@ -1188,7 +1361,7 @@ class WASI$1 {
1188
1361
  if (argc === 0 || argv_buf_size === 0) {
1189
1362
  return 28 /* WasiErrno.EINVAL */;
1190
1363
  }
1191
- const { view } = getMemory(this);
1364
+ const { view } = getMemory(this, Math.max(argc + 4, argv_buf_size + 4));
1192
1365
  const wasi = _wasi.get(this);
1193
1366
  const args = wasi.args;
1194
1367
  view.setUint32(argc, args.length, true);
@@ -1201,9 +1374,10 @@ class WASI$1 {
1201
1374
  if (environ === 0 || environ_buf === 0) {
1202
1375
  return 28 /* WasiErrno.EINVAL */;
1203
1376
  }
1204
- const { HEAPU8, view } = getMemory(this);
1205
1377
  const wasi = _wasi.get(this);
1206
1378
  const env = wasi.env;
1379
+ const envSize = encoder.encode(env.join('\0') + '\0').length;
1380
+ const { HEAPU8, view } = getMemory(this, Math.max(environ + env.length * 4, environ_buf + envSize));
1207
1381
  for (let i = 0; i < env.length; ++i) {
1208
1382
  const pair = env[i];
1209
1383
  view.setInt32(environ, environ_buf, true);
@@ -1220,7 +1394,7 @@ class WASI$1 {
1220
1394
  if (len === 0 || buflen === 0) {
1221
1395
  return 28 /* WasiErrno.EINVAL */;
1222
1396
  }
1223
- const { view } = getMemory(this);
1397
+ const { view } = getMemory(this, Math.max(len + 4, buflen + 4));
1224
1398
  const wasi = _wasi.get(this);
1225
1399
  view.setUint32(len, wasi.env.length, true);
1226
1400
  view.setUint32(buflen, encoder.encode(wasi.env.join('\0') + '\0').length, true);
@@ -1231,7 +1405,7 @@ class WASI$1 {
1231
1405
  if (resolution === 0) {
1232
1406
  return 28 /* WasiErrno.EINVAL */;
1233
1407
  }
1234
- const { view } = getMemory(this);
1408
+ const { view } = getMemory(this, resolution + 8);
1235
1409
  switch (id) {
1236
1410
  case 0 /* WasiClockid.REALTIME */:
1237
1411
  view.setBigUint64(resolution, BigInt(1000000), true);
@@ -1249,7 +1423,7 @@ class WASI$1 {
1249
1423
  if (time === 0) {
1250
1424
  return 28 /* WasiErrno.EINVAL */;
1251
1425
  }
1252
- const { view } = getMemory(this);
1426
+ const { view } = getMemory(this, time + 8);
1253
1427
  switch (id) {
1254
1428
  case 0 /* WasiClockid.REALTIME */:
1255
1429
  view.setBigUint64(time, BigInt(Date.now()) * BigInt(1000000), true);
@@ -1277,7 +1451,7 @@ class WASI$1 {
1277
1451
  }
1278
1452
  const wasi = _wasi.get(this);
1279
1453
  const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
1280
- const { view } = getMemory(this);
1454
+ const { view } = getMemory(this, fdstat + 24);
1281
1455
  view.setUint16(fdstat, fileDescriptor.type, true);
1282
1456
  view.setUint16(fdstat + 2, 0, true);
1283
1457
  view.setBigUint64(fdstat + 8, fileDescriptor.rightsBase, true);
@@ -1318,7 +1492,7 @@ class WASI$1 {
1318
1492
  }
1319
1493
  if (fileDescriptor.preopen !== 1)
1320
1494
  return 28 /* WasiErrno.EINVAL */;
1321
- const { view } = getMemory(this);
1495
+ const { view } = getMemory(this, prestat + 8);
1322
1496
  // preopen type is dir(0)
1323
1497
  view.setUint32(prestat, 0, true);
1324
1498
  view.setUint32(prestat + 4, encoder.encode(fileDescriptor.path).length, true);
@@ -1338,7 +1512,7 @@ class WASI$1 {
1338
1512
  const size = buffer.length;
1339
1513
  if (size > path_len)
1340
1514
  return 42 /* WasiErrno.ENOBUFS */;
1341
- const { HEAPU8 } = getMemory(this);
1515
+ const { HEAPU8 } = getMemory(this, path + size);
1342
1516
  HEAPU8.set(buffer, path);
1343
1517
  return 0 /* WasiErrno.ESUCCESS */;
1344
1518
  });
@@ -1352,7 +1526,7 @@ class WASI$1 {
1352
1526
  const wasi = _wasi.get(this);
1353
1527
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SEEK, BigInt(0));
1354
1528
  const r = fileDescriptor.seek(offset, whence);
1355
- const { view } = getMemory(this);
1529
+ const { view } = getMemory(this, newOffset + 8);
1356
1530
  view.setBigUint64(newOffset, r, true);
1357
1531
  return 0 /* WasiErrno.ESUCCESS */;
1358
1532
  });
@@ -1360,7 +1534,7 @@ class WASI$1 {
1360
1534
  const wasi = _wasi.get(this);
1361
1535
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_TELL, BigInt(0));
1362
1536
  const pos = BigInt(fileDescriptor.pos);
1363
- const { view } = getMemory(this);
1537
+ const { view } = getMemory(this, Number(offset) + 8);
1364
1538
  view.setBigUint64(Number(offset), pos, true);
1365
1539
  return 0 /* WasiErrno.ESUCCESS */;
1366
1540
  });
@@ -1373,7 +1547,7 @@ class WASI$1 {
1373
1547
  if (in_ptr === 0 || out_ptr === 0 || nsubscriptions === 0 || nevents === 0) {
1374
1548
  return 28 /* WasiErrno.EINVAL */;
1375
1549
  }
1376
- const { view } = getMemory(this);
1550
+ const { view } = getMemory(this, Math.max(in_ptr + nsubscriptions * 48, out_ptr + nsubscriptions * 32, nevents + 4));
1377
1551
  view.setUint32(nevents, 0, true);
1378
1552
  let i = 0;
1379
1553
  let timer_userdata = BigInt(0);
@@ -1492,7 +1666,7 @@ class WASI$1 {
1492
1666
  return 28 /* WasiErrno.EINVAL */;
1493
1667
  }
1494
1668
  buf_len = Number(buf_len);
1495
- const { HEAPU8, view } = getMemory(this);
1669
+ const { HEAPU8, view } = getMemory(this, buf + buf_len);
1496
1670
  if ((typeof SharedArrayBuffer === 'function' && HEAPU8.buffer instanceof SharedArrayBuffer) ||
1497
1671
  (Object.prototype.toString.call(HEAPU8.buffer) === '[object SharedArrayBuffer]')) {
1498
1672
  for (let i = buf; i < buf + buf_len; ++i) {
@@ -1514,7 +1688,7 @@ class WASI$1 {
1514
1688
  return 28 /* WasiErrno.EINVAL */;
1515
1689
  }
1516
1690
  buf_len = Number(buf_len);
1517
- const { view } = getMemory(this);
1691
+ const { view } = getMemory(this, buf + buf_len);
1518
1692
  for (let i = buf; i < buf + buf_len; ++i) {
1519
1693
  view.setUint8(i, Math.floor(Math.random() * 256));
1520
1694
  }
@@ -1607,7 +1781,7 @@ class WASI$1 {
1607
1781
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
1608
1782
  const fs = getFs(this);
1609
1783
  const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
1610
- const { view } = getMemory(this);
1784
+ const { view } = getMemory(this, buf + 64);
1611
1785
  toFileStat(view, buf, stat);
1612
1786
  return 0 /* WasiErrno.ESUCCESS */;
1613
1787
  }, async function fd_filestat_get(fd, buf) {
@@ -1618,7 +1792,7 @@ class WASI$1 {
1618
1792
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
1619
1793
  const h = fileDescriptor.fd;
1620
1794
  const stat = await h.stat({ bigint: true });
1621
- const { view } = getMemory(this);
1795
+ const { view } = getMemory(this, buf + 64);
1622
1796
  toFileStat(view, buf, stat);
1623
1797
  return 0 /* WasiErrno.ESUCCESS */;
1624
1798
  }, ['i32', 'i32'], ['i32']);
@@ -1669,7 +1843,7 @@ class WASI$1 {
1669
1843
  if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
1670
1844
  return 28 /* WasiErrno.EINVAL */;
1671
1845
  }
1672
- const { HEAPU8, view } = getMemory(this);
1846
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1673
1847
  const wasi = _wasi.get(this);
1674
1848
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
1675
1849
  if (!iovslen) {
@@ -1705,7 +1879,7 @@ class WASI$1 {
1705
1879
  if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
1706
1880
  return 28 /* WasiErrno.EINVAL */;
1707
1881
  }
1708
- const { HEAPU8, view } = getMemory(this);
1882
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1709
1883
  const wasi = _wasi.get(this);
1710
1884
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
1711
1885
  if (!iovslen) {
@@ -1734,7 +1908,7 @@ class WASI$1 {
1734
1908
  if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
1735
1909
  return 28 /* WasiErrno.EINVAL */;
1736
1910
  }
1737
- const { HEAPU8, view } = getMemory(this);
1911
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1738
1912
  const wasi = _wasi.get(this);
1739
1913
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
1740
1914
  if (!iovslen) {
@@ -1757,7 +1931,7 @@ class WASI$1 {
1757
1931
  if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
1758
1932
  return 28 /* WasiErrno.EINVAL */;
1759
1933
  }
1760
- const { HEAPU8, view } = getMemory(this);
1934
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1761
1935
  const wasi = _wasi.get(this);
1762
1936
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
1763
1937
  if (!iovslen) {
@@ -1780,7 +1954,7 @@ class WASI$1 {
1780
1954
  if ((iovs === 0 && iovslen) || size === 0) {
1781
1955
  return 28 /* WasiErrno.EINVAL */;
1782
1956
  }
1783
- const { HEAPU8, view } = getMemory(this);
1957
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1784
1958
  const wasi = _wasi.get(this);
1785
1959
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
1786
1960
  if (!iovslen) {
@@ -1827,7 +2001,7 @@ class WASI$1 {
1827
2001
  if ((iovs === 0 && iovslen) || size === 0) {
1828
2002
  return 28 /* WasiErrno.EINVAL */;
1829
2003
  }
1830
- const { HEAPU8, view } = getMemory(this);
2004
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1831
2005
  const wasi = _wasi.get(this);
1832
2006
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
1833
2007
  if (!iovslen) {
@@ -1871,7 +2045,7 @@ class WASI$1 {
1871
2045
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READDIR, BigInt(0));
1872
2046
  const fs = getFs(this);
1873
2047
  const entries = fs.readdirSync(fileDescriptor.realPath, { withFileTypes: true });
1874
- const { HEAPU8, view } = getMemory(this);
2048
+ const { HEAPU8, view } = getMemory(this, Math.max(buf + buf_len, bufused + 4));
1875
2049
  let bufferUsed = 0;
1876
2050
  for (let i = Number(cookie); i < entries.length; i++) {
1877
2051
  const nameData = encoder.encode(entries[i].name);
@@ -1921,7 +2095,7 @@ class WASI$1 {
1921
2095
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READDIR, BigInt(0));
1922
2096
  const fs = getFs(this);
1923
2097
  const entries = await fs.promises.readdir(fileDescriptor.realPath, { withFileTypes: true });
1924
- const { HEAPU8, view } = getMemory(this);
2098
+ const { HEAPU8, view } = getMemory(this, Math.max(buf + buf_len, bufused + 4));
1925
2099
  let bufferUsed = 0;
1926
2100
  for (let i = Number(cookie); i < entries.length; i++) {
1927
2101
  const nameData = encoder.encode(entries[i].name);
@@ -1989,7 +2163,7 @@ class WASI$1 {
1989
2163
  if ((iovs === 0 && iovslen) || size === 0) {
1990
2164
  return 28 /* WasiErrno.EINVAL */;
1991
2165
  }
1992
- const { HEAPU8, view } = getMemory(this);
2166
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1993
2167
  const wasi = _wasi.get(this);
1994
2168
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
1995
2169
  if (!iovslen) {
@@ -2019,7 +2193,7 @@ class WASI$1 {
2019
2193
  if ((iovs === 0 && iovslen) || size === 0) {
2020
2194
  return 28 /* WasiErrno.EINVAL */;
2021
2195
  }
2022
- const { HEAPU8, view } = getMemory(this);
2196
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
2023
2197
  const wasi = _wasi.get(this);
2024
2198
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
2025
2199
  if (!iovslen) {
@@ -2049,7 +2223,7 @@ class WASI$1 {
2049
2223
  if (path === 0) {
2050
2224
  return 28 /* WasiErrno.EINVAL */;
2051
2225
  }
2052
- const { HEAPU8 } = getMemory(this);
2226
+ const { HEAPU8 } = getMemory(this, path + path_len);
2053
2227
  const wasi = _wasi.get(this);
2054
2228
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
2055
2229
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2063,7 +2237,7 @@ class WASI$1 {
2063
2237
  if (path === 0) {
2064
2238
  return 28 /* WasiErrno.EINVAL */;
2065
2239
  }
2066
- const { HEAPU8 } = getMemory(this);
2240
+ const { HEAPU8 } = getMemory(this, path + path_len);
2067
2241
  const wasi = _wasi.get(this);
2068
2242
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
2069
2243
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2079,7 +2253,7 @@ class WASI$1 {
2079
2253
  if (path === 0 || filestat === 0) {
2080
2254
  return 28 /* WasiErrno.EINVAL */;
2081
2255
  }
2082
- const { HEAPU8, view } = getMemory(this);
2256
+ const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, filestat + 64));
2083
2257
  const wasi = _wasi.get(this);
2084
2258
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
2085
2259
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2101,7 +2275,7 @@ class WASI$1 {
2101
2275
  if (path === 0 || filestat === 0) {
2102
2276
  return 28 /* WasiErrno.EINVAL */;
2103
2277
  }
2104
- const { HEAPU8, view } = getMemory(this);
2278
+ const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, filestat + 64));
2105
2279
  const wasi = _wasi.get(this);
2106
2280
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
2107
2281
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2122,7 +2296,7 @@ class WASI$1 {
2122
2296
  path_len = Number(path_len);
2123
2297
  if (path === 0)
2124
2298
  return 28 /* WasiErrno.EINVAL */;
2125
- const { HEAPU8 } = getMemory(this);
2299
+ const { HEAPU8 } = getMemory(this, path + path_len);
2126
2300
  const wasi = _wasi.get(this);
2127
2301
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
2128
2302
  if (validateFstFlagsOrReturn(fst_flags)) {
@@ -2143,7 +2317,7 @@ class WASI$1 {
2143
2317
  path_len = Number(path_len);
2144
2318
  if (path === 0)
2145
2319
  return 28 /* WasiErrno.EINVAL */;
2146
- const { HEAPU8 } = getMemory(this);
2320
+ const { HEAPU8 } = getMemory(this, path + path_len);
2147
2321
  const wasi = _wasi.get(this);
2148
2322
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
2149
2323
  if (validateFstFlagsOrReturn(fst_flags)) {
@@ -2178,7 +2352,7 @@ class WASI$1 {
2178
2352
  oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE, BigInt(0));
2179
2353
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_LINK_TARGET, BigInt(0));
2180
2354
  }
2181
- const { HEAPU8 } = getMemory(this);
2355
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
2182
2356
  const fs = getFs(this);
2183
2357
  const resolvedOldPath = resolvePathSync(fs, oldWrap, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)), old_flags);
2184
2358
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
@@ -2202,7 +2376,7 @@ class WASI$1 {
2202
2376
  oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE, BigInt(0));
2203
2377
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_LINK_TARGET, BigInt(0));
2204
2378
  }
2205
- const { HEAPU8 } = getMemory(this);
2379
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
2206
2380
  const fs = getFs(this);
2207
2381
  const resolvedOldPath = await resolvePathAsync(fs, oldWrap, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)), old_flags);
2208
2382
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
@@ -2268,12 +2442,12 @@ class WASI$1 {
2268
2442
  const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
2269
2443
  const wasi = _wasi.get(this);
2270
2444
  const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
2271
- const memory = getMemory(this);
2445
+ const memory = getMemory(this, Math.max(path + path_len, fd + 4));
2272
2446
  const HEAPU8 = memory.HEAPU8;
2273
2447
  const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
2274
2448
  const fs = getFs(this);
2275
2449
  const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
2276
- const r = fs.openSync(resolved_path, flagsRes, 0o666);
2450
+ const r = fs.openSync(resolved_path, _isWin32Flags ? _toWinOpenFlags(flagsRes) : flagsRes, 0o666);
2277
2451
  const filetype = wasi.fds.getFileTypeByFd(r);
2278
2452
  if ((filetype !== 3 /* WasiFileType.DIRECTORY */) &&
2279
2453
  ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 ||
@@ -2304,12 +2478,12 @@ class WASI$1 {
2304
2478
  const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
2305
2479
  const wasi = _wasi.get(this);
2306
2480
  const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
2307
- const memory = getMemory(this);
2481
+ const memory = getMemory(this, Math.max(path + path_len, fd + 4));
2308
2482
  const HEAPU8 = memory.HEAPU8;
2309
2483
  const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
2310
2484
  const fs = getFs(this);
2311
2485
  const resolved_path = await resolvePathAsync(fs, fileDescriptor, pathString, dirflags);
2312
- const r = await fs.promises.open(resolved_path, flagsRes, 0o666);
2486
+ const r = await fs.promises.open(resolved_path, _isWin32Flags ? _toWinOpenFlags(flagsRes) : flagsRes, 0o666);
2313
2487
  const filetype = await wasi.fds.getFileTypeByFd(r);
2314
2488
  if ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 && filetype !== 3 /* WasiFileType.DIRECTORY */) {
2315
2489
  return 54 /* WasiErrno.ENOTDIR */;
@@ -2336,7 +2510,7 @@ class WASI$1 {
2336
2510
  if (path === 0 || buf === 0 || bufused === 0) {
2337
2511
  return 28 /* WasiErrno.EINVAL */;
2338
2512
  }
2339
- const { HEAPU8, view } = getMemory(this);
2513
+ const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, buf + buf_len, bufused + 4));
2340
2514
  const wasi = _wasi.get(this);
2341
2515
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
2342
2516
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2360,7 +2534,7 @@ class WASI$1 {
2360
2534
  if (path === 0 || buf === 0 || bufused === 0) {
2361
2535
  return 28 /* WasiErrno.EINVAL */;
2362
2536
  }
2363
- const { HEAPU8, view } = getMemory(this);
2537
+ const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, buf + buf_len, bufused + 4));
2364
2538
  const wasi = _wasi.get(this);
2365
2539
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
2366
2540
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2382,7 +2556,7 @@ class WASI$1 {
2382
2556
  if (path === 0) {
2383
2557
  return 28 /* WasiErrno.EINVAL */;
2384
2558
  }
2385
- const { HEAPU8 } = getMemory(this);
2559
+ const { HEAPU8 } = getMemory(this, path + path_len);
2386
2560
  const wasi = _wasi.get(this);
2387
2561
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
2388
2562
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2396,7 +2570,7 @@ class WASI$1 {
2396
2570
  if (path === 0) {
2397
2571
  return 28 /* WasiErrno.EINVAL */;
2398
2572
  }
2399
- const { HEAPU8 } = getMemory(this);
2573
+ const { HEAPU8 } = getMemory(this, path + path_len);
2400
2574
  const wasi = _wasi.get(this);
2401
2575
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
2402
2576
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2423,7 +2597,7 @@ class WASI$1 {
2423
2597
  oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE, BigInt(0));
2424
2598
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
2425
2599
  }
2426
- const { HEAPU8 } = getMemory(this);
2600
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
2427
2601
  const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)));
2428
2602
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
2429
2603
  const fs = getFs(this);
@@ -2447,7 +2621,7 @@ class WASI$1 {
2447
2621
  oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE, BigInt(0));
2448
2622
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
2449
2623
  }
2450
- const { HEAPU8 } = getMemory(this);
2624
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
2451
2625
  const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)));
2452
2626
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
2453
2627
  const fs = getFs(this);
@@ -2462,7 +2636,7 @@ class WASI$1 {
2462
2636
  if (old_path === 0 || new_path === 0) {
2463
2637
  return 28 /* WasiErrno.EINVAL */;
2464
2638
  }
2465
- const { HEAPU8 } = getMemory(this);
2639
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
2466
2640
  const wasi = _wasi.get(this);
2467
2641
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
2468
2642
  const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
@@ -2482,7 +2656,7 @@ class WASI$1 {
2482
2656
  if (old_path === 0 || new_path === 0) {
2483
2657
  return 28 /* WasiErrno.EINVAL */;
2484
2658
  }
2485
- const { HEAPU8 } = getMemory(this);
2659
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
2486
2660
  const wasi = _wasi.get(this);
2487
2661
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
2488
2662
  const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
@@ -2498,7 +2672,7 @@ class WASI$1 {
2498
2672
  if (path === 0) {
2499
2673
  return 28 /* WasiErrno.EINVAL */;
2500
2674
  }
2501
- const { HEAPU8 } = getMemory(this);
2675
+ const { HEAPU8 } = getMemory(this, path + path_len);
2502
2676
  const wasi = _wasi.get(this);
2503
2677
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
2504
2678
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2512,7 +2686,7 @@ class WASI$1 {
2512
2686
  if (path === 0) {
2513
2687
  return 28 /* WasiErrno.EINVAL */;
2514
2688
  }
2515
- const { HEAPU8 } = getMemory(this);
2689
+ const { HEAPU8 } = getMemory(this, path + path_len);
2516
2690
  const wasi = _wasi.get(this);
2517
2691
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
2518
2692
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));