Skip to content

Commit

Permalink
Use InstructionLocation instead of Location in insruction_locations f…
Browse files Browse the repository at this point in the history
…ield of Program
  • Loading branch information
fmoletta committed Dec 29, 2022
1 parent c7b5cfe commit 160fe32
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
22 changes: 9 additions & 13 deletions src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,21 @@ pub struct DebugInfo {
instruction_locations: HashMap<usize, InstructionLocation>,
}

#[derive(Deserialize, Debug, PartialEq)]
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct InstructionLocation {
inst: Location,
hints: Vec<HintLocation>,
pub inst: Location,
pub hints: Vec<HintLocation>,
}

#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
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<Option<BigInt>, D::Error>
Expand Down Expand Up @@ -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),
})
}

Expand Down
6 changes: 3 additions & 3 deletions src/types/program.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,7 +22,7 @@ pub struct Program {
pub reference_manager: ReferenceManager,
pub identifiers: HashMap<String, Identifier>,
pub error_message_attributes: Vec<Attribute>,
pub instruction_locations: Option<HashMap<usize, Location>>,
pub instruction_locations: Option<HashMap<usize, InstructionLocation>>,
}

impl Program {
Expand All @@ -36,7 +36,7 @@ impl Program {
reference_manager: ReferenceManager,
identifiers: HashMap<String, Identifier>,
error_message_attributes: Vec<Attribute>,
instruction_locations: Option<HashMap<usize, Location>>,
instruction_locations: Option<HashMap<usize, InstructionLocation>>,
) -> Result<Program, ProgramError> {
Ok(Self {
builtins,
Expand Down
29 changes: 22 additions & 7 deletions src/vm/errors/vm_exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn get_location(pc: usize, runner: &CairoRunner) -> Option<Location> {
.instruction_locations
.as_ref()?
.get(&pc)
.cloned()
.map(|inst_location| inst_location.inst.clone())
}

// Returns the traceback at the current pc.
Expand Down Expand Up @@ -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::*;
Expand All @@ -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,
Expand Down Expand Up @@ -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));
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 160fe32

Please sign in to comment.