mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-09 06:08:29 +00:00
Remove free method, call finalizers in C API
This commit is contained in:
29
lib/c-api/src/wasm_c_api/externals/function.rs
vendored
29
lib/c-api/src/wasm_c_api/externals/function.rs
vendored
@@ -29,7 +29,7 @@ pub type wasm_func_callback_with_env_t = unsafe extern "C" fn(
|
||||
) -> *mut wasm_trap_t;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
pub type wasm_env_finalizer_t = unsafe extern "C" fn(c_void);
|
||||
pub type wasm_env_finalizer_t = unsafe extern "C" fn(*mut c_void);
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_func_new(
|
||||
@@ -92,7 +92,7 @@ pub unsafe extern "C" fn wasm_func_new_with_env(
|
||||
function_type: Option<&wasm_functype_t>,
|
||||
callback: Option<wasm_func_callback_with_env_t>,
|
||||
env: *mut c_void,
|
||||
finalizer: wasm_env_finalizer_t,
|
||||
finalizer: Option<wasm_env_finalizer_t>,
|
||||
) -> Option<Box<wasm_func_t>> {
|
||||
let store = store?;
|
||||
let function_type = function_type?;
|
||||
@@ -101,14 +101,24 @@ pub unsafe extern "C" fn wasm_func_new_with_env(
|
||||
let func_sig = &function_type.inner().function_type;
|
||||
let num_rets = func_sig.results().len();
|
||||
|
||||
#[repr(transparent)]
|
||||
struct WrapperEnv(*mut c_void);
|
||||
#[repr(C)]
|
||||
struct WrapperEnv {
|
||||
env: *mut c_void,
|
||||
finalizer: Option<wasm_env_finalizer_t>,
|
||||
};
|
||||
|
||||
impl Drop for WrapperEnv {
|
||||
fn drop(&mut self) {
|
||||
if let Some(finalizer) = self.finalizer {
|
||||
unsafe { (finalizer)(self.env as _) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl wasmer::WasmerEnv for WrapperEnv {
|
||||
fn finish(&mut self, _instance: &wasmer::Instance) -> Result<(), wasmer::HostEnvInitError> {
|
||||
Ok(())
|
||||
}
|
||||
fn free(&mut self) {}
|
||||
}
|
||||
|
||||
let inner_callback =
|
||||
@@ -129,7 +139,7 @@ pub unsafe extern "C" fn wasm_func_new_with_env(
|
||||
]
|
||||
.into();
|
||||
|
||||
let trap = callback(env.0, &processed_args, &mut results);
|
||||
let trap = callback(env.env, &processed_args, &mut results);
|
||||
|
||||
if !trap.is_null() {
|
||||
let trap: Box<wasm_trap_t> = Box::from_raw(trap);
|
||||
@@ -148,7 +158,12 @@ pub unsafe extern "C" fn wasm_func_new_with_env(
|
||||
Ok(processed_results)
|
||||
};
|
||||
|
||||
let function = Function::new_with_env(&store.inner, &func_sig, WrapperEnv(env), inner_callback);
|
||||
let function = Function::new_with_env(
|
||||
&store.inner,
|
||||
&func_sig,
|
||||
WrapperEnv { env, finalizer },
|
||||
inner_callback,
|
||||
);
|
||||
|
||||
Some(Box::new(wasm_func_t {
|
||||
instance: None,
|
||||
|
||||
Reference in New Issue
Block a user