Skip to content

Commit 86328b3

Browse files
committed
wasmtime-c-api: Make wasm_table_set *not* take ownership of its reference
Same for `wasm_table_grow` and `wasm_table_new` and their `init` values.
1 parent 87fb90c commit 86328b3

4 files changed

Lines changed: 22 additions & 26 deletions

File tree

crates/c-api/include/doc-wasm.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,8 @@
17641764
* Returns an error if the #wasm_ref_t does not match the element type of the
17651765
* table provided or if it comes from a different store than the one provided.
17661766
*
1767+
* Does not take ownship of the `init` value.
1768+
*
17671769
* > Note: for funcref tables you can use #wasmtime_funcref_table_new as well.
17681770
*
17691771
* \fn wasm_tabletype_t *wasm_table_type(const wasm_table_t *);
@@ -1792,7 +1794,7 @@
17921794
* * The #wasm_ref_t comes from a different store than the table provided.
17931795
* * The #wasm_ref_t does not have an appropriate type to store in this table.
17941796
*
1795-
* Takes ownership of the given `wasm_ref_t*`.
1797+
* Does not take ownership of the given `wasm_ref_t*`.
17961798
*
17971799
* > Note: for funcref tables you can use #wasmtime_funcref_table_set to learn
17981800
* > about errors.
@@ -1813,6 +1815,8 @@
18131815
* * The #wasm_ref_t comes from a different store than the table provided.
18141816
* * The #wasm_ref_t does not have an appropriate type to store in this table.
18151817
*
1818+
* Does not take ownership of the givein `init` value.
1819+
*
18161820
* > Note: for funcref tables you can use #wasmtime_funcref_table_grow as well.
18171821
*/
18181822

crates/c-api/src/ref.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ pub(crate) enum WasmRefInner {
3030

3131
wasmtime_c_api_macros::declare_own!(wasm_ref_t);
3232

33-
pub(crate) fn ref_into_val(r: Option<Box<wasm_ref_t>>) -> Option<Val> {
34-
// Let callers decide whether to treat this as a null `funcref` or a
35-
// null `externref`.
36-
let r = r?;
37-
38-
Some(match r.r {
39-
WasmRefInner::ExternRef(x) => Val::ExternRef(Some(x)),
40-
WasmRefInner::FuncRef(f) => Val::FuncRef(Some(f)),
41-
})
42-
}
43-
4433
pub(crate) fn ref_to_val(r: &wasm_ref_t) -> Val {
4534
match &r.r {
4635
WasmRefInner::ExternRef(x) => Val::ExternRef(Some(x.clone())),

crates/c-api/src/table.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::r#ref::{ref_into_val, val_into_ref};
1+
use crate::r#ref::{ref_to_val, val_into_ref};
22
use crate::{handle_result, wasm_func_t, wasm_ref_t, wasmtime_error_t};
33
use crate::{wasm_extern_t, wasm_store_t, wasm_tabletype_t};
44
use std::ptr;
@@ -30,21 +30,24 @@ impl wasm_table_t {
3030
}
3131
}
3232

33-
fn ref_into_val_for_table(r: Option<Box<wasm_ref_t>>, table_ty: &TableType) -> Val {
34-
ref_into_val(r).unwrap_or_else(|| match table_ty.element() {
35-
ValType::FuncRef => Val::FuncRef(None),
36-
ValType::ExternRef => Val::ExternRef(None),
37-
ty => panic!("unsupported table element type: {:?}", ty),
38-
})
33+
fn ref_to_val_for_table(r: Option<&wasm_ref_t>, table_ty: &TableType) -> Val {
34+
r.map_or_else(
35+
|| match table_ty.element() {
36+
ValType::FuncRef => Val::FuncRef(None),
37+
ValType::ExternRef => Val::ExternRef(None),
38+
ty => panic!("unsupported table element type: {:?}", ty),
39+
},
40+
|r| ref_to_val(r),
41+
)
3942
}
4043

4144
#[no_mangle]
4245
pub extern "C" fn wasm_table_new(
4346
store: &wasm_store_t,
4447
tt: &wasm_tabletype_t,
45-
init: Option<Box<wasm_ref_t>>,
48+
init: Option<&wasm_ref_t>,
4649
) -> Option<Box<wasm_table_t>> {
47-
let init = ref_into_val_for_table(init, &tt.ty().ty);
50+
let init = ref_to_val_for_table(init, &tt.ty().ty);
4851
let table = Table::new(&store.store, tt.ty().ty.clone(), init).ok()?;
4952
Some(Box::new(wasm_table_t {
5053
ext: wasm_extern_t {
@@ -115,9 +118,9 @@ pub extern "C" fn wasmtime_funcref_table_get(
115118
pub unsafe extern "C" fn wasm_table_set(
116119
t: &wasm_table_t,
117120
index: wasm_table_size_t,
118-
r: Option<Box<wasm_ref_t>>,
121+
r: Option<&wasm_ref_t>,
119122
) -> bool {
120-
let val = ref_into_val_for_table(r, &t.table().ty());
123+
let val = ref_to_val_for_table(r, &t.table().ty());
121124
t.table().set(index, val).is_ok()
122125
}
123126

@@ -143,9 +146,9 @@ pub extern "C" fn wasm_table_size(t: &wasm_table_t) -> wasm_table_size_t {
143146
pub unsafe extern "C" fn wasm_table_grow(
144147
t: &wasm_table_t,
145148
delta: wasm_table_size_t,
146-
init: Option<Box<wasm_ref_t>>,
149+
init: Option<&wasm_ref_t>,
147150
) -> bool {
148-
let init = ref_into_val_for_table(init, &t.table().ty());
151+
let init = ref_to_val_for_table(init, &t.table().ty());
149152
t.table().grow(delta, init).is_ok()
150153
}
151154

examples/externref.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ int main() {
107107
assert(elem.kind == WASM_ANYREF);
108108
ok = wasm_table_set(table, 3, elem.of.ref);
109109
assert(ok);
110-
elem.of.ref = NULL;
111110

112111
// `table[3]` should now be our `externref`.
112+
wasm_ref_delete(elem.of.ref);
113113
elem.of.ref = wasm_table_get(table, 3);
114114
assert(elem.of.ref != NULL);
115115
assert(wasm_ref_same(elem.of.ref, externref.of.ref));

0 commit comments

Comments
 (0)