Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reference types in the C API #1996

Merged
merged 17 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wasmtime-c-api: Use a safe helper for initializing MaybeUninit out …
…pointers
  • Loading branch information
fitzgen committed Jul 10, 2020
commit d07fdca73aa71b3155782a4198c1c9733b4b1d7d
7 changes: 1 addition & 6 deletions crates/c-api/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,7 @@ fn _wasmtime_func_call(
match result {
Ok(Ok(out)) => {
for (slot, val) in results.iter_mut().zip(out.into_vec().into_iter()) {
unsafe {
// NB: The results array is likely uninitialized memory, so
// use `ptr::write` rather than assignment (which tries to
// run destructors).
ptr::write(slot.as_mut_ptr(), wasm_val_t::from_val(val));
}
crate::initialize(slot, wasm_val_t::from_val(val));
}
None
}
Expand Down
4 changes: 1 addition & 3 deletions crates/c-api/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ pub extern "C" fn wasm_global_type(g: &wasm_global_t) -> Box<wasm_globaltype_t>

#[no_mangle]
pub extern "C" fn wasm_global_get(g: &wasm_global_t, out: &mut MaybeUninit<wasm_val_t>) {
unsafe {
ptr::write(out.as_mut_ptr(), wasm_val_t::from_val(g.global().get()));
}
crate::initialize(out, wasm_val_t::from_val(g.global().get()));
}

#[no_mangle]
Expand Down
11 changes: 11 additions & 0 deletions crates/c-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,14 @@ pub struct wasm_foreign_t {
pub struct wasm_shared_module_t {
_unused: [u8; 0],
}

/// Initialize a `MaybeUninit<T>`
///
/// TODO: Replace calls to this function with
/// https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#method.write
/// once it is stable.
pub(crate) fn initialize<T>(dst: &mut std::mem::MaybeUninit<T>, val: T) {
unsafe {
std::ptr::write(dst.as_mut_ptr(), val);
}
}
29 changes: 13 additions & 16 deletions crates/c-api/src/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,34 +123,31 @@ pub extern "C" fn wasmtime_externref_new_with_finalizer(
finalizer: Option<wasmtime_externref_finalizer_t>,
valp: &mut MaybeUninit<wasm_val_t>,
) {
unsafe {
ptr::write(
valp.as_mut_ptr(),
wasm_val_t::from_val(Val::ExternRef(Some(ExternRef::new(CExternRef {
data,
finalizer,
})))),
)
}
crate::initialize(
valp,
wasm_val_t::from_val(Val::ExternRef(Some(ExternRef::new(CExternRef {
data,
finalizer,
})))),
);
}

#[no_mangle]
pub extern "C" fn wasmtime_externref_data(val: &wasm_val_t, datap: *mut *mut c_void) -> bool {
pub extern "C" fn wasmtime_externref_data(
val: &wasm_val_t,
datap: &mut MaybeUninit<*mut c_void>,
) -> bool {
match val.val() {
Val::ExternRef(None) => {
unsafe {
ptr::write(datap, ptr::null_mut());
}
crate::initialize(datap, ptr::null_mut());
true
}
Val::ExternRef(Some(x)) => {
let data = match x.data().downcast_ref::<CExternRef>() {
Some(r) => r.data,
None => x.data() as *const dyn Any as *mut c_void,
};
unsafe {
ptr::write(datap, data);
}
crate::initialize(datap, data);
true
}
_ => false,
Expand Down
4 changes: 2 additions & 2 deletions crates/c-api/src/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ impl wasm_val_t {

#[no_mangle]
pub unsafe extern "C" fn wasm_val_copy(out: &mut MaybeUninit<wasm_val_t>, source: &wasm_val_t) {
ptr::write(
out.as_mut_ptr(),
crate::initialize(
out,
match into_valtype(source.kind) {
ValType::I32
| ValType::I64
Expand Down