Merge variables in Stmt::Share.

This commit is contained in:
Stephen Chung
2022-10-29 15:17:12 +08:00
parent 91415b9750
commit d97f3f7ec4
3 changed files with 30 additions and 29 deletions

View File

@@ -977,23 +977,25 @@ impl Engine {
// Share statement
#[cfg(not(feature = "no_closure"))]
Stmt::Share(x, pos) => {
let (name, index) = &**x;
Stmt::Share(x) => {
x.iter()
.try_for_each(|(name, index, pos)| {
if let Some(index) = index
.map(|n| scope.len() - n.get())
.or_else(|| scope.search(name))
{
let val = scope.get_mut_by_index(index);
if let Some(index) = index
.map(|n| scope.len() - n.get())
.or_else(|| scope.search(name))
{
let val = scope.get_mut_by_index(index);
if !val.is_shared() {
// Replace the variable with a shared value.
*val = std::mem::take(val).into_shared();
}
Ok(Dynamic::UNIT)
} else {
Err(ERR::ErrorVariableNotFound(name.to_string(), *pos).into())
}
if !val.is_shared() {
// Replace the variable with a shared value.
*val = std::mem::take(val).into_shared();
}
Ok(())
} else {
Err(ERR::ErrorVariableNotFound(name.to_string(), *pos).into())
}
})
.map(|_| Dynamic::UNIT)
}
_ => unreachable!("statement cannot be evaluated: {:?}", stmt),