wasmer/lib/runtime-core/src/module.rs

134 lines
3.9 KiB
Rust
Raw Normal View History

2019-01-08 17:09:47 +00:00
use crate::{
2019-01-18 20:13:01 +00:00
backend::{FuncResolver, ProtectedCaller},
error::Result,
import::ImportObject,
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-29 21:04:42 +00:00
FuncIndex, GlobalDescriptor, GlobalIndex, GlobalInit, ImportedFuncIndex,
ImportedGlobalIndex, ImportedMemoryIndex, ImportedTableIndex, Initializer,
LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryDescriptor, MemoryIndex,
SigIndex, TableDescriptor, 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-28 19:55:44 +00:00
use std::sync::Arc;
2019-01-08 17:09:47 +00:00
2019-01-19 07:03:07 +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-18 20:13:01 +00:00
pub protected_caller: Box<dyn ProtectedCaller>,
2019-01-18 22:30:15 +00:00
2019-01-16 18:26:10 +00:00
// This are strictly local and the typsystem ensures that.
2019-01-29 21:04:42 +00:00
pub memories: Map<LocalMemoryIndex, MemoryDescriptor>,
2019-01-28 19:55:44 +00:00
pub globals: Map<LocalGlobalIndex, GlobalInit>,
2019-01-29 21:04:42 +00:00
pub tables: Map<LocalTableIndex, TableDescriptor>,
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>,
2019-01-29 21:04:42 +00:00
pub imported_memories: Map<ImportedMemoryIndex, (ImportName, MemoryDescriptor)>,
pub imported_tables: Map<ImportedTableIndex, (ImportName, TableDescriptor)>,
pub imported_globals: Map<ImportedGlobalIndex, (ImportName, GlobalDescriptor)>,
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,
}
/// A compiled WebAssembly module.
///
/// `Module` is returned by the [`compile`] and
/// [`compile_with`] functions.
///
/// [`compile`]: fn.compile.html
/// [`compile_with`]: fn.compile_with.html
2019-01-28 19:55:44 +00:00
pub struct Module(#[doc(hidden)] pub Arc<ModuleInner>);
2019-01-08 17:09:47 +00:00
impl Module {
2019-01-28 19:55:44 +00:00
pub(crate) fn new(inner: Arc<ModuleInner>) -> Self {
2019-01-13 03:02:19 +00:00
Module(inner)
2019-01-08 17:09:47 +00:00
}
/// Instantiate a WebAssembly module with the provided [`ImportObject`].
///
/// [`ImportObject`]: struct.ImportObject.html
///
/// # Note:
/// Instantiating a `Module` will also call the function designated as `start`
/// in the WebAssembly module, if there is one.
///
/// # Usage:
/// ```
/// # use wasmer_runtime_core::error::Result;
/// # use wasmer_runtime_core::Module;
/// # use wasmer_runtime_core::imports;
/// # fn instantiate(module: &Module) -> Result<()> {
/// let import_object = imports! {
/// // ...
/// };
/// let instance = module.instantiate(import_object)?;
/// // ...
/// # Ok(())
/// # }
/// ```
2019-02-02 23:58:33 +00:00
pub fn instantiate(&self, import_object: &ImportObject) -> Result<Instance> {
Instance::new(Arc::clone(&self.0), import_object)
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>,
}