From 9e1bd3884e74db9a1adda919169b9dfe27bddbcd Mon Sep 17 00:00:00 2001
From: stepan <stepan.sindelar@oracle.com>
Date: Fri, 23 Feb 2018 14:14:00 +0100
Subject: [PATCH] RFFI: implement R_getEmbeddingDllInfo up-call

---
 .../ffi/impl/common/JavaUpCallsRFFIImpl.java  |  5 ++++
 .../r/ffi/impl/upcalls/DLLUpCallsRFFI.java    |  9 +++++++-
 .../fficall/src/common/rffi_upcalls.h         |  1 +
 .../fficall/src/common/rffi_upcallsindex.h    | 23 ++++++++++---------
 .../Rinternals_truffle_common.h               |  7 ++++++
 com.oracle.truffle.r.native/version.source    |  2 +-
 .../com/oracle/truffle/r/runtime/ffi/DLL.java |  9 ++++++++
 7 files changed, 43 insertions(+), 13 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 4039b565c5..ecb9e56080 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
@@ -1589,6 +1589,11 @@ public abstract class JavaUpCallsRFFIImpl implements UpCallsRFFI {
         return new DotSymbol(name, new SymbolHandle(fun), numArgs);
     }
 
+    @Override
+    public Object getEmbeddingDLLInfo() {
+        return DLL.getEmbeddingDLLInfo();
+    }
+
     protected abstract Object setSymbol(DLLInfo dllInfo, int nstOrd, Object routines, int index);
 
     @Override
diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/DLLUpCallsRFFI.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/DLLUpCallsRFFI.java
index 336062d6f0..3064448d1d 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/DLLUpCallsRFFI.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/DLLUpCallsRFFI.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 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
@@ -71,4 +71,11 @@ public interface DLLUpCallsRFFI {
     @RFFICpointer
     Object getCCallable(@RFFICstring String pkgName, @RFFICstring String functionName);
 
+    /**
+     * Returns special {@link com.oracle.truffle.r.runtime.ffi.DLL.DLLInfo} instance that is
+     * supposed to be used when registering symbols from within code embedding R, i.e. code that
+     * cannot have its init method called by R runtime and must call {@code R_registerRoutines}
+     * itself.
+     */
+    Object getEmbeddingDLLInfo();
 }
diff --git a/com.oracle.truffle.r.native/fficall/src/common/rffi_upcalls.h b/com.oracle.truffle.r.native/fficall/src/common/rffi_upcalls.h
index f483086b44..307b1d2fcd 100644
--- a/com.oracle.truffle.r.native/fficall/src/common/rffi_upcalls.h
+++ b/com.oracle.truffle.r.native/fficall/src/common/rffi_upcalls.h
@@ -315,6 +315,7 @@ typedef void * (*call_setDotSymbolValues)(DllInfo *dllInfo, char *name, DL_FUNC
 typedef int (*call_forceSymbols)(DllInfo *dllInfo, Rboolean value);
 typedef int (*call_registerCCallable)(const char *pkgname, const char *name, void *fun);
 typedef void* (*call_getCCallable)(const char *pkgname, const char *name);
+typedef DllInfo* (*call_getEmbeddingDLLInfo)(void);
 
 // memory
 
diff --git a/com.oracle.truffle.r.native/fficall/src/common/rffi_upcallsindex.h b/com.oracle.truffle.r.native/fficall/src/common/rffi_upcallsindex.h
index afa389d701..fa00c66c35 100644
--- a/com.oracle.truffle.r.native/fficall/src/common/rffi_upcallsindex.h
+++ b/com.oracle.truffle.r.native/fficall/src/common/rffi_upcallsindex.h
@@ -181,17 +181,18 @@
 #define forceSymbols_x 176
 #define getCCallable_x 177
 #define getConnectionClassString_x 178
-#define getOpenModeString_x 179
-#define getSummaryDescription_x 180
-#define isSeekable_x 181
-#define octsize_x 182
-#define registerCCallable_x 183
-#define registerRoutines_x 184
-#define restoreHandlerStacks_x 185
-#define setDotSymbolValues_x 186
-#define unif_rand_x 187
-#define useDynamicSymbols_x 188
+#define getEmbeddingDLLInfo_x 179
+#define getOpenModeString_x 180
+#define getSummaryDescription_x 181
+#define isSeekable_x 182
+#define octsize_x 183
+#define registerCCallable_x 184
+#define registerRoutines_x 185
+#define restoreHandlerStacks_x 186
+#define setDotSymbolValues_x 187
+#define unif_rand_x 188
+#define useDynamicSymbols_x 189
 
-#define UPCALLS_TABLE_SIZE 189
+#define UPCALLS_TABLE_SIZE 190
 
 #endif // RFFI_UPCALLSINDEX_H
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 7d361d1da1..d512182aaf 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
@@ -1664,6 +1664,13 @@ R_registerRoutines(DllInfo *info, const R_CMethodDef * const croutines,
     return 1;
 }
 
+DllInfo *R_getEmbeddingDllInfo() {
+    TRACE0();
+    DllInfo *result = ((call_getEmbeddingDLLInfo) callbacks[getEmbeddingDLLInfo_x])();
+    checkExitCall();
+    return result;
+}
+
 Rboolean R_useDynamicSymbols(DllInfo *dllInfo, Rboolean value) {
     TRACE0();
     Rboolean result = ((call_useDynamicSymbols) callbacks[useDynamicSymbols_x])(dllInfo, value);
diff --git a/com.oracle.truffle.r.native/version.source b/com.oracle.truffle.r.native/version.source
index 82cced27d7..0691f67b20 100644
--- a/com.oracle.truffle.r.native/version.source
+++ b/com.oracle.truffle.r.native/version.source
@@ -1 +1 @@
-51
+52
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/DLL.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/DLL.java
index 07b6aeba98..ce08cd0808 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/DLL.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/DLL.java
@@ -217,6 +217,14 @@ public class DLL {
             return result;
         }
 
+        /**
+         * Embedding {@link DLLInfo} is just a placeholder. It does not represent any concrete dll
+         * and thus we e.g. cannot find any symbols in it.
+         */
+        public boolean isEmbeddingDllInfo() {
+            return handle == null;
+        }
+
         public void setNativeSymbols(int nstOrd, DotSymbol[] symbols) {
             nativeSymbols[nstOrd] = symbols;
         }
@@ -758,6 +766,7 @@ public class DLL {
      */
     public static SymbolHandle findSymbol(String name, DLLInfo dllInfo) {
         if (dllInfo != null) {
+            assert !dllInfo.isEmbeddingDllInfo() : "Dynamic symbols lookup is not supported for the embedding DLLInfo";
             return (SymbolHandle) DLLRFFI.DLSymRootNode.create(RContext.getInstance()).call(dllInfo.handle, name);
         } else {
             return (SymbolHandle) RFindSymbolRootNode.create(RContext.getInstance()).call(name, null, RegisteredNativeSymbol.any());
-- 
GitLab