Spaces:
Running

File size: 36,521 Bytes
1d919a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
// Browser hop: load/instantiate/run/call compiled .wasm.
// Host imports match ~/WASMLoader loader/host.go (wasi_snapshot_preview1,
// env.log, env.abort, optional packed string imports). The guest Calls
// this; it does not rewrite ~/WASMWin32.

export const ERRNO = {
  SUCCESS: 0,
  BADF: 8,
  INVAL: 28,
  NOSYS: 52,
};

class ExitError extends Error {
  constructor(code) {
    super("proc_exit " + code);
    this.code = code | 0;
    this.name = "ExitError";
  }
}

// Line-oriented stdin from the page (xterm.js). SharedArrayBuffer:
//   Int32[0] notify word (0 empty, 1 data)
//   Int32[1] byte length
//   bytes at offset 16
function stdinFromSab(sab) {
  const i32 = new Int32Array(sab);
  const u8 = new Uint8Array(sab);
  return {
    take(dst) {
      while (Atomics.load(i32, 1) === 0) {
        const w = Atomics.wait(i32, 0, 0);
        if (w === "timed-out") continue;
      }
      const n = Atomics.load(i32, 1);
      const take = Math.min(n, dst.length);
      dst.set(u8.subarray(16, 16 + take));
      const left = n - take;
      if (left > 0) {
        u8.copyWithin(16, 16 + take, 16 + n);
        Atomics.store(i32, 1, left);
      } else {
        Atomics.store(i32, 1, 0);
        Atomics.store(i32, 0, 0);
      }
      return take;
    },
  };
}

function u32(mem, ptr, v) {
  new DataView(mem.buffer).setUint32(ptr, v >>> 0, true);
}

function u64(mem, ptr, v) {
  const dv = new DataView(mem.buffer);
  dv.setUint32(ptr, v >>> 0, true);
  dv.setUint32(ptr + 4, Math.floor(v / 4294967296) >>> 0, true);
}

function bytes(mem, ptr, n) {
  return new Uint8Array(mem.buffer, ptr, n);
}

function writeCStr(mem, ptr, s) {
  const b = bytes(mem, ptr, s.length + 1);
  for (let i = 0; i < s.length; i++) b[i] = s.charCodeAt(i) & 255;
  b[s.length] = 0;
  return s.length + 1;
}

function writeStrings(mem, ptrs, buf, list) {
  let off = buf;
  for (let i = 0; i < list.length; i++) {
    u32(mem, ptrs + i * 4, off);
    off += writeCStr(mem, off, list[i]);
  }
}

