Skip to content

Commit 9791c5c

Browse files
committed
Migrate to 'SourceBuilder'.
1 parent a5ff174 commit 9791c5c

File tree

5 files changed

+55
-33
lines changed

5 files changed

+55
-33
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/PythonTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@
5858
import com.oracle.truffle.api.frame.FrameDescriptor;
5959
import com.oracle.truffle.api.frame.VirtualFrame;
6060
import com.oracle.truffle.api.nodes.RootNode;
61-
import com.oracle.truffle.api.source.MissingMIMETypeException;
62-
import com.oracle.truffle.api.source.MissingNameException;
6361
import com.oracle.truffle.api.source.Source;
64-
import com.oracle.truffle.api.source.Source.Builder;
62+
import com.oracle.truffle.api.source.Source.LiteralBuilder;
6563

6664
public class PythonTests {
6765
static {
@@ -260,12 +258,12 @@ public static RootNode getParseResult(com.oracle.truffle.api.source.Source sourc
260258

261259
public static RootNode getParseResult(String code) {
262260
final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
263-
Builder<RuntimeException, MissingMIMETypeException, MissingNameException> newBuilder = Source.newBuilder(code);
264-
newBuilder.mimeType(PythonLanguage.MIME_TYPE);
261+
262+
LiteralBuilder newBuilder = Source.newBuilder(PythonLanguage.ID, code, code);
265263
newBuilder.name("test");
266264
try {
267265
return PythonTests.getParseResult(newBuilder.build(), new PrintStream(byteArray), System.err);
268-
} catch (RuntimeException | MissingMIMETypeException | MissingNameException e) {
266+
} catch (RuntimeException e) {
269267
e.printStackTrace();
270268
return null;
271269
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/TestParserTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public TestParserTranslator() {
117117
}
118118

119119
RootNode parse(String src) {
120-
Source source = Source.newBuilder(src).mimeType(PythonLanguage.MIME_TYPE).name("foo").build();
120+
Source source = Source.newBuilder(PythonLanguage.ID, src, "foo").build();
121121
return (RootNode) context.getCore().getParser().parse(ParserMode.File, context.getCore(), source, null);
122122
}
123123

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
import com.oracle.truffle.api.nodes.Node;
7575
import com.oracle.truffle.api.nodes.RootNode;
7676
import com.oracle.truffle.api.source.Source;
77-
import com.oracle.truffle.api.source.Source.Builder;
77+
import com.oracle.truffle.api.source.Source.SourceBuilder;
7878

7979
@TruffleLanguage.Registration(id = PythonLanguage.ID, name = PythonLanguage.NAME, version = PythonLanguage.VERSION, mimeType = PythonLanguage.MIME_TYPE, interactive = true, internal = false, contextPolicy = TruffleLanguage.ContextPolicy.EXCLUSIVE)
8080
@ProvidedTags({StandardTags.CallTag.class, StandardTags.StatementTag.class, StandardTags.RootTag.class, StandardTags.TryBlockTag.class, DebuggerTags.AlwaysHalt.class})
@@ -366,24 +366,27 @@ public static TruffleLogger getLogger() {
366366
}
367367

368368
public static Source newSource(PythonContext ctxt, String src, String name) {
369-
return newSource(ctxt, Source.newBuilder(src), name);
369+
try {
370+
return newSource(ctxt, Source.newBuilder(ID, src, name), name);
371+
} catch (IOException e) {
372+
throw new AssertionError();
373+
}
370374
}
371375

372376
public static Source newSource(PythonContext ctxt, TruffleFile src, String name) throws IOException {
373-
return newSource(ctxt, ctxt.getEnv().newSourceBuilder(src), name);
377+
return newSource(ctxt, Source.newBuilder(ID, src), name);
374378
}
375379

376380
public static Source newSource(PythonContext ctxt, URL url, String name) throws IOException {
377-
return newSource(ctxt, Source.newBuilder(url), name);
381+
return newSource(ctxt, Source.newBuilder(ID, url), name);
378382
}
379383

380-
private static <E1 extends Exception, E2 extends Exception, E3 extends Exception> Source newSource(PythonContext ctxt, Builder<E1, E2, E3> srcBuilder,
381-
String name) throws E1 {
382-
Builder<E1, RuntimeException, RuntimeException> newBuilder = srcBuilder.name(name).mimeType(MIME_TYPE);
384+
private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder, String name) throws IOException {
385+
SourceBuilder newBuilder = srcBuilder.name(name).mimeType(MIME_TYPE);
383386
boolean coreIsInitialized = ctxt.getCore().isInitialized();
384387
boolean internal = !coreIsInitialized && !PythonOptions.getOption(ctxt, PythonOptions.ExposeInternalSources);
385388
if (internal) {
386-
srcBuilder.internal();
389+
srcBuilder.internal(true);
387390
}
388391
return newBuilder.build();
389392
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ImpModuleBuiltins.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
import com.oracle.truffle.api.interop.UnsupportedTypeException;
9494
import com.oracle.truffle.api.nodes.Node;
9595
import com.oracle.truffle.api.source.Source;
96-
import com.oracle.truffle.api.source.Source.Builder;
96+
import com.oracle.truffle.api.source.Source.SourceBuilder;
9797

9898
@CoreFunctions(defineModule = "_imp")
9999
public class ImpModuleBuiltins extends PythonBuiltins {
@@ -178,7 +178,7 @@ private Object loadDynamicModuleWithSpec(String name, String path, Node readNode
178178
String basename = name.substring(name.lastIndexOf('.') + 1);
179179
TruffleObject sulongLibrary;
180180
try {
181-
CallTarget callTarget = env.parse(env.newSourceBuilder(env.getTruffleFile(path)).language(LLVM_LANGUAGE).build());
181+
CallTarget callTarget = env.parse(Source.newBuilder(LLVM_LANGUAGE, env.getTruffleFile(path)).build());
182182
sulongLibrary = (TruffleObject) callTarget.call();
183183
} catch (SecurityException | IOException e) {
184184
throw raise(ImportError, "cannot load %s: %s", path, e.getMessage());
@@ -232,9 +232,9 @@ private void ensureCapiWasLoaded() {
232232
TruffleFile capiFile = env.getTruffleFile(PythonCore.getCoreHome(env) + PythonCore.FILE_SEPARATOR + "capi.bc");
233233
Object capi = null;
234234
try {
235-
Builder<IOException, RuntimeException, RuntimeException> capiSrcBuilder = env.newSourceBuilder(capiFile).language(LLVM_LANGUAGE);
235+
SourceBuilder capiSrcBuilder = Source.newBuilder(LLVM_LANGUAGE, capiFile);
236236
if (!PythonOptions.getOption(ctxt, PythonOptions.ExposeInternalSources)) {
237-
capiSrcBuilder.internal();
237+
capiSrcBuilder.internal(true);
238238
}
239239
capi = ctxt.getEnv().parse(capiSrcBuilder.build()).call();
240240
} catch (SecurityException | IOException e) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/InteropModuleBuiltins.java

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646

4747
import java.io.IOException;
4848
import java.util.List;
49+
import java.util.Map;
4950

51+
import com.oracle.graal.python.PythonLanguage;
5052
import com.oracle.graal.python.builtins.Builtin;
5153
import com.oracle.graal.python.builtins.CoreFunctions;
5254
import com.oracle.graal.python.builtins.PythonBuiltins;
@@ -68,9 +70,11 @@
6870
import com.oracle.truffle.api.interop.UnknownIdentifierException;
6971
import com.oracle.truffle.api.interop.UnsupportedMessageException;
7072
import com.oracle.truffle.api.interop.UnsupportedTypeException;
73+
import com.oracle.truffle.api.nodes.LanguageInfo;
7174
import com.oracle.truffle.api.nodes.Node;
7275
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;
7478

7579
@CoreFunctions(defineModule = "polyglot")
7680
public 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

Comments
 (0)