Avoid copying in Dynamic.
This commit is contained in:
parent
60b52c142e
commit
4f91e7fbcf
30
src/any.rs
30
src/any.rs
@ -252,6 +252,12 @@ impl Clone for Dynamic {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Dynamic {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self(Union::Unit(()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Cast a Boxed type into another type.
|
/// Cast a Boxed type into another type.
|
||||||
fn cast_box<X: Variant, T: Variant>(item: Box<X>) -> Result<T, Box<X>> {
|
fn cast_box<X: Variant, T: Variant>(item: Box<X>) -> Result<T, Box<X>> {
|
||||||
// Only allow casting to the exact same type
|
// Only allow casting to the exact same type
|
||||||
@ -359,22 +365,16 @@ impl Dynamic {
|
|||||||
return cast_box::<_, T>(Box::new(self)).ok();
|
return cast_box::<_, T>(Box::new(self)).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
match &self.0 {
|
match self.0 {
|
||||||
Union::Unit(value) => (value as &dyn Variant).downcast_ref::<T>().cloned(),
|
Union::Unit(ref value) => (value as &dyn Variant).downcast_ref::<T>().cloned(),
|
||||||
Union::Bool(value) => (value as &dyn Variant).downcast_ref::<T>().cloned(),
|
Union::Bool(ref value) => (value as &dyn Variant).downcast_ref::<T>().cloned(),
|
||||||
Union::Str(value) => (value.as_ref() as &dyn Variant)
|
Union::Str(value) => cast_box::<_, T>(value).ok(),
|
||||||
.downcast_ref::<T>()
|
Union::Char(ref value) => (value as &dyn Variant).downcast_ref::<T>().cloned(),
|
||||||
.cloned(),
|
Union::Int(ref value) => (value as &dyn Variant).downcast_ref::<T>().cloned(),
|
||||||
Union::Char(value) => (value as &dyn Variant).downcast_ref::<T>().cloned(),
|
|
||||||
Union::Int(value) => (value as &dyn Variant).downcast_ref::<T>().cloned(),
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
Union::Float(value) => (value as &dyn Variant).downcast_ref::<T>().cloned(),
|
Union::Float(ref value) => (value as &dyn Variant).downcast_ref::<T>().cloned(),
|
||||||
Union::Array(value) => (value.as_ref() as &dyn Variant)
|
Union::Array(value) => cast_box::<_, T>(value).ok(),
|
||||||
.downcast_ref::<T>()
|
Union::Map(value) => cast_box::<_, T>(value).ok(),
|
||||||
.cloned(),
|
|
||||||
Union::Map(value) => (value.as_ref() as &dyn Variant)
|
|
||||||
.downcast_ref::<T>()
|
|
||||||
.cloned(),
|
|
||||||
Union::Variant(value) => value.as_ref().as_ref().downcast_ref::<T>().cloned(),
|
Union::Variant(value) => value.as_ref().as_ref().downcast_ref::<T>().cloned(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
201
src/engine.rs
201
src/engine.rs
@ -88,7 +88,7 @@ enum Target<'a> {
|
|||||||
|
|
||||||
impl Target<'_> {
|
impl Target<'_> {
|
||||||
/// Get the value of the `Target` as a `Dynamic`.
|
/// Get the value of the `Target` as a `Dynamic`.
|
||||||
pub fn into_dynamic(self) -> Dynamic {
|
pub fn clone_into_dynamic(self) -> Dynamic {
|
||||||
match self {
|
match self {
|
||||||
Target::Ref(r) => r.clone(),
|
Target::Ref(r) => r.clone(),
|
||||||
Target::Value(v) => *v,
|
Target::Value(v) => *v,
|
||||||
@ -111,7 +111,7 @@ impl Target<'_> {
|
|||||||
.map_err(|_| EvalAltResult::ErrorCharMismatch(pos))?;
|
.map_err(|_| EvalAltResult::ErrorCharMismatch(pos))?;
|
||||||
|
|
||||||
let mut chars: Vec<char> = s.chars().collect();
|
let mut chars: Vec<char> = s.chars().collect();
|
||||||
let ch = *chars.get(x.1).expect("string index out of bounds");
|
let ch = chars[x.1];
|
||||||
|
|
||||||
// See if changed - if so, update the String
|
// See if changed - if so, update the String
|
||||||
if ch != new_ch {
|
if ch != new_ch {
|
||||||
@ -141,26 +141,31 @@ impl<T: Into<Dynamic>> From<T> for Target<'_> {
|
|||||||
|
|
||||||
/// A type to hold a number of `Dynamic` values in static storage for speed,
|
/// A type to hold a number of `Dynamic` values in static storage for speed,
|
||||||
/// and any spill-overs in a `Vec`.
|
/// and any spill-overs in a `Vec`.
|
||||||
struct StaticVec {
|
struct StaticVec<T: Default> {
|
||||||
/// Total number of values held.
|
/// Total number of values held.
|
||||||
len: usize,
|
len: usize,
|
||||||
/// Static storage.
|
/// Static storage. 4 slots should be enough for most cases - i.e. four levels of indirection.
|
||||||
list: [Dynamic; 4],
|
list: [T; 4],
|
||||||
/// Dynamic storage.
|
/// Dynamic storage. For spill-overs.
|
||||||
more: Vec<Dynamic>,
|
more: Vec<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StaticVec {
|
impl<T: Default> StaticVec<T> {
|
||||||
/// Create a new `StaticVec`.
|
/// Create a new `StaticVec`.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
len: 0,
|
len: 0,
|
||||||
list: [().into(), ().into(), ().into(), ().into()],
|
list: [
|
||||||
|
Default::default(),
|
||||||
|
Default::default(),
|
||||||
|
Default::default(),
|
||||||
|
Default::default(),
|
||||||
|
],
|
||||||
more: Vec::new(),
|
more: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Push a new value to the end of this `StaticVec`.
|
/// Push a new value to the end of this `StaticVec`.
|
||||||
pub fn push<T: Into<Dynamic>>(&mut self, value: T) {
|
pub fn push<X: Into<T>>(&mut self, value: X) {
|
||||||
if self.len >= self.list.len() {
|
if self.len >= self.list.len() {
|
||||||
self.more.push(value.into());
|
self.more.push(value.into());
|
||||||
} else {
|
} else {
|
||||||
@ -173,11 +178,11 @@ impl StaticVec {
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if the `StaticVec` is empty.
|
/// Panics if the `StaticVec` is empty.
|
||||||
pub fn pop(&mut self) -> Dynamic {
|
pub fn pop(&mut self) -> T {
|
||||||
let result = if self.len <= 0 {
|
let result = if self.len <= 0 {
|
||||||
panic!("nothing to pop!")
|
panic!("nothing to pop!")
|
||||||
} else if self.len <= self.list.len() {
|
} else if self.len <= self.list.len() {
|
||||||
mem::replace(self.list.get_mut(self.len - 1).unwrap(), ().into())
|
mem::replace(self.list.get_mut(self.len - 1).unwrap(), Default::default())
|
||||||
} else {
|
} else {
|
||||||
self.more.pop().unwrap()
|
self.more.pop().unwrap()
|
||||||
};
|
};
|
||||||
@ -572,24 +577,24 @@ impl Engine {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Getter function not found?
|
||||||
if let Some(prop) = extract_prop_from_getter(fn_name) {
|
if let Some(prop) = extract_prop_from_getter(fn_name) {
|
||||||
// Getter function not found
|
|
||||||
return Err(Box::new(EvalAltResult::ErrorDotExpr(
|
return Err(Box::new(EvalAltResult::ErrorDotExpr(
|
||||||
format!("- property '{}' unknown or write-only", prop),
|
format!("- property '{}' unknown or write-only", prop),
|
||||||
pos,
|
pos,
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setter function not found?
|
||||||
if let Some(prop) = extract_prop_from_setter(fn_name) {
|
if let Some(prop) = extract_prop_from_setter(fn_name) {
|
||||||
// Setter function not found
|
|
||||||
return Err(Box::new(EvalAltResult::ErrorDotExpr(
|
return Err(Box::new(EvalAltResult::ErrorDotExpr(
|
||||||
format!("- property '{}' unknown or read-only", prop),
|
format!("- property '{}' unknown or read-only", prop),
|
||||||
pos,
|
pos,
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return default value (if any)
|
||||||
if let Some(val) = def_val {
|
if let Some(val) = def_val {
|
||||||
// Return default value
|
|
||||||
return Ok(val.clone());
|
return Ok(val.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -621,8 +626,8 @@ impl Engine {
|
|||||||
let scope_len = scope.len();
|
let scope_len = scope.len();
|
||||||
let mut state = State::new();
|
let mut state = State::new();
|
||||||
|
|
||||||
|
// Put arguments into scope as variables - variable name is copied
|
||||||
scope.extend(
|
scope.extend(
|
||||||
// Put arguments into scope as variables - variable name is copied
|
|
||||||
// TODO - avoid copying variable name
|
// TODO - avoid copying variable name
|
||||||
fn_def
|
fn_def
|
||||||
.params
|
.params
|
||||||
@ -649,8 +654,8 @@ impl Engine {
|
|||||||
let mut scope = Scope::new();
|
let mut scope = Scope::new();
|
||||||
let mut state = State::new();
|
let mut state = State::new();
|
||||||
|
|
||||||
|
// Put arguments into scope as variables
|
||||||
scope.extend(
|
scope.extend(
|
||||||
// Put arguments into scope as variables
|
|
||||||
fn_def
|
fn_def
|
||||||
.params
|
.params
|
||||||
.iter()
|
.iter()
|
||||||
@ -698,7 +703,7 @@ impl Engine {
|
|||||||
Ok(self.map_type_name(args[0].type_name()).to_string().into())
|
Ok(self.map_type_name(args[0].type_name()).to_string().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
// eval
|
// eval - reaching this point it must be a method-style call
|
||||||
KEYWORD_EVAL if args.len() == 1 && !self.has_override(fn_lib, KEYWORD_EVAL) => {
|
KEYWORD_EVAL if args.len() == 1 && !self.has_override(fn_lib, KEYWORD_EVAL) => {
|
||||||
Err(Box::new(EvalAltResult::ErrorRuntime(
|
Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||||
"'eval' should not be called in method style. Try eval(...);".into(),
|
"'eval' should not be called in method style. Try eval(...);".into(),
|
||||||
@ -706,6 +711,7 @@ impl Engine {
|
|||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Normal method call
|
||||||
_ => self.call_fn_raw(None, fn_lib, fn_name, args, def_val, pos, level),
|
_ => self.call_fn_raw(None, fn_lib, fn_name, args, def_val, pos, level),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -757,7 +763,7 @@ impl Engine {
|
|||||||
fn_lib: &FunctionsLib,
|
fn_lib: &FunctionsLib,
|
||||||
mut target: Target,
|
mut target: Target,
|
||||||
rhs: &Expr,
|
rhs: &Expr,
|
||||||
idx_values: &mut StaticVec,
|
idx_values: &mut StaticVec<Dynamic>,
|
||||||
is_index: bool,
|
is_index: bool,
|
||||||
op_pos: Position,
|
op_pos: Position,
|
||||||
level: usize,
|
level: usize,
|
||||||
@ -790,12 +796,12 @@ impl Engine {
|
|||||||
_ if new_val.is_some() => {
|
_ if new_val.is_some() => {
|
||||||
let mut indexed_val = self.get_indexed_mut(obj, idx_val, rhs.position(), op_pos, true)?;
|
let mut indexed_val = self.get_indexed_mut(obj, idx_val, rhs.position(), op_pos, true)?;
|
||||||
indexed_val.set_value(new_val.unwrap(), rhs.position())?;
|
indexed_val.set_value(new_val.unwrap(), rhs.position())?;
|
||||||
Ok((().into(), true))
|
Ok((Default::default(), true))
|
||||||
}
|
}
|
||||||
// xxx[rhs]
|
// xxx[rhs]
|
||||||
_ => self
|
_ => self
|
||||||
.get_indexed_mut(obj, idx_val, rhs.position(), op_pos, false)
|
.get_indexed_mut(obj, idx_val, rhs.position(), op_pos, false)
|
||||||
.map(|v| (v.into_dynamic(), false))
|
.map(|v| (v.clone_into_dynamic(), false))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match rhs {
|
match rhs {
|
||||||
@ -814,13 +820,13 @@ impl Engine {
|
|||||||
let mut indexed_val =
|
let mut indexed_val =
|
||||||
self.get_indexed_mut(obj, id.to_string().into(), *pos, op_pos, true)?;
|
self.get_indexed_mut(obj, id.to_string().into(), *pos, op_pos, true)?;
|
||||||
indexed_val.set_value(new_val.unwrap(), rhs.position())?;
|
indexed_val.set_value(new_val.unwrap(), rhs.position())?;
|
||||||
Ok((().into(), true))
|
Ok((Default::default(), true))
|
||||||
}
|
}
|
||||||
// {xxx:map}.id
|
// {xxx:map}.id
|
||||||
Expr::Property(id, pos) if obj.is::<Map>() => {
|
Expr::Property(id, pos) if obj.is::<Map>() => {
|
||||||
let indexed_val =
|
let indexed_val =
|
||||||
self.get_indexed_mut(obj, id.to_string().into(), *pos, op_pos, false)?;
|
self.get_indexed_mut(obj, id.to_string().into(), *pos, op_pos, false)?;
|
||||||
Ok((indexed_val.into_dynamic(), false))
|
Ok((indexed_val.clone_into_dynamic(), false))
|
||||||
}
|
}
|
||||||
// xxx.id = ???
|
// xxx.id = ???
|
||||||
Expr::Property(id, pos) if new_val.is_some() => {
|
Expr::Property(id, pos) if new_val.is_some() => {
|
||||||
@ -858,8 +864,7 @@ impl Engine {
|
|||||||
// xxx.dot_lhs.rhs
|
// xxx.dot_lhs.rhs
|
||||||
Expr::Dot(dot_lhs, dot_rhs, pos) => {
|
Expr::Dot(dot_lhs, dot_rhs, pos) => {
|
||||||
let is_index = matches!(rhs, Expr::Index(_,_,_));
|
let is_index = matches!(rhs, Expr::Index(_,_,_));
|
||||||
let mut buf: Dynamic = ().into();
|
let mut args = [obj, &mut Default::default()];
|
||||||
let mut args = [obj, &mut buf];
|
|
||||||
|
|
||||||
let indexed_val = &mut (if let Expr::Property(id, pos) = dot_lhs.as_ref() {
|
let indexed_val = &mut (if let Expr::Property(id, pos) = dot_lhs.as_ref() {
|
||||||
let fn_name = make_getter(id);
|
let fn_name = make_getter(id);
|
||||||
@ -871,12 +876,12 @@ impl Engine {
|
|||||||
rhs.position(),
|
rhs.position(),
|
||||||
)));
|
)));
|
||||||
});
|
});
|
||||||
let (result, changed) = self.eval_dot_index_chain_helper(
|
let (result, may_be_changed) = self.eval_dot_index_chain_helper(
|
||||||
fn_lib, indexed_val.into(), dot_rhs, idx_values, is_index, *pos, level, new_val
|
fn_lib, indexed_val.into(), dot_rhs, idx_values, is_index, *pos, level, new_val
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Feed the value back via a setter just in case it has been updated
|
// Feed the value back via a setter just in case it has been updated
|
||||||
if changed {
|
if may_be_changed {
|
||||||
if let Expr::Property(id, pos) = dot_lhs.as_ref() {
|
if let Expr::Property(id, pos) = dot_lhs.as_ref() {
|
||||||
let fn_name = make_setter(id);
|
let fn_name = make_setter(id);
|
||||||
args[1] = indexed_val;
|
args[1] = indexed_val;
|
||||||
@ -884,7 +889,7 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((result, changed))
|
Ok((result, may_be_changed))
|
||||||
}
|
}
|
||||||
// Syntax error
|
// Syntax error
|
||||||
_ => Err(Box::new(EvalAltResult::ErrorDotExpr(
|
_ => Err(Box::new(EvalAltResult::ErrorDotExpr(
|
||||||
@ -931,15 +936,9 @@ impl Engine {
|
|||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let this_ptr = target.into();
|
||||||
self.eval_dot_index_chain_helper(
|
self.eval_dot_index_chain_helper(
|
||||||
fn_lib,
|
fn_lib, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val,
|
||||||
target.into(),
|
|
||||||
dot_rhs,
|
|
||||||
idx_values,
|
|
||||||
is_index,
|
|
||||||
op_pos,
|
|
||||||
level,
|
|
||||||
new_val,
|
|
||||||
)
|
)
|
||||||
.map(|(v, _)| v)
|
.map(|(v, _)| v)
|
||||||
}
|
}
|
||||||
@ -952,16 +951,9 @@ impl Engine {
|
|||||||
// {expr}.??? or {expr}[???]
|
// {expr}.??? or {expr}[???]
|
||||||
expr => {
|
expr => {
|
||||||
let val = self.eval_expr(scope, state, fn_lib, expr, level)?;
|
let val = self.eval_expr(scope, state, fn_lib, expr, level)?;
|
||||||
|
let this_ptr = val.into();
|
||||||
self.eval_dot_index_chain_helper(
|
self.eval_dot_index_chain_helper(
|
||||||
fn_lib,
|
fn_lib, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val,
|
||||||
val.into(),
|
|
||||||
dot_rhs,
|
|
||||||
idx_values,
|
|
||||||
is_index,
|
|
||||||
op_pos,
|
|
||||||
level,
|
|
||||||
new_val,
|
|
||||||
)
|
)
|
||||||
.map(|(v, _)| v)
|
.map(|(v, _)| v)
|
||||||
}
|
}
|
||||||
@ -979,7 +971,7 @@ impl Engine {
|
|||||||
state: &mut State,
|
state: &mut State,
|
||||||
fn_lib: &FunctionsLib,
|
fn_lib: &FunctionsLib,
|
||||||
expr: &Expr,
|
expr: &Expr,
|
||||||
idx_values: &mut StaticVec,
|
idx_values: &mut StaticVec<Dynamic>,
|
||||||
size: usize,
|
size: usize,
|
||||||
level: usize,
|
level: usize,
|
||||||
) -> Result<(), Box<EvalAltResult>> {
|
) -> Result<(), Box<EvalAltResult>> {
|
||||||
@ -992,12 +984,11 @@ impl Engine {
|
|||||||
|
|
||||||
idx_values.push(arg_values)
|
idx_values.push(arg_values)
|
||||||
}
|
}
|
||||||
// Store a placeholder - no need to copy the property name
|
Expr::Property(_, _) => idx_values.push(()), // Store a placeholder - no need to copy the property name
|
||||||
Expr::Property(_, _) => idx_values.push(()),
|
|
||||||
Expr::Index(lhs, rhs, _) | Expr::Dot(lhs, rhs, _) => {
|
Expr::Index(lhs, rhs, _) | Expr::Dot(lhs, rhs, _) => {
|
||||||
// Evaluate in left-to-right order
|
// Evaluate in left-to-right order
|
||||||
let lhs_val = match lhs.as_ref() {
|
let lhs_val = match lhs.as_ref() {
|
||||||
Expr::Property(_, _) => ().into(), // Store a placeholder in case of a property
|
Expr::Property(_, _) => Default::default(), // Store a placeholder in case of a property
|
||||||
_ => self.eval_expr(scope, state, fn_lib, lhs, level)?,
|
_ => self.eval_expr(scope, state, fn_lib, lhs, level)?,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1052,7 +1043,7 @@ impl Engine {
|
|||||||
.map_err(|_| EvalAltResult::ErrorStringIndexExpr(idx_pos))?;
|
.map_err(|_| EvalAltResult::ErrorStringIndexExpr(idx_pos))?;
|
||||||
|
|
||||||
Ok(if create {
|
Ok(if create {
|
||||||
map.entry(index).or_insert(().into()).into()
|
map.entry(index).or_insert(Default::default()).into()
|
||||||
} else {
|
} else {
|
||||||
map.get_mut(&index)
|
map.get_mut(&index)
|
||||||
.map(Target::from)
|
.map(Target::from)
|
||||||
@ -1103,40 +1094,35 @@ impl Engine {
|
|||||||
match rhs_value {
|
match rhs_value {
|
||||||
Dynamic(Union::Array(mut rhs_value)) => {
|
Dynamic(Union::Array(mut rhs_value)) => {
|
||||||
let def_value = false.into();
|
let def_value = false.into();
|
||||||
let mut result = false;
|
|
||||||
|
|
||||||
// Call the '==' operator to compare each value
|
// Call the '==' operator to compare each value
|
||||||
for value in rhs_value.iter_mut() {
|
for value in rhs_value.iter_mut() {
|
||||||
let args = &mut [&mut lhs_value, value];
|
let args = &mut [&mut lhs_value, value];
|
||||||
let def_value = Some(&def_value);
|
let def_value = Some(&def_value);
|
||||||
|
|
||||||
if self
|
if self
|
||||||
.call_fn_raw(None, fn_lib, "==", args, def_value, rhs.position(), level)?
|
.call_fn_raw(None, fn_lib, "==", args, def_value, rhs.position(), level)?
|
||||||
.as_bool()
|
.as_bool()
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
{
|
{
|
||||||
result = true;
|
return Ok(true.into());
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(result.into())
|
Ok(false.into())
|
||||||
}
|
}
|
||||||
Dynamic(Union::Map(rhs_value)) => {
|
Dynamic(Union::Map(rhs_value)) => match lhs_value {
|
||||||
// Only allows String or char
|
// Only allows String or char
|
||||||
match lhs_value {
|
Dynamic(Union::Str(s)) => Ok(rhs_value.contains_key(s.as_ref()).into()),
|
||||||
Dynamic(Union::Str(s)) => Ok(rhs_value.contains_key(s.as_ref()).into()),
|
Dynamic(Union::Char(c)) => Ok(rhs_value.contains_key(&c.to_string()).into()),
|
||||||
Dynamic(Union::Char(c)) => Ok(rhs_value.contains_key(&c.to_string()).into()),
|
_ => Err(Box::new(EvalAltResult::ErrorInExpr(lhs.position()))),
|
||||||
_ => Err(Box::new(EvalAltResult::ErrorInExpr(lhs.position()))),
|
},
|
||||||
}
|
Dynamic(Union::Str(rhs_value)) => match lhs_value {
|
||||||
}
|
|
||||||
Dynamic(Union::Str(rhs_value)) => {
|
|
||||||
// Only allows String or char
|
// Only allows String or char
|
||||||
match lhs_value {
|
Dynamic(Union::Str(s)) => Ok(rhs_value.contains(s.as_ref()).into()),
|
||||||
Dynamic(Union::Str(s)) => Ok(rhs_value.contains(s.as_ref()).into()),
|
Dynamic(Union::Char(c)) => Ok(rhs_value.contains(c).into()),
|
||||||
Dynamic(Union::Char(c)) => Ok(rhs_value.contains(c).into()),
|
_ => Err(Box::new(EvalAltResult::ErrorInExpr(lhs.position()))),
|
||||||
_ => Err(Box::new(EvalAltResult::ErrorInExpr(lhs.position()))),
|
},
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => Err(Box::new(EvalAltResult::ErrorInExpr(rhs.position()))),
|
_ => Err(Box::new(EvalAltResult::ErrorInExpr(rhs.position()))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1178,18 +1164,14 @@ impl Engine {
|
|||||||
*pos,
|
*pos,
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
Some((index, ScopeEntryType::Normal)) => {
|
|
||||||
// Avoid referencing scope which is used below as mut
|
|
||||||
*scope.get_mut(index).0 = rhs_val.clone();
|
|
||||||
Ok(rhs_val)
|
|
||||||
}
|
|
||||||
|
|
||||||
Some((_, ScopeEntryType::Constant)) => Err(Box::new(
|
Some((_, ScopeEntryType::Constant)) => Err(Box::new(
|
||||||
EvalAltResult::ErrorAssignmentToConstant(name.to_string(), *op_pos),
|
EvalAltResult::ErrorAssignmentToConstant(name.to_string(), *op_pos),
|
||||||
)),
|
)),
|
||||||
|
Some((index, ScopeEntryType::Normal)) => {
|
||||||
|
*scope.get_mut(index).0 = rhs_val;
|
||||||
|
Ok(Default::default())
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// idx_lhs[idx_expr] = rhs
|
// idx_lhs[idx_expr] = rhs
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Expr::Index(idx_lhs, idx_expr, op_pos) => {
|
Expr::Index(idx_lhs, idx_expr, op_pos) => {
|
||||||
@ -1213,7 +1195,6 @@ impl Engine {
|
|||||||
lhs.position(),
|
lhs.position(),
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Syntax error
|
// Syntax error
|
||||||
_ => Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS(
|
_ => Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS(
|
||||||
lhs.position(),
|
lhs.position(),
|
||||||
@ -1234,30 +1215,23 @@ impl Engine {
|
|||||||
),
|
),
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Expr::Array(contents, _) => {
|
Expr::Array(contents, _) => Ok(Dynamic(Union::Array(Box::new(
|
||||||
let mut arr = Array::new();
|
contents
|
||||||
|
.into_iter()
|
||||||
contents.into_iter().try_for_each(|item| {
|
.map(|item| self.eval_expr(scope, state, fn_lib, item, level))
|
||||||
self.eval_expr(scope, state, fn_lib, item, level)
|
.collect::<Result<Vec<_>, _>>()?,
|
||||||
.map(|val| arr.push(val))
|
)))),
|
||||||
})?;
|
|
||||||
|
|
||||||
Ok(Dynamic(Union::Array(Box::new(arr))))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
Expr::Map(contents, _) => {
|
Expr::Map(contents, _) => Ok(Dynamic(Union::Map(Box::new(
|
||||||
let mut map = Map::new();
|
contents
|
||||||
|
.into_iter()
|
||||||
contents.into_iter().try_for_each(|(key, expr, _)| {
|
.map(|(key, expr, _)| {
|
||||||
self.eval_expr(scope, state, fn_lib, &expr, level)
|
self.eval_expr(scope, state, fn_lib, &expr, level)
|
||||||
.map(|val| {
|
.map(|val| (key.clone(), val))
|
||||||
map.insert(key.clone(), val);
|
})
|
||||||
})
|
.collect::<Result<HashMap<_, _>, _>>()?,
|
||||||
})?;
|
)))),
|
||||||
|
|
||||||
Ok(Dynamic(Union::Map(Box::new(map))))
|
|
||||||
}
|
|
||||||
|
|
||||||
Expr::FunctionCall(fn_name, arg_exprs, def_val, pos) => {
|
Expr::FunctionCall(fn_name, arg_exprs, def_val, pos) => {
|
||||||
let mut arg_values = arg_exprs
|
let mut arg_values = arg_exprs
|
||||||
@ -1287,9 +1261,8 @@ impl Engine {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normal function call
|
// Normal function call - except for eval (handled above)
|
||||||
let def_val = def_val.as_deref();
|
self.exec_fn_call(fn_lib, fn_name, &mut args, def_val.as_deref(), *pos, level)
|
||||||
self.exec_fn_call(fn_lib, fn_name, &mut args, def_val, *pos, level)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Expr::In(lhs, rhs, _) => {
|
Expr::In(lhs, rhs, _) => {
|
||||||
@ -1345,7 +1318,7 @@ impl Engine {
|
|||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
match stmt {
|
match stmt {
|
||||||
// No-op
|
// No-op
|
||||||
Stmt::Noop(_) => Ok(().into()),
|
Stmt::Noop(_) => Ok(Default::default()),
|
||||||
|
|
||||||
// Expression as statement
|
// Expression as statement
|
||||||
Stmt::Expr(expr) => {
|
Stmt::Expr(expr) => {
|
||||||
@ -1353,7 +1326,7 @@ impl Engine {
|
|||||||
|
|
||||||
Ok(if let Expr::Assignment(_, _, _) = *expr.as_ref() {
|
Ok(if let Expr::Assignment(_, _, _) = *expr.as_ref() {
|
||||||
// If it is an assignment, erase the result at the root
|
// If it is an assignment, erase the result at the root
|
||||||
().into()
|
Default::default()
|
||||||
} else {
|
} else {
|
||||||
result
|
result
|
||||||
})
|
})
|
||||||
@ -1363,7 +1336,7 @@ impl Engine {
|
|||||||
Stmt::Block(block, _) => {
|
Stmt::Block(block, _) => {
|
||||||
let prev_len = scope.len();
|
let prev_len = scope.len();
|
||||||
|
|
||||||
let result = block.iter().try_fold(().into(), |_, stmt| {
|
let result = block.iter().try_fold(Default::default(), |_, stmt| {
|
||||||
self.eval_stmt(scope, state, fn_lib, stmt, level)
|
self.eval_stmt(scope, state, fn_lib, stmt, level)
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1387,7 +1360,7 @@ impl Engine {
|
|||||||
} else if let Some(stmt) = else_body {
|
} else if let Some(stmt) = else_body {
|
||||||
self.eval_stmt(scope, state, fn_lib, stmt.as_ref(), level)
|
self.eval_stmt(scope, state, fn_lib, stmt.as_ref(), level)
|
||||||
} else {
|
} else {
|
||||||
Ok(().into())
|
Ok(Default::default())
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@ -1401,11 +1374,11 @@ impl Engine {
|
|||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => match *err {
|
Err(err) => match *err {
|
||||||
EvalAltResult::ErrorLoopBreak(false, _) => (),
|
EvalAltResult::ErrorLoopBreak(false, _) => (),
|
||||||
EvalAltResult::ErrorLoopBreak(true, _) => return Ok(().into()),
|
EvalAltResult::ErrorLoopBreak(true, _) => return Ok(Default::default()),
|
||||||
_ => return Err(err),
|
_ => return Err(err),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Ok(false) => return Ok(().into()),
|
Ok(false) => return Ok(Default::default()),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return Err(Box::new(EvalAltResult::ErrorLogicGuard(guard.position())))
|
return Err(Box::new(EvalAltResult::ErrorLogicGuard(guard.position())))
|
||||||
}
|
}
|
||||||
@ -1418,7 +1391,7 @@ impl Engine {
|
|||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => match *err {
|
Err(err) => match *err {
|
||||||
EvalAltResult::ErrorLoopBreak(false, _) => (),
|
EvalAltResult::ErrorLoopBreak(false, _) => (),
|
||||||
EvalAltResult::ErrorLoopBreak(true, _) => return Ok(().into()),
|
EvalAltResult::ErrorLoopBreak(true, _) => return Ok(Default::default()),
|
||||||
_ => return Err(err),
|
_ => return Err(err),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -1453,7 +1426,7 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
scope.rewind(scope.len() - 1);
|
scope.rewind(scope.len() - 1);
|
||||||
Ok(().into())
|
Ok(Default::default())
|
||||||
} else {
|
} else {
|
||||||
Err(Box::new(EvalAltResult::ErrorFor(expr.position())))
|
Err(Box::new(EvalAltResult::ErrorFor(expr.position())))
|
||||||
}
|
}
|
||||||
@ -1467,7 +1440,7 @@ impl Engine {
|
|||||||
|
|
||||||
// Empty return
|
// Empty return
|
||||||
Stmt::ReturnWithVal(None, ReturnType::Return, pos) => {
|
Stmt::ReturnWithVal(None, ReturnType::Return, pos) => {
|
||||||
Err(Box::new(EvalAltResult::Return(().into(), *pos)))
|
Err(Box::new(EvalAltResult::Return(Default::default(), *pos)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return value
|
// Return value
|
||||||
@ -1494,13 +1467,13 @@ impl Engine {
|
|||||||
let val = self.eval_expr(scope, state, fn_lib, expr, level)?;
|
let val = self.eval_expr(scope, state, fn_lib, expr, level)?;
|
||||||
// TODO - avoid copying variable name in inner block?
|
// TODO - avoid copying variable name in inner block?
|
||||||
scope.push_dynamic_value(name.clone(), ScopeEntryType::Normal, val, false);
|
scope.push_dynamic_value(name.clone(), ScopeEntryType::Normal, val, false);
|
||||||
Ok(().into())
|
Ok(Default::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
Stmt::Let(name, None, _) => {
|
Stmt::Let(name, None, _) => {
|
||||||
// TODO - avoid copying variable name in inner block?
|
// TODO - avoid copying variable name in inner block?
|
||||||
scope.push(name.clone(), ());
|
scope.push(name.clone(), ());
|
||||||
Ok(().into())
|
Ok(Default::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Const statement
|
// Const statement
|
||||||
@ -1508,7 +1481,7 @@ impl Engine {
|
|||||||
let val = self.eval_expr(scope, state, fn_lib, expr, level)?;
|
let val = self.eval_expr(scope, state, fn_lib, expr, level)?;
|
||||||
// TODO - avoid copying variable name in inner block?
|
// TODO - avoid copying variable name in inner block?
|
||||||
scope.push_dynamic_value(name.clone(), ScopeEntryType::Constant, val, true);
|
scope.push_dynamic_value(name.clone(), ScopeEntryType::Constant, val, true);
|
||||||
Ok(().into())
|
Ok(Default::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
Stmt::Const(_, _, _) => panic!("constant expression not constant!"),
|
Stmt::Const(_, _, _) => panic!("constant expression not constant!"),
|
||||||
|
Loading…
Reference in New Issue
Block a user