Implement RFC 11: Redesigning Wasmtime's APIs (#2897)
Implement Wasmtime's new API as designed by RFC 11. This is quite a large commit which has had lots of discussion externally, so for more information it's best to read the RFC thread and the PR thread.
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
use crate::r#ref::{ref_to_val, WasmRefInner};
|
||||
use crate::{from_valtype, into_valtype, wasm_ref_t, wasm_valkind_t, WASM_I32};
|
||||
use std::mem::MaybeUninit;
|
||||
use crate::{from_valtype, into_valtype, wasm_ref_t, wasm_valkind_t, wasmtime_valkind_t, WASM_I32};
|
||||
use std::ffi::c_void;
|
||||
use std::mem::{self, ManuallyDrop, MaybeUninit};
|
||||
use std::ptr;
|
||||
use wasmtime::{Val, ValType};
|
||||
use wasmtime::{ExternRef, Func, Val, ValType};
|
||||
|
||||
#[repr(C)]
|
||||
pub struct wasm_val_t {
|
||||
@@ -147,3 +148,143 @@ pub unsafe extern "C" fn wasm_val_copy(out: &mut MaybeUninit<wasm_val_t>, source
|
||||
pub unsafe extern "C" fn wasm_val_delete(val: *mut wasm_val_t) {
|
||||
ptr::drop_in_place(val);
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct wasmtime_val_t {
|
||||
pub kind: wasmtime_valkind_t,
|
||||
pub of: wasmtime_val_union,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub union wasmtime_val_union {
|
||||
pub i32: i32,
|
||||
pub i64: i64,
|
||||
pub f32: u32,
|
||||
pub f64: u64,
|
||||
pub funcref: wasmtime_func_t,
|
||||
pub externref: ManuallyDrop<Option<ExternRef>>,
|
||||
pub v128: [u8; 16],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct wasmtime_func_t {
|
||||
pub store_id: u64,
|
||||
pub index: usize,
|
||||
}
|
||||
|
||||
impl wasmtime_val_t {
|
||||
pub fn from_val(val: Val) -> wasmtime_val_t {
|
||||
match val {
|
||||
Val::I32(i) => wasmtime_val_t {
|
||||
kind: crate::WASMTIME_I32,
|
||||
of: wasmtime_val_union { i32: i },
|
||||
},
|
||||
Val::I64(i) => wasmtime_val_t {
|
||||
kind: crate::WASMTIME_I64,
|
||||
of: wasmtime_val_union { i64: i },
|
||||
},
|
||||
Val::F32(i) => wasmtime_val_t {
|
||||
kind: crate::WASMTIME_F32,
|
||||
of: wasmtime_val_union { f32: i },
|
||||
},
|
||||
Val::F64(i) => wasmtime_val_t {
|
||||
kind: crate::WASMTIME_F64,
|
||||
of: wasmtime_val_union { f64: i },
|
||||
},
|
||||
Val::ExternRef(i) => wasmtime_val_t {
|
||||
kind: crate::WASMTIME_EXTERNREF,
|
||||
of: wasmtime_val_union {
|
||||
externref: ManuallyDrop::new(i),
|
||||
},
|
||||
},
|
||||
Val::FuncRef(i) => wasmtime_val_t {
|
||||
kind: crate::WASMTIME_FUNCREF,
|
||||
of: wasmtime_val_union {
|
||||
funcref: match i {
|
||||
Some(func) => unsafe { mem::transmute::<Func, wasmtime_func_t>(func) },
|
||||
None => wasmtime_func_t {
|
||||
store_id: 0,
|
||||
index: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Val::V128(val) => wasmtime_val_t {
|
||||
kind: crate::WASMTIME_V128,
|
||||
of: wasmtime_val_union {
|
||||
v128: val.to_le_bytes(),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn to_val(&self) -> Val {
|
||||
match self.kind {
|
||||
crate::WASMTIME_I32 => Val::I32(self.of.i32),
|
||||
crate::WASMTIME_I64 => Val::I64(self.of.i64),
|
||||
crate::WASMTIME_F32 => Val::F32(self.of.f32),
|
||||
crate::WASMTIME_F64 => Val::F64(self.of.f64),
|
||||
crate::WASMTIME_V128 => Val::V128(u128::from_le_bytes(self.of.v128)),
|
||||
crate::WASMTIME_FUNCREF => {
|
||||
let store = self.of.funcref.store_id;
|
||||
let index = self.of.funcref.index;
|
||||
Val::FuncRef(if store == 0 && index == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(mem::transmute::<wasmtime_func_t, Func>(self.of.funcref))
|
||||
})
|
||||
}
|
||||
crate::WASMTIME_EXTERNREF => Val::ExternRef((*self.of.externref).clone()),
|
||||
other => panic!("unknown wasmtime_valkind_t: {}", other),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for wasmtime_val_t {
|
||||
fn drop(&mut self) {
|
||||
if self.kind == crate::WASMTIME_EXTERNREF {
|
||||
unsafe {
|
||||
ManuallyDrop::drop(&mut self.of.externref);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_val_delete(val: &mut ManuallyDrop<wasmtime_val_t>) {
|
||||
ManuallyDrop::drop(val)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_val_copy(
|
||||
dst: &mut MaybeUninit<wasmtime_val_t>,
|
||||
src: &wasmtime_val_t,
|
||||
) {
|
||||
crate::initialize(dst, wasmtime_val_t::from_val(src.to_val()))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_externref_new(
|
||||
data: *mut c_void,
|
||||
finalizer: Option<extern "C" fn(*mut c_void)>,
|
||||
) -> ExternRef {
|
||||
ExternRef::new(crate::ForeignData { data, finalizer })
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_externref_data(externref: ManuallyDrop<ExternRef>) -> *mut c_void {
|
||||
externref
|
||||
.data()
|
||||
.downcast_ref::<crate::ForeignData>()
|
||||
.unwrap()
|
||||
.data
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_externref_clone(externref: ManuallyDrop<ExternRef>) -> ExternRef {
|
||||
(*externref).clone()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_externref_delete(_val: Option<ExternRef>) {}
|
||||
|
||||
Reference in New Issue
Block a user