UNPKG

@marioslab/ulang-vm

Version:

Virtual machine & assembler for recreational programming like it's 1992.

3 lines (2 loc) 23.3 kB
var ulang=(()=>{var q=Object.defineProperty,fe=Object.getOwnPropertyDescriptor,pe=Object.getOwnPropertyNames,ce=Object.prototype.hasOwnProperty,_e=(e,a)=>{for(var t in a)q(e,t,{get:a[t],enumerable:!0})},me=(e,a,t,u)=>{if(a&&typeof a=="object"||typeof a=="function")for(let s of pe(a))!ce.call(e,s)&&s!==t&&q(e,s,{get:()=>a[s],enumerable:!(u=fe(a,s))||u.enumerable});return e},he=e=>me(q({},"__esModule",{value:!0}),e),z=(e,a,t)=>new Promise((u,s)=>{var U=g=>{try{T(t.next(g))}catch(w){s(w)}},E=g=>{try{T(t.throw(g))}catch(w){s(w)}},T=g=>g.done?u(g.value):Promise.resolve(g.value).then(U,E);T((t=t.apply(e,a)).next())}),st={};_e(st,{LogLevel:()=>Nt,UlangPlayer:()=>qt,VirtualMachine:()=>Xt,VirtualMachineState:()=>Wt,compile:()=>Pt,createPlayerFromGist:()=>Fe,loadUlang:()=>jt,printMemory:()=>Ue});var ge=(()=>{var e=typeof document!="undefined"&&document.currentScript?document.currentScript.src:void 0;return function(a){a=a||{};var t=typeof a!="undefined"?a:{},u,s;t.ready=new Promise(function(n,r){u=n,s=r});var U=Object.assign({},t),E=[],T="./this.program",g=(n,r)=>{throw r},w=!0,L=!1,b="";function Ie(n){return t.locateFile?t.locateFile(n,b):b+n}var Be,ke,K,De;(w||L)&&(L?b=self.location.href:typeof document!="undefined"&&document.currentScript&&(b=document.currentScript.src),e&&(b=e),b.indexOf("blob:")!==0?b=b.substr(0,b.replace(/[?#].*/,"").lastIndexOf("/")+1):b="",Be=n=>{var r=new XMLHttpRequest;return r.open("GET",n,!1),r.send(null),r.responseText},L&&(K=n=>{var r=new XMLHttpRequest;return r.open("GET",n,!1),r.responseType="arraybuffer",r.send(null),new Uint8Array(r.response)}),ke=(n,r,i)=>{var l=new XMLHttpRequest;l.open("GET",n,!0),l.responseType="arraybuffer",l.onload=()=>{if(l.status==200||l.status==0&&l.response){r(l.response);return}i()},l.onerror=i,l.send(null)},De=n=>document.title=n);var Oe=t.print||console.log.bind(console),H=t.printErr||console.warn.bind(console);Object.assign(t,U),U=null,t.arguments&&(E=t.arguments),t.thisProgram&&(T=t.thisProgram),t.quit&&(g=t.quit);function Ve(n,r){if(typeof WebAssembly.Function=="function"){for(var i={i:"i32",j:"i64",f:"f32",d:"f64"},l={parameters:[],results:r[0]=="v"?[]:[i[r[0]]]},_=1;_<r.length;++_)l.parameters.push(i[r[_]]);return new WebAssembly.Function(l,n)}var c=[1,0,1,96],p=r.slice(0,1),f=r.slice(1),h={i:127,j:126,f:125,d:124};c.push(f.length);for(var _=0;_<f.length;++_)c.push(h[f[_]]);p=="v"?c.push(0):c=c.concat([1,h[p]]),c[1]=c.length-2;var d=new Uint8Array([0,97,115,109,1,0,0,0].concat(c,[2,7,1,1,101,1,102,0,0,7,5,1,1,102,0,0])),A=new WebAssembly.Module(d),j=new WebAssembly.Instance(A,{e:{f:n}}),P=j.exports.f;return P}var zt=[],C;function Me(){if(zt.length)return zt.pop();try{I.grow(1)}catch(n){throw n instanceof RangeError?"Unable to grow wasm table. Set ALLOW_TABLE_GROWTH.":n}return I.length-1}function Pe(n,r){for(var i=n;i<n+r;i++){var l=rt(i);l&&C.set(l,i)}}function We(n,r){if(C||(C=new WeakMap,Pe(0,I.length)),C.has(n))return C.get(n);var i=Me();try{ie(i,n)}catch(_){if(!(_ instanceof TypeError))throw _;var l=Ve(n,r);ie(i,l)}return C.set(n,i),i}var D;t.wasmBinary&&(D=t.wasmBinary);var zn=t.noExitRuntime||!0;typeof WebAssembly!="object"&&et("no native wasm support detected");var N,Gt=!1,Ne;function xt(n){var r=t["_"+n];return r}function Xe(n,r,i,l,_){var c={string:function(v){var B=0;if(v!=null&&v!==0){var oe=(v.length<<2)+1;B=at(oe),je(v,B,oe)}return B},array:function(v){var B=at(v.length);return Ge(v,B),B}};function p(v){return r==="string"?Jt(v):r==="boolean"?Boolean(v):v}var f=xt(n),h=[],d=0;if(l)for(var A=0;A<l.length;A++){var j=c[i[A]];j?(d===0&&(d=ue()),h[A]=j(l[A])):h[A]=l[A]}var P=f.apply(null,h);function jn(v){return d!==0&&le(d),p(v)}return P=jn(P),P}function Ye(n,r,i,l){i=i||[];var _=i.every(function(p){return p==="number"}),c=r!=="string";return c&&_&&!l?xt(n):function(){return Xe(n,r,i,arguments,l)}}var $t=typeof TextDecoder!="undefined"?new TextDecoder("utf8"):void 0;function Z(n,r,i){for(var l=r+i,_=r;n[_]&&!(_>=l);)++_;if(_-r>16&&n.subarray&&$t)return $t.decode(n.subarray(r,_));for(var c="";r<_;){var p=n[r++];if(!(p&128)){c+=String.fromCharCode(p);continue}var f=n[r++]&63;if((p&224)==192){c+=String.fromCharCode((p&31)<<6|f);continue}var h=n[r++]&63;if((p&240)==224?p=(p&15)<<12|f<<6|h:p=(p&7)<<18|f<<12|h<<6|n[r++]&63,p<65536)c+=String.fromCharCode(p);else{var d=p-65536;c+=String.fromCharCode(55296|d>>10,56320|d&1023)}}return c}function Jt(n,r){return n?Z(F,n,r):""}function Kt(n,r,i,l){if(!(l>0))return 0;for(var _=i,c=i+l-1,p=0;p<n.length;++p){var f=n.charCodeAt(p);if(f>=55296&&f<=57343){var h=n.charCodeAt(++p);f=65536+((f&1023)<<10)|h&1023}if(f<=127){if(i>=c)break;r[i++]=f}else if(f<=2047){if(i+1>=c)break;r[i++]=192|f>>6,r[i++]=128|f&63}else if(f<=65535){if(i+2>=c)break;r[i++]=224|f>>12,r[i++]=128|f>>6&63,r[i++]=128|f&63}else{if(i+3>=c)break;r[i++]=240|f>>18,r[i++]=128|f>>12&63,r[i++]=128|f>>6&63,r[i++]=128|f&63}}return r[i]=0,i-_}function je(n,r,i){return Kt(n,F,r,i)}function qe(n){for(var r=0,i=0;i<n.length;++i){var l=n.charCodeAt(i);l>=55296&&l<=57343&&(l=65536+((l&1023)<<10)|n.charCodeAt(++i)&1023),l<=127?++r:l<=2047?r+=2:l<=65535?r+=3:r+=4}return r}function ze(n){var r=qe(n)+1,i=se(r);return i&&Kt(n,Q,i,r),i}function Ge(n,r){Q.set(n,r)}var Zt,Q,F,xe,$e,O,Je,Ke,Ze;function Qt(n){Zt=n,t.HEAP8=Q=new Int8Array(n),t.HEAP16=xe=new Int16Array(n),t.HEAP32=O=new Int32Array(n),t.HEAPU8=F=new Uint8Array(n),t.HEAPU16=$e=new Uint16Array(n),t.HEAPU32=Je=new Uint32Array(n),t.HEAPF32=Ke=new Float32Array(n),t.HEAPF64=Ze=new Float64Array(n)}var Gn=t.INITIAL_MEMORY||16777216,I,te=[],ee=[],ne=[],Qe=!1;function tn(){if(t.preRun)for(typeof t.preRun=="function"&&(t.preRun=[t.preRun]);t.preRun.length;)rn(t.preRun.shift());nt(te)}function en(){Qe=!0,nt(ee)}function nn(){if(t.postRun)for(typeof t.postRun=="function"&&(t.postRun=[t.postRun]);t.postRun.length;)sn(t.postRun.shift());nt(ne)}function rn(n){te.unshift(n)}function an(n){ee.unshift(n)}function sn(n){ne.unshift(n)}var R=0,tt=null,V=null;function un(n){R++,t.monitorRunDependencies&&t.monitorRunDependencies(R)}function ln(n){if(R--,t.monitorRunDependencies&&t.monitorRunDependencies(R),R==0&&(tt!==null&&(clearInterval(tt),tt=null),V)){var r=V;V=null,r()}}t.preloadedImages={},t.preloadedAudios={};function et(n){t.onAbort&&t.onAbort(n),n="Aborted("+n+")",H(n),Gt=!0,Ne=1,n+=". Build with -s ASSERTIONS=1 for more info.";var r=new WebAssembly.RuntimeError(n);throw s(r),r}var on="data:application/octet-stream;base64,";function re(n){return n.startsWith(on)}var y;y="ulang.wasm",re(y)||(y=Ie(y));function ae(n){try{if(n==y&&D)return new Uint8Array(D);if(K)return K(n);throw"both async and sync fetching of the wasm failed"}catch(r){et(r)}}function fn(){return!D&&(w||L)&&typeof fetch=="function"?fetch(y,{credentials:"same-origin"}).then(function(n){if(!n.ok)throw"failed to load wasm binary file at '"+y+"'";return n.arrayBuffer()}).catch(function(){return ae(y)}):Promise.resolve().then(function(){return ae(y)})}function pn(){var n={a:vn};function r(p,f){var h=p.exports;t.asm=h,N=t.asm.d,Qt(N.buffer),I=t.asm.F,an(t.asm.e),ln("wasm-instantiate")}un("wasm-instantiate");function i(p){r(p.instance)}function l(p){return fn().then(function(f){return WebAssembly.instantiate(f,n)}).then(function(f){return f}).then(p,function(f){H("failed to asynchronously prepare wasm: "+f),et(f)})}function _(){return!D&&typeof WebAssembly.instantiateStreaming=="function"&&!re(y)&&typeof fetch=="function"?fetch(y,{credentials:"same-origin"}).then(function(p){var f=WebAssembly.instantiateStreaming(p,n);return f.then(i,function(h){return H("wasm streaming compile failed: "+h),H("falling back to ArrayBuffer instantiation"),l(i)})}):l(i)}if(t.instantiateWasm)try{var c=t.instantiateWasm(n,r);return c}catch(p){return H("Module.instantiateWasm callback failed with error: "+p),!1}return _().catch(s),{}}function nt(n){for(;n.length>0;){var r=n.shift();if(typeof r=="function"){r(t);continue}var i=r.func;typeof i=="number"?r.arg===void 0?rt(i)():rt(i)(r.arg):i(r.arg===void 0?null:r.arg)}}var M=[];function rt(n){var r=M[n];return r||(n>=M.length&&(M.length=n+1),M[n]=r=I.get(n)),r}function ie(n,r){I.set(n,r),M[n]=r}function cn(n,r,i){F.copyWithin(n,r,r+i)}function _n(){return 2147483648}function mn(n){try{return N.grow(n-Zt.byteLength+65535>>>16),Qt(N.buffer),1}catch(r){}}function hn(n){var r=F.length;n=n>>>0;var i=_n();if(n>i)return!1;let l=(h,d)=>h+(d-h%d)%d;for(var _=1;_<=4;_*=2){var c=r*(1+.2/_);c=Math.min(c,n+100663296);var p=Math.min(i,l(Math.max(n,c),65536)),f=mn(p);if(f)return!0}return!1}var X={buffers:[null,[],[]],printChar:function(n,r){var i=X.buffers[n];r===0||r===10?((n===1?Oe:H)(Z(i,0)),i.length=0):i.push(r)},varargs:void 0,get:function(){X.varargs+=4;var n=O[X.varargs-4>>2];return n},getStr:function(n){var r=Jt(n);return r},get64:function(n,r){return n}};function gn(n,r,i,l){for(var _=0,c=0;c<i;c++){var p=O[r>>2],f=O[r+4>>2];r+=8;for(var h=0;h<f;h++)X.printChar(n,F[p+h]);_+=f}return O[l>>2]=_,0}var vn={c:cn,b:hn,a:gn},xn=pn(),dn=t.___wasm_call_ctors=function(){return(dn=t.___wasm_call_ctors=t.asm.e).apply(null,arguments)},se=t._malloc=function(){return(se=t._malloc=t.asm.f).apply(null,arguments)},yn=t._free=function(){return(yn=t._free=t.asm.g).apply(null,arguments)},bn=t._ulang_free=function(){return(bn=t._ulang_free=t.asm.h).apply(null,arguments)},wn=t._ulang_calloc=function(){return(wn=t._ulang_calloc=t.asm.i).apply(null,arguments)},An=t._ulang_print_memory=function(){return(An=t._ulang_print_memory=t.asm.j).apply(null,arguments)},Tn=t._ulang_file_from_memory=function(){return(Tn=t._ulang_file_from_memory=t.asm.k).apply(null,arguments)},Rn=t._ulang_file_free=function(){return(Rn=t._ulang_file_free=t.asm.l).apply(null,arguments)},Sn=t._ulang_error_free=function(){return(Sn=t._ulang_error_free=t.asm.m).apply(null,arguments)},Un=t._ulang_error_print=function(){return(Un=t._ulang_error_print=t.asm.n).apply(null,arguments)},En=t._ulang_compile=function(){return(En=t._ulang_compile=t.asm.o).apply(null,arguments)},Ln=t._ulang_program_free=function(){return(Ln=t._ulang_program_free=t.asm.p).apply(null,arguments)},Hn=t._ulang_vm_print=function(){return(Hn=t._ulang_vm_print=t.asm.q).apply(null,arguments)},Cn=t._ulang_vm_step=function(){return(Cn=t._ulang_vm_step=t.asm.r).apply(null,arguments)},Fn=t._ulang_vm_init=function(){return(Fn=t._ulang_vm_init=t.asm.s).apply(null,arguments)},In=t._ulang_vm_step_n=function(){return(In=t._ulang_vm_step_n=t.asm.t).apply(null,arguments)},Bn=t._ulang_vm_step_n_bp=function(){return(Bn=t._ulang_vm_step_n_bp=t.asm.u).apply(null,arguments)},kn=t._ulang_vm_pop_int=function(){return(kn=t._ulang_vm_pop_int=t.asm.v).apply(null,arguments)},Dn=t._ulang_vm_pop_uint=function(){return(Dn=t._ulang_vm_pop_uint=t.asm.w).apply(null,arguments)},On=t._ulang_vm_pop_float=function(){return(On=t._ulang_vm_pop_float=t.asm.x).apply(null,arguments)},Vn=t._ulang_vm_push_int=function(){return(Vn=t._ulang_vm_push_int=t.asm.y).apply(null,arguments)},Mn=t._ulang_vm_push_uint=function(){return(Mn=t._ulang_vm_push_uint=t.asm.z).apply(null,arguments)},Pn=t._ulang_vm_push_float=function(){return(Pn=t._ulang_vm_push_float=t.asm.A).apply(null,arguments)},Wn=t._ulang_vm_free=function(){return(Wn=t._ulang_vm_free=t.asm.B).apply(null,arguments)},Nn=t._ulang_sizeof=function(){return(Nn=t._ulang_sizeof=t.asm.C).apply(null,arguments)},Xn=t._ulang_print_offsets=function(){return(Xn=t._ulang_print_offsets=t.asm.D).apply(null,arguments)},Yn=t._ulang_argb_to_rgba=function(){return(Yn=t._ulang_argb_to_rgba=t.asm.E).apply(null,arguments)},ue=t.stackSave=function(){return(ue=t.stackSave=t.asm.G).apply(null,arguments)},le=t.stackRestore=function(){return(le=t.stackRestore=t.asm.H).apply(null,arguments)},at=t.stackAlloc=function(){return(at=t.stackAlloc=t.asm.I).apply(null,arguments)};t.cwrap=Ye,t.UTF8ArrayToString=Z,t.addFunction=We,t.allocateUTF8=ze;var Y;V=function n(){Y||it(),Y||(V=n)};function it(n){if(n=n||E,R>0||(tn(),R>0))return;function r(){Y||(Y=!0,t.calledRun=!0,!Gt&&(en(),u(t),t.onRuntimeInitialized&&t.onRuntimeInitialized(),nn()))}t.setStatus?(t.setStatus("Running..."),setTimeout(function(){setTimeout(function(){t.setStatus("")},1),r()},1)):r()}if(t.run=it,t.preInit)for(typeof t.preInit=="function"&&(t.preInit=[t.preInit]);t.preInit.length>0;)t.preInit.pop()();return it(),a.ready}})(),ve=ge,o={onRuntimeInitialized:Te};function de(){return z(this,null,function*(){return yield ve(o)})}var G,S,ut,lt,ot,ft,pt,ct,_t,mt,ht,gt,vt,dt,yt,bt,wt,At,Tt,Rt,St,Ut,ye,be,we=e=>new DataView(o.HEAPU8.buffer).getInt8(e),Ae=e=>new DataView(o.HEAPU8.buffer).getInt16(e,!0),m=e=>new DataView(o.HEAPU8.buffer).getUint32(e,!0),Et=(e,a)=>new DataView(o.HEAPU8.buffer).setUint32(e,a,!0),x=e=>new DataView(o.HEAPU8.buffer).getInt32(e,!0),Lt=e=>new DataView(o.HEAPU8.buffer).getFloat32(e,!0),Ht,Ct,Ft,$;function Te(){G=o.cwrap("ulang_calloc","ptr",["number"]),S=o.cwrap("ulang_free","void",["ptr"]),ut=o.cwrap("ulang_print_memory","void",[]),lt=o.cwrap("ulang_file_from_memory","number",["ptr","ptr","ptr"]),ot=o.cwrap("ulang_file_free","void",["ptr"]),ft=o.cwrap("ulang_error_print","void",["ptr"]),pt=o.cwrap("ulang_error_free","void",["ptr"]),ct=o.cwrap("ulang_compile","number",["ptr","ptr","ptr"]),_t=o.cwrap("ulang_program_free","void",["ptr"]),mt=o.cwrap("ulang_vm_init","void",["ptr","ptr"]),ht=o.cwrap("ulang_vm_step","number",["ptr"]),gt=o.cwrap("ulang_vm_step_n","number",["ptr","number"]),vt=o.cwrap("ulang_vm_step_n_bp","number",["ptr","number","ptr","number"]),dt=o.cwrap("ulang_vm_print","void",["ptr"]),yt=o.cwrap("ulang_vm_pop_int","number",["ptr"]),bt=o.cwrap("ulang_vm_pop_uint","number",["ptr"]),wt=o.cwrap("ulang_vm_pop_float","number",["ptr"]),At=o.cwrap("ulang_vm_push_int","void",["ptr","number"]),Tt=o.cwrap("ulang_vm_push_uint","void",["ptr","number"]),Rt=o.cwrap("ulang_vm_push_float","void",["ptr","number"]),St=o.cwrap("ulang_vm_free","void",["ptr"]),Ut=o.cwrap("ulang_sizeof","number",["number"]),ye=o.cwrap("ulang_print_offsets","void",[]),Ht=be=o.cwrap("ulang_argb_to_rgba","ptr",["ptr","ptr","number"]),Ct=o.addFunction,Ft=o.UTF8ArrayToString,$=()=>o.HEAP8}function k(e){return{ptr:e,data:()=>m(e),length:()=>m(e+4),toString:()=>o.UTF8ArrayToString(o.HEAPU8,m(e),m(e+4))}}function It(e){return{ptr:e,data:()=>k(e),startLine:()=>m(e+8),endLine:()=>m(e+12)}}function Re(e){return{ptr:e,data:()=>k(e),lineNumber:()=>m(e+8)}}function J(e){return{ptr:e,fileName:()=>k(e),data:()=>k(e+8),lines:()=>{let a=[],t=m(e+16),u=m(e+20);for(let s=0;s<u+1;s++)a.push(Re(t)),t+=12;return a},free:()=>{ot(e),S(e)}}}function Bt(e){return{ptr:e,file:()=>J(m(e)),span:()=>It(e+4),message:()=>k(e+20),isSet:()=>x(e+28)!=0,print:()=>ft(e),free:()=>{pt(e),S(e)}}}function kt(e){return{ptr:e,label:()=>It(e),target:()=>m(e+16),address:()=>m(e+20)}}function Dt(e){return{ptr:e,code:()=>new DataView(o.HEAPU8.buffer,m(e),m(e+4)),data:()=>new DataView(o.HEAPU8.buffer,m(e+8),m(e+12)),reservedBytes:()=>m(e+16),labels:()=>{let a=[],t=m(e+20),u=m(e+24);for(let s=0;s<u;s++)a.push(kt(t)),t+=24;return a},constants:()=>{let a=[],t=m(e+28),u=m(e+32);for(let s=0;s<u;s++)a.push(kt(t)),t+=24;return a},file:()=>J(e+36),addressToLine:()=>{let a=[],t=m(e+40),u=m(e+44);for(let s=0;s<u;s++)a.push(m(t)),t+=4;return a},free:()=>{_t(e),S(e)}}}function Se(e){return{ptr:e,b:()=>we(e),s:()=>Ae(e),i:()=>x(e),ui:()=>m(e),f:()=>Lt(e)}}function Ot(e){return{ptr:e,registers:()=>{let a=[],t=e;for(let u=0;u<16;u++)a.push(Se(t)),t+=4;return a},memory:()=>new DataView(o.HEAPU8,m(e+64),m(e+68)),memoryPtr:()=>m(e+64),error:()=>Bt(m(e+1096)),program:()=>Dt(m(e+1128)),setSyscall:(a,t)=>{a<0||a>255||Et(e+72+a*4,t)},step:()=>ht(e)!=0,stepN:a=>gt(e,a)!=0,stepNBP:(a,t,u)=>vt(e,a,t,u),print:()=>dt(e),popInt:()=>yt(e),popUint:()=>bt(e),popFloat:()=>wt(e),pushInt:a=>At(e,a),pushUint:a=>Tt(e,a),pushFloat:a=>Rt(e,a),free:()=>{St(e),S(e)}}}function Vt(e){return G(e)}function W(e){return G(Ut(e))}function Mt(e){return S(e)}function Ue(){ut()}function Ee(e,a){let t=o.allocateUTF8(e),u=o.allocateUTF8(a),s=W(0);return lt(t,u,s),o._free(t),o._free(u),J(s)}function Le(){return Bt(W(1))}function He(){return Dt(W(2))}function Pt(e){let a=Le(),t=Ee("source",e),u=He(),s={error:a,file:t,program:u,free:()=>{u.free(),t.free(),a.free()}};return ct(s.file.ptr,s.program.ptr,s.error.ptr),s}function Ce(e){let a=Ot(W(3));return mt(a.ptr,e.ptr),a}var qn=1024*1024*32,Wt=(e=>(e[e.Stopped=0]="Stopped",e[e.Running=1]="Running",e[e.Paused=2]="Paused",e))(Wt||{}),Nt=(e=>(e[e.None=0]="None",e[e.Info=1]="Info",e))(Nt||{}),Xt=class{constructor(e){this.state=0,this.compilerResult=null,this.vmStart=0,this.executedInstructions=0,this.vsyncHit=!1,this.debugSyscallHit=!1,this.breakpoints=[],this.bpPtr=0,this.numBps=0,this.syscallHandlerPtr=0,this.lastStepHitBreakpoint=!1,this.stateChangeListener=null,this.logLevel=1,this.mouseX=0,this.mouseY=0,this.mouseButtonDown=!1,this.keys=new Map,this.listeners=[],typeof e=="string"?this.canvas=document.getElementById(e):this.canvas=e,this.rgbaFramePtr=Vt(320*240*4),this.addEventListener("mousemove",t=>{var u=this.canvas.getBoundingClientRect();this.mouseX=(t.clientX-u.left)/this.canvas.clientWidth*320|0,this.mouseY=(t.clientY-u.top)/this.canvas.clientHeight*240|0}),this.addEventListener("mousedown",t=>{var u=this.canvas.getBoundingClientRect();this.mouseX=(t.clientX-u.left)/this.canvas.clientWidth*320|0,this.mouseY=(t.clientY-u.top)/this.canvas.clientHeight*240|0,this.mouseButtonDown=!0}),this.addEventListener("mouseup",t=>{var u=this.canvas.getBoundingClientRect();this.mouseX=(t.clientX-u.left)/this.canvas.clientWidth*320|0,this.mouseY=(t.clientY-u.top)/this.canvas.clientHeight*240|0,this.mouseButtonDown=!1}),this.addEventListener("mouseleave",t=>{var u=this.canvas.getBoundingClientRect();this.mouseX=(t.clientX-u.left)/this.canvas.clientWidth*320|0,this.mouseY=(t.clientY-u.top)/this.canvas.clientHeight*240|0,this.mouseButtonDown=!1});let a=(t,u)=>{let s=Ot(u);switch(t){case 0:return this.pause(),this.debugSyscallHit=!0,0;case 1:let U=s.popUint();Ht(s.memoryPtr()+U,this.rgbaFramePtr,320*240);let E=new Uint8ClampedArray($().buffer,this.rgbaFramePtr,320*240*4),T=new ImageData(E,320,240);return this.canvas.getContext("2d").putImageData(T,0,0),this.vsyncHit=!0,0;case 2:{let g="";for(;;){let w=s.popUint();if(w==6)break;switch(w){case 0:g+=s.popInt();break;case 1:g+="0x"+s.popInt().toString(16);break;case 2:g+=s.popFloat();break;case 3:let L=s.popUint();g+=Ft($(),s.memoryPtr()+L);break;case 4:g+=" ";break;case 5:g+=` `;break;default:break}}return console.log(g),-1}case 3:return s.pushInt(this.mouseX),s.pushInt(this.mouseY),s.pushInt(this.mouseButtonDown?-1:0),-1;case 4:case 5:return s.pushFloat(performance.now()/1e3),-1}};this.syscallHandlerPtr=Ct(a,"iii")}addEventListener(e,a){this.canvas.addEventListener(e,a),this.listeners.push({type:e,listener:a})}dipose(){this.stop();for(let e=0;e<this.listeners.length;e++){let a=this.listeners[e];this.canvas.removeEventListener(a.type,a.listener)}Mt(this.rgbaFramePtr)}setLogLevel(e){this.logLevel=e}setStateChangeListener(e){this.stateChangeListener=e}setBreakpoints(e){this.breakpoints=e,this.bpPtr!=0&&(Mt(this.bpPtr),this.bpPtr=0,this.numBps=0),this.state!=0&&this.calculateBreakpoints()}calculateBreakpoints(){if(this.bpPtr!=0)return this.bpPtr;let e=this.vm.program().addressToLine(),a=this.bpPtr=Vt(4*this.breakpoints.length);for(let t=0;t<this.breakpoints.length;t++){let u=this.breakpoints[t];for(let s=0;s<e.length;s++)if(e[s]==u){Et(a,s*4),a+=4,this.numBps++;break}}return this.bpPtr}run(e){if(this.compilerResult&&(this.compilerResult.free(),this.compilerResult=null),this.vm&&(this.vm.free(),this.vm=null),this.compilerResult=Pt(e),this.compilerResult.error.isSet()){alert("Can't run program with errors."),this.compilerResult.error.print(),this.compilerResult.free(),this.stateChangeListener&&this.stateChangeListener(this,this.state);return}this.vm=Ce(this.compilerResult.program);for(let a=0;a<=255;a++)this.vm.setSyscall(a,this.syscallHandlerPtr);this.vmStart=performance.now(),this.executedInstructions=0,this.lastStepHitBreakpoint=!1,this.state=1,this.stateChangeListener&&this.stateChangeListener(this,this.state),requestAnimationFrame(()=>this.frame())}stop(){this.state!=1&&this.state!=2||(this.state=0,this.stateChangeListener&&this.stateChangeListener(this,this.state),this.printVmTime(),this.printVmState())}pause(){this.state==1&&(this.state=2,this.stateChangeListener&&this.stateChangeListener(this,this.state),this.printVmState())}continue(){this.state==2&&(this.state=1,this.stateChangeListener&&this.stateChangeListener(this,this.state),this.printVmState(),requestAnimationFrame(()=>this.frame()))}step(){if(this.state==2){if(!this.vm.step())if(this.vsyncHit||this.debugSyscallHit)this.vsyncHit=!1,this.debugSyscallHit=!1;else{this.state=0,this.stateChangeListener&&this.stateChangeListener(this,this.state);return}this.stateChangeListener&&this.stateChangeListener(this,this.state),this.printVmState()}}getCurrentLine(){if(this.state!=2)return-1;let e=this.vm.registers()[14].ui()>>2,a=this.vm.program().addressToLine();return e>=a.length?-1:a[e]}getState(){return this.state}getRegisters(){return this.vm.registers()}getProgram(){return this.vm.program()}getInt(e){return x(this.vm.memoryPtr()+e)}getFloat(e){return Lt(this.vm.memoryPtr()+e)}printVmTime(){if(this.logLevel==0)return;let e=(performance.now()-this.vmStart)/1e3;console.log("VM took "+e+" secs"),console.log("Executed "+this.executedInstructions+" instructions, "+(this.executedInstructions/e|0)+" ins/s")}printVmState(){this.logLevel!=0&&this.vm.print()}getCanvas(){return this.canvas}frame(){if(this.state==1){let e=performance.now();const a=2e4;if(this.lastStepHitBreakpoint&&(this.lastStepHitBreakpoint=!1,!this.vm.step())){if(this.vsyncHit||this.debugSyscallHit){this.vsyncHit=!1,this.debugSyscallHit=!1,requestAnimationFrame(()=>this.frame());return}this.state=0,this.stateChangeListener&&this.stateChangeListener(this,this.state),this.printVmTime(),this.printVmState();return}for(;;){this.executedInstructions+=a;let t=0;if(this.breakpoints.length>0&&this.calculateBreakpoints(),this.numBps>0){if(t=this.vm.stepNBP(a,this.bpPtr,this.numBps),t>=1){this.lastStepHitBreakpoint=!0,this.state=2,this.stateChangeListener&&this.stateChangeListener(this,this.state),this.printVmState();return}}else t=this.vm.stepN(a);if(!t){if(this.vsyncHit||this.debugSyscallHit){this.vsyncHit=!1,this.debugSyscallHit=!1,requestAnimationFrame(()=>this.frame());return}this.state=0,this.stateChangeListener&&this.stateChangeListener(this,this.state),this.printVmTime(),this.printVmState();return}if(performance.now()-e>16){requestAnimationFrame(()=>this.frame());return}}}}},Yt=!1;function jt(){return z(this,null,function*(){Yt||(Yt=!0,yield de())})}function Fe(e,a){return z(this,null,function*(){yield jt();let t=yield fetch(`https:/api.github.com/gists/${a}`);if(t.status>=400)throw new Error(`Couldn't fetch gist ${a}`);let u=yield t.json();if(!u.files||!u.files["source.ul"]||!u.files["source.ul"].content)throw new Error("Gist ${gistId} is not a ulang program.");return new qt(e,u.files["source.ul"].content)})}var qt=class{constructor(e,a){this.vm=new Xt(e),this.source=a}play(){this.vm.run(this.source)}getVirtualMachine(){return this.vm}getSource(){return this.source}dispose(){this.vm&&this.vm.dipose()}};return he(st)})();