From 02af02c54c0d3c26c19ef58af0132e2074f7ac3b Mon Sep 17 00:00:00 2001
From: Tomas Stupka <tomas.stupka@oracle.com>
Date: Wed, 11 Apr 2018 16:24:56 +0200
Subject: [PATCH] wherever possible, create Source with language id instead of
 mimetype

---
 .../com/oracle/truffle/r/engine/REngine.java  |  2 +-
 .../r/engine/TruffleRLanguageImpl.java        |  2 --
 .../r/ffi/impl/nfi/TruffleNFI_Context.java    |  2 +-
 .../r/ffi/impl/nfi/TruffleNFI_DLL.java        |  4 +--
 .../truffle/r/nodes/builtin/base/Parse.java   |  6 ++--
 .../r/nodes/builtin/fastr/FastRInterop.java   |  2 +-
 .../truffle/r/nodes/test/SpecialCallTest.java |  5 +--
 .../oracle/truffle/r/runtime/RRuntime.java    |  1 +
 .../com/oracle/truffle/r/runtime/RSource.java | 34 +++++++++----------
 9 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/REngine.java b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/REngine.java
index 86a0833200..6dd582b874 100644
--- a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/REngine.java
+++ b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/REngine.java
@@ -390,7 +390,7 @@ final class REngine implements Engine, Engine.Timings {
                         break;
                     }
                     sb.append(input);
