devalang_core/core/store/
global.rs

1use crate::core::{
2    parser::statement::Statement,
3    plugin::loader::PluginInfo,
4    preprocessor::module::Module,
5    store::{function::FunctionTable, variable::VariableTable},
6};
7use std::collections::HashMap;
8
9#[derive(Debug, Clone)]
10pub struct GlobalStore {
11    pub modules: HashMap<String, Module>,
12    pub variables: VariableTable,
13    pub functions: FunctionTable,
14    pub events: HashMap<String, Vec<Statement>>,
15    pub plugins: HashMap<String, (PluginInfo, Vec<u8>)>,
16}
17
18impl GlobalStore {
19    pub fn new() -> Self {
20        GlobalStore {
21            modules: HashMap::new(),
22            functions: FunctionTable::new(),
23            variables: VariableTable::new(),
24            events: HashMap::new(),
25            plugins: HashMap::new(),
26        }
27    }
28
29    pub fn insert_module(&mut self, path: String, module: Module) {
30        self.modules.insert(path, module);
31    }
32
33    pub fn modules_mut(&mut self) -> &mut HashMap<String, Module> {
34        &mut self.modules
35    }
36
37    pub fn get_module(&self, path: &str) -> Option<&Module> {
38        self.modules.get(path)
39    }
40
41    pub fn remove_module(&mut self, path: &str) -> Option<Module> {
42        self.modules.remove(path)
43    }
44
45    pub fn register_event_handler(&mut self, event: &str, handler: Statement) {
46        self.events
47            .entry(event.to_string())
48            .or_default()
49            .push(handler);
50    }
51
52    pub fn get_event_handlers(&self, event: &str) -> Option<&Vec<Statement>> {
53        self.events.get(event)
54    }
55}