Instantiate the module in the given store (without running the start function) ## Example ```rust # fn main() -> tinywasm::Result<()> { # use tinywasm::{ModuleInstance, Store}; # let wasm = wat::parse_str(r#" # (module # (global $g (mut i32) (i32.const 0)) # (func $start # i32.const 42 # global.set $g) # (start $start) # (export "g" (global $g))) # "#).
(store: &mut Store, module: &Module, imports: Option<Imports>)
| 216 | /// |
| 217 | /// See <https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation> |
| 218 | pub fn instantiate_no_start(store: &mut Store, module: &Module, imports: Option<Imports>) -> Result<Self> { |
| 219 | let idx = store.next_module_instance_idx(); |
| 220 | let mut addrs = imports.unwrap_or_default().link(store, module)?; |
| 221 | addrs.funcs.extend(store.init_funcs(&module.funcs, idx)); |
| 222 | addrs.tables.extend(store.init_tables(&module.table_types)); |
| 223 | match module.local_memory_allocation { |
| 224 | LocalMemoryAllocation::Skip => {} |
| 225 | LocalMemoryAllocation::Lazy => addrs.memories.extend(store.init_lazy_memories(&module.memory_types)?), |
| 226 | LocalMemoryAllocation::Eager => addrs.memories.extend(store.init_memories(&module.memory_types)?), |
| 227 | } |
| 228 | |
| 229 | store.init_globals(&mut addrs.globals, &module.globals, &addrs.funcs)?; |
| 230 | let (elem_addrs, elem_trapped) = |
| 231 | store.init_elements(&addrs.tables, &addrs.funcs, &addrs.globals, &module.elements)?; |
| 232 | let (data_addrs, data_trapped) = |
| 233 | store.init_data(&addrs.memories, &addrs.globals, &addrs.funcs, &module.data)?; |
| 234 | |
| 235 | let instance = ModuleInstanceInner { |
| 236 | store_id: store.id(), |
| 237 | idx, |
| 238 | types: module.func_types.clone(), |
| 239 | func_type_idxs: module.func_type_idxs.clone(), |
| 240 | func_addrs: addrs.funcs.into_boxed_slice(), |
| 241 | table_addrs: addrs.tables.into_boxed_slice(), |
| 242 | mem_addrs: addrs.memories.into_boxed_slice(), |
| 243 | global_addrs: addrs.globals.into_boxed_slice(), |
| 244 | elem_addrs, |
| 245 | data_addrs, |
| 246 | func_start: module.start_func, |
| 247 | exports: module.exports.clone(), |
| 248 | }; |
| 249 | |
| 250 | let instance = ModuleInstance(Rc::new(instance)); |
| 251 | store.add_instance(instance.clone()); |
| 252 | |
| 253 | match (elem_trapped, data_trapped) { |
| 254 | (Some(trap), _) | (_, Some(trap)) => { |
| 255 | cold_path(); |
| 256 | Err(trap.into()) |
| 257 | } |
| 258 | _ => Ok(instance), |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /// Get a export by name |
| 263 | pub fn export_addr(&self, name: &str) -> Option<ExternVal> { |
nothing calls this directly
no test coverage detected