@waffle-os/runtime 0.0.145 → 0.0.146

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 (133) hide show
  1. package/README.md +32 -0
  2. package/browser/command-input.ts +86 -0
  3. package/browser/command-owner.ts +5 -0
  4. package/browser/display/guest.ts +2 -18
  5. package/browser/display/surface-info.ts +19 -0
  6. package/browser/operations/cells.ts +90 -0
  7. package/browser/operations/guest.ts +94 -0
  8. package/browser/operations/owner.ts +201 -0
  9. package/browser/operations/protocol.ts +16 -0
  10. package/browser/operations/types.ts +41 -0
  11. package/browser/operations/writer.ts +106 -0
  12. package/browser/preview1-worker.ts +8 -1
  13. package/browser/process-broker.ts +372 -22
  14. package/browser/process-start.ts +5 -2
  15. package/browser/run-command.ts +388 -0
  16. package/browser/shared-fs.ts +20 -3
  17. package/browser/worker-memory.ts +6 -0
  18. package/browser/worker.ts +121 -27
  19. package/dist/browser/command-input.js +82 -0
  20. package/dist/browser/command-owner.js +2 -0
  21. package/dist/browser/display/guest.js +2 -18
  22. package/dist/browser/display/surface-info.js +18 -0
  23. package/dist/browser/operations/cells.js +80 -0
  24. package/dist/browser/operations/guest.js +67 -0
  25. package/dist/browser/operations/owner.js +175 -0
  26. package/dist/browser/operations/protocol.js +0 -0
  27. package/dist/browser/operations/types.js +0 -0
  28. package/dist/browser/operations/writer.js +98 -0
  29. package/dist/browser/preview1-worker.js +4 -0
  30. package/dist/browser/process-broker.js +317 -18
  31. package/dist/browser/process-start.js +3 -0
  32. package/dist/browser/run-command.js +377 -0
  33. package/dist/browser/shared-fs.js +8 -3
  34. package/dist/browser/worker-memory.js +10 -1
  35. package/dist/browser/worker.js +83 -28
  36. package/dist/fs/client.js +81 -18
  37. package/dist/fs/resolve-observation.js +11 -0
  38. package/dist/fs/service.js +51 -4
  39. package/dist/proc/command-transfer.js +72 -0
  40. package/dist/proc/directory.js +9 -1
  41. package/dist/proc/intrinsics/host.js +4 -1
  42. package/dist/proc/intrinsics.js +1 -0
  43. package/dist/proc/pipes.js +37 -4
  44. package/dist/proc/registry.js +25 -14
  45. package/dist/proc/start-command.js +101 -0
  46. package/dist/proc/supervisor.js +34 -10
  47. package/dist/proc/worker-broker.js +86 -31
  48. package/dist/proc/worker-client.js +67 -31
  49. package/dist/programs/collection.js +2 -0
  50. package/dist/runtime.js +14 -1
  51. package/dist/tooling/program/program-transformer.json +1 -1
  52. package/fs/client.ts +113 -17
  53. package/fs/resolve-observation.ts +25 -0
  54. package/fs/service.ts +63 -4
  55. package/index.ts +5 -0
  56. package/package.json +9 -1
  57. package/proc/command-transfer.ts +96 -0
  58. package/proc/directory.ts +10 -1
  59. package/proc/intrinsics/host.ts +5 -1
  60. package/proc/intrinsics.ts +1 -0
  61. package/proc/pipes.ts +39 -6
  62. package/proc/registry.ts +52 -17
  63. package/proc/start-command.ts +157 -0
  64. package/proc/supervisor.ts +34 -9
  65. package/proc/worker-broker.ts +100 -38
  66. package/proc/worker-client.ts +84 -31
  67. package/programs/collection.ts +4 -0
  68. package/runtime.ts +44 -3
  69. package/scripts/test-browser.ts +49 -29
  70. package/tests/browser-controls-discovery.test.ts +2 -10
  71. package/tests/browser-matrix.test.ts +105 -59
  72. package/tests/browser-navigation.test.ts +1 -1
  73. package/tests/browser-runner-lifecycle.ts +23 -13
  74. package/tests/command-input.test.ts +40 -0
  75. package/tests/command-scope.test.ts +422 -0
  76. package/tests/filesystem-client-intrinsics.test.ts +2 -0
  77. package/tests/filesystem-refcount.test.ts +2 -0
  78. package/tests/filesystem-resolve-observation.test.ts +185 -0
  79. package/tests/fixtures/command-bounded-admission.ts +31 -0
  80. package/tests/fixtures/command-disposal.ts +47 -0
  81. package/tests/fixtures/command-guests.ts +123 -0
  82. package/tests/fixtures/operation-fs-worker.ts +114 -0
  83. package/tests/fixtures/operation-observer-worker.ts +41 -0
  84. package/tests/fixtures/operation-page.ts +522 -0
  85. package/tests/fixtures/process-start-worker.ts +32 -0
  86. package/tests/link-browser-runner.ts +95 -22
  87. package/tests/node-exports.test.ts +29 -0
  88. package/tests/operation-diagnostics-browser.ts +192 -0
  89. package/tests/operation-diagnostics.test.ts +291 -0
  90. package/tests/page-runner/browser-controls-process-fixture.ts +24 -0
  91. package/tests/page-runner/browser-controls-runner.test.ts +20 -0
  92. package/tests/page-runner/browser-controls-runner.ts +1 -0
  93. package/tests/page-runner/browser-controls-suite.ts +20 -3
  94. package/tests/page-runner/control-run.ts +9 -1
  95. package/tests/page-runner/driver.ts +2 -0
  96. package/tests/page-runner/playwright-driver.ts +1 -1
  97. package/tests/page-runner/suite-host.ts +26 -1
  98. package/tests/page-runner/suite-startup.test.ts +39 -0
  99. package/tests/page-runner/threads-runtime-browser.test.ts +4 -6
  100. package/tests/page-runner/threads-runtime-suite.test.ts +14 -5
  101. package/tests/page-runner/worker-capability-controls.ts +174 -15
  102. package/tests/pipe-reader-reuse-races.test.ts +44 -0
  103. package/tests/process-start-browser-runner.ts +12 -4
  104. package/tests/process-start-browser.ts +66 -5
  105. package/tests/program-build-tools.test.ts +1 -1
  106. package/tests/runtime-facade.test.ts +5 -0
  107. package/tests/start-command.test.ts +109 -0
  108. package/tests/threads-guest-fixture.ts +1 -1
  109. package/tests/threads-guest-module.ts +6 -3
  110. package/tests/threads-guest.test.ts +25 -0
  111. package/tests/threads-runtime-browser-runner.ts +36 -13
  112. package/tests/threads-runtime-browser.ts +231 -92
  113. package/tests/threads-runtime-cases.ts +18 -1
  114. package/tests/threads-runtime-cleanup.ts +5 -1
  115. package/tests/threads-runtime-fd-browser.ts +247 -0
  116. package/tests/threads-runtime-fd-control.ts +3 -385
  117. package/tests/threads-runtime-fd-descriptors.test.ts +15 -13
  118. package/tests/threads-runtime-fd-module.ts +13 -2
  119. package/tests/threads-runtime-fd-service.ts +23 -2
  120. package/tests/threads-runtime-growth-worker.ts +61 -0
  121. package/tests/threads-runtime-host.ts +17 -4
  122. package/tests/threads-runtime-modules.ts +23 -5
  123. package/tests/threads-runtime-regression-controls.ts +386 -0
  124. package/tests/threads-runtime-regression-modules.ts +242 -0
  125. package/tests/threads-runtime-service-barrier.ts +8 -1
  126. package/tests/webkit-diagnostics.ts +125 -0
  127. package/tests/webkit-error-output.test.ts +118 -0
  128. package/tests/worker-memory-bootstrap.test.ts +19 -1
  129. package/tests/worker-memory-broker.test.ts +16 -8
  130. package/tooling/ci-paths.test.ts +329 -61
  131. package/tooling/private-authority.ts +14 -0
  132. package/tooling/program/program-transformer.json +1 -1
  133. package/tooling/tarball-smoke.ts +3 -1
