Skip to content

Commit

Permalink
wasmtime-c-api: Make wasm_table_set *not* take ownership of its ref…
Browse files Browse the repository at this point in the history
…erence

Same for `wasm_table_grow` and `wasm_table_new` and their `init` values.
  • Loading branch information
fitzgen committed Jul 10, 2020
1 parent d07fdca commit 89603bc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
6 changes: 5 additions & 1 deletion crates/c-api/include/doc-wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,8 @@
* Returns an error if the #wasm_ref_t does not match the element type of the
* table provided or if it comes from a different store than the one provided.
*
* Does not take ownship of the `init` value.
*
* > Note: for funcref tables you can use #wasmtime_funcref_table_new as well.
*
* \fn wasm_tabletype_t *wasm_table_type(const wasm_table_t *);
Expand Down Expand Up @@ -1792,7 +1794,7 @@
* * The #wasm_ref_t comes from a different store than the table provided.
* * The #wasm_ref_t does not have an appropriate type to store in this table.
*
* Takes ownership of the given `wasm_ref_t*`.
* Does not take ownership of the given `wasm_ref_t*`.
*
* > Note: for funcref tables you can use #wasmtime_funcref_table_set to learn
* > about errors.
Expand All @@ -1813,6 +1815,8 @@
* * The #wasm_ref_t comes from a different store than the table provided.
* * The #wasm_ref_t does not have an appropriate type to store in this table.
*
* Does not take ownership of the givein `init` value.
*
* > Note: for funcref tables you can use #wasmtime_funcref_table_grow as well.
*/

Expand Down
11 changes: 0 additions & 11 deletions crates/c-api/src/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ pub(crate) enum WasmRefInner {

wasmtime_c_api_macros::declare_own!(wasm_ref_t);

pub(crate) fn ref_into_val(r: Option<Box<wasm_ref_t>>) -> Option<Val> {
// Let callers decide whether to treat this as a null `funcref` or a
// null `externref`.
let r = r?;

Some(match r.r {
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(Some(x.clone())),
Expand Down
29 changes: 16 additions & 13 deletions crates/c-api/src/table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::r#ref::{ref_into_val, val_into_ref};
use crate::r#ref::{ref_to_val, val_into_ref};
use crate::{handle_result, wasm_func_t, wasm_ref_t, wasmtime_error_t};
use crate::{wasm_extern_t, wasm_store_t, wasm_tabletype_t};
use std::ptr;
Expand Down Expand Up @@ -30,21 +30,24 @@ impl wasm_table_t {
}
}

fn ref_into_val_for_table(r: Option<Box<wasm_ref_t>>, table_ty: &TableType) -> Val {
ref_into_val(r).unwrap_or_else(|| match table_ty.element() {
ValType::FuncRef => Val::FuncRef(None),
ValType::ExternRef => Val::ExternRef(None),
ty => panic!("unsupported table element type: {:?}", ty),
})
fn ref_to_val_for_table(r: Option<&wasm_ref_t>, table_ty: &TableType) -> Val {
r.map_or_else(
|| match table_ty.element() {
ValType::FuncRef => Val::FuncRef(None),
ValType::ExternRef => Val::ExternRef(None),
ty => panic!("unsupported table element type: {:?}", ty),
},
|r| ref_to_val(r),
)
}

#[no_mangle]
pub extern "C" fn wasm_table_new(
store: &wasm_store_t,
tt: &wasm_tabletype_t,
init: Option<Box<wasm_ref_t>>,
init: Option<&wasm_ref_t>,
) -> Option<Box<wasm_table_t>> {
let init = ref_into_val_for_table(init, &tt.ty().ty);
let init = ref_to_val_for_table(init, &tt.ty().ty);
let table = Table::new(&store.store, tt.ty().ty.clone(), init).ok()?;
Some(Box::new(wasm_table_t {
ext: wasm_extern_t {
Expand Down Expand Up @@ -115,9 +118,9 @@ pub extern "C" fn wasmtime_funcref_table_get(
pub unsafe extern "C" fn wasm_table_set(
t: &wasm_table_t,
index: wasm_table_size_t,
r: Option<Box<wasm_ref_t>>,
r: Option<&wasm_ref_t>,
) -> bool {
let val = ref_into_val_for_table(r, &t.table().ty());
let val = ref_to_val_for_table(r, &t.table().ty());
t.table().set(index, val).is_ok()
}

Expand All @@ -143,9 +146,9 @@ pub extern "C" fn wasm_table_size(t: &wasm_table_t) -> wasm_table_size_t {
pub unsafe extern "C" fn wasm_table_grow(
t: &wasm_table_t,
delta: wasm_table_size_t,
init: Option<Box<wasm_ref_t>>,
init: Option<&wasm_ref_t>,
) -> bool {
let init = ref_into_val_for_table(init, &t.table().ty());
let init = ref_to_val_for_table(init, &t.table().ty());
t.table().grow(delta, init).is_ok()
}

Expand Down
2 changes: 1 addition & 1 deletion examples/externref.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ int main() {
assert(elem.kind == WASM_ANYREF);
ok = wasm_table_set(table, 3, elem.of.ref);
assert(ok);
elem.of.ref = NULL;

// `table[3]` should now be our `externref`.
wasm_ref_delete(elem.of.ref);
elem.of.ref = wasm_table_get(table, 3);
assert(elem.of.ref != NULL);
assert(wasm_ref_same(elem.of.ref, externref.of.ref));
Expand Down

0 comments on commit 89603bc

Please sign in to comment.