Skip to content

Commit ca928c7

Browse files
committed
Use interop library to check foreign objects
1 parent 39515d4 commit ca928c7

File tree

12 files changed

+115
-72
lines changed

12 files changed

+115
-72
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/JavaModuleBuiltins.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6565
import com.oracle.truffle.api.dsl.NodeFactory;
6666
import com.oracle.truffle.api.dsl.Specialization;
67+
import com.oracle.truffle.api.interop.InteropLibrary;
6768
import com.oracle.truffle.api.interop.TruffleObject;
69+
import com.oracle.truffle.api.library.CachedLibrary;
6870

6971
@CoreFunctions(defineModule = "java")
7072
public class JavaModuleBuiltins extends PythonBuiltins {
@@ -173,8 +175,10 @@ boolean check(Object object) {
173175
@Builtin(name = "instanceof", minNumOfPositionalArgs = 2)
174176
@GenerateNodeFactory
175177
abstract static class InstanceOfNode extends PythonBinaryBuiltinNode {
176-
@Specialization(guards = {"!isForeignObject(object)", "isForeignTruffleObject(klass)"})
177-
boolean check(Object object, TruffleObject klass) {
178+
@Specialization(guards = {"!isForeignObject(object, iLibObject)", "isForeignObject(klass, iLibKlass)"}, limit = "3")
179+
boolean check(Object object, TruffleObject klass,
180+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary iLibObject,
181+
@SuppressWarnings("unused") @CachedLibrary("klass") InteropLibrary iLibKlass) {
178182
Env env = getContext().getEnv();
179183
try {
180184
Object hostKlass = env.asHostObject(klass);
@@ -187,8 +191,10 @@ boolean check(Object object, TruffleObject klass) {
187191
return false;
188192
}
189193

190-
@Specialization(guards = {"isForeignObject(object)", "isForeignTruffleObject(klass)"})
191-
boolean checkForeign(Object object, TruffleObject klass) {
194+
@Specialization(guards = {"isForeignObject(object, iLibObject)", "isForeignObject(klass, iLibKlass)"}, limit = "3")
195+
boolean checkForeign(Object object, TruffleObject klass,
196+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary iLibObject,
197+
@SuppressWarnings("unused") @CachedLibrary("klass") InteropLibrary iLibKlass) {
192198
Env env = getContext().getEnv();
193199
try {
194200
Object hostObject = env.asHostObject(object);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,6 @@ public final boolean isIterable(@Shared("thisObject") @Cached GetLazyClassNode g
714714

715715
@ExportMessage
716716
public final boolean isCallable(@Exclusive @Cached LookupInheritedAttributeNode.Dynamic callAttrGetterNode) {
717-
assert !PGuards.isCallable(this) || PGuards.isClass(this);
718717
Object call = callAttrGetterNode.execute(this, __CALL__);
719718
return PGuards.isCallable(call);
720719
}

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

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -446,23 +446,26 @@ static Object doPythonClassUncached(@SuppressWarnings("unused") CExtContext cext
446446
return PythonClassNativeWrapper.wrap(object, getNameNode.execute(object));
447447
}
448448

449-
@Specialization(guards = {"cachedClass == object.getClass()", "!isClass(object)", "!isNativeObject(object)", "!isSpecialSingleton(object)"}, limit = "3")
449+
@Specialization(guards = {"cachedClass == object.getClass()", "!isClass(object, lib)", "!isNativeObject(object)", "!isSpecialSingleton(object)"}, limit = "3")
450450
static Object runAbstractObjectCached(@SuppressWarnings("unused") CExtContext cextContext, PythonAbstractObject object,
451451
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile,
452-
@Cached("object.getClass()") Class<? extends PythonAbstractObject> cachedClass) {
452+
@Cached("object.getClass()") Class<? extends PythonAbstractObject> cachedClass,
453+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary lib) {
453454
assert object != PNone.NO_VALUE;
454455
return PythonObjectNativeWrapper.wrap(CompilerDirectives.castExact(object, cachedClass), noWrapperProfile);
455456
}
456457

457-
@Specialization(guards = {"!isClass(object)", "!isNativeObject(object)", "!isSpecialSingleton(object)"}, replaces = "runAbstractObjectCached")
458+
@Specialization(guards = {"!isClass(object, lib)", "!isNativeObject(object)", "!isSpecialSingleton(object)"}, replaces = "runAbstractObjectCached", limit = "3")
458459
static Object runAbstractObject(@SuppressWarnings("unused") CExtContext cextContext, PythonAbstractObject object,
459-
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile) {
460+
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile,
461+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary lib) {
460462
assert object != PNone.NO_VALUE;
461463
return PythonObjectNativeWrapper.wrap(object, noWrapperProfile);
462464
}
463465

464-
@Specialization(guards = {"isForeignTruffleObject(object)", "!isNativeWrapper(object)", "!isNativeNull(object)"})
465-
static Object doForeignObject(@SuppressWarnings("unused") CExtContext cextContext, TruffleObject object) {
466+
@Specialization(guards = {"isForeignObject(object, lib)", "!isNativeWrapper(object)", "!isNativeNull(object)"}, limit = "3")
467+
static Object doForeignObject(@SuppressWarnings("unused") CExtContext cextContext, TruffleObject object,
468+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary lib) {
466469
return TruffleObjectNativeWrapper.wrap(object);
467470
}
468471

@@ -681,25 +684,28 @@ static Object doPythonClassUncached(@SuppressWarnings("unused") CExtContext cext
681684
return PythonClassNativeWrapper.wrapNewRef(object, getNameNode.execute(object));
682685
}
683686

684-
@Specialization(guards = {"cachedClass == object.getClass()", "!isClass(object)", "!isNativeObject(object)", "!isSpecialSingleton(object)"}, limit = "3")
687+
@Specialization(guards = {"cachedClass == object.getClass()", "!isClass(object, lib)", "!isNativeObject(object)", "!isSpecialSingleton(object)"}, limit = "3")
685688
static Object runAbstractObjectCached(@SuppressWarnings("unused") CExtContext cextContext, PythonAbstractObject object,
686689
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile,
687-
@Cached("object.getClass()") Class<? extends PythonAbstractObject> cachedClass) {
690+
@Cached("object.getClass()") Class<? extends PythonAbstractObject> cachedClass,
691+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary lib) {
688692
assert object != PNone.NO_VALUE;
689693
return PythonObjectNativeWrapper.wrapNewRef(CompilerDirectives.castExact(object, cachedClass), noWrapperProfile);
690694
}
691695

692-
@Specialization(guards = {"!isClass(object)", "!isNativeObject(object)", "!isSpecialSingleton(object)"}, replaces = "runAbstractObjectCached")
696+
@Specialization(guards = {"!isClass(object, lib)", "!isNativeObject(object)", "!isSpecialSingleton(object)"}, replaces = "runAbstractObjectCached", limit = "3")
693697
static Object runAbstractObject(@SuppressWarnings("unused") CExtContext cextContext, PythonAbstractObject object,
694-
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile) {
698+
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile,
699+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary lib) {
695700
assert object != PNone.NO_VALUE;
696701
return PythonObjectNativeWrapper.wrapNewRef(object, noWrapperProfile);
697702
}
698703

699-
@Specialization(guards = {"isForeignTruffleObject(object)", "!isNativeWrapper(object)", "!isNativeNull(object)"})
700-
static Object doForeignObject(CExtContext cextContext, TruffleObject object) {
704+
@Specialization(guards = {"isForeignObject(object, lib)", "!isNativeWrapper(object)", "!isNativeNull(object)"}, limit = "3")
705+
static Object doForeignObject(CExtContext cextContext, TruffleObject object,
706+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary lib) {
701707
// this will always be a new wrapper; it's implicitly always a new reference in any case
702-
return ToSulongNode.doForeignObject(cextContext, object);
708+
return ToSulongNode.doForeignObject(cextContext, object, lib);
703709
}
704710

705711
@Specialization(guards = "isFallback(object, lib)", limit = "1")
@@ -837,25 +843,28 @@ static Object doPythonClassUncached(@SuppressWarnings("unused") CExtContext cext
837843
return PythonClassNativeWrapper.wrapNewRef(object, getNameNode.execute(object));
838844
}
839845

840-
@Specialization(guards = {"cachedClass == object.getClass()", "!isClass(object)", "!isNativeObject(object)", "!isSpecialSingleton(object)"}, limit = "3")
846+
@Specialization(guards = {"cachedClass == object.getClass()", "!isClass(object, lib)", "!isNativeObject(object)", "!isSpecialSingleton(object)"}, limit = "3")
841847
static Object runAbstractObjectCached(@SuppressWarnings("unused") CExtContext cextContext, PythonAbstractObject object,
842848
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile,
843-
@Cached("object.getClass()") Class<? extends PythonAbstractObject> cachedClass) {
849+
@Cached("object.getClass()") Class<? extends PythonAbstractObject> cachedClass,
850+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary lib) {
844851
assert object != PNone.NO_VALUE;
845852
return PythonObjectNativeWrapper.wrapNewRef(CompilerDirectives.castExact(object, cachedClass), noWrapperProfile);
846853
}
847854

848-
@Specialization(guards = {"!isClass(object)", "!isNativeObject(object)", "!isSpecialSingleton(object)"}, replaces = "runAbstractObjectCached")
855+
@Specialization(guards = {"!isClass(object, lib)", "!isNativeObject(object)", "!isSpecialSingleton(object)"}, replaces = "runAbstractObjectCached", limit = "3")
849856
static Object runAbstractObject(@SuppressWarnings("unused") CExtContext cextContext, PythonAbstractObject object,
850-
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile) {
857+
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile,
858+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary lib) {
851859
assert object != PNone.NO_VALUE;
852860
return PythonObjectNativeWrapper.wrapNewRef(object, noWrapperProfile);
853861
}
854862

855-
@Specialization(guards = {"isForeignTruffleObject(object)", "!isNativeWrapper(object)", "!isNativeNull(object)"})
856-
static Object doForeignObject(CExtContext cextContext, TruffleObject object) {
863+
@Specialization(guards = {"isForeignObject(object, lib)", "!isNativeWrapper(object)", "!isNativeNull(object)"}, limit = "3")
864+
static Object doForeignObject(CExtContext cextContext, TruffleObject object,
865+
@SuppressWarnings("unused") @CachedLibrary("object") InteropLibrary lib) {
857866
// this will always be a new wrapper; it's implicitly always a new reference in any case
858-
return ToSulongNode.doForeignObject(cextContext, object);
867+
return ToSulongNode.doForeignObject(cextContext, object, lib);
859868
}
860869

861870
@Specialization(guards = "isFallback(object, lib)", limit = "1")

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@ static ParserState doObject(ParserState stateIn, Object kwds, @SuppressWarnings(
869869
@Cached ExecuteConverterNode executeConverterNode,
870870
@Cached GetLazyClassNode getClassNode,
871871
@Cached IsSubtypeNode isSubtypeNode,
872+
@CachedLibrary(limit = "2") InteropLibrary lib,
872873
@Cached(value = "createTJ(stateIn)", uncached = "getUncachedTJ(stateIn)") CExtToJavaNode typeToJavaNode,
873874
@Cached(value = "createTN(stateIn)", uncached = "getUncachedTN(stateIn)") CExtToNativeNode toNativeNode,
874875
@Shared("writeOutVarNode") @Cached WriteOutVarNode writeOutVarNode,
@@ -880,7 +881,7 @@ static ParserState doObject(ParserState stateIn, Object kwds, @SuppressWarnings(
880881
if (!skipOptionalArg(arg, state.restOptional)) {
881882
Object typeObject = typeToJavaNode.execute(getVaArgNode.execute(varargs, state.outIndex));
882883
state = state.incrementOutIndex();
883-
assert PGuards.isClass(typeObject);
884+
assert PGuards.isClass(typeObject, lib);
884885
if (!isSubtypeNode.execute(getClassNode.execute(arg), (LazyPythonClass) typeObject)) {
885886
raiseNode.raiseIntWithoutFrame(0, TypeError, "expected object of type %s, got %p", typeObject, arg);
886887
throw ParseArgumentsException.raise();

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
import com.oracle.truffle.api.dsl.NodeFactory;
101101
import com.oracle.truffle.api.dsl.Specialization;
102102
import com.oracle.truffle.api.frame.VirtualFrame;
103+
import com.oracle.truffle.api.interop.InteropLibrary;
103104
import com.oracle.truffle.api.interop.UnsupportedMessageException;
104105
import com.oracle.truffle.api.library.CachedLibrary;
105106
import com.oracle.truffle.api.nodes.UnexpectedResultException;
@@ -511,9 +512,10 @@ protected boolean isBuiltinObjectExact(PythonObject self) {
511512
return exactBuiltinInstanceProfile.profileIsOtherBuiltinObject(self, PythonBuiltinClassType.PythonModule);
512513
}
513514

514-
@Specialization(guards = {"!isBuiltinObjectExact(self)", "!isClass(self)", "!isExactObjectInstance(self)", "isNoValue(none)"}, limit = "1")
515+
@Specialization(guards = {"!isBuiltinObjectExact(self)", "!isClass(self, iLib)", "!isExactObjectInstance(self)", "isNoValue(none)"}, limit = "1")
515516
Object dict(PythonObject self, @SuppressWarnings("unused") PNone none,
516-
@CachedLibrary("self") PythonObjectLibrary lib) {
517+
@CachedLibrary("self") PythonObjectLibrary lib,
518+
@SuppressWarnings("unused") @CachedLibrary("self") InteropLibrary iLib) {
517519
PHashingCollection dict = lib.getDict(self);
518520
if (dict == null) {
519521
dict = factory().createDictFixedStorage(self);
@@ -527,9 +529,10 @@ Object dict(PythonObject self, @SuppressWarnings("unused") PNone none,
527529
return dict;
528530
}
529531

530-
@Specialization(guards = {"!isBuiltinObjectExact(self)", "!isClass(self)", "!isExactObjectInstance(self)"}, limit = "1")
532+
@Specialization(guards = {"!isBuiltinObjectExact(self)", "!isClass(self, iLib)", "!isExactObjectInstance(self)"}, limit = "1")
531533
Object dict(PythonObject self, PDict dict,
532-
@CachedLibrary("self") PythonObjectLibrary lib) {
534+
@CachedLibrary("self") PythonObjectLibrary lib,
535+
@SuppressWarnings("unused") @CachedLibrary("self") InteropLibrary iLib) {
533536
try {
534537
lib.setDict(self, dict);
535538
} catch (UnsupportedMessageException e) {

0 commit comments

Comments
 (0)