@ai-ecoverse/slicc-kernel 1.17.0 → 1.17.1

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.
@@ -1446,6 +1446,25 @@ function ownReady(Fs, read, write) {
1446
1446
  write: write.filter((fd) => flags(fd) & (POLLOUT2 | POLLERR2 | POLLNVAL))
1447
1447
  };
1448
1448
  }
1449
+ function anySelect(Fs, transport, read, write, timeoutMs) {
1450
+ const kernel = (fd) => Fs.getStream(fd)?.sliccKernelFd;
1451
+ const own = (fd) => kernel(fd) === void 0;
1452
+ if (![...read, ...write].some(own)) return kernelSelect(Fs, transport, read, write, timeoutMs);
1453
+ const deadline = timeoutMs < 0 ? Number.POSITIVE_INFINITY : Date.now() + timeoutMs;
1454
+ const kernelRead = read.filter((fd) => !own(fd));
1455
+ const kernelWrite = write.filter((fd) => !own(fd));
1456
+ for (; ; ) {
1457
+ const local = ownReady(Fs, read.filter(own), write.filter(own));
1458
+ const pending = local.read.length > 0 || local.write.length > 0;
1459
+ const slice = pending ? 0 : Math.min(SELECT_SLICE_MS, Math.max(0, deadline - Date.now()));
1460
+ const got = kernelSelect(Fs, transport, kernelRead, kernelWrite, slice);
1461
+ if (typeof got === "number") return got;
1462
+ const ready = { read: [...local.read, ...got.read], write: [...local.write, ...got.write] };
1463
+ if (ready.read.length > 0 || ready.write.length > 0 || Date.now() >= deadline) {
1464
+ return ready;
1465
+ }
1466
+ }
1467
+ }
1449
1468
  function createProcessKernel(deps) {
1450
1469
  const { transport, Fs } = deps;
1451
1470
  const reaped = /* @__PURE__ */ new Map();
@@ -1487,35 +1506,52 @@ function createProcessKernel(deps) {
1487
1506
  Fs.write(stream, r.bytes, 0, r.bytes.length);
1488
1507
  }
1489
1508
  };
