|
47 | 47 | import com.oracle.graal.python.builtins.PythonBuiltins; |
48 | 48 | import com.oracle.graal.python.builtins.objects.str.PString; |
49 | 49 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 50 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
50 | 51 | import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
51 | 52 | import com.oracle.graal.python.runtime.exception.PythonErrorType; |
52 | 53 | import com.oracle.truffle.api.TruffleLanguage.Env; |
| 54 | +import com.oracle.truffle.api.dsl.Fallback; |
53 | 55 | import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
54 | 56 | import com.oracle.truffle.api.dsl.NodeFactory; |
55 | 57 | import com.oracle.truffle.api.dsl.Specialization; |
| 58 | +import com.oracle.truffle.api.interop.TruffleObject; |
56 | 59 |
|
57 | 60 | @CoreFunctions(defineModule = "java") |
58 | 61 | public class JavaModuleBuiltins extends PythonBuiltins { |
@@ -122,4 +125,34 @@ boolean check(Object object) { |
122 | 125 | return env.isHostSymbol(object); |
123 | 126 | } |
124 | 127 | } |
| 128 | + |
| 129 | + @Builtin(name = "instanceof", fixedNumOfPositionalArgs = 2) |
| 130 | + @GenerateNodeFactory |
| 131 | + abstract static class InstanceOfNode extends PythonBinaryBuiltinNode { |
| 132 | + @Specialization(guards = {"!isForeignObject(object)", "isForeignObject(klass)"}) |
| 133 | + boolean check(Object object, TruffleObject klass) { |
| 134 | + Env env = getContext().getEnv(); |
| 135 | + Object hostKlass = env.asHostObject(klass); |
| 136 | + if (hostKlass instanceof Class<?>) { |
| 137 | + return ((Class<?>) hostKlass).isInstance(object); |
| 138 | + } |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + @Specialization(guards = {"isForeignObject(object)", "isForeignObject(klass)"}) |
| 143 | + boolean checkForeign(Object object, TruffleObject klass) { |
| 144 | + Env env = getContext().getEnv(); |
| 145 | + Object hostObject = env.asHostObject(object); |
| 146 | + Object hostKlass = env.asHostObject(klass); |
| 147 | + if (hostKlass instanceof Class<?>) { |
| 148 | + return ((Class<?>) hostKlass).isInstance(hostObject); |
| 149 | + } |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + @Fallback |
| 154 | + boolean fallback(Object object, Object klass) { |
| 155 | + throw raise(PythonErrorType.TypeError, "unsupported instanceof(%p, %p)", object, klass); |
| 156 | + } |
| 157 | + } |
125 | 158 | } |
0 commit comments