wasmer/lib/runtime/src/module.rs

102 lines
2.8 KiB
Rust
Raw Normal View History

2019-01-08 17:09:47 +00:00
use crate::{
backend::FuncResolver,
2019-01-13 03:02:19 +00:00
import::Imports,
2019-01-09 02:57:28 +00:00
sig_registry::SigRegistry,
2019-01-16 18:26:10 +00:00
structures::Map,
2019-01-08 17:09:47 +00:00
types::{
2019-01-17 01:59:12 +00:00
FuncIndex, Global, GlobalDesc, GlobalIndex, ImportedFuncIndex, ImportedGlobalIndex,
2019-01-16 18:26:10 +00:00
ImportedMemoryIndex, ImportedTableIndex, Initializer, LocalGlobalIndex, LocalMemoryIndex,
LocalTableIndex, Memory, MemoryIndex, SigIndex, Table, TableIndex,
2019-01-08 17:09:47 +00:00
},
2019-01-11 03:59:57 +00:00
Instance,
2019-01-08 17:09:47 +00:00
};
use hashbrown::HashMap;
2019-01-11 03:59:57 +00:00
use std::rc::Rc;
2019-01-08 17:09:47 +00:00
/// This is used to instantiate a new webassembly module.
2019-01-13 03:02:19 +00:00
#[doc(hidden)]
2019-01-08 17:09:47 +00:00
pub struct ModuleInner {
pub func_resolver: Box<dyn FuncResolver>,
2019-01-16 18:26:10 +00:00
// This are strictly local and the typsystem ensures that.
pub memories: Map<LocalMemoryIndex, Memory>,
pub globals: Map<LocalGlobalIndex, Global>,
pub tables: Map<LocalTableIndex, Table>,
2019-01-08 17:09:47 +00:00
2019-01-16 18:26:10 +00:00
// These are strictly imported and the typesystem ensures that.
pub imported_functions: Map<ImportedFuncIndex, ImportName>,
pub imported_memories: Map<ImportedMemoryIndex, (ImportName, Memory)>,
pub imported_tables: Map<ImportedTableIndex, (ImportName, Table)>,
2019-01-17 01:59:12 +00:00
pub imported_globals: Map<ImportedGlobalIndex, (ImportName, GlobalDesc)>,
2019-01-08 17:09:47 +00:00
2019-01-11 03:59:57 +00:00
pub exports: HashMap<String, ExportIndex>,
2019-01-08 17:09:47 +00:00
pub data_initializers: Vec<DataInitializer>,
2019-01-17 01:59:12 +00:00
pub elem_initializers: Vec<TableInitializer>,
2019-01-08 17:09:47 +00:00
pub start_func: Option<FuncIndex>,
pub func_assoc: Map<FuncIndex, SigIndex>,
pub sig_registry: SigRegistry,
}
2019-01-11 03:59:57 +00:00
pub struct Module(Rc<ModuleInner>);
2019-01-08 17:09:47 +00:00
impl Module {
2019-01-13 03:02:19 +00:00
pub(crate) fn new(inner: Rc<ModuleInner>) -> Self {
Module(inner)
2019-01-08 17:09:47 +00:00
}
/// Instantiate a webassembly module with the provided imports.
2019-01-17 22:13:28 +00:00
pub fn instantiate(&self, imports: Imports) -> Result<Instance, String> {
Instance::new(Rc::clone(&self.0), Box::new(imports))
2019-01-08 17:09:47 +00:00
}
}
2019-01-16 18:26:10 +00:00
impl ModuleInner {}
2019-01-08 17:09:47 +00:00
2019-01-13 03:02:19 +00:00
#[doc(hidden)]
2019-01-08 17:09:47 +00:00
#[derive(Debug, Clone)]
pub struct ImportName {
2019-01-11 03:59:57 +00:00
pub namespace: String,
2019-01-08 17:09:47 +00:00
pub name: String,
}
impl From<(String, String)> for ImportName {
fn from(n: (String, String)) -> Self {
ImportName {
2019-01-11 03:59:57 +00:00
namespace: n.0,
2019-01-08 17:09:47 +00:00
name: n.1,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2019-01-11 03:59:57 +00:00
pub enum ExportIndex {
2019-01-08 17:09:47 +00:00
Func(FuncIndex),
Memory(MemoryIndex),
Global(GlobalIndex),
Table(TableIndex),
}
/// A data initializer for linear memory.
2019-01-16 18:26:10 +00:00
#[derive(Debug, Clone)]
2019-01-08 17:09:47 +00:00
pub struct DataInitializer {
/// The index of the memory to initialize.
pub memory_index: MemoryIndex,
2019-01-17 01:59:12 +00:00
/// Either a constant offset or a `get_global`
pub base: Initializer,
2019-01-08 17:09:47 +00:00
/// The initialization data.
pub data: Vec<u8>,
}
/// A WebAssembly table initializer.
2019-01-16 18:26:10 +00:00
#[derive(Debug, Clone)]
2019-01-08 17:09:47 +00:00
pub struct TableInitializer {
/// The index of a table to initialize.
pub table_index: TableIndex,
2019-01-16 18:26:10 +00:00
/// Either a constant offset or a `get_global`
pub base: Initializer,
2019-01-08 17:09:47 +00:00
/// The values to write into the table elements.
pub elements: Vec<FuncIndex>,
}