Implement RFC 11: Redesigning Wasmtime's APIs (#2897)

Implement Wasmtime's new API as designed by RFC 11. This is quite a large commit which has had lots of discussion externally, so for more information it's best to read the RFC thread and the PR thread.
This commit is contained in:
Alex Crichton
2021-06-03 09:10:53 -05:00
committed by GitHub
parent a5a28b1c5b
commit 7a1b7cdf92
233 changed files with 13349 additions and 11997 deletions

View File

@@ -1,9 +1,8 @@
use crate::{
handle_result, wasm_byte_vec_t, wasm_engine_t, wasm_exporttype_t, wasm_exporttype_vec_t,
wasm_extern_t, wasm_importtype_t, wasm_importtype_vec_t, wasm_moduletype_t, wasm_store_t,
wasmtime_error_t,
wasm_extern_t, wasm_importtype_t, wasm_importtype_vec_t, wasm_store_t, wasmtime_error_t,
wasmtime_moduletype_t, StoreRef,
};
use std::ptr;
use wasmtime::{Engine, Extern, Module};
#[derive(Clone)]
@@ -15,9 +14,10 @@ pub struct wasm_module_t {
wasmtime_c_api_macros::declare_ref!(wasm_module_t);
impl wasm_module_t {
pub(crate) fn new(module: Module) -> wasm_module_t {
pub(crate) fn new(store: StoreRef, module: Module) -> wasm_module_t {
wasm_module_t {
ext: wasm_extern_t {
store: store,
which: module.into(),
},
}
@@ -47,48 +47,22 @@ pub struct wasm_shared_module_t {
wasmtime_c_api_macros::declare_own!(wasm_shared_module_t);
#[no_mangle]
pub extern "C" fn wasm_module_new(
store: &wasm_store_t,
pub unsafe extern "C" fn wasm_module_new(
store: &mut wasm_store_t,
binary: &wasm_byte_vec_t,
) -> Option<Box<wasm_module_t>> {
let mut ret = ptr::null_mut();
let engine = wasm_engine_t {
engine: store.store.engine().clone(),
};
match wasmtime_module_new(&engine, binary, &mut ret) {
Some(_err) => None,
None => {
assert!(!ret.is_null());
Some(unsafe { Box::from_raw(ret) })
}
match Module::from_binary(store.store.context().engine(), binary.as_slice()) {
Ok(module) => Some(Box::new(wasm_module_t::new(store.store.clone(), module))),
Err(_) => None,
}
}
#[no_mangle]
pub extern "C" fn wasmtime_module_new(
engine: &wasm_engine_t,
pub unsafe extern "C" fn wasm_module_validate(
store: &mut wasm_store_t,
binary: &wasm_byte_vec_t,
ret: &mut *mut wasm_module_t,
) -> Option<Box<wasmtime_error_t>> {
let binary = binary.as_slice();
handle_result(Module::from_binary(&engine.engine, binary), |module| {
let module = Box::new(wasm_module_t::new(module));
*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();
handle_result(Module::validate(store.store.engine(), binary), |()| {})
) -> bool {
Module::validate(store.store.context().engine(), binary.as_slice()).is_ok()
}
#[no_mangle]
@@ -135,66 +109,96 @@ pub extern "C" fn wasm_module_share(module: &wasm_module_t) -> Box<wasm_shared_m
}
#[no_mangle]
pub extern "C" fn wasm_module_obtain(
store: &wasm_store_t,
pub unsafe extern "C" fn wasm_module_obtain(
store: &mut wasm_store_t,
shared_module: &wasm_shared_module_t,
) -> Option<Box<wasm_module_t>> {
let module = shared_module.module.clone();
if !Engine::same(store.store.engine(), module.engine()) {
return None;
if Engine::same(store.store.context().engine(), module.engine()) {
Some(Box::new(wasm_module_t::new(store.store.clone(), module)))
} else {
None
}
Some(Box::new(wasm_module_t::new(module)))
}
#[no_mangle]
pub extern "C" fn wasm_module_serialize(module: &wasm_module_t, ret: &mut wasm_byte_vec_t) {
drop(wasmtime_module_serialize(module, ret));
}
#[no_mangle]
pub extern "C" fn wasm_module_deserialize(
store: &wasm_store_t,
binary: &wasm_byte_vec_t,
) -> Option<Box<wasm_module_t>> {
let mut ret = ptr::null_mut();
let engine = wasm_engine_t {
engine: store.store.engine().clone(),
};
match wasmtime_module_deserialize(&engine, binary, &mut ret) {
Some(_err) => None,
None => {
assert!(!ret.is_null());
Some(unsafe { Box::from_raw(ret) })
}
if let Ok(buf) = module.module().serialize() {
ret.set_buffer(buf);
}
}
#[no_mangle]
pub extern "C" fn wasmtime_module_serialize(
module: &wasm_module_t,
ret: &mut wasm_byte_vec_t,
) -> Option<Box<wasmtime_error_t>> {
handle_result(module.module().serialize(), |buf| {
ret.set_buffer(buf);
})
pub unsafe extern "C" fn wasm_module_deserialize(
store: &mut wasm_store_t,
binary: &wasm_byte_vec_t,
) -> Option<Box<wasm_module_t>> {
match Module::deserialize(store.store.context().engine(), binary.as_slice()) {
Ok(module) => Some(Box::new(wasm_module_t::new(store.store.clone(), module))),
Err(_) => None,
}
}
#[derive(Clone)]
pub struct wasmtime_module_t {
pub(crate) module: Module,
}
#[no_mangle]
pub extern "C" fn wasmtime_module_deserialize(
pub unsafe extern "C" fn wasmtime_module_new(
engine: &wasm_engine_t,
binary: &wasm_byte_vec_t,
ret: &mut *mut wasm_module_t,
wasm: *const u8,
len: usize,
out: &mut *mut wasmtime_module_t,
) -> Option<Box<wasmtime_error_t>> {
handle_result(
unsafe { Module::deserialize(&engine.engine, binary.as_slice()) },
Module::from_binary(&engine.engine, crate::slice_from_raw_parts(wasm, len)),
|module| {
let module = Box::new(wasm_module_t::new(module));
*ret = Box::into_raw(module);
*out = Box::into_raw(Box::new(wasmtime_module_t { module }));
},
)
}
#[no_mangle]
pub extern "C" fn wasm_module_type(f: &wasm_module_t) -> Box<wasm_moduletype_t> {
Box::new(wasm_moduletype_t::new(f.module().ty()))
pub extern "C" fn wasmtime_module_delete(_module: Box<wasmtime_module_t>) {}
#[no_mangle]
pub extern "C" fn wasmtime_module_clone(module: &wasmtime_module_t) -> Box<wasmtime_module_t> {
Box::new(module.clone())
}
#[no_mangle]
pub unsafe extern "C" fn wasmtime_module_validate(
engine: &wasm_engine_t,
wasm: *const u8,
len: usize,
) -> Option<Box<wasmtime_error_t>> {
let binary = crate::slice_from_raw_parts(wasm, len);
handle_result(Module::validate(&engine.engine, binary), |()| {})
}
#[no_mangle]
pub extern "C" fn wasmtime_module_type(m: &wasmtime_module_t) -> Box<wasmtime_moduletype_t> {
Box::new(wasmtime_moduletype_t::new(m.module.ty()))
}
#[no_mangle]
pub extern "C" fn wasmtime_module_serialize(
module: &wasmtime_module_t,
ret: &mut wasm_byte_vec_t,
) -> Option<Box<wasmtime_error_t>> {
handle_result(module.module.serialize(), |buf| ret.set_buffer(buf))
}
#[no_mangle]
pub unsafe extern "C" fn wasmtime_module_deserialize(
engine: &wasm_engine_t,
bytes: *const u8,
len: usize,
out: &mut *mut wasmtime_module_t,
) -> Option<Box<wasmtime_error_t>> {
let bytes = crate::slice_from_raw_parts(bytes, len);
handle_result(Module::deserialize(&engine.engine, bytes), |module| {
*out = Box::into_raw(Box::new(wasmtime_module_t { module }));
})
}