linera_witty/runtime/wasmer/
memory.rs1use std::borrow::Cow;
7
8use wasmer::{Extern, Memory};
9
10use super::{super::traits::InstanceWithMemory, EntrypointInstance, ReentrantInstance};
11use crate::{GuestPointer, RuntimeError, RuntimeMemory};
12
13macro_rules! impl_memory_traits {
14 ($instance:ty) => {
15 impl<UserData: 'static> InstanceWithMemory for $instance {
16 fn memory_from_export(&self, export: Extern) -> Result<Option<Memory>, RuntimeError> {
17 Ok(match export {
18 Extern::Memory(memory) => Some(memory),
19 _ => None,
20 })
21 }
22 }
23
24 impl<UserData: 'static> RuntimeMemory<$instance> for Memory {
25 fn read<'instance>(
26 &self,
27 instance: &'instance $instance,
28 location: GuestPointer,
29 length: u32,
30 ) -> Result<Cow<'instance, [u8]>, RuntimeError> {
31 let mut buffer = vec![0u8; length as usize];
32 let start = location.0 as u64;
33
34 self.view(instance).read(start, &mut buffer)?;
35
36 Ok(Cow::Owned(buffer))
37 }
38
39 fn write(
40 &mut self,
41 instance: &mut $instance,
42 location: GuestPointer,
43 bytes: &[u8],
44 ) -> Result<(), RuntimeError> {
45 let start = location.0 as u64;
46
47 self.view(&*instance).write(start, bytes)?;
48
49 Ok(())
50 }
51 }
52 };
53}
54
55impl_memory_traits!(EntrypointInstance<UserData>);
56impl_memory_traits!(ReentrantInstance<'_, UserData>);