wasmer_wasix/utils/
store.rs

1use bincode::config;
2
3/// A snapshot that captures the runtime state of an instance.
4#[derive(Default, serde::Serialize, serde::Deserialize, Clone, Debug)]
5pub struct StoreSnapshot {
6    /// Values of all globals, indexed by the same index used in Webassembly.
7    pub globals: Vec<u128>,
8}
9
10impl StoreSnapshot {
11    pub fn serialize(&self) -> Result<Vec<u8>, bincode::error::EncodeError> {
12        bincode::serde::encode_to_vec(self, config::legacy())
13    }
14
15    pub fn deserialize(data: &[u8]) -> Result<Self, bincode::error::DecodeError> {
16        bincode::serde::decode_from_slice(data, config::legacy()).map(|(ret, _)| ret)
17    }
18}
19
20pub fn capture_store_snapshot(store: &mut impl wasmer::AsStoreMut) -> StoreSnapshot {
21    let objs = store.objects_mut();
22    let globals = objs.as_u128_globals();
23    StoreSnapshot { globals }
24}
25
26pub fn restore_store_snapshot(store: &mut impl wasmer::AsStoreMut, snapshot: &StoreSnapshot) {
27    let objs = store.objects_mut();
28
29    for (index, value) in snapshot.globals.iter().enumerate() {
30        objs.set_global_unchecked(index, *value);
31    }
32}