Minor refactor.

This commit is contained in:
Stephen Chung 2021-12-02 12:49:46 +08:00
parent 4b4b7a753b
commit d36e2d22d1
4 changed files with 8 additions and 9 deletions

View File

@ -335,7 +335,6 @@ impl Engine {
/// * `Ok(None)`: parsing complete and there are no more symbols to match.
/// * `Ok(Some(symbol))`: the next symbol to match, which can also be `$expr$`, `$ident$` or `$block$`.
/// * `Err(ParseError)`: error that is reflected back to the [`Engine`], normally `ParseError(ParseErrorType::BadInput(LexError::ImproperSymbol(message)), Position::NONE)` to indicate a syntax error, but it can be any [`ParseError`].
///
pub fn register_custom_syntax_raw(
&mut self,
key: impl Into<Identifier>,

View File

@ -313,7 +313,7 @@ impl Engine {
name: impl AsRef<str>,
hash: u64,
args: &mut FnCallArgs,
is_method_call: bool,
is_ref_mut: bool,
is_op_assign: bool,
pos: Position,
) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
@ -331,7 +331,8 @@ impl Engine {
// Calling pure function but the first argument is a reference?
let mut backup: Option<ArgBackup> = None;
if is_method_call && func.is_pure() && !args.is_empty() {
if is_ref_mut && func.is_pure() && !args.is_empty() {
// Clone the first argument
backup = Some(ArgBackup::new());
backup
.as_mut()
@ -397,6 +398,8 @@ impl Engine {
});
}
// Error handling
match name {
// index getter function not found?
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]

View File

@ -22,9 +22,6 @@ use std::{
ops::DerefMut,
};
#[cfg(not(feature = "no_closure"))]
use crate::engine::KEYWORD_IS_SHARED;
/// Level of optimization performed.
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
pub enum OptimizationLevel {
@ -986,7 +983,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
return;
}
#[cfg(not(feature = "no_closure"))]
KEYWORD_IS_SHARED if arg_values.len() == 1 => {
crate::engine::KEYWORD_IS_SHARED if arg_values.len() == 1 => {
state.set_dirty();
*expr = Expr::from_dynamic(Dynamic::FALSE, *pos);
return;
@ -1046,7 +1043,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
let result = match x.name.as_str() {
KEYWORD_TYPE_OF if arg_values.len() == 1 => Some(state.engine.map_type_name(arg_values[0].type_name()).into()),
#[cfg(not(feature = "no_closure"))]
KEYWORD_IS_SHARED if arg_values.len() == 1 => Some(Dynamic::FALSE),
crate::engine::KEYWORD_IS_SHARED if arg_values.len() == 1 => Some(Dynamic::FALSE),
_ => state.call_fn_with_constant_arguments(&x.name, arg_values)
};

View File

@ -143,7 +143,7 @@ fn test_fn_ptr_make_closure() -> Result<(), Box<EvalAltResult>> {
let fn_ptr = engine.eval_ast::<FnPtr>(&ast)?;
move |x: INT| -> Result<String, Box<EvalAltResult>> { fn_ptr.call(&engine, &ast, (x,)) }
move |x: INT| -> Result<String, _> { fn_ptr.call(&engine, &ast, (x,)) }
};
// 'f' captures: the Engine, the AST, and the closure