File: callJNI.c

package info (click to toggle)
rjava 0.9-3-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,196 kB
  • sloc: java: 7,733; ansic: 4,947; sh: 2,878; makefile: 137; perl: 33
file content (357 lines) | stat: -rw-r--r-- 10,338 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <stdarg.h>
#include <string.h>
#include "rJava.h"
#include <R_ext/Print.h>
#include <R_ext/Error.h>

#ifdef MEMPROF
FILE *memprof_f = 0;
#endif

/* local to JRI */
static void releaseLocal(JNIEnv *env, jobject o);

REPC SEXP RJavaCheckExceptions(SEXP silent) {
    int result = 0;
    JNIEnv *env = getJNIEnv();
    if (env)
	result = checkExceptionsX(env, asInteger(silent));
    return ScalarInteger(result);
}

HIDE void* errJNI(const char *err, ...) {
	char msg[512];
	va_list ap;
#ifndef RJ_DEBUG
	/* non-debug version goes straight to ckx - it should never return */
	ckx(NULL);
#endif
	va_start(ap, err);
	msg[511]=0;
	vsnprintf(msg, 511, err, ap);
#ifdef RJ_DEBUG
	Rf_warning(msg);
#else
	Rf_error(msg);
	/* this never returns and is just a fallback in case ckx doesn't return */
#endif
	va_end(ap);
	checkExceptionsX(getJNIEnv(), 0);
	return 0;
}

HIDE jclass findClass(JNIEnv *env, const char *cName) {
  if (clClassLoader) {
    char cn[128], *c=cn;
    jobject cns;
    jclass cl;

    strcpy(cn, cName);
    while (*c) { if (*c=='/') *c='.'; c++; };
    cns = newString(env, cn);
    if (!cns) error("unable to create Java string from '%s'", cn);
#ifdef DEBUG_CL
    printf("findClass(\"%s\") [with rJava loader]\n", cn);
#endif
    cl = (jclass) (*env)->CallStaticObjectMethod(env, javaClassClass, mid_forName, cns, (jboolean) 1, oClassLoader);
#if RJAVA_LEGACY
    clx(env);
#endif
    _mp(MEM_PROF_OUT("  %08x LNEW class\n", (int) cl))
    releaseObject(env, cns);
#ifdef DEBUG_CL
    printf(" - got %x\n", (unsigned int) cl);
#endif
#if RJAVA_LEGACY
    if (cl)
#endif
	return cl;
  }
#ifdef DEBUG_CL
  printf("findClass(\"%s\") (no loader)\n", cName);
#endif
  { 
    jclass cl = (*env)->FindClass(env, cName);
    _mp(MEM_PROF_OUT("  %08x LNEW class\n", (int) cl))
#ifdef DEBUG_CL
    printf(" - got %x\n", (unsigned int) cl); 
#endif
    return cl;
  }
}

HIDE jobject createObject(JNIEnv *env, const char *class, const char *sig, jvalue *par, int silent) {
  /* va_list ap; */
  jmethodID mid;
  jclass cls;
  jobject o;

  cls=findClass(env, class);
  if (!cls) return silent?0:errJNI("createObject.FindClass %s failed",class);
  mid=(*env)->GetMethodID(env, cls, "<init>", sig);
  if (!mid) {
    releaseLocal(env, cls);  
    return silent?0:errJNI("createObject.GetMethodID(\"%s\",\"%s\") failed",class,sig);
  }
  
  /*  va_start(ap, sig); */
  o=(*env)->NewObjectA(env, cls, mid, par);
  _mp(MEM_PROF_OUT("  %08x LNEW object\n", (int) o))
  /* va_end(ap); */
  releaseLocal(env, cls);  
  
  return (o||silent)?o:errJNI("NewObject(\"%s\",\"%s\",...) failed",class,sig);
}

HIDE void printObject(JNIEnv *env, jobject o) {
  jmethodID mid;
  jclass cls;
  jobject s;
  const char *c;

  cls=(*env)->GetObjectClass(env,o);
  _mp(MEM_PROF_OUT("  %08x LNEW class from object %08x (JRI-local)\n", (int)cls, (int)o))
  if (!cls) { releaseLocal(env, cls); errJNI("printObject.GetObjectClass failed"); return ; }
  mid=(*env)->GetMethodID(env, cls, "toString", "()Ljava/lang/String;");
  if (!mid) { releaseLocal(env, cls); errJNI("printObject.GetMethodID for toString() failed"); return; }
  s=(*env)->CallObjectMethod(env, o, mid);
  _mp(MEM_PROF_OUT("  %08x LNEW object method toString result (JRI-local)\n", (int)s))
  if (!s) { releaseLocal(env, cls); errJNI("printObject o.toString() call failed"); return; }
  c=(*env)->GetStringUTFChars(env, (jstring)s, 0);
  (*env)->ReleaseStringUTFChars(env, (jstring)s, c);
  releaseLocal(env, cls);  
  releaseLocal(env, s);
}

