Add lifetimes to GlobalRuntimeState and EvalState for future needs.

This commit is contained in:
Stephen Chung 2022-01-22 17:48:07 +08:00
parent c32ace40a4
commit fb0b071fe0
7 changed files with 29 additions and 21 deletions

View File

@ -18,7 +18,7 @@ fn bench_eval_module(bench: &mut Bencher) {
let ast = engine.compile(script).unwrap(); 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()); engine.register_static_module("testing", module.into());

View File

@ -151,7 +151,7 @@ impl Deref for Expression<'_> {
} }
} }
impl EvalContext<'_, '_, '_, '_, '_, '_, '_, '_> { impl EvalContext<'_, '_, '_, '_, '_, '_, '_, '_, '_, '_> {
/// Evaluate an [expression tree][Expression]. /// Evaluate an [expression tree][Expression].
/// ///
/// # WARNING - Low Level API /// # WARNING - Low Level API

View File

@ -7,15 +7,15 @@ use std::prelude::v1::*;
/// Context of a script evaluation process. /// Context of a script evaluation process.
#[derive(Debug)] #[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`]. /// The current [`Engine`].
pub(crate) engine: &'a Engine, pub(crate) engine: &'a Engine,
/// The current [`Scope`]. /// The current [`Scope`].
pub(crate) scope: &'x mut Scope<'px>, pub(crate) scope: &'x mut Scope<'px>,
/// The current [`GlobalRuntimeState`]. /// The current [`GlobalRuntimeState`].
pub(crate) global: &'m mut GlobalRuntimeState, pub(crate) global: &'m mut GlobalRuntimeState<'pm>,
/// The current [evaluation state][EvalState]. /// 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]. /// The current stack of imported [modules][Module].
pub(crate) lib: &'b [&'b Module], pub(crate) lib: &'b [&'b Module],
/// The current bound `this` pointer, if any. /// 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, pub(crate) level: usize,
} }
impl<'x, 'px, 'pt> EvalContext<'_, 'x, 'px, '_, '_, '_, '_, 'pt> { impl<'x, 'px, 'pt> EvalContext<'_, 'x, 'px, '_, '_, '_, '_, '_, '_, 'pt> {
/// The current [`Engine`]. /// The current [`Engine`].
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]

View File

