-
Notifications
You must be signed in to change notification settings - Fork 1
/
wat.min.js
6 lines (6 loc) · 21.9 KB
/
wat.min.js
1
2
3
4
5
6
var d={"type.i32":127,"type.i64":126,"type.f32":125,"type.f64":124,"type.void":64,"type.func":96,"type.funcref":112,"section.custom":0,"section.type":1,"section.import":2,"section.function":3,"section.table":4,"section.memory":5,"section.global":6,"section.export":7,"section.start":8,"section.element":9,"section.code":10,"section.data":11,"import.func":0,"import.table":1,"import.memory":2,"import.global":3,"export.function":0,"export.table":1,"export.memory":2,"export.global":3,"global.const":0,"global.var":1,"global.mut":1,"limits.min":0,"limits.minmax":1,"limits.shared":3},L=["unreachable","nop","block","loop","if","else",,,,,,"end","br","br_if","br_table","return","call","call_indirect",,,,,,,,,"drop","select",,,,,"local.get","local.set","local.tee","global.get","global.set",,,,"i32.load","i64.load","f32.load","f64.load","i32.load8_s","i32.load8_u","i32.load16_s","i32.load16_u","i64.load8_s","i64.load8_u","i64.load16_s","i64.load16_u","i64.load32_s","i64.load32_u","i32.store","i64.store","f32.store","f64.store","i32.store8","i32.store16","i64.store8","i64.store16","i64.store32","memory.size","memory.grow","i32.const","i64.const","f32.const","f64.const","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"],z={get_local:"local.get",set_local:"local.set",tee_local:"local.tee",get_global:"global.get",set_global:"global.set","i32.trunc_s/f32":"i32.trunc_f32_s","i32.trunc_u/f32":"i32.trunc_f32_u","i32.trunc_s/f64":"i32.trunc_f64_s","i32.trunc_u/f64":"i32.trunc_f64_u","i64.extend_s/i32":"i64.extend_i32_s","i64.extend_u/i32":"i64.extend_i32_u","i64.trunc_s/f32":"i64.trunc_f32_s","i64.trunc_u/f32":"i64.trunc_f32_u","i64.trunc_s/f64":"i64.trunc_f64_s","i64.trunc_u/f64":"i64.trunc_f64_u","f32.convert_s/i32":"f32.convert_i32_s","f32.convert_u/i32":"f32.convert_i32_u","f32.convert_s/i64":"f32.convert_i64_s","f32.convert_u/i64":"f32.convert_i64_u","f32.demote/f64":"f32.demote_f64","f64.convert_s/i32":"f64.convert_i32_s","f64.convert_u/i32":"f64.convert_i32_u","f64.convert_s/i64":"f64.convert_i64_s","f64.convert_u/i64":"f64.convert_i64_u","f64.promote/f32":"f64.promote_f32"};for(let[s,e]of L.entries())e!=null&&(d[e]=s);d["i32.trunc_sat_f64_s"]=[252,2];for(let s in z){let e=L.indexOf(z[s]);d[s]=e}var v={};for(let s in d){v[s]=U(s);let[e,t]=s.split(".");t!=null&&(d[e]=d[e]??{},d[e][t]=d[s],v[e]=v[e]??{},v[e][t]=U(s))}var R={"i32.load":4,"i64.load":8,"f32.load":4,"f64.load":8,"i32.load8_s":1,"i32.load8_u":1,"i32.load16_s":2,"i32.load16_u":2,"i64.load8_s":1,"i64.load8_u":1,"i64.load16_s":2,"i64.load16_u":2,"i64.load32_s":4,"i64.load32_u":4,"i32.store":4,"i64.store":8,"f32.store":4,"f64.store":8,"i32.store8":1,"i32.store16":2,"i64.store8":1,"i64.store16":2,"i64.store32":4};function*M(s){for(s=X(s);;){let e=Number(s&0x7Fn);if(s>>=7n,s===0n&&(e&64)===0||s===-1n&&(e&64)!==0){yield e;break}yield e|128}}function*S(s){let e=0,t=Math.ceil(Math.log2(Math.abs(s))),i=s<0,f=!0;for(;f;)e=s&127,s=s>>7,i&&(s=s|-(1<<t-7)),s==0&&(e&64)==0||s==-1&&(e&64)==64?f=!1:e=e|128,yield e}function*x(s,e=0){if(s<0)throw new TypeError("uint value must be positive, received: "+s);let t=0;do t=s&127,s=s>>7,(s!=0||e>0)&&(t=t|128),yield t,e--;while(s!=0||e>-1)}var k=new DataView(new BigInt64Array(1).buffer);function X(s){return k.setBigInt64(0,s),k.getBigInt64(0)}function*q(s){k.setFloat32(0,s);for(let e=4;e--;)yield k.getUint8(e)}function*C(s){k.setFloat64(0,s);for(let e=8;e--;)yield k.getUint8(e)}function G(s){s=s.toUpperCase();let e=s.indexOf("P"),t,i;e!==-1?(t=s.substring(0,e),i=parseInt(s.substring(e+1))):(t=s,i=0);let f=t.indexOf(".");if(f!==-1){let _=parseInt(t.substring(0,f),16),g=Math.sign(_);_=g*_;let y=t.length-f-1,u=parseInt(t.substring(f+1),16),c=y>0?u/Math.pow(16,y):0;g===0?c===0?t=g:Object.is(g,-0)?t=-c:t=c:t=g*(_+c)}else t=parseInt(t,16);return t*(e!==-1?Math.pow(2,i):1)}var Y=2147483648,J=2139095040;function*P(s){let e=parseInt(s.split("nan:")[1]);e|=J,s[0]==="-"&&(e|=Y),k.setInt32(0,e);for(let t=4;t--;)yield k.getUint8(t)}var W=0x8000000000000000n,K=0x7ff0000000000000n;function*$(s){let e=BigInt(s.split("nan:")[1]);e|=K,s[0]==="-"&&(e|=W),k.setBigInt64(0,e);for(let t=8;t--;)yield k.getUint8(t)}(function(s){function e(){}function t(u,c){if(u=u===void 0?"utf-8":u,c=c===void 0?{fatal:!1}:c,g.indexOf(u.toLowerCase())===-1)throw new RangeError("Failed to construct 'TextDecoder': The encoding label provided ('"+u+"') is invalid.");if(c.fatal)throw Error("Failed to construct 'TextDecoder': the 'fatal' option is unsupported.")}function i(u){return Buffer.from(u.buffer,u.byteOffset,u.byteLength).toString("utf-8")}function f(u){var c=URL.createObjectURL(new Blob([u],{type:"text/plain;charset=UTF-8"}));try{var r=new XMLHttpRequest;return r.open("GET",c,!1),r.send(),r.responseText}catch{return _(u)}finally{URL.revokeObjectURL(c)}}function _(u){for(var c=0,r=Math.min(65536,u.length+1),n=new Uint16Array(r),l=[],a=0;;){var o=c<u.length;if(!o||a>=r-1){if(l.push(String.fromCharCode.apply(null,n.subarray(0,a))),!o)return l.join("");u=u.subarray(c),a=c=0}if(o=u[c++],(o&128)===0)n[a++]=o;else if((o&224)===192){var p=u[c++]&63;n[a++]=(o&31)<<6|p}else if((o&240)===224){p=u[c++]&63;var h=u[c++]&63;n[a++]=(o&31)<<12|p<<6|h}else if((o&248)===240){p=u[c++]&63,h=u[c++]&63;var w=u[c++]&63;o=(o&7)<<18|p<<12|h<<6|w,65535<o&&(o-=65536,n[a++]=o>>>10&1023|55296,o=56320|o&1023),n[a++]=o}}}if(s.TextEncoder&&s.TextDecoder)return!1;var g=["utf-8","utf8","unicode-1-1-utf-8"];Object.defineProperty(e.prototype,"encoding",{value:"utf-8"}),e.prototype.encode=function(u,c){if(c=c===void 0?{stream:!1}:c,c.stream)throw Error("Failed to encode: the 'stream' option is unsupported.");c=0;for(var r=u.length,n=0,l=Math.max(32,r+(r>>>1)+7),a=new Uint8Array(l>>>3<<3);c<r;){var o=u.charCodeAt(c++);if(55296<=o&&56319>=o){if(c<r){var p=u.charCodeAt(c);(p&64512)===56320&&(++c,o=((o&1023)<<10)+(p&1023)+65536)}if(55296<=o&&56319>=o)continue}if(n+4>a.length&&(l+=8,l*=1+c/u.length*2,l=l>>>3<<3,p=new Uint8Array(l),p.set(a),a=p),(o&4294967168)===0)a[n++]=o;else{if((o&4294965248)===0)a[n++]=o>>>6&31|192;else if((o&4294901760)===0)a[n++]=o>>>12&15|224,a[n++]=o>>>6&63|128;else if((o&4292870144)===0)a[n++]=o>>>18&7|240,a[n++]=o>>>12&63|128,a[n++]=o>>>6&63|128;else continue;a[n++]=o&63|128}}return a.slice?a.slice(0,n):a.subarray(0,n)},Object.defineProperty(t.prototype,"encoding",{value:"utf-8"}),Object.defineProperty(t.prototype,"fatal",{value:!1}),Object.defineProperty(t.prototype,"ignoreBOM",{value:!1});var y=_;typeof Buffer=="function"&&Buffer.from?y=i:typeof Blob=="function"&&typeof URL=="function"&&typeof URL.createObjectURL=="function"&&(y=f),t.prototype.decode=function(u,c){if(c=c===void 0?{stream:!1}:c,c.stream)throw Error("Failed to decode: the 'stream' option is unsupported.");return u=u instanceof Uint8Array?u:u.buffer instanceof ArrayBuffer?new Uint8Array(u.buffer):new Uint8Array(u),y(u)},s.TextEncoder=e,s.TextDecoder=t})(typeof window<"u"?window:typeof global<"u"?global:globalThis);function U(s){return function(e,t){return Z(s,e!=null&&!Array.isArray(e)?[e]:e,t!=null&&!Array.isArray(t)?[t]:t)}}var Q={"f64.const":C,"f32.const":q};function*Z(s,e=[],t=[]){for(let i of t)switch(typeof i){case"number":yield i;break;default:yield*i;break}yield*Array.isArray(d[s])?d[s]:[d[s]];for(let i of e)switch(typeof i){case"bigint":yield*M(i);break;case"number":yield*(Q[s]??S)(i);break;default:yield*i}}var ee=new TextEncoder("utf-8");function T(s){return[...ee.encode(s)]}function D(){return[...T("\0asm"),1,0,0,0]}function m(s,e){return[d.section[s],...x(e.length),...e]}function b(s){return[...x(s.length),...s.flat()]}function te(s){let e=[],t=[],i;for(let f of s)f!==i&&t.length&&(e.push([...x(t.length),d.type[t[0]]]),t=[]),t.push(f),i=f;return t.length&&e.push([...x(t.length),d.type[t[0]]]),e}function E(s,e,t){return t!=null?[d.limits.shared,...x(s),...x(e)]:e!=null?[d.limits.minmax,...x(s),...x(e)]:[d.limits.min,...x(s)]}m.type=function(s){return m("type",b(s.map(([e,t])=>[d.type.func,...b(e.map(i=>d.type[i])),...b(t.map(i=>d.type[i]))])))};m.import=function(s){return m("import",b(s.map(([e,t,i,f])=>[...b(T(e)),...b(T(t)),d.import[i],...{func:()=>f.map(_=>[...x(_)]),memory:()=>E(...f)}[i]()])))};m.function=function(s){return m("function",b(s.map(e=>[...x(e)])))};m.table=function(s){return m("table",b(s.map(([e,t,i])=>[d.type[e],...E(t,i)])))};m.memory=function(s){return m("memory",b(s.map(([e,t])=>E(e,t))))};m.global=function(s){return m("global",b(s.map(([e,t,i])=>[d.type[t],d.global[e],...i,d.end])))};m.export=function(s){return m("export",b(s.map(([e,t,i])=>[...b(T(e)),d.export[t],...x(i)])))};m.start=function(s){return m("start",[...x(s)])};m.element=function(s){return m("element",b(s.map(([e,t,i])=>[...x(e),...t,d.end,...b(i)])))};m.code=function(s){return m("code",b(s.map(([e,t])=>b([...b(te(e)),...t,d.end]))))};m.data=function(s){return m("data",b(s.map(([e,t,i])=>[...x(e),...t,d.end,...b(i)])))};var H=class extends Array{log=[];write(e,t){return this.log.push(e,t),this.push(...e),this}get buffer(){return new Uint8Array(this)}},A=class{types=[];imports=[];tables=[];memories=[];globals=[];exports=[];starts="";elements=[];codes=[];datas=[];constructor(e){e&&Object.assign(this,e)}get funcs(){return this.codes.filter(e=>!e.imported)}ensureType(e,t){let i=[e.join(" "),t.join(" ")].join(),f=this.types.indexOf(i);return f>=0?f:this.types.push(i)-1}getGlobalIndexOf(e){return this.globals.find(t=>t.name===e).idx}getFunc(e){return this.codes.find(t=>t.name===e)}getMemory(e){return console.log(e,this.memories),this.memories.find(t=>t.name===e)}getType(e){return this.types[e]}type(e,t,i){return this.types[e]=this.ensureType(t,i),this}import(e,t,i,f,_,g){if(e==="func"){let y=this._func(t,_,g,[],[],!1,!0);this.imports.push({mod:i,field:f,type:e,desc:[y.type_idx]})}else e==="memory"&&this.imports.push({mod:i,field:f,type:e,desc:_});return this}table(e,t,i){return this.tables.push({type:e,min:t,max:i}),this}memory(e,t,i){return this.memories.push({name:e,min:t,max:i}),this}global(e,t,i,f){let _=this.globals.length;return this.globals.push({idx:_,name:e,valtype:i,mut:t,expr:f}),this}export(e,t,i){return this.exports.push({type:e,name:t,export_name:i}),this}start(e){return this.starts=e,this}elem(e,t){return this.elements.push({offset_idx_expr:e,codes:t}),this}_func(e,t=[],i=[],f=[],_=[],g=!1,y=!1){let u=this.ensureType(t,i),r={idx:this.codes.length,name:e,type_idx:u,locals:f,body:_,imported:y};return this.codes.push(r),g&&this.export("func",e,e),r}func(...e){return this._func(...e),this}data(e,t){return this.datas.push({offset_idx_expr:e,bytes:t}),this}build({metrics:e=!0}={}){let t=new H;t.write(D()),this.types.length&&t.write(m.type(this.types.map(i=>i.split(",").map(f=>f.split(" ").filter(Boolean))))),this.imports.length&&t.write(m.import(this.imports.map(i=>[i.mod,i.field,i.type,i.desc]))),this.funcs.length&&t.write(m.function(this.funcs.map(i=>i.type_idx))),this.elements.length&&t.write(m.table(this.tables.map(i=>[i.type,i.min,i.max]))),this.memories.length&&t.write(m.memory(this.memories.map(i=>[i.min,i.max]))),this.globals.length&&t.write(m.global(this.globals.map(i=>[i.mut,i.valtype,i.expr]))),this.exports.length&&t.write(m.export(this.exports.map(i=>i.type==="func"?[i.export_name,i.type,this.getFunc(i.name).idx]:i.type==="memory"?[i.export_name,i.type,this.getMemory(i.name).idx]:i.type==="global"?[i.export_name,i.type,this.getGlobalIndexOf(i.name)]:[]))),this.starts.length&&t.write(m.start(this.getFunc(this.starts).idx)),this.elements.length&&t.write(m.element(this.elements.map(i=>[0,i.offset_idx_expr,i.codes.map(f=>this.getFunc(f).idx)]))),this.funcs.length&&t.write(m.code(this.funcs.map(i=>[i.locals,i.body]))),this.datas.length&&t.write(m.data(this.datas.map(i=>[0,i.offset_idx_expr,i.bytes])));return t}};var B=class{globals=[];types=[];funcs=[];constructor(e){e&&(Object.assign(this,e),this.funcs.forEach(t=>{t.context=new F(this,t.context)}))}lookup(e,t){let i;switch(t){case"call":i=this.funcs.map(f=>f.name).lastIndexOf(e);break;case"type":i=this.types.map(f=>f.name).lastIndexOf(e);break;default:i=this.globals.map(f=>f.name).lastIndexOf(e)}if(!~i)throw new ReferenceError(`lookup failed at: ${t} "${e}"`);return x(i)}},F=class{#e=null;locals=[];depth=[];constructor(e,t){this.#e=e,t&&Object.assign(this,t)}lookup(e,t){let i;switch(t){case"br":case"br_table":case"br_if":i=this.depth.lastIndexOf(e),~i&&(i=this.depth.length-1-i);break;default:i=this.locals.lastIndexOf(e)}return~i?x(i):this.#e.lookup(e,t)}};function j(s,e,t){let i=new A(e),f=new B(t),_=[];function g(r,n=f,l="i32"){switch(r.kind){case"number":{if(r.value==="inf"||r.value==="+inf")return 1/0;if(r.value==="-inf")return-1/0;if(r.value==="nan"||r.value==="+nan")return NaN;if(r.value==="-nan")return NaN;if(l?.[0]==="f")return parseFloat(r.value)}case"hex":{let a;return l.indexOf("i64")===0?(r.value[0]==="-"?a=-BigInt(r.value.slice(1)):a=BigInt(r.value),a):l[0]==="f"?(r.value.indexOf("nan")>=0?l.indexOf("f32")===0?a=P(r.value):a=$(r.value):a=G(r.value),a):parseInt(r.value)}case"label":return n.lookup(r.value,l);default:return r.value}}function y(r,n,l){if(!(r in v)||typeof v[r]!="function")throw new Error("Unknown instruction: "+r);return[...v[r](n,l)]}function u(r,n=f){let l={offset:0,align:0},a=r.instr.value;switch(a){case"type":return i.getType(r.name.value);case"call_indirect":{let o=[u(r.children.shift(),n),0],p=r.children.flatMap(h=>u(h,n));return y(a,o,p)}case"memory.grow":{let o=[0],p=r.children.flatMap(h=>u(h,n));return y(a,o,p)}case"i32.load":case"i64.load":case"f32.load":case"f64.load":case"i32.load8_s":case"i32.load8_u":case"i32.load16_s":case"i32.load16_u":case"i64.load8_s":case"i64.load8_u":case"i64.load16_s":case"i64.load16_u":case"i64.load32_s":case"i64.load32_u":case"i32.store":case"i64.store":case"f32.store":case"f64.store":case"i32.store8":case"i32.store16":case"i64.store8":case"i64.store16":case"i64.store32":{l.align=R[a];for(let h of r.params)l[h.param.value]=g(h.value);let o=[Math.log2(l.align),l.offset].map(h=>{if(typeof h=="number")return x(h);if(typeof h=="bigint")return M(h)}),p=r.children.flatMap(h=>u(h,n));return y(a,o,p)}case"func":{let o={name:r.name?.value??f.funcs.length,params:[],results:[]};f.funcs.push(o);for(let p of r.children)switch(p.instr.value){case"param":o.params.push(...p.children.map(h=>h.instr.value));break;case"result":o.results.push(...p.children.map(h=>h.instr.value));break}return[o.name,o.params,o.results]}case"result":return r.children.flatMap(o=>v.type[o.instr.value]());case"else":case"then":return r.children.flatMap(o=>u(o,n));case"if":{let o=r.name?.value??n.depth.length,p=[],h=[],w,O;n.depth.push(o);for(let I of r.children)switch(I.instr.value){case"result":p.push(u(I,n));break;case"else":h.push(...v.else());case"then":O=u(I,n),h.push(O);break;default:w?O?(h.push(...v.else()),h.push(u(I,n))):(O=u(I,n),h.push(O)):w=u(I,n)}return n.depth.pop(),p.length||p.push(v.type.void()),[...v.if(p.flat(),w),...h.flat(),...v.end()]}case"loop":case"block":{let o=r.name?.value??n.depth.length,p=[],h=[];n.depth.push(o);for(let w of r.children)switch(w.instr.value){case"result":p.push(u(w,n));break;default:h.push(u(w,n))}return n.depth.pop(),p.length||p.push(v.type.void()),[...v[a](),...p.flat().map(w=>[...w]),...h.flat(),...v.end()]}case"br_table":{r.name&&r.params.unshift({param:{value:n.lookup(r.name.value,a)}});let o=r.params.map(h=>g(h.param,n,a)),p=r.children.flatMap(h=>u(h,n));return y(a,[o.length-1,...o],p)}default:{r.name&&r.params.unshift({param:{value:(a.startsWith("global")?f:n).lookup(r.name.value,a)}});let o=r.params.map(h=>g(h.param,n,a)),p=r.children.flatMap(h=>u(h,n));return y(a,o,p)}}}function c(r){switch(r.instr.value){case"module":r.children.forEach(n=>c(n));break;case"memory":{let n=r.name?.value??i.memories.length,l=r.params.map(a=>g(a.param)).flat();if(r.children?.[0]?.instr.value==="export"){let a=r.children[0].params[0].param.value,o=r.children[0].name?.value??n??0;i.export("memory",o,a)}i.memory(n,...l)}break;case"data":{let n=r.children.shift(),l=r.children.shift().data;i.data(u(n),l)}break;case"start":i.start(r.name.value);break;case"table":{let n=r.params.map(l=>g(l.param));n.unshift(n.pop()),i.table(...n)}break;case"elem":{let n=r.children.shift(),l=r.children.map(a=>a.ref.value);i.elem(u(n),l)}break;case"import":if(r.children[0].instr.value==="func"){let n=r.params.map(o=>g(o.param)),l=u(r.children[0]),a=l.shift();i.import("func",a,...n,...l)}else if(r.children[0].instr.value==="memory"){let n=r.children[0],l=r.params.map(p=>g(p.param)),a=n.instr.name,o=n.params.map(p=>g(p.param));i.import("memory",a,...l,o)}break;case"global":{let n={name:r.name?.value??i.globals.length,vartype:"const",type:r.children[0].instr.value};if(f.globals.push(n),n.type==="export"){let a=r.children.shift().params[0].param.value;i.export("global",n.name,a),n.type=r.children[0].instr.value}n.type==="mut"&&(n.vartype="var",n.type=r.children[0].children[0].instr.value);let l=r.children[1];i.global(n.name,n.vartype,n.type,u(l))}break;case"type":{let n={name:r.name?.value??i.types.length,params:[],results:[]};f.types.push(n);for(let l of r.children[0].children)switch(l.instr.value){case"param":n.params.push(...l.children.map(a=>a.instr.value));break;case"result":n.results.push(...l.children.map(a=>a.instr.value));break}i.type(n.name,n.params,n.results)}break;case"export":{let n={name:r.params[0].param.value};n.type=r.children[0].instr.value,n.internal_name=r.children[0].name.value,i.export(n.type,n.internal_name,n.name)}break;case"func":{let n={name:r.name?.value??f.funcs.length,context:new F(f),params:[],results:[],locals:[],body:[]};f.funcs.push(n);for(let l of r.children)switch(l.instr.value){case"export":{let a=l.params[0].param.value;i.export("func",n.name,a)}break;case"local":n.locals.push(...l.children.map(a=>a.instr.value)),n.context.locals.push(...l.children.map(()=>l.name?.value));break;case"param":n.params.push(...l.children.map(a=>a.instr.value)),n.context.locals.push(...l.children.map(()=>l.name?.value));break;case"result":n.results.push(...l.children.map(a=>a.instr.value));break;default:n.body.push(l)}_.push(()=>{i.func(n.name,n.params,n.results,n.locals,[...n.body.flatMap(l=>u(l,n.context))])})}break}}return c(s),_.forEach(r=>r()),{module:i,global:f}}var re=new RegExp([/(?<comment>;;.*|\(;[^]*?;\))/,/"(?<string>(?:\\"|[^"])*?)"/,/(?<param>offset|align|shared|funcref)=?/,/(?<hex>([+-]?nan:)?[+-]?0x[0-9a-f.p+-_]+)/,/(?<number>[+-]?inf|[+-]?nan|[+-]?\d[\d.e_+-]*)/,/(?<instr>[a-z][a-z0-9!#$%&'*+\-./:<=>?@\\^_`|~]+)/,/\$(?<label>[a-z0-9!#$%&'*+\-./:<=>?@\\^_`|~]+)/,/(?<lparen>\()|(?<rparen>\))|(?<nul>[ \t\n]+)|(?<error>.)/].map(s=>s.toString().slice(1,-1)).join("|"),"gi");function V(s){let e={},t={},i=s.matchAll(re);function f(){let r=i.next();if(r.done)return{value:{value:null,kind:"eof",index:s.length},done:!0};let[n,l]=Object.entries(r.value.groups).filter(a=>a[1]!=null)[0];return{value:{value:l,kind:n,index:r.value.index},done:!1}}function _(){e=t;do t=f().value;while(t.kind==="nul"||t.kind==="comment");return e}function g(r,n){return r!=null?n!=null?n===t.value:r===t.kind:t}function y(r,n){if(r===t.kind)if(n!=null){if(n===t.value)return _()}else return _();return null}function u(r,n){let l=y(r,n);if(!l)throw new SyntaxError("Unexpected token: "+t.value+`
expected: `+r+(n?' "'+n+'"':"")+`
but received: `+t.kind+`
at position: `+t.index);return l}return{[Symbol.iterator](){return this},next:f,advance:_,peek:g,accept:y,expect:u,start:_}}function N({start:s,peek:e,accept:t,expect:i}){let f=new TextEncoder("utf-8"),_=/[0-9a-f]/i,g={t:9,n:10,r:13,'"':34,"'":39,"\\":92};function y(){let r=[];for(;;){let n=t("string");if(!n)break;for(let l=0,a,o;l<n.value.length;l++){if(a=n.value[l],a==="\\"){if(o=n.value[l+1],o in g){r.push(g[o]),l++;continue}else if(_.test(o)){_.test(n.value[l+2])?r.push(parseInt(`${o}${n.value[l+=2]}`,16)):(r.push(parseInt(o,16)),l++);continue}}r.push(f.encode(a))}}return r}function*u(){let r;for(;;){if(r=t("number")){r.value=r.value.replace(/_/g,""),yield{param:r};continue}if(r=t("hex")){r.value=r.value.replace(/_/g,""),yield{param:r};continue}if(r=t("string")){yield{param:r};continue}if(r=t("label")){yield{param:r};continue}if(r=t("param")){let n;if(n=t("number")){yield{param:r,value:n};continue}if(n=t("hex")){yield{param:r,value:n};continue}else{yield{param:r};continue}}break}}function c(){let r=t("label");if(r)return{ref:r};if(e("string"))return{data:y()};let n=t("lparen"),l;if(n)l=i("instr");else if(l=t("instr"),!l)return;let a={instr:l,name:t("label"),params:[...u()],children:[]};if(n){let o;for(;!e("eof")&&(o=c());)a.children.push(o);a.params.push(...u()),i("rparen")}else if(l.value==="block"||l.value==="loop"){let o;for(;!e("eof")&&!e("instr","end")&&(o=c());)a.children.push(o);i("instr","end")}return a}return s(),c()}function ie(s,e,t={}){return j(N(V("(module "+s+")")),t.module,t.global).module.build(e).buffer}export{B as GlobalContext,A as ModuleBuilder,j as compile,ie as default,N as parse,V as tokenize};
//!time 'module build'
//!timeEnd 'module build'