Skip to content

Commit 9e81cb1

Browse files
committed
Add typeCheck to the library
1 parent 259b4f5 commit 9e81cb1

File tree

9 files changed

+100
-45
lines changed

9 files changed

+100
-45
lines changed

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
import com.oracle.graal.python.builtins.objects.method.PMethod;
6666
import com.oracle.graal.python.builtins.objects.module.PythonModule;
6767
import com.oracle.graal.python.builtins.objects.object.PythonObject;
68-
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
68+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
6969
import com.oracle.graal.python.nodes.ErrorMessages;
7070
import com.oracle.graal.python.nodes.argument.ReadIndexedArgumentNode;
7171
import com.oracle.graal.python.nodes.argument.ReadVarArgsNode;
@@ -75,7 +75,6 @@
7575
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
7676
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
7777
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
78-
import com.oracle.graal.python.nodes.object.GetClassNode;
7978
import com.oracle.graal.python.nodes.subscript.GetItemNode;
8079
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
8180
import com.oracle.graal.python.nodes.util.CannotCastException;
@@ -104,6 +103,7 @@
104103
import com.oracle.truffle.api.dsl.Specialization;
105104
import com.oracle.truffle.api.dsl.TypeSystemReference;
106105
import com.oracle.truffle.api.frame.VirtualFrame;
106+
import com.oracle.truffle.api.library.CachedLibrary;
107107
import com.oracle.truffle.api.nodes.LanguageInfo;
108108
import com.oracle.truffle.api.nodes.Node;
109109
import com.oracle.truffle.api.nodes.NodeUtil;
@@ -460,28 +460,14 @@ protected Object getToolPath(String tool) {
460460
}
461461
}
462462