1509
+ const execCaptures = /* @__PURE__ */ new Map();
1510
+ const execWait = (pid) => {
1511
+ const r = transport.call({ op: "proc-exec", pid }, Infinity, `exec ${pid}`);
1512
+ if (!r.ok) return -wasiErrno(r.errno);
1513
+ deps.afterChild();
1514
+ return r.kind === "json" ? r.json[1] : 0;
1515
+ };
1516
+ const spawnChild = (file, argv, env, cwd, fds, actions, exec) => {
1517
+ const unflushed = flushed(deps.beforeSpawn);
1518
+ if (unflushed < 0) return unflushed;
1519
+ const promote = deps.stdioPromoter?.();
1520
+ const stdio = [0, 1, 2].map((n) => slot(fds[n] ?? -1, n, promote));
1521
+ const inherit = deps.inherit?.(actions) ?? [];
1522
+ const r = transport.call(
1523
+ {
1524
+ op: "proc-spawn",
1525
+ file,
1526
+ argv,
1527
+ env: env ?? deps.env,
1528
+ cwd: cwd ?? Fs.cwd(),
1529
+ stdio,
1530
+ ...inherit.length > 0 ? { inherit } : {},
1531
+ ...exec ? { exec } : {}
1532
+ },
1533
+ Infinity,
1534
+ `proc-spawn ${file}`
1535
+ );
1536
+ if (!r.ok) return -wasiErrno(r.errno);
1537
+ const pid = r.kind === "json" ? r.json : 0;
1538
+ const captures = [1, 2].filter((n) => "capture" in stdio[n]);
1539
+ if (captures.length === 0) return pid;
1540
+ if (exec) {
1541
+ execCaptures.set(pid, () => {
1542
+ for (const n of captures) deliver(pid, n, fds[n]);
1543
+ });
1544
+ return pid;
1545
+ }
1546
+ const waited = kernelWait(pid, false);
1547
+ if (typeof waited === "number") return waited;
1548
+ for (const n of captures) deliver(pid, n, fds[n]);
1549
+ reaped.set(pid, waited[1]);
1550
+ return pid;
1551
+ };
1490
1552
  return {
1491
1553
  spawn(file, argv, env, cwd, fds, actions) {
1492
- const unflushed = flushed(deps.beforeSpawn);
1493
- if (unflushed < 0) return unflushed;
1494
- const promote = deps.stdioPromoter?.();
1495
- const stdio = [0, 1, 2].map((n) => slot(fds[n] ?? -1, n, promote));
1496
- const inherit = deps.inherit?.(actions) ?? [];
1497
- const r = transport.call(
1498
- {
1499
- op: "proc-spawn",
1500
- file,
1501
- argv,
1502
- env: env ?? deps.env,
1503
- cwd: cwd ?? Fs.cwd(),
1504
- stdio,
1505
- ...inherit.length > 0 ? { inherit } : {}
1506
- },
1507
- Infinity,
1508
- `proc-spawn ${file}`
1509
- );
1510
- if (!r.ok) return -wasiErrno(r.errno);
1511
- const pid = r.kind === "json" ? r.json : 0;
1512
- const captures = [1, 2].filter((n) => "capture" in stdio[n]);
1513
- if (captures.length === 0) return pid;
1514
- const waited = kernelWait(pid, false);
1515
- if (typeof waited === "number") return waited;
1516
- for (const n of captures) deliver(pid, n, fds[n]);
1517
- reaped.set(pid, waited[1]);
1518
- return pid;
1554
+ return spawnChild(file, argv, env, cwd, fds, actions, false);
1519
1555
  },
1520
1556
  fork(state) {
1521
1557
  const unflushed = flushed(deps.beforeSpawn);
@@ -1529,31 +1565,15 @@ function createProcessKernel(deps) {
1529
1565
  if (!r.ok) return -wasiErrno(r.errno);
1530
1566
  return r.kind === "json" ? r.json : -wasiErrno("EIO");
1531
1567
  },
1532
- select(read, write, timeoutMs) {
1533
- const kernel = (fd) => Fs.getStream(fd)?.sliccKernelFd;
1534
- const own = (fd) => kernel(fd) === void 0;
1535
- if (![...read, ...write].some(own))
1536
- return kernelSelect(Fs, transport, read, write, timeoutMs);
1537
- const deadline = timeoutMs < 0 ? Number.POSITIVE_INFINITY : Date.now() + timeoutMs;
1538
- const kernelRead = read.filter((fd) => !own(fd));
1539
- const kernelWrite = write.filter((fd) => !own(fd));
1540
- for (; ; ) {
1541
- const local = ownReady(Fs, read.filter(own), write.filter(own));
1542
- const pending = local.read.length > 0 || local.write.length > 0;
1543
- const slice = pending ? 0 : Math.min(SELECT_SLICE_MS, Math.max(0, deadline - Date.now()));
1544
- const got = kernelSelect(Fs, transport, kernelRead, kernelWrite, slice);
1545
- if (typeof got === "number") return got;
1546
- const ready = { read: [...local.read, ...got.read], write: [...local.write, ...got.write] };
1547
- if (ready.read.length > 0 || ready.write.length > 0 || Date.now() >= deadline) {
1548
- return ready;
1549
- }
1550
- }
1551
- },
1552
- execWait(pid) {
1553
- const r = transport.call({ op: "proc-exec", pid }, Infinity, `exec ${pid}`);
1554
- if (!r.ok) return -wasiErrno(r.errno);
1555
- deps.afterChild();
1556
- return r.kind === "json" ? r.json[1] : 0;
1568
+ select: (read, write, timeoutMs) => anySelect(Fs, transport, read, write, timeoutMs),
1569
+ execWait,
1570
+ execve(file, argv, env, cwd) {
1571
+ const pid = spawnChild(file, argv, env, cwd, [0, 1, 2], void 0, true);
1572
+ if (pid < 0) return pid;
1573
+ const status2 = execWait(pid);
1574
+ execCaptures.get(pid)?.();
1575
+ execCaptures.delete(pid);
1576
+ return status2;
1557
1577
  },
1558
1578
  pause() {
1559
1579
  return status(call2({ op: "sig-pause" }, "pause"));
@@ -5818,7 +5838,7 @@ var init_wasix_process = __esm({
5818
5838
  ...op.oflags & OFLAG_TRUNC ? { truncate: true } : {}
5819
5839
  });
5820
5840
  }
5821
- spawn(req) {
5841
+ spawn(req, exec = false) {
5822
5842
  const { stdio, inherit, cwd, opened } = this.childFds(req.ops ?? []);
5823
5843
  try {
5824
5844
  return this.call({
@@ -5828,7 +5848,8 @@ var init_wasix_process = __esm({
5828
5848
  env: req.env ?? this.environment(),
5829
5849
  cwd,
5830
5850
  stdio,
5831
- inherit
5851
+ inherit,
5852
+ ...exec ? { exec } : {}
5832
5853
  });
5833
5854
  } finally {
5834
5855
  for (const kfd of opened) this.host.o.kernel.sys.close(kfd);
@@ -5836,7 +5857,7 @@ var init_wasix_process = __esm({
5836
5857
  }
5837
5858
  }
5838
5859
  exec(req) {
5839
- const pid = this.spawn(req);
5860
+ const pid = this.spawn(req, true);
5840
5861
  const [, status2] = this.call({ op: "proc-exec", pid });
5841
5862
  const sig = status2 & 127;
5842
5863
  throw new WasiExit(sig ? 128 + sig : status2 >> 8 & 255);