11/*
2- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * The Universal Permissive License (UPL), Version 1.0
4545import java .util .List ;
4646import java .util .Set ;
4747
48+ import com .oracle .graal .python .PythonLanguage ;
4849import com .oracle .graal .python .builtins .objects .function .Arity ;
4950import com .oracle .graal .python .builtins .objects .object .PythonBuiltinObject ;
5051import com .oracle .graal .python .builtins .objects .type .LazyPythonClass ;
5354import com .oracle .graal .python .nodes .argument .ReadKeywordNode ;
5455import com .oracle .graal .python .nodes .argument .ReadVarArgsNode ;
5556import com .oracle .graal .python .nodes .argument .ReadVarKeywordsNode ;
57+ import com .oracle .graal .python .nodes .control .TopLevelExceptionHandler ;
5658import com .oracle .graal .python .nodes .frame .FrameSlotIDs ;
5759import com .oracle .graal .python .nodes .frame .WriteIdentifierNode ;
5860import com .oracle .graal .python .nodes .function .FunctionRootNode ;
5961import com .oracle .graal .python .nodes .generator .GeneratorFunctionRootNode ;
62+ import com .oracle .graal .python .runtime .PythonContext ;
6063import com .oracle .graal .python .runtime .PythonCore ;
61- import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
64+ import com .oracle .graal .python .runtime .PythonParser ;
65+ import com .oracle .graal .python .runtime .exception .PException ;
6266import com .oracle .truffle .api .CompilerDirectives ;
67+ import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
6368import com .oracle .truffle .api .RootCallTarget ;
6469import com .oracle .truffle .api .Truffle ;
6570import com .oracle .truffle .api .nodes .Node ;
6671import com .oracle .truffle .api .nodes .NodeUtil ;
6772import com .oracle .truffle .api .nodes .RootNode ;
73+ import com .oracle .truffle .api .source .Source ;
6874import com .oracle .truffle .api .source .SourceSection ;
75+ import org .graalvm .polyglot .io .ByteSequence ;
6976
7077public final class PCode extends PythonBuiltinObject {
7178 private final long FLAG_POS_GENERATOR = 5 ;
@@ -158,6 +165,8 @@ private static String[] extractFreeVars(RootNode rootNode) {
158165 return ((FunctionRootNode ) rootNode ).getFreeVars ();
159166 } else if (rootNode instanceof GeneratorFunctionRootNode ) {
160167 return ((GeneratorFunctionRootNode ) rootNode ).getFreeVars ();
168+ } else if (rootNode instanceof ModuleRootNode ) {
169+ return ((ModuleRootNode ) rootNode ).getFreeVars ();
161170 } else {
162171 return null ;
163172 }
@@ -168,6 +177,8 @@ private static String[] extractCellVars(RootNode rootNode) {
168177 return ((FunctionRootNode ) rootNode ).getCellVars ();
169178 } else if (rootNode instanceof GeneratorFunctionRootNode ) {
170179 return ((GeneratorFunctionRootNode ) rootNode ).getCellVars ();
180+ } else if (rootNode instanceof ModuleRootNode ) {
181+ return new String [0 ];
171182 } else {
172183 return null ;
173184 }
@@ -189,13 +200,16 @@ private static String extractFileName(RootNode rootNode) {
189200 private static int extractFirstLineno (RootNode rootNode ) {
190201 RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode ) ? ((GeneratorFunctionRootNode ) rootNode ).getFunctionRootNode () : rootNode ;
191202 SourceSection sourceSection = funcRootNode .getSourceSection ();
192- return (sourceSection != null ) ? sourceSection .getStartLine () : 1 ;
203+ if (sourceSection != null ) {
204+ return sourceSection .getStartLine ();
205+ }
206+ return 1 ;
193207 }
194208
195209 private static String extractName (RootNode rootNode ) {
196210 String name ;
197211 if (rootNode instanceof ModuleRootNode ) {
198- name = "<module>" ;
212+ name = rootNode . getName () ;
199213 } else if (rootNode instanceof FunctionRootNode ) {
200214 name = ((FunctionRootNode ) rootNode ).getFunctionName ();
201215 } else {
@@ -299,6 +313,18 @@ private void extractArgStats() {
299313 this .nlocals = varnamesSet .size ();
300314 }
301315
316+ @ TruffleBoundary
317+ public void extractCodeString (RootNode rootNode ) {
318+ RootNode funcRootNode = rootNode ;
319+ if (rootNode instanceof GeneratorFunctionRootNode ) {
320+ funcRootNode = ((GeneratorFunctionRootNode ) rootNode ).getFunctionRootNode ();
321+ }
322+ SourceSection sourceSection = funcRootNode .getSourceSection ();
323+ if (sourceSection != null ) {
324+ this .codestring = sourceSection .getCharacters ().toString ().getBytes ();
325+ }
326+ }
327+
302328 public RootNode getRootNode () {
303329 return rootNode ;
304330 }
@@ -389,6 +415,9 @@ public Object[] getVarnames() {
389415 }
390416
391417 public byte [] getCodestring () {
418+ if (codestring == null && hasRootNode ()) {
419+ extractCodeString (getRootNode ());
420+ }
392421 return codestring ;
393422 }
394423
0 commit comments