diff --git a/pkg/ludus.js b/pkg/ludus.js index 5dbdd46..6ed8425 100644 --- a/pkg/ludus.js +++ b/pkg/ludus.js @@ -85,11 +85,16 @@ function io_poller () { outbox = [] } if (ready && running) { + if (keys_down.size > 0) bundle_keys() worker.postMessage(outbox) outbox = [] } } +function bundle_keys () { + outbox.push({verb: "Keys", data: keys_down}) +} + function start_io_polling () { io_interval_id = setInterval(io_poller, 100) } @@ -102,17 +107,19 @@ export function run (source) { return "TODO: handle this? should not be running" } // start the vm + // wrapping the Run message in an array for the worker worker.postMessage([{verb: "Run", data: source}]) - // reset all my state + // update state for this run + code = source + running = true + // reset the rest of my state outbox = [] ludus_console = "" ludus_commands = [] ludus_result = null - code = source - running = true ready = false keys_down = new Set(); - // start the polling loop loop + // start the polling loop start_io_polling() } diff --git a/pkg/rudus.js b/pkg/rudus.js index fe7b387..6105680 100644 --- a/pkg/rudus.js +++ b/pkg/rudus.js @@ -403,7 +403,7 @@ function __wbg_get_imports() { _assertBoolean(ret); return ret; }; - imports.wbg.__wbindgen_closure_wrapper8073 = function() { return logError(function (arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper8075 = function() { return logError(function (arg0, arg1, arg2) { const ret = makeMutClosure(arg0, arg1, 354, __wbg_adapter_20); return ret; }, arguments) }; diff --git a/pkg/rudus_bg.wasm b/pkg/rudus_bg.wasm index 175aa8a..b8c8233 100644 --- a/pkg/rudus_bg.wasm +++ b/pkg/rudus_bg.wasm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:75ece955d2ce58175d7b1174f71a522c817398436678ee11a7019478e182f44c -size 16762988 +oid sha256:b016e6b434bf88ae8a0c84bd800f077585dcde16595c9960d0b3f7698a7919c7 +size 16763587 diff --git a/src/io.rs b/src/io.rs index 6054a2f..2a54492 100644 --- a/src/io.rs +++ b/src/io.rs @@ -34,7 +34,7 @@ pub enum MsgIn { Input(String), Fetch(String, f64, String), Kill, - Keyboard(Vec), + Key(Vec), } impl std::fmt::Display for MsgIn { @@ -64,12 +64,12 @@ impl MsgIn { Value::tuple(vec![url, result_tuple]) } MsgIn::Kill => Value::Nothing, - MsgIn::Keyboard(downkeys) => { + MsgIn::Key(downkeys) => { let mut vector = Vector::new(); for key in downkeys { vector.push_back(Value::String(Rc::new(key))); } - Value::List(Box::new(vector)) + Value::list(vector) } } } diff --git a/src/world.rs b/src/world.rs index ae2b020..67c1fcf 100644 --- a/src/world.rs +++ b/src/world.rs @@ -255,6 +255,7 @@ pub struct Buffers { fetch_out: Value, fetch_in: Value, input: Value, + keys_down: Value, } impl Buffers { @@ -265,6 +266,7 @@ impl Buffers { fetch_out: prelude.get(&Key::Keyword("fetch_outbox")).unwrap().clone(), fetch_in: prelude.get(&Key::Keyword("fetch_inbox")).unwrap().clone(), input: prelude.get(&Key::Keyword("input")).unwrap().clone(), + keys_down: prelude.get(&Key::Keyword("keys_down")).unwrap().clone(), } } @@ -288,6 +290,10 @@ impl Buffers { self.fetch_in.as_box() } + pub fn keys_down (&self) -> Rc> { + self.keys_down.as_box() + } + } #[derive(Debug, Clone, PartialEq)] @@ -465,13 +471,18 @@ impl World { inbox_rc.replace(reply); } + fn register_keys(&mut self, keys: Value) { + let keys_down_rc = self.buffers.keys_down(); + keys_down_rc.replace(keys); + } + fn fill_buffers(&mut self, inbox: Vec) { for msg in inbox { match msg { MsgIn::Input(str) => self.fill_input(str), MsgIn::Kill => self.kill_signal = true, MsgIn::Fetch(..) => self.fetch_reply(msg.into_value()), - _ => todo!() + MsgIn::Key(..) => self.register_keys(msg.into_value()), } } }