@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.
- package/README.md +32 -0
- package/browser/command-input.ts +86 -0
- package/browser/command-owner.ts +5 -0
- package/browser/display/guest.ts +2 -18
- package/browser/display/surface-info.ts +19 -0
- package/browser/operations/cells.ts +90 -0
- package/browser/operations/guest.ts +94 -0
- package/browser/operations/owner.ts +201 -0
- package/browser/operations/protocol.ts +16 -0
- package/browser/operations/types.ts +41 -0
- package/browser/operations/writer.ts +106 -0
- package/browser/preview1-worker.ts +8 -1
- package/browser/process-broker.ts +372 -22
- package/browser/process-start.ts +5 -2
- package/browser/run-command.ts +388 -0
- package/browser/shared-fs.ts +20 -3
- package/browser/worker-memory.ts +6 -0
- package/browser/worker.ts +121 -27
- package/dist/browser/command-input.js +82 -0
- package/dist/browser/command-owner.js +2 -0
- package/dist/browser/display/guest.js +2 -18
- package/dist/browser/display/surface-info.js +18 -0
- package/dist/browser/operations/cells.js +80 -0
- package/dist/browser/operations/guest.js +67 -0
- package/dist/browser/operations/owner.js +175 -0
- package/dist/browser/operations/protocol.js +0 -0
- package/dist/browser/operations/types.js +0 -0
- package/dist/browser/operations/writer.js +98 -0
- package/dist/browser/preview1-worker.js +4 -0
- package/dist/browser/process-broker.js +317 -18
- package/dist/browser/process-start.js +3 -0
- package/dist/browser/run-command.js +377 -0
- package/dist/browser/shared-fs.js +8 -3
- package/dist/browser/worker-memory.js +10 -1
- package/dist/browser/worker.js +83 -28
- package/dist/fs/client.js +81 -18
- package/dist/fs/resolve-observation.js +11 -0
- package/dist/fs/service.js +51 -4
- package/dist/proc/command-transfer.js +72 -0
- package/dist/proc/directory.js +9 -1
- package/dist/proc/intrinsics/host.js +4 -1
- package/dist/proc/intrinsics.js +1 -0
- package/dist/proc/pipes.js +37 -4
- package/dist/proc/registry.js +25 -14
- package/dist/proc/start-command.js +101 -0
- package/dist/proc/supervisor.js +34 -10
- package/dist/proc/worker-broker.js +86 -31
- package/dist/proc/worker-client.js +67 -31
- package/dist/programs/collection.js +2 -0
- package/dist/runtime.js +14 -1
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/client.ts +113 -17
- package/fs/resolve-observation.ts +25 -0
- package/fs/service.ts +63 -4
- package/index.ts +5 -0
- package/package.json +9 -1
- package/proc/command-transfer.ts +96 -0
- package/proc/directory.ts +10 -1
- package/proc/intrinsics/host.ts +5 -1
- package/proc/intrinsics.ts +1 -0
- package/proc/pipes.ts +39 -6
- package/proc/registry.ts +52 -17
- package/proc/start-command.ts +157 -0
- package/proc/supervisor.ts +34 -9
- package/proc/worker-broker.ts +100 -38
- package/proc/worker-client.ts +84 -31
- package/programs/collection.ts +4 -0
- package/runtime.ts +44 -3
- package/scripts/test-browser.ts +49 -29
- package/tests/browser-controls-discovery.test.ts +2 -10
- package/tests/browser-matrix.test.ts +105 -59
- package/tests/browser-navigation.test.ts +1 -1
- package/tests/browser-runner-lifecycle.ts +23 -13
- package/tests/command-input.test.ts +40 -0
- package/tests/command-scope.test.ts +422 -0
- package/tests/filesystem-client-intrinsics.test.ts +2 -0
- package/tests/filesystem-refcount.test.ts +2 -0
- package/tests/filesystem-resolve-observation.test.ts +185 -0
- package/tests/fixtures/command-bounded-admission.ts +31 -0
- package/tests/fixtures/command-disposal.ts +47 -0
- package/tests/fixtures/command-guests.ts +123 -0
- package/tests/fixtures/operation-fs-worker.ts +114 -0
- package/tests/fixtures/operation-observer-worker.ts +41 -0
- package/tests/fixtures/operation-page.ts +522 -0
- package/tests/fixtures/process-start-worker.ts +32 -0
- package/tests/link-browser-runner.ts +95 -22
- package/tests/node-exports.test.ts +29 -0
- package/tests/operation-diagnostics-browser.ts +192 -0
- package/tests/operation-diagnostics.test.ts +291 -0
- package/tests/page-runner/browser-controls-process-fixture.ts +24 -0
- package/tests/page-runner/browser-controls-runner.test.ts +20 -0
- package/tests/page-runner/browser-controls-runner.ts +1 -0
- package/tests/page-runner/browser-controls-suite.ts +20 -3
- package/tests/page-runner/control-run.ts +9 -1
- package/tests/page-runner/driver.ts +2 -0
- package/tests/page-runner/playwright-driver.ts +1 -1
- package/tests/page-runner/suite-host.ts +26 -1
- package/tests/page-runner/suite-startup.test.ts +39 -0
- package/tests/page-runner/threads-runtime-browser.test.ts +4 -6
- package/tests/page-runner/threads-runtime-suite.test.ts +14 -5
- package/tests/page-runner/worker-capability-controls.ts +174 -15
- package/tests/pipe-reader-reuse-races.test.ts +44 -0
- package/tests/process-start-browser-runner.ts +12 -4
- package/tests/process-start-browser.ts +66 -5
- package/tests/program-build-tools.test.ts +1 -1
- package/tests/runtime-facade.test.ts +5 -0
- package/tests/start-command.test.ts +109 -0
- package/tests/threads-guest-fixture.ts +1 -1
- package/tests/threads-guest-module.ts +6 -3
- package/tests/threads-guest.test.ts +25 -0
- package/tests/threads-runtime-browser-runner.ts +36 -13
- package/tests/threads-runtime-browser.ts +231 -92
- package/tests/threads-runtime-cases.ts +18 -1
- package/tests/threads-runtime-cleanup.ts +5 -1
- package/tests/threads-runtime-fd-browser.ts +247 -0
- package/tests/threads-runtime-fd-control.ts +3 -385
- package/tests/threads-runtime-fd-descriptors.test.ts +15 -13
- package/tests/threads-runtime-fd-module.ts +13 -2
- package/tests/threads-runtime-fd-service.ts +23 -2
- package/tests/threads-runtime-growth-worker.ts +61 -0
- package/tests/threads-runtime-host.ts +17 -4
- package/tests/threads-runtime-modules.ts +23 -5
- package/tests/threads-runtime-regression-controls.ts +386 -0
- package/tests/threads-runtime-regression-modules.ts +242 -0
- package/tests/threads-runtime-service-barrier.ts +8 -1
- package/tests/webkit-diagnostics.ts +125 -0
- package/tests/webkit-error-output.test.ts +118 -0
- package/tests/worker-memory-bootstrap.test.ts +19 -1
- package/tests/worker-memory-broker.test.ts +16 -8
- package/tooling/ci-paths.test.ts +329 -61
- package/tooling/private-authority.ts +14 -0
- package/tooling/program/program-transformer.json +1 -1
- package/tooling/tarball-smoke.ts +3 -1
package/browser/worker.ts
CHANGED
|
@@ -51,6 +51,7 @@ import {
|
|
|
51
51
|
sharedRevoked,
|
|
52
52
|
type FilesystemConnection,
|
|
53
53
|
} from "../fs/index.js";
|
|
54
|
+
import { registerResolveObservation } from "../fs/resolve-observation.js";
|
|
54
55
|
import { Wasi, WasiExit, type Stream } from "../core/index.js";
|
|
55
56
|
import { guardedStream } from "./guarded-stream.js";
|
|
56
57
|
import {
|
|
@@ -97,7 +98,9 @@ import {
|
|
|
97
98
|
wasmMemoryBuffer,
|
|
98
99
|
listenAbort,
|
|
99
100
|
unlistenAbort,
|
|
101
|
+
isInstance,
|
|
100
102
|
} from "../proc/intrinsics.ts";
|
|
103
|
+
import type { GuestOperationBinding, ObservedPreview1Bootstrap } from "./operations/protocol.js";
|
|
101
104
|
|
|
102
105
|
import type { MemoryGroupMemberRequest } from "../group/protocol.js";
|
|
103
106
|
import type { GroupMember } from "../group/member.js";
|
|
@@ -578,10 +581,29 @@ function requestProcessObserver<R extends RuntimeWorkerRequest>(
|
|
|
578
581
|
}
|
|
579
582
|
|
|
580
583
|
/** Instantiate one process using explicit capabilities; never creates a page runtime. */
|
|
581
|
-
export
|
|
584
|
+
export function bootstrap<R extends RuntimeWorkerRequest>(
|
|
582
585
|
req: R,
|
|
583
586
|
executor: WorkerExecutor<R> | HostProgramExecutor<R>,
|
|
584
587
|
host: RuntimeWorkerHost = globalThis as unknown as RuntimeWorkerHost,
|
|
588
|
+
): Promise<void> {
|
|
589
|
+
return bootstrapWorker(req, executor, host);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/** Internal entry point for the broker envelope. Executor requests never contain the binding. */
|
|
593
|
+
export function bootstrapObservedPreview1(envelope: ObservedPreview1Bootstrap): Promise<void> {
|
|
594
|
+
return bootstrapWorker(
|
|
595
|
+
envelope[0],
|
|
596
|
+
preview1Executor,
|
|
597
|
+
globalThis as unknown as RuntimeWorkerHost,
|
|
598
|
+
envelope[1],
|
|
599
|
+
);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
async function bootstrapWorker<R extends RuntimeWorkerRequest>(
|
|
603
|
+
req: R,
|
|
604
|
+
executor: WorkerExecutor<R> | HostProgramExecutor<R>,
|
|
605
|
+
host: RuntimeWorkerHost,
|
|
606
|
+
operations?: GuestOperationBinding,
|
|
585
607
|
): Promise<void> {
|
|
586
608
|
const hooks = captureWorkerHost(host);
|
|
587
609
|
if (!req.controlGrant) throw new TypeError("Worker bootstrap requires a shared control grant");
|
|
@@ -590,7 +612,7 @@ export async function bootstrap<R extends RuntimeWorkerRequest>(
|
|
|
590
612
|
);
|
|
591
613
|
const restoreAllocator = installSharedControlAllocator(allocateShared);
|
|
592
614
|
try {
|
|
593
|
-
await bootstrapAllocated(req, executor, host, hooks, allocateShared);
|
|
615
|
+
await bootstrapAllocated(req, executor, host, hooks, allocateShared, operations);
|
|
594
616
|
} finally {
|
|
595
617
|
restoreAllocator();
|
|
596
618
|
}
|
|
@@ -601,8 +623,13 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
601
623
|
host: RuntimeWorkerHost,
|
|
602
624
|
{ postToHost, addListener, removeListener }: ReturnType<typeof captureWorkerHost>,
|
|
603
625
|
allocateShared: (length: number) => SharedArrayBuffer,
|
|
626
|
+
operations?: GuestOperationBinding,
|
|
604
627
|
): Promise<void> {
|
|
605
628
|
postToHost({ type: "phase", name: "worker-start", duration: 0 });
|
|
629
|
+
let started = false;
|
|
630
|
+
const recording = operations
|
|
631
|
+
? await captureOperations(operations, req.filesystem, () => started)
|
|
632
|
+
: undefined;
|
|
606
633
|
const fs = new FilesystemClient(req.filesystem);
|
|
607
634
|
const { cancellation, notifications, startupCancelled, cancelStartup } = startupControl(
|
|
608
635
|
req.notifications,
|
|
@@ -715,7 +742,6 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
715
742
|
let groupMember: GroupAttachment = noGroup;
|
|
716
743
|
let releaseStartupStderr: (() => void) | undefined;
|
|
717
744
|
let done = false;
|
|
718
|
-
let started = false;
|
|
719
745
|
let socketNetwork:
|
|
720
746
|
| ReturnType<(typeof import("./preview1-socket.js"))["attachPreview1Sockets"]>
|
|
721
747
|
| undefined;
|
|
@@ -1233,7 +1259,7 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1233
1259
|
listenAbort(cancellation.signal, hook);
|
|
1234
1260
|
return () => unlistenAbort(cancellation.signal, hook);
|
|
1235
1261
|
},
|
|
1236
|
-
prepareExec: () => {
|
|
1262
|
+
prepareExec: (_pid, cleanupFailed) => {
|
|
1237
1263
|
checkActive();
|
|
1238
1264
|
const end = fs.beginTransfer();
|
|
1239
1265
|
let filesystem: ReturnType<typeof createProcessFilesystemIdentity> | undefined;
|
|
@@ -1257,15 +1283,17 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1257
1283
|
} catch (error) {
|
|
1258
1284
|
try {
|
|
1259
1285
|
cancel();
|
|
1286
|
+
} catch (cleanup) {
|
|
1287
|
+
cleanupFailed?.(cleanup);
|
|
1260
1288
|
} finally {
|
|
1261
1289
|
end();
|
|
1262
1290
|
}
|
|
1263
1291
|
throw error;
|
|
1264
1292
|
}
|
|
1265
1293
|
},
|
|
1266
|
-
|
|
1294
|
+
beginSpawnTransfer: () => fs.beginTransfer(),
|
|
1295
|
+
prepareSpawn: (pid, cleanupFailed) => {
|
|
1267
1296
|
checkActive();
|
|
1268
|
-
const end = fs.beginTransfer();
|
|
1269
1297
|
let filesystem: ReturnType<typeof createProcessFilesystemIdentity> | undefined;
|
|
1270
1298
|
let enrolled = false;
|
|
1271
1299
|
const cancel = () => {
|
|
@@ -1287,13 +1315,12 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1287
1315
|
codec: createRuntimeTerminalCodec({
|
|
1288
1316
|
fallback: pipes.codec(createFilesystemDescriptorCodec(fs, filesystem)),
|
|
1289
1317
|
}),
|
|
1290
|
-
release: end,
|
|
1291
1318
|
};
|
|
1292
1319
|
} catch (error) {
|
|
1293
1320
|
try {
|
|
1294
1321
|
cancel();
|
|
1295
|
-
}
|
|
1296
|
-
|
|
1322
|
+
} catch (cleanup) {
|
|
1323
|
+
cleanupFailed?.(cleanup);
|
|
1297
1324
|
}
|
|
1298
1325
|
throw error;
|
|
1299
1326
|
}
|
|
@@ -1304,7 +1331,15 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1304
1331
|
executor.beforeBindings?.(bindingsContext);
|
|
1305
1332
|
if (done) return;
|
|
1306
1333
|
adoptStdio(wasi.descriptors, discardedStdio);
|
|
1334
|
+
startStep(memorySampler, "installing-bindings");
|
|
1307
1335
|
runtimeProcess.installBindings();
|
|
1336
|
+
startStep(memorySampler, "bindings-installed");
|
|
1337
|
+
const { observedPoll, wait } = processPollWait(recording, {
|
|
1338
|
+
entered: () => started,
|
|
1339
|
+
memory: () => boundMemory,
|
|
1340
|
+
descriptors,
|
|
1341
|
+
checkActive,
|
|
1342
|
+
});
|
|
1308
1343
|
process = new ProcessABI({
|
|
1309
1344
|
memory: () => {
|
|
1310
1345
|
checkActive();
|
|
@@ -1320,23 +1355,9 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1320
1355
|
wake: runtimeProcess.wake,
|
|
1321
1356
|
futexReady: groupMember.watchReady,
|
|
1322
1357
|
createPipe: () => runtimeProcess!.createPipe(),
|
|
1323
|
-
wait
|
|
1324
|
-
checkActive();
|
|
1325
|
-
assertTerminalWaitAllowed();
|
|
1326
|
-
controlWait(word, 0, expected, ms);
|
|
1327
|
-
checkActive();
|
|
1328
|
-
},
|
|
1358
|
+
wait,
|
|
1329
1359
|
});
|
|
1330
|
-
|
|
1331
|
-
for (let index = 0; index < entries.length; index++) {
|
|
1332
|
-
const entry = entries[index]!,
|
|
1333
|
-
name = entry[0],
|
|
1334
|
-
call = entry[1];
|
|
1335
|
-
processImports[name] = (...args) => {
|
|
1336
|
-
checkActive();
|
|
1337
|
-
return call(...args);
|
|
1338
|
-
};
|
|
1339
|
-
}
|
|
1360
|
+
installProcessImports(processImports, process.imports, checkActive, observedPoll);
|
|
1340
1361
|
} else {
|
|
1341
1362
|
executor.beforeBindings?.(bindingsContext);
|
|
1342
1363
|
if (done) return;
|
|
@@ -1386,15 +1407,19 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1386
1407
|
throw new TypeError("executor imports hook cannot replace imported memory");
|
|
1387
1408
|
}
|
|
1388
1409
|
bindImportedMemory(req.module, linked, bindMemory);
|
|
1410
|
+
const instanceImports = executor.instantiate
|
|
1411
|
+
? linked
|
|
1412
|
+
: (memorySampler?.imports(req.module, linked) ?? linked);
|
|
1413
|
+
startStep(memorySampler, "instantiating");
|
|
1389
1414
|
const instance = executor.instantiate
|
|
1390
1415
|
? await Promise.race([
|
|
1391
|
-
executor.instantiate(
|
|
1416
|
+
executor.instantiate(instanceImports, { ...bindingsContext, bindMemory }),
|
|
1392
1417
|
startupCancelled.then(() => {
|
|
1393
1418
|
checkActive();
|
|
1394
1419
|
throw new Error("startup cancelled");
|
|
1395
1420
|
}),
|
|
1396
1421
|
])
|
|
1397
|
-
: new WebAssembly.Instance(req.module,
|
|
1422
|
+
: new WebAssembly.Instance(req.module, instanceImports);
|
|
1398
1423
|
if (done) return;
|
|
1399
1424
|
checkActive();
|
|
1400
1425
|
bindMemory(entryMemory(instance, groupMember.memory));
|
|
@@ -1426,6 +1451,75 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1426
1451
|
}
|
|
1427
1452
|
}
|
|
1428
1453
|
|
|
1454
|
+
async function captureOperations(
|
|
1455
|
+
binding: GuestOperationBinding,
|
|
1456
|
+
filesystem: FilesystemConnection,
|
|
1457
|
+
entered: () => boolean,
|
|
1458
|
+
) {
|
|
1459
|
+
try {
|
|
1460
|
+
const { createOperationWriters, observePipePoll } = await import("./operations/guest.js");
|
|
1461
|
+
const { pipe, resolve } = createOperationWriters(binding);
|
|
1462
|
+
registerResolveObservation(filesystem, { entered, writer: resolve });
|
|
1463
|
+
return { observePipePoll, writer: pipe };
|
|
1464
|
+
} catch {
|
|
1465
|
+
return undefined;
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
|
|
1469
|
+
type PipeObserver = typeof import("./operations/guest.js").observePipePoll;
|
|
1470
|
+
function processPollWait(
|
|
1471
|
+
recording: Awaited<ReturnType<typeof captureOperations>>,
|
|
1472
|
+
options: Omit<Parameters<PipeObserver>[0], "writer" | "stopped">,
|
|
1473
|
+
) {
|
|
1474
|
+
const observedPoll = recording && setupPipeObservation(recording, options);
|
|
1475
|
+
const { checkActive } = options;
|
|
1476
|
+
return {
|
|
1477
|
+
observedPoll,
|
|
1478
|
+
wait:
|
|
1479
|
+
observedPoll?.wait ??
|
|
1480
|
+
((ms: number, word: Int32Array, expected: number) => {
|
|
1481
|
+
checkActive();
|
|
1482
|
+
assertTerminalWaitAllowed();
|
|
1483
|
+
controlWait(word, 0, expected, ms);
|
|
1484
|
+
checkActive();
|
|
1485
|
+
}),
|
|
1486
|
+
};
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
function setupPipeObservation(
|
|
1490
|
+
recording: NonNullable<Awaited<ReturnType<typeof captureOperations>>>,
|
|
1491
|
+
options: Omit<Parameters<PipeObserver>[0], "writer" | "stopped">,
|
|
1492
|
+
): ReturnType<PipeObserver> | undefined {
|
|
1493
|
+
const { observePipePoll, writer } = recording;
|
|
1494
|
+
try {
|
|
1495
|
+
return observePipePoll({
|
|
1496
|
+
...options,
|
|
1497
|
+
writer,
|
|
1498
|
+
stopped: (error) => isInstance(error, WasiExit) || isInstance(error, RuntimeWorkerSignal),
|
|
1499
|
+
});
|
|
1500
|
+
} catch {
|
|
1501
|
+
return;
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
function installProcessImports(
|
|
1506
|
+
target: Record<string, (...args: number[]) => number | void>,
|
|
1507
|
+
imports: ProcessABI["imports"],
|
|
1508
|
+
checkActive: () => void,
|
|
1509
|
+
observedPoll: ReturnType<PipeObserver> | undefined,
|
|
1510
|
+
): void {
|
|
1511
|
+
const entries = objectEntries(imports);
|
|
1512
|
+
for (let index = 0; index < entries.length; index++) {
|
|
1513
|
+
const entry = entries[index]!;
|
|
1514
|
+
const name = entry[0];
|
|
1515
|
+
const call = name === "poll" && observedPoll ? observedPoll.wrap(entry[1]) : entry[1];
|
|
1516
|
+
target[name] = (...args) => {
|
|
1517
|
+
checkActive();
|
|
1518
|
+
return call(...args);
|
|
1519
|
+
};
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1429
1523
|
function socketWait(checkActive: () => void): (word: Int32Array, expected: number) => void {
|
|
1430
1524
|
return (word, expected) => {
|
|
1431
1525
|
checkActive();
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { PipeErrno } from "../proc/pipes.js";
|
|
2
|
+
import { ProcessError } from "../proc/types.js";
|
|
3
|
+
const BATCH = 32;
|
|
4
|
+
|
|
5
|
+
export class CommandInput {
|
|
6
|
+
writer;
|
|
7
|
+
reader;
|
|
8
|
+
closed = false;
|
|
9
|
+
joined;
|
|
10
|
+
constructor(writer, source, fail) {
|
|
11
|
+
this.writer = writer;
|
|
12
|
+
if (source instanceof ReadableStream)
|
|
13
|
+
this.reader = source.getReader();
|
|
14
|
+
this.joined = Promise.all([
|
|
15
|
+
this.pump(source).catch((error) => {
|
|
16
|
+
if (!this.closed)
|
|
17
|
+
fail(error);
|
|
18
|
+
}).finally(() => this.close(false)),
|
|
19
|
+
writer.waitReaderHangup().then(() => this.close())
|
|
20
|
+
]).then(() => {});
|
|
21
|
+
this.joined.catch(fail);
|
|
22
|
+
}
|
|
23
|
+
stop() {
|
|
24
|
+
this.close();
|
|
25
|
+
return this.joined;
|
|
26
|
+
}
|
|
27
|
+
close(cancel = true) {
|
|
28
|
+
if (this.closed)
|
|
29
|
+
return;
|
|
30
|
+
this.closed = true;
|
|
31
|
+
const reader = this.reader;
|
|
32
|
+
this.reader = undefined;
|
|
33
|
+
this.writer.close();
|
|
34
|
+
if (!reader)
|
|
35
|
+
return;
|
|
36
|
+
const stream = this.stream;
|
|
37
|
+
reader.releaseLock();
|
|
38
|
+
if (cancel && stream)
|
|
39
|
+
stream.cancel().catch(() => {});
|
|
40
|
+
}
|
|
41
|
+
stream;
|
|
42
|
+
async pump(source) {
|
|
43
|
+
if (source instanceof Uint8Array) {
|
|
44
|
+
await this.write(source);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
this.stream = source;
|
|
48
|
+
let chunks = 0;
|
|
49
|
+
while (this.reader && !this.closed) {
|
|
50
|
+
const next = await this.reader.read();
|
|
51
|
+
if (this.closed || next.done)
|
|
52
|
+
return;
|
|
53
|
+
if (!(next.value instanceof Uint8Array))
|
|
54
|
+
throw new TypeError("Command stdin requires byte chunks");
|
|
55
|
+
await this.write(next.value);
|
|
56
|
+
if (++chunks % BATCH === 0)
|
|
57
|
+
await yieldTask();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async write(bytes) {
|
|
61
|
+
let offset = 0, writes = 0;
|
|
62
|
+
while (!this.closed && offset < bytes.length) {
|
|
63
|
+
const accepted = this.writer.tryWrite([bytes.subarray(offset)]);
|
|
64
|
+
if (accepted === -PipeErrno.PIPE) {
|
|
65
|
+
this.close();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (accepted === -PipeErrno.AGAIN) {
|
|
69
|
+
await this.writer.waitWritable();
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (accepted <= 0)
|
|
73
|
+
throw new ProcessError(-accepted, "Command stdin write failed");
|
|
74
|
+
offset += accepted;
|
|
75
|
+
if (++writes % BATCH === 0)
|
|
76
|
+
await yieldTask();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function yieldTask() {
|
|
81
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
82
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { readSurfaceInfo } from "./surface-info.js";
|
|
1
2
|
import {
|
|
2
3
|
atomicsLoad,
|
|
3
4
|
DataViewConstructor,
|
|
@@ -50,7 +51,7 @@ export function createDisplayImports(connection, memory, live) {
|
|
|
50
51
|
for (let index = 0;index < surfaces.length; index++) {
|
|
51
52
|
if (surfaces[index].id !== id)
|
|
52
53
|
continue;
|
|
53
|
-
return
|
|
54
|
+
return readSurfaceInfo(new Int32ArrayConstructor(surfaces[index].info), output);
|
|
54
55
|
}
|
|
55
56
|
return -8;
|
|
56
57
|
} catch {
|
|
@@ -59,20 +60,3 @@ export function createDisplayImports(connection, memory, live) {
|
|
|
59
60
|
}
|
|
60
61
|
};
|
|
61
62
|
}
|
|
62
|
-
function readInfo(info, output) {
|
|
63
|
-
for (let attempt = 0;attempt < 16; attempt++) {
|
|
64
|
-
const version = atomicsLoad(info, 0);
|
|
65
|
-
if (version & 1)
|
|
66
|
-
continue;
|
|
67
|
-
const width = atomicsLoad(info, 1), height = atomicsLoad(info, 2);
|
|
68
|
-
const scale = atomicsLoad(info, 3), flags = atomicsLoad(info, 4);
|
|
69
|
-
if (version !== atomicsLoad(info, 0))
|
|
70
|
-
continue;
|
|
71
|
-
dataViewSetUint32(output, 0, width, true);
|
|
72
|
-
dataViewSetUint32(output, 4, height, true);
|
|
73
|
-
dataViewSetUint32(output, 8, scale, true);
|
|
74
|
-
dataViewSetUint32(output, 12, flags, true);
|
|
75
|
-
return 0;
|
|
76
|
-
}
|
|
77
|
-
return -6;
|
|
78
|
-
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { atomicsLoad, dataViewSetUint32 } from "../../proc/intrinsics.js";
|
|
2
|
+
export function readSurfaceInfo(info, output) {
|
|
3
|
+
for (let attempt = 0;attempt < 16; attempt++) {
|
|
4
|
+
const version = atomicsLoad(info, 0);
|
|
5
|
+
if (version & 1)
|
|
6
|
+
continue;
|
|
7
|
+
const width = atomicsLoad(info, 1), height = atomicsLoad(info, 2);
|
|
8
|
+
const scale = atomicsLoad(info, 3), flags = atomicsLoad(info, 4);
|
|
9
|
+
if (version !== atomicsLoad(info, 0))
|
|
10
|
+
continue;
|
|
11
|
+
dataViewSetUint32(output, 0, width, true);
|
|
12
|
+
dataViewSetUint32(output, 4, height, true);
|
|
13
|
+
dataViewSetUint32(output, 8, scale, true);
|
|
14
|
+
dataViewSetUint32(output, 12, flags, true);
|
|
15
|
+
return 0;
|
|
16
|
+
}
|
|
17
|
+
return -6;
|
|
18
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {
|
|
2
|
+
atomicsCompareExchange,
|
|
3
|
+
atomicsLoad,
|
|
4
|
+
BigInt64ArrayConstructor
|
|
5
|
+
} from "../../proc/intrinsics/core.js";
|
|
6
|
+
import { BigIntConstructor } from "../../proc/intrinsics/host.js";
|
|
7
|
+
export const OPERATION_SLOTS = 16;
|
|
8
|
+
const ROW_WORDS = 20;
|
|
9
|
+
export const OPERATION_BYTES = OPERATION_SLOTS * ROW_WORDS * 8;
|
|
10
|
+
export const MAX_VALUE = 4294967295;
|
|
11
|
+
const MASK = 0xffffffffn;
|
|
12
|
+
export const PIPE_OFFSET = 0;
|
|
13
|
+
export const RESOLVE_OFFSET = 9;
|
|
14
|
+
export const ACKNOWLEDGED = 18;
|
|
15
|
+
export const Field = {
|
|
16
|
+
sequence: 0,
|
|
17
|
+
active: 1,
|
|
18
|
+
started: 2,
|
|
19
|
+
result: 3,
|
|
20
|
+
duration: 4,
|
|
21
|
+
completed: 5,
|
|
22
|
+
cancelled: 6,
|
|
23
|
+
failed: 7,
|
|
24
|
+
timed: 8
|
|
25
|
+
};
|
|
26
|
+
export const Result = {
|
|
27
|
+
completed: 1,
|
|
28
|
+
signalPending: 2,
|
|
29
|
+
processStop: 3,
|
|
30
|
+
filesystemCancelled: 4,
|
|
31
|
+
pollError: 5,
|
|
32
|
+
resolveError: 6,
|
|
33
|
+
transportError: 7,
|
|
34
|
+
submissionError: 8
|
|
35
|
+
};
|
|
36
|
+
const perfNow = performance.now.bind(performance);
|
|
37
|
+
const timeOrigin = performance.timeOrigin;
|
|
38
|
+
const trunc = Math.trunc;
|
|
39
|
+
const min = Math.min;
|
|
40
|
+
const max = Math.max;
|
|
41
|
+
export function operationClock() {
|
|
42
|
+
return timeOrigin + perfNow();
|
|
43
|
+
}
|
|
44
|
+
export function elapsedFrom(origin, now = operationClock()) {
|
|
45
|
+
return min(MAX_VALUE, max(0, trunc(now - origin)));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class OperationCells {
|
|
49
|
+
#words;
|
|
50
|
+
#tag;
|
|
51
|
+
constructor(binding) {
|
|
52
|
+
this.#words = new BigInt64ArrayConstructor(binding.buffer, binding.slot * ROW_WORDS * 8, ROW_WORDS);
|
|
53
|
+
this.#tag = BigIntConstructor(binding.captureId);
|
|
54
|
+
}
|
|
55
|
+
read(index) {
|
|
56
|
+
const cell = atomicsLoad(this.#words, index);
|
|
57
|
+
if ((cell >> 32n & MASK) === this.#tag)
|
|
58
|
+
return NumberConstructor(cell & MASK);
|
|
59
|
+
}
|
|
60
|
+
write(index, value) {
|
|
61
|
+
const previous = atomicsLoad(this.#words, index);
|
|
62
|
+
if ((previous >> 32n & MASK) !== this.#tag)
|
|
63
|
+
return false;
|
|
64
|
+
const next = this.#tag << 32n | BigIntConstructor(value);
|
|
65
|
+
return atomicsCompareExchange(this.#words, index, previous, next) === previous;
|
|
66
|
+
}
|
|
67
|
+
assign() {
|
|
68
|
+
const next = this.#tag << 32n;
|
|
69
|
+
for (let index = 0;index < ROW_WORDS; index++) {
|
|
70
|
+
let previous = atomicsLoad(this.#words, index);
|
|
71
|
+
for (;; ) {
|
|
72
|
+
const actual = atomicsCompareExchange(this.#words, index, previous, next);
|
|
73
|
+
if (actual === previous)
|
|
74
|
+
break;
|
|
75
|
+
previous = actual;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const NumberConstructor = Number;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { RIGHT_FD_READ, RIGHT_POLL_FD_READWRITE } from "../../core/rights.js";
|
|
2
|
+
import { PipeEndpoint } from "../../proc/pipes.js";
|
|
3
|
+
import { controlWait } from "../../proc/control.js";
|
|
4
|
+
import { assertTerminalWaitAllowed } from "../../proc/terminal-turn.js";
|
|
5
|
+
import {
|
|
6
|
+
DataViewConstructor,
|
|
7
|
+
dataViewGetUint32,
|
|
8
|
+
isInstance,
|
|
9
|
+
wasmMemoryBuffer
|
|
10
|
+
} from "../../proc/intrinsics.js";
|
|
11
|
+
export { createOperationWriters } from "./writer.js";
|
|
12
|
+
const PIPE_READ_RIGHTS = RIGHT_FD_READ | RIGHT_POLL_FD_READWRITE;
|
|
13
|
+
function qualifyingPipe(memory, descriptors, ptr) {
|
|
14
|
+
if (!memory)
|
|
15
|
+
return false;
|
|
16
|
+
try {
|
|
17
|
+
const subscription = new DataViewConstructor(wasmMemoryBuffer(memory), ptr >>> 0, 16);
|
|
18
|
+
if (dataViewGetUint32(subscription, 0, true) !== 0)
|
|
19
|
+
return false;
|
|
20
|
+
const descriptor = descriptors.get(dataViewGetUint32(subscription, 4, true));
|
|
21
|
+
return !!descriptor && (descriptor.rightsBase & PIPE_READ_RIGHTS) === PIPE_READ_RIGHTS && isInstance(descriptor.resource, PipeEndpoint) && descriptor.resource.mode === "read";
|
|
22
|
+
} catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export function observePipePoll(options) {
|
|
27
|
+
const { writer, entered, memory, descriptors, checkActive, stopped } = options;
|
|
28
|
+
let qualified = false;
|
|
29
|
+
let guardStopped = false;
|
|
30
|
+
function guard() {
|
|
31
|
+
try {
|
|
32
|
+
checkActive();
|
|
33
|
+
} catch (error) {
|
|
34
|
+
guardStopped = stopped(error);
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function pollObserved(poll, ptr, count, timeout, out) {
|
|
39
|
+
if (count !== 1 || timeout !== -1 || !entered() || !qualifyingPipe(memory(), descriptors, ptr))
|
|
40
|
+
return poll(ptr, count, timeout, out);
|
|
41
|
+
qualified = true;
|
|
42
|
+
guardStopped = false;
|
|
43
|
+
try {
|
|
44
|
+
const result = poll(ptr, count, timeout, out);
|
|
45
|
+
writer.settle(result === 1 ? "completed" : result === 0 ? "signalPending" : "pollError");
|
|
46
|
+
return result;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
writer.settle(guardStopped ? "processStop" : "pollError");
|
|
49
|
+
throw error;
|
|
50
|
+
} finally {
|
|
51
|
+
qualified = false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
wait(ms, word, expected) {
|
|
56
|
+
guard();
|
|
57
|
+
assertTerminalWaitAllowed();
|
|
58
|
+
if (qualified)
|
|
59
|
+
writer.begin(1);
|
|
60
|
+
controlWait(word, 0, expected, ms);
|
|
61
|
+
guard();
|
|
62
|
+
},
|
|
63
|
+
wrap(poll) {
|
|
64
|
+
return (ptr, count, timeout, out) => pollObserved(poll, ptr, count, timeout, out);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|