Skip to content

Commit a8df096

Browse files
author
Pavel Marek
committed
Foreign wrappers and native mirrors for global var descriptors are static
1 parent 556a90c commit a8df096

File tree

9 files changed

+515
-439
lines changed

9 files changed

+515
-439
lines changed

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.oracle.truffle.r.runtime.conn.RConnection;
6666
import com.oracle.truffle.r.runtime.context.Engine.IncompleteSourceException;
6767
import com.oracle.truffle.r.runtime.context.Engine.ParseException;
68+
import com.oracle.truffle.r.runtime.context.GlobalNativeVarContext;
6869
import com.oracle.truffle.r.runtime.context.RContext;
6970
import com.oracle.truffle.r.runtime.data.CharSXPWrapper;
7071
import com.oracle.truffle.r.runtime.data.RArgsValuesAndNames;
@@ -2818,8 +2819,11 @@ public void FASTR_GlobalVarSetInt(Object globVarDescr, int value, RContext conte
28182819
public int FASTR_GlobalVarGetInt(Object globVarDescr, RContext context) {
28192820
assert globVarDescr instanceof RForeignObjectWrapper;
28202821
Object result = context.stateglobalNativeVar.getGlobalVar(((RForeignObjectWrapper) globVarDescr).getDelegate(), InteropLibrary.getUncached());
2821-
assert result instanceof Integer;
2822-
return (int) result;
2822+
if (!(result instanceof Integer)) {
2823+
throw RInternalError.shouldNotReachHere("FASTR_GlobalVarGetInt should return an integer, have you set integer via FASTR_GlobalVarSetInt before?");
2824+
} else {
2825+
return (int) result;
2826+
}
28232827
}
28242828

28252829
@Override
@@ -2832,8 +2836,11 @@ public void FASTR_GlobalVarSetDouble(Object globVarDescr, double value, RContext
28322836
public double FASTR_GlobalVarGetDouble(Object globVarDescr, RContext context) {
28332837
assert globVarDescr instanceof RForeignObjectWrapper;
28342838
Object result = context.stateglobalNativeVar.getGlobalVar(((RForeignObjectWrapper) globVarDescr).getDelegate(), InteropLibrary.getUncached());
2835-
assert result instanceof Double;
2836-
return (double) result;
2839+
if (!(result instanceof Double)) {
2840+
throw RInternalError.shouldNotReachHere("FASTR_GlobalVarGetDouble should return double, have you set double via FASTR_GlobalVarSetDouble before?");
2841+
} else {
2842+
return (double) result;
2843+
}
28372844
}
28382845

28392846
@Override
@@ -2846,8 +2853,17 @@ public void FASTR_GlobalVarSetBool(Object globVarDescr, boolean value, RContext
28462853
public boolean FASTR_GlobalVarGetBool(Object globVarDescr, RContext context) {
28472854
assert globVarDescr instanceof RForeignObjectWrapper;
28482855
Object result = context.stateglobalNativeVar.getGlobalVar(((RForeignObjectWrapper) globVarDescr).getDelegate(), InteropLibrary.getUncached());
2849-
assert result instanceof Boolean;
2850-
return (boolean) result;
2856+
if (!(result instanceof Boolean)) {
2857+
throw RInternalError.shouldNotReachHere("FASTR_GlobalVarGetBool should return bool, have you set bool via FASTR_GlobalVarSetBool before?");
2858+
} else {
2859+
return (boolean) result;
2860+
}
2861+
}
2862+
2863+
@Override
2864+
@TruffleBoundary
2865+
public void FASTR_GlobalVarPrintDescrs(RContext context) {
2866+
GlobalNativeVarContext.printAllDescriptors(context);
28512867
}
28522868

28532869
@Override

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/mixed/TruffleMixed_Context.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ public ContextState initialize(RContext context) {
117117
return this;
118118
}
119119

120-
/**
121-
* Currently, calls the native function only via NFI.
122-
*/
123120
@Override
124121
public Object callNativeFunction(Object nativeFunc, Type nativeFuncType, String signature, Object[] args, boolean[] whichArgToWrap) {
125122
switch (nativeFuncType) {

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/FastRUpCalls.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,25 @@ public interface FastRUpCalls {
6464
Object FASTR_DATAPTR(Object x);
6565

6666
// Global Var API
67+
68+
/**
69+
* Allocates a native global variable descriptor. Must be called at most once per DLL load.
70+
*/
6771
Object FASTR_GlobalVarAlloc(@RFFIInject RContext context);
6872

73+
/**
74+
* Initializes the {@code globVarDescr} with the information about the current context.
75+
* i.e. assign an index into the per-context array of global native variables.
76+
* Must be called at most once for every context.
77+
*/
6978
void FASTR_GlobalVarInit(Object globVarDescr, @RFFIInject RContext context);
7079

80+
/**
81+
* Same as {@link #FASTR_GlobalVarInit(Object, RContext)}, but also registers given destructor
82+
* to be called later during the context finalization.
83+
*
84+
* @param destructorNativeFunc Native function that will be called before the context finalizes.
85+
*/
7186
void FASTR_GlobalVarInitWithDtor(Object globVarDescr, @RFFICpointer Object destructorNativeFunc, @RFFIInject RContext context);
7287

7388
void FASTR_GlobalVarSetSEXP(Object globVarDescr, Object value, @RFFIInject RContext context);
@@ -90,4 +105,9 @@ public interface FastRUpCalls {
90105
void FASTR_GlobalVarSetBool(Object globalVarDescr, boolean value, @RFFIInject RContext context);
91106

92107
boolean FASTR_GlobalVarGetBool(Object globalVarDescr, @RFFIInject RContext context);
108+
109+
/**
110+
* Prints all descriptors for all contexts. For debugging purposes.
111+
*/
112+
void FASTR_GlobalVarPrintDescrs(@RFFIInject RContext context);
93113
}

com.oracle.truffle.r.native/fficall/src/common/rffi_upcalls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ typedef void (*call_FASTR_GlobalVarSetDouble)(FASTR_GlobalVar_t descr, double va
465465
typedef double (*call_FASTR_GlobalVarGetDouble)(FASTR_GlobalVar_t descr);
466466
typedef void (*call_FASTR_GlobalVarSetBool)(FASTR_GlobalVar_t descr, Rboolean value);
467467
typedef Rboolean (*call_FASTR_GlobalVarGetBool)(FASTR_GlobalVar_t descr);
468+
typedef void (*call_FASTR_GlobalVarPrintDescrs)();
468469
// ==========================================================
469470
// ALTREP framework
470471
// ==========================================================

0 commit comments

Comments
 (0)