Cleanup.
This commit is contained in:
@@ -44,44 +44,39 @@ impl FnResolutionCache {
|
||||
/// The following caches are contained inside this type:
|
||||
/// * A stack of [function resolution caches][FnResolutionCache]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Caches {
|
||||
/// Stack of [function resolution caches][FnResolutionCache].
|
||||
stack: StaticVec<FnResolutionCache>,
|
||||
}
|
||||
pub struct Caches(StaticVec<FnResolutionCache>);
|
||||
|
||||
impl Caches {
|
||||
/// Create an empty [`Caches`].
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
stack: StaticVec::new_const(),
|
||||
}
|
||||
Self(StaticVec::new_const())
|
||||
}
|
||||
/// Get the number of function resolution cache(s) in the stack.
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub fn fn_resolution_caches_len(&self) -> usize {
|
||||
self.stack.len()
|
||||
self.0.len()
|
||||
}
|
||||
/// Get a mutable reference to the current function resolution cache.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn fn_resolution_cache_mut(&mut self) -> &mut FnResolutionCache {
|
||||
if self.stack.is_empty() {
|
||||
if self.0.is_empty() {
|
||||
// Push a new function resolution cache if the stack is empty
|
||||
self.push_fn_resolution_cache();
|
||||
}
|
||||
self.stack.last_mut().unwrap()
|
||||
self.0.last_mut().unwrap()
|
||||
}
|
||||
/// Push an empty function resolution cache onto the stack and make it current.
|
||||
#[inline(always)]
|
||||
pub fn push_fn_resolution_cache(&mut self) {
|
||||
self.stack.push(Default::default());
|
||||
self.0.push(Default::default());
|
||||
}
|
||||
/// Rewind the function resolution caches stack to a particular size.
|
||||
#[inline(always)]
|
||||
pub fn rewind_fn_resolution_caches(&mut self, len: usize) {
|
||||
self.stack.truncate(len);
|
||||
self.0.truncate(len);
|
||||
}
|
||||
}
|
||||
|
@@ -742,8 +742,9 @@ impl Engine {
|
||||
let fn_name = crate::engine::FN_IDX_GET;
|
||||
let pos = Position::NONE;
|
||||
|
||||
let orig_level = global.level;
|
||||
global.level += 1;
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| g.level -= 1);
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| g.level = orig_level);
|
||||
|
||||
self.exec_native_fn_call(global, caches, fn_name, None, hash, args, true, pos)
|
||||
.map(|(r, ..)| r)
|
||||
@@ -765,8 +766,9 @@ impl Engine {
|
||||
let fn_name = crate::engine::FN_IDX_SET;
|
||||
let pos = Position::NONE;
|
||||
|
||||
let orig_level = global.level;
|
||||
global.level += 1;
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| g.level -= 1);
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| g.level = orig_level);
|
||||
|
||||
self.exec_native_fn_call(global, caches, fn_name, None, hash, args, is_ref_mut, pos)
|
||||
}
|
||||
|
@@ -147,15 +147,11 @@ impl Engine {
|
||||
return Err(ERR::ErrorTooManyOperations(pos).into());
|
||||
}
|
||||
|
||||
// Report progress - only in steps
|
||||
if let Some(ref progress) = self.progress {
|
||||
if let Some(token) = progress(num_operations) {
|
||||
// Terminate script if progress returns a termination token
|
||||
return Err(ERR::ErrorTerminated(token, pos).into());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
// Report progress
|
||||
self.progress
|
||||
.as_ref()
|
||||
.and_then(|p| p(num_operations))
|
||||
.map_or(Ok(()), |token| Err(ERR::ErrorTerminated(token, pos).into()))
|
||||
}
|
||||
|
||||
/// Check a result to ensure that it is valid.
|
||||
|
@@ -11,12 +11,12 @@ use std::prelude::v1::*;
|
||||
pub struct EvalContext<'a, 's, 'ps, 'g, 'c, 't> {
|
||||
/// The current [`Engine`].
|
||||
engine: &'a Engine,
|
||||
/// The current [`Scope`].
|
||||
scope: &'s mut Scope<'ps>,
|
||||
/// The current [`GlobalRuntimeState`].
|
||||
global: &'g mut GlobalRuntimeState,
|
||||
/// The current [caches][Caches], if available.
|
||||
caches: &'c mut Caches,
|
||||
/// The current [`Scope`].
|
||||
scope: &'s mut Scope<'ps>,
|
||||
/// The current bound `this` pointer, if any.
|
||||
this_ptr: &'t mut Dynamic,
|
||||
}
|
||||
@@ -34,9 +34,9 @@ impl<'a, 's, 'ps, 'g, 'c, 't> EvalContext<'a, 's, 'ps, 'g, 'c, 't> {
|
||||
) -> Self {
|
||||
Self {
|
||||
engine,
|
||||
scope,
|
||||
global,
|
||||
caches,
|
||||
scope,
|
||||
this_ptr,
|
||||
}
|
||||
}
|
||||
@@ -100,12 +100,18 @@ impl<'a, 's, 'ps, 'g, 'c, 't> EvalContext<'a, 's, 'ps, 'g, 'c, 't> {
|
||||
self.global
|
||||
}
|
||||
/// Get an iterator over the namespaces containing definition of all script-defined functions.
|
||||
///
|
||||
/// Not available under `no_function`.
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
#[inline]
|
||||
pub fn iter_namespaces(&self) -> impl Iterator<Item = &Module> {
|
||||
self.global.lib.iter().map(|m| m.as_ref())
|
||||
}
|
||||
/// _(internals)_ The current set of namespaces containing definitions of all script-defined functions.
|
||||
/// Exported under the `internals` feature only.
|
||||
///
|
||||
/// Not available under `no_function`.
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
#[cfg(feature = "internals")]
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
|
@@ -29,7 +29,8 @@ pub struct GlobalRuntimeState {
|
||||
/// Stack of imported [modules][crate::Module].
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
modules: StaticVec<SharedModule>,
|
||||
/// The current stack of loaded [modules][Module].
|
||||
/// The current stack of loaded [modules][crate::Module] containing script-defined functions.
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
pub lib: StaticVec<SharedModule>,
|
||||
/// Source of the current context.
|
||||
///
|
||||
@@ -86,6 +87,7 @@ impl GlobalRuntimeState {
|
||||
imports: StaticVec::new_const(),
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
modules: StaticVec::new_const(),
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
lib: StaticVec::new_const(),
|
||||
source: None,
|
||||
num_operations: 0,
|
||||
@@ -333,22 +335,31 @@ impl fmt::Debug for GlobalRuntimeState {
|
||||
let mut f = f.debug_struct("GlobalRuntimeState");
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
f.field("imports", &self.scan_imports_raw().collect::<Vec<_>>());
|
||||
f.field("imports", &self.scan_imports_raw().collect::<Vec<_>>())
|
||||
.field("num_modules_loaded", &self.num_modules_loaded)
|
||||
.field("embedded_module_resolver", &self.embedded_module_resolver);
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
f.field("lib", &self.lib);
|
||||
|
||||
f.field("source", &self.source)
|
||||
.field("num_operations", &self.num_operations);
|
||||
.field("num_operations", &self.num_operations)
|
||||
.field("level", &self.level)
|
||||
.field("scope_level", &self.scope_level)
|
||||
.field("always_search_scope", &self.always_search_scope);
|
||||
|
||||
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||
f.field("fn_hash_indexing", &self.fn_hash_indexing);
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
f.field("num_modules_loaded", &self.num_modules_loaded)
|
||||
.field("embedded_module_resolver", &self.embedded_module_resolver);
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
f.field("constants", &self.constants);
|
||||
|
||||
f.field("tag", &self.tag);
|
||||
|
||||
#[cfg(feature = "debugging")]
|
||||
f.field("debugger", &self.debugger);
|
||||
|
||||
f.finish()
|
||||
}
|
||||
}
|
||||
|
@@ -141,8 +141,9 @@ impl Engine {
|
||||
// Built-in found
|
||||
let op = op_assign_token.literal_syntax();
|
||||
|
||||
let orig_level = global.level;
|
||||
global.level += 1;
|
||||
let global = &*RestoreOnDrop::lock(global, move |g| g.level -= 1);
|
||||
let global = &*RestoreOnDrop::lock(global, move |g| g.level = orig_level);
|
||||
|
||||
let context = (self, op, None, global, *op_pos).into();
|
||||
return func(context, args).map(|_| ());
|
||||
|
Reference in New Issue
Block a user