46
46
47
47
import java .io .IOException ;
48
48
import java .util .List ;
49
+ import java .util .Map ;
49
50
51
+ import com .oracle .graal .python .PythonLanguage ;
50
52
import com .oracle .graal .python .builtins .Builtin ;
51
53
import com .oracle .graal .python .builtins .CoreFunctions ;
52
54
import com .oracle .graal .python .builtins .PythonBuiltins ;
68
70
import com .oracle .truffle .api .interop .UnknownIdentifierException ;
69
71
import com .oracle .truffle .api .interop .UnsupportedMessageException ;
70
72
import com .oracle .truffle .api .interop .UnsupportedTypeException ;
73
+ import com .oracle .truffle .api .nodes .LanguageInfo ;
71
74
import com .oracle .truffle .api .nodes .Node ;
72
75
import 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 ;
74
78
75
79
@ CoreFunctions (defineModule = "polyglot" )
76
80
public final class InteropModuleBuiltins extends PythonBuiltins {
@@ -99,20 +103,33 @@ public Object importSymbol(String name) {
99
103
abstract static class EvalInteropNode extends PythonBuiltinNode {
100
104
@ TruffleBoundary
101
105
@ 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 ();
103
108
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 ();
105
116
} catch (RuntimeException e ) {
106
117
throw raise (NotImplementedError , e .getMessage ());
107
118
}
108
119
}
109
120
110
121
@ TruffleBoundary
111
122
@ Specialization
112
- Object evalFile (String path , @ SuppressWarnings ("unused" ) PNone string , String lang ) {
123
+ Object evalFile (String path , @ SuppressWarnings ("unused" ) PNone string , String langOrMimeType ) {
113
124
Env env = getContext ().getEnv ();
114
125
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 ();
116
133
} catch (IOException e ) {
117
134
throw raise (OSError , "%s" , e );
118
135
} catch (RuntimeException e ) {
@@ -125,7 +142,7 @@ Object evalFile(String path, @SuppressWarnings("unused") PNone string, String la
125
142
Object evalFile (String path , @ SuppressWarnings ("unused" ) PNone string , @ SuppressWarnings ("unused" ) PNone lang ) {
126
143
Env env = getContext ().getEnv ();
127
144
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 ();
129
146
} catch (IOException e ) {
130
147
throw raise (OSError , "%s" , e );
131
148
} catch (RuntimeException e ) {
@@ -145,15 +162,19 @@ Object evalWithoutContent(Object path, Object string, Object lang) {
145
162
throw raise (ValueError , "polyglot.eval must pass strings as either 'path' or a 'string' keyword" );
146
163
}
147
164
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
+ }
155
172
}
156
- return builder ;
173
+ return null ;
174
+ }
175
+
176
+ protected boolean isMimeType (String lang ) {
177
+ return lang .contains ("/" );
157
178
}
158
179
}
159
180
0 commit comments