@waffle-os/runtime 0.0.153 → 0.0.154

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 (48) 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 +4 -0
  9. package/core/wasi.ts +49 -0
  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 +40 -0
  17. package/dist/fs/backend.js +7 -0
  18. package/dist/fs/client.js +69 -15
  19. package/dist/fs/dirty-admission.js +15 -8
  20. package/dist/fs/indexeddb.js +288 -96
  21. package/dist/fs/memory.js +106 -20
  22. package/dist/fs/multitab-backend.js +121 -37
  23. package/dist/fs/multitab-protocol.js +1 -0
  24. package/dist/fs/persistent.js +70 -16
  25. package/dist/fs/protocol.js +15 -7
  26. package/dist/fs/publication-attachment.js +9 -7
  27. package/dist/fs/service.js +34 -9
  28. package/dist/fs/shared.js +96 -6
  29. package/dist/proc/command-stdio.js +1 -1
  30. package/dist/tooling/program/program-transformer.json +1 -1
  31. package/fs/backend.ts +11 -0
  32. package/fs/client.ts +93 -16
  33. package/fs/dirty-admission.ts +22 -7
  34. package/fs/guest-watch.ts +1 -1
  35. package/fs/indexeddb.ts +379 -110
  36. package/fs/memory.ts +120 -19
  37. package/fs/multitab-backend.ts +206 -61
  38. package/fs/multitab-protocol.ts +1 -0
  39. package/fs/persistent.ts +82 -16
  40. package/fs/protocol.ts +26 -7
  41. package/fs/publication-attachment.ts +9 -7
  42. package/fs/service.ts +35 -7
  43. package/fs/shared.ts +106 -8
  44. package/package.json +1 -1
  45. package/proc/command-stdio.ts +1 -1
  46. package/testing/idle-workspace-cancellation-browser.ts +22 -13
  47. package/testing/indexeddb-storage-browser.ts +435 -1
  48. package/tooling/program/program-transformer.json +1 -1
