Clean up types.

This commit is contained in:
Stephen Chung 2022-11-08 15:01:40 +08:00
parent f4e2901353
commit 6053aa1641
28 changed files with 248 additions and 248 deletions

View File

@ -5,8 +5,8 @@ use crate::eval::{Caches, GlobalRuntimeState};
use crate::types::dynamic::Variant; use crate::types::dynamic::Variant;
use crate::types::RestoreOnDrop; use crate::types::RestoreOnDrop;
use crate::{ use crate::{
reify, Dynamic, Engine, FuncArgs, Position, RhaiResult, RhaiResultOf, Scope, Shared, StaticVec, reify, Dynamic, Engine, FuncArgs, Position, RhaiResult, RhaiResultOf, Scope, SharedModule,
AST, ERR, StaticVec, AST, ERR,
}; };
use std::any::{type_name, TypeId}; use std::any::{type_name, TypeId};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
@ -249,7 +249,7 @@ impl Engine {
arg_values: &mut [Dynamic], arg_values: &mut [Dynamic],
) -> RhaiResult { ) -> RhaiResult {
let statements = ast.statements(); let statements = ast.statements();
let lib = &[AsRef::<Shared<_>>::as_ref(ast).clone()]; let lib = &[AsRef::<SharedModule>::as_ref(ast).clone()];
let mut this_ptr = this_ptr; let mut this_ptr = this_ptr;
let orig_scope_len = scope.len(); let orig_scope_len = scope.len();
@ -260,7 +260,7 @@ impl Engine {
ast.resolver().cloned(), ast.resolver().cloned(),
); );
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let global = &mut *RestoreOnDrop::new(global, move |g| { let global = &mut *RestoreOnDrop::lock(global, move |g| {
g.embedded_module_resolver = orig_embedded_module_resolver g.embedded_module_resolver = orig_embedded_module_resolver
}); });

View File

