@tybys/wasm-util 0.8.1 → 0.8.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.
@@ -3,7 +3,7 @@ import { resolve } from "./path.mjs";
3
3
  import { WasiErrno, WasiRights, FileControlFlag, WasiFileControlFlag, WasiFdFlag, WasiFileType, WasiClockid, WasiFstFlag, WasiEventType, WasiSubclockflags } from "./types.mjs";
4
4
  import { concatBuffer, toFileStat, AsyncTable, SyncTable } from "./fd.mjs";
5
5
  import { WasiError } from "./error.mjs";
6
- import { isPromiseLike, sleepBreakIf } from "./util.mjs";
6
+ import { isPromiseLike, sleepBreakIf, unsharedSlice } from "./util.mjs";
7
7
  import { getRights } from "./rights.mjs";
8
8
  import { extendMemory } from "../memory.mjs";
9
9
  import { wrapAsyncImport } from "../jspi.mjs";
@@ -636,7 +636,14 @@ export class WASI {
636
636
  return HEAPU8.subarray(buf, buf + bufLen);
637
637
  });
638
638
  let nread = 0;
639
- const buffer = new Uint8Array(totalSize);
639
+ const buffer = (() => {
640
+ try {
641
+ return new Uint8Array(new SharedArrayBuffer(totalSize));
642
+ }
643
+ catch (_) {
644
+ return new Uint8Array(totalSize);
645
+ }
646
+ })();
640
647
  buffer._isBuffer = true;
641
648
  const fs = getFs(this);
642
649
  const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(offset));
@@ -733,7 +740,14 @@ export class WASI {
733
740
  nread = buffer ? copyMemory(ioVecs, buffer) : 0;
734
741
  }
735
742
  else {
736
- buffer = new Uint8Array(totalSize);
743
+ buffer = (() => {
744
+ try {
745
+ return new Uint8Array(new SharedArrayBuffer(totalSize));
746
+ }
747
+ catch (_) {
748
+ return new Uint8Array(totalSize);
749
+ }
750
+ })();
737
751
  buffer._isBuffer = true;
738
752
  const fs = getFs(this);
739
753
  const bytesRead = fs.readSync(fileDescriptor.fd, buffer, 0, buffer.length, Number(fileDescriptor.pos));
@@ -961,7 +975,7 @@ export class WASI {
961
975
  const { HEAPU8 } = getMemory(this);
962
976
  const wasi = _wasi.get(this);
963
977
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
964
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
978
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
965
979
  pathString = resolve(fileDescriptor.realPath, pathString);
966
980
  const fs = getFs(this);
967
981
  fs.mkdirSync(pathString);
@@ -975,7 +989,7 @@ export class WASI {
975
989
  const { HEAPU8 } = getMemory(this);
976
990
  const wasi = _wasi.get(this);
977
991
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
978
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
992
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
979
993
  pathString = resolve(fileDescriptor.realPath, pathString);
980
994
  const fs = getFs(this);
981
995
  await fs.promises.mkdir(pathString);
@@ -991,7 +1005,7 @@ export class WASI {
991
1005
  const { HEAPU8, view } = getMemory(this);
992
1006
  const wasi = _wasi.get(this);
993
1007
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
994
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1008
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
995
1009
  const fs = getFs(this);
996
1010
  pathString = resolve(fileDescriptor.realPath, pathString);
997
1011
  let stat;
@@ -1013,7 +1027,7 @@ export class WASI {
1013
1027
  const { HEAPU8, view } = getMemory(this);
1014
1028
  const wasi = _wasi.get(this);
1015
1029
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
1016
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1030
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1017
1031
  const fs = getFs(this);
1018
1032
  pathString = resolve(fileDescriptor.realPath, pathString);
1019
1033
  let stat;
@@ -1041,7 +1055,7 @@ export class WASI {
1041
1055
  const wasi = _wasi.get(this);
1042
1056
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1043
1057
  const fs = getFs(this);
1044
- const resolvedPath = resolvePathSync(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1058
+ const resolvedPath = resolvePathSync(fs, fileDescriptor, decoder.decode(unsharedSlice(HEAPU8, path, path + path_len)), flags);
1045
1059
  if ((fst_flags & WasiFstFlag.SET_ATIM_NOW) === WasiFstFlag.SET_ATIM_NOW) {
1046
1060
  atim = BigInt(Date.now() * 1000000);
1047
1061
  }
@@ -1065,7 +1079,7 @@ export class WASI {
1065
1079
  const wasi = _wasi.get(this);
1066
1080
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1067
1081
  const fs = getFs(this);
1068
- const resolvedPath = await resolvePathAsync(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1082
+ const resolvedPath = await resolvePathAsync(fs, fileDescriptor, decoder.decode(unsharedSlice(HEAPU8, path, path + path_len)), flags);
1069
1083
  if ((fst_flags & WasiFstFlag.SET_ATIM_NOW) === WasiFstFlag.SET_ATIM_NOW) {
1070
1084
  atim = BigInt(Date.now() * 1000000);
1071
1085
  }
@@ -1095,8 +1109,8 @@ export class WASI {
1095
1109
  }
1096
1110
  const { HEAPU8 } = getMemory(this);
1097
1111
  const fs = getFs(this);
1098
- const resolvedOldPath = resolvePathSync(fs, oldWrap, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)), old_flags);
1099
- const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
1112
+ const resolvedOldPath = resolvePathSync(fs, oldWrap, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)), old_flags);
1113
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1100
1114
  fs.linkSync(resolvedOldPath, resolvedNewPath);
1101
1115
  return WasiErrno.ESUCCESS;
1102
1116
  }, async function path_link(old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
@@ -1119,8 +1133,8 @@ export class WASI {
1119
1133
  }
1120
1134
  const { HEAPU8 } = getMemory(this);
1121
1135
  const fs = getFs(this);
1122
- const resolvedOldPath = await resolvePathAsync(fs, oldWrap, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)), old_flags);
1123
- const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
1136
+ const resolvedOldPath = await resolvePathAsync(fs, oldWrap, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)), old_flags);
1137
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1124
1138
  await fs.promises.link(resolvedOldPath, resolvedNewPath);
1125
1139
  return WasiErrno.ESUCCESS;
1126
1140
  }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
