Rename EvalState to Caches.

This commit is contained in:
Stephen Chung
2022-04-16 16:36:53 +08:00
parent b696390c13
commit 855cb76246
19 changed files with 349 additions and 354 deletions

69
src/eval/cache.rs Normal file
View File

@@ -0,0 +1,69 @@
//! System caches.
use crate::func::CallableFunction;
use crate::{Identifier, StaticVec};
use std::collections::BTreeMap;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
/// _(internals)_ An entry in a function resolution cache.
/// Exported under the `internals` feature only.
#[derive(Debug, Clone)]
pub struct FnResolutionCacheEntry {
/// Function.
pub func: CallableFunction,
/// Optional source.
/// No source if the string is empty.
pub source: Identifier,
}
/// _(internals)_ A function resolution cache.
/// Exported under the `internals` feature only.
///
/// [`FnResolutionCacheEntry`] is [`Box`]ed in order to pack as many entries inside a single B-Tree
/// level as possible.
pub type FnResolutionCache = BTreeMap<u64, Option<Box<FnResolutionCacheEntry>>>;
/// _(internals)_ A type containing system-wide caches.
/// Exported under the `internals` feature only.
///
/// The following caches are contained inside this type:
/// * A stack of [function resolution caches][FnResolutionCache]
#[derive(Debug, Clone)]
pub struct Caches(StaticVec<FnResolutionCache>);
impl Caches {
/// Create an empty [`Caches`].
#[inline(always)]
#[must_use]
pub const fn new() -> Self {
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.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.0.is_empty() {
// Push a new function resolution cache if the stack is empty
self.push_fn_resolution_cache();
}
self.0.last_mut().unwrap()
}
/// Push an empty function resolution cache onto the stack and make it current.
#[allow(dead_code)]
#[inline(always)]
pub fn push_fn_resolution_cache(&mut self) {
self.0.push(BTreeMap::new());
}
/// Rewind the function resolution caches stack to a particular size.
#[inline(always)]
pub fn rewind_fn_resolution_caches(&mut self, len: usize) {
self.0.truncate(len);
}
}

View File

