@@ -86,9 +86,9 @@ impl Default for Store {
86
86
pub ( crate ) struct StoreData {
87
87
pub ( crate ) funcs : Vec < Rc < FunctionInstance > > ,
88
88
pub ( crate ) tables : Vec < Rc < RefCell < TableInstance > > > ,
89
- pub ( crate ) mems : Vec < Rc < RefCell < MemoryInstance > > > ,
89
+ pub ( crate ) memories : Vec < Rc < RefCell < MemoryInstance > > > ,
90
90
pub ( crate ) globals : Vec < Rc < RefCell < GlobalInstance > > > ,
91
- pub ( crate ) elems : Vec < ElemInstance > ,
91
+ pub ( crate ) elements : Vec < ElementInstance > ,
92
92
pub ( crate ) datas : Vec < DataInstance > ,
93
93
}
94
94
@@ -142,15 +142,15 @@ impl Store {
142
142
}
143
143
144
144
/// Add memories to the store, returning their addresses in the store
145
- pub ( crate ) fn init_mems ( & mut self , mems : Vec < MemoryType > , idx : ModuleInstanceAddr ) -> Result < Vec < MemAddr > > {
146
- let mem_count = self . data . mems . len ( ) ;
145
+ pub ( crate ) fn init_memories ( & mut self , memories : Vec < MemoryType > , idx : ModuleInstanceAddr ) -> Result < Vec < MemAddr > > {
146
+ let mem_count = self . data . memories . len ( ) ;
147
147
let mut mem_addrs = Vec :: with_capacity ( mem_count) ;
148
- for ( i, mem) in mems . into_iter ( ) . enumerate ( ) {
148
+ for ( i, mem) in memories . into_iter ( ) . enumerate ( ) {
149
149
if let MemoryArch :: I64 = mem. arch {
150
150
return Err ( Error :: UnsupportedFeature ( "64-bit memories" . to_string ( ) ) ) ;
151
151
}
152
152
log:: info!( "adding memory: {:?}" , mem) ;
153
- self . data . mems . push ( Rc :: new ( RefCell :: new ( MemoryInstance :: new ( mem, idx) ) ) ) ;
153
+ self . data . memories . push ( Rc :: new ( RefCell :: new ( MemoryInstance :: new ( mem, idx) ) ) ) ;
154
154
155
155
mem_addrs. push ( ( i + mem_count) as MemAddr ) ;
156
156
}
@@ -175,16 +175,16 @@ impl Store {
175
175
176
176
/// Add elements to the store, returning their addresses in the store
177
177
/// Should be called after the tables have been added
178
- pub ( crate ) fn init_elems (
178
+ pub ( crate ) fn init_elements (
179
179
& mut self ,
180
180
table_addrs : & [ TableAddr ] ,
181
- elems : Vec < Element > ,
181
+ elements : Vec < Element > ,
182
182
idx : ModuleInstanceAddr ,
183
183
) -> Result < Box < [ Addr ] > > {
184
- let elem_count = self . data . elems . len ( ) ;
184
+ let elem_count = self . data . elements . len ( ) ;
185
185
let mut elem_addrs = Vec :: with_capacity ( elem_count) ;
186
- for ( i, elem ) in elems . into_iter ( ) . enumerate ( ) {
187
- let init = elem
186
+ for ( i, element ) in elements . into_iter ( ) . enumerate ( ) {
187
+ let init = element
188
188
. items
189
189
. iter ( )
190
190
. map ( |item| {
@@ -194,7 +194,7 @@ impl Store {
194
194
} )
195
195
. collect :: < Result < Vec < _ > > > ( ) ?;
196
196
197
- let items = match elem . kind {
197
+ let items = match element . kind {
198
198
// doesn't need to be initialized, can be initialized lazily using the `table.init` instruction
199
199
ElementKind :: Passive => Some ( init) ,
200
200
@@ -228,7 +228,7 @@ impl Store {
228
228
}
229
229
} ;
230
230
231
- self . data . elems . push ( ElemInstance :: new ( elem . kind , idx, items) ) ;
231
+ self . data . elements . push ( ElementInstance :: new ( element . kind , idx, items) ) ;
232
232
elem_addrs. push ( ( i + elem_count) as Addr ) ;
233
233
}
234
234
@@ -262,7 +262,7 @@ impl Store {
262
262
let offset = self . eval_i32_const ( & offset) ?;
263
263
264
264
let mem =
265
- self . data . mems . get_mut ( mem_addr as usize ) . ok_or_else ( || {
265
+ self . data . memories . get_mut ( mem_addr as usize ) . ok_or_else ( || {
266
266
Error :: Other ( format ! ( "memory {} not found for data segment {}" , mem_addr, i) )
267
267
} ) ?;
268
268
@@ -296,8 +296,8 @@ impl Store {
296
296
if let MemoryArch :: I64 = mem. arch {
297
297
return Err ( Error :: UnsupportedFeature ( "64-bit memories" . to_string ( ) ) ) ;
298
298
}
299
- self . data . mems . push ( Rc :: new ( RefCell :: new ( MemoryInstance :: new ( mem, idx) ) ) ) ;
300
- Ok ( self . data . mems . len ( ) as MemAddr - 1 )
299
+ self . data . memories . push ( Rc :: new ( RefCell :: new ( MemoryInstance :: new ( mem, idx) ) ) ) ;
300
+ Ok ( self . data . memories . len ( ) as MemAddr - 1 )
301
301
}
302
302
303
303
pub ( crate ) fn add_func ( & mut self , func : Function , type_idx : TypeAddr , idx : ModuleInstanceAddr ) -> Result < FuncAddr > {
@@ -348,7 +348,7 @@ impl Store {
348
348
349
349
/// Get the memory at the actual index in the store
350
350
pub ( crate ) fn get_mem ( & self , addr : usize ) -> Result < & Rc < RefCell < MemoryInstance > > > {
351
- self . data . mems . get ( addr) . ok_or_else ( || Error :: Other ( format ! ( "memory {} not found" , addr) ) )
351
+ self . data . memories . get ( addr) . ok_or_else ( || Error :: Other ( format ! ( "memory {} not found" , addr) ) )
352
352
}
353
353
354
354
/// Get the table at the actual index in the store
@@ -357,8 +357,8 @@ impl Store {
357
357
}
358
358
359
359
/// Get the element at the actual index in the store
360
- pub ( crate ) fn get_elem ( & self , addr : usize ) -> Result < & ElemInstance > {
361
- self . data . elems . get ( addr) . ok_or_else ( || Error :: Other ( format ! ( "element {} not found" , addr) ) )
360
+ pub ( crate ) fn get_elem ( & self , addr : usize ) -> Result < & ElementInstance > {
361
+ self . data . elements . get ( addr) . ok_or_else ( || Error :: Other ( format ! ( "element {} not found" , addr) ) )
362
362
}
363
363
364
364
/// Get the global at the actual index in the store
@@ -413,25 +413,29 @@ impl FunctionInstance {
413
413
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#table-instances>
414
414
#[ derive( Debug ) ]
415
415
pub ( crate ) struct TableInstance {
416
- pub ( crate ) elements : Vec < Addr > ,
416
+ pub ( crate ) elements : Vec < Option < Addr > > ,
417
417
pub ( crate ) _kind : TableType ,
418
418
pub ( crate ) _owner : ModuleInstanceAddr , // index into store.module_instances
419
419
}
420
420
421
421
impl TableInstance {
422
422
pub ( crate ) fn new ( kind : TableType , owner : ModuleInstanceAddr ) -> Self {
423
- Self { elements : vec ! [ 0 ; kind. size_initial as usize ] , _kind : kind, _owner : owner }
423
+ Self { elements : vec ! [ None ; kind. size_initial as usize ] , _kind : kind, _owner : owner }
424
424
}
425
425
426
426
pub ( crate ) fn get ( & self , addr : usize ) -> Result < Addr > {
427
- self . elements . get ( addr) . copied ( ) . ok_or_else ( || Trap :: UndefinedElement { index : addr } . into ( ) )
427
+ self . elements
428
+ . get ( addr)
429
+ . copied ( )
430
+ . ok_or_else ( || Error :: Trap ( Trap :: UndefinedElement { index : addr } ) )
431
+ . map ( |elem| elem. ok_or_else ( || Trap :: UninitializedElement { index : addr } . into ( ) ) ) ?
428
432
}
429
433
430
434
pub ( crate ) fn set ( & mut self , addr : usize , value : Addr ) -> Result < ( ) > {
431
435
if addr >= self . elements . len ( ) {
432
436
return Err ( Error :: Other ( format ! ( "table element {} not found" , addr) ) ) ;
433
437
}
434
- self . elements [ addr] = value;
438
+ self . elements [ addr] = Some ( value) ;
435
439
Ok ( ( ) )
436
440
}
437
441
@@ -440,6 +444,8 @@ impl TableInstance {
440
444
}
441
445
442
446
pub ( crate ) fn init ( & mut self , offset : i32 , init : & [ Addr ] ) -> Result < ( ) > {
447
+ let init = init. iter ( ) . map ( |item| Some ( * item) ) . collect :: < Vec < _ > > ( ) ;
448
+
443
449
let offset = offset as usize ;
444
450
let end = offset. checked_add ( init. len ( ) ) . ok_or_else ( || {
445
451
Error :: Trap ( crate :: Trap :: TableOutOfBounds { offset, len : init. len ( ) , max : self . elements . len ( ) } )
@@ -449,7 +455,7 @@ impl TableInstance {
449
455
return Err ( crate :: Trap :: TableOutOfBounds { offset, len : init. len ( ) , max : self . elements . len ( ) } . into ( ) ) ;
450
456
}
451
457
452
- self . elements [ offset..end] . copy_from_slice ( init) ;
458
+ self . elements [ offset..end] . copy_from_slice ( & init) ;
453
459
Ok ( ( ) )
454
460
}
455
461
}
@@ -573,13 +579,13 @@ impl GlobalInstance {
573
579
///
574
580
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#element-instances>
575
581
#[ derive( Debug ) ]
576
- pub ( crate ) struct ElemInstance {
582
+ pub ( crate ) struct ElementInstance {
577
583
pub ( crate ) kind : ElementKind ,
578
584
pub ( crate ) items : Option < Vec < u32 > > , // none is the element was dropped
579
585
_owner : ModuleInstanceAddr , // index into store.module_instances
580
586
}
581
587
582
- impl ElemInstance {
588
+ impl ElementInstance {
583
589
pub ( crate ) fn new ( kind : ElementKind , owner : ModuleInstanceAddr , items : Option < Vec < u32 > > ) -> Self {
584
590
Self { kind, _owner : owner, items }
585
591
}
0 commit comments