function makeWasi(opts) {
  const args = opts.args && opts.args.length ? opts.args.slice() : ["wasm"];
  const env = opts.env && opts.env.length ? opts.env.slice() : [];
  const stdout = typeof opts.stdout === "function" ? opts.stdout : (t) => console.log(t);
  const stderr = typeof opts.stderr === "function" ? opts.stderr : (t) => console.error(t);
  let stdin = opts.stdin ? Uint8Array.from(typeof opts.stdin === "string"
    ? [...opts.stdin].map((c) => c.charCodeAt(0))
    : opts.stdin) : new Uint8Array();
  let stdinOff = 0;
  const sabIn = opts.stdinSab ? stdinFromSab(opts.stdinSab) : null;
  const inst = { exports: {} };

  function mem() {
    return inst.exports.memory;
  }

  function fdWrite(fd, iovs, iovsLen, nwritten) {
    const m = mem();
    const dv = new DataView(m.buffer);
    let wrote = 0;
    let out = "";
    for (let i = 0; i < iovsLen; i++) {
      const ptr = dv.getUint32(iovs + i * 8, true);
      const len = dv.getUint32(iovs + i * 8 + 4, true);
      if (len) out += new TextDecoder("utf-8", { fatal: false }).decode(bytes(m, ptr, len));
      wrote += len;
    }
    if (fd === 2) stderr(out);
    else stdout(out);
    u32(m, nwritten, wrote);
    return ERRNO.SUCCESS;
  }

  function fdRead(fd, iovs, iovsLen, nread) {
    if (fd !== 0) return ERRNO.BADF;
    const m = mem();
    const dv = new DataView(m.buffer);
    let got = 0;
    for (let i = 0; i < iovsLen; i++) {
      const ptr = dv.getUint32(iovs + i * 8, true);
      const len = dv.getUint32(iovs + i * 8 + 4, true);
      if (!len) continue;
      const slot = bytes(m, ptr, len);
      if (sabIn) {
        got += sabIn.take(slot);
      } else {
        const n = Math.min(len, stdin.length - stdinOff);
        if (n > 0) {
          slot.set(stdin.subarray(stdinOff, stdinOff + n));
          stdinOff += n;
          got += n;
        }
      }
    }
    u32(m, nread, got);
    return ERRNO.SUCCESS;
  }

  const preview1 = {
    args_sizes_get(argc, bufsz) {
      const m = mem();
      u32(m, argc, args.length);
      u32(m, bufsz, args.reduce((n, s) => n + s.length + 1, 0));
      return ERRNO.SUCCESS;
    },
    args_get(argv, buf) {
      writeStrings(mem(), argv, buf, args);
      return ERRNO.SUCCESS;
    },
    environ_sizes_get(count, bufsz) {
      const m = mem();
      u32(m, count, env.length);
      u32(m, bufsz, env.reduce((n, s) => n + s.length + 1, 0));
      return ERRNO.SUCCESS;
    },
    environ_get(environ, buf) {
      writeStrings(mem(), environ, buf, env);
      return ERRNO.SUCCESS;
    },
    clock_res_get(id, res) {
      u64(mem(), res, 1000000);
      return ERRNO.SUCCESS;
    },
    clock_time_get(id, precision, time) {
      u64(mem(), time, Date.now() * 1e6);
      return ERRNO.SUCCESS;
    },
    random_get(ptr, n) {
      crypto.getRandomValues(bytes(mem(), ptr, n));
      return ERRNO.SUCCESS;
    },
    proc_exit(code) {
      throw new ExitError(code);
    },
    proc_raise() { return ERRNO.NOSYS; },
    sched_yield() { return ERRNO.SUCCESS; },
    fd_write: fdWrite,
    fd_read: fdRead,
    fd_seek(fd, offset, whence, newoff) {
      u64(mem(), newoff, 0);
      return fd <= 2 ? ERRNO.SUCCESS : ERRNO.BADF;
    },
    fd_tell(fd, off) {
      u64(mem(), off, 0);
      return fd <= 2 ? ERRNO.SUCCESS : ERRNO.BADF;
    },
    fd_close(fd) {
      return fd <= 2 ? ERRNO.SUCCESS : ERRNO.BADF;
    },
    fd_fdstat_get(fd, stat) {
      if (fd > 2) return ERRNO.BADF;
      const m = mem();
      const b = bytes(m, stat, 24);
      b.fill(0);
      b[0] = 2; // filetype character
      return ERRNO.SUCCESS;
    },
    fd_fdstat_set_flags() { return ERRNO.SUCCESS; },
    fd_prestat_get() { return ERRNO.BADF; },
    fd_prestat_dir_name() { return ERRNO.BADF; },
    fd_datasync() { return ERRNO.SUCCESS; },
    fd_sync() { return ERRNO.SUCCESS; },
    fd_advise() { return ERRNO.SUCCESS; },
    fd_allocate() { return ERRNO.NOSYS; },
    fd_filestat_get() { return ERRNO.NOSYS; },
    fd_filestat_set_size() { return ERRNO.NOSYS; },
    fd_filestat_set_times() { return ERRNO.NOSYS; },
    fd_pread() { return ERRNO.NOSYS; },
    fd_pwrite() { return ERRNO.NOSYS; },
    fd_readdir() { return ERRNO.NOSYS; },
    fd_renumber() { return ERRNO.NOSYS; },
    path_open() { return ERRNO.NOSYS; },
    path_filestat_get() { return ERRNO.NOSYS; },
    path_filestat_set_times() { return ERRNO.NOSYS; },
    path_create_directory() { return ERRNO.NOSYS; },
    path_unlink_file() { return ERRNO.NOSYS; },
    path_remove_directory() { return ERRNO.NOSYS; },
    path_rename() { return ERRNO.NOSYS; },
    path_symlink() { return ERRNO.NOSYS; },
    path_link() { return ERRNO.NOSYS; },
    path_readlink() { return ERRNO.NOSYS; },
    poll_oneoff() { return ERRNO.NOSYS; },
    sock_accept() { return ERRNO.NOSYS; },
    sock_recv() { return ERRNO.NOSYS; },
    sock_send() { return ERRNO.NOSYS; },
    sock_shutdown() { return ERRNO.NOSYS; },
  };

  return { preview1, inst, ExitError };
}

function packedStringImport(arity, fn) {
  return function (memoryHolder) {
    return function () {
      const mem = memoryHolder.exports.memory;
      const args = [];
      for (let i = 0; i < arity; i++) {
        const ptr = arguments[i * 2] >>> 0;
        const len = arguments[i * 2 + 1] >>> 0;
        args.push(len ? new TextDecoder().decode(bytes(mem, ptr, len)) : "");
      }
      let result = "";
      let err = "";
      try {
        const r = fn(args);
        result = r == null ? "" : String(r);
      } catch (e) {
        err = e && e.message ? e.message : String(e);
      }
      const body = JSON.stringify(err
        ? { ok: false, result: "", error: err }
        : { ok: true, result: result });
      const alloc = memoryHolder.exports.alloc;
      if (typeof alloc !== "function") return 0n;
      const ptr = alloc(body.length) >>> 0;
      const u8 = new TextEncoder().encode(body);
      bytes(mem, ptr, u8.length).set(u8);
      return (BigInt(ptr) << 32n) | BigInt(u8.length);
    };
  };
}

function writeLEB(arr, v) {
  v = v >>> 0;
  while (true) {
    const c = v & 0x7f;
    v >>>= 7;
    if (v) arr.push(c | 0x80);
    else {
      arr.push(c);
      return;
    }
  }
}