HIDE jdoubleArray newDoubleArray(JNIEnv *env, double *cont, int len) {
  jdoubleArray da=(*env)->NewDoubleArray(env,len);
  jdouble *dae;

  _mp(MEM_PROF_OUT("  %08x LNEW double[%d]\n", (int) da, len))
  if (!da) return errJNI("newDoubleArray.new(%d) failed",len);
  dae=(*env)->GetDoubleArrayElements(env, da, 0);
  if (!dae) {
    releaseLocal(env, da);
    return errJNI("newDoubleArray.GetDoubleArrayElements failed");
  }
  memcpy(dae,cont,sizeof(jdouble)*len);
  (*env)->ReleaseDoubleArrayElements(env, da, dae, 0);
  return da;
}

HIDE jintArray newIntArray(JNIEnv *env, int *cont, int len) {
  jintArray da=(*env)->NewIntArray(env,len);
  jint *dae;

  _mp(MEM_PROF_OUT("  %08x LNEW int[%d]\n", (int) da, len))
  if (!da) return errJNI("newIntArray.new(%d) failed",len);
  dae=(*env)->GetIntArrayElements(env, da, 0);
  if (!dae) {
    releaseLocal(env,da);
    return errJNI("newIntArray.GetIntArrayElements failed");
  }
  memcpy(dae,cont,sizeof(jint)*len);
  (*env)->ReleaseIntArrayElements(env, da, dae, 0);
  return da;
}

HIDE jbyteArray newByteArray(JNIEnv *env, void *cont, int len) {
  jbyteArray da=(*env)->NewByteArray(env,len);
  jbyte *dae;

  _mp(MEM_PROF_OUT("  %08x LNEW byte[%d]\n", (int) da, len))
  if (!da) return errJNI("newByteArray.new(%d) failed",len);
  dae=(*env)->GetByteArrayElements(env, da, 0);
  if (!dae) {
    releaseLocal(env,da);
    return errJNI("newByteArray.GetByteArrayElements failed");
  }
  memcpy(dae,cont,len);
  (*env)->ReleaseByteArrayElements(env, da, dae, 0);
  return da;
}

HIDE jbyteArray newByteArrayI(JNIEnv *env, int *cont, int len) {
  jbyteArray da=(*env)->NewByteArray(env,len);
  jbyte* dae;
  int i=0;

  _mp(MEM_PROF_OUT("  %08x LNEW byte[%d]\n", (int) da, len))
  if (!da) return errJNI("newByteArray.new(%d) failed",len);
  dae=(*env)->GetByteArrayElements(env, da, 0);
  if (!dae) {
    releaseLocal(env,da);
    return errJNI("newByteArray.GetByteArrayElements failed");
  }
  while (i<len) {
    dae[i]=(jbyte)cont[i];
    i++;
  }
  (*env)->ReleaseByteArrayElements(env, da, dae, 0);
  return da;
}

HIDE jbooleanArray newBooleanArrayI(JNIEnv *env, int *cont, int len) {
  jbooleanArray da=(*env)->NewBooleanArray(env,len);
  jboolean *dae;
  int i=0;

  _mp(MEM_PROF_OUT("  %08x LNEW bool[%d]\n", (int) da, len))
  if (!da) return errJNI("newBooleanArrayI.new(%d) failed",len);
  dae=(*env)->GetBooleanArrayElements(env, da, 0);
  if (!dae) {
    releaseLocal(env,da);
    return errJNI("newBooleanArrayI.GetBooleanArrayElements failed");
  }
  /* we cannot just memcpy since JNI uses unsigned char and R uses int */
  while (i<len) {
    dae[i]=(jboolean)cont[i];
    i++;
  }
  (*env)->ReleaseBooleanArrayElements(env, da, dae, 0);
  return da;
}

HIDE jcharArray newCharArrayI(JNIEnv *env, int *cont, int len) {
  jcharArray da=(*env)->NewCharArray(env,len);
  jchar *dae;
  int i=0;

  _mp(MEM_PROF_OUT("  %08x LNEW char[%d]\n", (int) da, len))
  if (!da) return errJNI("newCharArrayI.new(%d) failed",len);
  dae=(*env)->GetCharArrayElements(env, da, 0);
  if (!dae) {
    releaseLocal(env,da);
    return errJNI("newCharArrayI.GetCharArrayElements failed");
  }
  while (i<len) {
    dae[i]=(jchar)cont[i];
    i++;
  }
  (*env)->ReleaseCharArrayElements(env, da, dae, 0);
  return da;
}

