use crate::{ backend::{FuncResolver, ProtectedCaller}, error::Result, import::ImportObject, sig_registry::SigRegistry, structures::Map, types::{ FuncIndex, GlobalDescriptor, GlobalIndex, GlobalInit, ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex, ImportedTableIndex, Initializer, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryDescriptor, MemoryIndex, SigIndex, TableDescriptor, TableIndex, }, Instance, }; use hashbrown::HashMap; use std::sync::Arc; /// This is used to instantiate a new WebAssembly module. #[doc(hidden)] pub struct ModuleInner { pub func_resolver: Box, pub protected_caller: Box, // This are strictly local and the typsystem ensures that. pub memories: Map, pub globals: Map, pub tables: Map, // These are strictly imported and the typesystem ensures that. pub imported_functions: Map, pub imported_memories: Map, pub imported_tables: Map, pub imported_globals: Map, pub exports: HashMap, pub data_initializers: Vec, pub elem_initializers: Vec, pub start_func: Option, pub func_assoc: Map, 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 pub struct Module(#[doc(hidden)] pub Arc); impl Module { pub(crate) fn new(inner: Arc) -> Self { Module(inner) } /// 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(()) /// # } /// ``` pub fn instantiate(&self, import_object: &ImportObject) -> Result { Instance::new(Arc::clone(&self.0), import_object) } } impl ModuleInner {} #[doc(hidden)] #[derive(Debug, Clone)] pub struct ImportName { pub namespace: String, pub name: String, } impl From<(String, String)> for ImportName { fn from(n: (String, String)) -> Self { ImportName { namespace: n.0, name: n.1, } } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ExportIndex { Func(FuncIndex), Memory(MemoryIndex), Global(GlobalIndex), Table(TableIndex), } /// A data initializer for linear memory. #[derive(Debug, Clone)] pub struct DataInitializer { /// The index of the memory to initialize. pub memory_index: MemoryIndex, /// Either a constant offset or a `get_global` pub base: Initializer, /// The initialization data. pub data: Vec, } /// A WebAssembly table initializer. #[derive(Debug, Clone)] pub struct TableInitializer { /// The index of a table to initialize. pub table_index: TableIndex, /// Either a constant offset or a `get_global` pub base: Initializer, /// The values to write into the table elements. pub elements: Vec, }