Clean up types.
This commit is contained in:
@@ -6,7 +6,7 @@ use crate::ast::{ASTFlags, Expr, OpAssignment};
|
||||
use crate::types::dynamic::Union;
|
||||
use crate::types::RestoreOnDrop;
|
||||
use crate::{
|
||||
Dynamic, Engine, FnArgsVec, Module, Position, RhaiResult, RhaiResultOf, Scope, Shared, ERR,
|
||||
Dynamic, Engine, FnArgsVec, Position, RhaiResult, RhaiResultOf, Scope, SharedModule, ERR,
|
||||
};
|
||||
use std::hash::Hash;
|
||||
#[cfg(feature = "no_std")]
|
||||
@@ -43,7 +43,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
this_ptr: &mut Option<&mut Dynamic>,
|
||||
target: &mut Target,
|
||||
@@ -205,7 +205,7 @@ impl Engine {
|
||||
global, caches, lib, level, scope, this_ptr, rhs,
|
||||
)?;
|
||||
#[cfg(feature = "debugging")]
|
||||
let global = &mut *RestoreOnDrop::new(global, move |g| {
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| {
|
||||
g.debugger.reset_status(reset)
|
||||
});
|
||||
|
||||
@@ -216,7 +216,7 @@ impl Engine {
|
||||
// Truncate the index values upon exit
|
||||
let offset = idx_values.len() - args.len();
|
||||
let idx_values =
|
||||
&mut *RestoreOnDrop::new(idx_values, move |v| v.truncate(offset));
|
||||
&mut *RestoreOnDrop::lock(idx_values, move |v| v.truncate(offset));
|
||||
|
||||
let call_args = &mut idx_values[offset..];
|
||||
let pos1 = args.get(0).map_or(Position::NONE, Expr::position);
|
||||
@@ -384,7 +384,7 @@ impl Engine {
|
||||
global, caches, lib, level, scope, this_ptr, _node,
|
||||
)?;
|
||||
#[cfg(feature = "debugging")]
|
||||
let global = &mut *RestoreOnDrop::new(global, move |g| {
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| {
|
||||
g.debugger.reset_status(reset)
|
||||
});
|
||||
|
||||
@@ -394,7 +394,7 @@ impl Engine {
|
||||
|
||||
// Truncate the index values upon exit
|
||||
let offset = idx_values.len() - args.len();
|
||||
let idx_values = &mut *RestoreOnDrop::new(idx_values, move |v| {
|
||||
let idx_values = &mut *RestoreOnDrop::lock(idx_values, move |v| {
|
||||
v.truncate(offset)
|
||||
});
|
||||
|
||||
@@ -516,7 +516,7 @@ impl Engine {
|
||||
global, caches, lib, level, scope, this_ptr, _node,
|
||||
)?;
|
||||
#[cfg(feature = "debugging")]
|
||||
let global = &mut *RestoreOnDrop::new(global, move |g| {
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| {
|
||||
g.debugger.reset_status(reset)
|
||||
});
|
||||
|
||||
@@ -527,7 +527,7 @@ impl Engine {
|
||||
// Truncate the index values upon exit
|
||||
let offset = idx_values.len() - args.len();
|
||||
let idx_values =
|
||||
&mut *RestoreOnDrop::new(idx_values, move |v| {
|
||||
&mut *RestoreOnDrop::lock(idx_values, move |v| {
|
||||
v.truncate(offset)
|
||||
});
|
||||
|
||||
@@ -570,7 +570,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Option<&mut Dynamic>,
|
||||
@@ -661,7 +661,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Option<&mut Dynamic>,
|
||||
@@ -773,7 +773,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
target: &mut Dynamic,
|
||||
idx: &mut Dynamic,
|
||||
@@ -796,7 +796,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
target: &mut Dynamic,
|
||||
idx: &mut Dynamic,
|
||||
@@ -820,7 +820,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
target: &'t mut Dynamic,
|
||||
idx: &mut Dynamic,
|
||||
|
@@ -4,7 +4,7 @@
|
||||
use super::{Caches, EvalContext, GlobalRuntimeState};
|
||||
use crate::ast::{ASTNode, Expr, Stmt};
|
||||
use crate::{
|
||||
Dynamic, Engine, EvalAltResult, ImmutableString, Module, Position, RhaiResultOf, Scope, Shared,
|
||||
Dynamic, Engine, EvalAltResult, ImmutableString, Position, RhaiResultOf, Scope, SharedModule,
|
||||
};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
@@ -413,7 +413,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Option<&mut Dynamic>,
|
||||
@@ -440,7 +440,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Option<&mut Dynamic>,
|
||||
@@ -463,7 +463,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Option<&mut Dynamic>,
|
||||
@@ -510,7 +510,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Option<&mut Dynamic>,
|
||||
@@ -519,8 +519,7 @@ impl Engine {
|
||||
) -> Result<Option<DebuggerStatus>, Box<crate::EvalAltResult>> {
|
||||
let src = global.source_raw().cloned();
|
||||
let src = src.as_ref().map(|s| s.as_str());
|
||||
let context =
|
||||
crate::EvalContext::new(self, global, Some(caches), lib, level, scope, this_ptr);
|
||||
let context = crate::EvalContext::new(self, global, caches, lib, level, scope, this_ptr);
|
||||
|
||||
if let Some((.., ref on_debugger)) = self.debugger {
|
||||
let command = on_debugger(context, event, node, src, node.position())?;
|
||||
|
@@ -1,7 +1,7 @@
|
||||
//! Evaluation context.
|
||||
|
||||
use super::{Caches, GlobalRuntimeState};
|
||||
use crate::{Dynamic, Engine, Module, Scope, Shared};
|
||||
use crate::{Dynamic, Engine, Module, Scope, SharedModule};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
|
||||
@@ -16,9 +16,9 @@ pub struct EvalContext<'a, 's, 'ps, 'g, 'c, 't, 'pt> {
|
||||
/// The current [`GlobalRuntimeState`].
|
||||
global: &'g mut GlobalRuntimeState,
|
||||
/// The current [caches][Caches], if available.
|
||||
caches: Option<&'c mut Caches>,
|
||||
caches: &'c mut Caches,
|
||||
/// The current stack of imported [modules][Module].
|
||||
lib: &'a [Shared<Module>],
|
||||
lib: &'a [SharedModule],
|
||||
/// The current bound `this` pointer, if any.
|
||||
this_ptr: &'t mut Option<&'pt mut Dynamic>,
|
||||
/// The current nesting level of function calls.
|
||||
@@ -32,8 +32,8 @@ impl<'a, 's, 'ps, 'g, 'c, 't, 'pt> EvalContext<'a, 's, 'ps, 'g, 'c, 't, 'pt> {
|
||||
pub fn new(
|
||||
engine: &'a Engine,
|
||||
global: &'g mut GlobalRuntimeState,
|
||||
caches: Option<&'c mut Caches>,
|
||||
lib: &'a [Shared<Module>],
|
||||
caches: &'c mut Caches,
|
||||
lib: &'a [SharedModule],
|
||||
level: usize,
|
||||
scope: &'s mut Scope<'ps>,
|
||||
this_ptr: &'t mut Option<&'pt mut Dynamic>,
|
||||
@@ -117,7 +117,7 @@ impl<'a, 's, 'ps, 'g, 'c, 't, 'pt> EvalContext<'a, 's, 'ps, 'g, 'c, 't, 'pt> {
|
||||
#[cfg(feature = "internals")]
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub const fn namespaces(&self) -> &[Shared<Module>] {
|
||||
pub const fn namespaces(&self) -> &[SharedModule] {
|
||||
self.lib
|
||||
}
|
||||
/// The current bound `this` pointer, if any.
|
||||
@@ -173,17 +173,10 @@ impl<'a, 's, 'ps, 'g, 'c, 't, 'pt> EvalContext<'a, 's, 'ps, 'g, 'c, 't, 'pt> {
|
||||
) -> crate::RhaiResult {
|
||||
let expr: &crate::ast::Expr = expr;
|
||||
|
||||
let mut new_caches = Caches::new();
|
||||
|
||||
let caches = match self.caches.as_mut() {
|
||||
Some(c) => c,
|
||||
None => &mut new_caches,
|
||||
};
|
||||
|
||||
match expr {
|
||||
crate::ast::Expr::Stmt(statements) => self.engine.eval_stmt_block(
|
||||
self.global,
|
||||
caches,
|
||||
self.caches,
|
||||
self.lib,
|
||||
self.level,
|
||||
self.scope,
|
||||
@@ -193,7 +186,7 @@ impl<'a, 's, 'ps, 'g, 'c, 't, 'pt> EvalContext<'a, 's, 'ps, 'g, 'c, 't, 'pt> {
|
||||
),
|
||||
_ => self.engine.eval_expr(
|
||||
self.global,
|
||||
caches,
|
||||
self.caches,
|
||||
self.lib,
|
||||
self.level,
|
||||
self.scope,
|
||||
|
@@ -4,8 +4,7 @@ use super::{Caches, EvalContext, GlobalRuntimeState, Target};
|
||||
use crate::ast::{Expr, OpAssignment};
|
||||
use crate::engine::{KEYWORD_THIS, OP_CONCAT};
|
||||
use crate::types::dynamic::AccessMode;
|
||||
use crate::types::RestoreOnDrop;
|
||||
use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, Shared, ERR};
|
||||
use crate::{Dynamic, Engine, Position, RhaiResult, RhaiResultOf, Scope, SharedModule, ERR};
|
||||
use std::num::NonZeroUsize;
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
@@ -19,7 +18,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &GlobalRuntimeState,
|
||||
namespace: &crate::ast::Namespace,
|
||||
) -> Option<Shared<Module>> {
|
||||
) -> Option<SharedModule> {
|
||||
assert!(!namespace.is_empty());
|
||||
|
||||
let root = namespace.root();
|
||||
@@ -52,7 +51,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
scope: &'s mut Scope,
|
||||
this_ptr: &'s mut Option<&mut Dynamic>,
|
||||
@@ -138,7 +137,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
scope: &'s mut Scope,
|
||||
this_ptr: &'s mut Option<&mut Dynamic>,
|
||||
@@ -174,7 +173,7 @@ impl Engine {
|
||||
|
||||
// Check the variable resolver, if any
|
||||
if let Some(ref resolve_var) = self.resolve_var {
|
||||
let context = EvalContext::new(self, global, Some(caches), lib, level, scope, this_ptr);
|
||||
let context = EvalContext::new(self, global, caches, lib, level, scope, this_ptr);
|
||||
let var_name = expr.get_variable_name(true).expect("`Expr::Variable`");
|
||||
match resolve_var(var_name, index, context) {
|
||||
Ok(Some(mut result)) => {
|
||||
@@ -222,7 +221,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Option<&mut Dynamic>,
|
||||
@@ -238,7 +237,9 @@ impl Engine {
|
||||
let reset =
|
||||
self.run_debugger_with_reset(global, caches, lib, level, scope, this_ptr, expr)?;
|
||||
#[cfg(feature = "debugging")]
|
||||
let global = &mut *RestoreOnDrop::new(global, move |g| g.debugger.reset_status(reset));
|
||||
let global = &mut *crate::types::RestoreOnDrop::lock(global, move |g| {
|
||||
g.debugger.reset_status(reset)
|
||||
});
|
||||
|
||||
self.track_operation(global, expr.position())?;
|
||||
|
||||
@@ -269,7 +270,9 @@ impl Engine {
|
||||
let reset =
|
||||
self.run_debugger_with_reset(global, caches, lib, level, scope, this_ptr, expr)?;
|
||||
#[cfg(feature = "debugging")]
|
||||
let global = &mut *RestoreOnDrop::new(global, move |g| g.debugger.reset_status(reset));
|
||||
let global = &mut *crate::types::RestoreOnDrop::lock(global, move |g| {
|
||||
g.debugger.reset_status(reset)
|
||||
});
|
||||
|
||||
self.track_operation(global, expr.position())?;
|
||||
|
||||
@@ -418,7 +421,7 @@ impl Engine {
|
||||
))
|
||||
})?;
|
||||
let mut context =
|
||||
EvalContext::new(self, global, Some(caches), lib, level, scope, this_ptr);
|
||||
EvalContext::new(self, global, caches, lib, level, scope, this_ptr);
|
||||
|
||||
let result = (custom_def.func)(&mut context, &expressions, &custom.state);
|
||||
|
||||
|
@@ -28,7 +28,7 @@ pub struct GlobalRuntimeState {
|
||||
imports: crate::StaticVec<ImmutableString>,
|
||||
/// Stack of imported [modules][crate::Module].
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
modules: crate::StaticVec<crate::Shared<crate::Module>>,
|
||||
modules: crate::StaticVec<crate::SharedModule>,
|
||||
/// Source of the current context.
|
||||
///
|
||||
/// No source if the string is empty.
|
||||
@@ -127,7 +127,7 @@ impl GlobalRuntimeState {
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub fn get_shared_import(&self, index: usize) -> Option<crate::Shared<crate::Module>> {
|
||||
pub fn get_shared_import(&self, index: usize) -> Option<crate::SharedModule> {
|
||||
self.modules.get(index).cloned()
|
||||
}
|
||||
/// Get a mutable reference to the globally-imported [module][crate::Module] at a
|
||||
@@ -141,7 +141,7 @@ impl GlobalRuntimeState {
|
||||
pub(crate) fn get_shared_import_mut(
|
||||
&mut self,
|
||||
index: usize,
|
||||
) -> Option<&mut crate::Shared<crate::Module>> {
|
||||
) -> Option<&mut crate::SharedModule> {
|
||||
self.modules.get_mut(index)
|
||||
}
|
||||
/// Get the index of a globally-imported [module][crate::Module] by name.
|
||||
@@ -165,7 +165,7 @@ impl GlobalRuntimeState {
|
||||
pub fn push_import(
|
||||
&mut self,
|
||||
name: impl Into<ImmutableString>,
|
||||
module: impl Into<crate::Shared<crate::Module>>,
|
||||
module: impl Into<crate::SharedModule>,
|
||||
) {
|
||||
self.imports.push(name.into());
|
||||
self.modules.push(module.into());
|
||||
@@ -198,7 +198,7 @@ impl GlobalRuntimeState {
|
||||
#[inline]
|
||||
pub(crate) fn iter_imports_raw(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (&ImmutableString, &crate::Shared<crate::Module>)> {
|
||||
) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> {
|
||||
self.imports.iter().zip(self.modules.iter()).rev()
|
||||
}
|
||||
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in forward order.
|
||||
@@ -208,7 +208,7 @@ impl GlobalRuntimeState {
|
||||
#[inline]
|
||||
pub fn scan_imports_raw(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (&ImmutableString, &crate::Shared<crate::Module>)> {
|
||||
) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> {
|
||||
self.imports.iter().zip(self.modules.iter())
|
||||
}
|
||||
/// Can the particular function with [`Dynamic`] parameter(s) exist in the stack of
|
||||
@@ -316,11 +316,11 @@ impl GlobalRuntimeState {
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
impl IntoIterator for GlobalRuntimeState {
|
||||
type Item = (ImmutableString, crate::Shared<crate::Module>);
|
||||
type Item = (ImmutableString, crate::SharedModule);
|
||||
type IntoIter = std::iter::Rev<
|
||||
std::iter::Zip<
|
||||
smallvec::IntoIter<[ImmutableString; crate::STATIC_VEC_INLINE_SIZE]>,
|
||||
smallvec::IntoIter<[crate::Shared<crate::Module>; crate::STATIC_VEC_INLINE_SIZE]>,
|
||||
smallvec::IntoIter<[crate::SharedModule; crate::STATIC_VEC_INLINE_SIZE]>,
|
||||
>,
|
||||
>;
|
||||
|
||||
@@ -331,11 +331,11 @@ impl IntoIterator for GlobalRuntimeState {
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
impl<'a> IntoIterator for &'a GlobalRuntimeState {
|
||||
type Item = (&'a ImmutableString, &'a crate::Shared<crate::Module>);
|
||||
type Item = (&'a ImmutableString, &'a crate::SharedModule);
|
||||
type IntoIter = std::iter::Rev<
|
||||
std::iter::Zip<
|
||||
std::slice::Iter<'a, ImmutableString>,
|
||||
std::slice::Iter<'a, crate::Shared<crate::Module>>,
|
||||
std::slice::Iter<'a, crate::SharedModule>,
|
||||
>,
|
||||
>;
|
||||
|
||||
@@ -345,9 +345,7 @@ impl<'a> IntoIterator for &'a GlobalRuntimeState {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
impl<K: Into<ImmutableString>, M: Into<crate::Shared<crate::Module>>> Extend<(K, M)>
|
||||
for GlobalRuntimeState
|
||||
{
|
||||
impl<K: Into<ImmutableString>, M: Into<crate::SharedModule>> Extend<(K, M)> for GlobalRuntimeState {
|
||||
#[inline]
|
||||
fn extend<T: IntoIterator<Item = (K, M)>>(&mut self, iter: T) {
|
||||
for (k, m) in iter {
|
||||
|
144
src/eval/stmt.rs
144
src/eval/stmt.rs
@@ -8,10 +8,7 @@ use crate::ast::{
|
||||
use crate::func::{get_builtin_op_assignment_fn, get_hasher};
|
||||
use crate::types::dynamic::{AccessMode, Union};
|
||||
use crate::types::RestoreOnDrop;
|
||||
use crate::{
|
||||
Dynamic, Engine, ImmutableString, Module, Position, RhaiResult, RhaiResultOf, Scope, Shared,
|
||||
ERR, INT,
|
||||
};
|
||||
use crate::{Dynamic, Engine, Position, RhaiResult, RhaiResultOf, Scope, SharedModule, ERR, INT};
|
||||
use std::hash::{Hash, Hasher};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
@@ -29,7 +26,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Option<&mut Dynamic>,
|
||||
@@ -42,7 +39,7 @@ impl Engine {
|
||||
|
||||
// Restore scope at end of block if necessary
|
||||
let orig_scope_len = scope.len();
|
||||
let scope = &mut *RestoreOnDrop::new_if(restore_orig_state, scope, move |s| {
|
||||
let scope = &mut *RestoreOnDrop::lock_if(restore_orig_state, scope, move |s| {
|
||||
s.rewind(orig_scope_len);
|
||||
});
|
||||
|
||||
@@ -55,7 +52,7 @@ impl Engine {
|
||||
global.scope_level += 1;
|
||||
}
|
||||
|
||||
let global = &mut *RestoreOnDrop::new_if(restore_orig_state, global, move |g| {
|
||||
let global = &mut *RestoreOnDrop::lock_if(restore_orig_state, global, move |g| {
|
||||
g.scope_level -= 1;
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
g.truncate_imports(orig_imports_len);
|
||||
@@ -67,7 +64,7 @@ impl Engine {
|
||||
|
||||
// Pop new function resolution caches at end of block
|
||||
let orig_fn_resolution_caches_len = caches.fn_resolution_caches_len();
|
||||
let caches = &mut *RestoreOnDrop::new(caches, move |c| {
|
||||
let caches = &mut *RestoreOnDrop::lock(caches, move |c| {
|
||||
c.rewind_fn_resolution_caches(orig_fn_resolution_caches_len)
|
||||
});
|
||||
|
||||
@@ -96,10 +93,11 @@ impl Engine {
|
||||
.skip(imports_len)
|
||||
.any(|(.., m)| m.contains_indexed_global_functions())
|
||||
{
|
||||
// Different scenarios where the cache must be cleared - notice that this is
|
||||
// expensive as all function resolutions must start again
|
||||
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
|
||||
// a new cache, just clear it
|
||||
caches.fn_resolution_cache_mut().clear();
|
||||
} else if restore_orig_state {
|
||||
// When new module is imported with global functions, push a new cache
|
||||
@@ -120,20 +118,18 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
op_info: &OpAssignment,
|
||||
target: &mut Target,
|
||||
root: (&str, Position),
|
||||
new_val: Dynamic,
|
||||
mut new_val: Dynamic,
|
||||
) -> RhaiResultOf<()> {
|
||||
// Assignment to constant variable?
|
||||
if target.is_read_only() {
|
||||
// Assignment to constant variable
|
||||
return Err(ERR::ErrorAssignmentToConstant(root.0.to_string(), root.1).into());
|
||||
}
|
||||
|
||||
let mut new_val = new_val;
|
||||
|
||||
if op_info.is_op_assignment() {
|
||||
let OpAssignment {
|
||||
hash_op_assign,
|
||||
@@ -204,7 +200,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Option<&mut Dynamic>,
|
||||
@@ -215,7 +211,7 @@ impl Engine {
|
||||
let reset =
|
||||
self.run_debugger_with_reset(global, caches, lib, level, scope, this_ptr, stmt)?;
|
||||
#[cfg(feature = "debugging")]
|
||||
let global = &mut *RestoreOnDrop::new(global, move |g| g.debugger.reset_status(reset));
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| g.debugger.reset_status(reset));
|
||||
|
||||
// Coded this way for better branch prediction.
|
||||
// Popular branches are lifted out of the `match` statement into their own branches.
|
||||
@@ -240,11 +236,9 @@ impl Engine {
|
||||
.eval_expr(global, caches, lib, level, scope, this_ptr, rhs)?
|
||||
.flatten();
|
||||
|
||||
let search_val =
|
||||
let (mut lhs_ptr, pos) =
|
||||
self.search_namespace(global, caches, lib, level, scope, this_ptr, lhs)?;
|
||||
|
||||
let (mut lhs_ptr, pos) = search_val;
|
||||
|
||||
let var_name = x.3.as_str();
|
||||
|
||||
#[cfg(not(feature = "no_closure"))]
|
||||
@@ -267,16 +261,17 @@ impl Engine {
|
||||
return self
|
||||
.eval_op_assignment(global, caches, lib, level, op_info, lhs_ptr, root, rhs_val)
|
||||
.map(|_| Dynamic::UNIT);
|
||||
} else {
|
||||
let (op_info, BinaryExpr { lhs, rhs }) = &**x;
|
||||
}
|
||||
|
||||
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||
{
|
||||
let rhs_val = self.eval_expr(global, caches, lib, level, scope, this_ptr, rhs)?;
|
||||
|
||||
// Check if the result is a string. If so, intern it.
|
||||
#[cfg(not(feature = "no_closure"))]
|
||||
let is_string = !rhs_val.is_shared() && rhs_val.is::<ImmutableString>();
|
||||
let is_string = !rhs_val.is_shared() && rhs_val.is::<crate::ImmutableString>();
|
||||
#[cfg(feature = "no_closure")]
|
||||
let is_string = rhs_val.is::<ImmutableString>();
|
||||
let is_string = rhs_val.is::<crate::ImmutableString>();
|
||||
|
||||
let rhs_val = if is_string {
|
||||
self.get_interned_string(
|
||||
@@ -333,23 +328,20 @@ impl Engine {
|
||||
let (expr, if_block, else_block) = &**x;
|
||||
|
||||
let guard_val = self
|
||||
.eval_expr(global, caches, lib, level, scope, this_ptr, expr)
|
||||
.and_then(|v| {
|
||||
v.as_bool().map_err(|typ| {
|
||||
self.make_type_mismatch_err::<bool>(typ, expr.position())
|
||||
})
|
||||
});
|
||||
.eval_expr(global, caches, lib, level, scope, this_ptr, expr)?
|
||||
.as_bool()
|
||||
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, expr.position()))?;
|
||||
|
||||
match guard_val {
|
||||
Ok(true) if if_block.is_empty() => Ok(Dynamic::UNIT),
|
||||
Ok(true) => self.eval_stmt_block(
|
||||
if guard_val && !if_block.is_empty() {
|
||||
self.eval_stmt_block(
|
||||
global, caches, lib, level, scope, this_ptr, if_block, true,
|
||||
),
|
||||
Ok(false) if else_block.is_empty() => Ok(Dynamic::UNIT),
|
||||
Ok(false) => self.eval_stmt_block(
|
||||
)
|
||||
} else if !guard_val && !else_block.is_empty() {
|
||||
self.eval_stmt_block(
|
||||
global, caches, lib, level, scope, this_ptr, else_block, true,
|
||||
),
|
||||
err => err.map(Into::into),
|
||||
)
|
||||
} else {
|
||||
Ok(Dynamic::UNIT)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -421,14 +413,11 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(expr) = result {
|
||||
self.eval_expr(global, caches, lib, level, scope, this_ptr, expr)
|
||||
} else {
|
||||
def_case.as_ref().map_or(Ok(Dynamic::UNIT), |&index| {
|
||||
let def_expr = &expressions[index].expr;
|
||||
self.eval_expr(global, caches, lib, level, scope, this_ptr, def_expr)
|
||||
result
|
||||
.or_else(|| def_case.as_ref().map(|&index| &expressions[index].expr))
|
||||
.map_or(Ok(Dynamic::UNIT), |expr| {
|
||||
self.eval_expr(global, caches, lib, level, scope, this_ptr, expr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Loop
|
||||
@@ -439,17 +428,16 @@ impl Engine {
|
||||
loop {
|
||||
self.track_operation(global, body.position())?;
|
||||
}
|
||||
} else {
|
||||
loop {
|
||||
match self.eval_stmt_block(
|
||||
global, caches, lib, level, scope, this_ptr, body, true,
|
||||
) {
|
||||
Ok(_) => (),
|
||||
Err(err) => match *err {
|
||||
ERR::LoopBreak(false, ..) => (),
|
||||
ERR::LoopBreak(true, value, ..) => break Ok(value),
|
||||
_ => break Err(err),
|
||||
},
|
||||
}
|
||||
|
||||
loop {
|
||||
if let Err(err) = self
|
||||
.eval_stmt_block(global, caches, lib, level, scope, this_ptr, body, true)
|
||||
{
|
||||
match *err {
|
||||
ERR::LoopBreak(false, ..) => (),
|
||||
ERR::LoopBreak(true, value, ..) => break Ok(value),
|
||||
_ => break Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -469,15 +457,17 @@ impl Engine {
|
||||
break Ok(Dynamic::UNIT);
|
||||
}
|
||||
|
||||
if !body.is_empty() {
|
||||
if let Err(err) = self.eval_stmt_block(
|
||||
global, caches, lib, level, scope, this_ptr, body, true,
|
||||
) {
|
||||
match *err {
|
||||
ERR::LoopBreak(false, ..) => (),
|
||||
ERR::LoopBreak(true, value, ..) => break Ok(value),
|
||||
_ => break Err(err),
|
||||
}
|
||||
if body.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Err(err) = self
|
||||
.eval_stmt_block(global, caches, lib, level, scope, this_ptr, body, true)
|
||||
{
|
||||
match *err {
|
||||
ERR::LoopBreak(false, ..) => (),
|
||||
ERR::LoopBreak(true, value, ..) => break Ok(value),
|
||||
_ => break Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -544,7 +534,7 @@ impl Engine {
|
||||
if let Some(func) = func {
|
||||
// Restore scope at end of statement
|
||||
let orig_scope_len = scope.len();
|
||||
let scope = &mut *RestoreOnDrop::new(scope, move |s| {
|
||||
let scope = &mut *RestoreOnDrop::lock(scope, move |s| {
|
||||
s.rewind(orig_scope_len);
|
||||
});
|
||||
|
||||
@@ -616,12 +606,13 @@ impl Engine {
|
||||
Stmt::BreakLoop(expr, options, pos) => {
|
||||
let is_break = options.contains(ASTFlags::BREAK);
|
||||
|
||||
if let Some(ref expr) = expr {
|
||||
self.eval_expr(global, caches, lib, level, scope, this_ptr, expr)
|
||||
.and_then(|v| ERR::LoopBreak(is_break, v, *pos).into())
|
||||
let value = if let Some(ref expr) = expr {
|
||||
self.eval_expr(global, caches, lib, level, scope, this_ptr, expr)?
|
||||
} else {
|
||||
Err(ERR::LoopBreak(is_break, Dynamic::UNIT, *pos).into())
|
||||
}
|
||||
Dynamic::UNIT
|
||||
};
|
||||
|
||||
Err(ERR::LoopBreak(is_break, value, *pos).into())
|
||||
}
|
||||
|
||||
// Try/Catch statement
|
||||
@@ -638,9 +629,7 @@ impl Engine {
|
||||
match self
|
||||
.eval_stmt_block(global, caches, lib, level, scope, this_ptr, try_block, true)
|
||||
{
|
||||
Ok(_) => self.eval_stmt_block(
|
||||
global, caches, lib, level, scope, this_ptr, try_block, true,
|
||||
),
|
||||
r @ Ok(_) => r,
|
||||
Err(err) if err.is_pseudo_error() => Err(err),
|
||||
Err(err) if !err.is_catchable() => Err(err),
|
||||
Err(mut err) => {
|
||||
@@ -682,7 +671,7 @@ impl Engine {
|
||||
// Restore scope at end of block
|
||||
let orig_scope_len = scope.len();
|
||||
let scope =
|
||||
&mut *RestoreOnDrop::new_if(!catch_var.is_empty(), scope, move |s| {
|
||||
&mut *RestoreOnDrop::lock_if(!catch_var.is_empty(), scope, move |s| {
|
||||
s.rewind(orig_scope_len);
|
||||
});
|
||||
|
||||
@@ -757,7 +746,8 @@ impl Engine {
|
||||
nesting_level,
|
||||
will_shadow,
|
||||
};
|
||||
let context = EvalContext::new(self, global, None, lib, level, scope, this_ptr);
|
||||
let context =
|
||||
EvalContext::new(self, global, caches, lib, level, scope, this_ptr);
|
||||
|
||||
if !filter(true, info, context)? {
|
||||
return Err(ERR::ErrorForbiddenVariable(var_name.to_string(), *pos).into());
|
||||
@@ -917,7 +907,7 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[Shared<Module>],
|
||||
lib: &[SharedModule],
|
||||
level: usize,
|
||||
scope: &mut Scope,
|
||||
statements: &[Stmt],
|
||||
|
Reference in New Issue
Block a user