463-
// Equivalent of PyType_IsSubtype
464-
@Builtin(name = "is_subtype", minNumOfPositionalArgs = 2)
465-
@GenerateNodeFactory
466-
public abstract static class IsSubtypeNode extends PythonBinaryBuiltinNode {
467-
@Specialization
468-
public boolean isSubtype(VirtualFrame frame, Object derived, Object cls,
469-
@Cached com.oracle.graal.python.nodes.classes.IsSubtypeNode isSubtypeNode) {
470-
return isSubtypeNode.execute(frame, derived, cls);
471-
}
472-
}
473-
474463
// Equivalent of PyObject_TypeCheck
475464
@Builtin(name = "type_check", minNumOfPositionalArgs = 2)
476465
@GenerateNodeFactory
477466
public abstract static class TypeCheckNode extends PythonBinaryBuiltinNode {
478467
@Specialization(limit = "3")
479-
boolean typeCheck(VirtualFrame frame, Object instance, Object cls,
480-
@Cached GetClassNode getClassNode,
481-
@Cached TypeNodes.IsSameTypeNode isSameTypeNode,
482-
@Cached com.oracle.graal.python.nodes.classes.IsSubtypeNode isSubtypeNode) {
483-
Object instanceClass = getClassNode.execute(instance);
484-
return isSameTypeNode.execute(instanceClass, cls) || isSubtypeNode.execute(frame, instanceClass, cls);
468+
boolean typeCheck(Object instance, Object cls,
469+
@CachedLibrary("instance") PythonObjectLibrary lib) {
470+
return lib.typeCheck(instance, cls);
485471
}
486472
}
487473
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ public Object asIndexWithState(ThreadState state,
822822
@Shared("methodLib") @CachedLibrary(limit = "2") PythonObjectLibrary methodLib,
823823
@CachedLibrary(limit = "5") PythonObjectLibrary resultLib,
824824
@Shared("raise") @Cached PRaiseNode raise,
825-
@Exclusive @Cached IsSubtypeNode isSubtype,
825+
@Shared("isSubtypeNode") @Cached IsSubtypeNode isSubtype,
826826
@Exclusive @Cached ConditionProfile noIndex,
827827
@Exclusive @Cached ConditionProfile resultProfile) {
828828
// n.b.: the CPython shortcut "if (PyLong_Check(item)) return item;" is
@@ -863,7 +863,7 @@ public String asPathWithState(ThreadState state,
863863
public Object asPStringWithState(ThreadState state,
864864
@CachedLibrary("this") PythonObjectLibrary lib,
865865
@CachedLibrary(limit = "1") PythonObjectLibrary resultLib,
866-
@Exclusive @Cached IsSubtypeNode isSubtypeNode,
866+
@Shared("isSubtypeNode") @Cached IsSubtypeNode isSubtypeNode,
867867
@Shared("raise") @Cached PRaiseNode raise) {
868868
return asPString(lib, this, state, isSubtypeNode, resultLib, raise);
869869
}
@@ -1160,6 +1160,15 @@ public long asJavaLongWithState(ThreadState state,
11601160
}
11611161
}
11621162

1163+
@ExportMessage
1164+
public boolean typeCheck(Object type,
1165+
@CachedLibrary("this") PythonObjectLibrary lib,
1166+
@Cached TypeNodes.IsSameTypeNode isSameTypeNode,
1167+
@Shared("isSubtypeNode") @Cached IsSubtypeNode isSubtypeNode) {
1168+
Object instanceClass = lib.getLazyPythonClass(this);
1169+
return isSameTypeNode.execute(instanceClass, type) || isSubtypeNode.execute(instanceClass, type);
1170+
}
1171+
11631172
@ExportMessage
11641173
public final boolean isContextManager(@CachedLibrary("this") PythonObjectLibrary lib,
11651174
@Exclusive @Cached ConditionProfile profile) {

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import com.oracle.graal.python.builtins.objects.function.PArguments;
4848
import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
4949
import com.oracle.graal.python.builtins.objects.ints.PInt;
50+
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
51+
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
5052
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
5153
import com.oracle.graal.python.runtime.PythonContext;
5254
import com.oracle.graal.python.runtime.PythonOptions;
@@ -286,7 +288,7 @@ static int asPInt(Boolean receiver) {
286288
}
287289

288290
@ExportMessage
289-
public static Object lookupAttributeInternal(Boolean receiver, ThreadState state, String name, boolean strict,
291+
static Object lookupAttributeInternal(Boolean receiver, ThreadState state, String name, boolean strict,
290292
@Cached ConditionProfile gotState,
291293
@Exclusive @Cached PythonAbstractObject.LookupAttributeNode lookup) {
292294
VirtualFrame frame = null;
@@ -297,24 +299,32 @@ public static Object lookupAttributeInternal(Boolean receiver, ThreadState state
297299
}
298300

299301
@ExportMessage
300-
public static Object lookupAttributeOnTypeInternal(@SuppressWarnings("unused") Boolean receiver, String name, boolean strict,
302+
static Object lookupAttributeOnTypeInternal(@SuppressWarnings("unused") Boolean receiver, String name, boolean strict,
301303
@Exclusive @Cached PythonAbstractObject.LookupAttributeOnTypeNode lookup) {
302304
return lookup.execute(PythonBuiltinClassType.Boolean, name, strict);
303305
}
304306

305307
@ExportMessage
306-
public static Object lookupAndCallSpecialMethodWithState(Boolean receiver, ThreadState state, String methodName, Object[] arguments,
308+
static Object lookupAndCallSpecialMethodWithState(Boolean receiver, ThreadState state, String methodName, Object[] arguments,
307309
@CachedLibrary("receiver") PythonObjectLibrary plib,
308310
@Shared("methodLib") @CachedLibrary(limit = "2") PythonObjectLibrary methodLib) {
309311
Object method = plib.lookupAttributeOnTypeStrict(receiver, methodName);
310312
return methodLib.callUnboundMethodWithState(method, state, receiver, arguments);
311313
}
312314

313315
@ExportMessage
314-
public static Object lookupAndCallRegularMethodWithState(Boolean receiver, ThreadState state, String methodName, Object[] arguments,
316+
static Object lookupAndCallRegularMethodWithState(Boolean receiver, ThreadState state, String methodName, Object[] arguments,
315317
@CachedLibrary("receiver") PythonObjectLibrary plib,
316318
@Shared("methodLib") @CachedLibrary(limit = "2") PythonObjectLibrary methodLib) {
317319
Object method = plib.lookupAttributeStrictWithState(receiver, state, methodName);
318320
return methodLib.callObjectWithState(method, state, arguments);
319321
}
322+
323+
@ExportMessage
324+
static boolean typeCheck(@SuppressWarnings("unused") Boolean receiver, Object type,
325+
@Cached TypeNodes.IsSameTypeNode isSameTypeNode,
326+
@Cached IsSubtypeNode isSubtypeNode) {
327+
Object instanceClass = PythonBuiltinClassType.Boolean;
328+
return isSameTypeNode.execute(instanceClass, type) || isSubtypeNode.execute(instanceClass, type);
329+
}
320330
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.oracle.graal.python.builtins.objects.function.PArguments;
5151
import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
5252
import com.oracle.graal.python.builtins.objects.ints.PInt;
53+
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
5354
import com.oracle.graal.python.nodes.ErrorMessages;
5455
import com.oracle.graal.python.nodes.PRaiseNode;
5556
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
@@ -195,7 +196,7 @@ static int dO(Double receiver, Object other, @SuppressWarnings("unused") ThreadS
195196
static Object asPString(Double receiver,
196197
@CachedLibrary("receiver") PythonObjectLibrary lib,
197198
@CachedLibrary(limit = "1") PythonObjectLibrary resultLib,
198-
@Exclusive @Cached IsSubtypeNode isSubtypeNode,
199+
@Shared("isSubtypeNode") @Cached IsSubtypeNode isSubtypeNode,
199200
@Exclusive @Cached PRaiseNode raise) {
200201
return PythonAbstractObject.asPString(lib, receiver, null, isSubtypeNode, resultLib, raise);
201202
}
@@ -234,7 +235,7 @@ static int asPInt(Double receiver,
234235
}
235236

236237
@ExportMessage
237-
public static Object lookupAttributeInternal(Double receiver, ThreadState state, String name, boolean strict,
238+
static Object lookupAttributeInternal(Double receiver, ThreadState state, String name, boolean strict,
238239
@Cached ConditionProfile gotState,
239240
@Exclusive @Cached PythonAbstractObject.LookupAttributeNode lookup) {
240241
VirtualFrame frame = null;
@@ -245,24 +246,32 @@ public static Object lookupAttributeInternal(Double receiver, ThreadState state,
245246
}
246247

247248
@ExportMessage
248-
public static Object lookupAttributeOnTypeInternal(@SuppressWarnings("unused") Double receiver, String name, boolean strict,
249+
static Object lookupAttributeOnTypeInternal(@SuppressWarnings("unused") Double receiver, String name, boolean strict,
249250
@Exclusive @Cached PythonAbstractObject.LookupAttributeOnTypeNode lookup) {
250251
return lookup.execute(PythonBuiltinClassType.PFloat, name, strict);
251252
}
252253

253254
@ExportMessage
254-
public static Object lookupAndCallSpecialMethodWithState(Double receiver, ThreadState state, String methodName, Object[] arguments,
255+
static Object lookupAndCallSpecialMethodWithState(Double receiver, ThreadState state, String methodName, Object[] arguments,
255256
@CachedLibrary("receiver") PythonObjectLibrary plib,
256257
@Shared("methodLib") @CachedLibrary(limit = "2") PythonObjectLibrary methodLib) {
257258
Object method = plib.lookupAttributeOnTypeStrict(receiver, methodName);
258259
return methodLib.callUnboundMethodWithState(method, state, receiver, arguments);
259260
}
260261

261262
@ExportMessage
262-
public static Object lookupAndCallRegularMethodWithState(Double receiver, ThreadState state, String methodName, Object[] arguments,
263+
static Object lookupAndCallRegularMethodWithState(Double receiver, ThreadState state, String methodName, Object[] arguments,
263264
@CachedLibrary("receiver") PythonObjectLibrary plib,
264265
@Shared("methodLib") @CachedLibrary(limit = "2") PythonObjectLibrary methodLib) {
265266
Object method = plib.lookupAttributeStrictWithState(receiver, state, methodName);
266267
return methodLib.callObjectWithState(method, state, arguments);
267268
}
269+
270+
@ExportMessage
271+
static boolean typeCheck(@SuppressWarnings("unused") Double receiver, Object type,
272+
@Cached TypeNodes.IsSameTypeNode isSameTypeNode,
273+
@Shared("isSubtypeNode") @Cached IsSubtypeNode isSubtypeNode) {
274+
Object instanceClass = PythonBuiltinClassType.PFloat;
275+
return isSameTypeNode.execute(instanceClass, type) || isSubtypeNode.execute(instanceClass, type);
276+
}
268277
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import com.oracle.graal.python.builtins.objects.function.PArguments;
4747
import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
4848
import com.oracle.graal.python.builtins.objects.ints.PInt;
49+
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
50+
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
4951
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
5052
import com.oracle.graal.python.runtime.PythonOptions;
5153
import com.oracle.truffle.api.CompilerDirectives;
@@ -283,7 +285,7 @@ static int asPInt(Integer receiver) {
283285
}
284286

285287
@ExportMessage
286-
public static Object lookupAttributeInternal(Integer receiver, ThreadState state, String name, boolean strict,
288+
static Object lookupAttributeInternal(Integer receiver, ThreadState state, String name, boolean strict,
287289
@Cached ConditionProfile gotState,
288290
@Exclusive @Cached PythonAbstractObject.LookupAttributeNode lookup) {
289291
VirtualFrame frame = null;
@@ -294,24 +296,32 @@ public static Object lookupAttributeInternal(Integer receiver, ThreadState state
294296
}
295297

296298
@ExportMessage
297-
public static Object lookupAttributeOnTypeInternal(@SuppressWarnings("unused") Integer receiver, String name, boolean strict,
299+
static Object lookupAttributeOnTypeInternal(@SuppressWarnings("unused") Integer receiver, String name, boolean strict,
298300
@Exclusive @Cached PythonAbstractObject.LookupAttributeOnTypeNode lookup) {
299301
return lookup.execute(PythonBuiltinClassType.PInt, name, strict);
300302
}
301303

302304
@ExportMessage
303-
public static Object lookupAndCallSpecialMethodWithState(Integer receiver, ThreadState state, String methodName, Object[] arguments,
305+
static Object lookupAndCallSpecialMethodWithState(Integer receiver, ThreadState state, String methodName, Object[] arguments,
304306
@CachedLibrary("receiver") PythonObjectLibrary plib,
305307
@Shared("methodLib") @CachedLibrary(limit = "2") PythonObjectLibrary methodLib) {
306308
Object method = plib.lookupAttributeOnTypeStrict(receiver, methodName);
307309
return methodLib.callUnboundMethodWithState(method, state, receiver, arguments);
308310
}
309311

310312
@ExportMessage
311-
public static Object lookupAndCallRegularMethodWithState(Integer receiver, ThreadState state, String methodName, Object[] arguments,
313+
static Object lookupAndCallRegularMethodWithState(Integer receiver, ThreadState state, String methodName, Object[] arguments,
312314
@CachedLibrary("receiver") PythonObjectLibrary plib,
313315
@Shared("methodLib") @CachedLibrary(limit = "2") PythonObjectLibrary methodLib) {
314316
Object method = plib.lookupAttributeStrictWithState(receiver, state, methodName);
315317
return methodLib.callObjectWithState(method, state, arguments);
316318
}
319+
320+
@ExportMessage
321+
static boolean typeCheck(@SuppressWarnings("unused") Integer receiver, Object type,
322+
@Cached TypeNodes.IsSameTypeNode isSameTypeNode,
323+
@Cached IsSubtypeNode isSubtypeNode) {
324+
Object instanceClass = PythonBuiltinClassType.PInt;
325+
return isSameTypeNode.execute(instanceClass, type) || isSubtypeNode.execute(instanceClass, type);
326+
}
317327
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
import com.oracle.graal.python.builtins.objects.function.PArguments;
4848
import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
4949
import com.oracle.graal.python.builtins.objects.ints.PInt;
50+
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
5051
import com.oracle.graal.python.nodes.ErrorMessages;
5152
import com.oracle.graal.python.nodes.PRaiseNode;
53+
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
5254
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
5355
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
5456
import com.oracle.graal.python.runtime.PythonOptions;
@@ -327,24 +329,32 @@ public static Object lookupAttributeInternal(Long receiver, ThreadState state, S
327329
}
328330

329331
@ExportMessage
330-
public static Object lookupAttributeOnTypeInternal(@SuppressWarnings("unused") Long receiver, String name, boolean strict,
332+
static Object lookupAttributeOnTypeInternal(@SuppressWarnings("unused") Long receiver, String name, boolean strict,
331333
@Exclusive @Cached PythonAbstractObject.LookupAttributeOnTypeNode lookup) {
332334
return lookup.execute(PythonBuiltinClassType.PInt, name, strict);
333335
}
334336

335337
@ExportMessage
336-
public static Object lookupAndCallSpecialMethodWithState(Long receiver, ThreadState state, String methodName, Object[] arguments,
338+
static Object lookupAndCallSpecialMethodWithState(Long receiver, ThreadState state, String methodName, Object[] arguments,
337339
@CachedLibrary("receiver") PythonObjectLibrary plib,
338340
@Shared("methodLib") @CachedLibrary(limit = "2") PythonObjectLibrary methodLib) {
339341
Object method = plib.lookupAttributeOnTypeStrict(receiver, methodName);
340342
return methodLib.callUnboundMethodWithState(method, state, receiver, arguments);
341343
}
342344

343345
@ExportMessage
344-
public static Object lookupAndCallRegularMethodWithState(Long receiver, ThreadState state, String methodName, Object[] arguments,
346+
static Object lookupAndCallRegularMethodWithState(Long receiver, ThreadState state, String methodName, Object[] arguments,
345347
@CachedLibrary("receiver") PythonObjectLibrary plib,
346348
@Shared("methodLib") @CachedLibrary(limit = "2") PythonObjectLibrary methodLib) {
347349
Object method = plib.lookupAttributeStrictWithState(receiver, state, methodName);
348350
return methodLib.callObjectWithState(method, state, arguments);
349351
}
352+
353+
@ExportMessage
354+
static boolean typeCheck(@SuppressWarnings("unused") Long receiver, Object type,
355+
@Cached TypeNodes.IsSameTypeNode isSameTypeNode,
356+
@Cached IsSubtypeNode isSubtypeNode) {
357+
Object instanceClass = PythonBuiltinClassType.PInt;
358+
return isSameTypeNode.execute(instanceClass, type) || isSubtypeNode.execute(instanceClass, type);
359+
}
350360
}

0 commit comments

Comments
 (0)