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