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,4 +1,6 @@
|
||||
use crate::{handle_result, wasmtime_error_t};
|
||||
use crate::{wasm_extern_t, wasm_globaltype_t, wasm_store_t, wasm_val_t, ExternHost};
|
||||
use std::ptr;
|
||||
use wasmtime::{Global, HostRef};
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -35,13 +37,31 @@ pub extern "C" fn wasm_global_new(
|
||||
gt: &wasm_globaltype_t,
|
||||
val: &wasm_val_t,
|
||||
) -> Option<Box<wasm_global_t>> {
|
||||
let global =
|
||||
HostRef::new(Global::new(&store.store.borrow(), gt.ty().ty.clone(), val.val()).ok()?);
|
||||
Some(Box::new(wasm_global_t {
|
||||
ext: wasm_extern_t {
|
||||
which: ExternHost::Global(global),
|
||||
},
|
||||
}))
|
||||
let mut global = ptr::null_mut();
|
||||
match wasmtime_global_new(store, gt, val, &mut global) {
|
||||
Some(_err) => None,
|
||||
None => {
|
||||
assert!(!global.is_null());
|
||||
Some(unsafe { Box::from_raw(global) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_global_new(
|
||||
store: &wasm_store_t,
|
||||
gt: &wasm_globaltype_t,
|
||||
val: &wasm_val_t,
|
||||
ret: &mut *mut wasm_global_t,
|
||||
) -> Option<Box<wasmtime_error_t>> {
|
||||
let global = Global::new(&store.store.borrow(), gt.ty().ty.clone(), val.val());
|
||||
handle_result(global, |global| {
|
||||
*ret = Box::into_raw(Box::new(wasm_global_t {
|
||||
ext: wasm_extern_t {
|
||||
which: ExternHost::Global(HostRef::new(global)),
|
||||
},
|
||||
}));
|
||||
})
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -66,3 +86,11 @@ pub extern "C" fn wasm_global_set(g: &wasm_global_t, val: &wasm_val_t) {
|
||||
// FIXME(WebAssembly/wasm-c-api#131) should communicate the error here
|
||||
drop(result);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_global_set(
|
||||
g: &wasm_global_t,
|
||||
val: &wasm_val_t,
|
||||
) -> Option<Box<wasmtime_error_t>> {
|
||||
handle_result(g.global().borrow().set(val.val()), |()| {})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user