From 3a805dc1deb47d038a6d3a46bd8ddcca17e61807 Mon Sep 17 00:00:00 2001
From: Mick Jordan <mick.jordan@oracle.com>
Date: Tue, 30 Aug 2016 16:02:34 -0700
Subject: [PATCH] fix regression in embedded mode polyglot init

---
 .gitignore                                    |  2 ++
 .../com/oracle/truffle/r/engine/REngine.java  | 20 +++++++++++++++----
 .../truffle/r/engine/shell/REmbedded.java     |  8 +++++++-
 .../com/oracle/truffle/r/runtime/RSource.java |  3 ++-
 4 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/.gitignore b/.gitignore
index d39a0a4a07..4e690d74e0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,6 +26,8 @@
 /com.oracle.truffle.r.test.native/packages/*/lib/*
 /com.oracle.truffle.r.test/rpackages/testrlibs_user
 /com.oracle.truffle.r.test.native/urand/lib/liburand.so
+/com.oracle.truffle.r.release/lib/
+/com.oracle.truffle.r.release/library/
 /DEPARSE_ERROR
 /Rpkgsource/*
 /install.cran.logs/
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 03697c9230..38fb76d406 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
@@ -299,17 +299,29 @@ final class REngine implements Engine, Engine.Timings {
     @Override
     public CallTarget parseToCallTarget(Source source) throws ParseException {
         List<RSyntaxNode> statements = parseImpl(null, source);
-        return Truffle.getRuntime().createCallTarget(new PolyglotEngineRootNode(statements));
+        return Truffle.getRuntime().createCallTarget(new PolyglotEngineRootNode(statements, createSourceSection(statements)));
+    }
+
+    private static SourceSection createSourceSection(List<RSyntaxNode> statements) {
+        // All statements come from the same "Source"
+        if (statements.size() == 1) {
+            return statements.get(0).getSourceSection();
+        } else {
+            Source source = statements.get(0).getSourceSection().getSource();
+            return source.createSection(0, statements.get(statements.size() - 1).getSourceSection().getCharEndIndex());
+        }
     }
 
     private final class PolyglotEngineRootNode extends RootNode {
 
         private final List<RSyntaxNode> statements;
+        private final boolean printResult;
 
         @Child private Node findContext = TruffleRLanguage.INSTANCE.actuallyCreateFindContextNode();
 
-        PolyglotEngineRootNode(List<RSyntaxNode> statements) {
-            super(TruffleRLanguage.class, SourceSection.createUnavailable("repl", RSource.Internal.REPL_WRAPPER.string), new FrameDescriptor());
+        PolyglotEngineRootNode(List<RSyntaxNode> statements, SourceSection sourceSection) {
+            super(TruffleRLanguage.class, sourceSection, new FrameDescriptor());
+            this.printResult = !sourceSection.getSource().getName().equals(RSource.Internal.INIT_EMBEDDED.string);
             this.statements = statements;
         }
 
@@ -327,7 +339,7 @@ final class REngine implements Engine, Engine.Timings {
                 Object lastValue = RNull.instance;
                 for (int i = 0; i < statements.size(); i++) {
                     RSyntaxNode node = statements.get(i);
-                    RootCallTarget callTarget = doMakeCallTarget(node.asRNode(), RSource.Internal.REPL_WRAPPER.string, true, true);
+                    RootCallTarget callTarget = doMakeCallTarget(node.asRNode(), RSource.Internal.REPL_WRAPPER.string, printResult, true);
                     lastValue = callTarget.call(newContext.stateREnvironment.getGlobalFrame());
                 }
                 return lastValue;
diff --git a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/shell/REmbedded.java b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/shell/REmbedded.java
index 87bb3136b5..0e9fa5f656 100644
--- a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/shell/REmbedded.java
+++ b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/shell/REmbedded.java
@@ -27,6 +27,7 @@ import com.oracle.truffle.api.vm.PolyglotEngine;
 import com.oracle.truffle.r.runtime.RCmdOptions;
 import com.oracle.truffle.r.runtime.RRuntime;
 import com.oracle.truffle.r.runtime.RSource;
+import com.oracle.truffle.r.runtime.RSource.Internal;
 import com.oracle.truffle.r.runtime.RStartParams;
 import com.oracle.truffle.r.runtime.Utils;
 import com.oracle.truffle.r.runtime.context.ContextInfo;
@@ -73,7 +74,12 @@ public class REmbedded {
         return vm;
     }
 
-    private static final Source INIT = RSource.fromTextInternal("invisible(1)", RSource.Internal.GET_ECHO);
+    /**
+     * N.B. This expression cannot contain any R functions, e.g. "invisible", because at the time it
+     * is evaluated the R builtins have not been installed, see {@link #initializeR}. The
+     * suppression of printing is handled a a special case based on {@link Internal#INIT_EMBEDDED}.
+     */
+    private static final Source INIT = RSource.fromTextInternal("1", RSource.Internal.INIT_EMBEDDED);
 
     /**
      * GnuR distinguishes {@code setup_Rmainloop} and {@code run_Rmainloop}. Currently we don't have
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 d02be78943..6d8b059ca5 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
@@ -74,7 +74,8 @@ public class RSource {
         DEPARSE_ERROR("<package_deparse_error>"),
         LAPPLY("<lapply>"),
         R_PARSEVECTOR("<R_ParseVector>"),
-        PAIRLIST_DEPARSE("<pairlist deparse>");
+        PAIRLIST_DEPARSE("<pairlist deparse>"),
+        INIT_EMBEDDED("<init embedded>");
 
         public final String string;
 
-- 
GitLab