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,18 +1,20 @@
use crate::{bad_utf8, handle_result, wasmtime_error_t};
use crate::{wasm_extern_t, wasm_store_t};
use crate::{wasm_func_t, wasm_instance_t, wasm_module_t, wasm_name_t, wasm_trap_t};
use crate::{
bad_utf8, handle_result, wasm_engine_t, wasm_trap_t, wasmtime_error_t, wasmtime_extern_t,
wasmtime_module_t, CStoreContextMut,
};
use std::mem::MaybeUninit;
use std::str;
use wasmtime::Linker;
use wasmtime::{Func, Instance, Linker};
#[repr(C)]
pub struct wasmtime_linker_t {
linker: Linker,
linker: Linker<crate::StoreData>,
}
#[no_mangle]
pub extern "C" fn wasmtime_linker_new(store: &wasm_store_t) -> Box<wasmtime_linker_t> {
pub extern "C" fn wasmtime_linker_new(engine: &wasm_engine_t) -> Box<wasmtime_linker_t> {
Box::new(wasmtime_linker_t {
linker: Linker::new(&store.store),
linker: Linker::new(&engine.engine),
})
}
@@ -27,23 +29,28 @@ pub extern "C" fn wasmtime_linker_allow_shadowing(
#[no_mangle]
pub extern "C" fn wasmtime_linker_delete(_linker: Box<wasmtime_linker_t>) {}
macro_rules! to_str {
($ptr:expr, $len:expr) => {
match str::from_utf8(crate::slice_from_raw_parts($ptr, $len)) {
Ok(s) => s,
Err(_) => return bad_utf8(),
}
};
}
#[no_mangle]
pub extern "C" fn wasmtime_linker_define(
pub unsafe extern "C" fn wasmtime_linker_define(
linker: &mut wasmtime_linker_t,
module: &wasm_name_t,
name: &wasm_name_t,
item: &wasm_extern_t,
module: *const u8,
module_len: usize,
name: *const u8,
name_len: usize,
item: &wasmtime_extern_t,
) -> Option<Box<wasmtime_error_t>> {
let linker = &mut linker.linker;
let module = match str::from_utf8(module.as_slice()) {
Ok(s) => s,
Err(_) => return bad_utf8(),
};
let name = match str::from_utf8(name.as_slice()) {
Ok(s) => s,
Err(_) => return bad_utf8(),
};
let item = item.which.clone();
let module = to_str!(module, module_len);
let name = to_str!(name, name_len);
let item = item.to_extern();
handle_result(linker.define(module, name, item), |_linker| ())
}
@@ -51,87 +58,92 @@ pub extern "C" fn wasmtime_linker_define(
#[no_mangle]
pub extern "C" fn wasmtime_linker_define_wasi(
linker: &mut wasmtime_linker_t,
instance: &crate::wasi_instance_t,
) -> Option<Box<wasmtime_error_t>> {
let linker = &mut linker.linker;
handle_result(instance.add_to_linker(linker), |_linker| ())
handle_result(
wasmtime_wasi::add_to_linker(&mut linker.linker, |cx| cx.wasi.as_mut().unwrap()),
|_linker| (),
)
}
#[no_mangle]
pub extern "C" fn wasmtime_linker_define_instance(
pub unsafe extern "C" fn wasmtime_linker_define_instance(
linker: &mut wasmtime_linker_t,
name: &wasm_name_t,
instance: &wasm_instance_t,
store: CStoreContextMut<'_>,
name: *const u8,
name_len: usize,
instance: &Instance,
) -> Option<Box<wasmtime_error_t>> {
let linker = &mut linker.linker;
let name = match str::from_utf8(name.as_slice()) {
Ok(s) => s,
Err(_) => return bad_utf8(),
};
handle_result(linker.instance(name, instance.instance()), |_linker| ())
let name = to_str!(name, name_len);
handle_result(linker.instance(store, name, *instance), |_linker| ())
}
#[no_mangle]
pub extern "C" fn wasmtime_linker_instantiate(
linker: &wasmtime_linker_t,
module: &wasm_module_t,
instance_ptr: &mut *mut wasm_instance_t,
store: CStoreContextMut<'_>,
module: &wasmtime_module_t,
instance_ptr: &mut Instance,
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
let result = linker.linker.instantiate(module.module());
let result = linker.linker.instantiate(store, &module.module);
super::instance::handle_instantiate(result, instance_ptr, trap_ptr)
}
#[no_mangle]
pub extern "C" fn wasmtime_linker_module(
pub unsafe extern "C" fn wasmtime_linker_module(
linker: &mut wasmtime_linker_t,
name: &wasm_name_t,
module: &wasm_module_t,
store: CStoreContextMut<'_>,
name: *const u8,
name_len: usize,
module: &wasmtime_module_t,
) -> Option<Box<wasmtime_error_t>> {
let linker = &mut linker.linker;
let name = match str::from_utf8(name.as_slice()) {
Ok(s) => s,
Err(_) => return bad_utf8(),
};
handle_result(linker.module(name, module.module()), |_linker| ())
let name = to_str!(name, name_len);
handle_result(linker.module(store, name, &module.module), |_linker| ())
}
#[no_mangle]
pub extern "C" fn wasmtime_linker_get_default(
pub unsafe extern "C" fn wasmtime_linker_get_default(
linker: &wasmtime_linker_t,
name: &wasm_name_t,
func: &mut *mut wasm_func_t,
store: CStoreContextMut<'_>,
name: *const u8,
name_len: usize,
func: &mut Func,
) -> Option<Box<wasmtime_error_t>> {
let linker = &linker.linker;
let name = match str::from_utf8(name.as_slice()) {
Ok(s) => s,
Err(_) => return bad_utf8(),
};
handle_result(linker.get_default(name), |f| {
*func = Box::into_raw(Box::new(f.into()))
})
let name = to_str!(name, name_len);
handle_result(linker.get_default(store, name), |f| *func = f)
}
#[no_mangle]
pub extern "C" fn wasmtime_linker_get_one_by_name(
pub unsafe extern "C" fn wasmtime_linker_get(
linker: &wasmtime_linker_t,
module: &wasm_name_t,
name: Option<&wasm_name_t>,
item_ptr: &mut *mut wasm_extern_t,
) -> Option<Box<wasmtime_error_t>> {
store: CStoreContextMut<'_>,
module: *const u8,
module_len: usize,
name: *const u8,
name_len: usize,
item_ptr: &mut MaybeUninit<wasmtime_extern_t>,
) -> bool {
let linker = &linker.linker;
let module = match str::from_utf8(module.as_slice()) {
let module = match str::from_utf8(crate::slice_from_raw_parts(module, module_len)) {
Ok(s) => s,
Err(_) => return bad_utf8(),
Err(_) => return false,
};
let name = match name {
Some(name) => match str::from_utf8(name.as_slice()) {
let name = if name.is_null() {
None
} else {
match str::from_utf8(crate::slice_from_raw_parts(name, name_len)) {
Ok(s) => Some(s),
Err(_) => return bad_utf8(),
},
None => None,
Err(_) => return false,
}
};
handle_result(linker.get_one_by_name(module, name), |which| {
*item_ptr = Box::into_raw(Box::new(wasm_extern_t { which }))
})
match linker.get(store, module, name) {
Some(which) => {
crate::initialize(item_ptr, which.into());
true
}
None => false,
}
}