@ai-ecoverse/slicc-kernel 1.48.3 → 1.49.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.
@@ -1991,12 +1991,12 @@ var init_kernel_streams = __esm({
1991
1991
  usePipes(pipefs) {
1992
1992
  const createPipe = pipefs.createPipe.bind(pipefs);
1993
1993
  pipefs.createPipe = () => {
1994
- const [read, write] = this.call(() => this.sys.pipe());
1994
+ const [read2, write2] = this.call(() => this.sys.pipe());
1995
1995
  let fds;
1996
1996
  try {
1997
1997
  fds = createPipe();
1998
1998
  } catch (e) {
1999
- for (const kfd of [read, write]) {
1999
+ for (const kfd of [read2, write2]) {
2000
2000
  try {
2001
2001
  this.sys.close(kfd);
2002
2002
  } catch {
@@ -2004,8 +2004,8 @@ var init_kernel_streams = __esm({
2004
2004
  }
2005
2005
  throw e;
2006
2006
  }
2007
- this.attach(this.Fs.getStream(fds.readable_fd), read, false);
2008
- this.attach(this.Fs.getStream(fds.writable_fd), write, false);
2007
+ this.attach(this.Fs.getStream(fds.readable_fd), read2, false);
2008
+ this.attach(this.Fs.getStream(fds.writable_fd), write2, false);
2009
2009
  return fds;
2010
2010
  };
2011
2011
  }
@@ -2394,12 +2394,12 @@ function readInto(sys, kfd, target, at) {
2394
2394
  }
2395
2395
  return done;
2396
2396
  }
2397
- function positioned(original, write, deps) {
2397
+ function positioned(original, write2, deps) {
2398
2398
  return (fd, iov, iovcnt, ...rest2) => {
2399
2399
  const stream = deps.fs()?.getStream(Number(fd));
2400
2400
  const memory = deps.memory();
2401
2401
  const kfd = stream?.sliccKernelFile ? stream.sliccKernelFd : void 0;
2402
- if (kfd === void 0 || !memory || !(write ? deps.sys.pwrite : deps.sys.pread)) {
2402
+ if (kfd === void 0 || !memory || !(write2 ? deps.sys.pwrite : deps.sys.pread)) {
2403
2403
  return original(fd, iov, iovcnt, ...rest2);
2404
2404
  }
2405
2405
  const at = offsetOf(rest2);
@@ -2411,7 +2411,7 @@ function positioned(original, write, deps) {
2411
2411
  const ptr = view.getUint32(Number(iov) + i * 8, true);
2412
2412
  const len = view.getUint32(Number(iov) + i * 8 + 4, true);
2413
2413
  const part = new Uint8Array(memory.buffer, ptr, len);
2414
- const moved = write ? deps.sys.pwrite?.(kfd, part.slice(), at + done) ?? 0 : readInto(deps.sys, kfd, part, at + done);
2414
+ const moved = write2 ? deps.sys.pwrite?.(kfd, part.slice(), at + done) ?? 0 : readInto(deps.sys, kfd, part, at + done);
2415
2415
  done += moved;
2416
2416
  if (moved < len) break;
2417
2417
  }
@@ -2424,13 +2424,13 @@ function positioned(original, write, deps) {
2424
2424
  }
2425
2425
  function positionalIo(imports, deps) {
2426
2426
  for (const namespace of namespaces(imports)) {
2427
- for (const [name, write] of [
2427
+ for (const [name, write2] of [
2428
2428
  ["fd_pread", false],
2429
2429
  ["fd_pwrite", true]
2430
2430
  ]) {
2431
2431
  const key = keyOf(namespace, name, deps.glue);
2432
2432
  if (key !== void 0) {
2433
- namespace[key] = positioned(namespace[key], write, deps);
2433
+ namespace[key] = positioned(namespace[key], write2, deps);
2434
2434
  }
2435
2435
  }
2436
2436
  }
@@ -2558,10 +2558,10 @@ function useZeroDevices(Fs) {
2558
2558
  }
2559
2559
  ]
2560
2560
  ];
2561
- for (const [path, minor, write] of devices) {
2561
+ for (const [path, minor, write2] of devices) {
2562
2562
  if (analyzePath.call(Fs, path).exists) continue;
2563
2563
  const dev = makedev.call(Fs, 1, minor);
2564
- registerDevice.call(Fs, dev, { read: zeros, write, llseek: () => 0 });
2564
+ registerDevice.call(Fs, dev, { read: zeros, write: write2, llseek: () => 0 });
2565
2565
  mkdev.call(Fs, path, CHAR_DEVICE, dev);
2566
2566
  }
2567
2567
  }
@@ -2957,38 +2957,38 @@ function drain(Fs, stream) {
2957
2957
  }
2958
2958
  return out;
2959
2959
  }
2960
- function kernelSelect(Fs, transport, read, write, timeoutMs) {
2960
+ function kernelSelect(Fs, transport, read2, write2, timeoutMs) {
2961
2961
  const kernel = (fd) => Fs.getStream(fd)?.sliccKernelFd;
2962
- const kr = read.map(kernel);
2963
- const kw = write.map(kernel);
2962
+ const kr = read2.map(kernel);
2963
+ const kw = write2.map(kernel);
2964
2964
  const r = transport.call({ op: "fd-select", read: kr, write: kw, timeoutMs }, Infinity, "select");
2965
2965
  if (!r.ok) return -wasiErrno(r.errno);
2966
2966
  const got = r.kind === "json" ? r.json : { read: [], write: [] };
2967
2967
  return {
2968
- read: read.filter((_, i) => got.read.includes(kr[i])),
2969
- write: write.filter((_, i) => got.write.includes(kw[i]))
2968
+ read: read2.filter((_, i) => got.read.includes(kr[i])),
2969
+ write: write2.filter((_, i) => got.write.includes(kw[i]))
2970
2970
  };
2971
2971
  }
2972
- function ownReady(Fs, read, write) {
2972
+ function ownReady(Fs, read2, write2) {
2973
2973
  const flags = (fd) => {
2974
2974
  const stream = Fs.getStream(fd);
2975
2975
  if (!stream) return POLLNVAL;
2976
2976
  return stream.stream_ops.poll ? stream.stream_ops.poll(stream) : POLLIN2 | POLLOUT2;
2977
2977
  };
2978
2978
  return {
2979
- read: read.filter((fd) => flags(fd) & (POLLIN2 | POLLHUP2 | POLLERR2 | POLLNVAL)),
2980
- write: write.filter((fd) => flags(fd) & (POLLOUT2 | POLLERR2 | POLLNVAL))
2979
+ read: read2.filter((fd) => flags(fd) & (POLLIN2 | POLLHUP2 | POLLERR2 | POLLNVAL)),
2980
+ write: write2.filter((fd) => flags(fd) & (POLLOUT2 | POLLERR2 | POLLNVAL))
2981
2981
  };
2982
2982
  }
2983
- function anySelect(Fs, transport, read, write, timeoutMs) {
2983
+ function anySelect(Fs, transport, read2, write2, timeoutMs) {
2984
2984
  const kernel = (fd) => Fs.getStream(fd)?.sliccKernelFd;
2985
2985
  const own = (fd) => kernel(fd) === void 0;
2986
- if (![...read, ...write].some(own)) return kernelSelect(Fs, transport, read, write, timeoutMs);
2986
+ if (![...read2, ...write2].some(own)) return kernelSelect(Fs, transport, read2, write2, timeoutMs);
2987
2987
  const deadline = timeoutMs < 0 ? Number.POSITIVE_INFINITY : Date.now() + timeoutMs;
2988
- const kernelRead = read.filter((fd) => !own(fd));
2989
- const kernelWrite = write.filter((fd) => !own(fd));
2988
+ const kernelRead = read2.filter((fd) => !own(fd));
2989
+ const kernelWrite = write2.filter((fd) => !own(fd));
2990
2990
  for (; ; ) {
2991
- const local = ownReady(Fs, read.filter(own), write.filter(own));
2991
+ const local = ownReady(Fs, read2.filter(own), write2.filter(own));
2992
2992
  const pending = local.read.length > 0 || local.write.length > 0;
2993
2993
  const slice = pending ? 0 : Math.min(SELECT_SLICE_MS, Math.max(0, deadline - Date.now()));
2994
2994
  const got = kernelSelect(Fs, transport, kernelRead, kernelWrite, slice);
@@ -3095,7 +3095,7 @@ function createProcessKernel(deps) {
3095
3095
  if (!r.ok) return -wasiErrno(r.errno);
3096
3096
  return r.kind === "json" ? r.json : -wasiErrno("EIO");
3097
3097
  },
3098
- select: (read, write, timeoutMs) => anySelect(Fs, transport, read, write, timeoutMs),
3098
+ select: (read2, write2, timeoutMs) => anySelect(Fs, transport, read2, write2, timeoutMs),
3099
3099
  execWait,
3100
3100
  execve(file, argv, env, cwd) {
3101
3101
  const pid = spawnChild(file, argv, env, cwd, [0, 1, 2], void 0, true);
@@ -5393,17 +5393,17 @@ function writeEvents(mem, ptr, events) {
5393
5393
  }
5394
5394
  function classify(fds, subs) {
5395
5395
  const events = [];
5396
- const read = [];
5397
- const write = [];
5396
+ const read2 = [];
5397
+ const write2 = [];
5398
5398
  for (const s of subs) {
5399
5399
  if (s.type === EVENTTYPE.CLOCK) continue;
5400
5400
  const e = fds.find(s.fd);
5401
5401
  if (!e) events.push({ userdata: s.userdata, error: E.BADF, type: s.type });
5402
5402
  else if (e.type !== "kernel")
5403
5403
  events.push({ userdata: s.userdata, error: E.SUCCESS, type: s.type });
5404
- else (s.type === EVENTTYPE.FD_READ ? read : write).push(s.fd);
5404
+ else (s.type === EVENTTYPE.FD_READ ? read2 : write2).push(s.fd);
5405
5405
  }
5406
- return { events, read, write };
5406
+ return { events, read: read2, write: write2 };
5407
5407
  }
5408
5408
  function readyEvents(subs, ready) {
5409
5409
  return subs.filter(
@@ -5419,11 +5419,11 @@ function pollOneoff(deps, inPtr, outPtr, nsubs, neventsPtr) {
5419
5419
  if (nsubs === 0) return E.INVAL;
5420
5420
  const { mem, kernel } = deps;
5421
5421
  const subs = readSubscriptions(mem, inPtr, nsubs, deps.now);
5422
- const { events, read, write } = classify(deps.fds, subs);
5422
+ const { events, read: read2, write: write2 } = classify(deps.fds, subs);
5423
5423
  let interrupted = false;
5424
5424
  const earliest = subs.reduce((min, s) => Math.min(min, s.deadline), Infinity);
5425
5425
  const expired = () => performance.now() + CLOCK_SLACK_MS >= earliest;
5426
- if (read.length > 0 || write.length > 0 || events.length === 0) {
5426
+ if (read2.length > 0 || write2.length > 0 || events.length === 0) {
5427
5427
  do {
5428
5428
  let timeoutMs = -1;
5429
5429
  if (events.length > 0) timeoutMs = 0;
@@ -5431,7 +5431,7 @@ function pollOneoff(deps, inPtr, outPtr, nsubs, neventsPtr) {
5431
5431
  timeoutMs = Math.max(0, Math.ceil(earliest - performance.now()));
5432
5432
  let ready;
5433
5433
  try {
5434
- ready = kernel.call({ op: "fd-select", read, write, timeoutMs });
5434
+ ready = kernel.call({ op: "fd-select", read: read2, write: write2, timeoutMs });
5435
5435
  } catch (e) {
5436
5436
  if (!deps.interruptWakes || e.code !== "EINTR") throw e;
5437
5437
  interrupted = true;
@@ -6669,10 +6669,10 @@ function processes(host, call2, inThread) {
6669
6669
  const slot = (n, how, mine, theirs) => {
6670
6670
  if (how === "inherit") return { fd: n };
6671
6671
  if (how === "null") return { none: true };
6672
- const [read, write] = call2({ op: "fd-pipe" });
6673
- mine[n] = n === 0 ? write : read;
6674
- theirs.push(n === 0 ? read : write);
6675
- return { fd: n === 0 ? read : write };
6672
+ const [read2, write2] = call2({ op: "fd-pipe" });
6673
+ mine[n] = n === 0 ? write2 : read2;
6674
+ theirs.push(n === 0 ? read2 : write2);
6675
+ return { fd: n === 0 ? read2 : write2 };
6676
6676
  };
6677
6677
  return {
6678
6678
  spawn(o) {
@@ -7056,6 +7056,289 @@ var init_wasix_fork = __esm({
7056
7056
  }
7057
7057
  });
7058
7058
 
7059
+ // src/process/wasi/wasix-ffi.ts
7060
+ function read(view, at, type) {
7061
+ if (type === "i32") return view.getInt32(at, true);
7062
+ if (type === "i64") return view.getBigInt64(at, true);
7063
+ if (type === "f32") return view.getFloat32(at, true);
7064
+ return view.getFloat64(at, true);
7065
+ }
7066
+ function write(view, at, type, value2) {
7067
+ if (type === "i32") view.setInt32(at, Number(value2), true);
7068
+ else if (type === "i64") view.setBigInt64(at, BigInt(value2), true);
7069
+ else if (type === "f32") view.setFloat32(at, Number(value2), true);
7070
+ else view.setFloat64(at, Number(value2), true);
7071
+ }
7072
+ function scalars(types) {
7073
+ if (types.includes("other")) throw new WasiError("EINVAL");
7074
+ return types;
7075
+ }
7076
+ function callDynamic(ctx, id, values, valuesLen, results, resultsLen, strict) {
7077
+ const table = ctx.table();
7078
+ if (!table) throw new WasiError("ENOTSUP");
7079
+ const fn = id < table.length ? table.get(id) : null;
7080
+ const sig = fn ? ctx.types.of(fn) : void 0;
7081
+ if (!fn || !sig) throw new WasiError("EINVAL");
7082
+ const params = scalars(sig.params);
7083
+ const outs = scalars(sig.results);
7084
+ const view = ctx.mem.view();
7085
+ const end = values + valuesLen;
7086
+ let at = values;
7087
+ const args = params.map((type) => {
7088
+ if (at + SIZE2[type] > end) {
7089
+ if (strict) throw new WasiError("EINVAL");
7090
+ return type === "i64" ? 0n : 0;
7091
+ }
7092
+ const v = read(view, at, type);
7093
+ at += SIZE2[type];
7094
+ return v;
7095
+ });
7096
+ if (strict && at !== end) throw new WasiError("EINVAL");
7097
+ const returned = fn(...args);
7098
+ const got = outs.length === 1 ? [returned] : returned ?? [];
7099
+ const after = ctx.mem.view();
7100
+ const stop = results + resultsLen;
7101
+ let out = results;
7102
+ outs.forEach((type, i) => {
7103
+ if (out + SIZE2[type] > stop) {
7104
+ if (strict) throw new WasiError("EINVAL");
7105
+ return;
7106
+ }
7107
+ write(after, out, type, got[i]);
7108
+ out += SIZE2[type];
7109
+ });
7110
+ if (strict && out !== stop) throw new WasiError("EINVAL");
7111
+ }
7112
+ var SIZE2;
7113
+ var init_wasix_ffi = __esm({
7114
+ "src/process/wasi/wasix-ffi.ts"() {
7115
+ "use strict";
7116
+ init_wasi_files();
7117
+ SIZE2 = { i32: 4, i64: 8, f32: 4, f64: 8 };
7118
+ }
7119
+ });
7120
+
7121
+ // src/process/wasi/wasm-functypes.ts
7122
+ function readTypes(r, s) {
7123
+ for (let i = r.u32(); i > 0; i--) {
7124
+ r.byte();
7125
+ const params = [];
7126
+ for (let n = r.u32(); n > 0; n--) params.push(r.valtype());
7127
+ const results = [];
7128
+ for (let n = r.u32(); n > 0; n--) results.push(r.valtype());
7129
+ s.info.sigs.push({ params, results });
7130
+ }
7131
+ }
7132
+ function readImports(r, s) {
7133
+ for (let i = r.u32(); i > 0; i--) {
7134
+ r.name();
7135
+ const field2 = r.name();
7136
+ const kind = r.byte();
7137
+ if (kind === 0) s.info.funcs.push(r.u32());
7138
+ else if (kind === 1) {
7139
+ r.valtype();
7140
+ r.limits();
7141
+ } else if (kind === 2) r.limits();
7142
+ else if (kind === 3) {
7143
+ r.valtype();
7144
+ r.byte();
7145
+ s.globals.push(field2);
7146
+ } else {
7147
+ r.byte();
7148
+ r.u32();
7149
+ }
7150
+ }
7151
+ }
7152
+ function readFunctions(r, s) {
7153
+ for (let i = r.u32(); i > 0; i--) s.info.funcs.push(r.u32());
7154
+ }
7155
+ function constExpr(r, s) {
7156
+ let value2 = 0;
7157
+ let tableBase = false;
7158
+ let known = true;
7159
+ for (; ; ) {
7160
+ const op = r.byte();
7161
+ if (op === 11) return known ? { value: value2, tableBase } : void 0;
7162
+ if (op === 65) value2 += r.s32();
7163
+ else if (op === 35) {
7164
+ const g = s.globals[r.u32()];
7165
+ if (g === TABLE_BASE) tableBase = true;
7166
+ else if (typeof g === "number") value2 += g;
7167
+ else known = false;
7168
+ } else if (op === 210) {
7169
+ value2 = r.u32();
7170
+ } else if (op in SKIP) {
7171
+ SKIP[op]?.(r);
7172
+ known = false;
7173
+ } else if (op !== 106) {
7174
+ r.at = r.bytes.length;
7175
+ return void 0;
7176
+ }
7177
+ }
7178
+ }
7179
+ function readGlobals(r, s) {
7180
+ for (let i = r.u32(); i > 0; i--) {
7181
+ r.valtype();
7182
+ r.byte();
7183
+ const init = constExpr(r, s);
7184
+ s.globals.push(init && !init.tableBase ? init.value : void 0);
7185
+ }
7186
+ }
7187
+ function readExports(r, s) {
7188
+ for (let i = r.u32(); i > 0; i--) {
7189
+ const name = r.name();
7190
+ const kind = r.byte();
7191
+ const index2 = r.u32();
7192
+ if (kind === 0) s.info.exports[name] = index2;
7193
+ }
7194
+ }
7195
+ function readElems(r, s) {
7196
+ for (let i = r.u32(); i > 0; i--) {
7197
+ const flags = r.u32();
7198
+ const active = (flags & 1) === 0;
7199
+ const table = flags & 2 ? active ? r.u32() : 0 : 0;
7200
+ const offset = active ? constExpr(r, s) : void 0;
7201
+ if (flags & 3) r.valtype();
7202
+ const funcs = [];
7203
+ for (let n = r.u32(); n > 0; n--) {
7204
+ if (flags & 4) {
7205
+ const ref = constExpr(r, s);
7206
+ funcs.push(ref ? ref.value : -1);
7207
+ } else funcs.push(r.u32());
7208
+ }
7209
+ if (offset && table === 0) {
7210
+ s.info.elems.push({ offset: offset.value, tableBase: offset.tableBase, funcs });
7211
+ }
7212
+ }
7213
+ }
7214
+ function funcTypeInfo(bytes) {
7215
+ const r = new Reader(bytes);
7216
+ const s = { info: { sigs: [], funcs: [], exports: {}, elems: [] }, globals: [] };
7217
+ while (r.at < bytes.length) {
7218
+ const id = r.byte();
7219
+ const end = r.u32() + r.at;
7220
+ SECTIONS[id]?.(r, s);
7221
+ r.at = end;
7222
+ }
7223
+ return s.info;
7224
+ }
7225
+ var VALTYPES, TABLE_BASE, Reader, SKIP, SECTIONS, FuncTypes;
7226
+ var init_wasm_functypes = __esm({
7227
+ "src/process/wasi/wasm-functypes.ts"() {
7228
+ "use strict";
7229
+ VALTYPES = {
7230
+ 127: "i32",
7231
+ 126: "i64",
7232
+ 125: "f32",
7233
+ 124: "f64"
7234
+ };
7235
+ TABLE_BASE = "__table_base";
7236
+ Reader = class {
7237
+ at = 8;
7238
+ bytes;
7239
+ constructor(bytes) {
7240
+ this.bytes = bytes;
7241
+ }
7242
+ byte() {
7243
+ return this.bytes[this.at++] ?? 0;
7244
+ }
7245
+ u32() {
7246
+ let result = 0;
7247
+ let shift = 0;
7248
+ for (; ; ) {
7249
+ const b = this.byte();
7250
+ result |= (b & 127) << shift;
7251
+ if (!(b & 128)) return result >>> 0;
7252
+ shift += 7;
7253
+ }
7254
+ }
7255
+ s32() {
7256
+ let result = 0;
7257
+ let shift = 0;
7258
+ let b;
7259
+ do {
7260
+ b = this.byte();
7261
+ result |= (b & 127) << shift;
7262
+ shift += 7;
7263
+ } while (b & 128);
7264
+ return shift < 32 && b & 64 ? result | -1 << shift : result;
7265
+ }
7266
+ name() {
7267
+ const len = this.u32();
7268
+ const s = new TextDecoder().decode(this.bytes.subarray(this.at, this.at + len));
7269
+ this.at += len;
7270
+ return s;
7271
+ }
7272
+ valtype() {
7273
+ const code2 = this.byte();
7274
+ if (code2 === 99 || code2 === 100) this.s32();
7275
+ return VALTYPES[code2] ?? "other";
7276
+ }
7277
+ limits() {
7278
+ const flags = this.byte();
7279
+ this.u32();
7280
+ if (flags & 1) this.u32();
7281
+ }
7282
+ };
7283
+ SKIP = {
7284
+ 66: (r) => r.s32(),
7285
+ 67: (r) => {
7286
+ r.at += 4;
7287
+ },
7288
+ 68: (r) => {
7289
+ r.at += 8;
7290
+ },
7291
+ 208: (r) => r.valtype()
7292
+ };
7293
+ SECTIONS = {
7294
+ 1: readTypes,
7295
+ 2: readImports,
7296
+ 3: readFunctions,
7297
+ 6: readGlobals,
7298
+ 7: readExports,
7299
+ 9: readElems
7300
+ };
7301
+ FuncTypes = class {
7302
+ known = /* @__PURE__ */ new WeakMap();
7303
+ pending = [];
7304
+ register(read2, instance, table, tableBase) {
7305
+ this.pending.push(() => {
7306
+ const bytes = read2();
7307
+ if (bytes) this.add(funcTypeInfo(bytes), instance, table, tableBase);
7308
+ });
7309
+ }
7310
+ add(info, instance, table, tableBase) {
7311
+ const sig = (func) => info.sigs[info.funcs[func] ?? -1];
7312
+ for (const [name, func] of Object.entries(info.exports)) {
7313
+ const fn = instance.exports[name];
7314
+ const type = sig(func);
7315
+ if (typeof fn === "function" && type) this.known.set(fn, type);
7316
+ }
7317
+ if (!table) return;
7318
+ for (const seg of info.elems) {
7319
+ const start2 = seg.offset + (seg.tableBase ? tableBase : 0);
7320
+ seg.funcs.forEach((func, i) => {
7321
+ const type = sig(func);
7322
+ const fn = start2 + i < table.length ? table.get(start2 + i) : null;
7323
+ if (fn && type) this.known.set(fn, type);
7324
+ });
7325
+ }
7326
+ }
7327
+ set(fn, sig) {
7328
+ this.known.set(fn, sig);
7329
+ }
7330
+ of(fn) {
7331
+ const known = this.known.get(fn);
7332
+ if (known || this.pending.length === 0) return known;
7333
+ const pending = this.pending;
7334
+ this.pending = [];
7335
+ for (const run of pending) run();
7336
+ return this.known.get(fn);
7337
+ }
7338
+ };
7339
+ }
7340
+ });
7341
+
7059
7342
  // src/process/wasi/wasix-linker.ts
7060
7343
  function expandOrigin(entry, dir) {
7061
7344
  return entry.replace(/\$(?:ORIGIN\b|\{ORIGIN\})/g, dir);
@@ -7074,13 +7357,14 @@ function call(instance, name) {
7074
7357
  const fn = instance.exports[name];
7075
7358
  if (typeof fn === "function") fn();
7076
7359
  }
7077
- var TABLE_BASE, PAGE2, STACK_SIZE, LIBRARY_DIRS, DlError, WasixLinker;
7360
+ var TABLE_BASE2, PAGE2, STACK_SIZE, LIBRARY_DIRS, DlError, WasixLinker;
7078
7361
  var init_wasix_linker = __esm({
7079
7362
  "src/process/wasi/wasix-linker.ts"() {
7080
7363
  "use strict";
7081
7364
  init_dylink();
7082
7365
  init_wasi_files();
7083
- TABLE_BASE = 1;
7366
+ init_wasm_functypes();
7367
+ TABLE_BASE2 = 1;
7084
7368
  PAGE2 = 65536;
7085
7369
  STACK_SIZE = 8 * 1024 * 1024;
7086
7370
  LIBRARY_DIRS = ["/lib", "/usr/lib", "/usr/local/lib"];
@@ -7101,6 +7385,8 @@ var init_wasix_linker = __esm({
7101
7385
  slots = /* @__PURE__ */ new Map();
7102
7386
  nextHandle = 1;
7103
7387
  publisher;
7388
+ indices;
7389
+ types = new FuncTypes();
7104
7390
  memory;
7105
7391
  host;
7106
7392
  constructor(memory, mainInfo, host, thread = false) {
@@ -7112,7 +7398,7 @@ var init_wasix_linker = __esm({
7112
7398
  if (!thread) growTo(memory, this.stackHigh);
7113
7399
  this.table = new WebAssembly.Table({
7114
7400
  element: "anyfunc",
7115
- initial: TABLE_BASE + mainInfo.tableSize
7401
+ initial: TABLE_BASE2 + mainInfo.tableSize
7116
7402
  });
7117
7403
  this.stackPointer = new WebAssembly.Global(
7118
7404
  { value: "i32", mutable: true },
@@ -7132,7 +7418,7 @@ var init_wasix_linker = __esm({
7132
7418
  __indirect_function_table: this.table,
7133
7419
  __stack_pointer: this.stackPointer,
7134
7420
  __memory_base: i32(this.memoryBase),
7135
- __table_base: i32(TABLE_BASE),
7421
+ __table_base: i32(TABLE_BASE2),
7136
7422
  __c_longjmp: this.cLongjmp,
7137
7423
  __cpp_exception: this.cppException
7138
7424
  };
@@ -7184,7 +7470,27 @@ var init_wasix_linker = __esm({
7184
7470
  for (const [path, linked] of this.byPath) out[path] = linked.module;
7185
7471
  return out;
7186
7472
  }
7473
+ allocateClosure() {
7474
+ const index2 = this.indices.allocate(this.table.length);
7475
+ growTable(this.table, index2 + 1);
7476
+ return index2;
7477
+ }
7478
+ freeClosure(index2) {
7479
+ this.indices?.free(index2);
7480
+ this.table.set(index2, null);
7481
+ }
7482
+ reserve(size) {
7483
+ return this.indices?.reserve(this.table.length, size) ?? this.table.length;
7484
+ }
7187
7485
  replay(record) {
7486
+ if (record.kind === "closure") {
7487
+ growTable(this.table, record.index + 1);
7488
+ return;
7489
+ }
7490
+ if (record.kind === "closure-free") {
7491
+ if (record.index < this.table.length) this.table.set(record.index, null);
7492
+ return;
7493
+ }
7188
7494
  if (record.kind === "load") {
7189
7495
  if (this.modules.has(record.handle)) return;
7190
7496
  this.instantiate(record.path, this.moduleAt(record.path), record, false);
@@ -7237,7 +7543,7 @@ var init_wasix_linker = __esm({
7237
7543
  (n) => this.load(this.locate(n, cwd, search), cwd, ldPath).handle
7238
7544
  );
7239
7545
  const memoryBase = this.allocate(info.memorySize, 2 ** info.memoryAlign);
7240
- const tableBase = this.table.length;
7546
+ const tableBase = this.reserve(info.tableSize);
7241
7547
  const record = {
7242
7548
  kind: "load",
7243
7549
  handle: this.nextHandle++,
@@ -7299,6 +7605,7 @@ var init_wasix_linker = __esm({
7299
7605
  "GOT.func": gotFunc
7300
7606
  };
7301
7607
  linked.instance = new WebAssembly.Instance(module, imports);
7608
+ this.types.register(() => this.host.read(path), linked.instance, this.table, at.tableBase);
7302
7609
  this.modules.set(at.handle, linked);
7303
7610
  this.byPath.set(path, linked);
7304
7611
  for (const resolve of pending) resolve();
@@ -7360,8 +7667,8 @@ var init_wasix_linker = __esm({
7360
7667
  slot(fn, handle, name) {
7361
7668
  const known = this.slots.get(fn);
7362
7669
  if (known !== void 0) return known;
7363
- const index2 = this.table.length;
7364
- this.table.grow(1);
7670
+ const index2 = this.reserve(1);
7671
+ growTable(this.table, index2 + 1);
7365
7672
  this.table.set(index2, fn);
7366
7673
  this.slots.set(fn, index2);
7367
7674
  this.publisher?.({ kind: "slot", index: index2, handle, name });
@@ -7369,9 +7676,9 @@ var init_wasix_linker = __esm({
7369
7676
  }
7370
7677
  allocate(size, alignment) {
7371
7678
  if (size === 0) return 0;
7372
- const base = align(this.memory.buffer.byteLength, Math.max(alignment, 16));
7373
- growTo(this.memory, base + size);
7374
- return base;
7679
+ const to = Math.max(alignment, 16);
7680
+ const pages2 = Math.ceil((size + (to > PAGE2 ? to : 0)) / PAGE2);
7681
+ return align(this.memory.grow(pages2) * PAGE2, to);
7375
7682
  }
7376
7683
  };
7377
7684
  }
@@ -7676,9 +7983,11 @@ var init_wasix_host = __esm({
7676
7983
  init_wasi_files();
7677
7984
  init_wasi_host();
7678
7985
  init_wasi_threads();
7986
+ init_wasix_ffi();
7679
7987
  init_wasix_linker();
7680
7988
  init_wasix_process();
7681
7989
  init_wasix_sockets();
7990
+ init_wasm_functypes();
7682
7991
  FDFLAGSEXT_CLOEXEC = 1;
7683
7992
  SPAWN_OP_SIZE = 56;
7684
7993
  SPAWN_OPS = ["close", "dup2", "open", "chdir", "fchdir"];
@@ -7690,6 +7999,8 @@ var init_wasix_host = __esm({
7690
7999
  process;
7691
8000
  threads;
7692
8001
  linker;
8002
+ types = new FuncTypes();
8003
+ table;
7693
8004
  signals;
7694
8005
  host;
7695
8006
  driver;
@@ -7771,6 +8082,18 @@ var init_wasix_host = __esm({
7771
8082
  ...this.processImports(),
7772
8083
  ...this.threadImports(),
7773
8084
  ...this.dlImports(),
8085
+ closure_allocate: (out) => {
8086
+ if (!this.linker) throw new WasiError("ENOTSUP");
8087
+ this.mem.view().setUint32(out, this.linker.allocateClosure(), true);
8088
+ },
8089
+ closure_free: (index2) => {
8090
+ if (!this.linker) throw new WasiError("ENOTSUP");
8091
+ this.linker.freeClosure(index2);
8092
+ },
8093
+ call_dynamic: (...a) => void callDynamic(
8094
+ { mem: this.mem, table: () => this.table, types: this.types },
8095
+ ...a
8096
+ ),
7774
8097
  ...wasixSocketImports(this.host, this.host.imports())
7775
8098
  });
7776
8099
  }
@@ -8092,12 +8415,12 @@ function nameFrame(line, module, name) {
8092
8415
  const found = name(Number(m[4]));
8093
8416
  return found === void 0 ? line : `${m[1]}${found} (${m[2]})`;
8094
8417
  }
8095
- function sidecarNames(read, path) {
8418
+ function sidecarNames(read2, path) {
8096
8419
  let names;
8097
8420
  return (index2) => {
8098
8421
  if (!names) {
8099
8422
  try {
8100
- names = parseFunctionNames(read(path));
8423
+ names = parseFunctionNames(read2(path));
8101
8424
  } catch {
8102
8425
  names = /* @__PURE__ */ new Map();
8103
8426
  }
@@ -8267,7 +8590,8 @@ async function instantiate(host, module, memory, threads, {
8267
8590
  stats,
8268
8591
  signals,
8269
8592
  program,
8270
- foreign
8593
+ foreign,
8594
+ path
8271
8595
  } = {}) {
8272
8596
  const driver = new AsyncifyDriver(host.mem);
8273
8597
  const wasixHost = WebAssembly.Module.imports(module).some((i) => i.module === WASIX) ? new WasixHost(host, driver, module) : void 0;
@@ -8293,7 +8617,10 @@ async function instantiate(host, module, memory, threads, {
8293
8617
  ) : void 0;
8294
8618
  if (sync) {
8295
8619
  ({ preview1, wasix } = sync.guard(preview1, wasix));
8296
- if (wasixHost) wasixHost.linker = sync.linker;
8620
+ if (wasixHost) {
8621
+ wasixHost.linker = sync.linker;
8622
+ wasixHost.types = sync.linker.types;
8623
+ }
8297
8624
  if (threads) {
8298
8625
  sync.linker.cache = threads.received;
8299
8626
  threads.modules = () => sync.linker.compiled();
@@ -8324,6 +8651,14 @@ async function instantiate(host, module, memory, threads, {
8324
8651
  stats?.phase("instantiate");
8325
8652
  const exports = instance.exports;
8326
8653
  host.mem.bind(memory ?? exports.memory);
8654
+ if (wasixHost) {
8655
+ const table = sync?.linker.table ?? exportedTable(instance);
8656
+ wasixHost.table = table;
8657
+ if (path !== void 0) {
8658
+ const read2 = linkerHost(host, () => ({})).read;
8659
+ wasixHost.types.register(() => read2(path), instance, table, sync ? TABLE_BASE2 : 0);
8660
+ }
8661
+ }
8327
8662
  if (sync) {
8328
8663
  sync.linker.bindMain(instance, !thread);
8329
8664
  if (thread) sync.catchUp();
@@ -8331,6 +8666,10 @@ async function instantiate(host, module, memory, threads, {
8331
8666
  }
8332
8667
  return { instance, driver };
8333
8668
  }
8669
+ function exportedTable(instance) {
8670
+ const table = instance.exports.__indirect_function_table;
8671
+ return table instanceof WebAssembly.Table ? table : void 0;
8672
+ }
8334
8673
  function linkerHost(host, imports) {
8335
8674
  return {
8336
8675
  read: (path) => {
@@ -8410,6 +8749,7 @@ async function runWasiProcess(init, port2) {
8410
8749
  stats,
8411
8750
  signals,
8412
8751
  foreign: init.program.foreign,
8752
+ path: init.program.path,
8413
8753
  ...program ? { program } : {}
8414
8754
  });
8415
8755
  restoreExportedMemory(instance, memory, init);
@@ -8494,6 +8834,7 @@ async function runWasiThread(init, port2) {
8494
8834
  const { instance } = await instantiate(host, init.program.module, thread.memory, threads, {
8495
8835
  thread: true,
8496
8836
  foreign: init.program.foreign,
8837
+ path: init.program.path,
8497
8838
  ...program ? { program } : {}
8498
8839
  });
8499
8840
  signals.bind(instance.exports);
@@ -8573,6 +8914,15 @@ var init_wasi_runtime = __esm({
8573
8914
  this.call = call2;
8574
8915
  this.ids = ids;
8575
8916
  linker.publisher = (record) => this.publish(record);
8917
+ linker.indices = {
8918
+ reserve: (floor, size) => this.call({ op: "dl-reserve", floor, size }),
8919
+ allocate: (floor) => {
8920
+ const index2 = this.call({ op: "dl-closure", floor });
8921
+ this.catchUp();
8922
+ return index2;
8923
+ },
8924
+ free: (index2) => void this.call({ op: "dl-closure-free", index: index2 })
8925
+ };
8576
8926
  }
8577
8927
  publish(record) {
8578
8928
  const since = this.call({ op: "dl-log", append: record, from: this.count });
@@ -8931,10 +9281,10 @@ async function slots(kernel, o, mine, theirs) {
8931
9281
  else if (how === "null") stdio.push({ none: true });
8932
9282
  else if (typeof how === "number") stdio.push({ fd: how });
8933
9283
  else {
8934
- const [read, write] = await kernel.json({ op: "fd-pipe" });
8935
- mine[n] = n === 0 ? write : read;
8936
- theirs.push(n === 0 ? read : write);
8937
- stdio.push({ fd: n === 0 ? read : write });
9284
+ const [read2, write2] = await kernel.json({ op: "fd-pipe" });
9285
+ mine[n] = n === 0 ? write2 : read2;
9286
+ theirs.push(n === 0 ? read2 : write2);
9287
+ stdio.push({ fd: n === 0 ? read2 : write2 });
8938
9288
  }
8939
9289
  }
8940
9290
  return stdio;
@@ -9354,8 +9704,8 @@ function checkOpen(abs, o, existing) {
9354
9704
  if (existing?.isDirectory) throw new JsCallError("EISDIR", abs);
9355
9705
  }
9356
9706
  function flagsOf(o) {
9357
- const write = o.write || o.append || o.truncate;
9358
- const access = write ? o.read ? O_RDWR6 : O_WRONLY6 : 0;
9707
+ const write2 = o.write || o.append || o.truncate;
9708
+ const access = write2 ? o.read ? O_RDWR6 : O_WRONLY6 : 0;
9359
9709
  return access | (o.append ? O_APPEND3 : 0);
9360
9710
  }
9361
9711
  function openRequest(abs, o) {
@@ -9540,14 +9890,14 @@ function syncOps(kernel, shared) {
9540
9890
  infos.set(fd, got);
9541
9891
  return got;
9542
9892
  };
9543
- const read = (fd, max = CHUNK2) => {
9893
+ const read2 = (fd, max = CHUNK2) => {
9544
9894
  index(max, "read");
9545
9895
  const emulated = readDevice(info(fd), max, shared.random);
9546
9896
  if (emulated) return emulated;
9547
9897
  const r = kernel.blocking({ op: "fd-read", fd, max: Math.min(max, MAX_IO) });
9548
9898
  return r.ok && r.kind === "bytes" ? r.bytes : new Uint8Array(0);
9549
9899
  };
9550
- const write = (fd, data) => {
9900
+ const write2 = (fd, data) => {
9551
9901
  const bytes = typeof data === "string" ? encoder2.encode(data) : data;
9552
9902
  const i = info(fd);
9553
9903
  if (writeDevice(i)) return;
@@ -9594,8 +9944,8 @@ function syncOps(kernel, shared) {
9594
9944
  return handle.file;
9595
9945
  };
9596
9946
  return {
9597
- read,
9598
- write,
9947
+ read: read2,
9948
+ write: write2,
9599
9949
  close(fd) {
9600
9950
  handles.get(fd)?.();
9601
9951
  handles.delete(fd);
@@ -10029,7 +10379,7 @@ function createContext(o) {
10029
10379
  infos.set(fd, got);
10030
10380
  return got;
10031
10381
  };
10032
- const read = async (fd, max = CHUNK2, signal) => {
10382
+ const read2 = async (fd, max = CHUNK2, signal) => {
10033
10383
  index(max, "read");
10034
10384
  const emulated = readDevice(await info(fd), max, random);
10035
10385
  if (emulated) return emulated;
@@ -10049,7 +10399,7 @@ function createContext(o) {
10049
10399
  }
10050
10400
  }
10051
10401
  };
10052
- const write = (fd, data) => {
10402
+ const write2 = (fd, data) => {
10053
10403
  const bytes = typeof data === "string" ? encoder3.encode(data) : data;
10054
10404
  let settle;
10055
10405
  const settled = new Promise((resolve2) => settle = resolve2);
@@ -10063,7 +10413,7 @@ function createContext(o) {
10063
10413
  }
10064
10414
  })();
10065
10415
  };
10066
- const writable2 = (fd) => new WritableStream({ write: (chunk) => write(fd, chunk) });
10416
+ const writable2 = (fd) => new WritableStream({ write: (chunk) => write2(fd, chunk) });
10067
10417
  const statOf = async (op, path) => {
10068
10418
  const abs = resolve(path);
10069
10419
  await kernel.call({ op: "fd-path-flush", path: abs });
@@ -10099,7 +10449,7 @@ function createContext(o) {
10099
10449
  handles
10100
10450
  });
10101
10451
  const terminal = ttyOps(kernel);
10102
- const io = { read, send: writeAll, close: (fd) => ctx.close(fd) };
10452
+ const io = { read: read2, send: writeAll, close: (fd) => ctx.close(fd) };
10103
10453
  const children = childOps(kernel, io, { env: o.env, cwd: o.cwd, signal: signalNumber });
10104
10454
  const strays = o.strays ?? { exception: /* @__PURE__ */ new Set(), rejection: /* @__PURE__ */ new Set() };
10105
10455
  const ctx = {
@@ -10113,7 +10463,7 @@ function createContext(o) {
10113
10463
  stdin: new ReadableStream(
10114
10464
  {
10115
10465
  async pull(controller) {
10116
- const chunk = await read(0);
10466
+ const chunk = await read2(0);
10117
10467
  if (chunk.length === 0) controller.close();
10118
10468
  else controller.enqueue(chunk);
10119
10469
  }
@@ -10122,8 +10472,8 @@ function createContext(o) {
10122
10472
  ),
10123
10473
  stdout: writable2(1),
10124
10474
  stderr: writable2(2),
10125
- read,
10126
- write,
10475
+ read: read2,
10476
+ write: write2,
10127
10477
  async close(fd) {
10128
10478
  handles.get(fd)?.();
10129
10479
  handles.delete(fd);