Remove HostRef from the wasmtime public API (#788)

* 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
This commit is contained in:
Alex Crichton
2020-01-10 10:42:14 -06:00
committed by GitHub
parent 068c249439
commit 6571fb8f4f
25 changed files with 435 additions and 489 deletions

View File

@@ -125,18 +125,17 @@ impl ModuleData {
/// wasm interface types.
pub fn invoke_export(
&self,
instance: &wasmtime::HostRef<wasmtime::Instance>,
instance: &wasmtime::Instance,
export: &str,
args: &[Value],
) -> Result<Vec<Value>> {
let mut handle = instance.borrow().handle().clone();
let mut handle = instance.handle().clone();
let binding = self.binding_for_export(&mut handle, export)?;
let incoming = binding.param_bindings()?;
let outgoing = binding.result_bindings()?;
let f = instance
.borrow()
.find_export_by_name(export)
.ok_or_else(|| format_err!("failed to find export `{}`", export))?
.func()
@@ -148,7 +147,7 @@ impl ModuleData {
.into_iter()
.map(|rv| rv.into())
.collect::<Vec<_>>();
let wasm_results = match f.borrow().call(&wasm_args) {
let wasm_results = match f.call(&wasm_args) {
Ok(values) => values
.to_vec()
.into_iter()
@@ -322,20 +321,19 @@ trait TranslateContext {
unsafe fn get_memory(&mut self) -> Result<&mut [u8]>;
}
struct InstanceTranslateContext(pub wasmtime::HostRef<wasmtime::Instance>);
struct InstanceTranslateContext(pub wasmtime::Instance);
impl TranslateContext for InstanceTranslateContext {
fn invoke_alloc(&mut self, alloc_func_name: &str, len: i32) -> Result<i32> {
let alloc = self
.0
.borrow()
.find_export_by_name(alloc_func_name)
.ok_or_else(|| format_err!("failed to find alloc function `{}`", alloc_func_name))?
.func()
.ok_or_else(|| format_err!("`{}` is not a (alloc) function", alloc_func_name))?
.clone();
let alloc_args = vec![wasmtime::Val::I32(len)];
let results = match alloc.borrow().call(&alloc_args) {
let results = match alloc.call(&alloc_args) {
Ok(values) => values,
Err(trap) => bail!("trapped: {:?}", trap),
};
@@ -350,14 +348,13 @@ impl TranslateContext for InstanceTranslateContext {
unsafe fn get_memory(&mut self) -> Result<&mut [u8]> {
let memory = self
.0
.borrow()
.find_export_by_name("memory")
.ok_or_else(|| format_err!("failed to find `memory` export"))?
.memory()
.ok_or_else(|| format_err!("`memory` is not a memory"))?
.clone();
let ptr = memory.borrow().data_ptr();
let len = memory.borrow().data_size();
let ptr = memory.data_ptr();
let len = memory.data_size();
Ok(std::slice::from_raw_parts_mut(ptr, len))
}
}