* 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
96 lines
3.0 KiB
Rust
96 lines
3.0 KiB
Rust
use anyhow::{bail, Context};
|
|
use std::fs::File;
|
|
use std::path::Path;
|
|
use wasmtime::{Instance, Module, Store};
|
|
|
|
pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> anyhow::Result<()> {
|
|
let store = Store::default();
|
|
|
|
let global_exports = store.global_exports().clone();
|
|
let get_preopens = |workspace: Option<&Path>| -> anyhow::Result<Vec<_>> {
|
|
if let Some(workspace) = workspace {
|
|
let preopen_dir = wasi_common::preopen_dir(workspace)
|
|
.context(format!("error while preopening {:?}", workspace))?;
|
|
|
|
Ok(vec![(".".to_owned(), preopen_dir)])
|
|
} else {
|
|
Ok(vec![])
|
|
}
|
|
};
|
|
|
|
// Create our wasi context with pretty standard arguments/inheritance/etc.
|
|
// Additionally register andy preopened directories if we have them.
|
|
let mut builder = wasi_common::WasiCtxBuilder::new()
|
|
.arg(bin_name)
|
|
.arg(".")
|
|
.inherit_stdio();
|
|
for (dir, file) in get_preopens(workspace)? {
|
|
builder = builder.preopened_dir(file, dir);
|
|
}
|
|
|
|
// The nonstandard thing we do with `WasiCtxBuilder` is to ensure that
|
|
// `stdin` is always an unreadable pipe. This is expected in the test suite
|
|
// where `stdin` is never ready to be read. In some CI systems, however,
|
|
// stdin is closed which causes tests to fail.
|
|
let (reader, _writer) = os_pipe::pipe()?;
|
|
builder = builder.stdin(reader_to_file(reader));
|
|
let snapshot1 = Instance::from_handle(
|
|
&store,
|
|
wasmtime_wasi::instantiate_wasi_with_context(
|
|
global_exports.clone(),
|
|
builder.build().context("failed to build wasi context")?,
|
|
)
|
|
.context("failed to instantiate wasi")?,
|
|
);
|
|
|
|
let module = Module::new(&store, &data).context("failed to create wasm module")?;
|
|
let imports = module
|
|
.imports()
|
|
.iter()
|
|
.map(|i| {
|
|
let field_name = i.name();
|
|
if let Some(export) = snapshot1.find_export_by_name(field_name) {
|
|
Ok(export.clone())
|
|
} else {
|
|
bail!(
|
|
"import {} was not found in module {}",
|
|
field_name,
|
|
i.module(),
|
|
)
|
|
}
|
|
})
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
let instance = Instance::new(&store, &module, &imports).context(format!(
|
|
"error while instantiating Wasm module '{}'",
|
|
bin_name,
|
|
))?;
|
|
|
|
let export = instance
|
|
.find_export_by_name("_start")
|
|
.context("expected a _start export")?
|
|
.clone();
|
|
|
|
if let Err(trap) = export
|
|
.func()
|
|
.context("expected export to be a func")?
|
|
.call(&[])
|
|
{
|
|
bail!("trapped: {:?}", trap);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
fn reader_to_file(reader: os_pipe::PipeReader) -> File {
|
|
use std::os::unix::prelude::*;
|
|
unsafe { File::from_raw_fd(reader.into_raw_fd()) }
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
fn reader_to_file(reader: os_pipe::PipeReader) -> File {
|
|
use std::os::windows::prelude::*;
|
|
unsafe { File::from_raw_handle(reader.into_raw_handle()) }
|
|
}
|