@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/core/open-file.ts CHANGED
@@ -1,44 +1,229 @@
1
1
  import { allocateSharedControl } from "../memory/allocator.ts";
2
+ import {
3
+ atomicsAdd,
4
+ atomicsCompareExchange,
5
+ atomicsLoad,
6
+ atomicsNotify,
7
+ atomicsStore,
8
+ atomicsWait,
9
+ Int32ArrayConstructor,
10
+ reflectApply,
11
+ typedArrayBuffer,
12
+ typedArrayLength,
13
+ } from "../proc/intrinsics/core.ts";
2
14
  import type { Resource } from "./descriptors.js";
3
- import { FilesystemError } from "./filesystem.js";
15
+ import { FilesystemError, type FileSystem } from "./filesystem.js";
4
16
  import { FilesystemSection, isRevoked } from "../fs/section.js";
5
17
  import type { FilesystemIdentity } from "../fs/protocol.js";
6
18
 
7
- const atomicsLoad = Atomics.load;
8
- const atomicsStore = Atomics.store;
9
- const atomicsCompareExchange = Atomics.compareExchange;
10
19
  const atomicsOr = Atomics.or;
11
20
  const atomicsAnd = Atomics.and;
12
- const atomicsNotify = Atomics.notify;
13
- const atomicsWait = Atomics.wait;
14
- const Int32ArrayConstructor = Int32Array;
21
+ const bigintConstructor = BigInt;
22
+ const numberConstructor = Number;
15
23
  const BigUint64ArrayConstructor = BigUint64Array;
16
24
  const Float64ArrayConstructor = Float64Array;
17
25
  const WeakMapConstructor = WeakMap;
18
26
  const weakMapGet = WeakMap.prototype.get;
19
27
  const weakMapSet = WeakMap.prototype.set;
20
28
  const objectDefineProperties = Object.defineProperties;
21
- const typedArrayPrototype = Object.getPrototypeOf(Int32Array.prototype);
22
- const typedArrayBufferGetter = Object.getOwnPropertyDescriptor(typedArrayPrototype, "buffer")!.get!;
23
- const reflectApply = Reflect.apply;
24
29
 
25
30
  type FileResource = Extract<Resource, { kind: "file" }>;
