Skip to content

Commit 081832a

Browse files
committed
rust: support memory
1 parent 0f9f042 commit 081832a

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

bindings/rust/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,38 @@ impl From<ExecutionResult> for sys::FizzyExecutionResult {
139139
}
140140

141141
impl Instance {
142+
fn checked_memory_range(&self, offset: u32) -> core::ops::Range<usize> {
143+
let offset = offset as usize;
144+
let size = 0 as usize;
145+
offset..offset + size
146+
}
147+
148+
pub fn memory_size(&self) -> usize {
149+
unsafe { sys::fizzy_get_instance_memory_size(self.0.as_ptr()) }
150+
}
151+
152+
pub fn memory_get_into(&mut self, offset: u32, target: &mut [u8]) -> Result<(), ()> {
153+
let mem = unsafe {
154+
std::slice::from_raw_parts(
155+
sys::fizzy_get_instance_memory_data(self.0.as_ptr()),
156+
sys::fizzy_get_instance_memory_size(self.0.as_ptr()),
157+
)
158+
};
159+
target.copy_from_slice(&mem[self.checked_memory_range(offset)]);
160+
Ok(())
161+
}
162+
163+
pub fn memory_set(&mut self, offset: u32, value: &[u8]) -> Result<(), ()> {
164+
let mem = unsafe {
165+
std::slice::from_raw_parts_mut(
166+
sys::fizzy_get_instance_memory_data(self.0.as_ptr()),
167+
sys::fizzy_get_instance_memory_size(self.0.as_ptr()),
168+
)
169+
};
170+
mem[self.checked_memory_range(offset)].copy_from_slice(value);
171+
Ok(())
172+
}
173+
142174
pub unsafe fn unsafe_execute(&mut self, func_idx: u32, args: &[Value]) -> ExecutionResult {
143175
ExecutionResult {
144176
0: sys::fizzy_execute(self.0.as_ptr(), func_idx, args.as_ptr(), 0),

0 commit comments

Comments
 (0)