Add a concept of "global exports".

This adds a feature which allows one to look up an export by name
without knowing what module it's in -- `lookup_global_export` on an
`InstanceContents`.

The main expected use for this is to support APIs where module A
imports a function from module B, and module B needs to access module
A's memory. B can't import it from A in the normal way, because that
would create a dependency cycle. So for now, allow B to look up A's
exported memory dynamically with `lookup_global_export`.

In the future, with reference types and possibly host bindings, we'll be
able to pass references to memory as arguments, which will obviate the
need for this mechanism.
This commit is contained in:
Dan Gohman
2019-01-22 14:50:38 -08:00
parent dae04be948
commit 00a4e93bcd
16 changed files with 407 additions and 184 deletions

View File

@@ -18,12 +18,12 @@ cranelift-wasm = "0.26.0"
cast = { version = "0.2.2", default-features = false }
failure = { version = "0.1.3", default-features = false }
failure_derive = { version = "0.1.3", default-features = false }
hashbrown = { version = "0.1.8", optional = true }
indexmap = "1.0.2"
[features]
default = ["std"]
std = ["cranelift-codegen/std", "cranelift-wasm/std"]
core = ["hashbrown/nightly", "cranelift-codegen/core", "cranelift-wasm/core"]
core = ["cranelift-codegen/core", "cranelift-wasm/core"]
[badges]
maintenance = { status = "experimental" }

View File

@@ -34,11 +34,6 @@ extern crate alloc as std;
#[macro_use]
extern crate std;
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::collections::HashMap;
#[macro_use]
extern crate failure_derive;

View File

@@ -1,6 +1,5 @@
//! Data structures for representing decoded wasm modules.
use super::HashMap;
use crate::tunables::Tunables;
use cranelift_codegen::ir;
use cranelift_entity::{EntityRef, PrimaryMap};
@@ -8,6 +7,7 @@ use cranelift_wasm::{
DefinedFuncIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex, Global,
GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex,
};
use indexmap::IndexMap;
use std::string::String;
use std::vec::Vec;
@@ -162,7 +162,7 @@ pub struct Module {
pub globals: PrimaryMap<GlobalIndex, Global>,
/// Exported entities.
pub exports: HashMap<String, Export>,
pub exports: IndexMap<String, Export>,
/// The module "start" function, if present.
pub start_func: Option<FuncIndex>,
@@ -184,7 +184,7 @@ impl Module {
table_plans: PrimaryMap::new(),
memory_plans: PrimaryMap::new(),
globals: PrimaryMap::new(),
exports: HashMap::new(),
exports: IndexMap::new(),
start_func: None,
table_elements: Vec::new(),
}