|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, 2025, Oracle and/or its affiliates. |
| 3 | + * Copyright (c) 2014, Regents of the University of California |
| 4 | + * |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without modification, are |
| 8 | + * permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, this list of |
| 11 | + * conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of |
| 13 | + * conditions and the following disclaimer in the documentation and/or other materials provided |
| 14 | + * with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| 17 | + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 21 | + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 22 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 23 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 24 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +package com.oracle.graal.python.builtins.objects.foreign; |
| 28 | + |
| 29 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___CALL__; |
| 30 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___NEW__; |
| 31 | + |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +import com.oracle.graal.python.PythonLanguage; |
| 35 | +import com.oracle.graal.python.builtins.Builtin; |
| 36 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 37 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 38 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 39 | +import com.oracle.graal.python.builtins.objects.function.PKeyword; |
| 40 | +import com.oracle.graal.python.nodes.ErrorMessages; |
| 41 | +import com.oracle.graal.python.nodes.PRaiseNode; |
| 42 | +import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 43 | +import com.oracle.graal.python.nodes.function.PythonBuiltinNode; |
| 44 | +import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode; |
| 45 | +import com.oracle.graal.python.nodes.object.IsForeignObjectNode; |
| 46 | +import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext; |
| 47 | +import com.oracle.graal.python.runtime.GilNode; |
| 48 | +import com.oracle.graal.python.runtime.IndirectCallData; |
| 49 | +import com.oracle.graal.python.runtime.PythonContext; |
| 50 | +import com.oracle.graal.python.runtime.exception.PythonErrorType; |
| 51 | +import com.oracle.truffle.api.CompilerDirectives; |
| 52 | +import com.oracle.truffle.api.dsl.Bind; |
| 53 | +import com.oracle.truffle.api.dsl.Cached; |
| 54 | +import com.oracle.truffle.api.dsl.Fallback; |
| 55 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 56 | +import com.oracle.truffle.api.dsl.NodeFactory; |
| 57 | +import com.oracle.truffle.api.dsl.Specialization; |
| 58 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 59 | +import com.oracle.truffle.api.interop.ArityException; |
| 60 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 61 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
| 62 | +import com.oracle.truffle.api.interop.UnsupportedTypeException; |
| 63 | +import com.oracle.truffle.api.library.CachedLibrary; |
| 64 | +import com.oracle.truffle.api.nodes.Node; |
| 65 | + |
| 66 | +@CoreFunctions(extendClasses = PythonBuiltinClassType.ForeignInstantiable) |
| 67 | +public final class ForeignInstantiableBuiltins extends PythonBuiltins { |
| 68 | + @Override |
| 69 | + protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
| 70 | + return ForeignInstantiableBuiltinsFactory.getFactories(); |
| 71 | + } |
| 72 | + |
| 73 | + @Builtin(name = J___NEW__, minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true) |
| 74 | + @GenerateNodeFactory |
| 75 | + abstract static class NewNode extends PythonBuiltinNode { |
| 76 | + @Specialization(guards = {"isForeignObjectNode.execute(inliningTarget, callee)", "!isNoValue(callee)", "keywords.length == 0"}, limit = "1") |
| 77 | + static Object doInteropCall(Object callee, Object[] arguments, @SuppressWarnings("unused") PKeyword[] keywords, |
| 78 | + @SuppressWarnings("unused") @Bind("this") Node inliningTarget, |
| 79 | + @SuppressWarnings("unused") @Cached IsForeignObjectNode isForeignObjectNode, |
| 80 | + @CachedLibrary(limit = "3") InteropLibrary lib, |
| 81 | + @Cached PForeignToPTypeNode toPTypeNode, |
| 82 | + @Cached GilNode gil, |
| 83 | + @Cached PRaiseNode.Lazy raiseNode) { |
| 84 | + gil.release(true); |
| 85 | + try { |
| 86 | + Object res = lib.instantiate(callee, arguments); |
| 87 | + return toPTypeNode.executeConvert(res); |
| 88 | + } catch (ArityException | UnsupportedTypeException e) { |
| 89 | + throw raiseNode.get(inliningTarget).raise(PythonErrorType.TypeError, ErrorMessages.INVALID_INSTANTIATION_OF_FOREIGN_OBJ); |
| 90 | + } catch (UnsupportedMessageException e) { |
| 91 | + throw CompilerDirectives.shouldNotReachHere(e); |
| 92 | + } finally { |
| 93 | + gil.acquire(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Fallback |
| 98 | + @SuppressWarnings("unused") |
| 99 | + static Object doGeneric(Object callee, Object arguments, Object keywords, |
| 100 | + @Cached PRaiseNode raiseNode) { |
| 101 | + throw raiseNode.raise(PythonErrorType.TypeError, ErrorMessages.INVALID_INSTANTIATION_OF_FOREIGN_OBJ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Builtin(name = J___CALL__, minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true) |
| 106 | + @GenerateNodeFactory |
| 107 | + public abstract static class CallNode extends PythonBuiltinNode { |
| 108 | + @Specialization(guards = {"isForeignObjectNode.execute(inliningTarget, callee)", "!isNoValue(callee)", "keywords.length == 0"}, limit = "1") |
| 109 | + static Object doInteropCall(VirtualFrame frame, Object callee, Object[] arguments, @SuppressWarnings("unused") PKeyword[] keywords, |
| 110 | + @SuppressWarnings("unused") @Bind("this") Node inliningTarget, |
| 111 | + @Cached("createFor(this)") IndirectCallData indirectCallData, |
| 112 | + @SuppressWarnings("unused") @Cached IsForeignObjectNode isForeignObjectNode, |
| 113 | + @CachedLibrary(limit = "4") InteropLibrary lib, |
| 114 | + @Cached PForeignToPTypeNode toPTypeNode, |
| 115 | + @Cached GilNode gil, |
| 116 | + @Cached PRaiseNode.Lazy raiseNode) { |
| 117 | + PythonLanguage language = PythonLanguage.get(inliningTarget); |
| 118 | + PythonContext context = PythonContext.get(inliningTarget); |
| 119 | + try { |
| 120 | + Object state = IndirectCallContext.enter(frame, language, context, indirectCallData); |
| 121 | + gil.release(true); |
| 122 | + try { |
| 123 | + return toPTypeNode.executeConvert(lib.instantiate(callee, arguments)); |
| 124 | + } finally { |
| 125 | + gil.acquire(); |
| 126 | + IndirectCallContext.exit(frame, language, context, state); |
| 127 | + } |
| 128 | + } catch (ArityException | UnsupportedTypeException e) { |
| 129 | + throw raiseNode.get(inliningTarget).raise(PythonErrorType.TypeError, ErrorMessages.INVALID_INSTANTIATION_OF_FOREIGN_OBJ); |
| 130 | + } catch (UnsupportedMessageException e) { |
| 131 | + throw CompilerDirectives.shouldNotReachHere(e); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Fallback |
| 136 | + @SuppressWarnings("unused") |
| 137 | + static Object doGeneric(Object callee, Object arguments, Object keywords, |
| 138 | + @Cached PRaiseNode raiseNode) { |
| 139 | + throw raiseNode.raise(PythonErrorType.TypeError, ErrorMessages.INVALID_INSTANTIATION_OF_FOREIGN_OBJ); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments