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:
@@ -27,9 +27,9 @@ fn runtime_value(v: &wast::Expression<'_>) -> Result<Val> {
|
||||
pub struct WastContext {
|
||||
/// Wast files have a concept of a "current" module, which is the most
|
||||
/// recently defined.
|
||||
current: Option<HostRef<Instance>>,
|
||||
current: Option<Instance>,
|
||||
|
||||
instances: HashMap<String, HostRef<Instance>>,
|
||||
instances: HashMap<String, Instance>,
|
||||
store: Store,
|
||||
spectest: Option<HashMap<&'static str, Extern>>,
|
||||
}
|
||||
@@ -50,7 +50,7 @@ impl WastContext {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_instance(&self, instance_name: Option<&str>) -> Result<HostRef<Instance>> {
|
||||
fn get_instance(&self, instance_name: Option<&str>) -> Result<Instance> {
|
||||
match instance_name {
|
||||
Some(name) => self
|
||||
.instances
|
||||
@@ -64,7 +64,7 @@ impl WastContext {
|
||||
}
|
||||
}
|
||||
|
||||
fn instantiate(&self, module: &[u8]) -> Result<Outcome<HostRef<Instance>>> {
|
||||
fn instantiate(&self, module: &[u8]) -> Result<Outcome<Instance>> {
|
||||
let module = Module::new(&self.store, module)?;
|
||||
let mut imports = Vec::new();
|
||||
for import in module.imports() {
|
||||
@@ -85,7 +85,6 @@ impl WastContext {
|
||||
.get(import.module())
|
||||
.ok_or_else(|| anyhow!("no module named `{}`", import.module()))?;
|
||||
let export = instance
|
||||
.borrow()
|
||||
.find_export_by_name(import.name())
|
||||
.ok_or_else(|| anyhow!("unknown import `{}::{}`", import.name(), import.module()))?
|
||||
.clone();
|
||||
@@ -101,7 +100,7 @@ impl WastContext {
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
Ok(Outcome::Ok(HostRef::new(instance)))
|
||||
Ok(Outcome::Ok(instance))
|
||||
}
|
||||
|
||||
/// Register "spectest" which is used by the spec testsuite.
|
||||
@@ -159,12 +158,11 @@ impl WastContext {
|
||||
) -> Result<Outcome> {
|
||||
let values = args.iter().map(runtime_value).collect::<Result<Vec<_>>>()?;
|
||||
let instance = self.get_instance(instance_name.as_ref().map(|x| &**x))?;
|
||||
let instance = instance.borrow();
|
||||
let export = instance
|
||||
.find_export_by_name(field)
|
||||
.ok_or_else(|| anyhow!("no global named `{}`", field))?;
|
||||
let func = match export {
|
||||
Extern::Func(f) => f.borrow(),
|
||||
Extern::Func(f) => f,
|
||||
_ => bail!("export of `{}` wasn't a global", field),
|
||||
};
|
||||
Ok(match func.call(&values) {
|
||||
@@ -176,12 +174,11 @@ impl WastContext {
|
||||
/// Get the value of an exported global from an instance.
|
||||
fn get(&mut self, instance_name: Option<&str>, field: &str) -> Result<Outcome> {
|
||||
let instance = self.get_instance(instance_name.as_ref().map(|x| &**x))?;
|
||||
let instance = instance.borrow();
|
||||
let export = instance
|
||||
.find_export_by_name(field)
|
||||
.ok_or_else(|| anyhow!("no global named `{}`", field))?;
|
||||
let global = match export {
|
||||
Extern::Global(g) => g.borrow(),
|
||||
Extern::Global(g) => g,
|
||||
_ => bail!("export of `{}` wasn't a global", field),
|
||||
};
|
||||
Ok(Outcome::Ok(vec![global.get()]))
|
||||
|
||||
Reference in New Issue
Block a user