-
Notifications
You must be signed in to change notification settings - Fork 38
/
JNIBridge.h
409 lines (363 loc) · 15.6 KB
/
JNIBridge.h
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#pragma once
#include <stdint.h>
#include <jni.h>
#if 0 // ANDROID
#include <android/log.h>
#define JNI_TRACE(...) __android_log_print(ANDROID_LOG_VERBOSE, "JNIBridge", __VA_ARGS__)
#else
#define JNI_TRACE(...) do {} while (false)
#endif
typedef void* jvoid; // make it possible to return void
namespace jni
{
enum Errno
{
kJNI_NO_ERROR = 0,
kJNI_ATTACH_FAILED,
kJNI_INVALID_PARAMETERS,
kJNI_EXCEPTION_THROWN
};
extern jobject kNull;
// --------------------------------------------------------------------------------------
// Initialization and error functions
// --------------------------------------------------------------------------------------
void Initialize(JavaVM& vm);
Errno CheckError();
Errno PeekError();
const char* GetErrorMessage();
jthrowable ExceptionThrown(jclass clazz = 0);
// Internalish
bool CheckForParameterError(bool valid);
bool CheckForExceptionError(JNIEnv* env);
// --------------------------------------------------------------------------------------
// Oracle JNI functions (a selection of)
// http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/functions.html#wp9502
// --------------------------------------------------------------------------------------
JavaVM* GetJavaVM();
JNIEnv* GetEnv();
JNIEnv* AttachCurrentThread();
void DetachCurrentThread();
jclass FindClass(const char* name);
jint Throw(jthrowable object);
jint ThrowNew(jclass exception, const char* message);
void FatalError(const char* str);
jobject NewLocalRef(jobject obj);
void DeleteLocalRef(jobject obj);
jobject NewGlobalRef(jobject obj);
void DeleteGlobalRef(jobject obj);
jobject NewWeakGlobalRef(jobject obj);
void DeleteWeakGlobalRef(jobject obj);
jclass GetObjectClass(jobject object);
jboolean IsInstanceOf(jobject object, jclass clazz);
jboolean IsSameObject(jobject object1, jobject object2);
jmethodID GetMethodID(jclass clazz, const char* name, const char* signature);
jfieldID GetFieldID(jclass clazz, const char* name, const char* signature);
jmethodID GetStaticMethodID(jclass clazz, const char* name, const char* signature);
jfieldID GetStaticFieldID(jclass clazz, const char* name, const char* signature);
jobject ToReflectedMethod(jclass clazz, jmethodID methodID, bool isStatic);
jobject NewObject(jclass clazz, jmethodID methodID, ...);
jstring NewStringUTF(const char* str);
jsize GetStringUTFLength(jstring string);
const char* GetStringUTFChars(jstring str, jboolean* isCopy = 0);
void ReleaseStringUTFChars(jstring str, const char* utfchars);
size_t GetArrayLength(jarray obj);
jobject GetObjectArrayElement(jobjectArray obj, size_t index);
void SetObjectArrayElement(jobjectArray obj, size_t index, jobject val);
void* GetPrimitiveArrayCritical(jarray obj, jboolean *isCopy);
void ReleasePrimitiveArrayCritical(jarray obj, void *carray, jint mode);
jobjectArray NewObjectArray(jsize length, jclass elementClass, jobject initialElement = 0);
jobject NewDirectByteBuffer(void* buffer, jlong size);
void* GetDirectBufferAddress(jobject byteBuffer);
jlong GetDirectBufferCapacity(jobject byteBuffer);
class ThreadScope
{
public:
ThreadScope();
~ThreadScope();
private:
bool m_NeedDetach;
};
class LocalFrame
{
public:
LocalFrame(jint capacity = 64);
~LocalFrame();
private:
LocalFrame(const LocalFrame& frame);
LocalFrame& operator=(const LocalFrame& rhs);
bool m_FramePushed;
};
// Some logic explanation
// Only invoke function if thread can be attached
// Only invoke function if 'parameters' == true
// If function is 'exception safe' only check for exceptions after function has been invoked
// Only adjust return value on exception if function is not exception safe
#define JNI_CALL(parameters, check_exception, function) \
JNI_TRACE("%d:%d:%s", static_cast<bool>(parameters), check_exception, #function); \
JNIEnv* env(AttachCurrentThread()); \
if (env && !CheckForParameterError(parameters) && !(check_exception && CheckForExceptionError(env)))\
{ \
function; \
CheckForExceptionError(env); \
}
#define JNI_CALL_RETURN(type, parameters, check_exception, function) \
JNI_TRACE("%d:%d:%s %s", static_cast<bool>(parameters), check_exception, #type, #function); \
JNIEnv* env(AttachCurrentThread()); \
if (env && !CheckForParameterError(parameters) && !(check_exception && CheckForExceptionError(env)))\
{ \
type JNI_CALL_result = function; \
if (!(CheckForExceptionError(env) && check_exception)) \
return JNI_CALL_result; \
} \
return 0
#define JNI_CALL_DECLARE(type, result, parameters, check_exception, function) \
JNI_TRACE("%d:%d:%s %s", static_cast<bool>(parameters), check_exception, #type, #function); \
JNIEnv* env(AttachCurrentThread()); \
type result = 0; \
if (env && !CheckForParameterError(parameters) && !(check_exception && CheckForExceptionError(env)))\
{ \
result = function; \
if (CheckForExceptionError(env) && check_exception) \
result = 0; \
}
//----------------------------------------------------------------------------
// JNI Operations
// Heavily inspired by https://github.com/kohsuke/jnitl
//----------------------------------------------------------------------------
#if defined(__NDK_FPABI__)
# define JNITL_FUNCTION_ATTRIBUTES __NDK_FPABI__
#else
# define JNITL_FUNCTION_ATTRIBUTES
#endif
template <typename JT, typename RT,
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* CallMethodOP)(jobject, jmethodID, va_list),
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* CallNonvirtualMethodOP)(jobject, jclass, jmethodID, va_list),
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* CallStaticMethodOP)(jclass, jmethodID, va_list)
>
class MethodOps
{
public:
static JT CallMethod(jobject object, jmethodID id, ...)
{
va_list args;
va_start(args, id);
JNI_CALL_DECLARE(JT, result, object && id, true, static_cast<JT>((env->*CallMethodOP)(object, id, args)));
va_end(args);
return result;
}
static JT CallNonVirtualMethod(jobject object, jclass clazz, jmethodID id, ...)
{
va_list args;
va_start(args, id);
JNI_CALL_DECLARE(JT, result, object && clazz && id, true, static_cast<JT>((env->*CallNonvirtualMethodOP)(object, clazz, id, args)));
va_end(args);
return result;
}
static JT CallStaticMethod(jclass clazz, jmethodID id, ...)
{
va_list args;
va_start(args, id);
JNI_CALL_DECLARE(JT, result, clazz && id, true, static_cast<JT>((env->*CallStaticMethodOP)(clazz, id, args)));
va_end(args);
return result;
}
};
template <typename JT, typename RT,
RT (JNIEnv::* GetFieldOP)(jobject, jfieldID),
void (JNIEnv::* SetFieldOP)(jobject, jfieldID, RT),
RT (JNIEnv::* GetStaticFieldOP)(jclass, jfieldID),
void (JNIEnv::* SetStaticFieldOP)(jclass, jfieldID, RT)
>
class FieldOps
{
public:
static JT GetField(jobject object, jfieldID id)
{
JNI_CALL_RETURN(JT, object && id, true, static_cast<JT>((env->*GetFieldOP)(object, id)));
}
static void SetField(jobject object, jfieldID id, const RT& value)
{
JNI_CALL(object && id, true, (env->*SetFieldOP)(object, id, value));
}
static JT GetStaticField(jclass clazz, jfieldID id)
{
JNI_CALL_RETURN(JT, clazz && id, true, static_cast<JT>((env->*GetStaticFieldOP)(clazz, id)));
}
static void SetStaticField(jclass clazz, jfieldID id, const RT& value)
{
JNI_CALL(clazz && id, true, (env->*SetStaticFieldOP)(clazz, id, value));
}
};
template <typename JT, typename RT,
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* GetFieldOP)(jobject, jfieldID),
void (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* SetFieldOP)(jobject, jfieldID, RT),
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* GetStaticFieldOP)(jclass, jfieldID),
void (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* SetStaticFieldOP)(jclass, jfieldID, RT)
>
class FloatFieldOps
{
public:
static JT GetField(jobject object, jfieldID id)
{
JNI_CALL_RETURN(JT, object && id, true, static_cast<JT>((env->*GetFieldOP)(object, id)));
}
static void SetField(jobject object, jfieldID id, const RT& value)
{
JNI_CALL(object && id, true, (env->*SetFieldOP)(object, id, value));
}
static JT GetStaticField(jclass clazz, jfieldID id)
{
JNI_CALL_RETURN(JT, clazz && id, true, static_cast<JT>((env->*GetStaticFieldOP)(clazz, id)));
}
static void SetStaticField(jclass clazz, jfieldID id, const RT& value)
{
JNI_CALL(clazz && id, true, (env->*SetStaticFieldOP)(clazz, id, value));
}
};
template <typename RT, typename RAT,
RAT (JNIEnv::* NewArrayOP)(jsize),
RT* (JNIEnv::* GetArrayElementsOP)(RAT, jboolean*),
void (JNIEnv::* ReleaseArrayElementsOP)(RAT, RT*, jint),
void (JNIEnv::* GetArrayRegionOP)(RAT, jsize, jsize, RT*),
void (JNIEnv::* SetArrayRegionOP)(RAT, jsize, jsize, const RT*)
>
class ArrayOps
{
public:
static RAT NewArray(jsize size)
{
JNI_CALL_RETURN(RAT, true, true, static_cast<RAT>((env->*NewArrayOP)(size)));
}
static RT* GetArrayElements(RAT array, jboolean* isCopy = NULL)
{
JNI_CALL_RETURN(RT*, array, true, static_cast<RT*>((env->*GetArrayElementsOP)(array, isCopy)));
}
static void ReleaseArrayElements(RAT array, RT* elements, jint mode = 0)
{
JNI_CALL(array && elements, true, (env->*ReleaseArrayElementsOP)(array, elements, mode));
}
static void GetArrayRegion(RAT array, jsize start, jsize len, RT* buffer)
{
JNI_CALL(array && buffer, true, (env->*GetArrayRegionOP)(array, start, len, buffer));
}
static void SetArrayRegion(RAT array, jsize start, jsize len, RT* buffer)
{
JNI_CALL(array && buffer, true, (env->*SetArrayRegionOP)(array, start, len, buffer));
}
};
template <typename JT, typename RT,
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* CallMethodOP)(jobject, jmethodID, va_list),
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* CallNonvirtualMethodOP)(jobject, jclass, jmethodID, va_list),
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* CallStaticMethodOP)(jclass, jmethodID, va_list),
RT (JNIEnv::* GetFieldOP)(jobject, jfieldID),
void (JNIEnv::* SetFieldOP)(jobject, jfieldID, RT),
RT (JNIEnv::* GetStaticFieldOP)(jclass, jfieldID),
void (JNIEnv::* SetStaticFieldOP)(jclass, jfieldID, RT)
>
class Object_Op :
public MethodOps<JT, RT, CallMethodOP, CallNonvirtualMethodOP, CallStaticMethodOP>,
public FieldOps<JT, RT, GetFieldOP, SetFieldOP, GetStaticFieldOP, SetStaticFieldOP>
{ };
template <typename RT, typename RAT,
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* CallMethodOP)(jobject, jmethodID, va_list),
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* CallNonvirtualMethodOP)(jobject, jclass, jmethodID, va_list),
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* CallStaticMethodOP)(jclass, jmethodID, va_list),
RT (JNIEnv::* GetFieldOP)(jobject, jfieldID),
void (JNIEnv::* SetFieldOP)(jobject, jfieldID, RT),
RT (JNIEnv::* GetStaticFieldOP)(jclass, jfieldID),
void (JNIEnv::* SetStaticFieldOP)(jclass, jfieldID, RT),
RAT (JNIEnv::* NewArrayOP)(jsize),
RT* (JNIEnv::* GetArrayElementsOP)(RAT, jboolean*),
void (JNIEnv::* ReleaseArrayElementsOP)(RAT, RT*, jint),
void (JNIEnv::* GetArrayRegionOP)(RAT, jsize, jsize, RT*),
void (JNIEnv::* SetArrayRegionOP)(RAT, jsize, jsize, const RT*)
>
class Primitive_Op :
public MethodOps<RT, RT, CallMethodOP, CallNonvirtualMethodOP, CallStaticMethodOP>,
public FieldOps<RT, RT, GetFieldOP, SetFieldOP, GetStaticFieldOP, SetStaticFieldOP>,
public ArrayOps<RT, RAT, NewArrayOP, GetArrayElementsOP, ReleaseArrayElementsOP, GetArrayRegionOP, SetArrayRegionOP>
{ };
template <typename RT, typename RAT,
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* CallMethodOP)(jobject, jmethodID, va_list),
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* CallNonvirtualMethodOP)(jobject, jclass, jmethodID, va_list),
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* CallStaticMethodOP)(jclass, jmethodID, va_list),
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* GetFieldOP)(jobject, jfieldID),
void (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* SetFieldOP)(jobject, jfieldID, RT),
RT (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* GetStaticFieldOP)(jclass, jfieldID),
void (JNITL_FUNCTION_ATTRIBUTES JNIEnv::* SetStaticFieldOP)(jclass, jfieldID, RT),
RAT (JNIEnv::* NewArrayOP)(jsize),
RT* (JNIEnv::* GetArrayElementsOP)(RAT, jboolean*),
void (JNIEnv::* ReleaseArrayElementsOP)(RAT, RT*, jint),
void (JNIEnv::* GetArrayRegionOP)(RAT, jsize, jsize, RT*),
void (JNIEnv::* SetArrayRegionOP)(RAT, jsize, jsize, const RT*)
>
class FloatPrimitive_Op :
public MethodOps<RT, RT, CallMethodOP, CallNonvirtualMethodOP, CallStaticMethodOP>,
public FloatFieldOps<RT, RT, GetFieldOP, SetFieldOP, GetStaticFieldOP, SetStaticFieldOP>,
public ArrayOps<RT, RAT, NewArrayOP, GetArrayElementsOP, ReleaseArrayElementsOP, GetArrayRegionOP, SetArrayRegionOP>
{ };
#define JNITL_DEF_OP_LIST(t) \
&JNIEnv::Call##t##MethodV, \
&JNIEnv::CallNonvirtual##t##MethodV, \
&JNIEnv::CallStatic##t##MethodV, \
&JNIEnv::Get##t##Field, \
&JNIEnv::Set##t##Field, \
&JNIEnv::GetStatic##t##Field, \
&JNIEnv::SetStatic##t##Field
#define JNITL_DEF_PRIMITIVE_OP_LIST(t) \
JNITL_DEF_OP_LIST(t), \
&JNIEnv::New##t##Array, \
&JNIEnv::Get##t##ArrayElements, \
&JNIEnv::Release##t##ArrayElements, \
&JNIEnv::Get##t##ArrayRegion, \
&JNIEnv::Set##t##ArrayRegion
#define JNITL_DEF_PRIMITIVE_OP(jt,t) \
template <> \
class Op<jt> : public Primitive_Op<jt,jt##Array, \
JNITL_DEF_PRIMITIVE_OP_LIST(t) \
> {};
#define JNITL_DEF_FLOAT_PRIMITIVE_OP(jt,t) \
template <> \
class Op<jt> : public FloatPrimitive_Op<jt,jt##Array, \
JNITL_DEF_PRIMITIVE_OP_LIST(t) \
> {};
// it defaults to jobject
template<typename T>
class Op : public Object_Op<T, jobject, JNITL_DEF_OP_LIST(Object)> {};
// specialization for primitives
JNITL_DEF_PRIMITIVE_OP(jboolean,Boolean)
JNITL_DEF_PRIMITIVE_OP(jint,Int)
JNITL_DEF_PRIMITIVE_OP(jshort,Short)
JNITL_DEF_PRIMITIVE_OP(jbyte,Byte)
JNITL_DEF_PRIMITIVE_OP(jlong,Long)
JNITL_DEF_PRIMITIVE_OP(jchar,Char)
JNITL_DEF_FLOAT_PRIMITIVE_OP(jfloat,Float)
JNITL_DEF_FLOAT_PRIMITIVE_OP(jdouble,Double)
#undef JNITL_FUNCTION_ATTRIBUTES
#undef JNITL_DEF_FLOAT_PRIMITIVE_OP
#undef JNITL_DEF_PRIMITIVE_OP
#undef JNITL_DEF_PRIMITIVE_OP_LIST
#undef JNITL_DEF_OP_LIST
// void requires a specialization.
template <>
class Op<jvoid>
{
public:
static jvoid CallMethod(jobject object, jmethodID id, ...)
{
va_list args;
va_start(args, id);
JNI_CALL(object && id, true, env->CallVoidMethodV(object, id, args));
va_end(args);
return 0;
}
static jvoid CallStaticMethod(jclass clazz, jmethodID id, ...)
{
va_list args;
va_start(args, id);
JNI_CALL(clazz && id, true, env->CallStaticVoidMethodV(clazz, id, args));
va_end(args);
return 0;
}
};
}