Skip to content

Commit a6ab235

Browse files
committed
Factor out instance initialization with NewInstance.
This also separates instantiation from initialization in a manner similar to bytecodealliance/lucet#506.
1 parent f40f61e commit a6ab235

34 files changed

Lines changed: 339 additions & 210 deletions

RELEASES.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Unreleased
88

99
### Added
1010

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

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

crates/c-api/src/instance.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{wasm_store_t, wasmtime_error_t, ExternHost};
33
use anyhow::Result;
44
use std::cell::RefCell;
55
use std::ptr;
6-
use wasmtime::{Extern, HostRef, Instance, Store, Trap};
6+
use wasmtime::{Extern, HostRef, Instance, NewInstance, Store, Trap};
77

88
#[repr(C)]
99
#[derive(Clone)]
@@ -115,14 +115,17 @@ fn _wasmtime_instance_new(
115115
}
116116

117117
pub fn handle_instantiate(
118-
instance: Result<Instance>,
118+
instance: Result<NewInstance>,
119119
instance_ptr: &mut *mut wasm_instance_t,
120120
trap_ptr: &mut *mut wasm_trap_t,
121121
) -> Option<Box<wasmtime_error_t>> {
122122
fn write<T>(ptr: &mut *mut T, val: T) {
123123
*ptr = Box::into_raw(Box::new(val))
124124
}
125125

126+
// Run the wasm start function.
127+
let instance = instance.and_then(NewInstance::minimal_init);
128+
126129
match instance {
127130
Ok(instance) => {
128131
write(instance_ptr, wasm_instance_t::new(instance));

crates/test-programs/tests/wasm_tests/runtime.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,19 @@ pub fn instantiate(
6565
})
6666
.collect::<Result<Vec<_>, _>>()?;
6767

68-
let instance = Instance::new_wasi_abi(&module, &imports).context(format!(
69-
"error while instantiating Wasm module '{}'",
70-
bin_name,
71-
))?;
72-
73-
// If `module` is a command, `Instance::new_wasi_abi` will run it and
74-
// return `None`. If we get `Some`, it means `module` wasn't a command.
75-
if instance.is_some() {
76-
bail!("expected module to be a command with a \"_start\" function")
77-
}
78-
79-
Ok(())
68+
Instance::new(&module, &imports)
69+
.and_then(|new_instance| new_instance.run_command(&[]))
70+
.and_then(|results| {
71+
if !results.is_empty() {
72+
bail!("unexpected result values from command")
73+
} else {
74+
Ok(())
75+
}
76+
})
77+
.context(format!(
78+
"error while instantiating Wasm module '{}'",
79+
bin_name,
80+
))
8081
}
8182

8283
#[cfg(unix)]

crates/wasmtime/src/externals.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ impl Memory {
665665
/// let memory = Memory::new(&store, memory_ty);
666666
///
667667
/// let module = Module::new(&store, "(module (memory (import \"\" \"\") 1))")?;
668-
/// let instance = Instance::new(&module, &[memory.into()])?;
668+
/// let instance = Instance::new(&module, &[memory.into()])?.init_reactor(&[])?;
669669
/// // ...
670670
/// # Ok(())
671671
/// # }
@@ -688,7 +688,7 @@ impl Memory {
688688
/// # fn main() -> anyhow::Result<()> {
689689
/// let store = Store::default();
690690
/// let module = Module::new(&store, "(module (memory (export \"mem\") 1))")?;
691-
/// let instance = Instance::new(&module, &[])?;
691+
/// let instance = Instance::new(&module, &[])?.init_reactor(&[])?;
692692
/// let memory = instance.get_memory("mem").unwrap();
693693
/// let ty = memory.ty();
694694
/// assert_eq!(ty.limits().min(), 1);
@@ -800,7 +800,7 @@ impl Memory {
800800
/// # fn main() -> anyhow::Result<()> {
801801
/// let store = Store::default();
802802
/// let module = Module::new(&store, "(module (memory (export \"mem\") 1 2))")?;
803-
/// let instance = Instance::new(&module, &[])?;
803+
/// let instance = Instance::new(&module, &[])?.init_reactor(&[])?;
804804
/// let memory = instance.get_memory("mem").unwrap();
805805
///
806806
/// assert_eq!(memory.size(), 1);

crates/wasmtime/src/func.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use wasmtime_runtime::{Export, InstanceHandle, VMContext, VMFunctionBody};
4040
/// # fn main() -> anyhow::Result<()> {
4141
/// let store = Store::default();
4242
/// let module = Module::new(&store, r#"(module (func (export "foo")))"#)?;
43-
/// let instance = Instance::new(&module, &[])?;
43+
/// let instance = Instance::new(&module, &[])?.init_reactor(&[])?;
4444
/// let foo = instance.get_func("foo").expect("export wasn't a function");
4545
///
4646
/// // Work with `foo` as a `Func` at this point, such as calling it
@@ -90,7 +90,7 @@ use wasmtime_runtime::{Export, InstanceHandle, VMContext, VMFunctionBody};
9090
/// i32.add))
9191
/// "#,
9292
/// )?;
93-
/// let instance = Instance::new(&module, &[add.into()])?;
93+
/// let instance = Instance::new(&module, &[add.into()])?.init_reactor(&[])?;
9494
/// let call_add_twice = instance.get_func("call_add_twice").expect("export wasn't a function");
9595
/// let call_add_twice = call_add_twice.get0::<i32>()?;
9696
///
@@ -131,7 +131,7 @@ use wasmtime_runtime::{Export, InstanceHandle, VMContext, VMFunctionBody};
131131
/// (start $start))
132132
/// "#,
133133
/// )?;
134-
/// let instance = Instance::new(&module, &[double.into()])?;
134+
/// let instance = Instance::new(&module, &[double.into()])?.init_reactor(&[])?;
135135
/// // .. work with `instance` if necessary
136136
/// # Ok(())
137137
/// # }
@@ -344,7 +344,7 @@ impl Func {
344344
/// call $add))
345345
/// "#,
346346
/// )?;
347-
/// let instance = Instance::new(&module, &[add.into()])?;
347+
/// let instance = Instance::new(&module, &[add.into()])?.init_reactor(&[])?;
348348
/// let foo = instance.get_func("foo").unwrap().get2::<i32, i32, i32>()?;
349349
/// assert_eq!(foo(1, 2)?, 3);
350350
/// # Ok(())
@@ -375,7 +375,7 @@ impl Func {
375375
/// call $add))
376376
/// "#,
377377
/// )?;
378-
/// let instance = Instance::new(&module, &[add.into()])?;
378+
/// let instance = Instance::new(&module, &[add.into()])?.init_reactor(&[])?;
379379
/// let foo = instance.get_func("foo").unwrap().get2::<i32, i32, i32>()?;
380380
/// assert_eq!(foo(1, 2)?, 3);
381381
/// assert!(foo(i32::max_value(), 1).is_err());
@@ -408,7 +408,7 @@ impl Func {
408408
/// call $debug))
409409
/// "#,
410410
/// )?;
411-
/// let instance = Instance::new(&module, &[debug.into()])?;
411+
/// let instance = Instance::new(&module, &[debug.into()])?.init_reactor(&[])?;
412412
/// let foo = instance.get_func("foo").unwrap().get0::<()>()?;
413413
/// foo()?;
414414
/// # Ok(())
@@ -464,7 +464,7 @@ impl Func {
464464
/// (data (i32.const 4) "Hello, world!"))
465465
/// "#,
466466
/// )?;
467-
/// let instance = Instance::new(&module, &[log_str.into()])?;
467+
/// let instance = Instance::new(&module, &[log_str.into()])?.init_reactor(&[])?;
468468
/// let foo = instance.get_func("foo").unwrap().get0::<()>()?;
469469
/// foo()?;
470470
/// # Ok(())

0 commit comments

Comments
 (0)