wasmtime-c-api: Remove nested options from wasm_ref_t representation

This commit is contained in:
Nick Fitzgerald
2020-07-10 11:13:46 -07:00
parent 742f8ee1fc
commit b9bb095e0f
2 changed files with 33 additions and 21 deletions

View File

@@ -7,7 +7,7 @@ use wasmtime::{ExternRef, Func, Val};
/// `*mut wasm_ref_t` is a reference type (`externref` or `funcref`), as seen by
/// the C API. Because we do not have a uniform representation for `funcref`s
/// and `externref`s, a `*mut wasm_ref_t` is morally a
/// `Option<Box<Either<Option<ExternRef>, Option<Func>>>>`.
/// `Option<Box<Either<ExternRef, Func>>>`.
///
/// A null `*mut wasm_ref_t` is either a null `funcref` or a null `externref`
/// depending on context (e.g. the table's element type that it is going into or
@@ -23,8 +23,8 @@ pub struct wasm_ref_t {
#[derive(Clone)]
pub(crate) enum WasmRefInner {
ExternRef(Option<ExternRef>),
FuncRef(Option<Func>),
ExternRef(ExternRef),
FuncRef(Func),
}
wasmtime_c_api_macros::declare_own!(wasm_ref_t);
@@ -35,40 +35,40 @@ pub(crate) fn ref_into_val(r: Option<Box<wasm_ref_t>>) -> Option<Val> {
let r = r?;
Some(match r.r {
WasmRefInner::ExternRef(x) => Val::ExternRef(x),
WasmRefInner::FuncRef(x) => Val::FuncRef(x),
WasmRefInner::ExternRef(x) => Val::ExternRef(Some(x)),
WasmRefInner::FuncRef(f) => Val::FuncRef(Some(f)),
})
}
pub(crate) fn ref_to_val(r: &wasm_ref_t) -> Val {
match &r.r {
WasmRefInner::ExternRef(x) => Val::ExternRef(x.clone()),
WasmRefInner::FuncRef(x) => Val::FuncRef(x.clone()),
WasmRefInner::ExternRef(x) => Val::ExternRef(Some(x.clone())),
WasmRefInner::FuncRef(f) => Val::FuncRef(Some(f.clone())),
}
}
pub(crate) fn val_into_ref(val: Val) -> Option<Box<wasm_ref_t>> {
match val {
Val::ExternRef(x) => Some(Box::new(wasm_ref_t {
Val::ExternRef(Some(x)) => Some(Box::new(wasm_ref_t {
r: WasmRefInner::ExternRef(x),
})),
Val::FuncRef(x) => Some(Box::new(wasm_ref_t {
r: WasmRefInner::FuncRef(x),
Val::FuncRef(Some(f)) => Some(Box::new(wasm_ref_t {
r: WasmRefInner::FuncRef(f),
})),
_ => None,
}
}
#[no_mangle]
pub extern "C" fn wasm_ref_copy(r: &wasm_ref_t) -> Box<wasm_ref_t> {
Box::new(r.clone())
pub extern "C" fn wasm_ref_copy(r: Option<&wasm_ref_t>) -> Option<Box<wasm_ref_t>> {
r.map(|r| Box::new(r.clone()))
}
#[no_mangle]
pub extern "C" fn wasm_ref_same(a: &wasm_ref_t, b: &wasm_ref_t) -> bool {
match (&a.r, &b.r) {
(WasmRefInner::ExternRef(Some(a)), WasmRefInner::ExternRef(Some(b))) => a.ptr_eq(b),
(WasmRefInner::ExternRef(None), WasmRefInner::ExternRef(None)) => true,
pub extern "C" fn wasm_ref_same(a: Option<&wasm_ref_t>, b: Option<&wasm_ref_t>) -> bool {
match (a.map(|a| &a.r), b.map(|b| &b.r)) {
(Some(WasmRefInner::ExternRef(a)), Some(WasmRefInner::ExternRef(b))) => a.ptr_eq(b),
(None, None) => true,
// Note: we don't support equality for `Func`, so we always return
// `false` for `funcref`s.
_ => false,
@@ -76,19 +76,19 @@ pub extern "C" fn wasm_ref_same(a: &wasm_ref_t, b: &wasm_ref_t) -> bool {
}
#[no_mangle]
pub extern "C" fn wasm_ref_get_host_info(_ref: &wasm_ref_t) -> *mut c_void {
pub extern "C" fn wasm_ref_get_host_info(_ref: Option<&wasm_ref_t>) -> *mut c_void {
std::ptr::null_mut()
}
#[no_mangle]
pub extern "C" fn wasm_ref_set_host_info(_ref: &wasm_ref_t, _info: *mut c_void) {
pub extern "C" fn wasm_ref_set_host_info(_ref: Option<&wasm_ref_t>, _info: *mut c_void) {
eprintln!("`wasm_ref_set_host_info` is not implemented");
std::process::abort();
}
#[no_mangle]
pub extern "C" fn wasm_ref_set_host_info_with_finalizer(
_ref: &wasm_ref_t,
_ref: Option<&wasm_ref_t>,
_info: *mut c_void,
_finalizer: Option<extern "C" fn(*mut c_void)>,
) {