From 67303c95d2e9f505108122179608ed2d7b598ba7 Mon Sep 17 00:00:00 2001
From: Martin Entlicher <martin.entlicher@oracle.com>
Date: Thu, 19 Apr 2018 14:59:05 +0200
Subject: [PATCH] [GR-9400] Deparse to textual sources by default.

---
 .../oracle/truffle/r/runtime/FastROptions.java |  2 +-
 .../com/oracle/truffle/r/runtime/RDeparse.java | 18 +++++++++++++-----
 .../com/oracle/truffle/r/runtime/RSource.java  | 14 +++++++++++---
 3 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java
index dafc8d29b3..0285d35f34 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java
@@ -57,7 +57,7 @@ public enum FastROptions {
     SharedContexts("Whether all child contexts are to be shared contexts", true),
     SearchPathForcePromises("Whether all promises for frames on shared path are forced in presence of shared contexts", false),
     LoadPackagesNativeCode("Load native code of packages, including builtin packages.", !FastRConfig.ManagedMode),
-    EmitTmpSource("Write deparsed source code to temporary files for better debugging.", true),
+    EmitTmpSource("Write deparsed source code to temporary files for better debugging.", false),
     EmitTmpDir("The directory where to allocate temporary files with deparsed source code.", null, true),
     EmitTmpHashed("Use an SHA-256 hash as file name to reduce temporary file creation.", true),
     SynchronizeNativeCode("allow only one thread to enter packages' native code", false),
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java
index a786065307..3b74920c86 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java
@@ -392,12 +392,20 @@ public class RDeparse {
             if (FastROptions.EmitTmpSource.getBooleanValue() && deparsePath != null) {
                 fixupSourcesTempFile(deparsePath);
             } else {
-                fixupSourcesTextInternal();
+                fixupSourcesText();
             }
         }
 
-        private void fixupSourcesTextInternal() {
-            Source source = RSource.fromTextInternal(sb.toString(), RSource.Internal.DEPARSE);
+        private void fixupSourcesText() {
+            RootNode rootNode = getRootNode();
+            String name = rootNode != null ? rootNode.getName() : null;
+            String text = sb.toString();
+            if (name != null && !name.isEmpty() && !name.equals("<no source>")) {
+                name = name.replace(File.separatorChar, '_') + ".r";
+            } else {
+                name = "unknown.r";
+            }
+            Source source = RSource.fromText(text, name);
             for (SourceSectionElement s : sources) {
                 s.element.setSourceSection(source.createSection(s.start, s.length));
             }
@@ -416,10 +424,10 @@ public class RDeparse {
                 }
             } catch (AccessDeniedException | FileAlreadyExistsException | IllegalArgumentException e) {
                 // do not report because these exceptions are legitimate
-                fixupSourcesTextInternal();
+                fixupSourcesText();
             } catch (Throwable e) {
                 RInternalError.reportError(e);
-                fixupSourcesTextInternal();
+                fixupSourcesText();
             }
         }
 
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 33c82ab20d..93997cb505 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
@@ -117,6 +117,14 @@ public class RSource {
         return builder.build();
     }
 
+    /**
+     * Create a cached source from {@code text} and {@code name}.
+     */
+    public static Source fromText(String text, String name) {
+        String uniqueText = text.intern();
+        return getCachedByOrigin(uniqueText, origin -> Source.newBuilder(uniqueText).name(name).language(RRuntime.R_LANGUAGE_ID).build());
+    }
+
     /**
      * Create an {@code internal} source from {@code text} and {@code description}.
      */
@@ -294,7 +302,7 @@ public class RSource {
         }
     }
 
-    private static <T> Source getCachedByOrigin(T origin, SourceGenerator<T> generator) throws IOException {
+    private static <T, E extends Exception> Source getCachedByOrigin(T origin, SourceGenerator<T, E> generator) throws E {
         CompilerAsserts.neverPartOfCompilation();
         Source src;
         synchronized (deserializedSources) {
@@ -309,7 +317,7 @@ public class RSource {
     }
 
     @FunctionalInterface
-    private interface SourceGenerator<T> {
-        Source apply(T origin) throws IOException;
+    private interface SourceGenerator<T, E extends Exception> {
+        Source apply(T origin) throws E;
     }
 }
-- 
GitLab