Skip to content

Commit

Permalink
Foreign wrappers and native mirrors for global var descriptors are st…
Browse files Browse the repository at this point in the history
…atic
  • Loading branch information
Pavel Marek committed Sep 2, 2022
1 parent 556a90c commit a8df096
Show file tree
Hide file tree
Showing 9 changed files with 515 additions and 439 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import com.oracle.truffle.r.runtime.conn.RConnection;
import com.oracle.truffle.r.runtime.context.Engine.IncompleteSourceException;
import com.oracle.truffle.r.runtime.context.Engine.ParseException;
import com.oracle.truffle.r.runtime.context.GlobalNativeVarContext;
import com.oracle.truffle.r.runtime.context.RContext;
import com.oracle.truffle.r.runtime.data.CharSXPWrapper;
import com.oracle.truffle.r.runtime.data.RArgsValuesAndNames;
Expand Down Expand Up @@ -2818,8 +2819,11 @@ public void FASTR_GlobalVarSetInt(Object globVarDescr, int value, RContext conte
public int FASTR_GlobalVarGetInt(Object globVarDescr, RContext context) {
assert globVarDescr instanceof RForeignObjectWrapper;
Object result = context.stateglobalNativeVar.getGlobalVar(((RForeignObjectWrapper) globVarDescr).getDelegate(), InteropLibrary.getUncached());
assert result instanceof Integer;
return (int) result;
if (!(result instanceof Integer)) {
throw RInternalError.shouldNotReachHere("FASTR_GlobalVarGetInt should return an integer, have you set integer via FASTR_GlobalVarSetInt before?");
} else {
return (int) result;
}
}

@Override
Expand All @@ -2832,8 +2836,11 @@ public void FASTR_GlobalVarSetDouble(Object globVarDescr, double value, RContext
public double FASTR_GlobalVarGetDouble(Object globVarDescr, RContext context) {
assert globVarDescr instanceof RForeignObjectWrapper;
Object result = context.stateglobalNativeVar.getGlobalVar(((RForeignObjectWrapper) globVarDescr).getDelegate(), InteropLibrary.getUncached());
assert result instanceof Double;
return (double) result;
if (!(result instanceof Double)) {
throw RInternalError.shouldNotReachHere("FASTR_GlobalVarGetDouble should return double, have you set double via FASTR_GlobalVarSetDouble before?");
} else {
return (double) result;
}
}

@Override
Expand All @@ -2846,8 +2853,17 @@ public void FASTR_GlobalVarSetBool(Object globVarDescr, boolean value, RContext
public boolean FASTR_GlobalVarGetBool(Object globVarDescr, RContext context) {
assert globVarDescr instanceof RForeignObjectWrapper;
Object result = context.stateglobalNativeVar.getGlobalVar(((RForeignObjectWrapper) globVarDescr).getDelegate(), InteropLibrary.getUncached());
assert result instanceof Boolean;
return (boolean) result;
if (!(result instanceof Boolean)) {
throw RInternalError.shouldNotReachHere("FASTR_GlobalVarGetBool should return bool, have you set bool via FASTR_GlobalVarSetBool before?");
} else {
return (boolean) result;
}
}

@Override
@TruffleBoundary
public void FASTR_GlobalVarPrintDescrs(RContext context) {
GlobalNativeVarContext.printAllDescriptors(context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ public ContextState initialize(RContext context) {
return this;
}

/**
* Currently, calls the native function only via NFI.
*/
@Override
public Object callNativeFunction(Object nativeFunc, Type nativeFuncType, String signature, Object[] args, boolean[] whichArgToWrap) {
switch (nativeFuncType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,25 @@ public interface FastRUpCalls {
Object FASTR_DATAPTR(Object x);

// Global Var API

/**
* Allocates a native global variable descriptor. Must be called at most once per DLL load.
*/
Object FASTR_GlobalVarAlloc(@RFFIInject RContext context);

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

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

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

boolean FASTR_GlobalVarGetBool(Object globalVarDescr, @RFFIInject RContext context);

/**
* Prints all descriptors for all contexts. For debugging purposes.
*/
void FASTR_GlobalVarPrintDescrs(@RFFIInject RContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ typedef void (*call_FASTR_GlobalVarSetDouble)(FASTR_GlobalVar_t descr, double va
typedef double (*call_FASTR_GlobalVarGetDouble)(FASTR_GlobalVar_t descr);
typedef void (*call_FASTR_GlobalVarSetBool)(FASTR_GlobalVar_t descr, Rboolean value);
typedef Rboolean (*call_FASTR_GlobalVarGetBool)(FASTR_GlobalVar_t descr);
typedef void (*call_FASTR_GlobalVarPrintDescrs)();
// ==========================================================
// ALTREP framework
// ==========================================================
Expand Down
Loading

0 comments on commit a8df096

Please sign in to comment.