package/dist/fs/client.js CHANGED
@@ -1,9 +1,11 @@
1
1
  import { allocateSharedControl } from "../memory/control.js";
2
+ import { isInstance } from "../proc/intrinsics/core.js";
2
3
  import { FilesystemWatch } from "./watch.js";
3
4
  import { checkDirectoryPage } from "./directory-pages.js";
4
5
  import { DirtyCapacityError } from "./dirty-admission.js";
5
6
  import { ResidentAttachment, stopWorkspaceBridge } from "./publication-attachment.js";
6
7
  import { localPublication } from "./publication-internal.js";
8
+ import { takeResolveObservation } from "./resolve-observation.js";
7
9
  import {
8
10
  FilesystemRequestAllocator,
9
11
  FilesystemTransportError,
@@ -130,12 +132,16 @@ function transferList(payload) {
130
132
  };
131
133
  return { __proto__: null, [symbolIterator]: () => iterator };
132
134
  }
135
+ let waitForObservedResolve;
133
136
 
134
137
  export class FilesystemCompletion {
135
138
  id;
136
139
  buffer;
137
140
  client;
138
141
  publication;
142
+ static {
143
+ waitForObservedResolve = (completion, cancellation) => completion.#wait(cancellation);
144
+ }
139
145
  cells;
140
146
  changed = [];
141
147
  cancellations = new SetConstructor;
@@ -200,12 +206,16 @@ export class FilesystemCompletion {
200
206
  this.consumed = true;
201
207
  this.release();
202
208
  }
203
- check() {
209
+ check(cancellation) {
204
210
  const failure = this.client.transportError();
205
211
  if (failure)
206
212
  throw failure;
207
- if (isRevoked(this.client.connection) || requestCancelled(this.buffer) || this.cells[0] === State.Cancelled)
208
- throw new FilesystemError(11);
213
+ if (isRevoked(this.client.connection) || requestCancelled(this.buffer) || this.cells[0] === State.Cancelled) {
214
+ const error = new FilesystemError(11);
215
+ if (cancellation)
216
+ cancellation.error = error;
217
+ throw error;
218
+ }
209
219
  if (this.cells[0] === State.Failed)
210
220
  throw new FilesystemError(this.cells[3]);
211
221
  }
@@ -218,28 +228,42 @@ export class FilesystemCompletion {
218
228
  return new Uint8ArrayConstructor(typedArraySubarray(requestBytes(this.buffer), 0, available));
219
229
  }
220
230
  wait() {
231
+ return this.#wait();
232
+ }
233
+ #wait(cancellation) {
221
234
  const signal = new Int32ArrayConstructor(this.client.connection.signal);
222
235
  while (!this.ready) {
223
236
  const observed = atomicsLoad(signal, 0);
224
- this.check();
237
+ this.check(cancellation);
225
238
  if (!this.ready)
226
239
  atomicsWait(signal, 0, observed);
227
240
  }
228
- this.check();
241
+ this.check(cancellation);
229
242
  if (this.consumed)
230
243
  return this.decoded;
231
244
  const size = this.cells[2];
232
245
  let bytes = this.inlineBytes();
233
246
  if (this.cells[0] === State.Overflow) {
234
247
  bytes = new Uint8ArrayConstructor(size);
235
- for (let at = 0;at < size; at += PAGE)
236
- bytes.set(this.client.call("take", [this.id, at, PAGE]), at);
248
+ for (let at = 0;at < size; at += PAGE) {
249
+ const args = [this.id, at, PAGE];
250
+ const part = cancellation ? this.#takeObserved(args, cancellation) : this.client.call("take", args);
251
+ bytes.set(part, at);
252
+ }
237
253
  }
238
- this.check();
254
+ this.check(cancellation);
239
255
  this.decoded = this.decode(bytes);
240
256
  this.consumed = true;
241
257
  return this.decoded;
242
258
  }
259
+ #takeObserved(args, cancellation) {
260
+ const part = this.client.submit("take", args);
261
+ try {
262
+ return part.#wait(cancellation);
263
+ } finally {
264
+ part.release();
265
+ }
266
+ }
243
267
  async value() {
244
268
  if (!this.ready)
245
269
  await new Promise((resolve) => {
@@ -330,6 +354,19 @@ function clientWatches(cells, revoked, call) {
330
354
  }
331
355
  };
332
356
  }
357
+ function observeResolveWait(completion, writer) {
358
+ const cancellation = {};
359
+ writer.begin(completion.id);
360
+ let value;
361
+ try {
362
+ value = waitForObservedResolve(completion, cancellation);
363
+ } catch (error) {
364
+ writer.settle(isInstance(error, FilesystemTransportError) ? "transportError" : cancellation.error && error === cancellation.error ? "filesystemCancelled" : "resolveError");
365
+ throw error;
366
+ }
367
+ writer.settle(value && typeof value === "object" && typeof value.parent === "number" ? "completed" : "resolveError");
368
+ return value;
369
+ }
333
370
  function retiredLocalRelease(connection, error) {
334
371
  if (!(error instanceof FilesystemTransportError) || error.reason !== "stale")
335
372
  return false;
@@ -366,9 +403,12 @@ export class FilesystemClient {
366
403
  watchingRevocation = false;
367
404
  resident;
368
405
  workspaceStop;
406
+ #resolveCold;
369
407
  constructor(connection, options = {}) {
370
408
  this.connection = connection;
371
409
  this.options = options;
410
+ const observation = takeResolveObservation(connection);
411
+ this.#resolveCold = observation ? (key, args) => this.#resolveObserved(key, args, observation) : this.#resolveUnobserved;
372
412
  this.requests = new FilesystemRequestAllocator(connection.requests);
373
413
  this.capabilities = new Int32ArrayConstructor(connection.capabilities, 0, CAPABILITY_LENGTH);
374
414
  this.epoch = new Int32ArrayConstructor(connection.epoch);
@@ -394,6 +434,23 @@ export class FilesystemClient {
394
434
  };
395
435
  connection.port.start();
396
436
  }
437
+ #resolveObserved(key, args, { entered, writer }) {
438
+ if (!entered())
439
+ return this.#resolveUnobserved(key, args);
440
+ let completion;
441
+ try {
442
+ completion = this.submit("resolve", args);
443
+ } catch (error) {
444
+ writer.submissionFailed(isInstance(error, FilesystemTransportError) ? "transportError" : "submissionError");
445
+ throw error;
446
+ }
447
+ try {
448
+ const resolved = observeResolveWait(completion, writer);
449
+ return this.#cacheResolved(key, completion.epoch, resolved);
450
+ } finally {
451
+ completion.release();
452
+ }
453
+ }
397
454
  transportError() {
398
455
  return requestTransportError(this.requests.memory);
399
456
  }
@@ -1259,20 +1316,26 @@ export class FilesystemClient {
1259
1316
  const cached = this.paths.get(key);
1260
1317
  if (!this.resident && !this.shared.publication && !(epoch & 1) && cached?.epoch === epoch)
1261
1318
  return typeof cached.path === "number" ? cached.path : { ...cached.path };
1262
- const completion = this.submit("resolve", [directory, path, followFinal]);
1263
- try {
1264
- const resolved = completion.wait();
1265
- if (this.paths.size >= 256)
1266
- this.paths.clear();
1267
- this.paths.set(key, { path: resolved, epoch: completion.epoch });
1268
- return typeof resolved === "number" ? resolved : { ...resolved };
1269
- } finally {
1270
- completion.release();
1271
- }
1319
+ return this.#resolveCold(key, [directory, path, followFinal]);
1272
1320
  } catch (error) {
1273
1321
  return errno(error);
1274
1322
  }
1275
1323
  }
1324
+ #resolveUnobserved(key, args) {
1325
+ const completion = this.submit("resolve", args);
1326
+ try {
1327
+ const resolved = completion.wait();
1328
+ return this.#cacheResolved(key, completion.epoch, resolved);
1329
+ } finally {
1330
+ completion.release();
1331
+ }
1332
+ }
1333
+ #cacheResolved(key, epoch, resolved) {
1334
+ if (this.paths.size >= 256)
1335
+ this.paths.clear();
1336
+ this.paths.set(key, { path: resolved, epoch });
1337
+ return typeof resolved === "number" ? resolved : { ...resolved };
1338
+ }
1276
1339
  stat(handle) {
1277
1340
  this.retain(handle);
1278
1341
  try {
@@ -0,0 +1,11 @@
1
+ import { WeakMapConstructor, weakMapDelete, weakMapGet, weakMapSet } from "../proc/intrinsics.js";
2
+ const observations = new WeakMapConstructor;
3
+ export function registerResolveObservation(connection, observation) {
4
+ weakMapSet(observations, connection, observation);
5
+ }
6
+ export function takeResolveObservation(connection) {
7
+ const observation = weakMapGet(observations, connection);
8
+ if (observation)
9
+ weakMapDelete(observations, connection);
10
+ return observation;
11
+ }
@@ -77,6 +77,7 @@ const mutations = new Set([
77
77
  "remove",
78
78
  "move"
79
79
  ]);
80
+ let connectObserved;
80
81
  function sessionRevoked(session) {
81
82
  return isRevoked(session.connection) || !!requestTransportError(session.requests);
82
83
  }
@@ -98,6 +99,9 @@ function independentResponse(result, input, release) {
98
99
 
99
100
  export class FilesystemService {
100
101
  backend;
102
+ static {
103
+ connectObserved = (owner, connection, acceptResolve) => owner.#connect(connection, acceptResolve);
104
+ }
101
105
  sessions = new Set;
102
106
  requestBudget;
103
107
  readBudget;
@@ -149,6 +153,9 @@ export class FilesystemService {
149
153
  }
150
154
  }
151
155
  connect(connection) {
156
+ this.#connect(connection);
157
+ }
158
+ #connect(connection, acceptResolve) {
152
159
  if (this.ownerStopping) {
153
160
  this.refuseConnection(connection);
154
161
  throw new FilesystemError(11);
@@ -217,18 +224,37 @@ export class FilesystemService {
217
224
  if (!isRevoked(peer.connection))
218
225
  this.trim(peer);
219
226
  this.announceSegments(this.backend.sharedSegments?.() ?? [], [session]);
220
- connection.port.onmessage = ({ data }) => {
227
+ const receive = ({ data }) => {
221
228
  try {
222
229
  this.receive(session, data);
223
230
  } catch {
224
231
  this.signalTransportFailure(session, "protocol");
225
232
  }
226
233
  };
234
+ connection.port.onmessage = acceptResolve ? (event) => {
235
+ receive(event);
236
+ this.observeAcceptedResolve(session, event.data, acceptResolve);
237
+ } : receive;
227
238
  connection.port.onmessageerror = () => this.signalTransportFailure(session);
228
239
  connection.port.start();
229
240
  if (!session.closed)
230
241
  this.trackWork(session, this.watchTransport(session));
231
242
  }
243
+ observeAcceptedResolve(session, data, accept) {
244
+ try {
245
+ if (!("id" in data))
246
+ return;
247
+ if (requestTransportError(session.requests))
248
+ return;
249
+ const buffer = session.pending.get(data.id);
250
+ if (!buffer)
251
+ return;
252
+ const bytes = requestInput(session, data.id, requestBytes(buffer));
253
+ const { inputLength } = requestLengths(buffer, bytes);
254
+ if (unpack(bytes.subarray(0, inputLength)).operation === "resolve")
255
+ accept(data.id);
256
+ } catch {}
257
+ }
232
258
  announceSegments(segments, sessions) {
233
259
  if (!segments.length)
234
260
  return;
@@ -1666,6 +1692,21 @@ function diagnosticField(error, key) {
1666
1692
  return;
1667
1693
  }
1668
1694
  }
1695
+ async function prepareAcceptance(binding) {
1696
+ try {
1697
+ const { captureAcceptance } = await import("../browser/operations/writer.js");
1698
+ return captureAcceptance(binding);
1699
+ } catch {
1700
+ return;
1701
+ }
1702
+ }
1703
+ function backendOptions(data) {
1704
+ if (data.operations === undefined)
1705
+ return data;
1706
+ const options = { ...data };
1707
+ delete options.operations;
1708
+ return options;
1709
+ }
1669
1710
  export function installFilesystemWorker(scope, backendFactory = (options) => new MemoryBackend(undefined, undefined, undefined, undefined, options.connect)) {
1670
1711
  let service;
1671
1712
  let errorReported = false;
@@ -1693,12 +1734,12 @@ export function installFilesystemWorker(scope, backendFactory = (options) => new
1693
1734
  onStartupStage = (stage, timestamp) => scope.postMessage({ type: "filesystem-startup", stage, timestamp });
1694
1735
  reportRuntimeStartup(onStartupStage, "filesystem-connected");
1695
1736
  }
1696
- return backendFactory(data, onStartupStage, startupAbort.signal, () => {
1737
+ return backendFactory(backendOptions(data), onStartupStage, startupAbort.signal, () => {
1697
1738
  if (!disposing)
1698
1739
  scope.postMessage({ type: "filesystem-state", status: "waiting" });
1699
1740
  });
1700
1741
  }).then((backend) => new FilesystemService(backend));
1701
- service.then((owner) => {
1742
+ service.then(async (owner) => {
1702
1743
  if (disposing) {
1703
1744
  data.connect?.port.close();
1704
1745
  return;
@@ -1706,7 +1747,13 @@ export function installFilesystemWorker(scope, backendFactory = (options) => new
1706
1747
  if (data.socketScope && data.connect && data.reply) {
1707
1748
  attachSocketScopeControl(owner, data);
1708
1749
  } else if (data.connect) {
1709
- owner.connect(data.connect);
1750
+ const acceptResolve = data.operations ? await prepareAcceptance(data.operations) : undefined;
1751
+ if (disposing)
1752
+ return data.connect.port.close();
1753
+ if (acceptResolve)
1754
+ connectObserved(owner, data.connect, acceptResolve);
1755
+ else
1756
+ owner.connect(data.connect);
1710
1757
  scope.postMessage({ type: "filesystem-state", status: "ready" });
1711
1758
  reportRuntimeStartup(onStartupStage, "filesystem-ready");
1712
1759
  onStartupStage = undefined;
@@ -0,0 +1,72 @@
1
+ import {
2
+ BigInt64ArrayConstructor,
3
+ Int32ArrayConstructor,
4
+ Uint8ArrayConstructor,
5
+ atomicsCompareExchange,
6
+ atomicsLoad,
7
+ atomicsStore,
8
+ encodeUtf8Prefix,
9
+ decodeUtf8,
10
+ typedArraySet,
11
+ typedArrayLength
12
+ } from "./intrinsics.js";
13
+ import { packedControl, TaggedCells } from "./control.js";
14
+ import {
15
+ directoryCapacity,
16
+ directoryProcessValidity,
17
+ wakeDirectory
18
+ } from "./directory.js";
19
+ import { describeStartError } from "./start-diagnostic.js";
20
+ const HEADER_BYTES = 24;
21
+ const DIAGNOSTIC_BYTES = 1024;
22
+ const EXEC_WORDS_OFFSET = HEADER_BYTES + DIAGNOSTIC_BYTES;
23
+ export function commandTransferBytes(directory) {
24
+ return EXEC_WORDS_OFFSET + directoryCapacity(directory) * 8;
25
+ }
26
+ export function commandExecTransfer(source) {
27
+ const buffer = source.commandTransfer;
28
+ if (!buffer)
29
+ return;
30
+ return new TaggedCells(new BigInt64ArrayConstructor(buffer, EXEC_WORDS_OFFSET + source.slot * 8, 1), source.pid, directoryProcessValidity(source));
31
+ }
32
+ export function initializeCommandExec(source) {
33
+ const handoff = commandExecTransfer(source);
34
+ if (handoff)
35
+ atomicsStore(handoff.cells, 0, packedControl(source.pid, 0));
36
+ }
37
+ export function recordCommandTransferFailure(source, kind, target, error) {
38
+ const buffer = source.commandTransfer;
39
+ if (!buffer)
40
+ return;
41
+ const words = new Int32ArrayConstructor(buffer, 0, HEADER_BYTES / 4);
42
+ if (atomicsCompareExchange(words, 0, 0, -1) !== 0)
43
+ return;
44
+ atomicsStore(words, 1, source.pid);
45
+ atomicsStore(words, 2, source.generation);
46
+ atomicsStore(words, 3, target);
47
+ try {
48
+ const bytes = encodeUtf8Prefix(describeStartError(error), DIAGNOSTIC_BYTES);
49
+ typedArraySet(new Uint8ArrayConstructor(buffer, HEADER_BYTES), bytes);
50
+ atomicsStore(words, 4, typedArrayLength(bytes));
51
+ } finally {
52
+ atomicsStore(words, 0, kind === "spawn" ? 1 : 2);
53
+ wakeDirectory(source.directory);
54
+ }
55
+ }
56
+ export function commandTransferFailed(buffer) {
57
+ return buffer !== undefined && atomicsLoad(new Int32ArrayConstructor(buffer, 0, 1), 0) !== 0;
58
+ }
59
+ export function readCommandTransferFailure(buffer) {
60
+ const words = new Int32ArrayConstructor(buffer, 0, HEADER_BYTES / 4);
61
+ const operation = atomicsLoad(words, 0);
62
+ if (operation <= 0)
63
+ return;
64
+ const pid = atomicsLoad(words, 1);
65
+ const generation = atomicsLoad(words, 2);
66
+ const target = atomicsLoad(words, 3);
67
+ const length = atomicsLoad(words, 4);
68
+ const cause = decodeUtf8(new Uint8ArrayConstructor(buffer, HEADER_BYTES, length));
69
+ const kind = operation === 1 ? "spawn" : "exec";
70
+ const error = new Error(kind === "spawn" ? `Spawn transfer to process ${target} from process ${pid} image ${generation} could not be released` : `Exec transfer for process ${pid} image ${target} could not be released`, { cause: new Error(cause) });
71
+ return { kind, pid, generation, target, error };
72
+ }
@@ -73,12 +73,17 @@ export function directoryAdmissionVersion(directory) {
73
73
  throw new ProcessError(ProcessErrno.NOTSUP, "process admission is unresolved");
74
74
  return version;
75
75
  }
76
+ export function idleDirectoryAdmission(directory) {
77
+ const version = atomicsLoad(new BigInt64ArrayConstructor(directory, 16, 1), 0);
78
+ if ((version & 0xffffffffn) === 0n)
79
+ return version;
80
+ }
76
81
  export function commitDirectoryAdmission(directory, version) {
77
82
  const word = new BigInt64ArrayConstructor(directory, 16, 1);
78
83
  if (atomicsCompareExchange(word, 0, version, version + INTENT_STEP) !== version)
79
84
  throw new ProcessError(ProcessErrno.NOTSUP, "process admission changed during exec");
80
85
  }
81
- function directoryCapacity(directory) {
86
+ export function directoryCapacity(directory) {
82
87
  const capacity = (sharedArrayBufferByteLength(directory) - HEADER_BYTES) / ((PROCESS_WORDS + JOB_WORDS) * 8);
83
88
  if (!Number.isInteger(capacity) || capacity < 1)
84
89
  throw new TypeError("invalid process directory");
@@ -102,6 +107,9 @@ function validity(raw, id) {
102
107
  id
103
108
  };
104
109
  }
110
+ export function directoryProcessValidity(handle) {
111
+ return validity(row(handle.directory, handle.slot), handle.pid);
112
+ }
105
113
  function cells(raw, id, offset, count = 1) {
106
114
  return new TaggedCells(typedArraySubarray(raw, offset, offset + count), id);
107
115
  }
@@ -145,7 +145,7 @@ export const SetConstructor = Set;
145
145
  export const WeakMapConstructor = WeakMap;
146
146
  const setHasMethod = Set.prototype.has, setAddMethod = Set.prototype.add;
147
147
  const setDeleteMethod = Set.prototype.delete, setForEachMethod = Set.prototype.forEach;
148
- const weakMapGetMethod = WeakMap.prototype.get, weakMapSetMethod = WeakMap.prototype.set, weakMapHasMethod = WeakMap.prototype.has;
148
+ const weakMapGetMethod = WeakMap.prototype.get, weakMapSetMethod = WeakMap.prototype.set, weakMapHasMethod = WeakMap.prototype.has, weakMapDeleteMethod = WeakMap.prototype.delete;
149
149
  export function weakMapGet(map, key) {
150
150
  return apply(weakMapGetMethod, map, [key]);
151
151
  }
@@ -155,6 +155,9 @@ export function weakMapSet(map, key, value) {
155
155
  export function weakMapHas(map, key) {
156
156
  return apply(weakMapHasMethod, map, [key]);
157
157
  }
158
+ export function weakMapDelete(map, key) {
159
+ return apply(weakMapDeleteMethod, map, [key]);
160
+ }
158
161
  export function setHas(set, value) {
159
162
  return apply(setHasMethod, set, [value]);
160
163
  }
@@ -90,6 +90,7 @@ export {
90
90
  weakMapGet,
91
91
  weakMapSet,
92
92
  weakMapHas,
93
+ weakMapDelete,
93
94
  setHas,
94
95
  setAdd,
95
96
  setDelete,
@@ -559,7 +559,8 @@ class PipeState {
559
559
  for (let index = 0;index < this.writers.length + this.readers.length; index++) {
560
560
  const ring = index < this.writers.length ? this.writers[index] : this.readers[index - this.writers.length];
561
561
  const pid = ring.owner;
562
- if (pid && (pid === RECLAIMING || !this.registry?.isRevoked(Math.abs(pid))) && (pid < 0 ? !ring.closed : ring.references > 0)) {
562
+ const reader = index >= this.writers.length;
563
+ if (reader ? this.readerPending(ring) : pid && (pid === RECLAIMING || !this.registry?.isRevoked(Math.abs(pid))) && (pid < 0 ? !ring.closed : ring.references > 0)) {
563
564
  alive = true;
564
565
  break;
565
566
  }
@@ -589,6 +590,24 @@ class PipeState {
589
590
  hasReaders() {
590
591
  return ringSome(this.readers, (ring) => this.readerAlive(ring));
591
592
  }
593
+ readerHangup() {
594
+ const changed = atomicsLoad(this.changed, 0);
595
+ const generation = atomicsLoad(this.control, 5);
596
+ if (ringSome(this.readers, (ring) => this.readerPending(ring)))
597
+ return false;
598
+ if (atomicsLoad(this.control, 5) === generation && atomicsLoad(this.changed, 0) === changed)
599
+ return true;
600
+ this.signal();
601
+ return false;
602
+ }
603
+ readerPending(ring) {
604
+ const pid = ring.owner;
605
+ if (pid === RECLAIMING)
606
+ return true;
607
+ if (pid === 0 || this.registry?.isRevoked(Math.abs(pid)))
608
+ return false;
609
+ return pid < 0 || ring.references > 0;
610
+ }
592
611
  eof() {
593
612
  const generation = atomicsLoad(this.control, 5);
594
613
  if (!ringEvery(this.writers, (ring) => ring.owner === 0 || ring.closed))
@@ -760,6 +779,7 @@ class PipeState {
760
779
  return;
761
780
  if (!ring.advance(incarnation)) {
762
781
  atomicsStore(ring.pid, 0, owner);
782
+ this.signal();
763
783
  return;
764
784
  }
765
785
  try {
@@ -938,7 +958,7 @@ export class PipeEndpoint {
938
958
  if (!length(chunks))
939
959
  return 0;
940
960
  if (!this.state.hasReaders())
941
- return -PipeErrno.PIPE;
961
+ return this.state.readerHangup() ? -PipeErrno.PIPE : -PipeErrno.AGAIN;
942
962
  const result = this.ring.write(chunks, () => this.live, this.state.nextTicket(), this.incarnation);
943
963
  if (result > 0)
944
964
  this.state.signal();
@@ -998,13 +1018,26 @@ export class PipeEndpoint {
998
1018
  if (!this.live)
999
1019
  return { readable: this.mode === "read", writable: false, hangup: true, nbytes: 0 };
1000
1020
  if (this.mode === "write") {
1001
- const hangup = !this.state.hasReaders();
1002
- return { readable: false, writable: !hangup && this.ring.writable, hangup, nbytes: 0 };
1021
+ const writable = this.state.hasReaders() && this.ring.writable;
1022
+ return {
1023
+ readable: false,
1024
+ writable,
1025
+ hangup: !writable && this.state.readerHangup(),
1026
+ nbytes: 0
1027
+ };
1003
1028
  }
1004
1029
  const nbytes = this.state.relay ? this.state.relayAvailable() : this.state.queued();
1005
1030
  const hangup = this.state.relay ? this.ring.closed : this.state.eof();
1006
1031
  return { readable: nbytes > 0 || hangup, writable: false, hangup, nbytes };
1007
1032
  }
1033
+ async waitReaderHangup() {
1034
+ for (;; ) {
1035
+ const observed = atomicsLoad(this.notification, 0);
1036
+ if (!this.live || this.state.readerHangup())
1037
+ return;
1038
+ await waitChanged(this.notification, observed);
1039
+ }
1040
+ }
1008
1041
  waitReadable() {
1009
1042
  return this.wait("readable");
1010
1043
  }
@@ -70,8 +70,8 @@ export class ExecutableRegistry {
70
70
  async check(name, cwd = "/", assertActive = () => {}) {
71
71
  return (await this.admit(name, cwd, assertActive, undefined, "access")).entry;
72
72
  }
73
- async resolve(name, cwd = "/", assertActive = () => {}, argv) {
74
- const admitted = await this.admit(name, cwd, assertActive, argv, "execute");
73
+ async resolve(name, cwd = "/", assertActive = () => {}, argv, byteBackedGuestOnly = false) {
74
+ const admitted = await this.admit(name, cwd, assertActive, argv, byteBackedGuestOnly ? "byte-backed-guest" : "execute");
75
75
  const entry = admitted.entry;
76
76
  assertActive();
77
77
  if ("command" in entry)
@@ -162,18 +162,7 @@ export class ExecutableRegistry {
162
162
  const registered = this.entries.get(predicted);
163
163
  if (!this.lookup)
164
164
  return this.admitEntry(registered, name, cwd, assertActive, argv, mode, depth, seen, symlinks, scriptName);
165
- let file;
166
- try {
167
- file = await this.lookup(absolute);
168
- } catch (error) {
169
- assertActive();
170
- const errno = error !== null && typeof error === "object" && "errno" in error ? error.errno : undefined;
171
- if (errno === 44)
172
- throw new ExecutableNotFound(absolute);
173
- if (errno === 54)
174
- throw new ExecutableNotRunnable(absolute, false, "ENOTDIR");
175
- throw error;
176
- }
165
+ const file = await this.lookupFile(this.lookup, absolute, assertActive);
177
166
  assertActive();
178
167
  if (!file)
179
168
  throw new ExecutableNotFound(absolute);
@@ -186,6 +175,8 @@ export class ExecutableRegistry {
186
175
  return this.admitScript(target.startsWith("/") ? target : `${dirname(file.path)}/${target}`, cwd, assertActive, argv, mode, depth, seen, symlinks + 1, scriptName);
187
176
  }
188
177
  const resolved = file.path === predicted ? registered : this.entries.get(file.path);
178
+ if (mode === "byte-backed-guest")
179
+ return this.admitByteBackedGuest(file.kind === "file" ? resolved : undefined, file.path, argv);
189
180
  const admitted = resolved && !(("command" in resolved) && resolved.command.marker && !await this.matchesMarker(absolute, resolved.command.marker));
190
181
  assertActive();
191
182
  if (admitted && resolved && file.kind === "file")
@@ -205,7 +196,22 @@ export class ExecutableRegistry {
205
196
  const nextArgv = argv ? shebang.argument ? [shebang.interpreter, shebang.argument, scriptName].concat(argv.slice(1)) : [shebang.interpreter, scriptName].concat(argv.slice(1)) : undefined;
206
197
  return this.admitScript(shebang.interpreter, cwd, assertActive, nextArgv, mode, depth + 1, seen, symlinks);
207
198
  }
199
+ async lookupFile(lookup, path, assertActive) {
200
+ try {
201
+ return await lookup(path);
202
+ } catch (error) {
203
+ assertActive();
204
+ const errno = error !== null && typeof error === "object" && "errno" in error ? error.errno : undefined;
205
+ if (errno === 44)
206
+ throw new ExecutableNotFound(path);
207
+ if (errno === 54)
208
+ throw new ExecutableNotRunnable(path, false, "ENOTDIR");
209
+ throw error;
210
+ }
211
+ }
208
212
  async admitEntry(entry, path, cwd, assertActive, argv, mode, depth, seen, symlinks, scriptName) {
213
+ if (mode === "byte-backed-guest")
214
+ return this.admitByteBackedGuest(entry, path, argv);
209
215
  if (!entry)
210
216
  throw new ExecutableNotFound(path);
211
217
  if ("script" in entry)
@@ -214,6 +220,11 @@ export class ExecutableRegistry {
214
220
  this.checkCommand(entry.command);
215
221
  return { entry, ...argv ? { argv } : {} };
216
222
  }
223
+ admitByteBackedGuest(entry, path, argv) {
224
+ if (!entry || "script" in entry || "command" in entry || !entry.interruptible || entry.execution?.kind !== "guest")
225
+ throw new ExecutableNotRunnable(path, false);
226
+ return { entry, ...argv ? { argv } : {} };
227
+ }
217
228
  async admitRegisteredScript(executable, path, cwd, assertActive, argv, mode, depth, seen, symlinks, scriptName) {
218
229
  if (seen.has(path) || depth >= MAX_SHEBANG_DEPTH)
219
230
  throw new ExecutableNotRunnable(path, false, "ELOOP");