|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * The Universal Permissive License (UPL), Version 1.0
|
|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.nodes.builtins;
|
42 | 42 |
|
43 |
| -import java.util.ArrayList; |
44 |
| - |
45 | 43 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
46 | 44 | import com.oracle.graal.python.builtins.objects.PNone;
|
47 | 45 | import com.oracle.graal.python.builtins.objects.tuple.PTuple;
|
48 | 46 | import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
|
49 | 47 | import com.oracle.graal.python.nodes.PGuards;
|
50 | 48 | import com.oracle.graal.python.nodes.PNodeWithContext;
|
51 | 49 | import com.oracle.graal.python.nodes.SpecialMethodNames;
|
| 50 | +import com.oracle.graal.python.nodes.builtins.ListNodes.CreateStorageFromIteratorNode; |
52 | 51 | import com.oracle.graal.python.nodes.control.GetIteratorExpressionNode.GetIteratorNode;
|
53 |
| -import com.oracle.graal.python.nodes.control.GetNextNode; |
54 | 52 | import com.oracle.graal.python.nodes.object.GetLazyClassNode;
|
55 |
| -import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile; |
56 |
| -import com.oracle.graal.python.runtime.exception.PException; |
57 | 53 | import com.oracle.graal.python.runtime.object.PythonObjectFactory;
|
58 | 54 | import com.oracle.truffle.api.CompilerDirectives;
|
59 |
| -import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
60 | 55 | import com.oracle.truffle.api.dsl.Cached;
|
61 | 56 | import com.oracle.truffle.api.dsl.Fallback;
|
62 | 57 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
@@ -107,41 +102,16 @@ PTuple tuple(@SuppressWarnings("unused") LazyPythonClass cls, PTuple iterable) {
|
107 | 102 |
|
108 | 103 | @Specialization(guards = {"!isNoValue(iterable)", "createNewTuple(cls, iterable)"})
|
109 | 104 | PTuple tuple(VirtualFrame frame, LazyPythonClass cls, Object iterable,
|
110 |
| - @Cached("create()") GetIteratorNode getIterator, |
111 |
| - @Cached("create()") GetNextNode next, |
112 |
| - @Cached("create()") IsBuiltinClassProfile errorProfile) { |
113 |
| - |
114 |
| - Object iterator = getIterator.executeWith(frame, iterable); |
115 |
| - ArrayList<Object> internalStorage = makeList(); |
116 |
| - while (true) { |
117 |
| - try { |
118 |
| - addToList(internalStorage, next.execute(frame, iterator)); |
119 |
| - } catch (PException e) { |
120 |
| - e.expectStopIteration(errorProfile); |
121 |
| - return factory.createTuple(cls, listToArray(internalStorage)); |
122 |
| - } |
123 |
| - } |
124 |
| - } |
125 |
| - |
126 |
| - @TruffleBoundary |
127 |
| - private static ArrayList<Object> makeList() { |
128 |
| - return new ArrayList<>(); |
129 |
| - } |
130 |
| - |
131 |
| - @TruffleBoundary |
132 |
| - private static void addToList(ArrayList<Object> list, Object obj) { |
133 |
| - list.add(obj); |
134 |
| - } |
135 |
| - |
136 |
| - @TruffleBoundary |
137 |
| - private static Object[] listToArray(ArrayList<Object> list) { |
138 |
| - return list.toArray(); |
| 105 | + @Cached("create()") GetIteratorNode getIteratorNode, |
| 106 | + @Cached("create()") CreateStorageFromIteratorNode storageNode) { |
| 107 | + Object iterObj = getIteratorNode.executeWith(frame, iterable); |
| 108 | + return factory.createTuple(cls, storageNode.execute(frame, iterObj)); |
139 | 109 | }
|
140 | 110 |
|
141 | 111 | @Fallback
|
142 | 112 | public PTuple tuple(@SuppressWarnings("unused") LazyPythonClass cls, Object value) {
|
143 | 113 | CompilerDirectives.transferToInterpreter();
|
144 |
| - throw new RuntimeException("list does not support iterable object " + value); |
| 114 | + throw new RuntimeException("tuple does not support iterable object " + value); |
145 | 115 | }
|
146 | 116 |
|
147 | 117 | protected boolean createNewTuple(LazyPythonClass cls, Object iterable) {
|
|
0 commit comments