@waffle-os/runtime 0.0.155 → 0.0.157

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.
Files changed (77) hide show
  1. package/browser/filesystem-backend.ts +11 -1
  2. package/browser/preview-protocol.ts +2 -0
  3. package/browser/preview.ts +128 -18
  4. package/browser/runtime-terminals.ts +65 -24
  5. package/browser/shared-fs.ts +141 -14
  6. package/browser/worker.ts +73 -6
  7. package/core/descriptors.ts +19 -2
  8. package/core/devices.ts +237 -2
  9. package/core/fd-reopen.ts +105 -20
  10. package/core/filesystem.ts +79 -0
  11. package/core/index.ts +4 -0
  12. package/core/open-file.ts +212 -18
  13. package/core/wasi.ts +449 -47
  14. package/dist/browser/filesystem-backend.js +4 -2
  15. package/dist/browser/preview.js +120 -17
  16. package/dist/browser/runtime-terminals.js +53 -10
  17. package/dist/browser/shared-fs.js +124 -11
  18. package/dist/browser/worker.js +59 -6
  19. package/dist/core/descriptors.js +9 -1
  20. package/dist/core/devices.js +211 -1
  21. package/dist/core/fd-reopen.js +79 -18
  22. package/dist/core/filesystem.js +14 -0
  23. package/dist/core/open-file.js +166 -13
  24. package/dist/core/wasi.js +402 -29
  25. package/dist/fs/backend.js +51 -3
  26. package/dist/fs/client.js +204 -7
  27. package/dist/fs/cold-read-cache.js +23 -0
  28. package/dist/fs/index.js +21 -1
  29. package/dist/fs/indexeddb.js +30 -2
  30. package/dist/fs/locks.js +1112 -0
  31. package/dist/fs/memory.js +126 -2
  32. package/dist/fs/multitab-backend.js +317 -196
  33. package/dist/fs/multitab-protocol.js +2 -0
  34. package/dist/fs/persistent.js +32 -7
  35. package/dist/fs/protocol.js +9 -3
  36. package/dist/fs/service.js +387 -22
  37. package/dist/fs/shared.js +54 -1
  38. package/dist/fs/storage-geometry.js +2 -0
  39. package/dist/interrupt/rewriter-internal.js +4 -1
  40. package/dist/proc/abi.js +80 -6
  41. package/dist/proc/errors.js +1 -0
  42. package/dist/proc/filesystem-codec.js +40 -9
  43. package/dist/proc/index.js +10 -1
  44. package/dist/proc/pty.js +193 -0
  45. package/dist/proc/worker-client.js +11 -2
  46. package/dist/proc/worker-pipes.js +51 -0
  47. package/dist/runtime.js +11 -2
  48. package/dist/search/engine.js +53 -0
  49. package/dist/tooling/program/program-transformer.json +1 -1
  50. package/fs/backend.ts +54 -2
  51. package/fs/client.ts +230 -6
  52. package/fs/cold-read-cache.ts +38 -0
  53. package/fs/index.ts +22 -1
  54. package/fs/indexeddb.ts +36 -2
  55. package/fs/locks.ts +1406 -0
  56. package/fs/memory.ts +132 -1
  57. package/fs/multitab-backend.ts +563 -304
  58. package/fs/multitab-protocol.ts +2 -0
  59. package/fs/persistent.ts +46 -4
  60. package/fs/protocol.ts +17 -5
  61. package/fs/service.ts +469 -23
  62. package/fs/shared.ts +60 -2
  63. package/fs/storage-geometry.ts +3 -0
  64. package/index.ts +2 -0
  65. package/interrupt/rewriter-internal.ts +4 -1
  66. package/package.json +2 -2
  67. package/proc/abi.ts +79 -6
  68. package/proc/errors.ts +1 -0
  69. package/proc/filesystem-codec.ts +39 -9
  70. package/proc/index.ts +11 -0
  71. package/proc/pty.ts +227 -0
  72. package/proc/worker-client.ts +28 -6
  73. package/proc/worker-pipes.ts +57 -0
  74. package/runtime.ts +12 -2
  75. package/search/engine.ts +49 -0
  76. package/testing/indexeddb-storage-browser.ts +173 -2
  77. package/tooling/program/program-transformer.json +1 -1
