import*as Common from"../common/common.js";import*as WasmDis from"../third_party/wasmparser/package/dist/esm/WasmDis.js";import*as WasmParser from"../third_party/wasmparser/package/dist/esm/WasmParser.js";export function dissambleWASM(e,s){try{const t=Common.Base64.decode(e.content);let a=new WasmParser.BinaryReader;a.setData(t,0,t.byteLength);const r=new WasmDis.DevToolsNameGenerator;r.read(a);const o=new Uint8Array(t);a=new WasmParser.BinaryReader;const n=new WasmDis.WasmDisassembler;n.addOffsets=!0,n.exportMetadata=r.getExportMetadata(),n.nameResolver=r.getNameResolver();const i=[],m=[],c=[],f=1e6;let p=131072,l=new Uint8Array(p),d=0,g=0;for(let e=0;e<o.length;){p>o.length-e&&(p=o.length-e);const t=d+p;if(l.byteLength<t){const e=new Uint8Array(t);e.set(l),l=e}for(;d<t;)l[d++]=o[e++];a.setData(l.buffer,0,t,e===o.length);const r=n.disassembleChunk(a,g),h=n.getResult();for(const e of h.lines)i.push(e);for(const e of h.offsets)m.push(e);for(const e of h.functionBodyOffsets)c.push(e);if(i.length>f){i[f]=";; .... text is truncated due to size",i.splice(f+1),m&&m.splice(f+1);break}if(r)break;if(0===a.position){d=t;continue}const u=a.data.subarray(a.position,a.length);d=u.length,l.set(u),g+=a.position;s({event:"progress",params:{percentage:Math.floor(g/o.length*100)}})}s({event:"progress",params:{percentage:99}});const h=i.join("\n");s({event:"progress",params:{percentage:100}}),s({method:"disassemble",result:{source:h,offsets:m,functionBodyOffsets:c}})}catch(t){s({method:"disassemble",error:t})}}