Manic EMU is an all-in-one retro game emulator for iOS. It packs powerful features while keeping a clean, sleek UI and delivering buttery-smooth gameplay.
Swift
let glue=null;function readCString(e,t,r){for(var n=t+r,s=t;e[s]&&!(s>=n);)++s;for(var o="";t<s;)o+=String.fromCharCode(e[t++]);return o}function writeCString(e,t,r){for(var n=r,s=0;s<e.length;++s){var o=e.charCodeAt(s);t[r++]=255&o}return t[r]=0,r-n}class Glue{constructor(){this.instance=null,this.memoryBuffer=null,this.heapu8=null,this.importObject={env:{emscripten_notify_memory_growth:()=>{console.log("growing memory"),this.loadHeap()},log_msg:(e,t)=>{console.log("msg",this.readStringAt(e,t))}}}}async init(e){const t=await WebAssembly.instantiate(e,this.importObject);this.instance=t,this.instance.exports._initialize(),this.exports=this.instance.exports,this.loadHeap()}loadHeap(){this.memoryBuffer=this.exports.memory.buffer,this.heapu8=new Uint8Array(this.memoryBuffer)}malloc(e){return this.instance.exports.malloc(e)}free(e){return this.instance.exports.free(e)}writeInto(e,t){this.heapu8.set(new Uint8Array(t.buffer||t),e)}copyFrom(e,t){const r=new Uint8Array(t);return r.set(this.heapu8.subarray(e,e+t),0),r}readStringAt(e,t){return e?readCString(this.heapu8,e,t):""}allocString(e){const t=this.malloc(e.length+1);return writeCString(e,this.heapu8,t),t}}function createMsgHandler(e,t,r){e.onmessage=async n=>{const s=n.data;if(s&&s.cmd&&s.msgId){const{cmd:r,msgId:n}=s;if(t)try{const o=[],i=await t(r,s,o);e.postMessage({replyFor:n,value:i},o)}catch(t){e.postMessage({replyFor:n,error:t.toString()})}}else r&&r(n)}}createMsgHandler(self,async(e,t,r)=>{if("init"==e)glue=new Glue,await glue.init(t.module);else if("transcode"==e){glue||console.error("no glue?"),t=t.data;const e=glue.malloc(t.byteLength);glue.writeInto(e,t);const n=glue.exports.transcode(e,t.byteLength);if(glue.free(e),n){const e=glue.copyFrom(glue.exports.ob_get_data(n),glue.exports.ob_get_size(n)).buffer;return glue.exports.ob_free(n),r.push(e),e}return null}});