package/dist/core/wasi.js CHANGED
@@ -34,10 +34,29 @@ import {
34
34
  import { resolveDescriptorPath, reopenDescriptor, openInode } from "./fd-reopen.js";
35
35
  import { ExecutionUnwind } from "./unwind.js";
36
36
  import { controlLoad, controlWait } from "../proc/control.js";
37
- import { withFileOffset } from "./open-file.js";
38
37
  import {
38
+ markResourceOfdLocked,
39
+ releaseOriginOfd,
40
+ resourceOfdId,
41
+ withFileOffset
42
+ } from "./open-file.js";
43
+ import {
44
+ F_GETLK,
45
+ F_OFD_GETLK,
46
+ F_OFD_SETLK,
47
+ F_OFD_SETLKW,
48
+ F_RDLCK,
49
+ F_SETLKW,
50
+ F_UNLCK,
51
+ F_WRLCK,
39
52
  FilesystemError,
40
- fileTimeNow
53
+ fileTimeNow,
54
+ LOCK_EX,
55
+ LOCK_NB,
56
+ LOCK_SH,
57
+ LOCK_UN,
58
+ permissionsFor,
59
+ PROBE_PATH_LIMIT
41
60
  } from "./filesystem.js";
42
61
  import {
43
62
  DescriptorTable,
@@ -46,8 +65,18 @@ import {
46
65
  } from "./descriptors.js";
47
66
  import { memoryViews } from "./memory-views.js";
48
67
  import { GuestWatchTable } from "../fs/guest-watch.js";
49
- import { createNullDevice } from "./devices.js";
68
+ import {
69
+ createMachineIdFile,
70
+ createNullDevice,
71
+ MACHINE_ID_FILE_SIZE,
72
+ MACHINE_ID_INO,
73
+ readVirtualFileBytes,
74
+ seekVirtualFile,
75
+ tellVirtualFile,
76
+ validateMachineId
77
+ } from "./devices.js";
50
78
  const SUCCESS = 0;
79
+ const EACCES = 2;
51
80
  const EBADF = 8;
52
81
  const EEXIST = 20;
53
82
  const EFAULT = 21;
@@ -63,6 +92,7 @@ const ENOSYS = 52;
63
92
  const ENOTDIR = 54;
64
93
  const ENOTSUP = 58;
65
94
  const ENOTSOCK = 57;
95
+ const EOVERFLOW = 61;
66
96
  const ESPIPE = 70;
67
97
  const ENOTCAPABLE = 76;
68
98
  const MAX_FILE_SIZE = BigInt(Number.MAX_SAFE_INTEGER);
@@ -102,7 +132,7 @@ export class WasiExit extends ExecutionUnwind {
102
132
  }
103
133
  }
104
134
  const encoder = new TextEncoder;
105
- const decoder = new TextDecoder;
135
+ const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
106
136
  const fatalDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
107
137
  const perfNow = performance.now.bind(performance);
108
138
  const timeOrigin = performance.timeOrigin;
@@ -126,6 +156,27 @@ function guestPathname(bytes, ptr, len) {
126
156
  return;
127
157
  }
128
158
  }
