Change field names of EvalState.

This commit is contained in:
Stephen Chung 2021-07-26 10:03:46 +08:00
parent c7498503ba
commit 2c50738c6c
3 changed files with 18 additions and 17 deletions

View File

@ -653,19 +653,20 @@ pub struct EvalState {
/// Source of the current context. /// Source of the current context.
pub source: Option<Identifier>, pub source: Option<Identifier>,
/// Normally, access to variables are parsed with a relative offset into the [`Scope`] to avoid a lookup. /// Normally, access to variables are parsed with a relative offset into the [`Scope`] to avoid a lookup.
/// In some situation, e.g. after running an `eval` statement, subsequent offsets may become mis-aligned. /// In some situation, e.g. after running an `eval` statement, or after a custom syntax statement,
/// subsequent offsets may become mis-aligned.
/// When that happens, this flag is turned on to force a [`Scope`] search by name. /// When that happens, this flag is turned on to force a [`Scope`] search by name.
pub always_search_scope: bool, pub always_search_scope: bool,
/// Level of the current scope. The global (root) level is zero, a new block (or function call) /// Level of the current scope. The global (root) level is zero, a new block (or function call)
/// is one level higher, and so on. /// is one level higher, and so on.
pub scope_level: usize, pub scope_level: usize,
/// Number of operations performed. /// Number of operations performed.
pub operations: u64, pub num_operations: u64,
/// Number of modules loaded. /// Number of modules loaded.
pub modules: usize, pub num_modules: usize,
/// Embedded module resolver. /// Embedded module resolver.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
pub resolver: Option<Shared<crate::module::resolvers::StaticModuleResolver>>, pub embedded_module_resolver: Option<Shared<crate::module::resolvers::StaticModuleResolver>>,
/// Stack of function resolution caches. /// Stack of function resolution caches.
fn_resolution_caches: Vec<FnResolutionCache>, fn_resolution_caches: Vec<FnResolutionCache>,
} }
@ -679,10 +680,10 @@ impl EvalState {
source: None, source: None,
always_search_scope: false, always_search_scope: false,
scope_level: 0, scope_level: 0,
operations: 0, num_operations: 0,
modules: 0, num_modules: 0,
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
resolver: None, embedded_module_resolver: None,
fn_resolution_caches: Vec::new(), fn_resolution_caches: Vec::new(),
} }
} }
@ -2914,7 +2915,7 @@ impl Engine {
Stmt::Import(expr, export, _pos) => { Stmt::Import(expr, export, _pos) => {
// Guard against too many modules // Guard against too many modules
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
if state.modules >= self.max_modules() { if state.num_modules >= self.max_modules() {
return EvalAltResult::ErrorTooManyModules(*_pos).into(); return EvalAltResult::ErrorTooManyModules(*_pos).into();
} }
@ -2928,7 +2929,7 @@ impl Engine {
let path_pos = expr.position(); let path_pos = expr.position();
let module = state let module = state
.resolver .embedded_module_resolver
.as_ref() .as_ref()
.and_then(|r| match r.resolve(self, source, &path, path_pos) { .and_then(|r| match r.resolve(self, source, &path, path_pos) {
Err(err) Err(err)
@ -2958,7 +2959,7 @@ impl Engine {
} }
} }
state.modules += 1; state.num_modules += 1;
Ok(Dynamic::UNIT) Ok(Dynamic::UNIT)
} else { } else {
@ -3130,16 +3131,16 @@ impl Engine {
state: &mut EvalState, state: &mut EvalState,
pos: Position, pos: Position,
) -> Result<(), Box<EvalAltResult>> { ) -> Result<(), Box<EvalAltResult>> {
state.operations += 1; state.num_operations += 1;
// Guard against too many operations // Guard against too many operations
if self.max_operations() > 0 && state.operations > self.max_operations() { if self.max_operations() > 0 && state.num_operations > self.max_operations() {
return EvalAltResult::ErrorTooManyOperations(pos).into(); return EvalAltResult::ErrorTooManyOperations(pos).into();
} }
// Report progress - only in steps // Report progress - only in steps
if let Some(ref progress) = self.progress { if let Some(ref progress) = self.progress {
if let Some(token) = progress(state.operations) { if let Some(token) = progress(state.num_operations) {
// Terminate script if progress returns a termination token // Terminate script if progress returns a termination token
return EvalAltResult::ErrorTerminated(token, pos).into(); return EvalAltResult::ErrorTerminated(token, pos).into();
} }

View File

@ -1729,7 +1729,7 @@ impl Engine {
state.source = ast.source_raw().cloned(); state.source = ast.source_raw().cloned();
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
{ {
state.resolver = ast.resolver(); state.embedded_module_resolver = ast.resolver();
} }
let statements = ast.statements(); let statements = ast.statements();
@ -1811,7 +1811,7 @@ impl Engine {
state.source = ast.source_raw().cloned(); state.source = ast.source_raw().cloned();
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
{ {
state.resolver = ast.resolver(); state.embedded_module_resolver = ast.resolver();
} }
let statements = ast.statements(); let statements = ast.statements();

View File

@ -840,12 +840,12 @@ impl Engine {
// Evaluate the AST // Evaluate the AST
let mut new_state = EvalState::new(); let mut new_state = EvalState::new();
new_state.source = state.source.clone(); new_state.source = state.source.clone();
new_state.operations = state.operations; new_state.num_operations = state.num_operations;
let result = let result =
self.eval_global_statements(scope, mods, &mut new_state, statements, lib, level); self.eval_global_statements(scope, mods, &mut new_state, statements, lib, level);
state.operations = new_state.operations; state.num_operations = new_state.num_operations;
result result
} }