def try_cast_gcref_to_instance(Class, gcref): # Before translation, unwraps the RPython instance contained in a _GcRef. # After translation, it is a type-check performed by the GC. if we_are_translated(): from pypy.rpython.annlowlevel import base_ptr_lltype from pypy.rpython.annlowlevel import cast_base_ptr_to_instance from pypy.rpython.lltypesystem import rclass if _is_rpy_instance(gcref): objptr = lltype.cast_opaque_ptr(base_ptr_lltype(), gcref) if objptr.typeptr: # may be NULL, e.g. in rdict's dummykeyobj clsptr = _get_llcls_from_cls(Class) if rclass.ll_isinstance(objptr, clsptr): return cast_base_ptr_to_instance(Class, objptr) return None else: if isinstance(gcref._x, Class): return gcref._x return None
def instanceOf(self, instbox, clsbox): adr = clsbox.getaddr() bounding_class = llmemory.cast_adr_to_ptr(adr, rclass.CLASSTYPE) real_instance = instbox.getref(rclass.OBJECTPTR) return rclass.ll_isinstance(real_instance, bounding_class)