watr 4.1.0 → 4.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/watr.js +29 -6
- package/dist/watr.js +829 -17
- package/dist/watr.min.js +5 -5
- package/package.json +1 -1
- package/readme.md +13 -38
- package/src/compile.js +20 -10
- package/src/optimize.js +1184 -0
- package/src/parse.js +2 -2
- package/src/util.js +4 -4
- package/types/src/compile.d.ts.map +1 -1
- package/types/src/optimize.d.ts +88 -0
- package/types/src/optimize.d.ts.map +1 -0
- package/types/src/util.d.ts +3 -3
- package/types/src/util.d.ts.map +1 -1
- package/types/watr.d.ts +6 -3
- package/types/watr.d.ts.map +1 -1
- package/watr.js +12 -6
package/dist/watr.min.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
var
|
|
2
|
-
`?(i++,r=1):r++;t+=` at ${i}:${r}`}throw Error(t)};var bt=/^_|_$|[^\da-f]_|_[^\da-f]/i,At=/^[+-]?(?:0x[\da-f]+|\d+)$/i,Wt=new TextEncoder,Lt=new TextDecoder("utf-8",{fatal:!0,ignoreBOM:!0}),dt={n:10,r:13,t:9,'"':34,"'":39,"\\":92},et=t=>{let e=[],i=1,r,l,f="",a=()=>(f&&e.push(...Wt.encode(f)),f="");for(;i<t.length-1;)l=t[i++],r=null,l==="\\"&&(t[i]==="u"?(i++,i++,l=String.fromCodePoint(parseInt(t.slice(i,i=t.indexOf("}",i)),16)),i++):dt[t[i]]?r=dt[t[i++]]:isNaN(r=parseInt(t[i]+t[i+1],16))?l+=t[i]:(i++,i++)),r!=null?(a(),e.push(r)):f+=l;return a(),e.valueOf=()=>t,e},wt=t=>Lt.decode(new Uint8Array(et(t)));var _=(t,e=[])=>{if(t==null)return e;if(typeof t=="string"&&(t=/[_x]/i.test(t)?BigInt(t.replaceAll("_","")):C.parse(t)),typeof t=="bigint"){for(;;){let r=Number(t&0x7Fn);if(t>>=7n,t===0n){e.push(r);break}e.push(r|128)}return e}let i=t&127;return t>>>=7,t===0?(e.push(i),e):(e.push(i|128),_(t,e))};function jt(t){let e=[];for(let i=0;i<5;i++){let r=t&127;t>>>=7,i<4&&(r|=128),e.push(r)}return e}function C(t,e=[]){for(typeof t=="string"&&(t=C.parse(t));;){let i=Number(t&127);if(t>>=7,t===0&&!(i&64)||t===-1&&i&64){e.push(i);break}e.push(i|128)}return e}var $t=t=>!bt.test(t)&&At.test(t=t.replaceAll("_",""))?t:h(`Bad int ${t}`),Pt=C,Rt=C;C.parse=t=>(t=parseInt($t(t)),(t<-2147483648||t>4294967295)&&h("i32 constant out of range"),t);function V(t,e=[]){for(typeof t=="string"?t=V.parse(t):typeof t=="bigint"&&t>0x7fffffffffffffffn&&(t=t-0x10000000000000000n);;){let i=Number(t&0x7Fn);if(t>>=7n,t===0n&&!(i&64)||t===-1n&&i&64){e.push(i);break}e.push(i|128)}return e}V.parse=t=>(t=$t(t),t=t[0]==="-"?-BigInt(t.slice(1)):BigInt(t),(t<-0x8000000000000000n||t>0xffffffffffffffffn)&&h("i64 constant out of range"),q.setBigInt64(0,t),q.getBigInt64(0));var q=new DataView(new Float64Array(1).buffer),Vt=2147483648,Yt=2139095040;function Z(t,e,i){return typeof t=="string"&&~(i=t.indexOf("nan:"))?(e=C.parse(t.slice(i+4)),e|=Yt,t[0]==="-"&&(e|=Vt),q.setInt32(0,e)):(e=typeof t=="string"?Z.parse(t):t,q.setFloat32(0,e)),[q.getUint8(3),q.getUint8(2),q.getUint8(1),q.getUint8(0)]}var Gt=0x8000000000000000n,Ht=0x7ff0000000000000n;function Y(t,e,i){return typeof t=="string"&&~(i=t.indexOf("nan:"))?(e=V.parse(t.slice(i+4)),e|=Ht,t[0]==="-"&&(e|=Gt),q.setBigInt64(0,e)):(e=typeof t=="string"?Y.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)]}Y.parse=(t,e=Number.MAX_VALUE)=>{t=t.replaceAll("_","");let i=1;if(t[0]==="-"?(i=-1,t=t.slice(1)):t[0]==="+"&&(t=t.slice(1)),t[1]==="x"){let[r,l="0"]=t.split(/p/i),[f,a=""]=r.split("."),s=a.length??0,c=parseInt(f);isNaN(c)&&h();let n=a?parseInt("0x"+a)/16**s:0;l=parseInt(l,10);let o=i*(c+n)*2**l;return o=Math.max(-e,Math.min(e,o)),o}return t.includes("nan")?i<0?NaN:NaN:t.includes("inf")?i*(1/0):i*parseFloat(t)};Z.parse=t=>Y.parse(t,34028234663852886e22);var J=["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"]],N={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},F={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},It={func:96,struct:95,array:94,sub:80,subfinal:79,rec:78},mt={func:0,table:1,memory:2,global:3,tag:4};var j=t=>{let e=0,i=[],r="",l=0,f=0,a=()=>r&&(i.push(r),r=""),s=c=>{i.i=c;for(let n,o,u;e<t.length;)if(n=t.charCodeAt(e),l===34)r+=t[e++],n===92?r+=t[e++]:n===34&&(a(),l=0);else if(l>59)n===40&&t.charCodeAt(e+1)===59?(l++,r+=t[e++]+t[e++]):n===59&&t.charCodeAt(e+1)===41?(r+=t[e++]+t[e++],--l===59&&(a(),l=0)):r+=t[e++];else if(l<0)n===10||n===13?(r+=t[e++],a(),l=0):r+=t[e++];else if(n===34)r!=="$"&&a(),l=34,r+=t[e++];else if(n===40&&t.charCodeAt(e+1)===59)a(),l=60,r=t[e++]+t[e++];else if(n===59&&t.charCodeAt(e+1)===59)a(),l=-1,r=t[e++]+t[e++];else if(n===40&&t.charCodeAt(e+1)===64)a(),u=e,e+=2,r="@",f++,(o=i).push(i=[]),s(u),i=o;else if(n===40)a(),u=e++,f++,(o=i).push(i=[]),s(u),i=o;else{if(n===41)return a(),e++,f--;n<=32?(a(),e++):r+=t[e++]}l<0&&a(),a()};return s(0),l===34&&h("Unclosed quote",e),l>59&&h("Unclosed block comment",e),f>0&&h("Unclosed parenthesis",e),e<t.length&&h("Unexpected closing parenthesis",e),i.length>1?i:i[0]||[]};var Nt=(t,e)=>Array.isArray(t)?t[0]?.[0]==="@"&&t[0]!=="@custom"&&!t[0]?.startsWith?.("@metadata.code.")?null:(e=t.map(Nt).filter(i=>i!=null),e.i=t.i,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("\\")?"$"+wt(t.slice(1)):"$"+t.slice(2,-1):t[0]==='"'?et(t):t;function K(t){typeof t=="string"?(h.src=t,t=j(t)||[]):h.src="",h.i=0,t=Nt(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 K(t.slice(++e).map(f=>f.valueOf().slice(1,-1)).flat().join(""));let i=[];for(let f in N)(i[N[f]]=i[f]=[]).name=f;i.metadata={},t.slice(e).filter(f=>{if(!Array.isArray(f)){let c=h.src?.indexOf(f,h.i);c>=0&&(h.i=c),h(`Unexpected token ${f}`)}let[a,...s]=f;if(h.i=f.i,a==="@custom")i.custom.push(s);else if(a==="rec")for(let c=0;c<s.length;c++){let[,...n]=s[c];_t(n,i.type),(n=vt(n,i)).push(c?!0:[i.type.length,s.length]),i.type.push(n)}else if(a==="type")_t(s,i.type),i.type.push(vt(s,i));else if(a==="start"||a==="export")i[a].push(s);else return!0}).forEach(f=>{let[a,...s]=f;h.i=f.i;let c;a==="import"&&([a,...s]=(c=s).pop());let n=i[a];for(n||h(`Unknown section ${a}`),_t(s,n);s[0]?.[0]==="export";)i.export.push([s.shift()[1],[a,n?.length]]);if(s[0]?.[0]==="import"&&([,...c]=s.shift()),a==="table"){let o=s[0]==="i64",u=o?1:0;if(s[u+1]?.[0]==="elem"){let[x,[,...p]]=[s[u],s[u+1]];s=o?["i64",p.length,p.length,x]:[p.length,p.length,x],i.elem.push([["table",n.length],["offset",[o?"i64.const":"i32.const",o?0n:0]],x,...p])}}else if(a==="memory"){let o=s[0]==="i64",u=o?1:0;if(s[u]?.[0]==="data"){let[,...x]=s.splice(u,1)[0],p=""+Math.ceil(x.reduce((I,m)=>I+m.length,0)/65536);i.data.push([["memory",n.length],[o?"i64.const":"i32.const",o?0n:0],...x]),s=o?["i64",p,p]:[p,p]}}else if(a==="func"){let[o,u,x]=ot(s,i);o??=ft(u,x,i),!c&&i.code.push([[o,u,x],...P(s,i)]),s=[["type",o]]}else if(a==="tag"){let[o,u]=ot(s,i);o??=ft(u,[],i),s=[["type",o]]}c&&(i.import.push([...c,[a,...s]]),s=null),n.push(s)});let r=(f,a=!0)=>{let s=i[f].filter(Boolean).map(c=>yt[f](c,i)).filter(Boolean);return f===N.custom?s.flatMap(c=>[f,...k(c)]):s.length?[f,...k(a?k(s):s)]:[]},l=()=>{let f=[];for(let a in i.metadata){let s=k(et(`"metadata.code.${a}"`)),c=k(i.metadata[a].map(([n,o])=>[..._(n),...k(o.map(([u,x])=>[..._(u),...k(x)]))]));f.push(0,...k([...s,...c]))}return f};return Uint8Array.from([0,97,115,109,1,0,0,0,...r(N.custom),...r(N.type),...r(N.import),...r(N.func),...r(N.table),...r(N.memory),...r(N.tag),...r(N.global),...r(N.export),...r(N.start,!1),...r(N.elem),...r(N.datacount,!1),...r(N.code),...l(),...r(N.data)])}var M=t=>t?.[0]==="$"||!isNaN(t),O=t=>t?.[0]==="$",pt=t=>t?.[0]==="a"||t?.[0]==="o";function P(t,e){let i=[];for(t=[...t];t.length;){let r=t.shift();if(typeof r=="string")if(i.push(r),r==="block"||r==="if"||r==="loop")O(t[0])&&i.push(t.shift()),i.push(it(t,e));else if(r==="else"||r==="end")O(t[0])&&t.shift();else if(r==="select")i.push(nt(t)[1]);else if(r.endsWith("call_indirect")){let l=M(t[0])?t.shift():0,[f,a,s]=ot(t,e);i.push(l,["type",f??ft(a,s,e)])}else r==="table.init"?i.push(M(t[1])?t.shift():0,t.shift()):r==="table.copy"||r==="memory.copy"?i.push(M(t[0])?t.shift():0,M(t[0])?t.shift():0):r.startsWith("table.")?i.push(M(t[0])?t.shift():0):r==="memory.init"?(i.push(...M(t[1])?[t.shift(),t.shift()].reverse():[t.shift(),0]),e.datacount&&(e.datacount[0]=!0)):r==="data.drop"||r==="array.new_data"||r==="array.init_data"?(r==="data.drop"&&i.push(t.shift()),e.datacount&&(e.datacount[0]=!0)):(r.startsWith("memory.")||r.endsWith("load")||r.endsWith("store"))&&M(t[0])&&i.push(t.shift());else if(Array.isArray(r)){let l=r[0];if(r.i!=null&&(h.i=r.i),l?.startsWith?.("@metadata.code.")){let a=l.slice(15);i.push(["@metadata",a,r[1]]);continue}if(typeof l!="string"||!Array.isArray(J[l])){i.push(r);continue}let f=r.slice(1);if(l==="block"||l==="loop")i.push(l),O(f[0])&&i.push(f.shift()),i.push(it(f,e),...P(f,e),"end");else if(l==="if"){let a=[],s=[];f.at(-1)?.[0]==="else"&&(s=P(f.pop().slice(1),e)),f.at(-1)?.[0]==="then"&&(a=P(f.pop().slice(1),e));let c=[l];O(f[0])&&c.push(f.shift()),c.push(it(f,e)),i.push(...P(f,e),...c,...a),s.length&&i.push("else",...s),i.push("end")}else if(l==="try_table"){for(i.push(l),O(f[0])&&i.push(f.shift()),i.push(it(f,e));f[0]?.[0]==="catch"||f[0]?.[0]==="catch_ref"||f[0]?.[0]==="catch_all"||f[0]?.[0]==="catch_all_ref";)i.push(f.shift());i.push(...P(f,e),"end")}else{let a=[];for(;f.length&&(!Array.isArray(f[0])||"type,param,result,ref".includes(f[0][0]));)a.push(f.shift());i.push(...P(f,e),l,...a),t.unshift(...i.splice(i.length-1-a.length))}}else i.push(r)}return i}var ft=(t,e,i,r="$"+t+">"+e)=>(i.type[r]??=i.type.push(["func",[t,e]])-1,r),xt=(t,e)=>{let i=[];for(;t[0]?.[0]===e;){let[,...r]=t.shift(),l=O(r[0])&&r.shift();l&&(l in i?(()=>{throw Error(`Duplicate ${e} ${l}`)})():i[l]=i.length),i.push(...r)}return i},nt=t=>{let e=xt(t,"param"),i=xt(t,"result");if(t[0]?.[0]==="param")throw Error("Unexpected param");return[e,i]},ot=(t,e)=>{if(t[0]?.[0]!=="type")return[,...nt(t)];let[,i]=t.shift(),[r,l]=nt(t),f=e.type[typeof i=="string"&&isNaN(i)?e.type[i]:+i];if(!f)throw Error(`Unknown type ${i}`);if((r.length||l.length)&&f[1].join(">")!==r+">"+l)throw Error(`Type ${i} mismatch`);return[i,...f[1]]},it=(t,e)=>{let[i,r,l]=ot(t,e);if(!(!r.length&&!l.length))return!r.length&&l.length===1?["result",...l]:["type",i??ft(r,l,e)]},_t=(t,e)=>{let i=O(t[0])&&t.shift();return i&&(i in e?h(`Duplicate ${e.name} ${i}`):e[i]=e.length),i},vt=([t],e)=>{let i="subfinal",r=[],l;return t[0]==="sub"&&(i=t.shift(),t[0]==="final"&&(i+=t.shift()),t=(r=t).pop()),[l,...t]=t,l==="func"?(t=nt(t),e.type["$"+t.join(">")]??=e.type.length):l==="struct"?t=xt(t,"field"):l==="array"&&([t]=t),[l,t,i,r]},yt=[([t,...e],i)=>{let r=e;return(e[0]?.[0]==="before"||e[0]?.[0]==="after")&&(r=e.slice(1)),[...k(t),...r.flat()]},([t,e,i,r,l],f)=>{if(l===!0)return;let a;if(l){t="rec";let[s,c]=l,n=Array.from({length:c},(o,u)=>yt[N.type](f.type[s+u].slice(0,4),f));a=k(n)}else i==="sub"||r?.length?(a=[...k(r.map(s=>y(s,f.type))),...yt[N.type]([t,e],f)],t=i):t==="func"?a=[...k(e[0].map(s=>S(s,f))),...k(e[1].map(s=>S(s,f)))]:t==="array"?a=rt(e,f):t==="struct"&&(a=k(e.map(s=>rt(s,f))));return[It[t],...a]},([t,e,[i,...r]],l)=>{let f;if(i==="func"){let[[,a]]=r;f=_(y(a,l.type))}else if(i==="tag"){let[[,a]]=r;f=[0,..._(y(a,l.type))]}else i==="memory"?f=lt(r):i==="global"?f=rt(r[0],l):i==="table"?f=[...S(r.pop(),l),...lt(r)]:h(`Unknown kind ${i}`);return[...k(t),...k(e),mt[i],...f]},([[,t]],e)=>_(y(t,e.type)),(t,e)=>{let i=lt(t),r=S(t.shift(),e),[l]=t;return l?[64,0,...r,...i,...H(l,e)]:[...r,...i]},(t,e)=>lt(t),([t,e],i)=>[...rt(t,i),...H(e,i)],([t,[e,i]],r)=>[...k(t),mt[e],..._(y(i,r[e]))],([t],e)=>_(y(t,e.func)),(t,e)=>{let i=0,r=0,l=0,f=0,a,s,c;t[0]==="declare"&&(t.shift(),r=1),t[0]?.[0]==="table"?([,a]=t.shift(),a=y(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=y(t.shift(),e.table)),t[0]?.[0]==="offset"||Array.isArray(t[0])&&t[0][0]!=="item"&&!t[0][0].startsWith("ref")?(s=t.shift(),s[0]==="offset"&&([,s]=s),s=H(s,e)):r||(i=1),F[t[0]]||t[0]?.[0]==="ref"?c=S(t.shift(),e):t[0]==="func"?c=[F[t.shift()]]:c=[F.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"&&(l=1),o)),c[0]!==F.funcref&&(f=1,l=1);let n=l<<2|(i||r?r:!!a||f)<<1|(i||r);return[n,...n===0?s:n===1?[0]:n===2?[..._(a||0),...s,0]:n===3?[0]:n===4?s:n===5?c:n===6?[..._(a||0),...s,...c]:c,...k(t.map(l?o=>H(typeof o=="string"?["ref.func",o]:o,e):o=>_(y(o,e.func))))]},(t,e)=>{let[i,r]=t.shift();r||([,[r]]=e.type[y(i,e.type)]),e.local=Object.create(r),e.block=[],e.local.name="local",e.block.name="block",e._codeIdx===void 0&&(e._codeIdx=0);let l=e._codeIdx++;for(;t[0]?.[0]==="local";){let[,...c]=t.shift();if(O(c[0])){let n=c.shift();n in e.local?h(`Duplicate local ${n}`):e.local[n]=e.local.length}e.local.push(...c)}e.meta={};let f=Ut(t,e),a=e.import.filter(c=>c[2][0]==="func").length+l;for(let c in e.meta)((e.metadata??={})[c]??=[]).push([a,e.meta[c]]);let s=e.local.slice(r.length).reduce((c,n)=>(n==c[c.length-1]?.[1]?c[c.length-1][0]++:c.push([1,n]),c),[]);return e.local=e.block=e.meta=null,k([...k(s.map(([c,n])=>[..._(c),...S(n,e)])),...f])},(t,e)=>{let i,r=0;return t[0]?.[0]==="memory"?([,r]=t.shift(),r=y(r,e.memory)):(typeof t[0]=="string"||typeof t[0]=="number")&&(t[1]?.[0]==="offset"||Array.isArray(t[1])&&typeof t[1][0]=="string")&&(r=y(t.shift(),e.memory)),Array.isArray(t[0])&&typeof t[0]?.[0]=="string"&&(i=t.shift(),i[0]==="offset"&&([,i]=i),i??h("Bad offset",i)),[...r?[2,..._(r),...H(i,e)]:i?[0,...H(i,e)]:[1],...k(t.flat())]},(t,e)=>_(e.data.length),([[,t]],e)=>[0,..._(y(t,e.type))]],S=(t,e)=>t[0]==="ref"?t[1]=="null"?F[t[2]]?[F[t[2]]]:[F.refnull,..._(y(t[t.length-1],e.type))]:[F.ref,..._(F[t[t.length-1]]||y(t[t.length-1],e.type))]:[F[t]??h(`Unknown type ${t}`)],rt=(t,e,i=t[0]==="mut"?1:0)=>[...S(i?t[1]:t,e),i],Tt={null:()=>[],reversed:(t,e)=>{let i=t.shift(),r=t.shift();return[..._(y(r,e.elem)),..._(y(i,e.table))]},block:(t,e)=>{e.block.push(1),O(t[0])&&(e.block[t.shift()]=e.block.length);let i=t.shift();return i?i[0]==="result"?S(i[1],e):_(y(i[1],e.type)):[F.void]},try_table:(t,e)=>{O(t[0])&&(e.block[t.shift()]=e.block.length+1);let i=t.shift(),r=i?i[0]==="result"?S(i[1],e):_(y(i[1],e.type)):[F.void],l=[],f=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(),s=a[0]==="catch"?0:a[0]==="catch_ref"?1:a[0]==="catch_all"?2:3;s<=1?l.push(s,..._(y(a[1],e.tag)),..._(Q(a[2],e.block))):l.push(s,..._(Q(a[1],e.block))),f++}return e.block.push(1),[...r,..._(f),...l]},end:(t,e)=>(e.block.pop(),[]),call_indirect:(t,e)=>{let i=t.shift(),[,r]=t.shift();return[..._(y(r,e.type)),..._(y(i,e.table))]},br_table:(t,e)=>{let i=[],r=0;for(;t[0]&&(!isNaN(t[0])||O(t[0]));)i.push(..._(Q(t.shift(),e.block))),r++;return[..._(r-1),...i]},select:(t,e)=>{let i=t.shift()||[];return i.length?k(i.map(r=>S(r,e))):[]},ref_null:(t,e)=>{let i=t.shift();return F[i]?[F[i]]:_(y(i,e.type))},memarg:(t,e,i)=>kt(t,i,M(t[0])&&!pt(t[0])?y(t.shift(),e.memory):0),opt_memory:(t,e)=>_(y(M(t[0])?t.shift():0,e.memory)),reftype:(t,e)=>{let i=S(t.shift(),e);return i.length>1?i.slice(1):i},reftype2:(t,e)=>{let i=Q(t.shift(),e.block),r=S(t.shift(),e),l=S(t.shift(),e);return[(l[0]!==F.ref)<<1|r[0]!==F.ref,..._(i),r.pop(),l.pop()]},v128const:t=>{let[e,i]=t.shift().split("x"),r=+e.slice(1),l=r>>>3;if(i=+i,e[0]==="i"){let a=i===16?new Uint8Array(16):i===8?new Uint16Array(8):i===4?new Uint32Array(4):new BigUint64Array(2);for(let s=0;s<i;s++)a[s]=G[e].parse(t.shift());return[...new Uint8Array(a.buffer)]}let f=new Uint8Array(16);for(let a=0;a<i;a++)f.set(G[e](t.shift()),a*l);return[...f]},shuffle:t=>{let e=[];for(let i=0;i<16;i++)e.push(at(t.shift(),32));return typeof t[0]=="string"&&!isNaN(t[0])&&h("invalid lane length"),e},memlane:(t,e,i)=>{let r=O(t[0])||M(t[0])&&(pt(t[1])||M(t[1]))?y(t.shift(),e.memory):0;return[...kt(t,i,r),..._(at(t.shift()))]},"*":t=>_(t.shift()),labelidx:(t,e)=>_(Q(t.shift(),e.block)),laneidx:t=>[at(t.shift(),255)],funcidx:(t,e)=>_(y(t.shift(),e.func)),typeidx:(t,e)=>_(y(t.shift(),e.type)),tableidx:(t,e)=>_(y(t.shift(),e.table)),memoryidx:(t,e)=>_(y(t.shift(),e.memory)),globalidx:(t,e)=>_(y(t.shift(),e.global)),localidx:(t,e)=>_(y(t.shift(),e.local)),dataidx:(t,e)=>_(y(t.shift(),e.data)),elemidx:(t,e)=>_(y(t.shift(),e.elem)),tagidx:(t,e)=>_(y(t.shift(),e.tag)),"memoryidx?":(t,e)=>_(y(M(t[0])?t.shift():0,e.memory)),i32:t=>C(t.shift()),i64:t=>V(t.shift()),f32:t=>Z(t.shift()),f64:t=>Y(t.shift()),v128:t=>(void 0)(t.shift()),typeidx_field:(t,e)=>{let i=y(t.shift(),e.type);return[..._(i),..._(y(t.shift(),e.type[i][1]))]},typeidx_multi:(t,e)=>[..._(y(t.shift(),e.type)),..._(t.shift())],typeidx_dataidx:(t,e)=>[..._(y(t.shift(),e.type)),..._(y(t.shift(),e.data))],typeidx_elemidx:(t,e)=>[..._(y(t.shift(),e.type)),..._(y(t.shift(),e.elem))],typeidx_typeidx:(t,e)=>[..._(y(t.shift(),e.type)),..._(y(t.shift(),e.type))],dataidx_memoryidx:(t,e)=>[..._(y(t.shift(),e.data)),..._(y(t.shift(),e.memory))],memoryidx_memoryidx:(t,e)=>[..._(y(t.shift(),e.memory)),..._(y(t.shift(),e.memory))],tableidx_tableidx:(t,e)=>[..._(y(t.shift(),e.table)),..._(y(t.shift(),e.table))]},st={};(function t(e,i){for(let r=0,l,f,a;r<e.length;r++)(l=e[r])&&(Array.isArray(l)?t(l,r):([f,a]=l.split(" "),J[f]=i?[i,..._(r)]:[r],a&&(st[f]=Tt[a])))})(J);var Ut=(t,e)=>{let i=[],r=[];for(;t?.length;){let l=t.shift();if(l?.[0]==="@metadata"){r.push(l.slice(1));continue}Array.isArray(l)&&(l.i!=null&&(h.i=l.i),h(`Unknown instruction ${l[0]}`));let[...f]=J[l]||h(`Unknown instruction ${l}`);st[l]&&(l==="select"&&t[0]?.length?f[0]++:st[l]===Tt.reftype&&(t[0][1]==="null"||t[0][0]!=="ref")&&f[f.length-1]++,f.push(...st[l](t,e,l)));for(let[a,s]of r)(e.meta[a]??=[]).push([i.length,s]);i.push(...f)}return i.push(11),i},H=(t,e)=>Ut(P([t],e),e),y=(t,e,i)=>(i=O(t)?e[t]:+t,i in e?i:h(`Unknown ${e.name} ${t}`)),Q=(t,e,i)=>(i=O(t)?e.length-e[t]:+t,isNaN(i)||i>e.length?h(`Bad label ${t}`):i),Kt=t=>{let e,i,r,l;for(;pt(t[0]);)[r,l]=t.shift().split("="),r==="offset"?i=+l:r==="align"?e=+l:h(`Unknown param ${r}=${l}`);return(i<0||i>4294967295)&&h(`Bad offset ${i}`),(e<=0||e>4294967295)&&h(`Bad align ${e}`),e&&(e=Math.log2(e))%1&&h(`Bad align ${e}`),[e,i]},kt=(t,e,i=0)=>{let[r,l]=Kt(t),f=(r??Xt(e))|(i&&64);return i?[..._(f),..._(i),..._(l??0)]:[..._(f),..._(l??0)]},Xt=t=>{let e=t.indexOf(".",3)+1,i=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 f=t.slice(e,e+6).match(/\d+/);return Math.log2(f?f[0]/8:+i/8)}let r=t[e]==="l"?e+4:e+5,l=t.slice(r).match(/(\d+)(x|_|$)/);return Math.log2(l?l[2]==="x"?8:l[1]/8:+i/8)},lt=t=>{let e=t[0]==="i64"&&t.shift(),i=t[t.length-1]==="shared"&&t.pop(),r=!isNaN(parseInt(t[1])),l=(e?4:0)|(i?2:0)|(r?1:0),f=e?a=>{if(typeof a=="bigint")return a;let s=typeof a=="string"?a.replaceAll("_",""):String(a);return BigInt(s)}:at;return r?[l,..._(f(t.shift())),..._(f(t.shift()))]:[l,..._(f(t.shift()))]},at=(t,e=4294967295)=>{let i=typeof t=="string"&&t[0]!=="+"?C.parse(t):typeof t=="number"?t:h(`Bad int ${t}`);return i>e?h(`Value out of range ${t}`):i},k=t=>[..._(t.length),...t.flat()];function zt(t,e={}){typeof t=="string"&&(t=j(t));let{indent:i=" ",newline:r=`
|
|
3
|
-
`,comments:
|
|
4
|
-
`}else{let
|
|
5
|
-
`?n+="":(m&&m!==")"&&m!==" "||r||m===")")&&(n+=" "),n+=I,o=!1}}return u?`(${n.replaceAll(r+x+"("," (")})`:`(${n+r+i.repeat(c)})`}}var qt={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"]},gt=Object.keys(qt),Zt=t=>{if(t===!0)return Object.fromEntries(gt.map(e=>[e,!0]));if(t===!1)return{};if(typeof t=="string"){let e=new Set(t.split(/\s+/).filter(Boolean));return Object.fromEntries(gt.map(i=>[i,e.has(i)||e.has("all")]))}return{...t}},B=(t,e,i,r)=>{if(e(t,i,r),Array.isArray(t))for(let l=0;l<t.length;l++)B(t[l],e,t,l)},W=(t,e,i,r)=>{if(Array.isArray(t))for(let l=0;l<t.length;l++)W(t[l],e,t,l);e(t,i,r)},Jt=t=>{let e=new Set;return B(t,i=>{if(typeof i=="string")for(let[r,l]of Object.entries(qt))l.some(f=>i===f||i.startsWith(f+" "))&&e.add(r)}),B(t,i=>{if(!(!Array.isArray(i)||i[0]!=="global"))for(let r of i)Array.isArray(r)&&(r[0]==="i32.add"||r[0]==="i32.sub"||r[0]==="i32.mul"||r[0]==="i64.add"||r[0]==="i64.sub"||r[0]==="i64.mul")&&B(r,l=>{Array.isArray(l)&&l[0]==="global.get"&&e.add("extended_const")})}),B(t,i=>{if(!Array.isArray(i)||i[0]!=="func")return;let r=0;for(let l of i)Array.isArray(l)&&l[0]==="result"&&(r+=l.length-1);r>1&&e.add("multi_value")}),e},Ft=t=>Array.isArray(t)?t.map(Ft):t,Bt=(t,e)=>{let i=[],r=t[0]==="module"?1:0;for(let l=r;l<t.length;l++)Array.isArray(t[l])&&t[l][0]===e&&i.push({node:t[l],idx:l});return i},ct=(t,e,i)=>t.splice(e,0,i),Qt=0,L=t=>`$__${t}${Qt++}`,te=(t,e)=>{let i=new Set;if(B(t,n=>{Array.isArray(n)&&n[0]==="ref.func"&&i.add(n[1])}),!i.size)return t;let r=L("fntbl"),l=[...i],f=Object.fromEntries(l.map((n,o)=>[n,o])),a=Bt(t,"func"),s=a.length?a[0].idx:t[0]==="module"?1:0;ct(t,s,["table",r,"funcref",["elem",...l]]);let c={};return B(t,n=>{if(!Array.isArray(n)||n[0]!=="func")return;let o=typeof n[1]=="string"&&n[1][0]==="$"?n[1]:null;if(!o)return;let u=[],x=[];for(let p of n){if(Array.isArray(p)&&p[0]==="param")for(let I=1;I<p.length;I++)p[I][0]!=="$"&&u.push(p[I]);if(Array.isArray(p)&&p[0]==="result")for(let I=1;I<p.length;I++)x.push(p[I])}c[o]={params:u,results:x}}),W(t,(n,o,u)=>{if(!(!Array.isArray(n)||!o)){if(n[0]==="ref.func"&&f[n[1]]!==void 0&&(o[u]=["i32.const",f[n[1]]]),n[0]==="call_ref"){let x=n[1],p=n.slice(2);o[u]=["call_indirect",r,["type",x],...p]}if(n[0]==="return_call_ref"){let x=n[1],p=n.slice(2);o[u]=["return_call_indirect",r,["type",x],...p]}}}),t},D={funcref:te},ee={"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]},ie=(t,e)=>(W(t,(i,r,l)=>{if(!Array.isArray(i)||!r)return;let f=ee[i[0]];if(!f)return;let[a,s]=f,c=i.slice(1);r[l]=[`${a}.shr_s`,[`${a}.shl`,...c,[`${a}.const`,s]],[`${a}.const`,s]]}),t);D.sign_ext=ie;var Et={"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}},re=(t,e)=>{let i=new Set;if(B(t,l=>{Array.isArray(l)&&Et[l[0]]&&i.add(l[0])}),!i.size)return t;let r={};for(let l of i){let{itype:f,ftype:a,signed:s,min:c,max:n}=Et[l],o=L(`trunc_${f}_${a}_${s?"s":"u"}`);r[l]=o;let u=`${f}.trunc_${a}_${s?"s":"u"}`,x=f==="i64"?0n:0,p=["func",o,["param","$v",a],["result",f],["if",["result",f],[`${a}.ne`,["local.get","$v"],["local.get","$v"]],["then",[`${f}.const`,x]],["else",["if",["result",f],[`${a}.lt`,["local.get","$v"],[`${a}.const`,typeof c=="bigint"?Number(c):c]],["then",[`${f}.const`,c]],["else",["if",["result",f],[`${a}.gt`,["local.get","$v"],[`${a}.const`,typeof n=="bigint"?Number(n):n]],["then",[`${f}.const`,n]],["else",[u,["local.get","$v"]]]]]]]]];t.push(p)}return W(t,(l,f,a)=>{!Array.isArray(l)||!f||r[l[0]]&&(f[a]=["call",r[l[0]],...l.slice(1)])}),t};D.nontrapping=re;var le=(t,e)=>{let i=new Set,r=new Set;B(t,a=>{if(Array.isArray(a)){if(a[0]==="memory.copy"){let s=typeof a[1]=="number"?a[1]:0,c=typeof a[2]=="number"?a[2]:0;i.add(`${s}_${c}`)}if(a[0]==="memory.fill"){let s=typeof a[1]=="number"?a[1]:0;r.add(s)}}});let l={},f={};for(let a of i){let[s,c]=a.split("_").map(Number),n=L(`memcpy${a==="0_0"?"":"_"+a}`);l[a]=n;let o=s?["i32.store8",s]:["i32.store8"],u=c?["i32.load8_u",c]:["i32.load8_u"];t.push(["func",n,["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 r){let s=L(`memset${a===0?"":"_"+a}`);f[a]=s;let c=a?["i32.store8",a]:["i32.store8"];t.push(["func",s,["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 W(t,(a,s,c)=>{if(!(!Array.isArray(a)||!s)){if(a[0]==="memory.copy"){let n=typeof a[1]=="number"?a[1]:0,o=typeof a[2]=="number"?a[2]:0,u=a.filter(x=>Array.isArray(x)||typeof x=="string"&&x[0]==="$");s[c]=["call",l[`${n}_${o}`],...u]}if(a[0]==="memory.fill"){let n=typeof a[1]=="number"?a[1]:0,o=a.filter(u=>Array.isArray(u)||typeof u=="string"&&u[0]==="$");s[c]=["call",f[n],...o]}}}),t};D.bulk_memory=le;var se=(t,e)=>{let i=!1;return B(t,r=>{Array.isArray(r)&&(r[0]==="return_call"||r[0]==="return_call_indirect")&&(i=!0)}),i&&W(t,(r,l,f)=>{!Array.isArray(r)||!l||(r[0]==="return_call"&&(l[f]=["return",["call",...r.slice(1)]]),r[0]==="return_call_indirect"&&(l[f]=["return",["call_indirect",...r.slice(1)]]))}),t};D.return_call=se;var ae=(t,e)=>(W(t,(i,r,l)=>{if(!(!Array.isArray(i)||!r)&&(i[0]==="ref.i31"&&(r[l]=["i32.and",...i.slice(1),["i32.const",2147483647]]),i[0]==="i31.get_u"&&(r[l]=i.length>1?i[1]:["drop"]),i[0]==="i31.get_s")){let f=i.slice(1);r[l]=["i32.shr_s",["i32.shl",...f,["i32.const",1]],["i32.const",1]]}}),t);D.i31ref=ae;var fe=(t,e)=>{let i={};B(t,l=>{if(!Array.isArray(l)||l[0]!=="global")return;let f=typeof l[1]=="string"&&l[1][0]==="$"?l[1]:null;if(f)for(let a=l.length-1;a>=0;a--){let s=l[a];if(Array.isArray(s)&&(s[0]==="i32.const"||s[0]==="i64.const"||s[0]==="f32.const"||s[0]==="f64.const")){i[f]={type:s[0].split(".")[0],value:s[1]};break}}});let r=l=>{if(!Array.isArray(l))return l;let f=l[0];if(f==="global.get"&&i[l[1]]){let a=i[l[1]];return[`${a.type}.const`,a.value]}if(f==="i32.add"||f==="i64.add"){let a=r(l[1]),s=r(l[2]);if(a&&s&&a[0]?.endsWith(".const")&&s[0]?.endsWith(".const")){let c=f.split(".")[0],n=c==="i64"?BigInt(a[1]):Number(a[1]),o=c==="i64"?BigInt(s[1]):Number(s[1]);return[`${c}.const`,n+o]}}if(f==="i32.sub"||f==="i64.sub"){let a=r(l[1]),s=r(l[2]);if(a&&s&&a[0]?.endsWith(".const")&&s[0]?.endsWith(".const")){let c=f.split(".")[0],n=c==="i64"?BigInt(a[1]):Number(a[1]),o=c==="i64"?BigInt(s[1]):Number(s[1]);return[`${c}.const`,n-o]}}if(f==="i32.mul"||f==="i64.mul"){let a=r(l[1]),s=r(l[2]);if(a&&s&&a[0]?.endsWith(".const")&&s[0]?.endsWith(".const")){let c=f.split(".")[0],n=c==="i64"?BigInt(a[1]):Number(a[1]),o=c==="i64"?BigInt(s[1]):Number(s[1]);return[`${c}.const`,n*o]}}return l};return W(t,(l,f,a)=>{if(!(!Array.isArray(l)||l[0]!=="global"||!f)){for(let s=2;s<l.length;s++)if(Array.isArray(l[s])){let c=r(l[s]);c!==l[s]&&(l[s]=c)}}}),t};D.extended_const=fe;var ne=(t,e)=>{let i=new Map,r=[];if(B(t,s=>{if(!Array.isArray(s)||s[0]!=="func")return;let c=typeof s[1]=="string"&&s[1][0]==="$"?s[1]:null,n=[];for(let o of s)if(Array.isArray(o)&&o[0]==="result")for(let u=1;u<o.length;u++)n.push(o[u]);n.length>1&&c&&i.set(c,n)}),!i.size)return t;let l=Math.max(...[...i.values()].map(s=>s.length)),f={};for(let[s,c]of i)for(let n=1;n<c.length;n++){let o=c[n];if(f[o]||(f[o]=[]),f[o].length<n){let u=L(`ret_${o}_${f[o].length}`);f[o].push(u),r.push(["global",u,["mut",o],[`${o}.const`,o==="i64"?0n:0]])}}let a=t[0]==="module"?1:0;for(let s of r.reverse())ct(t,a,s);return W(t,(s,c,n)=>{if(!Array.isArray(s)||s[0]!=="func")return;let o=typeof s[1]=="string"&&s[1][0]==="$"?s[1]:null;if(!o||!i.has(o))return;let u=i.get(o);for(let x=0;x<s.length;x++)if(Array.isArray(s[x])&&s[x][0]==="result"){s[x]=["result",u[0]];break}}),t};D.multi_value=ne;var tt={i32:4,i64:8,f32:4,f64:8},oe=(t,e)=>{let i=new Map,r=new Map,l=1;if(B(t,m=>{if(!Array.isArray(m)||m[0]!=="type")return;let $=typeof m[1]=="string"&&m[1][0]==="$"?m[1]:null;if($){for(let w of m)if(Array.isArray(w)){if(w[0]==="struct"){let A=[];for(let g of w)if(Array.isArray(g)&&g[0]==="field"){let v=typeof g[1]=="string"&&g[1][0]==="$"?g[1]:null,d=v?g[2]:g[1],b=Array.isArray(d)&&d[0]==="mut"?d[1]:d;A.push({name:v,type:b})}i.set($,{kind:"struct",fields:A}),r.set($,l++)}if(w[0]==="array"){let A=w[1],g=Array.isArray(A)&&A[0]==="mut"?A[1]:A;i.set($,{kind:"array",elemType:g}),r.set($,l++)}}}}),!i.size)return t;let f=Bt(t,"memory").length>0,a=L("alloc"),s=L("heap_ptr"),c=t[0]==="module"?1:0;f||ct(t,c,["memory",1]),ct(t,c+1,["global",s,["mut","i32"],["i32.const",1024]]);let n=["func",a,["param","$size","i32"],["result","i32"],["local","$ptr","i32"],["local.set","$ptr",["global.get",s]],["global.set",s,["i32.add",["global.get",s],["local.get","$size"]]],["local.get","$ptr"]];t.push(n);let o=m=>{let $=4;for(let w of m.fields)$+=tt[w.type]||4;return $},u=(m,$)=>{let w=4;for(let A=0;A<$;A++)w+=tt[m.fields[A].type]||4;return w},x=(m,$)=>{for(let w=0;w<m.fields.length;w++)if(m.fields[w].name===$)return w;return-1},p=0,I=()=>`$__gc_tmp${p++}`;return B(t,m=>{if(!Array.isArray(m)||m[0]!=="func")return;let $=!1,w=!1,A=!1,g=!1;if(B(m,d=>{Array.isArray(d)&&((d[0]==="struct.new"||d[0]==="struct.new_default")&&($=!0),(d[0]==="array.new"||d[0]==="array.new_default")&&(w=!0,A=!0,g=!0))}),!$&&!w)return;let v=1;for(let d=1;d<m.length;d++){let b=m[d];if(Array.isArray(b)&&(b[0]==="param"||b[0]==="result"||b[0]==="local"||b[0]==="export"||b[0]==="type"))v=d+1;else if(typeof b=="string"&&b[0]==="$")v=d+1;else if(!Array.isArray(b))v=d+1;else break}$&&m.splice(v++,0,["local","$__gc_ptr","i32"]),w&&m.splice(v++,0,["local","$__gc_aptr","i32"]),A&&m.splice(v++,0,["local","$__gc_alen","i32"]),g&&m.splice(v++,0,["local","$__gc_aidx","i32"])}),W(t,(m,$,w)=>{if(!(!Array.isArray(m)||!$)){if(m[0]==="struct.new"||m[0]==="struct.new_default"){let A=m[1],g=i.get(A);if(!g||g.kind!=="struct")return;let v=o(g),d=r.get(A),b=m.slice(2),T="$__gc_ptr",E=[["local.set",T,["call",a,["i32.const",v]]],["i32.store",["local.get",T],["i32.const",d]]];if(m[0]==="struct.new")for(let U=0;U<g.fields.length;U++){let z=g.fields[U],X=u(g,U),R=z.type==="i64"?"i64.store":z.type==="f32"?"f32.store":z.type==="f64"?"f64.store":"i32.store";E.push([R,["i32.add",["local.get",T],["i32.const",X]],b[U]||[`${z.type}.const`,0]])}else for(let U=0;U<g.fields.length;U++){let z=g.fields[U],X=u(g,U),R=z.type==="i64"?"i64.store":z.type==="f32"?"f32.store":z.type==="f64"?"f64.store":"i32.store",Mt=z.type==="i64"?["i64.const",0n]:z.type==="f32"?["f32.const",0]:z.type==="f64"?["f64.const",0]:["i32.const",0];E.push([R,["i32.add",["local.get",T],["i32.const",X]],Mt])}E.push(["local.get",T]),$[w]=["block",["result","i32"],...E]}if(m[0]==="struct.get"){let A=m[1],g=m[2],v=m[3],d=i.get(A);if(!d||d.kind!=="struct")return;let b=typeof g=="string"&&g[0]==="$"?x(d,g):parseInt(g);if(b<0)return;let T=d.fields[b],E=u(d,b),U=T.type==="i64"?"i64.load":T.type==="f32"?"f32.load":T.type==="f64"?"f64.load":"i32.load";$[w]=[U,["i32.add",v,["i32.const",E]]]}if(m[0]==="struct.set"){let A=m[1],g=m[2],v=m[3],d=m[4],b=i.get(A);if(!b||b.kind!=="struct")return;let T=typeof g=="string"&&g[0]==="$"?x(b,g):parseInt(g);if(T<0)return;let E=b.fields[T],U=u(b,T),z=E.type==="i64"?"i64.store":E.type==="f32"?"f32.store":E.type==="f64"?"f64.store":"i32.store";$[w]=[z,["i32.add",v,["i32.const",U]],d]}if(m[0]==="array.new"||m[0]==="array.new_default"){let A=m[1],g=i.get(A);if(!g||g.kind!=="array")return;let v=r.get(A),d=tt[g.elemType]||4,b=m[0]==="array.new"?m[2]:null,T=m[0]==="array.new"?m[3]:m[2],E="$__gc_aptr",U="$__gc_alen",z="$__gc_aidx",X=g.elemType==="i64"?"i64.store":g.elemType==="f32"?"f32.store":g.elemType==="f64"?"f64.store":"i32.store",R=[["local.set",U,T],["local.set",E,["call",a,["i32.add",["i32.const",8],["i32.mul",["local.get",U],["i32.const",d]]]]],["i32.store",["local.get",E],["i32.const",v]],["i32.store",["i32.add",["local.get",E],["i32.const",4]],["local.get",U]]];b&&R.push(["local.set",z,["i32.const",0]],["block","$done",["loop","$loop",["br_if","$done",["i32.ge_u",["local.get",z],["local.get",U]]],[X,["i32.add",["i32.add",["local.get",E],["i32.const",8]],["i32.mul",["local.get",z],["i32.const",d]]],b],["local.set",z,["i32.add",["local.get",z],["i32.const",1]]],["br","$loop"]]]),R.push(["local.get",E]),$[w]=["block",["result","i32"],...R]}if(m[0]==="array.get"){let A=m[1],g=m[2],v=m[3],d=i.get(A);if(!d||d.kind!=="array")return;let b=tt[d.elemType]||4,T=d.elemType==="i64"?"i64.load":d.elemType==="f32"?"f32.load":d.elemType==="f64"?"f64.load":"i32.load";$[w]=[T,["i32.add",["i32.add",g,["i32.const",8]],["i32.mul",v,["i32.const",b]]]]}if(m[0]==="array.set"){let A=m[1],g=m[2],v=m[3],d=m[4],b=i.get(A);if(!b||b.kind!=="array")return;let T=tt[b.elemType]||4,E=b.elemType==="i64"?"i64.store":b.elemType==="f32"?"f32.store":b.elemType==="f64"?"f64.store":"i32.store";$[w]=[E,["i32.add",["i32.add",g,["i32.const",8]],["i32.mul",v,["i32.const",T]]],d]}if(m[0]==="array.len"){let A=m[1];$[w]=["i32.load",["i32.add",A,["i32.const",4]]]}}}),t};D.gc=oe;var ce=(t,e)=>{let i=new Map,r=1;return B(t,l=>{if(!Array.isArray(l)||l[0]!=="type")return;let f=typeof l[1]=="string"&&l[1][0]==="$"?l[1]:null;if(f)for(let a of l)Array.isArray(a)&&(a[0]==="struct"||a[0]==="array")&&i.set(f,r++)}),i.size&&W(t,(l,f,a)=>{if(!(!Array.isArray(l)||!f)){if(l[0]==="ref.test"){let s=l[1],c=null;Array.isArray(s)&&s[0]==="ref"&&(c=s[1]==="null"?s[2]:s[1]);let n=l[2],o=i.get(c);o!==void 0&&(f[a]=["if",["result","i32"],["i32.eqz",n],["then",["i32.const",0]],["else",["i32.eq",["i32.load",n],["i32.const",o]]]])}if(l[0]==="ref.cast"){let s=l[1],c=null,n=!1;Array.isArray(s)&&s[0]==="ref"&&(s[1]==="null"?(n=!0,c=s[2]):c=s[1]);let o=l[2],u=i.get(c);if(u!==void 0){let x=L("cast");n?f[a]=["block",["result","i32"],["local",x,"i32"],["local.set",x,o],["if",["i32.and",["i32.ne",["local.get",x],["i32.const",0]],["i32.ne",["i32.load",["local.get",x]],["i32.const",u]]],["then",["unreachable"]]],["local.get",x]]:f[a]=["block",["result","i32"],["local",x,"i32"],["local.set",x,o],["if",["i32.or",["i32.eqz",["local.get",x]],["i32.ne",["i32.load",["local.get",x]],["i32.const",u]]],["then",["unreachable"]]],["local.get",x]]}}if(l[0]==="br_on_cast"){let s=l[1],c=l[2],n=l[3],o=l[4],u=null;Array.isArray(n)&&n[0]==="ref"&&(u=n[1]==="null"?n[2]:n[1]);let x=i.get(u);if(x!==void 0){let p=L("brcast");f[a]=["block",["result","i32"],["local",p,"i32"],["local.set",p,o],["br_if",s,["i32.and",["i32.ne",["local.get",p],["i32.const",0]],["i32.eq",["i32.load",["local.get",p]],["i32.const",x]]]],["local.get",p]]}}if(l[0]==="br_on_cast_fail"){let s=l[1],c=l[2],n=l[3],o=l[4],u=null;Array.isArray(n)&&n[0]==="ref"&&(u=n[1]==="null"?n[2]:n[1]);let x=i.get(u);if(x!==void 0){let p=L("brfail");f[a]=["block",["result","i32"],["local",p,"i32"],["local.set",p,o],["br_if",s,["i32.or",["i32.eqz",["local.get",p]],["i32.ne",["i32.load",["local.get",p]],["i32.const",x]]]],["local.get",p]]}}}}),t};D.ref_cast=ce;function ut(t,e=!0){typeof t=="string"&&(t=j(t)),t=Ft(t),e=Zt(e);let i=Jt(t),r={uid:0};for(let l of gt)i.has(l)&&e[l]!==!1&&D[l]&&(t=D[l](t,r));return t}var Ot="\uE000",St=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},ue=(t,e={})=>{if(!Array.isArray(t))return typeof t=="string"&&t[0]==="$"&&e.locals?.[t]?e.locals[t]:null;let[i,...r]=t;return St(i)?St(i):i==="local.get"&&e.locals?.[r[0]]?e.locals[r[0]]:i==="call"&&e.funcs?.[r[0]]?e.funcs[r[0]].result?.[0]:null};function ht(t,e){if(t=e(t),Array.isArray(t))for(let i=0;i<t.length;i++){let r=ht(t[i],e);r?._splice?(t.splice(i,1,...r),i+=r.length-1):t[i]=r}return t}function me(t,e){let i=[],r=new Map;return ht(t,l=>{if(!Array.isArray(l))return l;if(l[0]==="call"&&typeof l[1]=="function"){let f=l[1];if(!r.has(f)){let s=[];for(let o=2;o<l.length;o++){let u=ue(l[o]);u&&s.push(u)}let c=i.length,n=f.name||`$fn${c}`;r.set(f,{idx:c,name:n.startsWith("$")?n:"$"+n,params:s,fn:f}),i.push(r.get(f))}let a=r.get(f);l[1]=a.name}return l}),i}function _e(t){return t.map(({name:e,params:i})=>["import",'"env"',`"${e.slice(1)}"`,["func",e,...i.map(r=>["param",r])]])}function pe(t,...e){let i={};if(!Array.isArray(t)&&e.length&&typeof e[e.length-1]=="object"&&e[e.length-1]!==null&&!(e[e.length-1]instanceof Uint8Array)&&(i=e.pop()),Array.isArray(t)&&t.raw){let r=t[0];for(let n=0;n<e.length;n++)r+=Ot+t[n+1];let l=j(r),f=[],a=0;l=ht(l,n=>{if(n===Ot){let o=e[a++];if(typeof o=="function")return f.push(o),o;if(typeof o=="string"&&(o[0]==="("||/^\s*\(/.test(o))){let u=j(o);return Array.isArray(u)&&Array.isArray(u[0])&&(u._splice=!0),u}return o instanceof Uint8Array?[...o]:o}return n});let s=null;if(f.length){let n=me(l,f);if(n.length){let o=_e(n);l[0]==="module"?l.splice(1,0,...o):typeof l[0]=="string"?l=[...o,l]:l.unshift(...o),s={env:{}};for(let u of n)s.env[u.name.slice(1)]=u.fn}}i.polyfill&&(l=ut(l,i.polyfill));let c=K(l);return s&&(c._imports=s),c}if(i.polyfill){let r=ut(t,i.polyfill);return K(r)}return K(t)}function xe(t,...e){let i=pe(t,...e),r=new WebAssembly.Module(i);return new WebAssembly.Instance(r,i._imports).exports}var Se=xe;export{pe as compile,Se as default,j as parse,ut as polyfill,zt as print,xe 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 J={};Kt(J,{f32:()=>et,f64:()=>X,i16:()=>te,i32:()=>D,i64:()=>K,i8:()=>Qt,uleb:()=>y,uleb5:()=>Zt});var $=(t,e=$.loc)=>{if(e!=null&&$.src){let r=1,i=1;for(let s=0;s<e&&s<$.src.length;s++)$.src[s]===`
|
|
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 n(t);return t.filter(a=>s||!l(a)).map(a=>n(a)).join(i);function l(a){return typeof a=="string"&&a[1]===";"}function n(a,c=0){if(!Array.isArray(a))return a;let f=a[0];if(!f)return"";let o=!1;if(f==="try_table"){let x=1;for(typeof a[x]=="string"&&a[x][0]==="$"&&(f+=" "+a[x++]),Array.isArray(a[x])&&(a[x][0]==="result"||a[x][0]==="type")&&(f+=" "+n(a[x++],c));Array.isArray(a[x])&&/^catch/.test(a[x][0]);)f+=" "+n(a[x++],c).trim();for(;x<a.length;x++)f+=Array.isArray(a[x])?i+r.repeat(c+1)+n(a[x],c+1):" "+a[x];return`(${f+i+r.repeat(c)})`}let u=!!i&&a.length<4&&!a.some(x=>typeof x=="string"&&x[0]===";"&&x[1]===";"),h=r.repeat(c+1);for(let x=1;x<a.length;x++){let I=a[x].valueOf();if(typeof I=="string"&&I[1]===";"){if(!s)continue;if(I[0]===";")if(i)f+=i+h+I.trimEnd(),o=!0;else{let p=f[f.length-1];p&&p!==" "&&p!=="("&&(f+=" "),f+=I.trimEnd()+`
|
|
4
|
+
`}else{let p=f[f.length-1];p&&p!==" "&&p!=="("&&(f+=" "),f+=I.trimEnd()}}else if(Array.isArray(I))u&&(u=I.every(p=>!Array.isArray(p))),f+=i+h+n(I,c+1),o=!1;else if(a[0]==="data")u=!1,(i||f[f.length-1]!==")")&&(f+=i||" "),f+=h+I,o=!1;else{let p=f[f.length-1];o&&i?f+=i+h:p===`
|
|
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};
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
_Light & fast WAT compiler_
|
|
6
6
|
|
|
7
7
|
* [feature](https://webassembly.org/features/) & [spec](https://webassembly.github.io/spec/core/text/index.html)-complete, zero deps
|
|
8
|
-
*
|
|
9
|
-
* instant wasm, JS interop, clear errors
|
|
8
|
+
* [compile](./docs.md#compilesource-options) · [polyfill](./docs.md#polyfillast-options) · [optimize](./docs.md#optimizeast-options) · [parse](./docs.md#parsesource-options) · [prettify](./docs.md#printtree-options) · [minify](./docs.md#printtree-options)
|
|
9
|
+
* instant wasm, JS interop, CLI, clear errors
|
|
10
10
|
|
|
11
11
|
**[docs](./docs.md)** **·** **[demo](https://dy.github.io/watr/play/)**
|
|
12
12
|
|
|
@@ -18,7 +18,7 @@ _Light & fast WAT compiler_
|
|
|
18
18
|
## Usage
|
|
19
19
|
|
|
20
20
|
```js
|
|
21
|
-
import watr, { compile, polyfill, parse, print } from 'watr'
|
|
21
|
+
import watr, { compile, polyfill, optimize, parse, print } from 'watr'
|
|
22
22
|
|
|
23
23
|
// compile to binary
|
|
24
24
|
const binary = compile('(func (export "f") (result f64) (f64.const 1))')
|
|
@@ -31,6 +31,9 @@ parse('(i32.const 42)') // ['i32.const', 42]
|
|
|
31
31
|
// polyfill (transform newer features to MVP)
|
|
32
32
|
print(polyfill('(func (i32.extend8_s ...))')) // (func (i32.shr_s (i32.shl ...) ...))
|
|
33
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
|
+
|
|
34
37
|
// print
|
|
35
38
|
print('(module(func(result i32)i32.const 42))') // (module\n (func (result i32)\n ...
|
|
36
39
|
|
|
@@ -52,45 +55,17 @@ npx watr input.wat # → input.wasm
|
|
|
52
55
|
npx watr input.wat -o out.wasm # custom output
|
|
53
56
|
npx watr input.wat --print # pretty-print
|
|
54
57
|
npx watr input.wat --minify # minify
|
|
55
|
-
npx watr input.wat --
|
|
58
|
+
npx watr input.wat --optimize # fold, treeshake, deadcode
|
|
59
|
+
npx watr input.wat --polyfill # newer features → MVP
|
|
56
60
|
```
|
|
57
61
|
|
|
58
|
-
## Features
|
|
59
|
-
|
|
60
|
-
| Feature | Status | Polyfill |
|
|
61
|
-
|---------|--------|----------|
|
|
62
|
-
| [MVP](https://webassembly.org/docs/mvp/) | yes | n/a |
|
|
63
|
-
| [BigInt / i64](https://github.com/WebAssembly/JS-BigInt-integration) | yes | n/a |
|
|
64
|
-
| [Multi-value](https://github.com/WebAssembly/multi-value) | yes | partial (no blocks) |
|
|
65
|
-
| [Sign extension](https://github.com/WebAssembly/sign-extension-ops) | yes | yes |
|
|
66
|
-
| [Non-trapping conversions](https://github.com/WebAssembly/nontrapping-float-to-int-conversions) | yes | yes |
|
|
67
|
-
| [Bulk memory](https://github.com/WebAssembly/bulk-memory-operations) | yes | copy/fill only |
|
|
68
|
-
| [Reference types](https://github.com/WebAssembly/reference-types) | yes | funcref only |
|
|
69
|
-
| [Typed function refs](https://github.com/WebAssembly/function-references) | yes | yes |
|
|
70
|
-
| [Tail calls](https://github.com/WebAssembly/tail-call) | yes | yes |
|
|
71
|
-
| [Extended const](https://github.com/WebAssembly/extended-const) | yes | yes |
|
|
72
|
-
| [Multiple memories](https://github.com/WebAssembly/multi-memory) | yes | n/a |
|
|
73
|
-
| [Memory64](https://github.com/WebAssembly/memory64) | yes | no |
|
|
74
|
-
| [SIMD](https://github.com/WebAssembly/simd) | yes | no |
|
|
75
|
-
| [Relaxed SIMD](https://github.com/WebAssembly/relaxed-simd) | yes | no |
|
|
76
|
-
| [Threads](https://github.com/WebAssembly/threads) | yes | no |
|
|
77
|
-
| [GC](https://github.com/WebAssembly/gc) | yes | i31/struct/array/cast |
|
|
78
|
-
| [Exceptions](https://github.com/WebAssembly/exception-handling) | yes | no |
|
|
79
|
-
| [Annotations](https://github.com/WebAssembly/annotations) | yes | n/a |
|
|
80
|
-
| [Wide arithmetic](https://github.com/WebAssembly/wide-arithmetic) | yes | n/a |
|
|
81
|
-
| [JS string builtins](https://github.com/WebAssembly/js-string-builtins) | yes | n/a |
|
|
82
|
-
|
|
83
|
-
**Polyfill legend**: `yes` = full support, `partial` = limited support noted, `no` = not feasible, `n/a` = already MVP or no runtime impact
|
|
84
|
-
|
|
85
62
|
## Metrics
|
|
86
63
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
[
|
|
91
|
-
[
|
|
92
|
-
[binaryen](https://github.com/WebAssembly/binaryen) | 1,100 KB | 718 op/s
|
|
93
|
-
[wat-compiler](https://github.com/stagas/wat-compiler) | 7.7 KB (MVP) | 539 op/s |
|
|
64
|
+
* **watr** — 10 KB, 4,460 op/s
|
|
65
|
+
* [spec/wast.js](https://github.com/WebAssembly/spec) — 216 KB, 2,185 op/s
|
|
66
|
+
* [wabt](https://github.com/WebAssembly/wabt) — 282 KB, 1,273 op/s
|
|
67
|
+
* [binaryen](https://github.com/WebAssembly/binaryen) — 1,100 KB, 718 op/s
|
|
68
|
+
* [wat-compiler](https://github.com/stagas/wat-compiler) — 7.7 KB (MVP), 539 op/s
|
|
94
69
|
|
|
95
70
|
|
|
96
71
|
## Used by
|
package/src/compile.js
CHANGED
|
@@ -7,7 +7,7 @@ import { err, unescape, str } from './util.js'
|
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Clean up AST: remove comments, normalize quoted ids, convert strings to bytes.
|
|
10
|
-
* Preserves @custom and @metadata.code.* annotations. Preserves .
|
|
10
|
+
* Preserves @custom and @metadata.code.* annotations. Preserves .loc for error reporting.
|
|
11
11
|
*
|
|
12
12
|
* @param {any} node - AST node
|
|
13
13
|
* @param {Array} [result] - Internal accumulator
|
|
@@ -25,8 +25,8 @@ const cleanup = (node, result) => !Array.isArray(node) ? (
|
|
|
25
25
|
) :
|
|
26
26
|
// remove annotations like (@name ...) except @custom and @metadata.code.*
|
|
27
27
|
node[0]?.[0] === '@' && node[0] !== '@custom' && !node[0]?.startsWith?.('@metadata.code.') ? null :
|
|
28
|
-
// unwrap single-element array containing module (after removing comments), preserve .
|
|
29
|
-
(result = node.map(cleanup).filter(n => n != null), result.
|
|
28
|
+
// unwrap single-element array containing module (after removing comments), preserve .loc
|
|
29
|
+
(result = node.map(cleanup).filter(n => n != null), result.loc = node.loc, result.length === 1 && result[0]?.[0] === 'module' ? result[0] : result)
|
|
30
30
|
|
|
31
31
|
|
|
32
32
|
/**
|
|
@@ -39,7 +39,7 @@ export default function compile(nodes) {
|
|
|
39
39
|
// normalize to (module ...) form
|
|
40
40
|
if (typeof nodes === 'string') err.src = nodes, nodes = parse(nodes) || []
|
|
41
41
|
else err.src = '' // clear source if AST passed directly
|
|
42
|
-
err.
|
|
42
|
+
err.loc = 0
|
|
43
43
|
|
|
44
44
|
nodes = cleanup(nodes) || []
|
|
45
45
|
|
|
@@ -64,12 +64,22 @@ export default function compile(nodes) {
|
|
|
64
64
|
// initialize types
|
|
65
65
|
nodes.slice(idx).filter((n) => {
|
|
66
66
|
if (!Array.isArray(n)) {
|
|
67
|
-
|
|
68
|
-
|
|
67
|
+
// find token as standalone word (not substring of another token)
|
|
68
|
+
let pos = err.loc, src = err.src, c
|
|
69
|
+
while ((pos = src.indexOf(n, pos)) >= 0) {
|
|
70
|
+
c = src.charCodeAt(pos - 1)
|
|
71
|
+
// check not preceded by word char or $
|
|
72
|
+
if (pos > 0 && (c > 47 && c < 58 || c > 64 && c < 91 || c > 96 && c < 123 || c === 95 || c === 36)) { pos++; continue }
|
|
73
|
+
c = src.charCodeAt(pos + n.length)
|
|
74
|
+
// check not followed by word char
|
|
75
|
+
if (c > 47 && c < 58 || c > 64 && c < 91 || c > 96 && c < 123 || c === 95) { pos++; continue }
|
|
76
|
+
break
|
|
77
|
+
}
|
|
78
|
+
if (pos >= 0) err.loc = pos
|
|
69
79
|
err(`Unexpected token ${n}`)
|
|
70
80
|
}
|
|
71
81
|
let [kind, ...node] = n
|
|
72
|
-
err.
|
|
82
|
+
err.loc = n.loc // track position for errors
|
|
73
83
|
// (@custom "name" placement? data) - custom section support
|
|
74
84
|
if (kind === '@custom') {
|
|
75
85
|
ctx.custom.push(node)
|
|
@@ -103,7 +113,7 @@ export default function compile(nodes) {
|
|
|
103
113
|
// prepare/normalize nodes
|
|
104
114
|
.forEach((n) => {
|
|
105
115
|
let [kind, ...node] = n
|
|
106
|
-
err.
|
|
116
|
+
err.loc = n.loc // track position for errors
|
|
107
117
|
let imported // if node needs to be imported
|
|
108
118
|
|
|
109
119
|
// import abbr
|
|
@@ -266,7 +276,7 @@ function normalize(nodes, ctx) {
|
|
|
266
276
|
}
|
|
267
277
|
else if (Array.isArray(node)) {
|
|
268
278
|
const op = node[0]
|
|
269
|
-
node.
|
|
279
|
+
node.loc != null && (err.loc = node.loc) // track position for errors
|
|
270
280
|
|
|
271
281
|
// code metadata annotations - pass through as marker with metadata type and data
|
|
272
282
|
// (@metadata.code.<type> data:str)
|
|
@@ -851,7 +861,7 @@ const instr = (nodes, ctx) => {
|
|
|
851
861
|
|
|
852
862
|
// Array = unknown instruction passed through from normalize
|
|
853
863
|
if (Array.isArray(op)) {
|
|
854
|
-
op.
|
|
864
|
+
op.loc != null && (err.loc = op.loc)
|
|
855
865
|
err(`Unknown instruction ${op[0]}`)
|
|
856
866
|
}
|
|
857
867
|
|