Compare commits

...

2 Commits

Author SHA1 Message Date
Scott Richmond
1158821aff build 2025-07-02 14:52:22 -04:00
Scott Richmond
cfe8009861 ready handshake for better message passing 2025-07-02 14:51:42 -04:00
6 changed files with 29 additions and 10 deletions

View File

@ -1,7 +1,7 @@
&&& buffers: shared memory with Rust &&& buffers: shared memory with Rust
& use types that are all either empty or any & use types that are all either empty or any
box console = [] box console = []
box input = "" box input = nil
box fetch_outbox = "" box fetch_outbox = ""
box fetch_inbox = () box fetch_inbox = ()
box keys_down = [] box keys_down = []
@ -1299,14 +1299,14 @@ fn fetch {
fn input_reader! { fn input_reader! {
(pid as :keyword) -> { (pid as :keyword) -> {
if empty? (unbox (input)) if not (unbox (input))
then { then {
yield! () yield! ()
input_reader! (pid) input_reader! (pid)
} }
else { else {
send (pid, (:reply, unbox (input))) send (pid, (:reply, unbox (input)))
store! (input, "") store! (input, nil)
} }
} }
} }

View File

@ -9,6 +9,7 @@ let ludus_commands = []
let ludus_result = null let ludus_result = null
let code = null let code = null
let running = false let running = false
let ready = false
let io_interval_id = null let io_interval_id = null
worker.onmessage = handle_messages worker.onmessage = handle_messages
@ -50,6 +51,10 @@ async function handle_messages (e) {
console.log("Main: js responds => ", text) console.log("Main: js responds => ", text)
outbox.push({verb: "Fetch", data: [msg.data, res.status, text]}) outbox.push({verb: "Fetch", data: [msg.data, res.status, text]})
} }
case "Ready": {
console.log("Main: ludus is ready")
ready = true
}
} }
} }
} }
@ -57,10 +62,13 @@ async function handle_messages (e) {
function io_poller () { function io_poller () {
if (io_interval_id && !running) { if (io_interval_id && !running) {
// flush the outbox one last time // flush the outbox one last time
// (presumably, with the kill message)
worker.postMessage(outbox) worker.postMessage(outbox)
// cancel the poller // cancel the poller
clearInterval(io_interval_id) clearInterval(io_interval_id)
} else { outbox = []
}
if (ready && running) {
worker.postMessage(outbox) worker.postMessage(outbox)
outbox = [] outbox = []
} }
@ -73,23 +81,28 @@ function start_io_polling () {
// runs a ludus script; does not return the result // runs a ludus script; does not return the result
// the result must be explicitly polled with `result` // the result must be explicitly polled with `result`
export function run (source) { export function run (source) {
if (running) "TODO: handle this? should not be running" if (running) {
return "TODO: handle this? should not be running"
}
running = true running = true
ready = false
result = null result = null
code = source code = source
outbox = [{verb: "Run", data: source}] worker.postMessage([{verb: "Run", data: source}])
outbox = []
start_io_polling() start_io_polling()
} }
// tells if the ludus script is still running // tells if the ludus script is still running
export function is_running() { export function is_running() {
return running return running && ready
} }
// kills a ludus script // kills a ludus script
export function kill () { export function kill () {
running = false running = false
outbox.push({verb: "Kill"}) outbox.push({verb: "Kill"})
console.log("Main: Killed Ludus")
} }
// sends text into ludus (status: not working) // sends text into ludus (status: not working)

View File

@ -328,7 +328,7 @@ function __wbg_get_imports() {
const ret = false; const ret = false;
return ret; return ret;
}; };
imports.wbg.__wbindgen_closure_wrapper985 = function(arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper984 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 329, __wbg_adapter_20); const ret = makeMutClosure(arg0, arg1, 329, __wbg_adapter_20);
return ret; return ret;
}; };

Binary file not shown.

View File

@ -33,6 +33,7 @@ pub enum MsgOut {
Commands(Commands), Commands(Commands),
Fetch(Url), Fetch(Url),
Complete(FinalValue), Complete(FinalValue),
Ready
} }
impl std::fmt::Display for MsgOut { impl std::fmt::Display for MsgOut {
@ -68,6 +69,9 @@ impl MsgOut {
let json_lines = format!("\"{json_lines}\""); let json_lines = format!("\"{json_lines}\"");
make_json_payload("Console", json_lines) make_json_payload("Console", json_lines)
} }
MsgOut::Ready => {
make_json_payload("Ready", "\"null\"".to_string())
}
} }
} }
} }

View File

@ -459,10 +459,12 @@ impl World {
pub async fn run(&mut self) { pub async fn run(&mut self) {
self.activate_main(); self.activate_main();
do_io(vec![MsgOut::Ready]).await;
loop { loop {
self.maybe_do_io().await; self.maybe_do_io().await;
if self.kill_signal { if self.kill_signal {
let outbox = self.flush_buffers(); let mut outbox = self.flush_buffers();
outbox.push(MsgOut::Complete(Err(Panic::Str("ludus killed by user"))));
do_io(outbox).await; do_io(outbox).await;
return; return;
} }