1
1
/*
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.
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
45
45
import java .util .List ;
46
46
import java .util .Set ;
47
47
48
+ import com .oracle .graal .python .PythonLanguage ;
48
49
import com .oracle .graal .python .builtins .objects .function .Arity ;
49
50
import com .oracle .graal .python .builtins .objects .object .PythonBuiltinObject ;
50
51
import com .oracle .graal .python .builtins .objects .type .LazyPythonClass ;
53
54
import com .oracle .graal .python .nodes .argument .ReadKeywordNode ;
54
55
import com .oracle .graal .python .nodes .argument .ReadVarArgsNode ;
55
56
import com .oracle .graal .python .nodes .argument .ReadVarKeywordsNode ;
57
+ import com .oracle .graal .python .nodes .control .TopLevelExceptionHandler ;
56
58
import com .oracle .graal .python .nodes .frame .FrameSlotIDs ;
57
59
import com .oracle .graal .python .nodes .frame .WriteIdentifierNode ;
58
60
import com .oracle .graal .python .nodes .function .FunctionRootNode ;
59
61
import com .oracle .graal .python .nodes .generator .GeneratorFunctionRootNode ;
62
+ import com .oracle .graal .python .runtime .PythonContext ;
60
63
import 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 ;
62
66
import com .oracle .truffle .api .CompilerDirectives ;
67
+ import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
63
68
import com .oracle .truffle .api .RootCallTarget ;
64
69
import com .oracle .truffle .api .Truffle ;
65
70
import com .oracle .truffle .api .nodes .Node ;
66
71
import com .oracle .truffle .api .nodes .NodeUtil ;
67
72
import com .oracle .truffle .api .nodes .RootNode ;
73
+ import com .oracle .truffle .api .source .Source ;
68
74
import com .oracle .truffle .api .source .SourceSection ;
75
+ import org .graalvm .polyglot .io .ByteSequence ;
69
76
70
77
public final class PCode extends PythonBuiltinObject {
71
78
private final long FLAG_POS_GENERATOR = 5 ;
@@ -158,6 +165,8 @@ private static String[] extractFreeVars(RootNode rootNode) {
158
165
return ((FunctionRootNode ) rootNode ).getFreeVars ();
159
166
} else if (rootNode instanceof GeneratorFunctionRootNode ) {
160
167
return ((GeneratorFunctionRootNode ) rootNode ).getFreeVars ();
168
+ } else if (rootNode instanceof ModuleRootNode ) {
169
+ return ((ModuleRootNode ) rootNode ).getFreeVars ();
161
170
} else {
162
171
return null ;
163
172
}
@@ -168,6 +177,8 @@ private static String[] extractCellVars(RootNode rootNode) {
168
177
return ((FunctionRootNode ) rootNode ).getCellVars ();
169
178
} else if (rootNode instanceof GeneratorFunctionRootNode ) {
170
179
return ((GeneratorFunctionRootNode ) rootNode ).getCellVars ();
180
+ } else if (rootNode instanceof ModuleRootNode ) {
181
+ return new String [0 ];
171
182
} else {
172
183
return null ;
173
184
}
@@ -189,13 +200,16 @@ private static String extractFileName(RootNode rootNode) {
189
200
private static int extractFirstLineno (RootNode rootNode ) {
190
201
RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode ) ? ((GeneratorFunctionRootNode ) rootNode ).getFunctionRootNode () : rootNode ;
191
202
SourceSection sourceSection = funcRootNode .getSourceSection ();
192
- return (sourceSection != null ) ? sourceSection .getStartLine () : 1 ;
203
+ if (sourceSection != null ) {
204
+ return sourceSection .getStartLine ();
205
+ }
206
+ return 1 ;
193
207
}
194
208
195
209
private static String extractName (RootNode rootNode ) {
196
210
String name ;
197
211
if (rootNode instanceof ModuleRootNode ) {
198
- name = "<module>" ;
212
+ name = rootNode . getName () ;
199
213
} else if (rootNode instanceof FunctionRootNode ) {
200
214
name = ((FunctionRootNode ) rootNode ).getFunctionName ();
201
215
} else {
@@ -299,6 +313,18 @@ private void extractArgStats() {
299
313
this .nlocals = varnamesSet .size ();
300
314
}
301
315
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
+
302
328
public RootNode getRootNode () {
303
329
return rootNode ;
304
330
}
@@ -389,6 +415,9 @@ public Object[] getVarnames() {
389
415
}
390
416
391
417
public byte [] getCodestring () {
418
+ if (codestring == null && hasRootNode ()) {
419
+ extractCodeString (getRootNode ());
420
+ }
392
421
return codestring ;
393
422
}
394
423
0 commit comments