|
1 | 1 | use alloc::{boxed::Box, string::ToString, sync::Arc, vec::Vec}; |
2 | 2 | use tinywasm_types::{ |
3 | | - DataAddr, ElmAddr, Export, ExternalKind, FuncAddr, FuncType, GlobalAddr, Import, MemAddr, ModuleInstanceAddr, |
4 | | - TableAddr, |
| 3 | + DataAddr, ElmAddr, ExternalKind, FuncAddr, FuncType, GlobalAddr, Import, MemAddr, ModuleInstanceAddr, TableAddr, |
5 | 4 | }; |
6 | 5 |
|
7 | 6 | use crate::{ |
8 | 7 | func::{FromWasmValueTuple, IntoWasmValueTuple}, |
9 | | - Error, ExportInstance, FuncHandle, Result, Store, TypedFuncHandle, |
| 8 | + Error, ExportInstance, FuncHandle, Module, Result, Store, TypedFuncHandle, |
10 | 9 | }; |
11 | 10 |
|
12 | 11 | /// A WebAssembly Module Instance |
@@ -36,6 +35,38 @@ pub(crate) struct ModuleInstanceInner { |
36 | 35 | } |
37 | 36 |
|
38 | 37 | impl ModuleInstance { |
| 38 | + /// Instantiate the module in the given store |
| 39 | + pub fn instantiate(store: &mut Store, module: Module) -> Result<Self> { |
| 40 | + let idx = store.next_module_instance_idx(); |
| 41 | + |
| 42 | + let func_addrs = store.add_funcs(module.data.funcs.into(), idx); |
| 43 | + let table_addrs = store.add_tables(module.data.table_types.into(), idx); |
| 44 | + let mem_addrs = store.add_mems(module.data.memory_types.into(), idx); |
| 45 | + let global_addrs = store.add_globals(module.data.globals.into(), idx); |
| 46 | + let elem_addrs = store.add_elems(module.data.elements.into(), idx); |
| 47 | + let data_addrs = store.add_datas(module.data.data.into(), idx); |
| 48 | + |
| 49 | + let instance = ModuleInstanceInner { |
| 50 | + store_id: store.id(), |
| 51 | + idx, |
| 52 | + |
| 53 | + types: module.data.func_types, |
| 54 | + func_addrs, |
| 55 | + table_addrs, |
| 56 | + mem_addrs, |
| 57 | + global_addrs, |
| 58 | + elem_addrs, |
| 59 | + data_addrs, |
| 60 | + |
| 61 | + func_start: module.data.start_func, |
| 62 | + imports: module.data.imports, |
| 63 | + exports: crate::ExportInstance(module.data.exports), |
| 64 | + }; |
| 65 | + let instance = ModuleInstance::new(instance); |
| 66 | + store.add_instance(instance.clone())?; |
| 67 | + Ok(instance) |
| 68 | + } |
| 69 | + |
39 | 70 | /// Get the module's exports |
40 | 71 | pub fn exports(&self) -> &ExportInstance { |
41 | 72 | &self.0.exports |
@@ -101,7 +132,7 @@ impl ModuleInstance { |
101 | 132 | /// (which is not part of the spec, but used by llvm) |
102 | 133 | /// |
103 | 134 | /// See <https://webassembly.github.io/spec/core/syntax/modules.html#start-function> |
104 | | - pub fn start_func(&mut self, store: &Store) -> Result<Option<FuncHandle>> { |
| 135 | + pub fn start_func(&self, store: &Store) -> Result<Option<FuncHandle>> { |
105 | 136 | if self.0.store_id != store.id() { |
106 | 137 | return Err(Error::InvalidStore); |
107 | 138 | } |
@@ -135,7 +166,7 @@ impl ModuleInstance { |
135 | 166 | /// Returns None if the module has no start function |
136 | 167 | /// |
137 | 168 | /// See <https://webassembly.github.io/spec/core/syntax/modules.html#syntax-start> |
138 | | - pub fn start(&mut self, store: &mut Store) -> Result<Option<()>> { |
| 169 | + pub fn start(&self, store: &mut Store) -> Result<Option<()>> { |
139 | 170 | let Some(func) = self.start_func(store)? else { |
140 | 171 | return Ok(None); |
141 | 172 | }; |
|
0 commit comments