Change lib to &[Shared<Module>] and remove dummy lifetimes.

This commit is contained in:
Stephen Chung 2022-11-07 16:18:59 +08:00
parent 4455d95abc
commit 0c79471fd3
16 changed files with 93 additions and 94 deletions

View File

@ -4,8 +4,8 @@
use crate::eval::{Caches, GlobalRuntimeState};
use crate::types::dynamic::Variant;
use crate::{
reify, Dynamic, Engine, FuncArgs, Position, RhaiResult, RhaiResultOf, Scope, StaticVec, AST,
ERR,
reify, Dynamic, Engine, FuncArgs, Position, RhaiResult, RhaiResultOf, Scope, Shared, StaticVec,
AST, ERR,
};
use std::any::{type_name, TypeId};
#[cfg(feature = "no_std")]
@ -248,7 +248,7 @@ impl Engine {
arg_values: &mut [Dynamic],
) -> RhaiResult {
let statements = ast.statements();
let lib = &[ast.as_ref()];
let lib = &[AsRef::<Shared<_>>::as_ref(ast).clone()];
let mut this_ptr = this_ptr;
let orig_scope_len = scope.len();

View File

@ -195,7 +195,7 @@ impl Engine {
global.debugger.status = crate::eval::DebuggerStatus::Terminate;
let lib = &[
#[cfg(not(feature = "no_function"))]
ast.as_ref(),
AsRef::<crate::Shared<_>>::as_ref(ast).clone(),
];
let node = &crate::ast::Stmt::Noop(Position::NONE);
self.run_debugger(global, caches, lib, 0, scope, &mut None, node)?;
@ -234,7 +234,7 @@ impl Engine {
let mut _lib = &[
#[cfg(not(feature = "no_function"))]
ast.as_ref(),
AsRef::<crate::Shared<_>>::as_ref(ast).clone(),
][..];
#[cfg(not(feature = "no_function"))]
if !ast.has_functions() {
@ -264,7 +264,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&crate::Module],
lib: &[crate::Shared<crate::Module>],
level: usize,
scope: &mut Scope,
statements: &[crate::ast::Stmt],

View File

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

View File

@ -3,7 +3,6 @@
use crate::func::{CallableFunction, StraightHashMap};
use crate::types::BloomFilterU64;
use crate::{ImmutableString, StaticVec};
use std::marker::PhantomData;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -45,21 +44,18 @@ impl FnResolutionCache {
/// The following caches are contained inside this type:
/// * A stack of [function resolution caches][FnResolutionCache]
#[derive(Debug, Clone)]
pub struct Caches<'a> {
pub struct Caches {
/// Stack of [function resolution caches][FnResolutionCache].
stack: StaticVec<FnResolutionCache>,
/// Take care of the lifetime parameter.
dummy: PhantomData<&'a ()>,
}
impl Caches<'_> {
impl Caches {
/// Create an empty [`Caches`].
#[inline(always)]
#[must_use]
pub const fn new() -> Self {
Self {
stack: StaticVec::new_const(),
dummy: PhantomData,
}
}
/// Get the number of function resolution cache(s) in the stack.

View File

@ -4,7 +4,9 @@
use super::{Caches, GlobalRuntimeState, Target};
use crate::ast::{ASTFlags, Expr, OpAssignment};
use crate::types::dynamic::Union;
use crate::{Dynamic, Engine, FnArgsVec, Module, Position, RhaiResult, RhaiResultOf, Scope, ERR};
use crate::{
Dynamic, Engine, FnArgsVec, Module, Position, RhaiResult, RhaiResultOf, Scope, Shared, ERR,
};
use std::hash::Hash;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -40,7 +42,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
this_ptr: &mut Option<&mut Dynamic>,
target: &mut Target,
@ -555,7 +557,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>,
@ -646,7 +648,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>,
@ -758,7 +760,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
target: &mut Dynamic,
idx: &mut Dynamic,
@ -781,7 +783,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
target: &mut Dynamic,
idx: &mut Dynamic,
@ -805,7 +807,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
target: &'t mut Dynamic,
idx: &mut Dynamic,

View File

@ -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,
Dynamic, Engine, EvalAltResult, ImmutableString, Module, Position, RhaiResultOf, Scope, Shared,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -413,7 +413,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
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: &[&Module],
lib: &[Shared<Module>],
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: &[&Module],
lib: &[Shared<Module>],
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: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>,

View File

@ -1,39 +1,39 @@
//! Evaluation context.
use super::{Caches, GlobalRuntimeState};
use crate::{Dynamic, Engine, Module, Scope};
use crate::{Dynamic, Engine, Module, Scope, Shared};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
/// Context of a script evaluation process.
#[derive(Debug)]
#[allow(dead_code)]
pub struct EvalContext<'a, 's, 'ps, 'g, 'pg, 'c, 'pc, 't, 'pt> {
pub struct EvalContext<'a, 's, 'ps, 'g, 'c, 't, 'pt> {
/// The current [`Engine`].
engine: &'a Engine,
/// The current [`Scope`].
scope: &'s mut Scope<'ps>,
/// The current [`GlobalRuntimeState`].
global: &'g mut GlobalRuntimeState<'pg>,
global: &'g mut GlobalRuntimeState,
/// The current [caches][Caches], if available.
caches: Option<&'c mut Caches<'pc>>,
caches: Option<&'c mut Caches>,
/// The current stack of imported [modules][Module].
lib: &'a [&'a Module],
lib: &'a [Shared<Module>],
/// The current bound `this` pointer, if any.
this_ptr: &'t mut Option<&'pt mut Dynamic>,
/// The current nesting level of function calls.
level: usize,
}
impl<'a, 's, 'ps, 'g, 'pg, 'c, 'pc, 't, 'pt> EvalContext<'a, 's, 'ps, 'g, 'pg, 'c, 'pc, 't, 'pt> {
impl<'a, 's, 'ps, 'g, 'c, 't, 'pt> EvalContext<'a, 's, 'ps, 'g, 'c, 't, 'pt> {
/// Create a new [`EvalContext`].
#[inline(always)]
#[must_use]
pub fn new(
engine: &'a Engine,
global: &'g mut GlobalRuntimeState<'pg>,
caches: Option<&'c mut Caches<'pc>>,
lib: &'a [&'a Module],
global: &'g mut GlobalRuntimeState,
caches: Option<&'c mut Caches>,
lib: &'a [Shared<Module>],
level: usize,
scope: &'s mut Scope<'ps>,
this_ptr: &'t mut Option<&'pt mut Dynamic>,
@ -104,20 +104,20 @@ impl<'a, 's, 'ps, 'g, 'pg, 'c, 'pc, 't, 'pt> EvalContext<'a, 's, 'ps, 'g, 'pg, '
#[cfg(feature = "internals")]
#[inline(always)]
#[must_use]
pub fn global_runtime_state_mut(&mut self) -> &mut &'g mut GlobalRuntimeState<'pg> {
pub fn global_runtime_state_mut(&mut self) -> &mut &'g mut GlobalRuntimeState {
&mut self.global
}
/// Get an iterator over the namespaces containing definition of all script-defined functions.
#[inline]
pub fn iter_namespaces(&self) -> impl Iterator<Item = &Module> {
self.lib.iter().copied()
self.lib.iter().map(|m| m.as_ref())
}
/// _(internals)_ The current set of namespaces containing definitions of all script-defined functions.
/// Exported under the `internals` feature only.
#[cfg(feature = "internals")]
#[inline(always)]
#[must_use]
pub const fn namespaces(&self) -> &[&Module] {
pub const fn namespaces(&self) -> &[Shared<Module>] {
self.lib
}
/// The current bound `this` pointer, if any.

View File

@ -4,7 +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::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, ERR};
use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, Shared, ERR};
use std::num::NonZeroUsize;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -18,7 +18,7 @@ impl Engine {
&self,
global: &GlobalRuntimeState,
namespace: &crate::ast::Namespace,
) -> Option<crate::Shared<Module>> {
) -> Option<Shared<Module>> {
assert!(!namespace.is_empty());
let root = namespace.root();
@ -51,7 +51,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &'s mut Scope,
this_ptr: &'s mut Option<&mut Dynamic>,
@ -137,7 +137,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &'s mut Scope,
this_ptr: &'s mut Option<&mut Dynamic>,
@ -160,7 +160,7 @@ impl Engine {
Expr::Variable(v, None, pos)
if lib
.iter()
.flat_map(|&m| m.iter_script_fn())
.flat_map(|m| m.iter_script_fn())
.any(|(_, _, f, ..)| f == v.3.as_str()) =>
{
let val: Dynamic =
@ -221,7 +221,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>,

View File

@ -1,9 +1,9 @@
//! Global runtime state.
use crate::{Dynamic, Engine, ImmutableString};
use std::fmt;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{fmt, marker::PhantomData};
/// Collection of globally-defined constants.
#[cfg(not(feature = "no_module"))]
@ -22,7 +22,7 @@ pub type GlobalConstants =
// Most usage will be looking up a particular key from the list and then getting the module that
// corresponds to that key.
#[derive(Clone)]
pub struct GlobalRuntimeState<'a> {
pub struct GlobalRuntimeState {
/// Names of imported [modules][crate::Module].
#[cfg(not(feature = "no_module"))]
imports: crate::StaticVec<ImmutableString>,
@ -70,11 +70,9 @@ pub struct GlobalRuntimeState<'a> {
/// Debugging interface.
#[cfg(feature = "debugging")]
pub debugger: super::Debugger,
/// Take care of the lifetime parameter.
dummy: PhantomData<&'a ()>,
}
impl GlobalRuntimeState<'_> {
impl GlobalRuntimeState {
/// Create a new [`GlobalRuntimeState`] based on an [`Engine`].
#[inline(always)]
#[must_use]
@ -112,8 +110,6 @@ impl GlobalRuntimeState<'_> {
None => Dynamic::UNIT,
},
),
dummy: PhantomData::default(),
}
}
/// Get the length of the stack of globally-imported [modules][crate::Module].
@ -319,7 +315,7 @@ impl GlobalRuntimeState<'_> {
}
#[cfg(not(feature = "no_module"))]
impl IntoIterator for GlobalRuntimeState<'_> {
impl IntoIterator for GlobalRuntimeState {
type Item = (ImmutableString, crate::Shared<crate::Module>);
type IntoIter = std::iter::Rev<
std::iter::Zip<
@ -334,7 +330,7 @@ impl IntoIterator for GlobalRuntimeState<'_> {
}
#[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 IntoIter = std::iter::Rev<
std::iter::Zip<
@ -350,7 +346,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<'_>
for GlobalRuntimeState
{
#[inline]
fn extend<T: IntoIterator<Item = (K, M)>>(&mut self, iter: T) {
@ -361,7 +357,7 @@ impl<K: Into<ImmutableString>, M: Into<crate::Shared<crate::Module>>> Extend<(K,
}
}
impl fmt::Debug for GlobalRuntimeState<'_> {
impl fmt::Debug for GlobalRuntimeState {
#[cold]
#[inline(never)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

View File

@ -8,7 +8,8 @@ use crate::ast::{
use crate::func::{get_builtin_op_assignment_fn, get_hasher};
use crate::types::dynamic::{AccessMode, Union};
use crate::{
Dynamic, Engine, ImmutableString, Module, Position, RhaiResult, RhaiResultOf, Scope, ERR, INT,
Dynamic, Engine, ImmutableString, Module, Position, RhaiResult, RhaiResultOf, Scope, Shared,
ERR, INT,
};
use std::hash::{Hash, Hasher};
#[cfg(feature = "no_std")]
@ -27,7 +28,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>,
@ -112,7 +113,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
op_info: &OpAssignment,
target: &mut Target,
@ -196,7 +197,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>,
@ -1018,7 +1019,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
statements: &[Stmt],

View File

@ -11,7 +11,7 @@ use crate::eval::{Caches, FnResolutionCacheEntry, GlobalRuntimeState};
use crate::tokenizer::{is_valid_function_name, Token};
use crate::{
calc_fn_hash, calc_fn_hash_full, Dynamic, Engine, FnArgsVec, FnPtr, ImmutableString, Module,
OptimizationLevel, Position, RhaiError, RhaiResult, RhaiResultOf, Scope, ERR,
OptimizationLevel, Position, RhaiError, RhaiResult, RhaiResultOf, Scope, Shared, ERR,
};
#[cfg(feature = "no_std")]
use hashbrown::hash_map::Entry;
@ -166,7 +166,7 @@ impl Engine {
_global: &GlobalRuntimeState,
caches: &'s mut Caches,
local_entry: &'s mut Option<FnResolutionCacheEntry>,
lib: &[&Module],
lib: &[Shared<Module>],
op_token: Option<&Token>,
hash_base: u64,
args: Option<&mut FnCallArgs>,
@ -193,8 +193,7 @@ impl Engine {
loop {
let func = lib
.iter()
.copied()
.chain(self.global_modules.iter().map(|m| m.as_ref()))
.chain(self.global_modules.iter())
.find_map(|m| m.get_fn(hash).map(|f| (f, m.id_raw())));
#[cfg(not(feature = "no_module"))]
@ -323,7 +322,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
name: &str,
op_token: Option<&Token>,
@ -538,7 +537,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
_scope: Option<&mut Scope>,
fn_name: &str,
@ -705,7 +704,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>,
@ -742,7 +741,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
fn_name: &str,
mut hash: FnCallHashes,
@ -967,7 +966,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>,
@ -1258,7 +1257,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>,
@ -1442,7 +1441,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
script: &str,
@ -1487,7 +1486,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>,

View File

@ -22,6 +22,8 @@ pub use callable_function::CallableFunction;
#[cfg(not(feature = "no_function"))]
pub use func::Func;
pub use hashing::{calc_fn_hash, calc_fn_hash_full, calc_var_hash, get_hasher, StraightHashMap};
#[cfg(feature = "internals")]
pub use native::NativeCallContextStore;
pub use native::{
locked_read, locked_write, shared_get_mut, shared_make_mut, shared_take, shared_take_or_clone,
shared_try_take, FnAny, FnPlugin, IteratorFn, Locked, NativeCallContext, SendSync, Shared,

View File

@ -4,7 +4,7 @@
use super::call::FnCallArgs;
use crate::ast::ScriptFnDef;
use crate::eval::{Caches, GlobalRuntimeState};
use crate::{Dynamic, Engine, Module, Position, RhaiError, RhaiResult, Scope, ERR};
use crate::{Dynamic, Engine, Module, Position, RhaiError, RhaiResult, Scope, Shared, ERR};
use std::mem;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -26,7 +26,7 @@ impl Engine {
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
level: usize,
scope: &mut Scope,
this_ptr: &mut Option<&mut Dynamic>,
@ -127,8 +127,8 @@ impl Engine {
lib
} else {
caches.push_fn_resolution_cache();
lib_merged.push(&**fn_lib);
lib_merged.extend(lib.iter().copied());
lib_merged.push(fn_lib.clone());
lib_merged.extend(lib.iter().cloned());
&lib_merged
},
Some(mem::replace(&mut global.constants, constants.clone())),
@ -230,7 +230,7 @@ impl Engine {
&self,
_global: Option<&GlobalRuntimeState>,
caches: &mut Caches,
lib: &[&Module],
lib: &[Shared<Module>],
hash_script: u64,
) -> bool {
let cache = caches.fn_resolution_cache_mut();

View File

@ -56,12 +56,12 @@ struct OptimizerState<'a> {
/// An [`Engine`] instance for eager function evaluation.
engine: &'a Engine,
/// The global runtime state.
global: GlobalRuntimeState<'a>,
global: GlobalRuntimeState,
/// Function resolution caches.
caches: Caches<'a>,
caches: Caches,
/// [Module][crate::Module] containing script-defined functions.
#[cfg(not(feature = "no_function"))]
lib: &'a [&'a crate::Module],
lib: &'a [crate::Shared<crate::Module>],
/// Optimization level.
optimization_level: OptimizationLevel,
}
@ -71,7 +71,7 @@ impl<'a> OptimizerState<'a> {
#[inline(always)]
pub fn new(
engine: &'a Engine,
#[cfg(not(feature = "no_function"))] lib: &'a [&'a crate::Module],
#[cfg(not(feature = "no_function"))] lib: &'a [crate::Shared<crate::Module>],
optimization_level: OptimizationLevel,
) -> Self {
Self {
@ -1189,7 +1189,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
=> {
// First search for script-defined functions (can override built-in)
#[cfg(not(feature = "no_function"))]
let has_script_fn = !x.hashes.is_native_only() && state.lib.iter().find_map(|&m| m.get_script_fn(&x.name, x.args.len())).is_some();
let has_script_fn = !x.hashes.is_native_only() && state.lib.iter().find_map(|m| m.get_script_fn(&x.name, x.args.len())).is_some();
#[cfg(feature = "no_function")]
let has_script_fn = false;
@ -1263,7 +1263,7 @@ fn optimize_top_level(
statements: StmtBlockContainer,
engine: &Engine,
scope: &Scope,
#[cfg(not(feature = "no_function"))] lib: &[&crate::Module],
#[cfg(not(feature = "no_function"))] lib: &[crate::Shared<crate::Module>],
optimization_level: OptimizationLevel,
) -> StmtBlockContainer {
let mut statements = statements;
@ -1317,7 +1317,7 @@ pub fn optimize_into_ast(
let mut statements = statements;
#[cfg(not(feature = "no_function"))]
let lib = {
let lib: crate::Shared<_> = {
let mut module = crate::Module::new();
if optimization_level != OptimizationLevel::None {
@ -1338,7 +1338,7 @@ pub fn optimize_into_ast(
});
}
let lib2 = &[&lib2];
let lib2 = &[lib2.into()];
for fn_def in functions {
let mut fn_def = crate::func::shared_take_or_clone(fn_def);
@ -1356,7 +1356,7 @@ pub fn optimize_into_ast(
}
}
module
module.into()
};
statements.shrink_to_fit();
@ -1369,7 +1369,7 @@ pub fn optimize_into_ast(
engine,
scope,
#[cfg(not(feature = "no_function"))]
&[&lib],
&[lib.clone()],
optimization_level,
),
},

View File

@ -55,7 +55,7 @@ pub struct ParseState<'e> {
/// External [scope][Scope] with constants.
pub scope: &'e Scope<'e>,
/// Global runtime state.
pub global: GlobalRuntimeState<'e>,
pub global: GlobalRuntimeState,
/// Encapsulates a local stack with variable names to simulate an actual runtime scope.
pub stack: Scope<'e>,
/// Size of the local variables stack upon entry of the current block scope.

View File

@ -3,8 +3,8 @@
use crate::tokenizer::is_valid_function_name;
use crate::types::dynamic::Variant;
use crate::{
Dynamic, Engine, FuncArgs, ImmutableString, Module, NativeCallContext, Position, RhaiError,
RhaiResult, RhaiResultOf, StaticVec, AST, ERR,
Dynamic, Engine, FuncArgs, ImmutableString, NativeCallContext, Position, RhaiError, RhaiResult,
RhaiResultOf, StaticVec, AST, ERR,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -152,13 +152,16 @@ impl FnPtr {
let lib = [
#[cfg(not(feature = "no_function"))]
_ast.as_ref(),
AsRef::<crate::Shared<_>>::as_ref(ast).clone(),
];
let lib = if lib.first().map_or(true, |m: &&Module| m.is_empty()) {
let lib = if lib.first().map_or(true, |m| m.is_empty()) {
&lib[0..0]
} else {
&lib
};
#[cfg(feature = "no_function")]
let lib = &[];
#[allow(deprecated)]
let ctx = NativeCallContext::new(engine, self.fn_name(), lib);