Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove HostRef<T> from the C API #1926

Merged
merged 1 commit into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions crates/c-api/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
}

#[no_mangle]
pub extern fn #same(a: &#ty, b: &#ty) -> bool {
a.externref().ptr_eq(&b.externref())
pub extern fn #same(_a: &#ty, _b: &#ty) -> bool {
eprintln!("`{}` is not implemented", stringify!(#same));
std::process::abort();
}

#[no_mangle]
Expand All @@ -98,13 +99,14 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

#[no_mangle]
pub extern fn #as_ref(a: &#ty) -> Box<crate::wasm_ref_t> {
let r = Some(a.externref());
Box::new(crate::wasm_ref_t { r })
eprintln!("`{}` is not implemented", stringify!(#as_ref));
std::process::abort();
}

#[no_mangle]
pub extern fn #as_ref_const(a: &#ty) -> Box<crate::wasm_ref_t> {
#as_ref(a)
eprintln!("`{}` is not implemented", stringify!(#as_ref_const));
std::process::abort();
}

// TODO: implement `wasm_ref_as_#name#`
Expand Down
40 changes: 7 additions & 33 deletions crates/c-api/src/extern.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,27 @@
use crate::host_ref::HostRef;
use crate::wasm_externkind_t;
use crate::{wasm_externtype_t, wasm_func_t, wasm_global_t, wasm_memory_t, wasm_table_t};
use wasmtime::{ExternType, Func, Global, Memory, Table};
use wasmtime::Extern;

#[derive(Clone)]
pub struct wasm_extern_t {
pub(crate) which: ExternHost,
pub(crate) which: Extern,
}

wasmtime_c_api_macros::declare_ref!(wasm_extern_t);

#[derive(Clone)]
pub(crate) enum ExternHost {
Func(HostRef<Func>),
Global(HostRef<Global>),
Memory(HostRef<Memory>),
Table(HostRef<Table>),
}

impl wasm_extern_t {
pub(crate) fn externref(&self) -> wasmtime::ExternRef {
match &self.which {
ExternHost::Func(f) => f.clone().into(),
ExternHost::Global(f) => f.clone().into(),
ExternHost::Memory(f) => f.clone().into(),
ExternHost::Table(f) => f.clone().into(),
}
}
}

#[no_mangle]
pub extern "C" fn wasm_extern_kind(e: &wasm_extern_t) -> wasm_externkind_t {
match e.which {
ExternHost::Func(_) => crate::WASM_EXTERN_FUNC,
ExternHost::Global(_) => crate::WASM_EXTERN_GLOBAL,
ExternHost::Table(_) => crate::WASM_EXTERN_TABLE,
ExternHost::Memory(_) => crate::WASM_EXTERN_MEMORY,
Extern::Func(_) => crate::WASM_EXTERN_FUNC,
Extern::Global(_) => crate::WASM_EXTERN_GLOBAL,
Extern::Table(_) => crate::WASM_EXTERN_TABLE,
Extern::Memory(_) => crate::WASM_EXTERN_MEMORY,
}
}

#[no_mangle]
pub extern "C" fn wasm_extern_type(e: &wasm_extern_t) -> Box<wasm_externtype_t> {
let ty = match &e.which {
ExternHost::Func(f) => ExternType::Func(f.borrow().ty()),
ExternHost::Global(f) => ExternType::Global(f.borrow().ty()),
ExternHost::Table(f) => ExternType::Table(f.borrow().ty()),
ExternHost::Memory(f) => ExternType::Memory(f.borrow().ty()),
};
Box::new(wasm_externtype_t::new(ty))
Box::new(wasm_externtype_t::new(e.which.ty()))
}

#[no_mangle]
Expand Down
43 changes: 15 additions & 28 deletions crates/c-api/src/func.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::host_ref::HostRef;
use crate::{wasm_extern_t, wasm_functype_t, wasm_store_t, wasm_val_t};
use crate::{wasm_name_t, wasm_trap_t, wasmtime_error_t, ExternHost};
use crate::{wasm_name_t, wasm_trap_t, wasmtime_error_t};
use anyhow::anyhow;
use std::ffi::c_void;
use std::panic::{self, AssertUnwindSafe};
Expand Down Expand Up @@ -59,29 +58,23 @@ impl Drop for Finalizer {
impl wasm_func_t {
pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_func_t> {
match &e.which {
ExternHost::Func(_) => Some(unsafe { &*(e as *const _ as *const _) }),
Extern::Func(_) => Some(unsafe { &*(e as *const _ as *const _) }),
_ => None,
}
}

pub(crate) fn func(&self) -> &HostRef<Func> {
pub(crate) fn func(&self) -> &Func {
match &self.ext.which {
ExternHost::Func(f) => f,
Extern::Func(f) => f,
_ => unsafe { std::hint::unreachable_unchecked() },
}
}

fn externref(&self) -> wasmtime::ExternRef {
self.func().clone().into()
}
}

impl From<HostRef<Func>> for wasm_func_t {
fn from(func: HostRef<Func>) -> wasm_func_t {
impl From<Func> for wasm_func_t {
fn from(func: Func) -> wasm_func_t {
wasm_func_t {
ext: wasm_extern_t {
which: ExternHost::Func(func),
},
ext: wasm_extern_t { which: func.into() },
}
}
}
Expand All @@ -101,14 +94,14 @@ fn create_function(
let mut out_results = vec![wasm_val_t::default(); results.len()];
let out = func(caller, params.as_ptr(), out_results.as_mut_ptr());
if let Some(trap) = out {
return Err(trap.trap.borrow().clone());
return Err(trap.trap.clone());
}
for i in 0..results.len() {
results[i] = out_results[i].val();
}
Ok(())
});
Box::new(HostRef::new(func).into())
Box::new(func.into())
}

#[no_mangle]
Expand Down Expand Up @@ -172,7 +165,7 @@ pub unsafe extern "C" fn wasm_func_call(
args: *const wasm_val_t,
results: *mut wasm_val_t,
) -> *mut wasm_trap_t {
let func = wasm_func.func().borrow();
let func = wasm_func.func();
let mut trap = ptr::null_mut();
let error = wasmtime_func_call(
wasm_func,
Expand Down Expand Up @@ -211,7 +204,7 @@ fn _wasmtime_func_call(
results: &mut [wasm_val_t],
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
let func = func.func().borrow();
let func = func.func();
if results.len() != func.result_arity() {
return Some(Box::new(anyhow!("wrong number of results provided").into()));
}
Expand Down Expand Up @@ -253,17 +246,17 @@ fn _wasmtime_func_call(

#[no_mangle]
pub extern "C" fn wasm_func_type(f: &wasm_func_t) -> Box<wasm_functype_t> {
Box::new(wasm_functype_t::new(f.func().borrow().ty()))
Box::new(wasm_functype_t::new(f.func().ty()))
}

#[no_mangle]
pub extern "C" fn wasm_func_param_arity(f: &wasm_func_t) -> usize {
f.func().borrow().param_arity()
f.func().param_arity()
}

#[no_mangle]
pub extern "C" fn wasm_func_result_arity(f: &wasm_func_t) -> usize {
f.func().borrow().result_arity()
f.func().result_arity()
}

#[no_mangle]
Expand All @@ -277,12 +270,6 @@ pub extern "C" fn wasmtime_caller_export_get(
name: &wasm_name_t,
) -> Option<Box<wasm_extern_t>> {
let name = str::from_utf8(name.as_slice()).ok()?;
let export = caller.caller.get_export(name)?;
let which = match export {
Extern::Func(f) => ExternHost::Func(HostRef::new(f)),
Extern::Global(g) => ExternHost::Global(HostRef::new(g)),
Extern::Memory(m) => ExternHost::Memory(HostRef::new(m)),
Extern::Table(t) => ExternHost::Table(HostRef::new(t)),
};
let which = caller.caller.get_export(name)?;
Some(Box::new(wasm_extern_t { which }))
}
25 changes: 10 additions & 15 deletions crates/c-api/src/global.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::host_ref::HostRef;
use crate::{handle_result, wasmtime_error_t};
use crate::{wasm_extern_t, wasm_globaltype_t, wasm_store_t, wasm_val_t, ExternHost};
use crate::{wasm_extern_t, wasm_globaltype_t, wasm_store_t, wasm_val_t};
use std::ptr;
use wasmtime::Global;
use wasmtime::{Extern, Global};

#[derive(Clone)]
#[repr(transparent)]
Expand All @@ -15,21 +14,17 @@ wasmtime_c_api_macros::declare_ref!(wasm_global_t);
impl wasm_global_t {
pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_global_t> {
match &e.which {
ExternHost::Global(_) => Some(unsafe { &*(e as *const _ as *const _) }),
Extern::Global(_) => Some(unsafe { &*(e as *const _ as *const _) }),
_ => None,
}
}

fn global(&self) -> &HostRef<Global> {
fn global(&self) -> &Global {
match &self.ext.which {
ExternHost::Global(g) => g,
Extern::Global(g) => g,
_ => unsafe { std::hint::unreachable_unchecked() },
}
}

fn externref(&self) -> wasmtime::ExternRef {
self.global().clone().into()
}
}

#[no_mangle]
Expand Down Expand Up @@ -59,7 +54,7 @@ pub extern "C" fn wasmtime_global_new(
handle_result(global, |global| {
*ret = Box::into_raw(Box::new(wasm_global_t {
ext: wasm_extern_t {
which: ExternHost::Global(HostRef::new(global)),
which: global.into(),
},
}));
})
Expand All @@ -72,18 +67,18 @@ pub extern "C" fn wasm_global_as_extern(g: &wasm_global_t) -> &wasm_extern_t {

#[no_mangle]
pub extern "C" fn wasm_global_type(g: &wasm_global_t) -> Box<wasm_globaltype_t> {
let globaltype = g.global().borrow().ty();
let globaltype = g.global().ty();
Box::new(wasm_globaltype_t::new(globaltype))
}

#[no_mangle]
pub extern "C" fn wasm_global_get(g: &wasm_global_t, out: &mut wasm_val_t) {
out.set(g.global().borrow().get());
out.set(g.global().get());
}

#[no_mangle]
pub extern "C" fn wasm_global_set(g: &wasm_global_t, val: &wasm_val_t) {
let result = g.global().borrow().set(val.val());
let result = g.global().set(val.val());
// FIXME(WebAssembly/wasm-c-api#131) should communicate the error here
drop(result);
}
Expand All @@ -93,5 +88,5 @@ 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()), |()| {})
handle_result(g.global().set(val.val()), |()| {})
}
101 changes: 0 additions & 101 deletions crates/c-api/src/host_ref.rs

This file was deleted.

Loading