@@ -42,16 +42,30 @@ impl ModuleInstance {
4242 }
4343
4444 /// Instantiate the module in the given store
45+ ///
46+ /// See <https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation>
4547 pub fn instantiate ( store : & mut Store , module : Module , imports : Option < Imports > ) -> Result < Self > {
48+ // This doesn't completely follow the steps in the spec, but the end result is the same
49+ // Constant expressions are evaluated directly where they are used, so we
50+ // don't need to create a auxiliary frame etc.
51+
4652 let idx = store. next_module_instance_idx ( ) ;
4753 let imports = imports. unwrap_or_default ( ) ;
54+
55+ // TODO: doesn't link other modules yet
4856 let linked_imports = imports. link ( store, & module) ?;
57+ let global_addrs = store. add_globals ( module. data . globals . into ( ) , & module. data . imports , & linked_imports, idx) ?;
4958
59+ // TODO: imported functions missing
5060 let func_addrs = store. add_funcs ( module. data . funcs . into ( ) , idx) ;
61+
5162 let table_addrs = store. add_tables ( module. data . table_types . into ( ) , idx) ;
5263 let mem_addrs = store. add_mems ( module. data . memory_types . into ( ) , idx) ?;
53- let global_addrs = store. add_globals ( module. data . globals . into ( ) , & module. data . imports , & linked_imports, idx) ?;
64+
65+ // TODO: active/declared elems need to be initialized
5466 let elem_addrs = store. add_elems ( module. data . elements . into ( ) , idx) ?;
67+
68+ // TODO: active data segments need to be initialized
5569 let data_addrs = store. add_datas ( module. data . data . into ( ) , idx) ;
5670
5771 let instance = ModuleInstanceInner {
@@ -70,8 +84,10 @@ impl ModuleInstance {
7084 imports : module. data . imports ,
7185 exports : crate :: ExportInstance ( module. data . exports ) ,
7286 } ;
87+
7388 let instance = ModuleInstance :: new ( instance) ;
7489 store. add_instance ( instance. clone ( ) ) ?;
90+
7591 Ok ( instance)
7692 }
7793
@@ -176,13 +192,18 @@ impl ModuleInstance {
176192 }
177193 } ;
178194
179- let func_addr = self . 0 . func_addrs [ func_index as usize ] ;
180- let func = store. get_func ( func_addr as usize ) ?;
195+ let func_addr = self
196+ . 0
197+ . func_addrs
198+ . get ( func_index as usize )
199+ . expect ( "No func addr for start func, this is a bug" ) ;
200+
201+ let func = store. get_func ( * func_addr as usize ) ?;
181202 let ty = self . 0 . types [ func. ty_addr ( ) as usize ] . clone ( ) ;
182203
183204 Ok ( Some ( FuncHandle {
184205 module : self . clone ( ) ,
185- addr : func_addr,
206+ addr : * func_addr,
186207 ty,
187208 name : None ,
188209 } ) )
0 commit comments