use crate::{ backend::{Backend, FuncResolver, ProtectedCaller}, error::Result, import::ImportObject, structures::{Map, TypedIndex}, typed_func::EARLY_TRAPPER, types::{ FuncIndex, FuncSig, GlobalDescriptor, GlobalIndex, GlobalInit, ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex, ImportedTableIndex, Initializer, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryDescriptor, MemoryIndex, SigIndex, TableDescriptor, TableIndex, }, Instance, }; use hashbrown::HashMap; use indexmap::IndexMap; 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, pub info: ModuleInfo, } #[cfg_attr(feature = "cache", derive(Serialize, Deserialize))] pub struct ModuleInfo { // 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 signatures: Map>, pub backend: Backend, pub namespace_table: StringTable, pub name_table: StringTable, } /// 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 { unsafe { EARLY_TRAPPER .with(|ucell| *ucell.get() = Some(inner.protected_caller.get_early_trapper())); } 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)] #[cfg_attr(feature = "cache", derive(Serialize, Deserialize))] #[derive(Debug, Clone)] pub struct ImportName { pub namespace_index: NamespaceIndex, pub name_index: NameIndex, } #[cfg_attr(feature = "cache", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ExportIndex { Func(FuncIndex), Memory(MemoryIndex), Global(GlobalIndex), Table(TableIndex), } /// A data initializer for linear memory. #[cfg_attr(feature = "cache", derive(Serialize, Deserialize))] #[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. #[cfg_attr(feature = "cache", serde(with = "serde_bytes"))] pub data: Vec, } /// A WebAssembly table initializer. #[cfg_attr(feature = "cache", derive(Serialize, Deserialize))] #[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, } pub struct StringTableBuilder { map: IndexMap, buffer: String, count: u32, } impl StringTableBuilder { pub fn new() -> Self { Self { map: IndexMap::new(), buffer: String::new(), count: 0, } } pub fn register(&mut self, s: S) -> K where S: Into + AsRef, { let s_str = s.as_ref(); if self.map.contains_key(s_str) { self.map[s_str].0 } else { let offset = self.buffer.len(); let length = s_str.len(); let index = TypedIndex::new(self.count as _); self.buffer.push_str(s_str); self.map .insert(s.into(), (index, offset as u32, length as u32)); self.count += 1; index } } pub fn finish(self) -> StringTable { let table = self .map .values() .map(|(_, offset, length)| (*offset, *length)) .collect(); StringTable { table, buffer: self.buffer, } } } #[cfg_attr(feature = "cache", derive(Serialize, Deserialize))] #[derive(Debug, Clone)] pub struct StringTable { table: Map, buffer: String, } impl StringTable { pub fn new() -> Self { Self { table: Map::new(), buffer: String::new(), } } pub fn get(&self, index: K) -> &str { let (offset, length) = self.table[index]; let offset = offset as usize; let length = length as usize; &self.buffer[offset..offset + length] } } #[cfg_attr(feature = "cache", derive(Serialize, Deserialize))] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub struct NamespaceIndex(u32); impl TypedIndex for NamespaceIndex { #[doc(hidden)] fn new(index: usize) -> Self { NamespaceIndex(index as _) } #[doc(hidden)] fn index(&self) -> usize { self.0 as usize } } #[cfg_attr(feature = "cache", derive(Serialize, Deserialize))] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub struct NameIndex(u32); impl TypedIndex for NameIndex { #[doc(hidden)] fn new(index: usize) -> Self { NameIndex(index as _) } #[doc(hidden)] fn index(&self) -> usize { self.0 as usize } }