@@ -1185,7 +1199,7 @@ export class WASI {
1185
1199
  const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1186
1200
  const memory = getMemory(this);
1187
1201
  const HEAPU8 = memory.HEAPU8;
1188
- const pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1202
+ const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1189
1203
  const fs = getFs(this);
1190
1204
  const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
1191
1205
  const r = fs.openSync(resolved_path, flagsRes, 0o666);
@@ -1219,7 +1233,7 @@ export class WASI {
1219
1233
  const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1220
1234
  const memory = getMemory(this);
1221
1235
  const HEAPU8 = memory.HEAPU8;
1222
- const pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1236
+ const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1223
1237
  const fs = getFs(this);
1224
1238
  const resolved_path = await resolvePathAsync(fs, fileDescriptor, pathString, dirflags);
1225
1239
  const r = await fs.promises.open(resolved_path, flagsRes, 0o666);
@@ -1252,7 +1266,7 @@ export class WASI {
1252
1266
  const { HEAPU8, view } = getMemory(this);
1253
1267
  const wasi = _wasi.get(this);
1254
1268
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
1255
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1269
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1256
1270
  pathString = resolve(fileDescriptor.realPath, pathString);
1257
1271
  const fs = getFs(this);
1258
1272
  const link = fs.readlinkSync(pathString);
@@ -1276,7 +1290,7 @@ export class WASI {
1276
1290
  const { HEAPU8, view } = getMemory(this);
1277
1291
  const wasi = _wasi.get(this);
1278
1292
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
1279
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1293
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1280
1294
  pathString = resolve(fileDescriptor.realPath, pathString);
1281
1295
  const fs = getFs(this);
1282
1296
  const link = await fs.promises.readlink(pathString);
@@ -1298,7 +1312,7 @@ export class WASI {
1298
1312
  const { HEAPU8 } = getMemory(this);
1299
1313
  const wasi = _wasi.get(this);
1300
1314
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
1301
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1315
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1302
1316
  pathString = resolve(fileDescriptor.realPath, pathString);
1303
1317
  const fs = getFs(this);
1304
1318
  fs.rmdirSync(pathString);
@@ -1312,7 +1326,7 @@ export class WASI {
1312
1326
  const { HEAPU8 } = getMemory(this);
1313
1327
  const wasi = _wasi.get(this);
1314
1328
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
1315
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1329
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1316
1330
  pathString = resolve(fileDescriptor.realPath, pathString);
1317
1331
  const fs = getFs(this);
1318
1332
  await fs.promises.rmdir(pathString);
@@ -1337,8 +1351,8 @@ export class WASI {
1337
1351
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
1338
1352
  }
1339
1353
  const { HEAPU8 } = getMemory(this);
1340
- const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)));
1341
- const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
1354
+ const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)));
1355
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1342
1356
  const fs = getFs(this);
