diff --git a/pkg/rudus.js b/pkg/rudus.js index b048e8d..eb4a397 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_wrapper8088 = function() { return logError(function (arg0, arg1, arg2) { + imports.wbg.__wbindgen_closure_wrapper8090 = 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 ec8f808..9511fd3 100644 Binary files a/pkg/rudus_bg.wasm and b/pkg/rudus_bg.wasm differ diff --git a/src/vm.rs b/src/vm.rs index 3898e83..f1d8a09 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -73,6 +73,8 @@ pub struct Creature { zoo: Rc>, r#yield: bool, scrutinee: Option, + parents: Vec<&'static str>, + siblings: Vec<&'static str>, } impl std::fmt::Display for Creature { @@ -120,6 +122,8 @@ impl Creature { r#yield: false, msg_idx: 0, scrutinee: None, + parents: vec![], + siblings: vec![], } } @@ -255,6 +259,45 @@ impl Creature { self.push(Value::Keyword("ok")); } + // TODO: fix these based on what I decide about `link` & `monitor` + fn link_report(&mut self, parent: Value, child: Value) { + let (Value::Keyword(parent), Value::Keyword(child)) = (parent, child) else { + unreachable!("expected keyword pids in link_report"); + }; + let child = if child == self.pid { + self + } else { + &mut self.zoo.borrow_mut().catch(child) + }; + child.parents.push(parent); + } + + fn link_panic(&mut self, left_pid: Value, right_pid: Value) { + let (Value::Keyword(left_id), Value::Keyword(right_id)) = (left_pid, right_pid) else { + unreachable!("expected keyword pids in link_panic"); + }; + // if we're linking to ourselves, we don't need to do anything + if left_id == self.pid && right_id == self.pid { + return; + } + let mut zoo = self.zoo.borrow_mut(); + // fancy footwork w/ cases: we can't catch ourselves in the zoo + if left_id == self.pid { + self.siblings.push(right_id); + let mut right = zoo.catch(right_id); + right.siblings.push(self.pid); + } else if right_id == self.pid { + self.siblings.push(left_id); + let mut left = zoo.catch(left_id); + left.siblings.push(self.pid); + } else { + let mut left = zoo.catch(left_id); + let mut right = zoo.catch(right_id); + left.siblings.push(right_id); + right.siblings.push(left_id); + } + } + fn handle_msg(&mut self, args: Vec) { println!("message received by {}: {}", self.pid, args[0]); let Value::Keyword(msg) = args.first().unwrap() else { @@ -267,7 +310,7 @@ impl Creature { let f = args[1].clone(); let proc = Creature::spawn(f, self.zoo.clone(), self.debug); let id = self.zoo.as_ref().borrow_mut().put(proc); - println!("spawning new process {id}!"); + console_log!("spawning new process {id}!"); self.push(Value::Keyword(id)); } "yield" => { @@ -286,7 +329,8 @@ impl Creature { self.push(Value::False) } } - "link" => todo!(), + "link_panic" => self.link_panic(args[1].clone(), args[2].clone()), + "link_report" => self.link_report(args[1].clone(), args[2].clone()), "flush" => { let msgs = self.mbx.iter().cloned().collect::>(); let msgs = Vector::from(msgs);