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,7 +1,8 @@
|
||||
use crate::{handle_result, wasmtime_error_t};
|
||||
use crate::{wasm_extern_t, wasm_globaltype_t, wasm_store_t, wasm_val_t};
|
||||
use crate::{
|
||||
handle_result, wasm_extern_t, wasm_globaltype_t, wasm_store_t, wasm_val_t, wasmtime_error_t,
|
||||
wasmtime_val_t, CStoreContext, CStoreContextMut,
|
||||
};
|
||||
use std::mem::MaybeUninit;
|
||||
use std::ptr;
|
||||
use wasmtime::{Extern, Global};
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -20,8 +21,8 @@ impl wasm_global_t {
|
||||
}
|
||||
}
|
||||
|
||||
fn global(&self) -> &Global {
|
||||
match &self.ext.which {
|
||||
fn global(&self) -> Global {
|
||||
match self.ext.which {
|
||||
Extern::Global(g) => g,
|
||||
_ => unsafe { std::hint::unreachable_unchecked() },
|
||||
}
|
||||
@@ -29,65 +30,88 @@ impl wasm_global_t {
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_global_new(
|
||||
store: &wasm_store_t,
|
||||
pub unsafe extern "C" fn wasm_global_new(
|
||||
store: &mut wasm_store_t,
|
||||
gt: &wasm_globaltype_t,
|
||||
val: &wasm_val_t,
|
||||
) -> Option<Box<wasm_global_t>> {
|
||||
let mut global = ptr::null_mut();
|
||||
match wasmtime_global_new(store, gt, val, &mut global) {
|
||||
Some(_err) => None,
|
||||
None => {
|
||||
assert!(!global.is_null());
|
||||
Some(unsafe { Box::from_raw(global) })
|
||||
}
|
||||
match Global::new(store.store.context_mut(), gt.ty().ty.clone(), val.val()) {
|
||||
Ok(global) => Some(Box::new(wasm_global_t {
|
||||
ext: wasm_extern_t {
|
||||
store: store.store.clone(),
|
||||
which: global.into(),
|
||||
},
|
||||
})),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_global_new(
|
||||
store: &wasm_store_t,
|
||||
gt: &wasm_globaltype_t,
|
||||
val: &wasm_val_t,
|
||||
ret: &mut *mut wasm_global_t,
|
||||
) -> Option<Box<wasmtime_error_t>> {
|
||||
let global = Global::new(&store.store, gt.ty().ty.clone(), val.val());
|
||||
handle_result(global, |global| {
|
||||
*ret = Box::into_raw(Box::new(wasm_global_t {
|
||||
ext: wasm_extern_t {
|
||||
which: global.into(),
|
||||
},
|
||||
}));
|
||||
})
|
||||
pub extern "C" fn wasm_global_as_extern(g: &mut wasm_global_t) -> &mut wasm_extern_t {
|
||||
&mut g.ext
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_global_as_extern(g: &wasm_global_t) -> &wasm_extern_t {
|
||||
pub extern "C" fn wasm_global_as_extern_const(g: &wasm_global_t) -> &wasm_extern_t {
|
||||
&g.ext
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_global_type(g: &wasm_global_t) -> Box<wasm_globaltype_t> {
|
||||
let globaltype = g.global().ty();
|
||||
pub unsafe extern "C" fn wasm_global_type(g: &wasm_global_t) -> Box<wasm_globaltype_t> {
|
||||
let globaltype = g.global().ty(&g.ext.store.context());
|
||||
Box::new(wasm_globaltype_t::new(globaltype))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_global_get(g: &wasm_global_t, out: &mut MaybeUninit<wasm_val_t>) {
|
||||
crate::initialize(out, wasm_val_t::from_val(g.global().get()));
|
||||
pub unsafe extern "C" fn wasm_global_get(g: &mut wasm_global_t, out: &mut MaybeUninit<wasm_val_t>) {
|
||||
let global = g.global();
|
||||
crate::initialize(
|
||||
out,
|
||||
wasm_val_t::from_val(global.get(g.ext.store.context_mut())),
|
||||
);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_global_set(g: &wasm_global_t, val: &wasm_val_t) {
|
||||
let result = g.global().set(val.val());
|
||||
// FIXME(WebAssembly/wasm-c-api#131) should communicate the error here
|
||||
drop(result);
|
||||
pub unsafe extern "C" fn wasm_global_set(g: &mut wasm_global_t, val: &wasm_val_t) {
|
||||
let global = g.global();
|
||||
drop(global.set(g.ext.store.context_mut(), val.val()));
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_global_set(
|
||||
g: &wasm_global_t,
|
||||
val: &wasm_val_t,
|
||||
pub unsafe extern "C" fn wasmtime_global_new(
|
||||
store: CStoreContextMut<'_>,
|
||||
gt: &wasm_globaltype_t,
|
||||
val: &wasmtime_val_t,
|
||||
ret: &mut Global,
|
||||
) -> Option<Box<wasmtime_error_t>> {
|
||||
handle_result(g.global().set(val.val()), |()| {})
|
||||
let global = Global::new(store, gt.ty().ty.clone(), val.to_val());
|
||||
handle_result(global, |global| {
|
||||
*ret = global;
|
||||
})
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_global_type(
|
||||
store: CStoreContext<'_>,
|
||||
global: &Global,
|
||||
) -> Box<wasm_globaltype_t> {
|
||||
Box::new(wasm_globaltype_t::new(global.ty(store)))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_global_get(
|
||||
store: CStoreContextMut<'_>,
|
||||
global: &Global,
|
||||
val: &mut MaybeUninit<wasmtime_val_t>,
|
||||
) {
|
||||
crate::initialize(val, wasmtime_val_t::from_val(global.get(store)))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_global_set(
|
||||
store: CStoreContextMut<'_>,
|
||||
global: &Global,
|
||||
val: &wasmtime_val_t,
|
||||
) -> Option<Box<wasmtime_error_t>> {
|
||||
handle_result(global.set(store, val.to_val()), |()| {})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user