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 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());

View File

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

View File

@ -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]

View File

@ -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<FnResolutionCache>,
/// Take care of the lifetime parameter
dummy: PhantomData<Option<&'a ()>>,
}
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.

View File

@ -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<str> here because `eval` may load a [module][Module] and
@ -45,20 +46,22 @@ pub struct GlobalRuntimeState {
#[cfg(not(feature = "no_function"))]
constants:
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)]
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<Module>);
type IntoIter =
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]
fn from_iter<T: IntoIterator<Item = (K, M)>>(iter: T) -> Self {
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]
fn extend<T: IntoIterator<Item = (K, M)>>(&mut self, iter: T) {
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]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_struct("GlobalRuntimeState");

View File

@ -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<str> + 'a + ?Sized>
&'a Engine,
&'a S,
Option<&'a S>,
&'a GlobalRuntimeState,
&'a GlobalRuntimeState<'a>,
&'a M,
Position,
)> for NativeCallContext<'a>

View File

@ -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)