@ -196,7 +196,7 @@ impl Engine {
global.debugger.status = crate::eval::DebuggerStatus::Terminate; global.debugger.status = crate::eval::DebuggerStatus::Terminate;
let lib = &[ let lib = &[
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
AsRef::<crate::Shared<_>>::as_ref(ast).clone(), AsRef::<crate::SharedModule>::as_ref(ast).clone(),
]; ];
let node = &crate::ast::Stmt::Noop(Position::NONE); let node = &crate::ast::Stmt::Noop(Position::NONE);
self.run_debugger(global, caches, lib, 0, scope, &mut None, node)?; self.run_debugger(global, caches, lib, 0, scope, &mut None, node)?;
@ -227,7 +227,7 @@ impl Engine {
ast.resolver().cloned(), ast.resolver().cloned(),
); );
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let global = &mut *RestoreOnDrop::new(global, move |g| { let global = &mut *RestoreOnDrop::lock(global, move |g| {
g.embedded_module_resolver = orig_embedded_module_resolver g.embedded_module_resolver = orig_embedded_module_resolver
}); });
@ -239,7 +239,7 @@ impl Engine {
let lib = &[ let lib = &[
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
AsRef::<crate::Shared<_>>::as_ref(ast).clone(), AsRef::<crate::SharedModule>::as_ref(ast).clone(),
]; ];
self.eval_global_statements(global, caches, lib, level, scope, statements) self.eval_global_statements(global, caches, lib, level, scope, statements)
@ -258,7 +258,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[crate::Shared<crate::Module>], lib: &[crate::SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
statements: &[crate::ast::Stmt], statements: &[crate::ast::Stmt],

View File

@ -4,6 +4,7 @@ use crate::func::{FnCallArgs, RegisterNativeFunction, SendSync};
use crate::types::dynamic::Variant; use crate::types::dynamic::Variant;
use crate::{ use crate::{
Engine, FnAccess, FnNamespace, Identifier, Module, NativeCallContext, RhaiResultOf, Shared, Engine, FnAccess, FnNamespace, Identifier, Module, NativeCallContext, RhaiResultOf, Shared,
SharedModule,
}; };
use std::any::{type_name, TypeId}; use std::any::{type_name, TypeId};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
@ -636,7 +637,7 @@ impl Engine {
/// When searching for functions, modules loaded later are preferred. In other words, loaded /// When searching for functions, modules loaded later are preferred. In other words, loaded
/// modules are searched in reverse order. /// modules are searched in reverse order.
#[inline(always)] #[inline(always)]
pub fn register_global_module(&mut self, module: Shared<Module>) -> &mut Self { pub fn register_global_module(&mut self, module: SharedModule) -> &mut Self {
// Insert the module into the front. // Insert the module into the front.
// The first module is always the global namespace. // The first module is always the global namespace.
self.global_modules.insert(1, module); self.global_modules.insert(1, module);
@ -661,7 +662,7 @@ impl Engine {
/// let mut module = Module::new(); /// let mut module = Module::new();
/// module.set_native_fn("calc", |x: i64| Ok(x + 1)); /// module.set_native_fn("calc", |x: i64| Ok(x + 1));
/// ///
/// let module: Shared<Module> = module.into(); /// let module: SharedModule = module.into();
/// ///
/// engine /// engine
/// // Register the module as a fixed sub-module /// // Register the module as a fixed sub-module
@ -680,12 +681,12 @@ impl Engine {
pub fn register_static_module( pub fn register_static_module(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
module: Shared<Module>, module: SharedModule,
) -> &mut Self { ) -> &mut Self {
fn register_static_module_raw( fn register_static_module_raw(
root: &mut std::collections::BTreeMap<Identifier, Shared<Module>>, root: &mut std::collections::BTreeMap<Identifier, SharedModule>,
name: &str, name: &str,
module: Shared<Module>, module: SharedModule,
) { ) {
let separator = crate::tokenizer::Token::DoubleColon.syntax(); let separator = crate::tokenizer::Token::DoubleColon.syntax();
let separator = separator.as_ref(); let separator = separator.as_ref();

View File

@ -2,7 +2,7 @@
use crate::eval::{Caches, GlobalRuntimeState}; use crate::eval::{Caches, GlobalRuntimeState};
use crate::parser::ParseState; use crate::parser::ParseState;
use crate::{Engine, RhaiResultOf, Scope, AST}; use crate::{Engine, RhaiResultOf, Scope, SharedModule, AST};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -122,9 +122,9 @@ impl Engine {
let statements = ast.statements(); let statements = ast.statements();
if !statements.is_empty() { if !statements.is_empty() {
let lib: &[crate::Shared<crate::Module>] = &[ let lib: &[SharedModule] = &[
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
AsRef::<crate::Shared<crate::Module>>::as_ref(ast).clone(), AsRef::<SharedModule>::as_ref(ast).clone(),
]; ];
let lib = if lib.first().map_or(true, |m| m.is_empty()) { let lib = if lib.first().map_or(true, |m| m.is_empty()) {
&[][..] &[][..]
@ -139,7 +139,7 @@ impl Engine {
global.debugger.status = crate::eval::DebuggerStatus::Terminate; global.debugger.status = crate::eval::DebuggerStatus::Terminate;
let lib = &[ let lib = &[
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
AsRef::<crate::Shared<_>>::as_ref(ast).clone(), AsRef::<crate::SharedModule>::as_ref(ast).clone(),
]; ];
let node = &crate::ast::Stmt::Noop(crate::Position::NONE); let node = &crate::ast::Stmt::Noop(crate::Position::NONE);
self.run_debugger(global, caches, lib, 0, scope, &mut None, node)?; self.run_debugger(global, caches, lib, 0, scope, &mut None, node)?;

View File

@ -28,7 +28,7 @@ pub struct AST {
body: StmtBlock, body: StmtBlock,
/// Script-defined functions. /// Script-defined functions.
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
lib: crate::Shared<crate::Module>, lib: crate::SharedModule,
/// Embedded module resolver, if any. /// Embedded module resolver, if any.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
resolver: Option<crate::Shared<crate::module::resolvers::StaticModuleResolver>>, resolver: Option<crate::Shared<crate::module::resolvers::StaticModuleResolver>>,
@ -74,7 +74,7 @@ impl AST {
#[must_use] #[must_use]
pub(crate) fn new( pub(crate) fn new(
statements: impl IntoIterator<Item = Stmt>, statements: impl IntoIterator<Item = Stmt>,
#[cfg(not(feature = "no_function"))] functions: impl Into<crate::Shared<crate::Module>>, #[cfg(not(feature = "no_function"))] functions: impl Into<crate::SharedModule>,
) -> Self { ) -> Self {
Self { Self {
source: None, source: None,
@ -94,7 +94,7 @@ impl AST {
#[must_use] #[must_use]
pub fn new( pub fn new(
statements: impl IntoIterator<Item = Stmt>, statements: impl IntoIterator<Item = Stmt>,
#[cfg(not(feature = "no_function"))] functions: impl Into<crate::Shared<crate::Module>>, #[cfg(not(feature = "no_function"))] functions: impl Into<crate::SharedModule>,
) -> Self { ) -> Self {
Self { Self {
source: None, source: None,
@ -113,7 +113,7 @@ impl AST {
#[must_use] #[must_use]
pub(crate) fn new_with_source( pub(crate) fn new_with_source(
statements: impl IntoIterator<Item = Stmt>, statements: impl IntoIterator<Item = Stmt>,
#[cfg(not(feature = "no_function"))] functions: impl Into<crate::Shared<crate::Module>>, #[cfg(not(feature = "no_function"))] functions: impl Into<crate::SharedModule>,
source: impl Into<ImmutableString>, source: impl Into<ImmutableString>,
) -> Self { ) -> Self {
let mut ast = Self::new( let mut ast = Self::new(
@ -131,7 +131,7 @@ impl AST {
#[must_use] #[must_use]
pub fn new_with_source( pub fn new_with_source(
statements: impl IntoIterator<Item = Stmt>, statements: impl IntoIterator<Item = Stmt>,
#[cfg(not(feature = "no_function"))] functions: impl Into<crate::Shared<crate::Module>>, #[cfg(not(feature = "no_function"))] functions: impl Into<crate::SharedModule>,
source: impl Into<ImmutableString>, source: impl Into<ImmutableString>,
) -> Self { ) -> Self {
let mut ast = Self::new( let mut ast = Self::new(
@ -267,7 +267,7 @@ impl AST {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub(crate) const fn shared_lib(&self) -> &crate::Shared<crate::Module> { pub(crate) const fn shared_lib(&self) -> &SharedModule {
&self.lib &self.lib
} }
/// _(internals)_ Get the internal shared [`Module`][crate::Module] containing all script-defined functions. /// _(internals)_ Get the internal shared [`Module`][crate::Module] containing all script-defined functions.
@ -278,7 +278,7 @@ impl AST {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub const fn shared_lib(&self) -> &crate::Shared<crate::Module> { pub const fn shared_lib(&self) -> &crate::SharedModule {
&self.lib &self.lib
} }
/// Get the embedded [module resolver][crate::ModuleResolver]. /// Get the embedded [module resolver][crate::ModuleResolver].
@ -957,19 +957,19 @@ impl AsRef<crate::Module> for AST {
} }
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
impl Borrow<crate::Shared<crate::Module>> for AST { impl Borrow<crate::SharedModule> for AST {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
fn borrow(&self) -> &crate::Shared<crate::Module> { fn borrow(&self) -> &crate::SharedModule {
self.shared_lib() self.shared_lib()
} }
} }
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
impl AsRef<crate::Shared<crate::Module>> for AST { impl AsRef<crate::SharedModule> for AST {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
fn as_ref(&self) -> &crate::Shared<crate::Module> { fn as_ref(&self) -> &crate::SharedModule {
self.shared_lib() self.shared_lib()
} }
} }

View File

@ -20,9 +20,9 @@ use std::{fmt, hash::Hash};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct EncapsulatedEnviron { pub struct EncapsulatedEnviron {
/// Functions defined within the same [`AST`][crate::AST]. /// Functions defined within the same [`AST`][crate::AST].
pub lib: crate::Shared<crate::Module>, pub lib: crate::SharedModule,
/// Imported [modules][crate::Module]. /// Imported [modules][crate::Module].
pub imports: Box<[(ImmutableString, crate::Shared<crate::Module>)]>, pub imports: Box<[(ImmutableString, crate::SharedModule)]>,
/// Globally-defined constants. /// Globally-defined constants.
pub constants: Option<crate::eval::GlobalConstants>, pub constants: Option<crate::eval::GlobalConstants>,
} }

View File

@ -9,7 +9,8 @@ use crate::packages::{Package, StandardPackage};
use crate::tokenizer::Token; use crate::tokenizer::Token;
use crate::types::StringsInterner; use crate::types::StringsInterner;
use crate::{ use crate::{
Dynamic, Identifier, ImmutableString, Locked, Module, OptimizationLevel, Shared, StaticVec, Dynamic, Identifier, ImmutableString, Locked, Module, OptimizationLevel, SharedModule,
StaticVec,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -91,10 +92,10 @@ pub const OP_INCLUSIVE_RANGE: &str = Token::InclusiveRange.literal_syntax();
/// ``` /// ```
pub struct Engine { pub struct Engine {
/// A collection of all modules loaded into the global namespace of the Engine. /// A collection of all modules loaded into the global namespace of the Engine.
pub(crate) global_modules: StaticVec<Shared<Module>>, pub(crate) global_modules: StaticVec<SharedModule>,
/// A collection of all sub-modules directly loaded into the Engine. /// A collection of all sub-modules directly loaded into the Engine.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
pub(crate) global_sub_modules: std::collections::BTreeMap<Identifier, Shared<Module>>, pub(crate) global_sub_modules: std::collections::BTreeMap<Identifier, SharedModule>,
/// A module resolution service. /// A module resolution service.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]

View File

@ -6,7 +6,7 @@ use crate::ast::{ASTFlags, Expr, OpAssignment};
use crate::types::dynamic::Union; use crate::types::dynamic::Union;
use crate::types::RestoreOnDrop; use crate::types::RestoreOnDrop;
use crate::{ 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; use std::hash::Hash;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
@ -43,7 +43,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
target: &mut Target, target: &mut Target,
@ -205,7 +205,7 @@ impl Engine {
global, caches, lib, level, scope, this_ptr, rhs, global, caches, lib, level, scope, this_ptr, rhs,
)?; )?;
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
let global = &mut *RestoreOnDrop::new(global, move |g| { let global = &mut *RestoreOnDrop::lock(global, move |g| {
g.debugger.reset_status(reset) g.debugger.reset_status(reset)
}); });
@ -216,7 +216,7 @@ impl Engine {
// Truncate the index values upon exit // Truncate the index values upon exit
let offset = idx_values.len() - args.len(); let offset = idx_values.len() - args.len();
let idx_values = 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 call_args = &mut idx_values[offset..];
let pos1 = args.get(0).map_or(Position::NONE, Expr::position); 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, global, caches, lib, level, scope, this_ptr, _node,
)?; )?;
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
let global = &mut *RestoreOnDrop::new(global, move |g| { let global = &mut *RestoreOnDrop::lock(global, move |g| {
g.debugger.reset_status(reset) g.debugger.reset_status(reset)
}); });
@ -394,7 +394,7 @@ impl Engine {
// Truncate the index values upon exit // Truncate the index values upon exit
let offset = idx_values.len() - args.len(); 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) v.truncate(offset)
}); });
@ -516,7 +516,7 @@ impl Engine {
global, caches, lib, level, scope, this_ptr, _node, global, caches, lib, level, scope, this_ptr, _node,
)?; )?;
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
let global = &mut *RestoreOnDrop::new(global, move |g| { let global = &mut *RestoreOnDrop::lock(global, move |g| {
g.debugger.reset_status(reset) g.debugger.reset_status(reset)
}); });
@ -527,7 +527,7 @@ impl Engine {
// Truncate the index values upon exit // Truncate the index values upon exit
let offset = idx_values.len() - args.len(); let offset = idx_values.len() - args.len();
let idx_values = let idx_values =
&mut *RestoreOnDrop::new(idx_values, move |v| { &mut *RestoreOnDrop::lock(idx_values, move |v| {
v.truncate(offset) v.truncate(offset)
}); });
@ -570,7 +570,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -661,7 +661,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -773,7 +773,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
target: &mut Dynamic, target: &mut Dynamic,
idx: &mut Dynamic, idx: &mut Dynamic,
@ -796,7 +796,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
target: &mut Dynamic, target: &mut Dynamic,
idx: &mut Dynamic, idx: &mut Dynamic,
@ -820,7 +820,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
target: &'t mut Dynamic, target: &'t mut Dynamic,
idx: &mut Dynamic, idx: &mut Dynamic,

View File

@ -4,7 +4,7 @@
use super::{Caches, EvalContext, GlobalRuntimeState}; use super::{Caches, EvalContext, GlobalRuntimeState};
use crate::ast::{ASTNode, Expr, Stmt}; use crate::ast::{ASTNode, Expr, Stmt};
use crate::{ use crate::{
Dynamic, Engine, EvalAltResult, ImmutableString, Module, Position, RhaiResultOf, Scope, Shared, Dynamic, Engine, EvalAltResult, ImmutableString, Position, RhaiResultOf, Scope, SharedModule,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -413,7 +413,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -440,7 +440,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -463,7 +463,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -510,7 +510,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -519,8 +519,7 @@ impl Engine {
) -> Result<Option<DebuggerStatus>, Box<crate::EvalAltResult>> { ) -> Result<Option<DebuggerStatus>, Box<crate::EvalAltResult>> {
let src = global.source_raw().cloned(); let src = global.source_raw().cloned();
let src = src.as_ref().map(|s| s.as_str()); let src = src.as_ref().map(|s| s.as_str());
let context = let context = crate::EvalContext::new(self, global, caches, lib, level, scope, this_ptr);
crate::EvalContext::new(self, global, Some(caches), lib, level, scope, this_ptr);
if let Some((.., ref on_debugger)) = self.debugger { if let Some((.., ref on_debugger)) = self.debugger {
let command = on_debugger(context, event, node, src, node.position())?; let command = on_debugger(context, event, node, src, node.position())?;

View File

@ -1,7 +1,7 @@
//! Evaluation context. //! Evaluation context.
use super::{Caches, GlobalRuntimeState}; use super::{Caches, GlobalRuntimeState};
use crate::{Dynamic, Engine, Module, Scope, Shared}; use crate::{Dynamic, Engine, Module, Scope, SharedModule};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -16,9 +16,9 @@ pub struct EvalContext<'a, 's, 'ps, 'g, 'c, 't, 'pt> {
/// The current [`GlobalRuntimeState`]. /// The current [`GlobalRuntimeState`].
global: &'g mut GlobalRuntimeState, global: &'g mut GlobalRuntimeState,
/// The current [caches][Caches], if available. /// The current [caches][Caches], if available.
caches: Option<&'c mut Caches>, caches: &'c mut Caches,
/// The current stack of imported [modules][Module]. /// The current stack of imported [modules][Module].
lib: &'a [Shared<Module>], lib: &'a [SharedModule],
/// The current bound `this` pointer, if any. /// The current bound `this` pointer, if any.
this_ptr: &'t mut Option<&'pt mut Dynamic>, this_ptr: &'t mut Option<&'pt mut Dynamic>,
/// The current nesting level of function calls. /// 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( pub fn new(
engine: &'a Engine, engine: &'a Engine,
global: &'g mut GlobalRuntimeState, global: &'g mut GlobalRuntimeState,
caches: Option<&'c mut Caches>, caches: &'c mut Caches,
lib: &'a [Shared<Module>], lib: &'a [SharedModule],
level: usize, level: usize,
scope: &'s mut Scope<'ps>, scope: &'s mut Scope<'ps>,
this_ptr: &'t mut Option<&'pt mut Dynamic>, 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")] #[cfg(feature = "internals")]
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub const fn namespaces(&self) -> &[Shared<Module>] { pub const fn namespaces(&self) -> &[SharedModule] {
self.lib self.lib
} }
/// The current bound `this` pointer, if any. /// 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 { ) -> crate::RhaiResult {
let expr: &crate::ast::Expr = expr; 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 { match expr {
crate::ast::Expr::Stmt(statements) => self.engine.eval_stmt_block( crate::ast::Expr::Stmt(statements) => self.engine.eval_stmt_block(
self.global, self.global,
caches, self.caches,
self.lib, self.lib,
self.level, self.level,
self.scope, 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.engine.eval_expr(
self.global, self.global,
caches, self.caches,
self.lib, self.lib,
self.level, self.level,
self.scope, self.scope,

View File

@ -4,8 +4,7 @@ use super::{Caches, EvalContext, GlobalRuntimeState, Target};
use crate::ast::{Expr, OpAssignment}; use crate::ast::{Expr, OpAssignment};
use crate::engine::{KEYWORD_THIS, OP_CONCAT}; use crate::engine::{KEYWORD_THIS, OP_CONCAT};
use crate::types::dynamic::AccessMode; use crate::types::dynamic::AccessMode;
use crate::types::RestoreOnDrop; use crate::{Dynamic, Engine, Position, RhaiResult, RhaiResultOf, Scope, SharedModule, ERR};
use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, Shared, ERR};
use std::num::NonZeroUsize; use std::num::NonZeroUsize;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -19,7 +18,7 @@ impl Engine {
&self, &self,
global: &GlobalRuntimeState, global: &GlobalRuntimeState,
namespace: &crate::ast::Namespace, namespace: &crate::ast::Namespace,
) -> Option<Shared<Module>> { ) -> Option<SharedModule> {
assert!(!namespace.is_empty()); assert!(!namespace.is_empty());
let root = namespace.root(); let root = namespace.root();
@ -52,7 +51,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &'s mut Scope, scope: &'s mut Scope,
this_ptr: &'s mut Option<&mut Dynamic>, this_ptr: &'s mut Option<&mut Dynamic>,
@ -138,7 +137,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &'s mut Scope, scope: &'s mut Scope,
this_ptr: &'s mut Option<&mut Dynamic>, this_ptr: &'s mut Option<&mut Dynamic>,
@ -174,7 +173,7 @@ impl Engine {
// Check the variable resolver, if any // Check the variable resolver, if any
if let Some(ref resolve_var) = self.resolve_var { 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`"); let var_name = expr.get_variable_name(true).expect("`Expr::Variable`");
match resolve_var(var_name, index, context) { match resolve_var(var_name, index, context) {
Ok(Some(mut result)) => { Ok(Some(mut result)) => {
@ -222,7 +221,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -238,7 +237,9 @@ impl Engine {
let reset = let reset =
self.run_debugger_with_reset(global, caches, lib, level, scope, this_ptr, expr)?; self.run_debugger_with_reset(global, caches, lib, level, scope, this_ptr, expr)?;
#[cfg(feature = "debugging")] #[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())?; self.track_operation(global, expr.position())?;
@ -269,7 +270,9 @@ impl Engine {
let reset = let reset =
self.run_debugger_with_reset(global, caches, lib, level, scope, this_ptr, expr)?; self.run_debugger_with_reset(global, caches, lib, level, scope, this_ptr, expr)?;
#[cfg(feature = "debugging")] #[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())?; self.track_operation(global, expr.position())?;
@ -418,7 +421,7 @@ impl Engine {
)) ))
})?; })?;
let mut context = 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); let result = (custom_def.func)(&mut context, &expressions, &custom.state);

View File

@ -28,7 +28,7 @@ pub struct GlobalRuntimeState {
imports: crate::StaticVec<ImmutableString>, imports: crate::StaticVec<ImmutableString>,
/// Stack of imported [modules][crate::Module]. /// Stack of imported [modules][crate::Module].
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
modules: crate::StaticVec<crate::Shared<crate::Module>>, modules: crate::StaticVec<crate::SharedModule>,
/// Source of the current context. /// Source of the current context.
/// ///
/// No source if the string is empty. /// No source if the string is empty.
@ -127,7 +127,7 @@ impl GlobalRuntimeState {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[inline(always)] #[inline(always)]
#[must_use] #[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() self.modules.get(index).cloned()
} }
/// Get a mutable reference to the globally-imported [module][crate::Module] at a /// 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( pub(crate) fn get_shared_import_mut(
&mut self, &mut self,
index: usize, index: usize,
) -> Option<&mut crate::Shared<crate::Module>> { ) -> Option<&mut crate::SharedModule> {
self.modules.get_mut(index) self.modules.get_mut(index)
} }
/// Get the index of a globally-imported [module][crate::Module] by name. /// Get the index of a globally-imported [module][crate::Module] by name.
@ -165,7 +165,7 @@ impl GlobalRuntimeState {
pub fn push_import( pub fn push_import(
&mut self, &mut self,
name: impl Into<ImmutableString>, name: impl Into<ImmutableString>,
module: impl Into<crate::Shared<crate::Module>>, module: impl Into<crate::SharedModule>,
) { ) {
self.imports.push(name.into()); self.imports.push(name.into());
self.modules.push(module.into()); self.modules.push(module.into());
@ -198,7 +198,7 @@ impl GlobalRuntimeState {
#[inline] #[inline]
pub(crate) fn iter_imports_raw( pub(crate) fn iter_imports_raw(
&self, &self,
) -> impl Iterator<Item = (&ImmutableString, &crate::Shared<crate::Module>)> { ) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> {
self.imports.iter().zip(self.modules.iter()).rev() self.imports.iter().zip(self.modules.iter()).rev()
} }
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in forward order. /// Get an iterator to the stack of globally-imported [modules][crate::Module] in forward order.
@ -208,7 +208,7 @@ impl GlobalRuntimeState {
#[inline] #[inline]
pub fn scan_imports_raw( pub fn scan_imports_raw(
&self, &self,
) -> impl Iterator<Item = (&ImmutableString, &crate::Shared<crate::Module>)> { ) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> {
self.imports.iter().zip(self.modules.iter()) self.imports.iter().zip(self.modules.iter())
} }
/// Can the particular function with [`Dynamic`] parameter(s) exist in the stack of /// Can the particular function with [`Dynamic`] parameter(s) exist in the stack of
@ -316,11 +316,11 @@ impl GlobalRuntimeState {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
impl IntoIterator for GlobalRuntimeState { impl IntoIterator for GlobalRuntimeState {
type Item = (ImmutableString, crate::Shared<crate::Module>); type Item = (ImmutableString, crate::SharedModule);
type IntoIter = std::iter::Rev< type IntoIter = std::iter::Rev<
std::iter::Zip< std::iter::Zip<
smallvec::IntoIter<[ImmutableString; crate::STATIC_VEC_INLINE_SIZE]>, 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"))] #[cfg(not(feature = "no_module"))]
impl<'a> IntoIterator for &'a GlobalRuntimeState { 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< type IntoIter = std::iter::Rev<
std::iter::Zip< std::iter::Zip<
std::slice::Iter<'a, ImmutableString>, 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"))] #[cfg(not(feature = "no_module"))]
impl<K: Into<ImmutableString>, M: Into<crate::Shared<crate::Module>>> Extend<(K, M)> impl<K: Into<ImmutableString>, M: Into<crate::SharedModule>> Extend<(K, M)> for GlobalRuntimeState {
for GlobalRuntimeState
{
#[inline] #[inline]
fn extend<T: IntoIterator<Item = (K, M)>>(&mut self, iter: T) { fn extend<T: IntoIterator<Item = (K, M)>>(&mut self, iter: T) {
for (k, m) in iter { for (k, m) in iter {

View File

@ -8,10 +8,7 @@ use crate::ast::{
use crate::func::{get_builtin_op_assignment_fn, get_hasher}; use crate::func::{get_builtin_op_assignment_fn, get_hasher};
use crate::types::dynamic::{AccessMode, Union}; use crate::types::dynamic::{AccessMode, Union};
use crate::types::RestoreOnDrop; use crate::types::RestoreOnDrop;
use crate::{ use crate::{Dynamic, Engine, Position, RhaiResult, RhaiResultOf, Scope, SharedModule, ERR, INT};
Dynamic, Engine, ImmutableString, Module, Position, RhaiResult, RhaiResultOf, Scope, Shared,
ERR, INT,
};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -29,7 +26,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -42,7 +39,7 @@ impl Engine {
// Restore scope at end of block if necessary // Restore scope at end of block if necessary
let orig_scope_len = scope.len(); 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); s.rewind(orig_scope_len);
}); });
@ -55,7 +52,7 @@ impl Engine {
global.scope_level += 1; 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; g.scope_level -= 1;
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
g.truncate_imports(orig_imports_len); g.truncate_imports(orig_imports_len);
@ -67,7 +64,7 @@ impl Engine {
// Pop new function resolution caches at end of block // Pop new function resolution caches at end of block
let orig_fn_resolution_caches_len = caches.fn_resolution_caches_len(); 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) c.rewind_fn_resolution_caches(orig_fn_resolution_caches_len)
}); });
@ -96,10 +93,11 @@ impl Engine {
.skip(imports_len) .skip(imports_len)
.any(|(.., m)| m.contains_indexed_global_functions()) .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 { if caches.fn_resolution_caches_len() > orig_fn_resolution_caches_len {
// When new module is imported with global functions and there is already // 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 // a new cache, just clear it
// resolutions must start again
caches.fn_resolution_cache_mut().clear(); caches.fn_resolution_cache_mut().clear();
} else if restore_orig_state { } else if restore_orig_state {
// When new module is imported with global functions, push a new cache // When new module is imported with global functions, push a new cache
@ -120,20 +118,18 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
op_info: &OpAssignment, op_info: &OpAssignment,
target: &mut Target, target: &mut Target,
root: (&str, Position), root: (&str, Position),
new_val: Dynamic, mut new_val: Dynamic,
) -> RhaiResultOf<()> { ) -> RhaiResultOf<()> {
// Assignment to constant variable?
if target.is_read_only() { if target.is_read_only() {
// Assignment to constant variable
return Err(ERR::ErrorAssignmentToConstant(root.0.to_string(), root.1).into()); return Err(ERR::ErrorAssignmentToConstant(root.0.to_string(), root.1).into());
} }
let mut new_val = new_val;
if op_info.is_op_assignment() { if op_info.is_op_assignment() {
let OpAssignment { let OpAssignment {
hash_op_assign, hash_op_assign,
@ -204,7 +200,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -215,7 +211,7 @@ impl Engine {
let reset = let reset =
self.run_debugger_with_reset(global, caches, lib, level, scope, this_ptr, stmt)?; self.run_debugger_with_reset(global, caches, lib, level, scope, this_ptr, stmt)?;
#[cfg(feature = "debugging")] #[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. // Coded this way for better branch prediction.
// Popular branches are lifted out of the `match` statement into their own branches. // 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)? .eval_expr(global, caches, lib, level, scope, this_ptr, rhs)?
.flatten(); .flatten();
let search_val = let (mut lhs_ptr, pos) =
self.search_namespace(global, caches, lib, level, scope, this_ptr, lhs)?; 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(); let var_name = x.3.as_str();
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
@ -267,16 +261,17 @@ impl Engine {
return self return self
.eval_op_assignment(global, caches, lib, level, op_info, lhs_ptr, root, rhs_val) .eval_op_assignment(global, caches, lib, level, op_info, lhs_ptr, root, rhs_val)
.map(|_| Dynamic::UNIT); .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)?; 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. // Check if the result is a string. If so, intern it.
#[cfg(not(feature = "no_closure"))] #[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")] #[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 { let rhs_val = if is_string {
self.get_interned_string( self.get_interned_string(
@ -333,23 +328,20 @@ impl Engine {
let (expr, if_block, else_block) = &**x; let (expr, if_block, else_block) = &**x;
let guard_val = self let guard_val = self
.eval_expr(global, caches, lib, level, scope, this_ptr, expr) .eval_expr(global, caches, lib, level, scope, this_ptr, expr)?
.and_then(|v| { .as_bool()
v.as_bool().map_err(|typ| { .map_err(|typ| self.make_type_mismatch_err::<bool>(typ, expr.position()))?;
self.make_type_mismatch_err::<bool>(typ, expr.position())
})
});
match guard_val { if guard_val && !if_block.is_empty() {
Ok(true) if if_block.is_empty() => Ok(Dynamic::UNIT), self.eval_stmt_block(
Ok(true) => self.eval_stmt_block(
global, caches, lib, level, scope, this_ptr, if_block, true, global, caches, lib, level, scope, this_ptr, if_block, true,
), )
Ok(false) if else_block.is_empty() => Ok(Dynamic::UNIT), } else if !guard_val && !else_block.is_empty() {
Ok(false) => self.eval_stmt_block( self.eval_stmt_block(
global, caches, lib, level, scope, this_ptr, else_block, true, 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 { result
self.eval_expr(global, caches, lib, level, scope, this_ptr, expr) .or_else(|| def_case.as_ref().map(|&index| &expressions[index].expr))
} else { .map_or(Ok(Dynamic::UNIT), |expr| {
def_case.as_ref().map_or(Ok(Dynamic::UNIT), |&index| { self.eval_expr(global, caches, lib, level, scope, this_ptr, expr)
let def_expr = &expressions[index].expr;
self.eval_expr(global, caches, lib, level, scope, this_ptr, def_expr)
}) })
}
} }
// Loop // Loop
@ -439,17 +428,16 @@ impl Engine {
loop { loop {
self.track_operation(global, body.position())?; self.track_operation(global, body.position())?;
} }
} else { }
loop {
match self.eval_stmt_block( loop {
global, caches, lib, level, scope, this_ptr, body, true, if let Err(err) = self
) { .eval_stmt_block(global, caches, lib, level, scope, this_ptr, body, true)
Ok(_) => (), {
Err(err) => match *err { match *err {
ERR::LoopBreak(false, ..) => (), ERR::LoopBreak(false, ..) => (),
ERR::LoopBreak(true, value, ..) => break Ok(value), ERR::LoopBreak(true, value, ..) => break Ok(value),
_ => break Err(err), _ => break Err(err),
},
} }
} }
} }
@ -469,15 +457,17 @@ impl Engine {
break Ok(Dynamic::UNIT); break Ok(Dynamic::UNIT);
} }
if !body.is_empty() { if body.is_empty() {
if let Err(err) = self.eval_stmt_block( continue;
global, caches, lib, level, scope, this_ptr, body, true, }
) {
match *err { if let Err(err) = self
ERR::LoopBreak(false, ..) => (), .eval_stmt_block(global, caches, lib, level, scope, this_ptr, body, true)
ERR::LoopBreak(true, value, ..) => break Ok(value), {
_ => break Err(err), 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 { if let Some(func) = func {
// Restore scope at end of statement // Restore scope at end of statement
let orig_scope_len = scope.len(); 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); s.rewind(orig_scope_len);
}); });
@ -616,12 +606,13 @@ impl Engine {
Stmt::BreakLoop(expr, options, pos) => { Stmt::BreakLoop(expr, options, pos) => {
let is_break = options.contains(ASTFlags::BREAK); let is_break = options.contains(ASTFlags::BREAK);
if let Some(ref expr) = expr { let value = if let Some(ref expr) = expr {
self.eval_expr(global, caches, lib, level, scope, this_ptr, expr) self.eval_expr(global, caches, lib, level, scope, this_ptr, expr)?
.and_then(|v| ERR::LoopBreak(is_break, v, *pos).into())
} else { } else {
Err(ERR::LoopBreak(is_break, Dynamic::UNIT, *pos).into()) Dynamic::UNIT
} };
Err(ERR::LoopBreak(is_break, value, *pos).into())
} }
// Try/Catch statement // Try/Catch statement
@ -638,9 +629,7 @@ impl Engine {
match self match self
.eval_stmt_block(global, caches, lib, level, scope, this_ptr, try_block, true) .eval_stmt_block(global, caches, lib, level, scope, this_ptr, try_block, true)
{ {
Ok(_) => self.eval_stmt_block( r @ Ok(_) => r,
global, caches, lib, level, scope, this_ptr, try_block, true,
),
Err(err) if err.is_pseudo_error() => Err(err), Err(err) if err.is_pseudo_error() => Err(err),
Err(err) if !err.is_catchable() => Err(err), Err(err) if !err.is_catchable() => Err(err),
Err(mut err) => { Err(mut err) => {
@ -682,7 +671,7 @@ impl Engine {
// Restore scope at end of block // Restore scope at end of block
let orig_scope_len = scope.len(); let orig_scope_len = scope.len();
let scope = 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); s.rewind(orig_scope_len);
}); });
@ -757,7 +746,8 @@ impl Engine {
nesting_level, nesting_level,
will_shadow, 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)? { if !filter(true, info, context)? {
return Err(ERR::ErrorForbiddenVariable(var_name.to_string(), *pos).into()); return Err(ERR::ErrorForbiddenVariable(var_name.to_string(), *pos).into());
@ -917,7 +907,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
statements: &[Stmt], statements: &[Stmt],

View File

@ -11,8 +11,8 @@ use crate::eval::{Caches, FnResolutionCacheEntry, GlobalRuntimeState};
use crate::tokenizer::{is_valid_function_name, Token}; use crate::tokenizer::{is_valid_function_name, Token};
use crate::types::RestoreOnDrop; use crate::types::RestoreOnDrop;
use crate::{ use crate::{
calc_fn_hash, calc_fn_hash_full, Dynamic, Engine, FnArgsVec, FnPtr, ImmutableString, Module, calc_fn_hash, calc_fn_hash_full, Dynamic, Engine, FnArgsVec, FnPtr, ImmutableString, SharedModule,
OptimizationLevel, Position, RhaiError, RhaiResult, RhaiResultOf, Scope, Shared, ERR, OptimizationLevel, Position, RhaiError, RhaiResult, RhaiResultOf, Scope, ERR,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use hashbrown::hash_map::Entry; use hashbrown::hash_map::Entry;
@ -169,7 +169,7 @@ impl Engine {
_global: &GlobalRuntimeState, _global: &GlobalRuntimeState,
caches: &'s mut Caches, caches: &'s mut Caches,
local_entry: &'s mut Option<FnResolutionCacheEntry>, local_entry: &'s mut Option<FnResolutionCacheEntry>,
lib: &[Shared<Module>], lib: &[SharedModule],
op_token: Option<&Token>, op_token: Option<&Token>,
hash_base: u64, hash_base: u64,
args: Option<&mut FnCallArgs>, args: Option<&mut FnCallArgs>,
@ -325,7 +325,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
name: &str, name: &str,
op_token: Option<&Token>, op_token: Option<&Token>,
@ -371,7 +371,7 @@ impl Engine {
} }
let args = let args =
&mut *RestoreOnDrop::new_if(swap, &mut args, move |a| backup.restore_first_arg(a)); &mut *RestoreOnDrop::lock_if(swap, &mut args, move |a| backup.restore_first_arg(a));
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
if self.debugger.is_some() { if self.debugger.is_some() {
@ -537,7 +537,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
_scope: Option<&mut Scope>, _scope: Option<&mut Scope>,
fn_name: &str, fn_name: &str,
@ -585,7 +585,7 @@ impl Engine {
} else { } else {
let hash_script = let hash_script =
calc_fn_hash(None, fn_name.as_str(), num_params as usize); calc_fn_hash(None, fn_name.as_str(), num_params as usize);
self.has_script_fn(Some(global), caches, lib, hash_script) self.has_script_fn(global, caches, lib, hash_script)
} }
.into(), .into(),
false, false,
@ -639,7 +639,7 @@ impl Engine {
}; };
let orig_source = mem::replace(&mut global.source, source.clone()); let orig_source = mem::replace(&mut global.source, source.clone());
let global = &mut *RestoreOnDrop::new(global, move |g| g.source = orig_source); let global = &mut *RestoreOnDrop::lock(global, move |g| g.source = orig_source);
return if _is_method_call { return if _is_method_call {
// Method call of script function - map first argument to `this` // Method call of script function - map first argument to `this`
@ -668,7 +668,7 @@ impl Engine {
backup.change_first_arg_to_copy(args); backup.change_first_arg_to_copy(args);
} }
let args = &mut *RestoreOnDrop::new_if(swap, &mut args, move |a| { let args = &mut *RestoreOnDrop::lock_if(swap, &mut args, move |a| {
backup.restore_first_arg(a) backup.restore_first_arg(a)
}); });
@ -694,7 +694,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -716,7 +716,7 @@ impl Engine {
matches!(status, crate::eval::DebuggerStatus::FunctionExit(..)) matches!(status, crate::eval::DebuggerStatus::FunctionExit(..))
}); });
#[cfg(feature = "debugging")] #[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));
self.eval_expr(global, caches, lib, level, scope, this_ptr, arg_expr) self.eval_expr(global, caches, lib, level, scope, this_ptr, arg_expr)
.map(|r| (r, arg_expr.start_position())) .map(|r| (r, arg_expr.start_position()))
@ -728,7 +728,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
fn_name: &str, fn_name: &str,
mut hash: FnCallHashes, mut hash: FnCallHashes,
@ -953,7 +953,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -1087,7 +1087,7 @@ impl Engine {
false false
} else { } else {
let hash_script = calc_fn_hash(None, &fn_name, num_params as usize); let hash_script = calc_fn_hash(None, &fn_name, num_params as usize);
self.has_script_fn(Some(global), caches, lib, hash_script) self.has_script_fn(global, caches, lib, hash_script)
} }
.into()); .into());
} }
@ -1244,7 +1244,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -1378,7 +1378,7 @@ impl Engine {
let new_scope = &mut Scope::new(); let new_scope = &mut Scope::new();
let orig_source = mem::replace(&mut global.source, module.id_raw().cloned()); let orig_source = mem::replace(&mut global.source, module.id_raw().cloned());
let global = &mut *RestoreOnDrop::new(global, move |g| g.source = orig_source); let global = &mut *RestoreOnDrop::lock(global, move |g| g.source = orig_source);
self.call_script_fn( self.call_script_fn(
global, caches, lib, level, new_scope, &mut None, fn_def, &mut args, true, pos, global, caches, lib, level, new_scope, &mut None, fn_def, &mut args, true, pos,
@ -1426,7 +1426,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
script: &str, script: &str,
@ -1471,7 +1471,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,

View File

@ -8,7 +8,7 @@ use crate::tokenizer::{is_valid_function_name, Token, TokenizeState};
use crate::types::dynamic::Variant; use crate::types::dynamic::Variant;
use crate::{ use crate::{
calc_fn_hash, Dynamic, Engine, EvalContext, FuncArgs, Module, Position, RhaiResult, calc_fn_hash, Dynamic, Engine, EvalContext, FuncArgs, Module, Position, RhaiResult,
RhaiResultOf, StaticVec, VarDefInfo, ERR, RhaiResultOf, SharedModule, StaticVec, VarDefInfo, ERR,
}; };
use std::any::type_name; use std::any::type_name;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
@ -74,7 +74,7 @@ pub struct NativeCallContext<'a> {
/// The current [`GlobalRuntimeState`], if any. /// The current [`GlobalRuntimeState`], if any.
global: Option<&'a GlobalRuntimeState>, global: Option<&'a GlobalRuntimeState>,
/// The current stack of loaded [modules][Module]. /// The current stack of loaded [modules][Module].
lib: &'a [Shared<Module>], lib: &'a [SharedModule],
/// [Position] of the function call. /// [Position] of the function call.
pos: Position, pos: Position,
/// The current nesting level of function calls. /// The current nesting level of function calls.
@ -93,7 +93,7 @@ pub struct NativeCallContextStore {
/// The current [`GlobalRuntimeState`], if any. /// The current [`GlobalRuntimeState`], if any.
pub global: GlobalRuntimeState, pub global: GlobalRuntimeState,
/// The current stack of loaded [modules][Module]. /// The current stack of loaded [modules][Module].
pub lib: StaticVec<Shared<Module>>, pub lib: StaticVec<SharedModule>,
/// [Position] of the function call. /// [Position] of the function call.
pub pos: Position, pub pos: Position,
/// The current nesting level of function calls. /// The current nesting level of function calls.
@ -116,7 +116,7 @@ impl<'a>
&'a str, &'a str,
Option<&'a str>, Option<&'a str>,
&'a GlobalRuntimeState, &'a GlobalRuntimeState,
&'a [Shared<Module>], &'a [SharedModule],
Position, Position,
usize, usize,
)> for NativeCallContext<'a> )> for NativeCallContext<'a>
@ -128,7 +128,7 @@ impl<'a>
&'a str, &'a str,
Option<&'a str>, Option<&'a str>,
&'a GlobalRuntimeState, &'a GlobalRuntimeState,
&'a [Shared<Module>], &'a [SharedModule],
Position, Position,
usize, usize,
), ),
@ -145,9 +145,9 @@ impl<'a>
} }
} }
impl<'a> From<(&'a Engine, &'a str, &'a [Shared<Module>])> for NativeCallContext<'a> { impl<'a> From<(&'a Engine, &'a str, &'a [SharedModule])> for NativeCallContext<'a> {
#[inline(always)] #[inline(always)]
fn from(value: (&'a Engine, &'a str, &'a [Shared<Module>])) -> Self { fn from(value: (&'a Engine, &'a str, &'a [SharedModule])) -> Self {
Self { Self {
engine: value.0, engine: value.0,
fn_name: value.1, fn_name: value.1,
@ -169,7 +169,7 @@ impl<'a> NativeCallContext<'a> {
)] )]
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn new(engine: &'a Engine, fn_name: &'a str, lib: &'a [Shared<Module>]) -> Self { pub fn new(engine: &'a Engine, fn_name: &'a str, lib: &'a [SharedModule]) -> Self {
Self { Self {
engine, engine,
fn_name, fn_name,
@ -193,7 +193,7 @@ impl<'a> NativeCallContext<'a> {
fn_name: &'a str, fn_name: &'a str,
source: Option<&'a str>, source: Option<&'a str>,
global: &'a GlobalRuntimeState, global: &'a GlobalRuntimeState,
lib: &'a [Shared<Module>], lib: &'a [SharedModule],
pos: Position, pos: Position,
level: usize, level: usize,
) -> Self { ) -> Self {
@ -291,7 +291,7 @@ impl<'a> NativeCallContext<'a> {
#[inline] #[inline]
pub(crate) fn iter_imports_raw( pub(crate) fn iter_imports_raw(
&self, &self,
) -> impl Iterator<Item = (&crate::ImmutableString, &Shared<Module>)> { ) -> impl Iterator<Item = (&crate::ImmutableString, &SharedModule)> {
self.global.iter().flat_map(|&g| g.iter_imports_raw()) self.global.iter().flat_map(|&g| g.iter_imports_raw())
} }
/// _(internals)_ The current [`GlobalRuntimeState`], if any. /// _(internals)_ The current [`GlobalRuntimeState`], if any.
@ -315,7 +315,7 @@ impl<'a> NativeCallContext<'a> {
#[cfg(feature = "internals")] #[cfg(feature = "internals")]
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub const fn namespaces(&self) -> &[Shared<Module>] { pub const fn namespaces(&self) -> &[SharedModule] {
self.lib self.lib
} }
/// Call a function inside the call context with the provided arguments. /// Call a function inside the call context with the provided arguments.

View File

@ -4,7 +4,7 @@
use super::call::FnCallArgs; use super::call::FnCallArgs;
use crate::ast::ScriptFnDef; use crate::ast::ScriptFnDef;
use crate::eval::{Caches, GlobalRuntimeState}; use crate::eval::{Caches, GlobalRuntimeState};
use crate::{Dynamic, Engine, Module, Position, RhaiError, RhaiResult, Scope, Shared, ERR}; use crate::{Dynamic, Engine, Position, RhaiError, RhaiResult, Scope, SharedModule, ERR};
use std::mem; use std::mem;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -26,7 +26,7 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
level: usize, level: usize,
scope: &mut Scope, scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
@ -228,9 +228,9 @@ impl Engine {
#[must_use] #[must_use]
pub(crate) fn has_script_fn( pub(crate) fn has_script_fn(
&self, &self,
_global: Option<&GlobalRuntimeState>, _global: &GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
lib: &[Shared<Module>], lib: &[SharedModule],
hash_script: u64, hash_script: u64,
) -> bool { ) -> bool {
let cache = caches.fn_resolution_cache_mut(); let cache = caches.fn_resolution_cache_mut();
@ -247,7 +247,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let result = result || let result = result ||
// Then check imported modules // Then check imported modules
_global.map_or(false, |m| m.contains_qualified_fn(hash_script)) _global.contains_qualified_fn(hash_script)
// Then check sub-modules // Then check sub-modules
|| self.global_sub_modules.values().any(|m| m.contains_qualified_fn(hash_script)); || self.global_sub_modules.values().any(|m| m.contains_qualified_fn(hash_script));

View File

@ -240,6 +240,9 @@ pub use func::Locked;
use func::{calc_fn_hash, calc_fn_hash_full, calc_var_hash}; use func::{calc_fn_hash, calc_fn_hash_full, calc_var_hash};
/// A shared [`Module`].
type SharedModule = Shared<Module>;
pub use rhai_codegen::*; pub use rhai_codegen::*;
pub use func::{plugin, FuncArgs}; pub use func::{plugin, FuncArgs};

View File

@ -10,7 +10,7 @@ use crate::func::{
use crate::types::{dynamic::Variant, BloomFilterU64, CustomTypesCollection}; use crate::types::{dynamic::Variant, BloomFilterU64, CustomTypesCollection};
use crate::{ use crate::{
calc_fn_hash, calc_fn_hash_full, Dynamic, Identifier, ImmutableString, NativeCallContext, calc_fn_hash, calc_fn_hash_full, Dynamic, Identifier, ImmutableString, NativeCallContext,
RhaiResultOf, Shared, SmartString, StaticVec, RhaiResultOf, Shared, SharedModule, SmartString, StaticVec,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -172,7 +172,7 @@ pub struct Module {
/// Custom types. /// Custom types.
custom_types: Option<CustomTypesCollection>, custom_types: Option<CustomTypesCollection>,
/// Sub-modules. /// Sub-modules.
modules: Option<BTreeMap<Identifier, Shared<Module>>>, modules: Option<BTreeMap<Identifier, SharedModule>>,
/// [`Module`] variables. /// [`Module`] variables.
variables: Option<BTreeMap<Identifier, Dynamic>>, variables: Option<BTreeMap<Identifier, Dynamic>>,
/// Flattened collection of all [`Module`] variables, including those in sub-modules. /// Flattened collection of all [`Module`] variables, including those in sub-modules.
@ -754,7 +754,7 @@ impl Module {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[inline] #[inline]
#[must_use] #[must_use]
pub(crate) fn get_sub_modules_mut(&mut self) -> &mut BTreeMap<Identifier, Shared<Module>> { pub(crate) fn get_sub_modules_mut(&mut self) -> &mut BTreeMap<Identifier, SharedModule> {
// We must assume that the user has changed the sub-modules // We must assume that the user has changed the sub-modules
// (otherwise why take a mutable reference?) // (otherwise why take a mutable reference?)
self.all_functions = None; self.all_functions = None;
@ -822,7 +822,7 @@ impl Module {
pub fn set_sub_module( pub fn set_sub_module(
&mut self, &mut self,
name: impl Into<Identifier>, name: impl Into<Identifier>,
sub_module: impl Into<Shared<Module>>, sub_module: impl Into<SharedModule>,
) -> &mut Self { ) -> &mut Self {
self.modules self.modules
.get_or_insert_with(|| Default::default()) .get_or_insert_with(|| Default::default())
@ -1830,7 +1830,7 @@ impl Module {
/// Get an iterator to the sub-modules in the [`Module`]. /// Get an iterator to the sub-modules in the [`Module`].
#[inline] #[inline]
pub fn iter_sub_modules(&self) -> impl Iterator<Item = (&str, &Shared<Module>)> { pub fn iter_sub_modules(&self) -> impl Iterator<Item = (&str, &SharedModule)> {
self.modules self.modules
.iter() .iter()
.flat_map(|m| m.iter().map(|(k, m)| (k.as_str(), m))) .flat_map(|m| m.iter().map(|(k, m)| (k.as_str(), m)))

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
Engine, Module, ModuleResolver, Position, RhaiResultOf, Shared, StaticVec, ERR, Engine, ModuleResolver, Position, RhaiResultOf, SharedModule, StaticVec, ERR,
STATIC_VEC_INLINE_SIZE, STATIC_VEC_INLINE_SIZE,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
@ -138,7 +138,7 @@ impl ModuleResolver for ModuleResolversCollection {
source_path: Option<&str>, source_path: Option<&str>,
path: &str, path: &str,
pos: Position, pos: Position,
) -> RhaiResultOf<Shared<Module>> { ) -> RhaiResultOf<SharedModule> {
for resolver in &self.0 { for resolver in &self.0 {
match resolver.resolve(engine, source_path, path, pos) { match resolver.resolve(engine, source_path, path, pos) {
Ok(module) => return Ok(module), Ok(module) => return Ok(module),

View File

@ -1,4 +1,4 @@
use crate::{Engine, Module, ModuleResolver, Position, RhaiResultOf, Shared, ERR}; use crate::{Engine, ModuleResolver, Position, RhaiResultOf, SharedModule, ERR};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -45,7 +45,7 @@ impl ModuleResolver for DummyModuleResolver {
_: Option<&str>, _: Option<&str>,
path: &str, path: &str,
pos: Position, pos: Position,
) -> RhaiResultOf<Shared<Module>> { ) -> RhaiResultOf<SharedModule> {
Err(ERR::ErrorModuleNotFound(path.into(), pos).into()) Err(ERR::ErrorModuleNotFound(path.into(), pos).into())
} }
} }

View File

@ -4,7 +4,8 @@
use crate::eval::GlobalRuntimeState; use crate::eval::GlobalRuntimeState;
use crate::func::{locked_read, locked_write}; use crate::func::{locked_read, locked_write};
use crate::{ use crate::{
Engine, Identifier, Locked, Module, ModuleResolver, Position, RhaiResultOf, Scope, Shared, ERR, Engine, Identifier, Locked, Module, ModuleResolver, Position, RhaiResultOf, Scope, Shared,
SharedModule, ERR,
}; };
use std::{ use std::{
@ -51,7 +52,7 @@ pub struct FileModuleResolver {
extension: Identifier, extension: Identifier,
cache_enabled: bool, cache_enabled: bool,
scope: Scope<'static>, scope: Scope<'static>,
cache: Locked<BTreeMap<PathBuf, Shared<Module>>>, cache: Locked<BTreeMap<PathBuf, SharedModule>>,
} }
impl Default for FileModuleResolver { impl Default for FileModuleResolver {
@ -258,7 +259,7 @@ impl FileModuleResolver {
/// The next time this path is resolved, the script file will be loaded once again. /// The next time this path is resolved, the script file will be loaded once again.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn clear_cache_for_path(&mut self, path: impl AsRef<Path>) -> Option<Shared<Module>> { pub fn clear_cache_for_path(&mut self, path: impl AsRef<Path>) -> Option<SharedModule> {
locked_write(&self.cache) locked_write(&self.cache)
.remove_entry(path.as_ref()) .remove_entry(path.as_ref())
.map(|(.., v)| v) .map(|(.., v)| v)
@ -293,7 +294,7 @@ impl FileModuleResolver {
source: Option<&str>, source: Option<&str>,
path: &str, path: &str,
pos: Position, pos: Position,
) -> Result<Shared<Module>, Box<crate::EvalAltResult>> { ) -> Result<SharedModule, Box<crate::EvalAltResult>> {
// Load relative paths from source if there is no base path specified // Load relative paths from source if there is no base path specified
let source_path = global let source_path = global
.as_ref() .as_ref()
@ -344,7 +345,7 @@ impl ModuleResolver for FileModuleResolver {
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
path: &str, path: &str,
pos: Position, pos: Position,
) -> RhaiResultOf<Shared<Module>> { ) -> RhaiResultOf<SharedModule> {
self.impl_resolve(engine, Some(global), None, path, pos) self.impl_resolve(engine, Some(global), None, path, pos)
} }
@ -355,7 +356,7 @@ impl ModuleResolver for FileModuleResolver {
source: Option<&str>, source: Option<&str>,
path: &str, path: &str,
pos: Position, pos: Position,
) -> RhaiResultOf<Shared<Module>> { ) -> RhaiResultOf<SharedModule> {
self.impl_resolve(engine, None, source, path, pos) self.impl_resolve(engine, None, source, path, pos)
} }

View File

@ -1,6 +1,6 @@
use crate::eval::GlobalRuntimeState; use crate::eval::GlobalRuntimeState;
use crate::func::SendSync; use crate::func::SendSync;
use crate::{Engine, Module, Position, RhaiResultOf, Shared, AST}; use crate::{Engine, Position, RhaiResultOf, SharedModule, AST};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -25,7 +25,7 @@ pub trait ModuleResolver: SendSync {
source: Option<&str>, source: Option<&str>,
path: &str, path: &str,
pos: Position, pos: Position,
) -> RhaiResultOf<Shared<Module>>; ) -> RhaiResultOf<SharedModule>;
/// Resolve a module based on a path string, given a [`GlobalRuntimeState`]. /// Resolve a module based on a path string, given a [`GlobalRuntimeState`].
/// ///
@ -38,7 +38,7 @@ pub trait ModuleResolver: SendSync {
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
path: &str, path: &str,
pos: Position, pos: Position,
) -> RhaiResultOf<Shared<Module>> { ) -> RhaiResultOf<SharedModule> {
self.resolve(engine, global.source(), path, pos) self.resolve(engine, global.source(), path, pos)
} }

View File

@ -1,5 +1,6 @@
use crate::{ use crate::{
Engine, Identifier, Module, ModuleResolver, Position, RhaiResultOf, Shared, SmartString, ERR, Engine, Identifier, Module, ModuleResolver, Position, RhaiResultOf, SharedModule, SmartString,
ERR,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -27,7 +28,7 @@ use std::{
/// engine.set_module_resolver(resolver); /// engine.set_module_resolver(resolver);
/// ``` /// ```
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct StaticModuleResolver(BTreeMap<Identifier, Shared<Module>>); pub struct StaticModuleResolver(BTreeMap<Identifier, SharedModule>);
impl StaticModuleResolver { impl StaticModuleResolver {
/// Create a new [`StaticModuleResolver`]. /// Create a new [`StaticModuleResolver`].
@ -65,7 +66,7 @@ impl StaticModuleResolver {
} }
/// Remove a [module][Module] given its path. /// Remove a [module][Module] given its path.
#[inline(always)] #[inline(always)]
pub fn remove(&mut self, path: &str) -> Option<Shared<Module>> { pub fn remove(&mut self, path: &str) -> Option<SharedModule> {
self.0.remove(path) self.0.remove(path)
} }
/// Does the path exist? /// Does the path exist?
@ -80,12 +81,12 @@ impl StaticModuleResolver {
} }
/// Get an iterator of all the [modules][Module]. /// Get an iterator of all the [modules][Module].
#[inline] #[inline]
pub fn iter(&self) -> impl Iterator<Item = (&str, &Shared<Module>)> { pub fn iter(&self) -> impl Iterator<Item = (&str, &SharedModule)> {
self.0.iter().map(|(k, v)| (k.as_str(), v)) self.0.iter().map(|(k, v)| (k.as_str(), v))
} }
/// Get a mutable iterator of all the [modules][Module]. /// Get a mutable iterator of all the [modules][Module].
#[inline] #[inline]
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut Shared<Module>)> { pub fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut SharedModule)> {
self.0.iter_mut().map(|(k, v)| (k.as_str(), v)) self.0.iter_mut().map(|(k, v)| (k.as_str(), v))
} }
/// Get an iterator of all the [module][Module] paths. /// Get an iterator of all the [module][Module] paths.
@ -95,7 +96,7 @@ impl StaticModuleResolver {
} }
/// Get an iterator of all the [modules][Module]. /// Get an iterator of all the [modules][Module].
#[inline(always)] #[inline(always)]
pub fn values(&self) -> impl Iterator<Item = &Shared<Module>> { pub fn values(&self) -> impl Iterator<Item = &SharedModule> {
self.0.values() self.0.values()
} }
/// Remove all [modules][Module]. /// Remove all [modules][Module].
@ -130,8 +131,8 @@ impl StaticModuleResolver {
} }
impl IntoIterator for StaticModuleResolver { impl IntoIterator for StaticModuleResolver {
type Item = (Identifier, Shared<Module>); type Item = (Identifier, SharedModule);
type IntoIter = IntoIter<SmartString, Shared<Module>>; type IntoIter = IntoIter<SmartString, SharedModule>;
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
@ -141,8 +142,8 @@ impl IntoIterator for StaticModuleResolver {
} }
impl<'a> IntoIterator for &'a StaticModuleResolver { impl<'a> IntoIterator for &'a StaticModuleResolver {
type Item = (&'a Identifier, &'a Shared<Module>); type Item = (&'a Identifier, &'a SharedModule);
type IntoIter = Iter<'a, SmartString, Shared<Module>>; type IntoIter = Iter<'a, SmartString, SharedModule>;
#[inline(always)] #[inline(always)]
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
@ -158,7 +159,7 @@ impl ModuleResolver for StaticModuleResolver {
_: Option<&str>, _: Option<&str>,
path: &str, path: &str,
pos: Position, pos: Position,
) -> RhaiResultOf<Shared<Module>> { ) -> RhaiResultOf<SharedModule> {
self.0 self.0
.get(path) .get(path)
.cloned() .cloned()

View File

@ -61,7 +61,7 @@ struct OptimizerState<'a> {
caches: Caches, caches: Caches,
/// [Module][crate::Module] containing script-defined functions. /// [Module][crate::Module] containing script-defined functions.
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
lib: &'a [crate::Shared<crate::Module>], lib: &'a [crate::SharedModule],
/// Optimization level. /// Optimization level.
optimization_level: OptimizationLevel, optimization_level: OptimizationLevel,
} }
@ -71,7 +71,7 @@ impl<'a> OptimizerState<'a> {
#[inline(always)] #[inline(always)]
pub fn new( pub fn new(
engine: &'a Engine, engine: &'a Engine,
#[cfg(not(feature = "no_function"))] lib: &'a [crate::Shared<crate::Module>], #[cfg(not(feature = "no_function"))] lib: &'a [crate::SharedModule],
optimization_level: OptimizationLevel, optimization_level: OptimizationLevel,
) -> Self { ) -> Self {
Self { Self {
@ -1263,7 +1263,7 @@ fn optimize_top_level(
statements: StmtBlockContainer, statements: StmtBlockContainer,
engine: &Engine, engine: &Engine,
scope: &Scope, scope: &Scope,
#[cfg(not(feature = "no_function"))] lib: &[crate::Shared<crate::Module>], #[cfg(not(feature = "no_function"))] lib: &[crate::SharedModule],
optimization_level: OptimizationLevel, optimization_level: OptimizationLevel,
) -> StmtBlockContainer { ) -> StmtBlockContainer {
let mut statements = statements; let mut statements = statements;

View File

@ -1,6 +1,6 @@
//! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages. //! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages.
use crate::{Engine, Module, Shared}; use crate::{Engine, Module, SharedModule};
pub(crate) mod arithmetic; pub(crate) mod arithmetic;
pub(crate) mod array_basic; pub(crate) mod array_basic;
@ -99,7 +99,7 @@ pub trait Package {
/// Get a reference to a shared module from this package. /// Get a reference to a shared module from this package.
#[must_use] #[must_use]
fn as_shared_module(&self) -> Shared<Module>; fn as_shared_module(&self) -> SharedModule;
} }
/// Macro that makes it easy to define a _package_ (which is basically a shared [module][Module]) /// Macro that makes it easy to define a _package_ (which is basically a shared [module][Module])

View File

@ -8,7 +8,7 @@ use crate::ast::{
SwitchCasesCollection, TryCatchBlock, SwitchCasesCollection, TryCatchBlock,
}; };
use crate::engine::{Precedence, KEYWORD_THIS, OP_CONTAINS}; use crate::engine::{Precedence, KEYWORD_THIS, OP_CONTAINS};
use crate::eval::GlobalRuntimeState; use crate::eval::{Caches, GlobalRuntimeState};
use crate::func::{hashing::get_hasher, StraightHashMap}; use crate::func::{hashing::get_hasher, StraightHashMap};
use crate::tokenizer::{ use crate::tokenizer::{
is_keyword_function, is_valid_function_name, is_valid_identifier, Token, TokenStream, is_keyword_function, is_valid_function_name, is_valid_identifier, Token, TokenStream,
@ -2906,15 +2906,17 @@ impl Engine {
nesting_level: level, nesting_level: level,
will_shadow, will_shadow,
}; };
let mut this_ptr = None; let caches = &mut Caches::new();
let this_ptr = &mut None;
let context = EvalContext::new( let context = EvalContext::new(
self, self,
&mut state.global, &mut state.global,
None, caches,
&[], &[],
level, level,
&mut state.stack, &mut state.stack,
&mut this_ptr, this_ptr,
); );
match filter(false, info, context) { match filter(false, info, context) {

View File

@ -4,7 +4,7 @@ use crate::tokenizer::is_valid_function_name;
use crate::types::dynamic::Variant; use crate::types::dynamic::Variant;
use crate::{ use crate::{
Dynamic, Engine, FuncArgs, ImmutableString, NativeCallContext, Position, RhaiError, RhaiResult, Dynamic, Engine, FuncArgs, ImmutableString, NativeCallContext, Position, RhaiError, RhaiResult,
RhaiResultOf, StaticVec, AST, ERR, RhaiResultOf, SharedModule, StaticVec, AST, ERR,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -150,9 +150,9 @@ impl FnPtr {
let mut arg_values = crate::StaticVec::new_const(); let mut arg_values = crate::StaticVec::new_const();
args.parse(&mut arg_values); args.parse(&mut arg_values);
let lib: &[crate::Shared<crate::Module>] = &[ let lib: &[SharedModule] = &[
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
AsRef::<crate::Shared<crate::Module>>::as_ref(ast).clone(), AsRef::<SharedModule>::as_ref(ast).clone(),
]; ];
let lib = if lib.first().map_or(true, |m| m.is_empty()) { let lib = if lib.first().map_or(true, |m| m.is_empty()) {
&[][..] &[][..]

View File

@ -12,18 +12,26 @@ pub struct RestoreOnDrop<'a, T, R: FnOnce(&mut T)> {
} }
impl<'a, T, R: FnOnce(&mut T)> RestoreOnDrop<'a, T, R> { impl<'a, T, R: FnOnce(&mut T)> RestoreOnDrop<'a, T, R> {
/// Create a new [`RestoreOnDrop`] that runs restoration logic at the end of scope only when /// Create a new [`RestoreOnDrop`] that locks a mutable reference and runs restoration logic at
/// `need_restore` is `true`. /// the end of scope only when `need_restore` is `true`.
///
/// Beware that the end of scope means the end of its lifetime, not necessarily waiting until
/// the current block scope is exited.
#[inline(always)] #[inline(always)]
pub fn new_if(need_restore: bool, value: &'a mut T, restore: R) -> Self { pub fn lock_if(need_restore: bool, value: &'a mut T, restore: R) -> Self {
Self { Self {
value, value,
restore: if need_restore { Some(restore) } else { None }, restore: if need_restore { Some(restore) } else { None },
} }
} }
/// Create a new [`RestoreOnDrop`] that runs restoration logic at the end of scope.
/// Create a new [`RestoreOnDrop`] that locks a mutable reference and runs restoration logic at
/// the end of scope.
///
/// Beware that the end of scope means the end of its lifetime, not necessarily waiting until
/// the current block scope is exited.
#[inline(always)] #[inline(always)]
pub fn new(value: &'a mut T, restore: R) -> Self { pub fn lock(value: &'a mut T, restore: R) -> Self {
Self { Self {
value, value,
restore: Some(restore), restore: Some(restore),