wasmtime-c-api: Use a safe helper for initializing MaybeUninit out pointers
This commit is contained in:
@@ -220,12 +220,7 @@ fn _wasmtime_func_call(
|
|||||||
match result {
|
match result {
|
||||||
Ok(Ok(out)) => {
|
Ok(Ok(out)) => {
|
||||||
for (slot, val) in results.iter_mut().zip(out.into_vec().into_iter()) {
|
for (slot, val) in results.iter_mut().zip(out.into_vec().into_iter()) {
|
||||||
unsafe {
|
crate::initialize(slot, wasm_val_t::from_val(val));
|
||||||
// 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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,9 +74,7 @@ pub extern "C" fn wasm_global_type(g: &wasm_global_t) -> Box<wasm_globaltype_t>
|
|||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn wasm_global_get(g: &wasm_global_t, out: &mut MaybeUninit<wasm_val_t>) {
|
pub extern "C" fn wasm_global_get(g: &wasm_global_t, out: &mut MaybeUninit<wasm_val_t>) {
|
||||||
unsafe {
|
crate::initialize(out, wasm_val_t::from_val(g.global().get()));
|
||||||
ptr::write(out.as_mut_ptr(), wasm_val_t::from_val(g.global().get()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
|||||||
@@ -62,3 +62,14 @@ pub struct wasm_foreign_t {
|
|||||||
pub struct wasm_shared_module_t {
|
pub struct wasm_shared_module_t {
|
||||||
_unused: [u8; 0],
|
_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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -123,24 +123,23 @@ pub extern "C" fn wasmtime_externref_new_with_finalizer(
|
|||||||
finalizer: Option<wasmtime_externref_finalizer_t>,
|
finalizer: Option<wasmtime_externref_finalizer_t>,
|
||||||
valp: &mut MaybeUninit<wasm_val_t>,
|
valp: &mut MaybeUninit<wasm_val_t>,
|
||||||
) {
|
) {
|
||||||
unsafe {
|
crate::initialize(
|
||||||
ptr::write(
|
valp,
|
||||||
valp.as_mut_ptr(),
|
wasm_val_t::from_val(Val::ExternRef(Some(ExternRef::new(CExternRef {
|
||||||
wasm_val_t::from_val(Val::ExternRef(Some(ExternRef::new(CExternRef {
|
data,
|
||||||
data,
|
finalizer,
|
||||||
finalizer,
|
})))),
|
||||||
})))),
|
);
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[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() {
|
match val.val() {
|
||||||
Val::ExternRef(None) => {
|
Val::ExternRef(None) => {
|
||||||
unsafe {
|
crate::initialize(datap, ptr::null_mut());
|
||||||
ptr::write(datap, ptr::null_mut());
|
|
||||||
}
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Val::ExternRef(Some(x)) => {
|
Val::ExternRef(Some(x)) => {
|
||||||
@@ -148,9 +147,7 @@ pub extern "C" fn wasmtime_externref_data(val: &wasm_val_t, datap: *mut *mut c_v
|
|||||||
Some(r) => r.data,
|
Some(r) => r.data,
|
||||||
None => x.data() as *const dyn Any as *mut c_void,
|
None => x.data() as *const dyn Any as *mut c_void,
|
||||||
};
|
};
|
||||||
unsafe {
|
crate::initialize(datap, data);
|
||||||
ptr::write(datap, data);
|
|
||||||
}
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
|
|||||||
@@ -124,8 +124,8 @@ impl wasm_val_t {
|
|||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn wasm_val_copy(out: &mut MaybeUninit<wasm_val_t>, source: &wasm_val_t) {
|
pub unsafe extern "C" fn wasm_val_copy(out: &mut MaybeUninit<wasm_val_t>, source: &wasm_val_t) {
|
||||||
ptr::write(
|
crate::initialize(
|
||||||
out.as_mut_ptr(),
|
out,
|
||||||
match into_valtype(source.kind) {
|
match into_valtype(source.kind) {
|
||||||
ValType::I32
|
ValType::I32
|
||||||
| ValType::I64
|
| ValType::I64
|
||||||
|
|||||||
Reference in New Issue
Block a user