43
43
import static com .oracle .graal .python .builtins .objects .cext .NativeCAPISymbols .FUN_GET_OB_TYPE ;
44
44
import static com .oracle .graal .python .builtins .objects .cext .NativeCAPISymbols .FUN_PY_OBJECT_GENERIC_GET_DICT ;
45
45
46
+ import java .lang .ref .WeakReference ;
46
47
import java .util .Objects ;
47
48
48
49
import com .oracle .graal .python .PythonLanguage ;
74
75
import com .oracle .truffle .api .dsl .Cached ;
75
76
import com .oracle .truffle .api .dsl .Cached .Exclusive ;
76
77
import com .oracle .truffle .api .dsl .Cached .Shared ;
78
+ import com .oracle .truffle .api .dsl .Fallback ;
77
79
import com .oracle .truffle .api .dsl .GenerateUncached ;
78
80
import com .oracle .truffle .api .dsl .Specialization ;
79
81
import com .oracle .truffle .api .interop .ArityException ;
89
91
import com .oracle .truffle .api .object .Shape ;
90
92
import com .oracle .truffle .api .profiles .ConditionProfile ;
91
93
import com .oracle .truffle .api .profiles .ValueProfile ;
92
- import com .oracle .truffle .api . utilities . TriState ;
94
+ import com .oracle .truffle .llvm . spi . ReferenceLibrary ;
93
95
94
96
@ ExportLibrary (PythonObjectLibrary .class )
97
+ @ ExportLibrary (ReferenceLibrary .class )
95
98
@ ExportLibrary (InteropLibrary .class )
96
99
public final class PythonAbstractNativeObject extends PythonAbstractObject implements PythonNativeObject , PythonNativeClass {
97
100
98
- public final TruffleObject ptr ;
101
+ public final TruffleObject object ;
99
102
100
- public PythonAbstractNativeObject (TruffleObject ptr ) {
101
- this .ptr = ptr ;
103
+ public PythonAbstractNativeObject (TruffleObject object ) {
104
+ this .object = object ;
102
105
}
103
106
104
107
public int compareTo (Object o ) {
@@ -118,14 +121,14 @@ public void lookupChanged() {
118
121
}
119
122
120
123
public TruffleObject getPtr () {
121
- return ptr ;
124
+ return object ;
122
125
}
123
126
124
127
@ Override
125
128
public int hashCode () {
126
129
CompilerAsserts .neverPartOfCompilation ();
127
130
// this is important for the default '__hash__' implementation
128
- return Objects .hashCode (ptr );
131
+ return Objects .hashCode (object );
129
132
}
130
133
131
134
@ Ignore
@@ -138,7 +141,7 @@ public boolean equals(Object obj) {
138
141
return false ;
139
142
}
140
143
PythonAbstractNativeObject other = (PythonAbstractNativeObject ) obj ;
141
- return Objects .equals (ptr , other .ptr );
144
+ return Objects .equals (object , other .object );
142
145
}
143
146
144
147
public boolean equalsProfiled (Object obj , ValueProfile profile ) {
@@ -149,13 +152,13 @@ public boolean equalsProfiled(Object obj, ValueProfile profile) {
149
152
return false ;
150
153
}
151
154
PythonAbstractNativeObject other = (PythonAbstractNativeObject ) obj ;
152
- return Objects .equals (profile .profile (ptr ), profile .profile (other .ptr ));
155
+ return Objects .equals (profile .profile (object ), profile .profile (other .object ));
153
156
}
154
157
155
158
@ Override
156
159
public String toString () {
157
160
CompilerAsserts .neverPartOfCompilation ();
158
- return String .format ("PythonAbstractNativeObject(%s)" , ptr );
161
+ return String .format ("PythonAbstractNativeObject(%s)" , object );
159
162
}
160
163
161
164
@ ExportMessage
@@ -225,11 +228,10 @@ public static Assumption getSingleContextAssumption() {
225
228
return PythonLanguage .getCurrent ().singleContextAssumption ;
226
229
}
227
230
228
- @ Specialization (guards = "object == cachedObject" , limit = "1" , assumptions = "singleContextAssumption" )
231
+ @ Specialization (guards = "object == cachedObject.get() " , limit = "1" , assumptions = "singleContextAssumption" )
229
232
static Object getNativeClassCachedIdentity (PythonAbstractNativeObject object ,
230
- // @Shared("assumption")
231
- @ Cached (value = "getSingleContextAssumption()" ) Assumption singleContextAssumption ,
232
- @ Exclusive @ Cached (value = "object" , weak = true ) PythonAbstractNativeObject cachedObject ,
233
+ @ Shared ("assumption" ) @ Cached (value = "getSingleContextAssumption()" ) Assumption singleContextAssumption ,
234
+ @ Exclusive @ Cached ("weak(object)" ) WeakReference <PythonAbstractNativeObject > cachedObject ,
233
235
@ Exclusive @ Cached ("getNativeClassUncached(object)" ) Object cachedClass ) {
234
236
// TODO: (tfel) is this really something we can do? It's so rare for this class to
235
237
// change that it shouldn't be worth the effort, but in native code, anything can
@@ -238,18 +240,17 @@ static Object getNativeClassCachedIdentity(PythonAbstractNativeObject object,
238
240
return cachedClass ;
239
241
}
240
242
241
- @ Specialization (guards = "lib.isIdentical( cachedObject, object, lib)" , assumptions = "getSingleContextAssumption() " )
243
+ @ Specialization (guards = "isSame(referenceLibrary, cachedObject, object)" , limit = "1" , assumptions = "singleContextAssumption " )
242
244
static Object getNativeClassCached (PythonAbstractNativeObject object ,
243
- @ Exclusive @ Cached ("object" ) PythonAbstractNativeObject cachedObject ,
245
+ @ Shared ("assumption" ) @ Cached (value = "getSingleContextAssumption()" ) Assumption singleContextAssumption ,
246
+ @ Exclusive @ Cached ("weak(object)" ) WeakReference <PythonAbstractNativeObject > cachedObject ,
244
247
@ Exclusive @ Cached ("getNativeClassUncached(object)" ) Object cachedClass ,
245
- @ CachedLibrary (limit = "3 " ) @ SuppressWarnings ("unused" ) InteropLibrary lib ) {
248
+ @ CachedLibrary ("object.object " ) @ SuppressWarnings ("unused" ) ReferenceLibrary referenceLibrary ) {
246
249
// TODO same as for 'getNativeClassCachedIdentity'
247
250
return cachedClass ;
248
251
}
249
252
250
- @ Specialization (guards = {"lib.hasMembers(object.getPtr())" }, // replaces = {// "getNativeClassCached",
251
- // "getNativeClassCachedIdentity"},
252
- limit = "1" , rewriteOn = {UnknownIdentifierException .class ,
253
+ @ Specialization (guards = {"lib.hasMembers(object.getPtr())" }, replaces = {"getNativeClassCached" , "getNativeClassCachedIdentity" }, limit = "1" , rewriteOn = {UnknownIdentifierException .class ,
253
254
UnsupportedMessageException .class })
254
255
static Object getNativeClassByMember (PythonAbstractNativeObject object ,
255
256
@ CachedLibrary ("object.getPtr()" ) InteropLibrary lib ,
@@ -260,8 +261,7 @@ static Object getNativeClassByMember(PythonAbstractNativeObject object,
260
261
return classProfile .profile (toJavaNode .execute (lib .readMember (object .getPtr (), NativeMember .OB_TYPE .getMemberName ())));
261
262
}
262
263
263
- // @Specialization(replaces = {"getNativeClassCached", "getNativeClassCachedIdentity", "getNativeClassByMember"})
264
- @ Specialization (replaces = {"getNativeClassByMember" , "getNativeClassCached" })
264
+ @ Specialization (replaces = {"getNativeClassCached" , "getNativeClassCachedIdentity" , "getNativeClassByMember" })
265
265
static Object getNativeClass (PythonAbstractNativeObject object ,
266
266
@ Exclusive @ Cached PCallCapiFunction callGetObTypeNode ,
267
267
@ Exclusive @ Cached AsPythonObjectNode toJavaNode ,
@@ -270,9 +270,14 @@ static Object getNativeClass(PythonAbstractNativeObject object,
270
270
return classProfile .profile (toJavaNode .execute (callGetObTypeNode .call (FUN_GET_OB_TYPE , object .getPtr ())));
271
271
}
272
272
273
- static boolean isSame (InteropLibrary lib , PythonAbstractNativeObject cachedObject , PythonAbstractNativeObject object ) {
273
+ static WeakReference <PythonAbstractNativeObject > weak (PythonAbstractNativeObject object ) {
274
+ return new WeakReference <>(object );
275
+ }
276
+
277
+ static boolean isSame (ReferenceLibrary referenceLibrary , WeakReference <PythonAbstractNativeObject > cachedObjectRef , PythonAbstractNativeObject object ) {
278
+ PythonAbstractNativeObject cachedObject = cachedObjectRef .get ();
274
279
if (cachedObject != null ) {
275
- return lib . isIdentical (cachedObject , object , lib );
280
+ return referenceLibrary . isSame (cachedObject . object , object . object );
276
281
}
277
282
return false ;
278
283
}
@@ -284,14 +289,19 @@ public static Object getNativeClassUncached(PythonAbstractNativeObject object) {
284
289
}
285
290
286
291
@ ExportMessage
287
- int identityHashCode (@ CachedLibrary ("this.ptr" ) InteropLibrary lib ) throws UnsupportedMessageException {
288
- return lib .identityHashCode (ptr );
289
- }
292
+ static class IsSame {
290
293
291
- @ ExportMessage
292
- TriState isIdenticalOrUndefined (Object other ,
293
- @ CachedLibrary (limit = "3" ) InteropLibrary lib ) {
294
- return lib .isIdentical (ptr , other , lib ) ? TriState .TRUE : TriState .FALSE ;
294
+ @ Specialization
295
+ static boolean doNativeObject (PythonAbstractNativeObject receiver , PythonAbstractNativeObject other ,
296
+ @ CachedLibrary ("receiver.object" ) ReferenceLibrary referenceLibrary ) {
297
+ return referenceLibrary .isSame (receiver .object , other .object );
298
+ }
299
+
300
+ @ Fallback
301
+ @ SuppressWarnings ("unused" )
302
+ static boolean doOther (PythonAbstractNativeObject receiver , Object other ) {
303
+ return false ;
304
+ }
295
305
}
296
306
297
307
@ ExportMessage (library = PythonObjectLibrary .class , name = "isLazyPythonClass" )
0 commit comments