|
48 | 48 |
|
49 | 49 | import com.oracle.graal.python.PythonLanguage;
|
50 | 50 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
| 51 | +import com.oracle.graal.python.builtins.objects.PNone; |
51 | 52 | import com.oracle.graal.python.builtins.objects.cell.PCell;
|
52 | 53 | import com.oracle.graal.python.builtins.objects.common.HashingStorage;
|
53 | 54 | import com.oracle.graal.python.builtins.objects.dict.PDict;
|
|
75 | 76 | import com.oracle.truffle.api.frame.FrameSlot;
|
76 | 77 | import com.oracle.truffle.api.frame.FrameSlotKind;
|
77 | 78 | import com.oracle.truffle.api.frame.MaterializedFrame;
|
| 79 | +import com.oracle.truffle.api.frame.VirtualFrame; |
78 | 80 | import com.oracle.truffle.api.nodes.Node;
|
79 | 81 | import com.oracle.truffle.api.nodes.NodeUtil;
|
80 | 82 | import com.oracle.truffle.api.nodes.RootNode;
|
@@ -158,60 +160,69 @@ public PCode(LazyPythonClass cls, int argcount, int kwonlyargcount,
|
158 | 160 |
|
159 | 161 | // Derive a new call target from the code string, if we can
|
160 | 162 | RootNode rootNode = null;
|
161 |
| - if ((flags & FLAG_MODULE) == 0) { |
162 |
| - // we're looking for the function, not the module |
163 |
| - String funcdef; |
164 |
| - if (freevars.length > 0) { |
165 |
| - // we build an outer function to provide the initial scoping |
166 |
| - String outernme = "_____" + System.nanoTime(); |
167 |
| - StringBuilder sb = new StringBuilder(); |
168 |
| - sb.append("def ").append(outernme).append("():\n"); |
169 |
| - for (Object freevar : freevars) { |
170 |
| - String v; |
171 |
| - if (freevar instanceof PString) { |
172 |
| - v = ((PString) freevar).getValue(); |
173 |
| - } else if (freevar instanceof String) { |
174 |
| - v = (String) freevar; |
175 |
| - } else { |
176 |
| - continue; |
| 163 | + if (codestring.length > 0) { |
| 164 | + if ((flags & FLAG_MODULE) == 0) { |
| 165 | + // we're looking for the function, not the module |
| 166 | + String funcdef; |
| 167 | + if (freevars.length > 0) { |
| 168 | + // we build an outer function to provide the initial scoping |
| 169 | + String outernme = "_____" + System.nanoTime(); |
| 170 | + StringBuilder sb = new StringBuilder(); |
| 171 | + sb.append("def ").append(outernme).append("():\n"); |
| 172 | + for (Object freevar : freevars) { |
| 173 | + String v; |
| 174 | + if (freevar instanceof PString) { |
| 175 | + v = ((PString) freevar).getValue(); |
| 176 | + } else if (freevar instanceof String) { |
| 177 | + v = (String) freevar; |
| 178 | + } else { |
| 179 | + continue; |
| 180 | + } |
| 181 | + sb.append(" ").append(v).append(" = None\n"); |
177 | 182 | }
|
178 |
| - sb.append(" ").append(v).append(" = None\n"); |
| 183 | + sb.append(" global ").append(name).append("\n"); |
| 184 | + sb.append(" ").append(new String(codestring)); |
| 185 | + sb.append("\n\n").append(outernme).append("()"); |
| 186 | + funcdef = sb.toString(); |
| 187 | + } else { |
| 188 | + funcdef = new String(codestring); |
179 | 189 | }
|
180 |
| - sb.append(" global ").append(name).append("\n"); |
181 |
| - sb.append(" ").append(new String(codestring)); |
182 |
| - sb.append("\n\n").append(outernme).append("()"); |
183 |
| - funcdef = sb.toString(); |
184 |
| - } else { |
185 |
| - funcdef = new String(codestring); |
186 |
| - } |
187 | 190 |
|
188 |
| - rootNode = (RootNode) PythonLanguage.getCore().getParser().parse(ParserMode.File, PythonLanguage.getCore(), Source.newBuilder("python", funcdef, name).build(), null); |
189 |
| - Object[] args = PArguments.create(); |
190 |
| - PDict globals = PythonLanguage.getCore().factory().createDict(); |
191 |
| - PArguments.setGlobals(args, globals); |
192 |
| - Truffle.getRuntime().createCallTarget(rootNode).call(args); |
193 |
| - Object function = globals.getDictStorage().getItem(name, HashingStorage.getSlowPathEquivalence(name)); |
194 |
| - if (function instanceof PFunction) { |
195 |
| - rootNode = ((PFunction) function).getFunctionRootNode(); |
| 191 | + rootNode = (RootNode) PythonLanguage.getCore().getParser().parse(ParserMode.File, PythonLanguage.getCore(), Source.newBuilder("python", funcdef, name).build(), null); |
| 192 | + Object[] args = PArguments.create(); |
| 193 | + PDict globals = PythonLanguage.getCore().factory().createDict(); |
| 194 | + PArguments.setGlobals(args, globals); |
| 195 | + Truffle.getRuntime().createCallTarget(rootNode).call(args); |
| 196 | + Object function = globals.getDictStorage().getItem(name, HashingStorage.getSlowPathEquivalence(name)); |
| 197 | + if (function instanceof PFunction) { |
| 198 | + rootNode = ((PFunction) function).getFunctionRootNode(); |
| 199 | + } else { |
| 200 | + throw PythonLanguage.getCore().raise(PythonBuiltinClassType.ValueError, "got an invalid codestring trying to create a function code object"); |
| 201 | + } |
196 | 202 | } else {
|
197 |
| - throw PythonLanguage.getCore().raise(PythonBuiltinClassType.ValueError, "got an invalid codestring trying to create a function code object"); |
| 203 | + MaterializedFrame frame = null; |
| 204 | + if (freevars.length > 0) { |
| 205 | + FrameDescriptor frameDescriptor = new FrameDescriptor(); |
| 206 | + frame = Truffle.getRuntime().createMaterializedFrame(new Object[0], frameDescriptor); |
| 207 | + for (int i = 0; i < freevars.length; i++) { |
| 208 | + Object ident = freevars[i]; |
| 209 | + FrameSlot slot = frameDescriptor.addFrameSlot(ident); |
| 210 | + frameDescriptor.setFrameSlotKind(slot, FrameSlotKind.Object); |
| 211 | + frame.setObject(slot, new PCell()); |
| 212 | + } |
| 213 | + } |
| 214 | + rootNode = (RootNode) PythonLanguage.getCore().getParser().parse(ParserMode.File, PythonLanguage.getCore(), Source.newBuilder("python", new String(codestring), name).build(), frame); |
| 215 | + assert rootNode instanceof ModuleRootNode; |
198 | 216 | }
|
| 217 | + this.callTarget = Truffle.getRuntime().createCallTarget(rootNode); |
199 | 218 | } else {
|
200 |
| - MaterializedFrame frame = null; |
201 |
| - if (freevars.length > 0) { |
202 |
| - FrameDescriptor frameDescriptor = new FrameDescriptor(); |
203 |
| - frame = Truffle.getRuntime().createMaterializedFrame(new Object[0], frameDescriptor); |
204 |
| - for (int i = 0; i < freevars.length; i++) { |
205 |
| - Object ident = freevars[i]; |
206 |
| - FrameSlot slot = frameDescriptor.addFrameSlot(ident); |
207 |
| - frameDescriptor.setFrameSlotKind(slot, FrameSlotKind.Object); |
208 |
| - frame.setObject(slot, new PCell()); |
| 219 | + this.callTarget = Truffle.getRuntime().createCallTarget(new RootNode(PythonLanguage.getCurrent()) { |
| 220 | + @Override |
| 221 | + public Object execute(VirtualFrame frame) { |
| 222 | + return PNone.NONE; |
209 | 223 | }
|
210 |
| - } |
211 |
| - rootNode = (RootNode) PythonLanguage.getCore().getParser().parse(ParserMode.File, PythonLanguage.getCore(), Source.newBuilder("python", new String(codestring), name).build(), frame); |
212 |
| - assert rootNode instanceof ModuleRootNode; |
| 224 | + }); |
213 | 225 | }
|
214 |
| - this.callTarget = Truffle.getRuntime().createCallTarget(rootNode); |
215 | 226 |
|
216 | 227 | char paramNom = 'A';
|
217 | 228 | String[] paramNames = new String[argcount];
|
|
0 commit comments