|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.builtins.modules;
|
42 | 42 |
|
| 43 | +import static com.oracle.graal.python.runtime.exception.PythonErrorType.RuntimeError; |
| 44 | +import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError; |
| 45 | + |
43 | 46 | import java.io.UnsupportedEncodingException;
|
44 | 47 | import java.util.List;
|
45 | 48 | import java.util.regex.Matcher;
|
|
53 | 56 | import com.oracle.graal.python.builtins.objects.bytes.PBytes;
|
54 | 57 | import com.oracle.graal.python.builtins.objects.str.PString;
|
55 | 58 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
| 59 | +import com.oracle.graal.python.nodes.function.PythonBuiltinNode; |
56 | 60 | import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
|
57 | 61 | import com.oracle.graal.python.runtime.exception.PythonErrorType;
|
58 | 62 | import com.oracle.truffle.api.CompilerAsserts;
|
59 | 63 | import com.oracle.truffle.api.CompilerDirectives;
|
60 | 64 | import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
|
61 | 65 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
| 66 | +import com.oracle.truffle.api.dsl.Cached; |
62 | 67 | import com.oracle.truffle.api.dsl.Fallback;
|
63 | 68 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
64 | 69 | import com.oracle.truffle.api.dsl.NodeFactory;
|
65 | 70 | import com.oracle.truffle.api.dsl.Specialization;
|
| 71 | +import com.oracle.truffle.api.interop.ArityException; |
| 72 | +import com.oracle.truffle.api.interop.ForeignAccess; |
| 73 | +import com.oracle.truffle.api.interop.Message; |
| 74 | +import com.oracle.truffle.api.interop.TruffleObject; |
| 75 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
| 76 | +import com.oracle.truffle.api.interop.UnsupportedTypeException; |
| 77 | +import com.oracle.truffle.api.nodes.Node; |
| 78 | +import com.oracle.truffle.api.profiles.BranchProfile; |
66 | 79 |
|
67 | 80 | @CoreFunctions(defineModule = "_sre")
|
68 | 81 | public class SREModuleBuiltins extends PythonBuiltins {
|
@@ -240,4 +253,29 @@ Object run(Object o) {
|
240 | 253 |
|
241 | 254 | }
|
242 | 255 |
|
| 256 | + @Builtin(name = "tregex_call_safe", minNumOfArguments = 1, takesVariableArguments = true) |
| 257 | + @GenerateNodeFactory |
| 258 | + abstract static class TRegexCallSafe extends PythonBuiltinNode { |
| 259 | + @Specialization(guards = "isForeignObject(callable)") |
| 260 | + Object call(TruffleObject callable, Object[] arguments, |
| 261 | + @Cached("create()") BranchProfile runtimeError, |
| 262 | + @Cached("create()") BranchProfile typeError, |
| 263 | + @Cached("createExecute()") Node invokeNode) { |
| 264 | + try { |
| 265 | + return ForeignAccess.sendExecute(invokeNode, callable, arguments); |
| 266 | + } catch (ArityException | UnsupportedTypeException | UnsupportedMessageException e) { |
| 267 | + typeError.enter(); |
| 268 | + throw raise(TypeError, e.getMessage()); |
| 269 | + } catch (RuntimeException e) { |
| 270 | + runtimeError.enter(); |
| 271 | + throw raise(RuntimeError, e.getMessage()); |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + protected static Node createExecute() { |
| 276 | + return Message.createExecute(0).createNode(); |
| 277 | + } |
| 278 | + |
| 279 | + } |
| 280 | + |
243 | 281 | }
|
0 commit comments