wasmer_c_api/wasm_c_api/
function_env.rs

1use crate::wasm_c_api::store::wasm_store_t;
2use std::ffi::c_void;
3use wasmer_api::FunctionEnv;
4
5#[derive(Clone, Copy)]
6#[repr(C)]
7pub struct FunctionCEnv {
8    #[allow(dead_code)]
9    inner: std::ptr::NonNull<c_void>,
10}
11
12impl FunctionCEnv {
13    #[allow(dead_code)]
14    pub(crate) fn as_ptr(&self) -> *mut c_void {
15        self.inner.as_ptr()
16    }
17}
18
19static NULL_ENV_PLACEHOLDER: u32 = 42;
20
21impl FunctionCEnv {
22    pub(crate) fn new(inner: std::ptr::NonNull<c_void>) -> Self {
23        Self { inner }
24    }
25}
26
27impl Default for FunctionCEnv {
28    fn default() -> Self {
29        Self {
30            inner: unsafe {
31                std::ptr::NonNull::new_unchecked(
32                    &NULL_ENV_PLACEHOLDER as *const u32 as *mut u32 as *mut c_void,
33                )
34            },
35        }
36    }
37}
38
39unsafe impl Send for FunctionCEnv {}
40
41#[derive(Clone)]
42#[allow(non_camel_case_types)]
43#[repr(C)]
44pub struct wasmer_funcenv_t {
45    inner: FunctionCEnv,
46}
47
48#[unsafe(no_mangle)]
49pub unsafe extern "C" fn wasmer_funcenv_new(
50    store: Option<&mut wasm_store_t>,
51    mut data: *mut c_void,
52) -> Option<Box<wasmer_funcenv_t>> {
53    let store = store?;
54    if data.is_null() {
55        data = &NULL_ENV_PLACEHOLDER as *const u32 as *mut u32 as *mut c_void;
56    }
57    let inner = FunctionCEnv::new(unsafe { std::ptr::NonNull::new_unchecked(data) });
58    let mut store_mut = unsafe { store.inner.store_mut() };
59    let _ = FunctionEnv::new(&mut store_mut, inner);
60    Some(Box::new(wasmer_funcenv_t { inner }))
61}
62
63#[unsafe(no_mangle)]
64pub unsafe extern "C" fn wasmer_funcenv_delete(_funcenv: Option<Box<wasmer_funcenv_t>>) {}