74
74
import java .io .PrintStream ;
75
75
import java .io .PrintWriter ;
76
76
import java .math .BigInteger ;
77
+ import java .net .URI ;
77
78
import java .nio .CharBuffer ;
78
79
import java .util .List ;
79
80
import java .util .function .Supplier ;
156
157
import com .oracle .graal .python .nodes .truffle .PythonArithmeticTypes ;
157
158
import com .oracle .graal .python .nodes .util .CastToIntegerFromIndexNode ;
158
159
import com .oracle .graal .python .nodes .util .CastToStringNode ;
160
+ import com .oracle .graal .python .runtime .PythonContext ;
159
161
import com .oracle .graal .python .runtime .PythonCore ;
160
162
import com .oracle .graal .python .runtime .PythonOptions ;
161
163
import com .oracle .graal .python .runtime .PythonParser .ParserMode ;
167
169
import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
168
170
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
169
171
import com .oracle .truffle .api .Truffle ;
172
+ import com .oracle .truffle .api .TruffleFile ;
170
173
import com .oracle .truffle .api .dsl .Cached ;
171
174
import com .oracle .truffle .api .dsl .Fallback ;
172
175
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
@@ -546,7 +549,7 @@ private static BigInteger[] divideAndRemainder(PInt a, PInt b) {
546
549
@ GenerateNodeFactory
547
550
public abstract static class EvalNode extends PythonBuiltinNode {
548
551
protected final String funcname = "eval" ;
549
- @ Child protected CompileNode compileNode = CompileNode .create ();
552
+ @ Child protected CompileNode compileNode = CompileNode .create (false );
550
553
@ Child private IndirectCallNode indirectCallNode = IndirectCallNode .create ();
551
554
@ Child private HasInheritedAttributeNode hasGetItemNode ;
552
555
@@ -700,6 +703,19 @@ public final Object execute(VirtualFrame frame) {
700
703
@ GenerateNodeFactory
701
704
@ TypeSystemReference (PythonArithmeticTypes .class )
702
705
public abstract static class CompileNode extends PythonBuiltinNode {
706
+ /**
707
+ * Decides wether this node should attempt to map the filename to a URI for the benefit of
708
+ * Truffle tooling
709
+ */
710
+ private final boolean mapFilenameToUri ;
711
+
712
+ public CompileNode (boolean mapFilenameToUri ) {
713
+ this .mapFilenameToUri = mapFilenameToUri ;
714
+ }
715
+
716
+ public CompileNode () {
717
+ this .mapFilenameToUri = true ;
718
+ }
703
719
704
720
public abstract PCode execute (Object source , String filename , String mode , Object kwFlags , Object kwDontInherit , Object kwOptimize );
705
721
@@ -720,7 +736,9 @@ PCode compile(OpaqueBytes source, String filename, String mode, Object kwFlags,
720
736
@ Specialization
721
737
@ TruffleBoundary
722
738
PCode compile (String expression , String filename , String mode , Object kwFlags , Object kwDontInherit , Object kwOptimize ) {
723
- Source source = PythonLanguage .newSource (getContext (), expression , filename );
739
+ PythonContext context = getContext ();
740
+ URI uri = mapToUri (expression , filename , context );
741
+ Source source = PythonLanguage .newSource (context , expression , filename , uri );
724
742
ParserMode pm ;
725
743
if (mode .equals ("exec" )) {
726
744
pm = ParserMode .File ;
@@ -739,14 +757,35 @@ PCode compile(String expression, String filename, String mode, Object kwFlags, O
739
757
}
740
758
}
741
759
760
+ private URI mapToUri (String expression , String filename , PythonContext context ) {
761
+ if (mapFilenameToUri ) {
762
+ URI uri = null ;
763
+ try {
764
+ TruffleFile truffleFile = context .getEnv ().getTruffleFile (filename );
765
+ if (truffleFile .exists ()) {
766
+ // XXX: (tfel): We don't know if the expression has anything to do with the
767
+ // filename that's given. We would really have to compare the entire
768
+ // contents, but as a first approximation, we compare the content lengths
769
+ if (expression .length () == truffleFile .size ()) {
770
+ uri = truffleFile .toUri ();
771
+ }
772
+ }
773
+ } catch (SecurityException | IOException e ) {
774
+ }
775
+ return uri ;
776
+ } else {
777
+ return null ;
778
+ }
779
+ }
780
+
742
781
@ SuppressWarnings ("unused" )
743
782
@ Specialization
744
783
PCode compile (PCode code , String filename , String mode , Object flags , Object dontInherit , Object optimize ) {
745
784
return code ;
746
785
}
747
786
748
- public static CompileNode create () {
749
- return BuiltinFunctionsFactory .CompileNodeFactory .create (new ReadArgumentNode []{});
787
+ public static CompileNode create (boolean mapFilenameToUri ) {
788
+ return BuiltinFunctionsFactory .CompileNodeFactory .create (mapFilenameToUri , new ReadArgumentNode []{});
750
789
}
751
790
}
752
791
0 commit comments