rhai/src/eval/global_state.rs

390 lines
14 KiB
Rust
Raw Normal View History

2022-01-07 11:43:47 +08:00
//! Global runtime state.
2022-10-29 14:12:18 +08:00
use crate::{Dynamic, Engine, ImmutableString};
2022-01-07 11:43:47 +08:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
2022-01-29 11:09:43 +08:00
use std::{fmt, marker::PhantomData};
2022-01-07 11:43:47 +08:00
/// Collection of globally-defined constants.
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
pub type GlobalConstants =
2022-10-29 14:12:18 +08:00
crate::Shared<crate::Locked<std::collections::BTreeMap<ImmutableString, Dynamic>>>;
2022-01-29 11:09:43 +08:00
/// _(internals)_ Global runtime states.
2022-01-07 11:43:47 +08:00
/// Exported under the `internals` feature only.
//
// # Implementation Notes
//
2022-01-29 11:09:43 +08:00
// This implementation for imported [modules][crate::Module] splits the module names from the shared
2022-02-24 10:36:20 +08:00
// modules to improve data locality.
//
// Most usage will be looking up a particular key from the list and then getting the module that
// corresponds to that key.
2022-01-07 11:43:47 +08:00
#[derive(Clone)]
pub struct GlobalRuntimeState<'a> {
2022-10-27 15:52:24 +08:00
/// Names of imported [modules][crate::Module].
#[cfg(not(feature = "no_module"))]
2022-10-29 14:12:18 +08:00
imports: crate::StaticVec<ImmutableString>,
2022-01-29 11:09:43 +08:00
/// Stack of imported [modules][crate::Module].
#[cfg(not(feature = "no_module"))]
2022-10-27 15:52:24 +08:00
modules: crate::StaticVec<crate::Shared<crate::Module>>,
2022-01-07 11:43:47 +08:00
/// Source of the current context.
2022-04-16 16:36:53 +08:00
///
2022-01-07 11:43:47 +08:00
/// No source if the string is empty.
2022-10-29 14:12:18 +08:00
pub source: Option<ImmutableString>,
2022-01-07 11:43:47 +08:00
/// Number of operations performed.
pub num_operations: u64,
/// Number of modules loaded.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
pub num_modules_loaded: usize,
2022-04-16 16:36:53 +08:00
/// Level of the current scope.
///
/// The global (root) level is zero, a new block (or function call) is one level higher, and so on.
pub scope_level: usize,
/// Force a [`Scope`][crate::Scope] search by name.
///
/// Normally, access to variables are parsed with a relative offset into the
/// [`Scope`][crate::Scope] to avoid a lookup.
///
/// In some situation, e.g. after running an `eval` statement, or after a custom syntax
/// statement, subsequent offsets may become mis-aligned.
///
/// When that happens, this flag is turned on.
pub always_search_scope: bool,
2022-01-07 11:43:47 +08:00
/// Function call hashes to index getters and setters.
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
fn_hash_indexing: (u64, u64),
2022-04-16 16:36:53 +08:00
/// Embedded [module][crate::Module] resolver.
2022-01-07 11:43:47 +08:00
#[cfg(not(feature = "no_module"))]
2022-01-29 11:09:43 +08:00
pub embedded_module_resolver:
Option<crate::Shared<crate::module::resolvers::StaticModuleResolver>>,
2022-01-07 11:43:47 +08:00
/// Cache of globally-defined constants.
2022-01-28 22:07:49 +08:00
///
/// Interior mutability is needed because it is shared in order to aid in cloning.
2022-01-07 11:43:47 +08:00
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
2022-04-16 23:32:14 +08:00
pub constants: Option<GlobalConstants>,
2022-05-02 00:03:45 +08:00
/// Custom state that can be used by the external host.
pub tag: Dynamic,
2022-01-24 17:04:40 +08:00
/// Debugging interface.
#[cfg(feature = "debugging")]
pub debugger: super::Debugger,
/// Take care of the lifetime parameter.
dummy: PhantomData<&'a ()>,
2022-01-07 11:43:47 +08:00
}
impl GlobalRuntimeState<'_> {
2022-01-28 18:59:18 +08:00
/// Create a new [`GlobalRuntimeState`] based on an [`Engine`].
2022-01-07 11:43:47 +08:00
#[inline(always)]
#[must_use]
2022-01-28 18:59:18 +08:00
pub fn new(engine: &Engine) -> Self {
2022-01-07 11:43:47 +08:00
Self {
2022-10-27 15:52:24 +08:00
#[cfg(not(feature = "no_module"))]
imports: crate::StaticVec::new_const(),
2022-01-29 11:09:43 +08:00
#[cfg(not(feature = "no_module"))]
modules: crate::StaticVec::new_const(),
2022-10-29 14:12:18 +08:00
source: None,
2022-01-07 11:43:47 +08:00
num_operations: 0,
2022-10-04 16:27:04 +08:00
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
num_modules_loaded: 0,
2022-04-16 16:36:53 +08:00
scope_level: 0,
always_search_scope: false,
2022-01-07 11:43:47 +08:00
#[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"))]
2022-01-28 22:07:49 +08:00
constants: None,
2022-05-03 21:55:01 +08:00
tag: engine.default_tag().clone(),
2022-05-03 21:55:01 +08:00
#[cfg(feature = "debugging")]
debugger: crate::eval::Debugger::new(
if engine.debugger.is_some() {
crate::eval::DebuggerStatus::Init
} else {
crate::eval::DebuggerStatus::CONTINUE
},
2022-10-10 16:46:35 +08:00
match engine.debugger {
Some((ref init, ..)) => init(engine),
None => Dynamic::UNIT,
},
),
2022-05-03 21:55:01 +08:00
dummy: PhantomData::default(),
2022-01-07 11:43:47 +08:00
}
}
2022-01-29 11:09:43 +08:00
/// Get the length of the stack of globally-imported [modules][crate::Module].
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[inline(always)]
#[must_use]
pub fn num_imports(&self) -> usize {
2022-10-14 12:23:04 +08:00
self.modules.len()
2022-01-07 11:43:47 +08:00
}
2022-04-16 16:36:53 +08:00
/// Get the globally-imported [module][crate::Module] at a particular index.
2022-01-29 11:09:43 +08:00
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[inline(always)]
#[must_use]
2022-01-29 11:09:43 +08:00
pub fn get_shared_import(&self, index: usize) -> Option<crate::Shared<crate::Module>> {
2022-10-27 15:52:24 +08:00
self.modules.get(index).cloned()
2022-01-07 11:43:47 +08:00
}
2022-04-16 16:36:53 +08:00
/// Get a mutable reference to the globally-imported [module][crate::Module] at a
2022-02-24 10:36:20 +08:00
/// particular index.
2022-01-29 11:09:43 +08:00
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[allow(dead_code)]
#[inline(always)]
#[must_use]
2022-01-29 11:09:43 +08:00
pub(crate) fn get_shared_import_mut(
&mut self,
index: usize,
) -> Option<&mut crate::Shared<crate::Module>> {
2022-10-27 15:52:24 +08:00
self.modules.get_mut(index)
2022-01-07 11:43:47 +08:00
}
2022-04-16 16:36:53 +08:00
/// Get the index of a globally-imported [module][crate::Module] by name.
2022-01-29 11:09:43 +08:00
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[inline]
#[must_use]
pub fn find_import(&self, name: &str) -> Option<usize> {
2022-10-27 15:52:24 +08:00
self.imports
.iter()
.rev()
2022-10-27 15:52:24 +08:00
.position(|key| key.as_str() == name)
.map(|i| self.imports.len() - 1 - i)
2022-01-07 11:43:47 +08:00
}
2022-04-16 16:36:53 +08:00
/// Push an imported [module][crate::Module] onto the stack.
2022-01-29 11:09:43 +08:00
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[inline(always)]
2022-01-29 11:09:43 +08:00
pub fn push_import(
&mut self,
2022-10-29 14:12:18 +08:00
name: impl Into<ImmutableString>,
2022-01-29 11:09:43 +08:00
module: impl Into<crate::Shared<crate::Module>>,
) {
2022-10-27 15:52:24 +08:00
self.imports.push(name.into());
self.modules.push(module.into());
2022-01-07 11:43:47 +08:00
}
2022-01-29 11:09:43 +08:00
/// Truncate the stack of globally-imported [modules][crate::Module] to a particular length.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[inline(always)]
pub fn truncate_imports(&mut self, size: usize) {
2022-10-27 15:52:24 +08:00
self.imports.truncate(size);
2022-01-07 11:43:47 +08:00
self.modules.truncate(size);
}
2022-01-29 11:09:43 +08:00
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[inline]
2022-01-29 11:09:43 +08:00
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &crate::Module)> {
2022-10-27 15:52:24 +08:00
self.imports
2022-01-07 11:43:47 +08:00
.iter()
2022-10-27 15:52:24 +08:00
.zip(self.modules.iter())
2022-01-07 11:43:47 +08:00
.rev()
2022-07-05 16:26:38 +08:00
.map(|(name, module)| (name.as_str(), &**module))
2022-01-07 11:43:47 +08:00
}
2022-01-29 11:09:43 +08:00
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[inline]
2022-01-29 11:09:43 +08:00
pub(crate) fn iter_imports_raw(
&self,
2022-10-29 14:12:18 +08:00
) -> impl Iterator<Item = (&ImmutableString, &crate::Shared<crate::Module>)> {
2022-10-27 15:52:24 +08:00
self.imports.iter().zip(self.modules.iter()).rev()
2022-01-07 11:43:47 +08:00
}
2022-01-29 11:09:43 +08:00
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in forward order.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[inline]
2022-01-29 11:09:43 +08:00
pub fn scan_imports_raw(
&self,
2022-10-29 14:12:18 +08:00
) -> impl Iterator<Item = (&ImmutableString, &crate::Shared<crate::Module>)> {
2022-10-27 15:52:24 +08:00
self.imports.iter().zip(self.modules.iter())
}
/// Can the particular function with [`Dynamic`] parameter(s) exist in the stack of
/// globally-imported [modules][crate::Module]?
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline(always)]
pub(crate) fn may_contain_dynamic_fn(&self, hash_script: u64) -> bool {
self.modules
.iter()
.any(|m| m.may_contain_dynamic_fn(hash_script))
2022-01-07 11:43:47 +08:00
}
2022-02-24 10:36:20 +08:00
/// Does the specified function hash key exist in the stack of globally-imported
/// [modules][crate::Module]?
2022-01-29 11:09:43 +08:00
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[allow(dead_code)]
#[inline]
#[must_use]
pub fn contains_qualified_fn(&self, hash: u64) -> bool {
2022-10-27 15:52:24 +08:00
self.modules.iter().any(|m| m.contains_qualified_fn(hash))
2022-01-07 11:43:47 +08:00
}
2022-02-24 10:36:20 +08:00
/// Get the specified function via its hash key from the stack of globally-imported
/// [modules][crate::Module].
2022-01-29 11:09:43 +08:00
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[inline]
#[must_use]
2022-01-29 11:09:43 +08:00
pub fn get_qualified_fn(
&self,
hash: u64,
2022-10-29 14:12:18 +08:00
) -> Option<(&crate::func::CallableFunction, Option<&ImmutableString>)> {
2022-01-07 11:43:47 +08:00
self.modules
.iter()
.rev()
2022-10-29 14:12:18 +08:00
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id_raw())))
2022-01-07 11:43:47 +08:00
}
/// Does the specified [`TypeId`][std::any::TypeId] iterator exist in the stack of
2022-01-29 11:09:43 +08:00
/// globally-imported [modules][crate::Module]?
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[allow(dead_code)]
#[inline]
#[must_use]
2022-01-29 11:09:43 +08:00
pub fn contains_iter(&self, id: std::any::TypeId) -> bool {
2022-10-27 15:52:24 +08:00
self.modules.iter().any(|m| m.contains_qualified_iter(id))
2022-01-07 11:43:47 +08:00
}
/// Get the specified [`TypeId`][std::any::TypeId] iterator from the stack of globally-imported
2022-01-29 11:09:43 +08:00
/// [modules][crate::Module].
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
2022-01-07 11:43:47 +08:00
#[inline]
#[must_use]
2022-01-29 11:09:43 +08:00
pub fn get_iter(&self, id: std::any::TypeId) -> Option<&crate::func::IteratorFn> {
2022-01-07 11:43:47 +08:00
self.modules
.iter()
.rev()
2022-10-27 15:52:24 +08:00
.find_map(|m| m.get_qualified_iter(id))
2022-01-07 11:43:47 +08:00
}
2022-01-24 17:04:40 +08:00
/// Get the current source.
2022-10-29 14:12:18 +08:00
#[inline(always)]
2022-01-24 17:04:40 +08:00
#[must_use]
pub fn source(&self) -> Option<&str> {
2022-10-29 14:12:18 +08:00
self.source.as_ref().map(|s| s.as_str())
}
/// Get the current source.
#[inline(always)]
#[must_use]
2022-10-30 16:13:51 +08:00
#[allow(dead_code)]
2022-10-29 14:12:18 +08:00
pub(crate) const fn source_raw(&self) -> Option<&ImmutableString> {
self.source.as_ref()
2022-01-24 17:04:40 +08:00
}
2022-01-07 11:43:47 +08:00
/// Get the pre-calculated index getter hash.
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[must_use]
pub(crate) fn hash_idx_get(&mut self) -> u64 {
2022-07-27 18:04:59 +08:00
if self.fn_hash_indexing == (0, 0) {
2022-09-21 11:46:23 +08:00
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);
2022-01-07 11:43:47 +08:00
self.fn_hash_indexing = (n1, n2);
n1
2022-07-27 18:04:59 +08:00
} else {
self.fn_hash_indexing.0
2022-01-07 11:43:47 +08:00
}
}
/// Get the pre-calculated index setter hash.
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[must_use]
pub(crate) fn hash_idx_set(&mut self) -> u64 {
2022-07-27 18:04:59 +08:00
if self.fn_hash_indexing == (0, 0) {
2022-09-21 11:46:23 +08:00
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);
2022-01-07 11:43:47 +08:00
self.fn_hash_indexing = (n1, n2);
n2
2022-07-27 18:04:59 +08:00
} else {
self.fn_hash_indexing.1
2022-01-07 11:43:47 +08:00
}
}
}
2022-01-29 11:09:43 +08:00
#[cfg(not(feature = "no_module"))]
impl IntoIterator for GlobalRuntimeState<'_> {
2022-10-29 14:12:18 +08:00
type Item = (ImmutableString, crate::Shared<crate::Module>);
2022-10-14 12:23:04 +08:00
type IntoIter = std::iter::Rev<
2022-10-27 15:52:24 +08:00
std::iter::Zip<
2022-10-29 14:12:18 +08:00
smallvec::IntoIter<[ImmutableString; crate::STATIC_VEC_INLINE_SIZE]>,
2022-10-27 15:52:24 +08:00
smallvec::IntoIter<[crate::Shared<crate::Module>; crate::STATIC_VEC_INLINE_SIZE]>,
2022-10-14 12:23:04 +08:00
>,
2022-01-29 11:09:43 +08:00
>;
2022-01-07 11:43:47 +08:00
fn into_iter(self) -> Self::IntoIter {
2022-10-27 15:52:24 +08:00
self.imports.into_iter().zip(self.modules.into_iter()).rev()
2022-01-07 11:43:47 +08:00
}
}
2022-07-05 16:26:38 +08:00
#[cfg(not(feature = "no_module"))]
impl<'a> IntoIterator for &'a GlobalRuntimeState<'_> {
2022-10-29 14:12:18 +08:00
type Item = (&'a ImmutableString, &'a crate::Shared<crate::Module>);
2022-10-14 12:23:04 +08:00
type IntoIter = std::iter::Rev<
2022-10-27 15:52:24 +08:00
std::iter::Zip<
2022-10-29 14:12:18 +08:00
std::slice::Iter<'a, ImmutableString>,
2022-10-27 15:52:24 +08:00
std::slice::Iter<'a, crate::Shared<crate::Module>>,
>,
2022-07-05 16:26:38 +08:00
>;
fn into_iter(self) -> Self::IntoIter {
2022-10-27 15:52:24 +08:00
self.imports.iter().zip(self.modules.iter()).rev()
2022-07-05 16:26:38 +08:00
}
}
2022-01-29 11:09:43 +08:00
#[cfg(not(feature = "no_module"))]
2022-10-29 14:12:18 +08:00
impl<K: Into<ImmutableString>, M: Into<crate::Shared<crate::Module>>> Extend<(K, M)>
2022-01-29 11:09:43 +08:00
for GlobalRuntimeState<'_>
{
2022-01-10 22:51:24 +08:00
#[inline]
2022-01-07 11:43:47 +08:00
fn extend<T: IntoIterator<Item = (K, M)>>(&mut self, iter: T) {
2022-01-28 18:59:18 +08:00
for (k, m) in iter {
2022-10-27 15:52:24 +08:00
self.imports.push(k.into());
self.modules.push(m.into());
2022-01-28 18:59:18 +08:00
}
2022-01-07 11:43:47 +08:00
}
}
impl fmt::Debug for GlobalRuntimeState<'_> {
2022-09-27 08:52:39 +08:00
#[cold]
#[inline(never)]
2022-01-07 11:43:47 +08:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2022-01-10 13:43:30 +08:00
let mut f = f.debug_struct("GlobalRuntimeState");
2022-01-29 11:09:43 +08:00
#[cfg(not(feature = "no_module"))]
2022-10-27 15:52:24 +08:00
f.field("imports", &self.scan_imports_raw().collect::<Vec<_>>());
2022-01-29 11:09:43 +08:00
f.field("source", &self.source)
2022-10-04 16:27:04 +08:00
.field("num_operations", &self.num_operations);
2022-01-10 13:43:30 +08:00
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
f.field("fn_hash_indexing", &self.fn_hash_indexing);
#[cfg(not(feature = "no_module"))]
2022-10-04 16:27:04 +08:00
f.field("num_modules_loaded", &self.num_modules_loaded)
.field("embedded_module_resolver", &self.embedded_module_resolver);
2022-01-10 13:43:30 +08:00
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
f.field("constants", &self.constants);
f.finish()
2022-01-07 11:43:47 +08:00
}
}