-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathwasm.rs
More file actions
79 lines (60 loc) · 2.1 KB
/
wasm.rs
File metadata and controls
79 lines (60 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::{alloc::Layout, marker::PhantomData};
use anyhow::bail;
use bump::WasmBump;
use wasmer::{Instance, LazyInit, Memory};
use crate::thread_pinned::ThreadPinned;
use super::{PluginPtr, PluginPtrMut};
mod bump;
#[derive(Default)]
pub struct WasmPluginContext {
bump: LazyInit<ThreadPinned<WasmBump>>,
memory: LazyInit<Memory>,
}
impl WasmPluginContext {
pub fn new() -> Self {
Self::default()
}
pub fn init_with_instance(&mut self, instance: &Instance) -> anyhow::Result<()> {
let allocate = instance.exports.get_function("quill_allocate")?;
let deallocate = instance.exports.get_function("quill_deallocate")?;
let bump = WasmBump::new(allocate.native()?, deallocate.native()?)?;
self.bump.initialize(ThreadPinned::new(bump));
self.memory
.initialize(instance.exports.get_memory("memory")?.clone());
Ok(())
}
pub unsafe fn deref_bytes(&self, ptr: PluginPtr<u8>, len: u32) -> anyhow::Result<&[u8]> {
let data = self.memory.get_ref().unwrap().data_unchecked();
let offset = ptr.ptr as usize;
if data.len() <= offset + len as usize {
bail!("pointer out of bounds");
}
Ok(&data[offset..(offset + len as usize)])
}
pub unsafe fn deref_bytes_mut(
&self,
ptr: PluginPtrMut<u8>,
len: u32,
) -> anyhow::Result<&mut [u8]> {
let data = self.memory.get_ref().unwrap().data_unchecked_mut();
let offset = ptr.ptr as usize;
if data.len() <= offset + len as usize {
bail!("pointer out of bounds");
}
Ok(&mut data[offset..(offset + len as usize)])
}
pub fn bump_allocate(&self, layout: Layout) -> anyhow::Result<PluginPtrMut<u8>> {
self.bump
.get_ref()
.unwrap()
.borrow_mut()
.alloc(layout)
.map(|wasm_ptr| PluginPtrMut {
ptr: wasm_ptr.offset() as u64,
_marker: PhantomData,
})
}
pub fn bump_reset(&self) {
let _ = self.bump.get_ref().unwrap().borrow_mut().reset();
}
}