wasmtime: Add support for func.ref and table.grow with funcrefs

`funcref`s are implemented as `NonNull<VMCallerCheckedAnyfunc>`.

This should be more efficient than using a `VMExternRef` that points at a
`VMCallerCheckedAnyfunc` because it gets rid of an indirection, dynamic
allocation, and some reference counting.

Note that the null function reference is *NOT* a null pointer; it is a
`VMCallerCheckedAnyfunc` that has a null `func_ptr` member.

Part of #929
This commit is contained in:
Nick Fitzgerald
2020-06-18 11:04:40 -07:00
parent ddc2ce8080
commit 58bb5dd953
37 changed files with 603 additions and 305 deletions

View File

@@ -42,7 +42,7 @@ pub extern "C" fn wasm_table_new(
) -> Option<Box<wasm_table_t>> {
let init: Val = match init {
Some(init) => init.r.into(),
None => Val::ExternRef(None),
None => Val::FuncRef(None),
};
let table = Table::new(&store.store, tt.ty().ty.clone(), init).ok()?;
Some(Box::new(wasm_table_t {
@@ -60,8 +60,8 @@ pub extern "C" fn wasmtime_funcref_table_new(
out: &mut *mut wasm_table_t,
) -> Option<Box<wasmtime_error_t>> {
let init: Val = match init {
Some(val) => Val::FuncRef(val.func().borrow().clone()),
None => Val::ExternRef(None),
Some(val) => Val::FuncRef(Some(val.func().borrow().clone())),
None => Val::FuncRef(None),
};
handle_result(
Table::new(&store.store, tt.ty().ty.clone(), init),
@@ -85,7 +85,7 @@ pub extern "C" fn wasm_table_type(t: &wasm_table_t) -> Box<wasm_tabletype_t> {
pub extern "C" fn wasm_table_get(t: &wasm_table_t, index: wasm_table_size_t) -> *mut wasm_ref_t {
match t.table().borrow().get(index) {
Some(val) => into_funcref(val),
None => into_funcref(Val::ExternRef(None)),
None => into_funcref(Val::FuncRef(None)),
}
}
@@ -99,14 +99,14 @@ pub extern "C" fn wasmtime_funcref_table_get(
Some(val) => {
*ptr = match val {
// TODO: what do do about creating new `HostRef` handles here?
Val::FuncRef(f) => {
Val::FuncRef(None) => ptr::null_mut(),
Val::FuncRef(Some(f)) => {
let store = match t.table().as_ref().store() {
None => return false,
Some(store) => store,
};
Box::into_raw(Box::new(HostRef::new(&store, f).into()))
}
Val::ExternRef(None) => ptr::null_mut(),
_ => return false,
};
}
@@ -133,14 +133,14 @@ pub extern "C" fn wasmtime_funcref_table_set(
val: Option<&wasm_func_t>,
) -> Option<Box<wasmtime_error_t>> {
let val = match val {
Some(val) => Val::FuncRef(val.func().borrow().clone()),
None => Val::ExternRef(None),
Some(val) => Val::FuncRef(Some(val.func().borrow().clone())),
None => Val::FuncRef(None),
};
handle_result(t.table().borrow().set(index, val), |()| {})
}
fn into_funcref(val: Val) -> *mut wasm_ref_t {
if let Val::ExternRef(None) = val {
if let Val::FuncRef(None) = val {
return ptr::null_mut();
}
let externref = match val.externref() {
@@ -155,7 +155,7 @@ unsafe fn from_funcref(r: *mut wasm_ref_t) -> Val {
if !r.is_null() {
Box::from_raw(r).r.into()
} else {
Val::ExternRef(None)
Val::FuncRef(None)
}
}
@@ -182,8 +182,8 @@ pub extern "C" fn wasmtime_funcref_table_grow(
prev_size: Option<&mut wasm_table_size_t>,
) -> Option<Box<wasmtime_error_t>> {
let val = match init {
Some(val) => Val::FuncRef(val.func().borrow().clone()),
None => Val::ExternRef(None),
Some(val) => Val::FuncRef(Some(val.func().borrow().clone())),
None => Val::FuncRef(None),
};
handle_result(t.table().borrow().grow(delta, val), |prev| {
if let Some(ptr) = prev_size {