@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.
- 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 +79 -0
- package/core/index.ts +4 -0
- package/core/open-file.ts +212 -18
- package/core/wasi.ts +449 -47
- 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 +14 -0
- package/dist/core/open-file.js +166 -13
- package/dist/core/wasi.js +402 -29
- package/dist/fs/backend.js +51 -3
- package/dist/fs/client.js +204 -7
- package/dist/fs/cold-read-cache.js +23 -0
- 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 +126 -2
- package/dist/fs/multitab-backend.js +317 -196
- package/dist/fs/multitab-protocol.js +2 -0
- package/dist/fs/persistent.js +32 -7
- package/dist/fs/protocol.js +9 -3
- package/dist/fs/service.js +387 -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 +54 -2
- package/fs/client.ts +230 -6
- package/fs/cold-read-cache.ts +38 -0
- package/fs/index.ts +22 -1
- package/fs/indexeddb.ts +36 -2
- package/fs/locks.ts +1406 -0
- package/fs/memory.ts +132 -1
- package/fs/multitab-backend.ts +563 -304
- package/fs/multitab-protocol.ts +2 -0
- package/fs/persistent.ts +46 -4
- package/fs/protocol.ts +17 -5
- package/fs/service.ts +469 -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/dist/core/open-file.js
CHANGED
|
@@ -1,37 +1,180 @@
|
|
|
1
1
|
import { allocateSharedControl } from "../memory/allocator.js";
|
|
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.js";
|
|
2
14
|
import { FilesystemError } from "./filesystem.js";
|
|
3
15
|
import { FilesystemSection, isRevoked } from "../fs/section.js";
|
|
4
|
-
const atomicsLoad = Atomics.load;
|
|
5
|
-
const atomicsStore = Atomics.store;
|
|
6
|
-
const atomicsCompareExchange = Atomics.compareExchange;
|
|
7
16
|
const atomicsOr = Atomics.or;
|
|
8
17
|
const atomicsAnd = Atomics.and;
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const Int32ArrayConstructor = Int32Array;
|
|
18
|
+
const bigintConstructor = BigInt;
|
|
19
|
+
const numberConstructor = Number;
|
|
12
20
|
const BigUint64ArrayConstructor = BigUint64Array;
|
|
13
21
|
const Float64ArrayConstructor = Float64Array;
|
|
14
22
|
const WeakMapConstructor = WeakMap;
|
|
15
23
|
const weakMapGet = WeakMap.prototype.get;
|
|
16
24
|
const weakMapSet = WeakMap.prototype.set;
|
|
17
25
|
const objectDefineProperties = Object.defineProperties;
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const reflectApply = Reflect.apply;
|
|
26
|
+
const OFD_FLAG_LOCKED = 1;
|
|
27
|
+
const OFD_FLAG_ORIGIN_CLOSED = 2;
|
|
21
28
|
const offsets = new WeakMapConstructor;
|
|
29
|
+
const sharedOfds = new WeakMapConstructor;
|
|
30
|
+
const localOfds = new WeakMapConstructor;
|
|
31
|
+
let nextLocalOfdSeq = 1;
|
|
22
32
|
function getSharedOffset(resource) {
|
|
23
33
|
return reflectApply(weakMapGet, offsets, [resource]);
|
|
24
34
|
}
|
|
35
|
+
function getSharedOfd(resource) {
|
|
36
|
+
return reflectApply(weakMapGet, sharedOfds, [resource]);
|
|
37
|
+
}
|
|
38
|
+
function getLocalOfd(resource) {
|
|
39
|
+
return reflectApply(weakMapGet, localOfds, [resource]);
|
|
40
|
+
}
|
|
41
|
+
function formatOfdId(owner, seq) {
|
|
42
|
+
return `${owner}:${seq}`;
|
|
43
|
+
}
|
|
44
|
+
function packOfdPair(owner, seq) {
|
|
45
|
+
return bigintConstructor(seq >>> 0) << 32n | bigintConstructor(owner >>> 0);
|
|
46
|
+
}
|
|
47
|
+
function unpackOfdPair(packed) {
|
|
48
|
+
const seq = numberConstructor(packed >> 32n & 0xffffffffn);
|
|
49
|
+
if (seq === 0)
|
|
50
|
+
return null;
|
|
51
|
+
return { owner: numberConstructor(packed & 0xffffffffn), seq };
|
|
52
|
+
}
|
|
53
|
+
function initOfdCells(buffer, resource, identity) {
|
|
54
|
+
const cells = new Int32ArrayConstructor(buffer);
|
|
55
|
+
const idPair = new BigUint64ArrayConstructor(buffer, 16, 1);
|
|
56
|
+
const local = getLocalOfd(resource);
|
|
57
|
+
const originOwner = local ? local.originOwner || identity.owner : identity.owner;
|
|
58
|
+
const originSeq = local ? local.originSeq : nextLocalOfdSeq++;
|
|
59
|
+
atomicsStore(idPair, 0, packOfdPair(originOwner, originSeq));
|
|
60
|
+
atomicsStore(cells, 6, 0);
|
|
61
|
+
if (local?.locked)
|
|
62
|
+
atomicsStore(cells, 7, OFD_FLAG_LOCKED);
|
|
63
|
+
return { cells, idPair };
|
|
64
|
+
}
|
|
65
|
+
function sharedOfdId(shared, sessionOwner) {
|
|
66
|
+
const existing = unpackOfdPair(atomicsLoad(shared.idPair, 0));
|
|
67
|
+
if (existing)
|
|
68
|
+
return formatOfdId(existing.owner, existing.seq);
|
|
69
|
+
const owner = sessionOwner || shared.identity.owner;
|
|
70
|
+
const seq = nextLocalOfdSeq++;
|
|
71
|
+
const packed = packOfdPair(owner, seq);
|
|
72
|
+
const prev = atomicsCompareExchange(shared.idPair, 0, 0n, packed);
|
|
73
|
+
const winner = unpackOfdPair(prev === 0n ? packed : prev);
|
|
74
|
+
return formatOfdId(winner.owner, winner.seq);
|
|
75
|
+
}
|
|
76
|
+
export function resourceOfdId(resource, sessionOwner = 0) {
|
|
77
|
+
const shared = getSharedOfd(resource);
|
|
78
|
+
if (shared)
|
|
79
|
+
return sharedOfdId(shared, sessionOwner);
|
|
80
|
+
let local = getLocalOfd(resource);
|
|
81
|
+
if (!local) {
|
|
82
|
+
const seq = nextLocalOfdSeq++;
|
|
83
|
+
local = {
|
|
84
|
+
id: formatOfdId(sessionOwner, seq),
|
|
85
|
+
originOwner: sessionOwner,
|
|
86
|
+
originSeq: seq,
|
|
87
|
+
locked: false
|
|
88
|
+
};
|
|
89
|
+
reflectApply(weakMapSet, localOfds, [resource, local]);
|
|
90
|
+
}
|
|
91
|
+
return local.id;
|
|
92
|
+
}
|
|
93
|
+
export function markResourceOfdLocked(resource, sessionOwner = 0) {
|
|
94
|
+
const id = resourceOfdId(resource, sessionOwner);
|
|
95
|
+
const shared = getSharedOfd(resource);
|
|
96
|
+
if (shared) {
|
|
97
|
+
atomicsOr(shared.cells, 7, OFD_FLAG_LOCKED);
|
|
98
|
+
return id;
|
|
99
|
+
}
|
|
100
|
+
const local = getLocalOfd(resource);
|
|
101
|
+
if (local)
|
|
102
|
+
local.locked = true;
|
|
103
|
+
return id;
|
|
104
|
+
}
|
|
105
|
+
export function markOriginOfdClosed(resource) {
|
|
106
|
+
const shared = getSharedOfd(resource);
|
|
107
|
+
if (shared)
|
|
108
|
+
atomicsOr(shared.cells, 7, OFD_FLAG_ORIGIN_CLOSED);
|
|
109
|
+
}
|
|
110
|
+
export function resourceOfdLockState(resource) {
|
|
111
|
+
const shared = getSharedOfd(resource);
|
|
112
|
+
if (shared) {
|
|
113
|
+
if ((atomicsLoad(shared.cells, 7) & OFD_FLAG_LOCKED) === 0)
|
|
114
|
+
return;
|
|
115
|
+
const pair = unpackOfdPair(atomicsLoad(shared.idPair, 0));
|
|
116
|
+
if (!pair)
|
|
117
|
+
return;
|
|
118
|
+
return {
|
|
119
|
+
id: formatOfdId(pair.owner, pair.seq),
|
|
120
|
+
shared: true,
|
|
121
|
+
inFlight: atomicsLoad(shared.cells, 6) > 0
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
const local = getLocalOfd(resource);
|
|
125
|
+
if (!local || !local.locked)
|
|
126
|
+
return;
|
|
127
|
+
return { id: local.id, shared: false, inFlight: false };
|
|
128
|
+
}
|
|
129
|
+
export function releaseOriginOfd(fs, resource) {
|
|
130
|
+
markOriginOfdClosed(resource);
|
|
131
|
+
const lock = resourceOfdLockState(resource);
|
|
132
|
+
if (lock)
|
|
133
|
+
fs.locks?.releaseOfd(resource.handle, lock.id, lock.inFlight);
|
|
134
|
+
fs.release(resource.handle);
|
|
135
|
+
}
|
|
136
|
+
export function retainSharedOfdTransfer(buffer) {
|
|
137
|
+
const cells = new Int32ArrayConstructor(buffer);
|
|
138
|
+
atomicsAdd(cells, 6, 1);
|
|
139
|
+
}
|
|
140
|
+
export function readSharedOfdBuffer(buffer) {
|
|
141
|
+
const cells = new Int32ArrayConstructor(buffer);
|
|
142
|
+
if (typedArrayLength(cells) < 8)
|
|
143
|
+
return;
|
|
144
|
+
const idPair = new BigUint64ArrayConstructor(buffer, 16, 1);
|
|
145
|
+
const pair = unpackOfdPair(atomicsLoad(idPair, 0));
|
|
146
|
+
if (!pair)
|
|
147
|
+
return;
|
|
148
|
+
const flags = atomicsLoad(cells, 7);
|
|
149
|
+
return {
|
|
150
|
+
id: formatOfdId(pair.owner, pair.seq),
|
|
151
|
+
originOwner: pair.owner,
|
|
152
|
+
locked: (flags & OFD_FLAG_LOCKED) !== 0,
|
|
153
|
+
originClosed: (flags & OFD_FLAG_ORIGIN_CLOSED) !== 0,
|
|
154
|
+
pendingTransfers: atomicsLoad(cells, 6)
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
export function shareDirectoryOfd(resource, identity, buffer) {
|
|
158
|
+
const existing = getSharedOfd(resource);
|
|
159
|
+
if (existing)
|
|
160
|
+
return existing.buffer;
|
|
161
|
+
const shared = buffer ?? allocateSharedControl(32);
|
|
162
|
+
const { cells, idPair } = buffer ? {
|
|
163
|
+
cells: new Int32ArrayConstructor(shared),
|
|
164
|
+
idPair: new BigUint64ArrayConstructor(shared, 16, 1)
|
|
165
|
+
} : initOfdCells(shared, resource, identity);
|
|
166
|
+
reflectApply(weakMapSet, sharedOfds, [resource, { buffer: shared, cells, idPair, identity }]);
|
|
167
|
+
return shared;
|
|
168
|
+
}
|
|
25
169
|
export function shareFileResource(resource, identity, buffer) {
|
|
26
170
|
const existing = getSharedOffset(resource);
|
|
27
171
|
if (existing)
|
|
28
172
|
return existing.buffer;
|
|
29
|
-
const shared = buffer ?? allocateSharedControl(
|
|
30
|
-
const cells = new Int32ArrayConstructor(shared);
|
|
173
|
+
const shared = buffer ?? allocateSharedControl(32);
|
|
31
174
|
const ownerStates = new Int32ArrayConstructor(identity.ownerStates);
|
|
32
175
|
const offset = new BigUint64ArrayConstructor(shared, 8, 1);
|
|
33
176
|
const value = new Float64ArrayConstructor(1);
|
|
34
|
-
const valueBuffer =
|
|
177
|
+
const valueBuffer = typedArrayBuffer(value);
|
|
35
178
|
const bits = new BigUint64ArrayConstructor(valueBuffer);
|
|
36
179
|
const readOffset = () => {
|
|
37
180
|
bits[0] = atomicsLoad(offset, 0);
|
|
@@ -41,14 +184,23 @@ export function shareFileResource(resource, identity, buffer) {
|
|
|
41
184
|
value[0] = next;
|
|
42
185
|
atomicsStore(offset, 0, bits[0]);
|
|
43
186
|
};
|
|
44
|
-
|
|
187
|
+
let cells;
|
|
188
|
+
let idPair;
|
|
45
189
|
if (!buffer) {
|
|
190
|
+
const initialized = initOfdCells(shared, resource, identity);
|
|
191
|
+
cells = initialized.cells;
|
|
192
|
+
idPair = initialized.idPair;
|
|
46
193
|
atomicsStore(cells, 1, (resource.fdflags ?? 0) & ~1 | (resource.append ? 1 : 0));
|
|
47
194
|
writeOffset(resource.offset);
|
|
195
|
+
} else {
|
|
196
|
+
cells = new Int32ArrayConstructor(shared);
|
|
197
|
+
idPair = new BigUint64ArrayConstructor(shared, 16, 1);
|
|
48
198
|
}
|
|
199
|
+
const readAppend = () => (atomicsLoad(cells, 1) & 1) !== 0;
|
|
49
200
|
const state = {
|
|
50
201
|
buffer: shared,
|
|
51
202
|
cells,
|
|
203
|
+
idPair,
|
|
52
204
|
ownerStates,
|
|
53
205
|
identity,
|
|
54
206
|
section: new FilesystemSection(identity),
|
|
@@ -82,6 +234,7 @@ export function shareFileResource(resource, identity, buffer) {
|
|
|
82
234
|
};
|
|
83
235
|
objectDefineProperties(resource, descriptors);
|
|
84
236
|
reflectApply(weakMapSet, offsets, [resource, state]);
|
|
237
|
+
reflectApply(weakMapSet, sharedOfds, [resource, state]);
|
|
85
238
|
return shared;
|
|
86
239
|
}
|
|
87
240
|
export function claimFileOffset(resource, holdSection = false) {
|