diff --git a/Makefile b/Makefile index b04a605..499bdef 100644 --- a/Makefile +++ b/Makefile @@ -2,14 +2,16 @@ name = "oh-my-git" all: linux macos windows +.PHONY: web + linux: mkdir -p build/$(name)-linux - godot --export "Linux" "build/$(name)-linux/$(name)" + godot --export-debug "Linux" "build/$(name)-linux/$(name)" cd build/$(name)-linux && zip -r ../$(name)-linux.zip * macos: mkdir -p build - godot --export "Mac OS" "build/$(name)-macos.zip" + godot --export-debug "Mac OS" "build/$(name)-macos.zip" windows: dependencies/windows/git/ mkdir -p build/$(name)-windows @@ -18,6 +20,11 @@ windows: dependencies/windows/git/ cp -r --parents dependencies/windows/git/ build/$(name)-windows/ cd build/$(name)-windows && zip -r ../$(name)-windows.zip * +web: + mkdir -p build/$(name)-web + godot --export-debug "Web" "build/$(name)-web/index.html" + cp -r web/web-shell/ build/$(name)-web/ + clean-unzipped: cd build && ls | grep -v '\.zip$$' | xargs rm -r diff --git a/export_presets.cfg b/export_presets.cfg index 7ce858b..c039200 100644 --- a/export_presets.cfg +++ b/export_presets.cfg @@ -128,8 +128,8 @@ variant/extensions_support=false vram_texture_compression/for_desktop=true vram_texture_compression/for_mobile=false html/export_icon=true -html/custom_html_shell="" -html/head_include="" +html/custom_html_shell="res://web/export.html" +html/head_include="" html/canvas_resize_policy=2 html/focus_canvas_on_start=true html/experimental_virtual_keyboard=false diff --git a/scenes/game.tscn b/scenes/game.tscn index bfdf02f..34d6f04 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -1,16 +1,16 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=4 format=3 uid="uid://dxsp6o8f372d1"] -[ext_resource path="res://scenes/game.gd" type="Script" id=1] -[ext_resource path="res://music/gigantic-greasy-giraffe.ogg" type="AudioStream" id=2] -[ext_resource path="res://scenes/tcp_server_shell.tscn" type="PackedScene" id=3] +[ext_resource type="Script" path="res://scenes/game.gd" id="1"] +[ext_resource type="AudioStream" uid="uid://3vg0hudrk1wl" path="res://music/gigantic-greasy-giraffe.ogg" id="2"] +[ext_resource type="PackedScene" path="res://scenes/tcp_server_shell.tscn" id="3"] [node name="Node" type="Node"] -script = ExtResource( 1 ) +script = ExtResource("1") [node name="Music" type="AudioStreamPlayer" parent="."] -stream = ExtResource( 2 ) +stream = ExtResource("2") volume_db = -15.0 autoplay = true -[node name="ShellServer" parent="." instance=ExtResource( 3 )] +[node name="ShellServer" parent="." instance=ExtResource("3")] port = 6666 diff --git a/scenes/shell.gd b/scenes/shell.gd index 6e25bd9..0989326 100644 --- a/scenes/shell.gd +++ b/scenes/shell.gd @@ -6,11 +6,13 @@ var exit_code var _cwd var _os = OS.get_name() +var web_shell = JavaScriptBridge.get_interface("web_shell") + func _init(): # Create required directories and move into the tmp directory. _cwd = "/tmp" run("mkdir -p '%s/repos'" % game.tmp_prefix) - _cwd = game.tmp_prefix + _cwd = game.tmp_prefixls func cd(dir): _cwd = dir @@ -85,13 +87,17 @@ func run_async_thread(shell_command): result = helpers.exec(_shell_binary(), [script_path], crash_on_fail) elif _os == "Web": hacky_command = hacky_command.replace("\\", "\\\\").replace("'", "\\'").replace("\n", "\\n") - var js_code = "console.log('" + hacky_command + "')" - print(js_code) - print(shell_command) - JavaScriptBridge.eval(js_code) - #JavaScriptBridge.eval("test('blubb')") + #var js_code = "await run_in_vm('" + hacky_command + "')" + #print(js_code) + #var output = JavaScriptBridge.eval(js_code, true) + + var output = JavaScriptBridge.eval("testy()") + + #print(hacky_command) + #var output = web_shell.run_in_vm(hacky_command) + result = {} - result["output"] = "Hi!" + result["output"] = output result["exit_code"] = 0 else: helpers.crash("Unimplemented OS: %s" % _os) diff --git a/scenes/terminal.gd b/scenes/terminal.gd index 4362a55..3cf033e 100644 --- a/scenes/terminal.gd +++ b/scenes/terminal.gd @@ -89,9 +89,17 @@ func send_command(command): command = editor_regex.sub(command, "fake-editor ") shell.cd(repository.path) - var cmd = shell.run_async(command, false) - await cmd.done - call_deferred("command_done", cmd) + + #var cmd = shell.run_async(command, false) + #await cmd.done + #call_deferred("command_done", cmd) + + var output = shell.run(command, false) + var shell_command = ShellCommand.new() + shell_command.exit_code = 0 + shell_command.output = output + shell_command.command = command + command_done(shell_command) func command_done(cmd): if cmd.exit_code == 0: diff --git a/web/export.html b/web/export.html new file mode 100644 index 0000000..c0a1595 --- /dev/null +++ b/web/export.html @@ -0,0 +1,21 @@ + + +
+>>0,8)+" | "+u);for(var z=0;z>>0,8)+" -> "+h(D.address,8)+" | "+u+" (at "+h(C,8)+")"))}}}}var e=this,g={};this.debug=g;g.init=function(){function l(p){10===p?(dbg_log(n,LOG_BIOS),n=""):n+=String.fromCharCode(p)}if(DEBUG&&e.io){var n="";e.io.register_write(1026,this,l);e.io.register_write(1280,this,l)}};g.get_regs_short=b;g.dump_regs=function(){if(DEBUG){var l=b();dbg_log(l[0], +LOG_CPU);dbg_log(l[1],LOG_CPU)}};g.get_state=a;g.dump_state=function(l){DEBUG&&dbg_log(a(l),LOG_CPU)};g.dump_stack=function(l,n){if(DEBUG){var p=e.reg32[REG_ESP];dbg_log("========= STACK ==========");if(n>=l||void 0===n)l=5,n=-5;for(;l>n;l--){var t=" ";l||(t="=> ");t+=h(l,2)+" | ";dbg_log(t+h(p+4*l,8)+" | "+h(e.read32s(p+4*l)>>>0))}}};g.dump_page_structures=function(){if(e.cr[4]&CR4_PAE){dbg_log("PAE enabled");for(var l=0;4>l;l++){var n=e.read32s(e.cr[3]+8*l);n&1&&d(n&4294963200,!0,l<<30)}}else dbg_log("PAE disabled"), +d(e.cr[3],!1,0)};g.dump_gdt_ldt=function(){function l(n,p){for(var t=0;t >4,x="",z=v>>5&3;x=v&128?x+" P ":x+"NP ";v&16?(x=u&4?x+"32b ":x+"16b ",v&8?(x+="X ",v&4&&(x+="C ")):x+="R ",x+="RW "):x+="sys: "+h(v&15);u&8&&(q=q<<12|4095);dbg_log(h(t&-8,4)+" "+h(r>>>0,8)+" ("+h(q>>>0,8)+" bytes) "+x+"; dpl = "+z+", a = "+v.toString(2)+", f = "+u.toString(2))}}DEBUG&&(dbg_log("gdt: (len = "+ +h(e.gdtr_size[0])+")"),l(e.translate_address_system_read(e.gdtr_offset[0]),e.gdtr_size[0]),dbg_log("\nldt: (len = "+h(e.segment_limits[REG_LDTR])+")"),l(e.translate_address_system_read(e.segment_offsets[REG_LDTR]),e.segment_limits[REG_LDTR]))};g.dump_idt=function(){if(DEBUG)for(var l=0;l
>5&3;var q=5===(n&31)?"task gate ":14===(n&31)?"intr gate ":15=== +(n&31)?"trap gate ":"invalid ";q=n&128?q+" P":q+"NP";dbg_log(h(l>>3,4)+" "+h(p>>>0,8)+", "+h(t,4)+"; "+q+"; dpl = "+r+", t = "+n.toString(2))}};g.get_memory_dump=function(l,n){if(DEBUG)return void 0===l?(l=0,n=e.memory_size[0]):void 0===n&&(n=l,l=0),e.mem8.slice(l,l+n).buffer};g.memory_hex_dump=function(l,n){if(DEBUG){n=n||64;for(var p,t,r=0;r >4;r++){p=h(l+(r<<4),5)+" ";for(var q=0;16>q;q++)t=e.read8(l+(r<<4)+q),p+=h(t,2)+" ";p+=" ";for(q=0;16>q;q++)t=e.read8(l+(r<<4)+q),p+=33>t||126 p;p++){n=h(128*p*l,8)+" | ";for(var t=0;128>t;t++)n+=0 >>0)+": "+v86util.pads(t.bytes.map(r=>h(r,2).slice(-2)).join(" "),20)+" "+t.mnemonic+" "+t.op_str)}),dbg_log("")}catch(t){dbg_log("Could not disassemble: "+Array.from(n).map(r=>h(r,2)).join(" "))}};let m;g.dump_wasm=function(l){if(void 0===m&&(m="function"===typeof require?require("./libwabt.js"):new window.WabtModule,void 0===m)){dbg_log("Warning: Missing libwabt, wasm dump not available"); +return}l=l.slice();try{var n=m.readWasm(l,{readDebugNames:!1});n.generateNames();n.applyNames();const r=n.toText({foldExprs:!0,inlineExport:!0});dbg_log(r)}catch(r){var p=new Blob([l]),t=document.createElement("a");t.download="failed.wasm";t.href=window.URL.createObjectURL(p);t.dataset.downloadurl=["application/octet-stream",t.download,t.href].join(":");t.click();window.URL.revokeObjectURL(t.src);console.log(r.toString())}finally{n&&n.destroy()}}};const ELF_MAGIC=1179403647; +let types=DataView.prototype,U8={size:1,get:types.getUint8,set:types.setUint8},U16={size:2,get:types.getUint16,set:types.setUint16},U32={size:4,get:types.getUint32,set:types.setUint32},pad=function(a){return{size:a,get:b=>-1}},Header=create_struct([{magic:U32},{class:U8},{data:U8},{version0:U8},{osabi:U8},{abiversion:U8},{pad0:pad(7)},{type:U16},{machine:U16},{version1:U32},{entry:U32},{phoff:U32},{shoff:U32},{flags:U32},{ehsize:U16},{phentsize:U16},{phnum:U16},{shentsize:U16},{shnum:U16},{shstrndx:U16}]); +console.assert(52===Header.reduce((a,b)=>a+b.size,0));let ProgramHeader=create_struct([{type:U32},{offset:U32},{vaddr:U32},{paddr:U32},{filesz:U32},{memsz:U32},{flags:U32},{align:U32}]);console.assert(32===ProgramHeader.reduce((a,b)=>a+b.size,0));let SectionHeader=create_struct([{name:U32},{type:U32},{flags:U32},{addr:U32},{offset:U32},{size:U32},{link:U32},{info:U32},{addralign:U32},{entsize:U32}]);console.assert(40===SectionHeader.reduce((a,b)=>a+b.size,0)); +function create_struct(a){return a.map(function(b){var c=Object.keys(b);console.assert(1===c.length);c=c[0];b=b[c];console.assert(0 >>0));console.assert(b.magic===ELF_MAGIC,"Bad magic");console.assert(1===b.class,"Unimplemented: 64 bit elf");console.assert(1===b.data,"Unimplemented: big endian");console.assert(1===b.version0,"Bad version0");console.assert(2===b.type,"Unimplemented type");console.assert(1===b.version1,"Bad version1");console.assert(52===b.ehsize, +"Bad header size");console.assert(32===b.phentsize,"Bad program header size");console.assert(40===b.shentsize,"Bad section header size");[d]=read_structs(view_slice(a,b.phoff,b.phentsize*b.phnum),ProgramHeader,b.phnum);[a]=read_structs(view_slice(a,b.shoff,b.shentsize*b.shnum),SectionHeader,b.shnum);if(DEBUG&&LOG_LEVEL){console.log("%d program headers:",d.length);for(let e of d)console.log("type=%s offset=%s vaddr=%s paddr=%s filesz=%s memsz=%s flags=%s align=%s",e.type.toString(16),e.offset.toString(16), +e.vaddr.toString(16),e.paddr.toString(16),e.filesz.toString(16),e.memsz.toString(16),e.flags.toString(16),e.align.toString(16));console.log("%d program headers:",a.length);for(let e of a)console.log("name=%s type=%s flags=%s addr=%s offset=%s size=%s link=%s info=%s addralign=%s entsize=%s",e.name.toString(16),e.type.toString(16),e.flags.toString(16),e.addr.toString(16),e.offset.toString(16),e.size.toString(16),e.link.toString(16),e.info.toString(16),e.addralign.toString(16),e.entsize.toString(16))}return{header:b, +program_headers:d,sections_headers:a}}function read_struct(a,b){let c={},d=0;for(let e of b)b=e.get.call(a,d,!0),console.assert(void 0===c[e.name]),c[e.name]=b,d+=e.size;return[c,d]}function read_structs(a,b,c){let d=[],e=0;for(var g=0;g >1];if(m!==LINUX_BOOT_HDR_CHECKSUM1)dbg_log("Bad checksum1: "+h(m));else if(m=g[LINUX_BOOT_HDR_HEADER>>1]|g[LINUX_BOOT_HDR_HEADER+2>>1]<<16,m!==LINUX_BOOT_HDR_CHECKSUM2)dbg_log("Bad checksum2: "+h(m));else{m=g[LINUX_BOOT_HDR_VERSION>>1];dbg_assert(514<=m);var l=e[LINUX_BOOT_HDR_LOADFLAGS];dbg_assert(l& +LINUX_BOOT_HDR_LOADFLAGS_LOADED_HIGH);var n=g[LINUX_BOOT_HDR_XLOADFLAGS>>1],p=f[LINUX_BOOT_HDR_INITRD_ADDR_MAX>>2],t=f[LINUX_BOOT_HDR_KERNEL_ALIGNMENT>>2],r=e[LINUX_BOOT_HDR_RELOCATABLE_KERNEL],q=e[LINUX_BOOT_HDR_MIN_ALIGNMENT],v=f[LINUX_BOOT_HDR_CMDLINE_SIZE>>2],u=f[LINUX_BOOT_HDR_PAYLOAD_OFFSET>>2],x=f[LINUX_BOOT_HDR_PAYLOAD_LENGTH>>2],z=f[LINUX_BOOT_HDR_PREF_ADDRESS>>2],C=f[LINUX_BOOT_HDR_PREF_ADDRESS+4>>2],D=f[LINUX_BOOT_HDR_INIT_SIZE>>2];dbg_log("kernel boot protocol version: "+h(m));dbg_log("flags="+ +h(l)+" xflags="+h(n));dbg_log("code32_start="+h(f[LINUX_BOOT_HDR_CODE32_START>>2]));dbg_log("initrd_addr_max="+h(p));dbg_log("kernel_alignment="+h(t));dbg_log("relocatable="+r);dbg_log("min_alignment="+h(q));dbg_log("cmdline max="+h(v));dbg_log("payload offset="+h(u)+" size="+h(x));dbg_log("pref_address="+h(C)+":"+h(z));dbg_log("init_size="+h(D));e[LINUX_BOOT_HDR_TYPE_OF_LOADER]=LINUX_BOOT_HDR_TYPE_OF_LOADER_NOT_ASSIGNED;e[LINUX_BOOT_HDR_LOADFLAGS]=l&~LINUX_BOOT_HDR_LOADFLAGS_QUIET_FLAG&~LINUX_BOOT_HDR_LOADFLAGS_KEEP_SEGMENTS| +LINUX_BOOT_HDR_LOADFLAGS_CAN_USE_HEAPS;g[LINUX_BOOT_HDR_HEAP_END_PTR>>1]=56832;g[LINUX_BOOT_HDR_VIDMODE>>1]=65535;dbg_log("heap_end_ptr="+h(56832));d+="\x00";dbg_assert(d.length >2]=581632;for(e=0;e >2]=k;f[LINUX_BOOT_HDR_RAMDISK_SIZE>>2]=e;dbg_assert(655360>524288+d.length);a.set(d,524288);a.set(b,1048576);return{option_rom:{name:"genroms/kernel.bin",data:make_linux_boot_rom(32768,57344)}}}} +function make_linux_boot_rom(a,b){const c=new Uint8Array(256);(new Uint16Array(c.buffer))[0]=43605;c[2]=1;var d=3;c[d++]=250;c[d++]=184;c[d++]=a>>0;c[d++]=a>>8;c[d++]=142;c[d++]=192;c[d++]=142;c[d++]=216;c[d++]=142;c[d++]=224;c[d++]=142;c[d++]=232;c[d++]=142;c[d++]=208;c[d++]=188;c[d++]=b>>0;c[d++]=b>>8;c[d++]=234;c[d++]=0;c[d++]=0;c[d++]=a+32>>0;c[d++]=a+32>>8;dbg_assert(512>d);a=d;b=c[a]=0;for(d=0;d keyCode not found: ",q,r)}};function MouseAdapter(a,b){function c(u){if(!v.enabled||!v.emu_enabled)return!1;var x=b||document.body,z;if(!(z=document.pointerLockElement))a:{for(u=u.target;u.parentNode;){if(u===x){z=!0;break a}u=u.parentNode}z=!1}return z}function d(u){c(u)&&(u=u.changedTouches)&&u.length&&(u=u[u.length-1],r=u.clientX,q=u.clientY)}function e(u){if(n||t||p)v.bus.send("mouse-click",[!1,!1,!1]),n=t=p=!1}function g(u){if(v.bus&&c(u)&&v.is_running){var x=0,z=0,C=u.changedTouches;C?C.length&&(C=C[C.length-1],x=C.clientX- +r,z=C.clientY-q,r=C.clientX,q=C.clientY,u.preventDefault()):"number"===typeof u.movementX?(x=u.movementX,z=u.movementY):"number"===typeof u.webkitMovementX?(x=u.webkitMovementX,z=u.webkitMovementY):"number"===typeof u.mozMovementX?(x=u.mozMovementX,z=u.mozMovementY):(x=u.clientX-r,z=u.clientY-q,r=u.clientX,q=u.clientY);v.bus.send("mouse-delta",[.15*x,-(.15*z)]);b&&v.bus.send("mouse-absolute",[u.pageX-b.offsetLeft,u.pageY-b.offsetTop,b.offsetWidth,b.offsetHeight])}}function f(u){c(u)&&m(u,!0)}function k(u){c(u)&& +m(u,!1)}function m(u,x){v.bus&&(1===u.which?n=x:2===u.which?t=x:3===u.which?p=x:dbg_log("Unknown event.which: "+u.which),v.bus.send("mouse-click",[n,t,p]),u.preventDefault())}function l(u){if(c(u)){var x=u.wheelDelta||-u.detail;0>x?x=-1:0 {switch(n.data.type){case "queue":l.queue_push(n.data.value);break;case "sampling-rate":l.source_samples_per_destination=n.data.value/sampleRate}};return l}var m=[new Float32Array(256),new Float32Array(256)];Reflect.setPrototypeOf(k.prototype,AudioWorkletProcessor.prototype);Reflect.setPrototypeOf(k,AudioWorkletProcessor);k.prototype.process=k.prototype.process=function(l,n,p){for(l=0;l l?(l+=this.source_buffer_previous[0].length,this.source_buffer_previous[n][l]):this.source_buffer_current[n][l]};k.prototype.ensure_enough_data=function(l){var n=this.source_buffer_current[0].length;n-this.source_block_start this.queued_samples&&this.queue_length&&this.dbg_log("Not enough samples - should not happen during midway of playback");this.source_buffer_previous=this.source_buffer_current;this.source_buffer_current=this.queue_shift();var l=this.source_buffer_current[0].length;if(256>l){for(var n=this.queue_start,p=0;256>l&&p this.queued_samples/this.source_samples_per_destination&&this.port.postMessage({type:"pump"})};k.prototype.queue_push=function(l){this.queue_length {URL.revokeObjectURL(g);this.node_processor=new AudioWorkletNode(this.audio_context,"dac-processor",{numberOfInputs:0,numberOfOutputs:1,outputChannelCount:[2],parameterData:{},processorOptions:{}});this.node_processor.port.postMessage({type:"sampling-rate",value:this.sampling_rate}); +this.node_processor.port.onmessage=f=>{switch(f.data.type){case "pump":this.pump();break;case "debug-log":dbg_log("SpeakerWorkletDAC - Worklet: "+f.data.value)}};this.node_processor.connect(this.node_output)});this.mixer_connection=c.add_source(this.node_output,MIXER_SRC_DAC);this.mixer_connection.set_gain_hidden(3);a.register("dac-send-data",function(f){this.queue(f)},this);a.register("dac-enable",function(f){this.enabled=!0},this);a.register("dac-disable",function(){this.enabled=!1},this);a.register("dac-tell-sampling-rate", +function(f){dbg_assert(0 this.pump(),1E3*b);a.start(this.buffered_time);this.buffered_time+= +c;setTimeout(()=>this.pump(),0)};SpeakerBufferSourceDAC.prototype.pump=function(){this.enabled&&(this.buffered_time-this.audio_context.currentTime>DAC_QUEUE_RESERVE||this.bus.send("dac-request-data"))}; +function SpeakerDACDebugger(a,b){this.audio_context=a;this.node_source=b;this.node_processor=null;this.node_gain=this.audio_context.createGain();this.node_gain.gain.setValueAtTime(0,this.audio_context.currentTime);this.node_gain.connect(this.audio_context.destination);this.is_active=!1;this.queued_history=[];this.output_history=[];this.queued=[[],[]];this.output=[[],[]]} +SpeakerDACDebugger.prototype.start=function(a){this.is_active=!0;this.queued=[[],[]];this.output=[[],[]];this.queued_history.push(this.queued);this.output_history.push(this.output);this.node_processor=this.audio_context.createScriptProcessor(1024,2,2);this.node_processor.onaudioprocess=b=>{this.output[0].push(b.inputBuffer.getChannelData(0).slice());this.output[1].push(b.inputBuffer.getChannelData(1).slice())};this.node_source.connect(this.node_processor);this.node_processor.connect(this.node_gain); +setTimeout(()=>{this.stop()},a)};SpeakerDACDebugger.prototype.stop=function(){this.is_active=!1;this.node_source.disconnect(this.node_processor);this.node_processor.disconnect();this.node_processor=null};SpeakerDACDebugger.prototype.push_queued_data=function(a){this.is_active&&(this.queued[0].push(a[0].slice()),this.queued[1].push(a[1].slice()))};SpeakerDACDebugger.prototype.download_txt=function(a,b){a=this.output_history[a][b].map(c=>c.join(" ")).join(" ");dump_file(a,"dacdata.txt")}; +SpeakerDACDebugger.prototype.download_csv=function(a){a=this.output_history[a];for(var b=[],c=0;cm?void 0===this.update_timer&&(this.update_timer=setTimeout(()=>{this.update_timer=void 0;var l=Date.now();dbg_assert(15<=l-this.last_update);this.last_update=l;this.render()},16-m)):(void 0!==this.update_timer&&(clearTimeout(this.update_timer),this.update_timer=void 0),this.last_update=k,this.render())};this.render=function(){a.value=this.text; +this.text_new_line&&(this.text_new_line=!1,a.scrollTop=1E9)};this.send_char=function(k){f.bus&&f.bus.send("serial0-input",k)}}function SerialRecordingAdapter(a){this.text="";a.register("serial0-output-char",function(b){this.text+=b},this)} +function SerialAdapterXtermJS(a,b){this.element=a;if(window.Terminal){var c=this.term=new window.Terminal;c.setOption("logLevel","off");c.write("This is the serial console. Whatever you type or paste here will be sent to COM1");var d=c.onData(function(e){for(let g=0;g a||(this.last_connect_attempt=Date.now(),this.socket=new WebSocket(this.url),this.socket.binaryType="arraybuffer",this.socket.onopen=this.handle_open.bind(this),this.socket.onmessage=this.handle_message.bind(this),this.socket.onclose=this.handle_close.bind(this),this.socket.onerror=this.handle_error.bind(this))}}; +NetworkAdapter.prototype.send=function(a){this.socket&&1===this.socket.readyState?this.socket.send(a):(this.send_queue.push(a),this.send_queue.length>2*this.send_queue_limit&&(this.send_queue=this.send_queue.slice(-this.send_queue_limit)),this.connect())};NetworkAdapter.prototype.change_proxy=function(a){this.url=a;this.socket&&(this.socket.onclose=function(){},this.socket.onerror=function(){},this.socket.close(),this.socket=void 0)};function V86Starter(a){this.cpu_is_running=!1;var b=Bus.create();this.bus=b[0];this.emulator_bus=b[1];var c,d;const e=new WebAssembly.Table({element:"anyfunc",initial:WASM_TABLE_SIZE+WASM_TABLE_OFFSET});b={cpu_exception_hook:f=>this.cpu_exception_hook&&this.cpu_exception_hook(f),hlt_op:function(){return c.hlt_op()},abort:function(){dbg_assert(!1)},microtick:v86.microtick,get_rand_int:function(){return v86util.get_rand_int()},pic_acknowledge:function(){c.pic_acknowledge()},io_port_read8:function(f){return c.io.port_read8(f)}, +io_port_read16:function(f){return c.io.port_read16(f)},io_port_read32:function(f){return c.io.port_read32(f)},io_port_write8:function(f,k){c.io.port_write8(f,k)},io_port_write16:function(f,k){c.io.port_write16(f,k)},io_port_write32:function(f,k){c.io.port_write32(f,k)},mmap_read8:function(f){return c.mmap_read8(f)},mmap_read16:function(f){return c.mmap_read16(f)},mmap_read32:function(f){return c.mmap_read32(f)},mmap_write8:function(f,k){c.mmap_write8(f,k)},mmap_write16:function(f,k){c.mmap_write16(f, +k)},mmap_write32:function(f,k){c.mmap_write32(f,k)},mmap_write64:function(f,k,m){c.mmap_write64(f,k,m)},mmap_write128:function(f,k,m,l,n){c.mmap_write128(f,k,m,l,n)},log_from_wasm:function(f,k){f=v86util.read_sized_string_from_mem(d,f,k);dbg_log(f,LOG_CPU)},console_log_from_wasm:function(f,k){f=v86util.read_sized_string_from_mem(d,f,k);console.error(f)},dbg_trace_from_wasm:function(){dbg_trace(LOG_CPU)},codegen_finalize:(f,k,m,l,n)=>{c.codegen_finalize(f,k,m,l,n)},jit_clear_func:f=>c.jit_clear_func(f), +jit_clear_all_funcs:()=>c.jit_clear_all_funcs(),__indirect_function_table:e};let g=a.wasm_fn;g||(g=f=>new Promise(k=>{let m=DEBUG?"v86-debug.wasm":"v86.wasm",l="v86-fallback.wasm";if(a.wasm_path){m=a.wasm_path;const n=m.lastIndexOf("/");l=(-1===n?"":m.substr(0,n))+"/"+l}else"undefined"===typeof window&&"string"===typeof __dirname?(m=__dirname+"/"+m,l=__dirname+"/"+l):(m="build/"+m,l="build/"+l);v86util.load_file(m,{done:async n=>{try{const {instance:p}=await WebAssembly.instantiate(n,f);k(p.exports)}catch(p){v86util.load_file(l, +{done:async t=>{({instance:t}=await WebAssembly.instantiate(t,f));k(t.exports)}})}},progress:n=>{this.emulator_bus.send("download-progress",{file_index:0,file_count:1,file_name:m,lengthComputable:n.lengthComputable,total:n.total,loaded:n.loaded})}})}));g({env:b}).then(f=>{d=f.memory;f.rust_init();f=this.v86=new v86(this.emulator_bus,{exports:f,wasm_table:e});c=f.cpu;this.continue_init(f,a)})} +V86Starter.prototype.continue_init=async function(a,b){function c(r,q){switch(r){case "hda":g.hda=this.disk_images.hda=q;break;case "hdb":g.hdb=this.disk_images.hdb=q;break;case "cdrom":g.cdrom=this.disk_images.cdrom=q;break;case "fda":g.fda=this.disk_images.fda=q;break;case "fdb":g.fdb=this.disk_images.fdb=q;break;case "multiboot":g.multiboot=this.disk_images.multiboot=q.buffer;break;case "bzimage":g.bzimage=this.disk_images.bzimage=q.buffer;break;case "initrd":g.initrd=this.disk_images.initrd=q.buffer; +break;case "bios":g.bios=q.buffer;break;case "vga_bios":g.vga_bios=q.buffer;break;case "initial_state":g.initial_state=q.buffer;break;case "fs9p_json":g.fs9p_json=q;break;default:dbg_assert(!1,r)}}function d(r,q){if(q)if(q.get&&q.set&&q.load)f.push({name:r,loadable:q});else{if("bios"===r||"vga_bios"===r||"initial_state"===r||"multiboot"===r||"bzimage"===r||"initrd"===r)q.async=!1;q.buffer instanceof ArrayBuffer?(q=new v86util.SyncBuffer(q.buffer),f.push({name:r,loadable:q})):"undefined"!==typeof File&& +q.buffer instanceof File?(void 0===q.async&&(q.async=268435456<=q.buffer.size),q=q.async?new v86util.AsyncFileBuffer(q.buffer):new v86util.SyncFileBuffer(q.buffer),f.push({name:r,loadable:q})):q.url?q.async?(q=q.use_parts?new v86util.AsyncXHRPartfileBuffer(q.url,q.size,q.fixed_chunk_size):new v86util.AsyncXHRBuffer(q.url,q.size,q.fixed_chunk_size),f.push({name:r,loadable:q})):f.push({name:r,url:q.url,size:q.size}):dbg_log("Ignored file: url="+q.url+" buffer="+q.buffer)}}async function e(){if(g.fs9p&& +g.fs9p_json){if(g.initial_state?dbg_log("Filesystem basefs ignored: Overridden by state image"):g.fs9p.load_from_json(g.fs9p_json),b.bzimage_initrd_from_filesystem){const {bzimage_path:r,initrd_path:q}=this.get_bzimage_initrd_from_filesystem(g.fs9p);dbg_log("Found bzimage: "+r+" and initrd: "+q);const [v,u]=await Promise.all([g.fs9p.read_file(q),g.fs9p.read_file(r)]);c.call(this,"initrd",new v86util.SyncBuffer(v.buffer));c.call(this,"bzimage",new v86util.SyncBuffer(u.buffer))}}else dbg_assert(!b.bzimage_initrd_from_filesystem, +"bzimage_initrd_from_filesystem: Requires a filesystem");this.serial_adapter&&this.serial_adapter.show&&this.serial_adapter.show();this.bus.send("cpu-init",g);g.initial_state&&(a.restore_state(g.initial_state),g.initial_state=void 0);b.autostart&&this.bus.send("cpu-run");this.emulator_bus.send("emulator-loaded")}this.bus.register("emulator-stopped",function(){this.cpu_is_running=!1},this);this.bus.register("emulator-started",function(){this.cpu_is_running=!0},this);var g={};this.disk_images={fda:void 0, +fdb:void 0,hda:void 0,hdb:void 0,cdrom:void 0};g.acpi=b.acpi;g.load_devices=!0;g.log_level=b.log_level;g.memory_size=b.memory_size||67108864;g.vga_memory_size=b.vga_memory_size||8388608;g.boot_order=b.boot_order||531;g.fastboot=b.fastboot||!1;g.fda=void 0;g.fdb=void 0;g.uart1=b.uart1;g.uart2=b.uart2;g.uart3=b.uart3;g.cmdline=b.cmdline;g.preserve_mac_from_state_image=b.preserve_mac_from_state_image;g.mac_address_translation=b.mac_address_translation;g.cpuid_level=b.cpuid_level;b.network_adapter?this.network_adapter= +b.network_adapter(this.bus):b.network_relay_url&&(this.network_adapter=new NetworkAdapter(b.network_relay_url,this.bus));g.enable_ne2k=!0;b.disable_keyboard||(this.keyboard_adapter=new KeyboardAdapter(this.bus));b.disable_mouse||(this.mouse_adapter=new MouseAdapter(this.bus,b.screen_container));b.screen_container?this.screen_adapter=new ScreenAdapter(b.screen_container,this.bus):b.screen_dummy&&(this.screen_adapter=new DummyScreenAdapter(this.bus));b.serial_container&&(this.serial_adapter=new SerialAdapter(b.serial_container, +this.bus));b.serial_container_xtermjs&&(this.serial_adapter=new SerialAdapterXtermJS(b.serial_container_xtermjs,this.bus));b.disable_speaker||(this.speaker_adapter=new SpeakerAdapter(this.bus));var f=[];b.state&&console.warn("Warning: Unknown option 'state'. Did you mean 'initial_state'?");for(var k="bios vga_bios cdrom hda hdb fda fdb initial_state multiboot bzimage initrd".split(" "),m=0;m "/"+e);a=(a.read_dir("/boot/")||[]).map(e=>"/boot/"+e);let c,d;for(let e of[].concat(b,a)){const g=/old/i.test(e)||/fallback/i.test(e),f=/vmlinuz/i.test(e)||/bzimage/i.test(e),k=/initrd/i.test(e)||/initramfs/i.test(e);!f||d&&g||(d=e);!k||c&&g||(c=e)}c&&d||(console.log("Failed to find bzimage or initrd in filesystem. Files:"),console.log(b.join(" ")),console.log(a.join(" ")));return{initrd_path:c, +bzimage_path:d}};V86Starter.prototype.run=async function(){this.bus.send("cpu-run")};goog.exportProperty(V86Starter.prototype,"run",V86Starter.prototype.run);V86Starter.prototype.stop=async function(){this.cpu_is_running&&await new Promise(a=>{const b=()=>{this.remove_listener("emulator-stopped",b);a()};this.add_listener("emulator-stopped",b);this.bus.send("cpu-stop")})};goog.exportProperty(V86Starter.prototype,"stop",V86Starter.prototype.stop); +V86Starter.prototype.destroy=async function(){await this.stop();this.v86.destroy();this.keyboard_adapter&&this.keyboard_adapter.destroy();this.network_adapter&&this.network_adapter.destroy();this.mouse_adapter&&this.mouse_adapter.destroy();this.screen_adapter&&this.screen_adapter.destroy();this.serial_adapter&&this.serial_adapter.destroy();this.speaker_adapter&&this.speaker_adapter.destroy()};goog.exportProperty(V86Starter.prototype,"destroy",V86Starter.prototype.destroy); +V86Starter.prototype.restart=function(){this.bus.send("cpu-restart")};goog.exportProperty(V86Starter.prototype,"restart",V86Starter.prototype.restart);V86Starter.prototype.add_listener=function(a,b){this.bus.register(a,b,this)};goog.exportProperty(V86Starter.prototype,"add_listener",V86Starter.prototype.add_listener);V86Starter.prototype.remove_listener=function(a,b){this.bus.unregister(a,b)};goog.exportProperty(V86Starter.prototype,"remove_listener",V86Starter.prototype.remove_listener); +V86Starter.prototype.restore_state=async function(a){dbg_assert(1===arguments.length);this.v86.restore_state(a)};goog.exportProperty(V86Starter.prototype,"restore_state",V86Starter.prototype.restore_state);V86Starter.prototype.save_state=async function(){dbg_assert(0===arguments.length);return this.v86.save_state()};goog.exportProperty(V86Starter.prototype,"save_state",V86Starter.prototype.save_state); +V86Starter.prototype.get_statistics=function(){console.warn("V86Starter.prototype.get_statistics is deprecated. Use events instead.");var a={cpu:{instruction_counter:this.get_instruction_counter()}};if(!this.v86)return a;var b=this.v86.cpu.devices;b.hda&&(a.hda=b.hda.stats);b.cdrom&&(a.cdrom=b.cdrom.stats);b.ps2&&(a.mouse={enabled:b.ps2.use_mouse});b.vga&&(a.vga={is_graphical:b.vga.stats.is_graphical});return a};goog.exportProperty(V86Starter.prototype,"get_statistics",V86Starter.prototype.get_statistics); +V86Starter.prototype.get_instruction_counter=function(){return this.v86?this.v86.cpu.instruction_counter[0]>>>0:0};goog.exportProperty(V86Starter.prototype,"get_instruction_counter",V86Starter.prototype.get_instruction_counter);V86Starter.prototype.is_running=function(){return this.cpu_is_running};goog.exportProperty(V86Starter.prototype,"is_running",V86Starter.prototype.is_running);V86Starter.prototype.keyboard_send_scancodes=function(a){for(var b=0;b {const k=this.fs9p.Mount(a,g);d&&(k===-ENOENT?d(new FileNotFoundError):k===-EEXIST?d(new FileExistsError):0>k?(dbg_assert(!1,"Unexpected error code: "+-k),d(Error("Failed to mount. Error number: "+-k))):d(null))};b?(dbg_assert("object"===typeof c,"Filesystem: basefs must be a JSON object"),g.load_from_json(c,()=>f())):f()}; +goog.exportProperty(V86Starter.prototype,"mount_fs",V86Starter.prototype.mount_fs);V86Starter.prototype.create_file=async function(a,b){dbg_assert(2===arguments.length);var c=this.fs9p;if(c){var d=a.split("/");d=d[d.length-1];var e=c.SearchPath(a).parentid;if(""!==d&&-1!==e)await c.CreateBinaryFile(d,e,b);else return Promise.reject(new FileNotFoundError)}};goog.exportProperty(V86Starter.prototype,"create_file",V86Starter.prototype.create_file); +V86Starter.prototype.read_file=async function(a){dbg_assert(1===arguments.length);var b=this.fs9p;if(b)return(b=await b.read_file(a))?b:Promise.reject(new FileNotFoundError)};goog.exportProperty(V86Starter.prototype,"read_file",V86Starter.prototype.read_file); +V86Starter.prototype.automatically=function(a){const b=c=>{const d=c[0];if(d){var e=c.slice(1);if(d.sleep)setTimeout(()=>b(e),1E3*d.sleep);else if(d.vga_text){const g=this.screen_adapter.get_text_screen();for(let f of g)if(f.includes(d.vga_text)){b(e);return}setTimeout(()=>b(c),1E3)}else d.keyboard_send?(d.keyboard_send instanceof Array?this.keyboard_send_scancodes(d.keyboard_send):(dbg_assert("string"===typeof d.keyboard_send),this.keyboard_send_text(d.keyboard_send)),b(e)):d.call?(d.call(),b(e)): +dbg_assert(!1,d)}};b(a)};V86Starter.prototype.read_memory=function(a,b){return this.v86.cpu.read_blob(a,b)};V86Starter.prototype.write_memory=function(a,b){this.v86.cpu.write_blob(a,b)};function FileExistsError(a){this.message=a||"File already exists"}FileExistsError.prototype=Error.prototype;function FileNotFoundError(a){this.message=a||"File not found"}FileNotFoundError.prototype=Error.prototype; +"undefined"!==typeof window?(window.V86Starter=V86Starter,window.V86=V86Starter):"undefined"!==typeof module&&"undefined"!==typeof module.exports?(module.exports.V86Starter=V86Starter,module.exports.V86=V86Starter):"function"===typeof importScripts&&(self.V86Starter=V86Starter,self.V86=V86Starter);var WorkerBus={Connector:function(a){this.listeners={};this.pair=a;a.addEventListener("message",function(b){b=b.data;for(var c=this.listeners[b[0]],d=0;d >20)+"m\n";b=b+"Config:\nMAX_PAGES="+(a.wm.exports.get_jit_config(0)+"\n");b+="JIT_USE_LOOP_SAFETY="+!!a.wm.exports.get_jit_config(1)+"\n";return b+="MAX_EXTRA_BASIC_BLOCKS="+a.wm.exports.get_jit_config(2)+"\n"},print_instruction_counts:function(a){return[print_stats.print_instruction_counts_offset(a, +!1,!1,!1,!1),print_stats.print_instruction_counts_offset(a,!0,!1,!1,!1),print_stats.print_instruction_counts_offset(a,!1,!0,!1,!1),print_stats.print_instruction_counts_offset(a,!1,!1,!0,!1),print_stats.print_instruction_counts_offset(a,!1,!1,!1,!0)].join("\n\n")},print_instruction_counts_offset:function(a,b,c,d,e){let g="";var f=[],k=b?"compiled":c?"jit exit":d?"unguarded register":e?"wasm size":"executed";for(let n=0;256>n;n++)for(let p=0;8>p;p++)for(let t of[!1,!0]){var m=a.wm.exports.get_opstats_buffer(b, +c,d,e,n,!1,t,p);f.push({opcode:n,count:m,is_mem:t,fixed_g:p});m=a.wm.exports.get_opstats_buffer(b,c,d,e,n,!0,t,p);f.push({opcode:3840|n,count:m,is_mem:t,fixed_g:p})}a=0;b=new Set([38,46,54,62,100,101,102,103,240,242,243]);for(let {count:n,opcode:p}of f)b.has(p)||(a+=n);if(0===a)return"";c=new Uint32Array(256);b=new Uint32Array(256);for(let {opcode:n,count:p}of f)3840==(n&65280)?b[n&255]+=p:c[n&255]+=p;g=g+"------------------\nTotal: "+(a+"\n");const l=1E7 +Math.round(n/l)));d=String(d).length;g+=`Instruction counts ${k} (in ${l}):\n`;for(e=0;256>e;e++)g+=e.toString(16).padStart(2,"0")+":"+v86util.pads(Math.round(c[e]/l),d),g=15==e%16?g+"\n":g+" ";g=g+"\n"+`Instruction counts ${k} (0f, in ${l}):\n`;for(k=0;256>k;k++)g+=(k&255).toString(16).padStart(2,"0")+":"+v86util.pads(Math.round(b[k]/l),d),g=15==k%16?g+"\n":g+" ";g+="\n";f=f.filter(({count:n})=>n).sort(({count:n},{count:p})=>p-n);for(let {opcode:n,is_mem:p,fixed_g:t,count:r}of f.slice(0,200))f=n.toString(16)+ +"_"+t+(p?"_m":"_r"),g+=f+":"+(r/a*100).toFixed(2)+" ";return g+"\n"}};"undefined"!==typeof module&&"undefined"!==typeof module.exports&&(module.exports.print_stats=print_stats);function FileStorageInterface(){}FileStorageInterface.prototype.read=function(a,b,c){};FileStorageInterface.prototype.cache=function(a,b){};FileStorageInterface.prototype.uncache=function(a){};function MemoryFileStorage(){this.filedata=new Map}MemoryFileStorage.prototype.read=async function(a,b,c){dbg_assert(a,"MemoryFileStorage read: sha256sum should be a non-empty string");return(a=this.filedata.get(a))?a.subarray(b,b+c):null}; +MemoryFileStorage.prototype.cache=async function(a,b){dbg_assert(a,"MemoryFileStorage cache: sha256sum should be a non-empty string");this.filedata.set(a,b)};MemoryFileStorage.prototype.uncache=function(a){this.filedata.delete(a)};function ServerFileStorageWrapper(a,b){dbg_assert(b,"ServerMemoryFileStorage: baseurl should not be empty");this.storage=a;this.baseurl=b} +ServerFileStorageWrapper.prototype.load_from_server=function(a){return new Promise((b,c)=>{v86util.load_file(this.baseurl+a,{done:async d=>{d=new Uint8Array(d);await this.cache(a,d);b(d)}})})};ServerFileStorageWrapper.prototype.read=async function(a,b,c){const d=await this.storage.read(a,b,c);return d?d:(await this.load_from_server(a)).subarray(b,b+c)};ServerFileStorageWrapper.prototype.cache=async function(a,b){return await this.storage.cache(a,b)};ServerFileStorageWrapper.prototype.uncache=function(a){this.storage.uncache(a)}; +"undefined"!==typeof window?(window.MemoryFileStorage=MemoryFileStorage,window.ServerFileStorageWrapper=ServerFileStorageWrapper):"undefined"!==typeof module&&"undefined"!==typeof module.exports?(module.exports.MemoryFileStorage=MemoryFileStorage,module.exports.ServerFileStorageWrapper=ServerFileStorageWrapper):"function"===typeof importScripts&&(self.MemoryFileStorage=MemoryFileStorage,self.ServerFileStorageWrapper=ServerFileStorageWrapper);var S_IRWXUGO=511,S_IFMT=61440,S_IFSOCK=49152,S_IFLNK=40960,S_IFREG=32768,S_IFBLK=24576,S_IFDIR=16384,S_IFCHR=8192,O_RDONLY=0,O_WRONLY=1,O_RDWR=2,O_ACCMODE=3,STATUS_INVALID=-1,STATUS_OK=0,STATUS_ON_STORAGE=2,STATUS_UNLINKED=4,STATUS_FORWARDING=5,JSONFS_VERSION=3,JSONFS_IDX_NAME=0,JSONFS_IDX_SIZE=1,JSONFS_IDX_MTIME=2,JSONFS_IDX_MODE=3,JSONFS_IDX_UID=4,JSONFS_IDX_GID=5,JSONFS_IDX_TARGET=6,JSONFS_IDX_SHA256=6; +function FS(a,b){this.inodes=[];this.events=[];this.storage=a;this.qidcounter=b||{last_qidnumber:0};this.inodedata={};this.total_size=274877906944;this.used_size=0;this.mounts=[];this.CreateDirectory("",-1)}FS.prototype.get_state=function(){let a=[];a[0]=this.inodes;a[1]=this.qidcounter.last_qidnumber;a[2]=[];for(const [b,c]of Object.entries(this.inodedata))0===(this.inodes[b].mode&S_IFDIR)&&a[2].push([b,c]);a[3]=this.total_size;a[4]=this.used_size;return a=a.concat(this.mounts)}; +FS.prototype.set_state=function(a){this.inodes=a[0].map(b=>{const c=new Inode(0);c.set_state(b);return c});this.qidcounter.last_qidnumber=a[1];this.inodedata={};for(let [b,c]of a[2])c.buffer.byteLength!==c.byteLength&&(c=c.slice()),this.inodedata[b]=c;this.total_size=a[3];this.used_size=a[4];this.mounts=a.slice(5)}; +FS.prototype.AddEvent=function(a,b){var c=this.inodes[a];c.status==STATUS_OK||c.status==STATUS_ON_STORAGE?b():this.is_forwarder(c)?this.follow_fs(c).AddEvent(c.foreign_id,b):this.events.push({id:a,OnEvent:b})};FS.prototype.HandleEvent=function(a){var b=this.inodes[a];this.is_forwarder(b)&&this.follow_fs(b).HandleEvent(b.foreign_id);b=[];for(var c=0;c >8;this.qid.version=a[11];this.qid.path=a[12];this.nlinks=a[13]}; +FS.prototype.divert=function(a,b){const c=this.Search(a,b),d=this.inodes[c],e=new Inode(-1);dbg_assert(d,"Filesystem divert: name ("+b+") not found");dbg_assert(this.IsDirectory(c)||1>=d.nlinks,"Filesystem: can't divert hardlinked file '"+b+"' with nlinks="+d.nlinks);Object.assign(e,d);const g=this.inodes.length;this.inodes.push(e);e.fid=g;this.is_forwarder(d)&&this.mounts[d.mount_id].backtrack.set(d.foreign_id,g);this.should_be_linked(d)&&(this.unlink_from_dir(a,b),this.link_under_dir(a,g,b));if(this.IsDirectory(c)&& +!this.is_forwarder(d))for(const [f,k]of e.direntries)"."!==f&&".."!==f&&this.IsDirectory(k)&&this.inodes[k].direntries.set("..",g);this.inodedata[g]=this.inodedata[c];delete this.inodedata[c];d.direntries=new Map;d.nlinks=0;return g};FS.prototype.copy_inode=function(a,b){Object.assign(b,a,{fid:b.fid,direntries:b.direntries,nlinks:b.nlinks})};FS.prototype.CreateInode=function(){const a=Math.round(Date.now()/1E3),b=new Inode(++this.qidcounter.last_qidnumber);b.atime=b.ctime=b.mtime=a;return b}; +FS.prototype.CreateDirectory=function(a,b){var c=this.inodes[b];if(0<=b&&this.is_forwarder(c))return b=c.foreign_id,a=this.follow_fs(c).CreateDirectory(a,b),this.create_forwarder(c.mount_id,a);c=this.CreateInode();c.mode=511|S_IFDIR;0<=b&&(c.uid=this.inodes[b].uid,c.gid=this.inodes[b].gid,c.mode=this.inodes[b].mode&511|S_IFDIR);c.qid.type=S_IFDIR>>8;this.PushInode(c,b,a);this.NotifyListeners(this.inodes.length-1,"newdir");return this.inodes.length-1}; +FS.prototype.CreateFile=function(a,b){var c=this.inodes[b];if(this.is_forwarder(c))return b=c.foreign_id,a=this.follow_fs(c).CreateFile(a,b),this.create_forwarder(c.mount_id,a);c=this.CreateInode();c.uid=this.inodes[b].uid;c.gid=this.inodes[b].gid;c.qid.type=S_IFREG>>8;c.mode=this.inodes[b].mode&438|S_IFREG;this.PushInode(c,b,a);this.NotifyListeners(this.inodes.length-1,"newfile");return this.inodes.length-1}; +FS.prototype.CreateNode=function(a,b,c,d){var e=this.inodes[b];if(this.is_forwarder(e))return b=e.foreign_id,a=this.follow_fs(e).CreateNode(a,b,c,d),this.create_forwarder(e.mount_id,a);e=this.CreateInode();e.major=c;e.minor=d;e.uid=this.inodes[b].uid;e.gid=this.inodes[b].gid;e.qid.type=S_IFSOCK>>8;e.mode=this.inodes[b].mode&438;this.PushInode(e,b,a);return this.inodes.length-1}; +FS.prototype.CreateSymlink=function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=d.foreign_id,a=this.follow_fs(d).CreateSymlink(a,b,c),this.create_forwarder(d.mount_id,a);d=this.CreateInode();d.uid=this.inodes[b].uid;d.gid=this.inodes[b].gid;d.qid.type=S_IFLNK>>8;d.symlink=c;d.mode=S_IFLNK;this.PushInode(d,b,a);return this.inodes.length-1}; +FS.prototype.CreateTextFile=async function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=d.foreign_id,c=await this.follow_fs(d).CreateTextFile(a,b,c),this.create_forwarder(d.mount_id,c);d=this.CreateFile(a,b);b=this.inodes[d];a=new Uint8Array(c.length);b.size=c.length;for(b=0;b f)return f}var k=this.inodes[e],m=this.inodes[a];f=this.inodes[c];if(this.is_forwarder(m)||this.is_forwarder(f))if(this.is_forwarder(m)&&m.mount_id===f.mount_id){if(a=await this.follow_fs(m).Rename(m.foreign_id,b,f.foreign_id,d),0>a)return a}else{if(this.is_a_root(e))return dbg_log("XXX: Attempted to move mountpoint ("+ +b+") - skipped",LOG_9P),-EPERM;if(!this.IsDirectory(e)&&1 f)return f;await this.DeleteData(m);a=this.Unlink(a,b);if(0>a)return a}else this.unlink_from_dir(a,b),this.link_under_dir(c,e,d),k.qid.version++;this.NotifyListeners(e,"rename",{oldpath:g});return 0}; +FS.prototype.Write=async function(a,b,c,d){this.NotifyListeners(a,"write");var e=this.inodes[a];if(this.is_forwarder(e))a=e.foreign_id,await this.follow_fs(e).Write(a,b,c,d);else{var g=await this.get_buffer(a);!g||g.lengthb.nlinks&&message.Debug("Error in filesystem: negative nlinks="+b.nlinks+" at id ="+a);if(this.IsDirectory(a)){b=this.GetInode(a);this.IsDirectory(a)&&0>this.GetParent(a)&&message.Debug("Error in filesystem: negative parent id "+a);for(const [c,d]of b.direntries){0===c.length&&message.Debug("Error in filesystem: inode with no name and id "+d);for(const e of c)32>e&& +message.Debug("Error in filesystem: Unallowed char in filename")}}}};FS.prototype.FillDirectory=function(a){var b=this.inodes[a];if(this.is_forwarder(b))this.follow_fs(b).FillDirectory(b.foreign_id);else{var c=0;for(const d of b.direntries.keys())c+=24+UTF8.UTF8Length(d);a=this.inodedata[a]=new Uint8Array(c);b.size=c;c=0;for(const [d,e]of b.direntries)b=this.GetInode(e),c+=marshall.Marshall(["Q","d","b","s"],[b.qid,c+13+8+1+2+UTF8.UTF8Length(d),b.mode>>12,d],a,c)}}; +FS.prototype.RoundToDirentry=function(a,b){const c=this.inodedata[a];dbg_assert(c,`FS directory data for dirid=${a} should be generated`);dbg_assert(c.length,"FS directory should have at least an entry");if(b>=c.length)return c.length;for(a=0;;){const d=marshall.Unmarshall(["Q","d"],c,{offset:a})[1];if(d>b)break;a=d}return a};FS.prototype.IsDirectory=function(a){a=this.inodes[a];return this.is_forwarder(a)?this.follow_fs(a).IsDirectory(a.foreign_id):(a.mode&S_IFMT)===S_IFDIR}; +FS.prototype.IsEmpty=function(a){a=this.inodes[a];if(this.is_forwarder(a))return this.follow_fs(a).IsDirectory(a.foreign_id);for(const b of a.direntries.keys())if("."!==b&&".."!==b)return!1;return!0};FS.prototype.GetChildren=function(a){dbg_assert(this.IsDirectory(a),"Filesystem: cannot get children of non-directory inode");a=this.inodes[a];if(this.is_forwarder(a))return this.follow_fs(a).GetChildren(a.foreign_id);const b=[];for(const c of a.direntries.keys())"."!==c&&".."!==c&&b.push(c);return b}; +FS.prototype.GetParent=function(a){dbg_assert(this.IsDirectory(a),"Filesystem: cannot get parent of non-directory inode");a=this.inodes[a];if(this.should_be_linked(a))return a.direntries.get("..");const b=this.follow_fs(a).GetParent(a.foreign_id);dbg_assert(-1!==b,"Filesystem: should not have invalid parent ids");return this.get_forwarder(a.mount_id,b)}; +FS.prototype.PrepareCAPs=function(a){a=this.GetInode(a);if(a.caps)return a.caps.length;a.caps=new Uint8Array(20);a.caps[0]=0;a.caps[1]=0;a.caps[2]=0;a.caps[3]=2;a.caps[4]=255;a.caps[5]=255;a.caps[6]=255;a.caps[7]=255;a.caps[8]=255;a.caps[9]=255;a.caps[10]=255;a.caps[11]=255;a.caps[12]=63;a.caps[13]=0;a.caps[14]=0;a.caps[15]=0;a.caps[16]=63;a.caps[17]=0;a.caps[18]=0;a.caps[19]=0;return a.caps.length};function FSMountInfo(a){this.fs=a;this.backtrack=new Map} +FSMountInfo.prototype.get_state=function(){const a=[];a[0]=this.fs;a[1]=[...this.backtrack];return a};FSMountInfo.prototype.set_state=function(a){this.fs=a[0];this.backtrack=new Map(a[1])}; +FS.prototype.set_forwarder=function(a,b,c){const d=this.inodes[a];dbg_assert(0===d.nlinks,"Filesystem: attempted to convert an inode into forwarder before unlinking the inode");this.is_forwarder(d)&&this.mounts[d.mount_id].backtrack.delete(d.foreign_id);d.status=STATUS_FORWARDING;d.mount_id=b;d.foreign_id=c;this.mounts[b].backtrack.set(c,a)};FS.prototype.create_forwarder=function(a,b){const c=this.CreateInode(),d=this.inodes.length;this.inodes.push(c);c.fid=d;this.set_forwarder(d,a,b);return d}; +FS.prototype.is_forwarder=function(a){return a.status===STATUS_FORWARDING};FS.prototype.is_a_root=function(a){return 0===this.GetInode(a).fid};FS.prototype.get_forwarder=function(a,b){var c=this.mounts[a];dbg_assert(0<=b,"Filesystem get_forwarder: invalid foreign_id: "+b);dbg_assert(c,"Filesystem get_forwarder: invalid mount number: "+a);c=c.backtrack.get(b);return void 0===c?this.create_forwarder(a,b):c}; +FS.prototype.delete_forwarder=function(a){dbg_assert(this.is_forwarder(a),"Filesystem delete_forwarder: expected forwarder");a.status=STATUS_INVALID;this.mounts[a.mount_id].backtrack.delete(a.foreign_id)};FS.prototype.follow_fs=function(a){const b=this.mounts[a.mount_id];dbg_assert(this.is_forwarder(a),"Filesystem follow_fs: inode should be a forwarding inode");dbg_assert(b,"Filesystem follow_fs: inode should point to valid mounted FS");return b.fs}; +FS.prototype.Mount=function(a,b){dbg_assert(b.qidcounter===this.qidcounter,"Cannot mount filesystem whose qid numbers aren't synchronised with current filesystem.");var c=this.SearchPath(a);if(-1===c.parentid)return dbg_log("Mount failed: parent for path not found: "+a,LOG_9P),-ENOENT;if(-1!==c.id)return dbg_log("Mount failed: file already exists at path: "+a,LOG_9P),-EEXIST;if(c.forward_path)return a=this.inodes[c.parentid],c=this.follow_fs(a).Mount(c.forward_path,b),0>c?c:this.get_forwarder(a.mount_id, +c);a=this.mounts.length;this.mounts.push(new FSMountInfo(b));b=this.create_forwarder(a,0);this.link_under_dir(c.parentid,b,c.name);return b};function FSLockRegion(){this.type=P9_LOCK_TYPE_UNLCK;this.start=0;this.length=Infinity;this.proc_id=-1;this.client_id=""}FSLockRegion.prototype.get_state=function(){const a=[];a[0]=this.type;a[1]=this.start;a[2]=Infinity===this.length?0:this.length;a[3]=this.proc_id;a[4]=this.client_id;return a}; +FSLockRegion.prototype.set_state=function(a){this.type=a[0];this.start=a[1];this.length=0===a[2]?Infinity:a[2];this.proc_id=a[3];this.client_id=a[4]};FSLockRegion.prototype.clone=function(){const a=new FSLockRegion;a.set_state(this.get_state());return a}; +FSLockRegion.prototype.conflicts_with=function(a){return this.proc_id===a.proc_id&&this.client_id===a.client_id||this.type===P9_LOCK_TYPE_UNLCK||a.type===P9_LOCK_TYPE_UNLCK||this.type!==P9_LOCK_TYPE_WRLCK&&a.type!==P9_LOCK_TYPE_WRLCK||this.start+this.length<=a.start||a.start+a.length<=this.start?!1:!0};FSLockRegion.prototype.is_alike=function(a){return a.proc_id===this.proc_id&&a.client_id===this.client_id&&a.type===this.type}; +FSLockRegion.prototype.may_merge_after=function(a){return this.is_alike(a)&&a.start+a.length===this.start};FS.prototype.DescribeLock=function(a,b,c,d,e){dbg_assert(a===P9_LOCK_TYPE_RDLCK||a===P9_LOCK_TYPE_WRLCK||a===P9_LOCK_TYPE_UNLCK,"Filesystem: Invalid lock type: "+a);dbg_assert(0<=b,"Filesystem: Invalid negative lock starting offset: "+b);dbg_assert(0 =g&&0 =g&&(d.locks.splice(c,1),c--)}if(b.type!==P9_LOCK_TYPE_UNLCK){c=b;a=!1;for(e=0;e "."!==b&&".."!==b)};FS.prototype.read_file=function(a){a=this.SearchPath(a);if(-1===a.id)return Promise.resolve(null);const b=this.GetInode(a.id);return this.Read(a.id,0,b.size)};var VIRTIO_MAGIC_REG=0,VIRTIO_VERSION_REG=4,VIRTIO_DEVICE_REG=8,VIRTIO_VENDOR_REG=12,VIRTIO_HOSTFEATURES_REG=16,VIRTIO_HOSTFEATURESSEL_REG=20,VIRTIO_GUESTFEATURES_REG=32,VIRTIO_GUESTFEATURESSEL_REG=36,VIRTIO_GUEST_PAGE_SIZE_REG=40,VIRTIO_QUEUESEL_REG=48,VIRTIO_QUEUENUMMAX_REG=52,VIRTIO_QUEUENUM_REG=56,VIRTIO_QUEUEALIGN_REG=60,VIRTIO_QUEUEPFN_REG=64,VIRTIO_QUEUENOTIFY_REG=80,VIRTIO_INTERRUPTSTATUS_REG=96,VIRTIO_INTERRUPTACK_REG=100,VIRTIO_STATUS_REG=112,VRING_DESC_F_NEXT=1,VRING_DESC_F_WRITE=2,VRING_DESC_F_INDIRECT= +4;function hex8(a){return h(a)}var message={Debug:function(a){dbg_log([].slice.apply(arguments).join(" "),LOG_9P)},Abort:function(){if(DEBUG)throw Error("message.Abort()");}},LoadBinaryResource; +LoadBinaryResource="undefined"!==typeof XMLHttpRequest?function(a,b,c){var d=new XMLHttpRequest;d.open("GET",a,!0);d.responseType="arraybuffer";d.onreadystatechange=function(){if(4==d.readyState)if(200!=d.status&&0!=d.status)c("Error: Could not load file "+a);else{var e=d.response;e?b(e):c("Error: No data received from: "+a)}};d.send(null)}:function(a,b,c){require("fs").readFile(a,function(d,e){d?c(d):b(e.buffer)})};var marshall={Marshall:function(a,b,c,d){for(var e,g=0,f=0;f >8&255;c[d++]=e>>16&255;c[d++]=e>>24&255;g+=4;break;case "d":c[d++]=e&255;c[d++]=e>>8&255;c[d++]=e>>16&255;c[d++]=e>>24&255;c[d++]=0;c[d++]=0;c[d++]=0;c[d++]=0;g+=8;break;case "h":c[d++]=e&255;c[d++]=e>>8;g+=2;break;case "b":c[d++]=e;g+=1;break;case "s":var k=d,m=0;c[d++]=0;c[d++]=0;g+=2;for(var l of e)UnicodeToUTF8Stream(l.charCodeAt(0)).forEach(function(n){c[d++]=n;g+=1;m++}); +c[k+0]=m&255;c[k+1]=m>>8&255;break;case "Q":marshall.Marshall(["b","w","d"],[e.type,e.version,e.path],c,d);d+=13;g+=13;break;default:message.Debug("Marshall: Unknown type="+a[f])}return g},Unmarshall:function(a,b,c){let d=c.offset;for(var e=[],g=0;g >>0;e.push(f);break;case "d":f=b[d++];f+=b[d++]<<8;f+=b[d++]<<16;f+=b[d++]<<24>>>0;d+=4;e.push(f);break;case "h":f=b[d++];e.push(f+(b[d++]<<8));break;case "b":e.push(b[d++]); +break;case "s":f=b[d++];f+=b[d++]<<8;for(var k="",m=new UTF8StreamToUnicode,l=0;l { + var output = ""; + var listener = (char) => { + if (char !== "\r") { + output += char; + } + + if (output.endsWith("# ")) { + emulator.remove_listener("serial0-output-char", listener); + let outputWithoutPrompt = output.slice(0, -4); + let outputWithoutFirstLine = outputWithoutPrompt.slice( + outputWithoutPrompt.indexOf("\n") + 1 + ); + if (outputWithoutFirstLine.endsWith("\n")) { + outputWithoutFirstLine = outputWithoutFirstLine.slice(0, -1); + } + emulator.remove_listener("serial0-output-char", listener); + resolve(outputWithoutFirstLine); + } + }; + emulator.add_listener("serial0-output-char", listener); + }); +} +window.run_in_vm = run_in_vm; +window.web_shell = { run_in_vm: run_in_vm }; + +/* + +// Run a test command and return true if the exit code is 0, false otherwise. +async function test(condition) { + let result = await run(`test ${condition} && echo 'yes' || echo 'no'`) + return result == "yes" +} + +*/ + +// Set emulator config. +let config = { + wasm_path: "web-shell/lib/v86.wasm", + memory_size: 64 * 1024 * 1024, + vga_memory_size: 2 * 1024 * 1024, + screen_container: document.getElementById("screen_container"), + bios: { url: "web-shell/images/seabios.bin" }, + vga_bios: { url: "web-shell/images/vgabios.bin" }, + cdrom: { url: "web-shell/images/image.iso.zst" }, + disable_mouse: true, + autostart: true, +}; +if (restoreState) { + config.initial_state = { + url: "web-shell/images/booted-state.bin.zst", + }; +} + +function boot() { + return new Promise((resolve, reject) => { + // Start the emulator! + emulator = window.emulator = new V86Starter(config); + + // Wait for the emulator to start, then resolve the promise. + var interval = setInterval(() => { + if (emulator.is_running()) { + clearInterval(interval); + resolve(); + } + }, 100); + }); +} + +/* + +// Allow saving and restoring the state using the buttons below the console. +var state +document.getElementById("save_restore").onclick = async function () { + var button = this + + if (state) { + button.value = "Save state" + await emulator.restore_state(state) + state = undefined + } else { + const new_state = await emulator.save_state() + console.log("Saved state of " + new_state.byteLength + " bytes") + button.value = "Restore state" + state = new_state + } + + button.blur() +} +document.getElementById("save_file").onclick = async function () { + const new_state = await emulator.save_state() + var a = document.createElement("a") + a.download = "v86state.bin" + a.href = window.URL.createObjectURL(new Blob([new_state])) + a.dataset.downloadurl = + "application/octet-stream:" + a.download + ":" + a.href + a.click() + + this.blur() +} +document.getElementById("restore_file").onchange = function () { + if (this.files.length) { + var filereader = new FileReader() + emulator.stop() + + filereader.onload = async function (e) { + await emulator.restore_state(e.target.result) + emulator.run() + } + + filereader.readAsArrayBuffer(this.files[0]) + + this.value = "" + } + + this.blur() +} + +*/