Skip to content
Snippets Groups Projects
Commit 9f2d638b authored by Mick Jordan's avatar Mick Jordan
Browse files

ffi: maintain stack of jmpbuf ptrs; run callEnter/callExit on Rembedded dowfalls

parent 35530e88
No related branches found
No related tags found
No related merge requests found
......@@ -406,14 +406,19 @@ void setupOverrides(void) {
}
static void REmbed_nativeWriteConsole(JNIEnv *jniEnv, jclass c, jstring string, int otype) {
int len = (*jniEnv)->GetStringUTFLength(jniEnv, string);
const char *cbuf = (*jniEnv)->GetStringUTFChars(jniEnv, string, NULL);
if (ptr_R_WriteConsole == NULL) {
(*ptr_R_WriteConsoleEx)(cbuf, len, otype);
} else {
(*ptr_R_WriteConsole)(cbuf, len);
jmp_buf error_jmpbuf;
callEnter(jniEnv, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
int len = (*jniEnv)->GetStringUTFLength(jniEnv, string);
const char *cbuf = (*jniEnv)->GetStringUTFChars(jniEnv, string, NULL);
if (ptr_R_WriteConsole == NULL) {
(*ptr_R_WriteConsoleEx)(cbuf, len, otype);
} else {
(*ptr_R_WriteConsole)(cbuf, len);
}
(*jniEnv)->ReleaseStringUTFChars(jniEnv, string, cbuf);
}
(*jniEnv)->ReleaseStringUTFChars(jniEnv, string, cbuf);
callExit(jniEnv);
}
JNIEXPORT void JNICALL Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1REmbed_nativeWriteConsole(JNIEnv *jniEnv, jclass c, jstring string) {
......@@ -425,22 +430,37 @@ JNIEXPORT void JNICALL Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1REmbed_nat
}
JNIEXPORT jstring JNICALL Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1REmbed_nativeReadConsole(JNIEnv *jniEnv, jclass c, jstring prompt) {
const char *cprompt = (*jniEnv)->GetStringUTFChars(jniEnv, prompt, NULL);
unsigned char cbuf[1024];
int n = (*ptr_R_ReadConsole)(cprompt, cbuf, 1024, 0);
jstring result;
result = (*jniEnv)->NewStringUTF(jniEnv, (const char *)cbuf);
(*jniEnv)->ReleaseStringUTFChars(jniEnv, prompt, cprompt);
jmp_buf error_jmpbuf;
jstring result = NULL;
callEnter(jniEnv, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
const char *cprompt = (*jniEnv)->GetStringUTFChars(jniEnv, prompt, NULL);
unsigned char cbuf[1024];
int n = (*ptr_R_ReadConsole)(cprompt, cbuf, 1024, 0);
result = (*jniEnv)->NewStringUTF(jniEnv, (const char *)cbuf);
(*jniEnv)->ReleaseStringUTFChars(jniEnv, prompt, cprompt);
}
callExit(jniEnv);
return result;
}
JNIEXPORT void JNICALL Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1REmbed_nativeCleanUp(JNIEnv *jniEnv, jclass c, jint x, jint y, jint z) {
jmp_buf error_jmpbuf;
callEnter(jniEnv, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
(*ptr_R_CleanUp)(x, y, z);
}
callExit(jniEnv);
}
JNIEXPORT void JNICALL Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1REmbed_nativeSuicide(JNIEnv *jniEnv, jclass c, jstring string) {
const char *cbuf = (*jniEnv)->GetStringUTFChars(jniEnv, string, NULL);
(*ptr_R_Suicide)(cbuf);
jmp_buf error_jmpbuf;
callEnter(jniEnv, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
const char *cbuf = (*jniEnv)->GetStringUTFChars(jniEnv, string, NULL);
(*ptr_R_Suicide)(cbuf);
}
callExit(jniEnv);
}
void uR_PolledEvents(void) {
......
......@@ -42,8 +42,6 @@ Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_nativeSetTempDir(JNIEnv
setTempDir(env, tempDir);
}
static jmp_buf error_jmpbuf;
// Boilerplate methods for the actual calls
typedef SEXP (*call0func)();
......@@ -343,6 +341,7 @@ typedef SEXP (*call64func)(SEXP arg1, SEXP arg2, SEXP arg3, SEXP arg4, SEXP arg5
JNIEXPORT jobject JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call0(JNIEnv *env, jclass c, jlong address) {
jmp_buf error_jmpbuf;
jobject result = NULL;
callEnter(env, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
......@@ -355,6 +354,7 @@ Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call0(JNIEnv *env, jclas
JNIEXPORT jobject JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call1(JNIEnv *env, jclass c, jlong address, jobject arg1) {
jmp_buf error_jmpbuf;
jobject result = NULL;
callEnter(env, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
......@@ -367,6 +367,7 @@ Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call1(JNIEnv *env, jclas
JNIEXPORT jobject JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call2(JNIEnv *env, jclass c, jlong address, jobject arg1, jobject arg2) {
jmp_buf error_jmpbuf;
jobject result = NULL;
callEnter(env, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
......@@ -380,6 +381,7 @@ Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call2(JNIEnv *env, jclas
JNIEXPORT jobject JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call3(JNIEnv *env, jclass c, jlong address, jobject arg1, jobject arg2,
jobject arg3) {
jmp_buf error_jmpbuf;
jobject result = NULL;
callEnter(env, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
......@@ -393,6 +395,7 @@ Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call3(JNIEnv *env, jclas
JNIEXPORT jobject JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call4(JNIEnv *env, jclass c, jlong address, jobject arg1, jobject arg2,
jobject arg3, jobject arg4) {
jmp_buf error_jmpbuf;
jobject result = NULL;
callEnter(env, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
......@@ -406,6 +409,7 @@ Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call4(JNIEnv *env, jclas
JNIEXPORT jobject JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call5(JNIEnv *env, jclass c, jlong address, jobject arg1, jobject arg2,
jobject arg3, jobject arg4, jobject arg5) {
jmp_buf error_jmpbuf;
jobject result = NULL;
callEnter(env, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
......@@ -419,6 +423,7 @@ Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call5(JNIEnv *env, jclas
JNIEXPORT jobject JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call6(JNIEnv *env, jclass c, jlong address, jobject arg1, jobject arg2,
jobject arg3, jobject arg4, jobject arg5, jobject arg6) {
jmp_buf error_jmpbuf;
jobject result = NULL;
callEnter(env, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
......@@ -432,6 +437,7 @@ Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call6(JNIEnv *env, jclas
JNIEXPORT jobject JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call7(JNIEnv *env, jclass c, jlong address, jobject arg1, jobject arg2,
jobject arg3, jobject arg4, jobject arg5, jobject arg6, jobject arg7) {
jmp_buf error_jmpbuf;
jobject result = NULL;
callEnter(env, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
......@@ -445,6 +451,7 @@ Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call7(JNIEnv *env, jclas
JNIEXPORT jobject JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call8(JNIEnv *env, jclass c, jlong address, jobject arg1, jobject arg2,
jobject arg3, jobject arg4, jobject arg5, jobject arg6, jobject arg7, jobject arg8) {
jmp_buf error_jmpbuf;
jobject result = NULL;
callEnter(env, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
......@@ -458,6 +465,7 @@ Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call8(JNIEnv *env, jclas
JNIEXPORT jobject JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call9(JNIEnv *env, jclass c, jlong address, jobject arg1, jobject arg2,
jobject arg3, jobject arg4, jobject arg5, jobject arg6, jobject arg7, jobject arg8, jobject arg9) {
jmp_buf error_jmpbuf;
jobject result = NULL;
callEnter(env, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
......@@ -470,6 +478,7 @@ Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call9(JNIEnv *env, jclas
JNIEXPORT jobject JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_call(JNIEnv *env, jclass c, jlong address, jobjectArray args) {
jmp_buf error_jmpbuf;
jobject result = NULL;
callEnter(env, &error_jmpbuf);
jsize len = (*env)->GetArrayLength(env, args);
......@@ -1214,6 +1223,7 @@ typedef void (*callVoid1func)(SEXP arg1);
JNIEXPORT void JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_callVoid1(JNIEnv *env, jclass c, jlong address, jobject arg1) {
jmp_buf error_jmpbuf;
callEnter(env, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
callVoid1func call1 = (callVoid1func) address;
......@@ -1226,6 +1236,7 @@ typedef void (*callVoid0func)();
JNIEXPORT void JNICALL
Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1CallRFFI_callVoid0(JNIEnv *env, jclass c, jlong address) {
jmp_buf error_jmpbuf;
callEnter(env, &error_jmpbuf);
if (!setjmp(error_jmpbuf)) {
callVoid0func call1 = (callVoid0func) address;
......
......@@ -43,7 +43,6 @@ jmethodID createSymbolMethodID;
static jmethodID validateMethodID;
static JNIEnv *curenv = NULL;
jmp_buf *callErrorJmpBuf;
// default for trace output when enabled
FILE *traceFile = NULL;
......@@ -84,10 +83,14 @@ void setEmbedded() {
}
// native down call depth, indexes nativeArrayTableHwmStack
int callDepth;
int callDepth = 0;
#define CALLDEPTH_STACK_SIZE 16
static int nativeArrayTableHwmStack[CALLDEPTH_STACK_SIZE];
// stack of jmp_buf ptrs for non-local control transfer on error
static jmp_buf* callErrorJmpBufTable[CALLDEPTH_STACK_SIZE];
#define NATIVE_ARRAY_TABLE_HWM_STACK_SIZE 16
int nativeArrayTableHwmStack[NATIVE_ARRAY_TABLE_HWM_STACK_SIZE] ;
void init_utils(JNIEnv *env) {
curenv = env;
......@@ -140,8 +143,9 @@ const char *stringToChars(JNIEnv *jniEnv, jstring string) {
void callEnter(JNIEnv *env, jmp_buf *jmpbuf) {
setEnv(env);
callErrorJmpBuf = jmpbuf;
if (callDepth >= NATIVE_ARRAY_TABLE_HWM_STACK_SIZE) {
//printf("callEnter: callDepth %d, jmpbufptr %p\n", callDepth, jmpbuf);
callErrorJmpBufTable[callDepth] = jmpbuf;
if (callDepth >= CALLDEPTH_STACK_SIZE) {
fatalError("call stack overflow\n");
}
nativeArrayTableHwmStack[callDepth] = nativeArrayTableHwm;
......@@ -149,7 +153,8 @@ void callEnter(JNIEnv *env, jmp_buf *jmpbuf) {
}
jmp_buf *getErrorJmpBuf() {
return callErrorJmpBuf;
// printf("getErrorJmpBuf: callDepth %d, jmpbufptr %p\n", callDepth, callErrorJmpBufTable[callDepth - 1]);
return callErrorJmpBufTable[callDepth - 1];
}
void callExit(JNIEnv *env) {
......
......@@ -64,6 +64,7 @@ void callExit(JNIEnv *env);
// called by callExit to deallocate transient memory
void allocExit();
// returns the jmp_buf at the current call depth
jmp_buf *getErrorJmpBuf();
// Given the x denotes an R vector type, return a pointer to
......
......@@ -30,8 +30,8 @@ rffi.TYPEOF <- function(x) {
.Call("invoke_TYPEOF", x, PACKAGE = "testrffi")
}
rffi.error <- function() {
.Call("invoke_error", PACKAGE = "testrffi")
rffi.error <- function(msg = "invoke_error in testrffi") {
.Call("invoke_error", msg, PACKAGE = "testrffi")
}
rffi.dotExternalAccessArgs <- function(...) {
......@@ -103,4 +103,10 @@ rffi.release_object <- function(x) {
invisible(.Call("release_object", x, PACKAGE = "testrffi"))
}
rffi.findvar <- function(x, env) {
if (is.character(x)) {
x = as.symbol(x)
}
.Call("findvar", x, env, PACKAGE = "testrffi")
}
......@@ -89,8 +89,8 @@ SEXP invoke_TYPEOF(SEXP x) {
return ScalarInteger(TYPEOF(x));
}
SEXP invoke_error() {
error("invoke_error in testrffi");
SEXP invoke_error(SEXP msg) {
error(R_CHAR(STRING_ELT(msg, 0)));
}
// returns a
......@@ -270,3 +270,13 @@ SEXP release_object(SEXP x) {
R_ReleaseObject(x);
return R_NilValue;
}
SEXP findvar(SEXP x, SEXP env) {
SEXP v = Rf_findVar(x, env);
if (v == R_UnboundValue) {
Rf_error("'%s' not found", R_CHAR(PRINTNAME(x)));
} else {
return v;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment