Start working on refactoring web-shell

This commit is contained in:
blinry 2023-09-07 17:46:58 +02:00
parent 8362b9d9b2
commit 4b758e6bca
4 changed files with 93 additions and 56 deletions

View file

@ -0,0 +1,3 @@
tabWidth: 4
semi: false
bracketSpacing: false

8
web/web-shell/index.html Normal file
View file

@ -0,0 +1,8 @@
<script src="./lib/libv86.js"></script>
<script src="./script.js"></script>
<script>
;(async () => {
await boot()
console.log("Booted!")
})()
</script>

24
web/web-shell/mutex.js Normal file
View file

@ -0,0 +1,24 @@
export class Mutex {
private mutex = Promise.resolve()
lock(): PromiseLike<() => void> {
let begin: (unlock: () => void) => void = (unlock) => {}
this.mutex = this.mutex.then(() => {
return new Promise(begin)
})
return new Promise((res) => {
begin = res
})
}
async dispatch(fn) {
const unlock = await this.lock()
try {
return await Promise.resolve(fn())
} finally {
unlock()
}
}
}

View file

@ -1,46 +1,48 @@
var emulator; //import {Mutex} from "./mutex.js"
var emulator
// Whether or not to restore the VM state from a file. Set to false to perform a regular boot. // Whether or not to restore the VM state from a file. Set to false to perform a regular boot.
let restoreState = true; let restoreState = true
function testy(cmd) { function testy(cmd) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
resolve("testy!!" + cmd); resolve("testy!!" + cmd)
}, 100); }, 100)
}); })
} }
window.testy = testy; window["testy"] = testy
// Run a command via the serial port (/dev/ttyS0) and return the output. // Run a command via the serial port (/dev/ttyS0) and return the output.
function run_in_vm(cmd) { function run(cmd) {
emulator.serial0_send(cmd + "\n"); emulator.serial0_send(cmd + "\n")
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var output = ""; var output = ""
var listener = (char) => { var listener = (char) => {
if (char !== "\r") { if (char !== "\r") {
output += char; output += char
} }
if (output.endsWith("# ")) { if (output.endsWith("# ")) {
emulator.remove_listener("serial0-output-char", listener); emulator.remove_listener("serial0-output-char", listener)
let outputWithoutPrompt = output.slice(0, -4); let outputWithoutPrompt = output.slice(0, -4)
let outputWithoutFirstLine = outputWithoutPrompt.slice( let outputWithoutFirstLine = outputWithoutPrompt.slice(
outputWithoutPrompt.indexOf("\n") + 1 outputWithoutPrompt.indexOf("\n") + 1
); )
if (outputWithoutFirstLine.endsWith("\n")) { if (outputWithoutFirstLine.endsWith("\n")) {
outputWithoutFirstLine = outputWithoutFirstLine.slice(0, -1); outputWithoutFirstLine = outputWithoutFirstLine.slice(0, -1)
} }
emulator.remove_listener("serial0-output-char", listener); emulator.remove_listener("serial0-output-char", listener)
resolve(outputWithoutFirstLine); resolve(outputWithoutFirstLine)
} }
};
emulator.add_listener("serial0-output-char", listener);
});
} }
window.run_in_vm = run_in_vm; emulator.add_listener("serial0-output-char", listener)
window.web_shell = { run_in_vm, testy }; })
}
window["run"] = run
window["web_shell"] = {run, testy}
/* /*
@ -54,35 +56,35 @@ async function test(condition) {
// Set emulator config. // Set emulator config.
let config = { let config = {
wasm_path: "web-shell/lib/v86.wasm", wasm_path: "/web-shell/lib/v86.wasm",
memory_size: 64 * 1024 * 1024, memory_size: 64 * 1024 * 1024,
vga_memory_size: 2 * 1024 * 1024, vga_memory_size: 2 * 1024 * 1024,
screen_container: document.getElementById("screen_container"), screen_container: document.getElementById("screen_container"),
bios: { url: "web-shell/images/seabios.bin" }, bios: {url: "/web-shell/images/seabios.bin"},
vga_bios: { url: "web-shell/images/vgabios.bin" }, vga_bios: {url: "/web-shell/images/vgabios.bin"},
cdrom: { url: "web-shell/images/image.iso.zst" }, cdrom: {url: "/web-shell/images/image.iso.zst"},
disable_mouse: true, disable_mouse: true,
autostart: true, autostart: true,
}; }
if (restoreState) { if (restoreState) {
config.initial_state = { config["initial_state"] = {
url: "web-shell/images/booted-state.bin.zst", url: "/web-shell/images/booted-state.bin.zst",
}; }
} }
function boot() { function boot() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Start the emulator! // Start the emulator!
emulator = window.emulator = new V86Starter(config); emulator = window["emulator"] = new V86Starter(config)
// Wait for the emulator to start, then resolve the promise. // Wait for the emulator to start, then resolve the promise.
var interval = setInterval(() => { var interval = setInterval(() => {
if (emulator.is_running()) { if (emulator.is_running()) {
clearInterval(interval); clearInterval(interval)
resolve(); resolve(true)
} }
}, 100); }, 100)
}); })
} }
/* /*