Skip to content

Commit 55fd727

Browse files
committed
Remove reflection check
1 parent 111de38 commit 55fd727

File tree

5 files changed

+39
-61
lines changed

5 files changed

+39
-61
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/CExtNodes.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,9 @@ static Object doForeignObject(@SuppressWarnings("unused") CExtContext cextContex
476476
static Object run(@SuppressWarnings("unused") CExtContext cextContext, Object object,
477477
@SuppressWarnings("unused") @CachedLibrary("object") PythonObjectLibrary lib) {
478478
assert object != null : "Java 'null' cannot be a Sulong value";
479-
assert CApiGuards.isNativeWrapper(object) : "unknown object cannot be a Sulong value";
480-
return object;
479+
Object o = lib.getDelegatedValue(object);
480+
assert CApiGuards.isNativeWrapper(o) : "unknown object cannot be a Sulong value";
481+
return o;
481482
}
482483

483484
protected static PythonClassNativeWrapper wrapNativeClass(PythonManagedClass object) {
@@ -487,7 +488,7 @@ protected static PythonClassNativeWrapper wrapNativeClass(PythonManagedClass obj
487488
static boolean isFallback(Object object, PythonObjectLibrary lib) {
488489
return !(object instanceof String || object instanceof Boolean || object instanceof Integer || object instanceof Long || object instanceof Double ||
489490
object instanceof PythonNativeNull || object == DescriptorDeleteMarker.INSTANCE || object instanceof PythonAbstractObject) &&
490-
!lib.isReflectedObject(object, object) && !(lib.isForeignObject(object) && !CApiGuards.isNativeWrapper(object));
491+
!(lib.isForeignObject(object) && !CApiGuards.isNativeWrapper(object));
491492
}
492493

493494
protected static boolean isNaN(double d) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/PythonObjectLibrary.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,19 +1078,13 @@ public boolean isForeignObject(Object receiver) {
10781078
}
10791079

10801080
/**
1081-
* When a {@code receiver} is a wrapped primitive object that utilizes a #ReflectionLibrary, the
1082-
* value will appear here as primitive contrary to the value in the call cite which should
1083-
* represent the {@code receiverOrigin}
1081+
* When a {@code receiver} is a wrapped primitive object that utilizes a #ReflectionLibrary,
1082+
* this message will return the delegated value of the receiver.
10841083
*
10851084
* @param receiver the receiver Object
1086-
* @param receiverOrigin also the receiver Object
1087-
* @return True if there has been a reflection
1085+
* @return the delegated value of the receiver
10881086
*/
1089-
public boolean isReflectedObject(Object receiver, Object receiverOrigin) {
1090-
return receiver != receiverOrigin;
1091-
}
1092-
1093-
public Object getReflectedObject(Object receiver) {
1087+
public Object getDelegatedValue(Object receiver) {
10941088
return receiver;
10951089
}
10961090

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/LookupAndCallBinaryNode.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -351,29 +351,25 @@ boolean callBoolean(VirtualFrame frame, double left, double right,
351351
}
352352
}
353353

354-
protected static boolean isReflectedObject(Object left, Object right, PythonObjectLibrary libLeft, PythonObjectLibrary libRight) {
355-
return libLeft.isReflectedObject(left, left) || libRight.isReflectedObject(right, right);
356-
}
357-
358354
// Object, Object
359355

360-
@Specialization(guards = {"!isReversible()", "!isReflectedObject(left, right, libLeft, libRight)"}, limit = "2")
356+
@Specialization(guards = {"!isReversible()"}, limit = "2")
361357
Object callObject(VirtualFrame frame, Object left, Object right,
362-
@SuppressWarnings("unused") @CachedLibrary("left") PythonObjectLibrary libLeft,
363-
@SuppressWarnings("unused") @CachedLibrary("right") PythonObjectLibrary libRight,
358+
@CachedLibrary("left") PythonObjectLibrary libLeft,
364359
@Cached("create(name, ignoreDescriptorException)") LookupSpecialMethodNode getattr) {
365-
Object leftCallable = getattr.execute(frame, libLeft.getLazyPythonClass(left), left);
360+
Object leftValue = libLeft.getDelegatedValue(left);
361+
Object leftCallable = getattr.execute(frame, libLeft.getLazyPythonClass(leftValue), leftValue);
366362
if (leftCallable == PNone.NO_VALUE) {
367363
if (handlerFactory != null) {
368-
return runErrorHandler(left, right);
364+
return runErrorHandler(leftValue, right);
369365
} else {
370366
return PNotImplemented.NOT_IMPLEMENTED;
371367
}
372368
}
373-
return ensureDispatch().executeObject(frame, leftCallable, left, right);
369+
return ensureDispatch().executeObject(frame, leftCallable, leftValue, right);
374370
}
375371

376-
@Specialization(guards = {"isReversible()", "!isReflectedObject(left, right, libLeft, libRight)"}, limit = "2")
372+
@Specialization(guards = {"isReversible()"}, limit = "4")
377373
Object callObject(VirtualFrame frame, Object left, Object right,
378374
@Cached("create(name, ignoreDescriptorException)") LookupSpecialMethodNode getattr,
379375
@Cached("create(rname, ignoreDescriptorException)") LookupSpecialMethodNode getattrR,
@@ -394,42 +390,36 @@ Object callObject(VirtualFrame frame, Object left, Object right,
394390

395391
Object result = PNotImplemented.NOT_IMPLEMENTED;
396392
Object leftClass = libLeft.getLazyPythonClass(left);
397-
Object leftCallable = getattr.execute(frame, leftClass, left);
393+
Object leftValue = libLeft.getDelegatedValue(left);
394+
Object leftCallable = getattr.execute(frame, leftClass, leftValue);
398395
Object rightClass = libRight.getLazyPythonClass(right);
399-
Object rightCallable = getattrR.execute(frame, rightClass, right);
396+
Object rightValue = libRight.getDelegatedValue(right);
397+
Object rightCallable = getattrR.execute(frame, rightClass, rightValue);
400398
if (!alwaysCheckReverse && leftCallable == rightCallable) {
401399
rightCallable = PNone.NO_VALUE;
402400
}
403401
if (leftCallable != PNone.NO_VALUE) {
404402
if (rightCallable != PNone.NO_VALUE && !isSameTypeNode.execute(leftClass, rightClass) && isSubtype.execute(frame, rightClass, leftClass)) {
405-
result = ensureReverseDispatch().executeObject(frame, rightCallable, right, left);
403+
result = ensureReverseDispatch().executeObject(frame, rightCallable, rightValue, leftValue);
406404
if (result != PNotImplemented.NOT_IMPLEMENTED) {
407405
return result;
408406
}
409407
rightCallable = PNone.NO_VALUE;
410408
}
411-
result = ensureDispatch().executeObject(frame, leftCallable, left, right);
409+
result = ensureDispatch().executeObject(frame, leftCallable, leftValue, rightValue);
412410
if (result != PNotImplemented.NOT_IMPLEMENTED) {
413411
return result;
414412
}
415413
}
416414
if (notImplementedBranch.profile(rightCallable != PNone.NO_VALUE)) {
417-
result = ensureReverseDispatch().executeObject(frame, rightCallable, right, left);
415+
result = ensureReverseDispatch().executeObject(frame, rightCallable, rightValue, leftValue);
418416
}
419417
if (handlerFactory != null && result == PNotImplemented.NOT_IMPLEMENTED) {
420-
return runErrorHandler(left, right);
418+
return runErrorHandler(leftValue, rightValue);
421419
}
422420
return result;
423421
}
424422

425-
@Specialization(guards = "isReflectedObject(left, right, libLeft, libRight)", limit = "1")
426-
Object callReflected(VirtualFrame frame, Object left, Object right,
427-
@CachedLibrary("left") PythonObjectLibrary libLeft,
428-
@CachedLibrary("right") PythonObjectLibrary libRight,
429-
@Cached("create(name, rname, handlerFactory)") LookupAndCallBinaryNode recursiveCall) {
430-
return recursiveCall.executeObject(frame, libLeft.getReflectedObject(left), libRight.getReflectedObject(right));
431-
}
432-
433423
private Object runErrorHandler(Object left, Object right) {
434424
if (handler == null) {
435425
CompilerDirectives.transferToInterpreterAndInvalidate();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/GetIteratorExpressionNode.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import com.oracle.graal.python.runtime.exception.PException;
6464
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6565
import com.oracle.truffle.api.dsl.Cached;
66-
import com.oracle.truffle.api.dsl.Cached.Shared;
6766
import com.oracle.truffle.api.dsl.GenerateUncached;
6867
import com.oracle.truffle.api.dsl.ImportStatic;
6968
import com.oracle.truffle.api.dsl.Specialization;
@@ -96,14 +95,14 @@ static PythonObject doPZip(PZip value) {
9695
return GetIteratorNode.doPZip(value);
9796
}
9897

99-
@Specialization(guards = {"!isNoValue(value)"})
98+
@Specialization(guards = {"!isNoValue(value)"}, limit = "4")
10099
static Object doGeneric(Object value,
101100
@Cached("createIdentityProfile()") ValueProfile getattributeProfile,
102-
@CachedLibrary(limit = "4") PythonObjectLibrary plib,
101+
@CachedLibrary("value") PythonObjectLibrary plib,
103102
@CachedLibrary(limit = "3") PythonObjectLibrary methodLib,
104103
@Cached IsIteratorObjectNode isIteratorObjectNode,
105104
@Cached PythonObjectFactory factory,
106-
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
105+
@Cached.Shared("raiseNode") @Cached PRaiseNode raiseNode) {
107106
// NOTE: it's fine to pass 'null' frame since the caller must already take care of the
108107
// global state
109108
return GetIteratorNode.doGeneric(null, value, getattributeProfile, plib, methodLib, isIteratorObjectNode, factory,
@@ -112,7 +111,7 @@ static Object doGeneric(Object value,
112111

113112
@Specialization
114113
static PythonObject doNone(PNone none,
115-
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
114+
@Cached.Shared("raiseNode") @Cached PRaiseNode raiseNode) {
116115
return GetIteratorNode.doNone(none, raiseNode);
117116
}
118117

@@ -134,18 +133,19 @@ static PythonObject doPZip(PZip value) {
134133
return value;
135134
}
136135

137-
@Specialization(guards = {"!isNoValue(value)", "!plib.isReflectedObject(value, value)"})
136+
@Specialization(guards = {"!isNoValue(value)"}, limit = "4")
138137
static Object doGeneric(VirtualFrame frame, Object value,
139138
@Cached("createIdentityProfile()") ValueProfile iterMethodProfile,
140-
@CachedLibrary(limit = "4") PythonObjectLibrary plib,
139+
@CachedLibrary("value") PythonObjectLibrary plib,
141140
@CachedLibrary(limit = "2") PythonObjectLibrary methodLib,
142141
@Cached IsIteratorObjectNode isIteratorObjectNode,
143142
@Cached PythonObjectFactory factory,
144-
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
143+
@Cached.Shared("raiseNode") @Cached PRaiseNode raiseNode) {
144+
Object v = plib.getDelegatedValue(value);
145145
Object iterMethod = iterMethodProfile.profile(plib.lookupAttributeOnType(value, __ITER__));
146146
if (iterMethod != PNone.NONE) {
147147
if (iterMethod != PNone.NO_VALUE) {
148-
Object iterObj = methodLib.callUnboundMethodIgnoreGetException(iterMethod, frame, value);
148+
Object iterObj = methodLib.callUnboundMethodIgnoreGetException(iterMethod, frame, v);
149149
if (iterObj != PNone.NO_VALUE && isIteratorObjectNode.execute(iterObj)) {
150150
return iterObj;
151151
} else {
@@ -154,22 +154,15 @@ static Object doGeneric(VirtualFrame frame, Object value,
154154
}
155155
Object getItemAttrObj = plib.lookupAttributeOnType(value, __GETITEM__);
156156
if (getItemAttrObj != PNone.NO_VALUE) {
157-
return factory.createSequenceIterator(value);
157+
return factory.createSequenceIterator(v);
158158
}
159159
}
160-
throw notIterable(raiseNode, value);
161-
}
162-
163-
@Specialization(guards = {"!isNoValue(value)", "plib.isReflectedObject(value, value)"})
164-
static Object doReflected(VirtualFrame frame, Object value,
165-
@CachedLibrary(limit = "4") PythonObjectLibrary plib,
166-
@Cached GetIteratorNode recursive) {
167-
return recursive.executeWith(frame, plib.getReflectedObject(value));
160+
throw notIterable(raiseNode, v);
168161
}
169162

170163
@Specialization
171164
static PythonObject doNone(PNone none,
172-
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
165+
@Cached PRaiseNode raiseNode) {
173166
throw notIterable(raiseNode, none);
174167
}
175168

@@ -193,13 +186,13 @@ abstract static class IsIteratorObjectNode extends Node {
193186
public abstract boolean execute(Object o);
194187

195188
@Specialization
196-
boolean doPIterator(@SuppressWarnings("unused") PBuiltinIterator it) {
189+
static boolean doPIterator(@SuppressWarnings("unused") PBuiltinIterator it) {
197190
// a PIterator object is guaranteed to be an iterator object
198191
return true;
199192
}
200193

201194
@Specialization
202-
boolean doGeneric(Object it,
195+
static boolean doGeneric(Object it,
203196
@Cached LookupInheritedAttributeNode.Dynamic lookupAttributeNode) {
204197
return lookupAttributeNode.execute(it, SpecialMethodNames.__NEXT__) != PNone.NO_VALUE;
205198
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/IsExpressionNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,13 @@ static boolean doGeneric(Object left, Object right,
310310
if (left == right) {
311311
return true;
312312
}
313-
if (lib.isForeignObject(left) || lib.isReflectedObject(left, left)) {
313+
if (lib.isForeignObject(left)) {
314314
// If left is foreign, this will check its identity via the interop message. If left
315315
// is an object that is a wrapped Python object and uses a ReflectionLibrary, it
316316
// will not appear foreign, but the isSame call will unpack it from its wrapper and
317317
// may lead straight back to this node, but this time with the unwrapped Python
318318
// object that will no longer satisfy the isReflectedObject condition.
319-
return lib.isSame(left, right);
319+
return lib.isSame(lib.getDelegatedValue(left), right);
320320
}
321321
return false;
322322
}

0 commit comments

Comments
 (0)