Add Wasmtime-specific C API functions to return errors (#1467)
* Add Wasmtime-specific C API functions to return errors This commit adds new `wasmtime_*` symbols to the C API, many of which mirror the existing counterparts in the `wasm.h` header. These APIs are enhanced in a number of respects: * Detailed error information is now available through a `wasmtime_error_t`. Currently this only exposes one function which is to extract a string version of the error. * There is a distinction now between traps and errors during instantiation and function calling. Traps only happen if wasm traps, and errors can happen for things like runtime type errors when interacting with the API. * APIs have improved safety with respect to embedders where the lengths of arrays are now taken as explicit parameters rather than assumed from other parameters. * Handle trap updates * Update C examples * Fix memory.c compile on MSVC * Update test assertions * Refactor C slightly * Bare-bones .NET update * Remove bogus nul handling
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::{wasm_extern_t, wasm_extern_vec_t, wasm_module_t, wasm_trap_t};
|
||||
use crate::{wasm_store_t, ExternHost};
|
||||
use crate::{wasm_store_t, wasmtime_error_t, ExternHost};
|
||||
use anyhow::Result;
|
||||
use std::cell::RefCell;
|
||||
use std::ptr;
|
||||
@@ -15,7 +15,7 @@ pub struct wasm_instance_t {
|
||||
wasmtime_c_api_macros::declare_ref!(wasm_instance_t);
|
||||
|
||||
impl wasm_instance_t {
|
||||
fn new(instance: Instance) -> wasm_instance_t {
|
||||
pub(crate) fn new(instance: Instance) -> wasm_instance_t {
|
||||
wasm_instance_t {
|
||||
instance: HostRef::new(instance),
|
||||
exports_cache: RefCell::new(None),
|
||||
@@ -30,59 +30,111 @@ impl wasm_instance_t {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_instance_new(
|
||||
store: &wasm_store_t,
|
||||
module: &wasm_module_t,
|
||||
wasm_module: &wasm_module_t,
|
||||
imports: *const Box<wasm_extern_t>,
|
||||
result: Option<&mut *mut wasm_trap_t>,
|
||||
) -> Option<Box<wasm_instance_t>> {
|
||||
let mut externs: Vec<Extern> = Vec::with_capacity((*module).imports.len());
|
||||
for i in 0..(*module).imports.len() {
|
||||
let import = &*imports.add(i);
|
||||
externs.push(match &import.which {
|
||||
ExternHost::Func(e) => Extern::Func(e.borrow().clone()),
|
||||
ExternHost::Table(e) => Extern::Table(e.borrow().clone()),
|
||||
ExternHost::Global(e) => Extern::Global(e.borrow().clone()),
|
||||
ExternHost::Memory(e) => Extern::Memory(e.borrow().clone()),
|
||||
});
|
||||
}
|
||||
let store = &(*store).store.borrow();
|
||||
let module = &(*module).module.borrow();
|
||||
let store = &store.store.borrow();
|
||||
let module = &wasm_module.module.borrow();
|
||||
if !Store::same(&store, module.store()) {
|
||||
if let Some(result) = result {
|
||||
let trap = Trap::new("wasm_store_t must match store in wasm_module_t");
|
||||
let trap = Box::new(wasm_trap_t {
|
||||
trap: HostRef::new(trap),
|
||||
});
|
||||
let trap = Box::new(wasm_trap_t::new(trap));
|
||||
*result = Box::into_raw(trap);
|
||||
}
|
||||
return None;
|
||||
}
|
||||
handle_instantiate(Instance::new(module, &externs), result)
|
||||
let mut instance = ptr::null_mut();
|
||||
let mut trap = ptr::null_mut();
|
||||
let err = wasmtime_instance_new(
|
||||
wasm_module,
|
||||
imports,
|
||||
wasm_module.imports.len(),
|
||||
&mut instance,
|
||||
&mut trap,
|
||||
);
|
||||
match err {
|
||||
Some(err) => {
|
||||
assert!(trap.is_null());
|
||||
assert!(instance.is_null());
|
||||
if let Some(result) = result {
|
||||
*result = Box::into_raw(err.to_trap());
|
||||
}
|
||||
None
|
||||
}
|
||||
None => {
|
||||
if instance.is_null() {
|
||||
assert!(!trap.is_null());
|
||||
if let Some(result) = result {
|
||||
*result = trap;
|
||||
} else {
|
||||
drop(Box::from_raw(trap))
|
||||
}
|
||||
None
|
||||
} else {
|
||||
assert!(trap.is_null());
|
||||
Some(Box::from_raw(instance))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_instance_new(
|
||||
module: &wasm_module_t,
|
||||
imports: *const Box<wasm_extern_t>,
|
||||
num_imports: usize,
|
||||
instance_ptr: &mut *mut wasm_instance_t,
|
||||
trap_ptr: &mut *mut wasm_trap_t,
|
||||
) -> Option<Box<wasmtime_error_t>> {
|
||||
_wasmtime_instance_new(
|
||||
module,
|
||||
std::slice::from_raw_parts(imports, num_imports),
|
||||
instance_ptr,
|
||||
trap_ptr,
|
||||
)
|
||||
}
|
||||
|
||||
fn _wasmtime_instance_new(
|
||||
module: &wasm_module_t,
|
||||
imports: &[Box<wasm_extern_t>],
|
||||
instance_ptr: &mut *mut wasm_instance_t,
|
||||
trap_ptr: &mut *mut wasm_trap_t,
|
||||
) -> Option<Box<wasmtime_error_t>> {
|
||||
let imports = imports
|
||||
.iter()
|
||||
.map(|import| match &import.which {
|
||||
ExternHost::Func(e) => Extern::Func(e.borrow().clone()),
|
||||
ExternHost::Table(e) => Extern::Table(e.borrow().clone()),
|
||||
ExternHost::Global(e) => Extern::Global(e.borrow().clone()),
|
||||
ExternHost::Memory(e) => Extern::Memory(e.borrow().clone()),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let module = &module.module.borrow();
|
||||
handle_instantiate(Instance::new(module, &imports), instance_ptr, trap_ptr)
|
||||
}
|
||||
|
||||
pub fn handle_instantiate(
|
||||
instance: Result<Instance>,
|
||||
result: Option<&mut *mut wasm_trap_t>,
|
||||
) -> Option<Box<wasm_instance_t>> {
|
||||
instance_ptr: &mut *mut wasm_instance_t,
|
||||
trap_ptr: &mut *mut wasm_trap_t,
|
||||
) -> Option<Box<wasmtime_error_t>> {
|
||||
fn write<T>(ptr: &mut *mut T, val: T) {
|
||||
*ptr = Box::into_raw(Box::new(val))
|
||||
}
|
||||
|
||||
match instance {
|
||||
Ok(instance) => {
|
||||
if let Some(result) = result {
|
||||
*result = ptr::null_mut();
|
||||
}
|
||||
Some(Box::new(wasm_instance_t::new(instance)))
|
||||
}
|
||||
Err(trap) => {
|
||||
if let Some(result) = result {
|
||||
let trap = match trap.downcast::<Trap>() {
|
||||
Ok(trap) => trap,
|
||||
Err(e) => Trap::new(format!("{:?}", e)),
|
||||
};
|
||||
let trap = Box::new(wasm_trap_t {
|
||||
trap: HostRef::new(trap),
|
||||
});
|
||||
*result = Box::into_raw(trap);
|
||||
}
|
||||
write(instance_ptr, wasm_instance_t::new(instance));
|
||||
None
|
||||
}
|
||||
Err(e) => match e.downcast::<Trap>() {
|
||||
Ok(trap) => {
|
||||
write(trap_ptr, wasm_trap_t::new(trap));
|
||||
None
|
||||
}
|
||||
Err(e) => Some(Box::new(e.into())),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user