@@ -13,7 +13,7 @@ use tinywasm_types::{
1313
1414use crate :: {
1515 runtime:: { self , DefaultRuntime } ,
16- Error , Extern , Imports , ModuleInstance , RawWasmValue , Result ,
16+ Error , Extern , LinkedImports , ModuleInstance , RawWasmValue , Result ,
1717} ;
1818
1919// global store id counter
@@ -153,7 +153,7 @@ impl Store {
153153 & mut self ,
154154 globals : Vec < Global > ,
155155 wasm_imports : & [ Import ] ,
156- user_imports : & Imports ,
156+ user_imports : & LinkedImports ,
157157 idx : ModuleInstanceAddr ,
158158 ) -> Result < Vec < Addr > > {
159159 // TODO: initialize imported globals
@@ -191,41 +191,58 @@ impl Store {
191191 let iterator = imported_globals. into_iter ( ) . chain ( globals. as_ref ( ) ) ;
192192
193193 for ( i, global) in iterator. enumerate ( ) {
194- use tinywasm_types:: ConstInstruction :: * ;
195- let val = match global. init {
196- F32Const ( f) => RawWasmValue :: from ( f) ,
197- F64Const ( f) => RawWasmValue :: from ( f) ,
198- I32Const ( i) => RawWasmValue :: from ( i) ,
199- I64Const ( i) => RawWasmValue :: from ( i) ,
200- GlobalGet ( addr) => {
201- let addr = global_addrs[ addr as usize ] ;
202- let global = self . data . globals [ addr as usize ] . clone ( ) ;
203- let val = global. borrow ( ) . value ;
204- val
205- }
206- RefNull ( _) => RawWasmValue :: default ( ) ,
207- RefFunc ( idx) => RawWasmValue :: from ( idx as i64 ) ,
208- } ;
209-
210- self . data
211- . globals
212- . push ( Rc :: new ( RefCell :: new ( GlobalInstance :: new ( global. ty , val, idx) ) ) ) ;
213-
194+ self . data . globals . push ( Rc :: new ( RefCell :: new ( GlobalInstance :: new (
195+ global. ty ,
196+ self . eval_const ( & global. init ) ?,
197+ idx,
198+ ) ) ) ) ;
214199 global_addrs. push ( ( i + global_count) as Addr ) ;
215200 }
216- log :: debug! ( "global_addrs: {:?}" , global_addrs ) ;
201+
217202 Ok ( global_addrs)
218203 }
219204
205+ pub ( crate ) fn eval_const ( & self , const_instr : & tinywasm_types:: ConstInstruction ) -> Result < RawWasmValue > {
206+ use tinywasm_types:: ConstInstruction :: * ;
207+ let val = match const_instr {
208+ F32Const ( f) => RawWasmValue :: from ( * f) ,
209+ F64Const ( f) => RawWasmValue :: from ( * f) ,
210+ I32Const ( i) => RawWasmValue :: from ( * i) ,
211+ I64Const ( i) => RawWasmValue :: from ( * i) ,
212+ GlobalGet ( addr) => {
213+ let addr = * addr as usize ;
214+ let global = self . data . globals [ addr] . clone ( ) ;
215+ let val = global. borrow ( ) . value ;
216+ val
217+ }
218+ RefNull ( _) => RawWasmValue :: default ( ) ,
219+ RefFunc ( idx) => RawWasmValue :: from ( * idx as i64 ) ,
220+ } ;
221+ Ok ( val)
222+ }
223+
220224 /// Add elements to the store, returning their addresses in the store
221- pub ( crate ) fn add_elems ( & mut self , elems : Vec < Element > , idx : ModuleInstanceAddr ) -> Vec < Addr > {
225+ /// Should be called after the tables have been added
226+ pub ( crate ) fn add_elems ( & mut self , elems : Vec < Element > , idx : ModuleInstanceAddr ) -> Result < Vec < Addr > > {
222227 let elem_count = self . data . elems . len ( ) ;
223228 let mut elem_addrs = Vec :: with_capacity ( elem_count) ;
224229 for ( i, elem) in elems. into_iter ( ) . enumerate ( ) {
225230 self . data . elems . push ( ElemInstance :: new ( elem. kind , idx) ) ;
226231 elem_addrs. push ( ( i + elem_count) as Addr ) ;
232+
233+ // match elem.kind {
234+ // ElementKind::Active { table, offset } => {
235+ // // let table = self.data.tables[table as usize];
236+
237+ // // let offset = self.eval_const(&offset)?;
238+ // // let offset = offset.raw_value() as usize;
239+ // // let offset = offset + elem_addrs[i] as usize;
240+ // // let offset = offset as Addr;
241+ // }
242+ // }
227243 }
228- elem_addrs
244+
245+ Ok ( elem_addrs)
229246 }
230247
231248 /// Add data to the store, returning their addresses in the store
@@ -313,8 +330,8 @@ pub(crate) struct TableInstance {
313330impl TableInstance {
314331 pub ( crate ) fn new ( kind : TableType , owner : ModuleInstanceAddr ) -> Self {
315332 Self {
333+ elements : vec ! [ 0 ; kind. size_initial as usize ] ,
316334 kind,
317- elements : Vec :: new ( ) ,
318335 owner,
319336 }
320337 }
0 commit comments