1- use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
1+ use core:: {
2+ cell:: RefCell ,
3+ sync:: atomic:: { AtomicUsize , Ordering } ,
4+ } ;
25
36use alloc:: { format, rc:: Rc , vec:: Vec } ;
47use tinywasm_types:: {
@@ -8,7 +11,7 @@ use tinywasm_types::{
811
912use crate :: {
1013 runtime:: { self , DefaultRuntime } ,
11- Error , ModuleInstance , Result ,
14+ Error , ModuleInstance , RawWasmValue , Result ,
1215} ;
1316
1417// global store id counter
@@ -82,7 +85,7 @@ pub(crate) struct StoreData {
8285 pub ( crate ) funcs : Vec < Rc < FunctionInstance > > ,
8386 pub ( crate ) tables : Vec < TableInstance > ,
8487 pub ( crate ) mems : Vec < Rc < MemoryInstance > > ,
85- pub ( crate ) globals : Vec < Rc < GlobalInstance > > ,
88+ pub ( crate ) globals : Vec < Rc < RefCell < GlobalInstance > > > ,
8689 pub ( crate ) elems : Vec < ElemInstance > ,
8790 pub ( crate ) datas : Vec < DataInstance > ,
8891}
@@ -143,8 +146,26 @@ impl Store {
143146 let mut global_addrs = Vec :: with_capacity ( global_count) ;
144147 for ( i, global) in globals. into_iter ( ) . enumerate ( ) {
145148 // TODO: initialize globals
146- // Don't fail here yet - we'll fail when we try to use the global
147- self . data . globals . push ( Rc :: new ( GlobalInstance :: new ( global. ty , 0 , idx) ) ) ;
149+ use tinywasm_types:: ConstInstruction :: * ;
150+ let val = match global. init {
151+ F32Const ( f) => RawWasmValue :: from ( f) ,
152+ F64Const ( f) => RawWasmValue :: from ( f) ,
153+ I32Const ( i) => RawWasmValue :: from ( i) ,
154+ I64Const ( i) => RawWasmValue :: from ( i) ,
155+ GlobalGet ( addr) => {
156+ let addr = global_addrs[ addr as usize ] ;
157+ let global = self . data . globals [ addr as usize ] . clone ( ) ;
158+ let val = global. borrow ( ) . value ;
159+ val
160+ }
161+ RefNull ( _) => RawWasmValue :: default ( ) ,
162+ RefFunc ( idx) => RawWasmValue :: from ( idx as i64 ) ,
163+ } ;
164+
165+ self . data
166+ . globals
167+ . push ( Rc :: new ( RefCell :: new ( GlobalInstance :: new ( global. ty , val, idx) ) ) ) ;
168+
148169 global_addrs. push ( ( i + global_count) as Addr ) ;
149170 }
150171 global_addrs
@@ -179,6 +200,22 @@ impl Store {
179200 . get ( addr)
180201 . ok_or_else ( || Error :: Other ( format ! ( "function {} not found" , addr) ) )
181202 }
203+
204+ pub ( crate ) fn get_global_val ( & self , addr : usize ) -> Result < RawWasmValue > {
205+ self . data
206+ . globals
207+ . get ( addr)
208+ . ok_or_else ( || Error :: Other ( format ! ( "global {} not found" , addr) ) )
209+ . map ( |global| global. borrow ( ) . value )
210+ }
211+
212+ pub ( crate ) fn set_global_val ( & mut self , addr : usize , value : RawWasmValue ) -> Result < ( ) > {
213+ self . data
214+ . globals
215+ . get ( addr)
216+ . ok_or_else ( || Error :: Other ( format ! ( "global {} not found" , addr) ) )
217+ . map ( |global| global. borrow_mut ( ) . value = value)
218+ }
182219}
183220
184221#[ derive( Debug ) ]
@@ -254,12 +291,12 @@ impl MemoryInstance {
254291#[ derive( Debug ) ]
255292pub ( crate ) struct GlobalInstance {
256293 pub ( crate ) ty : GlobalType ,
257- pub ( crate ) value : Addr ,
294+ pub ( crate ) value : RawWasmValue ,
258295 owner : ModuleInstanceAddr , // index into store.module_instances
259296}
260297
261298impl GlobalInstance {
262- pub ( crate ) fn new ( ty : GlobalType , value : Addr , owner : ModuleInstanceAddr ) -> Self {
299+ pub ( crate ) fn new ( ty : GlobalType , value : RawWasmValue , owner : ModuleInstanceAddr ) -> Self {
263300 Self { ty, value, owner }
264301 }
265302}
0 commit comments