function readLEB(b, off) {
  let v = 0;
  let shift = 0;
  for (let i = 0; i < 5 && off + i < b.length; i++) {
    const c = b[off + i];
    v |= (c & 0x7f) << shift;
    if ((c & 0x80) === 0) return [v >>> 0, i + 1];
    shift += 7;
  }
  return [0, 0];
}

function asU8(raw) {
  if (raw instanceof Uint8Array) return raw;
  return new Uint8Array(raw);
}

function magicAt(b, off, mag) {
  if (off + mag.length > b.length) return false;
  for (let i = 0; i < mag.length; i++) if (b[off + i] !== mag[i]) return false;
  return true;
}

const CORE_MAGIC = [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00];
const COMP_MAGIC = [0x00, 0x61, 0x73, 0x6d, 0x0d, 0x00, 0x01, 0x00];

// WASMMimicry wrapWASM: core v1 shell + custom section "unil" holding the
// inner bytes (wasigocvm component). Browser sees version 1; peel recovers
// the guest. Does not retarget the toolchain.
export function wrapMimicryWasm(inner) {
  const payload = asU8(inner);
  const name = [0x75, 0x6e, 0x69, 0x6c]; // "unil"
  const nameLEB = [];
  writeLEB(nameLEB, name.length);
  const customPayloadLen = nameLEB.length + name.length + payload.length;
  const sec = [0];
  writeLEB(sec, customPayloadLen);
  const out = new Uint8Array(8 + sec.length + customPayloadLen);
  out.set(CORE_MAGIC, 0);
  let o = 8;
  out.set(sec, o);
  o += sec.length;
  out.set(nameLEB, o);
  o += nameLEB.length;
  out.set(name, o);
  o += name.length;
  out.set(payload, o);
  return out;
}

export function peelMimicryWasm(raw) {
  const b = asU8(raw);
  if (!magicAt(b, 0, CORE_MAGIC)) return null;
  let off = 8;
  while (off < b.length) {
    const id = b[off++];
    const [n, nlen] = readLEB(b, off);
    if (!nlen) break;
    off += nlen;
    if (off + n > b.length) break;
    const payload = b.subarray(off, off + n);
    off += n;
    if (id !== 0) continue;
    const [nl, nllen] = readLEB(payload, 0);
    if (!nllen) continue;
    const name = new TextDecoder().decode(payload.subarray(nllen, nllen + nl));
    if (name === "unil" || name === "gocos") return payload.subarray(nllen + nl);
  }
  return null;
}

export function extractComponentCore(raw) {
  const b = asU8(raw);
  if (!magicAt(b, 0, COMP_MAGIC)) return null;
  let off = 8;
  let best = null;
  while (off < b.length) {
    const id = b[off++];
    const [n, nlen] = readLEB(b, off);
    if (!nlen) break;
    off += nlen;
    if (off + n > b.length) break;
    const payload = b.subarray(off, off + n);
    off += n;
    if (id !== 1) continue;
    let mod = null;
    if (magicAt(payload, 0, CORE_MAGIC)) mod = payload;
    else {
      const [count, clen] = readLEB(payload, 0);
      if (count && magicAt(payload, clen, CORE_MAGIC)) mod = payload.subarray(clen);
    }
    if (mod && (!best || mod.length > best.length)) best = mod;
  }
  return best;
}

export function guestCore(raw) {
  let b = asU8(raw);
  const peeled = peelMimicryWasm(b);
  if (peeled && peeled.length) b = peeled;
  const core = extractComponentCore(b);
  if (core && core.length) return core;
  return b;
}

export async function inspect(bytesIn) {
  const core = guestCore(bytesIn);
  const mod = await WebAssembly.compile(core);
  const imports = WebAssembly.Module.imports(mod).map((i) => ({
    module: i.module,
    name: i.name,
    kind: i.kind,
  }));
  const exports = WebAssembly.Module.exports(mod).map((e) => ({
    name: e.name,
    kind: e.kind,
  }));
  return { imports, exports, size: core.byteLength };
}

function stubImports(mod, base) {
  const imports = Object.assign({}, base);
  const nosys = () => ERRNO.NOSYS;
  const ok = () => ERRNO.SUCCESS;
  WebAssembly.Module.imports(mod).forEach((i) => {
    if (!imports[i.module]) imports[i.module] = {};
    if (imports[i.module][i.name] == null) {
      if (i.kind === "memory") {
        imports[i.module][i.name] = new WebAssembly.Memory({ initial: 1 });
      } else if (i.kind === "table") {
        imports[i.module][i.name] = new WebAssembly.Table({ initial: 0, element: "anyfunc" });
      } else if (i.kind === "global") {
        imports[i.module][i.name] = new WebAssembly.Global({ value: "i32", mutable: false }, 0);
      } else {
        imports[i.module][i.name] = i.name === "proc_exit" ? base.wasi_snapshot_preview1.proc_exit : nosys;
        if (i.name === "sched_yield") imports[i.module][i.name] = ok;
      }
    }
  });
  return imports;
}

