@@ -2,7 +2,7 @@ use anyhow::Result;
2
2
use std:: boxed:: Box ;
3
3
use std:: str;
4
4
use wasi_common:: { pipe:: WritePipe , sync:: WasiCtxBuilder , WasiCtx , WasiFile } ;
5
- use wasmtime:: { Engine , Instance , Linker , Store } ;
5
+ use wasmtime:: { AsContextMut , Engine , Instance , Linker , Store } ;
6
6
7
7
mod common;
8
8
@@ -49,9 +49,10 @@ fn run_js_src<T: WasiFile + Clone + 'static>(js_src: &str, stderr: &T) -> Result
49
49
let ( instance, mut store) = create_wasm_env ( stderr) ?;
50
50
51
51
let eval_bytecode_func =
52
- instance. get_typed_func :: < ( u32 , u32 ) , ( ) > ( & mut store, "eval_bytecode" ) ?;
53
- let ( bytecode_ptr, bytecode_len) = compile_src ( js_src. as_bytes ( ) , & instance, & mut store) ?;
54
- eval_bytecode_func. call ( & mut store, ( bytecode_ptr, bytecode_len) ) ?;
52
+ instance. get_typed_func :: < ( u32 , u32 ) , ( ) > ( store. as_context_mut ( ) , "eval_bytecode" ) ?;
53
+ let ( bytecode_ptr, bytecode_len) =
54
+ compile_src ( js_src. as_bytes ( ) , & instance, store. as_context_mut ( ) ) ?;
55
+ eval_bytecode_func. call ( store. as_context_mut ( ) , ( bytecode_ptr, bytecode_len) ) ?;
55
56
Ok ( ( ) )
56
57
}
57
58
@@ -62,11 +63,14 @@ fn run_invoke<T: WasiFile + Clone + 'static>(
62
63
) -> Result < ( ) > {
63
64
let ( instance, mut store) = create_wasm_env ( stderr) ?;
64
65
65
- let invoke_func = instance. get_typed_func :: < ( u32 , u32 , u32 , u32 ) , ( ) > ( & mut store, "invoke" ) ?;
66
- let ( bytecode_ptr, bytecode_len) = compile_src ( js_src. as_bytes ( ) , & instance, & mut store) ?;
67
- let ( fn_name_ptr, fn_name_len) = copy_func_name ( fn_to_invoke, & instance, & mut store) ?;
66
+ let invoke_func =
67
+ instance. get_typed_func :: < ( u32 , u32 , u32 , u32 ) , ( ) > ( store. as_context_mut ( ) , "invoke" ) ?;
68
+ let ( bytecode_ptr, bytecode_len) =
69
+ compile_src ( js_src. as_bytes ( ) , & instance, store. as_context_mut ( ) ) ?;
70
+ let ( fn_name_ptr, fn_name_len) =
71
+ copy_func_name ( fn_to_invoke, & instance, store. as_context_mut ( ) ) ?;
68
72
invoke_func. call (
69
- & mut store,
73
+ store. as_context_mut ( ) ,
70
74
( bytecode_ptr, bytecode_len, fn_name_ptr, fn_name_len) ,
71
75
) ?;
72
76
Ok ( ( ) )
@@ -84,25 +88,36 @@ fn create_wasm_env<T: WasiFile + Clone + 'static>(
84
88
let module = common:: create_quickjs_provider_module ( & engine) ?;
85
89
86
90
let mut store = Store :: new ( & engine, wasi) ;
87
- let instance = linker. instantiate ( & mut store, & module) ?;
91
+ let instance = linker. instantiate ( store. as_context_mut ( ) , & module) ?;
88
92
89
93
Ok ( ( instance, store) )
90
94
}
91
95
92
96
fn compile_src (
93
97
js_src : & [ u8 ] ,
94
98
instance : & Instance ,
95
- mut store : & mut Store < WasiCtx > ,
99
+ mut store : impl AsContextMut ,
96
100
) -> Result < ( u32 , u32 ) > {
97
- let memory = instance. get_memory ( & mut store, "memory" ) . unwrap ( ) ;
98
- let compile_src_func = instance. get_typed_func :: < ( u32 , u32 ) , u32 > ( & mut store, "compile_src" ) ?;
99
-
100
- let js_src_ptr = allocate_memory ( instance, store, 1 , js_src. len ( ) . try_into ( ) ?) ?;
101
- memory. write ( & mut store, js_src_ptr. try_into ( ) ?, js_src) ?;
101
+ let memory = instance
102
+ . get_memory ( store. as_context_mut ( ) , "memory" )
103
+ . unwrap ( ) ;
104
+ let compile_src_func =
105
+ instance. get_typed_func :: < ( u32 , u32 ) , u32 > ( store. as_context_mut ( ) , "compile_src" ) ?;
106
+
107
+ let js_src_ptr = allocate_memory (
108
+ instance,
109
+ store. as_context_mut ( ) ,
110
+ 1 ,
111
+ js_src. len ( ) . try_into ( ) ?,
112
+ ) ?;
113
+ memory. write ( store. as_context_mut ( ) , js_src_ptr. try_into ( ) ?, js_src) ?;
102
114
103
- let ret_ptr = compile_src_func. call ( & mut store, ( js_src_ptr, js_src. len ( ) . try_into ( ) ?) ) ?;
115
+ let ret_ptr = compile_src_func. call (
116
+ store. as_context_mut ( ) ,
117
+ ( js_src_ptr, js_src. len ( ) . try_into ( ) ?) ,
118
+ ) ?;
104
119
let mut ret_buffer = [ 0 ; 8 ] ;
105
- memory. read ( & mut store, ret_ptr. try_into ( ) ?, & mut ret_buffer) ?;
120
+ memory. read ( store. as_context ( ) , ret_ptr. try_into ( ) ?, & mut ret_buffer) ?;
106
121
let bytecode_ptr = u32:: from_le_bytes ( ret_buffer[ 0 ..4 ] . try_into ( ) ?) ;
107
122
let bytecode_len = u32:: from_le_bytes ( ret_buffer[ 4 ..8 ] . try_into ( ) ?) ;
108
123
@@ -112,27 +127,40 @@ fn compile_src(
112
127
fn copy_func_name (
113
128
fn_name : & str ,
114
129
instance : & Instance ,
115
- mut store : & mut Store < WasiCtx > ,
130
+ mut store : impl AsContextMut ,
116
131
) -> Result < ( u32 , u32 ) > {
117
- let memory = instance. get_memory ( & mut store, "memory" ) . unwrap ( ) ;
132
+ let memory = instance
133
+ . get_memory ( store. as_context_mut ( ) , "memory" )
134
+ . unwrap ( ) ;
118
135
let fn_name_bytes = fn_name. as_bytes ( ) ;
119
- let fn_name_ptr = allocate_memory ( instance, store, 1 , fn_name_bytes. len ( ) . try_into ( ) ?) ?;
120
- memory. write ( & mut store, fn_name_ptr. try_into ( ) ?, fn_name_bytes) ?;
136
+ let fn_name_ptr = allocate_memory (
137
+ instance,
138
+ store. as_context_mut ( ) ,
139
+ 1 ,
140
+ fn_name_bytes. len ( ) . try_into ( ) ?,
141
+ ) ?;
142
+ memory. write (
143
+ store. as_context_mut ( ) ,
144
+ fn_name_ptr. try_into ( ) ?,
145
+ fn_name_bytes,
146
+ ) ?;
121
147
122
148
Ok ( ( fn_name_ptr, fn_name_bytes. len ( ) . try_into ( ) ?) )
123
149
}
124
150
125
151
fn allocate_memory (
126
152
instance : & Instance ,
127
- mut store : & mut Store < WasiCtx > ,
153
+ mut store : impl AsContextMut ,
128
154
alignment : u32 ,
129
155
new_size : u32 ,
130
156
) -> Result < u32 > {
131
- let realloc_func = instance
132
- . get_typed_func :: < ( u32 , u32 , u32 , u32 ) , u32 > ( & mut store, "canonical_abi_realloc" ) ?;
157
+ let realloc_func = instance. get_typed_func :: < ( u32 , u32 , u32 , u32 ) , u32 > (
158
+ store. as_context_mut ( ) ,
159
+ "canonical_abi_realloc" ,
160
+ ) ?;
133
161
let orig_ptr = 0 ;
134
162
let orig_size = 0 ;
135
163
realloc_func
136
- . call ( & mut store, ( orig_ptr, orig_size, alignment, new_size) )
164
+ . call ( store, ( orig_ptr, orig_size, alignment, new_size) )
137
165
. map_err ( Into :: into)
138
166
}
0 commit comments