Skip to main content

wasmi_c_api/
memory.rs

1use crate::{wasm_extern_t, wasm_memorytype_t, wasm_store_t};
2use alloc::boxed::Box;
3use core::hint;
4use wasmi::{Extern, Memory};
5
6/// A Wasm linear memory.
7///
8/// Wraps [`Memory`].
9#[derive(Clone)]
10#[repr(transparent)]
11pub struct wasm_memory_t {
12    inner: wasm_extern_t,
13}
14
15wasmi_c_api_macros::declare_ref!(wasm_memory_t);
16
17/// Type specifying the number of pages of a Wasm linear memory.
18pub type wasm_memory_pages_t = u32;
19
20impl wasm_memory_t {
21    pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_memory_t> {
22        match &e.which {
23            Extern::Memory(_) => Some(unsafe { &*(e as *const _ as *const _) }),
24            _ => None,
25        }
26    }
27
28    pub(crate) fn try_from_mut(e: &mut wasm_extern_t) -> Option<&mut wasm_memory_t> {
29        match &mut e.which {
30            Extern::Memory(_) => Some(unsafe { &mut *(e as *mut _ as *mut _) }),
31            _ => None,
32        }
33    }
34
35    /// Returns the underlying [`Memory`] of the [`wasm_memory_t`].
36    fn memory(&self) -> Memory {
37        match self.inner.which {
38            Extern::Memory(m) => m,
39            _ => unsafe { hint::unreachable_unchecked() },
40        }
41    }
42}
43
44/// Creates a new [`wasm_memory_t`] from the given [`wasm_memorytype_t`].
45///
46/// Wraps [`Memory::new`].
47///
48/// # Safety
49///
50/// It is the caller's responsibility not to alias the [`wasm_memory_t`]
51/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
52#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
53#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
54pub unsafe extern "C" fn wasm_memory_new(
55    store: &mut wasm_store_t,
56    mt: &wasm_memorytype_t,
57) -> Option<Box<wasm_memory_t>> {
58    unsafe {
59        let memory = Memory::new(store.inner.context_mut(), mt.ty().ty).ok()?;
60        Some(Box::new(wasm_memory_t {
61            inner: wasm_extern_t {
62                store: store.inner.clone(),
63                which: memory.into(),
64            },
65        }))
66    }
67}
68
69/// Returns the [`wasm_memory_t`] as mutable reference to [`wasm_extern_t`].
70#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
71#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
72pub extern "C" fn wasm_memory_as_extern(m: &mut wasm_memory_t) -> &mut wasm_extern_t {
73    &mut m.inner
74}
75
76/// Returns the [`wasm_memory_t`] as shared reference to [`wasm_extern_t`].
77#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
78#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
79pub extern "C" fn wasm_memory_as_extern_const(m: &wasm_memory_t) -> &wasm_extern_t {
80    &m.inner
81}
82
83/// Returns the [`wasm_memorytype_t`] of the [`wasm_memory_t`].
84///
85/// Wraps [`Memory::ty`].
86///
87/// # Safety
88///
89/// It is the caller's responsibility not to alias the [`wasm_memory_t`]
90/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
91#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
92#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
93pub unsafe extern "C" fn wasm_memory_type(m: &wasm_memory_t) -> Box<wasm_memorytype_t> {
94    unsafe {
95        let ty = m.memory().ty(m.inner.store.context());
96        Box::new(wasm_memorytype_t::new(ty))
97    }
98}
99
100/// Returns the underlying data pointer of the [`wasm_memory_t`].
101///
102/// Wraps [`Memory::data_ptr`].
103///
104/// # Safety
105///
106/// It is the caller's responsibility not to alias the [`wasm_memory_t`]
107/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
108#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
109#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
110pub unsafe extern "C" fn wasm_memory_data(m: &wasm_memory_t) -> *mut u8 {
111    unsafe { m.memory().data_ptr(m.inner.store.context()) }
112}
113
114/// Returns the data buffer size of the [`wasm_memory_t`].
115///
116/// Wraps [`Memory::data_size`].
117///
118/// # Safety
119///
120/// It is the caller's responsibility not to alias the [`wasm_memory_t`]
121/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
122#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
123#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
124pub unsafe extern "C" fn wasm_memory_data_size(m: &wasm_memory_t) -> usize {
125    unsafe { m.memory().data_size(m.inner.store.context()) }
126}
127
128/// Returns the current number of Wasm pages of the [`wasm_memory_t`].
129///
130/// Wraps [`Memory::size`].
131///
132/// # Safety
133///
134/// It is the caller's responsibility not to alias the [`wasm_memory_t`]
135/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
136#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
137#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
138pub unsafe extern "C" fn wasm_memory_size(m: &wasm_memory_t) -> wasm_memory_pages_t {
139    unsafe {
140        let size = m.memory().size(m.inner.store.context());
141        let Ok(size32) = u32::try_from(size) else {
142            panic!("linear memory pages out of bounds: {size}")
143        };
144        size32
145    }
146}
147
148/// Grows the [`wasm_memory_t`] by `delta` Wasm pages.
149///
150/// Returns `true` if the operation was successful.
151///
152/// Wraps [`Memory::grow`].
153///
154/// # Safety
155///
156/// It is the caller's responsibility not to alias the [`wasm_memory_t`]
157/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
158#[cfg_attr(not(feature = "prefix-symbols"), unsafe(no_mangle))]
159#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
160pub unsafe extern "C" fn wasm_memory_grow(
161    m: &mut wasm_memory_t,
162    delta: wasm_memory_pages_t,
163) -> bool {
164    unsafe {
165        let memory = m.memory();
166        let mut store = m.inner.store.context_mut();
167        memory.grow(&mut store, u64::from(delta)).is_ok()
168    }
169}