1use std::{collections::HashMap, fmt::Debug};
4
5use crate::{config::Config, Module, WasmEdgeResult};
6use sys::{AsInstance, Instance};
7use wasmedge_sys as sys;
8
9pub struct Store<'inst, T: ?Sized> {
12 pub(crate) inner: sys::Store,
13 pub(crate) instances: HashMap<String, &'inst mut T>,
14 pub(crate) wasm_instance_map: HashMap<String, Instance>,
15 pub(crate) executor: sys::Executor,
16}
17
18impl<T: ?Sized> Debug for Store<'_, T> {
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 f.debug_struct("Store")
21 .field("inner", &self.inner)
22 .field("instance_map", &self.instances.keys())
23 .field("wasm_instance_map", &self.wasm_instance_map.keys())
24 .field("wasm_instance_map", &self.executor)
25 .finish()
26 }
27}
28
29impl<'inst, T: AsInstance + ?Sized> Store<'inst, T> {
30 pub fn new(
36 config: Option<&Config>,
37 instances: HashMap<String, &'inst mut T>,
38 ) -> WasmEdgeResult<Self> {
39 let mut store = sys::Store::create()?;
40 let mut executor = sys::Executor::create(config.map(|cfg| cfg.inner.as_ref()), None)?;
41
42 for v in instances.values() {
43 executor.register_import_module(&mut store, *v)?;
44 }
45
46 Ok(Self {
47 inner: store,
48 instances,
49 wasm_instance_map: Default::default(),
50 executor,
51 })
52 }
53
54 pub fn register_active_module(&mut self, module: &Module) -> WasmEdgeResult<Instance> {
66 let Store {
67 inner, executor, ..
68 } = self;
69 let inner = executor.register_active_module(inner, &module.inner)?;
70 Ok(inner)
71 }
72
73 pub fn register_named_module(
74 &mut self,
75 name: impl AsRef<str>,
76 module: &Module,
77 ) -> WasmEdgeResult<()> {
78 let Store {
79 inner,
80 executor,
81 wasm_instance_map,
82 ..
83 } = self;
84 let name = name.as_ref().to_string();
85 let inst = executor.register_named_module(inner, &module.inner, &name)?;
86 wasm_instance_map.insert(name, inst);
87 Ok(())
88 }
89
90 pub fn named_instance_count(&self) -> usize {
92 self.instances.len() + self.wasm_instance_map.len()
93 }
94
95 pub fn instance_names(&self) -> Vec<String> {
97 self.instances
98 .keys()
99 .chain(self.wasm_instance_map.keys())
100 .cloned()
101 .collect()
102 }
103
104 pub fn contains(&self, mod_name: impl AsRef<str>) -> bool {
111 let mod_name = mod_name.as_ref().to_string();
112 self.instances.contains_key(&mod_name) || self.wasm_instance_map.contains_key(&mod_name)
113 }
114
115 pub fn get_instance_and_executor(
116 &mut self,
117 mod_name: impl AsRef<str>,
118 ) -> Option<(&mut T, &mut sys::Executor)> {
119 let inst = self
120 .instances
121 .get_mut(mod_name.as_ref())
122 .map(|p| *p as &mut T)?;
123
124 Some((inst, &mut self.executor))
125 }
126
127 pub fn get_named_wasm_and_executor(
128 &mut self,
129 mod_name: impl AsRef<str>,
130 ) -> Option<(&mut Instance, &mut sys::Executor)> {
131 let wasm_mod = self.wasm_instance_map.get_mut(mod_name.as_ref())?;
132 Some((wasm_mod, &mut self.executor))
133 }
134
135 pub fn executor(&mut self) -> &mut sys::Executor {
136 &mut self.executor
137 }
138}