From 3da72a72fe716bf0969f2528525dff8316e08bc0 Mon Sep 17 00:00:00 2001
From: Lukas Stadler <lukas.stadler@oracle.com>
Date: Fri, 28 Jul 2017 09:45:13 +0200
Subject: [PATCH] various small fixes and cleanups in FFI

---
 .../truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java     | 6 +++---
 .../truffle/r/ffi/impl/common/TracingUpCallsRFFIImpl.java  | 5 ++---
 .../src/com/oracle/truffle/r/ffi/impl/nodes/MiscNodes.java | 5 +----
 .../oracle/truffle/r/ffi/impl/upcalls/StdUpCallsRFFI.java  | 7 +++----
 .../src/com/oracle/truffle/r/library/tools/DirChmod.java   | 5 ++++-
 com.oracle.truffle.r.native/fficall/src/jni/Rinternals.c   | 6 +++---
 .../fficall/src/truffle_common/Rinternals_truffle_common.h | 3 +--
 .../fficall/src/truffle_nfi/Rinternals.c                   | 7 +++++--
 .../com/oracle/truffle/r/runtime/interop/Foreign2R.java    | 6 +++---
 9 files changed, 25 insertions(+), 25 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 00481e0268..f58760ece3 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
@@ -1036,7 +1036,7 @@ public abstract class JavaUpCallsRFFIImpl implements UpCallsRFFI {
     }
 
     @Override
