1use crate::{wasm_extern_t, wasm_memorytype_t, wasm_store_t};
2use alloc::boxed::Box;
3use core::hint;
4use wasmi::{Extern, Memory};
5
6#[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
17pub 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 fn memory(&self) -> Memory {
37 match self.inner.which {
38 Extern::Memory(m) => m,
39 _ => unsafe { hint::unreachable_unchecked() },
40 }
41 }
42}
43
44#[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#[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#[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#[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#[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#[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#[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#[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}