@@ -54,19 +54,49 @@ pub(crate) struct Formatter {
5454unsafe impl Send for Formatter { }
5555unsafe impl Sync for Formatter { }
5656
57+ struct SymbolResolverAdapter {
58+ resolver : PyObject ,
59+ }
60+
61+ impl SymbolResolverAdapter {
62+ fn new ( resolver : PyObject ) -> SymbolResolverAdapter {
63+ SymbolResolverAdapter { resolver }
64+ }
65+ }
66+
67+ impl iced_x86:: SymbolResolver for SymbolResolverAdapter {
68+ fn symbol (
69+ & mut self , _instruction : & iced_x86:: Instruction , _operand : u32 , _instruction_operand : Option < u32 > , address : u64 , _address_size : u32 ,
70+ ) -> Option < iced_x86:: SymbolResult < ' _ > > {
71+ let instr = Instruction { instr : * _instruction } ;
72+ Python :: with_gil ( |py| {
73+ let sym: Option < String > =
74+ self . resolver . call1 ( py, ( instr, _operand, _instruction_operand, address, _address_size) ) . unwrap ( ) . extract ( py) . unwrap ( ) ;
75+
76+ if let Some ( val) = sym {
77+ Some ( iced_x86:: SymbolResult :: with_string ( address, val) )
78+ } else {
79+ None
80+ }
81+ } )
82+ }
83+ }
84+
5785#[ pymethods]
5886impl Formatter {
5987 #[ new]
60- #[ pyo3( text_signature = "(syntax)" ) ]
61- fn new ( syntax : u32 ) -> PyResult < Self > {
88+ #[ pyo3( signature = ( syntax, symbol_resolver = None ) ) ]
89+ fn new ( syntax : u32 , symbol_resolver : Option < PyObject > ) -> PyResult < Self > {
90+ let resolver = symbol_resolver. map ( |val| -> Box < dyn iced_x86:: SymbolResolver > { Box :: new ( SymbolResolverAdapter :: new ( val) ) } ) ;
91+
6292 let formatter: Box < dyn iced_x86:: Formatter > = if syntax == FormatterSyntax :: Gas as u32 {
63- Box :: new ( iced_x86:: GasFormatter :: new ( ) )
93+ Box :: new ( iced_x86:: GasFormatter :: with_options ( resolver , None ) )
6494 } else if syntax == FormatterSyntax :: Intel as u32 {
65- Box :: new ( iced_x86:: IntelFormatter :: new ( ) )
95+ Box :: new ( iced_x86:: IntelFormatter :: with_options ( resolver , None ) )
6696 } else if syntax == FormatterSyntax :: Masm as u32 {
67- Box :: new ( iced_x86:: MasmFormatter :: new ( ) )
97+ Box :: new ( iced_x86:: MasmFormatter :: with_options ( resolver , None ) )
6898 } else if syntax == FormatterSyntax :: Nasm as u32 {
69- Box :: new ( iced_x86:: NasmFormatter :: new ( ) )
99+ Box :: new ( iced_x86:: NasmFormatter :: with_options ( resolver , None ) )
70100 } else {
71101 return Err ( PyValueError :: new_err ( "Invalid formatter syntax" ) ) ;
72102 } ;
0 commit comments