@@ -475,8 +475,9 @@ Object doNativeWrapper(PythonNativeWrapper object) {
475475 Object doNativeObject (TruffleObject object ,
476476 @ Cached PythonObjectFactory factory ,
477477 @ SuppressWarnings ("unused" ) @ Cached ("create()" ) GetLazyClassNode getClassNode ,
478- @ SuppressWarnings ("unused" ) @ Cached ("create()" ) IsBuiltinClassProfile isForeignClassProfile ) {
479- return factory .createNativeObjectWrapper (object );
478+ @ SuppressWarnings ("unused" ) @ Cached ("create()" ) IsBuiltinClassProfile isForeignClassProfile ,
479+ @ CachedContext (PythonLanguage .class ) PythonContext context ) {
480+ return factory .createNativeObjectWrapper (object , context );
480481 }
481482
482483 @ Specialization
@@ -543,13 +544,13 @@ public static Object doSlowPath(Object object, boolean forceNativeClass) {
543544 return ((PythonNativeWrapper ) object ).getDelegate ();
544545 } else if (IsBuiltinClassProfile .profileClassSlowPath (GetClassNode .getUncached ().execute (object ), PythonBuiltinClassType .TruffleObject )) {
545546 if (forceNativeClass ) {
546- return PythonLanguage . getCore (). factory ().createNativeClassWrapper ((TruffleObject ) object );
547+ return PythonObjectFactory . getUncached ().createNativeClassWrapper ((TruffleObject ) object );
547548 }
548- return PythonLanguage . getCore (). factory ().createNativeObjectWrapper ((TruffleObject ) object );
549+ return PythonObjectFactory . getUncached ().createNativeObjectWrapper ((TruffleObject ) object );
549550 } else if (object instanceof String || object instanceof Number || object instanceof Boolean || object instanceof PythonNativeNull || object instanceof PythonAbstractObject ) {
550551 return object ;
551552 }
552- throw PythonLanguage . getCore ().raise (PythonErrorType .SystemError , "invalid object from native: %s" , object );
553+ throw PRaiseNode . getUncached ().raise (PythonErrorType .SystemError , "invalid object from native: %s" , object );
553554 }
554555
555556 // TODO(fa): Workaround for DSL bug: did not import factory at users
@@ -1673,11 +1674,14 @@ public abstract static class GetTypeMemberNode extends CExtBaseNode {
16731674 * native context, so we can be sure that the "nativeClassStableAssumption" (which is
16741675 * per-context) is from the context in which this native object was created.
16751676 */
1676- @ Specialization (guards = {"cachedObj.equals(obj)" , "memberName == cachedMemberName" }, limit = "1" , assumptions = "getNativeClassStableAssumption(cachedObj)" )
1677- public Object doCachedObj (@ SuppressWarnings ("unused" ) PythonNativeClass obj , @ SuppressWarnings ("unused" ) String memberName ,
1677+ @ Specialization (guards = {"isSameNativeObjectNode.execute(cachedObj, obj)" , "memberName == cachedMemberName" }, //
1678+ limit = "1" , //
1679+ assumptions = {"getNativeClassStableAssumption(cachedObj)" , "singleContextAssumption()" })
1680+ public Object doCachedObj (@ SuppressWarnings ("unused" ) PythonAbstractNativeObject obj , @ SuppressWarnings ("unused" ) String memberName ,
1681+ @ SuppressWarnings ("unused" ) @ Cached IsSameNativeObjectFastNode isSameNativeObjectNode ,
16781682 @ SuppressWarnings ("unused" ) @ Cached ("memberName" ) String cachedMemberName ,
16791683 @ SuppressWarnings ("unused" ) @ Cached ("getterFuncName(memberName)" ) String getterFuncName ,
1680- @ Cached ("obj" ) @ SuppressWarnings ("unused" ) PythonNativeClass cachedObj ,
1684+ @ Cached ("obj" ) @ SuppressWarnings ("unused" ) PythonAbstractNativeObject cachedObj ,
16811685 @ Cached ("doSlowPath(obj, getterFuncName)" ) Object result ) {
16821686 return result ;
16831687 }
@@ -1757,4 +1761,39 @@ static Object getNativeNullWithoutModule(@SuppressWarnings("unused") Object modu
17571761 }
17581762
17591763 }
1764+
1765+ public abstract static class IsSameNativeObjectNode extends CExtBaseNode {
1766+
1767+ public abstract boolean execute (PythonAbstractNativeObject left , PythonAbstractNativeObject right );
1768+
1769+ protected static boolean doNativeFast (PythonAbstractNativeObject left , PythonAbstractNativeObject right ) {
1770+ // This check is a bit dangerous since we cannot be sure about the code that is running.
1771+ // Currently, we assume that the pointer object is a Sulong pointer and for this it's
1772+ // fine.
1773+ return left .equals (right );
1774+ }
1775+
1776+ }
1777+
1778+ @ GenerateUncached
1779+ public abstract static class IsSameNativeObjectFastNode extends IsSameNativeObjectNode {
1780+
1781+ @ Specialization
1782+ boolean doSingleContext (PythonAbstractNativeObject left , PythonAbstractNativeObject right ) {
1783+ return IsSameNativeObjectNode .doNativeFast (left , right );
1784+ }
1785+ }
1786+
1787+ @ GenerateUncached
1788+ public abstract static class IsSameNativeObjectSlowNode extends IsSameNativeObjectNode {
1789+
1790+ @ Specialization
1791+ boolean doSingleContext (PythonAbstractNativeObject left , PythonAbstractNativeObject right ,
1792+ @ Cached PointerCompareNode pointerCompareNode ) {
1793+ if (IsSameNativeObjectNode .doNativeFast (left , right )) {
1794+ return true ;
1795+ }
1796+ return pointerCompareNode .execute (SpecialMethodNames .__EQ__ , left , right );
1797+ }
1798+ }
17601799}
0 commit comments