Skip to content

Commit b8b42a9

Browse files
committed
fix: introduced refcell to handle borrow problems
1 parent 4a650db commit b8b42a9

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

taurus/src/context/executor.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
use crate::context::Context;
2+
use crate::context::argument::{Argument, ParameterNode};
3+
use crate::context::registry::FunctionStore;
24
use crate::context::signal::Signal;
35
use crate::error::RuntimeError;
6+
use std::cell::RefCell;
47
use std::collections::HashMap;
5-
use tucana::shared::{NodeFunction, NodeParameter};
6-
use crate::context::argument::{Argument, ParameterNode};
7-
use crate::context::registry::FunctionStore;
8+
use tucana::shared::{NodeFunction};
89

910
pub struct Executor<'a> {
1011
functions: &'a FunctionStore,
1112
nodes: HashMap<i64, NodeFunction>,
12-
context: Context,
13+
context: RefCell<Context>,
1314
}
1415

15-
type HandleNodeParameterFn = fn(&mut Executor, node_parameter: &NodeParameter) -> Signal;
16-
1716
impl<'a> Executor<'a> {
1817
pub fn new(
1918
functions: &'a FunctionStore,
@@ -23,11 +22,11 @@ impl<'a> Executor<'a> {
2322
Executor {
2423
functions,
2524
nodes,
26-
context,
25+
context: RefCell::new(context),
2726
}
2827
}
2928

30-
pub fn execute(&mut self, starting_node_id: i64) -> Signal {
29+
pub fn execute(&self, starting_node_id: i64) -> Signal {
3130
let mut current_node_id = starting_node_id;
3231

3332
loop {
@@ -41,23 +40,35 @@ impl<'a> Executor<'a> {
4140
Some(n) => n.clone(),
4241
};
4342

44-
4543
let entry = match self.functions.get(node.runtime_function_id.as_str()) {
4644
None => {
47-
return Signal::Failure(RuntimeError::simple_str("FunctionNotFound","The function was not found"))
48-
},
45+
return Signal::Failure(RuntimeError::simple_str(
46+
"FunctionNotFound",
47+
"The function was not found",
48+
));
49+
}
4950
Some(f) => f,
5051
};
5152

5253
let mut args: Vec<Argument> = Vec::with_capacity(node.parameters.len());
5354
for parameter in &node.parameters {
5455
let node_value = match &parameter.value {
5556
Some(v) => v,
56-
None => return Signal::Failure(RuntimeError::simple_str("NodeValueNotFound","Missing parameter value")),
57+
None => {
58+
return Signal::Failure(RuntimeError::simple_str(
59+
"NodeValueNotFound",
60+
"Missing parameter value",
61+
));
62+
}
5763
};
5864
let value = match &node_value.value {
5965
Some(v) => v,
60-
None => return Signal::Failure(RuntimeError::simple_str("NodeValueNotFound","Missing inner value")),
66+
None => {
67+
return Signal::Failure(RuntimeError::simple_str(
68+
"NodeValueNotFound",
69+
"Missing inner value",
70+
));
71+
}
6172
};
6273

6374
match value {
@@ -73,26 +84,28 @@ impl<'a> Executor<'a> {
7384
}
7485
}
7586

76-
77-
// Eagerly evaluate args that the function *declares* as Eager
7887
for (i, a) in args.iter_mut().enumerate() {
79-
let mode = entry.param_modes.get(i).copied().unwrap_or(ParameterNode::Eager);
88+
let mode = entry
89+
.param_modes
90+
.get(i)
91+
.copied()
92+
.unwrap_or(ParameterNode::Eager);
8093
if matches!(mode, ParameterNode::Eager) {
8194
if let Argument::Thunk(id) = *a {
8295
match self.execute(id) {
8396
Signal::Success(v) => *a = Argument::Eval(v),
84-
// propagate control flow immediately
85-
s @ (Signal::Failure(_) | Signal::Return(_) | Signal::Respond(_) | Signal::Stop) => return s,
97+
s @ (Signal::Failure(_)
98+
| Signal::Return(_)
99+
| Signal::Respond(_)
100+
| Signal::Stop) => return s,
86101
}
87102
}
88103
}
89104
}
90105

91-
// Provide a runner for Lazy params
92106
let mut run = |node_id: i64| self.execute(node_id);
93-
94-
// Call the handler (no special cases anywhere)
95-
let result = (entry.handler)(&args, &mut self.context, &mut run);
107+
let mut ctx = self.context.borrow_mut();
108+
let result = (entry.handler)(&args, &mut ctx, &mut run);
96109

97110
match result {
98111
Signal::Success(value) => {

0 commit comments

Comments
 (0)