rudus/pkg/worker.js
2025-06-30 18:59:59 -04:00

41 lines
852 B
JavaScript

import init, {ludus} from "./rudus.js";
console.log("Worker: starting Ludus VM.")
export function io (out) {
if (out.length > 0) postMessage(out)
return new Promise((resolve, _) => {
onmessage = (e) => {
console.log(e.data)
resolve(JSON.stringify(e.data))
}
})
}
let loaded_wasm = false
async function run(e) {
if (!loaded_wasm) {
await init()
loaded_wasm = true
}
let msgs = e.data
for (const msg of msgs) {
if (msg.verb === "run" && typeof msg.data === 'string') {
console.log("running ludus!")
onmessage = () => {}
let result = await ludus(msg.data)
console.log(result)
onmessage = run
} else {
console.log("Did not get valid startup message. Instead got:")
console.log(e.data)
}
}
}
onmessage = run
console.log("Worker: Ludus VM is running.")