-    public Object R_tryEval(Object expr, Object env, boolean silent) {
+    public Object R_tryEval(Object expr, Object env, int silent) {
         Object handlerStack = RErrorHandling.getHandlerStack();
         Object restartStack = RErrorHandling.getRestartStack();
         try {
@@ -1412,10 +1412,10 @@ public abstract class JavaUpCallsRFFIImpl implements UpCallsRFFI {
     }
 
     @Override
-    public REnvironment R_NewHashedEnv(REnvironment parent, Object initialSize) {
+    public REnvironment R_NewHashedEnv(Object parent, Object initialSize) {
         // We know this is an RIntVector from use site in gramRd.c
         REnvironment env = RDataFactory.createNewEnv(REnvironment.UNNAMED, true, ((RIntVector) initialSize).getDataAt(0));
-        RArguments.initializeEnclosingFrame(env.getFrame(), parent.getFrame());
+        RArguments.initializeEnclosingFrame(env.getFrame(), guaranteeInstanceOf(parent, REnvironment.class).getFrame());
         return env;
     }
 
diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/TracingUpCallsRFFIImpl.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/TracingUpCallsRFFIImpl.java
index ad14f4a827..5a483d0457 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/TracingUpCallsRFFIImpl.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/TracingUpCallsRFFIImpl.java
@@ -28,7 +28,6 @@ import com.oracle.truffle.r.runtime.data.RExternalPtr;
 import com.oracle.truffle.r.runtime.data.RIntVector;
 import com.oracle.truffle.r.runtime.data.RLogicalVector;
 import com.oracle.truffle.r.runtime.data.RStringVector;
-import com.oracle.truffle.r.runtime.env.REnvironment;
 
 final class TracingUpCallsRFFIImpl implements UpCallsRFFI {
     // Checkstyle: stop method name check
@@ -484,7 +483,7 @@ final class TracingUpCallsRFFIImpl implements UpCallsRFFI {
     }
 
     @Override
-    public Object R_tryEval(Object expr, Object env, boolean silent) {
+    public Object R_tryEval(Object expr, Object env, int silent) {
         RFFIUtils.traceUpCall("R_tryEval", expr, env, silent);
         return delegate.R_tryEval(expr, env, silent);
     }
@@ -736,7 +735,7 @@ final class TracingUpCallsRFFIImpl implements UpCallsRFFI {
     }
 
     @Override
-    public REnvironment R_NewHashedEnv(REnvironment parent, Object initialSize) {
+    public Object R_NewHashedEnv(Object parent, Object initialSize) {
         RFFIUtils.traceUpCall("R_NewHashedEnv", parent, initialSize);
         return delegate.R_NewHashedEnv(parent, initialSize);
     }
diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/MiscNodes.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/MiscNodes.java
index edcb77b445..8a438e02e7 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/MiscNodes.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/MiscNodes.java
@@ -23,11 +23,9 @@
 package com.oracle.truffle.r.ffi.impl.nodes;
 
 import com.oracle.truffle.api.CompilerDirectives;
-import com.oracle.truffle.api.dsl.Cached;
 import com.oracle.truffle.api.dsl.Fallback;
 import com.oracle.truffle.api.dsl.Specialization;
 import com.oracle.truffle.api.dsl.TypeSystemReference;
-import com.oracle.truffle.api.profiles.ValueProfile;
 import com.oracle.truffle.r.nodes.access.AccessSlotNode;
 import com.oracle.truffle.r.nodes.access.AccessSlotNodeGen;
 import com.oracle.truffle.r.nodes.access.UpdateSlotNode;
@@ -86,8 +84,7 @@ public final class MiscNodes {
         }
 
         @Specialization
-        protected int length(REnvironment env,
-                        @Cached("createClassProfile()") ValueProfile frameAccessProfile) {
+        protected int length(REnvironment env) {
             // May seem wasteful of resources, but simple env.getFrame().getDescriptor().getSize()
             // is not correct!
             return env.ls(true, null, false).getLength();
diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/StdUpCallsRFFI.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/StdUpCallsRFFI.java
index edb3a9d21d..732ca80749 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/StdUpCallsRFFI.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/StdUpCallsRFFI.java
@@ -28,7 +28,6 @@ import com.oracle.truffle.r.runtime.data.RExternalPtr;
 import com.oracle.truffle.r.runtime.data.RIntVector;
 import com.oracle.truffle.r.runtime.data.RLogicalVector;
 import com.oracle.truffle.r.runtime.data.RStringVector;
-import com.oracle.truffle.r.runtime.env.REnvironment;
 
 /**
  * This class defines methods that match the functionality of the macro/function definitions in the
@@ -96,7 +95,7 @@ public interface StdUpCallsRFFI {
 
     int /* void */ Rf_setAttrib(Object obj, Object name, Object val);
 
-    int Rf_inherits(@RFFICstring Object x, Object clazz);
+    int Rf_inherits(Object x, @RFFICstring Object clazz);
 
     Object Rf_install(@RFFICstring Object name);
 
@@ -204,7 +203,7 @@ public interface StdUpCallsRFFI {
 
     int /* void */ Rf_copyMatrix(Object s, Object t, int byrow);
 
-    Object R_tryEval(Object expr, Object env, boolean silent);
+    Object R_tryEval(Object expr, Object env, int silent);
 
     Object R_ToplevelExec();
 
@@ -258,7 +257,7 @@ public interface StdUpCallsRFFI {
 
     int /* void */ R_CleanUp(int sa, int status, int runlast);
 
-    REnvironment R_NewHashedEnv(REnvironment parent, Object initialSize);
+    Object R_NewHashedEnv(Object parent, Object initialSize);
 
     int PRSEEN(Object x);
 
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/tools/DirChmod.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/tools/DirChmod.java
index 98db4f99c4..27121e5966 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/tools/DirChmod.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/tools/DirChmod.java
@@ -54,7 +54,10 @@ public abstract class DirChmod extends RExternalBuiltinNode.Arg2 {
         Path path = FileSystems.getDefault().getPath(pathName);
         int fileMask = setGroupWrite ? GRPWRITE_FILE_MASK : FILE_MASK;
         int dirMask = setGroupWrite ? GRPWRITE_DIR_MASK : DIR_MASK;
-        assert path.isAbsolute();
+        if (!path.toFile().exists()) {
+            return RNull.instance;
+        }
+        assert path.isAbsolute() : path;
         try (Stream<Path> stream = Files.walk(path, Integer.MAX_VALUE)) {
             Iterator<Path> iter = stream.iterator();
             while (iter.hasNext()) {
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 15589e93c3..642fdd7ad1 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Rinternals.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Rinternals.c
@@ -178,7 +178,7 @@ void init_internals(JNIEnv *env) {
 	Rf_allocArrayMethodID = checkGetMethodID(env, UpCallsRFFIClass, "Rf_allocArray", "(ILjava/lang/Object;)Ljava/lang/Object;", 0);
 	Rf_duplicateMethodID = checkGetMethodID(env, UpCallsRFFIClass, "Rf_duplicate", "(Ljava/lang/Object;I)Ljava/lang/Object;", 0);
 	Rf_any_duplicatedMethodID = checkGetMethodID(env, UpCallsRFFIClass, "Rf_any_duplicated", "(Ljava/lang/Object;I)J", 0);
-	R_NewHashedEnvMethodID = checkGetMethodID(env, UpCallsRFFIClass, "R_NewHashedEnv", "(Lcom/oracle/truffle/r/runtime/env/REnvironment;Ljava/lang/Object;)Lcom/oracle/truffle/r/runtime/env/REnvironment;", 0);
+	R_NewHashedEnvMethodID = checkGetMethodID(env, UpCallsRFFIClass, "R_NewHashedEnv", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", 0);
 	Rf_classgetsMethodID = checkGetMethodID(env, UpCallsRFFIClass, "Rf_classgets", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", 0);
 	RprintfMethodID = checkGetMethodID(env, UpCallsRFFIClass, "Rprintf", "(Ljava/lang/Object;)I", 0);
 	R_do_MAKE_CLASS_MethodID = checkGetMethodID(env, UpCallsRFFIClass, "R_do_MAKE_CLASS", "(Ljava/lang/Object;)Ljava/lang/Object;", 0);
@@ -233,7 +233,7 @@ void init_internals(JNIEnv *env) {
 	IS_S4_OBJECTMethodID = checkGetMethodID(env, UpCallsRFFIClass, "IS_S4_OBJECT", "(Ljava/lang/Object;)I", 0);
 	SET_S4_OBJECTMethodID = checkGetMethodID(env, UpCallsRFFIClass, "SET_S4_OBJECT", "(Ljava/lang/Object;)I", 0);
 	UNSET_S4_OBJECTMethodID = checkGetMethodID(env, UpCallsRFFIClass, "UNSET_S4_OBJECT", "(Ljava/lang/Object;)I", 0);
-	R_tryEvalMethodID = checkGetMethodID(env, UpCallsRFFIClass, "R_tryEval", "(Ljava/lang/Object;Ljava/lang/Object;Z)Ljava/lang/Object;", 0);
+	R_tryEvalMethodID = checkGetMethodID(env, UpCallsRFFIClass, "R_tryEval", "(Ljava/lang/Object;Ljava/lang/Object;I)Ljava/lang/Object;", 0);
 	RDEBUGMethodID = checkGetMethodID(env, UpCallsRFFIClass, "RDEBUG", "(Ljava/lang/Object;)I", 0);
 	SET_RDEBUGMethodID = checkGetMethodID(env, UpCallsRFFIClass, "SET_RDEBUG", "(Ljava/lang/Object;I)I", 0);
 	RSTEPMethodID = checkGetMethodID(env, UpCallsRFFIClass, "RSTEP", "(Ljava/lang/Object;)I", 0);
@@ -1390,7 +1390,7 @@ SEXP Rf_asS4(SEXP x, Rboolean b, int i) {
 static SEXP R_tryEvalInternal(SEXP x, SEXP y, int *ErrorOccurred, jboolean silent) {
 	JNIEnv *thisenv = getEnv();
     updateNativeArrays(thisenv);
-	jobject tryResult =  (*thisenv)->CallObjectMethod(thisenv, UpCallsRFFIObject, R_tryEvalMethodID, x, y, silent);
+	jobject tryResult =  (*thisenv)->CallObjectMethod(thisenv, UpCallsRFFIObject, R_tryEvalMethodID, x, y, (int) silent);
 	// If tryResult is NULL, an error occurred
 	if (ErrorOccurred) {
 		*ErrorOccurred = tryResult == NULL;
diff --git a/com.oracle.truffle.r.native/fficall/src/truffle_common/Rinternals_truffle_common.h b/com.oracle.truffle.r.native/fficall/src/truffle_common/Rinternals_truffle_common.h
index 81f9961f6c..5e7b770f35 100644
--- a/com.oracle.truffle.r.native/fficall/src/truffle_common/Rinternals_truffle_common.h
+++ b/com.oracle.truffle.r.native/fficall/src/truffle_common/Rinternals_truffle_common.h
@@ -793,8 +793,7 @@ int TYPEOF(SEXP x) {
 }
 
 SEXP ATTRIB(SEXP x){
-    unimplemented("ATTRIB");
-    return NULL;
+    return ((call_ATTRIB) callbacks[ATTRIB_x])(x);
 }
 
 int OBJECT(SEXP x){
diff --git a/com.oracle.truffle.r.native/fficall/src/truffle_nfi/Rinternals.c b/com.oracle.truffle.r.native/fficall/src/truffle_nfi/Rinternals.c
index b21117fe72..f38558150f 100644
--- a/com.oracle.truffle.r.native/fficall/src/truffle_nfi/Rinternals.c
+++ b/com.oracle.truffle.r.native/fficall/src/truffle_nfi/Rinternals.c
@@ -53,11 +53,14 @@ void *ensure_string(const char * x) {
 }
 
 SEXP newObjectHandle(SEXP x) {
-	return newObjectRef(x);
+    TruffleEnv* env = (*truffleContext)->getTruffleEnv(truffleContext);
+	SEXP result = (*env)->newObjectRef(env, x);
+	return result;
 }
 
 void releaseObjectHandle(SEXP x) {
-	releaseObjectRef(x);
+    TruffleEnv* env = (*truffleContext)->getTruffleEnv(truffleContext);
+	(*env)->releaseObjectRef(env, x);
 }
 
 #include "../truffle_common/Rinternals_truffle_common.h"
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/interop/Foreign2R.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/interop/Foreign2R.java
index 55e135df2c..0ab8bbb6bb 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/interop/Foreign2R.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/interop/Foreign2R.java
@@ -36,8 +36,8 @@ public abstract class Foreign2R extends RBaseNode {
     public abstract Object execute(Object obj);
 
     @Specialization
-    public byte doBoolean(Boolean obj) {
-        return RRuntime.asLogical((boolean) obj);
+    public byte doBoolean(boolean obj) {
+        return RRuntime.asLogical(obj);
     }
 
     @Specialization
@@ -66,7 +66,7 @@ public abstract class Foreign2R extends RBaseNode {
     }
 
     @Specialization(guards = "isNull(obj)")
-    public RNull doNull(Object obj) {
+    public RNull doNull(@SuppressWarnings("unused") Object obj) {
         return RNull.instance;
     }
 
-- 
GitLab