|
67 | 67 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
|
68 | 68 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__;
|
69 | 69 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__SUBCLASSCHECK__;
|
70 |
| -import static com.oracle.graal.python.nodes.frame.FrameSlotIDs.RETURN_SLOT_ID; |
71 | 70 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
|
72 | 71 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
|
73 | 72 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
|
|
106 | 105 | import com.oracle.graal.python.builtins.objects.function.Signature;
|
107 | 106 | import com.oracle.graal.python.builtins.objects.generator.PGenerator;
|
108 | 107 | import com.oracle.graal.python.builtins.objects.ints.PInt;
|
109 |
| -import com.oracle.graal.python.builtins.objects.list.PList; |
110 | 108 | import com.oracle.graal.python.builtins.objects.method.PMethod;
|
111 | 109 | import com.oracle.graal.python.builtins.objects.module.PythonModule;
|
112 | 110 | import com.oracle.graal.python.builtins.objects.object.PythonObject;
|
|
134 | 132 | import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
|
135 | 133 | import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
|
136 | 134 | import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
|
137 |
| -import com.oracle.graal.python.nodes.builtins.ListNodes.AppendNode; |
| 135 | +import com.oracle.graal.python.nodes.builtins.ListNodes; |
| 136 | +import com.oracle.graal.python.nodes.builtins.ListNodes.ConstructListContextManager; |
138 | 137 | import com.oracle.graal.python.nodes.call.CallNode;
|
139 | 138 | import com.oracle.graal.python.nodes.call.GenericInvokeNode;
|
140 | 139 | import com.oracle.graal.python.nodes.call.PythonCallNode;
|
|
188 | 187 | import com.oracle.truffle.api.dsl.Specialization;
|
189 | 188 | import com.oracle.truffle.api.dsl.TypeSystemReference;
|
190 | 189 | import com.oracle.truffle.api.frame.Frame;
|
191 |
| -import com.oracle.truffle.api.frame.FrameDescriptor; |
192 |
| -import com.oracle.truffle.api.frame.FrameSlot; |
193 | 190 | import com.oracle.truffle.api.frame.VirtualFrame;
|
194 | 191 | import com.oracle.truffle.api.interop.UnsupportedMessageException;
|
195 | 192 | import com.oracle.truffle.api.library.CachedLibrary;
|
@@ -521,41 +518,29 @@ Object hash(VirtualFrame frame, Object object,
|
521 | 518 | @Builtin(name = DIR, minNumOfPositionalArgs = 0, maxNumOfPositionalArgs = 1)
|
522 | 519 | @GenerateNodeFactory
|
523 | 520 | public abstract static class DirNode extends PythonBuiltinNode {
|
524 |
| - @Child private AppendNode appendNode; |
525 | 521 |
|
| 522 | + // logic like in 'Objects/object.c: _dir_locals' |
526 | 523 | @Specialization(guards = "isNoValue(object)")
|
527 |
| - @SuppressWarnings("unused") |
528 |
| - public Object dir(VirtualFrame frame, Object object) { |
529 |
| - PList locals = factory().createList(); |
530 |
| - FrameDescriptor frameDescriptor = frame.getFrameDescriptor(); |
531 |
| - addIdsFromDescriptor(locals, frameDescriptor); |
532 |
| - return locals; |
533 |
| - } |
| 524 | + Object locals(VirtualFrame frame, @SuppressWarnings("unused") Object object, |
| 525 | + @Cached ReadLocalsNode readLocalsNode, |
| 526 | + @Cached ReadCallerFrameNode readCallerFrameNode, |
| 527 | + @Cached MaterializeFrameNode materializeNode, |
| 528 | + @Cached("createBinaryProfile()") ConditionProfile inGenerator, |
| 529 | + @Cached("create(KEYS)") LookupAndCallUnaryNode callKeysNode, |
| 530 | + @Cached ListNodes.ConstructListNode constructListNode) { |
534 | 531 |
|
535 |
| - @TruffleBoundary |
536 |
| - private void addIdsFromDescriptor(PList locals, FrameDescriptor frameDescriptor) { |
537 |
| - for (FrameSlot slot : frameDescriptor.getSlots()) { |
538 |
| - // XXX: remove this special case |
539 |
| - if (slot.getIdentifier().equals(RETURN_SLOT_ID)) { |
540 |
| - continue; |
541 |
| - } |
542 |
| - getAppendNode().execute(locals, slot.getIdentifier()); |
| 532 | + Object localsDict = LocalsNode.getLocalsDict(frame, this, readLocalsNode, readCallerFrameNode, materializeNode, inGenerator); |
| 533 | + Object keysObj = callKeysNode.executeObject(frame, localsDict); |
| 534 | + try (ConstructListContextManager cm = constructListNode.withGlobalState(getContextRef(), frame)) { |
| 535 | + return cm.execute(keysObj); |
543 | 536 | }
|
544 | 537 | }
|
545 | 538 |
|
546 | 539 | @Specialization(guards = "!isNoValue(object)")
|
547 |
| - public Object dir(VirtualFrame frame, Object object, |
| 540 | + Object dir(VirtualFrame frame, Object object, |
548 | 541 | @Cached("create(__DIR__)") LookupAndCallUnaryNode dirNode) {
|
549 | 542 | return dirNode.executeObject(frame, object);
|
550 | 543 | }
|
551 |
| - |
552 |
| - private AppendNode getAppendNode() { |
553 |
| - if (appendNode == null) { |
554 |
| - CompilerDirectives.transferToInterpreterAndInvalidate(); |
555 |
| - appendNode = insert(AppendNode.create()); |
556 |
| - } |
557 |
| - return appendNode; |
558 |
| - } |
559 | 544 | }
|
560 | 545 |
|
561 | 546 | // divmod(a, b)
|
@@ -1973,19 +1958,24 @@ public Object globals(VirtualFrame frame,
|
1973 | 1958 | @Builtin(name = "locals", minNumOfPositionalArgs = 0)
|
1974 | 1959 | @GenerateNodeFactory
|
1975 | 1960 | abstract static class LocalsNode extends PythonBuiltinNode {
|
1976 |
| - private final ConditionProfile inGenerator = ConditionProfile.createBinaryProfile(); |
1977 | 1961 |
|
1978 | 1962 | @Specialization
|
1979 |
| - public Object locals(VirtualFrame frame, |
| 1963 | + Object locals(VirtualFrame frame, |
1980 | 1964 | @Cached ReadLocalsNode readLocalsNode,
|
1981 | 1965 | @Cached ReadCallerFrameNode readCallerFrameNode,
|
1982 |
| - @Cached MaterializeFrameNode materializeNode) { |
| 1966 | + @Cached MaterializeFrameNode materializeNode, |
| 1967 | + @Cached("createBinaryProfile()") ConditionProfile inGenerator) { |
| 1968 | + return getLocalsDict(frame, this, readLocalsNode, readCallerFrameNode, materializeNode, inGenerator); |
| 1969 | + } |
| 1970 | + |
| 1971 | + static Object getLocalsDict(VirtualFrame frame, Node n, ReadLocalsNode readLocalsNode, ReadCallerFrameNode readCallerFrameNode, MaterializeFrameNode materializeNode, |
| 1972 | + ConditionProfile inGenerator) { |
1983 | 1973 | PFrame callerFrame = readCallerFrameNode.executeWith(frame, 0);
|
1984 | 1974 | Frame generatorFrame = PArguments.getGeneratorFrame(callerFrame.getArguments());
|
1985 | 1975 | if (inGenerator.profile(generatorFrame == null)) {
|
1986 | 1976 | return readLocalsNode.execute(frame, callerFrame);
|
1987 | 1977 | } else {
|
1988 |
| - return readLocalsNode.execute(frame, materializeNode.execute(frame, this, false, false, generatorFrame)); |
| 1978 | + return readLocalsNode.execute(frame, materializeNode.execute(frame, n, false, false, generatorFrame)); |
1989 | 1979 | }
|
1990 | 1980 | }
|
1991 | 1981 | }
|
|
0 commit comments