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

View File

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

View File

@ -328,7 +328,7 @@ function __wbg_get_imports() {
const ret = false;
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);
return ret;
};

Binary file not shown.

View File

@ -33,6 +33,7 @@ pub enum MsgOut {
Commands(Commands),
Fetch(Url),
Complete(FinalValue),
Ready
}
impl std::fmt::Display for MsgOut {
@ -68,6 +69,9 @@ impl MsgOut {
let json_lines = format!("\"{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) {
self.activate_main();
do_io(vec![MsgOut::Ready]).await;
loop {
self.maybe_do_io().await;
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;
return;
}