|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * The Universal Permissive License (UPL), Version 1.0 |
| 5 | + * |
| 6 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 7 | + * person obtaining a copy of this software, associated documentation and/or data |
| 8 | + * (collectively the "Software"), free of charge and under any and all copyright |
| 9 | + * rights in the Software, and any and all patent rights owned or freely |
| 10 | + * licensable by each licensor hereunder covering either (i) the unmodified |
| 11 | + * Software as contributed to or provided by such licensor, or (ii) the Larger |
| 12 | + * Works (as defined below), to deal in both |
| 13 | + * |
| 14 | + * (a) the Software, and |
| 15 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 16 | + * one is included with the Software (each a "Larger Work" to which the |
| 17 | + * Software is contributed by such licensors), |
| 18 | + * |
| 19 | + * without restriction, including without limitation the rights to copy, create |
| 20 | + * derivative works of, display, perform, and distribute the Software and make, |
| 21 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 22 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 23 | + * either these or other terms. |
| 24 | + * |
| 25 | + * This license is subject to the following condition: |
| 26 | + * |
| 27 | + * The above copyright notice and either this complete permission notice or at a |
| 28 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 29 | + * portions of the Software. |
| 30 | + * |
| 31 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 32 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 33 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 34 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 35 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 36 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 37 | + * SOFTWARE. |
| 38 | + */ |
| 39 | +package com.oracle.graal.python.builtins.objects.cext; |
| 40 | + |
| 41 | +import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToJavaNode; |
| 42 | +import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToSulongNode; |
| 43 | +import com.oracle.graal.python.builtins.objects.cext.ManagedMethodWrappers.MethKeywords; |
| 44 | +import com.oracle.graal.python.builtins.objects.cext.ManagedMethodWrappers.MethVarargs; |
| 45 | +import com.oracle.graal.python.builtins.objects.cext.ManagedMethodWrappers.MethodWrapper; |
| 46 | +import com.oracle.graal.python.builtins.objects.function.PKeyword; |
| 47 | +import com.oracle.graal.python.nodes.argument.ArityCheckNode; |
| 48 | +import com.oracle.graal.python.nodes.argument.CreateArgumentsNode; |
| 49 | +import com.oracle.graal.python.nodes.argument.keywords.ExecuteKeywordStarargsNode; |
| 50 | +import com.oracle.graal.python.nodes.argument.positional.ExecutePositionalStarargsNode; |
| 51 | +import com.oracle.graal.python.nodes.argument.positional.PositionalArgumentsNode; |
| 52 | +import com.oracle.graal.python.nodes.call.CallDispatchNode; |
| 53 | +import com.oracle.graal.python.runtime.interop.PythonMessageResolution; |
| 54 | +import com.oracle.truffle.api.CompilerDirectives; |
| 55 | +import com.oracle.truffle.api.interop.ArityException; |
| 56 | +import com.oracle.truffle.api.interop.MessageResolution; |
| 57 | +import com.oracle.truffle.api.interop.Resolve; |
| 58 | +import com.oracle.truffle.api.nodes.ExplodeLoop; |
| 59 | +import com.oracle.truffle.api.nodes.Node; |
| 60 | +import com.oracle.truffle.api.profiles.ValueProfile; |
| 61 | + |
| 62 | +@MessageResolution(receiverType = MethodWrapper.class) |
| 63 | +public class ManagedMethodWrappersMR { |
| 64 | + |
| 65 | + @Resolve(message = "EXECUTE") |
| 66 | + abstract static class ExecuteNode extends Node { |
| 67 | + @Child PythonMessageResolution.ExecuteNode executeNode; |
| 68 | + @Child private ToJavaNode toJavaNode; |
| 69 | + @Child private ToSulongNode toSulongNode; |
| 70 | + |
| 71 | + @Child private ExecutePositionalStarargsNode posStarargsNode = ExecutePositionalStarargsNode.create(); |
| 72 | + @Child private PositionalArgumentsNode posArgsNode = PositionalArgumentsNode.create(); |
| 73 | + @Child private ExecuteKeywordStarargsNode expandKwargsNode = ExecuteKeywordStarargsNode.create(); |
| 74 | + @Child private CallDispatchNode dispatch; |
| 75 | + @Child private CreateArgumentsNode createArgs = CreateArgumentsNode.create(); |
| 76 | + @Child private ArityCheckNode arityCheckNode = ArityCheckNode.create(); |
| 77 | + final ValueProfile classProfile = ValueProfile.createClassProfile(); |
| 78 | + |
| 79 | + private CallDispatchNode getDispatchNode() { |
| 80 | + if (dispatch == null) { |
| 81 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 82 | + dispatch = insert(CallDispatchNode.create("<foreign-invoke>")); |
| 83 | + } |
| 84 | + return dispatch; |
| 85 | + } |
| 86 | + |
| 87 | + @ExplodeLoop |
| 88 | + public Object access(MethKeywords object, Object[] arguments) { |
| 89 | + if (executeNode == null) { |
| 90 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 91 | + executeNode = insert(new PythonMessageResolution.ExecuteNode()); |
| 92 | + } |
| 93 | + if (arguments.length != 2) { |
| 94 | + throw ArityException.raise(2, arguments.length); |
| 95 | + } |
| 96 | + |
| 97 | + // convert args |
| 98 | + Object[] converted = new Object[arguments.length]; |
| 99 | + for (int i = 0; i < arguments.length; i++) { |
| 100 | + converted[i] = getToJavaNode().execute(arguments[i]); |
| 101 | + } |
| 102 | + |
| 103 | + Object[] userArgs = posArgsNode.executeWithArguments(null, posStarargsNode.executeWith(converted[0])); |
| 104 | + Object[] pArgs = createArgs.execute(userArgs); |
| 105 | + PKeyword[] kwargs = expandKwargsNode.executeWith(converted[1]); |
| 106 | + return getToSulongNode().execute(getDispatchNode().executeCall(object.getMethod(), pArgs, kwargs)); |
| 107 | + } |
| 108 | + |
| 109 | + @ExplodeLoop |
| 110 | + public Object access(MethVarargs object, Object[] arguments) { |
| 111 | + if (executeNode == null) { |
| 112 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 113 | + executeNode = insert(new PythonMessageResolution.ExecuteNode()); |
| 114 | + } |
| 115 | + if (arguments.length != 1) { |
| 116 | + throw ArityException.raise(1, arguments.length); |
| 117 | + } |
| 118 | + |
| 119 | + // convert args |
| 120 | + return getToSulongNode().execute(executeNode.execute(object.getMethod(), new Object[]{getToJavaNode().execute(arguments[0])})); |
| 121 | + } |
| 122 | + |
| 123 | + private ToJavaNode getToJavaNode() { |
| 124 | + if (toJavaNode == null) { |
| 125 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 126 | + toJavaNode = insert(ToJavaNode.create()); |
| 127 | + } |
| 128 | + return toJavaNode; |
| 129 | + } |
| 130 | + |
| 131 | + private ToSulongNode getToSulongNode() { |
| 132 | + if (toSulongNode == null) { |
| 133 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 134 | + toSulongNode = insert(ToSulongNode.create()); |
| 135 | + } |
| 136 | + return toSulongNode; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments