From 500e214178e42ba37e099b089e30cdac7d3d0a64 Mon Sep 17 00:00:00 2001
From: Julien Lopez <julien.lopez@lri.fr>
Date: Tue, 27 Jun 2017 18:23:36 +0200
Subject: [PATCH] Transfer interface with databases functions to
 TruffleRLanguage

---
 .../com/oracle/truffle/r/engine/REngine.java  | 50 ------------------
 .../truffle/r/engine/TruffleRLanguage.java    | 52 +++++++++++++++++++
 2 files changed, 52 insertions(+), 50 deletions(-)

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 248c2488ea..eaa5cfbb48 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
@@ -23,8 +23,6 @@
 package com.oracle.truffle.r.engine;
 
 import java.io.File;
-import java.io.IOException;
-import java.io.Serializable;
 import java.util.Arrays;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -51,8 +49,6 @@ import com.oracle.truffle.api.profiles.ConditionProfile;
 import com.oracle.truffle.api.profiles.ValueProfile;
 import com.oracle.truffle.api.source.Source;
 import com.oracle.truffle.api.source.SourceSection;
-import com.oracle.truffle.api.vm.PolyglotEngine;
-import com.oracle.truffle.api.vm.PolyglotEngine.Value;
 import com.oracle.truffle.r.library.graphics.RGraphics;
 import com.oracle.truffle.r.nodes.RASTBuilder;
 import com.oracle.truffle.r.nodes.RASTUtils;
@@ -83,7 +79,6 @@ import com.oracle.truffle.r.runtime.RProfile;
 import com.oracle.truffle.r.runtime.RRuntime;
 import com.oracle.truffle.r.runtime.RSource;
 import com.oracle.truffle.r.runtime.RStartParams.SA_TYPE;
-import com.oracle.truffle.r.runtime.RValue;
 import com.oracle.truffle.r.runtime.ReturnException;
 import com.oracle.truffle.r.runtime.RootWithBody;
 import com.oracle.truffle.r.runtime.SubstituteVirtualFrame;
@@ -328,51 +323,6 @@ final class REngine implements Engine, Engine.Timings {
         }
     }
 
-    public static final RValue executeR(final String program) throws IOException {
-        RContext.setDeepEmbedded();
-        final Value v = PolyglotEngine.newBuilder().config("application/x-r", "REngine", null).build().eval(Source.newBuilder(program).name("RBuilder").mimeType(RRuntime.R_APP_MIME).build());
-        final Object res = v.get();
-        if (res instanceof Serializable)
-            return new RValue((Serializable) res);
-        return new RValue(v.getSourceLocation().getCode());
-    }
-
-    public static final RValue executeApply(final RValue fun, final RValue args[]) throws IOException {
-        return executeR("f = " + fun.getValue() + "\nf(" + Arrays.stream(args).map(arg -> arg.getValue().toString()).collect(Collectors.joining(", ")) + ")");
-    }
-
-    public static final RValue translate(final Integer i) throws IOException {
-        return new RValue(i);
-    }
-
-    public static final RValue translate(final Double d) throws IOException {
-        return new RValue(d);
-    }
-
-    public static final RValue translate(final String s) throws IOException {
-        return new RValue(s);
-    }
-
-    public static final RValue translate(final Boolean b) throws IOException {
-        return new RValue(b);
-    }
-
-    public static final Integer translateBackToInteger(final RValue v) {
-        return (Integer) v.getValue();
-    }
-
-    public static final Double translateBackToDouble(final RValue v) {
-        return (Double) v.getValue();
-    }
-
-    public static final String translateBackToString(final RValue v) {
-        return (String) v.getValue();
-    }
-
-    public static final Boolean translateBackToBoolean(final RValue v) {
-        return (Boolean) v.getValue();
-    }
-
     private final class PolyglotEngineRootNode extends RootNode {
 
         private final List<RSyntaxNode> statements;
diff --git a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguage.java b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguage.java
index 7a858d9213..afafa3c17c 100644
--- a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguage.java
+++ b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguage.java
@@ -22,7 +22,11 @@
  */
 package com.oracle.truffle.r.engine;
 
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.Arrays;
 import java.util.Locale;
+import java.util.stream.Collectors;
 
 import com.oracle.truffle.api.CallTarget;
 import com.oracle.truffle.api.CompilerAsserts;
@@ -37,7 +41,10 @@ import com.oracle.truffle.api.instrumentation.StandardTags;
 import com.oracle.truffle.api.metadata.ScopeProvider;
 import com.oracle.truffle.api.nodes.Node;
 import com.oracle.truffle.api.nodes.RootNode;
+import com.oracle.truffle.api.source.Source;
 import com.oracle.truffle.api.source.SourceSection;
+import com.oracle.truffle.api.vm.PolyglotEngine;
+import com.oracle.truffle.api.vm.PolyglotEngine.Value;
 import com.oracle.truffle.r.engine.interop.RForeignAccessFactoryImpl;
 import com.oracle.truffle.r.nodes.RASTBuilder;
 import com.oracle.truffle.r.nodes.builtin.RBuiltinPackages;
@@ -49,6 +56,7 @@ import com.oracle.truffle.r.runtime.RAccuracyInfo;
 import com.oracle.truffle.r.runtime.RDeparse;
 import com.oracle.truffle.r.runtime.RError;
 import com.oracle.truffle.r.runtime.RRuntime;
+import com.oracle.truffle.r.runtime.RValue;
 import com.oracle.truffle.r.runtime.Utils;
 import com.oracle.truffle.r.runtime.context.Engine.IncompleteSourceException;
 import com.oracle.truffle.r.runtime.context.Engine.ParseException;
@@ -252,4 +260,48 @@ public final class TruffleRLanguage extends TruffleLanguage<RContext> implements
     public AbstractScope findScope(RContext langContext, Node node, Frame frame) {
         return RScope.createScope(node, frame);
     }
+
+    public static final RValue executeR(final String program) throws IOException {
+        final Value v = PolyglotEngine.newBuilder().config("application/x-r", "REngine", null).build().eval(Source.newBuilder(program).name("RBuilder").mimeType(RRuntime.R_APP_MIME).build());
+        final Object res = v.get();
+        if (res instanceof Serializable)
+            return new RValue((Serializable) res);
+        return new RValue(v.getSourceLocation().getCode());
+    }
+
+    public static final RValue executeApply(final RValue fun, final RValue args[]) throws IOException {
+        return executeR("f = " + fun.getValue() + ";f(" + Arrays.stream(args).map(arg -> arg.getValue().toString()).collect(Collectors.joining(", ")) + ")");
+    }
+
+    public static final RValue translate(final Integer i) throws IOException {
+        return new RValue(i);
+    }
+
+    public static final RValue translate(final Double d) throws IOException {
+        return new RValue(d);
+    }
+
+    public static final RValue translate(final String s) throws IOException {
+        return new RValue(s);
+    }
+
+    public static final RValue translate(final Boolean b) throws IOException {
+        return new RValue(b);
+    }
+
+    public static final Integer translateBackToInteger(final RValue v) {
+        return (Integer) v.getValue();
+    }
+
+    public static final Double translateBackToDouble(final RValue v) {
+        return (Double) v.getValue();
+    }
+
+    public static final String translateBackToString(final RValue v) {
+        return (String) v.getValue();
+    }
+
+    public static final Boolean translateBackToBoolean(final RValue v) {
+        return (Boolean) v.getValue();
+    }
 }
-- 
GitLab