Skip to content

Commit 1247f2b

Browse files
authored
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.
1 parent fb0b9e3 commit 1247f2b

File tree

3 files changed

+123
-18
lines changed

3 files changed

+123
-18
lines changed

crates/c-api/include/wasmtime.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,32 @@ WASM_API_EXTERN own wasmtime_error_t *wasmtime_module_validate(
254254
const wasm_byte_vec_t *binary
255255
);
256256

257+
258+
// Similar to `wasm_table_*`, except these explicitly operate on funcref tables
259+
// and work with `wasm_func_t` values instead of `wasm_ref_t`.
260+
WASM_API_EXTERN own wasmtime_error_t *wasmtime_funcref_table_new(
261+
wasm_store_t *store,
262+
const wasm_tabletype_t *element_ty,
263+
wasm_func_t *init,
264+
own wasm_table_t **table
265+
);
266+
WASM_API_EXTERN bool wasmtime_funcref_table_get(
267+
const wasm_table_t *table,
268+
wasm_table_size_t index,
269+
own wasm_func_t **func
270+
);
271+
WASM_API_EXTERN own wasmtime_error_t *wasmtime_funcref_table_set(
272+
wasm_table_t *table,
273+
wasm_table_size_t index,
274+
const wasm_func_t *value
275+
);
276+
WASM_API_EXTERN wasmtime_error_t *wasmtime_funcref_table_grow(
277+
wasm_table_t *table,
278+
wasm_table_size_t delta,
279+
const wasm_func_t *init,
280+
wasm_table_size_t *prev_size
281+
);
282+
257283
#undef own
258284

259285
#ifdef __cplusplus

crates/c-api/src/func.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl wasm_func_t {
6363
}
6464
}
6565

66-
fn func(&self) -> &HostRef<Func> {
66+
pub(crate) fn func(&self) -> &HostRef<Func> {
6767
match &self.ext.which {
6868
ExternHost::Func(f) => f,
6969
_ => unsafe { std::hint::unreachable_unchecked() },
@@ -75,6 +75,16 @@ impl wasm_func_t {
7575
}
7676
}
7777

78+
impl From<HostRef<Func>> for wasm_func_t {
79+
fn from(func: HostRef<Func>) -> wasm_func_t {
80+
wasm_func_t {
81+
ext: wasm_extern_t {
82+
which: ExternHost::Func(func),
83+
},
84+
}
85+
}
86+
}
87+
7888
fn create_function(
7989
store: &wasm_store_t,
8090
ty: &wasm_functype_t,
@@ -97,11 +107,7 @@ fn create_function(
97107
}
98108
Ok(())
99109
});
100-
Box::new(wasm_func_t {
101-
ext: wasm_extern_t {
102-
which: ExternHost::Func(HostRef::new(func)),
103-
},
104-
})
110+
Box::new(HostRef::new(func).into())
105111
}
106112

107113
#[no_mangle]

crates/c-api/src/table.rs

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::wasm_ref_t;
1+
use crate::{handle_result, wasm_func_t, wasm_ref_t, wasmtime_error_t};
22
use crate::{wasm_extern_t, wasm_store_t, wasm_tabletype_t, ExternHost};
33
use std::ptr;
44
use wasmtime::{AnyRef, HostRef, Table, Val};
@@ -34,15 +34,14 @@ impl wasm_table_t {
3434
}
3535

