@ai-ecoverse/slicc-kernel 1.8.2 → 1.8.3

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.
@@ -1703,6 +1703,61 @@ function syncFsync(imports, fs) {
1703
1703
  };
1704
1704
  }
1705
1705
  }
1706
+ function offsetOf(rest) {
1707
+ const at = rest.length >= 3 ? Number(rest[1]) * 2 ** 32 + (Number(rest[0]) >>> 0) : Number(BigInt.asIntN(64, BigInt(rest[0])));
1708
+ return at >= 0 && Number.isSafeInteger(at) ? at : void 0;
1709
+ }
1710
+ function readInto(sys, kfd, target, at) {
1711
+ let done = 0;
1712
+ while (done < target.length) {
1713
+ const got = sys.pread?.(kfd, target.length - done, at + done) ?? new Uint8Array(0);
1714
+ if (got.length === 0) break;
1715
+ target.set(got, done);
1716
+ done += got.length;
1717
+ }
1718
+ return done;
1719
+ }
1720
+ function positioned(original, write, deps) {
1721
+ return (fd, iov, iovcnt, ...rest) => {
1722
+ const stream = deps.fs()?.getStream(Number(fd));
1723
+ const memory = deps.memory();
1724
+ const kfd = stream?.sliccKernelFile ? stream.sliccKernelFd : void 0;
1725
+ if (kfd === void 0 || !memory || !(write ? deps.sys.pwrite : deps.sys.pread)) {
1726
+ return original(fd, iov, iovcnt, ...rest);
1727
+ }
1728
+ const at = offsetOf(rest);
1729
+ if (at === void 0) return wasiErrno("EINVAL");
1730
+ const view = new DataView(memory.buffer);
1731
+ let done = 0;
1732
+ try {
1733
+ for (let i = 0; i < Number(iovcnt); i++) {
1734
+ const ptr = view.getUint32(Number(iov) + i * 8, true);
1735
+ const len = view.getUint32(Number(iov) + i * 8 + 4, true);
1736
+ const part = new Uint8Array(memory.buffer, ptr, len);
1737
+ const moved = write ? deps.sys.pwrite?.(kfd, part.slice(), at + done) ?? 0 : readInto(deps.sys, kfd, part, at + done);
1738
+ done += moved;
1739
+ if (moved < len) break;
1740
+ }
1741
+ } catch (err) {
1742
+ return wasiErrno(String(err.code));
1743
+ }
1744
+ view.setUint32(Number(rest[rest.length - 1]), done, true);
1745
+ return 0;
1746
+ };
1747
+ }
1748
+ function positionalIo(imports, deps) {
1749
+ for (const namespace of Object.values(imports)) {
1750
+ for (const [name, write] of [
1751
+ ["fd_pread", false],
1752
+ ["fd_pwrite", true]
1753
+ ]) {
1754
+ const original = namespace?.[name];
1755
+ if (typeof original === "function") {
1756
+ namespace[name] = positioned(original, write, deps);
1757
+ }
1758
+ }
1759
+ }
1760
+ }
1706
1761
  function wasmMemory(instance, imports) {
1707
1762
  const isMemory = (v) => v instanceof WebAssembly.Memory;
1708
1763
  const exported = Object.values(instance.exports).find(isMemory);
@@ -2344,6 +2399,11 @@ function kernelSys(transport) {
2344
2399
  const r = call2({ op: "fd-pread", fd, max, offset: at }, `fd-pread ${fd}`);
2345
2400
  return r.ok && r.kind === "bytes" ? r.bytes : new Uint8Array(0);
2346
2401
  },
2402
+ pwrite(fd, bytes, at) {
2403
+ return json(
2404
+ call2({ op: "fd-pwrite", fd, offset: at, body: bytes }, `fd-pwrite ${fd}`)
2405
+ );
2406
+ },
2347
2407
  isatty(fd) {
2348
2408
  return json(call2({ op: "fd-info", fd }, `fd-info ${fd}`))?.tty === true;
2349
2409
  },
@@ -2482,6 +2542,11 @@ async function runWasmProcess(init, port2, deps = {}) {
2482
2542
  }
2483
2543
  });
2484
2544
  syncFsync(imports, () => ownValue(module, "FS"));
2545
+ positionalIo(imports, {
2546
+ fs: () => ownValue(module, "FS"),
2547
+ sys,
2548
+ memory: () => memory
2549
+ });
2485
2550
  return WebAssembly.instantiate(init.program.module, imports);
2486
2551
  }).then((instance) => {
2487
2552
  memory = wasmMemory(instance, imports);