/// A WebAssembly.Module object representing the compiled WebAssembly module. extern crate cranelift_codegen; extern crate cranelift_entity; extern crate cranelift_wasm; use cranelift_codegen::ir; use cranelift_entity::{EntityRef, PrimaryMap}; use cranelift_wasm::{ DefinedFuncIndex, FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex, }; use std::collections::HashMap; /// A WebAssembly table initializer. #[derive(Clone, Debug)] pub struct TableElements { /// The index of a table to initialize. pub table_index: TableIndex, /// Optionally, a global variable giving a base index. pub base: Option, /// The offset to add to the base. pub offset: usize, /// The values to write into the table elements. pub elements: Vec, } /// An entity to export. #[derive(Clone, Debug)] pub enum Export { /// Function export. Function(FuncIndex), /// Table export. Table(TableIndex), /// Memory export. Memory(MemoryIndex), /// Global export. Global(GlobalIndex), } /// A translated WebAssembly module, excluding the function bodies and /// memory initializers. #[derive(Clone, Debug)] pub struct Module { /// Unprocessed signatures exactly as provided by `declare_signature()`. pub signatures: Vec, /// Names of imported functions. pub imported_funcs: Vec<(String, String)>, /// Types of functions, imported and local. pub functions: PrimaryMap, /// WebAssembly tables. pub tables: Vec, /// WebAssembly linear memories. pub memories: Vec, /// WebAssembly global variables. pub globals: Vec, /// Exported entities. pub exports: HashMap, /// The module "start" function, if present. pub start_func: Option, /// WebAssembly table initializers. pub table_elements: Vec, } impl Module { /// Allocates the module data structures. pub fn new() -> Self { Self { signatures: Vec::new(), imported_funcs: Vec::new(), functions: PrimaryMap::new(), tables: Vec::new(), memories: Vec::new(), globals: Vec::new(), exports: HashMap::new(), start_func: None, table_elements: Vec::new(), } } /// Convert a `DefinedFuncIndex` into a `FuncIndex`. pub fn func_index(&self, defined_func: DefinedFuncIndex) -> FuncIndex { FuncIndex::new(self.imported_funcs.len() + defined_func.index()) } /// Convert a `FuncIndex` into a `DefinedFuncIndex`. Returns None if the /// index is an imported function. pub fn defined_func_index(&self, func: FuncIndex) -> Option { if func.index() < self.imported_funcs.len() { None } else { Some(DefinedFuncIndex::new( func.index() - self.imported_funcs.len(), )) } } } /// A data initializer for linear memory. pub struct DataInitializer<'data> { /// The index of the memory to initialize. pub memory_index: MemoryIndex, /// Optionally a globalvar base to initialize at. pub base: Option, /// A constant offset to initialize at. pub offset: usize, /// The initialization data. pub data: &'data [u8], } /// References to the input wasm data buffer to be decoded and processed later, /// separately from the main module translation. pub struct LazyContents<'data> { /// References to the function bodies. pub function_body_inputs: PrimaryMap, /// References to the data initializers. pub data_initializers: Vec>, } impl<'data> LazyContents<'data> { pub fn new() -> Self { Self { function_body_inputs: PrimaryMap::new(), data_initializers: Vec::new(), } } }