159
+ const PROBE_KINDS = [undefined, "file", "directory"];
160
+ function probePathnames(bytes, ptr, len) {
161
+ if (len === 0 || bytes[ptr + len - 1] !== 0)
162
+ return;
163
+ const end = ptr + len;
164
+ const paths = [];
165
+ let start = ptr;
166
+ for (let at = ptr;at < end; at++) {
167
+ if (bytes[at] !== 0)
168
+ continue;
169
+ if (paths.length === PROBE_PATH_LIMIT)
170
+ return;
171
+ try {
172
+ paths.push(decodeBytes(bytes, start, at - start, fatalDecoder));
173
+ } catch {
174
+ return;
175
+ }
176
+ start = at + 1;
177
+ }
178
+ return paths;
179
+ }
129
180
  function writeUtf8Prefix(target, output) {
130
181
  if (output.length === 0)
131
182
  return 0;
@@ -178,6 +229,7 @@ export class Wasi {
178
229
  hostCall = null;
179
230
  descriptors = new DescriptorTable;
180
231
  watches;
232
+ posixLockedHandles;
181
233
  importedMemory;
182
234
  boundMemory;
183
235
  hostWrite;
@@ -185,6 +237,38 @@ export class Wasi {
185
237
  env;
186
238
  rootedSnapshot;
187
239
  literalRoot;
240
+ explicitMachineId;
241
+ machineId() {
242
+ return this.explicitMachineId ?? this.opts.fs.machineId;
243
+ }
244
+ virtualMachineIdStat() {
245
+ return {
246
+ kind: "file",
247
+ ino: MACHINE_ID_INO,
248
+ nlink: 1,
249
+ size: MACHINE_ID_FILE_SIZE,
250
+ atime: 0n,
251
+ mtime: 0n,
252
+ ctime: 0n,
253
+ mode: 292
254
+ };
255
+ }
256
+ resolveVirtualMachineId(entry, path, follow) {
257
+ if (this.machineId() === undefined)
258
+ return;
259
+ const alias = resolveDescriptorPath(this.descriptors, this.opts.fs, entry, path, follow ? "follow" : "nofollow", this.rooted, true);
260
+ if (typeof alias === "number")
261
+ return alias;
262
+ if (alias !== undefined && "virtualFile" in alias)
263
+ return alias.virtualFile;
264
+ return;
265
+ }
266
+ relativeVirtualMachineId(handle) {
267
+ const rootHandle = this.opts.fs.preopen("/");
268
+ if (handle === rootHandle || this.opts.fs.stat(handle).ino === this.opts.fs.stat(rootHandle).ino)
269
+ return "etc/machine-id";
270
+ return "machine-id";
271
+ }
188
272
  get rooted() {
189
273
  return this.rootedSnapshot ??= this.rootNamespace();
190
274
  }
@@ -229,6 +313,8 @@ export class Wasi {
229
313
  }
230
314
  constructor(opts) {
231
315
  this.opts = opts;
316
+ if (opts.machineId !== undefined)
317
+ this.explicitMachineId = validateMachineId(opts.machineId);
232
318
  const reserved = new Set;
233
319
  for (const fd of opts.reservedFds ?? []) {
234
320
  if (!Number.isInteger(fd) || fd < 0 || fd > MAX_DESCRIPTOR || reserved.has(fd))
@@ -268,6 +354,9 @@ export class Wasi {
268
354
  this.openFileAt(fd, { kind: "directory", handle }, RIGHTS_ALL, RIGHTS_ALL, name);
269
355
  fd++;
270
356
  }
357
+ if (opts.fs.locks) {
358
+ this.descriptors.setOnClose((closed) => this.releasePosixHandle(closed.resource));
359
+ }
271
360
  }
272
361
  openDevice(fdflags, base, inheriting, oflags, out) {
273
362
  if ((out >>> 0) + 4 > this.bytes().length)
@@ -280,9 +369,39 @@ export class Wasi {
280
369
  return EINVAL;
281
370
  return this.writeResult(this.descriptors.open(createNullDevice(fdflags), base, inheriting), out >>> 0);
282
371
  }
372
+ openVirtualMachineId(fdflags, base, inheriting, oflags, out) {
373
+ if ((out >>> 0) + 4 > this.bytes().length)
374
+ return EFAULT;
375
+ if (oflags & O_DIRECTORY)
376
+ return ENOTDIR;
377
+ if (oflags & O_CREAT && oflags & O_EXCL)
378
+ return EEXIST;
379
+ if (oflags & ~15 || fdflags & ~31)
380
+ return EINVAL;
381
+ if ((oflags & O_TRUNC) !== 0 || (base & RIGHT_FD_WRITE) !== 0n)
382
+ return EACCES;
383
+ const id = this.machineId();
384
+ if (id === undefined)
385
+ return ENOENT;
386
+ return this.writeResult(this.descriptors.open(createMachineIdFile(id, fdflags), base, inheriting), out >>> 0);
387
+ }
283
388
  openFileAt(fd, resource, base, inheriting, preopen) {
284
389
  this.opts.fs.retain(resource.handle);
285
- return this.descriptors.openAt(fd, resource, base, inheriting, () => this.opts.fs.release(resource.handle), preopen);
390
+ return this.descriptors.openAt(fd, resource, base, inheriting, () => releaseOriginOfd(this.opts.fs, resource), preopen);
391
+ }
392
+ releasePosixLocks() {
393
+ if (!this.posixLockedHandles || this.posixLockedHandles.size === 0)
394
+ return;
395
+ const handles = Array.from(this.posixLockedHandles);
396
+ this.posixLockedHandles.clear();
397
+ const pid = this.opts.pid ?? 1;
398
+ for (let i = 0;i < handles.length; i++) {
399
+ this.opts.fs.locks?.releasePosix(handles[i], pid);
400
+ }
401
+ }
402
+ suppressPosixClose() {
403
+ this.descriptors.setOnClose(undefined);
404
+ this.posixLockedHandles?.clear();
286
405
  }
287
406
  fdReopen(fd, oflags, fdflags, base, out) {
288
407
  try {
@@ -293,10 +412,10 @@ export class Wasi {
293
412
  throw error;
294
413
  }
295
414
  }
296
- reopen(source, oflags, fdflags, base, inheriting, out) {
415
+ reopen(source, oflags, fdflags, base, inheriting, out, intersectCapabilities = false) {
297
416
  if ((out >>> 0) + 4 > this.bytes().length)
298
417
  return 21;
299
- const result = reopenDescriptor(this.descriptors, this.opts.fs, source, oflags, fdflags, base, inheriting);
418
+ const result = reopenDescriptor(this.descriptors, this.opts.fs, source, oflags, fdflags, base, inheriting, intersectCapabilities);
300
419
  return this.writeResult(result, out >>> 0);
301
420
  }
302
421
  writeResult(result, out) {
@@ -347,11 +466,14 @@ export class Wasi {
347
466
  const filesystem = {
348
467
  path_permissions_get: this.pathPermissions.bind(this),
349
468
  path_realpath: this.pathRealpath.bind(this),
469
+ path_probe: this.pathProbe.bind(this),
350
470
  watch_open: this.watchOpen.bind(this),
351
471
  watch_ready: () => this.watches?.ready() ? 1 : 0,
352
472
  watch_read: this.watchRead.bind(this),
353
473
  watch_next: this.watchNext.bind(this),
354
- watch_close: (id) => this.watches?.close(id >>> 0) ?? EBADF
474
+ watch_close: (id) => this.watches?.close(id >>> 0) ?? EBADF,
475
+ fd_flock: this.fdFlock.bind(this),
476
+ fd_lock: this.fdLock.bind(this)
355
477
  };
356
478
  const out = Object.create(null);
357
479
  this.importedMemory = undefined;
@@ -560,6 +682,147 @@ export class Wasi {
560
682
  this.view().setUint32(out, result.length, true);
561
683
  return SUCCESS;
562
684
  }
685
+ lockWaitControl() {
686
+ if (!this.opts.checkActive && !this.opts.interruptPending && !this.opts.interrupt && !this.opts.wait)
687
+ return;
688
+ return {
689
+ checkActive: this.opts.checkActive,
690
+ interruptPending: () => this.opts.interruptPending?.() ?? (this.opts.interrupt !== undefined && controlLoad(this.opts.interrupt, 0) !== 0),
691
+ wait: this.opts.wait ? (word, expected, ms) => this.opts.wait(word, expected, ms) : undefined
692
+ };
693
+ }
694
+ resolveRangeBase(resource, whence) {
695
+ switch (whence) {
696
+ case 0:
697
+ return 0n;
698
+ case 1:
699
+ return BigInt(resource.kind === "file" ? resource.offset : 0);
700
+ case 2:
701
+ return BigInt(this.opts.fs.stat(resource.handle).size);
702
+ default:
703
+ return null;
704
+ }
705
+ }
706
+ resolveRange(resource, whence, start, len) {
707
+ const base = this.resolveRangeBase(resource, whence);
708
+ if (base === null)
709
+ return EINVAL;
710
+ const pos = base + start;
711
+ if (len === 0n) {
712
+ if (pos < 0n)
713
+ return EINVAL;
714
+ if (pos > MAX_FILE_SIZE)
715
+ return EOVERFLOW;
716
+ return { start: Number(pos), len: 0 };
717
+ }
718
+ const rangeStart = len > 0n ? pos : pos + len;
719
+ const rangeEnd = len > 0n ? pos + len : pos;
720
+ if (rangeStart < 0n || rangeEnd < 0n)
721
+ return EINVAL;
722
+ if (rangeStart > MAX_FILE_SIZE || rangeEnd > MAX_FILE_SIZE)
723
+ return EOVERFLOW;
724
+ return { start: Number(rangeStart), len: Number(rangeEnd - rangeStart) };
725
+ }
726
+ releasePosixHandle(resource) {
727
+ if (resource.kind !== "file" && resource.kind !== "directory")
728
+ return;
729
+ const tracked = this.posixLockedHandles?.delete(resource.handle) === true;
730
+ const locks = this.opts.fs.locks;
731
+ if (!locks)
732
+ return;
733
+ if (!tracked && !locks.hasActivePosixLocks?.())
734
+ return;
735
+ locks.releasePosix(resource.handle, this.opts.pid ?? 1);
736
+ }
737
+ releaseUninheritedPosixLocks(keptFds) {
738
+ this.descriptors.setOnClose(undefined);
739
+ const closedResources = [];
740
+ this.descriptors.forEach((entry, fd) => {
741
+ if (!keptFds.has(fd))
742
+ closedResources.push(entry.resource);
743
+ });
744
+ for (let i = 0;i < closedResources.length; i++) {
745
+ this.releasePosixHandle(closedResources[i]);
746
+ }
747
+ this.posixLockedHandles?.clear();
748
+ }
749
+ fdFlock(fd, operation) {
750
+ const entry = this.descriptors.get(fd >>> 0);
751
+ if (!entry)
752
+ return EBADF;
753
+ if (entry.resource.kind !== "file" && entry.resource.kind !== "directory")
754
+ return EINVAL;
755
+ if ((operation & ~(LOCK_SH | LOCK_EX | LOCK_NB | LOCK_UN)) !== 0)
756
+ return EINVAL;
757
+ const base = operation & ~LOCK_NB;
758
+ if (base !== LOCK_SH && base !== LOCK_EX && base !== LOCK_UN)
759
+ return EINVAL;
760
+ const locks = this.opts.fs.locks;
761
+ if (!locks)
762
+ return ENOTSUP;
763
+ const owner = locks.owner ?? 0;
764
+ const ofdId = resourceOfdId(entry.resource, owner);
765
+ const blocking = (operation & LOCK_NB) === 0 && base !== LOCK_UN;
766
+ const err = locks.flock(entry.resource.handle, ofdId, operation, blocking ? this.lockWaitControl() : undefined);
767
+ if (err === SUCCESS && base !== LOCK_UN)
768
+ markResourceOfdLocked(entry.resource, owner);
769
+ return err;
770
+ }
771
+ fdLock(fd, cmd, type, whence, start, len, outPtr) {
772
+ if (cmd < F_GETLK || cmd > F_OFD_SETLKW)
773
+ return EINVAL;
774
+ if (type !== F_RDLCK && type !== F_WRLCK && type !== F_UNLCK)
775
+ return EINVAL;
776
+ if (whence < 0 || whence > 2)
777
+ return EINVAL;
778
+ const isGet = cmd === F_GETLK || cmd === F_OFD_GETLK;
779
+ const out = outPtr >>> 0;
780
+ if (isGet && out + 24 > this.bytes().length)
781
+ return EFAULT;
782
+ const entry = this.descriptors.get(fd >>> 0);
783
+ if (!entry)
784
+ return EBADF;
785
+ if (entry.resource.kind !== "file" && entry.resource.kind !== "directory")
786
+ return EINVAL;
787
+ if (!isGet && type === F_RDLCK && !(entry.rightsBase & RIGHT_FD_READ))
788
+ return EBADF;
789
+ if (!isGet && type === F_WRLCK && !(entry.rightsBase & RIGHT_FD_WRITE))
790
+ return EBADF;
791
+ const locks = this.opts.fs.locks;
792
+ if (!locks)
793
+ return ENOTSUP;
794
+ const resolved = this.resolveRange(entry.resource, whence, start, len);
795
+ if (typeof resolved === "number")
796
+ return resolved;
797
+ const owner = locks.owner ?? 0;
798
+ const isOfd = cmd === F_OFD_GETLK || cmd === F_OFD_SETLK || cmd === F_OFD_SETLKW;
799
+ const ofdId = isOfd ? resourceOfdId(entry.resource, owner) : undefined;
800
+ const range = { type, start: resolved.start, len: resolved.len };
801
+ const pid = this.opts.pid ?? 1;
802
+ if (isGet) {
803
+ const conflict = locks.getRangeLock(entry.resource.handle, ofdId, range, pid);
804
+ if (typeof conflict === "number")
805
+ return conflict;
806
+ const view = this.view();
807
+ if (out + 24 > view.byteLength)
808
+ return EFAULT;
809
+ view.setUint32(out, conflict.type, true);
810
+ view.setInt32(out + 4, conflict.pid, true);
811
+ view.setBigInt64(out + 8, BigInt(conflict.start), true);
812
+ view.setBigInt64(out + 16, BigInt(conflict.len), true);
813
+ return SUCCESS;
814
+ }
815
+ const blocking = (cmd === F_SETLKW || cmd === F_OFD_SETLKW) && type !== F_UNLCK;
816
+ const err = locks.setRangeLock(entry.resource.handle, ofdId, pid, range, blocking, blocking ? this.lockWaitControl() : undefined);
817
+ if (err !== SUCCESS || type === F_UNLCK)
818
+ return err;
819
+ if (isOfd) {
820
+ markResourceOfdLocked(entry.resource, owner);
821
+ return err;
822
+ }
823
+ (this.posixLockedHandles ??= new Set).add(entry.resource.handle);
824
+ return err;
825
+ }
563
826
  pathRealpath(fd, ptr, len, buf, capacity, used) {
564
827
  ptr >>>= 0;
565
828
  len >>>= 0;
@@ -581,7 +844,14 @@ export class Wasi {
581
844
  return ENOTDIR;
582
845
  if (!(entry.rightsBase & RIGHT_PATH_FILESTAT_GET))
583
846
  return ENOTCAPABLE;
584
- const real = this.opts.fs.realpath(entry.resource.handle, path, this.rooted);
847
+ let real = this.opts.fs.realpath(entry.resource.handle, path, this.rooted);
848
+ if (real === ENOENT) {
849
+ const virtual = this.resolveVirtualMachineId(entry, path, true);
850
+ if (typeof virtual === "number")
851
+ return virtual;
852
+ if (virtual === "machine-id")
853
+ real = this.rooted ? "/etc/machine-id" : this.relativeVirtualMachineId(entry.resource.handle);
854
+ }
585
855
  if (typeof real === "number")
586
856
  return real;
587
857
  const encoded = encoder.encode(real);
@@ -591,6 +861,30 @@ export class Wasi {
591
861
  this.memory.range(buf, encoded.length).set(encoded);
592
862
  return SUCCESS;
593
863
  }
864
+ pathProbe(fd, ptr, len, kind, found) {
865
+ ptr >>>= 0;
866
+ len >>>= 0;
867
+ found >>>= 0;
868
+ const bytes = this.bytes();
869
+ if (ptr + len > bytes.length || found + 4 > bytes.length)
870
+ return EFAULT;
871
+ const paths = probePathnames(bytes, ptr, len);
872
+ if (paths === undefined)
873
+ return EINVAL;
874
+ const wanted = PROBE_KINDS[kind >>> 0];
875
+ if (wanted === undefined)
876
+ return EINVAL;
877
+ const entry = this.descriptors.get(fd >>> 0);
878
+ if (!entry)
879
+ return EBADF;
880
+ if (entry.resource.kind !== "directory")
881
+ return ENOTDIR;
882
+ if (!(entry.rightsBase & RIGHT_PATH_FILESTAT_GET))
883
+ return ENOTCAPABLE;
884
+ const index = this.opts.fs.probe(entry.resource.handle, paths, wanted, this.rooted);
885
+ this.view().setUint32(found, index < 0 ? 4294967295 : index, true);
886
+ return SUCCESS;
887
+ }
594
888
  pathPermissions(fd, ptr, len, flags, out) {
595
889
  if (flags !== 0)
596
890
  return ENOTSUP;
@@ -616,11 +910,17 @@ export class Wasi {
616
910
  if (!(entry.rightsBase & RIGHT_PATH_FILESTAT_GET))
617
911
  return ENOTCAPABLE;
618
912
  const resolved = this.opts.fs.resolve(entry.resource.handle, path, false, this.rooted);
619
- if (typeof resolved === "number")
620
- return resolved;
621
- if (resolved.node === undefined)
622
- return ENOENT;
623
- const result = this.opts.fs.permissions?.(resolved.node);
913
+ let result = typeof resolved !== "number" && resolved.node !== undefined ? this.opts.fs.permissions?.(resolved.node) : undefined;
914
+ if (typeof resolved === "number" || resolved.node === undefined) {
915
+ if (typeof resolved === "number" && resolved !== ENOENT)
916
+ return resolved;
917
+ const virtual = this.resolveVirtualMachineId(entry, path, false);
918
+ if (typeof virtual === "number")
919
+ return virtual;
920
+ if (virtual !== "machine-id")
921
+ return ENOENT;
922
+ result = permissionsFor("file", 292);
923
+ }
624
924
  if (!result)
625
925
  return ENOTSUP;
626
926
  const view = this.view();
@@ -758,9 +1058,26 @@ export class Wasi {
758
1058
  if (!entry)
759
1059
  return EBADF;
760
1060
  const view = this.view();
761
- const filetype = entry.resource.kind === "terminal" || entry.resource.kind === "device" ? FT_CHAR : entry.resource.kind === "directory" ? FT_DIR : entry.resource.kind === "socket" ? FT_SOCKET : entry.resource.kind === "pipe" ? 0 : FT_FILE;
1061
+ const resource = entry.resource;
1062
+ let filetype = FT_FILE;
1063
+ if (resource.kind === "terminal")
1064
+ filetype = FT_CHAR;
1065
+ else if (resource.kind === "device")
1066
+ filetype = resource.virtualFile ? FT_FILE : FT_CHAR;
1067
+ else if (resource.kind === "directory")
1068
+ filetype = FT_DIR;
1069
+ else if (resource.kind === "socket")
1070
+ filetype = FT_SOCKET;
1071
+ else if (resource.kind === "pipe")
1072
+ filetype = 0;
1073
+ let fdflags = 0;
1074
+ if (resource.kind === "file") {
1075
+ fdflags = (resource.fdflags ?? 0) & ~FDFLAG_APPEND | (resource.append ? FDFLAG_APPEND : 0);
1076
+ } else if (resource.kind !== "directory") {
1077
+ fdflags = resource.fdflags ?? 0;
1078
+ }
762
1079
  view.setUint8(ptr, filetype);
763
- view.setUint16(ptr + 2, entry.resource.kind === "file" ? (entry.resource.fdflags ?? 0) & ~FDFLAG_APPEND | (entry.resource.append ? FDFLAG_APPEND : 0) : entry.resource.kind === "directory" ? 0 : entry.resource.fdflags ?? 0, true);
1080
+ view.setUint16(ptr + 2, fdflags, true);
764
1081
  view.setBigUint64(ptr + 8, entry.rightsBase, true);
765
1082
  view.setBigUint64(ptr + 16, entry.rightsInheriting, true);
766
1083
  return SUCCESS;
@@ -796,10 +1113,21 @@ export class Wasi {
796
1113
  return SUCCESS;
797
1114
  },
798
1115
  fd_filestat_get: this.descriptorCall(RIGHT_FD_FILESTAT_GET, (resource, ptr) => {
799
- this.writeFilestat(ptr, resource.kind === "file" || resource.kind === "directory" ? this.opts.fs.stat(resource.handle) : null, resource.kind === "pipe" ? 0 : resource.kind === "socket" ? FT_SOCKET : FT_CHAR);
1116
+ if (resource.kind === "device" && resource.virtualFile) {
1117
+ this.writeFilestat(ptr, this.virtualMachineIdStat());
1118
+ return SUCCESS;
1119
+ }
1120
+ let streamType = FT_CHAR;
1121
+ if (resource.kind === "pipe")
1122
+ streamType = 0;
1123
+ else if (resource.kind === "socket")
1124
+ streamType = FT_SOCKET;
1125
+ this.writeFilestat(ptr, resource.kind === "file" || resource.kind === "directory" ? this.opts.fs.stat(resource.handle) : null, streamType);
800
1126
  return SUCCESS;
801
1127
  }),
802
1128
  fd_filestat_set_size: this.descriptorCall(RIGHT_FD_FILESTAT_SET_SIZE, (resource, size) => {
1129
+ if (resource.kind === "device" && resource.virtualFile)
1130
+ return EACCES;
803
1131
  if (resource.kind !== "file")
804
1132
  return EINVAL;
805
1133
  if (!isSupportedFileSize(size))
@@ -814,6 +1142,11 @@ export class Wasi {
814
1142
  fd_pread: this.descriptorCall(RIGHT_FD_READ | RIGHT_FD_SEEK, (resource, iovs, count, offset, nreadPtr) => {
815
1143
  if (resource.kind === "directory")
816
1144
  return EISDIR;
1145
+ if (resource.kind === "device" && resource.virtualFile) {
1146
+ if (offset < 0n)
1147
+ return EINVAL;
1148
+ return this.writeResult(readVirtualFileBytes(resource.virtualFile.data, this.iovecs(iovs, count), Number(offset)), nreadPtr);
1149
+ }
817
1150
  if (resource.kind !== "file")
818
1151
  return ESPIPE;
819
1152
  if (offset < 0n)
@@ -823,6 +1156,8 @@ export class Wasi {
823
1156
  fd_pwrite: this.descriptorCall(RIGHT_FD_WRITE | RIGHT_FD_SEEK, (resource, iovs, count, offset, nwrittenPtr) => {
824
1157
  if (resource.kind === "directory")
825
1158
  return EISDIR;
1159
+ if (resource.kind === "device" && resource.virtualFile)
1160
+ return EACCES;
826
1161
  if (resource.kind !== "file")
827
1162
  return ESPIPE;
828
1163
  if (offset < 0n)
@@ -870,9 +1205,7 @@ export class Wasi {
870
1205
  this.view().setUint32(usedPtr, page.length, true);
871
1206
  return SUCCESS;
872
1207
  }),
873
- fd_renumber: (from, to) => {
874
- return this.descriptors.renumber(from, to) ? SUCCESS : EBADF;
875
- },
1208
+ fd_renumber: (from, to) => this.descriptors.renumber(from, to) ? SUCCESS : EBADF,
876
1209
  fd_seek: (fd, offset, whence, newOffsetPtr) => {
877
1210
  const entry = this.descriptors.get(fd);
878
1211
  if (!entry)
@@ -882,11 +1215,24 @@ export class Wasi {
882
1215
  return ENOTCAPABLE;
883
1216
  if (entry.resource.kind === "directory")
884
1217
  return EISDIR;
1218
+ if (entry.resource.kind === "device" && entry.resource.virtualFile) {
1219
+ const next = seekVirtualFile(entry.resource.virtualFile, offset, whence);
1220
+ if (typeof next === "number")
1221
+ return -next;
1222
+ this.view().setBigUint64(newOffsetPtr, next, true);
1223
+ return SUCCESS;
1224
+ }
885
1225
  if (entry.resource.kind !== "file")
886
1226
  return ESPIPE;
887
1227
  const file = entry.resource;
888
1228
  const result = withFileOffset(file, () => {
889
- const base = whence === 0 ? 0 : whence === 1 ? file.offset : whence === 2 ? this.opts.fs.stat(file.handle).size : NaN;
1229
+ let base = Number.NaN;
1230
+ if (whence === 0)
1231
+ base = 0;
1232
+ else if (whence === 1)
1233
+ base = file.offset;
1234
+ else if (whence === 2)
1235
+ base = this.opts.fs.stat(file.handle).size;
890
1236
  const pos = base + Number(offset);
891
1237
  if (!(pos >= 0))
892
1238
  return -EINVAL;
@@ -897,6 +1243,10 @@ export class Wasi {
897
1243
  return result < 0 ? -result : result;
898
1244
  },
899
1245
  fd_tell: this.descriptorCall(RIGHT_FD_TELL, (resource, ptr) => {
1246
+ if (resource.kind === "device" && resource.virtualFile) {
1247
+ this.view().setBigUint64(ptr, tellVirtualFile(resource.virtualFile), true);
1248
+ return SUCCESS;
1249
+ }
900
1250
  if (resource.kind !== "file")
901
1251
  return ESPIPE;
902
1252
  this.view().setBigUint64(ptr, BigInt(resource.offset), true);
@@ -923,13 +1273,25 @@ export class Wasi {
923
1273
  return typeof result === "number" ? SUCCESS : result.error;
924
1274
  },
925
1275
  path_filestat_get: (fd, flags, ptr, len, buf) => {
926
- const path = this.resolve(fd, ptr, len, RIGHT_PATH_FILESTAT_GET, lookupFlags(flags));
927
- if (typeof path === "number")
928
- return path;
929
- const node = path.node;
930
- if (node === undefined)
1276
+ const follow = lookupFlags(flags);
1277
+ const path = this.resolve(fd, ptr, len, RIGHT_PATH_FILESTAT_GET, follow);
1278
+ if (typeof path === "number" || path.node === undefined) {
1279
+ if (typeof path === "number" && path !== ENOENT)
1280
+ return path;
1281
+ const entry = this.descriptors.get(fd);
1282
+ if (!entry)
1283
+ return EBADF;
1284
+ const name = this.str(ptr, len);
1285
+ const virtual = this.resolveVirtualMachineId(entry, name, followsFinal(follow, name));
1286
+ if (typeof virtual === "number")
1287
+ return virtual;
1288
+ if (virtual === "machine-id") {
1289
+ this.writeFilestat(buf, this.virtualMachineIdStat());
1290
+ return SUCCESS;
1291
+ }
931
1292
  return ENOENT;
932
- this.writeFilestat(buf, this.opts.fs.stat(node));
1293
+ }
1294
+ this.writeFilestat(buf, this.opts.fs.stat(path.node));
933
1295
  return SUCCESS;
934
1296
  },
935
1297
  path_filestat_set_times: (fd, flags, ptr, len, atim, mtim, fst) => {
@@ -970,10 +1332,20 @@ export class Wasi {
970
1332
  const name = this.str(ptr, len);
971
1333
  const exclusive = (oflags & O_CREAT && oflags & O_EXCL) !== 0;
972
1334
  const follow = !exclusive && followsFinal(lookupFlags(dirflags), name);
973
- const alias = resolveDescriptorPath(this.descriptors, this.opts.fs, dir, name, exclusive ? "exclusive-create" : follow ? "follow" : "nofollow", this.rooted);
1335
+ let finalLink = "nofollow";
1336
+ if (exclusive)
1337
+ finalLink = "exclusive-create";
1338
+ else if (follow)
1339
+ finalLink = "follow";
1340
+ const alias = resolveDescriptorPath(this.descriptors, this.opts.fs, dir, name, finalLink, this.rooted, this.machineId() !== undefined);
974
1341
  if (typeof alias === "number")
975
1342
  return alias;
976
1343
  if (alias !== undefined && !("resolved" in alias)) {
1344
+ if ("virtualFile" in alias) {
1345
+ if (fdOut + 4 > this.bytes().length)
1346
+ return EFAULT;
1347
+ return this.openVirtualMachineId(fdflags, rightsBase, rightsInheriting, oflags, fdOut);
1348
+ }
977
1349
  if ("device" in alias) {
978
1350
  if (fdOut + 4 > this.bytes().length)
979
1351
  return EFAULT;
@@ -983,7 +1355,7 @@ export class Wasi {
983
1355
  return ENOENT;
984
1356
  if (fdOut + 4 > this.bytes().length)
985
1357
  return EFAULT;
986
- return this.reopen(alias.source, oflags, fdflags, rightsBase, rightsInheriting, fdOut);
1358
+ return this.reopen(alias.source, oflags, fdflags, rightsBase, rightsInheriting, fdOut, true);
987
1359
  }
988
1360
  const path = alias?.resolved ?? this.opts.fs.resolve(dir.resource.handle, name, follow, this.rooted);
989
1361
  if (typeof path === "number")
@@ -1192,6 +1564,7 @@ export class Wasi {
1192
1564
  },
1193
1565
  proc_exit: (code) => {
1194
1566
  this.dropWatches();
1567
+ this.releasePosixLocks();
1195
1568
  this.descriptors.dispose();
1196
1569
  throw new WasiExit(code & 255);
1197
1570
  },