@@ -1,7 +1,7 @@
//! Types to support chaining operations (i.e. indexing and dotting).
#![cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
use super::{EvalState, GlobalRuntimeState, Target};
use super::{Caches, GlobalRuntimeState, Target};
use crate::ast::{ASTFlags, Expr, OpAssignment};
use crate::types::dynamic::Union;
use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, StaticVec, ERR};
@@ -122,7 +122,7 @@ impl Engine {
fn eval_dot_index_chain_helper(
&self,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
target: &mut Target,
@@ -155,7 +155,7 @@ impl Engine {
if !_parent_options.contains(ASTFlags::BREAK) =>
{
#[cfg(feature = "debugging")]
self.run_debugger(scope, global, state, lib, this_ptr, _parent, level)?;
self.run_debugger(scope, global, caches, lib, this_ptr, _parent, level)?;
let mut idx_val_for_setter = idx_val.clone();
let idx_pos = x.lhs.start_position();
@@ -163,14 +163,14 @@ impl Engine {
let (try_setter, result) = {
let mut obj = self.get_indexed_mut(
global, state, lib, target, idx_val, idx_pos, false, true, level,
global, caches, lib, target, idx_val, idx_pos, false, true, level,
)?;
let is_obj_temp_val = obj.is_temp_value();
let obj_ptr = &mut obj;
match self.eval_dot_index_chain_helper(
global, state, lib, this_ptr, obj_ptr, root, rhs, &x.rhs, *options,
idx_values, rhs_chain, level, new_val,
global, caches, lib, this_ptr, obj_ptr, root, rhs, &x.rhs,
*options, idx_values, rhs_chain, level, new_val,
) {
Ok((result, true)) if is_obj_temp_val => {
(Some(obj.take_or_clone()), (result, true))
@@ -185,7 +185,7 @@ impl Engine {
let idx = &mut idx_val_for_setter;
let new_val = &mut new_val;
self.call_indexer_set(
global, state, lib, target, idx, new_val, is_ref_mut, level,
global, caches, lib, target, idx, new_val, is_ref_mut, level,
)
.or_else(|e| match *e {
ERR::ErrorIndexingType(..) => Ok((Dynamic::UNIT, false)),
@@ -198,18 +198,18 @@ impl Engine {
// xxx[rhs] op= new_val
_ if new_val.is_some() => {
#[cfg(feature = "debugging")]
self.run_debugger(scope, global, state, lib, this_ptr, _parent, level)?;
self.run_debugger(scope, global, caches, lib, this_ptr, _parent, level)?;
let ((new_val, new_pos), (op_info, op_pos)) = new_val.expect("`Some`");
let mut idx_val2 = idx_val.clone();
let try_setter = match self.get_indexed_mut(
global, state, lib, target, idx_val, pos, true, false, level,
global, caches, lib, target, idx_val, pos, true, false, level,
) {
// Indexed value is not a temp value - update directly
Ok(ref mut obj_ptr) => {
self.eval_op_assignment(
global, state, lib, op_info, op_pos, obj_ptr, root, new_val,
global, caches, lib, op_info, op_pos, obj_ptr, root, new_val,
level,
)
.map_err(|err| err.fill_position(new_pos))?;
@@ -232,12 +232,12 @@ impl Engine {
let idx = &mut idx.clone();
// Call the index getter to get the current value
if let Ok(val) =
self.call_indexer_get(global, state, lib, target, idx, level)
self.call_indexer_get(global, caches, lib, target, idx, level)
{
let mut res = val.into();
// Run the op-assignment
self.eval_op_assignment(
global, state, lib, op_info, op_pos, &mut res, root,
global, caches, lib, op_info, op_pos, &mut res, root,
new_val, level,
)
.map_err(|err| err.fill_position(new_pos))?;
@@ -251,7 +251,7 @@ impl Engine {
// Try to call index setter
let new_val = &mut new_val;
self.call_indexer_set(
global, state, lib, target, idx, new_val, is_ref_mut, level,
global, caches, lib, target, idx, new_val, is_ref_mut, level,
)?;
}
@@ -260,10 +260,10 @@ impl Engine {
// xxx[rhs]
_ => {
#[cfg(feature = "debugging")]
self.run_debugger(scope, global, state, lib, this_ptr, _parent, level)?;
self.run_debugger(scope, global, caches, lib, this_ptr, _parent, level)?;
self.get_indexed_mut(
global, state, lib, target, idx_val, pos, false, true, level,
global, caches, lib, target, idx_val, pos, false, true, level,
)
.map(|v| (v.take_or_clone(), false))
}
@@ -280,11 +280,11 @@ impl Engine {
#[cfg(feature = "debugging")]
let reset_debugger = self.run_debugger_with_reset(
scope, global, state, lib, this_ptr, rhs, level,
scope, global, caches, lib, this_ptr, rhs, level,
)?;
let result = self.make_method_call(
global, state, lib, name, *hashes, target, call_args, *pos, level,
global, caches, lib, name, *hashes, target, call_args, *pos, level,
);
#[cfg(feature = "debugging")]
@@ -303,16 +303,16 @@ impl Engine {
// {xxx:map}.id op= ???
Expr::Property(x, pos) if target.is::<crate::Map>() && new_val.is_some() => {
#[cfg(feature = "debugging")]
self.run_debugger(scope, global, state, lib, this_ptr, rhs, level)?;
self.run_debugger(scope, global, caches, lib, this_ptr, rhs, level)?;
let index = x.2.clone().into();
let ((new_val, new_pos), (op_info, op_pos)) = new_val.expect("`Some`");
{
let val_target = &mut self.get_indexed_mut(
global, state, lib, target, index, *pos, true, false, level,
global, caches, lib, target, index, *pos, true, false, level,
)?;
self.eval_op_assignment(
global, state, lib, op_info, op_pos, val_target, root, new_val,
global, caches, lib, op_info, op_pos, val_target, root, new_val,
level,
)
.map_err(|err| err.fill_position(new_pos))?;
@@ -324,18 +324,18 @@ impl Engine {
// {xxx:map}.id
Expr::Property(x, pos) if target.is::<crate::Map>() => {
#[cfg(feature = "debugging")]
self.run_debugger(scope, global, state, lib, this_ptr, rhs, level)?;
self.run_debugger(scope, global, caches, lib, this_ptr, rhs, level)?;
let index = x.2.clone().into();
let val = self.get_indexed_mut(
global, state, lib, target, index, *pos, false, false, level,
global, caches, lib, target, index, *pos, false, false, level,
)?;
Ok((val.take_or_clone(), false))
}
// xxx.id op= ???
Expr::Property(x, pos) if new_val.is_some() => {
#[cfg(feature = "debugging")]
self.run_debugger(scope, global, state, lib, this_ptr, rhs, level)?;
self.run_debugger(scope, global, caches, lib, this_ptr, rhs, level)?;
let ((getter, hash_get), (setter, hash_set), name) = x.as_ref();
let ((mut new_val, new_pos), (op_info, op_pos)) = new_val.expect("`Some`");
@@ -345,15 +345,15 @@ impl Engine {
let args = &mut [target.as_mut()];
let (mut orig_val, ..) = self
.exec_fn_call(
None, global, state, lib, getter, hash, args, is_ref_mut, true,
*pos, level,
None, global, caches, lib, getter, hash, args, is_ref_mut,
true, *pos, level,
)
.or_else(|err| match *err {
// Try an indexer if property does not exist
ERR::ErrorDotExpr(..) => {
let mut prop = name.into();
self.call_indexer_get(
global, state, lib, target, &mut prop, level,
global, caches, lib, target, &mut prop, level,
)
.map(|r| (r, false))
.map_err(|e| {
@@ -370,7 +370,7 @@ impl Engine {
let orig_val = &mut (&mut orig_val).into();
self.eval_op_assignment(
global, state, lib, op_info, op_pos, orig_val, root, new_val,
global, caches, lib, op_info, op_pos, orig_val, root, new_val,
level,
)
.map_err(|err| err.fill_position(new_pos))?;
@@ -382,7 +382,7 @@ impl Engine {
let hash = crate::ast::FnCallHashes::from_native(*hash_set);
let args = &mut [target.as_mut(), &mut new_val];
self.exec_fn_call(
None, global, state, lib, setter, hash, args, is_ref_mut, true, *pos,
None, global, caches, lib, setter, hash, args, is_ref_mut, true, *pos,
level,
)
.or_else(|err| match *err {
@@ -391,7 +391,7 @@ impl Engine {
let idx = &mut name.into();
let new_val = &mut new_val;
self.call_indexer_set(
global, state, lib, target, idx, new_val, is_ref_mut, level,
global, caches, lib, target, idx, new_val, is_ref_mut, level,
)
.map_err(|e| match *e {
ERR::ErrorIndexingType(..) => err,
@@ -404,13 +404,13 @@ impl Engine {
// xxx.id
Expr::Property(x, pos) => {
#[cfg(feature = "debugging")]
self.run_debugger(scope, global, state, lib, this_ptr, rhs, level)?;
self.run_debugger(scope, global, caches, lib, this_ptr, rhs, level)?;
let ((getter, hash_get), _, name) = x.as_ref();
let hash = crate::ast::FnCallHashes::from_native(*hash_get);
let args = &mut [target.as_mut()];
self.exec_fn_call(
None, global, state, lib, getter, hash, args, is_ref_mut, true, *pos,
None, global, caches, lib, getter, hash, args, is_ref_mut, true, *pos,
level,
)
.map_or_else(
@@ -419,7 +419,7 @@ impl Engine {
ERR::ErrorDotExpr(..) => {
let mut prop = name.into();
self.call_indexer_get(
global, state, lib, target, &mut prop, level,
global, caches, lib, target, &mut prop, level,
)
.map(|r| (r, false))
.map_err(|e| match *e {
@@ -443,12 +443,12 @@ impl Engine {
Expr::Property(ref p, pos) => {
#[cfg(feature = "debugging")]
self.run_debugger(
scope, global, state, lib, this_ptr, _node, level,
scope, global, caches, lib, this_ptr, _node, level,
)?;
let index = p.2.clone().into();
self.get_indexed_mut(
global, state, lib, target, index, pos, false, true, level,
global, caches, lib, target, index, pos, false, true, level,
)?
}
// {xxx:map}.fn_name(arg_expr_list)[expr] | {xxx:map}.fn_name(arg_expr_list).expr
@@ -458,11 +458,11 @@ impl Engine {
#[cfg(feature = "debugging")]
let reset_debugger = self.run_debugger_with_reset(
scope, global, state, lib, this_ptr, _node, level,
scope, global, caches, lib, this_ptr, _node, level,
)?;
let result = self.make_method_call(
global, state, lib, name, *hashes, target, call_args, pos,
global, caches, lib, name, *hashes, target, call_args, pos,
level,
);
@@ -481,7 +481,7 @@ impl Engine {
let rhs_chain = rhs.into();
self.eval_dot_index_chain_helper(
global, state, lib, this_ptr, val_target, root, rhs, &x.rhs, *options,
global, caches, lib, this_ptr, val_target, root, rhs, &x.rhs, *options,
idx_values, rhs_chain, level, new_val,
)
.map_err(|err| err.fill_position(*x_pos))
@@ -495,7 +495,7 @@ impl Engine {
Expr::Property(ref p, pos) => {
#[cfg(feature = "debugging")]
self.run_debugger(
scope, global, state, lib, this_ptr, _node, level,
scope, global, caches, lib, this_ptr, _node, level,
)?;
let ((getter, hash_get), (setter, hash_set), name) = p.as_ref();
@@ -508,7 +508,7 @@ impl Engine {
// Assume getters are always pure
let (mut val, ..) = self
.exec_fn_call(
None, global, state, lib, getter, hash_get, args,
None, global, caches, lib, getter, hash_get, args,
is_ref_mut, true, pos, level,
)
.or_else(|err| match *err {
@@ -516,7 +516,7 @@ impl Engine {
ERR::ErrorDotExpr(..) => {
let mut prop = name.into();
self.call_indexer_get(
global, state, lib, target, &mut prop, level,
global, caches, lib, target, &mut prop, level,
)
.map(|r| (r, false))
.map_err(
@@ -533,7 +533,7 @@ impl Engine {
let (result, may_be_changed) = self
.eval_dot_index_chain_helper(
global, state, lib, this_ptr, val, root, rhs, &x.rhs,
global, caches, lib, this_ptr, val, root, rhs, &x.rhs,
*options, idx_values, rhs_chain, level, new_val,
)
.map_err(|err| err.fill_position(*x_pos))?;
@@ -544,7 +544,7 @@ impl Engine {
let mut arg_values = [target.as_mut(), val.as_mut()];
let args = &mut arg_values;
self.exec_fn_call(
None, global, state, lib, setter, hash_set, args,
None, global, caches, lib, setter, hash_set, args,
is_ref_mut, true, pos, level,
)
.or_else(
@@ -554,7 +554,7 @@ impl Engine {
let idx = &mut name.into();
let new_val = val;
self.call_indexer_set(
global, state, lib, target, idx, new_val,
global, caches, lib, target, idx, new_val,
is_ref_mut, level,
)
.or_else(|e| match *e {
@@ -581,11 +581,11 @@ impl Engine {
#[cfg(feature = "debugging")]
let reset_debugger = self.run_debugger_with_reset(
scope, global, state, lib, this_ptr, _node, level,
scope, global, caches, lib, this_ptr, _node, level,
)?;
let result = self.make_method_call(
global, state, lib, name, *hashes, target, args, pos, level,
global, caches, lib, name, *hashes, target, args, pos, level,
);
#[cfg(feature = "debugging")]
@@ -595,8 +595,8 @@ impl Engine {
let val = &mut val.into();
self.eval_dot_index_chain_helper(
global, state, lib, this_ptr, val, root, rhs, &x.rhs, *options,
idx_values, rhs_chain, level, new_val,
global, caches, lib, this_ptr, val, root, rhs, &x.rhs,
*options, idx_values, rhs_chain, level, new_val,
)
.map_err(|err| err.fill_position(pos))
}
@@ -620,7 +620,7 @@ impl Engine {
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
expr: &Expr,
@@ -638,7 +638,7 @@ impl Engine {
let idx_values = &mut StaticVec::new_const();
self.eval_dot_index_chain_arguments(
scope, global, state, lib, this_ptr, rhs, options, chain_type, idx_values, 0, level,
scope, global, caches, lib, this_ptr, rhs, options, chain_type, idx_values, 0, level,
)?;
let is_assignment = new_val.is_some();
@@ -647,19 +647,19 @@ impl Engine {
// id.??? or id[???]
Expr::Variable(x, .., var_pos) => {
#[cfg(feature = "debugging")]
self.run_debugger(scope, global, state, lib, this_ptr, lhs, level)?;
self.run_debugger(scope, global, caches, lib, this_ptr, lhs, level)?;
#[cfg(not(feature = "unchecked"))]
self.inc_operations(&mut global.num_operations, *var_pos)?;
let (mut target, ..) =
self.search_namespace(scope, global, state, lib, this_ptr, lhs, level)?;
self.search_namespace(scope, global, caches, lib, this_ptr, lhs, level)?;
let obj_ptr = &mut target;
let root = (x.3.as_str(), *var_pos);
self.eval_dot_index_chain_helper(
global, state, lib, &mut None, obj_ptr, root, expr, rhs, options, idx_values,
global, caches, lib, &mut None, obj_ptr, root, expr, rhs, options, idx_values,
chain_type, level, new_val,
)
.map(|(v, ..)| v)
@@ -669,11 +669,11 @@ impl Engine {
_ if is_assignment => unreachable!("cannot assign to an expression"),
// {expr}.??? or {expr}[???]
expr => {
let value = self.eval_expr(scope, global, state, lib, this_ptr, expr, level)?;
let value = self.eval_expr(scope, global, caches, lib, this_ptr, expr, level)?;
let obj_ptr = &mut value.into();
let root = ("", expr.start_position());
self.eval_dot_index_chain_helper(
global, state, lib, this_ptr, obj_ptr, root, expr, rhs, options, idx_values,
global, caches, lib, this_ptr, obj_ptr, root, expr, rhs, options, idx_values,
chain_type, level, new_val,
)
.map(|(v, ..)| if is_assignment { Dynamic::UNIT } else { v })
@@ -689,7 +689,7 @@ impl Engine {
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
expr: &Expr,
@@ -713,7 +713,7 @@ impl Engine {
(crate::FnArgsVec::with_capacity(args.len()), Position::NONE),
|(mut values, mut pos), expr| {
let (value, arg_pos) =
self.get_arg_value(scope, global, state, lib, this_ptr, expr, level)?;
self.get_arg_value(scope, global, caches, lib, this_ptr, expr, level)?;
if values.is_empty() {
pos = arg_pos;
}
@@ -758,7 +758,7 @@ impl Engine {
(crate::FnArgsVec::with_capacity(args.len()), Position::NONE),
|(mut values, mut pos), expr| {
let (value, arg_pos) = self.get_arg_value(
scope, global, state, lib, this_ptr, expr, level,
scope, global, caches, lib, this_ptr, expr, level,
)?;
if values.is_empty() {
pos = arg_pos
@@ -779,7 +779,7 @@ impl Engine {
}
#[cfg(not(feature = "no_index"))]
_ if _parent_chain_type == ChainType::Indexing => self
.eval_expr(scope, global, state, lib, this_ptr, lhs, level)
.eval_expr(scope, global, caches, lib, this_ptr, lhs, level)
.map(|v| {
super::ChainArgument::from_index_value(
v.flatten(),
@@ -793,7 +793,7 @@ impl Engine {
let chain_type = expr.into();
self.eval_dot_index_chain_arguments(
scope, global, state, lib, this_ptr, rhs, *options, chain_type, idx_values,
scope, global, caches, lib, this_ptr, rhs, *options, chain_type, idx_values,
size, level,
)?;
@@ -806,7 +806,7 @@ impl Engine {
}
#[cfg(not(feature = "no_index"))]
_ if _parent_chain_type == ChainType::Indexing => idx_values.push(
self.eval_expr(scope, global, state, lib, this_ptr, expr, level)
self.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.map(|v| {
super::ChainArgument::from_index_value(v.flatten(), expr.start_position())
})?,
@@ -823,7 +823,7 @@ impl Engine {
fn call_indexer_get(
&self,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
target: &mut Dynamic,
idx: &mut Dynamic,
@@ -835,7 +835,7 @@ impl Engine {
let pos = Position::NONE;
self.exec_fn_call(
None, global, state, lib, fn_name, hash_get, args, true, true, pos, level,
None, global, caches, lib, fn_name, hash_get, args, true, true, pos, level,
)
.map(|(r, ..)| r)
}
@@ -846,7 +846,7 @@ impl Engine {
fn call_indexer_set(
&self,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
target: &mut Dynamic,
idx: &mut Dynamic,
@@ -860,7 +860,7 @@ impl Engine {
let pos = Position::NONE;
self.exec_fn_call(
None, global, state, lib, fn_name, hash_set, args, is_ref_mut, true, pos, level,
None, global, caches, lib, fn_name, hash_set, args, is_ref_mut, true, pos, level,
)
}
@@ -869,7 +869,7 @@ impl Engine {
fn get_indexed_mut<'t>(
&self,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
target: &'t mut Dynamic,
mut idx: Dynamic,
@@ -1064,7 +1064,7 @@ impl Engine {
}
_ if use_indexers => self
.call_indexer_get(global, state, lib, target, &mut idx, level)
.call_indexer_get(global, caches, lib, target, &mut idx, level)
.map(Into::into),
_ => Err(ERR::ErrorIndexingType(

View File

@@ -1,7 +1,7 @@
//! Module defining the debugging interface.
#![cfg(feature = "debugging")]
use super::{EvalContext, EvalState, GlobalRuntimeState};
use super::{Caches, EvalContext, GlobalRuntimeState};
use crate::ast::{ASTNode, Expr, Stmt};
use crate::{Dynamic, Engine, EvalAltResult, Identifier, Module, Position, RhaiResultOf, Scope};
#[cfg(feature = "no_std")]
@@ -406,7 +406,7 @@ impl Engine {
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
node: impl Into<ASTNode<'a>>,
@@ -414,7 +414,7 @@ impl Engine {
) -> RhaiResultOf<()> {
if self.debugger.is_some() {
if let Some(cmd) =
self.run_debugger_with_reset_raw(scope, global, state, lib, this_ptr, node, level)?
self.run_debugger_with_reset_raw(scope, global, caches, lib, this_ptr, node, level)?
{
global.debugger.status = cmd;
}
@@ -434,14 +434,14 @@ impl Engine {
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
node: impl Into<ASTNode<'a>>,
level: usize,
) -> RhaiResultOf<Option<DebuggerStatus>> {
if self.debugger.is_some() {
self.run_debugger_with_reset_raw(scope, global, state, lib, this_ptr, node, level)
self.run_debugger_with_reset_raw(scope, global, caches, lib, this_ptr, node, level)
} else {
Ok(None)
}
@@ -458,7 +458,7 @@ impl Engine {
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
node: impl Into<ASTNode<'a>>,
@@ -490,7 +490,7 @@ impl Engine {
}
};
self.run_debugger_raw(scope, global, state, lib, this_ptr, node, event, level)
self.run_debugger_raw(scope, global, caches, lib, this_ptr, node, event, level)
}
/// Run the debugger callback unconditionally.
///
@@ -504,7 +504,7 @@ impl Engine {
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
node: ASTNode<'a>,
@@ -522,7 +522,7 @@ impl Engine {
engine: self,
scope,
global,
state,
caches,
lib,
this_ptr,
level,

View File

@@ -1,21 +1,21 @@
//! Evaluation context.
use super::{EvalState, GlobalRuntimeState};
use super::{Caches, GlobalRuntimeState};
use crate::{Dynamic, Engine, Module, Scope};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
/// Context of a script evaluation process.
#[derive(Debug)]
pub struct EvalContext<'a, 'x, 'px, 'm, 'pm, 's, 'ps, 'b, 't, 'pt> {
pub struct EvalContext<'a, 'x, 'px, 'm, 'pm, 'c, 'b, 't, 'pt> {
/// The current [`Engine`].
pub(crate) engine: &'a Engine,
/// The current [`Scope`].
pub(crate) scope: &'x mut Scope<'px>,
/// The current [`GlobalRuntimeState`].
pub(crate) global: &'m mut GlobalRuntimeState<'pm>,
/// The current [evaluation state][EvalState].
pub(crate) state: &'s mut EvalState<'ps>,
/// The current [caches][Caches].
pub(crate) caches: &'c mut Caches,
/// The current stack of imported [modules][Module].
pub(crate) lib: &'b [&'b Module],
/// The current bound `this` pointer, if any.
@@ -24,7 +24,7 @@ pub struct EvalContext<'a, 'x, 'px, 'm, 'pm, 's, 'ps, 'b, 't, 'pt> {
pub(crate) level: usize,
}
impl<'x, 'px, 'm, 'pm, 'pt> EvalContext<'_, 'x, 'px, 'm, 'pm, '_, '_, '_, '_, 'pt> {
impl<'x, 'px, 'm, 'pm, 'pt> EvalContext<'_, 'x, 'px, 'm, 'pm, '_, '_, '_, 'pt> {
/// The current [`Engine`].
#[inline(always)]
#[must_use]

View File

@@ -1,72 +0,0 @@
//! Evaluation state.
use crate::func::call::FnResolutionCache;
use crate::StaticVec;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{collections::BTreeMap, marker::PhantomData};
/// _(internals)_ A type that holds all the current states of the [`Engine`][crate::Engine].
/// Exported under the `internals` feature only.
#[derive(Debug, Clone)]
pub struct EvalState<'a> {
/// Force a [`Scope`][crate::Scope] search by name.
///
/// Normally, access to variables are parsed with a relative offset into the
/// [`Scope`][crate::Scope] to avoid a lookup.
///
/// 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.
pub always_search_scope: bool,
/// Level of the current scope.
///
/// The global (root) level is zero, a new block (or function call) is one level higher, and so on.
pub scope_level: usize,
/// Stack of function resolution caches.
fn_resolution_caches: StaticVec<FnResolutionCache>,
/// Take care of the lifetime parameter
dummy: PhantomData<&'a ()>,
}
impl EvalState<'_> {
/// Create a new [`EvalState`].
#[inline(always)]
#[must_use]
pub fn new() -> Self {
Self {
always_search_scope: false,
scope_level: 0,
fn_resolution_caches: StaticVec::new_const(),
dummy: Default::default(),
}
}
/// Get the number of function resolution cache(s) in the stack.
#[inline(always)]
#[must_use]
pub fn fn_resolution_caches_len(&self) -> usize {
self.fn_resolution_caches.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.fn_resolution_caches.is_empty() {
// Push a new function resolution cache if the stack is empty
self.push_fn_resolution_cache();
}
self.fn_resolution_caches.last_mut().unwrap()
}
/// Push an empty function resolution cache onto the stack and make it current.
#[allow(dead_code)]
#[inline(always)]
pub fn push_fn_resolution_cache(&mut self) {
self.fn_resolution_caches.push(BTreeMap::new());
}
/// Rewind the function resolution caches stack to a particular size.
#[inline(always)]
pub fn rewind_fn_resolution_caches(&mut self, len: usize) {
self.fn_resolution_caches.truncate(len);
}
}

View File

@@ -1,6 +1,6 @@
//! Module defining functions for evaluating an expression.
use super::{EvalContext, EvalState, GlobalRuntimeState, Target};
use super::{Caches, EvalContext, GlobalRuntimeState, Target};
use crate::ast::{Expr, FnCallExpr, OpAssignment};
use crate::engine::{KEYWORD_THIS, OP_CONCAT};
use crate::types::dynamic::AccessMode;
@@ -17,7 +17,6 @@ impl Engine {
pub(crate) fn search_imports(
&self,
global: &GlobalRuntimeState,
state: &mut EvalState,
namespace: &crate::ast::Namespace,
) -> Option<crate::Shared<Module>> {
assert!(!namespace.is_empty());
@@ -25,7 +24,7 @@ impl Engine {
let root = namespace.root();
// Qualified - check if the root module is directly indexed
let index = if state.always_search_scope {
let index = if global.always_search_scope {
None
} else {
namespace.index()
@@ -48,7 +47,7 @@ impl Engine {
&self,
scope: &'s mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &'s mut Option<&mut Dynamic>,
expr: &Expr,
@@ -56,24 +55,24 @@ impl Engine {
) -> RhaiResultOf<(Target<'s>, Position)> {
match expr {
Expr::Variable(_, Some(_), _) => {
self.search_scope_only(scope, global, state, lib, this_ptr, expr, level)
self.search_scope_only(scope, global, caches, lib, this_ptr, expr, level)
}
Expr::Variable(v, None, _var_pos) => match v.as_ref() {
// Normal variable access
#[cfg(not(feature = "no_module"))]
(_, ns, ..) if ns.is_empty() => {
self.search_scope_only(scope, global, state, lib, this_ptr, expr, level)
self.search_scope_only(scope, global, caches, lib, this_ptr, expr, level)
}
#[cfg(feature = "no_module")]
(_, (), ..) => {
self.search_scope_only(scope, global, state, lib, this_ptr, expr, level)
self.search_scope_only(scope, global, caches, lib, this_ptr, expr, level)
}
// Qualified variable access
#[cfg(not(feature = "no_module"))]
(_, namespace, hash_var, var_name) => {
// foo:bar::baz::VARIABLE
if let Some(module) = self.search_imports(global, state, namespace) {
if let Some(module) = self.search_imports(global, namespace) {
return if let Some(mut target) = module.get_qualified_var(*hash_var) {
// Module variables are constant
target.set_access_mode(AccessMode::ReadOnly);
@@ -131,7 +130,7 @@ impl Engine {
&self,
scope: &'s mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &'s mut Option<&mut Dynamic>,
expr: &Expr,
@@ -148,7 +147,7 @@ impl Engine {
Err(ERR::ErrorUnboundThis(*pos).into())
}
}
_ if state.always_search_scope => (0, expr.start_position()),
_ if global.always_search_scope => (0, expr.start_position()),
Expr::Variable(.., Some(i), pos) => (i.get() as usize, *pos),
Expr::Variable(v, None, pos) => (v.0.map(NonZeroUsize::get).unwrap_or(0), *pos),
_ => unreachable!("Expr::Variable expected but gets {:?}", expr),
@@ -160,7 +159,7 @@ impl Engine {
engine: self,
scope,
global,
state,
caches,
lib,
this_ptr,
level,
@@ -205,7 +204,7 @@ impl Engine {
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
expr: &FnCallExpr,
@@ -228,7 +227,7 @@ impl Engine {
let hash = hashes.native;
return self.make_qualified_function_call(
scope, global, state, lib, this_ptr, namespace, name, args, hash, pos, level,
scope, global, caches, lib, this_ptr, namespace, name, args, hash, pos, level,
);
}
@@ -239,7 +238,7 @@ impl Engine {
);
self.make_function_call(
scope, global, state, lib, this_ptr, name, first_arg, args, *hashes, *capture, pos,
scope, global, caches, lib, this_ptr, name, first_arg, args, *hashes, *capture, pos,
level,
)
}
@@ -256,7 +255,7 @@ impl Engine {
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
expr: &Expr,
@@ -270,13 +269,13 @@ impl Engine {
if let Expr::FnCall(x, ..) = expr {
#[cfg(feature = "debugging")]
let reset_debugger =
self.run_debugger_with_reset(scope, global, state, lib, this_ptr, expr, level)?;
self.run_debugger_with_reset(scope, global, caches, lib, this_ptr, expr, level)?;
#[cfg(not(feature = "unchecked"))]
self.inc_operations(&mut global.num_operations, expr.position())?;
let result =
self.eval_fn_call_expr(scope, global, state, lib, this_ptr, x, x.pos, level);
self.eval_fn_call_expr(scope, global, caches, lib, this_ptr, x, x.pos, level);
#[cfg(feature = "debugging")]
global.debugger.reset_status(reset_debugger);
@@ -289,7 +288,7 @@ impl Engine {
// will cost more than the mis-predicted `match` branch.
if let Expr::Variable(x, index, var_pos) = expr {
#[cfg(feature = "debugging")]
self.run_debugger(scope, global, state, lib, this_ptr, expr, level)?;
self.run_debugger(scope, global, caches, lib, this_ptr, expr, level)?;
#[cfg(not(feature = "unchecked"))]
self.inc_operations(&mut global.num_operations, expr.position())?;
@@ -300,14 +299,14 @@ impl Engine {
.cloned()
.ok_or_else(|| ERR::ErrorUnboundThis(*var_pos).into())
} else {
self.search_namespace(scope, global, state, lib, this_ptr, expr, level)
self.search_namespace(scope, global, caches, lib, this_ptr, expr, level)
.map(|(val, ..)| val.take_or_clone())
};
}
#[cfg(feature = "debugging")]
let reset_debugger =
self.run_debugger_with_reset(scope, global, state, lib, this_ptr, expr, level)?;
self.run_debugger_with_reset(scope, global, caches, lib, this_ptr, expr, level)?;
#[cfg(not(feature = "unchecked"))]
self.inc_operations(&mut global.num_operations, expr.position())?;
@@ -330,7 +329,7 @@ impl Engine {
for expr in x.iter() {
let item =
match self.eval_expr(scope, global, state, lib, this_ptr, expr, level) {
match self.eval_expr(scope, global, caches, lib, this_ptr, expr, level) {
Ok(r) => r,
err => {
result = err;
@@ -340,7 +339,7 @@ impl Engine {
if let Err(err) = self.eval_op_assignment(
global,
state,
caches,
lib,
Some(OpAssignment::new(OP_CONCAT)),
expr.start_position(),
@@ -367,7 +366,7 @@ impl Engine {
for item_expr in x.iter() {
let value = match self
.eval_expr(scope, global, state, lib, this_ptr, item_expr, level)
.eval_expr(scope, global, caches, lib, this_ptr, item_expr, level)
{
Ok(r) => r.flatten(),
err => {
@@ -405,7 +404,7 @@ impl Engine {
for (key, value_expr) in x.0.iter() {
let value = match self
.eval_expr(scope, global, state, lib, this_ptr, value_expr, level)
.eval_expr(scope, global, caches, lib, this_ptr, value_expr, level)
{
Ok(r) => r.flatten(),
err => {
@@ -431,7 +430,7 @@ impl Engine {
Expr::And(x, ..) => {
let lhs = self
.eval_expr(scope, global, state, lib, this_ptr, &x.lhs, level)
.eval_expr(scope, global, caches, lib, this_ptr, &x.lhs, level)
.and_then(|v| {
v.as_bool().map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, x.lhs.position())
@@ -439,7 +438,7 @@ impl Engine {
});
if let Ok(true) = lhs {
self.eval_expr(scope, global, state, lib, this_ptr, &x.rhs, level)
self.eval_expr(scope, global, caches, lib, this_ptr, &x.rhs, level)
.and_then(|v| {
v.as_bool()
.map_err(|typ| {
@@ -454,7 +453,7 @@ impl Engine {
Expr::Or(x, ..) => {
let lhs = self
.eval_expr(scope, global, state, lib, this_ptr, &x.lhs, level)
.eval_expr(scope, global, caches, lib, this_ptr, &x.lhs, level)
.and_then(|v| {
v.as_bool().map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, x.lhs.position())
@@ -462,7 +461,7 @@ impl Engine {
});
if let Ok(false) = lhs {
self.eval_expr(scope, global, state, lib, this_ptr, &x.rhs, level)
self.eval_expr(scope, global, caches, lib, this_ptr, &x.rhs, level)
.and_then(|v| {
v.as_bool()
.map_err(|typ| {
@@ -491,7 +490,7 @@ impl Engine {
engine: self,
scope,
global,
state,
caches,
lib,
this_ptr,
level,
@@ -504,17 +503,17 @@ impl Engine {
Expr::Stmt(x) if x.is_empty() => Ok(Dynamic::UNIT),
Expr::Stmt(x) => {
self.eval_stmt_block(scope, global, state, lib, this_ptr, x, true, level)
self.eval_stmt_block(scope, global, caches, lib, this_ptr, x, true, level)
}
#[cfg(not(feature = "no_index"))]
Expr::Index(..) => {
self.eval_dot_index_chain(scope, global, state, lib, this_ptr, expr, level, None)
self.eval_dot_index_chain(scope, global, caches, lib, this_ptr, expr, level, None)
}
#[cfg(not(feature = "no_object"))]
Expr::Dot(..) => {
self.eval_dot_index_chain(scope, global, state, lib, this_ptr, expr, level, None)
self.eval_dot_index_chain(scope, global, caches, lib, this_ptr, expr, level, None)
}
_ => unreachable!("expression cannot be evaluated: {:?}", expr),

View File

@@ -30,16 +30,31 @@ pub struct GlobalRuntimeState<'a> {
#[cfg(not(feature = "no_module"))]
modules: crate::StaticVec<crate::Shared<crate::Module>>,
/// Source of the current context.
///
/// No source if the string is empty.
pub source: Identifier,
/// Number of operations performed.
pub num_operations: u64,
/// Number of modules loaded.
pub num_modules_loaded: usize,
/// Level of the current scope.
///
/// The global (root) level is zero, a new block (or function call) is one level higher, and so on.
pub scope_level: usize,
/// Force a [`Scope`][crate::Scope] search by name.
///
/// Normally, access to variables are parsed with a relative offset into the
/// [`Scope`][crate::Scope] to avoid a lookup.
///
/// 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.
pub always_search_scope: bool,
/// Function call hashes to index getters and setters.
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
fn_hash_indexing: (u64, u64),
/// Embedded [crate::Module][crate::Module] resolver.
/// Embedded [module][crate::Module] resolver.
#[cfg(not(feature = "no_module"))]
pub embedded_module_resolver:
Option<crate::Shared<crate::module::resolvers::StaticModuleResolver>>,
@@ -71,6 +86,8 @@ impl GlobalRuntimeState<'_> {
source: Identifier::new_const(),
num_operations: 0,
num_modules_loaded: 0,
scope_level: 0,
always_search_scope: false,
#[cfg(not(feature = "no_module"))]
embedded_module_resolver: None,
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
@@ -92,7 +109,7 @@ impl GlobalRuntimeState<'_> {
pub fn num_imports(&self) -> usize {
self.keys.len()
}
/// Get the globally-imported [crate::Module][crate::Module] at a particular index.
/// Get the globally-imported [module][crate::Module] at a particular index.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
@@ -101,7 +118,7 @@ impl GlobalRuntimeState<'_> {
pub fn get_shared_import(&self, index: usize) -> Option<crate::Shared<crate::Module>> {
self.modules.get(index).cloned()
}
/// Get a mutable reference to the globally-imported [crate::Module][crate::Module] at a
/// Get a mutable reference to the globally-imported [module][crate::Module] at a
/// particular index.
///
/// Not available under `no_module`.
@@ -115,7 +132,7 @@ impl GlobalRuntimeState<'_> {
) -> Option<&mut crate::Shared<crate::Module>> {
self.modules.get_mut(index)
}
/// Get the index of a globally-imported [crate::Module][crate::Module] by name.
/// Get the index of a globally-imported [module][crate::Module] by name.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
@@ -132,7 +149,7 @@ impl GlobalRuntimeState<'_> {
}
})
}
/// Push an imported [crate::Module][crate::Module] onto the stack.
/// Push an imported [module][crate::Module] onto the stack.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]

View File

@@ -1,13 +1,14 @@
mod cache;
mod chaining;
mod data_check;
mod debugger;
mod eval_context;
mod eval_state;
mod expr;
mod global_state;
mod stmt;
mod target;
pub use cache::{Caches, FnResolutionCache, FnResolutionCacheEntry};
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
pub use chaining::{ChainArgument, ChainType};
#[cfg(feature = "debugging")]
@@ -16,7 +17,6 @@ pub use debugger::{
OnDebuggerCallback, OnDebuggingInit,
};
pub use eval_context::EvalContext;
pub use eval_state::EvalState;
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
pub use global_state::GlobalConstants;

View File

@@ -1,6 +1,6 @@
//! Module defining functions for evaluating a statement.
use super::{EvalContext, EvalState, GlobalRuntimeState, Target};
use super::{Caches, EvalContext, GlobalRuntimeState, Target};
use crate::api::events::VarDefInfo;
use crate::ast::{
ASTFlags, BinaryExpr, Expr, Ident, OpAssignment, Stmt, SwitchCases, TryCatchBlock,
@@ -25,7 +25,7 @@ impl Engine {
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
statements: &[Stmt],
@@ -36,14 +36,14 @@ impl Engine {
return Ok(Dynamic::UNIT);
}
let orig_always_search_scope = state.always_search_scope;
let orig_always_search_scope = global.always_search_scope;
let orig_scope_len = scope.len();
#[cfg(not(feature = "no_module"))]
let orig_imports_len = global.num_imports();
let orig_fn_resolution_caches_len = state.fn_resolution_caches_len();
let orig_fn_resolution_caches_len = caches.fn_resolution_caches_len();
if restore_orig_state {
state.scope_level += 1;
global.scope_level += 1;
}
let mut result = Ok(Dynamic::UNIT);
@@ -55,7 +55,7 @@ impl Engine {
result = self.eval_stmt(
scope,
global,
state,
caches,
lib,
this_ptr,
stmt,
@@ -76,34 +76,34 @@ impl Engine {
.skip(imports_len)
.any(|(.., m)| m.contains_indexed_global_functions())
{
if state.fn_resolution_caches_len() > orig_fn_resolution_caches_len {
if caches.fn_resolution_caches_len() > orig_fn_resolution_caches_len {
// When new module is imported with global functions and there is already
// a new cache, clear it - notice that this is expensive as all function
// resolutions must start again
state.fn_resolution_cache_mut().clear();
caches.fn_resolution_cache_mut().clear();
} else if restore_orig_state {
// When new module is imported with global functions, push a new cache
state.push_fn_resolution_cache();
caches.push_fn_resolution_cache();
} else {
// When the block is to be evaluated in-place, just clear the current cache
state.fn_resolution_cache_mut().clear();
caches.fn_resolution_cache_mut().clear();
}
}
}
}
// If imports list is modified, pop the functions lookup cache
state.rewind_fn_resolution_caches(orig_fn_resolution_caches_len);
caches.rewind_fn_resolution_caches(orig_fn_resolution_caches_len);
if restore_orig_state {
scope.rewind(orig_scope_len);
state.scope_level -= 1;
global.scope_level -= 1;
#[cfg(not(feature = "no_module"))]
global.truncate_imports(orig_imports_len);
// The impact of new local variables goes away at the end of a block
// because any new variables introduced will go out of scope
state.always_search_scope = orig_always_search_scope;
global.always_search_scope = orig_always_search_scope;
}
result
@@ -114,7 +114,7 @@ impl Engine {
pub(crate) fn eval_op_assignment(
&self,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
op_info: Option<OpAssignment>,
op_pos: Position,
@@ -157,7 +157,7 @@ impl Engine {
let level = level + 1;
match self.call_native_fn(
global, state, lib, op_assign, hash, args, true, true, op_pos, level,
global, caches, lib, op_assign, hash, args, true, true, op_pos, level,
) {
Ok(_) => {
#[cfg(not(feature = "unchecked"))]
@@ -167,7 +167,7 @@ impl Engine {
{
// Expand to `var = var op rhs`
let (value, ..) = self.call_native_fn(
global, state, lib, op, hash_op, args, true, false, op_pos, level,
global, caches, lib, op, hash_op, args, true, false, op_pos, level,
)?;
#[cfg(not(feature = "unchecked"))]
@@ -197,7 +197,7 @@ impl Engine {
&self,
scope: &mut Scope,
global: &mut GlobalRuntimeState,
state: &mut EvalState,
caches: &mut Caches,
lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>,
stmt: &Stmt,
@@ -206,7 +206,7 @@ impl Engine {
) -> RhaiResult {
#[cfg(feature = "debugging")]
let reset_debugger =
self.run_debugger_with_reset(scope, global, state, lib, this_ptr, stmt, level)?;
self.run_debugger_with_reset(scope, global, caches, lib, this_ptr, stmt, level)?;
// Coded this way for better branch prediction.
// Popular branches are lifted out of the `match` statement into their own branches.
@@ -217,7 +217,7 @@ impl Engine {
self.inc_operations(&mut global.num_operations, stmt.position())?;
let result =
self.eval_fn_call_expr(scope, global, state, lib, this_ptr, x, x.pos, level);
self.eval_fn_call_expr(scope, global, caches, lib, this_ptr, x, x.pos, level);
#[cfg(feature = "debugging")]
global.debugger.reset_status(reset_debugger);
@@ -236,12 +236,12 @@ impl Engine {
let (op_info, BinaryExpr { lhs, rhs }) = x.as_ref();
let rhs_result = self
.eval_expr(scope, global, state, lib, this_ptr, rhs, level)
.eval_expr(scope, global, caches, lib, this_ptr, rhs, level)
.map(Dynamic::flatten);
if let Ok(rhs_val) = rhs_result {
let search_result =
self.search_namespace(scope, global, state, lib, this_ptr, lhs, level);
self.search_namespace(scope, global, caches, lib, this_ptr, lhs, level);
if let Ok(search_val) = search_result {
let (mut lhs_ptr, pos) = search_val;
@@ -261,7 +261,7 @@ impl Engine {
let lhs_ptr = &mut lhs_ptr;
self.eval_op_assignment(
global, state, lib, *op_info, *op_pos, lhs_ptr, root, rhs_val, level,
global, caches, lib, *op_info, *op_pos, lhs_ptr, root, rhs_val, level,
)
.map_err(|err| err.fill_position(rhs.start_position()))
.map(|_| Dynamic::UNIT)
@@ -275,7 +275,7 @@ impl Engine {
let (op_info, BinaryExpr { lhs, rhs }) = x.as_ref();
let rhs_result = self
.eval_expr(scope, global, state, lib, this_ptr, rhs, level)
.eval_expr(scope, global, caches, lib, this_ptr, rhs, level)
.map(Dynamic::flatten);
if let Ok(rhs_val) = rhs_result {
@@ -291,14 +291,14 @@ impl Engine {
#[cfg(not(feature = "no_index"))]
Expr::Index(..) => self
.eval_dot_index_chain(
scope, global, state, lib, this_ptr, lhs, level, _new_val,
scope, global, caches, lib, this_ptr, lhs, level, _new_val,
)
.map(|_| Dynamic::UNIT),
// dot_lhs.dot_rhs op= rhs
#[cfg(not(feature = "no_object"))]
Expr::Dot(..) => self
.eval_dot_index_chain(
scope, global, state, lib, this_ptr, lhs, level, _new_val,
scope, global, caches, lib, this_ptr, lhs, level, _new_val,
)
.map(|_| Dynamic::UNIT),
_ => unreachable!("cannot assign to expression: {:?}", lhs),
@@ -323,21 +323,21 @@ impl Engine {
// Expression as statement
Stmt::Expr(expr) => self
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.map(Dynamic::flatten),
// Block scope
Stmt::Block(statements, ..) if statements.is_empty() => Ok(Dynamic::UNIT),
Stmt::Block(statements, ..) => {
self.eval_stmt_block(scope, global, state, lib, this_ptr, statements, true, level)
}
Stmt::Block(statements, ..) => self.eval_stmt_block(
scope, global, caches, lib, this_ptr, statements, true, level,
),
// If statement
Stmt::If(x, ..) => {
let (expr, if_block, else_block) = x.as_ref();
let guard_val = self
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.and_then(|v| {
v.as_bool().map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, expr.position())
@@ -348,7 +348,7 @@ impl Engine {
Ok(true) => {
if !if_block.is_empty() {
self.eval_stmt_block(
scope, global, state, lib, this_ptr, if_block, true, level,
scope, global, caches, lib, this_ptr, if_block, true, level,
)
} else {
Ok(Dynamic::UNIT)
@@ -357,7 +357,7 @@ impl Engine {
Ok(false) => {
if !else_block.is_empty() {
self.eval_stmt_block(
scope, global, state, lib, this_ptr, else_block, true, level,
scope, global, caches, lib, this_ptr, else_block, true, level,
)
} else {
Ok(Dynamic::UNIT)
@@ -378,7 +378,8 @@ impl Engine {
},
) = x.as_ref();
let value_result = self.eval_expr(scope, global, state, lib, this_ptr, expr, level);
let value_result =
self.eval_expr(scope, global, caches, lib, this_ptr, expr, level);
if let Ok(value) = value_result {
let stmt_block_result = if value.is_hashable() {
@@ -392,15 +393,17 @@ impl Engine {
.condition
.as_ref()
.map(|cond| {
self.eval_expr(scope, global, state, lib, this_ptr, cond, level)
.and_then(|v| {
v.as_bool().map_err(|typ| {
self.make_type_mismatch_err::<bool>(
typ,
cond.position(),
)
})
self.eval_expr(
scope, global, caches, lib, this_ptr, cond, level,
)
.and_then(|v| {
v.as_bool().map_err(|typ| {
self.make_type_mismatch_err::<bool>(
typ,
cond.position(),
)
})
})
})
.unwrap_or(Ok(true));
@@ -425,7 +428,7 @@ impl Engine {
.as_ref()
.map(|cond| {
self.eval_expr(
scope, global, state, lib, this_ptr, cond, level,
scope, global, caches, lib, this_ptr, cond, level,
)
.and_then(|v| {
v.as_bool().map_err(|typ| {
@@ -460,7 +463,7 @@ impl Engine {
if let Ok(Some(statements)) = stmt_block_result {
if !statements.is_empty() {
self.eval_stmt_block(
scope, global, state, lib, this_ptr, statements, true, level,
scope, global, caches, lib, this_ptr, statements, true, level,
)
} else {
Ok(Dynamic::UNIT)
@@ -469,7 +472,7 @@ impl Engine {
// Default match clause
if !def_case.is_empty() {
self.eval_stmt_block(
scope, global, state, lib, this_ptr, def_case, true, level,
scope, global, caches, lib, this_ptr, def_case, true, level,
)
} else {
Ok(Dynamic::UNIT)
@@ -488,7 +491,7 @@ impl Engine {
if !body.is_empty() {
match self
.eval_stmt_block(scope, global, state, lib, this_ptr, body, true, level)
.eval_stmt_block(scope, global, caches, lib, this_ptr, body, true, level)
{
Ok(_) => (),
Err(err) => match *err {
@@ -508,7 +511,7 @@ impl Engine {
let (expr, body) = x.as_ref();
let condition = self
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.and_then(|v| {
v.as_bool().map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, expr.position())
@@ -519,9 +522,9 @@ impl Engine {
Ok(false) => break Ok(Dynamic::UNIT),
Ok(true) if body.is_empty() => (),
Ok(true) => {
match self
.eval_stmt_block(scope, global, state, lib, this_ptr, body, true, level)
{
match self.eval_stmt_block(
scope, global, caches, lib, this_ptr, body, true, level,
) {
Ok(_) => (),
Err(err) => match *err {
ERR::LoopBreak(false, ..) => (),
@@ -541,7 +544,7 @@ impl Engine {
if !body.is_empty() {
match self
.eval_stmt_block(scope, global, state, lib, this_ptr, body, true, level)
.eval_stmt_block(scope, global, caches, lib, this_ptr, body, true, level)
{
Ok(_) => (),
Err(err) => match *err {
@@ -553,7 +556,7 @@ impl Engine {
}
let condition = self
.eval_expr(scope, global, state, lib, this_ptr, &expr, level)
.eval_expr(scope, global, caches, lib, this_ptr, &expr, level)
.and_then(|v| {
v.as_bool().map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, expr.position())
@@ -572,7 +575,7 @@ impl Engine {
let (var_name, counter, expr, statements) = x.as_ref();
let iter_result = self
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.map(Dynamic::flatten);
if let Ok(iter_obj) = iter_result {
@@ -671,7 +674,7 @@ impl Engine {
}
let result = self.eval_stmt_block(
scope, global, state, lib, this_ptr, statements, true, level,
scope, global, caches, lib, this_ptr, statements, true, level,
);
match result {
@@ -715,7 +718,7 @@ impl Engine {
} = x.as_ref();
let result = self
.eval_stmt_block(scope, global, state, lib, this_ptr, try_block, true, level)
.eval_stmt_block(scope, global, caches, lib, this_ptr, try_block, true, level)
.map(|_| Dynamic::UNIT);
match result {
@@ -767,7 +770,7 @@ impl Engine {
let result = self.eval_stmt_block(
scope,
global,
state,
caches,
lib,
this_ptr,
catch_block,
@@ -794,7 +797,7 @@ impl Engine {
// Throw value
Stmt::Return(Some(expr), options, pos) if options.contains(ASTFlags::BREAK) => self
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.and_then(|v| Err(ERR::ErrorRuntime(v.flatten(), *pos).into())),
// Empty throw
@@ -804,7 +807,7 @@ impl Engine {
// Return value
Stmt::Return(Some(expr), .., pos) => self
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.and_then(|v| Err(ERR::Return(v.flatten(), *pos).into())),
// Empty return
@@ -828,7 +831,7 @@ impl Engine {
// Check variable definition filter
let result = if let Some(ref filter) = self.def_var_filter {
let will_shadow = scope.contains(var_name);
let nesting_level = state.scope_level;
let nesting_level = global.scope_level;
let is_const = access == AccessMode::ReadOnly;
let info = VarDefInfo {
name: var_name,
@@ -840,7 +843,7 @@ impl Engine {
engine: self,
scope,
global,
state,
caches,
lib,
this_ptr,
level,
@@ -864,7 +867,7 @@ impl Engine {
} else {
// Evaluate initial value
let value_result = self
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
.map(Dynamic::flatten);
if let Ok(mut value) = value_result {
@@ -872,7 +875,7 @@ impl Engine {
// Put global constants into global module
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_module"))]
if state.scope_level == 0
if global.scope_level == 0
&& access == AccessMode::ReadOnly
&& lib.iter().any(|&m| !m.is_empty())
{
@@ -927,7 +930,7 @@ impl Engine {
}
let path_result = self
.eval_expr(scope, global, state, lib, this_ptr, &expr, level)
.eval_expr(scope, global, caches, lib, this_ptr, &expr, level)
.and_then(|v| {
let typ = v.type_name();
v.try_cast::<crate::ImmutableString>().ok_or_else(|| {