1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-12-25 15:30:03 +00:00
8bitworkshop/lib/zvm/ifvms.min.js
2020-07-07 18:40:03 -05:00

4 lines
40 KiB
JavaScript

/* ZVM v https://github.com/curiousdannii/ifvms.js */
(function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?this:self:global:window,t.ZVM=e()}})(function(){var e=Math.min,t=String.fromCharCode;return function(){function s(l,e,r){function t(o,i){if(!e[o]){if(!l[o]){var _="function"==typeof require&&require;if(!i&&_)return _(o,!0);if(n)return n(o,!0);var g=new Error("Cannot find module '"+o+"'");throw g.code="MODULE_NOT_FOUND",g}var a=e[o]={exports:{}};l[o][0].call(a.exports,function(e){var s=l[o][1][e];return t(s||e)},a,a.exports,s,l,e,r)}return e[o].exports}for(var n="function"==typeof require&&require,a=0;a<r.length;a++)t(r[a]);return t}return s}()({1:[function(e,t){"use strict";var s=e("../common/utils.js"),r=s.Class,i=s.U2S16,n=r.subClass({init:function(e,t){this.e=e,this.v=t},toString:function(){return this.v},U2S:function(){return i(this.v)}}),a=n.subClass({toString:function(){var e=this.v;return this.indirect?"e.indirect("+e+")":0===e?"s[--e.sp]":15>--e?"l["+e+"]":"e.m.getUint16("+(this.e.globals+2*(e-15))+")"},store:function(e){var t=this.v;return this.indirect?"e.indirect("+t+","+e+")":this.returnval?"e.variable("+t+","+e+")":0===t?"t="+e+";s[e.sp++]=t":15>--t?"l["+t+"]="+e:"e.ram.setUint16("+(this.e.globals+2*(t-15))+","+e+")"},U2S:function(){return"e.U2S("+this+")"}}),o=r.subClass({init:function(e,t,s,r,i,n){this.e=e,this.context=t,this.code=s,this.pc=r,this.labels=[this.pc+"/"+this.code],this.next=i,this.operands=n,this.post&&this.post()},toString:function(){return this.label()+(this.func?this.func.apply(this,this.operands):"")},args:function(e){return this.operands.join(e)},label:function(){return"/* "+this.labels.join()+" */ "}}),l=o.subClass({stopper:1}),p=l.subClass({post:function(){this.origfunc=this.func,this.func=this.newfunc},newfunc:function(){return"e.stop=1;e.pc="+this.next+";"+this.origfunc.apply(this,arguments)}}),_=p.subClass({storer:1,post:function(){this.storer=this.operands.pop(),this.origfunc=this.func,this.func=this.newfunc}}),g=r.subClass({init:function(e,t){this.ops=e||[],this.code=t||"||"},toString:function(){for(var e,t=0,s=[];t<this.ops.length;)e=this.ops[t++],s.push(e.func?(e.iftrue?"":"!(")+e.func.apply(e,e.operands)+(e.iftrue?"":")"):e);return(this.invert?"(!(":"(")+s.join(this.code)+(this.invert?"))":")")}}),u=o.subClass({brancher:1,keyword:"if",post:function(){var e,t,s=this.operands.pop(),r=s[1];this.iftrue=s[0],0===r||1===r?e="e.ret("+r+")":(r+=this.next-2,this.context.targets.push(r),e="e.pc="+r),this.result=e+";return",this.offset=r,this.cond=new g([this]),this.context.ops.length&&(t=this.context.ops.pop(),t.offset===r?(this.cond.ops.unshift(t.cond),this.labels=t.labels,this.labels.push(this.pc+"/"+this.code)):this.context.ops.push(t))},toString:function(){var e=this.result;return e instanceof h&&(this.e.options.debug&&(e.context=this.context),e+=e.stopper?"; return":"",1<this.result.ops.length&&(e="\n"+e+"\n",this.e.options.debug&&(e+=this.context.spacer))),this.label()+this.keyword+this.cond+" {"+e+"}"}}),c=u.subClass({storer:1,post:function(){c.super.post.call(this),this.storer=this.operands.pop(),this.storer.returnval=1,this.origfunc=this.func,this.func=this.newfunc},newfunc:function(){return this.storer.store(this.origfunc.apply(this,arguments))}}),d=o.subClass({storer:1,post:function(){this.storer=this.operands.pop()},toString:function(){var e=d.super.toString.call(this);return this.storer?this.storer.store(e):e}}),m=l.subClass({result:{v:-1},toString:function(){return this.label()+"e.call("+this.operands.shift()+","+this.result.v+","+this.next+",["+this.args()+"])"}}),f=m.subClass({storer:1,post:function(){this.result=this.operands.pop()}}),h=r.subClass({init:function(e,t){this.e=e,this.pc=t,this.pre=[],this.ops=[],this.post=[],this.targets=[],e.options.debug&&(this.spacer="")},toString:function(){return this.e.options.debug?(this.context&&(this.spacer=this.context.spacer+" "),this.pre.join("")+(1<this.ops.length?this.spacer:"")+this.ops.join(";\n"+this.spacer)+this.post.join("")):this.pre.join("")+this.ops.join(";")+this.post.join("")}}),U=h.subClass({toString:function(){return this.pre.unshift("var l=e.l,s=e.s,t=0;\n"),U.super.toString.call(this)}});t.exports={Operand:n,Variable:a,Opcode:o,Stopper:l,Pauser:p,PauserStorer:_,BrancherLogic:g,Brancher:u,BrancherStorer:c,Storer:d,Caller:m,CallerStorer:f,Context:h,RoutineContext:U,opcode_builder:function(e,t,s){return s=s||{},t&&(s.func=t),e.subClass(s)}}},{"../common/utils.js":3}],2:[function(e,t){"use strict";var s=e("./utils.js"),r=s.MemoryView,i=s.Class.subClass({init:function(e){if(this.type="",this.chunks=[],e){var t,s,n=r(e),a=12;if("FORM"!==n.getFourCC(0))throw new Error("Not an IFF file");for(this.type=n.getFourCC(8),t=n.getUint32(4)+8;a<t;){if(s=n.getUint32(a+4),0>s||s+a>t)throw new Error("IFF chunk out of range");this.chunks.push({type:n.getFourCC(a),offset:a,data:n.getUint8Array(a+8,s)}),a+=8+s,s%2&&a++}}},write:function(){for(var e,t,s=12,n=0,i=12;n<this.chunks.length;)this.chunks[n].data.buffer&&(this.chunks[n].data=this.chunks[n].data.buffer),this.chunks[n].length=this.chunks[n].data.byteLength||this.chunks[n].data.length,s+=8+this.chunks[n++].length,s%2&&s++;for(e=r(s),e.setFourCC(0,"FORM"),e.setUint32(4,s-8),e.setFourCC(8,this.type),n=0;n<this.chunks.length;)t=this.chunks[n++],e.setFourCC(i,t.type),e.setUint32(i+4,t.length),e.setUint8Array(i+8,t.data),i+=8+t.length,i%2&&i++;return e.buffer}}),n=i.subClass({init:function(e){if(this.super.init.call(this,e),e){if("IFRS"!==this.type)throw new Error("Not a Blorb file");if("RIdx"!==this.chunks[0].type)throw new Error("Malformed Blorb: chunk 1 is not RIdx");for(var t=r(this.chunks[0].data),s=4;s<this.chunks[0].data.length;){if("Exec"===t.getFourCC(s)&&0===t.getUint32(s+4))return void(this.exec=this.chunks.filter(function(e){return e.offset===t.getUint32(s+8)})[0]);s+=12}}}}),a=i.subClass({init:function(e){if(this.super.init.call(this,e),e){if("IFZS"!==this.type)throw new Error("Not a Quetzal savefile");for(var t,s,n,a=0;a<this.chunks.length;)t=this.chunks[a].type,s=this.chunks[a++].data,"CMem"===t||"UMem"===t?(this.memory=s,this.compressed="CMem"==t):"Stks"===t?this.stacks=s:"IFhd"===t&&(n=r(s.buffer),this.release=n.getUint16(0),this.serial=n.getUint8Array(2,6),this.checksum=n.getUint16(8),this.pc=16777215&n.getUint32(9))}},write:function(){this.type="IFZS";var e=r(13);return e.setUint16(0,this.release),e.setUint8Array(2,this.serial),e.setUint32(9,this.pc),e.setUint16(8,this.checksum),this.chunks=[{type:"IFhd",data:e},{type:this.compressed?"CMem":"UMem",data:this.memory},{type:"Stks",data:this.stacks}],this.super.write.call(this)}});t.exports={IFF:i,Blorb:n,Quetzal:a,identify:function(e){var t,s,i,a=r(e);if("FORM"===a.getFourCC(0)&&"IFRS"===a.getFourCC(8)?(t=new n(e),t.exec&&(s=t.exec.type,e=t.exec.data,"GLUL"===s&&(a=r(e),i=a.getUint32(4)),"ZCOD"===s&&(i=e[0]))):"Glul"===a.getFourCC(0)?(s="GLUL",i=a.getUint32(4)):(i=a.getUint8(0),0<i&&9>i&&(s="ZCOD")),s&&i)return{format:s,version:i,data:e}}}},{"./utils.js":3}],3:[function(e,s){"use strict";function r(){for(var e,t,s=arguments[0],r=1;r<arguments.length;)for(t in e=arguments[r++],e)s[t]=e[t];return s}function i(){}function n(e){for(var t=0,s=e.length,r=new Uint16Array(s/2);t<s;)r[t/2]=e[t++]<<8|e[t++];return r}i.subClass=function(e){function t(){this.init&&this.init.apply(this,arguments)}return t.prototype=r(Object.create(this.prototype),e),t.subClass=this.subClass,t.super=t.prototype.super=this.prototype,t},s.exports={extend:r,Class:i,MemoryView:function(e,s,i){return"number"==typeof e?e=new ArrayBuffer(e):e.buffer&&(s|=0,"undefined"==typeof i&&(i=e.byteLength-s),s+=e.byteOffset,e=e.buffer),r(new DataView(e,s,i),{getUint8Array:function(e,t){return e+=this.byteOffset,new Uint8Array(this.buffer.slice(e,e+t))},getUint16Array:function(e,t){return e+=this.byteOffset,n(new Uint8Array(this.buffer,e,2*t))},setUint8Array:function(e,t){t instanceof ArrayBuffer&&(t=new Uint8Array(t)),new Uint8Array(this.buffer,this.byteOffset,this.byteLength).set(t,e)},getFourCC:function(e){return t(this.getUint8(e),this.getUint8(e+1),this.getUint8(e+2),this.getUint8(e+3))},setFourCC:function(e,t){this.setUint8(e,t.charCodeAt(0)),this.setUint8(e+1,t.charCodeAt(1)),this.setUint8(e+2,t.charCodeAt(2)),this.setUint8(e+3,t.charCodeAt(3))}})},U2S16:function(e){return e<<16>>16},S2U16:function(e){return 65535&e},Uint8toUint16Array:n}},{}],4:[function(e,t){"use strict";var s=e("./common/utils.js"),r=e("./common/file.js"),i={stack_len:100000,undo_len:1000000},n=s.Class.subClass(s.extend({init:function(){this.jit={},this.init=this.start},prepare:function(e,t){if(!t.Glk)throw new Error("A reference to Glk is required");this.Glk=t.Glk,this.data=e,this.options=s.extend({},i,t)},start:function(){var e,t=this.Glk;try{if(e=r.identify(this.data),delete this.data,!e||"ZCOD"!==e.format)throw new Error("This is not a Z-Code file");if(0>[3,4,5,8].indexOf(e.version))throw new Error("Unsupported Z-Machine version: "+e.version);this.m=s.MemoryView(e.data),this.staticmem=this.m.getUint16(14),this.ram=s.MemoryView(this.m,0,this.staticmem),this.origram=this.m.getUint8Array(0,this.staticmem);let n="",a=0;for(;30>a;)n+=(16>this.origram[a]?"0":"")+this.origram[a++].toString(16);this.signature=n;let i;const o=this.options.Dialog;if(o)if(this.options.clear_vm_autosave)o.autosave_write(n,null);else if(this.options.do_vm_autosave)try{const e=o.autosave_read(n);e&&(this.do_autorestore(e),i=1)}catch(e){this.log("Autorestore failed, deleting it"),o.autosave_write(n,null)}i||(this.restart(),this.run()),this.quit||(this.glk_event=new t.RefStruct,this.glk_blocking_call?this.glk_event.push_field(this.glk_blocking_call):t.glk_select(this.glk_event)),t.update()}catch(s){t.fatal_error(s),console.log(s)}},resume:function(e){var t,s,r=this.Glk,i=this.glk_event;try{t=i.get_field(0),2===t&&(this.handle_char_input(i.get_field(2)),s=1),3===t&&(this.handle_line_input(i.get_field(2),i.get_field(3)),s=1),5===t&&this.update_screen_size(),"fileref_create_by_prompt"===t&&(s=this.handle_create_fileref(e)),this.glk_blocking_call=null,s&&this.run(),this.quit||(this.glk_event=new r.RefStruct,this.glk_blocking_call?this.glk_event.push_field(this.glk_blocking_call):r.glk_select(this.glk_event)),r.update()}catch(t){r.fatal_error(t),console.log(t)}},get_signature:function(){return this.signature},run:function(){var e,t;for(this.stop=0;!this.stop;)e=this.pc,this.jit[e]||this.compile(),t=this.jit[e](this),isNaN(t)||this.ret(t)},compile:function(){var e=this.disassemble();this.jit[e.pc]=new Function("e",""+e),e.pc<this.staticmem&&this.log("Caching a JIT function in dynamic memory: "+e.pc)}},e("./zvm/runtime.js"),e("./zvm/text.js"),e("./zvm/io.js"),e("./zvm/disassembler.js")));t.exports=n},{"./common/file.js":2,"./common/utils.js":3,"./zvm/disassembler.js":5,"./zvm/io.js":6,"./zvm/runtime.js":8,"./zvm/text.js":9}],5:[function(e,t){var s=e("../common/ast.js");t.exports.disassemble=function(){function e(e,t){for(var s=0;4>s;s++)t.push((192&e)>>6),e<<=2}for(var t,r,i,n,a,o,l,p=this.m,_=this.opcodes,g=new s.RoutineContext(this,this.pc);;){if(r=t=this.pc,n=p.getUint8(t++),190===n?(o=-1,n=p.getUint8(t++)+1e3):128&n?64&n?(o=-1,!(32&n)&&(n&=31)):(o=[(48&n)>>4],3>o[0]&&(n&=207)):(o=[64&n?2:1,32&n?2:1],n&=31),!_[n])throw this.log(""+g),this.stop=1,new Error("Unknown opcode #"+n+" at pc="+r);for(a=_[n].prototype,-1===o&&(o=[],e(p.getUint8(t++),o),(236===n||250===n)&&e(p.getUint8(t++),o)),l=[],i=0;i<o.length;)0===o[i]&&(l.push(new s.Operand(this,p.getUint16(t))),t+=2),1===o[i]&&l.push(new s.Operand(this,p.getUint8(t++))),2===o[i++]&&l.push(new s.Variable(this,p.getUint8(t++)));if(a.storer&&l.push(new s.Variable(this,p.getUint8(t++))),a.brancher&&(i=p.getUint8(t++),l.push([128&i,64&i?63&i:(i<<8|p.getUint8(t++))<<18>>18])),a.printer)for(l.push(t);t<this.eof&&(i=p.getUint8(t),t+=2,!(128&i)););if(this.pc=t,g.ops.push(new _[n](this,g,n,r,t,l)),i=0,a.stopper&&!i)break}return g}},{"../common/ast.js":1}],6:[function(s,r){"use strict";function i(e){const t=[0,8,16,25,33,41,49,58,66,74,82,90,99,107,115,123,132,140,148,156,165,173,181,189,197,206,214,222,230,239,247,255];return t[31&e]<<16|t[(992&e)>>5]<<8|t[(31744&e)>>10]}const n=s("../common/utils.js"),a=n.U2S16,o=function(){for(var e={4294967283:146,4294967284:152,4294967285:148,4294967286:154,4294967288:27,4294967289:8,4294967290:13,4294967291:130,4294967292:129,4294967293:132,4294967294:131},t=0;12>t;)e[4294967279-t]=133+t++;return e}(),l=[0,2,1,10,4,9,5,6],p=[65534,65535,0,29,832,957,22944,31775,30624,32767,23254,17969,11627];r.exports={init_io:function(){this.io=this.io||{reverse:0,bold:0,italic:0,mono:2&this.m.getUint8(17),transcript:1&this.m.getUint8(17),streams:[0,1,{},[],{}],currentwin:0,height:0,maxheight:0,seenheight:0,width:0,row:0,col:0},this.open_windows()},erase_line:function(e){if(1===e){var t=this.io,s=t.row,r=t.col;this._print(Array(t.width-t.col+1).join(" ")),this.set_cursor(s,r)}},erase_window:function(e){1>e&&this.Glk.glk_window_clear(this.mainwin),(1===e||-2===e)&&this.upperwin&&(this.Glk.glk_window_clear(this.upperwin),this.set_cursor(0,0)),-1===e&&this.split_window(0)},fileref_create_by_prompt:function(e){"undefined"==typeof e.run&&(e.run=1),this.fileref_data=e,this.glk_blocking_call="fileref_create_by_prompt",this.Glk.glk_fileref_create_by_prompt(e.usage,e.mode,e.rock||0)},fix_upper_window:function(){var e=this.Glk,t=this.io;t.seenheight>=t.maxheight&&(t.maxheight=t.height),this.upperwin&&(0===t.maxheight?(e.glk_window_close(this.upperwin),this.upperwin=null):e.glk_window_set_arrangement(e.glk_window_get_parent(this.upperwin),18,t.maxheight,null)),t.seenheight=t.maxheight,t.maxheight=t.height},format:function(){this.Glk.glk_set_style(l[!!this.io.mono|this.io.italic|this.io.bold]),this.Glk.glk_gestalt(4352,0)&&this.Glk.garglk_set_reversevideo(this.io.reverse)},get_cursor:function(e){this.ram.setUint16(e,this.io.row+1),this.ram.setUint16(e+2,this.io.col+1)},handle_char_input:function(e){var t=this.io.streams[4],s=o[e]||this.reverse_unicode_table[e]||63;this.variable(this.read_data.storer,s),1===t.mode&&(t.cache+=s),2===t.mode&&this.Glk.glk_put_char_stream_uni(t.str,s)},handle_create_fileref:function(e){var t,s=this.Glk,r=this.fileref_data;return e&&(t=r.unicode?s.glk_stream_open_file_uni(e,r.mode,r.rock||0):s.glk_stream_open_file(e,r.mode,r.rock||0),s.glk_fileref_destroy(e)),("restore"===r.func||"save"===r.func)&&this.save_restore_handler(t),"input_stream"===r.func&&(this.io.streams[0]=t),"output_stream"===r.func&&this.output_stream_handler(t),r.run},handle_line_input:function(e,s){var r=this.ram,i=this.read_data,n=this.io.streams,a=t.apply(null,i.buffer.slice(0,e))+"\n",o=this.text_to_zscii(a.slice(0,-1).toLowerCase());1===n[2].mode&&(n[2].cache+=a),2===n[2].mode&&this.Glk.glk_put_jstring_stream(n[2].str,a),1===n[4].mode&&(n[4].cache+=a),2===n[4].mode&&this.Glk.glk_put_jstring_stream(n[4].str,a),5>this.version?(o.push(0),r.setUint8Array(i.bufaddr+1,o)):(r.setUint8(i.bufaddr+1,e),r.setUint8Array(i.bufaddr+2,o),this.variable(i.storer,isNaN(s)?13:s)),i.parseaddr&&this.tokenise(i.bufaddr,i.parseaddr)},input_stream:function(e){var t=this.io;e&&!t.streams[0]&&this.fileref_create_by_prompt({func:"input_stream",mode:2,rock:212,unicode:1,usage:259}),!e&&t.streams[0]&&(this.Glk.glk_stream_close(t.streams[0]),t.streams[0]=0)},open_windows:function(){if(!this.mainwin){const e=this.Glk,t=[1,2,4,5,6,9,10];for(let s=0;7>s;s++)e.glk_stylehint_set(0,t[s],3,0),e.glk_stylehint_set(0,t[s],4,0),e.glk_stylehint_set(0,t[s],5,0),e.glk_stylehint_set(0,t[s],6,1);e.glk_stylehint_set(0,4,4,1),e.glk_stylehint_set(0,1,5,1),e.glk_stylehint_set(0,5,4,1),e.glk_stylehint_set(0,5,5,1),e.glk_stylehint_set(0,2,6,0),e.glk_stylehint_set(0,9,4,1),e.glk_stylehint_set(0,9,6,0),e.glk_stylehint_set(0,10,5,1),e.glk_stylehint_set(0,10,6,0),e.glk_stylehint_set(0,6,4,1),e.glk_stylehint_set(0,6,5,1),e.glk_stylehint_set(0,6,6,0),this.mainwin=e.glk_window_open(0,0,0,3,201),e.glk_set_window(this.mainwin),this.version3&&(this.statuswin=e.glk_window_open(this.mainwin,18,1,4,202),this.statuswin&&e.garglk_set_reversevideo_stream(e.glk_window_get_stream(this.statuswin),1))}},output_stream:function(e,t,s){var r,i,n=this.ram,o=this.io.streams;e=a(e),1===e&&(o[1]=1),-1===e&&(o[1]=0),2!==e||o[2].mode||(this.fileref_create_by_prompt({func:"output_stream",mode:5,rock:210,run:!s,str:2,unicode:1,usage:258}),o[2].cache="",o[2].mode=1,!s&&(this.stop=1)),-2===e&&(n.setUint8(17,254&n.getUint8(17)),2===o[2].mode&&this.Glk.glk_stream_close(o[2].str),o[2].mode=this.io.transcript=0),3===e&&o[3].unshift([t,""]),-3===e&&(r=o[3].shift(),i=this.text_to_zscii(r[1]),n.setUint16(r[0],i.length),n.setUint8Array(r[0]+2,i)),4!==e||o[4].mode||(this.fileref_create_by_prompt({func:"output_stream",mode:5,rock:211,str:4,unicode:1,usage:259}),o[4].cache="",o[4].mode=1,this.stop=1),-4===e&&(2===o[4].mode&&this.Glk.glk_stream_close(o[4].str),o[4].mode=0)},output_stream_handler:function(e){var t=this.ram,s=this.io.streams,r=this.fileref_data;2===r.str&&(t.setUint8(17,254&t.getUint8(17)|(e?1:0)),e?(s[2].mode=2,s[2].str=e,this.io.transcript=1,s[2].cache&&this.Glk.glk_put_jstring_stream(s[2].str,s[2].cache)):s[2].mode=this.io.transcript=0),4===r.str&&(e?(s[4].mode=2,s[4].str=e,s[4].cache&&this.Glk.glk_put_jstring_stream(s[4].str,s[4].cache)):s[4].mode=0)},_print:function(e){var t=this.Glk,s=this.io,r=0;if(s.streams[3].length)s.streams[3][0][1]+=e;else if(e=e.replace(/\r/g,"\n"),(1&this.m.getUint8(17))!==s.transcript&&this.output_stream(s.transcript?-2:2,0,1),(2&this.m.getUint8(17))!=(2&s.mono)&&(s.mono^=2,this.format()),s.currentwin&&this.upperwin)for(;r<e.length&&s.row<s.height;)t.glk_put_jstring(e[r++]),s.col++,s.col===s.width&&(s.col=0,s.row++);else s.currentwin||(s.streams[1]&&t.glk_put_jstring(e),1===s.streams[2].mode&&(s.streams[2].cache+=e),2===s.streams[2].mode&&t.glk_put_jstring_stream(s.streams[2].str,e))},print:function(e,s){var r,i;if(0===e&&(i=s),1===e&&(i=t(s)),2===e&&(i=this.jit[s]||this.decode(s)),3===e&&(r=this.m.getUint16(this.objects+(this.version3?9:14)*s+(this.version3?7:12)),i=this.decode(r+1,2*this.m.getUint8(r))),4===e){if(!this.unicode_table[s])return;i=this.unicode_table[s]}this._print(""+i)},print_table:function(e,t,s,r){s=s||1,r=r||0;for(var n=0;n++<s;)this._print(this.zscii_to_text(this.m.getUint8Array(e,t))+(n<s?"\r":"")),e+=t+r},read:function(e,s,r,i,n){var a,o,l=this.m.getUint8(s);if(this.version3&&(l++,this.v3_status()),a=Array(l),a.fill(0),this.read_data={buffer:a,bufaddr:s,parseaddr:r,routine:n,storer:e,time:i},this.io.streams[0]){if(o=this.Glk.glk_get_line_stream_uni(this.io.streams[0],a),10===a[o-1]&&o--,o)return this._print(t.apply(null,a.slice(0,o))+"\n"),this.handle_line_input(o),this.stop=0;this.input_stream(0)}this.Glk.glk_request_line_event_uni(this.io.currentwin?this.upperwin:this.mainwin,a,0),this.fix_upper_window()},read_char:function(e,t,s,r){if(this.io.streams[0]){var i=this.Glk.glk_get_char_stream_uni(this.io.streams[0]);if(-1===i)this.input_stream(0);else return this.variable(e,i),this.stop=0}this.read_data={routine:r,storer:e,time:s},this.Glk.glk_request_char_event_uni(this.io.currentwin?this.upperwin:this.mainwin),this.fix_upper_window()},set_colour:function(e,t){this.set_true_colour(p[e],p[t])},set_cursor:function(e,t){var s=this.io;s.currentwin&&(e>=s.height&&this.split_window(e+1),this.upperwin&&0<=e&&0<=t&&t<s.width&&(this.Glk.glk_window_move_cursor(this.upperwin,t,e),s.row=e,s.col=t))},set_font:function(e){var t=4&this.io.mono?4:1;return 0===e?t:1!==e&&4!==e?0:(e!==t&&(this.io.mono^=4,this.format()),t)},set_style:function(e){var t=this.io;0===e&&(t.reverse=t.bold=t.italic=0,t.mono&=254),1&e&&(t.reverse=1),2&e&&(t.bold=4),4&e&&(t.italic=2),8&e&&(t.mono|=1),this.format()},set_true_colour:function(e,t){const s=this.Glk;if(s.glk_gestalt(4352,0)){let r,n;65534===e?r=-2:65535===e?r=-1:(r=i(e),s.glk_stylehint_set(0,0,7,r)),65534===t?n=-2:65535===t?n=-1:(n=i(t),s.glk_stylehint_set(0,0,8,n)),s.garglk_set_zcolors_stream(this.mainwin.str,r,n),this.upperwin&&s.garglk_set_zcolors_stream(this.upperwin.str,r,n),this.statuswin&&s.garglk_set_zcolors_stream(this.statuswin.str,r,n)}},set_window:function(e){this.io.currentwin=e,e&&this.set_cursor(0,0),this.Glk.glk_set_window(this.upperwin&&e?this.upperwin:this.mainwin),this.format()},split_window:function(e){var t,s=this.Glk,r=this.io,i=r.row,n=r.col,a=r.height;if(r.height=e,this.upperwin&&e>a){for(t=s.glk_window_get_stream(this.upperwin);a<e;)s.glk_window_move_cursor(this.upperwin,0,a++),s.glk_put_jstring_stream(t,Array(r.width+1).join(" "));s.glk_window_move_cursor(this.upperwin,n,i)}e>r.maxheight&&(r.maxheight=e,this.upperwin?s.glk_window_set_arrangement(s.glk_window_get_parent(this.upperwin),18,r.maxheight,null):this.upperwin=s.glk_window_open(this.mainwin,18,r.maxheight,4,203)),e&&(r.row>=e&&this.set_cursor(0,0),this.version3&&s.glk_window_clear(this.upperwin))},update_header:function(){var e=this.ram;return this.xorshift_seed=0,this.update_screen_size(),this.version3?e.setUint8(1,64|(143&e.getUint8(1)|(this.statuswin?32:16))):void(e.setUint8(1,0|(28|(this.Glk.glk_gestalt(4352,0)?1:0))),e.setUint8(17,87&e.getUint8(17)),4<this.version&&e.setUint16(38,257),e.setUint16(50,258),this.extension_table(4,0))},update_screen_size:function(){const t=this.Glk,s=new t.RefBox,r=new t.RefBox,i=t.glk_window_open(this.mainwin,18,0,4,0);let n=0,a=0;t.glk_window_get_size(this.mainwin,r,s),n=s.get_value(),this.upperwin&&(t.glk_window_get_size(this.upperwin,r,s),n+=s.get_value()),this.statuswin&&(t.glk_window_get_size(this.statuswin,r,s),n+=s.get_value()),i&&(t.glk_window_get_size(i,r,0),t.glk_window_close(i)),a=r.get_value(),n=e(n,254),a=this.io.width=e(a,255),3<this.version&&(this.ram.setUint8(32,n),this.ram.setUint8(33,a)),4<this.version&&(this.ram.setUint16(34,a),this.ram.setUint16(36,n)),this.io.col>=a&&(this.io.col=a-1)},v3_status:function(){if(this.statuswin){var e,t=this.Glk,s=t.glk_window_get_stream(this.statuswin),r=this.m,i=this.io.width,n=r.getUint16(this.globals+2),a=r.getUint16(this.globals+4),o=r.getUint16(this.objects+9*r.getUint16(this.globals)+7),l=""+this.decode(o+1,2*r.getUint8(o));e=2&r.getUint8(1)?"Time: "+(0==n%12?12:n%12)+":"+(10>a?"0":"")+a+" "+(11<n?"PM":"AM"):"Score: "+n+" Turns: "+a,t.glk_window_move_cursor(this.statuswin,0,0),t.glk_put_jstring_stream(s,Array(i+1).join(" ")),t.glk_window_move_cursor(this.statuswin,0,0),t.glk_put_jstring_stream(s," "+l.slice(0,i-e.length-4)),t.glk_window_move_cursor(this.statuswin,i-e.length-1,0),t.glk_put_jstring_stream(s,e)}}}},{"../common/utils.js":3}],7:[function(e,t){"use strict";var s=e("../common/ast.js"),r=s.Variable,i=s.Opcode,n=s.Stopper,a=s.Pauser,o=s.PauserStorer,l=s.Brancher,p=s.BrancherStorer,_=s.Storer,g=s.Caller,u=s.CallerStorer,c=s.opcode_builder,d=function(e){return""+e},m=new r(this.e,0),f=c(l,function(){return 1}),h=c(_,function(e){return"e.S2U(~"+e+")"}),U=_.subClass({storer:0,post:function(){var e=this.operands,t=e[0],s=t instanceof r;e[0]=new r(this.e,s?t:t.v),(s||0===t.v)&&(e[0].indirect=1),this.storer=142===this.code?e.pop():e.shift(),0===e.length&&e.push(m)},func:d}),k=i.subClass({func:function(e){var t=e.v-1,s=this.code%2?1:-1;return e instanceof r||14<t?"e.incdec("+e+","+s+")":(0>t?"e.s[e.sp-1]":"e.l["+t+"]")+(1==s?"++":"--")}}),b=n.subClass({brancher:1,toString:function(){return"e.stop=1;e."+(181===this.code?"save":"restore")+"("+(this.pc+1)+")"}}),w=c(o,function(){return"e.restore("+(this.next-1)+")"}),v=c(o,function(){return"e.save("+(this.next-1)+")"});t.exports=function(e){return{1:c(l,function(){return 2===arguments.length?this.args("==="):"e.jeq("+this.args()+")"}),2:c(l,function(e,t){return e.U2S()+"<"+t.U2S()}),3:c(l,function(e,t){return e.U2S()+">"+t.U2S()}),4:c(l,function(e,t){return"e.U2S(e.incdec("+e+",-1))<"+t.U2S()}),5:c(l,function(e,t){return"e.U2S(e.incdec("+e+",1))>"+t.U2S()}),6:c(l,function(){return"e.jin("+this.args()+")"}),7:c(l,function(){return"e.test("+this.args()+")"}),8:c(_,function(){return this.args("|")}),9:c(_,function(){return this.args("&")}),10:c(l,function(){return"e.test_attr("+this.args()+")"}),11:c(i,function(){return"e.set_attr("+this.args()+")"}),12:c(i,function(){return"e.clear_attr("+this.args()+")"}),13:U,14:c(i,function(){return"e.insert_obj("+this.args()+")"}),15:c(_,function(e,t){return"e.m.getUint16(e.S2U("+e+"+2*"+t.U2S()+"))"}),16:c(_,function(e,t){return"e.m.getUint8(e.S2U("+e+"+"+t.U2S()+"))"}),17:c(_,function(){return"e.get_prop("+this.args()+")"}),18:c(_,function(){return"e.find_prop("+this.args()+")"}),19:c(_,function(){return"e.find_prop("+this.args(",0,")+")"}),20:c(_,function(){return"e.S2U("+this.args("+")+")"}),21:c(_,function(){return"e.S2U("+this.args("-")+")"}),22:c(_,function(){return"e.S2U("+this.args("*")+")"}),23:c(_,function(e,t){return"e.S2U(parseInt("+e.U2S()+"/"+t.U2S()+"))"}),24:c(_,function(e,t){return"e.S2U("+e.U2S()+"%"+t.U2S()+")"}),25:u,26:g,27:c(i,function(){return"e.set_colour("+this.args()+")"}),28:c(n,function(e,t){return"while(e.frames.length+1>"+t+"){e.frameptr=e.frames.pop()}return "+e}),128:c(l,function(e){return e+"===0"}),129:c(p,function(e){return"e.get_sibling("+e+")"}),130:c(p,function(e){return"e.get_child("+e+")"}),131:c(_,function(e){return"e.get_parent("+e+")"}),132:c(_,function(e){return"e.get_prop_len("+e+")"}),133:k,134:k,135:c(i,function(e){return"e.print(2,"+e+")"}),136:u,137:c(i,function(e){return"e.remove_obj("+e+")"}),138:c(i,function(e){return"e.print(3,"+e+")"}),139:c(n,function(e){return"return "+e}),140:c(n,function(e){return"e.pc="+e.U2S()+"+"+(this.next-2)}),141:c(i,function(e){return"e.print(2,"+e+"*"+this.e.addr_multipler+")"}),142:U.subClass({storer:1}),143:5>e?h:g,176:c(n,function(){return"return 1"}),177:c(n,function(){return"return 0"}),178:c(i,function(e){return"e.print(2,"+e+")"},{printer:1}),179:c(n,function(e){return"e.print(2,"+e+");e.print(1,13);return 1"},{printer:1}),180:i,181:4>e?b:v,182:4>e?b:w,183:c(n,function(){return"e.erase_window(-1);e.restart()"}),184:c(n,function(e){return"return "+e},{post:function(){this.operands.push(m)}}),185:5>e?c(i,function(){return"s[--e.sp]"}):c(_,function(){return"e.frames.length+1"}),186:c(a,function(){return"e.quit=1;e.Glk.glk_exit()"}),187:c(i,function(){return"e.print(1,13)"}),188:4>e?c(n,function(){return"e.pc="+this.next+";e.v3_status()"}):i,189:f,191:f,224:u,225:c(i,function(e,t,s){return"e.ram.setUint16(e.S2U("+e+"+2*"+t.U2S()+"),"+s+")"}),226:c(i,function(e,t,s){return"e.ram.setUint8(e.S2U("+e+"+"+t.U2S()+"),"+s+")"}),227:c(i,function(){return"e.put_prop("+this.args()+")"}),228:5>e?c(a,function(){return"e.read(0,"+this.args()+")"}):c(o,function(){return"e.read("+this.storer.v+","+this.args()+")"}),229:c(i,function(e){return"e.print(4,"+e+")"}),230:c(i,function(e){return"e.print(0,"+e.U2S()+")"}),231:c(_,function(e){return"e.random("+e.U2S()+")"}),232:c(_,d,{post:function(){this.storer=m},storer:0}),233:U,234:c(i,function(e){return"e.split_window("+e+")"}),235:c(i,function(e){return"e.set_window("+e+")"}),236:u,237:c(i,function(e){return"e.erase_window("+e.U2S()+")"}),238:c(i,function(e){return"e.erase_line("+e+")"}),239:c(i,function(e,t){return"e.set_cursor("+e+"-1,"+t+"-1)"}),240:c(i,function(e){return"e.get_cursor("+e+")"}),241:c(i,function(e){return"e.set_style("+e+")"}),242:i,243:c(n,function(){return"e.pc="+this.next+";e.output_stream("+this.args()+")"}),244:c(a,function(){return"e.input_stream("+this.args()+")"}),245:i,246:c(o,function(){return"e.read_char("+this.storer.v+","+(this.args()||"1")+")"}),247:c(p,function(){return"e.scan_table("+this.args()+")"}),248:h,249:g,250:g,251:c(i,function(){return"e.tokenise("+this.args()+")"}),252:c(i,function(){return"e.encode_text("+this.args()+")"}),253:c(i,function(){return"e.copy_table("+this.args()+")"}),254:c(i,function(){return"e.print_table("+this.args()+")"}),255:c(l,function(e){return"e.stack.getUint8(e.frameptr+5)&(1<<("+e+"-1))"}),1e3:v,1001:w,1002:c(_,function(e,t){return"e.S2U(e.log_shift("+e+","+t.U2S()+"))"}),1003:c(_,function(e,t){return"e.S2U(e.art_shift("+e.U2S()+","+t.U2S()+"))"}),1004:c(_,function(e){return"e.set_font("+e+")"}),1009:c(_,function(){return"e.save_undo("+this.next+","+this.storer.v+")"}),1010:c(i,function(){return"if(e.restore_undo())return"},{storer:1}),1011:c(i,function(e){return"e.print(1,"+e+")"}),1012:c(_,function(){return 3}),1013:c(i,function(){return"e.set_true_colour("+this.args()+")"}),1014:i.subClass({brancher:1}),1030:c(_,function(){return"e.gestalt("+this.args()+")"})}}},{"../common/ast.js":1}],8:[function(e,t){"use strict";function s(e){const t=(e)=>"object"==typeof e?s(e):e,r={};if(Array.isArray(e))return e.map(t);for(let s in e)"buffer"!=s&&"str"!=s&&(r[s]=t(e[s]));return r}function r(e,t,s,r){if(p&&!r)for(;t<s;)e.setUint16(t,e.getUint16(t,1)),t+=2}const n=e("../common/file.js"),a=e("../common/utils.js"),i=a.extend,o=a.U2S16,l=a.S2U16,p=function(){var e=new Uint8Array(2),t=new Uint16Array(e.buffer);return t[0]=1,1===e[0]}();t.exports={art_shift:function(e,t){return 0<t?e<<t:e>>-t},call:function(e,t,s,r){if(0===e)return 0<=t&&this.variable(t,0),this.pc=s;this.pc=e*this.addr_multipler;var n=this.m.getUint8(this.pc++),a=this.stack,o=0,i=this.frameptr;for(a.setUint16(i+6,this.sp),this.frames.push(i),i=this.frameptr=this.s.byteOffset+2*this.sp,a.setUint32(i,s<<8),a.setUint8(i+3,(0<=t?0:16)|n),a.setUint8(i+4,0<=t?t:0),a.setUint8(i+5,(1<<r.length)-1),this.make_stacks(),this.sp=0;o<n;)this.l[o]=o<r.length?r[o]:5>this.version?this.m.getUint16(this.pc+2*o):0,o++;5>this.version&&(this.pc+=2*n)},clear_attr:function(e,t){var s=0|this.objects+(this.version3?9:14)*e+t/8;this.ram.setUint8(s,this.m.getUint8(s)&~(128>>t%8))},copy_table:function(e,t,s){s=o(s);var r=this.ram,n=0,i=0>s;if(s=Math.abs(s),0===t){for(;n<s;)r.setUint8(e+n++,0);return}if(i)for(;n<s;)r.setUint8(t+n,this.m.getUint8(e+n++));else r.setUint8Array(t,this.m.getUint8Array(e,s))},do_autorestore:function(e){const t=this.Glk;t.restore_allstate(e.glk),this.io=e.io;const s=new t.RefBox;let r;for(;r=t.glk_window_iterate(r,s);)201===s.value&&(this.mainwin=r,r.linebuf&&(e.read_data.buffer=r.linebuf)),202===s.value&&(this.statuswin=r),203===s.value&&(this.upperwin=r);for(r=null;r=t.glk_stream_iterate(r,s);)210===s.value&&(this.io.streams[2].str=r),211===s.value&&(this.io.streams[4].str=r);this.restart(),this.restore_file(new Uint8Array(e.ram),1),this.read_data=e.read_data,this.xorshift_seed=e.xorshift_seed},do_autosave:function(e){if(!this.options.Dialog)throw new Error("A reference to Dialog is required");let t=null;0<=(e||0)&&(t={glk:this.Glk.save_allstate(),io:s(this.io),ram:this.save_file(this.pc,1),read_data:s(this.read_data),xorshift_seed:this.xorshift_seed}),this.options.Dialog.autosave_write(this.signature,t)},encode_text:function(e,t,s,r){this.ram.setUint8Array(r,this.encode(this.m.getUint8Array(e+s,t)))},extension_table:function(e,t){var s=this.extension;return!s||e>this.extension_count?0:(s+=2*e,void 0===t?this.m.getUint16(s):void this.ram.setUint16(s,t))},find_prop:function(e,t,s){var r,i,n=this.m,a=this.version3,o=0,l=n.getUint16(this.objects+(a?9:14)*e+(a?7:12));for(l+=2*n.getUint8(l)+1;;){if(r=n.getUint8(l),i=r&(a?31:63),o===s)return i;if(i===t)return l+(!a&&128&r?2:1);if(i<t)return 0;o=i,a?l+=(r>>5)+2:128&r?(i=63&n.getUint8(l+1),l+=i?i+2:66):l+=64&r?3:2}},gestalt:function(e){return 1===e?258:0},get_child:function(e){return this.version3?this.m.getUint8(this.objects+9*e+6):this.m.getUint16(this.objects+14*e+10)},get_sibling:function(e){return this.version3?this.m.getUint8(this.objects+9*e+5):this.m.getUint16(this.objects+14*e+8)},get_parent:function(e){return this.version3?this.m.getUint8(this.objects+9*e+4):this.m.getUint16(this.objects+14*e+6)},get_prop:function(e,t){var s,r=this.m,i=this.find_prop(e,t);return i?(s=r.getUint8(i-1),r[(this.version3?s>>5:64&s)?"getUint16":"getUint8"](i)):r.getUint16(this.properties+2*(t-1))},get_prop_len:function(e){if(0===e)return 0;var t=this.m.getUint8(e-1);return this.version3?(t>>5)+1:128&t?(t&=63,0===t?64:t):64&t?2:1},incdec:function(e,t){if(0===e)return this.s[this.sp-1]+=t,this.s[this.sp-1];if(15>--e)return this.l[e]+=t,this.l[e];var s=this.globals+2*(e-15);return this.ram.setUint16(s,this.m.getUint16(s)+t),this.ram.getUint16(s)},indirect:function(e,t){return 0===e?1<arguments.length?this.s[this.sp-1]=t:this.s[this.sp-1]:this.variable(e,t)},insert_obj:function(e,t){this.remove_obj(e),this.set_family(e,t,t,e,e,this.get_child(t))},jeq:function(){for(var e=1;e<arguments.length;)if(arguments[e++]===arguments[0])return 1},jin:function(e,t){return this.get_parent(e)===t},log:function(e){this.options.GlkOte&&this.options.GlkOte.log(e)},log_shift:function(e,t){return 0<t?e<<t:e>>>-t},make_stacks:function(){var e=15&this.stack.getUint8(this.frameptr+3);this.l=new Uint16Array(this.stack.buffer,this.frameptr+8,e),this.s=new Uint16Array(this.stack.buffer,this.frameptr+8+2*e)},put_prop:function(e,t,s){var r,i=this.find_prop(e,t);i&&(r=this.m.getUint8(i-1),this.ram[(this.version3?r>>5:64&r)?"setUint16":"setUint8"](i,s))},random:function(e){var t=this.xorshift_seed;return 1>e?(this.xorshift_seed=e,0):0===t?0|1+Math.random()*e:(t^=t<<13,t^=t>>17,this.xorshift_seed=t^=t<<5,1+(32767&t)%e)},remove_obj:function(e){var t,s,r,i=this.get_parent(e);if(0!==i)if(t=this.get_child(i),s=this.get_sibling(e),t===e)this.set_family(e,0,i,s);else{for(;r=this.get_sibling(t),r!==e;)t=r;this.set_family(e,0,0,0,t,s)}},restart:function(){var t=this.ram,s=t.getUint8(0),r=3===s,n=r?2:8===s?8:4,o=t.getUint8(17),l=t.getUint16(10),p=4<s?t.getUint16(54):0,_=a.MemoryView(this.options.stack_len);t.setUint8Array(0,this.origram),t.setUint8(17,o),i(this,{stack:_,frameptr:0,frames:[],s:new Uint16Array(_.buffer,8),sp:0,l:[],undo:[],undo_len:0,glk_blocking_call:null,version:s,version3:r,pc:t.getUint16(6),properties:l,objects:l+(r?53:112),globals:t.getUint16(12),eof:(t.getUint16(26)||65536)*n,extension:p,extension_count:p?t.getUint16(p):0,addr_multipler:n,opcodes:e("./opcodes.js")(s)}),this.init_text(),this.init_io(),this.update_header()},restore:function(e){this.pc=e,this.fileref_create_by_prompt({func:"restore",mode:2,usage:1})},restore_file:function(e,t){var s,a=this.ram,o=new n.Quetzal(e),l=o.memory,p=this.stack,_=a.getUint8(17),g=0,i=0;if(a.getUint16(2)!==o.release||a.getUint16(28)!==o.checksum)return 0;for(;6>g;)if(a.getUint8(18+g)!==o.serial[g++])return 0;if(g=0,a.setUint8Array(0,this.origram),o.compressed)for(;g<l.length;)s=l[g++],0===s?i+=1+l[g++]:a.setUint8(i,s^this.origram[i++]);else a.setUint8Array(0,l);for(a.setUint8(17,_),p.setUint8Array(0,o.stacks),this.frames=[],g=0;g<o.stacks.byteLength;)this.frameptr=g,this.frames.push(g),r(p,i=g+8,i+=2*(15&p.getUint8(g+3)),t),r(p,i,i+=2*p.getUint16(g+6),t),g=i;return this.frames.pop(),this.sp=p.getUint16(this.frameptr+6),this.make_stacks(),this.pc=o.pc,this.update_header(),this.version3&&this.split_window(0),2},restore_undo:function(){if(0===this.undo.length)return 0;var e=this.undo.pop();return this.frameptr=e.frameptr,this.pc=e.pc,this.undo_len-=e.ram.byteLength+e.stack.byteLength,e.ram[17]=this.m.getUint8(17),this.ram.setUint8Array(0,e.ram),this.frames=e.frames,this.sp=e.sp,this.stack.setUint8Array(0,e.stack),this.make_stacks(),this.variable(e.var,2),1},ret:function(e){var t=this.stack,s=this.frameptr,r=16&t.getUint8(s+3)?-1:t.getUint8(s+4);this.pc=t.getUint32(s)>>8,s=this.frameptr=this.frames.pop(),this.make_stacks(),this.sp=t.getUint16(s+6),0<=r&&this.variable(r,e||0)},save:function(e){this.pc=e,this.fileref_create_by_prompt({func:"save",mode:1,usage:1})},save_file:function(e,t){var s,i,o,l=this.m,_=new n.Quetzal,g=a.MemoryView(this.stack.buffer.slice()),u=0,c=this.frameptr;if(_.release=l.getUint16(2),_.serial=l.getUint8Array(18,6),_.checksum=l.getUint16(28),_.pc=e,t)_.memory=this.m.getUint8Array(0,this.staticmem);else{const e=[];for(_.compressed=1,s=0;s<this.staticmem;s++)o=l.getUint8(s)^this.origram[s],0===o?256==++u&&(e.push(0,255),u=0):(u&&(e.push(0,u-1),u=0),e.push(o));_.memory=e}if(g.setUint16(c+6,this.sp),p&&!t){const e=this.frames.slice();for(e.push(c),s=0;s<e.length;s++)c=e[s],r(g,i=c+8,i+=2*(15&g.getUint8(c+3))),r(g,i,i+=2*g.getUint16(c+6))}return _.stacks=g.getUint8Array(0,this.frameptr+8+2*(15&g.getUint8(c+3))+2*this.sp),_.write()},save_restore_handler:function(e){var t,s,r,i=this.m,n=this.Glk,a=0,o=[];e&&("save"===this.fileref_data.func?(n.glk_put_buffer_stream(e,new Uint8Array(this.save_file(this.pc))),a=1):(o=new Uint8Array(131072),n.glk_get_buffer_stream(e,o),a=this.restore_file(o.buffer)),n.glk_stream_close(e)),this.version3?(t=i.getUint8(this.pc++),s=128&t,r=64&t?63&t:(t<<8|i.getUint8(this.pc++))<<18>>18,!a==!s&&(0===r||1===r?this.ret(r):this.pc+=r-2)):this.variable(i.getUint8(this.pc++),a)},save_undo:function(e,t){var s;return this.undo_len>this.options.undo_len&&(s=this.undo.shift(),this.undo_len-=s.ram.byteLength+s.stack.byteLength),s={frameptr:this.frameptr,frames:this.frames.slice(),pc:e,ram:this.m.getUint8Array(0,this.staticmem),sp:this.sp,stack:this.stack.getUint8Array(0,this.s.byteOffset+2*this.sp),var:t},this.undo_len+=s.ram.byteLength+s.stack.byteLength,this.undo.push(s),1},scan_table:function(e,t,s,r){r=r||130;var i=128&r?"getUint16":"getUint8";for(r&=127,s=t+s*r;t<s;){if(this.m[i](t)===e)return t;t+=r}return 0},set_attr:function(e,t){var s=0|this.objects+(this.version3?9:14)*e+t/8;this.ram.setUint8(s,this.m.getUint8(s)|128>>t%8)},set_family:function(e,t,s,r,i,n){var a=this.ram,o=this.objects;this.version3?(a.setUint8(o+9*e+4,t),s&&a.setUint8(o+9*s+6,r),i&&a.setUint8(o+9*i+5,n)):(a.setUint16(o+14*e+6,t),s&&a.setUint16(o+14*s+10,r),i&&a.setUint16(o+14*i+8,n))},test:function(e,t){return(e&t)===t},test_attr:function(e,t){return 128&this.m.getUint8(0|this.objects+(this.version3?9:14)*e+t/8)<<t%8},variable:function(e,t){var s,r=t!==void 0;if(0===e){if(r)this.s[this.sp++]=t;else return this.s[--this.sp];}else if(15>--e){if(r)this.l[e]=t;else return this.l[e];}else if(s=this.globals+2*(e-15),r)this.ram.setUint16(s,t);else return this.m.getUint16(s);return t},U2S:o,S2U:l}},{"../common/file.js":2,"../common/utils.js":3,"./opcodes.js":7}],9:[function(s,r){r.exports={init_text:function(){var e=this,s=this.m,r=4<this.version&&s.getUint16(52),i=this.extension_table(3),n=i&&s.getUint8(i++);this.abbr_addr=s.getUint16(24),function(t){for(var s=[[],[],[]],r=0;78>r;)s[0|r/26][r%26]=t[r++];s[2][1]=13,e.alphabets=s}(r?s.getUint8Array(r,78):this.text_to_zscii("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \r0123456789.,!?_#'\"/\\-:()",1)),function(s){for(var r={13:"\r"},n={13:13},a=0;a<s.length;)r[155+a]=t(s[a]),n[s[a]]=155+a++;for(a=32;127>a;)r[a]=t(a),n[a]=a++;e.unicode_table=r,e.reverse_unicode_table=n}(i?s.getUint16Array(i,n):this.text_to_zscii(unescape("%E4%F6%FC%C4%D6%DC%DF%BB%AB%EB%EF%FF%CB%CF%E1%E9%ED%F3%FA%FD%C1%C9%CD%D3%DA%DD%E0%E8%EC%F2%F9%C0%C8%CC%D2%D9%E2%EA%EE%F4%FB%C2%CA%CE%D4%DB%E5%C5%F8%D8%E3%F1%F5%C3%D1%D5%E6%C6%E7%C7%FE%F0%DE%D0%A3%u0153%u0152%A1%BF"),1)),this.dictionaries={},this.dict=s.getUint16(8),this.parse_dict(this.dict)},decode:function(e,t){var s,r,n,a=this.m,o=e,l=[],p=0,i=0,_=[],g=[],u=0;if(this.jit[e])return this.jit[e];for(t=t?t+e:this.eof;e<t&&(s=a.getUint16(e),e+=2,l.push(31&s>>10,31&s>>5,31&s),!(32768&s)););for(;p<l.length;)r=l[p++],0===r?_.push(32):4>r?(n=1,_.push(-1),g.push("\uE000+this.abbr("+(32*(r-1)+l[p++])+")+\uE000")):6>r?i=r:2==i&&6===r?p+1<l.length&&_.push(l[p++]<<5|l[p++]):32>r&&_.push(this.alphabets[i][r-6]),i=4>i?0:i-3,0==p%3&&(p+=u,u=0);return _=this.zscii_to_text(_,g),n&&(_={toString:Function("return\""+_.replace(/\\/g,"\\\\").replace(/"/g,"\\\"").replace(/\r/g,"\\r").replace(/\uE000/g,"\"")+"\"").bind(this)}),o>=this.staticmem&&(this.jit[o]=_),_},encode:function(e){for(var t,s,r=this.alphabets,n=[],a=this.version3?6:9,o=0,i=[];n.length<a;)t=e[o++],32===t?n.push(0):0<=(s=r[0].indexOf(t))?n.push(s+6):0<=(s=r[1].indexOf(t))?n.push(4,s+6):0<=(s=r[2].indexOf(t))?n.push(5,s+6):void 0===t?n.push(5):n.push(5,6,t>>5,31&t);for(n.length=a,o=0;o<a;)i.push(n[o++]<<2|n[o]>>3,(7&n[o++])<<5|n[o++]);return i[i.length-2]|=128,i},zscii_to_text:function(e,t){for(var s,r=0,i=e.length,n=0,a="";r<i;)s=e[r++],-1===s&&(a+=t[n++]),(s=this.unicode_table[s])&&(a+=s);return a},text_to_zscii:function(e,t){for(var s,r=[],n=0,i=e.length;n<i;)s=e.charCodeAt(n++),t||(s=this.reverse_unicode_table[s]||63),r.push(s);return r},parse_dict:function(e){var t,s,r=this.m,i=e,n={},a=r.getUint8(e++);for(n.separators=Array.prototype.slice.call(r.getUint8Array(e,a)),e+=a,t=r.getUint8(e++),s=e+2+t*r.getUint16(e),e+=2;e<s;)n[Array.prototype.toString.call(r.getUint8Array(e,this.version3?4:6))]=e,e+=t;return this.dictionaries[i]=n,n},abbr:function(e){return this.decode(2*this.m.getUint16(this.abbr_addr+2*e))},tokenise:function(t,s,r,n){r=r||this.dict,r=this.dictionaries[r]||this.parse_dict(r);var a,o,l,p,_=this.m,g=this.ram,u=1e3,c=1,i=r.separators,d=[],m=0;for(4<this.version&&(u=_.getUint8(t+c++)+2);c<u&&(a=_.getUint8(t+c),0!==a);)32===a||0<=i.indexOf(a)?(32!==a&&d.push([[a],c]),o=null):(o||(d.push([[],c]),o=d[d.length-1][0]),o.push(a)),c++;for(l=e(d.length,_.getUint8(s));m<l;)p=r[""+this.encode(d[m][0])],(!n||p)&&(g.setUint16(s+2+4*m,p||0),g.setUint8(s+4+4*m,d[m][0].length),g.setUint8(s+5+4*m,d[m][1])),m++;g.setUint8(s+1,m)}}},{}]},{},[4])(4)});