Skip to content

Commit

Permalink
feat: add Program method for iterating identifiers (#1079)
Browse files Browse the repository at this point in the history
* Add `Program::iter_identifiers`

* Add unit test

* Update changelog

* Change &String -> &str
  • Loading branch information
MegaRedHand committed Apr 27, 2023
1 parent 45a43aa commit 1dc3fe4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
segments.write_arg(ids.blake2s_ptr_end, padding)
%}

* Add `Program::iter_identifiers(&self) -> Iterator<Item = (&str, &Identifier)>` to get an iterator over the program's identifiers [#1079](https://github.com/lambdaclass/cairo-rs/pull/1079)

* Implement hint on `assert_le_felt` for versions 0.6.0 and 0.8.2 [#1047](https://github.com/lambdaclass/cairo-rs/pull/1047):

`BuiltinHintProcessor` now supports the following hints:
Expand Down
71 changes: 71 additions & 0 deletions src/types/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ impl Program {
pub fn get_identifier(&self, id: &str) -> Option<&Identifier> {
self.shared_program_data.identifiers.get(id)
}

pub fn iter_identifiers(&self) -> impl Iterator<Item = (&str, &Identifier)> {
self.shared_program_data
.identifiers
.iter()
.map(|(cairo_type, identifier)| (cairo_type.as_str(), identifier))
}
}

impl Default for Program {
Expand Down Expand Up @@ -429,6 +436,70 @@ mod tests {
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn iter_identifiers() {
let reference_manager = ReferenceManager {
references: Vec::new(),
};

let builtins: Vec<BuiltinName> = Vec::new();

let data: Vec<MaybeRelocatable> = vec![
mayberelocatable!(5189976364521848832),
mayberelocatable!(1000),
mayberelocatable!(5189976364521848832),
mayberelocatable!(2000),
mayberelocatable!(5201798304953696256),
mayberelocatable!(2345108766317314046),
];

let mut identifiers: HashMap<String, Identifier> = HashMap::new();

identifiers.insert(
String::from("__main__.main"),
Identifier {
pc: Some(0),
type_: Some(String::from("function")),
value: None,
full_name: None,
members: None,
cairo_type: None,
},
);

identifiers.insert(
String::from("__main__.main.SIZEOF_LOCALS"),
Identifier {
pc: None,
type_: Some(String::from("const")),
value: Some(Felt252::zero()),
full_name: None,
members: None,
cairo_type: None,
},
);

let program = Program::new(
builtins,
data,
None,
HashMap::new(),
reference_manager,
identifiers.clone(),
Vec::new(),
None,
)
.unwrap();

let collected_identifiers: HashMap<_, _> = program
.iter_identifiers()
.map(|(cairo_type, identifier)| (cairo_type.to_string(), identifier.clone()))
.collect();

assert_eq!(collected_identifiers, identifiers);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn new_program_with_invalid_identifiers() {
Expand Down

0 comments on commit 1dc3fe4

Please sign in to comment.