|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.nodes.bytecode; |
| 42 | + |
| 43 | +import static com.oracle.graal.python.builtins.PythonBuiltinClassType.GeneratorExit; |
| 44 | + |
| 45 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 46 | +import com.oracle.graal.python.builtins.objects.exception.StopIterationBuiltins; |
| 47 | +import com.oracle.graal.python.builtins.objects.generator.GeneratorBuiltins; |
| 48 | +import com.oracle.graal.python.builtins.objects.generator.PGenerator; |
| 49 | +import com.oracle.graal.python.lib.PyObjectLookupAttr; |
| 50 | +import com.oracle.graal.python.nodes.PNodeWithContext; |
| 51 | +import com.oracle.graal.python.nodes.WriteUnraisableNode; |
| 52 | +import com.oracle.graal.python.nodes.call.CallNode; |
| 53 | +import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile; |
| 54 | +import com.oracle.graal.python.runtime.exception.PException; |
| 55 | +import com.oracle.truffle.api.dsl.Cached; |
| 56 | +import com.oracle.truffle.api.dsl.Cached.Shared; |
| 57 | +import com.oracle.truffle.api.dsl.Fallback; |
| 58 | +import com.oracle.truffle.api.dsl.Specialization; |
| 59 | +import com.oracle.truffle.api.frame.Frame; |
| 60 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 61 | + |
| 62 | +public abstract class ThrowNode extends PNodeWithContext { |
| 63 | + // Returns true when the generator finished |
| 64 | + public abstract boolean execute(VirtualFrame virtualFrame, int stackTop, Frame localFrame, Object iter, PException exception); |
| 65 | + |
| 66 | + @Specialization |
| 67 | + boolean doGenerator(VirtualFrame virtualFrame, int stackTop, Frame localFrame, PGenerator generator, PException exception, |
| 68 | + @Cached GeneratorBuiltins.ThrowNode throwNode, |
| 69 | + @Cached GeneratorBuiltins.CloseNode closeNode, |
| 70 | + @Shared("exitProfile") @Cached IsBuiltinClassProfile profileExit, |
| 71 | + @Shared("profile") @Cached IsBuiltinClassProfile stopIterationProfile, |
| 72 | + @Shared("getValue") @Cached StopIterationBuiltins.StopIterationValueNode getValue) { |
| 73 | + if (profileExit.profileException(exception, GeneratorExit)) { |
| 74 | + closeNode.execute(virtualFrame, generator); |
| 75 | + throw exception; |
| 76 | + } else { |
| 77 | + try { |
| 78 | + Object value = throwNode.execute(virtualFrame, generator, exception.getEscapedException(), PNone.NO_VALUE, PNone.NO_VALUE); |
| 79 | + localFrame.setObject(stackTop, value); |
| 80 | + return false; |
| 81 | + } catch (PException e) { |
| 82 | + handleException(e, stopIterationProfile, getValue, stackTop, localFrame); |
| 83 | + return true; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Fallback |
| 89 | + boolean doOther(VirtualFrame virtualFrame, int stackTop, Frame localFrame, Object obj, PException exception, |
| 90 | + @Cached PyObjectLookupAttr lookupThrow, |
| 91 | + @Cached PyObjectLookupAttr lookupClose, |
| 92 | + @Cached CallNode callThrow, |
| 93 | + @Cached CallNode callClose, |
| 94 | + @Cached WriteUnraisableNode writeUnraisableNode, |
| 95 | + @Shared("exitProfile") @Cached IsBuiltinClassProfile profileExit, |
| 96 | + @Shared("profile") @Cached IsBuiltinClassProfile stopIterationProfile, |
| 97 | + @Shared("getValue") @Cached StopIterationBuiltins.StopIterationValueNode getValue) { |
| 98 | + if (profileExit.profileException(exception, GeneratorExit)) { |
| 99 | + Object close = PNone.NO_VALUE; |
| 100 | + try { |
| 101 | + close = lookupClose.execute(virtualFrame, obj, "close"); |
| 102 | + } catch (PException e) { |
| 103 | + writeUnraisableNode.execute(virtualFrame, e.getEscapedException(), null, obj); |
| 104 | + } |
| 105 | + if (close != PNone.NO_VALUE) { |
| 106 | + callClose.execute(virtualFrame, close); |
| 107 | + } |
| 108 | + throw exception; |
| 109 | + } else { |
| 110 | + Object throwMethod = lookupThrow.execute(virtualFrame, obj, "throw"); |
| 111 | + if (throwMethod == PNone.NO_VALUE) { |
| 112 | + throw exception; |
| 113 | + } |
| 114 | + try { |
| 115 | + Object value = callThrow.execute(virtualFrame, throwMethod); |
| 116 | + localFrame.setObject(stackTop, value); |
| 117 | + return false; |
| 118 | + } catch (PException e) { |
| 119 | + handleException(e, stopIterationProfile, getValue, stackTop, localFrame); |
| 120 | + return true; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private static void handleException(PException e, IsBuiltinClassProfile stopIterationProfile, StopIterationBuiltins.StopIterationValueNode getValue, int stackTop, Frame localFrame) { |
| 126 | + e.expectStopIteration(stopIterationProfile); |
| 127 | + Object value = getValue.execute(e.getUnreifiedException()); |
| 128 | + localFrame.setObject(stackTop, null); |
| 129 | + localFrame.setObject(stackTop - 1, value); |
| 130 | + } |
| 131 | + |
| 132 | + public static ThrowNode create() { |
| 133 | + return ThrowNodeGen.create(); |
| 134 | + } |
| 135 | +} |
0 commit comments