@waffle-os/runtime 0.0.153 → 0.0.155

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 (61) hide show
  1. package/browser/net/helper.ts +114 -19
  2. package/browser/net/ring.ts +18 -6
  3. package/browser/net/runtime.ts +2 -2
  4. package/browser/notification-relay.ts +105 -18
  5. package/browser/notification-wait.ts +45 -4
  6. package/browser/notification-worker.ts +169 -76
  7. package/browser/preview.ts +15 -2
  8. package/core/filesystem.ts +18 -0
  9. package/core/wasi.ts +75 -4
  10. package/dist/browser/net/helper.js +83 -18
  11. package/dist/browser/net/ring.js +6 -5
  12. package/dist/browser/net/runtime.js +2 -2
  13. package/dist/browser/notification-relay.js +78 -15
  14. package/dist/browser/notification-wait.js +37 -5
  15. package/dist/browser/notification-worker.js +146 -67
  16. package/dist/core/wasi.js +62 -4
  17. package/dist/fs/backend.js +7 -0
  18. package/dist/fs/client.js +75 -17
  19. package/dist/fs/dirty-admission.js +15 -8
  20. package/dist/fs/guest-watch.js +267 -94
  21. package/dist/fs/indexeddb-chunks.js +88 -11
  22. package/dist/fs/indexeddb.js +366 -110
  23. package/dist/fs/memory.js +106 -20
  24. package/dist/fs/multitab-backend.js +291 -74
  25. package/dist/fs/multitab-protocol.js +1 -0
  26. package/dist/fs/persistent.js +70 -16
  27. package/dist/fs/protocol.js +15 -7
  28. package/dist/fs/publication-attachment.js +9 -7
  29. package/dist/fs/service.js +62 -9
  30. package/dist/fs/shared.js +100 -6
  31. package/dist/fs/storage.js +107 -8
  32. package/dist/fs/watch-service.js +97 -2
  33. package/dist/fs/watch-subscriptions.js +121 -8
  34. package/dist/proc/command-stdio.js +1 -1
  35. package/dist/proc/intrinsics/core.js +5 -0
  36. package/dist/proc/intrinsics.js +1 -0
  37. package/dist/tooling/program/program-transformer.json +1 -1
  38. package/fs/backend.ts +17 -0
  39. package/fs/client.ts +99 -16
  40. package/fs/dirty-admission.ts +22 -7
  41. package/fs/guest-watch.ts +299 -103
  42. package/fs/indexeddb-chunks.ts +103 -9
  43. package/fs/indexeddb.ts +477 -121
  44. package/fs/memory.ts +120 -19
  45. package/fs/multitab-backend.ts +440 -95
  46. package/fs/multitab-protocol.ts +1 -0
  47. package/fs/persistent.ts +88 -16
  48. package/fs/protocol.ts +26 -7
  49. package/fs/publication-attachment.ts +9 -7
  50. package/fs/service.ts +62 -7
  51. package/fs/shared.ts +110 -8
  52. package/fs/storage.ts +128 -10
  53. package/fs/watch-service.ts +110 -3
  54. package/fs/watch-subscriptions.ts +115 -7
  55. package/package.json +1 -1
  56. package/proc/command-stdio.ts +1 -1
  57. package/proc/intrinsics/core.ts +7 -0
  58. package/proc/intrinsics.ts +1 -0
  59. package/testing/idle-workspace-cancellation-browser.ts +22 -13
  60. package/testing/indexeddb-storage-browser.ts +564 -1
  61. package/tooling/program/program-transformer.json +1 -1
@@ -7,12 +7,16 @@ import { WatchNames, type WatchNameReservation } from "./watch-names.js";
7
7
  import { WATCH_CAPACITY } from "./watch-ring.js";
8
8
  import type { WatchBatch, WatchEvent } from "./watch.js";
9
9
 
10
- const WIDTH = 48;
10
+ const WIDTH = 56;
11
11
  const EVENT = 32;
12
12
  const HEAD = 20;
13
13
  const TAIL = 24;
14
14
  const COUNT = 28;
15
+ const READY_NEXT = 48;
16
+ const READY_PREV = 52;
15
17
  const OVERFLOW = 2;
18
+ const READY = 4;
19
+ const INACCESSIBLE = 8;
16
20
  const NULL_NAME = 0xffffffff;
