Skip to content

Commit 328de8b

Browse files
authored
Add APIs to lookup values in Linker (#1480)
* Add APIs to lookup values in `Linker` This commit adds three new methods to `Linker` in order to inspect it after values have been inserted: * `Linker::iter` - iterates over all defined values * `Linker::get` - lookup a value by its `ImportType` * `Linker::get_by_name` - lookup values based on their name Closes #1454 * More apis!
1 parent 1a2eccc commit 328de8b

File tree

2 files changed

+109
-33
lines changed

2 files changed

+109
-33
lines changed

crates/api/src/linker.rs

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
Extern, ExternType, Func, FuncType, GlobalType, ImportType, Instance, IntoFunc, Module, Store,
33
};
4-
use anyhow::{bail, Result};
4+
use anyhow::{anyhow, bail, Result};
55
use std::collections::hash_map::{Entry, HashMap};
66
use std::rc::Rc;
77

@@ -270,6 +270,27 @@ impl Linker {
270270
Ok(self)
271271
}
272272

273+
/// Aliases one module's name as another.
274+
///
275+
/// This method will alias all currently defined under `module` to also be
276+
/// defined under the name `as_module` too.
277+
///
278+
/// # Errors
279+
///
280+
/// Returns an error if any shadowing violations happen while defining new
281+
/// items.
282+
pub fn alias(&mut self, module: &str, as_module: &str) -> Result<()> {
283+
let items = self
284+
.iter()
285+
.filter(|(m, _, _)| *m == module)
286+
.map(|(_, name, item)| (name.to_string(), item.clone()))
287+
.collect::<Vec<_>>();
288+
for (name, item) in items {
289+
self.define(as_module, &name, item)?;
290+
}
291+
Ok(())
292+
}
293+
273294
fn insert(&mut self, module: &str, name: &str, ty: &ExternType, item: Extern) -> Result<()> {
274295
let key = self.import_key(module, name, ty);
275296
match self.map.entry(key) {
@@ -357,7 +378,7 @@ impl Linker {
357378
pub fn instantiate(&self, module: &Module) -> Result<Instance> {
358379
let mut imports = Vec::new();
359380
for import in module.imports() {
360-
if let Some(item) = self.import_get(import) {
381+
if let Some(item) = self.get(import) {
361382
imports.push(item.clone());
362383
continue;
363384
}
@@ -395,7 +416,30 @@ impl Linker {
395416
Instance::new(module, &imports)
396417
}
397418

398-
fn import_get(&self, import: &ImportType) -> Option<&Extern> {
419+
/// Returns the [`Store`] that this linker is connected to.
420+
pub fn store(&self) -> &Store {
421+
&self.store
422+
}
423+
424+
/// Returns an iterator over all items defined in this `Linker`.
425+
///
426+
/// The iterator returned will yield 3-tuples where the first two elements
427+
/// are the module name and item name for the external item, and the third
428+
/// item is the item itself that is defined.
429+
///
430+
/// Note that multiple `Extern` items may be defined for the same
431+
/// module/name pair.
432+
pub fn iter(&self) -> impl Iterator<Item = (&str, &str, &Extern)> {
433+
self.map
434+
.iter()
435+
.map(move |(key, item)| (&*self.strings[key.module], &*self.strings[key.name], item))
436+
}
437+
438+
/// Looks up a value in this `Linker` which matches the `import` type
439+
/// provided.
440+
///
441+
/// Returns `None` if no match was found.
442+
pub fn get(&self, import: &ImportType) -> Option<&Extern> {
399443
let key = ImportKey {
400444
module: *self.string2idx.get(import.module())?,
401445
name: *self.string2idx.get(import.name())?,
@@ -404,8 +448,37 @@ impl Linker {
404448
self.map.get(&key)
405449
}
406450

407-
/// Returns the [`Store`] that this linker is connected to.
408-
pub fn store(&self) -> &Store {
409-
&self.store
451+
/// Returns all items defined for the `module` and `name` pair.
452+
///
453+
/// This may return an empty iterator, but it may also return multiple items
454+
/// if the module/name have been defined twice.
455+
pub fn get_by_name<'a: 'p, 'p>(
456+
&'a self,
457+
module: &'p str,
458+
name: &'p str,
459+
) -> impl Iterator<Item = &'a Extern> + 'p {
460+
self.map
461+
.iter()
462+
.filter(move |(key, _item)| {
463+
&*self.strings[key.module] == module && &*self.strings[key.name] == name
464+
})
465+
.map(|(_, item)| item)
466+
}
467+
468+
/// Returns the single item defined for the `module` and `name` pair.
469+
///
470+
/// Unlike the similar [`Linker::get_by_name`] method this function returns
471+
/// a single `&Extern` item. If the `module` and `name` pair isn't defined
472+
/// in this linker then an error is returned. If more than one value exists
473+
/// for the `module` and `name` pairs, then an error is returned as well.
474+
pub fn get_one_by_name(&self, module: &str, name: &str) -> Result<&Extern> {
475+
let mut items = self.get_by_name(module, name);
476+
let ret = items
477+
.next()
478+
.ok_or_else(|| anyhow!("no item named `{}` in `{}`", name, module))?;
479+
if items.next().is_some() {
480+
bail!("too many items named `{}` in `{}`", name, module);
481+
}
482+
Ok(ret)
410483
}
411484
}

crates/wast/src/wast.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::spectest::link_spectest;
22
use anyhow::{anyhow, bail, Context as _, Result};
3-
use std::collections::HashMap;
43
use std::path::Path;
54
use std::str;
65
use wasmtime::*;
@@ -28,8 +27,9 @@ pub struct WastContext {
2827
/// Wast files have a concept of a "current" module, which is the most
2928
/// recently defined.
3029
current: Option<Instance>,
31-
32-
instances: HashMap<String, Instance>,
30+
// FIXME(#1479) this is only needed to retain correct trap information after
31+
// we've dropped previous `Instance` values.
32+
modules: Vec<Module>,
3333
linker: Linker,
3434
store: Store,
3535
}
@@ -58,28 +58,27 @@ impl WastContext {
5858
linker.allow_shadowing(true);
5959
Self {
6060
current: None,
61-
instances: HashMap::new(),
6261
linker,
6362
store,
63+
modules: Vec::new(),
6464
}
6565
}
6666

67-
fn get_instance(&self, instance_name: Option<&str>) -> Result<Instance> {
68-
match instance_name {
69-
Some(name) => self
70-
.instances
71-
.get(name)
72-
.cloned()
73-
.ok_or_else(|| anyhow!("failed to find instance named `{}`", name)),
67+
fn get_export(&self, module: Option<&str>, name: &str) -> Result<&Extern> {
68+
match module {
69+
Some(module) => self.linker.get_one_by_name(module, name),
7470
None => self
7571
.current
76-
.clone()
77-
.ok_or_else(|| anyhow!("no previous instance found")),
72+
.as_ref()
73+
.ok_or_else(|| anyhow!("no previous instance found"))?
74+
.get_export(name)
75+
.ok_or_else(|| anyhow!("no item named `{}` found", name)),
7876
}
7977
}
8078

81-
fn instantiate(&self, module: &[u8]) -> Result<Outcome<Instance>> {
79+
fn instantiate(&mut self, module: &[u8]) -> Result<Outcome<Instance>> {
8280
let module = Module::new(&self.store, module)?;
81+
self.modules.push(module.clone());
8382
let instance = match self.linker.instantiate(&module) {
8483
Ok(i) => i,
8584
Err(e) => return e.downcast::<Trap>().map(Outcome::Trap),
@@ -125,7 +124,6 @@ impl WastContext {
125124
Outcome::Trap(e) => bail!("instantiation failed with: {}", e.message()),
126125
};
127126
if let Some(name) = instance_name {
128-
self.instances.insert(name.to_string(), instance.clone());
129127
self.linker.instance(name, &instance)?;
130128
}
131129
self.current = Some(instance);
@@ -134,10 +132,17 @@ impl WastContext {
134132

135133
/// Register an instance to make it available for performing actions.
136134
fn register(&mut self, name: Option<&str>, as_name: &str) -> Result<()> {
137-
let instance = self.get_instance(name)?.clone();
138-
self.linker.instance(as_name, &instance)?;
139-
self.instances.insert(as_name.to_string(), instance);
140-
Ok(())
135+
match name {
136+
Some(name) => self.linker.alias(name, as_name),
137+
None => {
138+
let current = self
139+
.current
140+
.as_ref()
141+
.ok_or(anyhow!("no previous instance"))?;
142+
self.linker.instance(as_name, current)?;
143+
Ok(())
144+
}
145+
}
141146
}
142147

143148
/// Invoke an exported function from an instance.
@@ -147,10 +152,9 @@ impl WastContext {
147152
field: &str,
148153
args: &[Val],
149154
) -> Result<Outcome> {
150-
let instance = self.get_instance(instance_name.as_ref().map(|x| &**x))?;
151-
let func = instance
152-
.get_export(field)
153-
.and_then(|e| e.func())
155+
let func = self
156+
.get_export(instance_name, field)?
157+
.func()
154158
.ok_or_else(|| anyhow!("no function named `{}`", field))?;
155159
Ok(match func.call(args) {
156160
Ok(result) => Outcome::Ok(result.into()),
@@ -160,10 +164,9 @@ impl WastContext {
160164

161165
/// Get the value of an exported global from an instance.
162166
fn get(&mut self, instance_name: Option<&str>, field: &str) -> Result<Outcome> {
163-
let instance = self.get_instance(instance_name.as_ref().map(|x| &**x))?;
164-
let global = instance
165-
.get_export(field)
166-
.and_then(|e| e.global())
167+
let global = self
168+
.get_export(instance_name, field)?
169+
.global()
167170
.ok_or_else(|| anyhow!("no global named `{}`", field))?;
168171
Ok(Outcome::Ok(vec![global.get()]))
169172
}

0 commit comments

Comments
 (0)