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,7 @@
|
||||
use crate::{handle_result, wasmtime_error_t};
|
||||
use crate::{wasm_byte_vec_t, wasm_exporttype_vec_t, wasm_importtype_vec_t};
|
||||
use crate::{wasm_exporttype_t, wasm_importtype_t, wasm_store_t};
|
||||
use std::ptr;
|
||||
use wasmtime::{HostRef, Module};
|
||||
|
||||
#[repr(C)]
|
||||
@@ -23,31 +25,57 @@ pub extern "C" fn wasm_module_new(
|
||||
store: &wasm_store_t,
|
||||
binary: &wasm_byte_vec_t,
|
||||
) -> Option<Box<wasm_module_t>> {
|
||||
let mut ret = ptr::null_mut();
|
||||
match wasmtime_module_new(store, binary, &mut ret) {
|
||||
Some(_err) => None,
|
||||
None => {
|
||||
assert!(!ret.is_null());
|
||||
Some(unsafe { Box::from_raw(ret) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_module_new(
|
||||
store: &wasm_store_t,
|
||||
binary: &wasm_byte_vec_t,
|
||||
ret: &mut *mut wasm_module_t,
|
||||
) -> Option<Box<wasmtime_error_t>> {
|
||||
let binary = binary.as_slice();
|
||||
let store = &store.store.borrow();
|
||||
let module = Module::from_binary(store, binary).ok()?;
|
||||
let imports = module
|
||||
.imports()
|
||||
.iter()
|
||||
.map(|i| wasm_importtype_t::new(i.clone()))
|
||||
.collect::<Vec<_>>();
|
||||
let exports = module
|
||||
.exports()
|
||||
.iter()
|
||||
.map(|e| wasm_exporttype_t::new(e.clone()))
|
||||
.collect::<Vec<_>>();
|
||||
Some(Box::new(wasm_module_t {
|
||||
module: HostRef::new(module),
|
||||
imports,
|
||||
exports,
|
||||
}))
|
||||
handle_result(Module::from_binary(store, binary), |module| {
|
||||
let imports = module
|
||||
.imports()
|
||||
.iter()
|
||||
.map(|i| wasm_importtype_t::new(i.clone()))
|
||||
.collect::<Vec<_>>();
|
||||
let exports = module
|
||||
.exports()
|
||||
.iter()
|
||||
.map(|e| wasm_exporttype_t::new(e.clone()))
|
||||
.collect::<Vec<_>>();
|
||||
let module = Box::new(wasm_module_t {
|
||||
module: HostRef::new(module),
|
||||
imports,
|
||||
exports,
|
||||
});
|
||||
*ret = Box::into_raw(module);
|
||||
})
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_module_validate(store: &wasm_store_t, binary: &wasm_byte_vec_t) -> bool {
|
||||
wasmtime_module_validate(store, binary).is_none()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_module_validate(
|
||||
store: &wasm_store_t,
|
||||
binary: &wasm_byte_vec_t,
|
||||
) -> Option<Box<wasmtime_error_t>> {
|
||||
let binary = binary.as_slice();
|
||||
let store = &store.store.borrow();
|
||||
Module::validate(store, binary).is_ok()
|
||||
handle_result(Module::validate(store, binary), |()| {})
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
Reference in New Issue
Block a user