Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update WebAssembly C API submodule to latest commit. #2579

Merged
merged 2 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions crates/c-api/include/doc-wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@
*
* \fn wasm_name_new_new_uninitialized
* \brief Convenience alias
*
* \fn wasm_name_new_from_string
* \brief Create a new name from a C string.
*
* \fn wasm_name_new_from_string_nt
* \brief Create a new name from a C string with null terminator.
*
* \fn wasm_name_copy
* \brief Convenience alias
Expand Down Expand Up @@ -407,7 +413,7 @@
*
* See #wasm_byte_vec_delete for more information.
*
* \fn own wasm_valtype_t* wasm_valtype_copy(wasm_valtype_t *)
* \fn own wasm_valtype_t* wasm_valtype_copy(const wasm_valtype_t *)
* \brief Creates a new value which matches the provided one.
*
* The caller is responsible for deleting the returned value.
Expand Down Expand Up @@ -477,7 +483,7 @@
*
* See #wasm_byte_vec_delete for more information.
*
* \fn own wasm_functype_t* wasm_functype_copy(wasm_functype_t *)
* \fn own wasm_functype_t* wasm_functype_copy(const wasm_functype_t *)
* \brief Creates a new value which matches the provided one.
*
* The caller is responsible for deleting the returned value.
Expand Down Expand Up @@ -548,7 +554,7 @@
*
* See #wasm_byte_vec_delete for more information.
*
* \fn own wasm_globaltype_t* wasm_globaltype_copy(wasm_globaltype_t *)
* \fn own wasm_globaltype_t* wasm_globaltype_copy(const wasm_globaltype_t *)
* \brief Creates a new value which matches the provided one.
*
* The caller is responsible for deleting the returned value.
Expand Down Expand Up @@ -625,7 +631,7 @@
*
* See #wasm_byte_vec_delete for more information.
*
* \fn own wasm_tabletype_t* wasm_tabletype_copy(wasm_tabletype_t *)
* \fn own wasm_tabletype_t* wasm_tabletype_copy(const wasm_tabletype_t *)
* \brief Creates a new value which matches the provided one.
*
* The caller is responsible for deleting the returned value.
Expand Down Expand Up @@ -711,7 +717,7 @@
*
* See #wasm_byte_vec_delete for more information.
*
* \fn own wasm_memorytype_t* wasm_memorytype_copy(wasm_memorytype_t *)
* \fn own wasm_memorytype_t* wasm_memorytype_copy(const wasm_memorytype_t *)
* \brief Creates a new value which matches the provided one.
*
* The caller is responsible for deleting the returned value.
Expand Down Expand Up @@ -780,7 +786,7 @@
*
* See #wasm_byte_vec_delete for more information.
*
* \fn own wasm_externtype_t* wasm_externtype_copy(wasm_externtype_t *)
* \fn own wasm_externtype_t* wasm_externtype_copy(const wasm_externtype_t *)
* \brief Creates a new value which matches the provided one.
*
* The caller is responsible for deleting the returned value.
Expand Down Expand Up @@ -957,7 +963,7 @@
*
* See #wasm_byte_vec_delete for more information.
*
* \fn own wasm_importtype_t* wasm_importtype_copy(wasm_importtype_t *)
* \fn own wasm_importtype_t* wasm_importtype_copy(const wasm_importtype_t *)
* \brief Creates a new value which matches the provided one.
*
* The caller is responsible for deleting the returned value.
Expand Down Expand Up @@ -1038,7 +1044,7 @@
*
* See #wasm_byte_vec_delete for more information.
*
* \fn own wasm_exporttype_t* wasm_exporttype_copy(wasm_exporttype_t *)
* \fn own wasm_exporttype_t* wasm_exporttype_copy(const wasm_exporttype_t *)
* \brief Creates a new value which matches the provided one.
*
* The caller is responsible for deleting the returned value.
Expand Down Expand Up @@ -1520,8 +1526,6 @@
* and types of results as the original type signature. It is undefined behavior
* to return other types or different numbers of values.
*
* This function takes ownership of all of the parameters given. It's expected
* that the caller will invoke `wasm_val_delete` for each one provided.
* Ownership of the results and the trap returned, if any, is passed to the
* caller of this function.
*
Expand Down Expand Up @@ -1608,7 +1612,7 @@
* \fn size_t wasm_func_result_arity(const wasm_func_t *);
* \brief Returns the number of results returned by this function.
*
* \fn own wasm_trap_t *wasm_func_call(const wasm_func_t *, const wasm_val_t args[], const wasm_val_t results[]);
* \fn own wasm_trap_t *wasm_func_call(const wasm_func_t *, const wasm_val_vec_t *args, wasm_val_vec_t *results);
* \brief Calls the provided function with the arguments given.
*
* This function is used to call WebAssembly from the host. The parameter array
Expand Down Expand Up @@ -2164,7 +2168,7 @@
* \fn wasm_ref_as_instance_const(const wasm_ref_t *);
* \brief Unimplemented in Wasmtime, aborts the process if called.
*
* \fn own wasm_instance_t *wasm_instance_new(wasm_store_t *, const wasm_module_t *, const wasm_extern_t *const[], wasm_trap_t **);
* \fn own wasm_instance_t *wasm_instance_new(wasm_store_t *, const wasm_module_t *, const wasm_extern_vec_t *, wasm_trap_t **);
* \brief Instantiates a module with the provided imports.
*
* This function will instantiate the provided #wasm_module_t into the provided
Expand Down Expand Up @@ -2194,3 +2198,29 @@
* the same length as #wasm_module_exports called on the original module. Each
* element is 1:1 matched with the elements in the list of #wasm_module_exports.
*/

/**
* \def WASM_EMPTY_VEC
* \brief Used to initialize an empty vector type.
*
* \def WASM_ARRAY_VEC
* \brief Used to initialize a vector type from a C array.
*
* \def WASM_I32_VAL
* \brief Used to initialize a 32-bit integer wasm_val_t value.
*
* \def WASM_I64_VAL
* \brief Used to initialize a 64-bit integer wasm_val_t value.
*
* \def WASM_F32_VAL
* \brief Used to initialize a 32-bit floating point wasm_val_t value.
*
* \def WASM_F64_VAL
* \brief Used to initialize a 64-bit floating point wasm_val_t value.
*
* \def WASM_REF_VAL
* \brief Used to initialize an externref wasm_val_t value.
*
* \def WASM_INIT_VAL
* \brief Used to initialize a null externref wasm_val_t value.
*/
15 changes: 5 additions & 10 deletions crates/c-api/include/wasmtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,6 @@ WASM_API_EXTERN const wasm_name_t *wasmtime_frame_module_name(const wasm_frame_t
*
* This function is similar to #wasm_func_call, but with a few tweaks:
*
* * `args` and `results` have a size parameter saying how big the arrays are
* * An error *and* a trap can be returned
* * Errors are returned if `args` have the wrong types, if the args/results
* arrays have the wrong lengths, or if values come from the wrong store.
Expand All @@ -697,10 +696,8 @@ WASM_API_EXTERN const wasm_name_t *wasmtime_frame_module_name(const wasm_frame_t
*/
WASM_API_EXTERN own wasmtime_error_t *wasmtime_func_call(
wasm_func_t *func,
const wasm_val_t *args,
size_t num_args,
wasm_val_t *results,
size_t num_results,
const wasm_val_vec_t *args,
wasm_val_vec_t *results,
own wasm_trap_t **trap
);

Expand Down Expand Up @@ -741,7 +738,6 @@ WASM_API_EXTERN own wasmtime_error_t *wasmtime_global_set(
* This function is similar to #wasm_instance_new, but with a few tweaks:
*
* * An error message can be returned from this function.
* * The number of imports specified is passed as an argument
* * The `trap` pointer is required to not be NULL.
*
* The states of return values from this function are similar to
Expand All @@ -759,8 +755,7 @@ WASM_API_EXTERN own wasmtime_error_t *wasmtime_global_set(
WASM_API_EXTERN own wasmtime_error_t *wasmtime_instance_new(
wasm_store_t *store,
const wasm_module_t *module,
const wasm_extern_t* const imports[],
size_t num_imports,
const wasm_extern_vec_t* imports,
own wasm_instance_t **instance,
own wasm_trap_t **trap
);
Expand Down Expand Up @@ -1016,7 +1011,7 @@ WASM_API_EXTERN own wasmtime_error_t *wasmtime_module_deserialize(
*
* See #wasm_byte_vec_delete for more information.
*
* \fn own wasm_instancetype_t* wasm_instancetype_copy(wasm_instancetype_t *)
* \fn own wasm_instancetype_t* wasm_instancetype_copy(const wasm_instancetype_t *)
* \brief Creates a new value which matches the provided one.
*
* The caller is responsible for deleting the returned value.
Expand Down Expand Up @@ -1113,7 +1108,7 @@ WASM_API_EXTERN const wasm_instancetype_t* wasm_externtype_as_instancetype_const
*
* See #wasm_byte_vec_delete for more information.
*
* \fn own wasm_moduletype_t* wasm_moduletype_copy(wasm_moduletype_t *)
* \fn own wasm_moduletype_t* wasm_moduletype_copy(const wasm_moduletype_t *)
* \brief Creates a new value which matches the provided one.
*
* The caller is responsible for deleting the returned value.
Expand Down
57 changes: 29 additions & 28 deletions crates/c-api/src/func.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{wasm_extern_t, wasm_functype_t, wasm_store_t, wasm_val_t};
use crate::{wasm_extern_t, wasm_functype_t, wasm_store_t, wasm_val_t, wasm_val_vec_t};
use crate::{wasm_name_t, wasm_trap_t, wasmtime_error_t};
use anyhow::anyhow;
use std::ffi::c_void;
Expand All @@ -21,26 +21,28 @@ pub struct wasmtime_caller_t<'a> {
caller: Caller<'a>,
}

pub type wasm_func_callback_t =
extern "C" fn(args: *const wasm_val_t, results: *mut wasm_val_t) -> Option<Box<wasm_trap_t>>;
pub type wasm_func_callback_t = extern "C" fn(
args: *const wasm_val_vec_t,
results: *mut wasm_val_vec_t,
) -> Option<Box<wasm_trap_t>>;

pub type wasm_func_callback_with_env_t = extern "C" fn(
env: *mut std::ffi::c_void,
args: *const wasm_val_t,
results: *mut wasm_val_t,
args: *const wasm_val_vec_t,
results: *mut wasm_val_vec_t,
) -> Option<Box<wasm_trap_t>>;

pub type wasmtime_func_callback_t = extern "C" fn(
caller: *const wasmtime_caller_t,
args: *const wasm_val_t,
results: *mut wasm_val_t,
args: *const wasm_val_vec_t,
results: *mut wasm_val_vec_t,
) -> Option<Box<wasm_trap_t>>;

pub type wasmtime_func_callback_with_env_t = extern "C" fn(
caller: *const wasmtime_caller_t,
env: *mut std::ffi::c_void,
args: *const wasm_val_t,
results: *mut wasm_val_t,
args: *const wasm_val_vec_t,
results: *mut wasm_val_vec_t,
) -> Option<Box<wasm_trap_t>>;

struct Finalizer {
Expand Down Expand Up @@ -83,21 +85,25 @@ impl From<Func> for wasm_func_t {
fn create_function(
store: &wasm_store_t,
ty: &wasm_functype_t,
func: impl Fn(Caller<'_>, *const wasm_val_t, *mut wasm_val_t) -> Option<Box<wasm_trap_t>> + 'static,
func: impl Fn(Caller<'_>, *const wasm_val_vec_t, *mut wasm_val_vec_t) -> Option<Box<wasm_trap_t>>
+ 'static,
) -> Box<wasm_func_t> {
let store = &store.store;
let ty = ty.ty().ty.clone();
let func = Func::new(store, ty, move |caller, params, results| {
let params = params
let params: wasm_val_vec_t = params
.iter()
.cloned()
.map(|p| wasm_val_t::from_val(p))
.collect::<Vec<_>>();
let mut out_results = vec![wasm_val_t::default(); results.len()];
let out = func(caller, params.as_ptr(), out_results.as_mut_ptr());
.collect::<Vec<_>>()
.into();
let mut out_results: wasm_val_vec_t = vec![wasm_val_t::default(); results.len()].into();
let out = func(caller, &params, &mut out_results);
if let Some(trap) = out {
return Err(trap.trap.clone());
}

let out_results = out_results.as_slice();
for i in 0..results.len() {
results[i] = out_results[i].val();
}
Expand Down Expand Up @@ -164,17 +170,14 @@ pub extern "C" fn wasmtime_func_new_with_env(
#[no_mangle]
pub unsafe extern "C" fn wasm_func_call(
wasm_func: &wasm_func_t,
args: *const wasm_val_t,
results: *mut MaybeUninit<wasm_val_t>,
args: *const wasm_val_vec_t,
results: *mut wasm_val_vec_t,
) -> *mut wasm_trap_t {
let func = wasm_func.func();
let mut trap = ptr::null_mut();
let error = wasmtime_func_call(
let error = _wasmtime_func_call(
wasm_func,
args,
func.param_arity(),
results,
func.result_arity(),
(*args).as_slice(),
(*results).as_uninit_slice(),
&mut trap,
);
match error {
Expand All @@ -186,16 +189,14 @@ pub unsafe extern "C" fn wasm_func_call(
#[no_mangle]
pub unsafe extern "C" fn wasmtime_func_call(
func: &wasm_func_t,
args: *const wasm_val_t,
num_args: usize,
results: *mut MaybeUninit<wasm_val_t>,
num_results: usize,
args: *const wasm_val_vec_t,
results: *mut wasm_val_vec_t,
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
_wasmtime_func_call(
func,
std::slice::from_raw_parts(args, num_args),
std::slice::from_raw_parts_mut(results, num_results),
(*args).as_slice(),
(*results).as_uninit_slice(),
trap_ptr,
)
}
Expand Down
25 changes: 10 additions & 15 deletions crates/c-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@ impl wasm_instance_t {
pub unsafe extern "C" fn wasm_instance_new(
store: &wasm_store_t,
wasm_module: &wasm_module_t,
imports: *const Box<wasm_extern_t>,
imports: *const wasm_extern_vec_t,
result: Option<&mut *mut wasm_trap_t>,
) -> Option<Box<wasm_instance_t>> {
let mut instance = ptr::null_mut();
let mut trap = ptr::null_mut();
let err = wasmtime_instance_new(
let err = _wasmtime_instance_new(
store,
wasm_module,
imports,
wasm_module.module().imports().len(),
(*imports).as_slice(),
&mut instance,
&mut trap,
);
Expand Down Expand Up @@ -83,31 +82,27 @@ pub unsafe extern "C" fn wasm_instance_new(
pub unsafe extern "C" fn wasmtime_instance_new(
store: &wasm_store_t,
module: &wasm_module_t,
imports: *const Box<wasm_extern_t>,
num_imports: usize,
imports: *const wasm_extern_vec_t,
instance_ptr: &mut *mut wasm_instance_t,
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
_wasmtime_instance_new(
store,
module,
std::slice::from_raw_parts(imports, num_imports),
instance_ptr,
trap_ptr,
)
_wasmtime_instance_new(store, module, (*imports).as_slice(), instance_ptr, trap_ptr)
}

fn _wasmtime_instance_new(
store: &wasm_store_t,
module: &wasm_module_t,
imports: &[Box<wasm_extern_t>],
imports: &[Option<Box<wasm_extern_t>>],
instance_ptr: &mut *mut wasm_instance_t,
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
let store = &store.store;
let imports = imports
.iter()
.map(|import| import.which.clone())
.filter_map(|import| match import {
Some(i) => Some(i.which.clone()),
None => None,
})
.collect::<Vec<_>>();
handle_instantiate(
Instance::new(store, module.module(), &imports),
Expand Down
Loading