watr 4.2.1 → 4.3.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.
- package/dist/watr.js +149 -38
- package/dist/watr.min.js +5 -5
- package/package.json +1 -1
- package/readme.md +7 -12
- package/src/compile.js +145 -39
- package/src/const.js +12 -4
- package/types/src/compile.d.ts.map +1 -1
- package/types/src/const.d.ts +6 -0
- package/types/src/const.d.ts.map +1 -1
package/dist/watr.js
CHANGED
|
@@ -447,13 +447,14 @@ var INSTR = [
|
|
|
447
447
|
,
|
|
448
448
|
,
|
|
449
449
|
,
|
|
450
|
-
|
|
451
|
-
,
|
|
452
|
-
,
|
|
453
|
-
,
|
|
454
|
-
,
|
|
455
|
-
,
|
|
456
|
-
,
|
|
450
|
+
// 0xe0-0xe6: stack switching (Phase 3)
|
|
451
|
+
"cont.new typeidx",
|
|
452
|
+
"cont.bind cont_bind",
|
|
453
|
+
"suspend tagidx",
|
|
454
|
+
"resume resume",
|
|
455
|
+
"resume_throw resume_throw",
|
|
456
|
+
"resume_throw_ref resume_throw_ref",
|
|
457
|
+
"switch switch_cont",
|
|
457
458
|
,
|
|
458
459
|
,
|
|
459
460
|
,
|
|
@@ -506,7 +507,15 @@ var INSTR = [
|
|
|
506
507
|
"extern.convert_any",
|
|
507
508
|
"ref.i31",
|
|
508
509
|
"i31.get_s",
|
|
509
|
-
"i31.get_u"
|
|
510
|
+
"i31.get_u",
|
|
511
|
+
,
|
|
512
|
+
"struct.new_desc typeidx",
|
|
513
|
+
"struct.new_default_desc typeidx",
|
|
514
|
+
"ref.get_desc typeidx",
|
|
515
|
+
"ref.cast_desc_eq reftype",
|
|
516
|
+
,
|
|
517
|
+
"br_on_cast_desc_eq reftype2",
|
|
518
|
+
"br_on_cast_desc_eq_fail reftype2"
|
|
510
519
|
],
|
|
511
520
|
// 0xfc: Bulk memory/table operations (nested array)
|
|
512
521
|
[
|
|
@@ -920,6 +929,9 @@ var TYPE = {
|
|
|
920
929
|
i31: 108,
|
|
921
930
|
struct: 107,
|
|
922
931
|
array: 106,
|
|
932
|
+
cont: 104,
|
|
933
|
+
nocont: 117,
|
|
934
|
+
// stack switching (Phase 3)
|
|
923
935
|
// Reference type abbreviations (absheaptype abbrs)
|
|
924
936
|
nullfuncref: 115,
|
|
925
937
|
nullexternref: 114,
|
|
@@ -933,6 +945,9 @@ var TYPE = {
|
|
|
933
945
|
i31ref: 108,
|
|
934
946
|
structref: 107,
|
|
935
947
|
arrayref: 106,
|
|
948
|
+
contref: 104,
|
|
949
|
+
nocontref: 117,
|
|
950
|
+
// stack switching abbreviations
|
|
936
951
|
// ref, refnull
|
|
937
952
|
ref: 100,
|
|
938
953
|
// -0x1c
|
|
@@ -943,7 +958,7 @@ var TYPE = {
|
|
|
943
958
|
subfinal: 79,
|
|
944
959
|
rec: 78
|
|
945
960
|
};
|
|
946
|
-
var DEFTYPE = { func: 96, struct: 95, array: 94, sub: 80, subfinal: 79, rec: 78 };
|
|
961
|
+
var DEFTYPE = { func: 96, struct: 95, array: 94, cont: 93, sub: 80, subfinal: 79, rec: 78 };
|
|
947
962
|
var KIND = { func: 0, table: 1, memory: 2, global: 3, tag: 4 };
|
|
948
963
|
|
|
949
964
|
// src/parse.js
|
|
@@ -1010,6 +1025,17 @@ function compile(nodes) {
|
|
|
1010
1025
|
else if (typeof nodes[0] === "string") nodes = [nodes];
|
|
1011
1026
|
if (nodes[idx] === "binary") return Uint8Array.from(nodes.slice(++idx).flat());
|
|
1012
1027
|
if (nodes[idx] === "quote") return compile(nodes.slice(++idx).map((v) => v.valueOf().slice(1, -1)).flat().join(""));
|
|
1028
|
+
nodes = nodes.flatMap((n, i) => {
|
|
1029
|
+
if (i < idx || !Array.isArray(n) || n[0] !== "import") return [n];
|
|
1030
|
+
const [, mod, ...rest] = n;
|
|
1031
|
+
if (!rest.some((r) => Array.isArray(r) && r[0] === "item")) return [n];
|
|
1032
|
+
const lastIsType = Array.isArray(rest.at(-1)) && rest.at(-1)[0] !== "item";
|
|
1033
|
+
if (lastIsType) {
|
|
1034
|
+
const type = rest.at(-1);
|
|
1035
|
+
return rest.slice(0, -1).filter((r) => r[0] === "item").map(([, nm]) => ["import", mod, nm, type]);
|
|
1036
|
+
}
|
|
1037
|
+
return rest.filter((r) => r[0] === "item").map(([, nm, type]) => ["import", mod, nm, type]);
|
|
1038
|
+
});
|
|
1013
1039
|
const ctx = [];
|
|
1014
1040
|
for (let kind in SECTION) (ctx[SECTION[kind]] = ctx[kind] = []).name = kind;
|
|
1015
1041
|
ctx.metadata = {};
|
|
@@ -1040,12 +1066,19 @@ function compile(nodes) {
|
|
|
1040
1066
|
for (let i = 0; i < node.length; i++) {
|
|
1041
1067
|
let [, ...subnode] = node[i];
|
|
1042
1068
|
name(subnode, ctx.type);
|
|
1069
|
+
const tdesc = [];
|
|
1070
|
+
while (subnode[0]?.[0] === "descriptor" || subnode[0]?.[0] === "describes") tdesc.push(subnode.shift());
|
|
1043
1071
|
(subnode = typedef(subnode, ctx)).push(i ? true : [ctx.type.length, node.length]);
|
|
1072
|
+
if (tdesc.length) subnode.desc = subnode.desc ? [...tdesc, ...subnode.desc] : tdesc;
|
|
1044
1073
|
ctx.type.push(subnode);
|
|
1045
1074
|
}
|
|
1046
1075
|
} else if (kind === "type") {
|
|
1047
1076
|
name(node, ctx.type);
|
|
1048
|
-
|
|
1077
|
+
const tdesc = [];
|
|
1078
|
+
while (node[0]?.[0] === "descriptor" || node[0]?.[0] === "describes") tdesc.push(node.shift());
|
|
1079
|
+
const td = typedef(node, ctx);
|
|
1080
|
+
if (tdesc.length) td.desc = td.desc ? [...tdesc, ...td.desc] : tdesc;
|
|
1081
|
+
ctx.type.push(td);
|
|
1049
1082
|
} else if (kind === "start" || kind === "export") ctx[kind].push(node);
|
|
1050
1083
|
else return true;
|
|
1051
1084
|
}).forEach((n) => {
|
|
@@ -1068,7 +1101,8 @@ function compile(nodes) {
|
|
|
1068
1101
|
} else if (kind === "memory") {
|
|
1069
1102
|
const is64 = node[0] === "i64", idx2 = is64 ? 1 : 0;
|
|
1070
1103
|
if (node[idx2]?.[0] === "data") {
|
|
1071
|
-
|
|
1104
|
+
const ps = node.find((n2) => Array.isArray(n2) && n2[0] === "pagesize")?.[1] ?? 65536;
|
|
1105
|
+
let [, ...data] = node.splice(idx2, 1)[0], m = "" + Math.ceil(data.reduce((s, d) => s + d.length, 0) / ps);
|
|
1072
1106
|
ctx.data.push([["memory", items.length], [is64 ? "i64.const" : "i32.const", is64 ? 0n : 0], ...data]);
|
|
1073
1107
|
node = is64 ? ["i64", m, m] : [m, m];
|
|
1074
1108
|
}
|
|
@@ -1195,7 +1229,7 @@ function normalize(nodes, ctx) {
|
|
|
1195
1229
|
out.push(...normalize(parts, ctx), "end");
|
|
1196
1230
|
} else {
|
|
1197
1231
|
const imm = [];
|
|
1198
|
-
while (parts.length && (!Array.isArray(parts[0]) || "type,param,result,ref".includes(parts[0][0]))) imm.push(parts.shift());
|
|
1232
|
+
while (parts.length && (!Array.isArray(parts[0]) || "type,param,result,ref,exact,on".includes(parts[0][0]))) imm.push(parts.shift());
|
|
1199
1233
|
out.push(...normalize(parts, ctx), op, ...imm);
|
|
1200
1234
|
nodes.unshift(...out.splice(out.length - 1 - imm.length));
|
|
1201
1235
|
}
|
|
@@ -1240,16 +1274,19 @@ var name = (node, list) => {
|
|
|
1240
1274
|
return nm;
|
|
1241
1275
|
};
|
|
1242
1276
|
var typedef = ([dfn], ctx) => {
|
|
1243
|
-
let subkind = "subfinal", supertypes = [], compkind;
|
|
1277
|
+
let subkind = "subfinal", supertypes = [], compkind, desc = [];
|
|
1244
1278
|
if (dfn[0] === "sub") {
|
|
1245
1279
|
subkind = dfn.shift(), dfn[0] === "final" && (subkind += dfn.shift());
|
|
1246
1280
|
dfn = (supertypes = dfn).pop();
|
|
1281
|
+
supertypes = supertypes.filter((n) => Array.isArray(n) && (n[0] === "descriptor" || n[0] === "describes") ? (desc.push(n), false) : true);
|
|
1247
1282
|
}
|
|
1248
1283
|
[compkind, ...dfn] = dfn;
|
|
1249
1284
|
if (compkind === "func") dfn = paramres(dfn), ctx.type["$" + dfn.join(">")] ??= ctx.type.length;
|
|
1250
1285
|
else if (compkind === "struct") dfn = fieldseq(dfn, "field");
|
|
1251
1286
|
else if (compkind === "array") [dfn] = dfn;
|
|
1252
|
-
|
|
1287
|
+
const result = [compkind, dfn, subkind, supertypes];
|
|
1288
|
+
if (desc.length) result.desc = desc;
|
|
1289
|
+
return result;
|
|
1253
1290
|
};
|
|
1254
1291
|
var build = [
|
|
1255
1292
|
// (@custom "name" placement? data) - custom section builder
|
|
@@ -1264,29 +1301,37 @@ var build = [
|
|
|
1264
1301
|
// (func params result)
|
|
1265
1302
|
// (array i8)
|
|
1266
1303
|
// (struct ...fields)
|
|
1267
|
-
(
|
|
1304
|
+
// (cont $ft) - stack switching (Phase 3)
|
|
1305
|
+
(node, ctx) => {
|
|
1306
|
+
const [kind, fields, subkind, supertypes, rec] = node;
|
|
1268
1307
|
if (rec === true) return;
|
|
1269
|
-
|
|
1308
|
+
const descPfx = (node.desc ?? []).flatMap(([clause, ref]) => [clause === "descriptor" ? 77 : 76, ...uleb(id(ref, ctx.type))]);
|
|
1309
|
+
const comptype = (k, f) => {
|
|
1310
|
+
if (k === "func") return [DEFTYPE.func, ...vec(f[0].map((t) => reftype(t, ctx))), ...vec(f[1].map((t) => reftype(t, ctx)))];
|
|
1311
|
+
if (k === "array") return [DEFTYPE.array, ...fieldtype(f, ctx)];
|
|
1312
|
+
if (k === "struct") return [DEFTYPE.struct, ...vec(f.map((t) => fieldtype(t, ctx)))];
|
|
1313
|
+
if (k === "cont") return [DEFTYPE.cont, ...uleb(id(f[0] ?? f, ctx.type))];
|
|
1314
|
+
return [DEFTYPE[k]];
|
|
1315
|
+
};
|
|
1270
1316
|
if (rec) {
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1317
|
+
let [from, length] = rec;
|
|
1318
|
+
const subtypes = Array.from({ length }, (_, i) => {
|
|
1319
|
+
const t = ctx.type[from + i], sub = t.slice(0, 4);
|
|
1320
|
+
if (t.desc) sub.desc = t.desc;
|
|
1321
|
+
return build[SECTION.type](sub, ctx);
|
|
1322
|
+
});
|
|
1323
|
+
return [DEFTYPE.rec, ...vec(subtypes)];
|
|
1274
1324
|
} else if (subkind === "sub" || supertypes?.length) {
|
|
1275
|
-
|
|
1276
|
-
kind = subkind;
|
|
1277
|
-
} else if (kind === "func") {
|
|
1278
|
-
details = [...vec(fields[0].map((t) => reftype(t, ctx))), ...vec(fields[1].map((t) => reftype(t, ctx)))];
|
|
1279
|
-
} else if (kind === "array") {
|
|
1280
|
-
details = fieldtype(fields, ctx);
|
|
1281
|
-
} else if (kind === "struct") {
|
|
1282
|
-
details = vec(fields.map((t) => fieldtype(t, ctx)));
|
|
1325
|
+
return [DEFTYPE[subkind], ...vec(supertypes.map((n) => id(n, ctx.type))), ...descPfx, ...comptype(kind, fields)];
|
|
1283
1326
|
}
|
|
1284
|
-
return [
|
|
1327
|
+
return [...descPfx, ...comptype(kind, fields)];
|
|
1285
1328
|
},
|
|
1286
1329
|
// (import "math" "add" (func|table|global|memory|tag dfn?))
|
|
1287
1330
|
([mod, field, [kind, ...dfn]], ctx) => {
|
|
1288
|
-
let details;
|
|
1331
|
+
let details, kindByte = KIND[kind];
|
|
1289
1332
|
if (kind === "func") {
|
|
1333
|
+
const isExact = dfn[0] === "exact" && dfn.shift();
|
|
1334
|
+
if (isExact) kindByte = 32;
|
|
1290
1335
|
let [[, typeidx]] = dfn;
|
|
1291
1336
|
details = uleb(id(typeidx, ctx.type));
|
|
1292
1337
|
} else if (kind === "tag") {
|
|
@@ -1299,7 +1344,7 @@ var build = [
|
|
|
1299
1344
|
} else if (kind === "table") {
|
|
1300
1345
|
details = [...reftype(dfn.pop(), ctx), ...limits(dfn)];
|
|
1301
1346
|
} else err(`Unknown kind ${kind}`);
|
|
1302
|
-
return [...vec(mod), ...vec(field),
|
|
1347
|
+
return [...vec(mod), ...vec(field), kindByte, ...details];
|
|
1303
1348
|
},
|
|
1304
1349
|
// (func $name? ...params result ...body)
|
|
1305
1350
|
([[, typeidx]], ctx) => uleb(id(typeidx, ctx.type)),
|
|
@@ -1413,6 +1458,7 @@ var build = [
|
|
|
1413
1458
|
// (data (i32.const 0) "\aa" "\bb"?)
|
|
1414
1459
|
// (data (memory ref) (offset (i32.const 0)) "\aa" "\bb"?)
|
|
1415
1460
|
// (data (global.get $x) "\aa" "\bb"?)
|
|
1461
|
+
// (data (i8 1 2 3) ...) numeric values (WAT numeric values, Phase 2)
|
|
1416
1462
|
(inits, ctx) => {
|
|
1417
1463
|
let offset, memidx = 0;
|
|
1418
1464
|
if (inits[0]?.[0] === "memory") {
|
|
@@ -1435,7 +1481,7 @@ var build = [
|
|
|
1435
1481
|
[1]
|
|
1436
1482
|
)
|
|
1437
1483
|
),
|
|
1438
|
-
...vec(inits.
|
|
1484
|
+
...vec(inits.flatMap((item) => numdata(item) ?? [...item]))
|
|
1439
1485
|
];
|
|
1440
1486
|
},
|
|
1441
1487
|
// datacount
|
|
@@ -1443,7 +1489,13 @@ var build = [
|
|
|
1443
1489
|
// (tag $name? (type idx))
|
|
1444
1490
|
([[, typeidx]], ctx) => [0, ...uleb(id(typeidx, ctx.type))]
|
|
1445
1491
|
];
|
|
1446
|
-
var reftype = (t, ctx) => t[0] === "ref" ? t[1] == "null" ?
|
|
1492
|
+
var reftype = (t, ctx) => t[0] === "ref" ? t[1] == "null" ? (
|
|
1493
|
+
// (ref null (exact $T)) - exact nullable ref
|
|
1494
|
+
Array.isArray(t[2]) && t[2][0] === "exact" ? [TYPE.refnull, 98, ...uleb(id(t[2][1], ctx.type))] : TYPE[t[2]] ? [TYPE[t[2]]] : [TYPE.refnull, ...uleb(id(t[t.length - 1], ctx.type))]
|
|
1495
|
+
) : (
|
|
1496
|
+
// (ref (exact $T)) - exact non-null ref
|
|
1497
|
+
Array.isArray(t[1]) && t[1][0] === "exact" ? [TYPE.ref, 98, ...uleb(id(t[1][1], ctx.type))] : [TYPE.ref, ...uleb(TYPE[t[t.length - 1]] || id(t[t.length - 1], ctx.type))]
|
|
1498
|
+
) : (
|
|
1447
1499
|
// abbrs
|
|
1448
1500
|
[TYPE[t] ?? err(`Unknown type ${t}`)]
|
|
1449
1501
|
);
|
|
@@ -1491,7 +1543,7 @@ var IMM = {
|
|
|
1491
1543
|
},
|
|
1492
1544
|
ref_null: (n, c) => {
|
|
1493
1545
|
let t = n.shift();
|
|
1494
|
-
return TYPE[t] ? [TYPE[t]] : uleb(id(t, c.type));
|
|
1546
|
+
return Array.isArray(t) && t[0] === "exact" ? [98, ...uleb(id(t[1], c.type))] : TYPE[t] ? [TYPE[t]] : uleb(id(t, c.type));
|
|
1495
1547
|
},
|
|
1496
1548
|
memarg: (n, c, op) => memargEnc(n, op, isIdx(n[0]) && !isMemParam(n[0]) ? id(n.shift(), c.memory) : 0),
|
|
1497
1549
|
opt_memory: (n, c) => uleb(id(isIdx(n[0]) ? n.shift() : 0, c.memory)),
|
|
@@ -1500,8 +1552,8 @@ var IMM = {
|
|
|
1500
1552
|
return ht.length > 1 ? ht.slice(1) : ht;
|
|
1501
1553
|
},
|
|
1502
1554
|
reftype2: (n, c) => {
|
|
1503
|
-
let b = blockid(n.shift(), c.block), h1 = reftype(n.shift(), c), h2 = reftype(n.shift(), c);
|
|
1504
|
-
return [(h2[0] !== TYPE.ref) << 1 | h1[0] !== TYPE.ref, ...uleb(b), h1
|
|
1555
|
+
let b = blockid(n.shift(), c.block), h1 = reftype(n.shift(), c), h2 = reftype(n.shift(), c), ht = (h) => h.length > 1 ? h.slice(1) : h;
|
|
1556
|
+
return [(h2[0] !== TYPE.ref) << 1 | h1[0] !== TYPE.ref, ...uleb(b), ...ht(h1), ...ht(h2)];
|
|
1505
1557
|
},
|
|
1506
1558
|
v128const: (n) => {
|
|
1507
1559
|
let [t, num] = n.shift().split("x"), bits = +t.slice(1), stride = bits >>> 3;
|
|
@@ -1556,7 +1608,47 @@ var IMM = {
|
|
|
1556
1608
|
typeidx_typeidx: (n, c) => [...uleb(id(n.shift(), c.type)), ...uleb(id(n.shift(), c.type))],
|
|
1557
1609
|
dataidx_memoryidx: (n, c) => [...uleb(id(n.shift(), c.data)), ...uleb(id(n.shift(), c.memory))],
|
|
1558
1610
|
memoryidx_memoryidx: (n, c) => [...uleb(id(n.shift(), c.memory)), ...uleb(id(n.shift(), c.memory))],
|
|
1559
|
-
tableidx_tableidx: (n, c) => [...uleb(id(n.shift(), c.table)), ...uleb(id(n.shift(), c.table))]
|
|
1611
|
+
tableidx_tableidx: (n, c) => [...uleb(id(n.shift(), c.table)), ...uleb(id(n.shift(), c.table))],
|
|
1612
|
+
// stack switching handlers (Phase 3)
|
|
1613
|
+
cont_bind: (n, c) => [...uleb(id(n.shift(), c.type)), ...uleb(id(n.shift(), c.type))],
|
|
1614
|
+
switch_cont: (n, c) => [...uleb(id(n.shift(), c.type)), ...uleb(id(n.shift(), c.tag))],
|
|
1615
|
+
resume: (n, c) => {
|
|
1616
|
+
const typeidx = uleb(id(n.shift(), c.type));
|
|
1617
|
+
const handlers = [];
|
|
1618
|
+
let cnt = 0;
|
|
1619
|
+
while (n[0]?.[0] === "on") {
|
|
1620
|
+
const [, tag, label] = n.shift();
|
|
1621
|
+
if (label === "switch") handlers.push(1, ...uleb(id(tag, c.tag)));
|
|
1622
|
+
else handlers.push(0, ...uleb(id(tag, c.tag)), ...uleb(blockid(label, c.block)));
|
|
1623
|
+
cnt++;
|
|
1624
|
+
}
|
|
1625
|
+
return [...typeidx, ...uleb(cnt), ...handlers];
|
|
1626
|
+
},
|
|
1627
|
+
resume_throw: (n, c) => {
|
|
1628
|
+
const typeidx = uleb(id(n.shift(), c.type));
|
|
1629
|
+
const exnidx = uleb(id(n.shift(), c.tag));
|
|
1630
|
+
const handlers = [];
|
|
1631
|
+
let cnt = 0;
|
|
1632
|
+
while (n[0]?.[0] === "on") {
|
|
1633
|
+
const [, tag, label] = n.shift();
|
|
1634
|
+
if (label === "switch") handlers.push(1, ...uleb(id(tag, c.tag)));
|
|
1635
|
+
else handlers.push(0, ...uleb(id(tag, c.tag)), ...uleb(blockid(label, c.block)));
|
|
1636
|
+
cnt++;
|
|
1637
|
+
}
|
|
1638
|
+
return [...typeidx, ...exnidx, ...uleb(cnt), ...handlers];
|
|
1639
|
+
},
|
|
1640
|
+
resume_throw_ref: (n, c) => {
|
|
1641
|
+
const typeidx = uleb(id(n.shift(), c.type));
|
|
1642
|
+
const handlers = [];
|
|
1643
|
+
let cnt = 0;
|
|
1644
|
+
while (n[0]?.[0] === "on") {
|
|
1645
|
+
const [, tag, label] = n.shift();
|
|
1646
|
+
if (label === "switch") handlers.push(1, ...uleb(id(tag, c.tag)));
|
|
1647
|
+
else handlers.push(0, ...uleb(id(tag, c.tag)), ...uleb(blockid(label, c.block)));
|
|
1648
|
+
cnt++;
|
|
1649
|
+
}
|
|
1650
|
+
return [...typeidx, ...uleb(cnt), ...handlers];
|
|
1651
|
+
}
|
|
1560
1652
|
};
|
|
1561
1653
|
var HANDLER = {};
|
|
1562
1654
|
(function populate(items, pre) {
|
|
@@ -1616,17 +1708,36 @@ var align = (op) => {
|
|
|
1616
1708
|
let k = op[i] === "l" ? i + 4 : i + 5, m = op.slice(k).match(/(\d+)(x|_|$)/);
|
|
1617
1709
|
return Math.log2(m ? m[2] === "x" ? 8 : m[1] / 8 : +group / 8);
|
|
1618
1710
|
};
|
|
1711
|
+
var numdata = (item) => {
|
|
1712
|
+
if (!Array.isArray(item)) return null;
|
|
1713
|
+
const [t, ...vs] = item;
|
|
1714
|
+
if (t !== "i8" && t !== "i16" && t !== "i32" && t !== "i64" && t !== "f32" && t !== "f64") return null;
|
|
1715
|
+
const out = [], dv = new DataView(new ArrayBuffer(8));
|
|
1716
|
+
for (const v of vs) {
|
|
1717
|
+
if (t === "i8") out.push(i32.parse(v) & 255 + 256 & 255);
|
|
1718
|
+
else if (t === "i16") dv.setInt16(0, i32.parse(v), true), out.push(...new Uint8Array(dv.buffer, 0, 2));
|
|
1719
|
+
else if (t === "i32") dv.setInt32(0, i32.parse(v), true), out.push(...new Uint8Array(dv.buffer, 0, 4));
|
|
1720
|
+
else if (t === "i64") dv.setBigInt64(0, BigInt(v), true), out.push(...new Uint8Array(dv.buffer, 0, 8));
|
|
1721
|
+
else if (t === "f32") out.push(...f32(v));
|
|
1722
|
+
else if (t === "f64") out.push(...f64(v));
|
|
1723
|
+
}
|
|
1724
|
+
return out;
|
|
1725
|
+
};
|
|
1619
1726
|
var limits = (node) => {
|
|
1620
1727
|
const is64 = node[0] === "i64" && node.shift();
|
|
1621
1728
|
const shared = node[node.length - 1] === "shared" && node.pop();
|
|
1729
|
+
const psIdx = node.findIndex((n) => Array.isArray(n) && n[0] === "pagesize");
|
|
1730
|
+
let psLog2 = -1;
|
|
1731
|
+
if (psIdx >= 0) psLog2 = Math.log2(+node.splice(psIdx, 1)[0][1]);
|
|
1622
1732
|
const hasMax = !isNaN(parseInt(node[1]));
|
|
1623
|
-
const flag = (is64 ? 4 : 0) | (shared ? 2 : 0) | (hasMax ? 1 : 0);
|
|
1733
|
+
const flag = (psLog2 >= 0 ? 8 : 0) | (is64 ? 4 : 0) | (shared ? 2 : 0) | (hasMax ? 1 : 0);
|
|
1624
1734
|
const parse = is64 ? (v) => {
|
|
1625
1735
|
if (typeof v === "bigint") return v;
|
|
1626
1736
|
const str2 = typeof v === "string" ? v.replaceAll("_", "") : String(v);
|
|
1627
1737
|
return BigInt(str2);
|
|
1628
1738
|
} : parseUint;
|
|
1629
|
-
|
|
1739
|
+
const ps = psLog2 >= 0 ? uleb(psLog2) : [];
|
|
1740
|
+
return hasMax ? [flag, ...uleb(parse(node.shift())), ...uleb(parse(node.shift())), ...ps] : [flag, ...uleb(parse(node.shift())), ...ps];
|
|
1630
1741
|
};
|
|
1631
1742
|
var parseUint = (v, max = 4294967295) => {
|
|
1632
1743
|
const n = typeof v === "string" && v[0] !== "+" ? i32.parse(v) : typeof v === "number" ? v : err(`Bad int ${v}`);
|
package/dist/watr.min.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
var Gt=Object.defineProperty;var Kt=(t,e)=>{for(var r in e)Gt(t,r,{get:e[r],enumerable:!0})};var
|
|
2
|
-
`?(r++,i=1):i++;t+=` at ${r}:${i}`}throw Error(t)};var kt=/^_|_$|[^\da-f]_|_[^\da-f]/i,Bt=/^[+-]?(?:0x[\da-f]+|\d+)$/i,Xt=new TextEncoder,Jt=new TextDecoder("utf-8",{fatal:!0,ignoreBOM:!0}),Nt={n:10,r:13,t:9,'"':34,"'":39,"\\":92},lt=t=>{let e=[],r=1,i,s,l="",n=()=>(l&&e.push(...Xt.encode(l)),l="");for(;r<t.length-1;)s=t[r++],i=null,s==="\\"&&(t[r]==="u"?(r++,r++,s=String.fromCodePoint(parseInt(t.slice(r,r=t.indexOf("}",r)),16)),r++):Nt[t[r]]?i=Nt[t[r++]]:isNaN(i=parseInt(t[r]+t[r+1],16))?s+=t[r]:(r++,r++)),i!=null?(n(),e.push(i)):l+=s;return n(),e.valueOf=()=>t,e},Mt=t=>Jt.decode(new Uint8Array(lt(t)));var y=(t,e=[])=>{if(t==null)return e;if(typeof t=="string"&&(t=/[_x]/i.test(t)?BigInt(t.replaceAll("_","")):D.parse(t)),typeof t=="bigint"){for(;;){let i=Number(t&0x7Fn);if(t>>=7n,t===0n){e.push(i);break}e.push(i|128)}return e}let r=t&127;return t>>>=7,t===0?(e.push(r),e):(e.push(r|128),y(t,e))};function Zt(t){let e=[];for(let r=0;r<5;r++){let i=t&127;t>>>=7,r<4&&(i|=128),e.push(i)}return e}function D(t,e=[]){for(typeof t=="string"&&(t=D.parse(t));;){let r=Number(t&127);if(t>>=7,t===0&&!(r&64)||t===-1&&r&64){e.push(r);break}e.push(r|128)}return e}var zt=t=>!kt.test(t)&&Bt.test(t=t.replaceAll("_",""))?t:$(`Bad int ${t}`),Qt=D,te=D;D.parse=t=>(t=parseInt(zt(t)),(t<-2147483648||t>4294967295)&&$("i32 constant out of range"),t);function K(t,e=[]){for(typeof t=="string"?t=K.parse(t):typeof t=="bigint"&&t>0x7fffffffffffffffn&&(t=t-0x10000000000000000n);;){let r=Number(t&0x7Fn);if(t>>=7n,t===0n&&!(r&64)||t===-1n&&r&64){e.push(r);break}e.push(r|128)}return e}K.parse=t=>(t=zt(t),t=t[0]==="-"?-BigInt(t.slice(1)):BigInt(t),(t<-0x8000000000000000n||t>0xffffffffffffffffn)&&$("i64 constant out of range"),E.setBigInt64(0,t),E.getBigInt64(0));var E=new DataView(new Float64Array(1).buffer),ee=2147483648,re=2139095040;function et(t,e,r){return typeof t=="string"&&~(r=t.indexOf("nan:"))?(e=D.parse(t.slice(r+4)),e|=re,t[0]==="-"&&(e|=ee),E.setInt32(0,e)):(e=typeof t=="string"?et.parse(t):t,E.setFloat32(0,e)),[E.getUint8(3),E.getUint8(2),E.getUint8(1),E.getUint8(0)]}var ie=0x8000000000000000n,se=0x7ff0000000000000n;function X(t,e,r){return typeof t=="string"&&~(r=t.indexOf("nan:"))?(e=K.parse(t.slice(r+4)),e|=se,t[0]==="-"&&(e|=ie),E.setBigInt64(0,e)):(e=typeof t=="string"?X.parse(t):t,E.setFloat64(0,e)),[E.getUint8(7),E.getUint8(6),E.getUint8(5),E.getUint8(4),E.getUint8(3),E.getUint8(2),E.getUint8(1),E.getUint8(0)]}X.parse=(t,e=Number.MAX_VALUE)=>{t=t.replaceAll("_","");let r=1;if(t[0]==="-"?(r=-1,t=t.slice(1)):t[0]==="+"&&(t=t.slice(1)),t[1]==="x"){let[i,s="0"]=t.split(/p/i),[l,n=""]=i.split("."),a=n.length??0,c=parseInt(l);isNaN(c)&&$();let f=n?parseInt("0x"+n)/16**a:0;s=parseInt(s,10);let o=r*(c+f)*2**s;return o=Math.max(-e,Math.min(e,o)),o}return t.includes("nan")?r<0?NaN:NaN:t.includes("inf")?r*(1/0):r*parseFloat(t)};et.parse=t=>X.parse(t,34028234663852886e22);var rt=["unreachable","nop","block block","loop block","if block","else null","then null",,"throw tagidx",,"throw_ref","end end","br labelidx","br_if labelidx","br_table br_table","return","call funcidx","call_indirect call_indirect","return_call funcidx","return_call_indirect call_indirect","call_ref typeidx","return_call_ref typeidx",,,,,"drop","select select","",,,"try_table try_table","local.get localidx","local.set localidx","local.tee localidx","global.get globalidx","global.set globalidx","table.get tableidx","table.set tableidx",,"i32.load memarg","i64.load memarg","f32.load memarg","f64.load memarg","i32.load8_s memarg","i32.load8_u memarg","i32.load16_s memarg","i32.load16_u memarg","i64.load8_s memarg","i64.load8_u memarg","i64.load16_s memarg","i64.load16_u memarg","i64.load32_s memarg","i64.load32_u memarg","i32.store memarg","i64.store memarg","f32.store memarg","f64.store memarg","i32.store8 memarg","i32.store16 memarg","i64.store8 memarg","i64.store16 memarg","i64.store32 memarg","memory.size opt_memory","memory.grow opt_memory","i32.const i32","i64.const i64","f32.const f32","f64.const f64","i32.eqz","i32.eq","i32.ne","i32.lt_s","i32.lt_u","i32.gt_s","i32.gt_u","i32.le_s","i32.le_u","i32.ge_s","i32.ge_u","i64.eqz","i64.eq","i64.ne","i64.lt_s","i64.lt_u","i64.gt_s","i64.gt_u","i64.le_s","i64.le_u","i64.ge_s","i64.ge_u","f32.eq","f32.ne","f32.lt","f32.gt","f32.le","f32.ge","f64.eq","f64.ne","f64.lt","f64.gt","f64.le","f64.ge","i32.clz","i32.ctz","i32.popcnt","i32.add","i32.sub","i32.mul","i32.div_s","i32.div_u","i32.rem_s","i32.rem_u","i32.and","i32.or","i32.xor","i32.shl","i32.shr_s","i32.shr_u","i32.rotl","i32.rotr","i64.clz","i64.ctz","i64.popcnt","i64.add","i64.sub","i64.mul","i64.div_s","i64.div_u","i64.rem_s","i64.rem_u","i64.and","i64.or","i64.xor","i64.shl","i64.shr_s","i64.shr_u","i64.rotl","i64.rotr","f32.abs","f32.neg","f32.ceil","f32.floor","f32.trunc","f32.nearest","f32.sqrt","f32.add","f32.sub","f32.mul","f32.div","f32.min","f32.max","f32.copysign","f64.abs","f64.neg","f64.ceil","f64.floor","f64.trunc","f64.nearest","f64.sqrt","f64.add","f64.sub","f64.mul","f64.div","f64.min","f64.max","f64.copysign","i32.wrap_i64","i32.trunc_f32_s","i32.trunc_f32_u","i32.trunc_f64_s","i32.trunc_f64_u","i64.extend_i32_s","i64.extend_i32_u","i64.trunc_f32_s","i64.trunc_f32_u","i64.trunc_f64_s","i64.trunc_f64_u","f32.convert_i32_s","f32.convert_i32_u","f32.convert_i64_s","f32.convert_i64_u","f32.demote_f64","f64.convert_i32_s","f64.convert_i32_u","f64.convert_i64_s","f64.convert_i64_u","f64.promote_f32","i32.reinterpret_f32","i64.reinterpret_f64","f32.reinterpret_i32","f64.reinterpret_i64","i32.extend8_s","i32.extend16_s","i64.extend8_s","i64.extend16_s","i64.extend32_s",,,,,,,,,,,,"ref.null ref_null","ref.is_null","ref.func funcidx","ref.eq","ref.as_non_null","br_on_null labelidx","br_on_non_null labelidx",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,["struct.new typeidx","struct.new_default typeidx","struct.get typeidx_field","struct.get_s typeidx_field","struct.get_u typeidx_field","struct.set typeidx_field","array.new typeidx","array.new_default typeidx","array.new_fixed typeidx_multi","array.new_data typeidx_dataidx","array.new_elem typeidx_elemidx","array.get typeidx","array.get_s typeidx","array.get_u typeidx","array.set typeidx","array.len","array.fill typeidx","array.copy typeidx_typeidx","array.init_data typeidx_dataidx","array.init_elem typeidx_elemidx","ref.test reftype","","ref.cast reftype","","br_on_cast reftype2","br_on_cast_fail reftype2","any.convert_extern","extern.convert_any","ref.i31","i31.get_s","i31.get_u"],["i32.trunc_sat_f32_s","i32.trunc_sat_f32_u","i32.trunc_sat_f64_s","i32.trunc_sat_f64_u","i64.trunc_sat_f32_s","i64.trunc_sat_f32_u","i64.trunc_sat_f64_s","i64.trunc_sat_f64_u","memory.init dataidx_memoryidx","data.drop dataidx","memory.copy memoryidx_memoryidx","memory.fill memoryidx?","table.init reversed","elem.drop elemidx","table.copy tableidx_tableidx","table.grow tableidx","table.size tableidx","table.fill tableidx",,"i64.add128","i64.sub128","i64.mul_wide_s","i64.mul_wide_u"],["v128.load memarg","v128.load8x8_s memarg","v128.load8x8_u memarg","v128.load16x4_s memarg","v128.load16x4_u memarg","v128.load32x2_s memarg","v128.load32x2_u memarg","v128.load8_splat memarg","v128.load16_splat memarg","v128.load32_splat memarg","v128.load64_splat memarg","v128.store memarg","v128.const v128const","i8x16.shuffle shuffle","i8x16.swizzle","i8x16.splat","i16x8.splat","i32x4.splat","i64x2.splat","f32x4.splat","f64x2.splat","i8x16.extract_lane_s laneidx","i8x16.extract_lane_u laneidx","i8x16.replace_lane laneidx","i16x8.extract_lane_s laneidx","i16x8.extract_lane_u laneidx","i16x8.replace_lane laneidx","i32x4.extract_lane laneidx","i32x4.replace_lane laneidx","i64x2.extract_lane laneidx","i64x2.replace_lane laneidx","f32x4.extract_lane laneidx","f32x4.replace_lane laneidx","f64x2.extract_lane laneidx","f64x2.replace_lane laneidx","i8x16.eq","i8x16.ne","i8x16.lt_s","i8x16.lt_u","i8x16.gt_s","i8x16.gt_u","i8x16.le_s","i8x16.le_u","i8x16.ge_s","i8x16.ge_u","i16x8.eq","i16x8.ne","i16x8.lt_s","i16x8.lt_u","i16x8.gt_s","i16x8.gt_u","i16x8.le_s","i16x8.le_u","i16x8.ge_s","i16x8.ge_u","i32x4.eq","i32x4.ne","i32x4.lt_s","i32x4.lt_u","i32x4.gt_s","i32x4.gt_u","i32x4.le_s","i32x4.le_u","i32x4.ge_s","i32x4.ge_u","f32x4.eq","f32x4.ne","f32x4.lt","f32x4.gt","f32x4.le","f32x4.ge","f64x2.eq","f64x2.ne","f64x2.lt","f64x2.gt","f64x2.le","f64x2.ge","v128.not","v128.and","v128.andnot","v128.or","v128.xor","v128.bitselect","v128.any_true","v128.load8_lane memlane","v128.load16_lane memlane","v128.load32_lane memlane","v128.load64_lane memlane","v128.store8_lane memlane","v128.store16_lane memlane","v128.store32_lane memlane","v128.store64_lane memlane","v128.load32_zero memarg","v128.load64_zero memarg","f32x4.demote_f64x2_zero","f64x2.promote_low_f32x4","i8x16.abs","i8x16.neg","i8x16.popcnt","i8x16.all_true","i8x16.bitmask","i8x16.narrow_i16x8_s","i8x16.narrow_i16x8_u","f32x4.ceil","f32x4.floor","f32x4.trunc","f32x4.nearest","i8x16.shl","i8x16.shr_s","i8x16.shr_u","i8x16.add","i8x16.add_sat_s","i8x16.add_sat_u","i8x16.sub","i8x16.sub_sat_s","i8x16.sub_sat_u","f64x2.ceil","f64x2.floor","i8x16.min_s","i8x16.min_u","i8x16.max_s","i8x16.max_u","f64x2.trunc","i8x16.avgr_u","i16x8.extadd_pairwise_i8x16_s","i16x8.extadd_pairwise_i8x16_u","i32x4.extadd_pairwise_i16x8_s","i32x4.extadd_pairwise_i16x8_u","i16x8.abs","i16x8.neg","i16x8.q15mulr_sat_s","i16x8.all_true","i16x8.bitmask","i16x8.narrow_i32x4_s","i16x8.narrow_i32x4_u","i16x8.extend_low_i8x16_s","i16x8.extend_high_i8x16_s","i16x8.extend_low_i8x16_u","i16x8.extend_high_i8x16_u","i16x8.shl","i16x8.shr_s","i16x8.shr_u","i16x8.add","i16x8.add_sat_s","i16x8.add_sat_u","i16x8.sub","i16x8.sub_sat_s","i16x8.sub_sat_u","f64x2.nearest","i16x8.mul","i16x8.min_s","i16x8.min_u","i16x8.max_s","i16x8.max_u",,"i16x8.avgr_u","i16x8.extmul_low_i8x16_s","i16x8.extmul_high_i8x16_s","i16x8.extmul_low_i8x16_u","i16x8.extmul_high_i8x16_u","i32x4.abs","i32x4.neg",,"i32x4.all_true","i32x4.bitmask",,,"i32x4.extend_low_i16x8_s","i32x4.extend_high_i16x8_s","i32x4.extend_low_i16x8_u","i32x4.extend_high_i16x8_u","i32x4.shl","i32x4.shr_s","i32x4.shr_u","i32x4.add",,,"i32x4.sub",,,,"i32x4.mul","i32x4.min_s","i32x4.min_u","i32x4.max_s","i32x4.max_u","i32x4.dot_i16x8_s",,"i32x4.extmul_low_i16x8_s","i32x4.extmul_high_i16x8_s","i32x4.extmul_low_i16x8_u","i32x4.extmul_high_i16x8_u","i64x2.abs","i64x2.neg",,"i64x2.all_true","i64x2.bitmask",,,"i64x2.extend_low_i32x4_s","i64x2.extend_high_i32x4_s","i64x2.extend_low_i32x4_u","i64x2.extend_high_i32x4_u","i64x2.shl","i64x2.shr_s","i64x2.shr_u","i64x2.add",,,"i64x2.sub",,,,"i64x2.mul","i64x2.eq","i64x2.ne","i64x2.lt_s","i64x2.gt_s","i64x2.le_s","i64x2.ge_s","i64x2.extmul_low_i32x4_s","i64x2.extmul_high_i32x4_s","i64x2.extmul_low_i32x4_u","i64x2.extmul_high_i32x4_u","f32x4.abs","f32x4.neg",,"f32x4.sqrt","f32x4.add","f32x4.sub","f32x4.mul","f32x4.div","f32x4.min","f32x4.max","f32x4.pmin","f32x4.pmax","f64x2.abs","f64x2.neg",,"f64x2.sqrt","f64x2.add","f64x2.sub","f64x2.mul","f64x2.div","f64x2.min","f64x2.max","f64x2.pmin","f64x2.pmax","i32x4.trunc_sat_f32x4_s","i32x4.trunc_sat_f32x4_u","f32x4.convert_i32x4_s","f32x4.convert_i32x4_u","i32x4.trunc_sat_f64x2_s_zero","i32x4.trunc_sat_f64x2_u_zero","f64x2.convert_low_i32x4_s","f64x2.convert_low_i32x4_u","i8x16.relaxed_swizzle","i32x4.relaxed_trunc_f32x4_s","i32x4.relaxed_trunc_f32x4_u","i32x4.relaxed_trunc_f64x2_s_zero","i32x4.relaxed_trunc_f64x2_u_zero","f32x4.relaxed_madd","f32x4.relaxed_nmadd","f64x2.relaxed_madd","f64x2.relaxed_nmadd","i8x16.relaxed_laneselect","i16x8.relaxed_laneselect","i32x4.relaxed_laneselect","i64x2.relaxed_laneselect","f32x4.relaxed_min","f32x4.relaxed_max","f64x2.relaxed_min","f64x2.relaxed_max","i16x8.relaxed_q15mulr_s","i16x8.relaxed_dot_i8x16_i7x16_s","i32x4.relaxed_dot_i8x16_i7x16_add_s"],["memory.atomic.notify memarg","memory.atomic.wait32 memarg","memory.atomic.wait64 memarg","atomic.fence opt_memory",,,,,,,,,,,,,"i32.atomic.load memarg","i64.atomic.load memarg","i32.atomic.load8_u memarg","i32.atomic.load16_u memarg","i64.atomic.load8_u memarg","i64.atomic.load16_u memarg","i64.atomic.load32_u memarg","i32.atomic.store memarg","i64.atomic.store memarg","i32.atomic.store8 memarg","i32.atomic.store16 memarg","i64.atomic.store8 memarg","i64.atomic.store16 memarg","i64.atomic.store32 memarg","i32.atomic.rmw.add memarg","i64.atomic.rmw.add memarg","i32.atomic.rmw8.add_u memarg","i32.atomic.rmw16.add_u memarg","i64.atomic.rmw8.add_u memarg","i64.atomic.rmw16.add_u memarg","i64.atomic.rmw32.add_u memarg","i32.atomic.rmw.sub memarg","i64.atomic.rmw.sub memarg","i32.atomic.rmw8.sub_u memarg","i32.atomic.rmw16.sub_u memarg","i64.atomic.rmw8.sub_u memarg","i64.atomic.rmw16.sub_u memarg","i64.atomic.rmw32.sub_u memarg","i32.atomic.rmw.and memarg","i64.atomic.rmw.and memarg","i32.atomic.rmw8.and_u memarg","i32.atomic.rmw16.and_u memarg","i64.atomic.rmw8.and_u memarg","i64.atomic.rmw16.and_u memarg","i64.atomic.rmw32.and_u memarg","i32.atomic.rmw.or memarg","i64.atomic.rmw.or memarg","i32.atomic.rmw8.or_u memarg","i32.atomic.rmw16.or_u memarg","i64.atomic.rmw8.or_u memarg","i64.atomic.rmw16.or_u memarg","i64.atomic.rmw32.or_u memarg","i32.atomic.rmw.xor memarg","i64.atomic.rmw.xor memarg","i32.atomic.rmw8.xor_u memarg","i32.atomic.rmw16.xor_u memarg","i64.atomic.rmw8.xor_u memarg","i64.atomic.rmw16.xor_u memarg","i64.atomic.rmw32.xor_u memarg","i32.atomic.rmw.xchg memarg","i64.atomic.rmw.xchg memarg","i32.atomic.rmw8.xchg_u memarg","i32.atomic.rmw16.xchg_u memarg","i64.atomic.rmw8.xchg_u memarg","i64.atomic.rmw16.xchg_u memarg","i64.atomic.rmw32.xchg_u memarg","i32.atomic.rmw.cmpxchg memarg","i64.atomic.rmw.cmpxchg memarg","i32.atomic.rmw8.cmpxchg_u memarg","i32.atomic.rmw16.cmpxchg_u memarg","i64.atomic.rmw8.cmpxchg_u memarg","i64.atomic.rmw16.cmpxchg_u memarg","i64.atomic.rmw32.cmpxchg_u memarg"]],B={custom:0,type:1,import:2,func:3,table:4,memory:5,tag:13,global:6,export:7,start:8,elem:9,datacount:12,code:10,data:11},q={i8:120,i16:119,i32:127,i64:126,f32:125,f64:124,void:64,v128:123,exn:105,noexn:116,nofunc:115,noextern:114,none:113,func:112,extern:111,any:110,eq:109,i31:108,struct:107,array:106,nullfuncref:115,nullexternref:114,nullexnref:116,nullref:113,funcref:112,externref:111,exnref:105,anyref:110,eqref:109,i31ref:108,structref:107,arrayref:106,ref:100,refnull:99,sub:80,subfinal:79,rec:78},Ut={func:96,struct:95,array:94,sub:80,subfinal:79,rec:78},xt={func:0,table:1,memory:2,global:3,tag:4};var C=t=>{let e=0,r=[],i="",s=0,l=0,n=()=>i&&(r.push(i),i=""),a=c=>{r.loc=c;for(let f,o,u;e<t.length;)if(f=t.charCodeAt(e),s===34)i+=t[e++],f===92?i+=t[e++]:f===34&&(n(),s=0);else if(s>59)f===40&&t.charCodeAt(e+1)===59?(s++,i+=t[e++]+t[e++]):f===59&&t.charCodeAt(e+1)===41?(i+=t[e++]+t[e++],--s===59&&(n(),s=0)):i+=t[e++];else if(s<0)f===10||f===13?(i+=t[e++],n(),s=0):i+=t[e++];else if(f===34)i!=="$"&&n(),s=34,i+=t[e++];else if(f===40&&t.charCodeAt(e+1)===59)n(),s=60,i=t[e++]+t[e++];else if(f===59&&t.charCodeAt(e+1)===59)n(),s=-1,i=t[e++]+t[e++];else if(f===40&&t.charCodeAt(e+1)===64)n(),u=e,e+=2,i="@",l++,(o=r).push(r=[]),a(u),r=o;else if(f===40)n(),u=e++,l++,(o=r).push(r=[]),a(u),r=o;else{if(f===41)return n(),e++,l--;f<=32?(n(),e++):i+=t[e++]}s<0&&n(),n()};return a(0),s===34&&$("Unclosed quote",e),s>59&&$("Unclosed block comment",e),l>0&&$("Unclosed parenthesis",e),e<t.length&&$("Unexpected closing parenthesis",e),r.length>1?r:r[0]||[]};var qt=(t,e)=>Array.isArray(t)?t[0]?.[0]==="@"&&t[0]!=="@custom"&&!t[0]?.startsWith?.("@metadata.code.")?null:(e=t.map(qt).filter(r=>r!=null),e.loc=t.loc,e.length===1&&e[0]?.[0]==="module"?e[0]:e):typeof t!="string"?t:t[0]===";"||t[1]===";"?null:t[0]==="$"&&t[1]==='"'?t.includes("\\")?"$"+Mt(t.slice(1)):"$"+t.slice(2,-1):t[0]==='"'?lt(t):t;function Q(t){typeof t=="string"?($.src=t,t=C(t)||[]):$.src="",$.loc=0,t=qt(t)||[];let e=0;if(t[0]==="module"?(e++,O(t[e])&&e++):typeof t[0]=="string"&&(t=[t]),t[e]==="binary")return Uint8Array.from(t.slice(++e).flat());if(t[e]==="quote")return Q(t.slice(++e).map(l=>l.valueOf().slice(1,-1)).flat().join(""));let r=[];for(let l in B)(r[B[l]]=r[l]=[]).name=l;r.metadata={},t.slice(e).filter(l=>{if(!Array.isArray(l)){let c=$.loc,f=$.src,o;for(;(c=f.indexOf(l,c))>=0;){if(o=f.charCodeAt(c-1),c>0&&(o>47&&o<58||o>64&&o<91||o>96&&o<123||o===95||o===36)){c++;continue}if(o=f.charCodeAt(c+l.length),o>47&&o<58||o>64&&o<91||o>96&&o<123||o===95){c++;continue}break}c>=0&&($.loc=c),$(`Unexpected token ${l}`)}let[n,...a]=l;if($.loc=l.loc,n==="@custom")r.custom.push(a);else if(n==="rec")for(let c=0;c<a.length;c++){let[,...f]=a[c];ht(f,r.type),(f=Tt(f,r)).push(c?!0:[r.type.length,a.length]),r.type.push(f)}else if(n==="type")ht(a,r.type),r.type.push(Tt(a,r));else if(n==="start"||n==="export")r[n].push(a);else return!0}).forEach(l=>{let[n,...a]=l;$.loc=l.loc;let c;n==="import"&&([n,...a]=(c=a).pop());let f=r[n];for(f||$(`Unknown section ${n}`),ht(a,f);a[0]?.[0]==="export";)r.export.push([a.shift()[1],[n,f?.length]]);if(a[0]?.[0]==="import"&&([,...c]=a.shift()),n==="table"){let o=a[0]==="i64",u=o?1:0;if(a[u+1]?.[0]==="elem"){let[h,[,...x]]=[a[u],a[u+1]];a=o?["i64",x.length,x.length,h]:[x.length,x.length,h],r.elem.push([["table",f.length],["offset",[o?"i64.const":"i32.const",o?0n:0]],h,...x])}}else if(n==="memory"){let o=a[0]==="i64",u=o?1:0;if(a[u]?.[0]==="data"){let[,...h]=a.splice(u,1)[0],x=""+Math.ceil(h.reduce((I,p)=>I+p.length,0)/65536);r.data.push([["memory",f.length],[o?"i64.const":"i32.const",o?0n:0],...h]),a=o?["i64",x,x]:[x,x]}}else if(n==="func"){let[o,u,h]=_t(a,r);o??=ut(u,h,r),!c&&r.code.push([[o,u,h],...V(a,r)]),a=[["type",o]]}else if(n==="tag"){let[o,u]=_t(a,r);o??=ut(u,[],r),a=[["type",o]]}c&&(r.import.push([...c,[n,...a]]),a=null),f.push(a)});let i=(l,n=!0)=>{let a=r[l].filter(Boolean).map(c=>At[l](c,r)).filter(Boolean);return l===B.custom?a.flatMap(c=>[l,...k(c)]):a.length?[l,...k(n?k(a):a)]:[]},s=()=>{let l=[];for(let n in r.metadata){let a=k(lt(`"metadata.code.${n}"`)),c=k(r.metadata[n].map(([f,o])=>[...y(f),...k(o.map(([u,h])=>[...y(u),...k(h)]))]));l.push(0,...k([...a,...c]))}return l};return Uint8Array.from([0,97,115,109,1,0,0,0,...i(B.custom),...i(B.type),...i(B.import),...i(B.func),...i(B.table),...i(B.memory),...i(B.tag),...i(B.global),...i(B.export),...i(B.start,!1),...i(B.elem),...i(B.datacount,!1),...i(B.code),...s(),...i(B.data)])}var L=t=>t?.[0]==="$"||!isNaN(t),O=t=>t?.[0]==="$",dt=t=>t?.[0]==="a"||t?.[0]==="o";function V(t,e){let r=[];for(t=[...t];t.length;){let i=t.shift();if(typeof i=="string")if(r.push(i),i==="block"||i==="if"||i==="loop")O(t[0])&&r.push(t.shift()),r.push(at(t,e));else if(i==="else"||i==="end")O(t[0])&&t.shift();else if(i==="select")r.push(mt(t)[1]);else if(i.endsWith("call_indirect")){let s=L(t[0])?t.shift():0,[l,n,a]=_t(t,e);r.push(s,["type",l??ut(n,a,e)])}else i==="table.init"?r.push(L(t[1])?t.shift():0,t.shift()):i==="table.copy"||i==="memory.copy"?r.push(L(t[0])?t.shift():0,L(t[0])?t.shift():0):i.startsWith("table.")?r.push(L(t[0])?t.shift():0):i==="memory.init"?(r.push(...L(t[1])?[t.shift(),t.shift()].reverse():[t.shift(),0]),e.datacount&&(e.datacount[0]=!0)):i==="data.drop"||i==="array.new_data"||i==="array.init_data"?(i==="data.drop"&&r.push(t.shift()),e.datacount&&(e.datacount[0]=!0)):(i.startsWith("memory.")||i.endsWith("load")||i.endsWith("store"))&&L(t[0])&&r.push(t.shift());else if(Array.isArray(i)){let s=i[0];if(i.loc!=null&&($.loc=i.loc),s?.startsWith?.("@metadata.code.")){let n=s.slice(15);r.push(["@metadata",n,i[1]]);continue}if(typeof s!="string"||!Array.isArray(rt[s])){r.push(i);continue}let l=i.slice(1);if(s==="block"||s==="loop")r.push(s),O(l[0])&&r.push(l.shift()),r.push(at(l,e),...V(l,e),"end");else if(s==="if"){let n=[],a=[];l.at(-1)?.[0]==="else"&&(a=V(l.pop().slice(1),e)),l.at(-1)?.[0]==="then"&&(n=V(l.pop().slice(1),e));let c=[s];O(l[0])&&c.push(l.shift()),c.push(at(l,e)),r.push(...V(l,e),...c,...n),a.length&&r.push("else",...a),r.push("end")}else if(s==="try_table"){for(r.push(s),O(l[0])&&r.push(l.shift()),r.push(at(l,e));l[0]?.[0]==="catch"||l[0]?.[0]==="catch_ref"||l[0]?.[0]==="catch_all"||l[0]?.[0]==="catch_all_ref";)r.push(l.shift());r.push(...V(l,e),"end")}else{let n=[];for(;l.length&&(!Array.isArray(l[0])||"type,param,result,ref".includes(l[0][0]));)n.push(l.shift());r.push(...V(l,e),s,...n),t.unshift(...r.splice(r.length-1-n.length))}}else r.push(i)}return r}var ut=(t,e,r,i="$"+t+">"+e)=>(r.type[i]??=r.type.push(["func",[t,e]])-1,i),bt=(t,e)=>{let r=[];for(;t[0]?.[0]===e;){let[,...i]=t.shift(),s=O(i[0])&&i.shift();s&&(s in r?(()=>{throw Error(`Duplicate ${e} ${s}`)})():r[s]=r.length),r.push(...i)}return r},mt=t=>{let e=bt(t,"param"),r=bt(t,"result");if(t[0]?.[0]==="param")throw Error("Unexpected param");return[e,r]},_t=(t,e)=>{if(t[0]?.[0]!=="type")return[,...mt(t)];let[,r]=t.shift(),[i,s]=mt(t),l=e.type[typeof r=="string"&&isNaN(r)?e.type[r]:+r];if(!l)throw Error(`Unknown type ${r}`);if((i.length||s.length)&&l[1].join(">")!==i+">"+s)throw Error(`Type ${r} mismatch`);return[r,...l[1]]},at=(t,e)=>{let[r,i,s]=_t(t,e);if(!(!i.length&&!s.length))return!i.length&&s.length===1?["result",...s]:["type",r??ut(i,s,e)]},ht=(t,e)=>{let r=O(t[0])&&t.shift();return r&&(r in e?$(`Duplicate ${e.name} ${r}`):e[r]=e.length),r},Tt=([t],e)=>{let r="subfinal",i=[],s;return t[0]==="sub"&&(r=t.shift(),t[0]==="final"&&(r+=t.shift()),t=(i=t).pop()),[s,...t]=t,s==="func"?(t=mt(t),e.type["$"+t.join(">")]??=e.type.length):s==="struct"?t=bt(t,"field"):s==="array"&&([t]=t),[s,t,r,i]},At=[([t,...e],r)=>{let i=e;return(e[0]?.[0]==="before"||e[0]?.[0]==="after")&&(i=e.slice(1)),[...k(t),...i.flat()]},([t,e,r,i,s],l)=>{if(s===!0)return;let n;if(s){t="rec";let[a,c]=s,f=Array.from({length:c},(o,u)=>At[B.type](l.type[a+u].slice(0,4),l));n=k(f)}else r==="sub"||i?.length?(n=[...k(i.map(a=>A(a,l.type))),...At[B.type]([t,e],l)],t=r):t==="func"?n=[...k(e[0].map(a=>W(a,l))),...k(e[1].map(a=>W(a,l)))]:t==="array"?n=nt(e,l):t==="struct"&&(n=k(e.map(a=>nt(a,l))));return[Ut[t],...n]},([t,e,[r,...i]],s)=>{let l;if(r==="func"){let[[,n]]=i;l=y(A(n,s.type))}else if(r==="tag"){let[[,n]]=i;l=[0,...y(A(n,s.type))]}else r==="memory"?l=ft(i):r==="global"?l=nt(i[0],s):r==="table"?l=[...W(i.pop(),s),...ft(i)]:$(`Unknown kind ${r}`);return[...k(t),...k(e),xt[r],...l]},([[,t]],e)=>y(A(t,e.type)),(t,e)=>{let r=ft(t),i=W(t.shift(),e),[s]=t;return s?[64,0,...i,...r,...Z(s,e)]:[...i,...r]},(t,e)=>ft(t),([t,e],r)=>[...nt(t,r),...Z(e,r)],([t,[e,r]],i)=>[...k(t),xt[e],...y(A(r,i[e]))],([t],e)=>y(A(t,e.func)),(t,e)=>{let r=0,i=0,s=0,l=0,n,a,c;t[0]==="declare"&&(t.shift(),i=1),t[0]?.[0]==="table"?([,n]=t.shift(),n=A(n,e.table)):(typeof t[0]=="string"||typeof t[0]=="number")&&(t[1]?.[0]==="offset"||Array.isArray(t[1])&&t[1][0]!=="item"&&!t[1][0]?.startsWith("ref"))&&(n=A(t.shift(),e.table)),t[0]?.[0]==="offset"||Array.isArray(t[0])&&t[0][0]!=="item"&&!t[0][0].startsWith("ref")?(a=t.shift(),a[0]==="offset"&&([,a]=a),a=Z(a,e)):i||(r=1),q[t[0]]||t[0]?.[0]==="ref"?c=W(t.shift(),e):t[0]==="func"?c=[q[t.shift()]]:c=[q.func],t=t.map(o=>(o[0]==="item"&&(o=o.length===3&&o[1]==="ref.func"?o[2]:o[1]),o[0]==="ref.func"&&([,o]=o),typeof o!="string"&&(s=1),o)),c[0]!==q.funcref&&(l=1,s=1);let f=s<<2|(r||i?i:!!n||l)<<1|(r||i);return[f,...f===0?a:f===1?[0]:f===2?[...y(n||0),...a,0]:f===3?[0]:f===4?a:f===5?c:f===6?[...y(n||0),...a,...c]:c,...k(t.map(s?o=>Z(typeof o=="string"?["ref.func",o]:o,e):o=>y(A(o,e.func))))]},(t,e)=>{let[r,i]=t.shift();i||([,[i]]=e.type[A(r,e.type)]),e.local=Object.create(i),e.block=[],e.local.name="local",e.block.name="block",e._codeIdx===void 0&&(e._codeIdx=0);let s=e._codeIdx++;for(;t[0]?.[0]==="local";){let[,...c]=t.shift();if(O(c[0])){let f=c.shift();f in e.local?$(`Duplicate local ${f}`):e.local[f]=e.local.length}e.local.push(...c)}e.meta={};let l=Ot(t,e),n=e.import.filter(c=>c[2][0]==="func").length+s;for(let c in e.meta)((e.metadata??={})[c]??=[]).push([n,e.meta[c]]);let a=e.local.slice(i.length).reduce((c,f)=>(f==c[c.length-1]?.[1]?c[c.length-1][0]++:c.push([1,f]),c),[]);return e.local=e.block=e.meta=null,k([...k(a.map(([c,f])=>[...y(c),...W(f,e)])),...l])},(t,e)=>{let r,i=0;return t[0]?.[0]==="memory"?([,i]=t.shift(),i=A(i,e.memory)):(typeof t[0]=="string"||typeof t[0]=="number")&&(t[1]?.[0]==="offset"||Array.isArray(t[1])&&typeof t[1][0]=="string")&&(i=A(t.shift(),e.memory)),Array.isArray(t[0])&&typeof t[0]?.[0]=="string"&&(r=t.shift(),r[0]==="offset"&&([,r]=r),r??$("Bad offset",r)),[...i?[2,...y(i),...Z(r,e)]:r?[0,...Z(r,e)]:[1],...k(t.flat())]},(t,e)=>y(e.data.length),([[,t]],e)=>[0,...y(A(t,e.type))]],W=(t,e)=>t[0]==="ref"?t[1]=="null"?q[t[2]]?[q[t[2]]]:[q.refnull,...y(A(t[t.length-1],e.type))]:[q.ref,...y(q[t[t.length-1]]||A(t[t.length-1],e.type))]:[q[t]??$(`Unknown type ${t}`)],nt=(t,e,r=t[0]==="mut"?1:0)=>[...W(r?t[1]:t,e),r],St={null:()=>[],reversed:(t,e)=>{let r=t.shift(),i=t.shift();return[...y(A(i,e.elem)),...y(A(r,e.table))]},block:(t,e)=>{e.block.push(1),O(t[0])&&(e.block[t.shift()]=e.block.length);let r=t.shift();return r?r[0]==="result"?W(r[1],e):y(A(r[1],e.type)):[q.void]},try_table:(t,e)=>{O(t[0])&&(e.block[t.shift()]=e.block.length+1);let r=t.shift(),i=r?r[0]==="result"?W(r[1],e):y(A(r[1],e.type)):[q.void],s=[],l=0;for(;t[0]?.[0]==="catch"||t[0]?.[0]==="catch_ref"||t[0]?.[0]==="catch_all"||t[0]?.[0]==="catch_all_ref";){let n=t.shift(),a=n[0]==="catch"?0:n[0]==="catch_ref"?1:n[0]==="catch_all"?2:3;a<=1?s.push(a,...y(A(n[1],e.tag)),...y(it(n[2],e.block))):s.push(a,...y(it(n[1],e.block))),l++}return e.block.push(1),[...i,...y(l),...s]},end:(t,e)=>(e.block.pop(),[]),call_indirect:(t,e)=>{let r=t.shift(),[,i]=t.shift();return[...y(A(i,e.type)),...y(A(r,e.table))]},br_table:(t,e)=>{let r=[],i=0;for(;t[0]&&(!isNaN(t[0])||O(t[0]));)r.push(...y(it(t.shift(),e.block))),i++;return[...y(i-1),...r]},select:(t,e)=>{let r=t.shift()||[];return r.length?k(r.map(i=>W(i,e))):[]},ref_null:(t,e)=>{let r=t.shift();return q[r]?[q[r]]:y(A(r,e.type))},memarg:(t,e,r)=>Et(t,r,L(t[0])&&!dt(t[0])?A(t.shift(),e.memory):0),opt_memory:(t,e)=>y(A(L(t[0])?t.shift():0,e.memory)),reftype:(t,e)=>{let r=W(t.shift(),e);return r.length>1?r.slice(1):r},reftype2:(t,e)=>{let r=it(t.shift(),e.block),i=W(t.shift(),e),s=W(t.shift(),e);return[(s[0]!==q.ref)<<1|i[0]!==q.ref,...y(r),i.pop(),s.pop()]},v128const:t=>{let[e,r]=t.shift().split("x"),i=+e.slice(1),s=i>>>3;if(r=+r,e[0]==="i"){let n=r===16?new Uint8Array(16):r===8?new Uint16Array(8):r===4?new Uint32Array(4):new BigUint64Array(2);for(let a=0;a<r;a++)n[a]=J[e].parse(t.shift());return[...new Uint8Array(n.buffer)]}let l=new Uint8Array(16);for(let n=0;n<r;n++)l.set(J[e](t.shift()),n*s);return[...l]},shuffle:t=>{let e=[];for(let r=0;r<16;r++)e.push(ct(t.shift(),32));return typeof t[0]=="string"&&!isNaN(t[0])&&$("invalid lane length"),e},memlane:(t,e,r)=>{let i=O(t[0])||L(t[0])&&(dt(t[1])||L(t[1]))?A(t.shift(),e.memory):0;return[...Et(t,r,i),...y(ct(t.shift()))]},"*":t=>y(t.shift()),labelidx:(t,e)=>y(it(t.shift(),e.block)),laneidx:t=>[ct(t.shift(),255)],funcidx:(t,e)=>y(A(t.shift(),e.func)),typeidx:(t,e)=>y(A(t.shift(),e.type)),tableidx:(t,e)=>y(A(t.shift(),e.table)),memoryidx:(t,e)=>y(A(t.shift(),e.memory)),globalidx:(t,e)=>y(A(t.shift(),e.global)),localidx:(t,e)=>y(A(t.shift(),e.local)),dataidx:(t,e)=>y(A(t.shift(),e.data)),elemidx:(t,e)=>y(A(t.shift(),e.elem)),tagidx:(t,e)=>y(A(t.shift(),e.tag)),"memoryidx?":(t,e)=>y(A(L(t[0])?t.shift():0,e.memory)),i32:t=>D(t.shift()),i64:t=>K(t.shift()),f32:t=>et(t.shift()),f64:t=>X(t.shift()),v128:t=>(void 0)(t.shift()),typeidx_field:(t,e)=>{let r=A(t.shift(),e.type);return[...y(r),...y(A(t.shift(),e.type[r][1]))]},typeidx_multi:(t,e)=>[...y(A(t.shift(),e.type)),...y(t.shift())],typeidx_dataidx:(t,e)=>[...y(A(t.shift(),e.type)),...y(A(t.shift(),e.data))],typeidx_elemidx:(t,e)=>[...y(A(t.shift(),e.type)),...y(A(t.shift(),e.elem))],typeidx_typeidx:(t,e)=>[...y(A(t.shift(),e.type)),...y(A(t.shift(),e.type))],dataidx_memoryidx:(t,e)=>[...y(A(t.shift(),e.data)),...y(A(t.shift(),e.memory))],memoryidx_memoryidx:(t,e)=>[...y(A(t.shift(),e.memory)),...y(A(t.shift(),e.memory))],tableidx_tableidx:(t,e)=>[...y(A(t.shift(),e.table)),...y(A(t.shift(),e.table))]},ot={};(function t(e,r){for(let i=0,s,l,n;i<e.length;i++)(s=e[i])&&(Array.isArray(s)?t(s,i):([l,n]=s.split(" "),rt[l]=r?[r,...y(i)]:[i],n&&(ot[l]=St[n])))})(rt);var Ot=(t,e)=>{let r=[],i=[];for(;t?.length;){let s=t.shift();if(s?.[0]==="@metadata"){i.push(s.slice(1));continue}Array.isArray(s)&&(s.loc!=null&&($.loc=s.loc),$(`Unknown instruction ${s[0]}`));let[...l]=rt[s]||$(`Unknown instruction ${s}`);ot[s]&&(s==="select"&&t[0]?.length?l[0]++:ot[s]===St.reftype&&(t[0][1]==="null"||t[0][0]!=="ref")&&l[l.length-1]++,l.push(...ot[s](t,e,s)));for(let[n,a]of i)(e.meta[n]??=[]).push([r.length,a]);r.push(...l)}return r.push(11),r},Z=(t,e)=>Ot(V([t],e),e),A=(t,e,r)=>(r=O(t)?e[t]:+t,r in e?r:$(`Unknown ${e.name} ${t}`)),it=(t,e,r)=>(r=O(t)?e.length-e[t]:+t,isNaN(r)||r>e.length?$(`Bad label ${t}`):r),le=t=>{let e,r,i,s;for(;dt(t[0]);)[i,s]=t.shift().split("="),i==="offset"?r=+s:i==="align"?e=+s:$(`Unknown param ${i}=${s}`);return(r<0||r>4294967295)&&$(`Bad offset ${r}`),(e<=0||e>4294967295)&&$(`Bad align ${e}`),e&&(e=Math.log2(e))%1&&$(`Bad align ${e}`),[e,r]},Et=(t,e,r=0)=>{let[i,s]=le(t),l=(i??ae(e))|(r&&64);return r?[...y(l),...y(r),...y(s??0)]:[...y(l),...y(s??0)]},ae=t=>{let e=t.indexOf(".",3)+1,r=t.slice(1,t[0]==="v"?4:3);if(t[e]==="a"&&(e=t.indexOf(".",e)+1),t[0]==="m")return t.includes("64")?3:2;if(t[e]==="r"){let l=t.slice(e,e+6).match(/\d+/);return Math.log2(l?l[0]/8:+r/8)}let i=t[e]==="l"?e+4:e+5,s=t.slice(i).match(/(\d+)(x|_|$)/);return Math.log2(s?s[2]==="x"?8:s[1]/8:+r/8)},ft=t=>{let e=t[0]==="i64"&&t.shift(),r=t[t.length-1]==="shared"&&t.pop(),i=!isNaN(parseInt(t[1])),s=(e?4:0)|(r?2:0)|(i?1:0),l=e?n=>{if(typeof n=="bigint")return n;let a=typeof n=="string"?n.replaceAll("_",""):String(n);return BigInt(a)}:ct;return i?[s,...y(l(t.shift())),...y(l(t.shift()))]:[s,...y(l(t.shift()))]},ct=(t,e=4294967295)=>{let r=typeof t=="string"&&t[0]!=="+"?D.parse(t):typeof t=="number"?t:$(`Bad int ${t}`);return r>e?$(`Value out of range ${t}`):r},k=t=>[...y(t.length),...t.flat()];function Ft(t,e={}){typeof t=="string"&&(t=C(t));let{indent:r=" ",newline:i=`
|
|
3
|
-
`,comments:s=!0}=e;if(r||="",i||="",typeof t[0]=="string"&&t[0][0]!==";")return
|
|
4
|
-
`}else{let
|
|
5
|
-
`?f+="":(p&&p!==")"&&p!==" "||i||p===")")&&(f+=" "),f+=I,o=!1}}return u?`(${f.replaceAll(i+h+"("," (")})`:`(${f+i+r.repeat(c)})`}}var Ct={funcref:["ref.func","call_ref","return_call_ref"],sign_ext:["i32.extend8_s","i32.extend16_s","i64.extend8_s","i64.extend16_s","i64.extend32_s"],nontrapping:["i32.trunc_sat_f32_s","i32.trunc_sat_f32_u","i32.trunc_sat_f64_s","i32.trunc_sat_f64_u","i64.trunc_sat_f32_s","i64.trunc_sat_f32_u","i64.trunc_sat_f64_s","i64.trunc_sat_f64_u"],bulk_memory:["memory.copy","memory.fill"],return_call:["return_call","return_call_indirect"],i31ref:["ref.i31","i31.get_s","i31.get_u"],extended_const:["global.get"],multi_value:[],gc:["struct.new","struct.get","struct.set","array.new","array.get","array.set","array.len","struct.new_default","array.new_default","array.new_fixed","array.copy"],ref_cast:["ref.test","ref.cast","br_on_cast","br_on_cast_fail"]},wt=Object.keys(Ct),ne=t=>{if(t===!0)return Object.fromEntries(wt.map(e=>[e,!0]));if(t===!1)return{};if(typeof t=="string"){let e=new Set(t.split(/\s+/).filter(Boolean));return Object.fromEntries(wt.map(r=>[r,e.has(r)||e.has("all")]))}return{...t}},S=(t,e,r,i)=>{if(e(t,r,i),Array.isArray(t))for(let s=0;s<t.length;s++)S(t[s],e,t,s)},j=(t,e,r,i)=>{if(Array.isArray(t))for(let s=0;s<t.length;s++)j(t[s],e,t,s);e(t,r,i)},fe=t=>{let e=new Set;return S(t,r=>{if(typeof r=="string")for(let[i,s]of Object.entries(Ct))s.some(l=>r===l||r.startsWith(l+" "))&&e.add(i)}),S(t,r=>{if(!(!Array.isArray(r)||r[0]!=="global"))for(let i of r)Array.isArray(i)&&(i[0]==="i32.add"||i[0]==="i32.sub"||i[0]==="i32.mul"||i[0]==="i64.add"||i[0]==="i64.sub"||i[0]==="i64.mul")&&S(i,s=>{Array.isArray(s)&&s[0]==="global.get"&&e.add("extended_const")})}),S(t,r=>{if(!Array.isArray(r)||r[0]!=="func")return;let i=0;for(let s of r)Array.isArray(s)&&s[0]==="result"&&(i+=s.length-1);i>1&&e.add("multi_value")}),e},Lt=t=>Array.isArray(t)?t.map(Lt):t,Dt=(t,e)=>{let r=[],i=t[0]==="module"?1:0;for(let s=i;s<t.length;s++)Array.isArray(t[s])&&t[s][0]===e&&r.push({node:t[s],idx:s});return r},pt=(t,e,r)=>t.splice(e,0,r),oe=0,R=t=>`$__${t}${oe++}`,ce=(t,e)=>{let r=new Set;if(S(t,f=>{Array.isArray(f)&&f[0]==="ref.func"&&r.add(f[1])}),!r.size)return t;let i=R("fntbl"),s=[...r],l=Object.fromEntries(s.map((f,o)=>[f,o])),n=Dt(t,"func"),a=n.length?n[0].idx:t[0]==="module"?1:0;pt(t,a,["table",i,"funcref",["elem",...s]]);let c={};return S(t,f=>{if(!Array.isArray(f)||f[0]!=="func")return;let o=typeof f[1]=="string"&&f[1][0]==="$"?f[1]:null;if(!o)return;let u=[],h=[];for(let x of f){if(Array.isArray(x)&&x[0]==="param")for(let I=1;I<x.length;I++)x[I][0]!=="$"&&u.push(x[I]);if(Array.isArray(x)&&x[0]==="result")for(let I=1;I<x.length;I++)h.push(x[I])}c[o]={params:u,results:h}}),j(t,(f,o,u)=>{if(!(!Array.isArray(f)||!o)){if(f[0]==="ref.func"&&l[f[1]]!==void 0&&(o[u]=["i32.const",l[f[1]]]),f[0]==="call_ref"){let h=f[1],x=f.slice(2);o[u]=["call_indirect",i,["type",h],...x]}if(f[0]==="return_call_ref"){let h=f[1],x=f.slice(2);o[u]=["return_call_indirect",i,["type",h],...x]}}}),t},P={funcref:ce},ue={"i32.extend8_s":["i32",24],"i32.extend16_s":["i32",16],"i64.extend8_s":["i64",56n],"i64.extend16_s":["i64",48n],"i64.extend32_s":["i64",32n]},me=(t,e)=>(j(t,(r,i,s)=>{if(!Array.isArray(r)||!i)return;let l=ue[r[0]];if(!l)return;let[n,a]=l,c=r.slice(1);i[s]=[`${n}.shr_s`,[`${n}.shl`,...c,[`${n}.const`,a]],[`${n}.const`,a]]}),t);P.sign_ext=me;var Wt={"i32.trunc_sat_f32_s":{itype:"i32",ftype:"f32",signed:!0,min:-2147483648,max:2147483647},"i32.trunc_sat_f32_u":{itype:"i32",ftype:"f32",signed:!1,min:0,max:4294967295},"i32.trunc_sat_f64_s":{itype:"i32",ftype:"f64",signed:!0,min:-2147483648,max:2147483647},"i32.trunc_sat_f64_u":{itype:"i32",ftype:"f64",signed:!1,min:0,max:4294967295},"i64.trunc_sat_f32_s":{itype:"i64",ftype:"f32",signed:!0,min:-9223372036854775808n,max:9223372036854775807n},"i64.trunc_sat_f32_u":{itype:"i64",ftype:"f32",signed:!1,min:0n,max:18446744073709551615n},"i64.trunc_sat_f64_s":{itype:"i64",ftype:"f64",signed:!0,min:-9223372036854775808n,max:9223372036854775807n},"i64.trunc_sat_f64_u":{itype:"i64",ftype:"f64",signed:!1,min:0n,max:18446744073709551615n}},_e=(t,e)=>{let r=new Set;if(S(t,s=>{Array.isArray(s)&&Wt[s[0]]&&r.add(s[0])}),!r.size)return t;let i={};for(let s of r){let{itype:l,ftype:n,signed:a,min:c,max:f}=Wt[s],o=R(`trunc_${l}_${n}_${a?"s":"u"}`);i[s]=o;let u=`${l}.trunc_${n}_${a?"s":"u"}`,h=l==="i64"?0n:0,x=["func",o,["param","$v",n],["result",l],["if",["result",l],[`${n}.ne`,["local.get","$v"],["local.get","$v"]],["then",[`${l}.const`,h]],["else",["if",["result",l],[`${n}.lt`,["local.get","$v"],[`${n}.const`,typeof c=="bigint"?Number(c):c]],["then",[`${l}.const`,c]],["else",["if",["result",l],[`${n}.gt`,["local.get","$v"],[`${n}.const`,typeof f=="bigint"?Number(f):f]],["then",[`${l}.const`,f]],["else",[u,["local.get","$v"]]]]]]]]];t.push(x)}return j(t,(s,l,n)=>{!Array.isArray(s)||!l||i[s[0]]&&(l[n]=["call",i[s[0]],...s.slice(1)])}),t};P.nontrapping=_e;var pe=(t,e)=>{let r=new Set,i=new Set;S(t,n=>{if(Array.isArray(n)){if(n[0]==="memory.copy"){let a=typeof n[1]=="number"?n[1]:0,c=typeof n[2]=="number"?n[2]:0;r.add(`${a}_${c}`)}if(n[0]==="memory.fill"){let a=typeof n[1]=="number"?n[1]:0;i.add(a)}}});let s={},l={};for(let n of r){let[a,c]=n.split("_").map(Number),f=R(`memcpy${n==="0_0"?"":"_"+n}`);s[n]=f;let o=a?["i32.store8",a]:["i32.store8"],u=c?["i32.load8_u",c]:["i32.load8_u"];t.push(["func",f,["param","$dst","i32"],["param","$src","i32"],["param","$len","i32"],["local","$i","i32"],["block","$done",["loop","$loop",["br_if","$done",["i32.ge_u",["local.get","$i"],["local.get","$len"]]],[...o,["i32.add",["local.get","$dst"],["local.get","$i"]],[...u,["i32.add",["local.get","$src"],["local.get","$i"]]]],["local.set","$i",["i32.add",["local.get","$i"],["i32.const",1]]],["br","$loop"]]]])}for(let n of i){let a=R(`memset${n===0?"":"_"+n}`);l[n]=a;let c=n?["i32.store8",n]:["i32.store8"];t.push(["func",a,["param","$dst","i32"],["param","$val","i32"],["param","$len","i32"],["local","$i","i32"],["block","$done",["loop","$loop",["br_if","$done",["i32.ge_u",["local.get","$i"],["local.get","$len"]]],[...c,["i32.add",["local.get","$dst"],["local.get","$i"]],["local.get","$val"]],["local.set","$i",["i32.add",["local.get","$i"],["i32.const",1]]],["br","$loop"]]]])}return j(t,(n,a,c)=>{if(!(!Array.isArray(n)||!a)){if(n[0]==="memory.copy"){let f=typeof n[1]=="number"?n[1]:0,o=typeof n[2]=="number"?n[2]:0,u=n.filter(h=>Array.isArray(h)||typeof h=="string"&&h[0]==="$");a[c]=["call",s[`${f}_${o}`],...u]}if(n[0]==="memory.fill"){let f=typeof n[1]=="number"?n[1]:0,o=n.filter(u=>Array.isArray(u)||typeof u=="string"&&u[0]==="$");a[c]=["call",l[f],...o]}}}),t};P.bulk_memory=pe;var ye=(t,e)=>{let r=!1;return S(t,i=>{Array.isArray(i)&&(i[0]==="return_call"||i[0]==="return_call_indirect")&&(r=!0)}),r&&j(t,(i,s,l)=>{!Array.isArray(i)||!s||(i[0]==="return_call"&&(s[l]=["return",["call",...i.slice(1)]]),i[0]==="return_call_indirect"&&(s[l]=["return",["call_indirect",...i.slice(1)]]))}),t};P.return_call=ye;var ge=(t,e)=>(j(t,(r,i,s)=>{if(!(!Array.isArray(r)||!i)&&(r[0]==="ref.i31"&&(i[s]=["i32.and",...r.slice(1),["i32.const",2147483647]]),r[0]==="i31.get_u"&&(i[s]=r.length>1?r[1]:["drop"]),r[0]==="i31.get_s")){let l=r.slice(1);i[s]=["i32.shr_s",["i32.shl",...l,["i32.const",1]],["i32.const",1]]}}),t);P.i31ref=ge;var xe=(t,e)=>{let r={};S(t,s=>{if(!Array.isArray(s)||s[0]!=="global")return;let l=typeof s[1]=="string"&&s[1][0]==="$"?s[1]:null;if(l)for(let n=s.length-1;n>=0;n--){let a=s[n];if(Array.isArray(a)&&(a[0]==="i32.const"||a[0]==="i64.const"||a[0]==="f32.const"||a[0]==="f64.const")){r[l]={type:a[0].split(".")[0],value:a[1]};break}}});let i=s=>{if(!Array.isArray(s))return s;let l=s[0];if(l==="global.get"&&r[s[1]]){let n=r[s[1]];return[`${n.type}.const`,n.value]}if(l==="i32.add"||l==="i64.add"){let n=i(s[1]),a=i(s[2]);if(n&&a&&n[0]?.endsWith(".const")&&a[0]?.endsWith(".const")){let c=l.split(".")[0],f=c==="i64"?BigInt(n[1]):Number(n[1]),o=c==="i64"?BigInt(a[1]):Number(a[1]);return[`${c}.const`,f+o]}}if(l==="i32.sub"||l==="i64.sub"){let n=i(s[1]),a=i(s[2]);if(n&&a&&n[0]?.endsWith(".const")&&a[0]?.endsWith(".const")){let c=l.split(".")[0],f=c==="i64"?BigInt(n[1]):Number(n[1]),o=c==="i64"?BigInt(a[1]):Number(a[1]);return[`${c}.const`,f-o]}}if(l==="i32.mul"||l==="i64.mul"){let n=i(s[1]),a=i(s[2]);if(n&&a&&n[0]?.endsWith(".const")&&a[0]?.endsWith(".const")){let c=l.split(".")[0],f=c==="i64"?BigInt(n[1]):Number(n[1]),o=c==="i64"?BigInt(a[1]):Number(a[1]);return[`${c}.const`,f*o]}}return s};return j(t,(s,l,n)=>{if(!(!Array.isArray(s)||s[0]!=="global"||!l)){for(let a=2;a<s.length;a++)if(Array.isArray(s[a])){let c=i(s[a]);c!==s[a]&&(s[a]=c)}}}),t};P.extended_const=xe;var he=(t,e)=>{let r=new Map,i=[];if(S(t,a=>{if(!Array.isArray(a)||a[0]!=="func")return;let c=typeof a[1]=="string"&&a[1][0]==="$"?a[1]:null,f=[];for(let o of a)if(Array.isArray(o)&&o[0]==="result")for(let u=1;u<o.length;u++)f.push(o[u]);f.length>1&&c&&r.set(c,f)}),!r.size)return t;let s=Math.max(...[...r.values()].map(a=>a.length)),l={};for(let[a,c]of r)for(let f=1;f<c.length;f++){let o=c[f];if(l[o]||(l[o]=[]),l[o].length<f){let u=R(`ret_${o}_${l[o].length}`);l[o].push(u),i.push(["global",u,["mut",o],[`${o}.const`,o==="i64"?0n:0]])}}let n=t[0]==="module"?1:0;for(let a of i.reverse())pt(t,n,a);return j(t,(a,c,f)=>{if(!Array.isArray(a)||a[0]!=="func")return;let o=typeof a[1]=="string"&&a[1][0]==="$"?a[1]:null;if(!o||!r.has(o))return;let u=r.get(o);for(let h=0;h<a.length;h++)if(Array.isArray(a[h])&&a[h][0]==="result"){a[h]=["result",u[0]];break}}),t};P.multi_value=he;var st={i32:4,i64:8,f32:4,f64:8},de=(t,e)=>{let r=new Map,i=new Map,s=1;if(S(t,p=>{if(!Array.isArray(p)||p[0]!=="type")return;let N=typeof p[1]=="string"&&p[1][0]==="$"?p[1]:null;if(N){for(let m of p)if(Array.isArray(m)){if(m[0]==="struct"){let g=[];for(let _ of m)if(Array.isArray(_)&&_[0]==="field"){let b=typeof _[1]=="string"&&_[1][0]==="$"?_[1]:null,d=b?_[2]:_[1],v=Array.isArray(d)&&d[0]==="mut"?d[1]:d;g.push({name:b,type:v})}r.set(N,{kind:"struct",fields:g}),i.set(N,s++)}if(m[0]==="array"){let g=m[1],_=Array.isArray(g)&&g[0]==="mut"?g[1]:g;r.set(N,{kind:"array",elemType:_}),i.set(N,s++)}}}}),!r.size)return t;let l=Dt(t,"memory").length>0,n=R("alloc"),a=R("heap_ptr"),c=t[0]==="module"?1:0;l||pt(t,c,["memory",1]),pt(t,c+1,["global",a,["mut","i32"],["i32.const",1024]]);let f=["func",n,["param","$size","i32"],["result","i32"],["local","$ptr","i32"],["local.set","$ptr",["global.get",a]],["global.set",a,["i32.add",["global.get",a],["local.get","$size"]]],["local.get","$ptr"]];t.push(f);let o=p=>{let N=4;for(let m of p.fields)N+=st[m.type]||4;return N},u=(p,N)=>{let m=4;for(let g=0;g<N;g++)m+=st[p.fields[g].type]||4;return m},h=(p,N)=>{for(let m=0;m<p.fields.length;m++)if(p.fields[m].name===N)return m;return-1},x=0,I=()=>`$__gc_tmp${x++}`;return S(t,p=>{if(!Array.isArray(p)||p[0]!=="func")return;let N=!1,m=!1,g=!1,_=!1;if(S(p,d=>{Array.isArray(d)&&((d[0]==="struct.new"||d[0]==="struct.new_default")&&(N=!0),(d[0]==="array.new"||d[0]==="array.new_default")&&(m=!0,g=!0,_=!0))}),!N&&!m)return;let b=1;for(let d=1;d<p.length;d++){let v=p[d];if(Array.isArray(v)&&(v[0]==="param"||v[0]==="result"||v[0]==="local"||v[0]==="export"||v[0]==="type"))b=d+1;else if(typeof v=="string"&&v[0]==="$")b=d+1;else if(!Array.isArray(v))b=d+1;else break}N&&p.splice(b++,0,["local","$__gc_ptr","i32"]),m&&p.splice(b++,0,["local","$__gc_aptr","i32"]),g&&p.splice(b++,0,["local","$__gc_alen","i32"]),_&&p.splice(b++,0,["local","$__gc_aidx","i32"])}),j(t,(p,N,m)=>{if(!(!Array.isArray(p)||!N)){if(p[0]==="struct.new"||p[0]==="struct.new_default"){let g=p[1],_=r.get(g);if(!_||_.kind!=="struct")return;let b=o(_),d=i.get(g),v=p.slice(2),M="$__gc_ptr",T=[["local.set",M,["call",n,["i32.const",b]]],["i32.store",["local.get",M],["i32.const",d]]];if(p[0]==="struct.new")for(let z=0;z<_.fields.length;z++){let U=_.fields[z],tt=u(_,z),G=U.type==="i64"?"i64.store":U.type==="f32"?"f32.store":U.type==="f64"?"f64.store":"i32.store";T.push([G,["i32.add",["local.get",M],["i32.const",tt]],v[z]||[`${U.type}.const`,0]])}else for(let z=0;z<_.fields.length;z++){let U=_.fields[z],tt=u(_,z),G=U.type==="i64"?"i64.store":U.type==="f32"?"f32.store":U.type==="f64"?"f64.store":"i32.store",Yt=U.type==="i64"?["i64.const",0n]:U.type==="f32"?["f32.const",0]:U.type==="f64"?["f64.const",0]:["i32.const",0];T.push([G,["i32.add",["local.get",M],["i32.const",tt]],Yt])}T.push(["local.get",M]),N[m]=["block",["result","i32"],...T]}if(p[0]==="struct.get"){let g=p[1],_=p[2],b=p[3],d=r.get(g);if(!d||d.kind!=="struct")return;let v=typeof _=="string"&&_[0]==="$"?h(d,_):parseInt(_);if(v<0)return;let M=d.fields[v],T=u(d,v),z=M.type==="i64"?"i64.load":M.type==="f32"?"f32.load":M.type==="f64"?"f64.load":"i32.load";N[m]=[z,["i32.add",b,["i32.const",T]]]}if(p[0]==="struct.set"){let g=p[1],_=p[2],b=p[3],d=p[4],v=r.get(g);if(!v||v.kind!=="struct")return;let M=typeof _=="string"&&_[0]==="$"?h(v,_):parseInt(_);if(M<0)return;let T=v.fields[M],z=u(v,M),U=T.type==="i64"?"i64.store":T.type==="f32"?"f32.store":T.type==="f64"?"f64.store":"i32.store";N[m]=[U,["i32.add",b,["i32.const",z]],d]}if(p[0]==="array.new"||p[0]==="array.new_default"){let g=p[1],_=r.get(g);if(!_||_.kind!=="array")return;let b=i.get(g),d=st[_.elemType]||4,v=p[0]==="array.new"?p[2]:null,M=p[0]==="array.new"?p[3]:p[2],T="$__gc_aptr",z="$__gc_alen",U="$__gc_aidx",tt=_.elemType==="i64"?"i64.store":_.elemType==="f32"?"f32.store":_.elemType==="f64"?"f64.store":"i32.store",G=[["local.set",z,M],["local.set",T,["call",n,["i32.add",["i32.const",8],["i32.mul",["local.get",z],["i32.const",d]]]]],["i32.store",["local.get",T],["i32.const",b]],["i32.store",["i32.add",["local.get",T],["i32.const",4]],["local.get",z]]];v&&G.push(["local.set",U,["i32.const",0]],["block","$done",["loop","$loop",["br_if","$done",["i32.ge_u",["local.get",U],["local.get",z]]],[tt,["i32.add",["i32.add",["local.get",T],["i32.const",8]],["i32.mul",["local.get",U],["i32.const",d]]],v],["local.set",U,["i32.add",["local.get",U],["i32.const",1]]],["br","$loop"]]]),G.push(["local.get",T]),N[m]=["block",["result","i32"],...G]}if(p[0]==="array.get"){let g=p[1],_=p[2],b=p[3],d=r.get(g);if(!d||d.kind!=="array")return;let v=st[d.elemType]||4,M=d.elemType==="i64"?"i64.load":d.elemType==="f32"?"f32.load":d.elemType==="f64"?"f64.load":"i32.load";N[m]=[M,["i32.add",["i32.add",_,["i32.const",8]],["i32.mul",b,["i32.const",v]]]]}if(p[0]==="array.set"){let g=p[1],_=p[2],b=p[3],d=p[4],v=r.get(g);if(!v||v.kind!=="array")return;let M=st[v.elemType]||4,T=v.elemType==="i64"?"i64.store":v.elemType==="f32"?"f32.store":v.elemType==="f64"?"f64.store":"i32.store";N[m]=[T,["i32.add",["i32.add",_,["i32.const",8]],["i32.mul",b,["i32.const",M]]],d]}if(p[0]==="array.len"){let g=p[1];N[m]=["i32.load",["i32.add",g,["i32.const",4]]]}}}),t};P.gc=de;var be=(t,e)=>{let r=new Map,i=1;return S(t,s=>{if(!Array.isArray(s)||s[0]!=="type")return;let l=typeof s[1]=="string"&&s[1][0]==="$"?s[1]:null;if(l)for(let n of s)Array.isArray(n)&&(n[0]==="struct"||n[0]==="array")&&r.set(l,i++)}),r.size&&j(t,(s,l,n)=>{if(!(!Array.isArray(s)||!l)){if(s[0]==="ref.test"){let a=s[1],c=null;Array.isArray(a)&&a[0]==="ref"&&(c=a[1]==="null"?a[2]:a[1]);let f=s[2],o=r.get(c);o!==void 0&&(l[n]=["if",["result","i32"],["i32.eqz",f],["then",["i32.const",0]],["else",["i32.eq",["i32.load",f],["i32.const",o]]]])}if(s[0]==="ref.cast"){let a=s[1],c=null,f=!1;Array.isArray(a)&&a[0]==="ref"&&(a[1]==="null"?(f=!0,c=a[2]):c=a[1]);let o=s[2],u=r.get(c);if(u!==void 0){let h=R("cast");f?l[n]=["block",["result","i32"],["local",h,"i32"],["local.set",h,o],["if",["i32.and",["i32.ne",["local.get",h],["i32.const",0]],["i32.ne",["i32.load",["local.get",h]],["i32.const",u]]],["then",["unreachable"]]],["local.get",h]]:l[n]=["block",["result","i32"],["local",h,"i32"],["local.set",h,o],["if",["i32.or",["i32.eqz",["local.get",h]],["i32.ne",["i32.load",["local.get",h]],["i32.const",u]]],["then",["unreachable"]]],["local.get",h]]}}if(s[0]==="br_on_cast"){let a=s[1],c=s[2],f=s[3],o=s[4],u=null;Array.isArray(f)&&f[0]==="ref"&&(u=f[1]==="null"?f[2]:f[1]);let h=r.get(u);if(h!==void 0){let x=R("brcast");l[n]=["block",["result","i32"],["local",x,"i32"],["local.set",x,o],["br_if",a,["i32.and",["i32.ne",["local.get",x],["i32.const",0]],["i32.eq",["i32.load",["local.get",x]],["i32.const",h]]]],["local.get",x]]}}if(s[0]==="br_on_cast_fail"){let a=s[1],c=s[2],f=s[3],o=s[4],u=null;Array.isArray(f)&&f[0]==="ref"&&(u=f[1]==="null"?f[2]:f[1]);let h=r.get(u);if(h!==void 0){let x=R("brfail");l[n]=["block",["result","i32"],["local",x,"i32"],["local.set",x,o],["br_if",a,["i32.or",["i32.eqz",["local.get",x]],["i32.ne",["i32.load",["local.get",x]],["i32.const",h]]]],["local.get",x]]}}}}),t};P.ref_cast=be;function yt(t,e=!0){typeof t=="string"&&(t=C(t)),t=Lt(t),e=ne(e);let r=fe(t),i={uid:0};for(let s of wt)r.has(s)&&e[s]!==!1&&P[s]&&(t=P[s](t,i));return t}var vt={treeshake:!0,fold:!0,deadcode:!0,locals:!0,identity:!0,strength:!0,branch:!0,propagate:!0,inline:!0},$t=Object.keys(vt),Ae=t=>{if(t===!0)return{...vt};if(t===!1)return{};if(typeof t=="string"){let e=new Set(t.split(/\s+/).filter(Boolean));return e.size===1&&$t.includes([...e][0])?Object.fromEntries($t.map(r=>[r,e.has(r)])):Object.fromEntries($t.map(r=>[r,e.has(r)||e.has("all")]))}return{...vt,...t}},F=t=>Array.isArray(t)?t.map(F):t,H=(t,e,r,i)=>{if(e(t,r,i),Array.isArray(t))for(let s=0;s<t.length;s++)H(t[s],e,t,s)},Y=(t,e,r,i)=>{if(Array.isArray(t))for(let l=0;l<t.length;l++){let n=Y(t[l],e,t,l);n!==void 0&&(t[l]=n)}let s=e(t,r,i);return s!==void 0?s:t},we=t=>{if(!Array.isArray(t)||t[0]!=="module")return t;let e=new Map,r=new Map,i=new Map,s=new Map,l=new Map,n=[],a=[],c=0,f=0,o=0,u=0,h=0,x=0;for(let m of t.slice(1)){if(!Array.isArray(m))continue;let g=m[0];if(g==="type"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:o;i.set(_,{node:m,idx:o,used:!1}),typeof _=="string"&&i.set(o,i.get(_)),o++}else if(g==="func"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:c,b=m.some(d=>Array.isArray(d)&&d[0]==="export");e.set(_,{node:m,idx:c,used:b}),typeof _=="string"&&e.set(c,e.get(_)),c++}else if(g==="global"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:f,b=m.some(d=>Array.isArray(d)&&d[0]==="export");r.set(_,{node:m,idx:f,used:b}),typeof _=="string"&&r.set(f,r.get(_)),f++}else if(g==="table"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:u,b=m.some(d=>Array.isArray(d)&&d[0]==="export");s.set(_,{node:m,idx:u,used:b}),typeof _=="string"&&s.set(u,s.get(_)),u++}else if(g==="memory"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:h,b=m.some(d=>Array.isArray(d)&&d[0]==="export");l.set(_,{node:m,idx:h,used:b}),typeof _=="string"&&l.set(h,l.get(_)),h++}else if(g==="import"){for(let _ of m)if(Array.isArray(_)&&_[0]==="func"){let b=typeof _[1]=="string"&&_[1][0]==="$"?_[1]:x;e.set(b,{node:m,idx:x,used:!0,isImport:!0}),typeof b=="string"&&e.set(x,e.get(b)),x++,c++}}else g==="export"?n.push(m):g==="start"&&a.push(m)}for(let m of n)for(let g of m){if(!Array.isArray(g))continue;let[_,b]=g;_==="func"&&e.has(b)?e.get(b).used=!0:_==="global"&&r.has(b)?r.get(b).used=!0:_==="table"&&s.has(b)?s.get(b).used=!0:_==="memory"&&l.has(b)&&(l.get(b).used=!0)}for(let m of a){let g=m[1];typeof g=="string"&&g[0]!=="$"&&(g=+g),e.has(g)&&(e.get(g).used=!0)}let I=n.length>0||a.length>0;if(!I){for(let[,m]of e)if(m.used){I=!0;break}if(!I){for(let[,m]of r)if(m.used){I=!0;break}}if(!I){for(let[,m]of s)if(m.used){I=!0;break}}if(!I){for(let[,m]of l)if(m.used){I=!0;break}}}if(!I){for(let[,m]of e)m.used=!0;for(let[,m]of r)m.used=!0;for(let[,m]of s)m.used=!0;for(let[,m]of l)m.used=!0}for(let m of t.slice(1))!Array.isArray(m)||m[0]!=="elem"||H(m,g=>{if(Array.isArray(g)&&g[0]==="ref.func"){let _=g[1];e.has(_)&&(e.get(_).used=!0)}typeof g=="string"&&g[0]==="$"&&e.has(g)&&(e.get(g).used=!0)});let p=!0;for(;p;){p=!1;for(let[,m]of e)!m.used||m.isImport||H(m.node,g=>{if(!Array.isArray(g)){typeof g=="string"&&g[0]==="$"&&e.has(g)&&!e.get(g).used&&(e.get(g).used=!0,p=!0);return}let[_,b]=g;if((_==="call"||_==="return_call"||_==="ref.func")&&e.has(b)&&!e.get(b).used&&(e.get(b).used=!0,p=!0),(_==="global.get"||_==="global.set")&&r.has(b)&&!r.get(b).used&&(r.get(b).used=!0,p=!0),_==="call_indirect"||_==="return_call_indirect")for(let d of g)typeof d=="string"&&d[0]==="$"&&s.has(d)&&!s.get(d).used&&(s.get(d).used=!0,p=!0);_==="type"&&i.has(b)&&!i.get(b).used&&(i.get(b).used=!0,p=!0)})}let N=["module"];for(let m of t.slice(1)){if(!Array.isArray(m)){N.push(m);continue}let g=m[0];if(g==="func"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:null;(_?e.get(_):[...e.values()].find(d=>d.node===m))?.used&&N.push(m)}else if(g==="global"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:null;(_?r.get(_):[...r.values()].find(d=>d.node===m))?.used&&N.push(m)}else N.push(m)}return N},$e={"i32.add":(t,e)=>t+e|0,"i32.sub":(t,e)=>t-e|0,"i32.mul":(t,e)=>Math.imul(t,e),"i32.div_s":(t,e)=>e!==0?t/e|0:null,"i32.div_u":(t,e)=>e!==0?(t>>>0)/(e>>>0)|0:null,"i32.rem_s":(t,e)=>e!==0?t%e|0:null,"i32.rem_u":(t,e)=>e!==0?(t>>>0)%(e>>>0)|0:null,"i32.and":(t,e)=>t&e,"i32.or":(t,e)=>t|e,"i32.xor":(t,e)=>t^e,"i32.shl":(t,e)=>t<<(e&31),"i32.shr_s":(t,e)=>t>>(e&31),"i32.shr_u":(t,e)=>t>>>(e&31),"i32.rotl":(t,e)=>(e&=31,t<<e|t>>>32-e|0),"i32.rotr":(t,e)=>(e&=31,t>>>e|t<<32-e|0),"i32.eq":(t,e)=>t===e?1:0,"i32.ne":(t,e)=>t!==e?1:0,"i32.lt_s":(t,e)=>t<e?1:0,"i32.lt_u":(t,e)=>t>>>0<e>>>0?1:0,"i32.gt_s":(t,e)=>t>e?1:0,"i32.gt_u":(t,e)=>t>>>0>e>>>0?1:0,"i32.le_s":(t,e)=>t<=e?1:0,"i32.le_u":(t,e)=>t>>>0<=e>>>0?1:0,"i32.ge_s":(t,e)=>t>=e?1:0,"i32.ge_u":(t,e)=>t>>>0>=e>>>0?1:0,"i32.eqz":t=>t===0?1:0,"i32.clz":t=>Math.clz32(t),"i32.ctz":t=>t===0?32:31-Math.clz32(t&-t),"i32.popcnt":t=>{let e=0;for(;t;)e+=t&1,t>>>=1;return e},"i32.wrap_i64":t=>Number(BigInt.asIntN(32,t)),"i64.add":(t,e)=>BigInt.asIntN(64,t+e),"i64.sub":(t,e)=>BigInt.asIntN(64,t-e),"i64.mul":(t,e)=>BigInt.asIntN(64,t*e),"i64.div_s":(t,e)=>e!==0n?BigInt.asIntN(64,t/e):null,"i64.div_u":(t,e)=>e!==0n?BigInt.asUintN(64,BigInt.asUintN(64,t)/BigInt.asUintN(64,e)):null,"i64.rem_s":(t,e)=>e!==0n?BigInt.asIntN(64,t%e):null,"i64.rem_u":(t,e)=>e!==0n?BigInt.asUintN(64,BigInt.asUintN(64,t)%BigInt.asUintN(64,e)):null,"i64.and":(t,e)=>BigInt.asIntN(64,t&e),"i64.or":(t,e)=>BigInt.asIntN(64,t|e),"i64.xor":(t,e)=>BigInt.asIntN(64,t^e),"i64.shl":(t,e)=>BigInt.asIntN(64,t<<(e&63n)),"i64.shr_s":(t,e)=>BigInt.asIntN(64,t>>(e&63n)),"i64.shr_u":(t,e)=>BigInt.asUintN(64,BigInt.asUintN(64,t)>>(e&63n)),"i64.eq":(t,e)=>t===e?1:0,"i64.ne":(t,e)=>t!==e?1:0,"i64.lt_s":(t,e)=>t<e?1:0,"i64.lt_u":(t,e)=>BigInt.asUintN(64,t)<BigInt.asUintN(64,e)?1:0,"i64.gt_s":(t,e)=>t>e?1:0,"i64.gt_u":(t,e)=>BigInt.asUintN(64,t)>BigInt.asUintN(64,e)?1:0,"i64.le_s":(t,e)=>t<=e?1:0,"i64.le_u":(t,e)=>BigInt.asUintN(64,t)<=BigInt.asUintN(64,e)?1:0,"i64.ge_s":(t,e)=>t>=e?1:0,"i64.ge_u":(t,e)=>BigInt.asUintN(64,t)>=BigInt.asUintN(64,e)?1:0,"i64.eqz":t=>t===0n?1:0,"i64.extend_i32_s":t=>BigInt(t),"i64.extend_i32_u":t=>BigInt(t>>>0),"f32.add":(t,e)=>Math.fround(t+e),"f32.sub":(t,e)=>Math.fround(t-e),"f32.mul":(t,e)=>Math.fround(t*e),"f32.div":(t,e)=>Math.fround(t/e),"f32.neg":t=>Math.fround(-t),"f32.abs":t=>Math.fround(Math.abs(t)),"f32.sqrt":t=>Math.fround(Math.sqrt(t)),"f32.ceil":t=>Math.fround(Math.ceil(t)),"f32.floor":t=>Math.fround(Math.floor(t)),"f32.trunc":t=>Math.fround(Math.trunc(t)),"f32.nearest":t=>Math.fround(Math.round(t)),"f64.add":(t,e)=>t+e,"f64.sub":(t,e)=>t-e,"f64.mul":(t,e)=>t*e,"f64.div":(t,e)=>t/e,"f64.neg":t=>-t,"f64.abs":t=>Math.abs(t),"f64.sqrt":t=>Math.sqrt(t),"f64.ceil":t=>Math.ceil(t),"f64.floor":t=>Math.floor(t),"f64.trunc":t=>Math.trunc(t),"f64.nearest":t=>Math.round(t)},w=t=>{if(!Array.isArray(t)||t.length!==2)return null;let[e,r]=t;return e==="i32.const"?{type:"i32",value:Number(r)|0}:e==="i64.const"?{type:"i64",value:BigInt(r)}:e==="f32.const"?{type:"f32",value:Math.fround(Number(r))}:e==="f64.const"?{type:"f64",value:Number(r)}:null},Pt=(t,e)=>t==="i32"?["i32.const",e|0]:t==="i64"?["i64.const",e]:t==="f32"?["f32.const",Math.fround(e)]:t==="f64"?["f64.const",e]:null,ve=t=>Y(F(t),e=>{if(!Array.isArray(e))return;let r=e[0],i=$e[r];if(i){if(i.length===1&&e.length===2){let s=w(e[1]);if(!s)return;let l=i(s.value);if(l===null)return;let n=r.startsWith("i64.")&&!r.includes("eqz")?"i64":r.startsWith("f32.")?"f32":r.startsWith("f64.")?"f64":"i32";return Pt(n,l)}if(i.length===2&&e.length===3){let s=w(e[1]),l=w(e[2]);if(!s||!l)return;let n=i(s.value,l.value);if(n===null)return;let c=/\.(eq|ne|[lg][te])/.test(r)?"i32":r.startsWith("i64.")?"i64":r.startsWith("f32.")?"f32":r.startsWith("f64.")?"f64":"i32";return Pt(c,n)}}}),Ie={"i32.add":(t,e)=>{let r=w(t),i=w(e);return r?.value===0?e:i?.value===0?t:null},"i64.add":(t,e)=>{let r=w(t),i=w(e);return r?.value===0n?e:i?.value===0n?t:null},"i32.sub":(t,e)=>w(e)?.value===0?t:null,"i64.sub":(t,e)=>w(e)?.value===0n?t:null,"i32.mul":(t,e)=>{let r=w(t),i=w(e);return r?.value===1?e:i?.value===1?t:null},"i64.mul":(t,e)=>{let r=w(t),i=w(e);return r?.value===1n?e:i?.value===1n?t:null},"i32.div_s":(t,e)=>w(e)?.value===1?t:null,"i32.div_u":(t,e)=>w(e)?.value===1?t:null,"i64.div_s":(t,e)=>w(e)?.value===1n?t:null,"i64.div_u":(t,e)=>w(e)?.value===1n?t:null,"i32.and":(t,e)=>{let r=w(t),i=w(e);return r?.value===-1?e:i?.value===-1?t:null},"i64.and":(t,e)=>{let r=w(t),i=w(e);return r?.value===-1n?e:i?.value===-1n?t:null},"i32.or":(t,e)=>{let r=w(t),i=w(e);return r?.value===0?e:i?.value===0?t:null},"i64.or":(t,e)=>{let r=w(t),i=w(e);return r?.value===0n?e:i?.value===0n?t:null},"i32.xor":(t,e)=>{let r=w(t),i=w(e);return r?.value===0?e:i?.value===0?t:null},"i64.xor":(t,e)=>{let r=w(t),i=w(e);return r?.value===0n?e:i?.value===0n?t:null},"i32.shl":(t,e)=>w(e)?.value===0?t:null,"i32.shr_s":(t,e)=>w(e)?.value===0?t:null,"i32.shr_u":(t,e)=>w(e)?.value===0?t:null,"i64.shl":(t,e)=>w(e)?.value===0n?t:null,"i64.shr_s":(t,e)=>w(e)?.value===0n?t:null,"i64.shr_u":(t,e)=>w(e)?.value===0n?t:null},Ne=t=>Y(F(t),e=>{if(!Array.isArray(e)||e.length!==3)return;let r=Ie[e[0]];if(!r)return;let i=r(e[1],e[2]);if(i!==null)return i}),ke=t=>Y(F(t),e=>{if(!Array.isArray(e)||e.length!==3)return;let[r,i,s]=e;if(r==="i32.mul"){let l=w(s);if(l&&l.value>0&&!(l.value&l.value-1)){let a=Math.log2(l.value);if(Number.isInteger(a))return["i32.shl",i,["i32.const",a]]}let n=w(i);if(n&&n.value>0&&!(n.value&n.value-1)){let a=Math.log2(n.value);if(Number.isInteger(a))return["i32.shl",s,["i32.const",a]]}}if(r==="i64.mul"){let l=w(s);if(l&&l.value>0n&&(l.value&l.value-1n)===0n){let a=BigInt(l.value.toString(2).length-1);return["i64.shl",i,["i64.const",a]]}let n=w(i);if(n&&n.value>0n&&(n.value&n.value-1n)===0n){let a=BigInt(n.value.toString(2).length-1);return["i64.shl",s,["i64.const",a]]}}if(r==="i32.div_u"){let l=w(s);if(l&&l.value>0&&!(l.value&l.value-1)){let n=Math.log2(l.value);if(Number.isInteger(n))return["i32.shr_u",i,["i32.const",n]]}}if(r==="i64.div_u"){let l=w(s);if(l&&l.value>0n&&(l.value&l.value-1n)===0n){let n=BigInt(l.value.toString(2).length-1);return["i64.shr_u",i,["i64.const",n]]}}if(r==="i32.rem_u"){let l=w(s);if(l&&l.value>0&&!(l.value&l.value-1))return["i32.and",i,["i32.const",l.value-1]]}if(r==="i64.rem_u"){let l=w(s);if(l&&l.value>0n&&(l.value&l.value-1n)===0n)return["i64.and",i,["i64.const",l.value-1n]]}}),Be=t=>Y(F(t),e=>{if(!Array.isArray(e))return;let r=e[0];if(r==="if"){let i=1;for(;i<e.length;){let c=e[i];if(Array.isArray(c)&&(c[0]==="then"||c[0]==="else"||c[0]==="result"||c[0]==="param")){i++;continue}break}let s=e[i],l=w(s);if(!l)return;let n=null,a=null;for(let c=i+1;c<e.length;c++){let f=e[c];Array.isArray(f)&&(f[0]==="then"?n=f:f[0]==="else"&&(a=f))}if(l.value!==0&&l.value!==0n){if(n&&n.length>1){let c=n.slice(1);return c.length===1?c[0]:["block",...c]}return["nop"]}else{if(a&&a.length>1){let c=a.slice(1);return c.length===1?c[0]:["block",...c]}return["nop"]}}if(r==="br_if"&&e.length>=3){let i=e[e.length-1],s=w(i);return s?s.value===0||s.value===0n?["nop"]:["br",e[1]]:void 0}if(r==="select"&&e.length>=4){let i=e[e.length-1],s=w(i);return s?s.value===0||s.value===0n?e[2]:e[1]:void 0}}),jt=new Set(["unreachable","return","br","br_table"]),Me=t=>{let e=F(t);return H(e,r=>{if(!Array.isArray(r))return;let i=r[0];if((i==="func"||i==="block"||i==="loop")&&Rt(r),i==="if")for(let s=1;s<r.length;s++)Array.isArray(r[s])&&(r[s][0]==="then"||r[s][0]==="else")&&Rt(r[s])}),e},Rt=t=>{let e=!1,r=-1;for(let i=1;i<t.length;i++){let s=t[i];if(Array.isArray(s)){let l=s[0];if(l==="param"||l==="result"||l==="local"||l==="type"||l==="export")continue;e&&r===-1&&(r=i),jt.has(l)&&(e=!0,r=i+1)}else typeof s=="string"&&(e&&r===-1&&(r=i),jt.has(s)&&(e=!0,r=i+1))}r>0&&r<t.length&&t.splice(r)},ze=t=>{let e=F(t);return H(e,r=>{if(!Array.isArray(r)||r[0]!=="func")return;let i=[],s=new Map,l=new Set;for(let n=1;n<r.length;n++){let a=r[n];Array.isArray(a)&&(a[0]==="local"&&(i.push({idx:n,node:a}),typeof a[1]=="string"&&a[1][0]==="$"&&s.set(a[1],a[2])),a[0]==="param"&&typeof a[1]=="string"&&a[1][0]==="$"&&(s.set(a[1],a[2]),l.add(a[1])))}H(r,n=>{if(!Array.isArray(n))return;let a=n[0];if(a==="local.get"||a==="local.set"||a==="local.tee"){let c=n[1];typeof c=="string"&&l.add(c)}});for(let n=i.length-1;n>=0;n--){let{idx:a,node:c}=i[n],f=typeof c[1]=="string"&&c[1][0]==="$"?c[1]:null;f&&!l.has(f)&&r.splice(a,1)}}),e},Ue=t=>{let e=F(t);return H(e,r=>{if(!Array.isArray(r)||r[0]!=="func")return;let i=new Map;((l,n=1)=>{for(let a=n;a<l.length;a++){let c=l[a];if(!Array.isArray(c))continue;let f=c[0];if(f==="local.set"&&c.length===3){let o=c[1],u=c[2];w(u)&&typeof o=="string"?i.set(o,u):typeof o=="string"&&i.delete(o)}else if(f==="local.tee"&&c.length===3){let o=c[1],u=c[2];w(u)&&typeof o=="string"?i.set(o,u):typeof o=="string"&&i.delete(o)}else if(f==="local.get"&&c.length===2){let o=c[1];if(typeof o=="string"&&i.has(o)){let u=i.get(o);c.length=0,c.push(...F(u))}}else(f==="block"||f==="loop"||f==="if"||f==="call"||f==="call_indirect")&&i.clear();Y(c,o=>{if(!Array.isArray(o)||o[0]!=="local.get"||o.length!==2)return;let u=o[1];if(typeof u=="string"&&i.has(u)){let h=i.get(u);return F(h)}})}})(r)}),e},Te=t=>{if(!Array.isArray(t)||t[0]!=="module")return t;let e=F(t),r=new Map;for(let i of e.slice(1)){if(!Array.isArray(i)||i[0]!=="func")continue;let s=typeof i[1]=="string"&&i[1][0]==="$"?i[1]:null;if(!s)continue;let l=[],n=[],a=!1,c=!1;for(let f=1;f<i.length;f++){let o=i[f];if(Array.isArray(o))if(o[0]==="param")if(typeof o[1]=="string"&&o[1][0]==="$")l.push({name:o[1],type:o[2]});else{l=null;break}else o[0]==="local"?a=!0:o[0]==="export"?c=!0:o[0]!=="result"&&o[0]!=="type"&&n.push(o)}if(l&&!a&&!c&&l.length<=2&&n.length===1){let f=new Set(l.map(u=>u.name)),o=!1;H(n[0],u=>{Array.isArray(u)&&(u[0]==="local.set"||u[0]==="local.tee")&&f.has(u[1])&&(o=!0)}),o||r.set(s,{body:n[0],params:l})}}return r.size===0||Y(e,i=>{if(!Array.isArray(i)||i[0]!=="call")return;let s=i[1];if(!r.has(s))return;let{body:l,params:n}=r.get(s),a=i.slice(2);if(n.length===0)return F(l);let c=F(l);return Y(c,f=>{if(!Array.isArray(f)||f[0]!=="local.get")return;let o=f[1],u=n.findIndex(h=>h.name===o);if(u!==-1&&a[u])return F(a[u])}),c}),e};function gt(t,e=!0){return typeof t=="string"&&(t=C(t)),t=F(t),e=Ae(e),e.fold&&(t=ve(t)),e.identity&&(t=Ne(t)),e.strength&&(t=ke(t)),e.branch&&(t=Be(t)),e.propagate&&(t=Ue(t)),e.inline&&(t=Te(t)),e.deadcode&&(t=Me(t)),e.locals&&(t=ze(t)),e.treeshake&&(t=we(t)),t}var Vt="\uE000",Ht=t=>{if(!t||typeof t!="string")return null;let e=t.split(".")[0];return/^[if](32|64)|v128/.test(e)?e:/\.(eq|ne|[lg][te]|eqz)/.test(t)||t==="memory.size"||t==="memory.grow"?"i32":null},Ee=(t,e={})=>{if(!Array.isArray(t))return typeof t=="string"&&t[0]==="$"&&e.locals?.[t]?e.locals[t]:null;let[r,...i]=t;return Ht(r)?Ht(r):r==="local.get"&&e.locals?.[i[0]]?e.locals[i[0]]:r==="call"&&e.funcs?.[i[0]]?e.funcs[i[0]].result?.[0]:null};function It(t,e){if(t=e(t),Array.isArray(t))for(let r=0;r<t.length;r++){let i=It(t[r],e);i?._splice?(t.splice(r,1,...i),r+=i.length-1):t[r]=i}return t}function qe(t,e){let r=[],i=new Map;return It(t,s=>{if(!Array.isArray(s))return s;if(s[0]==="call"&&typeof s[1]=="function"){let l=s[1];if(!i.has(l)){let a=[];for(let o=2;o<s.length;o++){let u=Ee(s[o]);u&&a.push(u)}let c=r.length,f=l.name||`$fn${c}`;i.set(l,{idx:c,name:f.startsWith("$")?f:"$"+f,params:a,fn:l}),r.push(i.get(l))}let n=i.get(l);s[1]=n.name}return s}),r}function Se(t){return t.map(({name:e,params:r})=>["import",'"env"',`"${e.slice(1)}"`,["func",e,...r.map(i=>["param",i])]])}function Oe(t,...e){let r={};if(!Array.isArray(t)&&e.length&&typeof e[e.length-1]=="object"&&e[e.length-1]!==null&&!(e[e.length-1]instanceof Uint8Array)&&(r=e.pop()),Array.isArray(t)&&t.raw){let i=t[0];for(let f=0;f<e.length;f++)i+=Vt+t[f+1];let s=C(i),l=[],n=0;s=It(s,f=>{if(f===Vt){let o=e[n++];if(typeof o=="function")return l.push(o),o;if(typeof o=="string"&&(o[0]==="("||/^\s*\(/.test(o))){let u=C(o);return Array.isArray(u)&&Array.isArray(u[0])&&(u._splice=!0),u}return o instanceof Uint8Array?[...o]:o}return f});let a=null;if(l.length){let f=qe(s,l);if(f.length){let o=Se(f);s[0]==="module"?s.splice(1,0,...o):typeof s[0]=="string"?s=[...o,s]:s.unshift(...o),a={env:{}};for(let u of f)a.env[u.name.slice(1)]=u.fn}}r.polyfill&&(s=yt(s,r.polyfill)),r.optimize&&(s=gt(s,r.optimize));let c=Q(s);return a&&(c._imports=a),c}if(r.polyfill||r.optimize){let i=typeof t=="string"?C(t):t;return r.polyfill&&(i=yt(i,r.polyfill)),r.optimize&&(i=gt(i,r.optimize)),Q(i)}return Q(t)}function Fe(t,...e){let r=Oe(t,...e),i=new WebAssembly.Module(r);return new WebAssembly.Instance(i,r._imports).exports}var nr=Fe;export{Oe as compile,nr as default,gt as optimize,C as parse,yt as polyfill,Ft as print,Fe as watr};
|
|
1
|
+
var Gt=Object.defineProperty;var Kt=(t,e)=>{for(var r in e)Gt(t,r,{get:e[r],enumerable:!0})};var tt={};Kt(tt,{f32:()=>Q,f64:()=>J,i16:()=>te,i32:()=>W,i64:()=>Z,i8:()=>Qt,uleb:()=>p,uleb5:()=>Zt});var v=(t,e=v.loc)=>{if(e!=null&&v.src){let r=1,i=1;for(let s=0;s<e&&s<v.src.length;s++)v.src[s]===`
|
|
2
|
+
`?(r++,i=1):i++;t+=` at ${r}:${i}`}throw Error(t)};var Nt=/^_|_$|[^\da-f]_|_[^\da-f]/i,Bt=/^[+-]?(?:0x[\da-f]+|\d+)$/i,Xt=new TextEncoder,Jt=new TextDecoder("utf-8",{fatal:!0,ignoreBOM:!0}),kt={n:10,r:13,t:9,'"':34,"'":39,"\\":92},nt=t=>{let e=[],r=1,i,s,l="",a=()=>(l&&e.push(...Xt.encode(l)),l="");for(;r<t.length-1;)s=t[r++],i=null,s==="\\"&&(t[r]==="u"?(r++,r++,s=String.fromCodePoint(parseInt(t.slice(r,r=t.indexOf("}",r)),16)),r++):kt[t[r]]?i=kt[t[r++]]:isNaN(i=parseInt(t[r]+t[r+1],16))?s+=t[r]:(r++,r++)),i!=null?(a(),e.push(i)):l+=s;return a(),e.valueOf=()=>t,e},Mt=t=>Jt.decode(new Uint8Array(nt(t)));var p=(t,e=[])=>{if(t==null)return e;if(typeof t=="string"&&(t=/[_x]/i.test(t)?BigInt(t.replaceAll("_","")):W.parse(t)),typeof t=="bigint"){for(;;){let i=Number(t&0x7Fn);if(t>>=7n,t===0n){e.push(i);break}e.push(i|128)}return e}let r=t&127;return t>>>=7,t===0?(e.push(r),e):(e.push(r|128),p(t,e))};function Zt(t){let e=[];for(let r=0;r<5;r++){let i=t&127;t>>>=7,r<4&&(i|=128),e.push(i)}return e}function W(t,e=[]){for(typeof t=="string"&&(t=W.parse(t));;){let r=Number(t&127);if(t>>=7,t===0&&!(r&64)||t===-1&&r&64){e.push(r);break}e.push(r|128)}return e}var zt=t=>!Nt.test(t)&&Bt.test(t=t.replaceAll("_",""))?t:v(`Bad int ${t}`),Qt=W,te=W;W.parse=t=>(t=parseInt(zt(t)),(t<-2147483648||t>4294967295)&&v("i32 constant out of range"),t);function Z(t,e=[]){for(typeof t=="string"?t=Z.parse(t):typeof t=="bigint"&&t>0x7fffffffffffffffn&&(t=t-0x10000000000000000n);;){let r=Number(t&0x7Fn);if(t>>=7n,t===0n&&!(r&64)||t===-1n&&r&64){e.push(r);break}e.push(r|128)}return e}Z.parse=t=>(t=zt(t),t=t[0]==="-"?-BigInt(t.slice(1)):BigInt(t),(t<-0x8000000000000000n||t>0xffffffffffffffffn)&&v("i64 constant out of range"),q.setBigInt64(0,t),q.getBigInt64(0));var q=new DataView(new Float64Array(1).buffer),ee=2147483648,re=2139095040;function Q(t,e,r){return typeof t=="string"&&~(r=t.indexOf("nan:"))?(e=W.parse(t.slice(r+4)),e|=re,t[0]==="-"&&(e|=ee),q.setInt32(0,e)):(e=typeof t=="string"?Q.parse(t):t,q.setFloat32(0,e)),[q.getUint8(3),q.getUint8(2),q.getUint8(1),q.getUint8(0)]}var ie=0x8000000000000000n,se=0x7ff0000000000000n;function J(t,e,r){return typeof t=="string"&&~(r=t.indexOf("nan:"))?(e=Z.parse(t.slice(r+4)),e|=se,t[0]==="-"&&(e|=ie),q.setBigInt64(0,e)):(e=typeof t=="string"?J.parse(t):t,q.setFloat64(0,e)),[q.getUint8(7),q.getUint8(6),q.getUint8(5),q.getUint8(4),q.getUint8(3),q.getUint8(2),q.getUint8(1),q.getUint8(0)]}J.parse=(t,e=Number.MAX_VALUE)=>{t=t.replaceAll("_","");let r=1;if(t[0]==="-"?(r=-1,t=t.slice(1)):t[0]==="+"&&(t=t.slice(1)),t[1]==="x"){let[i,s="0"]=t.split(/p/i),[l,a=""]=i.split("."),n=a.length??0,c=parseInt(l);isNaN(c)&&v();let f=a?parseInt("0x"+a)/16**n:0;s=parseInt(s,10);let o=r*(c+f)*2**s;return o=Math.max(-e,Math.min(e,o)),o}return t.includes("nan")?r<0?NaN:NaN:t.includes("inf")?r*(1/0):r*parseFloat(t)};Q.parse=t=>J.parse(t,34028234663852886e22);var st=["unreachable","nop","block block","loop block","if block","else null","then null",,"throw tagidx",,"throw_ref","end end","br labelidx","br_if labelidx","br_table br_table","return","call funcidx","call_indirect call_indirect","return_call funcidx","return_call_indirect call_indirect","call_ref typeidx","return_call_ref typeidx",,,,,"drop","select select","",,,"try_table try_table","local.get localidx","local.set localidx","local.tee localidx","global.get globalidx","global.set globalidx","table.get tableidx","table.set tableidx",,"i32.load memarg","i64.load memarg","f32.load memarg","f64.load memarg","i32.load8_s memarg","i32.load8_u memarg","i32.load16_s memarg","i32.load16_u memarg","i64.load8_s memarg","i64.load8_u memarg","i64.load16_s memarg","i64.load16_u memarg","i64.load32_s memarg","i64.load32_u memarg","i32.store memarg","i64.store memarg","f32.store memarg","f64.store memarg","i32.store8 memarg","i32.store16 memarg","i64.store8 memarg","i64.store16 memarg","i64.store32 memarg","memory.size opt_memory","memory.grow opt_memory","i32.const i32","i64.const i64","f32.const f32","f64.const f64","i32.eqz","i32.eq","i32.ne","i32.lt_s","i32.lt_u","i32.gt_s","i32.gt_u","i32.le_s","i32.le_u","i32.ge_s","i32.ge_u","i64.eqz","i64.eq","i64.ne","i64.lt_s","i64.lt_u","i64.gt_s","i64.gt_u","i64.le_s","i64.le_u","i64.ge_s","i64.ge_u","f32.eq","f32.ne","f32.lt","f32.gt","f32.le","f32.ge","f64.eq","f64.ne","f64.lt","f64.gt","f64.le","f64.ge","i32.clz","i32.ctz","i32.popcnt","i32.add","i32.sub","i32.mul","i32.div_s","i32.div_u","i32.rem_s","i32.rem_u","i32.and","i32.or","i32.xor","i32.shl","i32.shr_s","i32.shr_u","i32.rotl","i32.rotr","i64.clz","i64.ctz","i64.popcnt","i64.add","i64.sub","i64.mul","i64.div_s","i64.div_u","i64.rem_s","i64.rem_u","i64.and","i64.or","i64.xor","i64.shl","i64.shr_s","i64.shr_u","i64.rotl","i64.rotr","f32.abs","f32.neg","f32.ceil","f32.floor","f32.trunc","f32.nearest","f32.sqrt","f32.add","f32.sub","f32.mul","f32.div","f32.min","f32.max","f32.copysign","f64.abs","f64.neg","f64.ceil","f64.floor","f64.trunc","f64.nearest","f64.sqrt","f64.add","f64.sub","f64.mul","f64.div","f64.min","f64.max","f64.copysign","i32.wrap_i64","i32.trunc_f32_s","i32.trunc_f32_u","i32.trunc_f64_s","i32.trunc_f64_u","i64.extend_i32_s","i64.extend_i32_u","i64.trunc_f32_s","i64.trunc_f32_u","i64.trunc_f64_s","i64.trunc_f64_u","f32.convert_i32_s","f32.convert_i32_u","f32.convert_i64_s","f32.convert_i64_u","f32.demote_f64","f64.convert_i32_s","f64.convert_i32_u","f64.convert_i64_s","f64.convert_i64_u","f64.promote_f32","i32.reinterpret_f32","i64.reinterpret_f64","f32.reinterpret_i32","f64.reinterpret_i64","i32.extend8_s","i32.extend16_s","i64.extend8_s","i64.extend16_s","i64.extend32_s",,,,,,,,,,,,"ref.null ref_null","ref.is_null","ref.func funcidx","ref.eq","ref.as_non_null","br_on_null labelidx","br_on_non_null labelidx",,,,,,,,,,"cont.new typeidx","cont.bind cont_bind","suspend tagidx","resume resume","resume_throw resume_throw","resume_throw_ref resume_throw_ref","switch switch_cont",,,,,,,,,,,,,,,,,,,,,["struct.new typeidx","struct.new_default typeidx","struct.get typeidx_field","struct.get_s typeidx_field","struct.get_u typeidx_field","struct.set typeidx_field","array.new typeidx","array.new_default typeidx","array.new_fixed typeidx_multi","array.new_data typeidx_dataidx","array.new_elem typeidx_elemidx","array.get typeidx","array.get_s typeidx","array.get_u typeidx","array.set typeidx","array.len","array.fill typeidx","array.copy typeidx_typeidx","array.init_data typeidx_dataidx","array.init_elem typeidx_elemidx","ref.test reftype","","ref.cast reftype","","br_on_cast reftype2","br_on_cast_fail reftype2","any.convert_extern","extern.convert_any","ref.i31","i31.get_s","i31.get_u",,"struct.new_desc typeidx","struct.new_default_desc typeidx","ref.get_desc typeidx","ref.cast_desc_eq reftype",,"br_on_cast_desc_eq reftype2","br_on_cast_desc_eq_fail reftype2"],["i32.trunc_sat_f32_s","i32.trunc_sat_f32_u","i32.trunc_sat_f64_s","i32.trunc_sat_f64_u","i64.trunc_sat_f32_s","i64.trunc_sat_f32_u","i64.trunc_sat_f64_s","i64.trunc_sat_f64_u","memory.init dataidx_memoryidx","data.drop dataidx","memory.copy memoryidx_memoryidx","memory.fill memoryidx?","table.init reversed","elem.drop elemidx","table.copy tableidx_tableidx","table.grow tableidx","table.size tableidx","table.fill tableidx",,"i64.add128","i64.sub128","i64.mul_wide_s","i64.mul_wide_u"],["v128.load memarg","v128.load8x8_s memarg","v128.load8x8_u memarg","v128.load16x4_s memarg","v128.load16x4_u memarg","v128.load32x2_s memarg","v128.load32x2_u memarg","v128.load8_splat memarg","v128.load16_splat memarg","v128.load32_splat memarg","v128.load64_splat memarg","v128.store memarg","v128.const v128const","i8x16.shuffle shuffle","i8x16.swizzle","i8x16.splat","i16x8.splat","i32x4.splat","i64x2.splat","f32x4.splat","f64x2.splat","i8x16.extract_lane_s laneidx","i8x16.extract_lane_u laneidx","i8x16.replace_lane laneidx","i16x8.extract_lane_s laneidx","i16x8.extract_lane_u laneidx","i16x8.replace_lane laneidx","i32x4.extract_lane laneidx","i32x4.replace_lane laneidx","i64x2.extract_lane laneidx","i64x2.replace_lane laneidx","f32x4.extract_lane laneidx","f32x4.replace_lane laneidx","f64x2.extract_lane laneidx","f64x2.replace_lane laneidx","i8x16.eq","i8x16.ne","i8x16.lt_s","i8x16.lt_u","i8x16.gt_s","i8x16.gt_u","i8x16.le_s","i8x16.le_u","i8x16.ge_s","i8x16.ge_u","i16x8.eq","i16x8.ne","i16x8.lt_s","i16x8.lt_u","i16x8.gt_s","i16x8.gt_u","i16x8.le_s","i16x8.le_u","i16x8.ge_s","i16x8.ge_u","i32x4.eq","i32x4.ne","i32x4.lt_s","i32x4.lt_u","i32x4.gt_s","i32x4.gt_u","i32x4.le_s","i32x4.le_u","i32x4.ge_s","i32x4.ge_u","f32x4.eq","f32x4.ne","f32x4.lt","f32x4.gt","f32x4.le","f32x4.ge","f64x2.eq","f64x2.ne","f64x2.lt","f64x2.gt","f64x2.le","f64x2.ge","v128.not","v128.and","v128.andnot","v128.or","v128.xor","v128.bitselect","v128.any_true","v128.load8_lane memlane","v128.load16_lane memlane","v128.load32_lane memlane","v128.load64_lane memlane","v128.store8_lane memlane","v128.store16_lane memlane","v128.store32_lane memlane","v128.store64_lane memlane","v128.load32_zero memarg","v128.load64_zero memarg","f32x4.demote_f64x2_zero","f64x2.promote_low_f32x4","i8x16.abs","i8x16.neg","i8x16.popcnt","i8x16.all_true","i8x16.bitmask","i8x16.narrow_i16x8_s","i8x16.narrow_i16x8_u","f32x4.ceil","f32x4.floor","f32x4.trunc","f32x4.nearest","i8x16.shl","i8x16.shr_s","i8x16.shr_u","i8x16.add","i8x16.add_sat_s","i8x16.add_sat_u","i8x16.sub","i8x16.sub_sat_s","i8x16.sub_sat_u","f64x2.ceil","f64x2.floor","i8x16.min_s","i8x16.min_u","i8x16.max_s","i8x16.max_u","f64x2.trunc","i8x16.avgr_u","i16x8.extadd_pairwise_i8x16_s","i16x8.extadd_pairwise_i8x16_u","i32x4.extadd_pairwise_i16x8_s","i32x4.extadd_pairwise_i16x8_u","i16x8.abs","i16x8.neg","i16x8.q15mulr_sat_s","i16x8.all_true","i16x8.bitmask","i16x8.narrow_i32x4_s","i16x8.narrow_i32x4_u","i16x8.extend_low_i8x16_s","i16x8.extend_high_i8x16_s","i16x8.extend_low_i8x16_u","i16x8.extend_high_i8x16_u","i16x8.shl","i16x8.shr_s","i16x8.shr_u","i16x8.add","i16x8.add_sat_s","i16x8.add_sat_u","i16x8.sub","i16x8.sub_sat_s","i16x8.sub_sat_u","f64x2.nearest","i16x8.mul","i16x8.min_s","i16x8.min_u","i16x8.max_s","i16x8.max_u",,"i16x8.avgr_u","i16x8.extmul_low_i8x16_s","i16x8.extmul_high_i8x16_s","i16x8.extmul_low_i8x16_u","i16x8.extmul_high_i8x16_u","i32x4.abs","i32x4.neg",,"i32x4.all_true","i32x4.bitmask",,,"i32x4.extend_low_i16x8_s","i32x4.extend_high_i16x8_s","i32x4.extend_low_i16x8_u","i32x4.extend_high_i16x8_u","i32x4.shl","i32x4.shr_s","i32x4.shr_u","i32x4.add",,,"i32x4.sub",,,,"i32x4.mul","i32x4.min_s","i32x4.min_u","i32x4.max_s","i32x4.max_u","i32x4.dot_i16x8_s",,"i32x4.extmul_low_i16x8_s","i32x4.extmul_high_i16x8_s","i32x4.extmul_low_i16x8_u","i32x4.extmul_high_i16x8_u","i64x2.abs","i64x2.neg",,"i64x2.all_true","i64x2.bitmask",,,"i64x2.extend_low_i32x4_s","i64x2.extend_high_i32x4_s","i64x2.extend_low_i32x4_u","i64x2.extend_high_i32x4_u","i64x2.shl","i64x2.shr_s","i64x2.shr_u","i64x2.add",,,"i64x2.sub",,,,"i64x2.mul","i64x2.eq","i64x2.ne","i64x2.lt_s","i64x2.gt_s","i64x2.le_s","i64x2.ge_s","i64x2.extmul_low_i32x4_s","i64x2.extmul_high_i32x4_s","i64x2.extmul_low_i32x4_u","i64x2.extmul_high_i32x4_u","f32x4.abs","f32x4.neg",,"f32x4.sqrt","f32x4.add","f32x4.sub","f32x4.mul","f32x4.div","f32x4.min","f32x4.max","f32x4.pmin","f32x4.pmax","f64x2.abs","f64x2.neg",,"f64x2.sqrt","f64x2.add","f64x2.sub","f64x2.mul","f64x2.div","f64x2.min","f64x2.max","f64x2.pmin","f64x2.pmax","i32x4.trunc_sat_f32x4_s","i32x4.trunc_sat_f32x4_u","f32x4.convert_i32x4_s","f32x4.convert_i32x4_u","i32x4.trunc_sat_f64x2_s_zero","i32x4.trunc_sat_f64x2_u_zero","f64x2.convert_low_i32x4_s","f64x2.convert_low_i32x4_u","i8x16.relaxed_swizzle","i32x4.relaxed_trunc_f32x4_s","i32x4.relaxed_trunc_f32x4_u","i32x4.relaxed_trunc_f64x2_s_zero","i32x4.relaxed_trunc_f64x2_u_zero","f32x4.relaxed_madd","f32x4.relaxed_nmadd","f64x2.relaxed_madd","f64x2.relaxed_nmadd","i8x16.relaxed_laneselect","i16x8.relaxed_laneselect","i32x4.relaxed_laneselect","i64x2.relaxed_laneselect","f32x4.relaxed_min","f32x4.relaxed_max","f64x2.relaxed_min","f64x2.relaxed_max","i16x8.relaxed_q15mulr_s","i16x8.relaxed_dot_i8x16_i7x16_s","i32x4.relaxed_dot_i8x16_i7x16_add_s"],["memory.atomic.notify memarg","memory.atomic.wait32 memarg","memory.atomic.wait64 memarg","atomic.fence opt_memory",,,,,,,,,,,,,"i32.atomic.load memarg","i64.atomic.load memarg","i32.atomic.load8_u memarg","i32.atomic.load16_u memarg","i64.atomic.load8_u memarg","i64.atomic.load16_u memarg","i64.atomic.load32_u memarg","i32.atomic.store memarg","i64.atomic.store memarg","i32.atomic.store8 memarg","i32.atomic.store16 memarg","i64.atomic.store8 memarg","i64.atomic.store16 memarg","i64.atomic.store32 memarg","i32.atomic.rmw.add memarg","i64.atomic.rmw.add memarg","i32.atomic.rmw8.add_u memarg","i32.atomic.rmw16.add_u memarg","i64.atomic.rmw8.add_u memarg","i64.atomic.rmw16.add_u memarg","i64.atomic.rmw32.add_u memarg","i32.atomic.rmw.sub memarg","i64.atomic.rmw.sub memarg","i32.atomic.rmw8.sub_u memarg","i32.atomic.rmw16.sub_u memarg","i64.atomic.rmw8.sub_u memarg","i64.atomic.rmw16.sub_u memarg","i64.atomic.rmw32.sub_u memarg","i32.atomic.rmw.and memarg","i64.atomic.rmw.and memarg","i32.atomic.rmw8.and_u memarg","i32.atomic.rmw16.and_u memarg","i64.atomic.rmw8.and_u memarg","i64.atomic.rmw16.and_u memarg","i64.atomic.rmw32.and_u memarg","i32.atomic.rmw.or memarg","i64.atomic.rmw.or memarg","i32.atomic.rmw8.or_u memarg","i32.atomic.rmw16.or_u memarg","i64.atomic.rmw8.or_u memarg","i64.atomic.rmw16.or_u memarg","i64.atomic.rmw32.or_u memarg","i32.atomic.rmw.xor memarg","i64.atomic.rmw.xor memarg","i32.atomic.rmw8.xor_u memarg","i32.atomic.rmw16.xor_u memarg","i64.atomic.rmw8.xor_u memarg","i64.atomic.rmw16.xor_u memarg","i64.atomic.rmw32.xor_u memarg","i32.atomic.rmw.xchg memarg","i64.atomic.rmw.xchg memarg","i32.atomic.rmw8.xchg_u memarg","i32.atomic.rmw16.xchg_u memarg","i64.atomic.rmw8.xchg_u memarg","i64.atomic.rmw16.xchg_u memarg","i64.atomic.rmw32.xchg_u memarg","i32.atomic.rmw.cmpxchg memarg","i64.atomic.rmw.cmpxchg memarg","i32.atomic.rmw8.cmpxchg_u memarg","i32.atomic.rmw16.cmpxchg_u memarg","i64.atomic.rmw8.cmpxchg_u memarg","i64.atomic.rmw16.cmpxchg_u memarg","i64.atomic.rmw32.cmpxchg_u memarg"]],U={custom:0,type:1,import:2,func:3,table:4,memory:5,tag:13,global:6,export:7,start:8,elem:9,datacount:12,code:10,data:11},T={i8:120,i16:119,i32:127,i64:126,f32:125,f64:124,void:64,v128:123,exn:105,noexn:116,nofunc:115,noextern:114,none:113,func:112,extern:111,any:110,eq:109,i31:108,struct:107,array:106,cont:104,nocont:117,nullfuncref:115,nullexternref:114,nullexnref:116,nullref:113,funcref:112,externref:111,exnref:105,anyref:110,eqref:109,i31ref:108,structref:107,arrayref:106,contref:104,nocontref:117,ref:100,refnull:99,sub:80,subfinal:79,rec:78},V={func:96,struct:95,array:94,cont:93,sub:80,subfinal:79,rec:78},ht={func:0,table:1,memory:2,global:3,tag:4};var D=t=>{let e=0,r=[],i="",s=0,l=0,a=()=>i&&(r.push(i),i=""),n=c=>{r.loc=c;for(let f,o,u;e<t.length;)if(f=t.charCodeAt(e),s===34)i+=t[e++],f===92?i+=t[e++]:f===34&&(a(),s=0);else if(s>59)f===40&&t.charCodeAt(e+1)===59?(s++,i+=t[e++]+t[e++]):f===59&&t.charCodeAt(e+1)===41?(i+=t[e++]+t[e++],--s===59&&(a(),s=0)):i+=t[e++];else if(s<0)f===10||f===13?(i+=t[e++],a(),s=0):i+=t[e++];else if(f===34)i!=="$"&&a(),s=34,i+=t[e++];else if(f===40&&t.charCodeAt(e+1)===59)a(),s=60,i=t[e++]+t[e++];else if(f===59&&t.charCodeAt(e+1)===59)a(),s=-1,i=t[e++]+t[e++];else if(f===40&&t.charCodeAt(e+1)===64)a(),u=e,e+=2,i="@",l++,(o=r).push(r=[]),n(u),r=o;else if(f===40)a(),u=e++,l++,(o=r).push(r=[]),n(u),r=o;else{if(f===41)return a(),e++,l--;f<=32?(a(),e++):i+=t[e++]}s<0&&a(),a()};return n(0),s===34&&v("Unclosed quote",e),s>59&&v("Unclosed block comment",e),l>0&&v("Unclosed parenthesis",e),e<t.length&&v("Unexpected closing parenthesis",e),r.length>1?r:r[0]||[]};var Et=(t,e)=>Array.isArray(t)?t[0]?.[0]==="@"&&t[0]!=="@custom"&&!t[0]?.startsWith?.("@metadata.code.")?null:(e=t.map(Et).filter(r=>r!=null),e.loc=t.loc,e.length===1&&e[0]?.[0]==="module"?e[0]:e):typeof t!="string"?t:t[0]===";"||t[1]===";"?null:t[0]==="$"&&t[1]==='"'?t.includes("\\")?"$"+Mt(t.slice(1)):"$"+t.slice(2,-1):t[0]==='"'?nt(t):t;function rt(t){typeof t=="string"?(v.src=t,t=D(t)||[]):v.src="",v.loc=0,t=Et(t)||[];let e=0;if(t[0]==="module"?(e++,F(t[e])&&e++):typeof t[0]=="string"&&(t=[t]),t[e]==="binary")return Uint8Array.from(t.slice(++e).flat());if(t[e]==="quote")return rt(t.slice(++e).map(l=>l.valueOf().slice(1,-1)).flat().join(""));t=t.flatMap((l,a)=>{if(a<e||!Array.isArray(l)||l[0]!=="import")return[l];let[,n,...c]=l;if(!c.some(o=>Array.isArray(o)&&o[0]==="item"))return[l];if(Array.isArray(c.at(-1))&&c.at(-1)[0]!=="item"){let o=c.at(-1);return c.slice(0,-1).filter(u=>u[0]==="item").map(([,u])=>["import",n,u,o])}return c.filter(o=>o[0]==="item").map(([,o,u])=>["import",n,o,u])});let r=[];for(let l in U)(r[U[l]]=r[l]=[]).name=l;r.metadata={},t.slice(e).filter(l=>{if(!Array.isArray(l)){let c=v.loc,f=v.src,o;for(;(c=f.indexOf(l,c))>=0;){if(o=f.charCodeAt(c-1),c>0&&(o>47&&o<58||o>64&&o<91||o>96&&o<123||o===95||o===36)){c++;continue}if(o=f.charCodeAt(c+l.length),o>47&&o<58||o>64&&o<91||o>96&&o<123||o===95){c++;continue}break}c>=0&&(v.loc=c),v(`Unexpected token ${l}`)}let[a,...n]=l;if(v.loc=l.loc,a==="@custom")r.custom.push(n);else if(a==="rec")for(let c=0;c<n.length;c++){let[,...f]=n[c];dt(f,r.type);let o=[];for(;f[0]?.[0]==="descriptor"||f[0]?.[0]==="describes";)o.push(f.shift());(f=Ut(f,r)).push(c?!0:[r.type.length,n.length]),o.length&&(f.desc=f.desc?[...o,...f.desc]:o),r.type.push(f)}else if(a==="type"){dt(n,r.type);let c=[];for(;n[0]?.[0]==="descriptor"||n[0]?.[0]==="describes";)c.push(n.shift());let f=Ut(n,r);c.length&&(f.desc=f.desc?[...c,...f.desc]:c),r.type.push(f)}else if(a==="start"||a==="export")r[a].push(n);else return!0}).forEach(l=>{let[a,...n]=l;v.loc=l.loc;let c;a==="import"&&([a,...n]=(c=n).pop());let f=r[a];for(f||v(`Unknown section ${a}`),dt(n,f);n[0]?.[0]==="export";)r.export.push([n.shift()[1],[a,f?.length]]);if(n[0]?.[0]==="import"&&([,...c]=n.shift()),a==="table"){let o=n[0]==="i64",u=o?1:0;if(n[u+1]?.[0]==="elem"){let[d,[,...h]]=[n[u],n[u+1]];n=o?["i64",h.length,h.length,d]:[h.length,h.length,d],r.elem.push([["table",f.length],["offset",[o?"i64.const":"i32.const",o?0n:0]],d,...h])}}else if(a==="memory"){let o=n[0]==="i64",u=o?1:0;if(n[u]?.[0]==="data"){let d=n.find(y=>Array.isArray(y)&&y[0]==="pagesize")?.[1]??65536,[,...h]=n.splice(u,1)[0],$=""+Math.ceil(h.reduce((y,k)=>y+k.length,0)/d);r.data.push([["memory",f.length],[o?"i64.const":"i32.const",o?0n:0],...h]),n=o?["i64",$,$]:[$,$]}}else if(a==="func"){let[o,u,d]=_t(n,r);o??=mt(u,d,r),!c&&r.code.push([[o,u,d],...Y(n,r)]),n=[["type",o]]}else if(a==="tag"){let[o,u]=_t(n,r);o??=mt(u,[],r),n=[["type",o]]}c&&(r.import.push([...c,[a,...n]]),n=null),f.push(n)});let i=(l,a=!0)=>{let n=r[l].filter(Boolean).map(c=>qt[l](c,r)).filter(Boolean);return l===U.custom?n.flatMap(c=>[l,...N(c)]):n.length?[l,...N(a?N(n):n)]:[]},s=()=>{let l=[];for(let a in r.metadata){let n=N(nt(`"metadata.code.${a}"`)),c=N(r.metadata[a].map(([f,o])=>[...p(f),...N(o.map(([u,d])=>[...p(u),...N(d)]))]));l.push(0,...N([...n,...c]))}return l};return Uint8Array.from([0,97,115,109,1,0,0,0,...i(U.custom),...i(U.type),...i(U.import),...i(U.func),...i(U.table),...i(U.memory),...i(U.tag),...i(U.global),...i(U.export),...i(U.start,!1),...i(U.elem),...i(U.datacount,!1),...i(U.code),...s(),...i(U.data)])}var L=t=>t?.[0]==="$"||!isNaN(t),F=t=>t?.[0]==="$",bt=t=>t?.[0]==="a"||t?.[0]==="o";function Y(t,e){let r=[];for(t=[...t];t.length;){let i=t.shift();if(typeof i=="string")if(r.push(i),i==="block"||i==="if"||i==="loop")F(t[0])&&r.push(t.shift()),r.push(at(t,e));else if(i==="else"||i==="end")F(t[0])&&t.shift();else if(i==="select")r.push(pt(t)[1]);else if(i.endsWith("call_indirect")){let s=L(t[0])?t.shift():0,[l,a,n]=_t(t,e);r.push(s,["type",l??mt(a,n,e)])}else i==="table.init"?r.push(L(t[1])?t.shift():0,t.shift()):i==="table.copy"||i==="memory.copy"?r.push(L(t[0])?t.shift():0,L(t[0])?t.shift():0):i.startsWith("table.")?r.push(L(t[0])?t.shift():0):i==="memory.init"?(r.push(...L(t[1])?[t.shift(),t.shift()].reverse():[t.shift(),0]),e.datacount&&(e.datacount[0]=!0)):i==="data.drop"||i==="array.new_data"||i==="array.init_data"?(i==="data.drop"&&r.push(t.shift()),e.datacount&&(e.datacount[0]=!0)):(i.startsWith("memory.")||i.endsWith("load")||i.endsWith("store"))&&L(t[0])&&r.push(t.shift());else if(Array.isArray(i)){let s=i[0];if(i.loc!=null&&(v.loc=i.loc),s?.startsWith?.("@metadata.code.")){let a=s.slice(15);r.push(["@metadata",a,i[1]]);continue}if(typeof s!="string"||!Array.isArray(st[s])){r.push(i);continue}let l=i.slice(1);if(s==="block"||s==="loop")r.push(s),F(l[0])&&r.push(l.shift()),r.push(at(l,e),...Y(l,e),"end");else if(s==="if"){let a=[],n=[];l.at(-1)?.[0]==="else"&&(n=Y(l.pop().slice(1),e)),l.at(-1)?.[0]==="then"&&(a=Y(l.pop().slice(1),e));let c=[s];F(l[0])&&c.push(l.shift()),c.push(at(l,e)),r.push(...Y(l,e),...c,...a),n.length&&r.push("else",...n),r.push("end")}else if(s==="try_table"){for(r.push(s),F(l[0])&&r.push(l.shift()),r.push(at(l,e));l[0]?.[0]==="catch"||l[0]?.[0]==="catch_ref"||l[0]?.[0]==="catch_all"||l[0]?.[0]==="catch_all_ref";)r.push(l.shift());r.push(...Y(l,e),"end")}else{let a=[];for(;l.length&&(!Array.isArray(l[0])||"type,param,result,ref,exact,on".includes(l[0][0]));)a.push(l.shift());r.push(...Y(l,e),s,...a),t.unshift(...r.splice(r.length-1-a.length))}}else r.push(i)}return r}var mt=(t,e,r,i="$"+t+">"+e)=>(r.type[i]??=r.type.push(["func",[t,e]])-1,i),At=(t,e)=>{let r=[];for(;t[0]?.[0]===e;){let[,...i]=t.shift(),s=F(i[0])&&i.shift();s&&(s in r?(()=>{throw Error(`Duplicate ${e} ${s}`)})():r[s]=r.length),r.push(...i)}return r},pt=t=>{let e=At(t,"param"),r=At(t,"result");if(t[0]?.[0]==="param")throw Error("Unexpected param");return[e,r]},_t=(t,e)=>{if(t[0]?.[0]!=="type")return[,...pt(t)];let[,r]=t.shift(),[i,s]=pt(t),l=e.type[typeof r=="string"&&isNaN(r)?e.type[r]:+r];if(!l)throw Error(`Unknown type ${r}`);if((i.length||s.length)&&l[1].join(">")!==i+">"+s)throw Error(`Type ${r} mismatch`);return[r,...l[1]]},at=(t,e)=>{let[r,i,s]=_t(t,e);if(!(!i.length&&!s.length))return!i.length&&s.length===1?["result",...s]:["type",r??mt(i,s,e)]},dt=(t,e)=>{let r=F(t[0])&&t.shift();return r&&(r in e?v(`Duplicate ${e.name} ${r}`):e[r]=e.length),r},Ut=([t],e)=>{let r="subfinal",i=[],s,l=[];t[0]==="sub"&&(r=t.shift(),t[0]==="final"&&(r+=t.shift()),t=(i=t).pop(),i=i.filter(n=>Array.isArray(n)&&(n[0]==="descriptor"||n[0]==="describes")?(l.push(n),!1):!0)),[s,...t]=t,s==="func"?(t=pt(t),e.type["$"+t.join(">")]??=e.type.length):s==="struct"?t=At(t,"field"):s==="array"&&([t]=t);let a=[s,t,r,i];return l.length&&(a.desc=l),a},qt=[([t,...e],r)=>{let i=e;return(e[0]?.[0]==="before"||e[0]?.[0]==="after")&&(i=e.slice(1)),[...N(t),...i.flat()]},(t,e)=>{let[r,i,s,l,a]=t;if(a===!0)return;let n=(t.desc??[]).flatMap(([f,o])=>[f==="descriptor"?77:76,...p(x(o,e.type))]),c=(f,o)=>f==="func"?[V.func,...N(o[0].map(u=>C(u,e))),...N(o[1].map(u=>C(u,e)))]:f==="array"?[V.array,...ft(o,e)]:f==="struct"?[V.struct,...N(o.map(u=>ft(u,e)))]:f==="cont"?[V.cont,...p(x(o[0]??o,e.type))]:[V[f]];if(a){let[f,o]=a,u=Array.from({length:o},(d,h)=>{let $=e.type[f+h],y=$.slice(0,4);return $.desc&&(y.desc=$.desc),qt[U.type](y,e)});return[V.rec,...N(u)]}else if(s==="sub"||l?.length)return[V[s],...N(l.map(f=>x(f,e.type))),...n,...c(r,i)];return[...n,...c(r,i)]},([t,e,[r,...i]],s)=>{let l,a=ht[r];if(r==="func"){i[0]==="exact"&&i.shift()&&(a=32);let[[,c]]=i;l=p(x(c,s.type))}else if(r==="tag"){let[[,n]]=i;l=[0,...p(x(n,s.type))]}else r==="memory"?l=ot(i):r==="global"?l=ft(i[0],s):r==="table"?l=[...C(i.pop(),s),...ot(i)]:v(`Unknown kind ${r}`);return[...N(t),...N(e),a,...l]},([[,t]],e)=>p(x(t,e.type)),(t,e)=>{let r=ot(t),i=C(t.shift(),e),[s]=t;return s?[64,0,...i,...r,...et(s,e)]:[...i,...r]},(t,e)=>ot(t),([t,e],r)=>[...ft(t,r),...et(e,r)],([t,[e,r]],i)=>[...N(t),ht[e],...p(x(r,i[e]))],([t],e)=>p(x(t,e.func)),(t,e)=>{let r=0,i=0,s=0,l=0,a,n,c;t[0]==="declare"&&(t.shift(),i=1),t[0]?.[0]==="table"?([,a]=t.shift(),a=x(a,e.table)):(typeof t[0]=="string"||typeof t[0]=="number")&&(t[1]?.[0]==="offset"||Array.isArray(t[1])&&t[1][0]!=="item"&&!t[1][0]?.startsWith("ref"))&&(a=x(t.shift(),e.table)),t[0]?.[0]==="offset"||Array.isArray(t[0])&&t[0][0]!=="item"&&!t[0][0].startsWith("ref")?(n=t.shift(),n[0]==="offset"&&([,n]=n),n=et(n,e)):i||(r=1),T[t[0]]||t[0]?.[0]==="ref"?c=C(t.shift(),e):t[0]==="func"?c=[T[t.shift()]]:c=[T.func],t=t.map(o=>(o[0]==="item"&&(o=o.length===3&&o[1]==="ref.func"?o[2]:o[1]),o[0]==="ref.func"&&([,o]=o),typeof o!="string"&&(s=1),o)),c[0]!==T.funcref&&(l=1,s=1);let f=s<<2|(r||i?i:!!a||l)<<1|(r||i);return[f,...f===0?n:f===1?[0]:f===2?[...p(a||0),...n,0]:f===3?[0]:f===4?n:f===5?c:f===6?[...p(a||0),...n,...c]:c,...N(t.map(s?o=>et(typeof o=="string"?["ref.func",o]:o,e):o=>p(x(o,e.func))))]},(t,e)=>{let[r,i]=t.shift();i||([,[i]]=e.type[x(r,e.type)]),e.local=Object.create(i),e.block=[],e.local.name="local",e.block.name="block",e._codeIdx===void 0&&(e._codeIdx=0);let s=e._codeIdx++;for(;t[0]?.[0]==="local";){let[,...c]=t.shift();if(F(c[0])){let f=c.shift();f in e.local?v(`Duplicate local ${f}`):e.local[f]=e.local.length}e.local.push(...c)}e.meta={};let l=Ft(t,e),a=e.import.filter(c=>c[2][0]==="func").length+s;for(let c in e.meta)((e.metadata??={})[c]??=[]).push([a,e.meta[c]]);let n=e.local.slice(i.length).reduce((c,f)=>(f==c[c.length-1]?.[1]?c[c.length-1][0]++:c.push([1,f]),c),[]);return e.local=e.block=e.meta=null,N([...N(n.map(([c,f])=>[...p(c),...C(f,e)])),...l])},(t,e)=>{let r,i=0;return t[0]?.[0]==="memory"?([,i]=t.shift(),i=x(i,e.memory)):(typeof t[0]=="string"||typeof t[0]=="number")&&(t[1]?.[0]==="offset"||Array.isArray(t[1])&&typeof t[1][0]=="string")&&(i=x(t.shift(),e.memory)),Array.isArray(t[0])&&typeof t[0]?.[0]=="string"&&(r=t.shift(),r[0]==="offset"&&([,r]=r),r??v("Bad offset",r)),[...i?[2,...p(i),...et(r,e)]:r?[0,...et(r,e)]:[1],...N(t.flatMap(s=>ae(s)??[...s]))]},(t,e)=>p(e.data.length),([[,t]],e)=>[0,...p(x(t,e.type))]],C=(t,e)=>t[0]==="ref"?t[1]=="null"?Array.isArray(t[2])&&t[2][0]==="exact"?[T.refnull,98,...p(x(t[2][1],e.type))]:T[t[2]]?[T[t[2]]]:[T.refnull,...p(x(t[t.length-1],e.type))]:Array.isArray(t[1])&&t[1][0]==="exact"?[T.ref,98,...p(x(t[1][1],e.type))]:[T.ref,...p(T[t[t.length-1]]||x(t[t.length-1],e.type))]:[T[t]??v(`Unknown type ${t}`)],ft=(t,e,r=t[0]==="mut"?1:0)=>[...C(r?t[1]:t,e),r],St={null:()=>[],reversed:(t,e)=>{let r=t.shift(),i=t.shift();return[...p(x(i,e.elem)),...p(x(r,e.table))]},block:(t,e)=>{e.block.push(1),F(t[0])&&(e.block[t.shift()]=e.block.length);let r=t.shift();return r?r[0]==="result"?C(r[1],e):p(x(r[1],e.type)):[T.void]},try_table:(t,e)=>{F(t[0])&&(e.block[t.shift()]=e.block.length+1);let r=t.shift(),i=r?r[0]==="result"?C(r[1],e):p(x(r[1],e.type)):[T.void],s=[],l=0;for(;t[0]?.[0]==="catch"||t[0]?.[0]==="catch_ref"||t[0]?.[0]==="catch_all"||t[0]?.[0]==="catch_all_ref";){let a=t.shift(),n=a[0]==="catch"?0:a[0]==="catch_ref"?1:a[0]==="catch_all"?2:3;n<=1?s.push(n,...p(x(a[1],e.tag)),...p(H(a[2],e.block))):s.push(n,...p(H(a[1],e.block))),l++}return e.block.push(1),[...i,...p(l),...s]},end:(t,e)=>(e.block.pop(),[]),call_indirect:(t,e)=>{let r=t.shift(),[,i]=t.shift();return[...p(x(i,e.type)),...p(x(r,e.table))]},br_table:(t,e)=>{let r=[],i=0;for(;t[0]&&(!isNaN(t[0])||F(t[0]));)r.push(...p(H(t.shift(),e.block))),i++;return[...p(i-1),...r]},select:(t,e)=>{let r=t.shift()||[];return r.length?N(r.map(i=>C(i,e))):[]},ref_null:(t,e)=>{let r=t.shift();return Array.isArray(r)&&r[0]==="exact"?[98,...p(x(r[1],e.type))]:T[r]?[T[r]]:p(x(r,e.type))},memarg:(t,e,r)=>Tt(t,r,L(t[0])&&!bt(t[0])?x(t.shift(),e.memory):0),opt_memory:(t,e)=>p(x(L(t[0])?t.shift():0,e.memory)),reftype:(t,e)=>{let r=C(t.shift(),e);return r.length>1?r.slice(1):r},reftype2:(t,e)=>{let r=H(t.shift(),e.block),i=C(t.shift(),e),s=C(t.shift(),e),l=a=>a.length>1?a.slice(1):a;return[(s[0]!==T.ref)<<1|i[0]!==T.ref,...p(r),...l(i),...l(s)]},v128const:t=>{let[e,r]=t.shift().split("x"),i=+e.slice(1),s=i>>>3;if(r=+r,e[0]==="i"){let a=r===16?new Uint8Array(16):r===8?new Uint16Array(8):r===4?new Uint32Array(4):new BigUint64Array(2);for(let n=0;n<r;n++)a[n]=tt[e].parse(t.shift());return[...new Uint8Array(a.buffer)]}let l=new Uint8Array(16);for(let a=0;a<r;a++)l.set(tt[e](t.shift()),a*s);return[...l]},shuffle:t=>{let e=[];for(let r=0;r<16;r++)e.push(ut(t.shift(),32));return typeof t[0]=="string"&&!isNaN(t[0])&&v("invalid lane length"),e},memlane:(t,e,r)=>{let i=F(t[0])||L(t[0])&&(bt(t[1])||L(t[1]))?x(t.shift(),e.memory):0;return[...Tt(t,r,i),...p(ut(t.shift()))]},"*":t=>p(t.shift()),labelidx:(t,e)=>p(H(t.shift(),e.block)),laneidx:t=>[ut(t.shift(),255)],funcidx:(t,e)=>p(x(t.shift(),e.func)),typeidx:(t,e)=>p(x(t.shift(),e.type)),tableidx:(t,e)=>p(x(t.shift(),e.table)),memoryidx:(t,e)=>p(x(t.shift(),e.memory)),globalidx:(t,e)=>p(x(t.shift(),e.global)),localidx:(t,e)=>p(x(t.shift(),e.local)),dataidx:(t,e)=>p(x(t.shift(),e.data)),elemidx:(t,e)=>p(x(t.shift(),e.elem)),tagidx:(t,e)=>p(x(t.shift(),e.tag)),"memoryidx?":(t,e)=>p(x(L(t[0])?t.shift():0,e.memory)),i32:t=>W(t.shift()),i64:t=>Z(t.shift()),f32:t=>Q(t.shift()),f64:t=>J(t.shift()),v128:t=>(void 0)(t.shift()),typeidx_field:(t,e)=>{let r=x(t.shift(),e.type);return[...p(r),...p(x(t.shift(),e.type[r][1]))]},typeidx_multi:(t,e)=>[...p(x(t.shift(),e.type)),...p(t.shift())],typeidx_dataidx:(t,e)=>[...p(x(t.shift(),e.type)),...p(x(t.shift(),e.data))],typeidx_elemidx:(t,e)=>[...p(x(t.shift(),e.type)),...p(x(t.shift(),e.elem))],typeidx_typeidx:(t,e)=>[...p(x(t.shift(),e.type)),...p(x(t.shift(),e.type))],dataidx_memoryidx:(t,e)=>[...p(x(t.shift(),e.data)),...p(x(t.shift(),e.memory))],memoryidx_memoryidx:(t,e)=>[...p(x(t.shift(),e.memory)),...p(x(t.shift(),e.memory))],tableidx_tableidx:(t,e)=>[...p(x(t.shift(),e.table)),...p(x(t.shift(),e.table))],cont_bind:(t,e)=>[...p(x(t.shift(),e.type)),...p(x(t.shift(),e.type))],switch_cont:(t,e)=>[...p(x(t.shift(),e.type)),...p(x(t.shift(),e.tag))],resume:(t,e)=>{let r=p(x(t.shift(),e.type)),i=[],s=0;for(;t[0]?.[0]==="on";){let[,l,a]=t.shift();a==="switch"?i.push(1,...p(x(l,e.tag))):i.push(0,...p(x(l,e.tag)),...p(H(a,e.block))),s++}return[...r,...p(s),...i]},resume_throw:(t,e)=>{let r=p(x(t.shift(),e.type)),i=p(x(t.shift(),e.tag)),s=[],l=0;for(;t[0]?.[0]==="on";){let[,a,n]=t.shift();n==="switch"?s.push(1,...p(x(a,e.tag))):s.push(0,...p(x(a,e.tag)),...p(H(n,e.block))),l++}return[...r,...i,...p(l),...s]},resume_throw_ref:(t,e)=>{let r=p(x(t.shift(),e.type)),i=[],s=0;for(;t[0]?.[0]==="on";){let[,l,a]=t.shift();a==="switch"?i.push(1,...p(x(l,e.tag))):i.push(0,...p(x(l,e.tag)),...p(H(a,e.block))),s++}return[...r,...p(s),...i]}},ct={};(function t(e,r){for(let i=0,s,l,a;i<e.length;i++)(s=e[i])&&(Array.isArray(s)?t(s,i):([l,a]=s.split(" "),st[l]=r?[r,...p(i)]:[i],a&&(ct[l]=St[a])))})(st);var Ft=(t,e)=>{let r=[],i=[];for(;t?.length;){let s=t.shift();if(s?.[0]==="@metadata"){i.push(s.slice(1));continue}Array.isArray(s)&&(s.loc!=null&&(v.loc=s.loc),v(`Unknown instruction ${s[0]}`));let[...l]=st[s]||v(`Unknown instruction ${s}`);ct[s]&&(s==="select"&&t[0]?.length?l[0]++:ct[s]===St.reftype&&(t[0][1]==="null"||t[0][0]!=="ref")&&l[l.length-1]++,l.push(...ct[s](t,e,s)));for(let[a,n]of i)(e.meta[a]??=[]).push([r.length,n]);r.push(...l)}return r.push(11),r},et=(t,e)=>Ft(Y([t],e),e),x=(t,e,r)=>(r=F(t)?e[t]:+t,r in e?r:v(`Unknown ${e.name} ${t}`)),H=(t,e,r)=>(r=F(t)?e.length-e[t]:+t,isNaN(r)||r>e.length?v(`Bad label ${t}`):r),le=t=>{let e,r,i,s;for(;bt(t[0]);)[i,s]=t.shift().split("="),i==="offset"?r=+s:i==="align"?e=+s:v(`Unknown param ${i}=${s}`);return(r<0||r>4294967295)&&v(`Bad offset ${r}`),(e<=0||e>4294967295)&&v(`Bad align ${e}`),e&&(e=Math.log2(e))%1&&v(`Bad align ${e}`),[e,r]},Tt=(t,e,r=0)=>{let[i,s]=le(t),l=(i??ne(e))|(r&&64);return r?[...p(l),...p(r),...p(s??0)]:[...p(l),...p(s??0)]},ne=t=>{let e=t.indexOf(".",3)+1,r=t.slice(1,t[0]==="v"?4:3);if(t[e]==="a"&&(e=t.indexOf(".",e)+1),t[0]==="m")return t.includes("64")?3:2;if(t[e]==="r"){let l=t.slice(e,e+6).match(/\d+/);return Math.log2(l?l[0]/8:+r/8)}let i=t[e]==="l"?e+4:e+5,s=t.slice(i).match(/(\d+)(x|_|$)/);return Math.log2(s?s[2]==="x"?8:s[1]/8:+r/8)},ae=t=>{if(!Array.isArray(t))return null;let[e,...r]=t;if(e!=="i8"&&e!=="i16"&&e!=="i32"&&e!=="i64"&&e!=="f32"&&e!=="f64")return null;let i=[],s=new DataView(new ArrayBuffer(8));for(let l of r)e==="i8"?i.push(W.parse(l)&511&255):e==="i16"?(s.setInt16(0,W.parse(l),!0),i.push(...new Uint8Array(s.buffer,0,2))):e==="i32"?(s.setInt32(0,W.parse(l),!0),i.push(...new Uint8Array(s.buffer,0,4))):e==="i64"?(s.setBigInt64(0,BigInt(l),!0),i.push(...new Uint8Array(s.buffer,0,8))):e==="f32"?i.push(...Q(l)):e==="f64"&&i.push(...J(l));return i},ot=t=>{let e=t[0]==="i64"&&t.shift(),r=t[t.length-1]==="shared"&&t.pop(),i=t.findIndex(f=>Array.isArray(f)&&f[0]==="pagesize"),s=-1;i>=0&&(s=Math.log2(+t.splice(i,1)[0][1]));let l=!isNaN(parseInt(t[1])),a=(s>=0?8:0)|(e?4:0)|(r?2:0)|(l?1:0),n=e?f=>{if(typeof f=="bigint")return f;let o=typeof f=="string"?f.replaceAll("_",""):String(f);return BigInt(o)}:ut,c=s>=0?p(s):[];return l?[a,...p(n(t.shift())),...p(n(t.shift())),...c]:[a,...p(n(t.shift())),...c]},ut=(t,e=4294967295)=>{let r=typeof t=="string"&&t[0]!=="+"?W.parse(t):typeof t=="number"?t:v(`Bad int ${t}`);return r>e?v(`Value out of range ${t}`):r},N=t=>[...p(t.length),...t.flat()];function Ot(t,e={}){typeof t=="string"&&(t=D(t));let{indent:r=" ",newline:i=`
|
|
3
|
+
`,comments:s=!0}=e;if(r||="",i||="",typeof t[0]=="string"&&t[0][0]!==";")return a(t);return t.filter(n=>s||!l(n)).map(n=>a(n)).join(i);function l(n){return typeof n=="string"&&n[1]===";"}function a(n,c=0){if(!Array.isArray(n))return n;let f=n[0];if(!f)return"";let o=!1;if(f==="try_table"){let h=1;for(typeof n[h]=="string"&&n[h][0]==="$"&&(f+=" "+n[h++]),Array.isArray(n[h])&&(n[h][0]==="result"||n[h][0]==="type")&&(f+=" "+a(n[h++],c));Array.isArray(n[h])&&/^catch/.test(n[h][0]);)f+=" "+a(n[h++],c).trim();for(;h<n.length;h++)f+=Array.isArray(n[h])?i+r.repeat(c+1)+a(n[h],c+1):" "+n[h];return`(${f+i+r.repeat(c)})`}let u=!!i&&n.length<4&&!n.some(h=>typeof h=="string"&&h[0]===";"&&h[1]===";"),d=r.repeat(c+1);for(let h=1;h<n.length;h++){let $=n[h].valueOf();if(typeof $=="string"&&$[1]===";"){if(!s)continue;if($[0]===";")if(i)f+=i+d+$.trimEnd(),o=!0;else{let y=f[f.length-1];y&&y!==" "&&y!=="("&&(f+=" "),f+=$.trimEnd()+`
|
|
4
|
+
`}else{let y=f[f.length-1];y&&y!==" "&&y!=="("&&(f+=" "),f+=$.trimEnd()}}else if(Array.isArray($))u&&(u=$.every(y=>!Array.isArray(y))),f+=i+d+a($,c+1),o=!1;else if(n[0]==="data")u=!1,(i||f[f.length-1]!==")")&&(f+=i||" "),f+=d+$,o=!1;else{let y=f[f.length-1];o&&i?f+=i+d:y===`
|
|
5
|
+
`?f+="":(y&&y!==")"&&y!==" "||i||y===")")&&(f+=" "),f+=$,o=!1}}return u?`(${f.replaceAll(i+d+"("," (")})`:`(${f+i+r.repeat(c)})`}}var Ct={funcref:["ref.func","call_ref","return_call_ref"],sign_ext:["i32.extend8_s","i32.extend16_s","i64.extend8_s","i64.extend16_s","i64.extend32_s"],nontrapping:["i32.trunc_sat_f32_s","i32.trunc_sat_f32_u","i32.trunc_sat_f64_s","i32.trunc_sat_f64_u","i64.trunc_sat_f32_s","i64.trunc_sat_f32_u","i64.trunc_sat_f64_s","i64.trunc_sat_f64_u"],bulk_memory:["memory.copy","memory.fill"],return_call:["return_call","return_call_indirect"],i31ref:["ref.i31","i31.get_s","i31.get_u"],extended_const:["global.get"],multi_value:[],gc:["struct.new","struct.get","struct.set","array.new","array.get","array.set","array.len","struct.new_default","array.new_default","array.new_fixed","array.copy"],ref_cast:["ref.test","ref.cast","br_on_cast","br_on_cast_fail"]},wt=Object.keys(Ct),fe=t=>{if(t===!0)return Object.fromEntries(wt.map(e=>[e,!0]));if(t===!1)return{};if(typeof t=="string"){let e=new Set(t.split(/\s+/).filter(Boolean));return Object.fromEntries(wt.map(r=>[r,e.has(r)||e.has("all")]))}return{...t}},S=(t,e,r,i)=>{if(e(t,r,i),Array.isArray(t))for(let s=0;s<t.length;s++)S(t[s],e,t,s)},j=(t,e,r,i)=>{if(Array.isArray(t))for(let s=0;s<t.length;s++)j(t[s],e,t,s);e(t,r,i)},oe=t=>{let e=new Set;return S(t,r=>{if(typeof r=="string")for(let[i,s]of Object.entries(Ct))s.some(l=>r===l||r.startsWith(l+" "))&&e.add(i)}),S(t,r=>{if(!(!Array.isArray(r)||r[0]!=="global"))for(let i of r)Array.isArray(i)&&(i[0]==="i32.add"||i[0]==="i32.sub"||i[0]==="i32.mul"||i[0]==="i64.add"||i[0]==="i64.sub"||i[0]==="i64.mul")&&S(i,s=>{Array.isArray(s)&&s[0]==="global.get"&&e.add("extended_const")})}),S(t,r=>{if(!Array.isArray(r)||r[0]!=="func")return;let i=0;for(let s of r)Array.isArray(s)&&s[0]==="result"&&(i+=s.length-1);i>1&&e.add("multi_value")}),e},Dt=t=>Array.isArray(t)?t.map(Dt):t,Lt=(t,e)=>{let r=[],i=t[0]==="module"?1:0;for(let s=i;s<t.length;s++)Array.isArray(t[s])&&t[s][0]===e&&r.push({node:t[s],idx:s});return r},yt=(t,e,r)=>t.splice(e,0,r),ce=0,R=t=>`$__${t}${ce++}`,ue=(t,e)=>{let r=new Set;if(S(t,f=>{Array.isArray(f)&&f[0]==="ref.func"&&r.add(f[1])}),!r.size)return t;let i=R("fntbl"),s=[...r],l=Object.fromEntries(s.map((f,o)=>[f,o])),a=Lt(t,"func"),n=a.length?a[0].idx:t[0]==="module"?1:0;yt(t,n,["table",i,"funcref",["elem",...s]]);let c={};return S(t,f=>{if(!Array.isArray(f)||f[0]!=="func")return;let o=typeof f[1]=="string"&&f[1][0]==="$"?f[1]:null;if(!o)return;let u=[],d=[];for(let h of f){if(Array.isArray(h)&&h[0]==="param")for(let $=1;$<h.length;$++)h[$][0]!=="$"&&u.push(h[$]);if(Array.isArray(h)&&h[0]==="result")for(let $=1;$<h.length;$++)d.push(h[$])}c[o]={params:u,results:d}}),j(t,(f,o,u)=>{if(!(!Array.isArray(f)||!o)){if(f[0]==="ref.func"&&l[f[1]]!==void 0&&(o[u]=["i32.const",l[f[1]]]),f[0]==="call_ref"){let d=f[1],h=f.slice(2);o[u]=["call_indirect",i,["type",d],...h]}if(f[0]==="return_call_ref"){let d=f[1],h=f.slice(2);o[u]=["return_call_indirect",i,["type",d],...h]}}}),t},P={funcref:ue},me={"i32.extend8_s":["i32",24],"i32.extend16_s":["i32",16],"i64.extend8_s":["i64",56n],"i64.extend16_s":["i64",48n],"i64.extend32_s":["i64",32n]},pe=(t,e)=>(j(t,(r,i,s)=>{if(!Array.isArray(r)||!i)return;let l=me[r[0]];if(!l)return;let[a,n]=l,c=r.slice(1);i[s]=[`${a}.shr_s`,[`${a}.shl`,...c,[`${a}.const`,n]],[`${a}.const`,n]]}),t);P.sign_ext=pe;var Wt={"i32.trunc_sat_f32_s":{itype:"i32",ftype:"f32",signed:!0,min:-2147483648,max:2147483647},"i32.trunc_sat_f32_u":{itype:"i32",ftype:"f32",signed:!1,min:0,max:4294967295},"i32.trunc_sat_f64_s":{itype:"i32",ftype:"f64",signed:!0,min:-2147483648,max:2147483647},"i32.trunc_sat_f64_u":{itype:"i32",ftype:"f64",signed:!1,min:0,max:4294967295},"i64.trunc_sat_f32_s":{itype:"i64",ftype:"f32",signed:!0,min:-9223372036854775808n,max:9223372036854775807n},"i64.trunc_sat_f32_u":{itype:"i64",ftype:"f32",signed:!1,min:0n,max:18446744073709551615n},"i64.trunc_sat_f64_s":{itype:"i64",ftype:"f64",signed:!0,min:-9223372036854775808n,max:9223372036854775807n},"i64.trunc_sat_f64_u":{itype:"i64",ftype:"f64",signed:!1,min:0n,max:18446744073709551615n}},_e=(t,e)=>{let r=new Set;if(S(t,s=>{Array.isArray(s)&&Wt[s[0]]&&r.add(s[0])}),!r.size)return t;let i={};for(let s of r){let{itype:l,ftype:a,signed:n,min:c,max:f}=Wt[s],o=R(`trunc_${l}_${a}_${n?"s":"u"}`);i[s]=o;let u=`${l}.trunc_${a}_${n?"s":"u"}`,d=l==="i64"?0n:0,h=["func",o,["param","$v",a],["result",l],["if",["result",l],[`${a}.ne`,["local.get","$v"],["local.get","$v"]],["then",[`${l}.const`,d]],["else",["if",["result",l],[`${a}.lt`,["local.get","$v"],[`${a}.const`,typeof c=="bigint"?Number(c):c]],["then",[`${l}.const`,c]],["else",["if",["result",l],[`${a}.gt`,["local.get","$v"],[`${a}.const`,typeof f=="bigint"?Number(f):f]],["then",[`${l}.const`,f]],["else",[u,["local.get","$v"]]]]]]]]];t.push(h)}return j(t,(s,l,a)=>{!Array.isArray(s)||!l||i[s[0]]&&(l[a]=["call",i[s[0]],...s.slice(1)])}),t};P.nontrapping=_e;var ye=(t,e)=>{let r=new Set,i=new Set;S(t,a=>{if(Array.isArray(a)){if(a[0]==="memory.copy"){let n=typeof a[1]=="number"?a[1]:0,c=typeof a[2]=="number"?a[2]:0;r.add(`${n}_${c}`)}if(a[0]==="memory.fill"){let n=typeof a[1]=="number"?a[1]:0;i.add(n)}}});let s={},l={};for(let a of r){let[n,c]=a.split("_").map(Number),f=R(`memcpy${a==="0_0"?"":"_"+a}`);s[a]=f;let o=n?["i32.store8",n]:["i32.store8"],u=c?["i32.load8_u",c]:["i32.load8_u"];t.push(["func",f,["param","$dst","i32"],["param","$src","i32"],["param","$len","i32"],["local","$i","i32"],["block","$done",["loop","$loop",["br_if","$done",["i32.ge_u",["local.get","$i"],["local.get","$len"]]],[...o,["i32.add",["local.get","$dst"],["local.get","$i"]],[...u,["i32.add",["local.get","$src"],["local.get","$i"]]]],["local.set","$i",["i32.add",["local.get","$i"],["i32.const",1]]],["br","$loop"]]]])}for(let a of i){let n=R(`memset${a===0?"":"_"+a}`);l[a]=n;let c=a?["i32.store8",a]:["i32.store8"];t.push(["func",n,["param","$dst","i32"],["param","$val","i32"],["param","$len","i32"],["local","$i","i32"],["block","$done",["loop","$loop",["br_if","$done",["i32.ge_u",["local.get","$i"],["local.get","$len"]]],[...c,["i32.add",["local.get","$dst"],["local.get","$i"]],["local.get","$val"]],["local.set","$i",["i32.add",["local.get","$i"],["i32.const",1]]],["br","$loop"]]]])}return j(t,(a,n,c)=>{if(!(!Array.isArray(a)||!n)){if(a[0]==="memory.copy"){let f=typeof a[1]=="number"?a[1]:0,o=typeof a[2]=="number"?a[2]:0,u=a.filter(d=>Array.isArray(d)||typeof d=="string"&&d[0]==="$");n[c]=["call",s[`${f}_${o}`],...u]}if(a[0]==="memory.fill"){let f=typeof a[1]=="number"?a[1]:0,o=a.filter(u=>Array.isArray(u)||typeof u=="string"&&u[0]==="$");n[c]=["call",l[f],...o]}}}),t};P.bulk_memory=ye;var ge=(t,e)=>{let r=!1;return S(t,i=>{Array.isArray(i)&&(i[0]==="return_call"||i[0]==="return_call_indirect")&&(r=!0)}),r&&j(t,(i,s,l)=>{!Array.isArray(i)||!s||(i[0]==="return_call"&&(s[l]=["return",["call",...i.slice(1)]]),i[0]==="return_call_indirect"&&(s[l]=["return",["call_indirect",...i.slice(1)]]))}),t};P.return_call=ge;var xe=(t,e)=>(j(t,(r,i,s)=>{if(!(!Array.isArray(r)||!i)&&(r[0]==="ref.i31"&&(i[s]=["i32.and",...r.slice(1),["i32.const",2147483647]]),r[0]==="i31.get_u"&&(i[s]=r.length>1?r[1]:["drop"]),r[0]==="i31.get_s")){let l=r.slice(1);i[s]=["i32.shr_s",["i32.shl",...l,["i32.const",1]],["i32.const",1]]}}),t);P.i31ref=xe;var he=(t,e)=>{let r={};S(t,s=>{if(!Array.isArray(s)||s[0]!=="global")return;let l=typeof s[1]=="string"&&s[1][0]==="$"?s[1]:null;if(l)for(let a=s.length-1;a>=0;a--){let n=s[a];if(Array.isArray(n)&&(n[0]==="i32.const"||n[0]==="i64.const"||n[0]==="f32.const"||n[0]==="f64.const")){r[l]={type:n[0].split(".")[0],value:n[1]};break}}});let i=s=>{if(!Array.isArray(s))return s;let l=s[0];if(l==="global.get"&&r[s[1]]){let a=r[s[1]];return[`${a.type}.const`,a.value]}if(l==="i32.add"||l==="i64.add"){let a=i(s[1]),n=i(s[2]);if(a&&n&&a[0]?.endsWith(".const")&&n[0]?.endsWith(".const")){let c=l.split(".")[0],f=c==="i64"?BigInt(a[1]):Number(a[1]),o=c==="i64"?BigInt(n[1]):Number(n[1]);return[`${c}.const`,f+o]}}if(l==="i32.sub"||l==="i64.sub"){let a=i(s[1]),n=i(s[2]);if(a&&n&&a[0]?.endsWith(".const")&&n[0]?.endsWith(".const")){let c=l.split(".")[0],f=c==="i64"?BigInt(a[1]):Number(a[1]),o=c==="i64"?BigInt(n[1]):Number(n[1]);return[`${c}.const`,f-o]}}if(l==="i32.mul"||l==="i64.mul"){let a=i(s[1]),n=i(s[2]);if(a&&n&&a[0]?.endsWith(".const")&&n[0]?.endsWith(".const")){let c=l.split(".")[0],f=c==="i64"?BigInt(a[1]):Number(a[1]),o=c==="i64"?BigInt(n[1]):Number(n[1]);return[`${c}.const`,f*o]}}return s};return j(t,(s,l,a)=>{if(!(!Array.isArray(s)||s[0]!=="global"||!l)){for(let n=2;n<s.length;n++)if(Array.isArray(s[n])){let c=i(s[n]);c!==s[n]&&(s[n]=c)}}}),t};P.extended_const=he;var de=(t,e)=>{let r=new Map,i=[];if(S(t,n=>{if(!Array.isArray(n)||n[0]!=="func")return;let c=typeof n[1]=="string"&&n[1][0]==="$"?n[1]:null,f=[];for(let o of n)if(Array.isArray(o)&&o[0]==="result")for(let u=1;u<o.length;u++)f.push(o[u]);f.length>1&&c&&r.set(c,f)}),!r.size)return t;let s=Math.max(...[...r.values()].map(n=>n.length)),l={};for(let[n,c]of r)for(let f=1;f<c.length;f++){let o=c[f];if(l[o]||(l[o]=[]),l[o].length<f){let u=R(`ret_${o}_${l[o].length}`);l[o].push(u),i.push(["global",u,["mut",o],[`${o}.const`,o==="i64"?0n:0]])}}let a=t[0]==="module"?1:0;for(let n of i.reverse())yt(t,a,n);return j(t,(n,c,f)=>{if(!Array.isArray(n)||n[0]!=="func")return;let o=typeof n[1]=="string"&&n[1][0]==="$"?n[1]:null;if(!o||!r.has(o))return;let u=r.get(o);for(let d=0;d<n.length;d++)if(Array.isArray(n[d])&&n[d][0]==="result"){n[d]=["result",u[0]];break}}),t};P.multi_value=de;var lt={i32:4,i64:8,f32:4,f64:8},be=(t,e)=>{let r=new Map,i=new Map,s=1;if(S(t,y=>{if(!Array.isArray(y)||y[0]!=="type")return;let k=typeof y[1]=="string"&&y[1][0]==="$"?y[1]:null;if(k){for(let m of y)if(Array.isArray(m)){if(m[0]==="struct"){let g=[];for(let _ of m)if(Array.isArray(_)&&_[0]==="field"){let A=typeof _[1]=="string"&&_[1][0]==="$"?_[1]:null,b=A?_[2]:_[1],I=Array.isArray(b)&&b[0]==="mut"?b[1]:b;g.push({name:A,type:I})}r.set(k,{kind:"struct",fields:g}),i.set(k,s++)}if(m[0]==="array"){let g=m[1],_=Array.isArray(g)&&g[0]==="mut"?g[1]:g;r.set(k,{kind:"array",elemType:_}),i.set(k,s++)}}}}),!r.size)return t;let l=Lt(t,"memory").length>0,a=R("alloc"),n=R("heap_ptr"),c=t[0]==="module"?1:0;l||yt(t,c,["memory",1]),yt(t,c+1,["global",n,["mut","i32"],["i32.const",1024]]);let f=["func",a,["param","$size","i32"],["result","i32"],["local","$ptr","i32"],["local.set","$ptr",["global.get",n]],["global.set",n,["i32.add",["global.get",n],["local.get","$size"]]],["local.get","$ptr"]];t.push(f);let o=y=>{let k=4;for(let m of y.fields)k+=lt[m.type]||4;return k},u=(y,k)=>{let m=4;for(let g=0;g<k;g++)m+=lt[y.fields[g].type]||4;return m},d=(y,k)=>{for(let m=0;m<y.fields.length;m++)if(y.fields[m].name===k)return m;return-1},h=0,$=()=>`$__gc_tmp${h++}`;return S(t,y=>{if(!Array.isArray(y)||y[0]!=="func")return;let k=!1,m=!1,g=!1,_=!1;if(S(y,b=>{Array.isArray(b)&&((b[0]==="struct.new"||b[0]==="struct.new_default")&&(k=!0),(b[0]==="array.new"||b[0]==="array.new_default")&&(m=!0,g=!0,_=!0))}),!k&&!m)return;let A=1;for(let b=1;b<y.length;b++){let I=y[b];if(Array.isArray(I)&&(I[0]==="param"||I[0]==="result"||I[0]==="local"||I[0]==="export"||I[0]==="type"))A=b+1;else if(typeof I=="string"&&I[0]==="$")A=b+1;else if(!Array.isArray(I))A=b+1;else break}k&&y.splice(A++,0,["local","$__gc_ptr","i32"]),m&&y.splice(A++,0,["local","$__gc_aptr","i32"]),g&&y.splice(A++,0,["local","$__gc_alen","i32"]),_&&y.splice(A++,0,["local","$__gc_aidx","i32"])}),j(t,(y,k,m)=>{if(!(!Array.isArray(y)||!k)){if(y[0]==="struct.new"||y[0]==="struct.new_default"){let g=y[1],_=r.get(g);if(!_||_.kind!=="struct")return;let A=o(_),b=i.get(g),I=y.slice(2),B="$__gc_ptr",E=[["local.set",B,["call",a,["i32.const",A]]],["i32.store",["local.get",B],["i32.const",b]]];if(y[0]==="struct.new")for(let M=0;M<_.fields.length;M++){let z=_.fields[M],it=u(_,M),X=z.type==="i64"?"i64.store":z.type==="f32"?"f32.store":z.type==="f64"?"f64.store":"i32.store";E.push([X,["i32.add",["local.get",B],["i32.const",it]],I[M]||[`${z.type}.const`,0]])}else for(let M=0;M<_.fields.length;M++){let z=_.fields[M],it=u(_,M),X=z.type==="i64"?"i64.store":z.type==="f32"?"f32.store":z.type==="f64"?"f64.store":"i32.store",Yt=z.type==="i64"?["i64.const",0n]:z.type==="f32"?["f32.const",0]:z.type==="f64"?["f64.const",0]:["i32.const",0];E.push([X,["i32.add",["local.get",B],["i32.const",it]],Yt])}E.push(["local.get",B]),k[m]=["block",["result","i32"],...E]}if(y[0]==="struct.get"){let g=y[1],_=y[2],A=y[3],b=r.get(g);if(!b||b.kind!=="struct")return;let I=typeof _=="string"&&_[0]==="$"?d(b,_):parseInt(_);if(I<0)return;let B=b.fields[I],E=u(b,I),M=B.type==="i64"?"i64.load":B.type==="f32"?"f32.load":B.type==="f64"?"f64.load":"i32.load";k[m]=[M,["i32.add",A,["i32.const",E]]]}if(y[0]==="struct.set"){let g=y[1],_=y[2],A=y[3],b=y[4],I=r.get(g);if(!I||I.kind!=="struct")return;let B=typeof _=="string"&&_[0]==="$"?d(I,_):parseInt(_);if(B<0)return;let E=I.fields[B],M=u(I,B),z=E.type==="i64"?"i64.store":E.type==="f32"?"f32.store":E.type==="f64"?"f64.store":"i32.store";k[m]=[z,["i32.add",A,["i32.const",M]],b]}if(y[0]==="array.new"||y[0]==="array.new_default"){let g=y[1],_=r.get(g);if(!_||_.kind!=="array")return;let A=i.get(g),b=lt[_.elemType]||4,I=y[0]==="array.new"?y[2]:null,B=y[0]==="array.new"?y[3]:y[2],E="$__gc_aptr",M="$__gc_alen",z="$__gc_aidx",it=_.elemType==="i64"?"i64.store":_.elemType==="f32"?"f32.store":_.elemType==="f64"?"f64.store":"i32.store",X=[["local.set",M,B],["local.set",E,["call",a,["i32.add",["i32.const",8],["i32.mul",["local.get",M],["i32.const",b]]]]],["i32.store",["local.get",E],["i32.const",A]],["i32.store",["i32.add",["local.get",E],["i32.const",4]],["local.get",M]]];I&&X.push(["local.set",z,["i32.const",0]],["block","$done",["loop","$loop",["br_if","$done",["i32.ge_u",["local.get",z],["local.get",M]]],[it,["i32.add",["i32.add",["local.get",E],["i32.const",8]],["i32.mul",["local.get",z],["i32.const",b]]],I],["local.set",z,["i32.add",["local.get",z],["i32.const",1]]],["br","$loop"]]]),X.push(["local.get",E]),k[m]=["block",["result","i32"],...X]}if(y[0]==="array.get"){let g=y[1],_=y[2],A=y[3],b=r.get(g);if(!b||b.kind!=="array")return;let I=lt[b.elemType]||4,B=b.elemType==="i64"?"i64.load":b.elemType==="f32"?"f32.load":b.elemType==="f64"?"f64.load":"i32.load";k[m]=[B,["i32.add",["i32.add",_,["i32.const",8]],["i32.mul",A,["i32.const",I]]]]}if(y[0]==="array.set"){let g=y[1],_=y[2],A=y[3],b=y[4],I=r.get(g);if(!I||I.kind!=="array")return;let B=lt[I.elemType]||4,E=I.elemType==="i64"?"i64.store":I.elemType==="f32"?"f32.store":I.elemType==="f64"?"f64.store":"i32.store";k[m]=[E,["i32.add",["i32.add",_,["i32.const",8]],["i32.mul",A,["i32.const",B]]],b]}if(y[0]==="array.len"){let g=y[1];k[m]=["i32.load",["i32.add",g,["i32.const",4]]]}}}),t};P.gc=be;var Ae=(t,e)=>{let r=new Map,i=1;return S(t,s=>{if(!Array.isArray(s)||s[0]!=="type")return;let l=typeof s[1]=="string"&&s[1][0]==="$"?s[1]:null;if(l)for(let a of s)Array.isArray(a)&&(a[0]==="struct"||a[0]==="array")&&r.set(l,i++)}),r.size&&j(t,(s,l,a)=>{if(!(!Array.isArray(s)||!l)){if(s[0]==="ref.test"){let n=s[1],c=null;Array.isArray(n)&&n[0]==="ref"&&(c=n[1]==="null"?n[2]:n[1]);let f=s[2],o=r.get(c);o!==void 0&&(l[a]=["if",["result","i32"],["i32.eqz",f],["then",["i32.const",0]],["else",["i32.eq",["i32.load",f],["i32.const",o]]]])}if(s[0]==="ref.cast"){let n=s[1],c=null,f=!1;Array.isArray(n)&&n[0]==="ref"&&(n[1]==="null"?(f=!0,c=n[2]):c=n[1]);let o=s[2],u=r.get(c);if(u!==void 0){let d=R("cast");f?l[a]=["block",["result","i32"],["local",d,"i32"],["local.set",d,o],["if",["i32.and",["i32.ne",["local.get",d],["i32.const",0]],["i32.ne",["i32.load",["local.get",d]],["i32.const",u]]],["then",["unreachable"]]],["local.get",d]]:l[a]=["block",["result","i32"],["local",d,"i32"],["local.set",d,o],["if",["i32.or",["i32.eqz",["local.get",d]],["i32.ne",["i32.load",["local.get",d]],["i32.const",u]]],["then",["unreachable"]]],["local.get",d]]}}if(s[0]==="br_on_cast"){let n=s[1],c=s[2],f=s[3],o=s[4],u=null;Array.isArray(f)&&f[0]==="ref"&&(u=f[1]==="null"?f[2]:f[1]);let d=r.get(u);if(d!==void 0){let h=R("brcast");l[a]=["block",["result","i32"],["local",h,"i32"],["local.set",h,o],["br_if",n,["i32.and",["i32.ne",["local.get",h],["i32.const",0]],["i32.eq",["i32.load",["local.get",h]],["i32.const",d]]]],["local.get",h]]}}if(s[0]==="br_on_cast_fail"){let n=s[1],c=s[2],f=s[3],o=s[4],u=null;Array.isArray(f)&&f[0]==="ref"&&(u=f[1]==="null"?f[2]:f[1]);let d=r.get(u);if(d!==void 0){let h=R("brfail");l[a]=["block",["result","i32"],["local",h,"i32"],["local.set",h,o],["br_if",n,["i32.or",["i32.eqz",["local.get",h]],["i32.ne",["i32.load",["local.get",h]],["i32.const",d]]]],["local.get",h]]}}}}),t};P.ref_cast=Ae;function gt(t,e=!0){typeof t=="string"&&(t=D(t)),t=Dt(t),e=fe(e);let r=oe(t),i={uid:0};for(let s of wt)r.has(s)&&e[s]!==!1&&P[s]&&(t=P[s](t,i));return t}var $t={treeshake:!0,fold:!0,deadcode:!0,locals:!0,identity:!0,strength:!0,branch:!0,propagate:!0,inline:!0},vt=Object.keys($t),we=t=>{if(t===!0)return{...$t};if(t===!1)return{};if(typeof t=="string"){let e=new Set(t.split(/\s+/).filter(Boolean));return e.size===1&&vt.includes([...e][0])?Object.fromEntries(vt.map(r=>[r,e.has(r)])):Object.fromEntries(vt.map(r=>[r,e.has(r)||e.has("all")]))}return{...$t,...t}},O=t=>Array.isArray(t)?t.map(O):t,G=(t,e,r,i)=>{if(e(t,r,i),Array.isArray(t))for(let s=0;s<t.length;s++)G(t[s],e,t,s)},K=(t,e,r,i)=>{if(Array.isArray(t))for(let l=0;l<t.length;l++){let a=K(t[l],e,t,l);a!==void 0&&(t[l]=a)}let s=e(t,r,i);return s!==void 0?s:t},ve=t=>{if(!Array.isArray(t)||t[0]!=="module")return t;let e=new Map,r=new Map,i=new Map,s=new Map,l=new Map,a=[],n=[],c=0,f=0,o=0,u=0,d=0,h=0;for(let m of t.slice(1)){if(!Array.isArray(m))continue;let g=m[0];if(g==="type"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:o;i.set(_,{node:m,idx:o,used:!1}),typeof _=="string"&&i.set(o,i.get(_)),o++}else if(g==="func"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:c,A=m.some(b=>Array.isArray(b)&&b[0]==="export");e.set(_,{node:m,idx:c,used:A}),typeof _=="string"&&e.set(c,e.get(_)),c++}else if(g==="global"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:f,A=m.some(b=>Array.isArray(b)&&b[0]==="export");r.set(_,{node:m,idx:f,used:A}),typeof _=="string"&&r.set(f,r.get(_)),f++}else if(g==="table"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:u,A=m.some(b=>Array.isArray(b)&&b[0]==="export");s.set(_,{node:m,idx:u,used:A}),typeof _=="string"&&s.set(u,s.get(_)),u++}else if(g==="memory"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:d,A=m.some(b=>Array.isArray(b)&&b[0]==="export");l.set(_,{node:m,idx:d,used:A}),typeof _=="string"&&l.set(d,l.get(_)),d++}else if(g==="import"){for(let _ of m)if(Array.isArray(_)&&_[0]==="func"){let A=typeof _[1]=="string"&&_[1][0]==="$"?_[1]:h;e.set(A,{node:m,idx:h,used:!0,isImport:!0}),typeof A=="string"&&e.set(h,e.get(A)),h++,c++}}else g==="export"?a.push(m):g==="start"&&n.push(m)}for(let m of a)for(let g of m){if(!Array.isArray(g))continue;let[_,A]=g;_==="func"&&e.has(A)?e.get(A).used=!0:_==="global"&&r.has(A)?r.get(A).used=!0:_==="table"&&s.has(A)?s.get(A).used=!0:_==="memory"&&l.has(A)&&(l.get(A).used=!0)}for(let m of n){let g=m[1];typeof g=="string"&&g[0]!=="$"&&(g=+g),e.has(g)&&(e.get(g).used=!0)}let $=a.length>0||n.length>0;if(!$){for(let[,m]of e)if(m.used){$=!0;break}if(!$){for(let[,m]of r)if(m.used){$=!0;break}}if(!$){for(let[,m]of s)if(m.used){$=!0;break}}if(!$){for(let[,m]of l)if(m.used){$=!0;break}}}if(!$){for(let[,m]of e)m.used=!0;for(let[,m]of r)m.used=!0;for(let[,m]of s)m.used=!0;for(let[,m]of l)m.used=!0}for(let m of t.slice(1))!Array.isArray(m)||m[0]!=="elem"||G(m,g=>{if(Array.isArray(g)&&g[0]==="ref.func"){let _=g[1];e.has(_)&&(e.get(_).used=!0)}typeof g=="string"&&g[0]==="$"&&e.has(g)&&(e.get(g).used=!0)});let y=!0;for(;y;){y=!1;for(let[,m]of e)!m.used||m.isImport||G(m.node,g=>{if(!Array.isArray(g)){typeof g=="string"&&g[0]==="$"&&e.has(g)&&!e.get(g).used&&(e.get(g).used=!0,y=!0);return}let[_,A]=g;if((_==="call"||_==="return_call"||_==="ref.func")&&e.has(A)&&!e.get(A).used&&(e.get(A).used=!0,y=!0),(_==="global.get"||_==="global.set")&&r.has(A)&&!r.get(A).used&&(r.get(A).used=!0,y=!0),_==="call_indirect"||_==="return_call_indirect")for(let b of g)typeof b=="string"&&b[0]==="$"&&s.has(b)&&!s.get(b).used&&(s.get(b).used=!0,y=!0);_==="type"&&i.has(A)&&!i.get(A).used&&(i.get(A).used=!0,y=!0)})}let k=["module"];for(let m of t.slice(1)){if(!Array.isArray(m)){k.push(m);continue}let g=m[0];if(g==="func"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:null;(_?e.get(_):[...e.values()].find(b=>b.node===m))?.used&&k.push(m)}else if(g==="global"){let _=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:null;(_?r.get(_):[...r.values()].find(b=>b.node===m))?.used&&k.push(m)}else k.push(m)}return k},$e={"i32.add":(t,e)=>t+e|0,"i32.sub":(t,e)=>t-e|0,"i32.mul":(t,e)=>Math.imul(t,e),"i32.div_s":(t,e)=>e!==0?t/e|0:null,"i32.div_u":(t,e)=>e!==0?(t>>>0)/(e>>>0)|0:null,"i32.rem_s":(t,e)=>e!==0?t%e|0:null,"i32.rem_u":(t,e)=>e!==0?(t>>>0)%(e>>>0)|0:null,"i32.and":(t,e)=>t&e,"i32.or":(t,e)=>t|e,"i32.xor":(t,e)=>t^e,"i32.shl":(t,e)=>t<<(e&31),"i32.shr_s":(t,e)=>t>>(e&31),"i32.shr_u":(t,e)=>t>>>(e&31),"i32.rotl":(t,e)=>(e&=31,t<<e|t>>>32-e|0),"i32.rotr":(t,e)=>(e&=31,t>>>e|t<<32-e|0),"i32.eq":(t,e)=>t===e?1:0,"i32.ne":(t,e)=>t!==e?1:0,"i32.lt_s":(t,e)=>t<e?1:0,"i32.lt_u":(t,e)=>t>>>0<e>>>0?1:0,"i32.gt_s":(t,e)=>t>e?1:0,"i32.gt_u":(t,e)=>t>>>0>e>>>0?1:0,"i32.le_s":(t,e)=>t<=e?1:0,"i32.le_u":(t,e)=>t>>>0<=e>>>0?1:0,"i32.ge_s":(t,e)=>t>=e?1:0,"i32.ge_u":(t,e)=>t>>>0>=e>>>0?1:0,"i32.eqz":t=>t===0?1:0,"i32.clz":t=>Math.clz32(t),"i32.ctz":t=>t===0?32:31-Math.clz32(t&-t),"i32.popcnt":t=>{let e=0;for(;t;)e+=t&1,t>>>=1;return e},"i32.wrap_i64":t=>Number(BigInt.asIntN(32,t)),"i64.add":(t,e)=>BigInt.asIntN(64,t+e),"i64.sub":(t,e)=>BigInt.asIntN(64,t-e),"i64.mul":(t,e)=>BigInt.asIntN(64,t*e),"i64.div_s":(t,e)=>e!==0n?BigInt.asIntN(64,t/e):null,"i64.div_u":(t,e)=>e!==0n?BigInt.asUintN(64,BigInt.asUintN(64,t)/BigInt.asUintN(64,e)):null,"i64.rem_s":(t,e)=>e!==0n?BigInt.asIntN(64,t%e):null,"i64.rem_u":(t,e)=>e!==0n?BigInt.asUintN(64,BigInt.asUintN(64,t)%BigInt.asUintN(64,e)):null,"i64.and":(t,e)=>BigInt.asIntN(64,t&e),"i64.or":(t,e)=>BigInt.asIntN(64,t|e),"i64.xor":(t,e)=>BigInt.asIntN(64,t^e),"i64.shl":(t,e)=>BigInt.asIntN(64,t<<(e&63n)),"i64.shr_s":(t,e)=>BigInt.asIntN(64,t>>(e&63n)),"i64.shr_u":(t,e)=>BigInt.asUintN(64,BigInt.asUintN(64,t)>>(e&63n)),"i64.eq":(t,e)=>t===e?1:0,"i64.ne":(t,e)=>t!==e?1:0,"i64.lt_s":(t,e)=>t<e?1:0,"i64.lt_u":(t,e)=>BigInt.asUintN(64,t)<BigInt.asUintN(64,e)?1:0,"i64.gt_s":(t,e)=>t>e?1:0,"i64.gt_u":(t,e)=>BigInt.asUintN(64,t)>BigInt.asUintN(64,e)?1:0,"i64.le_s":(t,e)=>t<=e?1:0,"i64.le_u":(t,e)=>BigInt.asUintN(64,t)<=BigInt.asUintN(64,e)?1:0,"i64.ge_s":(t,e)=>t>=e?1:0,"i64.ge_u":(t,e)=>BigInt.asUintN(64,t)>=BigInt.asUintN(64,e)?1:0,"i64.eqz":t=>t===0n?1:0,"i64.extend_i32_s":t=>BigInt(t),"i64.extend_i32_u":t=>BigInt(t>>>0),"f32.add":(t,e)=>Math.fround(t+e),"f32.sub":(t,e)=>Math.fround(t-e),"f32.mul":(t,e)=>Math.fround(t*e),"f32.div":(t,e)=>Math.fround(t/e),"f32.neg":t=>Math.fround(-t),"f32.abs":t=>Math.fround(Math.abs(t)),"f32.sqrt":t=>Math.fround(Math.sqrt(t)),"f32.ceil":t=>Math.fround(Math.ceil(t)),"f32.floor":t=>Math.fround(Math.floor(t)),"f32.trunc":t=>Math.fround(Math.trunc(t)),"f32.nearest":t=>Math.fround(Math.round(t)),"f64.add":(t,e)=>t+e,"f64.sub":(t,e)=>t-e,"f64.mul":(t,e)=>t*e,"f64.div":(t,e)=>t/e,"f64.neg":t=>-t,"f64.abs":t=>Math.abs(t),"f64.sqrt":t=>Math.sqrt(t),"f64.ceil":t=>Math.ceil(t),"f64.floor":t=>Math.floor(t),"f64.trunc":t=>Math.trunc(t),"f64.nearest":t=>Math.round(t)},w=t=>{if(!Array.isArray(t)||t.length!==2)return null;let[e,r]=t;return e==="i32.const"?{type:"i32",value:Number(r)|0}:e==="i64.const"?{type:"i64",value:BigInt(r)}:e==="f32.const"?{type:"f32",value:Math.fround(Number(r))}:e==="f64.const"?{type:"f64",value:Number(r)}:null},Pt=(t,e)=>t==="i32"?["i32.const",e|0]:t==="i64"?["i64.const",e]:t==="f32"?["f32.const",Math.fround(e)]:t==="f64"?["f64.const",e]:null,Ie=t=>K(O(t),e=>{if(!Array.isArray(e))return;let r=e[0],i=$e[r];if(i){if(i.length===1&&e.length===2){let s=w(e[1]);if(!s)return;let l=i(s.value);if(l===null)return;let a=r.startsWith("i64.")&&!r.includes("eqz")?"i64":r.startsWith("f32.")?"f32":r.startsWith("f64.")?"f64":"i32";return Pt(a,l)}if(i.length===2&&e.length===3){let s=w(e[1]),l=w(e[2]);if(!s||!l)return;let a=i(s.value,l.value);if(a===null)return;let c=/\.(eq|ne|[lg][te])/.test(r)?"i32":r.startsWith("i64.")?"i64":r.startsWith("f32.")?"f32":r.startsWith("f64.")?"f64":"i32";return Pt(c,a)}}}),ke={"i32.add":(t,e)=>{let r=w(t),i=w(e);return r?.value===0?e:i?.value===0?t:null},"i64.add":(t,e)=>{let r=w(t),i=w(e);return r?.value===0n?e:i?.value===0n?t:null},"i32.sub":(t,e)=>w(e)?.value===0?t:null,"i64.sub":(t,e)=>w(e)?.value===0n?t:null,"i32.mul":(t,e)=>{let r=w(t),i=w(e);return r?.value===1?e:i?.value===1?t:null},"i64.mul":(t,e)=>{let r=w(t),i=w(e);return r?.value===1n?e:i?.value===1n?t:null},"i32.div_s":(t,e)=>w(e)?.value===1?t:null,"i32.div_u":(t,e)=>w(e)?.value===1?t:null,"i64.div_s":(t,e)=>w(e)?.value===1n?t:null,"i64.div_u":(t,e)=>w(e)?.value===1n?t:null,"i32.and":(t,e)=>{let r=w(t),i=w(e);return r?.value===-1?e:i?.value===-1?t:null},"i64.and":(t,e)=>{let r=w(t),i=w(e);return r?.value===-1n?e:i?.value===-1n?t:null},"i32.or":(t,e)=>{let r=w(t),i=w(e);return r?.value===0?e:i?.value===0?t:null},"i64.or":(t,e)=>{let r=w(t),i=w(e);return r?.value===0n?e:i?.value===0n?t:null},"i32.xor":(t,e)=>{let r=w(t),i=w(e);return r?.value===0?e:i?.value===0?t:null},"i64.xor":(t,e)=>{let r=w(t),i=w(e);return r?.value===0n?e:i?.value===0n?t:null},"i32.shl":(t,e)=>w(e)?.value===0?t:null,"i32.shr_s":(t,e)=>w(e)?.value===0?t:null,"i32.shr_u":(t,e)=>w(e)?.value===0?t:null,"i64.shl":(t,e)=>w(e)?.value===0n?t:null,"i64.shr_s":(t,e)=>w(e)?.value===0n?t:null,"i64.shr_u":(t,e)=>w(e)?.value===0n?t:null},Ne=t=>K(O(t),e=>{if(!Array.isArray(e)||e.length!==3)return;let r=ke[e[0]];if(!r)return;let i=r(e[1],e[2]);if(i!==null)return i}),Be=t=>K(O(t),e=>{if(!Array.isArray(e)||e.length!==3)return;let[r,i,s]=e;if(r==="i32.mul"){let l=w(s);if(l&&l.value>0&&!(l.value&l.value-1)){let n=Math.log2(l.value);if(Number.isInteger(n))return["i32.shl",i,["i32.const",n]]}let a=w(i);if(a&&a.value>0&&!(a.value&a.value-1)){let n=Math.log2(a.value);if(Number.isInteger(n))return["i32.shl",s,["i32.const",n]]}}if(r==="i64.mul"){let l=w(s);if(l&&l.value>0n&&(l.value&l.value-1n)===0n){let n=BigInt(l.value.toString(2).length-1);return["i64.shl",i,["i64.const",n]]}let a=w(i);if(a&&a.value>0n&&(a.value&a.value-1n)===0n){let n=BigInt(a.value.toString(2).length-1);return["i64.shl",s,["i64.const",n]]}}if(r==="i32.div_u"){let l=w(s);if(l&&l.value>0&&!(l.value&l.value-1)){let a=Math.log2(l.value);if(Number.isInteger(a))return["i32.shr_u",i,["i32.const",a]]}}if(r==="i64.div_u"){let l=w(s);if(l&&l.value>0n&&(l.value&l.value-1n)===0n){let a=BigInt(l.value.toString(2).length-1);return["i64.shr_u",i,["i64.const",a]]}}if(r==="i32.rem_u"){let l=w(s);if(l&&l.value>0&&!(l.value&l.value-1))return["i32.and",i,["i32.const",l.value-1]]}if(r==="i64.rem_u"){let l=w(s);if(l&&l.value>0n&&(l.value&l.value-1n)===0n)return["i64.and",i,["i64.const",l.value-1n]]}}),Me=t=>K(O(t),e=>{if(!Array.isArray(e))return;let r=e[0];if(r==="if"){let i=1;for(;i<e.length;){let c=e[i];if(Array.isArray(c)&&(c[0]==="then"||c[0]==="else"||c[0]==="result"||c[0]==="param")){i++;continue}break}let s=e[i],l=w(s);if(!l)return;let a=null,n=null;for(let c=i+1;c<e.length;c++){let f=e[c];Array.isArray(f)&&(f[0]==="then"?a=f:f[0]==="else"&&(n=f))}if(l.value!==0&&l.value!==0n){if(a&&a.length>1){let c=a.slice(1);return c.length===1?c[0]:["block",...c]}return["nop"]}else{if(n&&n.length>1){let c=n.slice(1);return c.length===1?c[0]:["block",...c]}return["nop"]}}if(r==="br_if"&&e.length>=3){let i=e[e.length-1],s=w(i);return s?s.value===0||s.value===0n?["nop"]:["br",e[1]]:void 0}if(r==="select"&&e.length>=4){let i=e[e.length-1],s=w(i);return s?s.value===0||s.value===0n?e[2]:e[1]:void 0}}),jt=new Set(["unreachable","return","br","br_table"]),ze=t=>{let e=O(t);return G(e,r=>{if(!Array.isArray(r))return;let i=r[0];if((i==="func"||i==="block"||i==="loop")&&Rt(r),i==="if")for(let s=1;s<r.length;s++)Array.isArray(r[s])&&(r[s][0]==="then"||r[s][0]==="else")&&Rt(r[s])}),e},Rt=t=>{let e=!1,r=-1;for(let i=1;i<t.length;i++){let s=t[i];if(Array.isArray(s)){let l=s[0];if(l==="param"||l==="result"||l==="local"||l==="type"||l==="export")continue;e&&r===-1&&(r=i),jt.has(l)&&(e=!0,r=i+1)}else typeof s=="string"&&(e&&r===-1&&(r=i),jt.has(s)&&(e=!0,r=i+1))}r>0&&r<t.length&&t.splice(r)},Ue=t=>{let e=O(t);return G(e,r=>{if(!Array.isArray(r)||r[0]!=="func")return;let i=[],s=new Map,l=new Set;for(let a=1;a<r.length;a++){let n=r[a];Array.isArray(n)&&(n[0]==="local"&&(i.push({idx:a,node:n}),typeof n[1]=="string"&&n[1][0]==="$"&&s.set(n[1],n[2])),n[0]==="param"&&typeof n[1]=="string"&&n[1][0]==="$"&&(s.set(n[1],n[2]),l.add(n[1])))}G(r,a=>{if(!Array.isArray(a))return;let n=a[0];if(n==="local.get"||n==="local.set"||n==="local.tee"){let c=a[1];typeof c=="string"&&l.add(c)}});for(let a=i.length-1;a>=0;a--){let{idx:n,node:c}=i[a],f=typeof c[1]=="string"&&c[1][0]==="$"?c[1]:null;f&&!l.has(f)&&r.splice(n,1)}}),e},Te=t=>{let e=O(t);return G(e,r=>{if(!Array.isArray(r)||r[0]!=="func")return;let i=new Map;((l,a=1)=>{for(let n=a;n<l.length;n++){let c=l[n];if(!Array.isArray(c))continue;let f=c[0];if(f==="local.set"&&c.length===3){let o=c[1],u=c[2];w(u)&&typeof o=="string"?i.set(o,u):typeof o=="string"&&i.delete(o)}else if(f==="local.tee"&&c.length===3){let o=c[1],u=c[2];w(u)&&typeof o=="string"?i.set(o,u):typeof o=="string"&&i.delete(o)}else if(f==="local.get"&&c.length===2){let o=c[1];if(typeof o=="string"&&i.has(o)){let u=i.get(o);c.length=0,c.push(...O(u))}}else(f==="block"||f==="loop"||f==="if"||f==="call"||f==="call_indirect")&&i.clear();K(c,o=>{if(!Array.isArray(o)||o[0]!=="local.get"||o.length!==2)return;let u=o[1];if(typeof u=="string"&&i.has(u)){let d=i.get(u);return O(d)}})}})(r)}),e},Ee=t=>{if(!Array.isArray(t)||t[0]!=="module")return t;let e=O(t),r=new Map;for(let i of e.slice(1)){if(!Array.isArray(i)||i[0]!=="func")continue;let s=typeof i[1]=="string"&&i[1][0]==="$"?i[1]:null;if(!s)continue;let l=[],a=[],n=!1,c=!1;for(let f=1;f<i.length;f++){let o=i[f];if(Array.isArray(o))if(o[0]==="param")if(typeof o[1]=="string"&&o[1][0]==="$")l.push({name:o[1],type:o[2]});else{l=null;break}else o[0]==="local"?n=!0:o[0]==="export"?c=!0:o[0]!=="result"&&o[0]!=="type"&&a.push(o)}if(l&&!n&&!c&&l.length<=2&&a.length===1){let f=new Set(l.map(u=>u.name)),o=!1;G(a[0],u=>{Array.isArray(u)&&(u[0]==="local.set"||u[0]==="local.tee")&&f.has(u[1])&&(o=!0)}),o||r.set(s,{body:a[0],params:l})}}return r.size===0||K(e,i=>{if(!Array.isArray(i)||i[0]!=="call")return;let s=i[1];if(!r.has(s))return;let{body:l,params:a}=r.get(s),n=i.slice(2);if(a.length===0)return O(l);let c=O(l);return K(c,f=>{if(!Array.isArray(f)||f[0]!=="local.get")return;let o=f[1],u=a.findIndex(d=>d.name===o);if(u!==-1&&n[u])return O(n[u])}),c}),e};function xt(t,e=!0){return typeof t=="string"&&(t=D(t)),t=O(t),e=we(e),e.fold&&(t=Ie(t)),e.identity&&(t=Ne(t)),e.strength&&(t=Be(t)),e.branch&&(t=Me(t)),e.propagate&&(t=Te(t)),e.inline&&(t=Ee(t)),e.deadcode&&(t=ze(t)),e.locals&&(t=Ue(t)),e.treeshake&&(t=ve(t)),t}var Vt="\uE000",Ht=t=>{if(!t||typeof t!="string")return null;let e=t.split(".")[0];return/^[if](32|64)|v128/.test(e)?e:/\.(eq|ne|[lg][te]|eqz)/.test(t)||t==="memory.size"||t==="memory.grow"?"i32":null},qe=(t,e={})=>{if(!Array.isArray(t))return typeof t=="string"&&t[0]==="$"&&e.locals?.[t]?e.locals[t]:null;let[r,...i]=t;return Ht(r)?Ht(r):r==="local.get"&&e.locals?.[i[0]]?e.locals[i[0]]:r==="call"&&e.funcs?.[i[0]]?e.funcs[i[0]].result?.[0]:null};function It(t,e){if(t=e(t),Array.isArray(t))for(let r=0;r<t.length;r++){let i=It(t[r],e);i?._splice?(t.splice(r,1,...i),r+=i.length-1):t[r]=i}return t}function Se(t,e){let r=[],i=new Map;return It(t,s=>{if(!Array.isArray(s))return s;if(s[0]==="call"&&typeof s[1]=="function"){let l=s[1];if(!i.has(l)){let n=[];for(let o=2;o<s.length;o++){let u=qe(s[o]);u&&n.push(u)}let c=r.length,f=l.name||`$fn${c}`;i.set(l,{idx:c,name:f.startsWith("$")?f:"$"+f,params:n,fn:l}),r.push(i.get(l))}let a=i.get(l);s[1]=a.name}return s}),r}function Fe(t){return t.map(({name:e,params:r})=>["import",'"env"',`"${e.slice(1)}"`,["func",e,...r.map(i=>["param",i])]])}function Oe(t,...e){let r={};if(!Array.isArray(t)&&e.length&&typeof e[e.length-1]=="object"&&e[e.length-1]!==null&&!(e[e.length-1]instanceof Uint8Array)&&(r=e.pop()),Array.isArray(t)&&t.raw){let i=t[0];for(let f=0;f<e.length;f++)i+=Vt+t[f+1];let s=D(i),l=[],a=0;s=It(s,f=>{if(f===Vt){let o=e[a++];if(typeof o=="function")return l.push(o),o;if(typeof o=="string"&&(o[0]==="("||/^\s*\(/.test(o))){let u=D(o);return Array.isArray(u)&&Array.isArray(u[0])&&(u._splice=!0),u}return o instanceof Uint8Array?[...o]:o}return f});let n=null;if(l.length){let f=Se(s,l);if(f.length){let o=Fe(f);s[0]==="module"?s.splice(1,0,...o):typeof s[0]=="string"?s=[...o,s]:s.unshift(...o),n={env:{}};for(let u of f)n.env[u.name.slice(1)]=u.fn}}r.polyfill&&(s=gt(s,r.polyfill)),r.optimize&&(s=xt(s,r.optimize));let c=rt(s);return n&&(c._imports=n),c}if(r.polyfill||r.optimize){let i=typeof t=="string"?D(t):t;return r.polyfill&&(i=gt(i,r.polyfill)),r.optimize&&(i=xt(i,r.optimize)),rt(i)}return rt(t)}function We(t,...e){let r=Oe(t,...e),i=new WebAssembly.Module(r);return new WebAssembly.Instance(i,r._imports).exports}var fr=We;export{Oe as compile,fr as default,xt as optimize,D as parse,gt as polyfill,Ot as print,We as watr};
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# <img src="./watr.svg" height="16"> watr [](https://bundlephobia.com/package/watr) [](https://npmjs.org/watr) [](https://github.com/dy/watr/actions/workflows/test.js.yml)
|
|
2
2
|
|
|
3
|
-
<div align="left">
|
|
4
3
|
|
|
5
4
|
_Light & fast WAT compiler_
|
|
6
5
|
|
|
@@ -13,29 +12,25 @@ _Light & fast WAT compiler_
|
|
|
13
12
|
|
|
14
13
|
<!-- _Use for_: backends, compilers, DSLs, codegen, dev tools -->
|
|
15
14
|
|
|
16
|
-
</div>
|
|
17
15
|
|
|
18
16
|
## Usage
|
|
19
17
|
|
|
20
18
|
```js
|
|
21
|
-
import watr, { compile,
|
|
19
|
+
import watr, { compile, parse, print } from 'watr'
|
|
22
20
|
|
|
23
21
|
// compile to binary
|
|
24
|
-
const binary = compile('(func (export "f") (result f64) (f64.const 1))'
|
|
22
|
+
const binary = compile('(func (export "f") (result f64) (f64.const 1))', {
|
|
23
|
+
polyfill: false, // transform newer features to MVP
|
|
24
|
+
optimize: true // fold constants, treeshake, eliminate dead code ...
|
|
25
|
+
})
|
|
25
26
|
const module = new WebAssembly.Module(binary)
|
|
26
27
|
const { f } = new WebAssembly.Instance(module).exports
|
|
27
28
|
|
|
28
29
|
// parse
|
|
29
30
|
parse('(i32.const 42)') // ['i32.const', 42]
|
|
30
31
|
|
|
31
|
-
// polyfill (transform newer features to MVP)
|
|
32
|
-
print(polyfill('(func (i32.extend8_s ...))')) // (func (i32.shr_s (i32.shl ...) ...))
|
|
33
|
-
|
|
34
|
-
// optimize (constant folding, treeshake, dead code elimination)
|
|
35
|
-
print(optimize('(func (i32.add (i32.const 1) (i32.const 2)))')) // (func (i32.const 3))
|
|
36
|
-
|
|
37
32
|
// print
|
|
38
|
-
print('(module(func(result i32)i32.const 42))') // (module\n (func (result i32)\n
|
|
33
|
+
print('(module(func(result i32)i32.const 42))') // (module\n (func (result i32)\n ...
|
|
39
34
|
|
|
40
35
|
// instant wasm function
|
|
41
36
|
const { add } = watr`(func (export "add") (param i32 i32) (result i32)
|
|
@@ -43,7 +38,7 @@ const { add } = watr`(func (export "add") (param i32 i32) (result i32)
|
|
|
43
38
|
)`
|
|
44
39
|
add(2, 3) // 5
|
|
45
40
|
|
|
46
|
-
// auto-import
|
|
41
|
+
// instant wasm: interpolate, auto-import ...
|
|
47
42
|
const { test } = watr`(func (export "test") (call ${console.log} (i32.const 42)))`
|
|
48
43
|
test() // logs 42
|
|
49
44
|
```
|
package/src/compile.js
CHANGED
|
@@ -56,6 +56,20 @@ export default function compile(nodes) {
|
|
|
56
56
|
// quote "a" "b"
|
|
57
57
|
if (nodes[idx] === 'quote') return compile(nodes.slice(++idx).map(v => v.valueOf().slice(1, -1)).flat().join(''))
|
|
58
58
|
|
|
59
|
+
// expand grouped imports: (import "mod" (item "name" type)*) -> individual imports
|
|
60
|
+
// compact import section (Phase 3)
|
|
61
|
+
nodes = nodes.flatMap((n, i) => {
|
|
62
|
+
if (i < idx || !Array.isArray(n) || n[0] !== 'import') return [n]
|
|
63
|
+
const [, mod, ...rest] = n
|
|
64
|
+
if (!rest.some(r => Array.isArray(r) && r[0] === 'item')) return [n]
|
|
65
|
+
const lastIsType = Array.isArray(rest.at(-1)) && rest.at(-1)[0] !== 'item'
|
|
66
|
+
if (lastIsType) {
|
|
67
|
+
const type = rest.at(-1)
|
|
68
|
+
return rest.slice(0, -1).filter(r => r[0] === 'item').map(([, nm]) => ['import', mod, nm, type])
|
|
69
|
+
}
|
|
70
|
+
return rest.filter(r => r[0] === 'item').map(([, nm, type]) => ['import', mod, nm, type])
|
|
71
|
+
})
|
|
72
|
+
|
|
59
73
|
// scopes are aliased by key as well, eg. section.func.$name = section[SECTION.func] = idx
|
|
60
74
|
const ctx = []
|
|
61
75
|
for (let kind in SECTION) (ctx[SECTION[kind]] = ctx[kind] = []).name = kind
|
|
@@ -91,8 +105,12 @@ export default function compile(nodes) {
|
|
|
91
105
|
// add rest of subtypes as regular type nodes with subtype flag
|
|
92
106
|
for (let i = 0; i < node.length; i++) {
|
|
93
107
|
let [, ...subnode] = node[i]
|
|
94
|
-
name(subnode, ctx.type)
|
|
95
|
-
|
|
108
|
+
name(subnode, ctx.type)
|
|
109
|
+
// extract top-level descriptor/describes (custom descriptors, Phase 3)
|
|
110
|
+
const tdesc = []
|
|
111
|
+
while (subnode[0]?.[0] === 'descriptor' || subnode[0]?.[0] === 'describes') tdesc.push(subnode.shift())
|
|
112
|
+
;(subnode = typedef(subnode, ctx)).push(i ? true : [ctx.type.length, node.length])
|
|
113
|
+
if (tdesc.length) subnode.desc = subnode.desc ? [...tdesc, ...subnode.desc] : tdesc
|
|
96
114
|
ctx.type.push(subnode)
|
|
97
115
|
}
|
|
98
116
|
}
|
|
@@ -101,8 +119,13 @@ export default function compile(nodes) {
|
|
|
101
119
|
// (type (struct (field a)*)
|
|
102
120
|
// (type (sub final? $nm* (struct|array|func ...)))
|
|
103
121
|
else if (kind === 'type') {
|
|
104
|
-
name(node, ctx.type)
|
|
105
|
-
|
|
122
|
+
name(node, ctx.type)
|
|
123
|
+
// extract top-level descriptor/describes (custom descriptors, Phase 3)
|
|
124
|
+
const tdesc = []
|
|
125
|
+
while (node[0]?.[0] === 'descriptor' || node[0]?.[0] === 'describes') tdesc.push(node.shift())
|
|
126
|
+
const td = typedef(node, ctx)
|
|
127
|
+
if (tdesc.length) td.desc = td.desc ? [...tdesc, ...td.desc] : tdesc
|
|
128
|
+
ctx.type.push(td)
|
|
106
129
|
}
|
|
107
130
|
// other sections may have id
|
|
108
131
|
else if (kind === 'start' || kind === 'export') ctx[kind].push(node)
|
|
@@ -146,7 +169,8 @@ export default function compile(nodes) {
|
|
|
146
169
|
else if (kind === 'memory') {
|
|
147
170
|
const is64 = node[0] === 'i64', idx = is64 ? 1 : 0
|
|
148
171
|
if (node[idx]?.[0] === 'data') {
|
|
149
|
-
|
|
172
|
+
const ps = (node.find(n => Array.isArray(n) && n[0] === 'pagesize')?.[1]) ?? 65536
|
|
173
|
+
let [, ...data] = node.splice(idx, 1)[0], m = '' + Math.ceil(data.reduce((s, d) => s + d.length, 0) / ps)
|
|
150
174
|
ctx.data.push([['memory', items.length], [is64 ? 'i64.const' : 'i32.const', is64 ? 0n : 0], ...data])
|
|
151
175
|
node = is64 ? ['i64', m, m] : [m, m]
|
|
152
176
|
}
|
|
@@ -318,7 +342,7 @@ function normalize(nodes, ctx) {
|
|
|
318
342
|
else {
|
|
319
343
|
const imm = []
|
|
320
344
|
// Collect immediate operands (non-arrays or special forms like type/param/result/ref)
|
|
321
|
-
while (parts.length && (!Array.isArray(parts[0]) || 'type,param,result,ref'.includes(parts[0][0]))) imm.push(parts.shift())
|
|
345
|
+
while (parts.length && (!Array.isArray(parts[0]) || 'type,param,result,ref,exact,on'.includes(parts[0][0]))) imm.push(parts.shift())
|
|
322
346
|
out.push(...normalize(parts, ctx), op, ...imm)
|
|
323
347
|
nodes.unshift(...out.splice(out.length - 1 - imm.length))
|
|
324
348
|
}
|
|
@@ -418,18 +442,20 @@ const name = (node, list) => {
|
|
|
418
442
|
}
|
|
419
443
|
|
|
420
444
|
/**
|
|
421
|
-
* Parse type definition: func, array, struct, or sub(type).
|
|
422
|
-
* Handles recursive types and
|
|
445
|
+
* Parse type definition: func, array, struct, cont, or sub(type).
|
|
446
|
+
* Handles recursive types, subtyping, and custom descriptor clauses (Phase 3).
|
|
423
447
|
*
|
|
424
|
-
* @param {Array} node - [definition] where definition is func/array/struct/sub
|
|
448
|
+
* @param {Array} node - [definition] where definition is func/array/struct/cont/sub
|
|
425
449
|
* @param {Object} ctx - Compilation context
|
|
426
|
-
* @returns {
|
|
450
|
+
* @returns {Array} [kind, fields, subkind, supertypes] with optional .desc property
|
|
427
451
|
*/
|
|
428
452
|
const typedef = ([dfn], ctx) => {
|
|
429
|
-
let subkind = 'subfinal', supertypes = [], compkind
|
|
453
|
+
let subkind = 'subfinal', supertypes = [], compkind, desc = []
|
|
430
454
|
if (dfn[0] === 'sub') {
|
|
431
455
|
subkind = dfn.shift(), dfn[0] === 'final' && (subkind += dfn.shift())
|
|
432
456
|
dfn = (supertypes = dfn).pop() // last item is definition
|
|
457
|
+
// extract descriptor/describes from supertypes (custom descriptors, Phase 3)
|
|
458
|
+
supertypes = supertypes.filter(n => Array.isArray(n) && (n[0] === 'descriptor' || n[0] === 'describes') ? (desc.push(n), false) : true)
|
|
433
459
|
}
|
|
434
460
|
|
|
435
461
|
[compkind, ...dfn] = dfn // composite type kind
|
|
@@ -437,8 +463,11 @@ const typedef = ([dfn], ctx) => {
|
|
|
437
463
|
if (compkind === 'func') dfn = paramres(dfn), ctx.type['$' + dfn.join('>')] ??= ctx.type.length
|
|
438
464
|
else if (compkind === 'struct') dfn = fieldseq(dfn, 'field')
|
|
439
465
|
else if (compkind === 'array') [dfn] = dfn
|
|
466
|
+
// cont type: (cont $ft) - continuation wrapping function type (stack switching, Phase 3)
|
|
440
467
|
|
|
441
|
-
|
|
468
|
+
const result = [compkind, dfn, subkind, supertypes]
|
|
469
|
+
if (desc.length) result.desc = desc
|
|
470
|
+
return result
|
|
442
471
|
}
|
|
443
472
|
|
|
444
473
|
|
|
@@ -460,40 +489,50 @@ const build = [
|
|
|
460
489
|
// (func params result)
|
|
461
490
|
// (array i8)
|
|
462
491
|
// (struct ...fields)
|
|
463
|
-
(
|
|
492
|
+
// (cont $ft) - stack switching (Phase 3)
|
|
493
|
+
(node, ctx) => {
|
|
494
|
+
const [kind, fields, subkind, supertypes, rec] = node
|
|
464
495
|
if (rec === true) return // ignore rec subtypes cept for 1st one
|
|
465
496
|
|
|
466
|
-
|
|
497
|
+
// descriptor/describes prefix bytes (custom descriptors, Phase 3)
|
|
498
|
+
const descPfx = (node.desc ?? []).flatMap(([clause, ref]) =>
|
|
499
|
+
[clause === 'descriptor' ? 0x4D : 0x4C, ...uleb(id(ref, ctx.type))])
|
|
500
|
+
|
|
501
|
+
// build comptype bytes without sub wrapper or descriptor prefix
|
|
502
|
+
const comptype = (k, f) => {
|
|
503
|
+
if (k === 'func') return [DEFTYPE.func, ...vec(f[0].map(t => reftype(t, ctx))), ...vec(f[1].map(t => reftype(t, ctx)))]
|
|
504
|
+
if (k === 'array') return [DEFTYPE.array, ...fieldtype(f, ctx)]
|
|
505
|
+
if (k === 'struct') return [DEFTYPE.struct, ...vec(f.map(t => fieldtype(t, ctx)))]
|
|
506
|
+
if (k === 'cont') return [DEFTYPE.cont, ...uleb(id(f[0] ?? f, ctx.type))]
|
|
507
|
+
return [DEFTYPE[k]]
|
|
508
|
+
}
|
|
509
|
+
|
|
467
510
|
// (rec (sub ...)*)
|
|
468
511
|
if (rec) {
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
512
|
+
let [from, length] = rec
|
|
513
|
+
const subtypes = Array.from({ length }, (_, i) => {
|
|
514
|
+
const t = ctx.type[from + i], sub = t.slice(0, 4)
|
|
515
|
+
if (t.desc) sub.desc = t.desc
|
|
516
|
+
return build[SECTION.type](sub, ctx)
|
|
517
|
+
})
|
|
518
|
+
return [DEFTYPE.rec, ...vec(subtypes)]
|
|
472
519
|
}
|
|
473
520
|
// (sub final? sups* (type...))
|
|
474
521
|
else if (subkind === 'sub' || supertypes?.length) {
|
|
475
|
-
|
|
476
|
-
kind = subkind
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
else if (kind === 'func') {
|
|
480
|
-
details = [...vec(fields[0].map(t => reftype(t, ctx))), ...vec(fields[1].map(t => reftype(t, ctx)))]
|
|
481
|
-
}
|
|
482
|
-
else if (kind === 'array') {
|
|
483
|
-
details = fieldtype(fields, ctx)
|
|
484
|
-
}
|
|
485
|
-
else if (kind === 'struct') {
|
|
486
|
-
details = vec(fields.map(t => fieldtype(t, ctx)))
|
|
522
|
+
return [DEFTYPE[subkind], ...vec(supertypes.map(n => id(n, ctx.type))), ...descPfx, ...comptype(kind, fields)]
|
|
487
523
|
}
|
|
488
524
|
|
|
489
|
-
return [
|
|
525
|
+
return [...descPfx, ...comptype(kind, fields)]
|
|
490
526
|
},
|
|
491
527
|
|
|
492
528
|
// (import "math" "add" (func|table|global|memory|tag dfn?))
|
|
493
529
|
([mod, field, [kind, ...dfn]], ctx) => {
|
|
494
|
-
let details
|
|
530
|
+
let details, kindByte = KIND[kind]
|
|
495
531
|
|
|
496
532
|
if (kind === 'func') {
|
|
533
|
+
// exact func import: (func exact (type $t)) - custom descriptors (Phase 3)
|
|
534
|
+
const isExact = dfn[0] === 'exact' && dfn.shift()
|
|
535
|
+
if (isExact) kindByte = 0x20
|
|
497
536
|
// we track imported funcs in func section to share namespace, and skip them on final build
|
|
498
537
|
let [[, typeidx]] = dfn
|
|
499
538
|
details = uleb(id(typeidx, ctx.type))
|
|
@@ -513,7 +552,7 @@ const build = [
|
|
|
513
552
|
}
|
|
514
553
|
else err(`Unknown kind ${kind}`)
|
|
515
554
|
|
|
516
|
-
return ([...vec(mod), ...vec(field),
|
|
555
|
+
return ([...vec(mod), ...vec(field), kindByte, ...details])
|
|
517
556
|
},
|
|
518
557
|
|
|
519
558
|
// (func $name? ...params result ...body)
|
|
@@ -677,6 +716,7 @@ const build = [
|
|
|
677
716
|
// (data (i32.const 0) "\aa" "\bb"?)
|
|
678
717
|
// (data (memory ref) (offset (i32.const 0)) "\aa" "\bb"?)
|
|
679
718
|
// (data (global.get $x) "\aa" "\bb"?)
|
|
719
|
+
// (data (i8 1 2 3) ...) numeric values (WAT numeric values, Phase 2)
|
|
680
720
|
(inits, ctx) => {
|
|
681
721
|
let offset, memidx = 0
|
|
682
722
|
|
|
@@ -707,7 +747,7 @@ const build = [
|
|
|
707
747
|
// passive: 1
|
|
708
748
|
[1]
|
|
709
749
|
),
|
|
710
|
-
...vec(inits.
|
|
750
|
+
...vec(inits.flatMap(item => numdata(item) ?? [...item]))
|
|
711
751
|
])
|
|
712
752
|
},
|
|
713
753
|
|
|
@@ -720,10 +760,15 @@ const build = [
|
|
|
720
760
|
|
|
721
761
|
// Build reference type encoding (ref/refnull forms, not related to regtype which handles func types)
|
|
722
762
|
// https://webassembly.github.io/gc/core/binary/types.html#reference-types
|
|
763
|
+
// (exact $T) support added for custom descriptors (Phase 3): encoded as 0x62 typeidx
|
|
723
764
|
const reftype = (t, ctx) => (
|
|
724
765
|
t[0] === 'ref' ?
|
|
725
766
|
t[1] == 'null' ?
|
|
767
|
+
// (ref null (exact $T)) - exact nullable ref
|
|
768
|
+
Array.isArray(t[2]) && t[2][0] === 'exact' ? [TYPE.refnull, 0x62, ...uleb(id(t[2][1], ctx.type))] :
|
|
726
769
|
TYPE[t[2]] ? [TYPE[t[2]]] : [TYPE.refnull, ...uleb(id(t[t.length - 1], ctx.type))] :
|
|
770
|
+
// (ref (exact $T)) - exact non-null ref
|
|
771
|
+
Array.isArray(t[1]) && t[1][0] === 'exact' ? [TYPE.ref, 0x62, ...uleb(id(t[1][1], ctx.type))] :
|
|
727
772
|
[TYPE.ref, ...uleb(TYPE[t[t.length - 1]] || id(t[t.length - 1], ctx.type))] :
|
|
728
773
|
// abbrs
|
|
729
774
|
[TYPE[t] ?? err(`Unknown type ${t}`)]
|
|
@@ -770,11 +815,11 @@ const IMM = {
|
|
|
770
815
|
return [...uleb(count - 1), ...labels]
|
|
771
816
|
},
|
|
772
817
|
select: (n, c) => { let r = n.shift() || []; return r.length ? vec(r.map(t => reftype(t, c))) : [] },
|
|
773
|
-
ref_null: (n, c) => { let t = n.shift(); return TYPE[t] ? [TYPE[t]] : uleb(id(t, c.type)) },
|
|
818
|
+
ref_null: (n, c) => { let t = n.shift(); return Array.isArray(t) && t[0] === 'exact' ? [0x62, ...uleb(id(t[1], c.type))] : TYPE[t] ? [TYPE[t]] : uleb(id(t, c.type)) },
|
|
774
819
|
memarg: (n, c, op) => memargEnc(n, op, isIdx(n[0]) && !isMemParam(n[0]) ? id(n.shift(), c.memory) : 0),
|
|
775
820
|
opt_memory: (n, c) => uleb(id(isIdx(n[0]) ? n.shift() : 0, c.memory)),
|
|
776
821
|
reftype: (n, c) => { let ht = reftype(n.shift(), c); return ht.length > 1 ? ht.slice(1) : ht },
|
|
777
|
-
reftype2: (n, c) => { let b = blockid(n.shift(), c.block), h1 = reftype(n.shift(), c), h2 = reftype(n.shift(), c); return [((h2[0] !== TYPE.ref) << 1) | (h1[0] !== TYPE.ref), ...uleb(b), h1
|
|
822
|
+
reftype2: (n, c) => { let b = blockid(n.shift(), c.block), h1 = reftype(n.shift(), c), h2 = reftype(n.shift(), c), ht = h => h.length > 1 ? h.slice(1) : h; return [((h2[0] !== TYPE.ref) << 1) | (h1[0] !== TYPE.ref), ...uleb(b), ...ht(h1), ...ht(h2)] },
|
|
778
823
|
v128const: (n) => {
|
|
779
824
|
let [t, num] = n.shift().split('x'), bits = +t.slice(1), stride = bits >>> 3; num = +num
|
|
780
825
|
if (t[0] === 'i') {
|
|
@@ -828,7 +873,45 @@ const IMM = {
|
|
|
828
873
|
typeidx_typeidx: (n, c) => [...uleb(id(n.shift(), c.type)), ...uleb(id(n.shift(), c.type))],
|
|
829
874
|
dataidx_memoryidx: (n, c) => [...uleb(id(n.shift(), c.data)), ...uleb(id(n.shift(), c.memory))],
|
|
830
875
|
memoryidx_memoryidx: (n, c) => [...uleb(id(n.shift(), c.memory)), ...uleb(id(n.shift(), c.memory))],
|
|
831
|
-
tableidx_tableidx: (n, c) => [...uleb(id(n.shift(), c.table)), ...uleb(id(n.shift(), c.table))]
|
|
876
|
+
tableidx_tableidx: (n, c) => [...uleb(id(n.shift(), c.table)), ...uleb(id(n.shift(), c.table))],
|
|
877
|
+
|
|
878
|
+
// stack switching handlers (Phase 3)
|
|
879
|
+
cont_bind: (n, c) => [...uleb(id(n.shift(), c.type)), ...uleb(id(n.shift(), c.type))],
|
|
880
|
+
switch_cont: (n, c) => [...uleb(id(n.shift(), c.type)), ...uleb(id(n.shift(), c.tag))],
|
|
881
|
+
resume: (n, c) => {
|
|
882
|
+
const typeidx = uleb(id(n.shift(), c.type))
|
|
883
|
+
const handlers = []; let cnt = 0
|
|
884
|
+
while (n[0]?.[0] === 'on') {
|
|
885
|
+
const [, tag, label] = n.shift()
|
|
886
|
+
if (label === 'switch') handlers.push(0x01, ...uleb(id(tag, c.tag)))
|
|
887
|
+
else handlers.push(0x00, ...uleb(id(tag, c.tag)), ...uleb(blockid(label, c.block)))
|
|
888
|
+
cnt++
|
|
889
|
+
}
|
|
890
|
+
return [...typeidx, ...uleb(cnt), ...handlers]
|
|
891
|
+
},
|
|
892
|
+
resume_throw: (n, c) => {
|
|
893
|
+
const typeidx = uleb(id(n.shift(), c.type))
|
|
894
|
+
const exnidx = uleb(id(n.shift(), c.tag))
|
|
895
|
+
const handlers = []; let cnt = 0
|
|
896
|
+
while (n[0]?.[0] === 'on') {
|
|
897
|
+
const [, tag, label] = n.shift()
|
|
898
|
+
if (label === 'switch') handlers.push(0x01, ...uleb(id(tag, c.tag)))
|
|
899
|
+
else handlers.push(0x00, ...uleb(id(tag, c.tag)), ...uleb(blockid(label, c.block)))
|
|
900
|
+
cnt++
|
|
901
|
+
}
|
|
902
|
+
return [...typeidx, ...exnidx, ...uleb(cnt), ...handlers]
|
|
903
|
+
},
|
|
904
|
+
resume_throw_ref: (n, c) => {
|
|
905
|
+
const typeidx = uleb(id(n.shift(), c.type))
|
|
906
|
+
const handlers = []; let cnt = 0
|
|
907
|
+
while (n[0]?.[0] === 'on') {
|
|
908
|
+
const [, tag, label] = n.shift()
|
|
909
|
+
if (label === 'switch') handlers.push(0x01, ...uleb(id(tag, c.tag)))
|
|
910
|
+
else handlers.push(0x00, ...uleb(id(tag, c.tag)), ...uleb(blockid(label, c.block)))
|
|
911
|
+
cnt++
|
|
912
|
+
}
|
|
913
|
+
return [...typeidx, ...uleb(cnt), ...handlers]
|
|
914
|
+
}
|
|
832
915
|
};
|
|
833
916
|
|
|
834
917
|
// per-op imm handlers
|
|
@@ -939,23 +1022,46 @@ const align = (op) => {
|
|
|
939
1022
|
return Math.log2(m ? (m[2] === 'x' ? 8 : m[1] / 8) : +group / 8)
|
|
940
1023
|
}
|
|
941
1024
|
|
|
1025
|
+
// Convert WAT numeric data (i8/i16/i32/i64/f32/f64 lists) to bytes (Phase 2: WAT numeric values)
|
|
1026
|
+
const numdata = (item) => {
|
|
1027
|
+
if (!Array.isArray(item)) return null
|
|
1028
|
+
const [t, ...vs] = item
|
|
1029
|
+
if (t !== 'i8' && t !== 'i16' && t !== 'i32' && t !== 'i64' && t !== 'f32' && t !== 'f64') return null
|
|
1030
|
+
const out = [], dv = new DataView(new ArrayBuffer(8))
|
|
1031
|
+
for (const v of vs) {
|
|
1032
|
+
if (t === 'i8') out.push((i32.parse(v) & 0xFF + 0x100) & 0xFF)
|
|
1033
|
+
else if (t === 'i16') (dv.setInt16(0, i32.parse(v), true), out.push(...new Uint8Array(dv.buffer, 0, 2)))
|
|
1034
|
+
else if (t === 'i32') (dv.setInt32(0, i32.parse(v), true), out.push(...new Uint8Array(dv.buffer, 0, 4)))
|
|
1035
|
+
else if (t === 'i64') (dv.setBigInt64(0, BigInt(v), true), out.push(...new Uint8Array(dv.buffer, 0, 8)))
|
|
1036
|
+
else if (t === 'f32') out.push(...encode.f32(v))
|
|
1037
|
+
else if (t === 'f64') out.push(...encode.f64(v))
|
|
1038
|
+
}
|
|
1039
|
+
return out
|
|
1040
|
+
}
|
|
1041
|
+
|
|
942
1042
|
// build limits sequence (consuming)
|
|
943
1043
|
// Memory64: i64 index type uses flags 0x04-0x07 (bit 2 = is_64)
|
|
1044
|
+
// Custom page sizes (Phase 3): (pagesize N) attr adds bit 3, appends log2(pagesize) as u32
|
|
944
1045
|
const limits = (node) => {
|
|
945
1046
|
const is64 = node[0] === 'i64' && node.shift()
|
|
946
1047
|
const shared = node[node.length - 1] === 'shared' && node.pop()
|
|
1048
|
+
// custom page size: (pagesize N) sub-node
|
|
1049
|
+
const psIdx = node.findIndex(n => Array.isArray(n) && n[0] === 'pagesize')
|
|
1050
|
+
let psLog2 = -1
|
|
1051
|
+
if (psIdx >= 0) psLog2 = Math.log2(+node.splice(psIdx, 1)[0][1])
|
|
947
1052
|
const hasMax = !isNaN(parseInt(node[1]))
|
|
948
|
-
const flag = (is64 ? 4 : 0) | (shared ? 2 : 0) | (hasMax ? 1 : 0)
|
|
1053
|
+
const flag = (psLog2 >= 0 ? 8 : 0) | (is64 ? 4 : 0) | (shared ? 2 : 0) | (hasMax ? 1 : 0)
|
|
949
1054
|
// For i64, parse as unsigned BigInt (limits are always unsigned)
|
|
950
1055
|
const parse = is64 ? v => {
|
|
951
1056
|
if (typeof v === 'bigint') return v
|
|
952
1057
|
const str = typeof v === 'string' ? v.replaceAll('_', '') : String(v)
|
|
953
1058
|
return BigInt(str)
|
|
954
1059
|
} : parseUint
|
|
1060
|
+
const ps = psLog2 >= 0 ? uleb(psLog2) : []
|
|
955
1061
|
|
|
956
1062
|
return hasMax
|
|
957
|
-
? [flag, ...uleb(parse(node.shift())), ...uleb(parse(node.shift()))]
|
|
958
|
-
: [flag, ...uleb(parse(node.shift()))]
|
|
1063
|
+
? [flag, ...uleb(parse(node.shift())), ...uleb(parse(node.shift())), ...ps]
|
|
1064
|
+
: [flag, ...uleb(parse(node.shift())), ...ps]
|
|
959
1065
|
}
|
|
960
1066
|
|
|
961
1067
|
// check if node is valid int in a range
|
package/src/const.js
CHANGED
|
@@ -48,15 +48,21 @@ export const INSTR = [
|
|
|
48
48
|
'i32.extend8_s', 'i32.extend16_s', 'i64.extend8_s', 'i64.extend16_s', 'i64.extend32_s', , , , , , , , , , , ,
|
|
49
49
|
// 0xd0-0xd6: reference
|
|
50
50
|
'ref.null ref_null', 'ref.is_null', 'ref.func funcidx', 'ref.eq', 'ref.as_non_null', 'br_on_null labelidx', 'br_on_non_null labelidx',
|
|
51
|
-
// 0xd7-
|
|
52
|
-
, , , , , , , , ,
|
|
51
|
+
// 0xd7-0xdf: padding (9 empty slots)
|
|
52
|
+
, , , , , , , , ,
|
|
53
|
+
// 0xe0-0xe6: stack switching (Phase 3)
|
|
54
|
+
'cont.new typeidx', 'cont.bind cont_bind', 'suspend tagidx', 'resume resume', 'resume_throw resume_throw', 'resume_throw_ref resume_throw_ref', 'switch switch_cont',
|
|
55
|
+
// 0xe7-0xfa: padding (20 empty slots)
|
|
56
|
+
, , , , , , , , , , , , , , , , , , , ,
|
|
53
57
|
// 0xfb: GC instructions (nested array for multi-byte opcodes)
|
|
54
58
|
[
|
|
55
59
|
'struct.new typeidx', 'struct.new_default typeidx', 'struct.get typeidx_field', 'struct.get_s typeidx_field', 'struct.get_u typeidx_field', 'struct.set typeidx_field',
|
|
56
60
|
'array.new typeidx', 'array.new_default typeidx', 'array.new_fixed typeidx_multi', 'array.new_data typeidx_dataidx', 'array.new_elem typeidx_elemidx',
|
|
57
61
|
'array.get typeidx', 'array.get_s typeidx', 'array.get_u typeidx', 'array.set typeidx', 'array.len', 'array.fill typeidx', 'array.copy typeidx_typeidx',
|
|
58
62
|
'array.init_data typeidx_dataidx', 'array.init_elem typeidx_elemidx', 'ref.test reftype', '', 'ref.cast reftype', '', 'br_on_cast reftype2', 'br_on_cast_fail reftype2',
|
|
59
|
-
'any.convert_extern', 'extern.convert_any', 'ref.i31', 'i31.get_s', 'i31.get_u'
|
|
63
|
+
'any.convert_extern', 'extern.convert_any', 'ref.i31', 'i31.get_s', 'i31.get_u',
|
|
64
|
+
// custom descriptors (Phase 3): 0xFB 0x20-0x26
|
|
65
|
+
, 'struct.new_desc typeidx', 'struct.new_default_desc typeidx', 'ref.get_desc typeidx', 'ref.cast_desc_eq reftype', , 'br_on_cast_desc_eq reftype2', 'br_on_cast_desc_eq_fail reftype2'
|
|
60
66
|
],
|
|
61
67
|
|
|
62
68
|
// 0xfc: Bulk memory/table operations (nested array)
|
|
@@ -146,9 +152,11 @@ export const TYPE = {
|
|
|
146
152
|
i8: 0x78, i16: 0x77, i32: 0x7f, i64: 0x7e, f32: 0x7d, f64: 0x7c, void: 0x40, v128: 0x7B,
|
|
147
153
|
// Heap types
|
|
148
154
|
exn: 0x69, noexn: 0x74, nofunc: 0x73, noextern: 0x72, none: 0x71, func: 0x70, extern: 0x6F, any: 0x6E, eq: 0x6D, i31: 0x6C, struct: 0x6B, array: 0x6A,
|
|
155
|
+
cont: 0x68, nocont: 0x75, // stack switching (Phase 3)
|
|
149
156
|
// Reference type abbreviations (absheaptype abbrs)
|
|
150
157
|
nullfuncref: 0x73, nullexternref: 0x72, nullexnref: 0x74, nullref: 0x71,
|
|
151
158
|
funcref: 0x70, externref: 0x6F, exnref: 0x69, anyref: 0x6E, eqref: 0x6D, i31ref: 0x6C, structref: 0x6B, arrayref: 0x6A,
|
|
159
|
+
contref: 0x68, nocontref: 0x75, // stack switching abbreviations
|
|
152
160
|
// ref, refnull
|
|
153
161
|
ref: 0x64, // -0x1c
|
|
154
162
|
refnull: 0x63, // -0x1d
|
|
@@ -157,7 +165,7 @@ export const TYPE = {
|
|
|
157
165
|
}
|
|
158
166
|
|
|
159
167
|
// Type definition codes (different from heap types - func is 0x60 not 0x70)
|
|
160
|
-
export const DEFTYPE = { func: 0x60, struct: 0x5F, array: 0x5E, sub: 0x50, subfinal: 0x4F, rec: 0x4E }
|
|
168
|
+
export const DEFTYPE = { func: 0x60, struct: 0x5F, array: 0x5E, cont: 0x5D, sub: 0x50, subfinal: 0x4F, rec: 0x4E }
|
|
161
169
|
|
|
162
170
|
// Import/export kind codes
|
|
163
171
|
export const KIND = { func: 0, table: 1, memory: 2, global: 3, tag: 4 }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/compile.js"],"names":[],"mappings":"AA+BA;;;;;GAKG;AACH,uCAHW,MAAM,QAAM,GACV,UAAU,
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/compile.js"],"names":[],"mappings":"AA+BA;;;;;GAKG;AACH,uCAHW,MAAM,QAAM,GACV,UAAU,CAqNtB"}
|
package/types/src/const.d.ts
CHANGED
|
@@ -40,6 +40,8 @@ export namespace TYPE {
|
|
|
40
40
|
export let i31: number;
|
|
41
41
|
export let struct: number;
|
|
42
42
|
export let array: number;
|
|
43
|
+
export let cont: number;
|
|
44
|
+
export let nocont: number;
|
|
43
45
|
export let nullfuncref: number;
|
|
44
46
|
export let nullexternref: number;
|
|
45
47
|
export let nullexnref: number;
|
|
@@ -52,6 +54,8 @@ export namespace TYPE {
|
|
|
52
54
|
export let i31ref: number;
|
|
53
55
|
export let structref: number;
|
|
54
56
|
export let arrayref: number;
|
|
57
|
+
export let contref: number;
|
|
58
|
+
export let nocontref: number;
|
|
55
59
|
export let ref: number;
|
|
56
60
|
export let refnull: number;
|
|
57
61
|
export let sub: number;
|
|
@@ -65,6 +69,8 @@ export namespace DEFTYPE {
|
|
|
65
69
|
export { struct_1 as struct };
|
|
66
70
|
let array_1: number;
|
|
67
71
|
export { array_1 as array };
|
|
72
|
+
let cont_1: number;
|
|
73
|
+
export { cont_1 as cont };
|
|
68
74
|
let sub_1: number;
|
|
69
75
|
export { sub_1 as sub };
|
|
70
76
|
let subfinal_1: number;
|
package/types/src/const.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"const.d.ts","sourceRoot":"","sources":["../../src/const.js"],"names":[],"mappings":"AAIA,
|
|
1
|
+
{"version":3,"file":"const.d.ts","sourceRoot":"","sources":["../../src/const.js"],"names":[],"mappings":"AAIA,0CA2IC"}
|