|
| 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.builtins.objects.method; |
| 42 | + |
| 43 | +import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError; |
| 44 | +import static com.oracle.graal.python.nodes.ErrorMessages.FIRST_ARG_MUST_BE_CALLABLE_S; |
| 45 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DOC__; |
| 46 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.__FUNC__; |
| 47 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__; |
| 48 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__; |
| 49 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__; |
| 50 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__GET__; |
| 51 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__INIT__; |
| 52 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__; |
| 53 | + |
| 54 | +import java.util.List; |
| 55 | + |
| 56 | +import com.oracle.graal.python.builtins.Builtin; |
| 57 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 58 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 59 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 60 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 61 | +import com.oracle.graal.python.builtins.objects.PythonAbstractObject; |
| 62 | +import com.oracle.graal.python.builtins.objects.function.PKeyword; |
| 63 | +import com.oracle.graal.python.builtins.objects.object.ObjectBuiltins; |
| 64 | +import com.oracle.graal.python.lib.PyCallableCheckNode; |
| 65 | +import com.oracle.graal.python.lib.PyObjectGetAttr; |
| 66 | +import com.oracle.graal.python.nodes.PGuards; |
| 67 | +import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 68 | +import com.oracle.graal.python.nodes.function.PythonBuiltinNode; |
| 69 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
| 70 | +import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode; |
| 71 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 72 | +import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode; |
| 73 | +import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode.VarargsBuiltinDirectInvocationNotSupported; |
| 74 | +import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile; |
| 75 | +import com.oracle.graal.python.runtime.exception.PException; |
| 76 | +import com.oracle.graal.python.util.PythonUtils; |
| 77 | +import com.oracle.truffle.api.dsl.Cached; |
| 78 | +import com.oracle.truffle.api.dsl.Cached.Shared; |
| 79 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 80 | +import com.oracle.truffle.api.dsl.ImportStatic; |
| 81 | +import com.oracle.truffle.api.dsl.NodeFactory; |
| 82 | +import com.oracle.truffle.api.dsl.Specialization; |
| 83 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 84 | + |
| 85 | +@CoreFunctions(extendClasses = {PythonBuiltinClassType.PInstancemethod}) |
| 86 | +public class InstancemethodBuiltins extends PythonBuiltins { |
| 87 | + |
| 88 | + @Override |
| 89 | + protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
| 90 | + return InstancemethodBuiltinsFactory.getFactories(); |
| 91 | + } |
| 92 | + |
| 93 | + @Builtin(name = __INIT__, minNumOfPositionalArgs = 2) |
| 94 | + @GenerateNodeFactory |
| 95 | + abstract static class InitNode extends PythonBinaryBuiltinNode { |
| 96 | + @Specialization(guards = "checkCallableNode.execute(callable)", limit = "1") |
| 97 | + protected static PNone init(PDecoratedMethod self, Object callable, |
| 98 | + @Shared("checkCallable") @SuppressWarnings("unused") @Cached PyCallableCheckNode checkCallableNode) { |
| 99 | + self.setCallable(callable); |
| 100 | + return PNone.NONE; |
| 101 | + } |
| 102 | + |
| 103 | + @Specialization(guards = "!checkCallableNode.execute(callable)", limit = "1") |
| 104 | + protected PNone noCallble(@SuppressWarnings("unused") PDecoratedMethod self, Object callable, |
| 105 | + @Shared("checkCallable") @SuppressWarnings("unused") @Cached PyCallableCheckNode checkCallableNode) { |
| 106 | + throw raise(TypeError, FIRST_ARG_MUST_BE_CALLABLE_S, callable); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Builtin(name = __FUNC__, minNumOfPositionalArgs = 1, isGetter = true) |
| 111 | + @GenerateNodeFactory |
| 112 | + abstract static class FuncNode extends PythonUnaryBuiltinNode { |
| 113 | + @Specialization |
| 114 | + protected static Object func(PDecoratedMethod self) { |
| 115 | + return self.getCallable(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @ImportStatic(PGuards.class) |
| 120 | + @Builtin(name = __GETATTRIBUTE__, minNumOfPositionalArgs = 2) |
| 121 | + @GenerateNodeFactory |
| 122 | + public abstract static class GetattributeNode extends PythonBuiltinNode { |
| 123 | + @Specialization |
| 124 | + protected static Object doIt(VirtualFrame frame, PDecoratedMethod self, Object key, |
| 125 | + @Cached ObjectBuiltins.GetAttributeNode objectGetattrNode, |
| 126 | + @Cached PyObjectGetAttr getAttrNode, |
| 127 | + @Cached IsBuiltinClassProfile errorProfile) { |
| 128 | + try { |
| 129 | + return objectGetattrNode.execute(frame, self, key); |
| 130 | + } catch (PException e) { |
| 131 | + e.expectAttributeError(errorProfile); |
| 132 | + return getAttrNode.execute(frame, self.getCallable(), key); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Builtin(name = __DOC__, maxNumOfPositionalArgs = 1, isGetter = true) |
| 138 | + @GenerateNodeFactory |
| 139 | + abstract static class DocNode extends PythonUnaryBuiltinNode { |
| 140 | + @Specialization |
| 141 | + static Object doc(VirtualFrame frame, PDecoratedMethod self, |
| 142 | + @Cached PyObjectGetAttr getAttr) { |
| 143 | + return getAttr.execute(frame, self.getCallable(), __DOC__); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Builtin(name = __CALL__, minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true) |
| 148 | + @GenerateNodeFactory |
| 149 | + public abstract static class CallNode extends PythonVarargsBuiltinNode { |
| 150 | + @Specialization |
| 151 | + protected static Object doIt(VirtualFrame frame, PDecoratedMethod self, Object[] arguments, PKeyword[] keywords, |
| 152 | + @Cached CallNode callNode) { |
| 153 | + return callNode.execute(frame, self.getCallable(), arguments, keywords); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public Object varArgExecute(VirtualFrame frame, @SuppressWarnings("unused") Object self, Object[] arguments, PKeyword[] keywords) throws VarargsBuiltinDirectInvocationNotSupported { |
| 158 | + Object[] argsWithoutSelf = new Object[arguments.length - 1]; |
| 159 | + PythonUtils.arraycopy(arguments, 1, argsWithoutSelf, 0, argsWithoutSelf.length); |
| 160 | + return execute(frame, arguments[0], argsWithoutSelf, keywords); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + @Builtin(name = __GET__, minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3) |
| 165 | + @GenerateNodeFactory |
| 166 | + public abstract static class GetNode extends PythonTernaryBuiltinNode { |
| 167 | + @Specialization |
| 168 | + Object doGeneric(PDecoratedMethod self, Object obj, @SuppressWarnings("unused") Object cls) { |
| 169 | + if (obj == null || obj == PNone.NONE) { |
| 170 | + return self.getCallable(); |
| 171 | + } |
| 172 | + return factory().createMethod(obj, self.getCallable()); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + @Builtin(name = __REPR__, minNumOfPositionalArgs = 1) |
| 177 | + @GenerateNodeFactory |
| 178 | + abstract static class ReprNode extends PythonUnaryBuiltinNode { |
| 179 | + @Specialization |
| 180 | + static Object reprBuiltinFunction(VirtualFrame frame, PDecoratedMethod self, |
| 181 | + @Cached PyObjectGetAttr getNameNode) { |
| 182 | + return PythonUtils.format("<instancemethod %s at 0x%x>", getNameNode.execute(frame, self.getCallable(), __NAME__), PythonAbstractObject.systemHashCode(self.getCallable())); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments