@tybys/wasm-util 0.8.1 → 0.8.2

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";
@@ -961,7 +961,7 @@ export class WASI {
961
961
  const { HEAPU8 } = getMemory(this);
962
962
  const wasi = _wasi.get(this);
963
963
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
964
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
964
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
965
965
  pathString = resolve(fileDescriptor.realPath, pathString);
966
966
  const fs = getFs(this);
967
967
  fs.mkdirSync(pathString);
@@ -975,7 +975,7 @@ export class WASI {
975
975
  const { HEAPU8 } = getMemory(this);
976
976
  const wasi = _wasi.get(this);
977
977
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
978
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
978
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
979
979
  pathString = resolve(fileDescriptor.realPath, pathString);
980
980
  const fs = getFs(this);
981
981
  await fs.promises.mkdir(pathString);
@@ -991,7 +991,7 @@ export class WASI {
991
991
  const { HEAPU8, view } = getMemory(this);
992
992
  const wasi = _wasi.get(this);
993
993
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
994
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
994
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
995
995
  const fs = getFs(this);
996
996
  pathString = resolve(fileDescriptor.realPath, pathString);
997
997
  let stat;
@@ -1013,7 +1013,7 @@ export class WASI {
1013
1013
  const { HEAPU8, view } = getMemory(this);
1014
1014
  const wasi = _wasi.get(this);
1015
1015
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
1016
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1016
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1017
1017
  const fs = getFs(this);
1018
1018
  pathString = resolve(fileDescriptor.realPath, pathString);
1019
1019
  let stat;
@@ -1041,7 +1041,7 @@ export class WASI {
1041
1041
  const wasi = _wasi.get(this);
1042
1042
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1043
1043
  const fs = getFs(this);
1044
- const resolvedPath = resolvePathSync(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1044
+ const resolvedPath = resolvePathSync(fs, fileDescriptor, decoder.decode(unsharedSlice(HEAPU8, path, path + path_len)), flags);
1045
1045
  if ((fst_flags & WasiFstFlag.SET_ATIM_NOW) === WasiFstFlag.SET_ATIM_NOW) {
1046
1046
  atim = BigInt(Date.now() * 1000000);
1047
1047
  }
@@ -1065,7 +1065,7 @@ export class WASI {
1065
1065
  const wasi = _wasi.get(this);
1066
1066
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1067
1067
  const fs = getFs(this);
1068
- const resolvedPath = await resolvePathAsync(fs, fileDescriptor, decoder.decode(HEAPU8.subarray(path, path + path_len)), flags);
1068
+ const resolvedPath = await resolvePathAsync(fs, fileDescriptor, decoder.decode(unsharedSlice(HEAPU8, path, path + path_len)), flags);
1069
1069
  if ((fst_flags & WasiFstFlag.SET_ATIM_NOW) === WasiFstFlag.SET_ATIM_NOW) {
1070
1070
  atim = BigInt(Date.now() * 1000000);
1071
1071
  }
@@ -1095,8 +1095,8 @@ export class WASI {
1095
1095
  }
1096
1096
  const { HEAPU8 } = getMemory(this);
1097
1097
  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)));
1098
+ const resolvedOldPath = resolvePathSync(fs, oldWrap, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)), old_flags);
1099
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1100
1100
  fs.linkSync(resolvedOldPath, resolvedNewPath);
1101
1101
  return WasiErrno.ESUCCESS;
1102
1102
  }, async function path_link(old_fd, old_flags, old_path, old_path_len, new_fd, new_path, new_path_len) {
@@ -1119,8 +1119,8 @@ export class WASI {
1119
1119
  }
1120
1120
  const { HEAPU8 } = getMemory(this);
1121
1121
  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)));
1122
+ const resolvedOldPath = await resolvePathAsync(fs, oldWrap, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)), old_flags);
1123
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1124
1124
  await fs.promises.link(resolvedOldPath, resolvedNewPath);
1125
1125
  return WasiErrno.ESUCCESS;
1126
1126
  }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
@@ -1185,7 +1185,7 @@ export class WASI {
1185
1185
  const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1186
1186
  const memory = getMemory(this);
1187
1187
  const HEAPU8 = memory.HEAPU8;
1188
- const pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1188
+ const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1189
1189
  const fs = getFs(this);
1190
1190
  const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
1191
1191
  const r = fs.openSync(resolved_path, flagsRes, 0o666);
@@ -1219,7 +1219,7 @@ export class WASI {
1219
1219
  const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1220
1220
  const memory = getMemory(this);
1221
1221
  const HEAPU8 = memory.HEAPU8;
1222
- const pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1222
+ const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1223
1223
  const fs = getFs(this);
1224
1224
  const resolved_path = await resolvePathAsync(fs, fileDescriptor, pathString, dirflags);
1225
1225
  const r = await fs.promises.open(resolved_path, flagsRes, 0o666);
@@ -1252,7 +1252,7 @@ export class WASI {
1252
1252
  const { HEAPU8, view } = getMemory(this);
1253
1253
  const wasi = _wasi.get(this);
1254
1254
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
1255
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1255
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1256
1256
  pathString = resolve(fileDescriptor.realPath, pathString);
1257
1257
  const fs = getFs(this);
1258
1258
  const link = fs.readlinkSync(pathString);
@@ -1276,7 +1276,7 @@ export class WASI {
1276
1276
  const { HEAPU8, view } = getMemory(this);
1277
1277
  const wasi = _wasi.get(this);
1278
1278
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
1279
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1279
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1280
1280
  pathString = resolve(fileDescriptor.realPath, pathString);
1281
1281
  const fs = getFs(this);
1282
1282
  const link = await fs.promises.readlink(pathString);
@@ -1298,7 +1298,7 @@ export class WASI {
1298
1298
  const { HEAPU8 } = getMemory(this);
1299
1299
  const wasi = _wasi.get(this);
1300
1300
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
1301
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1301
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1302
1302
  pathString = resolve(fileDescriptor.realPath, pathString);
1303
1303
  const fs = getFs(this);
1304
1304
  fs.rmdirSync(pathString);
@@ -1312,7 +1312,7 @@ export class WASI {
1312
1312
  const { HEAPU8 } = getMemory(this);
1313
1313
  const wasi = _wasi.get(this);
1314
1314
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
1315
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1315
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1316
1316
  pathString = resolve(fileDescriptor.realPath, pathString);
1317
1317
  const fs = getFs(this);
1318
1318
  await fs.promises.rmdir(pathString);
@@ -1337,8 +1337,8 @@ export class WASI {
1337
1337
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
1338
1338
  }
1339
1339
  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)));
1340
+ const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)));
1341
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1342
1342
  const fs = getFs(this);
1343
1343
  fs.renameSync(resolvedOldPath, resolvedNewPath);
1344
1344
  return WasiErrno.ESUCCESS;
@@ -1361,8 +1361,8 @@ export class WASI {
1361
1361
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
1362
1362
  }
1363
1363
  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)));
1364
+ const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)));
1365
+ const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1366
1366
  const fs = getFs(this);
1367
1367
  await fs.promises.rename(resolvedOldPath, resolvedNewPath);
1368
1368
  return WasiErrno.ESUCCESS;
@@ -1378,8 +1378,8 @@ export class WASI {
1378
1378
  const { HEAPU8 } = getMemory(this);
1379
1379
  const wasi = _wasi.get(this);
1380
1380
  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));
1381
+ const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
1382
+ let newPath = decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len));
1383
1383
  newPath = resolve(fileDescriptor.realPath, newPath);
1384
1384
  const fs = getFs(this);
1385
1385
  fs.symlinkSync(oldPath, newPath);
@@ -1395,8 +1395,8 @@ export class WASI {
1395
1395
  const { HEAPU8 } = getMemory(this);
1396
1396
  const wasi = _wasi.get(this);
1397
1397
  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));
1398
+ const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
1399
+ let newPath = decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len));
1400
1400
  newPath = resolve(fileDescriptor.realPath, newPath);
1401
1401
  const fs = getFs(this);
1402
1402
  await fs.promises.symlink(oldPath, newPath);
@@ -1411,7 +1411,7 @@ export class WASI {
1411
1411
  const { HEAPU8 } = getMemory(this);
1412
1412
  const wasi = _wasi.get(this);
1413
1413
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
1414
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1414
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1415
1415
  pathString = resolve(fileDescriptor.realPath, pathString);
1416
1416
  const fs = getFs(this);
1417
1417
  fs.unlinkSync(pathString);
@@ -1425,7 +1425,7 @@ export class WASI {
1425
1425
  const { HEAPU8 } = getMemory(this);
1426
1426
  const wasi = _wasi.get(this);
1427
1427
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
1428
- let pathString = decoder.decode(HEAPU8.subarray(path, path + path_len));
1428
+ let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1429
1429
  pathString = resolve(fileDescriptor.realPath, pathString);
1430
1430
  const fs = getFs(this);
1431
1431
  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.2",
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",