Skip to content

Commit

Permalink
Factor out instance initialization with NewInstance.
Browse files Browse the repository at this point in the history
This also separates instantiation from initialization in a manner
similar to bytecodealliance/lucet#506.
  • Loading branch information
sunfishcode committed May 18, 2020
1 parent 15831f3 commit aa8c1f5
Show file tree
Hide file tree
Showing 34 changed files with 339 additions and 210 deletions.
8 changes: 4 additions & 4 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Unreleased

### Added

* The [WASI commands and reactors ABI] is now supported. To use, create a
`Linker` instance with `wasmtime_wasi::wasi_linker`, and instantiate modules
with `Linker::instantiate_wasi_abi`. This will automatically run commands
and automatically initialize reactors.
* The [WASI commands and reactors ABI] is now supported. `Instance::new` and
`Linker::instantiate` now return a `NewInstance`; to perform initialization
and obtain the `Instance`, call `.start`, `.run_command`, or
`.init_reactor` on it as needed.

[WASI commands and reactors ABI]: https://github.com/WebAssembly/WASI/blob/master/design/application-abi.md#current-unstable-abi

Expand Down
7 changes: 5 additions & 2 deletions crates/c-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{wasm_store_t, wasmtime_error_t, ExternHost};
use anyhow::Result;
use std::cell::RefCell;
use std::ptr;
use wasmtime::{Extern, HostRef, Instance, Store, Trap};
use wasmtime::{Extern, HostRef, Instance, NewInstance, Store, Trap};

#[repr(C)]
#[derive(Clone)]
Expand Down Expand Up @@ -115,14 +115,17 @@ fn _wasmtime_instance_new(
}

