diff --git a/benches/eval_module.rs b/benches/eval_module.rs index de7843c0..33941818 100644 --- a/benches/eval_module.rs +++ b/benches/eval_module.rs @@ -18,7 +18,7 @@ fn bench_eval_module(bench: &mut Bencher) { let ast = engine.compile(script).unwrap(); - let module = Module::eval_ast_as_new(Default::default(), &ast, &engine).unwrap(); + let module = Module::eval_ast_as_new(&mut Scope::new(), &ast, &engine).unwrap(); engine.register_static_module("testing", module.into()); diff --git a/src/api/custom_syntax.rs b/src/api/custom_syntax.rs index 3bb29821..c35c49af 100644 --- a/src/api/custom_syntax.rs +++ b/src/api/custom_syntax.rs @@ -151,7 +151,7 @@ impl Deref for Expression<'_> { } } -impl EvalContext<'_, '_, '_, '_, '_, '_, '_, '_> { +impl EvalContext<'_, '_, '_, '_, '_, '_, '_, '_, '_, '_> { /// Evaluate an [expression tree][Expression]. /// /// # WARNING - Low Level API diff --git a/src/eval/eval_context.rs b/src/eval/eval_context.rs index 4629e185..7b0aa5dd 100644 --- a/src/eval/eval_context.rs +++ b/src/eval/eval_context.rs @@ -7,15 +7,15 @@ use std::prelude::v1::*; /// Context of a script evaluation process. #[derive(Debug)] -pub struct EvalContext<'a, 'x, 'px, 'm, 's, 'b, 't, 'pt> { +pub struct EvalContext<'a, 'x, 'px, 'm, 'pm, 's, 'ps, 'b, 't, 'pt> { /// The current [`Engine`]. pub(crate) engine: &'a Engine, /// The current [`Scope`]. pub(crate) scope: &'x mut Scope<'px>, /// The current [`GlobalRuntimeState`]. - pub(crate) global: &'m mut GlobalRuntimeState, + pub(crate) global: &'m mut GlobalRuntimeState<'pm>, /// The current [evaluation state][EvalState]. - pub(crate) state: &'s mut EvalState, + pub(crate) state: &'s mut EvalState<'ps>, /// The current stack of imported [modules][Module]. pub(crate) lib: &'b [&'b Module], /// The current bound `this` pointer, if any. @@ -24,7 +24,7 @@ pub struct EvalContext<'a, 'x, 'px, 'm, 's, 'b, 't, 'pt> { pub(crate) level: usize, } -impl<'x, 'px, 'pt> EvalContext<'_, 'x, 'px, '_, '_, '_, '_, 'pt> { +impl<'x, 'px, 'pt> EvalContext<'_, 'x, 'px, '_, '_, '_, '_, '_, '_, 'pt> { /// The current [`Engine`]. #[inline(always)] #[must_use] diff --git a/src/eval/eval_state.rs b/src/eval/eval_state.rs index 3d15fb34..d0e9b2bc 100644 --- a/src/eval/eval_state.rs +++ b/src/eval/eval_state.rs @@ -3,13 +3,14 @@ use crate::func::call::FnResolutionCache; use crate::StaticVec; use std::collections::BTreeMap; +use std::marker::PhantomData; #[cfg(feature = "no_std")] use std::prelude::v1::*; /// _(internals)_ A type that holds all the current states of the [`Engine`]. /// Exported under the `internals` feature only. #[derive(Debug, Clone)] -pub struct EvalState { +pub struct EvalState<'a> { /// Force a [`Scope`] search by name. /// /// Normally, access to variables are parsed with a relative offset into the [`Scope`] to avoid a lookup. @@ -25,17 +26,20 @@ pub struct EvalState { pub scope_level: usize, /// Stack of function resolution caches. fn_resolution_caches: StaticVec, + /// Take care of the lifetime parameter + dummy: PhantomData>, } -impl EvalState { +impl EvalState<'_> { /// Create a new [`EvalState`]. #[inline(always)] #[must_use] - pub const fn new() -> Self { + pub fn new() -> Self { Self { always_search_scope: false, scope_level: 0, fn_resolution_caches: StaticVec::new_const(), + dummy: PhantomData::default(), } } /// Get the number of function resolution cache(s) in the stack. diff --git a/src/eval/global_state.rs b/src/eval/global_state.rs index 0cf54632..b2dc0181 100644 --- a/src/eval/global_state.rs +++ b/src/eval/global_state.rs @@ -8,6 +8,7 @@ use std::{ any::TypeId, fmt, iter::{FromIterator, Rev, Zip}, + marker::PhantomData, }; /// _(internals)_ A stack of imported [modules][Module] plus mutable global runtime states. @@ -19,7 +20,7 @@ use std::{ // 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 { +pub struct GlobalRuntimeState<'a> { /// Stack of module names. // // We cannot use Cow here because `eval` may load a [module][Module] and @@ -45,20 +46,22 @@ pub struct GlobalRuntimeState { #[cfg(not(feature = "no_function"))] constants: Option>>>, + /// Take care of the lifetime parameter. + dummy: PhantomData<&'a ()>, } -impl Default for GlobalRuntimeState { +impl Default for GlobalRuntimeState<'_> { #[inline(always)] fn default() -> Self { Self::new() } } -impl GlobalRuntimeState { +impl GlobalRuntimeState<'_> { /// Create a new [`GlobalRuntimeState`]. #[inline(always)] #[must_use] - pub const fn new() -> Self { + pub fn new() -> Self { Self { keys: StaticVec::new_const(), modules: StaticVec::new_const(), @@ -72,6 +75,7 @@ impl GlobalRuntimeState { #[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_function"))] constants: None, + dummy: PhantomData::default(), } } /// Get the length of the stack of globally-imported [modules][Module]. @@ -230,7 +234,7 @@ impl GlobalRuntimeState { } } -impl IntoIterator for GlobalRuntimeState { +impl IntoIterator for GlobalRuntimeState<'_> { type Item = (Identifier, Shared); type IntoIter = Zip>, Rev; 3]>>>; @@ -244,7 +248,7 @@ impl IntoIterator for GlobalRuntimeState { } } -impl, M: Into>> FromIterator<(K, M)> for GlobalRuntimeState { +impl, M: Into>> FromIterator<(K, M)> for GlobalRuntimeState<'_> { #[inline] fn from_iter>(iter: T) -> Self { let mut lib = Self::new(); @@ -253,7 +257,7 @@ impl, M: Into>> FromIterator<(K, M)> for Glob } } -impl, M: Into>> Extend<(K, M)> for GlobalRuntimeState { +impl, M: Into>> Extend<(K, M)> for GlobalRuntimeState<'_> { #[inline] fn extend>(&mut self, iter: T) { iter.into_iter().for_each(|(k, m)| { @@ -263,7 +267,7 @@ impl, M: Into>> Extend<(K, M)> for GlobalRunt } } -impl fmt::Debug for GlobalRuntimeState { +impl fmt::Debug for GlobalRuntimeState<'_> { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut f = f.debug_struct("GlobalRuntimeState"); diff --git a/src/func/native.rs b/src/func/native.rs index d3062816..17b93eac 100644 --- a/src/func/native.rs +++ b/src/func/native.rs @@ -65,7 +65,7 @@ pub struct NativeCallContext<'a> { /// Function source, if any. source: Option<&'a str>, /// The current [`GlobalRuntimeState`], if any. - global: Option<&'a GlobalRuntimeState>, + global: Option<&'a GlobalRuntimeState<'a>>, /// The current stack of loaded [modules][Module]. lib: &'a [&'a Module], /// [Position] of the function call. @@ -77,7 +77,7 @@ impl<'a, M: AsRef<[&'a Module]> + ?Sized, S: AsRef + 'a + ?Sized> &'a Engine, &'a S, Option<&'a S>, - &'a GlobalRuntimeState, + &'a GlobalRuntimeState<'a>, &'a M, Position, )> for NativeCallContext<'a> diff --git a/src/module/mod.rs b/src/module/mod.rs index f8aa351b..7e79214e 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -750,7 +750,7 @@ impl Module { let return_type = param_names.pop().unwrap(); (param_names, return_type) } else { - (param_names, Default::default()) + (param_names, crate::SmartString::new_const()) }; f.metadata.params_info = param_names; f.metadata.return_type = return_type_name; @@ -882,7 +882,7 @@ impl Module { let return_type = if names.len() > arg_types.as_ref().len() { names.pop().unwrap() } else { - Default::default() + crate::SmartString::new_const() }; names.shrink_to_fit(); (names, return_type)