From 8d0aba3b7d1bf97d221a228c6e55fdffa24d5883 Mon Sep 17 00:00:00 2001
From: Mick Jordan <mick.jordan@oracle.com>
Date: Wed, 22 Jun 2016 08:05:45 -0700
Subject: [PATCH] Implement .Options baseenv variable

---
 .../oracle/truffle/r/runtime/ROptions.java    | 28 +++++++++++++++++++
 .../truffle/r/runtime/context/RContext.java   |  1 +
 2 files changed, 29 insertions(+)

diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ROptions.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ROptions.java
index a2a7f8b73f..21336121a8 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ROptions.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ROptions.java
@@ -28,7 +28,9 @@ import com.oracle.truffle.r.runtime.data.RIntVector;
 import com.oracle.truffle.r.runtime.data.RLanguage;
 import com.oracle.truffle.r.runtime.data.RLogicalVector;
 import com.oracle.truffle.r.runtime.data.RNull;
+import com.oracle.truffle.r.runtime.data.RPairList;
 import com.oracle.truffle.r.runtime.data.RStringVector;
+import com.oracle.truffle.r.runtime.env.REnvironment;
 
 /**
  * Central location for all R options, that is for the {@code options(...)} and {@code getOption}
@@ -51,6 +53,7 @@ public class ROptions {
 
         private ContextStateImpl(HashMap<String, Object> map) {
             this.map = map;
+            // cannot call updateDotOptions here
         }
 
         public Set<Entry<String, Object>> getValues() {
@@ -83,6 +86,7 @@ public class ROptions {
             } else {
                 map.put(name, coercedValue);
             }
+            updateDotOptions();
             return previous;
         }
 
@@ -95,6 +99,25 @@ public class ROptions {
             }
             return new ContextStateImpl(map);
         }
+
+        /**
+         * Creates/updates the {@code .Options} variable in {@code baseenv}.
+         */
+        public void updateDotOptions() {
+            // TODO make incremental?
+            RPairList ppl = null;
+            RPairList head = null;
+            for (Map.Entry<String, Object> entry : getValues()) {
+                RPairList pl = RDataFactory.createPairList(entry.getValue(), RNull.instance, RDataFactory.createSymbol(entry.getKey()));
+                if (ppl != null) {
+                    ppl.setCdr(pl);
+                } else {
+                    head = pl;
+                }
+                ppl = pl;
+            }
+            REnvironment.baseEnv().safePut(DOT_OPTIONS, head);
+        }
     }
 
     @SuppressWarnings("serial")
@@ -108,6 +131,11 @@ public class ROptions {
         }
     }
 
+    /**
+     * S compatibility - pair list of the options
+     */
+    private static final String DOT_OPTIONS = ".Options";
+
     private static final Set<String> CHECKED_OPTIONS_SET = new HashSet<>(Arrays.asList("width", "deparse.cutoff", "digits", "expressions", "keep.source", "editor", "continue", "prompt", "contrasts",
                     "check.bounds", "warn", "warning.length", "warning.expression", "max.print", "nwarnings", "error", "show.error.messages", "echo", "OutDec", "max.contour.segments",
                     "rl_word_breaks", "warnPartialMatchDollar", "warnPartialMatchArgs", "warnPartialMatchAttr", "showWarnCalls", "showErrorCalls", "showNCalls", "par.ask.default",
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java
index 1e1cf27e2e..e3cd344f2d 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java
@@ -478,6 +478,7 @@ public final class RContext extends ExecutionContext implements TruffleObject {
         doEnvOptionsProfileInitialization();
         validateContextStates();
         engine.activate(stateREnvironment);
+        stateROptions.updateDotOptions();
         initialContextInitialized = true;
     }
 
-- 
GitLab