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"), 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    let memory = Memory::new(store.inner.context_mut(), mt.ty().ty).ok()?;
59    Some(Box::new(wasm_memory_t {
60        inner: wasm_extern_t {
61            store: store.inner.clone(),
62            which: memory.into(),
63        },
64    }))
65}
66
67/// Returns the [`wasm_memory_t`] as mutable reference to [`wasm_extern_t`].
68#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
69#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
70pub extern "C" fn wasm_memory_as_extern(m: &mut wasm_memory_t) -> &mut wasm_extern_t {
71    &mut m.inner
72}
73
74/// Returns the [`wasm_memory_t`] as shared reference to [`wasm_extern_t`].
75#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
76#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
77pub extern "C" fn wasm_memory_as_extern_const(m: &wasm_memory_t) -> &wasm_extern_t {
78    &m.inner
79}
80
81/// Returns the [`wasm_memorytype_t`] of the [`wasm_memory_t`].
82///
83/// Wraps [`Memory::ty`].
84///
85/// # Safety
86///
87/// It is the caller's responsibility not to alias the [`wasm_memory_t`]
88/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
89#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
90#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
91pub unsafe extern "C" fn wasm_memory_type(m: &wasm_memory_t) -> Box<wasm_memorytype_t> {
92    let ty = m.memory().ty(m.inner.store.context());
93    Box::new(wasm_memorytype_t::new(ty))
94}
95
96/// Returns the underlying data pointer of the [`wasm_memory_t`].
97///
98/// Wraps [`Memory::data_ptr`].
99///
100/// # Safety
101///
102/// It is the caller's responsibility not to alias the [`wasm_memory_t`]
103/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
104#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
105#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
106pub unsafe extern "C" fn wasm_memory_data(m: &wasm_memory_t) -> *mut u8 {
107    m.memory().data_ptr(m.inner.store.context())
108}
109
110/// Returns the data buffer size of the [`wasm_memory_t`].
111///
112/// Wraps [`Memory::data_size`].
113///
114/// # Safety
115///
116/// It is the caller's responsibility not to alias the [`wasm_memory_t`]
117/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
118#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
119#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
120pub unsafe extern "C" fn wasm_memory_data_size(m: &wasm_memory_t) -> usize {
121    m.memory().data_size(m.inner.store.context())
122}
123
124/// Returns the current number of Wasm pages of the [`wasm_memory_t`].
125///
126/// Wraps [`Memory::size`].
127///
128/// # Safety
129///
130/// It is the caller's responsibility not to alias the [`wasm_memory_t`]
131/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
132#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
133#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
134pub unsafe extern "C" fn wasm_memory_size(m: &wasm_memory_t) -> wasm_memory_pages_t {
135    let size = m.memory().size(m.inner.store.context());
136    let Ok(size32) = u32::try_from(size) else {
137        panic!("linear memory pages out of bounds: {size}")
138    };
139    size32
140}
141
142/// Grows the [`wasm_memory_t`] by `delta` Wasm pages.
143///
144/// Returns `true` if the operation was successful.
145///
146/// Wraps [`Memory::grow`].
147///
148/// # Safety
149///
150/// It is the caller's responsibility not to alias the [`wasm_memory_t`]
151/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
152#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
153#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
154pub unsafe extern "C" fn wasm_memory_grow(
155    m: &mut wasm_memory_t,
156    delta: wasm_memory_pages_t,
157) -> bool {
158    let memory = m.memory();
159    let mut store = m.inner.store.context_mut();
160    memory.grow(&mut store, u64::from(delta)).is_ok()
161}