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"), 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#[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#[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#[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#[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#[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#[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#[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}