From 160fe325606bb782050d62231bb3372431880b84 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 29 Dec 2022 10:42:00 -0300 Subject: [PATCH] Use InstructionLocation instead of Location in insruction_locations field of Program --- src/serde/deserialize_program.rs | 22 +++++++++------------- src/types/program.rs | 6 +++--- src/vm/errors/vm_exception.rs | 29 ++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/serde/deserialize_program.rs b/src/serde/deserialize_program.rs index becdbc7c45..dea2f9351c 100644 --- a/src/serde/deserialize_program.rs +++ b/src/serde/deserialize_program.rs @@ -100,10 +100,10 @@ pub struct DebugInfo { instruction_locations: HashMap, } -#[derive(Deserialize, Debug, PartialEq)] +#[derive(Deserialize, Debug, PartialEq, Eq, Clone)] pub struct InstructionLocation { - inst: Location, - hints: Vec, + pub inst: Location, + pub hints: Vec, } #[derive(Deserialize, Clone, Debug, PartialEq, Eq)] @@ -111,10 +111,10 @@ pub struct InputFile { pub filename: String, } -#[derive(Deserialize, Debug, PartialEq)] +#[derive(Deserialize, Debug, PartialEq, Eq, Clone)] pub struct HintLocation { - location: Location, - n_prefix_newlines: u32, + pub location: Location, + pub n_prefix_newlines: u32, } fn bigint_from_number<'de, D>(deserializer: D) -> Result, D::Error> @@ -368,13 +368,9 @@ pub fn deserialize_program( .into_iter() .filter(|attr| attr.name == "error_message") .collect(), - instruction_locations: program_json.debug_info.map(|debug_info| { - debug_info - .instruction_locations - .into_iter() - .map(|(offset, instruction_location)| (offset, instruction_location.inst)) - .collect() - }), + instruction_locations: program_json + .debug_info + .map(|debug_info| debug_info.instruction_locations), }) } diff --git a/src/types/program.rs b/src/types/program.rs index a349daacb7..5e94ebd84f 100644 --- a/src/types/program.rs +++ b/src/types/program.rs @@ -1,5 +1,5 @@ use crate::serde::deserialize_program::{ - deserialize_program, Attribute, HintParams, Identifier, Location, ReferenceManager, + deserialize_program, Attribute, HintParams, Identifier, InstructionLocation, ReferenceManager, }; use crate::types::errors::program_errors::ProgramError; use crate::types::relocatable::MaybeRelocatable; @@ -22,7 +22,7 @@ pub struct Program { pub reference_manager: ReferenceManager, pub identifiers: HashMap, pub error_message_attributes: Vec, - pub instruction_locations: Option>, + pub instruction_locations: Option>, } impl Program { @@ -36,7 +36,7 @@ impl Program { reference_manager: ReferenceManager, identifiers: HashMap, error_message_attributes: Vec, - instruction_locations: Option>, + instruction_locations: Option>, ) -> Result { Ok(Self { builtins, diff --git a/src/vm/errors/vm_exception.rs b/src/vm/errors/vm_exception.rs index a043b2e850..328a741aef 100644 --- a/src/vm/errors/vm_exception.rs +++ b/src/vm/errors/vm_exception.rs @@ -56,7 +56,7 @@ pub fn get_location(pc: usize, runner: &CairoRunner) -> Option { .instruction_locations .as_ref()? .get(&pc) - .cloned() + .map(|inst_location| inst_location.inst.clone()) } // Returns the traceback at the current pc. @@ -169,7 +169,7 @@ mod test { use std::path::Path; use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor; - use crate::serde::deserialize_program::{Attribute, InputFile}; + use crate::serde::deserialize_program::{Attribute, InputFile, InstructionLocation}; use crate::types::program::Program; use crate::types::relocatable::Relocatable; use crate::utils::test_utils::*; @@ -188,8 +188,13 @@ mod test { start_line: 1, start_col: 1, }; - let program = - program!(instruction_locations = Some(HashMap::from([(pc, location.clone())])),); + let instruction_location = InstructionLocation { + inst: location, + hints: vec![], + }; + let program = program!( + instruction_locations = Some(HashMap::from([(pc, instruction_location.clone())])), + ); let runner = cairo_runner!(program); let vm_excep = VmException { pc, @@ -408,8 +413,13 @@ mod test { start_line: 1, start_col: 1, }; - let program = - program!(instruction_locations = Some(HashMap::from([(2, location.clone())])),); + let instruction_location = InstructionLocation { + inst: location.clone(), + hints: vec![], + }; + let program = program!( + instruction_locations = Some(HashMap::from([(2, instruction_location.clone())])), + ); let runner = cairo_runner!(program); assert_eq!(get_location(2, &runner), Some(location)); } @@ -426,7 +436,12 @@ mod test { start_line: 1, start_col: 1, }; - let program = program!(instruction_locations = Some(HashMap::from([(2, location)])),); + let instruction_location = InstructionLocation { + inst: location, + hints: vec![], + }; + let program = + program!(instruction_locations = Some(HashMap::from([(2, instruction_location)])),); let runner = cairo_runner!(program); assert_eq!(get_location(3, &runner), None); }