1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::collections::HashMap;
use crate::compile::Item;
use crate::macros::{MacroContext, TokenStream};
use crate::runtime::{ConstValue, Stack, TypeCheck, VmError};
use crate::Hash;
use std::fmt;
use std::sync::Arc;
pub(crate) type FunctionHandler = dyn Fn(&mut Stack, usize) -> Result<(), VmError> + Send + Sync;
pub(crate) type MacroHandler =
dyn Fn(&mut MacroContext, &TokenStream) -> crate::Result<TokenStream> + Send + Sync;
#[derive(Default)]
pub struct RuntimeContext {
pub(crate) functions: HashMap<Hash, Arc<FunctionHandler>>,
pub(crate) types: HashMap<Hash, TypeCheck>,
pub(crate) constants: HashMap<Hash, ConstValue>,
}
impl RuntimeContext {
pub fn new() -> Self {
Self::default()
}
pub fn type_check_for(&self, item: &Item) -> Option<TypeCheck> {
Some(*self.types.get(&Hash::type_hash(item))?)
}
pub fn lookup(&self, hash: Hash) -> Option<&Arc<FunctionHandler>> {
self.functions.get(&hash)
}
pub fn constant(&self, hash: Hash) -> Option<&ConstValue> {
self.constants.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);