@ -3,13 +3,14 @@
use crate::func::call::FnResolutionCache; use crate::func::call::FnResolutionCache;
use crate::StaticVec; use crate::StaticVec;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::marker::PhantomData;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
/// _(internals)_ A type that holds all the current states of the [`Engine`]. /// _(internals)_ A type that holds all the current states of the [`Engine`].
/// Exported under the `internals` feature only. /// Exported under the `internals` feature only.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct EvalState { pub struct EvalState<'a> {
/// Force a [`Scope`] search by name. /// Force a [`Scope`] search by name.
/// ///
/// Normally, access to variables are parsed with a relative offset into the [`Scope`] to avoid a lookup. /// 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, pub scope_level: usize,
/// Stack of function resolution caches. /// Stack of function resolution caches.
fn_resolution_caches: StaticVec<FnResolutionCache>, fn_resolution_caches: StaticVec<FnResolutionCache>,
/// Take care of the lifetime parameter
dummy: PhantomData<Option<&'a ()>>,
} }
impl EvalState { impl EvalState<'_> {
/// Create a new [`EvalState`]. /// Create a new [`EvalState`].
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub const fn new() -> Self { pub fn new() -> Self {
Self { Self {
always_search_scope: false, always_search_scope: false,
scope_level: 0, scope_level: 0,
fn_resolution_caches: StaticVec::new_const(), fn_resolution_caches: StaticVec::new_const(),
dummy: PhantomData::default(),
} }
} }
/// Get the number of function resolution cache(s) in the stack. /// Get the number of function resolution cache(s) in the stack.

View File

@ -8,6 +8,7 @@ use std::{
any::TypeId, any::TypeId,
fmt, fmt,
iter::{FromIterator, Rev, Zip}, iter::{FromIterator, Rev, Zip},
marker::PhantomData,
}; };
/// _(internals)_ A stack of imported [modules][Module] plus mutable global runtime states. /// _(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 // Most usage will be looking up a particular key from the list and then getting the module that
// corresponds to that key. // corresponds to that key.
#[derive(Clone)] #[derive(Clone)]
pub struct GlobalRuntimeState { pub struct GlobalRuntimeState<'a> {
/// Stack of module names. /// Stack of module names.
// //
// We cannot use Cow<str> here because `eval` may load a [module][Module] and // We cannot use Cow<str> here because `eval` may load a [module][Module] and
@ -45,20 +46,22 @@ pub struct GlobalRuntimeState {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
constants: constants:
Option<Shared<crate::Locked<std::collections::BTreeMap<Identifier, crate::Dynamic>>>>, Option<Shared<crate::Locked<std::collections::BTreeMap<Identifier, crate::Dynamic>>>>,
/// Take care of the lifetime parameter.
dummy: PhantomData<&'a ()>,
} }
impl Default for GlobalRuntimeState { impl Default for GlobalRuntimeState<'_> {
#[inline(always)] #[inline(always)]
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }
} }
impl GlobalRuntimeState { impl GlobalRuntimeState<'_> {
/// Create a new [`GlobalRuntimeState`]. /// Create a new [`GlobalRuntimeState`].
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub const fn new() -> Self { pub fn new() -> Self {
Self { Self {
keys: StaticVec::new_const(), keys: StaticVec::new_const(),
modules: StaticVec::new_const(), modules: StaticVec::new_const(),
@ -72,6 +75,7 @@ impl GlobalRuntimeState {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
constants: None, constants: None,
dummy: PhantomData::default(),
} }
} }
/// Get the length of the stack of globally-imported [modules][Module]. /// 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<Module>); type Item = (Identifier, Shared<Module>);
type IntoIter = type IntoIter =
Zip<Rev<smallvec::IntoIter<[Identifier; 3]>>, Rev<smallvec::IntoIter<[Shared<Module>; 3]>>>; Zip<Rev<smallvec::IntoIter<[Identifier; 3]>>, Rev<smallvec::IntoIter<[Shared<Module>; 3]>>>;
@ -244,7 +248,7 @@ impl IntoIterator for GlobalRuntimeState {
} }
} }
impl<K: Into<Identifier>, M: Into<Shared<Module>>> FromIterator<(K, M)> for GlobalRuntimeState { impl<K: Into<Identifier>, M: Into<Shared<Module>>> FromIterator<(K, M)> for GlobalRuntimeState<'_> {
#[inline] #[inline]
fn from_iter<T: IntoIterator<Item = (K, M)>>(iter: T) -> Self { fn from_iter<T: IntoIterator<Item = (K, M)>>(iter: T) -> Self {
let mut lib = Self::new(); let mut lib = Self::new();
@ -253,7 +257,7 @@ impl<K: Into<Identifier>, M: Into<Shared<Module>>> FromIterator<(K, M)> for Glob
} }
} }
impl<K: Into<Identifier>, M: Into<Shared<Module>>> Extend<(K, M)> for GlobalRuntimeState { impl<K: Into<Identifier>, M: Into<Shared<Module>>> Extend<(K, M)> 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) {
iter.into_iter().for_each(|(k, m)| { iter.into_iter().for_each(|(k, m)| {
@ -263,7 +267,7 @@ impl<K: Into<Identifier>, M: Into<Shared<Module>>> Extend<(K, M)> for GlobalRunt
} }
} }
impl fmt::Debug for GlobalRuntimeState { impl fmt::Debug for GlobalRuntimeState<'_> {
#[inline] #[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_struct("GlobalRuntimeState"); let mut f = f.debug_struct("GlobalRuntimeState");

View File

@ -65,7 +65,7 @@ pub struct NativeCallContext<'a> {
/// Function source, if any. /// Function source, if any.
source: Option<&'a str>, source: Option<&'a str>,
/// The current [`GlobalRuntimeState`], if any. /// The current [`GlobalRuntimeState`], if any.
global: Option<&'a GlobalRuntimeState>, global: Option<&'a GlobalRuntimeState<'a>>,
/// The current stack of loaded [modules][Module]. /// The current stack of loaded [modules][Module].
lib: &'a [&'a Module], lib: &'a [&'a Module],
/// [Position] of the function call. /// [Position] of the function call.
@ -77,7 +77,7 @@ impl<'a, M: AsRef<[&'a Module]> + ?Sized, S: AsRef<str> + 'a + ?Sized>
&'a Engine, &'a Engine,
&'a S, &'a S,
Option<&'a S>, Option<&'a S>,
&'a GlobalRuntimeState, &'a GlobalRuntimeState<'a>,
&'a M, &'a M,
Position, Position,
)> for NativeCallContext<'a> )> for NativeCallContext<'a>

View File

@ -750,7 +750,7 @@ impl Module {
let return_type = param_names.pop().unwrap(); let return_type = param_names.pop().unwrap();
(param_names, return_type) (param_names, return_type)
} else { } else {
(param_names, Default::default()) (param_names, crate::SmartString::new_const())
}; };
f.metadata.params_info = param_names; f.metadata.params_info = param_names;
f.metadata.return_type = return_type_name; f.metadata.return_type = return_type_name;
@ -882,7 +882,7 @@ impl Module {
let return_type = if names.len() > arg_types.as_ref().len() { let return_type = if names.len() > arg_types.as_ref().len() {
names.pop().unwrap() names.pop().unwrap()
} else { } else {
Default::default() crate::SmartString::new_const()
}; };
names.shrink_to_fit(); names.shrink_to_fit();
(names, return_type) (names, return_type)