21
21
22
22
use wasmtime:: Caller ;
23
23
24
- use sc_allocator :: { AllocationStats , FreeingBumpHeapAllocator } ;
25
- use sp_wasm_interface:: { Pointer , WordSize } ;
24
+ pub use sp_wasm_interface :: HostState ;
25
+ use sp_wasm_interface:: { util , FunctionContextToken , Pointer , WordSize } ;
26
26
27
- use crate :: { instance_wrapper:: MemoryWrapper , runtime:: StoreData , util} ;
28
-
29
- /// The state required to construct a HostContext context. The context only lasts for one host
30
- /// call, whereas the state is maintained for the duration of a Wasm runtime call, which may make
31
- /// many different host calls that must share state.
32
- pub struct HostState {
33
- /// The allocator instance to keep track of allocated memory.
34
- ///
35
- /// This is stored as an `Option` as we need to temporarily set this to `None` when we are
36
- /// allocating/deallocating memory. The problem being that we can only mutable access `caller`
37
- /// once.
38
- allocator : Option < FreeingBumpHeapAllocator > ,
39
- panic_message : Option < String > ,
40
- }
41
-
42
- impl HostState {
43
- /// Constructs a new `HostState`.
44
- pub fn new ( allocator : FreeingBumpHeapAllocator ) -> Self {
45
- HostState { allocator : Some ( allocator) , panic_message : None }
46
- }
47
-
48
- /// Takes the error message out of the host state, leaving a `None` in its place.
49
- pub fn take_panic_message ( & mut self ) -> Option < String > {
50
- self . panic_message . take ( )
51
- }
52
-
53
- pub ( crate ) fn allocation_stats ( & self ) -> AllocationStats {
54
- self . allocator . as_ref ( )
55
- . expect ( "Allocator is always set and only unavailable when doing an allocation/deallocation; qed" )
56
- . stats ( )
57
- }
58
- }
27
+ use crate :: runtime:: StoreData ;
59
28
60
29
/// A `HostContext` implements `FunctionContext` for making host calls from a Wasmtime
61
30
/// runtime. The `HostContext` exists only for the lifetime of the call and borrows state from
@@ -64,15 +33,6 @@ pub(crate) struct HostContext<'a> {
64
33
pub ( crate ) caller : Caller < ' a , StoreData > ,
65
34
}
66
35
67
- impl < ' a > HostContext < ' a > {
68
- fn host_state_mut ( & mut self ) -> & mut HostState {
69
- self . caller
70
- . data_mut ( )
71
- . host_state_mut ( )
72
- . expect ( "host state is not empty when calling a function in wasm; qed" )
73
- }
74
- }
75
-
76
36
impl < ' a > sp_wasm_interface:: FunctionContext for HostContext < ' a > {
77
37
fn read_memory_into (
78
38
& self ,
@@ -87,42 +47,27 @@ impl<'a> sp_wasm_interface::FunctionContext for HostContext<'a> {
87
47
}
88
48
89
49
fn allocate_memory ( & mut self , size : WordSize ) -> sp_wasm_interface:: Result < Pointer < u8 > > {
90
- let memory = self . caller . data ( ) . memory ( ) ;
91
- let mut allocator = self
92
- . host_state_mut ( )
93
- . allocator
94
- . take ( )
95
- . expect ( "allocator is not empty when calling a function in wasm; qed" ) ;
96
-
97
- // We can not return on error early, as we need to store back allocator.
98
- let res = allocator
99
- . allocate ( & mut MemoryWrapper ( & memory, & mut self . caller ) , size)
100
- . map_err ( |e| e. to_string ( ) ) ;
101
-
102
- self . host_state_mut ( ) . allocator = Some ( allocator) ;
103
-
104
- res
50
+ util:: allocate_memory ( & mut self . caller , size)
105
51
}
106
52
107
53
fn deallocate_memory ( & mut self , ptr : Pointer < u8 > ) -> sp_wasm_interface:: Result < ( ) > {
108
- let memory = self . caller . data ( ) . memory ( ) ;
109
- let mut allocator = self
110
- . host_state_mut ( )
111
- . allocator
112
- . take ( )
113
- . expect ( "allocator is not empty when calling a function in wasm; qed" ) ;
114
-
115
- // We can not return on error early, as we need to store back allocator.
116
- let res = allocator
117
- . deallocate ( & mut MemoryWrapper ( & memory, & mut self . caller ) , ptr)
118
- . map_err ( |e| e. to_string ( ) ) ;
119
-
120
- self . host_state_mut ( ) . allocator = Some ( allocator) ;
121
-
122
- res
54
+ util:: deallocate_memory ( & mut self . caller , ptr)
123
55
}
124
56
125
57
fn register_panic_error_message ( & mut self , message : & str ) {
126
- self . host_state_mut ( ) . panic_message = Some ( message. to_owned ( ) ) ;
58
+ self . caller
59
+ . data_mut ( )
60
+ . host_state_mut ( )
61
+ . expect ( "host state is not empty when calling a function in wasm; qed" )
62
+ . panic_message = Some ( message. to_owned ( ) ) ;
63
+ }
64
+
65
+ fn with_caller_mut_impl (
66
+ & mut self ,
67
+ _: FunctionContextToken ,
68
+ context : * mut ( ) ,
69
+ callback : fn ( * mut ( ) , & mut Caller < StoreData > ) ,
70
+ ) {
71
+ callback ( context, & mut self . caller )
127
72
}
128
73
}
0 commit comments