@@ -534,6 +534,12 @@ impl PyCairoRunner {
534534 . collect :: < Vec < _ > > ( )
535535 . to_object ( py) )
536536 }
537+
538+ /// Add (or replace if already present) a custom hash builtin.
539+ pub fn add_additional_hash_builtin ( & self ) {
540+ let mut vm = ( * self . pyvm . vm ) . borrow_mut ( ) ;
541+ self . inner . add_additional_hash_builtin ( & mut vm) ;
542+ }
537543}
538544
539545#[ pyclass]
@@ -1198,6 +1204,10 @@ mod test {
11981204 segment_index: 6 ,
11991205 offset: 0 ,
12001206 } ) ] ,
1207+ vec![ RelocatableValue ( PyRelocatable {
1208+ segment_index: 7 ,
1209+ offset: 0 ,
1210+ } ) ] ,
12011211 ] ;
12021212
12031213 Python :: with_gil ( |py| {
@@ -1491,4 +1501,37 @@ mod test {
14911501 ) ;
14921502 } ) ;
14931503 }
1504+
1505+ /// Test that add_additional_hash_builtin() returns successfully.
1506+ #[ test]
1507+ fn add_additional_hash_builtin ( ) {
1508+ Python :: with_gil ( |_| {
1509+ let program = fs:: read_to_string ( "cairo_programs/fibonacci.json" ) . unwrap ( ) ;
1510+ let runner = PyCairoRunner :: new (
1511+ program,
1512+ Some ( "main" . to_string ( ) ) ,
1513+ Some ( "small" . to_string ( ) ) ,
1514+ false ,
1515+ )
1516+ . unwrap ( ) ;
1517+
1518+ runner. add_additional_hash_builtin ( ) ;
1519+ assert_eq ! (
1520+ ( * runner. pyvm. vm)
1521+ . borrow( )
1522+ . get_builtin_runners( )
1523+ . last( )
1524+ . map( |( key, _) | key. as_str( ) ) ,
1525+ Some ( "hash_builtin" ) ,
1526+ ) ;
1527+
1528+ let mut vm = ( * runner. pyvm . vm ) . borrow_mut ( ) ;
1529+ // Check that the segment exists by writing to it.
1530+ vm. insert_value (
1531+ & Relocatable :: from ( ( 0 , 0 ) ) ,
1532+ MaybeRelocatable :: Int ( bigint ! ( 42 ) ) ,
1533+ )
1534+ . expect ( "memory insert failed" ) ;
1535+ } ) ;
1536+ }
14941537}
0 commit comments