1
- use alloc:: { boxed:: Box , format, string:: ToString , sync:: Arc , vec :: Vec } ;
1
+ use alloc:: { boxed:: Box , format, string:: ToString , sync:: Arc } ;
2
2
use tinywasm_types:: {
3
3
DataAddr , ElemAddr , Export , ExternVal , ExternalKind , FuncAddr , FuncType , GlobalAddr , Import , MemAddr ,
4
4
ModuleInstanceAddr , TableAddr ,
@@ -11,11 +11,11 @@ use crate::{
11
11
12
12
/// A WebAssembly Module Instance
13
13
///
14
- /// Addrs are indices into the store's data structures.
14
+ /// Backed by an Arc, so cloning is cheap
15
15
///
16
16
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#module-instances>
17
17
#[ derive( Debug , Clone ) ]
18
- pub struct ModuleInstance ( pub ( crate ) Arc < ModuleInstanceInner > ) ;
18
+ pub struct ModuleInstance ( Arc < ModuleInstanceInner > ) ;
19
19
20
20
#[ allow( dead_code) ]
21
21
#[ derive( Debug ) ]
@@ -25,19 +25,24 @@ pub(crate) struct ModuleInstanceInner {
25
25
26
26
pub ( crate ) types : Box < [ FuncType ] > ,
27
27
28
- pub ( crate ) func_addrs : Vec < FuncAddr > ,
29
- pub ( crate ) table_addrs : Vec < TableAddr > ,
30
- pub ( crate ) mem_addrs : Vec < MemAddr > ,
31
- pub ( crate ) global_addrs : Vec < GlobalAddr > ,
32
- pub ( crate ) elem_addrs : Vec < ElemAddr > ,
33
- pub ( crate ) data_addrs : Vec < DataAddr > ,
28
+ pub ( crate ) func_addrs : Box < [ FuncAddr ] > ,
29
+ pub ( crate ) table_addrs : Box < [ TableAddr ] > ,
30
+ pub ( crate ) mem_addrs : Box < [ MemAddr ] > ,
31
+ pub ( crate ) global_addrs : Box < [ GlobalAddr ] > ,
32
+ pub ( crate ) elem_addrs : Box < [ ElemAddr ] > ,
33
+ pub ( crate ) data_addrs : Box < [ DataAddr ] > ,
34
34
35
35
pub ( crate ) func_start : Option < FuncAddr > ,
36
36
pub ( crate ) imports : Box < [ Import ] > ,
37
37
pub ( crate ) exports : Box < [ Export ] > ,
38
38
}
39
39
40
40
impl ModuleInstance {
41
+ // drop the module instance reference and swap it with another one
42
+ pub ( crate ) fn swap ( & mut self , other : Self ) {
43
+ self . 0 = other. 0 ;
44
+ }
45
+
41
46
/// Get the module instance's address
42
47
pub fn id ( & self ) -> ModuleInstanceAddr {
43
48
self . 0 . idx
@@ -57,6 +62,7 @@ impl ModuleInstance {
57
62
let mut addrs = imports. link ( store, & module, idx) ?;
58
63
let data = module. data ;
59
64
65
+ // TODO: check if the compiler correctly optimizes this to prevent wasted allocations
60
66
addrs. globals . extend ( store. init_globals ( data. globals . into ( ) , idx) ?) ;
61
67
addrs. funcs . extend ( store. init_funcs ( data. funcs . into ( ) , idx) ?) ;
62
68
addrs. tables . extend ( store. init_tables ( data. table_types . into ( ) , idx) ?) ;
@@ -70,12 +76,12 @@ impl ModuleInstance {
70
76
idx,
71
77
72
78
types : data. func_types ,
73
- func_addrs : addrs. funcs ,
74
- table_addrs : addrs. tables ,
75
- mem_addrs : addrs. mems ,
76
- global_addrs : addrs. globals ,
79
+ func_addrs : addrs. funcs . into_boxed_slice ( ) ,
80
+ table_addrs : addrs. tables . into_boxed_slice ( ) ,
81
+ mem_addrs : addrs. mems . into_boxed_slice ( ) ,
82
+ global_addrs : addrs. globals . into_boxed_slice ( ) ,
77
83
elem_addrs,
78
- data_addrs,
84
+ data_addrs : data_addrs ,
79
85
func_start : data. start_func ,
80
86
imports : data. imports ,
81
87
exports : data. exports ,
0 commit comments