@waffle-os/runtime 0.0.156 → 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.
- package/browser/filesystem-backend.ts +11 -1
- package/browser/preview-protocol.ts +2 -0
- package/browser/preview.ts +128 -18
- package/browser/runtime-terminals.ts +65 -24
- package/browser/shared-fs.ts +141 -14
- package/browser/worker.ts +73 -6
- package/core/descriptors.ts +19 -2
- package/core/devices.ts +237 -2
- package/core/fd-reopen.ts +105 -20
- package/core/filesystem.ts +71 -0
- package/core/index.ts +4 -0
- package/core/open-file.ts +212 -18
- package/core/wasi.ts +397 -46
- package/dist/browser/filesystem-backend.js +4 -2
- package/dist/browser/preview.js +120 -17
- package/dist/browser/runtime-terminals.js +53 -10
- package/dist/browser/shared-fs.js +124 -11
- package/dist/browser/worker.js +59 -6
- package/dist/core/descriptors.js +9 -1
- package/dist/core/devices.js +211 -1
- package/dist/core/fd-reopen.js +79 -18
- package/dist/core/filesystem.js +13 -0
- package/dist/core/open-file.js +166 -13
- package/dist/core/wasi.js +353 -27
- package/dist/fs/backend.js +29 -2
- package/dist/fs/client.js +199 -6
- package/dist/fs/index.js +21 -1
- package/dist/fs/indexeddb.js +30 -2
- package/dist/fs/locks.js +1112 -0
- package/dist/fs/memory.js +109 -1
- package/dist/fs/multitab-backend.js +315 -195
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +13 -6
- package/dist/fs/protocol.js +9 -3
- package/dist/fs/service.js +386 -22
- package/dist/fs/shared.js +54 -1
- package/dist/fs/storage-geometry.js +2 -0
- package/dist/interrupt/rewriter-internal.js +4 -1
- package/dist/proc/abi.js +80 -6
- package/dist/proc/errors.js +1 -0
- package/dist/proc/filesystem-codec.js +40 -9
- package/dist/proc/index.js +10 -1
- package/dist/proc/pty.js +193 -0
- package/dist/proc/worker-client.js +11 -2
- package/dist/proc/worker-pipes.js +51 -0
- package/dist/runtime.js +11 -2
- package/dist/search/engine.js +53 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +33 -1
- package/fs/client.ts +225 -6
- package/fs/index.ts +22 -1
- package/fs/indexeddb.ts +36 -2
- package/fs/locks.ts +1406 -0
- package/fs/memory.ts +114 -1
- package/fs/multitab-backend.ts +561 -303
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +14 -3
- package/fs/protocol.ts +17 -5
- package/fs/service.ts +468 -23
- package/fs/shared.ts +60 -2
- package/fs/storage-geometry.ts +3 -0
- package/index.ts +2 -0
- package/interrupt/rewriter-internal.ts +4 -1
- package/package.json +2 -2
- package/proc/abi.ts +79 -6
- package/proc/errors.ts +1 -0
- package/proc/filesystem-codec.ts +39 -9
- package/proc/index.ts +11 -0
- package/proc/pty.ts +227 -0
- package/proc/worker-client.ts +28 -6
- package/proc/worker-pipes.ts +57 -0
- package/runtime.ts +12 -2
- package/search/engine.ts +49 -0
- package/testing/indexeddb-storage-browser.ts +173 -2
- 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
|
|
13
|
-
const
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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(
|
|
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 =
|
|
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
|
-
|
|
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
|
|