|
26 | 26 |
|
27 | 27 | package com.oracle.graal.python.builtins.objects.function;
|
28 | 28 |
|
| 29 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__; |
29 | 30 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__GET__;
|
30 | 31 |
|
31 | 32 | import java.util.List;
|
|
36 | 37 | import com.oracle.graal.python.builtins.objects.PNone;
|
37 | 38 | import com.oracle.graal.python.builtins.objects.method.PBuiltinMethod;
|
38 | 39 | import com.oracle.graal.python.builtins.objects.method.PMethod;
|
| 40 | +import com.oracle.graal.python.builtins.objects.str.PString; |
| 41 | +import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode; |
| 42 | +import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode; |
39 | 43 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
| 44 | +import com.oracle.graal.python.nodes.function.PythonBuiltinNode; |
40 | 45 | import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
|
| 46 | +import com.oracle.graal.python.runtime.exception.PythonErrorType; |
| 47 | +import com.oracle.truffle.api.CompilerDirectives; |
| 48 | +import com.oracle.truffle.api.dsl.Cached; |
| 49 | +import com.oracle.truffle.api.dsl.Fallback; |
41 | 50 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
42 | 51 | import com.oracle.truffle.api.dsl.NodeFactory;
|
43 | 52 | import com.oracle.truffle.api.dsl.Specialization;
|
@@ -84,4 +93,54 @@ protected Object doBuiltinFunction(PBuiltinFunction self, PNone instance, Object
|
84 | 93 | return self;
|
85 | 94 | }
|
86 | 95 | }
|
| 96 | + |
| 97 | + @Builtin(name = __NAME__, minNumOfArguments = 1, maxNumOfArguments = 2, isGetter = true, isSetter = true) |
| 98 | + @GenerateNodeFactory |
| 99 | + abstract static class NameNode extends PythonBuiltinNode { |
| 100 | + @Child WriteAttributeToObjectNode writeNode; |
| 101 | + |
| 102 | + @Specialization(guards = "isNoValue(noValue)") |
| 103 | + Object getName(PFunction self, @SuppressWarnings("unused") PNone noValue, |
| 104 | + @Cached("create()") ReadAttributeFromObjectNode readNode) { |
| 105 | + Object storedName = readNode.execute(self, __NAME__); |
| 106 | + if (storedName == PNone.NO_VALUE) { |
| 107 | + return self.getName(); |
| 108 | + } else { |
| 109 | + return storedName; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Specialization(guards = "isNoValue(noValue)") |
| 114 | + Object getName(PBuiltinFunction self, @SuppressWarnings("unused") PNone noValue) { |
| 115 | + return self.getName(); |
| 116 | + } |
| 117 | + |
| 118 | + @Specialization |
| 119 | + Object setName(PFunction self, String value) { |
| 120 | + if (writeNode == null) { |
| 121 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 122 | + writeNode = insert(WriteAttributeToObjectNode.create()); |
| 123 | + } |
| 124 | + writeNode.execute(self, __NAME__, value); |
| 125 | + return PNone.NONE; |
| 126 | + } |
| 127 | + |
| 128 | + @Specialization |
| 129 | + Object setName(PFunction self, PString value) { |
| 130 | + return setName(self, value.getValue()); |
| 131 | + } |
| 132 | + |
| 133 | + @SuppressWarnings("unused") |
| 134 | + @Specialization |
| 135 | + Object setName(PBuiltinFunction self, Object value) { |
| 136 | + throw raise(PythonErrorType.AttributeError, "attribute '__name__' of builtin function is not writable"); |
| 137 | + } |
| 138 | + |
| 139 | + @SuppressWarnings("unused") |
| 140 | + @Fallback |
| 141 | + Object setName(Object self, Object value) { |
| 142 | + throw raise(PythonErrorType.TypeError, "__name__ must be set to a string object"); |
| 143 | + } |
| 144 | + } |
| 145 | + |
87 | 146 | }
|
0 commit comments