Add wasmtime-specific C APIs for tables (#1654)

This commit adds a suite of `wasmtime_funcref_table_*` APIs which mirror
the standard APIs but have a few differences:

* More errors are returned. For example error messages are communicated
  through `wasmtime_error_t` and out-of-bounds vs load of null can be
  differentiated in the `get` API.

* APIs take `wasm_func_t` instead of `wasm_ref_t`. Given the recent
  decision to remove subtyping from the anyref proposal it's not clear
  how the C API for tables will be affected, so for now these APIs are
  all specialized to only funcref tables.

* Growth now allows access to the previous size of the table, if
  desired, which mirrors the `table.grow` instruction.

This was originally motivated by bytecodealliance/wasmtime-go#5 where
the current APIs we have for working with tables don't quite work. We
don't have a great way to take an anyref constructed from a `Func` and
get the `Func` back out, so for now this sidesteps those concerns while
we sort out the anyref story.

It's intended that once the anyref story has settled and the official C
API has updated we'll likely delete these wasmtime-specific APIs or
implement them as trivial wrappers around the official ones.
This commit is contained in:
Alex Crichton
2020-05-13 18:16:29 -05:00
committed by GitHub
parent fb0b9e3ae6
commit 1247f2b4ae
3 changed files with 123 additions and 18 deletions

View File

@@ -63,7 +63,7 @@ impl wasm_func_t {
}
}
fn func(&self) -> &HostRef<Func> {
pub(crate) fn func(&self) -> &HostRef<Func> {
match &self.ext.which {
ExternHost::Func(f) => f,
_ => unsafe { std::hint::unreachable_unchecked() },
@@ -75,6 +75,16 @@ impl wasm_func_t {
}
}
impl From<HostRef<Func>> for wasm_func_t {
fn from(func: HostRef<Func>) -> wasm_func_t {
wasm_func_t {
ext: wasm_extern_t {
which: ExternHost::Func(func),
},
}
}
}
fn create_function(
store: &wasm_store_t,
ty: &wasm_functype_t,
@@ -97,11 +107,7 @@ fn create_function(
}
Ok(())
});
Box::new(wasm_func_t {
ext: wasm_extern_t {
which: ExternHost::Func(HostRef::new(func)),
},
})
Box::new(HostRef::new(func).into())
}
#[no_mangle]