3636
#[no_mangle]
37-
pub unsafe extern "C" fn wasm_table_new(
37+
pub extern "C" fn wasm_table_new(
3838
store: &wasm_store_t,
3939
tt: &wasm_tabletype_t,
40-
init: *mut wasm_ref_t,
40+
init: Option<Box<wasm_ref_t>>,
4141
) -> Option<Box<wasm_table_t>> {
42-
let init: Val = if !init.is_null() {
43-
Box::from_raw(init).r.into()
44-
} else {
45-
Val::AnyRef(AnyRef::Null)
42+
let init: Val = match init {
43+
Some(init) => init.r.into(),
44+
None => Val::AnyRef(AnyRef::Null),
4645
};
4746
let table = Table::new(&store.store.borrow(), tt.ty().ty.clone(), init).ok()?;
4847
Some(Box::new(wasm_table_t {
@@ -52,23 +51,66 @@ pub unsafe extern "C" fn wasm_table_new(
5251
}))
5352
}
5453

54+
#[no_mangle]
55+
pub extern "C" fn wasmtime_funcref_table_new(
56+
store: &wasm_store_t,
57+
tt: &wasm_tabletype_t,
58+
init: Option<&wasm_func_t>,
59+
out: &mut *mut wasm_table_t,
60+
) -> Option<Box<wasmtime_error_t>> {
61+
let init: Val = match init {
62+
Some(val) => Val::FuncRef(val.func().borrow().clone()),
63+
None => Val::AnyRef(AnyRef::Null),
64+
};
65+
handle_result(
66+
Table::new(&store.store.borrow(), tt.ty().ty.clone(), init),
67+
|table| {
68+
*out = Box::into_raw(Box::new(wasm_table_t {
69+
ext: wasm_extern_t {
70+
which: ExternHost::Table(HostRef::new(table)),
71+
},
72+
}));
73+
println!("ret at {:?}", *out);
74+
},
75+
)
76+
}
77+
5578
#[no_mangle]
5679
pub extern "C" fn wasm_table_type(t: &wasm_table_t) -> Box<wasm_tabletype_t> {
5780
let ty = t.table().borrow().ty();
5881
Box::new(wasm_tabletype_t::new(ty))
5982
}
6083

6184
#[no_mangle]
62-
pub unsafe extern "C" fn wasm_table_get(
63-
t: &wasm_table_t,
64-
index: wasm_table_size_t,
65-
) -> *mut wasm_ref_t {
85+
pub extern "C" fn wasm_table_get(t: &wasm_table_t, index: wasm_table_size_t) -> *mut wasm_ref_t {
6686
match t.table().borrow().get(index) {
6787
Some(val) => into_funcref(val),
6888
None => into_funcref(Val::AnyRef(AnyRef::Null)),
6989
}
7090
}
7191

92+
#[no_mangle]
93+
pub extern "C" fn wasmtime_funcref_table_get(
94+
t: &wasm_table_t,
95+
index: wasm_table_size_t,
96+
ptr: &mut *mut wasm_func_t,
97+
) -> bool {
98+
println!("get {:p} at {}", t, index);
99+
match t.table().borrow().get(index) {
100+
Some(val) => {
101+
*ptr = match val {
102+
// TODO: what do do about creating new `HostRef` handles here?
103+
Val::FuncRef(f) => Box::into_raw(Box::new(HostRef::new(f).into())),
104+
Val::AnyRef(AnyRef::Null) => ptr::null_mut(),
105+
_ => return false,
106+
};
107+
}
108+
109+
_ => return false,
110+
}
111+
true
112+
}
113+
72114
#[no_mangle]
73115
pub unsafe extern "C" fn wasm_table_set(
74116
t: &wasm_table_t,
@@ -79,7 +121,20 @@ pub unsafe extern "C" fn wasm_table_set(
79121
t.table().borrow().set(index, val).is_ok()
80122
}
81123

82-
unsafe fn into_funcref(val: Val) -> *mut wasm_ref_t {
124+
#[no_mangle]
125+
pub extern "C" fn wasmtime_funcref_table_set(
126+
t: &wasm_table_t,
127+
index: wasm_table_size_t,
128+
val: Option<&wasm_func_t>,
129+
) -> Option<Box<wasmtime_error_t>> {
130+
let val = match val {
131+
Some(val) => Val::FuncRef(val.func().borrow().clone()),
132+
None => Val::AnyRef(AnyRef::Null),
133+
};
134+
handle_result(t.table().borrow().set(index, val), |()| {})
135+
}
136+
137+
fn into_funcref(val: Val) -> *mut wasm_ref_t {
83138
if let Val::AnyRef(AnyRef::Null) = val {
84139
return ptr::null_mut();
85140
}
@@ -114,6 +169,24 @@ pub unsafe extern "C" fn wasm_table_grow(
114169
t.table().borrow().grow(delta, init).is_ok()
115170
}
116171

172+
#[no_mangle]
173+
pub extern "C" fn wasmtime_funcref_table_grow(
174+
t: &wasm_table_t,
175+
delta: wasm_table_size_t,
176+
init: Option<&wasm_func_t>,
177+
prev_size: Option<&mut wasm_table_size_t>,
178+
) -> Option<Box<wasmtime_error_t>> {
179+
let val = match init {
180+
Some(val) => Val::FuncRef(val.func().borrow().clone()),
181+
None => Val::AnyRef(AnyRef::Null),
182+
};
183+
handle_result(t.table().borrow().grow(delta, val), |prev| {
184+
if let Some(ptr) = prev_size {
185+
*ptr = prev;
186+
}
187+
})
188+
}
189+
117190
#[no_mangle]
118191
pub extern "C" fn wasm_table_as_extern(t: &wasm_table_t) -> &wasm_extern_t {
119192
&t.ext

0 commit comments

Comments
 (0)