UNPKG

ifvms

Version:

The Interactive Fiction Virtual Machines Suite - in Javascript

4 lines (2 loc) 42 kB
/* ZVM v1.1.6 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 p(_,e,t){function r(i,n){if(!e[i]){if(!_[i]){var o="function"==typeof require&&require;if(!n&&o)return o(i,!0);if(s)return s(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var g=e[i]={exports:{}};_[i][0].call(g.exports,function(t){var e=_[i][1][t];return r(e?e:t)},g,g.exports,p,_,e,t)}return e[i].exports}for(var s="function"==typeof require&&require,i=0;i<t.length;i++)r(t[i]);return r}return p}()({1:[function(e,t){"use strict";var r=e("../common/utils.js"),s=r.Class,i=r.U2S16,n=s.subClass({init:function(e,t){this.e=e,this.v=t},toString:function(){return this.v},U2S:function(){return i(this.v)}}),o=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+")"}}),a=s.subClass({init:function(e,t,r,s,i,n){this.e=e,this.context=t,this.code=r,this.pc=s,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=a.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=s.subClass({init:function(e,t){this.ops=e||[],this.code=t||"||"},toString:function(){for(var e,t=0,r=[];t<this.ops.length;)e=this.ops[t++],r.push(e.func?(e.iftrue?"":"!(")+e.func.apply(e,e.operands)+(e.iftrue?"":")"):e);return(this.invert?"(!(":"(")+r.join(this.code)+(this.invert?"))":")")}}),u=a.subClass({brancher:1,keyword:"if",post:function(){var e,t,r=this.operands.pop(),s=r[1];this.iftrue=r[0],0===s||1===s?e="e.ret("+s+")":(s+=this.next-2,this.context.targets.push(s),e="e.pc="+s),this.result=e+";return",this.offset=s,this.cond=new g([this]),this.context.ops.length&&(t=this.context.ops.pop(),t.offset===s?(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=a.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=s.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:o,Opcode:a,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,r){return r=r||{},t&&(r.func=t),e.subClass(r)}}},{"../common/utils.js":3}],2:[function(e,t){"use strict";var r=e("./utils.js"),s=r.MemoryView,i=r.Class.subClass({init:function(e){if(this.type="",this.chunks=[],e){var t,r,n=s(e),o=12;if("FORM"!==n.getFourCC(0))throw new Error("Not an IFF file");for(this.type=n.getFourCC(8),t=n.getUint32(4)+8;o<t;){if(r=n.getUint32(o+4),0>r||r+o>t)throw new Error("IFF chunk out of range");this.chunks.push({type:n.getFourCC(o),offset:o,data:n.getUint8Array(o+8,r)}),o+=8+r,r%2&&o++}}},write:function(){for(var e,t,r=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,r+=8+this.chunks[n++].length,r%2&&r++;for(e=s(r),e.setFourCC(0,"FORM"),e.setUint32(4,r-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=s(this.chunks[0].data),r=4;r<this.chunks[0].data.length;){if("Exec"===t.getFourCC(r)&&0===t.getUint32(r+4))return void(this.exec=this.chunks.filter(function(e){return e.offset===t.getUint32(r+8)})[0]);r+=12}}}}),o=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,r,n,o=0;o<this.chunks.length;)t=this.chunks[o].type,r=this.chunks[o++].data,"CMem"===t||"UMem"===t?(this.memory=r,this.compressed="CMem"==t):"Stks"===t?this.stacks=r:"IFhd"===t&&(n=s(r.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=s(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:o,identify:function(e){var t,r,i,o=s(e);if("FORM"===o.getFourCC(0)&&"IFRS"===o.getFourCC(8)?(t=new n(e),t.exec&&(r=t.exec.type,e=t.exec.data,"GLUL"===r&&(o=s(e),i=o.getUint32(4)),"ZCOD"===r&&(i=e[0]))):"Glul"===o.getFourCC(0)?(r="GLUL",i=o.getUint32(4)):(i=o.getUint8(0),0<i&&9>i&&(r="ZCOD")),r&&i)return{format:r,version:i,data:e}}}},{"./utils.js":3}],3:[function(e,r){"use strict";function s(){for(var e,t,r=arguments[0],s=1;s<arguments.length;)for(t in e=arguments[s++],e)r[t]=e[t];return r}function i(){}function n(e){for(var t=0,r=e.length,s=new Uint16Array(r/2);t<r;)s[t/2]=e[t++]<<8|e[t++];return s}i.subClass=function(e){function t(){this.init&&this.init.apply(this,arguments)}return t.prototype=s(Object.create(this.prototype),e),t.subClass=this.subClass,t.super=t.prototype.super=this.prototype,t},r.exports={extend:s,Class:i,MemoryView:function(e,r,i){return"number"==typeof e?e=new ArrayBuffer(e):e.buffer&&(r|=0,"undefined"==typeof i&&(i=e.byteLength-r),r+=e.byteOffset,e=e.buffer),s(new DataView(e,r,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 r=e("./common/utils.js"),s=e("./common/file.js"),i={stack_len:100000,undo_len:1000000},n=r.Class.subClass(r.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=r.extend({},i,t)},start:function(){var e,t=this.Glk;try{if(e=s.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=r.MemoryView(e.data),this.staticmem=this.m.getUint16(14),this.ram=r.MemoryView(this.m,0,this.staticmem),this.origram=this.m.getUint8Array(0,this.staticmem);let n="",o=0;for(;30>o;)n+=(16>this.origram[o]?"0":"")+this.origram[o++].toString(16);this.signature=n;let i;const a=this.options.Dialog;if(a)if(this.options.clear_vm_autosave)a.autosave_write(n,null);else if(this.options.do_vm_autosave)try{const e=a.autosave_read(n);e&&(this.do_autorestore(e),i=1)}catch(e){this.log("Autorestore failed, deleting it: "+e),a.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(r){t.fatal_error(r),console.log(r)}},resume:function(e){var t,r,s=this.Glk,i=this.glk_event;try{t=i.get_field(0),2===t&&(this.handle_char_input(i.get_field(2)),r=1),3===t&&(this.handle_line_input(i.get_field(2),i.get_field(3)),r=1),5===t&&this.update_screen_size(),"fileref_create_by_prompt"===t&&(r=this.handle_create_fileref(e)),this.glk_blocking_call=null,r&&this.run(),this.quit||(this.glk_event=new s.RefStruct,this.glk_blocking_call?this.glk_event.push_field(this.glk_blocking_call):s.glk_select(this.glk_event)),s.update()}catch(t){s.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 r=e("../common/ast.js");t.exports.disassemble=function(){function e(e,t){for(var r=0;4>r;r++)t.push((192&e)>>6),e<<=2}for(var t,s,i,n,o,a,l,p=this.m,_=this.opcodes,g=new r.RoutineContext(this,this.pc);;){if(s=t=this.pc,n=p.getUint8(t++),190===n?(a=-1,n=p.getUint8(t++)+1e3):128&n?64&n?(a=-1,!(32&n)&&(n&=31)):(a=[(48&n)>>4],3>a[0]&&(n&=207)):(a=[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="+s);for(o=_[n].prototype,-1===a&&(a=[],e(p.getUint8(t++),a),(236===n||250===n)&&e(p.getUint8(t++),a)),l=[],i=0;i<a.length;)0===a[i]&&(l.push(new r.Operand(this,p.getUint16(t))),t+=2),1===a[i]&&l.push(new r.Operand(this,p.getUint8(t++))),2===a[i++]&&l.push(new r.Variable(this,p.getUint8(t++)));if(o.storer&&l.push(new r.Variable(this,p.getUint8(t++))),o.brancher&&(i=p.getUint8(t++),l.push([128&i,64&i?63&i:(i<<8|p.getUint8(t++))<<18>>18])),o.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,s,t,l)),i=0,o.stopper&&!i)break}return g}},{"../common/ast.js":1}],6:[function(r,s){"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=r("../common/utils.js"),o=n.U2S16,a=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];s.exports={init_io:function(){this.io={reverse:0,bold:0,italic:0,bg:-1,fg:-1,mono:2&this.m.getUint8(17),transcript:1&this.m.getUint8(17),streams:[0,1,{},[],{}],currentwin:0,height:0,glkheight: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,r=t.row,s=t.col;this._print(Array(t.width-t.col+1).join(" ")),this.set_cursor(r,s)}},erase_window:function(e){1>e&&(this.Glk.glk_window_clear(this.mainwin),0<=this.io.bg?this.Glk.glk_stylehint_set(3,0,8,this.io.bg):-1===this.io.bg&&this.Glk.glk_stylehint_clear(3,0,8)),0!==e&&(-1===e&&this.split_window(0),this.upperwin&&(this.Glk.glk_window_clear(this.upperwin),this.set_cursor(0,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):t.maxheight!==t.glkheight&&e.glk_window_set_arrangement(e.glk_window_get_parent(this.upperwin),18,t.maxheight,null),t.glkheight=t.maxheight),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],r=a[e]||this.reverse_unicode_table[e]||63;this.variable(this.read_data.storer,r),1===t.mode&&(t.cache+=r),2===t.mode&&this.Glk.glk_put_char_stream_uni(t.str,r)},handle_create_fileref:function(e){var t,r=this.Glk,s=this.fileref_data;return e&&(t=s.unicode?r.glk_stream_open_file_uni(e,s.mode,s.rock||0):r.glk_stream_open_file(e,s.mode,s.rock||0),r.glk_fileref_destroy(e)),("restore"===s.func||"save"===s.func)&&this.save_restore_handler(t),"input_stream"===s.func&&(this.io.streams[0]=t),"output_stream"===s.func&&this.output_stream_handler(t),s.run},handle_line_input:function(e,r){var s=this.ram,i=this.read_data,n=this.io.streams,o=t.apply(null,i.buffer.slice(0,e))+"\n",a=this.text_to_zscii(o.slice(0,-1).toLowerCase());1===n[2].mode&&(n[2].cache+=o),2===n[2].mode&&this.Glk.glk_put_jstring_stream(n[2].str,o),1===n[4].mode&&(n[4].cache+=o),2===n[4].mode&&this.Glk.glk_put_jstring_stream(n[4].str,o),5>this.version?(a.push(0),s.setUint8Array(i.bufaddr+1,a)):(s.setUint8(i.bufaddr+1,e),s.setUint8Array(i.bufaddr+2,a),this.variable(i.storer,isNaN(r)?13:r)),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(){const e=this.Glk;if(!this.mainwin){const t=[1,2,4,5,6,9,10];for(let r=0;7>r;r++)e.glk_stylehint_set(0,t[r],3,0),e.glk_stylehint_set(0,t[r],4,0),e.glk_stylehint_set(0,t[r],5,0),e.glk_stylehint_set(0,t[r],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&&this.Glk.glk_gestalt(4352,0)&&e.garglk_set_reversevideo_stream(e.glk_window_get_stream(this.statuswin),1))}else e.glk_stylehint_clear(0,0,8),this.Glk.glk_gestalt(4352,0)&&e.garglk_set_zcolors_stream(this.mainwin.str,this.io.fg,this.io.bg),e.glk_window_clear(this.mainwin),this.upperwin&&(e.glk_window_close(this.upperwin),this.upperwin=null)},output_stream:function(e,t,r){var s,i,n=this.ram,a=this.io.streams;e=o(e),1===e&&(a[1]=1),-1===e&&(a[1]=0),2!==e||a[2].mode||(this.fileref_create_by_prompt({func:"output_stream",mode:5,rock:210,run:!r,str:2,unicode:1,usage:258}),a[2].cache="",a[2].mode=1,!r&&(this.stop=1)),-2===e&&(n.setUint8(17,254&n.getUint8(17)),2===a[2].mode&&this.Glk.glk_stream_close(a[2].str),a[2].mode=this.io.transcript=0),3===e&&a[3].unshift([t,""]),-3===e&&(s=a[3].shift(),i=this.text_to_zscii(s[1]),n.setUint16(s[0],i.length),n.setUint8Array(s[0]+2,i)),4!==e||a[4].mode||(this.fileref_create_by_prompt({func:"output_stream",mode:5,rock:211,str:4,unicode:1,usage:259}),a[4].cache="",a[4].mode=1,this.stop=1),-4===e&&(2===a[4].mode&&this.Glk.glk_stream_close(a[4].str),a[4].mode=0)},output_stream_handler:function(e){var t=this.ram,r=this.io.streams,s=this.fileref_data;2===s.str&&(t.setUint8(17,254&t.getUint8(17)|(e?1:0)),e?(r[2].mode=2,r[2].str=e,this.io.transcript=1,r[2].cache&&this.Glk.glk_put_jstring_stream(r[2].str,r[2].cache)):r[2].mode=this.io.transcript=0),4===s.str&&(e?(r[4].mode=2,r[4].str=e,r[4].cache&&this.Glk.glk_put_jstring_stream(r[4].str,r[4].cache)):r[4].mode=0)},_print:function(e){var t=this.Glk,r=this.io,s=0;if(r.streams[3].length)r.streams[3][0][1]+=e;else if(e=e.replace(/\r/g,"\n"),(1&this.m.getUint8(17))!==r.transcript&&this.output_stream(r.transcript?-2:2,0,1),(2&this.m.getUint8(17))!=(2&r.mono)&&(r.mono^=2,this.format()),r.currentwin&&this.upperwin)for(;s<e.length&&r.row<r.height;)t.glk_put_jstring(e[s++]),r.col++,r.col===r.width&&(r.col=0,r.row++);else r.currentwin||(r.streams[1]&&t.glk_put_jstring(e),1===r.streams[2].mode&&(r.streams[2].cache+=e),2===r.streams[2].mode&&t.glk_put_jstring_stream(r.streams[2].str,e))},print:function(e,r){var s,i;if(0===e&&(i=r),1===e&&(i=t(r)),2===e&&(i=this.jit[r]||this.decode(r)),3===e&&(s=this.m.getUint16(this.objects+(this.version3?9:14)*r+(this.version3?7:12)),i=this.decode(s+1,2*this.m.getUint8(s))),4===e){if(!this.unicode_table[r])return;i=this.unicode_table[r]}this._print(""+i)},print_table:function(e,t,r,s){r=r||1,s=s||0;for(var n=0;n++<r;)this._print(this.zscii_to_text(this.m.getUint8Array(e,t))+(n<r?"\r":"")),e+=t+s},read:function(e,r,s,i,n){var o,a,l=this.m.getUint8(r);if(this.version3&&this.v3_status(),5>this.version&&l--,o=Array(l),o.fill(0),this.read_data={buffer:o,bufaddr:r,parseaddr:s,routine:n,storer:e,time:i},this.io.streams[0]){if(a=this.Glk.glk_get_line_stream_uni(this.io.streams[0],o),10===o[a-1]&&a--,a)return this._print(t.apply(null,o.slice(0,a))+"\n"),this.handle_line_input(a),this.stop=0;this.input_stream(0)}this.Glk.glk_request_line_event_uni(this.io.currentwin?this.upperwin:this.mainwin,o,0),this.fix_upper_window()},read_char:function(e,t,r,s){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:s,storer:e,time:r},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 r=this.io;r.currentwin&&(e>=r.height&&this.split_window(e+1),this.upperwin&&0<=e&&0<=t&&t<r.width&&(this.Glk.glk_window_move_cursor(this.upperwin,t,e),r.row=e,r.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 r=this.Glk;if(r.glk_gestalt(4352,0)){let s,n;65534===e?s=-2:(s=65535===e?-1:i(e),this.io.fg=s),65534===t?n=-2:(n=65535===t?-1:i(t),this.io.bg=n),r.garglk_set_zcolors_stream(this.mainwin.str,s,n),this.upperwin&&r.garglk_set_zcolors_stream(this.upperwin.str,s,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,r=this.Glk,s=this.io,i=s.row,n=s.col,o=s.height;if(s.height=e,this.upperwin&&e>o){for(t=r.glk_window_get_stream(this.upperwin);o<e;)r.glk_window_move_cursor(this.upperwin,0,o++),r.glk_put_jstring_stream(t,Array(s.width+1).join(" "));r.glk_window_move_cursor(this.upperwin,n,i)}e>s.maxheight&&(s.maxheight=e,this.upperwin?r.glk_window_set_arrangement(r.glk_window_get_parent(this.upperwin),18,s.maxheight,null):(0<=this.io.bg&&r.glk_stylehint_set(4,0,8,this.io.bg),this.upperwin=r.glk_window_open(this.mainwin,18,s.maxheight,4,203),this.Glk.glk_gestalt(4352,0)&&r.garglk_set_zcolors_stream(this.upperwin.str,this.io.fg,this.io.bg),r.glk_stylehint_clear(4,0,8)),s.glkheight=s.maxheight),e&&(s.row>=e&&this.set_cursor(0,0),this.version3&&r.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,r=new t.RefBox,s=new t.RefBox,i=t.glk_window_open(this.mainwin,18,0,4,0);let n=0,o=0;t.glk_window_get_size(this.mainwin,s,r),n=r.get_value(),this.upperwin&&(t.glk_window_get_size(this.upperwin,s,r),n+=r.get_value()),this.statuswin&&(t.glk_window_get_size(this.statuswin,s,r),n+=r.get_value()),i&&(t.glk_window_get_size(i,s,0),t.glk_window_close(i)),o=s.get_value(),n=e(n,254),o=this.io.width=e(o,255),3<this.version&&(this.ram.setUint8(32,n),this.ram.setUint8(33,o)),4<this.version&&(this.ram.setUint16(34,o),this.ram.setUint16(36,n)),this.io.col>=o&&(this.io.col=o-1)},v3_status:function(){if(this.statuswin){var e,t=this.Glk,r=t.glk_window_get_stream(this.statuswin),s=this.m,i=this.io.width,n=s.getUint16(this.globals+2),o=s.getUint16(this.globals+4),a=s.getUint16(this.objects+9*s.getUint16(this.globals)+7),l=""+this.decode(a+1,2*s.getUint8(a));e=2&s.getUint8(1)?"Time: "+(0==n%12?12:n%12)+":"+(10>o?"0":"")+o+" "+(11<n?"PM":"AM"):"Score: "+n+" Turns: "+o,t.glk_window_move_cursor(this.statuswin,0,0),t.glk_put_jstring_stream(r,Array(i+1).join(" ")),t.glk_window_move_cursor(this.statuswin,0,0),t.glk_put_jstring_stream(r," "+l.slice(0,i-e.length-4)),t.glk_window_move_cursor(this.statuswin,i-e.length-1,0),t.glk_put_jstring_stream(r,e)}}}},{"../common/utils.js":3}],7:[function(e,t){"use strict";var r=e("../common/ast.js"),s=r.Variable,i=r.Opcode,n=r.Stopper,o=r.Pauser,a=r.PauserStorer,l=r.Brancher,p=r.BrancherStorer,_=r.Storer,g=r.Caller,u=r.CallerStorer,c=r.opcode_builder,d=function(e){return""+e},m=new s(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],r=t instanceof s;e[0]=new s(this.e,r?t:t.v),(r||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,r=this.code%2?1:-1;return e instanceof s||14<t?"e.incdec("+e+","+r+")":(0>t?"e.s[e.sp-1]":"e.l["+t+"]")+(1==r?"++":"--")}}),b=n.subClass({brancher:1,toString:function(){return"e.stop=1;e."+(181===this.code?"save":"restore")+"("+(this.pc+1)+")"}}),w=c(a,function(){return"e.restore("+(this.next-1)+")"}),v=c(a,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.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(o,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,r){return"e.ram.setUint16(e.S2U("+e+"+2*"+t.U2S()+"),"+r+")"}),226:c(i,function(e,t,r){return"e.ram.setUint8(e.S2U("+e+"+"+t.U2S()+"),"+r+")"}),227:c(i,function(){return"e.put_prop("+this.args()+")"}),228:5>e?c(o,function(){return"e.read(0,"+this.args()+")"}):c(a,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(o,function(){return"e.input_stream("+this.args()+")"}),245:i,246:c(a,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 r(e){const t=(e)=>"object"==typeof e?r(e):e,s={};if(Array.isArray(e))return e.map(t);for(let r in e)"buffer"!=r&&"str"!=r&&(s[r]=t(e[r]));return s}function s(e,t,r,s){if(p&&!s)for(;t<r;)e.setUint16(t,e.getUint16(t,1)),t+=2}const n=e("../common/file.js"),o=e("../common/utils.js"),i=o.extend,a=o.U2S16,l=o.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,r,s){if(0===e)return 0<=t&&this.variable(t,0),this.pc=r;this.pc=e*this.addr_multipler;var n=this.m.getUint8(this.pc++),o=this.stack,a=0,i=this.frameptr;for(o.setUint16(i+6,this.sp),this.frames.push(i),i=this.frameptr=this.s.byteOffset+2*this.sp,o.setUint32(i,r<<8),o.setUint8(i+3,(0<=t?0:16)|n),o.setUint8(i+4,0<=t?t:0),o.setUint8(i+5,(1<<s.length)-1),this.make_stacks(),this.sp=0;a<n;)this.l[a]=a<s.length?s[a]:5>this.version?this.m.getUint16(this.pc+2*a):0,a++;5>this.version&&(this.pc+=2*n)},clear_attr:function(e,t){var r=0|this.objects+(this.version3?9:14)*e+t/8;this.ram.setUint8(r,this.m.getUint8(r)&~(128>>t%8))},copy_table:function(e,t,r){r=a(r);var s=this.ram,n=0,i=0>r;if(r=Math.abs(r),0===t){for(;n<r;)s.setUint8(e+n++,0);return}if(i)for(;n<r;)s.setUint8(t+n,this.m.getUint8(e+n++));else s.setUint8Array(t,this.m.getUint8Array(e,r))},do_autorestore:function(e){const t=this.Glk;t.restore_allstate(e.glk),this.io=e.io;const r=new t.RefBox;let s;for(;s=t.glk_window_iterate(s,r);)201===r.value&&(this.mainwin=s,s.linebuf&&(e.read_data.buffer=s.linebuf)),202===r.value&&(this.statuswin=s),203===r.value&&(this.upperwin=s);for(s=null;s=t.glk_stream_iterate(s,r);)210===r.value&&(this.io.streams[2].str=s),211===r.value&&(this.io.streams[4].str=s);this.restart(1),this.restore_file(this.options.Dialog.streaming?new Uint8Array(e.ram):Uint8Array.from(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;if(0<=(e||0)){const e=this.save_file(this.pc,1);t={glk:this.Glk.save_allstate(),io:r(this.io),ram:this.options.Dialog.streaming?e:Array.from(new Uint8Array(e)),read_data:r(this.read_data),xorshift_seed:this.xorshift_seed}}this.options.Dialog.autosave_write(this.signature,t)},encode_text:function(e,t,r,s){this.ram.setUint8Array(s,this.encode(this.m.getUint8Array(e+r,t)))},extension_table:function(e,t){var r=this.extension;return!r||e>this.extension_count?0:(r+=2*e,void 0===t?this.m.getUint16(r):void this.ram.setUint16(r,t))},find_prop:function(e,t,r){var s,i,n=this.m,o=this.version3,a=0,l=n.getUint16(this.objects+(o?9:14)*e+(o?7:12));for(l+=2*n.getUint8(l)+1;;){if(s=n.getUint8(l),i=s&(o?31:63),a===r)return i;if(i===t)return l+(!o&&128&s?2:1);if(i<t)return 0;a=i,o?l+=(s>>5)+2:128&s?(i=63&n.getUint8(l+1),l+=i?i+2:66):l+=64&s?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 r,s=this.m,i=this.find_prop(e,t);return i?(r=s.getUint8(i-1),s[(this.version3?r>>5:64&r)?"getUint16":"getUint8"](i)):s.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 r=this.globals+2*(e-15);return this.ram.setUint16(r,this.m.getUint16(r)+t),this.ram.getUint16(r)},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,r){var s,i=this.find_prop(e,t);i&&(s=this.m.getUint8(i-1),this.ram[(this.version3?s>>5:64&s)?"setUint16":"setUint8"](i,r))},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,r,s,i=this.get_parent(e);if(0!==i)if(t=this.get_child(i),r=this.get_sibling(e),t===e)this.set_family(e,0,i,r);else{for(;s=this.get_sibling(t),s!==e;)t=s;this.set_family(e,0,0,0,t,r)}},restart:function(t){var r=this.ram,s=r.getUint8(0),n=3===s,a=n?2:8===s?8:4,l=r.getUint8(17),p=r.getUint16(10),_=4<s?r.getUint16(54):0,g=o.MemoryView(this.options.stack_len);r.setUint8Array(0,this.origram),r.setUint8(17,l),i(this,{stack:g,frameptr:0,frames:[],s:new Uint16Array(g.buffer,8),sp:0,l:[],undo:[],undo_len:0,glk_blocking_call:null,version:s,version3:n,pc:r.getUint16(6),properties:p,objects:p+(n?53:112),globals:r.getUint16(12),eof:(r.getUint16(26)||65536)*a,extension:_,extension_count:_?r.getUint16(_):0,addr_multipler:a,opcodes:e("./opcodes.js")(s)}),this.init_text(),t||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 r,o=this.ram,a=new n.Quetzal(e),l=a.memory,p=this.stack,_=o.getUint8(17),g=0,i=0;if(o.getUint16(2)!==a.release||o.getUint16(28)!==a.checksum)return 0;for(;6>g;)if(o.getUint8(18+g)!==a.serial[g++])return 0;if(g=0,o.setUint8Array(0,this.origram),a.compressed)for(;g<l.length;)r=l[g++],0===r?i+=1+l[g++]:o.setUint8(i,r^this.origram[i++]);else o.setUint8Array(0,l);for(o.setUint8(17,_),p.setUint8Array(0,a.stacks),this.frames=[],g=0;g<a.stacks.byteLength;)this.frameptr=g,this.frames.push(g),s(p,i=g+8,i+=2*(15&p.getUint8(g+3)),t),s(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=a.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,r=this.frameptr,s=16&t.getUint8(r+3)?-1:t.getUint8(r+4);this.pc=t.getUint32(r)>>8,r=this.frameptr=this.frames.pop(),this.make_stacks(),this.sp=t.getUint16(r+6),0<=s&&this.variable(s,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 r,i,a,l=this.m,_=new n.Quetzal,g=o.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,r=0;r<this.staticmem;r++)a=l.getUint8(r)^this.origram[r],0===a?256==++u&&(e.push(0,255),u=0):(u&&(e.push(0,u-1),u=0),e.push(a));_.memory=e}if(g.setUint16(c+6,this.sp),p&&!t){const e=this.frames.slice();for(e.push(c),r=0;r<e.length;r++)c=e[r],s(g,i=c+8,i+=2*(15&g.getUint8(c+3))),s(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,r,s,i=this.m,n=this.Glk,o=0,a=[];e&&("save"===this.fileref_data.func?(n.glk_put_buffer_stream(e,new Uint8Array(this.save_file(this.pc))),o=1):(a=new Uint8Array(131072),n.glk_get_buffer_stream(e,a),o=this.restore_file(a.buffer)),n.glk_stream_close(e)),this.version3?(t=i.getUint8(this.pc++),r=128&t,s=64&t?63&t:(t<<8|i.getUint8(this.pc++))<<18>>18,!o==!r&&(0===s||1===s?this.ret(s):this.pc+=s-2)):this.variable(i.getUint8(this.pc++),o)},save_undo:function(e,t){var r;return this.undo_len>this.options.undo_len&&(r=this.undo.shift(),this.undo_len-=r.ram.byteLength+r.stack.byteLength),r={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+=r.ram.byteLength+r.stack.byteLength,this.undo.push(r),1},scan_table:function(e,t,r,s){s=s||130;var i=128&s?"getUint16":"getUint8";for(s&=127,r=t+r*s;t<r;){if(this.m[i](t)===e)return t;t+=s}return 0},set_attr:function(e,t){var r=0|this.objects+(this.version3?9:14)*e+t/8;this.ram.setUint8(r,this.m.getUint8(r)|128>>t%8)},set_family:function(e,t,r,s,i,n){var o=this.ram,a=this.objects;this.version3?(o.setUint8(a+9*e+4,t),r&&o.setUint8(a+9*r+6,s),i&&o.setUint8(a+9*i+5,n)):(o.setUint16(a+14*e+6,t),r&&o.setUint16(a+14*r+10,s),i&&o.setUint16(a+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 r,s=t!==void 0;if(0===e){if(s)this.s[this.sp++]=t;else return this.s[--this.sp];}else if(15>--e){if(s)this.l[e]=t;else return this.l[e];}else if(r=this.globals+2*(e-15),s)this.ram.setUint16(r,t);else return this.m.getUint16(r);return t},U2S:a,S2U:l}},{"../common/file.js":2,"../common/utils.js":3,"./opcodes.js":7}],9:[function(r,s){s.exports={init_text:function(){var e=this,r=this.m,s=4<this.version&&r.getUint16(52),i=this.extension_table(3),n=i&&r.getUint8(i++);this.abbr_addr=r.getUint16(24),function(t){for(var r=[[],[],[]],s=0;78>s;)r[0|s/26][s%26]=t[s++];r[2][1]=13,e.alphabets=r}(s?r.getUint8Array(s,78):this.text_to_zscii("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \r0123456789.,!?_#'\"/\\-:()",1)),function(r){for(var s={13:"\r"},n={13:13},o=0;o<r.length;)s[155+o]=t(r[o]),n[r[o]]=155+o++;for(o=32;127>o;)s[o]=t(o),n[o]=o++;e.unicode_table=s,e.reverse_unicode_table=n}(i?r.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=r.getUint16(8),this.parse_dict(this.dict)},decode:function(e,t){var r,s,n,o=this.m,a=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&&(r=o.getUint16(e),e+=2,l.push(31&r>>10,31&r>>5,31&r),!(32768&r)););for(;p<l.length;)s=l[p++],0===s?_.push(32):4>s?(n=1,_.push(-1),g.push("\uE000+this.abbr("+(32*(s-1)+l[p++])+")+\uE000")):6>s?i=s:2==i&&6===s?p+1<l.length&&_.push(l[p++]<<5|l[p++]):32>s&&_.push(this.alphabets[i][s-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)}),a>=this.staticmem&&(this.jit[a]=_),_},encode:function(e){for(var t,r,s=this.alphabets,n=[],o=this.version3?6:9,a=0,i=[];n.length<o;)t=e[a++],32===t?n.push(0):0<=(r=s[0].indexOf(t))?n.push(r+6):0<=(r=s[1].indexOf(t))?n.push(4,r+6):0<=(r=s[2].indexOf(t))?n.push(5,r+6):void 0===t?n.push(5):n.push(5,6,t>>5,31&t);for(n.length=o,a=0;a<o;)i.push(n[a++]<<2|n[a]>>3,(7&n[a++])<<5|n[a++]);return i[i.length-2]|=128,i},zscii_to_text:function(e,t){for(var r,s=0,i=e.length,n=0,o="";s<i;)r=e[s++],-1===r&&(o+=t[n++]),(r=this.unicode_table[r])&&(o+=r);return o},text_to_zscii:function(e,t){for(var r,s=[],n=0,i=e.length;n<i;)r=e.charCodeAt(n++),t||(r=this.reverse_unicode_table[r]||63),s.push(r);return s},parse_dict:function(e){var t,r,s=this.m,i=e,n={},o=s.getUint8(e++);for(n.separators=Array.prototype.slice.call(s.getUint8Array(e,o)),e+=o,t=s.getUint8(e++),r=e+2+t*s.getUint16(e),e+=2;e<r;)n[Array.prototype.toString.call(s.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,r,s,n){s=s||this.dict,s=this.dictionaries[s]||this.parse_dict(s);var o,a,l,p,_=this.m,g=this.ram,u=1e3,c=1,i=s.separators,d=[],m=0;for(4<this.version&&(u=_.getUint8(t+c++)+2);c<u&&(o=_.getUint8(t+c),0!==o);)32===o||0<=i.indexOf(o)?(32!==o&&d.push([[o],c]),a=null):(a||(d.push([[],c]),a=d[d.length-1][0]),a.push(o)),c++;for(l=e(d.length,_.getUint8(r));m<l;)p=s[""+this.encode(d[m][0])],(!n||p)&&(g.setUint16(r+2+4*m,p||0),g.setUint8(r+4+4*m,d[m][0].length),g.setUint8(r+5+4*m,d[m][1])),m++;g.setUint8(r+1,m)}}},{}]},{},[4])(4)});