HIDE jshortArray newShortArrayI(JNIEnv *env, int *cont, int len) {
  jshortArray da=(*env)->NewShortArray(env,len);
  jshort *dae;
  int i=0;

  _mp(MEM_PROF_OUT("  %08x LNEW short[%d]\n", (int) da, len))
  if (!da) return errJNI("newShortArrayI.new(%d) failed",len);
  dae=(*env)->GetShortArrayElements(env, da, 0);
  if (!dae) {
    releaseLocal(env,da);
    return errJNI("newShortArrayI.GetShortArrayElements failed");
  }
  while (i<len) {
    dae[i]=(jshort)cont[i];
    i++;
  }
  (*env)->ReleaseShortArrayElements(env, da, dae, 0);
  return da;
}

HIDE jfloatArray newFloatArrayD(JNIEnv *env, double *cont, int len) {
  jfloatArray da=(*env)->NewFloatArray(env,len);
  jfloat *dae;
  int i=0;

  _mp(MEM_PROF_OUT("  %08x LNEW float[%d]\n", (int) da, len))
  if (!da) return errJNI("newFloatArrayD.new(%d) failed",len);
  dae=(*env)->GetFloatArrayElements(env, da, 0);
  if (!dae) {
    releaseLocal(env,da);
    return errJNI("newFloatArrayD.GetFloatArrayElements failed");
  }
  /* we cannot just memcpy since JNI uses float and R uses double */
  while (i<len) {
    dae[i]=(jfloat)cont[i];
    i++;
  }
  (*env)->ReleaseFloatArrayElements(env, da, dae, 0);
  return da;
}

HIDE jlongArray newLongArrayD(JNIEnv *env, double *cont, int len) {
	jlongArray da=(*env)->NewLongArray(env,len);
	jlong *dae;
	int i=0;
	
	_mp(MEM_PROF_OUT("  %08x LNEW long[%d]\n", (int) da, len))
	if (!da) return errJNI("newLongArrayD.new(%d) failed",len);
	dae=(*env)->GetLongArrayElements(env, da, 0);
	if (!dae) {
	  releaseLocal(env, da);
	  return errJNI("newLongArrayD.GetFloatArrayElements failed");
	}
	/* we cannot just memcpy since JNI uses long and R uses double */
	while (i<len) {
		/* we're effectively rounding to prevent representation issues
		   however, we still may introduce some errors this way */
		dae[i]=(jlong)(cont[i]+0.5);
		i++;
	}
	(*env)->ReleaseLongArrayElements(env, da, dae, 0);
	return da;
}

HIDE jstring newString(JNIEnv *env, const char *cont) {
  jstring s=(*env)->NewStringUTF(env, cont);
  _mp(MEM_PROF_OUT("  %08x LNEW string \"%s\"\n", (int) s, cont))
  return s?s:errJNI("newString(\"%s\") failed",cont);
}

HIDE void releaseObject(JNIEnv *env, jobject o) {
  /* Rprintf("releaseObject: %lx\n", (long)o);
     printObject(env, o); */
  _mp(MEM_PROF_OUT("  %08x LREL\n", (int)o))
  (*env)->DeleteLocalRef(env, o);
}

HIDE jclass objectClass(JNIEnv *env, jobject o) {
  jclass cls=(*env)->GetObjectClass(env,o);
  _mp(MEM_PROF_OUT("  %08x LNEW class from object %08x\n", (int) cls, (int) o))
    return cls;
}  

static void releaseLocal(JNIEnv *env, jobject o) {
  _mp(MEM_PROF_OUT("  %08x LREL (JRI-local)\n", (int)o))
  (*env)->DeleteLocalRef(env, o);
}

HIDE jobject makeGlobal(JNIEnv *env, jobject o) {
  jobject g=(*env)->NewGlobalRef(env,o);
  _mp(MEM_PROF_OUT("G %08x GNEW %08x\n", (int) g, (int) o))
  return g?g:errJNI("makeGlobal: failed to make global reference");
}

HIDE void releaseGlobal(JNIEnv *env, jobject o) {
  /* Rprintf("releaseGlobal: %lx\n", (long)o);
     printObject(env, o); */
  _mp(MEM_PROF_OUT("G %08x GREL\n", (int) o))
  (*env)->DeleteGlobalRef(env,o);
}

static jobject nullEx = 0;

HIDE int checkExceptionsX(JNIEnv *env, int silent) {
  jthrowable t=(*env)->ExceptionOccurred(env);
  
  if (t == nullEx) t = 0; else {
    if ((*env)->IsSameObject(env, t, 0)) {
      nullEx = t; t = 0;
    } else {
      _mp(MEM_PROF_OUT("  %08x LNEW exception\n", (int) t))
    }
  }

  if (t) {
	  if (!silent) 
		  ckx(env);
    (*env)->ExceptionClear(env);
    releaseLocal(env, t);
    return 1;
  }
  return 0;
}