-                    Source src = Source.newBuilder(sb.toString()).mimeType(RRuntime.R_APP_MIME).name(file + "#" + startLine + "-" + lineIndex).uri(uri).build();
+                    Source src = Source.newBuilder(sb.toString()).language(RRuntime.R_LANGUAGE_ID).name(file + "#" + startLine + "-" + lineIndex).uri(uri).build();
                     lineIndex++;
                     List<RSyntaxNode> currentStmts = null;
                     try {
diff --git a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguageImpl.java b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguageImpl.java
index 50633e95da..17940abb3e 100644
--- a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguageImpl.java
+++ b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguageImpl.java
@@ -94,8 +94,6 @@ public final class TruffleRLanguageImpl extends TruffleRLanguage {
 
     private static boolean systemInitialized;
 
-    public static final String MIME = RRuntime.R_APP_MIME;
-
     @Override
     protected boolean isObjectOfLanguage(Object object) {
         return object instanceof RTypedValue;
diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_Context.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_Context.java
index d1e3aba025..1f1a0ddfc0 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_Context.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_Context.java
@@ -308,7 +308,7 @@ final class TruffleNFI_Context extends RFFIContext {
                     // new thread, initialize properly
                     assert defaultLibrary == null && rlibDLLInfo == null;
                     rlibDLLInfo = DLL.findLibraryContainingSymbol(context, "dot_call0");
-                    defaultLibrary = (TruffleObject) RContext.getInstance().getEnv().parse(Source.newBuilder("default").name("(load default)").mimeType("application/x-native").build()).call();
+                    defaultLibrary = (TruffleObject) RContext.getInstance().getEnv().parse(Source.newBuilder("default").name("(load default)").language("native").build()).call();
                     initCallbacks(context);
                     break;
                 case SHARE_PARENT_RO:
diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_DLL.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_DLL.java
index b55ff1ec00..c9922c8ffa 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_DLL.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_DLL.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -57,7 +57,7 @@ public class TruffleNFI_DLL implements DLLRFFI {
         public Object execute(String path, boolean local, boolean now) {
             String libName = DLL.libName(path);
             Env env = RContext.getInstance().getEnv();
-            TruffleObject libHandle = (TruffleObject) env.parse(Source.newBuilder(prepareLibraryOpen(path, local, now)).name(path).mimeType("application/x-native").build()).call();
+            TruffleObject libHandle = (TruffleObject) env.parse(Source.newBuilder(prepareLibraryOpen(path, local, now)).name(path).language("native").build()).call();
             return new NFIHandle(libName, libHandle);
         }
     }
diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Parse.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Parse.java
index b35736e7e3..0fb4a4a351 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Parse.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Parse.java
@@ -223,12 +223,12 @@ public abstract class Parse extends RBuiltinNode.Arg6 {
                 assert result != null : "Source created from environment should not be null";
                 return result;
             } else {
-                return Source.newBuilder(coalescedLines).name("<parse>").mimeType(RRuntime.R_APP_MIME).build();
+                return Source.newBuilder(coalescedLines).name("<parse>").language(RRuntime.R_LANGUAGE_ID).build();
             }
         } else {
             String srcFileText = RRuntime.asString(srcFile);
             if (srcFileText.equals("<text>")) {
-                return Source.newBuilder(coalescedLines).name("<parse>").mimeType(RRuntime.R_APP_MIME).build();
+                return Source.newBuilder(coalescedLines).name("<parse>").language(RRuntime.R_LANGUAGE_ID).build();
             } else {
                 return createFileSource(ConnectionSupport.removeFileURLPrefix(srcFileText), coalescedLines, false);
             }
@@ -247,7 +247,7 @@ public abstract class Parse extends RBuiltinNode.Arg6 {
             return RSource.fromFileName(chars, path, internal);
         } catch (URISyntaxException e) {
             // Note: to be compatible with GnuR we construct Source even with a malformed path
-            Source.Builder<RuntimeException, RuntimeException, RuntimeException> builder = Source.newBuilder(chars).name(path).mimeType(RRuntime.R_APP_MIME);
+            Source.Builder<RuntimeException, RuntimeException, RuntimeException> builder = Source.newBuilder(chars).name(path).language(RRuntime.R_LANGUAGE_ID);
             if (internal) {
                 builder.internal();
             }
diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRInterop.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRInterop.java
index 554c855c75..9d1335b53a 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRInterop.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRInterop.java
@@ -169,7 +169,7 @@ public class FastRInterop {
         protected CallTarget parse(String languageId, String source) {
             CompilerAsserts.neverPartOfCompilation();
 
-            Source sourceObject = Source.newBuilder(source).name(RSource.Internal.EVAL_WRAPPER.string).language(languageId).internal().build();
+            Source sourceObject = RSource.fromTextInternalInvisible(source, RSource.Internal.EVAL_WRAPPER, languageId);
             try {
                 return RContext.getInstance().getEnv().parse(sourceObject);
             } catch (Throwable t) {
diff --git a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/SpecialCallTest.java b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/SpecialCallTest.java
index 89a959f4b3..cef464a9b4 100644
--- a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/SpecialCallTest.java
+++ b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/SpecialCallTest.java
@@ -32,6 +32,7 @@ import com.oracle.truffle.r.engine.TruffleRLanguageImpl;
 import com.oracle.truffle.r.runtime.ArgumentsSignature;
 import com.oracle.truffle.r.runtime.FastROptions;
 import com.oracle.truffle.r.runtime.RError;
+import com.oracle.truffle.r.runtime.RRuntime;
 import com.oracle.truffle.r.runtime.RootWithBody;
 import com.oracle.truffle.r.runtime.context.RContext;
 import com.oracle.truffle.r.runtime.data.RExpression;
@@ -287,8 +288,8 @@ public class SpecialCallTest extends TestBase {
         if (!FastROptions.UseSpecials.getBooleanValue()) {
             return;
         }
-        Source setupSource = Source.newBuilder("{" + setup + "}").mimeType(TruffleRLanguageImpl.MIME).name("test").build();
-        Source testSource = Source.newBuilder(test).mimeType(TruffleRLanguageImpl.MIME).name("test").build();
+        Source setupSource = Source.newBuilder("{" + setup + "}").language(RRuntime.R_LANGUAGE_ID).name("test").build();
+        Source testSource = Source.newBuilder(test).language(RRuntime.R_LANGUAGE_ID).name("test").build();
 
         RExpression setupExpression = testVMContext.getThisEngine().parse(setupSource);
         RExpression testExpression = testVMContext.getThisEngine().parse(testSource);
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RRuntime.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RRuntime.java
index 9ce0b2aea1..43a08e94cc 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RRuntime.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RRuntime.java
@@ -72,6 +72,7 @@ public class RRuntime {
     public static final boolean True = true;
     public static final boolean False = false;
 
+    public static final String R_LANGUAGE_ID = "R";
     public static final String R_APP_MIME = "application/x-r";
     public static final String R_TEXT_MIME = "text/x-r";
 
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RSource.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RSource.java
index 4e66f50794..d1429ce1de 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RSource.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RSource.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -110,7 +110,7 @@ public class RSource {
     public static Source fromFileName(String text, String path, boolean internal) throws URISyntaxException {
         File file = new File(path).getAbsoluteFile();
         URI uri = new URI("file://" + file.getAbsolutePath());
-        Source.Builder<RuntimeException, RuntimeException, RuntimeException> builder = Source.newBuilder(file).content(text).uri(uri).mimeType(RRuntime.R_APP_MIME);
+        Source.Builder<RuntimeException, RuntimeException, RuntimeException> builder = Source.newBuilder(file).content(text).uri(uri).language(RRuntime.R_LANGUAGE_ID);
         if (internal) {
             builder.internal();
         }
@@ -121,32 +121,32 @@ public class RSource {
      * Create an {@code internal} source from {@code text} and {@code description}.
      */
     public static Source fromTextInternal(String text, Internal description) {
-        return fromTextInternal(text, description, RRuntime.R_APP_MIME);
+        return fromTextInternal(text, description, RRuntime.R_LANGUAGE_ID);
     }
 
     /**
      * Create an {@code internal} source from {@code text} and {@code description}.
      */
     public static Source fromTextInternalInvisible(String text, Internal description) {
-        return fromTextInternalInvisible(text, description, RRuntime.R_APP_MIME);
+        return fromTextInternalInvisible(text, description, RRuntime.R_LANGUAGE_ID);
     }
 
     /**
      * Create an {@code internal} source from {@code text} and {@code description} of given
-     * {@code mimeType}.
+     * {@code languageId}.
      */
 
-    public static Source fromTextInternal(String text, Internal description, String mimeType) {
-        return Source.newBuilder(text).name(description.string).mimeType(mimeType).internal().interactive().build();
+    public static Source fromTextInternal(String text, Internal description, String languageId) {
+        return Source.newBuilder(text).name(description.string).language(languageId).internal().interactive().build();
     }
 
     /**
      * Create an {@code internal} source from {@code text} and {@code description} of given
-     * {@code mimeType}.
+     * {@code languageId}.
      */
 
-    public static Source fromTextInternalInvisible(String text, Internal description, String mimeType) {
-        return Source.newBuilder(text).name(description.string).mimeType(mimeType).internal().build();
+    public static Source fromTextInternalInvisible(String text, Internal description, String languageId) {
+        return Source.newBuilder(text).name(description.string).language(languageId).internal().build();
     }
 
     /**
@@ -155,7 +155,7 @@ public class RSource {
      */
     public static Source fromPackageTextInternal(String text, String packageName) {
         String name = String.format(Internal.PACKAGE.string, packageName);
-        return Source.newBuilder(text).name(name).mimeType(RRuntime.R_APP_MIME).build();
+        return Source.newBuilder(text).name(name).language(RRuntime.R_LANGUAGE_ID).build();
     }
 
     /**
@@ -167,7 +167,7 @@ public class RSource {
         if (functionName == null) {
             return fromPackageTextInternal(text, packageName);
         } else {
-            return Source.newBuilder(text).name(packageName + "::" + functionName).mimeType(RRuntime.R_APP_MIME).build();
+            return Source.newBuilder(text).name(packageName + "::" + functionName).language(RRuntime.R_LANGUAGE_ID).build();
         }
     }
 
@@ -177,7 +177,7 @@ public class RSource {
     public static Source fromFileName(String path, boolean internal) throws IOException {
         File file = new File(path);
         return getCachedByOrigin(file, origin -> {
-            Source.Builder<IOException, RuntimeException, RuntimeException> builder = Source.newBuilder(file).mimeType(RRuntime.R_APP_MIME);
+            Source.Builder<IOException, RuntimeException, RuntimeException> builder = Source.newBuilder(file).language(RRuntime.R_LANGUAGE_ID);
             if (internal) {
                 builder.internal();
             }
@@ -189,21 +189,21 @@ public class RSource {
      * Create an (external) source from the file system path denoted by {@code file}.
      */
     public static Source fromFile(File file) throws IOException {
-        return getCachedByOrigin(file, origin -> Source.newBuilder(file).name(file.getName()).mimeType(RRuntime.R_APP_MIME).build());
+        return getCachedByOrigin(file, origin -> Source.newBuilder(file).name(file.getName()).language(RRuntime.R_LANGUAGE_ID).build());
     }
 
     /**
      * Create a source from the file system path denoted by {@code file}.
      */
     public static Source fromTempFile(File file) throws IOException {
-        return getCachedByOrigin(file, origin -> Source.newBuilder(file).name(file.getName()).mimeType(RRuntime.R_APP_MIME).internal().build());
+        return getCachedByOrigin(file, origin -> Source.newBuilder(file).name(file.getName()).language(RRuntime.R_LANGUAGE_ID).internal().build());
     }
 
     /**
      * Create an (external) source from {@code url}.
      */
     public static Source fromURL(URL url, String name) throws IOException {
-        return getCachedByOrigin(url, origin -> Source.newBuilder(url).name(name).mimeType(RRuntime.R_APP_MIME).build());
+        return getCachedByOrigin(url, origin -> Source.newBuilder(url).name(name).language(RRuntime.R_LANGUAGE_ID).build());
     }
 
     /**
@@ -240,7 +240,7 @@ public class RSource {
      * Create an unknown source with the given name.
      */
     public static SourceSection createUnknown(String name) {
-        return Source.newBuilder("").name(name).mimeType(RRuntime.R_APP_MIME).build().createSection(0, 0);
+        return Source.newBuilder("").name(name).language(RRuntime.R_LANGUAGE_ID).build().createSection(0, 0);
     }
 
     /**
-- 
GitLab