// WASI 0.2 as flattened core imports (i32 handles + retptrs). Not WIT,
// not a component-model instantiator — wasm-component-ld already lowered
// the names; this is libc-shaped host for those functions.
function makeWasi02(opts, inst) {
  const stdout = typeof opts.stdout === "function" ? opts.stdout : (t) => console.log(t);
  const stderr = typeof opts.stderr === "function" ? opts.stderr : (t) => console.error(t);
  const args = opts.args && opts.args.length ? opts.args.slice() : ["wasm"];
  const envList = opts.env && opts.env.length ? opts.env.slice() : [];
  const sabIn = opts.stdinSab ? stdinFromSab(opts.stdinSab) : null;
  const handles = new Map();
  let nextH = 1;
  function intern(kind) {
    const h = nextH++;
    handles.set(h, kind);
    return h;
  }
  const hIn = intern("in");
  const hOut = intern("out");
  const hErr = intern("err");

  function memory() {
    return inst.exports.memory;
  }
  function realloc(oldPtr, oldLen, align, n) {
    const fn = inst.exports.cabi_realloc;
    if (typeof fn !== "function") return 0;
    return fn(oldPtr >>> 0, oldLen >>> 0, align >>> 0, n >>> 0) >>> 0;
  }
  function i32(ptr, v) {
    new DataView(memory().buffer).setInt32(ptr, v | 0, true);
  }
  function u32get(ptr) {
    return new DataView(memory().buffer).getUint32(ptr, true);
  }
  function u64(ptr, v) {
    const dv = new DataView(memory().buffer);
    dv.setUint32(ptr, v >>> 0, true);
    dv.setUint32(ptr + 4, Math.floor(v / 4294967296) >>> 0, true);
  }
  function putBytes(ptr, arr) {
    bytes(memory(), ptr, arr.length).set(arr);
  }
  function allocBytes(arr) {
    const p = realloc(0, 0, 1, arr.length);
    if (p && arr.length) putBytes(p, arr);
    return p;
  }
  function allocList(strs) {
    const n = strs.length;
    const lp = realloc(0, 0, 4, n * 8);
    for (let i = 0; i < n; i++) {
      const enc = new TextEncoder().encode(strs[i]);
      const sp = allocBytes(enc);
      i32(lp + i * 8, sp);
      i32(lp + i * 8 + 4, enc.length);
    }
    return lp;
  }
  function okEmpty(ret) {
    if (ret) i32(ret, 0);
  }
  function okU64(ret, n) {
    if (!ret) return;
    i32(ret, 0);
    u64(ret + 8, n);
  }
  function errClosed(ret) {
    if (!ret) return;
    i32(ret, 1);
    i32(ret + 4, 1);
  }
  function errGeneric(ret) {
    if (!ret) return;
    i32(ret, 1);
    i32(ret + 4, 0);
  }
  function putListU8(ret, arr) {
    const p = allocBytes(arr);
    i32(ret, 0);
    i32(ret + 4, p);
    i32(ret + 8, arr.length);
  }
  function drop(h) {
    handles.delete(h >>> 0);
  }
  function stdinBlock() {
    if (!opts.stdinSab) return;
    const i32a = new Int32Array(opts.stdinSab);
    while (Atomics.load(i32a, 1) === 0) {
      Atomics.wait(i32a, 0, 0);
    }
  }

  function dispatch(mod, name, a) {
    const m = String(mod);
    const n = String(name);
    if (n.indexOf("[resource-drop]") === 0) {
      drop(a[0]);
      return;
    }
    if (n === "exit" || n === "exit-with-code") {
      throw new ExitError(a[0] | 0);
    }
    if (n === "get-stdout") return hOut;
    if (n === "get-stderr") return hErr;
    if (n === "get-stdin") return hIn;
    if (n === "get-arguments") {
      const lp = allocList(args);
      i32(a[0], lp);
      i32(a[0] + 4, args.length);
      return;
    }
    if (n === "get-environment") {
      const lp = allocList(envList);
      i32(a[0], lp);
      i32(a[0] + 4, envList.length);
      return;
    }
    if (n === "now" && m.indexOf("monotonic-clock") >= 0) {
      return BigInt(Date.now()) * 1000000n;
    }
    if (n === "resolution" && m.indexOf("monotonic-clock") >= 0) {
      return 1000000n;
    }
    if (n === "now" && m.indexOf("wall-clock") >= 0) {
      const ms = Date.now();
      u64(a[0], Math.floor(ms / 1000));
      i32(a[0] + 8, (ms % 1000) * 1000000);
      return;
    }
    if (n === "resolution" && m.indexOf("wall-clock") >= 0) {
      u64(a[0], 0);
      i32(a[0] + 8, 1000000);
      return;
    }
    if (n === "get-random-bytes") {
      const want = Number(a[0]);
      const buf = new Uint8Array(want > 0 && want < 1e6 ? want : 0);
      if (buf.length) crypto.getRandomValues(buf);
      putListU8(a[1], buf);
      return;
    }
    if (n === "[method]output-stream.check-write") {
      okU64(a[1], 4096);
      return;
    }
    if (n === "[method]output-stream.write") {
      const ptr = a[1] >>> 0;
      const len = a[2] >>> 0;
      if (len) {
        const t = new TextDecoder("utf-8", { fatal: false }).decode(bytes(memory(), ptr, len));
        const k = handles.get(a[0] >>> 0);
        if (k === "err") stderr(t);
        else stdout(t);
      }
      okEmpty(a[3]);
      return;
    }
    if (n === "[method]output-stream.blocking-flush") {
      okEmpty(a[1]);
      return;
    }
    if (n === "[method]output-stream.subscribe" || n === "[method]input-stream.subscribe") {
      return intern("poll");
    }
    if (n === "[method]pollable.block") {
      const k = handles.get(a[0] >>> 0);
      if (k === "in" || k === "poll") stdinBlock();
      return;
    }
    if (n === "poll") {
      const inPtr = a[0] >>> 0;
      const inLen = a[1] >>> 0;
      const ret = a[2] >>> 0;
      for (let i = 0; i < inLen; i++) {
        const h = u32get(inPtr + i * 4);
        if (handles.get(h) === "in") stdinBlock();
      }
      const lp = realloc(0, 0, 4, inLen * 4);
      for (let i = 0; i < inLen; i++) i32(lp + i * 4, i);
      i32(ret, lp);
      i32(ret + 4, inLen);
      return;
    }
    if (n === "[method]input-stream.read") {
      const max = Number(a[1]);
      const nmax = max > 0 && max < 65536 ? max : 0;
      const buf = new Uint8Array(nmax);
      let got = 0;
      if (nmax && sabIn) got = sabIn.take(buf);
      putListU8(a[2], buf.subarray(0, got));
      return;
    }
    if (n.indexOf("get-terminal-") === 0) {
      i32(a[0], 0);
      return;
    }
    if (n === "subscribe-instant" || n === "subscribe-duration") {
      return intern("poll");
    }
    if (n === "instance-network") return intern("net");
    if (m.indexOf("filesystem") >= 0 || m.indexOf("sockets") >= 0) {
      errGeneric(a[a.length - 1]);
      return 0;
    }
    if (n.charAt(0) === "[" || a.length && typeof a[a.length - 1] === "number") {
      errClosed(a[a.length - 1]);
    }
    return 0;
  }

  return { dispatch, intern };
}

