From 8b8fc3b81db8de88092fda7fc70439dc103c8444 Mon Sep 17 00:00:00 2001
From: stepan <stepan.sindelar@oracle.com>
Date: Tue, 4 Apr 2017 13:53:43 +0200
Subject: [PATCH] FastR Grid: helper to run internal R code from outside of
 fast path

---
 .../r/library/fastrGrid/GridContext.java       | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridContext.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridContext.java
index 4f14e7b14e..f00b510ec4 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridContext.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridContext.java
@@ -30,14 +30,21 @@ import com.oracle.truffle.r.library.fastrGrid.device.GridDevice.DeviceCloseExcep
 import com.oracle.truffle.r.library.fastrGrid.device.awt.BufferedJFrameDevice;
 import com.oracle.truffle.r.library.fastrGrid.device.awt.JFrameDevice;
 import com.oracle.truffle.r.library.fastrGrid.graphics.RGridGraphicsAdapter;
+import com.oracle.truffle.r.runtime.RCaller;
 import com.oracle.truffle.r.runtime.RError;
 import com.oracle.truffle.r.runtime.RError.Message;
+import com.oracle.truffle.r.runtime.RInternalCode;
+import com.oracle.truffle.r.runtime.context.RContext;
+import com.oracle.truffle.r.runtime.data.RFunction;
+import com.oracle.truffle.r.runtime.env.REnvironment;
 
 /**
  * Encapsulated the access to the global grid state.
  */
 public final class GridContext {
     private static final GridContext INSTANCE = new GridContext();
+
+    private RInternalCode internalCode;
     private final GridState gridState = new GridState();
     /**
      * This list should correspond to the names inside {@code .Devices} variable in R.
@@ -100,6 +107,17 @@ public final class GridContext {
         }
     }
 
+    /**
+     * Runs arbitrary function from 'fastrGrid.R' file and returns its result.
+     */
+    public Object evalInternalRFunction(String functionName, Object... args) {
+        if (internalCode == null) {
+            internalCode = RInternalCode.lookup(RContext.getInstance(), "grid", RInternalCode.loadSourceRelativeTo(LInitGrid.class, "fastrGrid.R"));
+        }
+        RFunction redrawAll = internalCode.lookupFunction(functionName);
+        return RContext.getEngine().evalFunction(redrawAll, REnvironment.baseEnv().getFrame(), RCaller.topLevel, null, args);
+    }
+
     private static final class DeviceAndState {
         final GridDevice device;
         final GridDeviceState state;
-- 
GitLab