feat!: pass a context object through the API to the wasm engine functions (#34)

This commit is contained in:
Valery Antopol
2023-02-14 17:48:09 +03:00
committed by GitHub
parent da020b8c6a
commit 06e0dd40d6
25 changed files with 687 additions and 382 deletions

View File

@ -18,44 +18,72 @@ mod errors;
pub use errors::MemoryAccessError;
pub trait MemoryReadable {
pub trait Store {
type ActualStore<'c>;
}
pub trait MemoryReadable<Store: self::Store> {
/// This function will panic if the `offset` is out of bounds.
/// It is caller's responsibility to check if the offset is in bounds
/// using `MemoryView::check_bounds` function
fn read_byte(&self, offset: u32) -> u8;
fn read_byte(&self, store: &mut <Store as self::Store>::ActualStore<'_>, offset: u32) -> u8;
/// This function will panic if `[offset..offset + COUNT]` is out of bounds.
/// It is caller's responsibility to check if the offset is in bounds
/// using `MemoryView::check_bounds` function.
fn read_array<const COUNT: usize>(&self, offset: u32) -> [u8; COUNT];
fn read_array<const COUNT: usize>(
&self,
store: &mut <Store as self::Store>::ActualStore<'_>,
offset: u32,
) -> [u8; COUNT];
/// This function will panic if `[offset..offset + size]` is out of bounds.
/// It is caller's responsibility to check if the offset is in bounds
/// using `MemoryView::check_bounds` function.
fn read_vec(&self, offset: u32, size: u32) -> Vec<u8>;
fn read_vec(
&self,
store: &mut <Store as self::Store>::ActualStore<'_>,
offset: u32,
size: u32,
) -> Vec<u8>;
}
pub trait MemoryWritable {
pub trait MemoryWritable<Store: self::Store> {
/// This function will panic if `offset` is out of bounds.
/// It is caller's responsibility to check if the offset is in bounds
/// using `MemoryView::check_bounds` function.
fn write_byte(&self, offset: u32, value: u8);
fn write_byte(
&self,
store: &mut <Store as self::Store>::ActualStore<'_>,
offset: u32,
value: u8,
);
/// This function will panic if `[offset..offset + bytes.len()]`.is out of bounds.
/// It is caller's responsibility to check if the offset is in bounds
/// using `MemoryView::check_bounds` function.
fn write_bytes(&self, offset: u32, bytes: &[u8]);
fn write_bytes(
&self,
store: &mut <Store as self::Store>::ActualStore<'_>,
offset: u32,
bytes: &[u8],
);
}
pub trait MemoryView: MemoryWritable + MemoryReadable {
pub trait MemoryView<Store: self::Store>: MemoryWritable<Store> + MemoryReadable<Store> {
/// For optimization purposes, user must check bounds first, then try read-write to memory
/// `MemoryWritable` and `MemoryReadable` functions will panic in case of out of bounds access`
fn check_bounds(&self, offset: u32, size: u32) -> Result<(), MemoryAccessError>;
fn check_bounds(
&self,
store: &mut <Store as self::Store>::ActualStore<'_>,
offset: u32,
size: u32,
) -> Result<(), MemoryAccessError>;
}
pub trait Memory<View>
pub trait Memory<View, Store: self::Store>
where
View: MemoryView,
View: MemoryView<Store>,
{
fn view(&self) -> View;
}