wasmer_wasix/utils/
store.rs

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