Skip to content

Commit aa3589e

Browse files
committed
Add convenience methods to instance for getting specific extern types.
1 parent d1e639f commit aa3589e

File tree

18 files changed

+64
-74
lines changed

18 files changed

+64
-74
lines changed

crates/api/src/externals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ impl Memory {
702702
/// let store = Store::default();
703703
/// let module = Module::new(&store, "(module (memory (export \"mem\") 1))")?;
704704
/// let instance = Instance::new(&module, &[])?;
705-
/// let memory = instance.get_export("mem").unwrap().into_memory().unwrap();
705+
/// let memory = instance.get_memory("mem").unwrap();
706706
/// let ty = memory.ty();
707707
/// assert_eq!(ty.limits().min(), 1);
708708
/// # Ok(())

crates/api/src/instance.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::externals::{Export, Extern};
1+
use crate::externals::{Export, Extern, Global, Memory, Table};
2+
use crate::func::Func;
23
use crate::module::Module;
34
use crate::runtime::{Config, Store};
45
use crate::trap::Trap;
@@ -188,6 +189,38 @@ impl Instance {
188189
))
189190
}
190191

192+
/// Looks up an exported [`Func`] value by name.
193+
///
194+
/// Returns `None` if there was no export named `name`, or if there was but
195+
/// it wasn't a function.
196+
pub fn get_func(&self, name: &str) -> Option<Func> {
197+
self.get_export(name)?.into_func()
198+
}
199+
200+
/// Looks up an exported [`Table`] value by name.
201+
///
202+
/// Returns `None` if there was no export named `name`, or if there was but
203+
/// it wasn't a table.
204+
pub fn get_table(&self, name: &str) -> Option<Table> {
205+
self.get_export(name)?.into_table()
206+
}
207+
208+
/// Looks up an exported [`Memory`] value by name.
209+
///
210+
/// Returns `None` if there was no export named `name`, or if there was but
211+
/// it wasn't a memory.
212+
pub fn get_memory(&self, name: &str) -> Option<Memory> {
213+
self.get_export(name)?.into_memory()
214+
}
215+
216+
/// Looks up an exported [`Global`] value by name.
217+
///
218+
/// Returns `None` if there was no export named `name`, or if there was but
219+
/// it wasn't a global.
220+
pub fn get_global(&self, name: &str) -> Option<Global> {
221+
self.get_export(name)?.into_global()
222+
}
223+
191224
#[doc(hidden)]
192225
pub fn handle(&self) -> &InstanceHandle {
193226
&self.instance_handle

crates/fuzzing/src/oracles.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,11 @@ pub fn differential_execution(
185185
// infinite loop when calling another export.
186186
init_hang_limit(&instance);
187187

188-
let f = match instance
188+
let f = instance
189189
.get_export(&name)
190190
.expect("instance should have export from module")
191-
{
192-
Extern::Func(f) => f.clone(),
193-
_ => panic!("export should be a function"),
194-
};
191+
.into_func()
192+
.expect("export should be a function");
195193

196194
let ty = f.ty();
197195
let params = match dummy::dummy_values(ty.params()) {

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,11 @@ pub fn instantiate(
5252
let imports = module
5353
.imports()
5454
.map(|i| {
55-
let field_name = i.name();
56-
if let Some(export) = snapshot1.get_export(field_name) {
55+
let field_name = i.name;
56+
if let Some(export) = snapshot1.get_export(&field_name) {
5757
Ok(export.clone().into())
5858
} else {
59-
bail!(
60-
"import {} was not found in module {}",
61-
field_name,
62-
i.module(),
63-
)
59+
bail!("import {} was not found in module {}", field_name, i.module,)
6460
}
6561
})
6662
.collect::<Result<Vec<_>, _>>()?;
@@ -70,13 +66,10 @@ pub fn instantiate(
7066
bin_name,
7167
))?;
7268

73-
let export = instance
69+
instance
7470
.get_export("_start")
7571
.context("expected a _start export")?
76-
.clone();
77-
78-
export
79-
.func()
72+
.into_func()
8073
.context("expected export to be a func")?
8174
.call(&[])?;
8275

docs/wasm-wat.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ let wat = r#"
4747
"#;
4848
let module = Module::new(&store, wat)?;
4949
let instance = Instance::new(&module, &[])?;
50-
let add = instance.get_export("add").and_then(|f| f.into_func()).unwrap();
50+
let add = instance.get_func("add").unwrap();
5151
let add = add.get2::<i32, i32, i32>()?;
5252
println!("1 + 2 = {}", add(1, 2)?);
5353
# Ok(())

examples/fib-debug/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ fn main() -> Result<()> {
2222

2323
// Invoke `fib` export
2424
let fib = instance
25-
.get_export("fib")
26-
.and_then(|e| e.into_func())
25+
.get_func("fib")
2726
.ok_or(anyhow::format_err!("failed to find `fib` function export"))?
2827
.get1::<i32, i32>()?;
2928
println!("fib(6) = {}", fib(6)?);

examples/gcd.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ fn main() -> Result<()> {
1616

1717
// Invoke `gcd` export
1818
let gcd = instance
19-
.get_export("gcd")
20-
.and_then(|e| e.into_func())
19+
.get_func("gcd")
2120
.ok_or(anyhow::format_err!("failed to find `gcd` function export"))?
2221
.get2::<i32, i32, i32>()?;
2322

examples/hello.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ fn main() -> Result<()> {
3535
// Next we poke around a bit to extract the `run` function from the module.
3636
println!("Extracting export...");
3737
let run = instance
38-
.get_export("run")
39-
.and_then(|e| e.into_func())
38+
.get_func("run")
4039
.ok_or(anyhow::format_err!("failed to find `run` function export"))?
4140
.get0::<()>()?;
4241

examples/linking.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ fn main() -> Result<()> {
2626

2727
// And with that we can perform the final link and the execute the module.
2828
let linking1 = linker.instantiate(&linking1)?;
29-
let run = linking1
30-
.get_export("run")
31-
.and_then(|e| e.into_func())
32-
.unwrap();
29+
let run = linking1.get_func("run").unwrap();
3330
let run = run.get0::<()>()?;
3431
run()?;
3532
Ok(())

examples/memory.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,18 @@ fn main() -> Result<()> {
1818

1919
// Load up our exports from the instance
2020
let memory = instance
21-
.get_export("memory")
22-
.and_then(|e| e.into_memory())
21+
.get_memory("memory")
2322
.ok_or(anyhow::format_err!("failed to find `memory` export"))?;
2423
let size = instance
25-
.get_export("size")
26-
.and_then(|e| e.into_func())
24+
.get_func("size")
2725
.ok_or(anyhow::format_err!("failed to find `size` export"))?
2826
.get0::<i32>()?;
2927
let load = instance
30-
.get_export("load")
31-
.and_then(|e| e.into_func())
28+
.get_func("load")
3229
.ok_or(anyhow::format_err!("failed to find `load` export"))?
3330
.get1::<i32, i32>()?;
3431
let store = instance
35-
.get_export("store")
36-
.and_then(|e| e.into_func())
32+
.get_func("store")
3733
.ok_or(anyhow::format_err!("failed to find `store` export"))?
3834
.get2::<i32, i32, ()>()?;
3935

0 commit comments

Comments
 (0)