17
21
  export interface HeldWatchBatch {
18
22
  readonly batch: WatchBatch;
@@ -24,6 +28,7 @@ export class WatchSubscriptions {
24
28
  private readonly records: WatchArena;
25
29
  private readonly ids: WatchIdIndex;
26
30
  private readonly owners: WatchIdIndex;
31
+ private readonly readyOwners: WatchIdIndex;
27
32
  private readonly events: WatchArena;
28
33
  private readonly names: WatchNames;
29
34
  private tail = 0;
@@ -40,6 +45,7 @@ export class WatchSubscriptions {
40
45
  this.records = new WatchArena(account);
41
46
  this.ids = new WatchIdIndex(account);
42
47
  this.owners = new WatchIdIndex(account);
48
+ this.readyOwners = new WatchIdIndex(account);
43
49
  this.events = new WatchArena(account, "events");
44
50
  this.names = new WatchNames(account, 12288, "events");
45
51
  }
@@ -75,17 +81,23 @@ export class WatchSubscriptions {
75
81
  const record = this.records.stage(slot * WIDTH);
76
82
  let lookup: WatchGrowth | undefined;
77
83
  let owners: WatchGrowth | undefined;
84
+ let readyOwners: WatchGrowth | undefined;
78
85
  try {
79
86
  lookup = this.ids.stage();
80
- if (!this.owners.get(BigInt(owner))) owners = this.owners.stage();
87
+ if (!this.owners.get(BigInt(owner))) {
88
+ owners = this.owners.stage();
89
+ readyOwners = this.readyOwners.stage();
90
+ }
81
91
  this.index.pin(root);
82
92
  record.commit();
83
93
  lookup.commit();
84
94
  owners?.commit();
95
+ readyOwners?.commit();
85
96
  } catch (error) {
86
97
  record.cancel();
87
98
  lookup?.cancel();
88
99
  owners?.cancel();
100
+ readyOwners?.cancel();
89
101
  throw error;
90
102
  }
91
103
  const at = (slot - 1) * WIDTH;
@@ -105,17 +117,99 @@ export class WatchSubscriptions {
105
117
  }
106
118
  private attach(slot: number, root: bigint, owner: number): void {
107
119
  const at = (slot - 1) * WIDTH;
108
- const ownerHead = this.owners.get(BigInt(owner));
120
+ const ownerKey = BigInt(owner);
121
+ const ownerHead = this.owners.get(ownerKey);
109
122
  const rootHead = this.index.watchHead(root);
110
123
  this.records.view.setUint32(at + 32, ownerHead, true);
111
124
  this.records.view.setUint32(at + 40, rootHead, true);
112
125
  if (ownerHead) {
113
126
  this.records.view.setUint32((ownerHead - 1) * WIDTH + 36, slot, true);
114
- this.owners.set(BigInt(owner), slot);
115
- } else this.owners.add(BigInt(owner), slot);
127
+ this.owners.set(ownerKey, slot);
128
+ } else {
129
+ this.owners.add(ownerKey, slot);
130
+ this.readyOwners.add(ownerKey, 0);
131
+ }
116
132
  if (rootHead) this.records.view.setUint32((rootHead - 1) * WIDTH + 44, slot, true);
117
133
  this.index.setWatchHead(root, slot);
118
134
  }
135
+ private markReady(slot: number, owner: number): void {
136
+ const at = (slot - 1) * WIDTH;
137
+ const view = this.records.view;
138
+ const flags = view.getUint32(at + 16, true);
139
+ if (flags & READY) return;
140
+ view.setUint32(at + 16, flags | READY, true);
141
+ const ownerKey = BigInt(owner);
142
+ const head = this.readyOwners.get(ownerKey);
143
+ if (!head) {
144
+ view.setUint32(at + READY_NEXT, slot, true);
145
+ view.setUint32(at + READY_PREV, slot, true);
146
+ this.readyOwners.set(ownerKey, slot);
147
+ return;
148
+ }
149
+ const headAt = (head - 1) * WIDTH;
150
+ const tail = view.getUint32(headAt + READY_PREV, true);
151
+ view.setUint32(at + READY_NEXT, head, true);
152
+ view.setUint32(at + READY_PREV, tail, true);
153
+ view.setUint32((tail - 1) * WIDTH + READY_NEXT, slot, true);
154
+ view.setUint32(headAt + READY_PREV, slot, true);
155
+ }
156
+ private unmarkReady(slot: number, owner: number): void {
157
+ const at = (slot - 1) * WIDTH;
158
+ const view = this.records.view;
159
+ const flags = view.getUint32(at + 16, true);
160
+ if (!(flags & READY)) return;
161
+ view.setUint32(at + 16, flags & ~READY, true);
162
+ const next = view.getUint32(at + READY_NEXT, true);
163
+ const previous = view.getUint32(at + READY_PREV, true);
164
+ const ownerKey = BigInt(owner);
165
+ if (next === slot) {
166
+ this.readyOwners.set(ownerKey, 0);
167
+ } else {
168
+ view.setUint32((previous - 1) * WIDTH + READY_NEXT, next, true);
169
+ view.setUint32((next - 1) * WIDTH + READY_PREV, previous, true);
170
+ if (this.readyOwners.get(ownerKey) === slot) this.readyOwners.set(ownerKey, next);
171
+ }
172
+ view.setUint32(at + READY_NEXT, 0, true);
173
+ view.setUint32(at + READY_PREV, 0, true);
174
+ }
175
+ setInaccessible(owner: number, id: number, inaccessible: boolean): void {
176
+ const at = this.at(owner, id);
177
+ const slot = at / WIDTH + 1;
178
+ const view = this.records.view;
179
+ const flags = view.getUint32(at + 16, true);
180
+ if (inaccessible) {
181
+ view.setUint32(at + 16, flags | INACCESSIBLE, true);
182
+ this.markReady(slot, owner);
183
+ return;
184
+ }
185
+ if (!(flags & INACCESSIBLE)) return;
186
+ view.setUint32(at + 16, flags & ~INACCESSIBLE, true);
187
+ if (!(flags & OVERFLOW) && !view.getUint32(at + COUNT, true)) this.unmarkReady(slot, owner);
188
+ }
189
+ forReady(owner: number, visit: (id: number) => boolean | void): void {
190
+ const ownerKey = BigInt(owner);
191
+ const start = this.readyOwners.get(ownerKey);
192
+ if (!start) return;
193
+ const last = this.records.view.getUint32((start - 1) * WIDTH + READY_PREV, true);
194
+ let slot = start;
195
+ while (slot) {
196
+ const at = (slot - 1) * WIDTH;
197
+ const done = slot === last;
198
+ const next = this.records.view.getUint32(at + READY_NEXT, true);
199
+ const keepGoing = visit(this.records.view.getFloat64(at, true));
200
+ if (keepGoing === false) return;
201
+ if (this.records.view.getUint32(at + 16, true) & READY && next !== slot) {
202
+ this.readyOwners.set(ownerKey, next);
203
+ }
204
+ if (done) return;
205
+ slot = next;
206
+ }
207
+ }
208
+ queuedCount(owner: number, id: number): number {
209
+ const at = this.at(owner, id);
210
+ if (this.records.view.getUint32(at + 16, true) & OVERFLOW) return 0;
211
+ return this.records.view.getUint32(at + COUNT, true);
212
+ }
119
213
  root(owner: number, id: number): bigint {
120
214
  const at = this.at(owner, id);
121
215
  return this.index.id(this.records.view.getUint32(at + 12, true));
@@ -153,6 +247,7 @@ export class WatchSubscriptions {
153
247
  return this.overflow(owner, id);
154
248
  try {
155
249
  this.append(at, event);
250
+ this.markReady(at / WIDTH + 1, owner);
156
251
  } catch (error) {
157
252
  if (!(error instanceof FilesystemError) || (error.errno !== 6 && error.errno !== 48))
158
253
  throw error;
@@ -239,7 +334,9 @@ export class WatchSubscriptions {
239
334
  name: length === NULL_NAME ? null : this.names.read(view.getUint32(at + 28, true), length),
240
335
  };
241
336
  }
242
- batch(owner: number, id: number): HeldWatchBatch {
337
+ batch(owner: number, id: number): HeldWatchBatch;
338
+ batch(owner: number, id: number, deferOnBudget: boolean): HeldWatchBatch | undefined;
339
+ batch(owner: number, id: number, deferOnBudget = false): HeldWatchBatch | undefined {
243
340
  const at = this.at(owner, id);
244
341
  if (this.records.view.getUint32(at + 16, true) & OVERFLOW)
245
342
  return { batch: { kind: "overflow", events: [] }, release() {}, rollback() {} };
@@ -255,6 +352,7 @@ export class WatchSubscriptions {
255
352
  control = this.account.reserveControl(units);
256
353
  } catch (error) {
257
354
  if (!(error instanceof FilesystemError) || error.errno !== 6) throw error;
355
+ if (deferOnBudget) return undefined;
258
356
  this.overflow(owner, id);
259
357
  return { batch: { kind: "overflow", events: [] }, release() {}, rollback() {} };
260
358
  }
@@ -271,6 +369,8 @@ export class WatchSubscriptions {
271
369
  this.records.view.setUint32(at + HEAD, 0, true);
272
370
  this.records.view.setUint32(at + TAIL, 0, true);
273
371
  this.records.view.setUint32(at + COUNT, 0, true);
372
+ if (!(this.records.view.getUint32(at + 16, true) & (OVERFLOW | INACCESSIBLE)))
373
+ this.unmarkReady(at / WIDTH + 1, owner);
274
374
  let held = true;
275
375
  const settle = (rollback: boolean) => {
276
376
  if (!held) return;
@@ -303,6 +403,7 @@ export class WatchSubscriptions {
303
403
  this.records.view.setUint32(at + HEAD, first, true);
304
404
  if (!current) this.records.view.setUint32(at + TAIL, last, true);
305
405
  this.records.view.setUint32(at + COUNT, current + count, true);
406
+ this.markReady(at / WIDTH + 1, owner);
306
407
  return true;
307
408
  }
308
409
 
@@ -330,6 +431,7 @@ export class WatchSubscriptions {
330
431
  this.records.view.getUint32(at + 16, true) | OVERFLOW,
331
432
  true,
332
433
  );
434
+ this.markReady(at / WIDTH + 1, owner);
333
435
  }
334
436
  close(owner: number, id: number): boolean {
335
437
  if (!this.slot(id)) return false;
@@ -348,12 +450,14 @@ export class WatchSubscriptions {
348
450
  this.records.close();
349
451
  this.ids.close();
350
452
  this.owners.close();
453
+ this.readyOwners.close();
351
454
  this.tail = this.free = 0;
352
455
  if (!this.queued) this.resetEvents();
353
456
  }
354
457
  return true;
355
458
  }
356
459
  private detach(slot: number, root: bigint, owner: number): void {
460
+ this.unmarkReady(slot, owner);
357
461
  const at = (slot - 1) * WIDTH;
358
462
  const ownerNext = this.records.view.getUint32(at + 32, true);
359
463
  const ownerPrevious = this.records.view.getUint32(at + 36, true);
@@ -362,7 +466,10 @@ export class WatchSubscriptions {
362
466
  if (ownerPrevious)
363
467
  this.records.view.setUint32((ownerPrevious - 1) * WIDTH + 32, ownerNext, true);
364
468
  else if (ownerNext) this.owners.set(BigInt(owner), ownerNext);
365
- else this.owners.delete(BigInt(owner));
469
+ else {
470
+ this.owners.delete(BigInt(owner));
471
+ this.readyOwners.delete(BigInt(owner));
472
+ }
366
473
  if (ownerNext) this.records.view.setUint32((ownerNext - 1) * WIDTH + 36, ownerPrevious, true);
367
474
  if (rootPrevious) this.records.view.setUint32((rootPrevious - 1) * WIDTH + 40, rootNext, true);
368
475
  else this.index.setWatchHead(root, rootNext);
@@ -387,6 +494,7 @@ export class WatchSubscriptions {
387
494
  this.records.close();
388
495
  this.ids.close();
389
496
  this.owners.close();
497
+ this.readyOwners.close();
390
498
  this.events.close();
391
499
  this.names.close();
392
500
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waffle-os/runtime",
3
- "version": "0.0.153",
3
+ "version": "0.0.155",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/ShaulLavo/waffle.git",
@@ -250,7 +250,7 @@ export class CommandStdio {
250
250
  if (!filesystem)
251
251
  throw new CommandStdioError("EBADF", "File descriptor has no filesystem connection");
252
252
  const offset = shareFileResource(resource, filesystem.connection);
253
- return [new Int32Array(filesystem.connection.signal), new Int32Array(offset, 0, 1)];
253
+ return [new Int32Array(filesystem.connection.signal, 0, 1), new Int32Array(offset, 0, 1)];
254
254
  }
255
255
 
256
256
  private file(
@@ -38,7 +38,14 @@ const mapGetMethod = Map.prototype.get,
38
38
  const mapHasMethod = Map.prototype.has,
39
39
  mapDeleteMethod = Map.prototype.delete;
40
40
  const mapForEachMethod = Map.prototype.forEach;
41
+ const mapValuesMethod = Map.prototype.values;
42
+ const mapIteratorNext = objectGetPrototypeOf(new MapConstructor().values()).next as (
43
+ this: MapIterator<unknown>,
44
+ ) => IteratorResult<unknown>;
41
45
  const mapSizeGetter = descriptor(Map.prototype, "size")!.get!;
46
+ export function mapFirst<V>(map: ReadonlyMap<unknown, V>): V | undefined {
47
+ return apply(mapIteratorNext, apply(mapValuesMethod, map, []), []).value as V | undefined;
48
+ }
42
49
  export function mapGet<K, V>(map: ReadonlyMap<K, V>, key: K): V | undefined {
43
50
  return apply(mapGetMethod, map, [key]);
44
51
  }
@@ -19,6 +19,7 @@ export {
19
19
  mapSet,
20
20
  mapHas,
21
21
  mapDelete,
22
+ mapFirst,
22
23
  mapSize,
23
24
  mapForEach,
24
25
  objectDefineProperty,
@@ -40,6 +40,7 @@ async function cancelledLoad(name: string, phase: "metadata" | "manifest"): Prom
40
40
  const controller = new AbortController();
41
41
  const reason = new Error(`cancel idle ${phase} validation`);
42
42
  const nativeGet = IDBObjectStore.prototype.get;
43
+ const nativeGetAll = IDBObjectStore.prototype.getAll;
43
44
  const nativeClose = IDBDatabase.prototype.close;
44
45
  const nativeOpen = IDBFactory.prototype.open;
45
46
  let settled = false,
@@ -54,27 +55,34 @@ async function cancelledLoad(name: string, phase: "metadata" | "manifest"): Prom
54
55
  event.preventDefault();
55
56
  };
56
57
  globalThis.addEventListener("unhandledrejection", observeRejection);
57
- IDBObjectStore.prototype.get = function (key) {
58
- if (this.name === "data" && typeof key === "string" && controller.signal.aborted)
59
- manifestsAfterAbort++;
60
- const result = nativeGet.call(this, key);
61
- const selected =
62
- phase === "metadata"
63
- ? this.name === "metadata" && key === 0
64
- : this.name === "data" && typeof key === "string";
65
- if (!selected || controller.signal.aborted) return result;
66
- const aborted = this.transaction.onabort;
67
- const completed = this.transaction.oncomplete;
68
- this.transaction.onabort = function (event) {
58
+ /** Cancels once, right after the selected native request was placed. */
59
+ const cancelAfter = (store: IDBObjectStore, selected: boolean) => {
60
+ if (!selected || controller.signal.aborted) return;
61
+ const aborted = store.transaction.onabort;
62
+ const completed = store.transaction.oncomplete;
63
+ store.transaction.onabort = function (event) {
69
64
  settled = true;
70
65
  aborted?.call(this, event);
71
66
  };
72
- this.transaction.oncomplete = function (event) {
67
+ store.transaction.oncomplete = function (event) {
73
68
  settled = true;
74
69
  completed?.call(this, event);
75
70
  };
76
71
  successor = IndexedDBStorage.open(name);
77
72
  controller.abort(reason);
73
+ };
74
+ IDBObjectStore.prototype.get = function (key) {
75
+ if (this.name === "data" && typeof key === "string" && controller.signal.aborted)
76
+ manifestsAfterAbort++;
77
+ const result = nativeGet.call(this, key);
78
+ cancelAfter(this, phase === "metadata" && this.name === "metadata" && key === 0);
79
+ return result;
80
+ };
81
+ // Manifests are validated from one bulk read of the data store's string keys.
82
+ IDBObjectStore.prototype.getAll = function (...args: Parameters<IDBObjectStore["getAll"]>) {
83
+ if (this.name === "data" && controller.signal.aborted) manifestsAfterAbort++;
84
+ const result = nativeGetAll.apply(this, args);
85
+ cancelAfter(this, phase === "manifest" && this.name === "data");
78
86
  return result;
79
87
  };
80
88
  IDBDatabase.prototype.close = function () {
@@ -114,6 +122,7 @@ async function cancelledLoad(name: string, phase: "metadata" | "manifest"): Prom
114
122
  assert(unhandled.length === 0, "cancelled native requests have rejection observers");
115
123
  } finally {
116
124
  IDBObjectStore.prototype.get = nativeGet;
125
+ IDBObjectStore.prototype.getAll = nativeGetAll;
117
126
  IDBDatabase.prototype.close = nativeClose;
118
127
  IDBFactory.prototype.open = nativeOpen;
119
128
  globalThis.removeEventListener("unhandledrejection", observeRejection);