@@ -26,33 +26,40 @@ use super::StoreApi;
26
26
use crate :: Trap ;
27
27
28
28
#[ derive( Debug , Default ) ]
29
- pub struct Store ( InterpreterStore < ' static > ) ;
29
+ pub struct Store {
30
+ inst : Option < InstId > ,
31
+ store : InterpreterStore < ' static > ,
32
+ }
30
33
31
34
impl Store {
32
35
pub fn instantiate (
33
36
& mut self , module : Module < ' static > , memory : & ' static mut [ u8 ] ,
34
37
) -> Result < InstId , Error > {
35
- self . 0 . instantiate ( module, memory)
38
+ // We assume a single module per applet.
39
+ assert ! ( self . inst. is_none( ) ) ;
40
+ let inst = self . store . instantiate ( module, memory) ?;
41
+ self . inst = Some ( inst) ;
42
+ Ok ( inst)
36
43
}
37
44
38
45
pub fn link_func (
39
46
& mut self , module : & ' static str , name : & ' static str , params : usize , results : usize ,
40
47
) -> Result < ( ) , Error > {
41
- self . 0 . link_func ( module, name, params, results)
48
+ self . store . link_func ( module, name, params, results)
42
49
}
43
50
44
51
pub fn link_func_default ( & mut self , module : & ' static str ) -> Result < ( ) , Error > {
45
- self . 0 . link_func_default ( module)
52
+ self . store . link_func_default ( module)
46
53
}
47
54
48
55
pub fn invoke < ' a > (
49
56
& ' a mut self , inst : InstId , name : & str , args : Vec < Val > ,
50
57
) -> Result < RunResult < ' a , ' static > , Error > {
51
- self . 0 . invoke ( inst, name, args)
58
+ self . store . invoke ( inst, name, args)
52
59
}
53
60
54
61
pub fn last_call ( & mut self ) -> Option < Call < ' _ , ' static > > {
55
- self . 0 . last_call ( )
62
+ self . store . last_call ( )
56
63
}
57
64
}
58
65
@@ -61,26 +68,25 @@ impl StoreApi for Store {
61
68
= Memory < ' a >
62
69
where Self : ' a ;
63
70
64
- fn memory ( & mut self ) -> Memory < ' _ > {
65
- Memory :: new ( & mut self . 0 )
71
+ fn memory < ' a > ( & ' a mut self ) -> Memory < ' a > {
72
+ let inst = self . inst . unwrap ( ) ;
73
+ let store = self as * mut Store ;
74
+ let memory = SliceCell :: new ( self . store . memory ( inst) . unwrap ( ) ) ;
75
+ Memory { store, memory }
66
76
}
67
77
}
68
78
69
79
pub struct Memory < ' a > {
70
- store : * mut InterpreterStore < ' static > ,
80
+ store : * mut Store ,
71
81
memory : SliceCell < ' a , u8 > ,
72
82
}
73
83
74
84
impl < ' a > Memory < ' a > {
75
- fn new ( store : & ' a mut InterpreterStore < ' static > ) -> Self {
76
- Self { store, memory : SliceCell :: new ( store. last_call ( ) . unwrap ( ) . mem ( ) ) }
77
- }
78
-
79
- fn with_store < T > ( & mut self , f : impl FnOnce ( & mut InterpreterStore < ' static > ) -> T ) -> T {
85
+ fn with_store < T > ( & mut self , f : impl FnOnce ( & mut Store ) -> T ) -> T {
80
86
self . memory . reset ( ) ; // only to free the internal state early
81
87
let store = unsafe { & mut * self . store } ;
82
88
let result = f ( store) ;
83
- * self = Memory :: new ( store) ;
89
+ * self = store. memory ( ) ;
84
90
result
85
91
}
86
92
}
0 commit comments