@ai-ecoverse/slicc-kernel 1.86.0 → 1.87.0

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.
@@ -1984,12 +1984,10 @@ var init_wasi_fds = __esm({
1984
1984
  let fd;
1985
1985
  let kind;
1986
1986
  if (path === SEALED_PATH && openSealed) {
1987
- if (oflags & OFLAGS.DIRECTORY) throw new WasiError("ENOTDIR");
1988
- if (oflags & OFLAGS.CREAT && oflags & OFLAGS.EXCL) throw new WasiError("EEXIST");
1989
- fd = openSealed.call(this.kernel.sys);
1987
+ fd = this.openCharDevice(openSealed, oflags, rights);
1990
1988
  kind = "sealed";
1991
1989
  } else if (path === CLIPBOARD_PATH && openClipboard) {
1992
- fd = this.openClipboard(openClipboard, oflags, rights);
1990
+ fd = this.openCharDevice(openClipboard, oflags, rights);
1993
1991
  kind = "clipboard";
1994
1992
  } else if (path === "/dev/tty" || /^\/dev\/tty\d+$/.test(path)) {
1995
1993
  fd = openTty?.call(this.kernel.sys, path === "/dev/tty" ? void 0 : path);
@@ -2000,12 +1998,12 @@ var init_wasi_fds = __esm({
2000
1998
  this.bump();
2001
1999
  return fd;
2002
2000
  }
2003
- openClipboard(openClipboard, oflags, rights) {
2001
+ openCharDevice(openDevice2, oflags, rights) {
2004
2002
  if (oflags & OFLAGS.DIRECTORY) throw new WasiError("ENOTDIR");
2005
2003
  if (oflags & OFLAGS.CREAT && oflags & OFLAGS.EXCL) throw new WasiError("EEXIST");
2006
2004
  const want = wantOf(oflags, rights);
2007
2005
  const flags = want === 6 ? O_RDWR : want === 2 ? O_WRONLY : 0;
2008
- return openClipboard.call(this.kernel.sys, flags);
2006
+ return openDevice2.call(this.kernel.sys, flags);
2009
2007
  }
2010
2008
  open(path, oflags, rights, fdflags) {
2011
2009
  const device2 = deviceOf(path);
@@ -3621,7 +3619,7 @@ function importsContext({
3621
3619
  }
3622
3620
  function processes(host, call2, inThread) {
3623
3621
  const close = (fd) => void call2({ op: "fd-close", fd });
3624
- const slot = (n, how, mine, theirs) => {
3622
+ const slot2 = (n, how, mine, theirs) => {
3625
3623
  if (how === "inherit") return { fd: n };
3626
3624
  if (how === "null") return { none: true };
3627
3625
  const [read2, write2] = call2({ op: "fd-pipe" });
@@ -3635,7 +3633,7 @@ function processes(host, call2, inThread) {
3635
3633
  const theirs = [];
3636
3634
  host.fds.promoteFiles();
3637
3635
  try {
3638
- const stdio = STDIO.map((name2, n) => slot(n, o[name2] ?? "inherit", mine, theirs));
3636
+ const stdio = STDIO.map((name2, n) => slot2(n, o[name2] ?? "inherit", mine, theirs));
3639
3637
  const req = {
3640
3638
  op: "proc-spawn",
3641
3639
  file: o.argv[0],
@@ -4081,7 +4079,7 @@ function writable(io, fd) {
4081
4079
  abort: () => io.close(fd)
4082
4080
  });
4083
4081
  }
4084
- function childOps(kernel, io, defaults) {
4082
+ function childOps(kernel, io, defaults, sync) {
4085
4083
  const wait2 = async (pid) => {
4086
4084
  const r = await kernel.blocking({ op: "proc-wait", pid, nohang: false });
4087
4085
  const [, status2] = r.ok && r.kind === "json" ? r.json : [0, 0];
@@ -4101,25 +4099,43 @@ function childOps(kernel, io, defaults) {
4101
4099
  kill: (signal) => kill(pid, signal)
4102
4100
  };
4103
4101
  };
4104
- const spawn = async (o) => {
4102
+ const request = (o) => {
4105
4103
  const [file] = o.argv;
4106
4104
  if (file === void 0) throw new JsCallError("EINVAL", "spawn");
4107
4105
  const group = groupOf(o, "spawn");
4108
4106
  const named = argv0Of(o.argv0, "spawn");
4107
+ return (stdio) => ({
4108
+ op: "proc-spawn",
4109
+ file,
4110
+ argv: [...o.argv],
4111
+ ...named,
4112
+ env: { ...o.env ?? defaults.env },
4113
+ cwd: o.cwd ?? defaults.cwd,
4114
+ stdio,
4115
+ ...group
4116
+ });
4117
+ };
4118
+ const spawnSync = (o) => {
4119
+ const spawning = request(o);
4120
+ const mine = [];
4121
+ const theirs = [];
4122
+ try {
4123
+ const stdio = slotsSync(sync, o, mine, theirs);
4124
+ return childOf(sync.json(spawning(stdio)), mine);
4125
+ } catch (err) {
4126
+ closeAllSync(sync, mine);
4127
+ throw err;
4128
+ } finally {
4129
+ closeAllSync(sync, theirs);
4130
+ }
4131
+ };
4132
+ const spawn = async (o) => {
4133
+ const spawning = request(o);
4109
4134
  const mine = [];
4110
4135
  const theirs = [];
4111
4136
  try {
4112
4137
  const stdio = await slots2(kernel, o, mine, theirs);
4113
- const pid = await kernel.json({
4114
- op: "proc-spawn",
4115
- file,
4116
- argv: [...o.argv],
4117
- ...named,
4118
- env: { ...o.env ?? defaults.env },
4119
- cwd: o.cwd ?? defaults.cwd,
4120
- stdio,
4121
- ...group
4122
- });
4138
+ const pid = await kernel.json(spawning(stdio));
4123
4139
  return childOf(pid, mine);
4124
4140
  } catch (err) {
4125
4141
  await closeAll(io, mine);
@@ -4128,7 +4144,7 @@ function childOps(kernel, io, defaults) {
4128
4144
  await closeAll(io, theirs);
4129
4145
  }
4130
4146
  };
4131
- return { spawn, wait: wait2, kill };
4147
+ return { spawn, wait: wait2, kill, spawnSync };
4132
4148
  }
4133
4149
  function groupOf(o, call2) {
4134
4150
  const { pgid, session } = o;
@@ -4147,27 +4163,43 @@ function argv0Of(argv0, call2) {
4147
4163
  async function closeAll(io, fds) {
4148
4164
  for (const fd of fds) if (fd !== void 0) await io.close(fd).catch(() => void 0);
4149
4165
  }
4150
- async function preflight(kernel, hows) {
4166
+ function closeAllSync(sync, fds) {
4167
+ for (const fd of fds) if (fd !== void 0) sync.raw({ op: "fd-close", fd });
4168
+ }
4169
+ function checked(hows) {
4170
+ const fds = [];
4151
4171
  for (const [n, how] of hows.entries()) {
4152
4172
  if (how === "pipe" || how === "null") continue;
4153
4173
  if (how !== "inherit" && typeof how !== "number") throw new JsCallError("EINVAL", "spawn");
4154
- await kernel.json({ op: "fd-info", fd: how === "inherit" ? n : how });
4174
+ fds.push(how === "inherit" ? n : how);
4155
4175
  }
4176
+ return fds;
4177
+ }
4178
+ async function preflight(kernel, hows) {
4179
+ for (const fd of checked(hows)) await kernel.json({ op: "fd-info", fd });
4180
+ }
4181
+ function slot(n, how, pipe, mine, theirs) {
4182
+ if (how === "inherit") return { fd: n };
4183
+ if (how === "null") return { none: true };
4184
+ if (typeof how === "number") return { fd: how };
4185
+ const [read2, write2] = pipe();
4186
+ mine[n] = n === 0 ? write2 : read2;
4187
+ theirs.push(n === 0 ? read2 : write2);
4188
+ return { fd: n === 0 ? read2 : write2 };
4189
+ }
4190
+ function slotsSync(sync, o, mine, theirs) {
4191
+ const hows = STDIO2.map((name2) => o[name2] ?? "inherit");
4192
+ for (const fd of checked(hows)) sync.json({ op: "fd-info", fd });
4193
+ const pipe = () => sync.json({ op: "fd-pipe" });
4194
+ return hows.map((how, n) => slot(n, how, pipe, mine, theirs));
4156
4195
  }
4157
4196
  async function slots2(kernel, o, mine, theirs) {
4158
4197
  const hows = STDIO2.map((name2) => o[name2] ?? "inherit");
4159
4198
  await preflight(kernel, hows);
4160
4199
  const stdio = [];
4161
4200
  for (const [n, how] of hows.entries()) {
4162
- if (how === "inherit") stdio.push({ fd: n });
4163
- else if (how === "null") stdio.push({ none: true });
4164
- else if (typeof how === "number") stdio.push({ fd: how });
4165
- else {
4166
- const [read2, write2] = await kernel.json({ op: "fd-pipe" });
4167
- mine[n] = n === 0 ? write2 : read2;
4168
- theirs.push(n === 0 ? read2 : write2);
4169
- stdio.push({ fd: n === 0 ? read2 : write2 });
4170
- }
4201
+ const pair = how === "pipe" ? await kernel.json({ op: "fd-pipe" }) : [];
4202
+ stdio.push(slot(n, how, () => pair, mine, theirs));
4171
4203
  }
4172
4204
  return stdio;
4173
4205
  }
@@ -4433,6 +4465,10 @@ function* aliasOf(fd, abs, o) {
4433
4465
  }
4434
4466
  return { fd: copy2, file };
4435
4467
  }
4468
+ function accessFlags(o) {
4469
+ const write2 = o.write === true || o.append === true || o.truncate === true;
4470
+ return write2 ? o.read === true ? 2 : 1 : 0;
4471
+ }
4436
4472
  function* opened(abs, o) {
4437
4473
  const node = yield* lookup(abs);
4438
4474
  if (isExclusive(o)) throw new JsCallError("EEXIST", abs);
@@ -4441,13 +4477,13 @@ function* opened(abs, o) {
4441
4477
  return { fd: yield openRequest(abs, { ...o, create: false }), file: false };
4442
4478
  case "tty":
4443
4479
  return { fd: yield { op: "fd-open-tty" }, file: false };
4444
- case "clipboard": {
4445
- const write2 = o.write === true || o.append === true || o.truncate === true;
4446
- const flags = write2 ? o.read === true ? 2 : 1 : 0;
4447
- return { fd: yield { op: "fd-open-clipboard", flags }, file: false };
4448
- }
4480
+ case "clipboard":
4481
+ return {
4482
+ fd: yield { op: "fd-open-clipboard", flags: accessFlags(o) },
4483
+ file: false
4484
+ };
4449
4485
  case "sealed":
4450
- return { fd: yield { op: "fd-open-sealed" }, file: false };
4486
+ return { fd: yield { op: "fd-open-sealed", flags: accessFlags(o) }, file: false };
4451
4487
  case "terminal": {
4452
4488
  const req = node.pts === void 0 ? { op: "fd-open-tty", name: node.name } : { op: "pty-slave-open", n: node.pts, noctty: true };
4453
4489
  return { fd: yield req, file: false };
@@ -6165,6 +6201,7 @@ function createContext(o) {
6165
6201
  const sync = {
6166
6202
  ...syncOps(syncKernel, { pid: o.pid, resolve, random, infos, handles }),
6167
6203
  run: (options) => runSync(syncKernel, { env: o.env, cwd: o.cwd, resolve, signal: signalNumber }, options),
6204
+ spawn: (options) => spawnSync(options),
6168
6205
  exit: (status2 = 0) => {
6169
6206
  exiting ??= status2 & 255;
6170
6207
  return exitAtOnce(lane, [...writes], terminal.modes(), exiting);
@@ -6179,7 +6216,8 @@ function createContext(o) {
6179
6216
  }
6180
6217
  };
6181
6218
  const io = { read: read2, send: writeAll, close: (fd) => ctx.close(fd) };
6182
- const children = childOps(kernel, io, { env: o.env, cwd: o.cwd, signal: signalNumber });
6219
+ const defaults = { env: o.env, cwd: o.cwd, signal: signalNumber };
6220
+ const { spawnSync, ...children } = childOps(kernel, io, defaults, syncKernel);
6183
6221
  const strays = o.strays ?? { exception: /* @__PURE__ */ new Set(), rejection: /* @__PURE__ */ new Set() };
6184
6222
  const ctx = {
6185
6223
  argv: o.argv,
@@ -7901,7 +7939,7 @@ var init_kernel_streams = __esm({
7901
7939
  openSealed(open, path, flags, mode) {
7902
7940
  const { openSealed } = this.sys;
7903
7941
  if (path !== SEALED_PATH2 || !openSealed) return void 0;
7904
- const kfd = this.call(() => openSealed.call(this.sys));
7942
+ const kfd = this.call(() => openSealed.call(this.sys, flags & 3));
7905
7943
  return this.vessel(open, kfd, { path, flags, mode }, false);
7906
7944
  }
7907
7945
  openClipboard(open, path, flags, mode) {
@@ -8953,8 +8991,8 @@ function describeInherited(Fs, sys, streams, livePath, actions = []) {
8953
8991
  const out = [];
8954
8992
  for (const [fd, stream] of slots3) {
8955
8993
  promote(stream);
8956
- const slot = inheritedSlot(fd, stream);
8957
- if (slot) out.push(slot);
8994
+ const slot2 = inheritedSlot(fd, stream);
8995
+ if (slot2) out.push(slot2);
8958
8996
  }
8959
8997
  return out;
8960
8998
  }
@@ -9172,7 +9210,7 @@ function createProcessKernel(deps) {
9172
9210
  const reaped = /* @__PURE__ */ new Map();
9173
9211
  const call2 = (req, label) => transport.call(req, Infinity, label);
9174
9212
  const at = (path) => path === "" || path.startsWith("/") ? path : `${Fs.cwd()}/${path}`;
9175
- const slot = (fd, n, promote) => {
9213
+ const slot2 = (fd, n, promote) => {
9176
9214
  const stream = fd >= 0 ? Fs.getStream(fd) : null;
9177
9215
  if (!stream) return { none: true };
9178
9216
  promote?.(stream);
@@ -9221,7 +9259,7 @@ function createProcessKernel(deps) {
9221
9259
  const unflushed = flushed(deps.beforeSpawn);
9222
9260
  if (unflushed < 0) return unflushed;
9223
9261
  const promote = deps.stdioPromoter?.();
9224
- const stdio = [0, 1, 2].map((n) => slot(fds[n] ?? -1, n, promote));
9262
+ const stdio = [0, 1, 2].map((n) => slot2(fds[n] ?? -1, n, promote));
9225
9263
  const inherit = deps.inherit?.(actions) ?? [];
9226
9264
  const req = {
9227
9265
  op: "proc-spawn",
@@ -10146,8 +10184,8 @@ function kernelSys(transport) {
10146
10184
  openPts(n, noctty) {
10147
10185
  return json2(call2({ op: "pty-slave-open", n, noctty }, `pty-slave-open ${n}`));
10148
10186
  },
10149
- openSealed() {
10150
- return json2(call2({ op: "fd-open-sealed" }, "fd-open-sealed"));
10187
+ openSealed(flags) {
10188
+ return json2(call2({ op: "fd-open-sealed", flags }, "fd-open-sealed"));
10151
10189
  },
10152
10190
  openClipboard(flags) {
10153
10191
  return json2(call2({ op: "fd-open-clipboard", flags }, "fd-open-clipboard"));