function bindP2Imports(mod, imports, p2) {
  WebAssembly.Module.imports(mod).forEach((i) => {
    const m = String(i.module);
    if (m.indexOf("wasi:") !== 0) return;
    if (!imports[m]) imports[m] = {};
    const n = i.name;
    imports[m][n] = function () {
      return p2.dispatch(m, n, Array.prototype.slice.call(arguments));
    };
  });
}

// A file's bytes: a Uint8Array, or a lazy source {size, readAt(pos, n)} the
// page seeded (a Blob/File, {url, size} read by HTTP Range, {sab, off, size}
// as a live view of a SharedArrayBuffer, or any {size, readAt} the worker
// built, such as a BNZ view). Lazy files are read in the pieces the guest
// asks for, so a large one (model weights) never sits whole in JS memory.
// Blob and HTTP readers are synchronous: FileReaderSync and sync XHR exist
// in workers, which is where a guest runs.
function lazySource(v) {
  if (v && typeof v.readAt === "function" && typeof v.size === "number" && typeof v.url !== "string" && !(typeof Blob !== "undefined" && v instanceof Blob) && !(typeof SharedArrayBuffer !== "undefined" && v.sab instanceof SharedArrayBuffer)) {
    const src = {
      size: v.size,
      readAt(pos, n) { return v.readAt(pos, n); },
    };
    // A streaming store (the phone embedding file) writes in place. Dropping
    // writeAt would make the guest copy the whole file into one array.
    if (typeof v.writeAt === "function") src.writeAt = function (pos, data) { return v.writeAt(pos, data); };
    if (typeof v.flush === "function") src.flush = function () { v.flush(); };
    return src;
  }
  if (typeof Blob !== "undefined" && v instanceof Blob) {
    return {
      size: v.size,
      readAt(pos, n) {
        return new Uint8Array(new FileReaderSync().readAsArrayBuffer(v.slice(pos, pos + n)));
      },
    };
  }
  // A live view of shared memory: the page writes it, the guest reads its
  // current bytes on every read (a stop flag, a control word), and a guest
  // write lands in it in place (a frame the page reads back). Its size is
  // fixed: writeAt refuses bytes past the end.
  if (v && typeof SharedArrayBuffer !== "undefined" && v.sab instanceof SharedArrayBuffer) {
    const off = v.off | 0;
    const size = typeof v.size === "number" ? v.size : v.sab.byteLength - off;
    return {
      size,
      readAt(pos, n) {
        return new Uint8Array(v.sab, off + pos, Math.max(0, Math.min(n, size - pos))).slice();
      },
      writeAt(pos, src) {
        if (pos < 0 || pos + src.length > size) return false;
        new Uint8Array(v.sab, off + pos, src.length).set(src);
        return true;
      },
    };
  }
  if (v && typeof v.url === "string" && typeof v.size === "number") {
    return {
      size: v.size,
      readAt(pos, n) {
        const x = new XMLHttpRequest();
        x.open("GET", v.url, false);
        x.setRequestHeader("Range", "bytes=" + pos + "-" + (pos + n - 1));
        x.responseType = "arraybuffer";
        x.send();
        if (x.status !== 206 && x.status !== 200) throw new Error(v.url + ": HTTP " + x.status);
        const got = new Uint8Array(x.response);
        // A server that ignores Range sends the whole file.
        return x.status === 200 ? got.subarray(pos, pos + n) : got;
      },
    };
  }
  return null;
}

