diff --git a/src/context.rs b/src/context.rs index 61a5a3b..1f85fb2 100644 --- a/src/context.rs +++ b/src/context.rs @@ -31,17 +31,17 @@ impl Context { } pub fn get_func(&self, name: &str) -> Option> { - let value = self.get(name)?; - match value { - ContextValue::Function(func) => Some(func), + let binding = self.0.lock().unwrap(); + match binding.get(name)? { + ContextValue::Function(func) => Some(func.clone()), ContextValue::Variable(_) => None, } } pub fn get_variable(&self, name: &str) -> Option { - let value = self.get(name)?; - match value { - ContextValue::Variable(v) => Some(v), + let binding = self.0.lock().unwrap(); + match binding.get(name)? { + ContextValue::Variable(v) => Some(v.clone()), ContextValue::Function(_) => None, } } @@ -51,13 +51,15 @@ impl Context { } pub fn value(&self, name: &str) -> Result { - // Lock is acquired and released inside `get` early to avoid contention during execution - let value = self.get(name); - match value { - Some(ContextValue::Variable(v)) => Ok(v), - Some(ContextValue::Function(func)) => func(Vec::new()), - None => Ok(Value::None), - } + let func = { + let binding = self.0.lock().unwrap(); + match binding.get(name) { + Some(ContextValue::Variable(v)) => return Ok(v.clone()), + Some(ContextValue::Function(func)) => func.clone(), + None => return Ok(Value::None), + } + }; + func(Vec::new()) } }