@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
@@ -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 atomicsNotify = Atomics.notify;
10
- const atomicsWait = Atomics.wait;
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 typedArrayPrototype = Object.getPrototypeOf(Int32Array.prototype);
19
- const typedArrayBufferGetter = Object.getOwnPropertyDescriptor(typedArrayPrototype, "buffer").get;
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(16);
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 = reflectApply(typedArrayBufferGetter, value, []);
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
- const readAppend = () => (atomicsLoad(cells, 1) & 1) !== 0;
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) {