use crate::{Dynamic, Engine, Identifier};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{fmt, marker::PhantomData};
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
pub type GlobalConstants =
crate::Shared<crate::Locked<std::collections::BTreeMap<crate::ImmutableString, Dynamic>>>;
#[derive(Clone)]
pub struct GlobalRuntimeState<'a> {
#[cfg(not(feature = "no_module"))]
keys: crate::StaticVec<crate::ImmutableString>,
#[cfg(not(feature = "no_module"))]
modules: crate::StaticVec<crate::Shared<crate::Module>>,
pub source: Identifier,
pub num_operations: u64,
pub num_modules_loaded: usize,
pub scope_level: usize,
pub always_search_scope: bool,
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
fn_hash_indexing: (u64, u64),
#[cfg(not(feature = "no_module"))]
pub embedded_module_resolver:
Option<crate::Shared<crate::module::resolvers::StaticModuleResolver>>,
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
pub constants: Option<GlobalConstants>,
pub tag: Dynamic,
#[cfg(feature = "debugging")]
pub debugger: super::Debugger,
dummy: PhantomData<&'a ()>,
}
impl GlobalRuntimeState<'_> {
#[inline(always)]
#[must_use]
pub fn new(engine: &Engine) -> Self {
Self {
#[cfg(not(feature = "no_module"))]
keys: crate::StaticVec::new_const(),
#[cfg(not(feature = "no_module"))]
modules: crate::StaticVec::new_const(),
source: Identifier::new_const(),
num_operations: 0,
num_modules_loaded: 0,
scope_level: 0,
always_search_scope: false,
#[cfg(not(feature = "no_module"))]
embedded_module_resolver: None,
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
fn_hash_indexing: (0, 0),
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
constants: None,
tag: engine.default_tag().clone(),
#[cfg(feature = "debugging")]
debugger: crate::eval::Debugger::new(
if engine.debugger.is_some() {
crate::eval::DebuggerStatus::Init
} else {
crate::eval::DebuggerStatus::CONTINUE
},
if let Some((ref init, ..)) = engine.debugger {
init(engine)
} else {
Dynamic::UNIT
},
),
dummy: PhantomData::default(),
}
}
#[cfg(not(feature = "no_module"))]
#[inline(always)]
#[must_use]
pub fn num_imports(&self) -> usize {
self.keys.len()
}
#[cfg(not(feature = "no_module"))]
#[inline(always)]
#[must_use]
pub fn get_shared_import(&self, index: usize) -> Option<crate::Shared<crate::Module>> {
self.modules.get(index).cloned()
}
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline(always)]
#[must_use]
pub(crate) fn get_shared_import_mut(
&mut self,
index: usize,
) -> Option<&mut crate::Shared<crate::Module>> {
self.modules.get_mut(index)
}
#[cfg(not(feature = "no_module"))]
#[inline]
#[must_use]
pub fn find_import(&self, name: &str) -> Option<usize> {
let len = self.keys.len();
self.keys
.iter()
.rev()
.position(|key| key.as_str() == name)
.map(|i| len - 1 - i)
}
#[cfg(not(feature = "no_module"))]
#[inline(always)]
pub fn push_import(
&mut self,
name: impl Into<crate::ImmutableString>,
module: impl Into<crate::Shared<crate::Module>>,
) {
self.keys.push(name.into());
self.modules.push(module.into());
}
#[cfg(not(feature = "no_module"))]
#[inline(always)]
pub fn truncate_imports(&mut self, size: usize) {
self.keys.truncate(size);
self.modules.truncate(size);
}
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline]
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &crate::Module)> {
self.keys
.iter()
.rev()
.zip(self.modules.iter().rev())
.map(|(name, module)| (name.as_str(), &**module))
}
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline]
pub(crate) fn iter_imports_raw(
&self,
) -> impl Iterator<Item = (&crate::ImmutableString, &crate::Shared<crate::Module>)> {
self.keys.iter().rev().zip(self.modules.iter().rev())
}
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline]
pub fn scan_imports_raw(
&self,
) -> impl Iterator<Item = (&crate::ImmutableString, &crate::Shared<crate::Module>)> {
self.keys.iter().zip(self.modules.iter())
}
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline]
#[must_use]
pub fn contains_qualified_fn(&self, hash: u64) -> bool {
self.modules.iter().any(|m| m.contains_qualified_fn(hash))
}
#[cfg(not(feature = "no_module"))]
#[inline]
#[must_use]
pub fn get_qualified_fn(
&self,
hash: u64,
) -> Option<(&crate::func::CallableFunction, Option<&str>)> {
self.modules
.iter()
.rev()
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id())))
}
#[cfg(not(feature = "no_module"))]
#[allow(dead_code)]
#[inline]
#[must_use]
pub fn contains_iter(&self, id: std::any::TypeId) -> bool {
self.modules.iter().any(|m| m.contains_qualified_iter(id))
}
#[cfg(not(feature = "no_module"))]
#[inline]
#[must_use]
pub fn get_iter(&self, id: std::any::TypeId) -> Option<&crate::func::IteratorFn> {
self.modules
.iter()
.rev()
.find_map(|m| m.get_qualified_iter(id))
}
#[inline]
#[must_use]
pub fn source(&self) -> Option<&str> {
if self.source.is_empty() {
None
} else {
Some(self.source.as_str())
}
}
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[must_use]
pub(crate) fn hash_idx_get(&mut self) -> u64 {
if self.fn_hash_indexing == (0, 0) {
let n1 = crate::calc_fn_hash(None, crate::engine::FN_IDX_GET, 2);
let n2 = crate::calc_fn_hash(None, crate::engine::FN_IDX_SET, 3);
self.fn_hash_indexing = (n1, n2);
n1
} else {
self.fn_hash_indexing.0
}
}
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[must_use]
pub(crate) fn hash_idx_set(&mut self) -> u64 {
if self.fn_hash_indexing == (0, 0) {
let n1 = crate::calc_fn_hash(None, crate::engine::FN_IDX_GET, 2);
let n2 = crate::calc_fn_hash(None, crate::engine::FN_IDX_SET, 3);
self.fn_hash_indexing = (n1, n2);
n2
} else {
self.fn_hash_indexing.1
}
}
}
#[cfg(not(feature = "no_module"))]
impl IntoIterator for GlobalRuntimeState<'_> {
type Item = (crate::ImmutableString, crate::Shared<crate::Module>);
type IntoIter = std::iter::Zip<
std::iter::Rev<smallvec::IntoIter<[crate::ImmutableString; 3]>>,
std::iter::Rev<smallvec::IntoIter<[crate::Shared<crate::Module>; 3]>>,
>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.keys
.into_iter()
.rev()
.zip(self.modules.into_iter().rev())
}
}
#[cfg(not(feature = "no_module"))]
impl<'a> IntoIterator for &'a GlobalRuntimeState<'_> {
type Item = (&'a crate::ImmutableString, &'a crate::Shared<crate::Module>);
type IntoIter = std::iter::Zip<
std::iter::Rev<std::slice::Iter<'a, crate::ImmutableString>>,
std::iter::Rev<std::slice::Iter<'a, crate::Shared<crate::Module>>>,
>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
let x = self.keys.iter().rev().zip(self.modules.iter().rev());
x
}
}
#[cfg(not(feature = "no_module"))]
impl<K: Into<crate::ImmutableString>, M: Into<crate::Shared<crate::Module>>> Extend<(K, M)>
for GlobalRuntimeState<'_>
{
#[inline]
fn extend<T: IntoIterator<Item = (K, M)>>(&mut self, iter: T) {
for (k, m) in iter {
self.keys.push(k.into());
self.modules.push(m.into());
}
}
}
impl fmt::Debug for GlobalRuntimeState<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_struct("GlobalRuntimeState");
#[cfg(not(feature = "no_module"))]
f.field("imports", &self.keys.iter().zip(self.modules.iter()));
f.field("source", &self.source)
.field("num_operations", &self.num_operations)
.field("num_modules_loaded", &self.num_modules_loaded);
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
f.field("fn_hash_indexing", &self.fn_hash_indexing);
#[cfg(not(feature = "no_module"))]
f.field("embedded_module_resolver", &self.embedded_module_resolver);
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
f.field("constants", &self.constants);
f.finish()
}
}