rune/runtime/
runtime_context.rsuse core::fmt;
use ::rust_alloc::sync::Arc;
use crate as rune;
use crate::alloc::prelude::*;
use crate::hash;
use crate::runtime::{ConstConstruct, ConstValue, InstAddress, Memory, Output, VmResult};
use crate::Hash;
pub(crate) type FunctionHandler =
dyn Fn(&mut dyn Memory, InstAddress, usize, Output) -> VmResult<()> + Send + Sync;
#[derive(Default, TryClone)]
pub struct RuntimeContext {
functions: hash::Map<Arc<FunctionHandler>>,
constants: hash::Map<ConstValue>,
construct: hash::Map<Arc<dyn ConstConstruct>>,
}
assert_impl!(RuntimeContext: Send + Sync);
impl RuntimeContext {
pub(crate) fn new(
functions: hash::Map<Arc<FunctionHandler>>,
constants: hash::Map<ConstValue>,
construct: hash::Map<Arc<dyn ConstConstruct>>,
) -> Self {
Self {
functions,
constants,
construct,
}
}
#[inline]
pub fn function(&self, hash: &Hash) -> Option<&Arc<FunctionHandler>> {
self.functions.get(hash)
}
#[inline]
pub fn constant(&self, hash: &Hash) -> Option<&ConstValue> {
self.constants.get(hash)
}
#[inline]
pub(crate) fn construct(&self, hash: &Hash) -> Option<&dyn ConstConstruct> {
Some(&**self.construct.get(hash)?)
}
}
impl fmt::Debug for RuntimeContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "RuntimeContext")
}
}
#[cfg(test)]
static_assertions::assert_impl_all!(RuntimeContext: Send, Sync);