26
- interface SharedOffset {
27
- buffer: SharedArrayBuffer;
28
- cells: Int32Array<SharedArrayBuffer>;
31
+ type DirectoryResource = Extract<Resource, { kind: "directory" }>;
32
+ type LockableResource = FileResource | DirectoryResource;
33
+
34
+ interface LocalOfd {
35
+ readonly id: string;
36
+ readonly originOwner: number;
37
+ readonly originSeq: number;
38
+ locked: boolean;
39
+ }
40
+ interface SharedOfdCells {
41
+ readonly buffer: SharedArrayBuffer;
42
+ readonly cells: Int32Array<SharedArrayBuffer>;
43
+ readonly idPair: BigUint64Array<SharedArrayBuffer>;
44
+ readonly identity: FilesystemIdentity;
45
+ }
46
+ interface SharedOffset extends SharedOfdCells {
29
47
  ownerStates: Int32Array<SharedArrayBuffer>;
30
- identity: FilesystemIdentity;
31
48
  section: FilesystemSection;
32
49
  readOffset(): number;
33
50
  writeOffset(next: number): void;
34
51
  readAppend(): boolean;
35
52
  }
53
+
54
+ const OFD_FLAG_LOCKED = 1;
55
+ const OFD_FLAG_ORIGIN_CLOSED = 2;
56
+
36
57
  const offsets = new WeakMapConstructor<FileResource, SharedOffset>();
58
+ const sharedOfds = new WeakMapConstructor<LockableResource, SharedOfdCells>();
59
+ const localOfds = new WeakMapConstructor<LockableResource, LocalOfd>();
60
+ let nextLocalOfdSeq = 1;
37
61
 
38
62
  function getSharedOffset(resource: FileResource): SharedOffset | undefined {
39
63
  return reflectApply(weakMapGet, offsets, [resource]) as SharedOffset | undefined;
40
64
  }
41
65
 
66
+ function getSharedOfd(resource: LockableResource): SharedOfdCells | undefined {
67
+ return reflectApply(weakMapGet, sharedOfds, [resource]) as SharedOfdCells | undefined;
68
+ }
69
+
70
+ function getLocalOfd(resource: LockableResource): LocalOfd | undefined {
71
+ return reflectApply(weakMapGet, localOfds, [resource]) as LocalOfd | undefined;
72
+ }
73
+
74
+ function formatOfdId(owner: number, seq: number): string {
75
+ return `${owner}:${seq}`;
76
+ }
77
+
78
+ function packOfdPair(owner: number, seq: number): bigint {
79
+ return (bigintConstructor(seq >>> 0) << 32n) | bigintConstructor(owner >>> 0);
80
+ }
81
+
82
+ function unpackOfdPair(packed: bigint): { readonly owner: number; readonly seq: number } | null {
83
+ const seq = numberConstructor((packed >> 32n) & 0xffffffffn);
84
+ if (seq === 0) return null;
85
+ return { owner: numberConstructor(packed & 0xffffffffn), seq };
86
+ }
87
+
88
+ function initOfdCells(
89
+ buffer: SharedArrayBuffer,
90
+ resource: LockableResource,
91
+ identity: FilesystemIdentity,
92
+ ): { cells: Int32Array<SharedArrayBuffer>; idPair: BigUint64Array<SharedArrayBuffer> } {
93
+ const cells = new Int32ArrayConstructor(buffer);
94
+ const idPair = new BigUint64ArrayConstructor(buffer, 16, 1);
95
+ const local = getLocalOfd(resource);
96
+ const originOwner = local ? local.originOwner || identity.owner : identity.owner;
97
+ const originSeq = local ? local.originSeq : nextLocalOfdSeq++;
98
+ atomicsStore(idPair, 0, packOfdPair(originOwner, originSeq));
99
+ atomicsStore(cells, 6, 0);
100
+ if (local?.locked) atomicsStore(cells, 7, OFD_FLAG_LOCKED);
101
+ return { cells, idPair };
102
+ }
103
+
104
+ function sharedOfdId(shared: SharedOfdCells, sessionOwner: number): string {
105
+ const existing = unpackOfdPair(atomicsLoad(shared.idPair, 0));
106
+ if (existing) return formatOfdId(existing.owner, existing.seq);
107
+ const owner = sessionOwner || shared.identity.owner;
108
+ const seq = nextLocalOfdSeq++;
109
+ const packed = packOfdPair(owner, seq);
110
+ const prev = atomicsCompareExchange(shared.idPair, 0, 0n, packed);
111
+ const winner = unpackOfdPair(prev === 0n ? packed : prev)!;
112
+ return formatOfdId(winner.owner, winner.seq);
113
+ }
114
+
115
+ export interface SharedOfdMetadata {
116
+ readonly id: string;
117
+ readonly originOwner: number;
118
+ readonly locked: boolean;
119
+ readonly originClosed: boolean;
120
+ readonly pendingTransfers: number;
121
+ }
122
+
123
+ /** Return (and lazily assign) the open-file-description lock ID for a file or directory resource. */
124
+ export function resourceOfdId(resource: LockableResource, sessionOwner = 0): string {
125
+ const shared = getSharedOfd(resource);
126
+ if (shared) return sharedOfdId(shared, sessionOwner);
127
+ let local = getLocalOfd(resource);
128
+ if (!local) {
129
+ const seq = nextLocalOfdSeq++;
130
+ local = {
131
+ id: formatOfdId(sessionOwner, seq),
132
+ originOwner: sessionOwner,
133
+ originSeq: seq,
134
+ locked: false,
135
+ };
136
+ reflectApply(weakMapSet, localOfds, [resource, local]);
137
+ }
138
+ return local.id;
139
+ }
140
+
141
+ export function markResourceOfdLocked(resource: LockableResource, sessionOwner = 0): string {
142
+ const id = resourceOfdId(resource, sessionOwner);
143
+ const shared = getSharedOfd(resource);
144
+ if (shared) {
145
+ atomicsOr(shared.cells, 7, OFD_FLAG_LOCKED);
146
+ return id;
147
+ }
148
+ const local = getLocalOfd(resource);
149
+ if (local) local.locked = true;
150
+ return id;
151
+ }
152
+
153
+ export function markOriginOfdClosed(resource: LockableResource): void {
154
+ const shared = getSharedOfd(resource);
155
+ if (shared) atomicsOr(shared.cells, 7, OFD_FLAG_ORIGIN_CLOSED);
156
+ }
157
+
158
+ export function resourceOfdLockState(
159
+ resource: LockableResource,
160
+ ): { readonly id: string; readonly shared: boolean; readonly inFlight: boolean } | undefined {
161
+ const shared = getSharedOfd(resource);
162
+ if (shared) {
163
+ if ((atomicsLoad(shared.cells, 7) & OFD_FLAG_LOCKED) === 0) return undefined;
164
+ const pair = unpackOfdPair(atomicsLoad(shared.idPair, 0));
165
+ if (!pair) return undefined;
166
+ return {
167
+ id: formatOfdId(pair.owner, pair.seq),
168
+ shared: true,
169
+ inFlight: atomicsLoad(shared.cells, 6) > 0,
170
+ };
171
+ }
172
+ const local = getLocalOfd(resource);
173
+ if (!local || !local.locked) return undefined;
174
+ return { id: local.id, shared: false, inFlight: false };
175
+ }
176
+
177
+ /** Teardown helper shared by Wasi and fd-reopen for originating file/directory descriptions. */
178
+ export function releaseOriginOfd(fs: FileSystem, resource: LockableResource): void {
179
+ markOriginOfdClosed(resource);
180
+ const lock = resourceOfdLockState(resource);
181
+ if (lock) fs.locks?.releaseOfd(resource.handle, lock.id, lock.inFlight);
182
+ fs.release(resource.handle);
183
+ }
184
+
185
+ /** Record an in-flight exported reservation referencing this shared OFD buffer. */
186
+ export function retainSharedOfdTransfer(buffer: SharedArrayBuffer): void {
187
+ const cells = new Int32ArrayConstructor(buffer);
188
+ atomicsAdd(cells, 6, 1);
189
+ }
190
+
191
+ /** Read shared OFD identity and flags from a promoted open-file control block. */
192
+ export function readSharedOfdBuffer(buffer: SharedArrayBuffer): SharedOfdMetadata | undefined {
193
+ const cells = new Int32ArrayConstructor(buffer);
194
+ if (typedArrayLength(cells) < 8) return undefined;
195
+ const idPair = new BigUint64ArrayConstructor(buffer, 16, 1);
196
+ const pair = unpackOfdPair(atomicsLoad(idPair, 0));
197
+ if (!pair) return undefined;
198
+ const flags = atomicsLoad(cells, 7);
199
+ return {
200
+ id: formatOfdId(pair.owner, pair.seq),
201
+ originOwner: pair.owner,
202
+ locked: (flags & OFD_FLAG_LOCKED) !== 0,
203
+ originClosed: (flags & OFD_FLAG_ORIGIN_CLOSED) !== 0,
204
+ pendingTransfers: atomicsLoad(cells, 6),
205
+ };
206
+ }
207
+
208
+ /** Promote a directory description so inherited copies share one OFD identity and lock lifetime. */
209
+ export function shareDirectoryOfd(
210
+ resource: DirectoryResource,
211
+ identity: FilesystemIdentity,
212
+ buffer?: SharedArrayBuffer,
213
+ ): SharedArrayBuffer {
214
+ const existing = getSharedOfd(resource);
215
+ if (existing) return existing.buffer;
216
+ const shared = buffer ?? allocateSharedControl(32);
217
+ const { cells, idPair } = buffer
218
+ ? {
219
+ cells: new Int32ArrayConstructor(shared),
220
+ idPair: new BigUint64ArrayConstructor(shared, 16, 1),
221
+ }
222
+ : initOfdCells(shared, resource, identity);
223
+ reflectApply(weakMapSet, sharedOfds, [resource, { buffer: shared, cells, idPair, identity }]);
224
+ return shared;
225
+ }
226
+
42
227
  /** Promote the existing description in place, so every local dup keeps the same offset. */
43
228
  export function shareFileResource(
44
229
  resource: FileResource,
@@ -47,14 +232,13 @@ export function shareFileResource(
47
232
  ): SharedArrayBuffer {
48
233
  const existing = getSharedOffset(resource);
49
234
  if (existing) return existing.buffer;
50
- const shared = buffer ?? allocateSharedControl(16);
51
- const cells = new Int32ArrayConstructor(shared);
235
+ const shared = buffer ?? allocateSharedControl(32);
52
236
  const ownerStates = new Int32ArrayConstructor(identity.ownerStates);
53
237
  // Atomics has no Float64 operation. Share the number's bits, not a signed integer
54
238
  // conversion: existing JS offsets above 2^63 must not wrap into negative positions.
55
239
  const offset = new BigUint64ArrayConstructor(shared, 8, 1);
56
240
  const value = new Float64ArrayConstructor(1);
57
- const valueBuffer = reflectApply(typedArrayBufferGetter, value, []) as ArrayBuffer;
241
+ const valueBuffer = typedArrayBuffer(value as unknown as Int32Array) as ArrayBuffer;
58
242
  const bits = new BigUint64ArrayConstructor(valueBuffer);
59
243
  const readOffset = (): number => {
60
244
  bits[0] = atomicsLoad(offset, 0);
@@ -64,14 +248,23 @@ export function shareFileResource(
64
248
  value[0] = next;
65
249
  atomicsStore(offset, 0, bits[0]!);
66
250
  };
67
- const readAppend = (): boolean => (atomicsLoad(cells, 1) & 1) !== 0;
251
+ let cells: Int32Array<SharedArrayBuffer>;
252
+ let idPair: BigUint64Array<SharedArrayBuffer>;
68
253
  if (!buffer) {
254
+ const initialized = initOfdCells(shared, resource, identity);
255
+ cells = initialized.cells;
256
+ idPair = initialized.idPair;
69
257
  atomicsStore(cells, 1, ((resource.fdflags ?? 0) & ~1) | (resource.append ? 1 : 0));
70
258
  writeOffset(resource.offset);
259
+ } else {
260
+ cells = new Int32ArrayConstructor(shared);
261
+ idPair = new BigUint64ArrayConstructor(shared, 16, 1);
71
262
  }
263
+ const readAppend = (): boolean => (atomicsLoad(cells, 1) & 1) !== 0;
72
264
  const state: SharedOffset = {
73
265
  buffer: shared,
74
266
  cells,
267
+ idPair,
75
268
  ownerStates,
76
269
  identity,
77
270
  section: new FilesystemSection(identity),
@@ -105,6 +298,7 @@ export function shareFileResource(
105
298
  } as unknown as PropertyDescriptorMap;
106
299
  objectDefineProperties(resource, descriptors);
107
300
  reflectApply(weakMapSet, offsets, [resource, state]);
301
+ reflectApply(weakMapSet, sharedOfds, [resource, state]);
108
302
  return shared;
109
303
  }
110
304