Streamline global state.
This commit is contained in:
parent
b65b7b05a5
commit
3d0626c76f
@ -23,12 +23,9 @@ pub type GlobalConstants =
|
|||||||
// corresponds to that key.
|
// corresponds to that key.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct GlobalRuntimeState<'a> {
|
pub struct GlobalRuntimeState<'a> {
|
||||||
/// Stack of module names.
|
|
||||||
#[cfg(not(feature = "no_module"))]
|
|
||||||
keys: crate::StaticVec<crate::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::ImmutableString, crate::Shared<crate::Module>)>,
|
||||||
/// Source of the current context.
|
/// Source of the current context.
|
||||||
///
|
///
|
||||||
/// No source if the string is empty.
|
/// No source if the string is empty.
|
||||||
@ -80,8 +77,6 @@ impl GlobalRuntimeState<'_> {
|
|||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new(engine: &Engine) -> Self {
|
pub fn new(engine: &Engine) -> Self {
|
||||||
Self {
|
Self {
|
||||||
#[cfg(not(feature = "no_module"))]
|
|
||||||
keys: crate::StaticVec::new_const(),
|
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
modules: crate::StaticVec::new_const(),
|
modules: crate::StaticVec::new_const(),
|
||||||
source: Identifier::new_const(),
|
source: Identifier::new_const(),
|
||||||
@ -123,7 +118,7 @@ impl GlobalRuntimeState<'_> {
|
|||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn num_imports(&self) -> usize {
|
pub fn num_imports(&self) -> usize {
|
||||||
self.keys.len()
|
self.modules.len()
|
||||||
}
|
}
|
||||||
/// Get the globally-imported [module][crate::Module] at a particular index.
|
/// Get the globally-imported [module][crate::Module] at a particular index.
|
||||||
///
|
///
|
||||||
@ -132,7 +127,7 @@ impl GlobalRuntimeState<'_> {
|
|||||||
#[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::Shared<crate::Module>> {
|
||||||
self.modules.get(index).cloned()
|
self.modules.get(index).map(|(_, m)| m).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
|
||||||
/// particular index.
|
/// particular index.
|
||||||
@ -146,7 +141,7 @@ impl GlobalRuntimeState<'_> {
|
|||||||
&mut self,
|
&mut self,
|
||||||
index: usize,
|
index: usize,
|
||||||
) -> Option<&mut crate::Shared<crate::Module>> {
|
) -> Option<&mut crate::Shared<crate::Module>> {
|
||||||
self.modules.get_mut(index)
|
self.modules.get_mut(index).map(|(_, m)| m)
|
||||||
}
|
}
|
||||||
/// Get the index of a globally-imported [module][crate::Module] by name.
|
/// Get the index of a globally-imported [module][crate::Module] by name.
|
||||||
///
|
///
|
||||||
@ -155,12 +150,12 @@ impl GlobalRuntimeState<'_> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn find_import(&self, name: &str) -> Option<usize> {
|
pub fn find_import(&self, name: &str) -> Option<usize> {
|
||||||
let len = self.keys.len();
|
let len = self.modules.len();
|
||||||
|
|
||||||
self.keys
|
self.modules
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.position(|key| key.as_str() == name)
|
.position(|(key, _)| key.as_str() == name)
|
||||||
.map(|i| len - 1 - i)
|
.map(|i| len - 1 - i)
|
||||||
}
|
}
|
||||||
/// Push an imported [module][crate::Module] onto the stack.
|
/// Push an imported [module][crate::Module] onto the stack.
|
||||||
@ -173,8 +168,7 @@ impl GlobalRuntimeState<'_> {
|
|||||||
name: impl Into<crate::ImmutableString>,
|
name: impl Into<crate::ImmutableString>,
|
||||||
module: impl Into<crate::Shared<crate::Module>>,
|
module: impl Into<crate::Shared<crate::Module>>,
|
||||||
) {
|
) {
|
||||||
self.keys.push(name.into());
|
self.modules.push((name.into(), module.into()));
|
||||||
self.modules.push(module.into());
|
|
||||||
}
|
}
|
||||||
/// Truncate the stack of globally-imported [modules][crate::Module] to a particular length.
|
/// Truncate the stack of globally-imported [modules][crate::Module] to a particular length.
|
||||||
///
|
///
|
||||||
@ -182,7 +176,6 @@ impl GlobalRuntimeState<'_> {
|
|||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn truncate_imports(&mut self, size: usize) {
|
pub fn truncate_imports(&mut self, size: usize) {
|
||||||
self.keys.truncate(size);
|
|
||||||
self.modules.truncate(size);
|
self.modules.truncate(size);
|
||||||
}
|
}
|
||||||
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
|
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
|
||||||
@ -192,10 +185,9 @@ impl GlobalRuntimeState<'_> {
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &crate::Module)> {
|
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &crate::Module)> {
|
||||||
self.keys
|
self.modules
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.zip(self.modules.iter().rev())
|
|
||||||
.map(|(name, module)| (name.as_str(), &**module))
|
.map(|(name, module)| (name.as_str(), &**module))
|
||||||
}
|
}
|
||||||
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
|
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
|
||||||
@ -206,8 +198,8 @@ impl GlobalRuntimeState<'_> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn iter_imports_raw(
|
pub(crate) fn iter_imports_raw(
|
||||||
&self,
|
&self,
|
||||||
) -> impl Iterator<Item = (&crate::ImmutableString, &crate::Shared<crate::Module>)> {
|
) -> impl Iterator<Item = &(crate::ImmutableString, crate::Shared<crate::Module>)> {
|
||||||
self.keys.iter().rev().zip(self.modules.iter().rev())
|
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.
|
||||||
///
|
///
|
||||||
@ -217,8 +209,8 @@ impl GlobalRuntimeState<'_> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn scan_imports_raw(
|
pub fn scan_imports_raw(
|
||||||
&self,
|
&self,
|
||||||
) -> impl Iterator<Item = (&crate::ImmutableString, &crate::Shared<crate::Module>)> {
|
) -> impl Iterator<Item = &(crate::ImmutableString, crate::Shared<crate::Module>)> {
|
||||||
self.keys.iter().zip(self.modules.iter())
|
self.modules.iter()
|
||||||
}
|
}
|
||||||
/// Does the specified function hash key exist in the stack of globally-imported
|
/// Does the specified function hash key exist in the stack of globally-imported
|
||||||
/// [modules][crate::Module]?
|
/// [modules][crate::Module]?
|
||||||
@ -229,7 +221,9 @@ impl GlobalRuntimeState<'_> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn contains_qualified_fn(&self, hash: u64) -> bool {
|
pub fn contains_qualified_fn(&self, hash: u64) -> bool {
|
||||||
self.modules.iter().any(|m| m.contains_qualified_fn(hash))
|
self.modules
|
||||||
|
.iter()
|
||||||
|
.any(|(_, m)| m.contains_qualified_fn(hash))
|
||||||
}
|
}
|
||||||
/// Get the specified function via its hash key from the stack of globally-imported
|
/// Get the specified function via its hash key from the stack of globally-imported
|
||||||
/// [modules][crate::Module].
|
/// [modules][crate::Module].
|
||||||
@ -245,7 +239,7 @@ impl GlobalRuntimeState<'_> {
|
|||||||
self.modules
|
self.modules
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id())))
|
.find_map(|(_, m)| m.get_qualified_fn(hash).map(|f| (f, m.id())))
|
||||||
}
|
}
|
||||||
/// Does the specified [`TypeId`][std::any::TypeId] iterator exist in the stack of
|
/// Does the specified [`TypeId`][std::any::TypeId] iterator exist in the stack of
|
||||||
/// globally-imported [modules][crate::Module]?
|
/// globally-imported [modules][crate::Module]?
|
||||||
@ -256,7 +250,9 @@ impl GlobalRuntimeState<'_> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn contains_iter(&self, id: std::any::TypeId) -> bool {
|
pub fn contains_iter(&self, id: std::any::TypeId) -> bool {
|
||||||
self.modules.iter().any(|m| m.contains_qualified_iter(id))
|
self.modules
|
||||||
|
.iter()
|
||||||
|
.any(|(_, m)| m.contains_qualified_iter(id))
|
||||||
}
|
}
|
||||||
/// Get the specified [`TypeId`][std::any::TypeId] iterator from the stack of globally-imported
|
/// Get the specified [`TypeId`][std::any::TypeId] iterator from the stack of globally-imported
|
||||||
/// [modules][crate::Module].
|
/// [modules][crate::Module].
|
||||||
@ -269,7 +265,7 @@ impl GlobalRuntimeState<'_> {
|
|||||||
self.modules
|
self.modules
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.find_map(|m| m.get_qualified_iter(id))
|
.find_map(|(_, m)| m.get_qualified_iter(id))
|
||||||
}
|
}
|
||||||
/// Get the current source.
|
/// Get the current source.
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -312,29 +308,26 @@ impl GlobalRuntimeState<'_> {
|
|||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
impl IntoIterator for GlobalRuntimeState<'_> {
|
impl IntoIterator for GlobalRuntimeState<'_> {
|
||||||
type Item = (crate::ImmutableString, crate::Shared<crate::Module>);
|
type Item = (crate::ImmutableString, crate::Shared<crate::Module>);
|
||||||
type IntoIter = std::iter::Zip<
|
type IntoIter = std::iter::Rev<
|
||||||
std::iter::Rev<smallvec::IntoIter<[crate::ImmutableString; 3]>>,
|
smallvec::IntoIter<
|
||||||
std::iter::Rev<smallvec::IntoIter<[crate::Shared<crate::Module>; 3]>>,
|
[(crate::ImmutableString, crate::Shared<crate::Module>); crate::STATIC_VEC_INLINE_SIZE],
|
||||||
|
>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
self.keys
|
self.modules.into_iter().rev()
|
||||||
.into_iter()
|
|
||||||
.rev()
|
|
||||||
.zip(self.modules.into_iter().rev())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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 crate::ImmutableString, &'a crate::Shared<crate::Module>);
|
type Item = &'a (crate::ImmutableString, crate::Shared<crate::Module>);
|
||||||
type IntoIter = std::iter::Zip<
|
type IntoIter = std::iter::Rev<
|
||||||
std::iter::Rev<std::slice::Iter<'a, crate::ImmutableString>>,
|
std::slice::Iter<'a, (crate::ImmutableString, crate::Shared<crate::Module>)>,
|
||||||
std::iter::Rev<std::slice::Iter<'a, crate::Shared<crate::Module>>>,
|
|
||||||
>;
|
>;
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
self.keys.iter().rev().zip(self.modules.iter().rev())
|
self.modules.iter().rev()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -345,8 +338,7 @@ impl<K: Into<crate::ImmutableString>, M: Into<crate::Shared<crate::Module>>> Ext
|
|||||||
#[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 {
|
||||||
self.keys.push(k.into());
|
self.modules.push((k.into(), m.into()));
|
||||||
self.modules.push(m.into());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -358,7 +350,7 @@ impl fmt::Debug for GlobalRuntimeState<'_> {
|
|||||||
let mut f = f.debug_struct("GlobalRuntimeState");
|
let mut f = f.debug_struct("GlobalRuntimeState");
|
||||||
|
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
f.field("imports", &self.keys.iter().zip(self.modules.iter()));
|
f.field("imports", &self.modules);
|
||||||
|
|
||||||
f.field("source", &self.source)
|
f.field("source", &self.source)
|
||||||
.field("num_operations", &self.num_operations);
|
.field("num_operations", &self.num_operations);
|
||||||
|
@ -235,7 +235,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, Shared<Module>)> {
|
||||||
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.
|
||||||
|
Loading…
Reference in New Issue
Block a user