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!
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
Extern, ExternType, Func, FuncType, GlobalType, ImportType, Instance, IntoFunc, Module, Store,
|
Extern, ExternType, Func, FuncType, GlobalType, ImportType, Instance, IntoFunc, Module, Store,
|
||||||
};
|
};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{anyhow, bail, Result};
|
||||||
use std::collections::hash_map::{Entry, HashMap};
|
use std::collections::hash_map::{Entry, HashMap};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@@ -270,6 +270,27 @@ impl Linker {
|
|||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Aliases one module's name as another.
|
||||||
|
///
|
||||||
|
/// This method will alias all currently defined under `module` to also be
|
||||||
|
/// defined under the name `as_module` too.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Returns an error if any shadowing violations happen while defining new
|
||||||
|
/// items.
|
||||||
|
pub fn alias(&mut self, module: &str, as_module: &str) -> Result<()> {
|
||||||
|
let items = self
|
||||||
|
.iter()
|
||||||
|
.filter(|(m, _, _)| *m == module)
|
||||||
|
.map(|(_, name, item)| (name.to_string(), item.clone()))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
for (name, item) in items {
|
||||||
|
self.define(as_module, &name, item)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn insert(&mut self, module: &str, name: &str, ty: &ExternType, item: Extern) -> Result<()> {
|
fn insert(&mut self, module: &str, name: &str, ty: &ExternType, item: Extern) -> Result<()> {
|
||||||
let key = self.import_key(module, name, ty);
|
let key = self.import_key(module, name, ty);
|
||||||
match self.map.entry(key) {
|
match self.map.entry(key) {
|
||||||
@@ -357,7 +378,7 @@ impl Linker {
|
|||||||
pub fn instantiate(&self, module: &Module) -> Result<Instance> {
|
pub fn instantiate(&self, module: &Module) -> Result<Instance> {
|
||||||
let mut imports = Vec::new();
|
let mut imports = Vec::new();
|
||||||
for import in module.imports() {
|
for import in module.imports() {
|
||||||
if let Some(item) = self.import_get(import) {
|
if let Some(item) = self.get(import) {
|
||||||
imports.push(item.clone());
|
imports.push(item.clone());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -395,7 +416,30 @@ impl Linker {
|
|||||||
Instance::new(module, &imports)
|
Instance::new(module, &imports)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn import_get(&self, import: &ImportType) -> Option<&Extern> {
|
/// Returns the [`Store`] that this linker is connected to.
|
||||||
|
pub fn store(&self) -> &Store {
|
||||||
|
&self.store
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator over all items defined in this `Linker`.
|
||||||
|
///
|
||||||
|
/// The iterator returned will yield 3-tuples where the first two elements
|
||||||
|
/// are the module name and item name for the external item, and the third
|
||||||
|
/// item is the item itself that is defined.
|
||||||
|
///
|
||||||
|
/// Note that multiple `Extern` items may be defined for the same
|
||||||
|
/// module/name pair.
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = (&str, &str, &Extern)> {
|
||||||
|
self.map
|
||||||
|
.iter()
|
||||||
|
.map(move |(key, item)| (&*self.strings[key.module], &*self.strings[key.name], item))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Looks up a value in this `Linker` which matches the `import` type
|
||||||
|
/// provided.
|
||||||
|
///
|
||||||
|
/// Returns `None` if no match was found.
|
||||||
|
pub fn get(&self, import: &ImportType) -> Option<&Extern> {
|
||||||
let key = ImportKey {
|
let key = ImportKey {
|
||||||
module: *self.string2idx.get(import.module())?,
|
module: *self.string2idx.get(import.module())?,
|
||||||
name: *self.string2idx.get(import.name())?,
|
name: *self.string2idx.get(import.name())?,
|
||||||
@@ -404,8 +448,37 @@ impl Linker {
|
|||||||
self.map.get(&key)
|
self.map.get(&key)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the [`Store`] that this linker is connected to.
|
/// Returns all items defined for the `module` and `name` pair.
|
||||||
pub fn store(&self) -> &Store {
|
///
|
||||||
&self.store
|
/// This may return an empty iterator, but it may also return multiple items
|
||||||
|
/// if the module/name have been defined twice.
|
||||||
|
pub fn get_by_name<'a: 'p, 'p>(
|
||||||
|
&'a self,
|
||||||
|
module: &'p str,
|
||||||
|
name: &'p str,
|
||||||
|
) -> impl Iterator<Item = &'a Extern> + 'p {
|
||||||
|
self.map
|
||||||
|
.iter()
|
||||||
|
.filter(move |(key, _item)| {
|
||||||
|
&*self.strings[key.module] == module && &*self.strings[key.name] == name
|
||||||
|
})
|
||||||
|
.map(|(_, item)| item)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the single item defined for the `module` and `name` pair.
|
||||||
|
///
|
||||||
|
/// Unlike the similar [`Linker::get_by_name`] method this function returns
|
||||||
|
/// a single `&Extern` item. If the `module` and `name` pair isn't defined
|
||||||
|
/// in this linker then an error is returned. If more than one value exists
|
||||||
|
/// for the `module` and `name` pairs, then an error is returned as well.
|
||||||
|
pub fn get_one_by_name(&self, module: &str, name: &str) -> Result<&Extern> {
|
||||||
|
let mut items = self.get_by_name(module, name);
|
||||||
|
let ret = items
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| anyhow!("no item named `{}` in `{}`", name, module))?;
|
||||||
|
if items.next().is_some() {
|
||||||
|
bail!("too many items named `{}` in `{}`", name, module);
|
||||||
|
}
|
||||||
|
Ok(ret)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use crate::spectest::link_spectest;
|
use crate::spectest::link_spectest;
|
||||||
use anyhow::{anyhow, bail, Context as _, Result};
|
use anyhow::{anyhow, bail, Context as _, Result};
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::str;
|
use std::str;
|
||||||
use wasmtime::*;
|
use wasmtime::*;
|
||||||
@@ -28,8 +27,9 @@ pub struct WastContext {
|
|||||||
/// Wast files have a concept of a "current" module, which is the most
|
/// Wast files have a concept of a "current" module, which is the most
|
||||||
/// recently defined.
|
/// recently defined.
|
||||||
current: Option<Instance>,
|
current: Option<Instance>,
|
||||||
|
// FIXME(#1479) this is only needed to retain correct trap information after
|
||||||
instances: HashMap<String, Instance>,
|
// we've dropped previous `Instance` values.
|
||||||
|
modules: Vec<Module>,
|
||||||
linker: Linker,
|
linker: Linker,
|
||||||
store: Store,
|
store: Store,
|
||||||
}
|
}
|
||||||
@@ -58,28 +58,27 @@ impl WastContext {
|
|||||||
linker.allow_shadowing(true);
|
linker.allow_shadowing(true);
|
||||||
Self {
|
Self {
|
||||||
current: None,
|
current: None,
|
||||||
instances: HashMap::new(),
|
|
||||||
linker,
|
linker,
|
||||||
store,
|
store,
|
||||||
|
modules: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_instance(&self, instance_name: Option<&str>) -> Result<Instance> {
|
fn get_export(&self, module: Option<&str>, name: &str) -> Result<&Extern> {
|
||||||
match instance_name {
|
match module {
|
||||||
Some(name) => self
|
Some(module) => self.linker.get_one_by_name(module, name),
|
||||||
.instances
|
|
||||||
.get(name)
|
|
||||||
.cloned()
|
|
||||||
.ok_or_else(|| anyhow!("failed to find instance named `{}`", name)),
|
|
||||||
None => self
|
None => self
|
||||||
.current
|
.current
|
||||||
.clone()
|
.as_ref()
|
||||||
.ok_or_else(|| anyhow!("no previous instance found")),
|
.ok_or_else(|| anyhow!("no previous instance found"))?
|
||||||
|
.get_export(name)
|
||||||
|
.ok_or_else(|| anyhow!("no item named `{}` found", name)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn instantiate(&self, module: &[u8]) -> Result<Outcome<Instance>> {
|
fn instantiate(&mut self, module: &[u8]) -> Result<Outcome<Instance>> {
|
||||||
let module = Module::new(&self.store, module)?;
|
let module = Module::new(&self.store, module)?;
|
||||||
|
self.modules.push(module.clone());
|
||||||
let instance = match self.linker.instantiate(&module) {
|
let instance = match self.linker.instantiate(&module) {
|
||||||
Ok(i) => i,
|
Ok(i) => i,
|
||||||
Err(e) => return e.downcast::<Trap>().map(Outcome::Trap),
|
Err(e) => return e.downcast::<Trap>().map(Outcome::Trap),
|
||||||
@@ -125,7 +124,6 @@ impl WastContext {
|
|||||||
Outcome::Trap(e) => bail!("instantiation failed with: {}", e.message()),
|
Outcome::Trap(e) => bail!("instantiation failed with: {}", e.message()),
|
||||||
};
|
};
|
||||||
if let Some(name) = instance_name {
|
if let Some(name) = instance_name {
|
||||||
self.instances.insert(name.to_string(), instance.clone());
|
|
||||||
self.linker.instance(name, &instance)?;
|
self.linker.instance(name, &instance)?;
|
||||||
}
|
}
|
||||||
self.current = Some(instance);
|
self.current = Some(instance);
|
||||||
@@ -134,11 +132,18 @@ impl WastContext {
|
|||||||
|
|
||||||
/// Register an instance to make it available for performing actions.
|
/// Register an instance to make it available for performing actions.
|
||||||
fn register(&mut self, name: Option<&str>, as_name: &str) -> Result<()> {
|
fn register(&mut self, name: Option<&str>, as_name: &str) -> Result<()> {
|
||||||
let instance = self.get_instance(name)?.clone();
|
match name {
|
||||||
self.linker.instance(as_name, &instance)?;
|
Some(name) => self.linker.alias(name, as_name),
|
||||||
self.instances.insert(as_name.to_string(), instance);
|
None => {
|
||||||
|
let current = self
|
||||||
|
.current
|
||||||
|
.as_ref()
|
||||||
|
.ok_or(anyhow!("no previous instance"))?;
|
||||||
|
self.linker.instance(as_name, current)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Invoke an exported function from an instance.
|
/// Invoke an exported function from an instance.
|
||||||
fn invoke(
|
fn invoke(
|
||||||
@@ -147,10 +152,9 @@ impl WastContext {
|
|||||||
field: &str,
|
field: &str,
|
||||||
args: &[Val],
|
args: &[Val],
|
||||||
) -> Result<Outcome> {
|
) -> Result<Outcome> {
|
||||||
let instance = self.get_instance(instance_name.as_ref().map(|x| &**x))?;
|
let func = self
|
||||||
let func = instance
|
.get_export(instance_name, field)?
|
||||||
.get_export(field)
|
.func()
|
||||||
.and_then(|e| e.func())
|
|
||||||
.ok_or_else(|| anyhow!("no function named `{}`", field))?;
|
.ok_or_else(|| anyhow!("no function named `{}`", field))?;
|
||||||
Ok(match func.call(args) {
|
Ok(match func.call(args) {
|
||||||
Ok(result) => Outcome::Ok(result.into()),
|
Ok(result) => Outcome::Ok(result.into()),
|
||||||
@@ -160,10 +164,9 @@ impl WastContext {
|
|||||||
|
|
||||||
/// Get the value of an exported global from an instance.
|
/// Get the value of an exported global from an instance.
|
||||||
fn get(&mut self, instance_name: Option<&str>, field: &str) -> Result<Outcome> {
|
fn get(&mut self, instance_name: Option<&str>, field: &str) -> Result<Outcome> {
|
||||||
let instance = self.get_instance(instance_name.as_ref().map(|x| &**x))?;
|
let global = self
|
||||||
let global = instance
|
.get_export(instance_name, field)?
|
||||||
.get_export(field)
|
.global()
|
||||||
.and_then(|e| e.global())
|
|
||||||
.ok_or_else(|| anyhow!("no global named `{}`", field))?;
|
.ok_or_else(|| anyhow!("no global named `{}`", field))?;
|
||||||
Ok(Outcome::Ok(vec![global.get()]))
|
Ok(Outcome::Ok(vec![global.get()]))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user