11use crate :: {
2- db:: { Db , DbRecordEvent , DbStep } ,
2+ db:: { Db , DbRecordEvent } ,
33 expr_loader:: ExprLoader ,
44 task:: {
5- self ,
65 Action , BranchesTaken , CoreTrace , FlowEvent , FlowStep , FlowUpdate , FlowUpdateState , FlowUpdateStateKind ,
76 FlowMode , FlowViewUpdate , Iteration , Location , Loop , LoopId , LoopIterationSteps , Position , RRTicks , StepCount
87 } ,
98 replay:: Replay ,
109 value:: Value ,
1110} ;
1211use log:: { info, warn, error} ;
13- use runtime_tracing:: { CallKey , FullValueRecord , Line , StepId } ;
12+ use runtime_tracing:: { CallKey , Line , StepId , ValueRecord , TypeId } ;
1413use std:: collections:: { HashMap , HashSet } ;
1514use std:: path:: PathBuf ;
1615use std:: error:: Error ;
@@ -34,7 +33,7 @@ impl FlowPreloader {
3433 match self . expr_loader . load_file ( & path_buf) {
3534 Ok ( _) => {
3635 info ! ( "Expression loader complete!" ) ;
37- let mut call_flow_preloader: CallFlowPreloader = CallFlowPreloader :: new ( self , location, HashSet :: new ( ) , HashSet :: new ( ) , mode) ;
36+ let mut call_flow_preloader: CallFlowPreloader = CallFlowPreloader :: new ( self , location. clone ( ) , HashSet :: new ( ) , HashSet :: new ( ) , mode) ;
3837 call_flow_preloader. load_flow ( location, replay)
3938 }
4039 Err ( e) => {
@@ -71,15 +70,15 @@ impl FlowPreloader {
7170 }
7271
7372 let mut call_flow_preloader = CallFlowPreloader :: new ( self , Location :: default ( ) , diff_lines, diff_call_keys, FlowMode :: Diff ) ;
74- let location = Location { line : Line ( 1 ) , ..Location :: default ( ) } ;
73+ let location = Location { line : 1 , ..Location :: default ( ) } ;
7574 call_flow_preloader. load_flow ( location, replay)
7675 }
7776
7877 // fn load_file(&mut self, path: &str) {
7978 // self.expr_loader.load_file(&PathBuf::from(path.to_string())).unwrap();
8079 // }
8180
82- pub fn get_var_list ( & self , line : Line , location : & Location ) -> Option < Vec < String > > {
81+ pub fn get_var_list ( & self , line : Position , location : & Location ) -> Option < Vec < String > > {
8382 self . expr_loader . get_expr_list ( line, location)
8483 }
8584
@@ -163,18 +162,21 @@ impl<'a> CallFlowPreloader<'a> {
163162 }
164163 }
165164
166- fn add_return_value ( & mut self , mut flow_view_update : FlowViewUpdate , replay : & mut dyn Replay , call_key : CallKey ) -> FlowViewUpdate {
165+ fn add_return_value ( & mut self , mut flow_view_update : FlowViewUpdate , replay : & mut dyn Replay ) -> FlowViewUpdate {
166+ // assumes that replay is stopped on the place where return value is available
167+
168+ let return_string = "return" . to_string ( ) ;
169+
167170 // The if condition ensures, that the Options on which .unwrap() is called
168171 // are never None, so it is safe to unwrap them.
169- let return_string = "return" . to_string ( ) ;
170172 if !flow_view_update. steps . is_empty ( ) {
171173
172- let db = Db :: new ( PathBuf :: from ( "" ) ) ; // we don't need its state for to_ct_value
174+ let db = Db :: new ( & PathBuf :: from ( "" ) ) ; // we don't need its state for to_ct_value
173175 let return_value_record = replay. load_return_value ( ) . unwrap_or ( ValueRecord :: Error {
174176 msg : "<return value error>" . to_string ( ) ,
175177 type_id : TypeId ( 0 ) ,
176178 } ) ;
177- let return_value = db. to_ct_value ( return_value_record) ;
179+ let return_value = db. to_ct_value ( & return_value_record) ;
178180
179181 #[ allow( clippy:: unwrap_used) ]
180182 flow_view_update. steps . last_mut ( ) . unwrap ( ) . before_values . insert (
@@ -207,7 +209,7 @@ impl<'a> CallFlowPreloader<'a> {
207209 flow_view_update
208210 }
209211
210- fn next_diff_flow_step ( & self , from_step_id : StepId , including_from : bool , replay : & mut dyn Replay ) -> ( StepId , bool ) {
212+ fn next_diff_flow_step ( & self , _from_step_id : StepId , _including_from : bool , _replay : & mut dyn Replay ) -> ( StepId , bool ) {
211213 // TODO: maybe combination of replay.next, diff_call_keys check, different for cases?s
212214 //
213215 // if from_step_id.0 >= db.steps.len() as i64 {
@@ -233,12 +235,12 @@ impl<'a> CallFlowPreloader<'a> {
233235 todo ! ( )
234236 }
235237
236- fn move_to_first_step ( & self , from_step_id : StepId , replay : & mut dyn Replay ) -> ( StepId , bool ) {
238+ fn move_to_first_step ( & self , from_step_id : StepId , replay : & mut dyn Replay ) -> Result < ( StepId , bool ) , Box < dyn Error > > {
237239 let ( step_id, progressing) = match self . mode {
238240 FlowMode :: Call => ( from_step_id, true ) ,
239241 FlowMode :: Diff => self . next_diff_flow_step ( StepId ( 0 ) , true , replay) ,
240242 } ;
241- replay. jump_to ( step_id) ;
243+ replay. jump_to ( step_id) ? ;
242244 Ok ( ( step_id, progressing) )
243245 }
244246
@@ -248,7 +250,8 @@ impl<'a> CallFlowPreloader<'a> {
248250 // let step_to_different_line = true; // for flow for now makes sense to try to always reach a new line
249251 // db.next_step_id_relative_to(from_step_id, true, step_to_different_line),
250252 replay. step ( Action :: Next , true ) . unwrap ( ) ; // TODO: handle error
251- let location = replay. load_location ( & mut self . flow_preloader . expr_loader ) . unwrap ( ) ; // TODO: handle error
253+ let mut expr_loader = ExprLoader :: new ( CoreTrace :: default ( ) ) ;
254+ let location = replay. load_location ( & mut expr_loader) . unwrap ( ) ; // TODO: handle error
252255 let new_step_id = StepId ( location. rr_ticks . 0 ) ;
253256 let progressing = new_step_id != from_step_id;
254257 ( new_step_id, progressing)
@@ -257,23 +260,23 @@ impl<'a> CallFlowPreloader<'a> {
257260 }
258261 }
259262
260- fn call_key_from ( & self , location : Location ) -> Result < CallKey , Box < dyn Error > > {
263+ fn call_key_from ( & self , location : & Location ) -> Result < CallKey , Box < dyn Error > > {
261264 Ok ( CallKey ( location. key . parse :: < i64 > ( ) ?) ) // for now still assume it's an integer
262265 }
263266
264267 fn load_view_update ( & mut self , replay : & mut dyn Replay ) -> Result < FlowViewUpdate , Box < dyn Error > > {
265268 // let start_step_id = StepId(self.location.rr_ticks.0);
266269 // db.calls[call_key].step_id;
267270 // let mut path_buf = &PathBuf::from(&self.location.path);
268- let mut iter_step_id = StepId ( self . location . rrTicks . 0 ) ;
271+ let mut iter_step_id = StepId ( self . location . rr_ticks . 0 ) ;
269272 let mut flow_view_update = FlowViewUpdate :: new ( self . location . clone ( ) ) ;
270273 let mut step_count = 0 ;
271274 let mut first = true ;
272- let last_call_key_result = self . call_key_from ( self . location ) ;
273- let mut last_call_key = CallKey ( 0 ) ;
274- match last_call_key_result {
275+ let tracked_call_key_result = self . call_key_from ( & self . location ) ;
276+ let tracked_call_key ; // = CallKey(0);
277+ match tracked_call_key_result {
275278 Ok ( call_key) => {
276- last_call_key = call_key;
279+ tracked_call_key = call_key;
277280 } ,
278281 Err ( e) => {
279282 error ! ( "call key parse error: {e:?}" ) ;
@@ -291,15 +294,15 @@ impl<'a> CallFlowPreloader<'a> {
291294 // };
292295 let ( step_id, progressing) = if first {
293296 first = false ;
294- self . move_to_first_step ( iter_step_id, replay) ;
297+ self . move_to_first_step ( iter_step_id, replay) ?
295298 } else {
296- self . move_to_next_step ( iter_step_id, replay) ;
299+ self . move_to_next_step ( iter_step_id, replay)
297300 } ;
298301
299302 iter_step_id = step_id;
300303 let mut expr_loader = ExprLoader :: new ( CoreTrace :: default ( ) ) ;
301- self . location = replay. load_location ( & mut expr_loader) ;
302- let new_call_key = match self . call_key_from ( self . location ) {
304+ self . location = replay. load_location ( & mut expr_loader) ? ;
305+ let new_call_key = match self . call_key_from ( & self . location ) {
303306 Ok ( call_key) => {
304307 call_key
305308 }
@@ -309,8 +312,20 @@ impl<'a> CallFlowPreloader<'a> {
309312 }
310313 } ;
311314
312- if self . mode == FlowMode :: Call && last_call_key != new_call_key || !progressing {
313- flow_view_update = self . add_return_value ( flow_view_update, replay, call_key) ;
315+ if self . mode == FlowMode :: Call && tracked_call_key != new_call_key || !progressing {
316+ replay. step ( Action :: StepIn , false ) ?; // hopefully go back to the end of our original function
317+ let return_location = replay. load_location ( & mut expr_loader) ?;
318+ let mut load_return_value = false ;
319+ // maybe this can be improved with a limited loop/jump to return/exit of call in the future
320+ if let Ok ( return_call_key) = self . call_key_from ( & return_location) {
321+ if return_call_key == tracked_call_key {
322+ flow_view_update = self . add_return_value ( flow_view_update, replay) ;
323+ load_return_value = true ;
324+ }
325+ }
326+ if !load_return_value {
327+ warn ! ( "we can't load return value" ) ;
328+ }
314329 info ! ( "break flow" ) ;
315330 break ;
316331 }
@@ -333,8 +348,8 @@ impl<'a> CallFlowPreloader<'a> {
333348 flow_view_update. add_step_count ( line, step_count) ;
334349 info ! ( "process loops" ) ;
335350 let path_buf = & PathBuf :: from ( & self . location . path ) ;
336- flow_view_update = self . process_loops ( flow_view_update. clone ( ) , Position ( self . location . line . 0 ) , replay. current_step_id ( ) , path_buf, step_count) ;
337- flow_view_update = self . log_expressions ( flow_view_update. clone ( ) , Position ( self . location . 0 ) , replay, step_id) ;
351+ flow_view_update = self . process_loops ( flow_view_update. clone ( ) , Position ( self . location . line ) , replay. current_step_id ( ) , path_buf, step_count) ;
352+ flow_view_update = self . log_expressions ( flow_view_update. clone ( ) , Position ( self . location . line ) , replay, step_id) ;
338353 step_count += 1 ;
339354 }
340355 let path_buf = & PathBuf :: from ( & self . location . path ) ;
@@ -346,7 +361,7 @@ impl<'a> CallFlowPreloader<'a> {
346361 . expr_loader
347362 . final_branch_load ( path_buf, & flow_view_update. branches_taken [ 0 ] [ 0 ] . table ) ,
348363 ) ;
349- flow_view_update
364+ Ok ( flow_view_update)
350365 }
351366
352367 #[ allow( clippy:: unwrap_used) ]
@@ -358,7 +373,7 @@ impl<'a> CallFlowPreloader<'a> {
358373 path_buf : & PathBuf ,
359374 step_count : i64 ,
360375 ) -> FlowViewUpdate {
361- if let Some ( loop_shape) = self . flow_preloader . expr_loader . get_loop_shape ( & line, path_buf) {
376+ if let Some ( loop_shape) = self . flow_preloader . expr_loader . get_loop_shape ( line, path_buf) {
362377 if loop_shape. first . 0 == line. 0 && !self . active_loops . contains ( & loop_shape. first ) {
363378 flow_view_update. loops . push ( Loop {
364379 base : LoopId ( loop_shape. loop_id . 0 ) ,
@@ -418,14 +433,14 @@ impl<'a> CallFlowPreloader<'a> {
418433 . insert ( line. 0 as usize , step_count as usize ) ;
419434 flow_view_update. add_branches (
420435 flow_view_update. loops . clone ( ) . last_mut ( ) . unwrap ( ) . base . 0 ,
421- self . flow_preloader . expr_loader . load_branch_for_position ( & line, path_buf) ,
436+ self . flow_preloader . expr_loader . load_branch_for_position ( line, path_buf) ,
422437 ) ;
423438 }
424439 } else {
425440 flow_view_update. loop_iteration_steps [ 0 ] [ 0 ]
426441 . table
427442 . insert ( line. 0 as usize , step_count as usize ) ;
428- flow_view_update. add_branches ( 0 , self . flow_preloader . expr_loader . load_branch_for_position ( & line, path_buf) ) ;
443+ flow_view_update. add_branches ( 0 , self . flow_preloader . expr_loader . load_branch_for_position ( line, path_buf) ) ;
429444 }
430445 flow_view_update
431446 }
@@ -479,12 +494,12 @@ impl<'a> CallFlowPreloader<'a> {
479494 // variable_map.insert(name.clone(), full_value_record);
480495 // }
481496
482- if let Some ( var_list) = self . flow_preloader . get_var_list ( line. 0 , & self . location ) {
497+ if let Some ( var_list) = self . flow_preloader . get_var_list ( line, & self . location ) {
483498 info ! ( "var_list {:?}" , var_list. clone( ) ) ;
484499 for value_name in & var_list {
485500 if let Ok ( value) = replay. load_value ( value_name) {
486501 // if variable_map.contains_key(value_name) {
487- let db = Db :: new ( PathBuf :: from ( "" ) ) ; // no state needed for to_ct_value
502+ let db = Db :: new ( & PathBuf :: from ( "" ) ) ; // no state needed for to_ct_value
488503 let ct_value = db. to_ct_value ( & value) ;
489504 flow_view_update
490505 . steps
@@ -507,7 +522,7 @@ impl<'a> CallFlowPreloader<'a> {
507522 if variable_map. contains_key ( variable) {
508523 flow_view_update. steps [ index]
509524 . after_values
510- . insert ( variable. clone ( ) , variable_map[ variable] ) ;
525+ . insert ( variable. clone ( ) , variable_map[ variable] . clone ( ) ) ;
511526 }
512527 }
513528 }
0 commit comments