@@ -8,6 +8,7 @@ use strum::IntoEnumIterator;
88use strum_macros:: EnumIter ;
99use crate :: parser:: CodeBlock ;
1010use crate :: pyarena:: PyArena ;
11+ use mopa:: { mopafy} ;
1112
1213#[ derive( Clone , Debug , EnumIter ) ]
1314pub enum PyMagicMethod {
@@ -146,7 +147,7 @@ impl PyMagicMethods {
146147 }
147148
148149 pub fn set_method ( & mut self , magic_method : PyMagicMethod , new_method : Rc < PyInternalFunction > ) {
149- let mut internal_func= magic_method. get_method_mut ( self ) ;
150+ let internal_func= magic_method. get_method_mut ( self ) ;
150151
151152 * internal_func = Some ( new_method) ;
152153 }
@@ -300,31 +301,45 @@ pub struct PyFunction {
300301}
301302
302303#[ derive( Debug ) ]
303- pub struct PyInstance {
304+ pub struct PyInstanceGeneric {
304305 class : Rc < PyClass > ,
305306 attributes : RwLock < HashMap < String , PyPointer < PyObject > > > ,
306- pub internal_storage : Vec < PyObject >
307+ // pub internal_storage: Vec<PyObject>
307308
308309}
309310
310- impl PyInstance {
311- pub fn new ( py_class : Rc < PyClass > ) -> Self {
312- PyInstance {
313- class : py_class ,
314- attributes : RwLock :: new ( HashMap :: new ( ) ) ,
315- internal_storage : vec ! [ ]
316- }
317- }
311+ pub trait PyInstance : mopa :: Any + Debug {
312+ // fn new(py_class: Rc<PyClass>) -> Self;
313+ fn set_field ( & mut self , key : String , value : PyPointer < PyObject > ) ;
314+ fn get_field ( & self , key : & str ) -> Option < PyPointer < PyObject > > ;
315+ fn get_class ( & self ) -> Rc < PyClass > ;
316+ }
317+ 318+ mopafy ! ( PyInstance ) ;
318319
319- pub fn set_field ( & mut self , key : String , value : PyPointer < PyObject > ) {
320- let mut attributes = self . attributes . get_mut ( ) . unwrap_or_else ( |e| panic ! ( "Failed to get mutable attributes: {:?}" , e) ) ;
320+ impl PyInstance for PyInstanceGeneric {
321+ fn set_field ( & mut self , key : String , value : PyPointer < PyObject > ) {
322+ let attributes = self . attributes . get_mut ( ) . unwrap_or_else ( |e| panic ! ( "Failed to get mutable attributes: {:?}" , e) ) ;
321323 let _old_value = attributes. insert ( key, value) ;
322324 }
323325
324- pub fn get_field ( & self , key : & str ) -> Option < PyPointer < PyObject > > {
326+ fn get_field ( & self , key : & str ) -> Option < PyPointer < PyObject > > {
325327 let attributes = self . attributes . read ( ) . ok ( ) ?;
326328 attributes. get ( key) . cloned ( )
327329 }
330+ 331+ fn get_class ( & self ) -> Rc < PyClass > {
332+ self . class . clone ( )
333+ }
334+ }
335+ 336+ impl PyInstanceGeneric {
337+ fn new ( py_class : Rc < PyClass > ) -> Self {
338+ PyInstanceGeneric {
339+ class : py_class,
340+ attributes : RwLock :: new ( HashMap :: new ( ) ) ,
341+ }
342+ }
328343}
329344
330345#[ derive( Debug ) ]
@@ -337,7 +352,7 @@ pub enum PyObject {
337352 // Dict(HashMap<String, PyObject>),
338353 Bool ( bool ) ,
339354 Class ( Rc < PyClass > ) ,
340- Instance ( PyInstance ) ,
355+ Instance ( Box < dyn PyInstance > ) ,
341356 Function ( PyFunction ) ,
342357 Exception ( PyException ) ,
343358 InternalSlot ( Rc < PyInternalFunction > ) ,
@@ -348,7 +363,7 @@ pub enum PyObject {
348363impl PyObject {
349364 pub fn get_class ( & self , arena : & mut PyArena ) -> Rc < PyClass > {
350365 match self {
351- PyObject :: Instance ( py_instance) => py_instance. class . clone ( ) ,
366+ PyObject :: Instance ( py_instance) => py_instance. get_class ( ) . clone ( ) ,
352367 PyObject :: Int ( _) => arena. globals . int_class . clone ( ) ,
353368 PyObject :: Float ( _) => { todo ! ( ) }
354369 PyObject :: Str ( _) => { todo ! ( ) }
@@ -379,7 +394,7 @@ impl PyObject {
379394 PyObject :: Bool ( _) => { todo ! ( ) }
380395 PyObject :: Class ( _) => { todo ! ( ) }
381396 PyObject :: Instance ( instance) => {
382- instance. class . clone ( ) . search_for_attribute ( py_magic_method)
397+ instance. get_class ( ) . clone ( ) . search_for_attribute ( py_magic_method)
383398 }
384399 PyObject :: Function ( _) => { todo ! ( ) }
385400 PyObject :: Exception ( _) => { todo ! ( ) }
@@ -439,14 +454,14 @@ impl PyObject {
439454 _ => panic ! ( "Expected internal slot" ) , // TODO make python error
440455 }
441456 }
442- pub fn expect_instance ( & self ) -> & PyInstance {
457+ pub fn expect_instance ( & self ) -> & Box < dyn PyInstance > {
443458 match self {
444459 PyObject :: Instance ( instance) => instance,
445460 _ => panic ! ( "Expected internal slot" ) , // TODO make python error
446461 }
447462 }
448463
449- pub fn expect_instance_mut ( & mut self ) -> & mut PyInstance {
464+ pub fn expect_instance_mut ( & mut self ) -> & mut Box < dyn PyInstance > {
450465 match self {
451466 PyObject :: Instance ( instance) => instance,
452467 _ => panic ! ( "Expected internal slot" ) , // TODO make python error
0 commit comments