wasmer/lib/runtime/src/module.rs

117 lines
3.1 KiB
Rust
Raw Normal View History

2019-01-08 17:09:47 +00:00
use crate::{
backend::FuncResolver,
2019-01-11 03:59:57 +00:00
import::ImportResolver,
2019-01-09 02:57:28 +00:00
sig_registry::SigRegistry,
2019-01-08 17:09:47 +00:00
types::{
2019-01-09 02:57:28 +00:00
FuncIndex, Global, GlobalDesc, GlobalIndex, Map, MapIndex, 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-09 02:57:28 +00:00
use std::ops::Deref;
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.
pub struct ModuleInner {
pub func_resolver: Box<dyn FuncResolver>,
pub memories: Map<MemoryIndex, Memory>,
pub globals: Map<GlobalIndex, Global>,
pub tables: Map<TableIndex, Table>,
pub imported_functions: Map<FuncIndex, ImportName>,
pub imported_memories: Map<MemoryIndex, (ImportName, Memory)>,
pub imported_tables: Map<TableIndex, (ImportName, Table)>,
pub imported_globals: Map<GlobalIndex, (ImportName, GlobalDesc)>,
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>,
pub table_initializers: Vec<TableInitializer>,
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 {
#[inline]
pub fn new(inner: ModuleInner) -> Self {
2019-01-11 03:59:57 +00:00
Module(Rc::new(inner))
2019-01-08 17:09:47 +00:00
}
/// Instantiate a webassembly module with the provided imports.
2019-01-12 22:52:14 +00:00
pub fn instantiate(&self, imports: Rc<dyn ImportResolver>) -> Result<Instance, String> {
2019-01-11 03:59:57 +00:00
Instance::new(Module(Rc::clone(&self.0)), imports)
2019-01-08 17:09:47 +00:00
}
}
impl ModuleInner {
pub(crate) fn is_imported_function(&self, func_index: FuncIndex) -> bool {
func_index.index() < self.imported_functions.len()
}
2019-01-12 20:34:23 +00:00
pub(crate) fn is_imported_memory(&self, memory_index: MemoryIndex) -> bool {
memory_index.index() < self.imported_memories.len()
}
2019-01-08 17:09:47 +00:00
}
impl Deref for Module {
type Target = ModuleInner;
2019-01-09 02:57:28 +00:00
2019-01-08 17:09:47 +00:00
fn deref(&self) -> &ModuleInner {
&*self.0
}
}
#[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.
#[derive(Debug)]
pub struct DataInitializer {
/// The index of the memory to initialize.
pub memory_index: MemoryIndex,
/// Optionally a globalvalue base to initialize at.
pub base: Option<GlobalIndex>,
/// A constant offset to initialize at.
pub offset: usize,
/// The initialization data.
pub data: Vec<u8>,
}
/// A WebAssembly table initializer.
#[derive(Clone, Debug)]
pub struct TableInitializer {
/// The index of a table to initialize.
pub table_index: TableIndex,
/// Optionally, a global variable giving a base index.
pub base: Option<GlobalIndex>,
/// The offset to add to the base.
pub offset: usize,
/// The values to write into the table elements.
pub elements: Vec<FuncIndex>,
}