Skip eval if statements are empty.

This commit is contained in:
Stephen Chung 2021-04-17 22:19:34 +08:00
parent 90198d5440
commit 2efe9d08a4
2 changed files with 23 additions and 10 deletions

View File

@ -1696,6 +1696,11 @@ impl Engine {
} }
let statements = ast.statements(); let statements = ast.statements();
if statements.is_empty() {
return Ok(Dynamic::UNIT);
}
let lib = &[ast.lib()]; let lib = &[ast.lib()];
self.eval_global_statements(scope, mods, &mut state, statements, lib, level) self.eval_global_statements(scope, mods, &mut state, statements, lib, level)
} }
@ -1771,9 +1776,12 @@ impl Engine {
{ {
state.resolver = ast.resolver(); state.resolver = ast.resolver();
} }
let statements = ast.statements(); let statements = ast.statements();
if !statements.is_empty() {
let lib = &[ast.lib()]; let lib = &[ast.lib()];
self.eval_global_statements(scope, mods, &mut state, statements, lib, 0)?; self.eval_global_statements(scope, mods, &mut state, statements, lib, 0)?;
}
Ok(()) Ok(())
} }
/// Call a script function defined in an [`AST`] with multiple arguments. /// Call a script function defined in an [`AST`] with multiple arguments.
@ -1931,21 +1939,21 @@ impl Engine {
let state = &mut Default::default(); let state = &mut Default::default();
let mods = &mut Default::default(); let mods = &mut Default::default();
let lib = &[ast.lib()]; let lib = &[ast.lib()];
let statements = ast.statements();
let name = name.as_ref();
if eval_ast { if eval_ast && !statements.is_empty() {
self.eval_global_statements(scope, mods, state, ast.statements(), lib, 0)?; self.eval_global_statements(scope, mods, state, statements, lib, 0)?;
} }
let fn_def = ast let fn_def = ast
.lib() .lib()
.get_script_fn(name.as_ref(), args.len()) .get_script_fn(name, args.len())
.ok_or_else(|| { .ok_or_else(|| EvalAltResult::ErrorFunctionNotFound(name.into(), Position::NONE))?;
EvalAltResult::ErrorFunctionNotFound(name.as_ref().into(), Position::NONE)
})?;
// Check for data race. // Check for data race.
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
crate::fn_call::ensure_no_data_race(name.as_ref(), args, false)?; crate::fn_call::ensure_no_data_race(name, args, false)?;
self.call_script_fn( self.call_script_fn(
scope, scope,

View File

@ -867,13 +867,18 @@ impl Engine {
return Err(ParseErrorType::WrongFnDefinition.into()); return Err(ParseErrorType::WrongFnDefinition.into());
} }
let statements = ast.statements();
if statements.is_empty() {
return Ok(Dynamic::UNIT);
}
// Evaluate the AST // Evaluate the AST
let mut new_state: State = Default::default(); let mut new_state: State = Default::default();
new_state.source = state.source.clone(); new_state.source = state.source.clone();
new_state.operations = state.operations; new_state.operations = state.operations;
let result = let result =
self.eval_global_statements(scope, mods, &mut new_state, ast.statements(), lib, level); self.eval_global_statements(scope, mods, &mut new_state, statements, lib, level);
state.operations = new_state.operations; state.operations = new_state.operations;