wasmtime_wiggle/
lib.rs

1pub use wasmtime_wiggle_macro::*;
2pub use wiggle::*;
3
4use wiggle_borrow::BorrowChecker;
5
6/// Lightweight `wasmtime::Memory` wrapper so we can implement the
7/// `wiggle::GuestMemory` trait on it.
8pub struct WasmtimeGuestMemory {
9    mem: wasmtime::Memory,
10    bc: BorrowChecker,
11}
12
13impl WasmtimeGuestMemory {
14    pub fn new(mem: wasmtime::Memory) -> Self {
15        Self {
16            mem,
17            // Wiggle does not expose any methods for functions to re-enter
18            // the WebAssembly instance, or expose the memory via non-wiggle
19            // mechanisms. However, the user-defined code may end up
20            // re-entering the instance, in which case this is an incorrect
21            // implementation - we require exactly one BorrowChecker exist per
22            // instance.
23            // This BorrowChecker construction is a holdover until it is
24            // integrated fully with wasmtime:
25            // https://github.com/bytecodealliance/wasmtime/issues/1917
26            bc: BorrowChecker::new(),
27        }
28    }
29}
30
31unsafe impl GuestMemory for WasmtimeGuestMemory {
32    fn base(&self) -> (*mut u8, u32) {
33        (self.mem.data_ptr(), self.mem.data_size() as _)
34    }
35    fn has_outstanding_borrows(&self) -> bool {
36        self.bc.has_outstanding_borrows()
37    }
38    fn is_shared_borrowed(&self, r: Region) -> bool {
39        self.bc.is_shared_borrowed(r)
40    }
41    fn is_mut_borrowed(&self, r: Region) -> bool {
42        self.bc.is_mut_borrowed(r)
43    }
44    fn shared_borrow(&self, r: Region) -> Result<BorrowHandle, GuestError> {
45        self.bc.shared_borrow(r)
46    }
47    fn mut_borrow(&self, r: Region) -> Result<BorrowHandle, GuestError> {
48        self.bc.mut_borrow(r)
49    }
50    fn shared_unborrow(&self, h: BorrowHandle) {
51        self.bc.shared_unborrow(h)
52    }
53    fn mut_unborrow(&self, h: BorrowHandle) {
54        self.bc.mut_unborrow(h)
55    }
56}