From 550ed22a2f687b07ae3c83486b3d2eea7af4b983 Mon Sep 17 00:00:00 2001
From: Mick Jordan <mick.jordan@oracle.com>
Date: Thu, 23 Jun 2016 16:11:47 -0700
Subject: [PATCH] More FFI stuff

---
 .../oracle/truffle/r/engine/shell/REmbedded.java  |  4 ++++
 .../fficall/src/jni/Rembedded.c                   | 11 +++++++----
 .../fficall/src/jni/Rinternals.c                  | 15 +++++++++++++--
 .../oracle/truffle/r/runtime/ffi/RFFIUtils.java   |  2 +-
 .../truffle/r/runtime/ffi/jnr/CallRFFIHelper.java | 10 +++++++++-
 5 files changed, 34 insertions(+), 8 deletions(-)

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 42a9482ac6..43d1fa997f 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
@@ -113,4 +113,8 @@ public class REmbedded {
         runRmainloop(vm);
     }
 
+    private static void R_Suicide(String msg) {
+
+    }
+
 }
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Rembedded.c b/com.oracle.truffle.r.native/fficall/src/jni/Rembedded.c
index 6c00b92146..4bde43ed94 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Rembedded.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Rembedded.c
@@ -14,6 +14,7 @@
 #include <sys/stat.h>
 #include <rffiutils.h>
 #include <R_ext/RStartup.h>
+#define R_INTERFACE_PTRS
 #include <Rinterface.h>
 
 extern char **environ;
@@ -217,9 +218,8 @@ void R_SaveGlobalEnvToFile(const char *f) {
 	unimplemented("R_SaveGlobalEnvToFile");
 }
 
-void R_Suicide(const char *x) {
-	unimplemented("R_Suicide");
-}
+void R_Suicide(const char *s) { ptr_R_Suicide(s); }
+
 
 void R_DefParams(Rstart rs) {
     // These are the GnuR defaults and correspond to the settings in RStartParams
@@ -298,7 +298,10 @@ void Rf_mainloop(void) {
 // functions that can be assigned by an embedded client to change behavior
 
 void uR_Suicide(const char *x) {
-	unimplemented("R_Suicide");
+	JNIEnv *jniEnv = getEnv();
+	jstring msg = (*jniEnv)->NewStringUTF(jniEnv, x);
+	jmethodID suicideMethod = checkGetMethodID(jniEnv, rembeddedClass, "R_Suicide", "(Ljava/lang/String;)V", 1);
+	(*jniEnv)->CallStaticVoidMethod(jniEnv, rembeddedClass, suicideMethod, msg);
 }
 
 void uR_ShowMessage(const char *x) {
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Rinternals.c b/com.oracle.truffle.r.native/fficall/src/jni/Rinternals.c
index 5d5cf9f4d1..b093067e62 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Rinternals.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Rinternals.c
@@ -96,6 +96,7 @@ static jmethodID SET_RDEBUGMethodID;
 static jmethodID RSTEPMethodID;
 static jmethodID SET_RSTEPMethodID;
 static jmethodID ENCLOSMethodID;
+static jmethodID R_lsInternal3MethodID;
 
 static jclass rErrorHandlingClass;
 static jclass handlerStacksClass;
@@ -189,6 +190,7 @@ void init_internals(JNIEnv *env) {
 	RSTEPMethodID = checkGetMethodID(env, CallRFFIHelperClass, "RSTEP", "(Ljava/lang/Object;)I", 1);
 	SET_RSTEPMethodID = checkGetMethodID(env, CallRFFIHelperClass, "SET_RSTEP", "(Ljava/lang/Object;I)V", 1);
 	ENCLOSMethodID = checkGetMethodID(env, CallRFFIHelperClass, "ENCLOS", "(Ljava/lang/Object;)Ljava/lang/Object;", 1);
+	R_lsInternal3MethodID = checkGetMethodID(env, CallRFFIHelperClass, "R_lsInternal3", "(Ljava/lang/Object;II)Ljava/lang/Object;", 1);
 
 	rErrorHandlingClass = checkFindClass(env, "com/oracle/truffle/r/runtime/RErrorHandling");
 	handlerStacksClass = checkFindClass(env, "com/oracle/truffle/r/runtime/RErrorHandling$HandlerStacks");
@@ -671,10 +673,19 @@ SEXP Rf_xlengthgets(SEXP x, R_xlen_t y) {
 
 }
 
-SEXP R_lsInternal(SEXP x, Rboolean y) {
-	return unimplemented("R_lsInternal");
+SEXP R_lsInternal(SEXP env, Rboolean all) {
+	return R_lsInternal3(env, all, TRUE);
 }
 
+SEXP R_lsInternal3(SEXP env, Rboolean all, Rboolean sorted) {
+	JNIEnv *thisenv = getEnv();
+	SEXP result = (*thisenv)->CallStaticObjectMethod(thisenv, CallRFFIHelperClass, R_lsInternal3MethodID, env, all, sorted);
+	return checkRef(thisenv, result);
+
+
+}
+
+
 SEXP Rf_namesgets(SEXP x, SEXP y) {
 	return unimplemented("Rf_namesgets");
 }
diff --git a/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/RFFIUtils.java b/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/RFFIUtils.java
index 01dc849c60..26f1c5599d 100644
--- a/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/RFFIUtils.java
+++ b/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/RFFIUtils.java
@@ -36,7 +36,7 @@ public class RFFIUtils {
     /**
      * Set this to {@code true} when it is not possible to set {@link FastROptions}.
      */
-    private static boolean alwaysTrace;
+    private static boolean alwaysTrace = true;
 
     public static byte[] wrapChar(char v) {
         return new byte[]{(byte) v};
diff --git a/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/jnr/CallRFFIHelper.java b/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/jnr/CallRFFIHelper.java
index fd341adf53..04d6011744 100644
--- a/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/jnr/CallRFFIHelper.java
+++ b/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/jnr/CallRFFIHelper.java
@@ -107,7 +107,7 @@ public class CallRFFIHelper {
     }
 
     private static RuntimeException unimplemented(String message) {
-        System.out.println(message);
+        System.err.println(message);
         try {
             throw RInternalError.unimplemented(message);
         } catch (Error e) {
@@ -919,6 +919,14 @@ public class CallRFFIHelper {
 
     }
 
+    public static Object R_lsInternal3(Object envArg, int allArg, int sortedArg) {
+        RFFIUtils.traceUpCall("R_lsInternal3", envArg, allArg, sortedArg);
+        boolean sorted = sortedArg != 0;
+        boolean all = allArg != 0;
+        REnvironment env = guaranteeInstanceOf(envArg, REnvironment.class);
+        return env.ls(all, null, sorted);
+    }
+
     @SuppressWarnings("unused")
     private static String R_HomeDir() {
         RFFIUtils.traceUpCall("R_HomeDir");
-- 
GitLab