Avoid copying property name for map property access.

This commit is contained in:
Stephen Chung 2020-07-21 20:59:12 +08:00
parent ec3074106e
commit e6c3f8134d
3 changed files with 19 additions and 29 deletions

View File

@ -188,7 +188,7 @@ impl Target<'_> {
}
}
/// Update the value of the `Target`.
/// Position in `EvalAltResult` is None and must be set afterwards.
/// Position in `EvalAltResult` is `None` and must be set afterwards.
pub fn set_value(&mut self, new_val: Dynamic) -> Result<(), Box<EvalAltResult>> {
match self {
Self::Ref(r) => **r = new_val,
@ -419,12 +419,9 @@ pub fn make_getter(id: &str) -> String {
fn extract_prop_from_getter(fn_name: &str) -> Option<&str> {
#[cfg(not(feature = "no_object"))]
if fn_name.starts_with(FN_GET) {
Some(&fn_name[FN_GET.len()..])
} else {
None
return Some(&fn_name[FN_GET.len()..]);
}
#[cfg(feature = "no_object")]
None
}
@ -437,12 +434,9 @@ pub fn make_setter(id: &str) -> String {
fn extract_prop_from_setter(fn_name: &str) -> Option<&str> {
#[cfg(not(feature = "no_object"))]
if fn_name.starts_with(FN_SET) {
Some(&fn_name[FN_SET.len()..])
} else {
None
return Some(&fn_name[FN_SET.len()..]);
}
#[cfg(feature = "no_object")]
None
}
@ -454,7 +448,7 @@ fn default_print(s: &str) {
}
/// Search for a module within an imports stack.
/// Position in `EvalAltResult` is None and must be set afterwards.
/// Position in `EvalAltResult` is `None` and must be set afterwards.
fn search_imports<'s>(
mods: &'s Imports,
state: &mut State,
@ -487,7 +481,7 @@ fn search_imports<'s>(
}
/// Search for a module within an imports stack.
/// Position in `EvalAltResult` is None and must be set afterwards.
/// Position in `EvalAltResult` is `None` and must be set afterwards.
fn search_imports_mut<'s>(
mods: &'s mut Imports,
state: &mut State,
@ -637,7 +631,7 @@ impl Engine {
}
/// Universal method for calling functions either registered with the `Engine` or written in Rhai.
/// Position in `EvalAltResult` is None and must be set afterwards.
/// Position in `EvalAltResult` is `None` and must be set afterwards.
///
/// ## WARNING
///
@ -878,7 +872,7 @@ impl Engine {
}
/// Call a script-defined function.
/// Position in `EvalAltResult` is None and must be set afterwards.
/// Position in `EvalAltResult` is `None` and must be set afterwards.
///
/// ## WARNING
///
@ -960,7 +954,7 @@ impl Engine {
}
/// Perform an actual function call, taking care of special functions
/// Position in `EvalAltResult` is None and must be set afterwards.
/// Position in `EvalAltResult` is `None` and must be set afterwards.
///
/// ## WARNING
///
@ -1023,7 +1017,7 @@ impl Engine {
}
/// Evaluate a text string as a script - used primarily for 'eval'.
/// Position in `EvalAltResult` is None and must be set afterwards.
/// Position in `EvalAltResult` is `None` and must be set afterwards.
fn eval_script_expr(
&self,
scope: &mut Scope,
@ -1161,7 +1155,7 @@ impl Engine {
}
/// Chain-evaluate a dot/index chain.
/// Position in `EvalAltResult` is None and must be set afterwards.
/// Position in `EvalAltResult` is `None` and must be set afterwards.
fn eval_dot_index_chain_helper(
&self,
state: &mut State,
@ -1210,14 +1204,14 @@ impl Engine {
}
// xxx[rhs] = new_val
_ if new_val.is_some() => {
let mut new_val = new_val.unwrap();
let mut idx_val2 = idx_val.clone();
match self.get_indexed_mut(state, lib, target, idx_val, pos, true, level) {
// Indexed value is an owned value - the only possibility is an indexer
// Try to call an index setter
Ok(obj_ptr) if obj_ptr.is_value() => {
let args =
&mut [target.as_mut(), &mut idx_val2, &mut new_val.unwrap()];
let args = &mut [target.as_mut(), &mut idx_val2, &mut new_val];
self.exec_fn_call(
state, lib, FN_IDX_SET, true, 0, args, is_ref, true, None,
@ -1236,17 +1230,13 @@ impl Engine {
// Indexed value is a reference - update directly
Ok(ref mut obj_ptr) => {
obj_ptr
.set_value(new_val.unwrap())
.set_value(new_val)
.map_err(|err| err.new_position(rhs.position()))?;
}
Err(err) => match *err {
// No index getter - try to call an index setter
EvalAltResult::ErrorIndexingType(_, _) => {
let args = &mut [
target.as_mut(),
&mut idx_val2,
&mut new_val.unwrap(),
];
let args = &mut [target.as_mut(), &mut idx_val2, &mut new_val];
self.exec_fn_call(
state, lib, FN_IDX_SET, true, 0, args, is_ref, true, None,
@ -2632,7 +2622,7 @@ impl Engine {
}
/// Check if the number of operations stay within limit.
/// Position in `EvalAltResult` is None and must be set afterwards.
/// Position in `EvalAltResult` is `None` and must be set afterwards.
fn inc_operations(&self, state: &mut State) -> Result<(), Box<EvalAltResult>> {
state.operations += 1;

View File

@ -400,7 +400,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
// All other items can be thrown away.
state.set_dirty();
let pos = m.1;
m.0.into_iter().find(|((name, _), _)| name.as_str() == prop)
m.0.into_iter().find(|((name, _), _)| name.as_str() == prop.as_str())
.map(|(_, expr)| expr.set_position(pos))
.unwrap_or_else(|| Expr::Unit(pos))
}

View File

@ -643,7 +643,7 @@ pub enum Expr {
)>,
),
/// Property access.
Property(Box<((String, String, String), Position)>),
Property(Box<((ImmutableString, String, String), Position)>),
/// { stmt }
Stmt(Box<(Stmt, Position)>),
/// Wrapped expression - should not be optimized away.
@ -937,7 +937,7 @@ impl Expr {
let (name, pos) = x.0;
let getter = make_getter(&name);
let setter = make_setter(&name);
Self::Property(Box::new(((name.clone(), getter, setter), pos)))
Self::Property(Box::new(((name.into(), getter, setter), pos)))
}
_ => self,
}
@ -1834,7 +1834,7 @@ fn make_dot_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result<Expr, ParseEr
let getter = make_getter(&name);
let setter = make_setter(&name);
let rhs = Expr::Property(Box::new(((name, getter, setter), pos)));
let rhs = Expr::Property(Box::new(((name.into(), getter, setter), pos)));
Expr::Dot(Box::new((lhs, rhs, op_pos)))
}