mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-08 21:58:20 +00:00
Merge branch 'master' into feature/host-env-prototype
This commit is contained in:
64
lib/c-api/src/wasm_c_api/externals/function.rs
vendored
64
lib/c-api/src/wasm_c_api/externals/function.rs
vendored
@@ -7,6 +7,7 @@ use std::ffi::c_void;
|
||||
use std::sync::Arc;
|
||||
use wasmer::{Function, Instance, RuntimeError, Val};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub struct wasm_func_t {
|
||||
pub(crate) inner: Function,
|
||||
@@ -32,12 +33,15 @@ pub type wasm_env_finalizer_t = unsafe extern "C" fn(c_void);
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_func_new(
|
||||
store: &wasm_store_t,
|
||||
ft: &wasm_functype_t,
|
||||
callback: wasm_func_callback_t,
|
||||
store: Option<&wasm_store_t>,
|
||||
function_type: Option<&wasm_functype_t>,
|
||||
callback: Option<wasm_func_callback_t>,
|
||||
) -> Option<Box<wasm_func_t>> {
|
||||
// TODO: handle null pointers?
|
||||
let func_sig = ft.sig();
|
||||
let store = store?;
|
||||
let function_type = function_type?;
|
||||
let callback = callback?;
|
||||
|
||||
let func_sig = &function_type.inner().function_type;
|
||||
let num_rets = func_sig.results().len();
|
||||
let inner_callback = move |args: &[Val]| -> Result<Vec<Val>, RuntimeError> {
|
||||
let processed_args: wasm_val_vec_t = args
|
||||
@@ -84,14 +88,17 @@ pub unsafe extern "C" fn wasm_func_new(
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_func_new_with_env(
|
||||
store: &wasm_store_t,
|
||||
ft: &wasm_functype_t,
|
||||
callback: wasm_func_callback_with_env_t,
|
||||
store: Option<&wasm_store_t>,
|
||||
function_type: Option<&wasm_functype_t>,
|
||||
callback: Option<wasm_func_callback_with_env_t>,
|
||||
env: *mut c_void,
|
||||
finalizer: wasm_env_finalizer_t,
|
||||
) -> Option<Box<wasm_func_t>> {
|
||||
// TODO: handle null pointers?
|
||||
let func_sig = ft.sig();
|
||||
let store = store?;
|
||||
let function_type = function_type?;
|
||||
let callback = callback?;
|
||||
|
||||
let func_sig = &function_type.inner().function_type;
|
||||
let num_rets = func_sig.results().len();
|
||||
|
||||
#[repr(transparent)]
|
||||
@@ -154,10 +161,13 @@ pub unsafe extern "C" fn wasm_func_delete(_func: Option<Box<wasm_func_t>>) {}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_func_call(
|
||||
func: &wasm_func_t,
|
||||
args: &wasm_val_vec_t,
|
||||
func: Option<&wasm_func_t>,
|
||||
args: Option<&wasm_val_vec_t>,
|
||||
results: &mut wasm_val_vec_t,
|
||||
) -> Option<Box<wasm_trap_t>> {
|
||||
let func = func?;
|
||||
let args = args?;
|
||||
|
||||
let params = args
|
||||
.into_slice()
|
||||
.map(|slice| {
|
||||
@@ -165,18 +175,34 @@ pub unsafe extern "C" fn wasm_func_call(
|
||||
.into_iter()
|
||||
.map(TryInto::try_into)
|
||||
.collect::<Result<Vec<Val>, _>>()
|
||||
.expect("Argument conversion failed")
|
||||
.expect("Arguments conversion failed")
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
match func.inner.call(¶ms) {
|
||||
Ok(wasm_results) => {
|
||||
*results = wasm_results
|
||||
let vals = wasm_results
|
||||
.into_iter()
|
||||
.map(TryInto::try_into)
|
||||
.collect::<Result<Vec<wasm_val_t>, _>>()
|
||||
.expect("Argument conversion failed")
|
||||
.into();
|
||||
.expect("Results conversion failed");
|
||||
|
||||
// `results` is an uninitialized vector. Set a new value.
|
||||
if results.is_uninitialized() {
|
||||
*results = vals.into();
|
||||
}
|
||||
// `results` is an initialized but empty vector. Fill it
|
||||
// item per item.
|
||||
else {
|
||||
let slice = results
|
||||
.into_slice_mut()
|
||||
.expect("`wasm_func_call`, results' size is greater than 0 but data is NULL");
|
||||
|
||||
for (result, value) in slice.iter_mut().zip(vals.iter()) {
|
||||
(*result).kind = value.kind;
|
||||
(*result).of = value.of;
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
@@ -195,6 +221,8 @@ pub unsafe extern "C" fn wasm_func_result_arity(func: &wasm_func_t) -> usize {
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_func_type(func: &wasm_func_t) -> Box<wasm_functype_t> {
|
||||
Box::new(wasm_functype_t::new(func.inner.ty().clone()))
|
||||
pub extern "C" fn wasm_func_type(func: Option<&wasm_func_t>) -> Option<Box<wasm_functype_t>> {
|
||||
let func = func?;
|
||||
|
||||
Some(Box::new(wasm_functype_t::new(func.inner.ty().clone())))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user