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:
Alex Crichton
2021-06-03 09:10:53 -05:00
committed by GitHub
parent a5a28b1c5b
commit 7a1b7cdf92
233 changed files with 13349 additions and 11997 deletions

View File

@@ -1,7 +1,9 @@
use crate::r#ref::{ref_to_val, val_into_ref};
use crate::{handle_result, wasm_func_t, wasm_ref_t, wasmtime_error_t};
use crate::{wasm_extern_t, wasm_store_t, wasm_tabletype_t};
use std::ptr;
use crate::{
handle_result, wasm_extern_t, wasm_ref_t, wasm_store_t, wasm_tabletype_t, wasmtime_error_t,
wasmtime_val_t, CStoreContext, CStoreContextMut,
};
use std::mem::MaybeUninit;
use wasmtime::{Extern, Table, TableType, Val, ValType};
#[derive(Clone)]
@@ -22,8 +24,8 @@ impl wasm_table_t {
}
}
fn table(&self) -> &Table {
match &self.ext.which {
fn table(&self) -> Table {
match self.ext.which {
Extern::Table(t) => t,
_ => unsafe { std::hint::unreachable_unchecked() },
}
@@ -42,135 +44,138 @@ fn ref_to_val_for_table(r: Option<&wasm_ref_t>, table_ty: &TableType) -> Val {
}
#[no_mangle]
pub extern "C" fn wasm_table_new(
store: &wasm_store_t,
pub unsafe extern "C" fn wasm_table_new(
store: &mut wasm_store_t,
tt: &wasm_tabletype_t,
init: Option<&wasm_ref_t>,
) -> Option<Box<wasm_table_t>> {
let init = ref_to_val_for_table(init, &tt.ty().ty);
let table = Table::new(&store.store, tt.ty().ty.clone(), init).ok()?;
let table = Table::new(store.store.context_mut(), tt.ty().ty.clone(), init).ok()?;
Some(Box::new(wasm_table_t {
ext: wasm_extern_t {
store: store.store.clone(),
which: table.into(),
},
}))
}
#[no_mangle]
pub extern "C" fn wasmtime_funcref_table_new(
store: &wasm_store_t,
tt: &wasm_tabletype_t,
init: Option<&wasm_func_t>,
out: &mut *mut wasm_table_t,
) -> Option<Box<wasmtime_error_t>> {
let init: Val = match init {
Some(val) => Val::FuncRef(Some(val.func().clone())),
None => Val::FuncRef(None),
};
handle_result(
Table::new(&store.store, tt.ty().ty.clone(), init),
|table| {
*out = Box::into_raw(Box::new(wasm_table_t {
ext: wasm_extern_t {
which: table.into(),
},
}));
},
)
pub unsafe extern "C" fn wasm_table_type(t: &wasm_table_t) -> Box<wasm_tabletype_t> {
let table = t.table();
let store = t.ext.store.context();
Box::new(wasm_tabletype_t::new(table.ty(&store)))
}
#[no_mangle]
pub extern "C" fn wasm_table_type(t: &wasm_table_t) -> Box<wasm_tabletype_t> {
let ty = t.table().ty();
Box::new(wasm_tabletype_t::new(ty))
}
#[no_mangle]
pub extern "C" fn wasm_table_get(
t: &wasm_table_t,
pub unsafe extern "C" fn wasm_table_get(
t: &mut wasm_table_t,
index: wasm_table_size_t,
) -> Option<Box<wasm_ref_t>> {
let val = t.table().get(index)?;
let table = t.table();
let val = table.get(t.ext.store.context_mut(), index)?;
val_into_ref(val)
}
#[no_mangle]
pub extern "C" fn wasmtime_funcref_table_get(
t: &wasm_table_t,
index: wasm_table_size_t,
ptr: &mut *mut wasm_func_t,
) -> bool {
match t.table().get(index) {
Some(val) => {
*ptr = match val {
Val::FuncRef(None) => ptr::null_mut(),
Val::FuncRef(Some(f)) => Box::into_raw(Box::new(f.into())),
_ => return false,
};
}
_ => return false,
}
true
}
#[no_mangle]
pub unsafe extern "C" fn wasm_table_set(
t: &wasm_table_t,
t: &mut wasm_table_t,
index: wasm_table_size_t,
r: Option<&wasm_ref_t>,
) -> bool {
let val = ref_to_val_for_table(r, &t.table().ty());
t.table().set(index, val).is_ok()
let table = t.table();
let val = ref_to_val_for_table(r, &table.ty(t.ext.store.context()));
table.set(t.ext.store.context_mut(), index, val).is_ok()
}
#[no_mangle]
pub extern "C" fn wasmtime_funcref_table_set(
t: &wasm_table_t,
index: wasm_table_size_t,
val: Option<&wasm_func_t>,
) -> Option<Box<wasmtime_error_t>> {
let val = match val {
Some(val) => Val::FuncRef(Some(val.func().clone())),
None => Val::FuncRef(None),
};
handle_result(t.table().set(index, val), |()| {})
}
#[no_mangle]
pub extern "C" fn wasm_table_size(t: &wasm_table_t) -> wasm_table_size_t {
t.table().size()
pub unsafe extern "C" fn wasm_table_size(t: &wasm_table_t) -> wasm_table_size_t {
let table = t.table();
let store = t.ext.store.context();
table.size(&store)
}
#[no_mangle]
pub unsafe extern "C" fn wasm_table_grow(
t: &wasm_table_t,
t: &mut wasm_table_t,
delta: wasm_table_size_t,
init: Option<&wasm_ref_t>,
) -> bool {
let init = ref_to_val_for_table(init, &t.table().ty());
t.table().grow(delta, init).is_ok()
let table = t.table();
let init = ref_to_val_for_table(init, &table.ty(t.ext.store.context()));
table.grow(t.ext.store.context_mut(), delta, init).is_ok()
}
#[no_mangle]
pub extern "C" fn wasmtime_funcref_table_grow(
t: &wasm_table_t,
delta: wasm_table_size_t,
init: Option<&wasm_func_t>,
prev_size: Option<&mut wasm_table_size_t>,
) -> Option<Box<wasmtime_error_t>> {
let val = match init {
Some(val) => Val::FuncRef(Some(val.func().clone())),
None => Val::FuncRef(None),
};
handle_result(t.table().grow(delta, val), |prev| {
if let Some(ptr) = prev_size {
*ptr = prev;
}
})
pub extern "C" fn wasm_table_as_extern(t: &mut wasm_table_t) -> &mut wasm_extern_t {
&mut t.ext
}
#[no_mangle]
pub extern "C" fn wasm_table_as_extern(t: &wasm_table_t) -> &wasm_extern_t {
pub extern "C" fn wasm_table_as_extern_const(t: &wasm_table_t) -> &wasm_extern_t {
&t.ext
}
#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_new(
store: CStoreContextMut<'_>,
tt: &wasm_tabletype_t,
init: &wasmtime_val_t,
out: &mut Table,
) -> Option<Box<wasmtime_error_t>> {
handle_result(
Table::new(store, tt.ty().ty.clone(), init.to_val()),
|table| *out = table,
)
}
#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_type(
store: CStoreContext<'_>,
table: &Table,
) -> Box<wasm_tabletype_t> {
Box::new(wasm_tabletype_t::new(table.ty(store)))
}
#[no_mangle]
pub extern "C" fn wasmtime_table_get(
store: CStoreContextMut<'_>,
table: &Table,
index: u32,
ret: &mut MaybeUninit<wasmtime_val_t>,
) -> bool {
match table.get(store, index) {
Some(val) => {
crate::initialize(ret, wasmtime_val_t::from_val(val));
true
}
None => false,
}
}
#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_set(
store: CStoreContextMut<'_>,
table: &Table,
index: u32,
val: &wasmtime_val_t,
) -> Option<Box<wasmtime_error_t>> {
handle_result(table.set(store, index, val.to_val()), |()| {})
}
#[no_mangle]
pub extern "C" fn wasmtime_table_size(store: CStoreContext<'_>, table: &Table) -> u32 {
table.size(store)
}
#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_grow(
store: CStoreContextMut<'_>,
table: &Table,
delta: u32,
val: &wasmtime_val_t,
prev_size: &mut u32,
) -> Option<Box<wasmtime_error_t>> {
handle_result(table.grow(store, delta, val.to_val()), |prev| {
*prev_size = prev
})
}