* Remove `HostRef` from the `wasmtime` public API
This commit removes all remaining usages of `HostRef` in the public API
of the `wasmtime` crate. This involved a number of API decisions such
as:
* None of `Func`, `Global`, `Table`, or `Memory` are wrapped in `HostRef`
* All of `Func`, `Global`, `Table`, and `Memory` implement `Clone` now.
* Methods called `type` are renamed to `ty` to avoid typing `r#type`.
* Methods requiring mutability for external items now no longer require
mutability. The mutable reference here is sort of a lie anyway since
the internals are aliased by the underlying module anyway. This
affects:
* `Table::set`
* `Table::grow`
* `Memory::grow`
* `Instance::set_signal_handler`
* The `Val::FuncRef` type is now no longer automatically coerced to
`AnyRef`. This is technically a breaking change which is pretty bad,
but I'm hoping that we can live with this interim state while we sort
out the `AnyRef` story in general.
* The implementation of the C API was refactored and updated in a few
locations to account for these changes:
* Accessing the exports of an instance are now cached to ensure we
always hand out the same `HostRef` values.
* `wasm_*_t` for external values no longer have internal cache,
instead they all wrap `wasm_external_t` and have an unchecked
accessor for the underlying variant (since the type is proof that
it's there). This makes casting back and forth much more trivial.
This is all related to #708 and while there's still more work to be done
in terms of documentation, this is the major bulk of the rest of the
implementation work on #708 I believe.
* More API updates
* Run rustfmt
* Fix a doc test
* More test updates
89 lines
3.0 KiB
Rust
89 lines
3.0 KiB
Rust
//! Dummy implementations of things that a Wasm module can import.
|
|
|
|
use std::rc::Rc;
|
|
use wasmtime::{
|
|
Callable, Extern, ExternType, Func, FuncType, Global, GlobalType, ImportType, Memory,
|
|
MemoryType, Store, Table, TableType, Trap, Val, ValType,
|
|
};
|
|
|
|
/// Create a set of dummy functions/globals/etc for the given imports.
|
|
pub fn dummy_imports(store: &Store, import_tys: &[ImportType]) -> Result<Vec<Extern>, Trap> {
|
|
let mut imports = Vec::with_capacity(import_tys.len());
|
|
for imp in import_tys {
|
|
imports.push(match imp.ty() {
|
|
ExternType::Func(func_ty) => Extern::Func(DummyFunc::new(&store, func_ty.clone())),
|
|
ExternType::Global(global_ty) => {
|
|
Extern::Global(dummy_global(&store, global_ty.clone())?)
|
|
}
|
|
ExternType::Table(table_ty) => Extern::Table(dummy_table(&store, table_ty.clone())?),
|
|
ExternType::Memory(mem_ty) => Extern::Memory(dummy_memory(&store, mem_ty.clone())),
|
|
});
|
|
}
|
|
Ok(imports)
|
|
}
|
|
|
|
/// A function that doesn't do anything but return the default (zero) value for
|
|
/// the function's type.
|
|
#[derive(Debug)]
|
|
pub struct DummyFunc(FuncType);
|
|
|
|
impl DummyFunc {
|
|
/// Construct a new dummy `Func`.
|
|
pub fn new(store: &Store, ty: FuncType) -> Func {
|
|
let callable = DummyFunc(ty.clone());
|
|
Func::new(store, ty, Rc::new(callable) as _)
|
|
}
|
|
}
|
|
|
|
impl Callable for DummyFunc {
|
|
fn call(&self, _params: &[Val], results: &mut [Val]) -> Result<(), Trap> {
|
|
for (ret_ty, result) in self.0.results().iter().zip(results) {
|
|
*result = dummy_value(ret_ty)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
/// Construct a dummy value for the given value type.
|
|
pub fn dummy_value(val_ty: &ValType) -> Result<Val, Trap> {
|
|
Ok(match val_ty {
|
|
ValType::I32 => Val::I32(0),
|
|
ValType::I64 => Val::I64(0),
|
|
ValType::F32 => Val::F32(0),
|
|
ValType::F64 => Val::F64(0),
|
|
ValType::V128 => {
|
|
return Err(Trap::new(
|
|
"dummy_value: unsupported function return type: v128".to_string(),
|
|
))
|
|
}
|
|
ValType::AnyRef => {
|
|
return Err(Trap::new(
|
|
"dummy_value: unsupported function return type: anyref".to_string(),
|
|
))
|
|
}
|
|
ValType::FuncRef => {
|
|
return Err(Trap::new(
|
|
"dummy_value: unsupported function return type: funcref".to_string(),
|
|
))
|
|
}
|
|
})
|
|
}
|
|
|
|
/// Construct a dummy global for the given global type.
|
|
pub fn dummy_global(store: &Store, ty: GlobalType) -> Result<Global, Trap> {
|
|
let val = dummy_value(ty.content())?;
|
|
Ok(Global::new(store, ty, val))
|
|
}
|
|
|
|
/// Construct a dummy table for the given table type.
|
|
pub fn dummy_table(store: &Store, ty: TableType) -> Result<Table, Trap> {
|
|
let init_val = dummy_value(&ty.element())?;
|
|
Ok(Table::new(store, ty, init_val))
|
|
}
|
|
|
|
/// Construct a dummy memory for the given memory type.
|
|
pub fn dummy_memory(store: &Store, ty: MemoryType) -> Memory {
|
|
Memory::new(store, ty)
|
|
}
|