package/fs/client.ts CHANGED
@@ -103,6 +103,8 @@ import {
103
103
  reapFilesystem,
104
104
  unpack,
105
105
  wake,
106
+ wakeTransport,
107
+ SIGNAL_TRANSPORT,
106
108
  releaseFilesystemReservation,
107
109
  type FilesystemIdentity,
108
110
  type FilesystemReservation,
@@ -196,6 +198,19 @@ export interface DirectDisabledIOControl {
196
198
  deferWrite?(completion: FilesystemCompletion): void;
197
199
  }
198
200
 
201
+ /** Operations whose inline replies the service keeps nothing for. */
202
+ const PLAIN_REPLIES = new SetConstructor([
203
+ "stat",
204
+ "create",
205
+ "write",
206
+ "append",
207
+ "grow",
208
+ "resize",
209
+ "setTimes",
210
+ "setMode",
211
+ "read",
212
+ "readlink",
213
+ ]);
199
214
  export class FilesystemCompletion {
200
215
  static {
201
216
  waitForObservedResolve = (completion, cancellation) => completion.#wait(cancellation);
@@ -209,6 +224,8 @@ export class FilesystemCompletion {
209
224
  private loading?: Promise<unknown>;
210
225
  private consumed = false;
211
226
  private released = false;
227
+ /** Whether the service may keep state for this reply until its discard. */
228
+ retained = true;
212
229
  constructor(
213
230
  readonly id: number,
214
231
  readonly buffer: FilesystemRequestBuffer,
@@ -275,7 +292,9 @@ export class FilesystemCompletion {
275
292
  // release cancels that value and notifies its waiters before removing the completion.
276
293
  if (!this.consumed) this.cancel();
277
294
  else {
278
- this.client.connection.port.postMessage({ discard: this.id });
295
+ // The service keeps nothing for a plain inline reply, so its discard would do nothing.
296
+ if (this.retained || atomicsLoad(this.cells, 0) !== State.Complete)
297
+ this.client.connection.port.postMessage({ discard: this.id });
279
298
  this.notify();
280
299
  }
281
300
  mapDelete(this.client.completions, this.id);
@@ -291,6 +310,7 @@ export class FilesystemCompletion {
291
310
  }
292
311
  arrayAppend(this.backendWaiters, callback);
293
312
  this.client.watchRevocation();
313
+ this.client.watchBackendSettlement();
294
314
  if (this.backendSettled) this.notify();
295
315
  }
296
316
  /** If the service already published State.Complete, decode and return the inline result even
@@ -587,7 +607,9 @@ export class FilesystemClient implements FileSystem {
587
607
  private capabilities: Int32Array;
588
608
  private epoch: Int32Array;
589
609
  /** Shared wake counter, viewed once: a blocking wait reads it on every loop. */
610
+ /** Word 0 of the shared signal, one word long, as poll's wake-word checks require. */
590
611
  readonly signal: Int32Array;
612
+ private readonly transportSignal: Int32Array;
591
613
  private revoked: () => boolean;
592
614
  private readonly section: FilesystemSection;
593
615
  readonly shared: SharedFilesystemState;
@@ -607,7 +629,8 @@ export class FilesystemClient implements FileSystem {
607
629
  this.requests = new FilesystemRequestAllocator(connection.requests);
608
630
  this.capabilities = new Int32ArrayConstructor(connection.capabilities, 0, CAPABILITY_LENGTH);
609
631
  this.epoch = new Int32ArrayConstructor(connection.epoch);
610
- this.signal = new Int32ArrayConstructor(connection.signal);
632
+ this.signal = new Int32ArrayConstructor(connection.signal, 0, 1);
633
+ this.transportSignal = new Int32ArrayConstructor(connection.signal, SIGNAL_TRANSPORT * 4, 1);
611
634
  const revoked = sharedRevoked(connection);
612
635
  this.revoked = () => this.disposed || revoked() || !!this.transportError();
613
636
  if (connection.residentPublication)
@@ -633,7 +656,7 @@ export class FilesystemClient implements FileSystem {
633
656
  };
634
657
  connection.port.onmessageerror = () => {
635
658
  closeRequestMemory(this.requests.memory);
636
- wake(connection);
659
+ wakeTransport(connection);
637
660
  mapForEach(this.completions, (completion) => completion.notify());
638
661
  connection.port.postMessage({ transportClosed: true });
639
662
  connection.port.close();
@@ -696,7 +719,9 @@ export class FilesystemClient implements FileSystem {
696
719
  return client;
697
720
  }
698
721
  /** A single shared-signal waiter handles service death without a port completion message.
699
- * Normal replies still use the port; the watcher exits when the last completion is released.
722
+ * Normal replies still use the port. The waiter parks on the transport word, or on word 0
723
+ * while a deferred write waits for its backend, and is checked again on each wake; one left
724
+ * parked after the last completion is released serves the next request.
700
725
  */
701
726
  private disposed = false;
702
727
  private finishedDispose = false;
@@ -769,24 +794,55 @@ export class FilesystemClient implements FileSystem {
769
794
  this.watchingRevocation = true;
770
795
  this.monitorRevocation();
771
796
  }
797
+ /** A deferred write needs the backend's terminal state even if its port reply is lost, and
798
+ * that state wakes word 0 only. Move a waiter parked on the transport word over to it. */
799
+ watchBackendSettlement(): void {
800
+ this.backendWaiterVersion++;
801
+ if (this.revocationWord === this.transportSignal) atomicsNotify(this.transportSignal, 0);
802
+ }
803
+ /** The word the monitor is parked on, or undefined while it runs or is stopped. */
804
+ private revocationWord: Int32Array | undefined;
805
+ /** Bumped by every backend-waiter registration. A pass that saw it change, for example a
806
+ * settlement callback registering the next deferred write, chooses its word again. */
807
+ private backendWaiterVersion = 0;
808
+ private unsettledBackendWaiter(): boolean {
809
+ let found = false;
810
+ mapForEach(this.completions, (completion) => {
811
+ if (completion.hasBackendWaiters && !completion.backendSettled) found = true;
812
+ });
813
+ return found;
814
+ }
815
+ /** Completions arrive as port messages, so the waiter needs only revocation, reap, retirement
816
+ * and transport failure, which all wake SIGNAL_TRANSPORT. Two cases keep word 0: a deferred
817
+ * write's backend settlement publishes only there, and a publication domain's stop wakes
818
+ * nothing itself. */
819
+ private revocationSignal(): Int32Array {
820
+ return this.connection.publication || this.unsettledBackendWaiter()
821
+ ? this.signal
822
+ : this.transportSignal;
823
+ }
772
824
  private monitorRevocation(): void {
773
- const signal = this.signal;
774
825
  const stop = () => {
826
+ this.revocationWord = undefined;
775
827
  this.watchingRevocation = false;
776
828
  };
777
829
  const step = () => {
830
+ this.revocationWord = undefined;
778
831
  try {
779
832
  while (mapSize(this.completions)) {
833
+ const version = this.backendWaiterVersion;
834
+ const signal = this.revocationSignal();
780
835
  const observed = atomicsLoad(signal, 0);
781
- if (this.revoked()) {
782
- if (!this.sweepRevokedCompletions()) break;
783
- } else {
784
- this.notifySettledBackendWaiters();
785
- }
786
- if (!mapSize(this.completions)) break;
787
- const waiting = atomicsWaitAsync(signal, 0, observed);
788
- if (waiting.async) {
789
- afterNativePromise(waiting.value, step, stop);
836
+ let waiting = true;
837
+ if (this.revoked()) waiting = this.sweepRevokedCompletions();
838
+ else this.notifySettledBackendWaiters();
839
+ // Callbacks run above can register waiters; neither park nor stop on a stale choice.
840
+ if (version !== this.backendWaiterVersion) continue;
841
+ if (!waiting || !mapSize(this.completions)) break;
842
+ const parked = atomicsWaitAsync(signal, 0, observed);
843
+ if (parked.async) {
844
+ this.revocationWord = signal;
845
+ afterNativePromise(parked.value, step, stop);
790
846
  return;
791
847
  }
792
848
  }
@@ -957,14 +1013,28 @@ export class FilesystemClient implements FileSystem {
957
1013
  capacity: number,
958
1014
  publication?: PublicationAdmission,
959
1015
  ): FilesystemCompletion {
960
- const input = pack({ operation, args, publication });
1016
+ // An absent admission stays absent: encoding undefined would tag the envelope and send every
1017
+ // request down the service's tagged-value decode.
1018
+ const input = pack(publication ? { operation, args, publication } : { operation, args });
961
1019
  this.section.enter();
962
1020
  try {
963
- return this.postRequest(input, data, capacity, publication);
1021
+ const completion = this.postRequest(input, data, capacity, publication);
1022
+ completion.retained = this.retainsReply(operation);
1023
+ return completion;
964
1024
  } finally {
965
1025
  this.section.exit();
966
1026
  }
967
1027
  }
1028
+ /** The service keeps a reply until its discard for a resolve (its held references), a stage,
1029
+ * a walk, a watch, a workspace read and any overflowing reply. Only these operations are known
1030
+ * to keep nothing for an inline reply; the caller still checks overflow from the reply state. */
1031
+ private retainsReply(operation: string): boolean {
1032
+ return (
1033
+ !setHas(PLAIN_REPLIES, operation) ||
1034
+ this.connection.residentPublication !== undefined ||
1035
+ this.shared.publication !== undefined
1036
+ );
1037
+ }
968
1038
  private postRequest(
969
1039
  input: Uint8Array,
970
1040
  data: Uint8Array,
@@ -2529,6 +2599,13 @@ export class FilesystemClient implements FileSystem {
2529
2599
  return errno(error);
2530
2600
  }
2531
2601
  }
2602
+ realpath(directory: FileHandle, path: string, rooted = false): string | number {
2603
+ try {
2604
+ return this.call("realpath", [directory, path, rooted]);
2605
+ } catch (error) {
2606
+ return errno(error);
2607
+ }
2608
+ }
2532
2609
  readlink(handle: FileHandle): string {
2533
2610
  this.retain(handle);
2534
2611
  try {
@@ -151,14 +151,25 @@ export class DirtyAdmission {
151
151
  get revision(): number {
152
152
  return this.values[REVISION]!;
153
153
  }
154
- clean(revision: number, namespace: number): boolean {
155
- if (!this.enabled || this.revision !== revision) return false;
156
- this.values[DIRTY_BYTES] = 0;
157
- this.values[DIRTY_METADATA] = 0;
154
+ releaseSlot(bytes: number, metadata: number): void {
155
+ if (!this.enabled || (!bytes && !metadata)) return;
156
+ this.values[DIRTY_BYTES] = Math.max(0, this.values[DIRTY_BYTES]! - bytes);
157
+ this.values[DIRTY_METADATA] = Math.max(0, this.values[DIRTY_METADATA]! - metadata);
158
+ this.values[REVISION]!++;
159
+ this.notify();
160
+ }
161
+ reconcile(
162
+ namespace: number,
163
+ dirtyNamespace: number,
164
+ dirtyBytes: number,
165
+ dirtyMetadata: number,
166
+ ): void {
167
+ if (!this.enabled) return;
168
+ this.values[DIRTY_BYTES] = dirtyBytes;
169
+ this.values[DIRTY_METADATA] = dirtyMetadata;
158
170
  this.values[NAMESPACE] = namespace;
159
- this.values[BASE_NAMESPACE] = namespace;
171
+ this.values[BASE_NAMESPACE] = namespace - dirtyNamespace;
160
172
  this.notify();
161
- return true;
162
173
  }
163
174
  private notify(): void {
164
175
  Atomics.add(this.signals, 0, 1);
@@ -183,7 +194,11 @@ export class DirtyAdmission {
183
194
  }
184
195
  }
185
196
  get hasDebt(): boolean {
186
- return this.values[DIRTY_BYTES]! > 0 || this.values[DIRTY_METADATA]! > 0;
197
+ return (
198
+ this.values[DIRTY_BYTES]! > 0 ||
199
+ this.values[DIRTY_METADATA]! > 0 ||
200
+ this.values[NAMESPACE]! > this.values[BASE_NAMESPACE]!
201
+ );
187
202
  }
188
203
  capacityReleased(): void {
189
204
  if (this.enabled) this.notify();
package/fs/guest-watch.ts CHANGED
@@ -26,7 +26,7 @@ const encoder = new TextEncoder();
26
26
  export interface GuestWatchReadiness {
27
27
  /** At least one open watch has not reached its end. */
28
28
  readonly active: boolean;
29
- /** The root's watch cells; index 3 moves on every committed watch record. */
29
+ /** The root's watch cells; index 3 moves on every committed watch record and revocation. */
30
30
  readonly word: Int32Array;
31
31
  /** Unrevoked, unended watches have queued records or an unobserved commit. */
32
32
  pending(): boolean;