|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.builtins.objects.cext;
|
42 | 42 |
|
| 43 | +import java.util.List; |
| 44 | + |
| 45 | +import com.oracle.graal.python.builtins.Builtin; |
43 | 46 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
44 | 47 | import com.oracle.graal.python.builtins.modules.BuiltinFunctions.GetAttrNode;
|
45 | 48 | import com.oracle.graal.python.builtins.objects.PNone;
|
|
67 | 70 | import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
|
68 | 71 | import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.TruffleObjectNativeWrapper;
|
69 | 72 | import com.oracle.graal.python.builtins.objects.floats.PFloat;
|
| 73 | +import com.oracle.graal.python.builtins.objects.function.PFunction; |
70 | 74 | import com.oracle.graal.python.builtins.objects.function.PKeyword;
|
| 75 | +import com.oracle.graal.python.builtins.objects.function.PythonCallable; |
71 | 76 | import com.oracle.graal.python.builtins.objects.ints.PInt;
|
72 | 77 | import com.oracle.graal.python.builtins.objects.str.PString;
|
73 | 78 | import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
|
74 | 79 | import com.oracle.graal.python.builtins.objects.type.PythonClass;
|
75 |
| -import com.oracle.graal.python.nodes.PNodeWithContext; |
76 | 80 | import com.oracle.graal.python.nodes.PGuards;
|
| 81 | +import com.oracle.graal.python.nodes.PNodeWithContext; |
77 | 82 | import com.oracle.graal.python.nodes.SpecialMethodNames;
|
| 83 | +import com.oracle.graal.python.nodes.argument.CreateArgumentsNode; |
| 84 | +import com.oracle.graal.python.nodes.argument.ReadArgumentNode; |
| 85 | +import com.oracle.graal.python.nodes.argument.ReadVarArgsNode; |
78 | 86 | import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
|
79 | 87 | import com.oracle.graal.python.nodes.call.CallNode;
|
| 88 | +import com.oracle.graal.python.nodes.call.InvokeNode; |
80 | 89 | import com.oracle.graal.python.nodes.call.special.CallBinaryMethodNode;
|
81 | 90 | import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode;
|
82 | 91 | import com.oracle.graal.python.nodes.call.special.CallUnaryMethodNode;
|
83 | 92 | import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
|
84 | 93 | import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
|
| 94 | +import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 95 | +import com.oracle.graal.python.nodes.function.PythonBuiltinNode; |
| 96 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
| 97 | +import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode; |
| 98 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
85 | 99 | import com.oracle.graal.python.nodes.object.GetClassNode;
|
86 | 100 | import com.oracle.graal.python.nodes.util.CastToIndexNode;
|
87 | 101 | import com.oracle.graal.python.runtime.PythonCore;
|
| 102 | +import com.oracle.graal.python.runtime.exception.PException; |
88 | 103 | import com.oracle.graal.python.runtime.exception.PythonErrorType;
|
| 104 | +import com.oracle.graal.python.runtime.object.PythonObjectFactory; |
89 | 105 | import com.oracle.truffle.api.CompilerDirectives;
|
90 | 106 | import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
|
91 | 107 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
92 | 108 | import com.oracle.truffle.api.dsl.Cached;
|
93 | 109 | import com.oracle.truffle.api.dsl.Fallback;
|
94 | 110 | import com.oracle.truffle.api.dsl.ImportStatic;
|
| 111 | +import com.oracle.truffle.api.dsl.NodeFactory; |
95 | 112 | import com.oracle.truffle.api.dsl.Specialization;
|
96 | 113 | import com.oracle.truffle.api.frame.VirtualFrame;
|
97 | 114 | import com.oracle.truffle.api.interop.ArityException;
|
|
104 | 121 | import com.oracle.truffle.api.interop.UnsupportedTypeException;
|
105 | 122 | import com.oracle.truffle.api.nodes.ExplodeLoop;
|
106 | 123 | import com.oracle.truffle.api.nodes.Node;
|
| 124 | +import com.oracle.truffle.api.nodes.NodeUtil; |
107 | 125 | import com.oracle.truffle.api.profiles.BranchProfile;
|
108 | 126 | import com.oracle.truffle.api.profiles.ConditionProfile;
|
109 | 127 |
|
@@ -1185,4 +1203,133 @@ public static PCallBinaryCapiFunction create(String name) {
|
1185 | 1203 | }
|
1186 | 1204 | }
|
1187 | 1205 |
|
| 1206 | + public static class MayRaiseNodeFactory<T extends PythonBuiltinBaseNode> implements NodeFactory<T> { |
| 1207 | + private final T node; |
| 1208 | + |
| 1209 | + public MayRaiseNodeFactory(T node) { |
| 1210 | + this.node = node; |
| 1211 | + } |
| 1212 | + |
| 1213 | + public T createNode(Object... arguments) { |
| 1214 | + return NodeUtil.cloneNode(node); |
| 1215 | + } |
| 1216 | + |
| 1217 | + @SuppressWarnings("unchecked") |
| 1218 | + public Class<T> getNodeClass() { |
| 1219 | + return (Class<T>) node.getClass(); |
| 1220 | + } |
| 1221 | + |
| 1222 | + public List<List<Class<?>>> getNodeSignatures() { |
| 1223 | + throw new IllegalAccessError(); |
| 1224 | + } |
| 1225 | + |
| 1226 | + public List<Class<? extends Node>> getExecutionSignature() { |
| 1227 | + throw new IllegalAccessError(); |
| 1228 | + } |
| 1229 | + } |
| 1230 | + |
| 1231 | + @Builtin(fixedNumOfPositionalArgs = 1) |
| 1232 | + public static abstract class MayRaiseUnaryNode extends PythonUnaryBuiltinNode { |
| 1233 | + @Child private CreateArgumentsNode createArgsNode; |
| 1234 | + @Child private InvokeNode invokeNode; |
| 1235 | + private final Object errorResult; |
| 1236 | + |
| 1237 | + public MayRaiseUnaryNode(PFunction func, Object errorResult) { |
| 1238 | + this.createArgsNode = CreateArgumentsNode.create(); |
| 1239 | + this.invokeNode = InvokeNode.create(func); |
| 1240 | + this.errorResult = errorResult; |
| 1241 | + } |
| 1242 | + |
| 1243 | + @Specialization |
| 1244 | + Object doit(Object argument) { |
| 1245 | + try { |
| 1246 | + Object[] arguments = createArgsNode.execute(argument); |
| 1247 | + return invokeNode.execute(null, arguments, new PKeyword[0]); |
| 1248 | + } catch (PException e) { |
| 1249 | + getContext().setCurrentException(e); |
| 1250 | + return errorResult; |
| 1251 | + } |
| 1252 | + } |
| 1253 | + } |
| 1254 | + |
| 1255 | + @Builtin(fixedNumOfPositionalArgs = 2) |
| 1256 | + public static abstract class MayRaiseBinaryNode extends PythonBinaryBuiltinNode { |
| 1257 | + @Child private CreateArgumentsNode createArgsNode; |
| 1258 | + @Child private InvokeNode invokeNode; |
| 1259 | + private final Object errorResult; |
| 1260 | + |
| 1261 | + public MayRaiseBinaryNode(PFunction func, Object errorResult) { |
| 1262 | + this.createArgsNode = CreateArgumentsNode.create(); |
| 1263 | + this.invokeNode = InvokeNode.create(func); |
| 1264 | + this.errorResult = errorResult; |
| 1265 | + } |
| 1266 | + |
| 1267 | + @Specialization |
| 1268 | + Object doit(Object arg1, Object arg2) { |
| 1269 | + try { |
| 1270 | + Object[] arguments = createArgsNode.execute(arg1, arg2); |
| 1271 | + return invokeNode.execute(null, arguments, new PKeyword[0]); |
| 1272 | + } catch (PException e) { |
| 1273 | + getContext().setCurrentException(e); |
| 1274 | + return errorResult; |
| 1275 | + } |
| 1276 | + } |
| 1277 | + } |
| 1278 | + |
| 1279 | + @Builtin(fixedNumOfPositionalArgs = 3) |
| 1280 | + public static abstract class MayRaiseTernaryNode extends PythonTernaryBuiltinNode { |
| 1281 | + @Child private CreateArgumentsNode createArgsNode; |
| 1282 | + @Child private InvokeNode invokeNode; |
| 1283 | + private final Object errorResult; |
| 1284 | + |
| 1285 | + public MayRaiseTernaryNode(PFunction func, Object errorResult) { |
| 1286 | + this.createArgsNode = CreateArgumentsNode.create(); |
| 1287 | + this.invokeNode = InvokeNode.create(func); |
| 1288 | + this.errorResult = errorResult; |
| 1289 | + } |
| 1290 | + |
| 1291 | + @Specialization |
| 1292 | + Object doit(Object arg1, Object arg2, Object arg3) { |
| 1293 | + try { |
| 1294 | + Object[] arguments = createArgsNode.execute(arg1, arg2, arg3); |
| 1295 | + return invokeNode.execute(null, arguments, new PKeyword[0]); |
| 1296 | + } catch (PException e) { |
| 1297 | + getContext().setCurrentException(e); |
| 1298 | + return errorResult; |
| 1299 | + } |
| 1300 | + } |
| 1301 | + } |
| 1302 | + |
| 1303 | + @Builtin(takesVarArgs = true) |
| 1304 | + public static class MayRaiseNode extends PythonBuiltinNode { |
| 1305 | + @Child private InvokeNode invokeNode; |
| 1306 | + @Child private ReadVarArgsNode readVarargsNode; |
| 1307 | + @Child private CreateArgumentsNode createArgsNode; |
| 1308 | + @Child private PythonObjectFactory factory; |
| 1309 | + private final Object errorResult; |
| 1310 | + |
| 1311 | + public MayRaiseNode(PythonCallable callable, Object errorResult) { |
| 1312 | + this.readVarargsNode = ReadVarArgsNode.create(0, true); |
| 1313 | + this.createArgsNode = CreateArgumentsNode.create(); |
| 1314 | + this.invokeNode = InvokeNode.create(callable); |
| 1315 | + this.errorResult = errorResult; |
| 1316 | + } |
| 1317 | + |
| 1318 | + @Override |
| 1319 | + public final Object execute(VirtualFrame frame) { |
| 1320 | + Object[] args = readVarargsNode.executeObjectArray(frame); |
| 1321 | + try { |
| 1322 | + Object[] arguments = createArgsNode.execute(args); |
| 1323 | + return invokeNode.execute(null, arguments, new PKeyword[0]); |
| 1324 | + } catch (PException e) { |
| 1325 | + getContext().setCurrentException(e); |
| 1326 | + return errorResult; |
| 1327 | + } |
| 1328 | + } |
| 1329 | + |
| 1330 | + @Override |
| 1331 | + protected ReadArgumentNode[] getArguments() { |
| 1332 | + throw new IllegalAccessError(); |
| 1333 | + } |
| 1334 | + } |
1188 | 1335 | }
|
0 commit comments