function sizeOf(data) {
  return data instanceof Uint8Array ? data.length : data.size;
}

function readOf(data, pos, n) {
  return data instanceof Uint8Array ? data.subarray(pos, pos + n) : data.readAt(pos, n);
}

// Client file table for goclibc. The unil sandbox (wasmv16 / quickjs-ng)
// is the JS runner, and this page is that client. serve.mjs only delivers
// bytes; it does not instantiate the guest.
function makeGocLibc(mem, files) {
  const boxes = [null];
  const fds = [null];
  const dirs = new Set();
  let cwd = ".";
  function norm(p) {
    return String(p).replace(/\\/g, "/").replace(/\/+$/, "");
  }
  function parent(p) {
    const s = norm(p);
    const i = s.lastIndexOf("/");
    return i < 0 ? "" : s.slice(0, i);
  }
  function readStr(ptr, n) {
    const m = mem();
    if (!m || n <= 0) return "";
    let s = "";
    const b = bytes(m, ptr, n);
    for (let i = 0; i < b.length; i++) s += String.fromCharCode(b[i]);
    return s;
  }
  function putStat(out, isdir, size) {
    const m = mem();
    const mode = isdir ? 0x4000 | 0o755 : 0x8000 | 0o644;
    u32(m, out, mode);
    u32(m, out + 4, isdir ? 1 : 0);
    u64(m, out + 8, size);
  }
  function newBox(path, data) {
    const box = { path: norm(path), data: data || new Uint8Array(0), refs: 1 };
    // A phone opens one embedding slice per read (path/@offset). Reuse a
    // closed slice slot so a long chat does not keep a box per token.
    if (box.path.indexOf("/@") >= 0) {
      for (let i = 1; i < boxes.length; i++) {
        if (!boxes[i]) {
          boxes[i] = box;
          return i;
        }
      }
    }
    boxes.push(box);
    return boxes.length - 1;
  }
  function newFd(box, app, pos) {
    fds.push({ box, pos, append: app });
    return fds.length - 1;
  }
  function fdOk(fd) {
    return fd > 0 && fd < fds.length && fds[fd];
  }
  const api = {
    open(pathPtr, pathLen, flags) {
      const path = norm(readStr(pathPtr, pathLen));
      if (!path) return -22;
      const trunc = flags & 8;
      const creat = flags & 4;
      const app = flags & 16 ? 1 : 0;
      let data = null;
      const hit = boxes.find((b) => b && b.path === path);
      // embed.bf16/@12345 is a read that starts at that byte. The guest has
      // no Seek; the embedding table stays one OPFS file.
      if (!hit && !trunc && !creat) {
        const at = path.lastIndexOf("/@");
        if (at > 0 && /^\d+$/.test(path.slice(at + 2))) {
          const base = boxes.find((b) => b && b.path === path.slice(0, at));
          const off = Number(path.slice(at + 2));
          if (base && off >= 0 && off <= sizeOf(base.data)) {
            const parent = base.data;
            const size = sizeOf(parent) - off;
            data = {
              size,
              readAt(pos, n) {
                const avail = size - pos;
                if (pos < 0 || avail <= 0) return new Uint8Array(0);
                return readOf(parent, off + pos, Math.min(n, avail));
              },
            };
            return newFd(newBox(path, data), 0, 0);
          }
        }
      }
      if (hit && hit.data && typeof hit.data.writeAt === "function") data = hit.data; // shared memory keeps its bytes: truncating means writing from the start
      else if (!trunc && hit) data = hit.data;
      else if (!trunc && !creat && !hit) return -2;
      else data = new Uint8Array(0);
      const id = newBox(path, data);
      let pos = 0;
      if (app) pos = sizeOf(boxes[id].data);
      return newFd(id, app, pos);
    },
    read(fd, ptr, n) {
      if (!fdOk(fd) || n <= 0) return fdOk(fd) ? 0 : -9;
      const slot = fds[fd];
      const data = boxes[slot.box].data;
      const size = sizeOf(data);
      if (slot.pos >= size) return 0;
      const avail = Math.min(n, size - slot.pos);
      bytes(mem(), ptr, avail).set(readOf(data, slot.pos, avail));
      slot.pos += avail;
      return avail;
    },
    write(fd, ptr, n) {
      if (!fdOk(fd)) return -9;
      const slot = fds[fd];
      const box = boxes[slot.box];
      const src = n > 0 ? bytes(mem(), ptr, n) : new Uint8Array(0);
      if (typeof box.data.writeAt === "function") { // shared memory, written in place
        const at = slot.append ? box.data.size : slot.pos;
        if (!box.data.writeAt(at, src)) return -27; // EFBIG: past the shared memory's end
        slot.pos = at + src.length;
        return src.length;
      }
      if (!(box.data instanceof Uint8Array)) box.data = box.data.readAt(0, box.data.size);
      if (slot.append) slot.pos = box.data.length;
      const end = slot.pos + src.length;
      const next = new Uint8Array(Math.max(box.data.length, end));
      next.set(box.data);
      next.set(src, slot.pos);
      box.data = next;
      slot.pos = end;
      return src.length;
    },
    close(fd) {
      if (!fdOk(fd)) return -9;
      const slot = fds[fd];
      const box = boxes[slot.box];
      box.refs--;
      if (box.refs <= 0 && box.path.indexOf("/@") >= 0) boxes[slot.box] = null;
      fds[fd] = null;
      return 0;
    },
    seek(fd, off, whence) {
      if (!fdOk(fd)) return -9;
      const slot = fds[fd];
      const len = sizeOf(boxes[slot.box].data);
      let base = 0;
      if (whence === 1) base = slot.pos;
      if (whence === 2) base = len;
      const npos = base + (off | 0);
      if (npos < 0) return -22;
      slot.pos = npos;
      return npos;
    },
    flush(fd) {
      if (!fdOk(fd)) return -9;
      const data = boxes[fds[fd].box].data;
      if (data && typeof data.flush === "function") data.flush();
      return 0;
    },
    stat(pathPtr, pathLen, out) {
      const path = norm(readStr(pathPtr, pathLen));
      if (dirs.has(path)) {
        putStat(out, 1, 0);
        return 0;
      }
      const hit = boxes.find((b) => b && b.path === path && b.refs > 0);
      if (!hit) return -2;
      putStat(out, 0, sizeOf(hit.data));
      return 0;
    },
    fstat(fd, out) {
      if (!fdOk(fd)) return -9;
      const box = boxes[fds[fd].box];
      putStat(out, dirs.has(box.path) ? 1 : 0, sizeOf(box.data));
      return 0;
    },
    mkdir(pathPtr, pathLen) {
      const path = norm(readStr(pathPtr, pathLen));
      if (!path) return -22;
      if (dirs.has(path)) return -17;
      dirs.add(path);
      return 0;
    },
    unlink(pathPtr, pathLen) {
      const path = norm(readStr(pathPtr, pathLen));
      if (dirs.has(path)) return -21;
      let found = false;
      for (let i = 1; i < boxes.length; i++) {
        if (boxes[i] && boxes[i].path === path) {
          boxes[i] = null;
          found = true;
        }
      }
      return found ? 0 : -2;
    },
    rmdir(pathPtr, pathLen) {
      const path = norm(readStr(pathPtr, pathLen));
      if (!dirs.has(path)) return -2;
      dirs.delete(path);
      return 0;
    },
    rename(oldPtr, oldLen, newPtr, newLen) {
      const a = norm(readStr(oldPtr, oldLen));
      const b = norm(readStr(newPtr, newLen));
      if (dirs.has(a)) {
        dirs.delete(a);
        dirs.add(b);
        return 0;
      }
      const hit = boxes.find((x) => x && x.path === a);
      if (!hit) return -2;
      hit.path = b;
      return 0;
    },
    access(pathPtr, pathLen) {
      const path = norm(readStr(pathPtr, pathLen));
      if (dirs.has(path)) return 0;
      return boxes.some((b) => b && b.path === path) ? 0 : -2;
    },
    getcwd(buf, cap) {
      const s = cwd;
      if (cap < s.length + 1) return -22;
      const m = mem();
      const b = bytes(m, buf, s.length + 1);
      for (let i = 0; i < s.length; i++) b[i] = s.charCodeAt(i) & 255;
      b[s.length] = 0;
      return s.length;
    },
    chdir(pathPtr, pathLen) {
      const path = norm(readStr(pathPtr, pathLen));
      if (!dirs.has(path) && path !== "." && path !== "") return -2;
      cwd = path || ".";
      return 0;
    },
    readdir(pathPtr, pathLen, buf, cap) {
      const path = norm(readStr(pathPtr, pathLen));
      const names = [];
      dirs.forEach((d) => {
        if (parent(d) === path) names.push("d" + d.slice(path.length + 1));
      });
      boxes.forEach((b) => {
        if (b && parent(b.path) === path) names.push("f" + b.path.slice(b.path.lastIndexOf("/") + 1));
      });
      const out = [];
      for (let i = 0; i < names.length; i++) {
        const kind = names[i].charCodeAt(0);
        const rest = names[i].slice(1);
        out.push(kind);
        for (let j = 0; j < rest.length; j++) out.push(rest.charCodeAt(j) & 255);
        out.push(0);
      }
      if (out.length > cap) return -22;
      if (out.length) bytes(mem(), buf, out.length).set(out);
      return out.length;
    },
    dup(fd) {
      if (!fdOk(fd)) return -9;
      const slot = fds[fd];
      boxes[slot.box].refs++;
      return newFd(slot.box, slot.append, slot.pos);
    },
  };
  // Files the page seeded. fopen of a seeded path hits this table. A value
  // is bytes (Uint8Array / ArrayBuffer / array), or a Blob / {url, size},
  // which is read lazily (lazySource).
  if (files) {
    for (const path of Object.keys(files)) {
      const p = norm(path);
      let data = files[path];
      if (!(data instanceof Uint8Array)) data = lazySource(data) || new Uint8Array(data || []);
      newBox(p, data);
      let d = parent(p);
      while (d) {
        dirs.add(d);
        d = parent(d);
      }
    }
  }
  return api;
}

