From df4ac095dac8318ed4231a30553e2875bd1c91e3 Mon Sep 17 00:00:00 2001
From: stepan <stepan.sindelar@oracle.com>
Date: Mon, 29 Jan 2018 19:22:39 +0100
Subject: [PATCH] UpCalls should not return optimized promises, return the
 value instead

---
 .../r/ffi/impl/common/JavaUpCallsRFFIImpl.java     | 14 ++++++--------
 .../oracle/truffle/r/runtime/data/RPromise.java    | 10 +++++++++-
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java
index 967be7aeef..9c89ec3258 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java
@@ -131,8 +131,6 @@ import sun.misc.Unsafe;
  */
 public abstract class JavaUpCallsRFFIImpl implements UpCallsRFFI {
 
-    private final Map<String, Object> nameSymbolCache = new ConcurrentHashMap<>();
-
     private static RuntimeException implementedAsNode() {
         // TODO: Exception handling over native boundaries is currently missing. Once this works,
         // remove the following two lines.
@@ -270,6 +268,11 @@ public abstract class JavaUpCallsRFFIImpl implements UpCallsRFFI {
         while (env != REnvironment.emptyEnv()) {
             Object value = env.get(name.getName());
             if (value != null) {
+                if (value instanceof RPromise && ((RPromise) value).isOptimized()) {
+                    // From the point of view of RFFI, optimized promises (i.e. promises with null
+                    // env) should not show up
+                    return ((RPromise) value).getRawValue();
+                }
                 return value;
             }
             if (!inherits) {
@@ -350,12 +353,7 @@ public abstract class JavaUpCallsRFFIImpl implements UpCallsRFFI {
     @Override
     @TruffleBoundary
     public Object Rf_install(String name) {
-        Object ret = nameSymbolCache.get(name);
-        if (ret == null) {
-            ret = RDataFactory.createSymbolInterned(name);
-            nameSymbolCache.put(name, ret);
-        }
-        return ret;
+        return RDataFactory.createSymbolInterned(name);
     }
 
     @Override
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RPromise.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RPromise.java
index 8f4bc9a99b..acf61beac8 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RPromise.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RPromise.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 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
@@ -215,6 +215,14 @@ public class RPromise extends RObject implements RTypedValue {
         this.value = newValue;
     }
 
+    /**
+     * Promises to constants can be optimized, which means that they only hold the value, but do not
+     * need to keep the frame and will never need the frame.
+     */
+    public boolean isOptimized() {
+        return this.execFrame == null;
+    }
+
     /**
      * Returns {@code true} if this promise has been evaluated?
      */
-- 
GitLab