pub fn handle_instantiate(
instance: Result<Instance>,
instance: Result<NewInstance>,
instance_ptr: &mut *mut wasm_instance_t,
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
fn write<T>(ptr: &mut *mut T, val: T) {
*ptr = Box::into_raw(Box::new(val))
}

// Run the wasm start function.
let instance = instance.and_then(NewInstance::minimal_init);

match instance {
Ok(instance) => {
write(instance_ptr, wasm_instance_t::new(instance));
Expand Down
25 changes: 13 additions & 12 deletions crates/test-programs/tests/wasm_tests/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,19 @@ pub fn instantiate(
})
.collect::<Result<Vec<_>, _>>()?;

let instance = Instance::new_wasi_abi(&module, &imports).context(format!(
"error while instantiating Wasm module '{}'",
bin_name,
))?;

// If `module` is a command, `Instance::new_wasi_abi` will run it and
// return `None`. If we get `Some`, it means `module` wasn't a command.
if instance.is_some() {
bail!("expected module to be a command with a \"_start\" function")
}

Ok(())
Instance::new(&module, &imports)
.and_then(|new_instance| new_instance.run_command(&[]))
.and_then(|results| {
if !results.is_empty() {
bail!("unexpected result values from command")
} else {
Ok(())
}
})
.context(format!(
"error while instantiating Wasm module '{}'",
bin_name,
))
}

#[cfg(unix)]
Expand Down
6 changes: 3 additions & 3 deletions crates/wasmtime/src/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ impl Memory {
/// let memory = Memory::new(&store, memory_ty);
///
/// let module = Module::new(&store, "(module (memory (import \"\" \"\") 1))")?;
/// let instance = Instance::new(&module, &[memory.into()])?;
/// let instance = Instance::new(&module, &[memory.into()])?.init_reactor(&[])?;
/// // ...
/// # Ok(())
/// # }
Expand All @@ -688,7 +688,7 @@ impl Memory {
/// # fn main() -> anyhow::Result<()> {
/// let store = Store::default();
/// let module = Module::new(&store, "(module (memory (export \"mem\") 1))")?;
/// let instance = Instance::new(&module, &[])?;
/// let instance = Instance::new(&module, &[])?.init_reactor(&[])?;
/// let memory = instance.get_memory("mem").unwrap();
/// let ty = memory.ty();
/// assert_eq!(ty.limits().min(), 1);
Expand Down Expand Up @@ -800,7 +800,7 @@ impl Memory {
/// # fn main() -> anyhow::Result<()> {
/// let store = Store::default();
/// let module = Module::new(&store, "(module (memory (export \"mem\") 1 2))")?;
/// let instance = Instance::new(&module, &[])?;
/// let instance = Instance::new(&module, &[])?.init_reactor(&[])?;
/// let memory = instance.get_memory("mem").unwrap();
///
/// assert_eq!(memory.size(), 1);
Expand Down
14 changes: 7 additions & 7 deletions crates/wasmtime/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use wasmtime_runtime::{Export, InstanceHandle, VMContext, VMFunctionBody};
/// # fn main() -> anyhow::Result<()> {
/// let store = Store::default();
/// let module = Module::new(&store, r#"(module (func (export "foo")))"#)?;
/// let instance = Instance::new(&module, &[])?;
/// let instance = Instance::new(&module, &[])?.init_reactor(&[])?;
/// let foo = instance.get_func("foo").expect("export wasn't a function");
///
/// // Work with `foo` as a `Func` at this point, such as calling it
Expand Down Expand Up @@ -90,7 +90,7 @@ use wasmtime_runtime::{Export, InstanceHandle, VMContext, VMFunctionBody};
/// i32.add))
/// "#,
/// )?;
/// let instance = Instance::new(&module, &[add.into()])?;
/// let instance = Instance::new(&module, &[add.into()])?.init_reactor(&[])?;
/// let call_add_twice = instance.get_func("call_add_twice").expect("export wasn't a function");
/// let call_add_twice = call_add_twice.get0::<i32>()?;
///
Expand Down Expand Up @@ -131,7 +131,7 @@ use wasmtime_runtime::{Export, InstanceHandle, VMContext, VMFunctionBody};
/// (start $start))
/// "#,
/// )?;
/// let instance = Instance::new(&module, &[double.into()])?;
/// let instance = Instance::new(&module, &[double.into()])?.init_reactor(&[])?;
/// // .. work with `instance` if necessary
/// # Ok(())
/// # }
Expand Down Expand Up @@ -344,7 +344,7 @@ impl Func {
/// call $add))
/// "#,
/// )?;
/// let instance = Instance::new(&module, &[add.into()])?;
/// let instance = Instance::new(&module, &[add.into()])?.init_reactor(&[])?;
/// let foo = instance.get_func("foo").unwrap().get2::<i32, i32, i32>()?;
/// assert_eq!(foo(1, 2)?, 3);
/// # Ok(())
Expand Down Expand Up @@ -375,7 +375,7 @@ impl Func {
/// call $add))
/// "#,
/// )?;
/// let instance = Instance::new(&module, &[add.into()])?;
/// let instance = Instance::new(&module, &[add.into()])?.init_reactor(&[])?;
/// let foo = instance.get_func("foo").unwrap().get2::<i32, i32, i32>()?;
/// assert_eq!(foo(1, 2)?, 3);
/// assert!(foo(i32::max_value(), 1).is_err());
Expand Down Expand Up @@ -408,7 +408,7 @@ impl Func {
/// call $debug))
/// "#,
/// )?;
/// let instance = Instance::new(&module, &[debug.into()])?;
/// let instance = Instance::new(&module, &[debug.into()])?.init_reactor(&[])?;
/// let foo = instance.get_func("foo").unwrap().get0::<()>()?;
/// foo()?;
/// # Ok(())
Expand Down Expand Up @@ -464,7 +464,7 @@ impl Func {
/// (data (i32.const 4) "Hello, world!"))
/// "#,
/// )?;
/// let instance = Instance::new(&module, &[log_str.into()])?;
/// let instance = Instance::new(&module, &[log_str.into()])?.init_reactor(&[])?;
/// let foo = instance.get_func("foo").unwrap().get0::<()>()?;
/// foo()?;
/// # Ok(())
Expand Down
Loading

0 comments on commit aa8c1f5

Please sign in to comment.