summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Cully <bjc@spork.org>2025-08-25 18:12:49 -0400
committerBrian Cully <bjc@spork.org>2025-08-25 18:12:49 -0400
commit660dfa5ab8003fe89ec6d6544a5c4392b21eaefb (patch)
tree962a846c409b8cb011bea8574b417c937a727fd4
parent0e9662364cd477684f29a3d0a435421f21a1aa07 (diff)
downloadautomathon-660dfa5ab8003fe89ec6d6544a5c4392b21eaefb.tar.gz
automathon-660dfa5ab8003fe89ec6d6544a5c4392b21eaefb.zip
wip: robo plugin stuff
i have no idea how to make this work with rust without resorting to unsafe.
-rwxr-xr-xsrc/lib.rs1
-rw-r--r--src/robo.rs62
2 files changed, 63 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index e9060f0..27ff2f4 100755
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -2,6 +2,7 @@ use log::{Level, error, info};
use wasm_bindgen::prelude::*;
pub mod forth;
+pub mod robo;
#[wasm_bindgen]
pub struct ExportedInstructionPointer {
diff --git a/src/robo.rs b/src/robo.rs
new file mode 100644
index 0000000..5da2e18
--- /dev/null
+++ b/src/robo.rs
@@ -0,0 +1,62 @@
+use std::collections::HashMap;
+use std::sync::Mutex;
+
+// vector describing speed in x,y dimensions
+pub type Speed = (usize, usize);
+
+pub struct Robo {
+ speed: Mutex<Speed>,
+}
+
+impl Robo {
+ pub fn new() -> Self {
+ Self {
+ speed: Mutex::new((0, 0)),
+ }
+ }
+
+ pub fn make_ins(&mut self) -> HashMap<&'static str, Box<dyn FnMut() + '_>> {
+ let mut map = HashMap::new();
+ let s = &mut self.speed;
+ let op_speed = || {
+ let mut x =
+ s.lock().expect("couldn't get lock on speed");
+ *x = (1, 1);
+ };
+ let op_speed2 = || {
+ let mut x =
+ s.lock().expect("couldn't get lock on speed");
+ *x = (2, 2);
+ };
+ map.insert("move", Box::new(op_speed) as Box<dyn FnMut()>);
+ map.insert("move2", Box::new(op_speed2) as Box<dyn FnMut()>);
+ map
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn can_run_two() {
+ let mut r = Robo::new();
+ {
+ let hm = &mut r.make_ins();
+ let fun = hm.get_mut("move").expect("move should exist");
+ fun();
+ }
+ let s = r.speed.lock().expect("couldn't get lock on speed");
+ assert_eq!(*s, (1, 1));
+ drop(s);
+
+ {
+ let hm = &mut r.make_ins();
+ let fun = hm.get_mut("move2").expect("move2 should exist");
+ fun();
+ }
+ let s = r.speed.lock().expect("couldn't get lock on speed");
+ assert_eq!(*s, (2, 2));
+ drop(s);
+ }
+}