Skip to content

Commit

Permalink
feat: add missing 'cairo_type' for Identifier (#773)
Browse files Browse the repository at this point in the history
* feat: add missing 'cairo_type' for Identifier

In cairo compiled program, funtion returned values types is stored in `cairo_type` key.

* test: update `deserialize_program_with_type_definition` test to use locally compiled file instead of in source JSON data.
  • Loading branch information
ptisserand authored Jan 26, 2023
1 parent 7c05e6b commit 79e4987
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct Identifier {

pub full_name: Option<String>,
pub members: Option<HashMap<String, Member>>,
pub cairo_type: Option<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
Expand Down Expand Up @@ -852,6 +853,7 @@ mod tests {
value: None,
full_name: None,
members: None,
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -864,6 +866,7 @@ mod tests {
)),
full_name: None,
members: None,
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -874,6 +877,7 @@ mod tests {
value: None,
full_name: None,
members: None,
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -886,6 +890,7 @@ mod tests {
)),
full_name: None,
members: None,
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -896,6 +901,7 @@ mod tests {
value: Some(Felt::new(3)),
full_name: None,
members: None,
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -906,6 +912,7 @@ mod tests {
value: Some(Felt::zero()),
full_name: None,
members: None,
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -916,6 +923,7 @@ mod tests {
value: Some(felt_str!("340282366920938463463374607431768211456")),
full_name: None,
members: None,
cairo_type: None,
},
);

Expand Down Expand Up @@ -1268,4 +1276,34 @@ mod tests {

assert_eq!(program_json.debug_info, Some(debug_info));
}

#[test]
fn deserialize_program_with_type_definition() {
let file = File::open("cairo_programs/uint256_integration_tests.json").unwrap();
let reader = BufReader::new(file);

let program_json: ProgramJson = serde_json::from_reader(reader).unwrap();

assert_eq!(
program_json.identifiers["starkware.cairo.common.alloc.alloc.Return"]
.cairo_type
.as_ref()
.expect("key not found"),
"(ptr: felt*)"
);
assert_eq!(
program_json.identifiers["starkware.cairo.common.uint256.uint256_add.Return"]
.cairo_type
.as_ref()
.expect("key not found"),
"(res: starkware.cairo.common.uint256.Uint256, carry: felt)"
);
assert_eq!(
program_json.identifiers["__main__.test_unsigned_div_rem.Return"]
.cairo_type
.as_ref()
.expect("key not found"),
"()"
);
}
}
14 changes: 14 additions & 0 deletions src/types/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ mod tests {
value: None,
full_name: None,
members: None,
cairo_type: None,
},
);

Expand All @@ -189,6 +190,7 @@ mod tests {
value: Some(Felt::zero()),
full_name: None,
members: None,
cairo_type: None,
},
);

Expand Down Expand Up @@ -245,6 +247,7 @@ mod tests {
value: None,
full_name: None,
members: None,
cairo_type: None,
},
);

Expand All @@ -256,6 +259,7 @@ mod tests {
value: None,
full_name: None,
members: None,
cairo_type: None,
},
);

Expand Down Expand Up @@ -302,6 +306,7 @@ mod tests {
value: None,
full_name: None,
members: None,
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -312,6 +317,7 @@ mod tests {
value: None,
full_name: Some("__main__.main.Args".to_string()),
members: Some(HashMap::new()),
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -322,6 +328,7 @@ mod tests {
value: None,
full_name: Some("__main__.main.ImplicitArgs".to_string()),
members: Some(HashMap::new()),
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -332,6 +339,7 @@ mod tests {
value: None,
full_name: Some("__main__.main.Return".to_string()),
members: Some(HashMap::new()),
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -342,6 +350,7 @@ mod tests {
value: Some(Felt::zero()),
full_name: None,
members: None,
cairo_type: None,
},
);

Expand Down Expand Up @@ -396,6 +405,7 @@ mod tests {
value: None,
full_name: None,
members: None,
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -406,6 +416,7 @@ mod tests {
value: None,
full_name: Some("__main__.main.Args".to_string()),
members: Some(HashMap::new()),
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -416,6 +427,7 @@ mod tests {
value: None,
full_name: Some("__main__.main.ImplicitArgs".to_string()),
members: Some(HashMap::new()),
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -426,6 +438,7 @@ mod tests {
value: None,
full_name: Some("__main__.main.Return".to_string()),
members: Some(HashMap::new()),
cairo_type: None,
},
);
identifiers.insert(
Expand All @@ -436,6 +449,7 @@ mod tests {
value: Some(Felt::zero()),
full_name: None,
members: None,
cairo_type: None,
},
);

Expand Down
4 changes: 4 additions & 0 deletions src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3929,6 +3929,7 @@ mod tests {
value: None,
full_name: None,
members: None,
cairo_type: None,
},
)]
.into_iter()
Expand All @@ -3955,6 +3956,7 @@ mod tests {
value: None,
full_name: None,
members: None,
cairo_type: None,
},
),
(
Expand All @@ -3965,6 +3967,7 @@ mod tests {
value: None,
full_name: None,
members: None,
cairo_type: None,
},
),
]
Expand Down Expand Up @@ -3992,6 +3995,7 @@ mod tests {
value: None,
full_name: None,
members: None,
cairo_type: None,
},
)]
.into_iter()
Expand Down

0 comments on commit 79e4987

Please sign in to comment.