4646
4747import java .io .IOException ;
4848import java .util .List ;
49+ import java .util .Map ;
4950
51+ import com .oracle .graal .python .PythonLanguage ;
5052import com .oracle .graal .python .builtins .Builtin ;
5153import com .oracle .graal .python .builtins .CoreFunctions ;
5254import com .oracle .graal .python .builtins .PythonBuiltins ;
6870import com .oracle .truffle .api .interop .UnknownIdentifierException ;
6971import com .oracle .truffle .api .interop .UnsupportedMessageException ;
7072import com .oracle .truffle .api .interop .UnsupportedTypeException ;
73+ import com .oracle .truffle .api .nodes .LanguageInfo ;
7174import com .oracle .truffle .api .nodes .Node ;
7275import com .oracle .truffle .api .source .Source ;
73- import com .oracle .truffle .api .source .Source .Builder ;
76+ import com .oracle .truffle .api .source .Source .LiteralBuilder ;
77+ import com .oracle .truffle .api .source .Source .SourceBuilder ;
7478
7579@ CoreFunctions (defineModule = "polyglot" )
7680public final class InteropModuleBuiltins extends PythonBuiltins {
@@ -99,20 +103,33 @@ public Object importSymbol(String name) {
99103 abstract static class EvalInteropNode extends PythonBuiltinNode {
100104 @ TruffleBoundary
101105 @ Specialization
102- Object evalString (@ SuppressWarnings ("unused" ) PNone path , String value , String lang ) {
106+ Object evalString (@ SuppressWarnings ("unused" ) PNone path , String value , String langOrMimeType ) {
107+ Env env = getContext ().getEnv ();
103108 try {
104- return getContext ().getEnv ().parse (builderWithMimeType (lang , Source .newBuilder (value ).name (value )).build ()).call ();
109+ boolean mimeType = isMimeType (langOrMimeType );
110+ String lang = mimeType ? findLanguageByMimeType (env , langOrMimeType ) : langOrMimeType ;
111+ LiteralBuilder newBuilder = Source .newBuilder (lang , value , value );
112+ if (mimeType ) {
113+ newBuilder = newBuilder .mimeType (langOrMimeType );
114+ }
115+ return env .parse (newBuilder .build ()).call ();
105116 } catch (RuntimeException e ) {
106117 throw raise (NotImplementedError , e .getMessage ());
107118 }
108119 }
109120
110121 @ TruffleBoundary
111122 @ Specialization
112- Object evalFile (String path , @ SuppressWarnings ("unused" ) PNone string , String lang ) {
123+ Object evalFile (String path , @ SuppressWarnings ("unused" ) PNone string , String langOrMimeType ) {
113124 Env env = getContext ().getEnv ();
114125 try {
115- return getContext ().getEnv ().parse (builderWithMimeType (lang , env .newSourceBuilder (env .getTruffleFile (path )).name (path )).build ()).call ();
126+ boolean mimeType = isMimeType (langOrMimeType );
127+ String lang = mimeType ? findLanguageByMimeType (env , langOrMimeType ) : langOrMimeType ;
128+ SourceBuilder newBuilder = Source .newBuilder (lang , env .getTruffleFile (path ));
129+ if (mimeType ) {
130+ newBuilder = newBuilder .mimeType (langOrMimeType );
131+ }
132+ return getContext ().getEnv ().parse (newBuilder .name (path ).build ()).call ();
116133 } catch (IOException e ) {
117134 throw raise (OSError , "%s" , e );
118135 } catch (RuntimeException e ) {
@@ -125,7 +142,7 @@ Object evalFile(String path, @SuppressWarnings("unused") PNone string, String la
125142 Object evalFile (String path , @ SuppressWarnings ("unused" ) PNone string , @ SuppressWarnings ("unused" ) PNone lang ) {
126143 Env env = getContext ().getEnv ();
127144 try {
128- return getContext ().getEnv ().parse (env . newSourceBuilder ( env .getTruffleFile (path )).name (path ).build ()).call ();
145+ return getContext ().getEnv ().parse (Source . newBuilder ( PythonLanguage . ID , env .getTruffleFile (path )).name (path ).build ()).call ();
129146 } catch (IOException e ) {
130147 throw raise (OSError , "%s" , e );
131148 } catch (RuntimeException e ) {
@@ -145,15 +162,19 @@ Object evalWithoutContent(Object path, Object string, Object lang) {
145162 throw raise (ValueError , "polyglot.eval must pass strings as either 'path' or a 'string' keyword" );
146163 }
147164
148- private static < T extends Exception , T2 extends Exception > Builder < T , RuntimeException , RuntimeException > builderWithMimeType ( String lang ,
149- Builder < T , T2 , RuntimeException > baseBuilder ) {
150- Builder < T , RuntimeException , RuntimeException > builder ;
151- if ( lang . contains ( "/" )) {
152- builder = baseBuilder . mimeType ( lang );
153- } else {
154- builder = baseBuilder . language ( lang );
165+ @ TruffleBoundary ( transferToInterpreterOnException = false )
166+ private static String findLanguageByMimeType ( Env env , String mimeType ) {
167+ Map < String , LanguageInfo > languages = env . getLanguages () ;
168+ for ( String registeredMimeType : languages . keySet ( )) {
169+ if ( mimeType . equals ( registeredMimeType )) {
170+ return languages . get ( registeredMimeType ). getId ();
171+ }
155172 }
156- return builder ;
173+ return null ;
174+ }
175+
176+ protected boolean isMimeType (String lang ) {
177+ return lang .contains ("/" );
157178 }
158179 }
159180
0 commit comments