js & rust code for key inputs

This commit is contained in:
Scott Richmond 2025-07-05 23:33:39 -04:00
parent f635e878c9
commit a95f575260
5 changed files with 27 additions and 9 deletions

View File

@ -85,11 +85,16 @@ function io_poller () {
outbox = [] outbox = []
} }
if (ready && running) { if (ready && running) {
if (keys_down.size > 0) bundle_keys()
worker.postMessage(outbox) worker.postMessage(outbox)
outbox = [] outbox = []
} }
} }
function bundle_keys () {
outbox.push({verb: "Keys", data: keys_down})
}
function start_io_polling () { function start_io_polling () {
io_interval_id = setInterval(io_poller, 100) io_interval_id = setInterval(io_poller, 100)
} }
@ -102,17 +107,19 @@ export function run (source) {
return "TODO: handle this? should not be running" return "TODO: handle this? should not be running"
} }
// start the vm // start the vm
// wrapping the Run message in an array for the worker
worker.postMessage([{verb: "Run", data: source}]) 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 = [] outbox = []
ludus_console = "" ludus_console = ""
ludus_commands = [] ludus_commands = []
ludus_result = null ludus_result = null
code = source
running = true
ready = false ready = false
keys_down = new Set(); keys_down = new Set();
// start the polling loop loop // start the polling loop
start_io_polling() start_io_polling()
} }

View File

@ -403,7 +403,7 @@ function __wbg_get_imports() {
_assertBoolean(ret); _assertBoolean(ret);
return 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); const ret = makeMutClosure(arg0, arg1, 354, __wbg_adapter_20);
return ret; return ret;
}, arguments) }; }, arguments) };

Binary file not shown.

View File

@ -34,7 +34,7 @@ pub enum MsgIn {
Input(String), Input(String),
Fetch(String, f64, String), Fetch(String, f64, String),
Kill, Kill,
Keyboard(Vec<String>), Key(Vec<String>),
} }
impl std::fmt::Display for MsgIn { impl std::fmt::Display for MsgIn {
@ -64,12 +64,12 @@ impl MsgIn {
Value::tuple(vec![url, result_tuple]) Value::tuple(vec![url, result_tuple])
} }
MsgIn::Kill => Value::Nothing, MsgIn::Kill => Value::Nothing,
MsgIn::Keyboard(downkeys) => { MsgIn::Key(downkeys) => {
let mut vector = Vector::new(); let mut vector = Vector::new();
for key in downkeys { for key in downkeys {
vector.push_back(Value::String(Rc::new(key))); vector.push_back(Value::String(Rc::new(key)));
} }
Value::List(Box::new(vector)) Value::list(vector)
} }
} }
} }

View File

@ -255,6 +255,7 @@ pub struct Buffers {
fetch_out: Value, fetch_out: Value,
fetch_in: Value, fetch_in: Value,
input: Value, input: Value,
keys_down: Value,
} }
impl Buffers { impl Buffers {
@ -265,6 +266,7 @@ impl Buffers {
fetch_out: prelude.get(&Key::Keyword("fetch_outbox")).unwrap().clone(), fetch_out: prelude.get(&Key::Keyword("fetch_outbox")).unwrap().clone(),
fetch_in: prelude.get(&Key::Keyword("fetch_inbox")).unwrap().clone(), fetch_in: prelude.get(&Key::Keyword("fetch_inbox")).unwrap().clone(),
input: prelude.get(&Key::Keyword("input")).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() self.fetch_in.as_box()
} }
pub fn keys_down (&self) -> Rc<RefCell<Value>> {
self.keys_down.as_box()
}
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -465,13 +471,18 @@ impl World {
inbox_rc.replace(reply); 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<MsgIn>) { fn fill_buffers(&mut self, inbox: Vec<MsgIn>) {
for msg in inbox { for msg in inbox {
match msg { match msg {
MsgIn::Input(str) => self.fill_input(str), MsgIn::Input(str) => self.fill_input(str),
MsgIn::Kill => self.kill_signal = true, MsgIn::Kill => self.kill_signal = true,
MsgIn::Fetch(..) => self.fetch_reply(msg.into_value()), MsgIn::Fetch(..) => self.fetch_reply(msg.into_value()),
_ => todo!() MsgIn::Key(..) => self.register_keys(msg.into_value()),
} }
} }
} }