export async function instantiate(bytes, opts) {
  opts = opts || {};
  const core = guestCore(bytes);
  const mod = await WebAssembly.compile(core);
  const wasi = makeWasi(opts);
  const p2 = makeWasi02(opts, wasi.inst);
  const env = {
    log(ptr, len) {
      const m = wasi.inst.exports.memory;
      if (!m || !len) return;
      const t = new TextDecoder("utf-8", { fatal: false }).decode(bytes(m, ptr, len));
      const stdout = typeof opts.stdout === "function" ? opts.stdout : (s) => console.log(s);
      stdout(t);
    },
    abort(code) {
      throw new ExitError(code | 0);
    },
  };
  if (opts.stringImports) {
    for (const name of Object.keys(opts.stringImports)) {
      const spec = opts.stringImports[name];
      env[name] = packedStringImport(spec.arity, spec.fn)(wasi.inst);
    }
  }
  const base = {
    wasi_snapshot_preview1: wasi.preview1,
    wasi_unstable: wasi.preview1,
    env,
    goclibc: makeGocLibc(() => wasi.inst.exports.memory, opts.files),
  };
  const imports = stubImports(mod, base);
  bindP2Imports(mod, imports, p2);
  // webgpu.batch (WASMWebGPU, linked into a guest that imports "webgpu"):
  // a JSPI suspending import. opts.webgpu is WASMWebGPU/js/webgpu-exec.js
  // ({runBatch, writeReply}); the batch runs on this host's navigator.gpu
  // and the guest is suspended while a WebGPU promise settles. Without JSPI
  // or opts.webgpu the import answers -1 and the guest reports no WebGPU.
  let suspends = false;
  const gpuImport = WebAssembly.Module.imports(mod).some((i) => i.module === "webgpu" && i.name === "batch");
  if (gpuImport) {
    const jspi = typeof WebAssembly.Suspending === "function" && typeof WebAssembly.promising === "function";
    if (jspi && opts.webgpu) {
      const gpu = opts.webgpu;
      imports.webgpu.batch = new WebAssembly.Suspending((req, len, outPtr, cap) => {
        const m = wasi.inst.exports.memory;
        const answer = (reply) => gpu.writeReply(reply, new Uint8Array(m.buffer, outPtr >>> 0, cap >>> 0));
        let reply;
        try {
          reply = gpu.runBatch(new Uint8Array(m.buffer, req >>> 0, len >>> 0));
        } catch (e) {
          return answer({ text: "error=webgpu batch: " + (e && e.message ? e.message : String(e)), bin: null });
        }
        return reply && typeof reply.then === "function" ? reply.then(answer) : answer(reply);
      });
      suspends = true;
    } else {
      imports.webgpu.batch = () => -1;
    }
  }
  const instance = await WebAssembly.instantiate(mod, imports);
  wasi.inst.exports = instance.exports;
  return { module: mod, instance, exports: instance.exports, ExitError, suspends };
}