1343
1357
  fs.renameSync(resolvedOldPath, resolvedNewPath);
1344
1358
  return WasiErrno.ESUCCESS;
@@ -1361,8 +1375,8 @@ export class WASI {
1361
1375
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
1362
1376
  }
1363
1377
  const { HEAPU8 } = getMemory(this);
1364
- const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len)));
1365
- const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len)));
1378
+ const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)));
1379
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1366
1380
  const fs = getFs(this);
1367
1381
  await fs.promises.rename(resolvedOldPath, resolvedNewPath);
1368
1382
  return WasiErrno.ESUCCESS;
@@ -1378,8 +1392,8 @@ export class WASI {
1378
1392
  const { HEAPU8 } = getMemory(this);
1379
1393
  const wasi = _wasi.get(this);
1380
1394
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
1381
- const oldPath = decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len));
1382
- let newPath = decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len));
1395
+ const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
1396
+ let newPath = decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len));
1383
1397
  newPath = resolve(fileDescriptor.realPath, newPath);
1384
1398
  const fs = getFs(this);
1385
1399
  fs.symlinkSync(oldPath, newPath);
@@ -1395,8 +1409,8 @@ export class WASI {
1395
1409
  const { HEAPU8 } = getMemory(this);
1396
1410
  const wasi = _wasi.get(this);
1397
1411
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
1398
- const oldPath = decoder.decode(HEAPU8.subarray(old_path, old_path + old_path_len));
1399
- let newPath = decoder.decode(HEAPU8.subarray(new_path, new_path + new_path_len));
1412
+ const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
1413
+ let newPath = decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len));
1400
1414
  newPath = resolve(fileDescriptor.realPath, newPath);
1401
1415
  const fs = getFs(this);
1402
1416
  await fs.promises.symlink(oldPath, newPath);
@@ -1411,7 +1425,7 @@ export class WASI {
1411
1425
  const { HEAPU8 } = getMemory(this);
1412
1426
  const wasi = _wasi.get(this);
1413
1427
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
1414
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1428
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1415
1429
  pathString = resolve(fileDescriptor.realPath, pathString);
1416
1430
  const fs = getFs(this);
1417
1431
  fs.unlinkSync(pathString);
@@ -1425,7 +1439,7 @@ export class WASI {
1425
1439
  const { HEAPU8 } = getMemory(this);
1426
1440
  const wasi = _wasi.get(this);
1427
1441
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
1428
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1442
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1429
1443
  pathString = resolve(fileDescriptor.realPath, pathString);
1430
1444
  const fs = getFs(this);
1431
1445
  await fs.promises.unlink(pathString);
@@ -106,3 +106,8 @@ export function sleepBreakIf(delay, breakIf) {
106
106
  }
107
107
  return ret;
108
108
  }
109
+ export function unsharedSlice(view, start, end) {
110
+ return ((typeof SharedArrayBuffer === 'function' && view.buffer instanceof SharedArrayBuffer) || (Object.prototype.toString.call(view.buffer.constructor) === '[object SharedArrayBuffer]'))
111
+ ? view.slice(start, end)
112
+ : view.subarray(start, end);
113
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tybys/wasm-util",
3
- "version": "0.8.1",
3
+ "version": "0.8.3",
4
4
  "description": "WASI polyfill for browser and some wasm util",
5
5
  "main": "./lib/cjs/index.js",
6
6
  "module": "./dist/wasm-util.esm-bundler.js",