@@ -132,70 +132,66 @@ pub fn run(params: FunctionRunParams) -> Result<FunctionRunResult> {
132132 let output_stream = MemoryOutputPipe :: new ( usize:: MAX ) ;
133133 let error_stream = MemoryOutputPipe :: new ( usize:: MAX ) ;
134134
135- let memory_usage: u64 ;
136- let instructions: u64 ;
137135 let mut error_logs: String = String :: new ( ) ;
138- let mut module_result: Result < ( ) , anyhow:: Error > ;
139- let profile_data: Option < String > ;
140136
141- {
142- let mut linker = Linker :: new ( & engine) ;
143- wasmtime_wasi:: preview1:: add_to_linker_sync ( & mut linker, |ctx : & mut FunctionContext | {
144- & mut ctx. wasi
145- } ) ?;
146- deterministic_wasi_ctx:: replace_scheduling_functions ( & mut linker) ?;
147- let mut wasi_builder = WasiCtxBuilder :: new ( ) ;
148- wasi_builder. stdin ( input_stream) ;
149- wasi_builder. stdout ( output_stream. clone ( ) ) ;
150- wasi_builder. stderr ( error_stream. clone ( ) ) ;
151- deterministic_wasi_ctx:: add_determinism_to_wasi_ctx_builder ( & mut wasi_builder) ;
152- let wasi = wasi_builder. build_p1 ( ) ;
153- let function_context = FunctionContext :: new ( wasi) ;
154- let mut store = Store :: new ( & engine, function_context) ;
155- store. limiter ( |s| & mut s. limiter ) ;
156- store. set_fuel ( STARTING_FUEL ) ?;
157- store. set_epoch_deadline ( 1 ) ;
158-
159- import_modules ( & module, & engine, & mut linker, & mut store) ;
160-
161- linker. module ( & mut store, "Function" , & module) ?;
162- let instance = linker. instantiate ( & mut store, & module) ?;
163-
164- let func = instance. get_typed_func :: < ( ) , ( ) > ( store. as_context_mut ( ) , export) ?;
165-
166- ( module_result, profile_data) = if let Some ( profile_opts) = profile_opts {
167- let ( result, profile_data) = wasmprof:: ProfilerBuilder :: new ( & mut store)
168- . frequency ( profile_opts. interval )
169- . weight_unit ( wasmprof:: WeightUnit :: Fuel )
170- . profile ( |store| func. call ( store. as_context_mut ( ) , ( ) ) ) ;
171-
172- (
173- result,
174- Some ( profile_data. into_collapsed_stacks ( ) . to_string ( ) ) ,
175- )
176- } else {
177- ( func. call ( store. as_context_mut ( ) , ( ) ) , None )
178- } ;
179-
180- // modules may exit with a specific exit code, an exit code of 0 is considered success but is reported as
181- // a GuestFault by wasmtime, so we need to map it to a success result. Any other exit code is considered
182- // a failure.
183- module_result = module_result. or_else ( |error| match error. downcast_ref :: < I32Exit > ( ) {
184- Some ( I32Exit ( 0 ) ) => Ok ( ( ) ) ,
185- Some ( I32Exit ( code) ) => Err ( anyhow ! ( "module exited with code: {}" , code) ) ,
186- None => Err ( error) ,
187- } ) ;
137+ let mut linker = Linker :: new ( & engine) ;
138+ wasmtime_wasi:: preview1:: add_to_linker_sync ( & mut linker, |ctx : & mut FunctionContext | {
139+ & mut ctx. wasi
140+ } ) ?;
141+ deterministic_wasi_ctx:: replace_scheduling_functions ( & mut linker) ?;
142+ let mut wasi_builder = WasiCtxBuilder :: new ( ) ;
143+ wasi_builder. stdin ( input_stream) ;
144+ wasi_builder. stdout ( output_stream. clone ( ) ) ;
145+ wasi_builder. stderr ( error_stream. clone ( ) ) ;
146+ deterministic_wasi_ctx:: add_determinism_to_wasi_ctx_builder ( & mut wasi_builder) ;
147+ let wasi = wasi_builder. build_p1 ( ) ;
148+ let function_context = FunctionContext :: new ( wasi) ;
149+ let mut store = Store :: new ( & engine, function_context) ;
150+ store. limiter ( |s| & mut s. limiter ) ;
151+ store. set_fuel ( STARTING_FUEL ) ?;
152+ store. set_epoch_deadline ( 1 ) ;
153+
154+ import_modules ( & module, & engine, & mut linker, & mut store) ;
155+
156+ linker. module ( & mut store, "Function" , & module) ?;
157+ let instance = linker. instantiate ( & mut store, & module) ?;
158+
159+ let func = instance. get_typed_func :: < ( ) , ( ) > ( store. as_context_mut ( ) , export) ?;
160+
161+ let ( mut module_result, profile_data) = if let Some ( profile_opts) = profile_opts {
162+ let ( result, profile_data) = wasmprof:: ProfilerBuilder :: new ( & mut store)
163+ . frequency ( profile_opts. interval )
164+ . weight_unit ( wasmprof:: WeightUnit :: Fuel )
165+ . profile ( |store| func. call ( store. as_context_mut ( ) , ( ) ) ) ;
166+
167+ (
168+ result,
169+ Some ( profile_data. into_collapsed_stacks ( ) . to_string ( ) ) ,
170+ )
171+ } else {
172+ ( func. call ( store. as_context_mut ( ) , ( ) ) , None )
173+ } ;
174+
175+ // modules may exit with a specific exit code, an exit code of 0 is considered success but is reported as
176+ // a GuestFault by wasmtime, so we need to map it to a success result. Any other exit code is considered
177+ // a failure.
178+ module_result = module_result. or_else ( |error| match error. downcast_ref :: < I32Exit > ( ) {
179+ Some ( I32Exit ( 0 ) ) => Ok ( ( ) ) ,
180+ Some ( I32Exit ( code) ) => Err ( anyhow ! ( "module exited with code: {}" , code) ) ,
181+ None => Err ( error) ,
182+ } ) ;
188183
189- memory_usage = store. data ( ) . max_memory_bytes ( ) as u64 / 1024 ;
190- instructions = STARTING_FUEL . saturating_sub ( store. get_fuel ( ) . unwrap_or_default ( ) ) ;
184+ let memory_usage = store. data ( ) . max_memory_bytes ( ) as u64 / 1024 ;
185+ let instructions = STARTING_FUEL . saturating_sub ( store. get_fuel ( ) . unwrap_or_default ( ) ) ;
191186
192- match module_result {
193- Ok ( _) => { }
194- Err ( ref e) => {
195- error_logs = e. to_string ( ) ;
196- }
187+ match module_result {
188+ Ok ( _) => { }
189+ Err ( ref e) => {
190+ error_logs = e. to_string ( ) ;
197191 }
198- } ;
192+ }
193+
194+ drop ( store) ;
199195
200196 let mut logs = error_stream
201197 . try_into_inner ( )
0 commit comments