// A guest with a suspending import runs through WebAssembly.promising, so
// it can suspend; the others are called directly.
async function enter(loaded, fn, args) {
  if (loaded.suspends) return WebAssembly.promising(fn)(...(args || []));
  return fn(...(args || []));
}

export async function run(loaded) {
  const exp = loaded.exports || loaded.instance.exports;
  try {
    if (typeof exp._start === "function") {
      await enter(loaded, exp._start);
      return 0;
    }
    const names = Object.keys(exp);
    for (let i = 0; i < names.length; i++) {
      const n = names[i];
      if (typeof exp[n] !== "function") continue;
      if (n === "run" || n.indexOf("#run") >= 0) {
        const got = await enter(loaded, exp[n]);
        return got | 0;
      }
    }
    return 0;
  } catch (e) {
    if (e && (e.name === "ExitError" || typeof e.code === "number")) return e.code | 0;
    throw e;
  }
}

export async function call(loaded, name, args) {
  const fn = (loaded.exports || loaded.instance.exports)[name];
  if (typeof fn !== "function") throw new Error("unknown export " + name);
  const a = (args || []).map((x) => x | 0);
  try {
    const got = await enter(loaded, fn, a);
    return got;
  } catch (e) {
    if (e && (e.name === "ExitError" || typeof e.code === "number")) return e.code | 0;
    throw e;
  }
}

export { ExitError };