blob: b9b1ab011ef4d0525d86143d3d78117b9c335de1 [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070015#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070016#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070018#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070021#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070022#include "scoped_jni_thread_state.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070023#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070025
26namespace art {
27
Shih-wei Liao31384c52011-09-06 15:27:45 -070028void* FindNativeMethod(Thread* thread) {
29 DCHECK(Thread::Current() == thread);
30
31 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
32 DCHECK(method != NULL);
33
34 // Lookup symbol address for method, on failure we'll return NULL with an
35 // exception set, otherwise we return the address of the method we found.
36 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
37 if (native_code == NULL) {
38 DCHECK(thread->IsExceptionPending());
39 return NULL;
40 } else {
41 // Register so that future calls don't come here
42 method->RegisterNative(native_code);
43 return native_code;
44 }
45}
46
Elliott Hughesc5f7c912011-08-18 14:00:42 -070047/*
48 * Add a local reference for an object to the current stack frame. When
49 * the native function returns, the reference will be discarded.
50 *
51 * We need to allow the same reference to be added multiple times.
52 *
53 * This will be called on otherwise unreferenced objects. We cannot do
54 * GC allocations here, and it's best if we don't grab a mutex.
55 *
56 * Returns the local reference (currently just the same pointer that was
57 * passed in), or NULL on failure.
58 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070059template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070060T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
61 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
62 Object* obj = const_cast<Object*>(const_obj);
63
Elliott Hughesc5f7c912011-08-18 14:00:42 -070064 if (obj == NULL) {
65 return NULL;
66 }
67
Elliott Hughesbf86d042011-08-31 17:53:14 -070068 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
69 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070070
71 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
72 IndirectRef ref = locals.Add(cookie, obj);
73 if (ref == NULL) {
74 // TODO: just change Add's DCHECK to CHECK and lose this?
75 locals.Dump();
76 LOG(FATAL) << "Failed adding to JNI local reference table "
77 << "(has " << locals.Capacity() << " entries)";
78 // TODO: dvmDumpThread(dvmThreadSelf(), false);
79 }
80
81#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070082 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070083 size_t entry_count = locals.Capacity();
84 if (entry_count > 16) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -070085 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -070086 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -070087 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070088 locals.Dump();
89 // TODO: dvmDumpThread(dvmThreadSelf(), false);
90 // dvmAbort();
91 }
92 }
93#endif
94
Elliott Hughesbf86d042011-08-31 17:53:14 -070095 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070096 // Hand out direct pointers to support broken old apps.
97 return reinterpret_cast<T>(obj);
98 }
99
100 return reinterpret_cast<T>(ref);
101}
102
Elliott Hughesbf86d042011-08-31 17:53:14 -0700103// For external use.
104template<typename T>
105T Decode(JNIEnv* public_env, jobject obj) {
106 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
107 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
108}
109// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700110template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700111template Class* Decode<Class*>(JNIEnv*, jobject);
112template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
113template Object* Decode<Object*>(JNIEnv*, jobject);
114template String* Decode<String*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700115template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700116
117namespace {
118
Elliott Hughescdf53122011-08-19 15:46:09 -0700119jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
120 if (obj == NULL) {
121 return NULL;
122 }
Elliott Hughes75770752011-08-24 17:52:38 -0700123 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700124 IndirectReferenceTable& weak_globals = vm->weak_globals;
125 MutexLock mu(vm->weak_globals_lock);
126 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
127 return reinterpret_cast<jweak>(ref);
128}
129
Elliott Hughesbf86d042011-08-31 17:53:14 -0700130// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700131template<typename T>
132T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700133 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700134}
135
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700136byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700137 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700138 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700139 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700140 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
141 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700142 case 'Z':
143 case 'B':
144 case 'C':
145 case 'S':
146 case 'I':
147 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
148 offset += 4;
149 break;
150 case 'F':
151 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
152 offset += 4;
153 break;
154 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700155 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700156 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
157 offset += sizeof(Object*);
158 break;
159 }
160 case 'D':
161 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
162 offset += 8;
163 break;
164 case 'J':
165 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
166 offset += 8;
167 break;
168 }
169 }
170 return arg_array.release();
171}
172
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700173byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700174 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700175 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700176 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700177 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
178 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700179 case 'Z':
180 case 'B':
181 case 'C':
182 case 'S':
183 case 'I':
184 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
185 offset += 4;
186 break;
187 case 'F':
188 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
189 offset += 4;
190 break;
191 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700192 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700193 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
194 offset += sizeof(Object*);
195 break;
196 }
197 case 'D':
198 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
199 offset += 8;
200 break;
201 case 'J':
202 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
203 offset += 8;
204 break;
205 }
206 }
207 return arg_array.release();
208}
209
Elliott Hughes72025e52011-08-23 17:50:30 -0700210JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
211 Method* method, byte* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700212 JValue result;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700213 method->Invoke(ts.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700214 return result;
215}
216
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700217JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700218 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700219 Method* method = DecodeMethod(mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700220 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700221 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700222}
223
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700224JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700225 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700226 Method* method = DecodeMethod(mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700227 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700228 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
229}
230
Elliott Hughes75770752011-08-24 17:52:38 -0700231Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700232 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700233}
234
Brian Carlstrom30b94452011-08-25 21:35:26 -0700235JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700236 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700237 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700238 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700239 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
240}
241
Brian Carlstrom30b94452011-08-25 21:35:26 -0700242JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700243 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700244 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700245 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700246 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700247}
248
Elliott Hughes6b436852011-08-12 10:16:44 -0700249// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
250// separated with slashes but aren't wrapped with "L;" like regular descriptors
251// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
252// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
253// supported names with dots too (such as "a.b.C").
254std::string NormalizeJniClassDescriptor(const char* name) {
255 std::string result;
256 // Add the missing "L;" if necessary.
257 if (name[0] == '[') {
258 result = name;
259 } else {
260 result += 'L';
261 result += name;
262 result += ';';
263 }
264 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700265 if (result.find('.') != std::string::npos) {
266 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
267 << "\"" << name << "\"";
268 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700269 }
270 return result;
271}
272
Elliott Hughescdf53122011-08-19 15:46:09 -0700273jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
274 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700275 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
276 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700277 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700278
279 Method* method = NULL;
280 if (is_static) {
281 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700282 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700283 method = c->FindVirtualMethod(name, sig);
284 if (method == NULL) {
285 // No virtual method matching the signature. Search declared
286 // private methods and constructors.
287 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700288 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700289 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700290
Elliott Hughescdf53122011-08-19 15:46:09 -0700291 if (method == NULL || method->IsStatic() != is_static) {
292 Thread* self = Thread::Current();
Elliott Hughesa2501992011-08-26 19:39:54 -0700293 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700294 // TODO: try searching for the opposite kind of method from is_static
295 // for better diagnostics?
296 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700297 "no %s method %s", is_static ? "static" : "non-static",
298 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700299 return NULL;
300 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700301
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700302 return reinterpret_cast<jmethodID>(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700303}
304
Elliott Hughescdf53122011-08-19 15:46:09 -0700305jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
306 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700307 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
308 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700309 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700310
311 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 Class* field_type;
313 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
314 if (sig[1] != '\0') {
315 // TODO: need to get the appropriate ClassLoader.
316 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
317 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700318 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700319 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700320 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700321 if (field_type == NULL) {
322 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700323 DCHECK(ts.Self()->IsExceptionPending());
324 ts.Self()->ClearException();
325 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
326 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
327 "no type \"%s\" found and so no field \"%s\" could be found in class "
328 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 return NULL;
330 }
331 if (is_static) {
332 field = c->FindStaticField(name, field_type);
333 } else {
334 field = c->FindInstanceField(name, field_type);
335 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700336 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700337 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700338 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700339 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700340 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700341 return NULL;
342 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343 // Check invariant that all jfieldIDs have resolved types (how else would
344 // the type equality in Find...Field hold?)
345 DCHECK(field->GetType() != NULL);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700346 return reinterpret_cast<jfieldID>(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700347}
348
Elliott Hughes75770752011-08-24 17:52:38 -0700349void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
350 JavaVMExt* vm = ts.Vm();
351 MutexLock mu(vm->pins_lock);
352 vm->pin_table.Add(array);
353}
354
355void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
356 JavaVMExt* vm = ts.Vm();
357 MutexLock mu(vm->pins_lock);
358 vm->pin_table.Remove(array);
359}
360
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700361template<typename JniT, typename ArtT>
362JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
363 CHECK_GE(length, 0); // TODO: ReportJniError
364 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700365 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700366}
367
Elliott Hughes75770752011-08-24 17:52:38 -0700368template <typename ArrayT, typename CArrayT, typename ArtArrayT>
369CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
370 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
371 PinPrimitiveArray(ts, array);
372 if (is_copy != NULL) {
373 *is_copy = JNI_FALSE;
374 }
375 return array->GetData();
376}
377
378template <typename ArrayT>
379void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
380 if (mode != JNI_COMMIT) {
381 Array* array = Decode<Array*>(ts, java_array);
382 UnpinPrimitiveArray(ts, array);
383 }
384}
385
Elliott Hughes814e4032011-08-23 12:07:56 -0700386void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
387 std::string type(PrettyType(array));
388 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
389 "%s offset=%d length=%d %s.length=%d",
390 type.c_str(), start, length, identifier, array->GetLength());
391}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700392void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
393 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
394 "offset=%d length=%d string.length()=%d", start, length, array_length);
395}
Elliott Hughes814e4032011-08-23 12:07:56 -0700396
397template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700398void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700399 ArrayT* array = Decode<ArrayT*>(ts, java_array);
400 if (start < 0 || length < 0 || start + length > array->GetLength()) {
401 ThrowAIOOBE(ts, array, start, length, "src");
402 } else {
403 JavaT* data = array->GetData();
404 memcpy(buf, data + start, length * sizeof(JavaT));
405 }
406}
407
408template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700409void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700410 ArrayT* array = Decode<ArrayT*>(ts, java_array);
411 if (start < 0 || length < 0 || start + length > array->GetLength()) {
412 ThrowAIOOBE(ts, array, start, length, "dst");
413 } else {
414 JavaT* data = array->GetData();
415 memcpy(data + start, buf, length * sizeof(JavaT));
416 }
417}
418
Elliott Hughes75770752011-08-24 17:52:38 -0700419jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700420 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
421 CHECK(buffer_class.get() != NULL);
422 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
423}
424
Elliott Hughes75770752011-08-24 17:52:38 -0700425jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700426 static jclass buffer_class = InitDirectByteBufferClass(env);
427 return buffer_class;
428}
429
Elliott Hughes75770752011-08-24 17:52:38 -0700430jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
431 if (vm == NULL || p_env == NULL) {
432 return JNI_ERR;
433 }
434
435 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
436 JavaVMAttachArgs args;
437 if (thr_args == NULL) {
438 // Allow the v1.1 calling convention.
439 args.version = JNI_VERSION_1_2;
440 args.name = NULL;
441 args.group = NULL; // TODO: get "main" thread group
442 } else {
443 args.version = in_args->version;
444 args.name = in_args->name;
445 if (in_args->group != NULL) {
446 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
447 args.group = NULL; // TODO: decode in_args->group
448 } else {
449 args.group = NULL; // TODO: get "main" thread group
450 }
451 }
452 CHECK_GE(args.version, JNI_VERSION_1_2);
453
454 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700455 runtime->AttachCurrentThread(args.name, as_daemon);
456 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700457 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700458}
459
Elliott Hughes79082e32011-08-25 12:07:32 -0700460class SharedLibrary {
461 public:
462 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
463 : path_(path),
464 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700465 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700466 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700467 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700468 jni_on_load_result_(kPending) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700469 CHECK_PTHREAD_CALL(pthread_cond_init, (&jni_on_load_cond_, NULL), "jni_on_load_cond_");
Elliott Hughes79082e32011-08-25 12:07:32 -0700470 }
471
Elliott Hughes79082e32011-08-25 12:07:32 -0700472 Object* GetClassLoader() {
473 return class_loader_;
474 }
475
476 std::string GetPath() {
477 return path_;
478 }
479
480 /*
481 * Check the result of an earlier call to JNI_OnLoad on this library. If
482 * the call has not yet finished in another thread, wait for it.
483 */
484 bool CheckOnLoadResult(JavaVMExt* vm) {
485 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700486 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700487 // Check this so we don't end up waiting for ourselves. We need
488 // to return "true" so the caller can continue.
489 LOG(INFO) << *self << " recursive attempt to load library "
490 << "\"" << path_ << "\"";
491 return true;
492 }
493
494 MutexLock mu(jni_on_load_lock_);
495 while (jni_on_load_result_ == kPending) {
496 if (vm->verbose_jni) {
497 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
498 << "JNI_OnLoad...]";
499 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700500 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700501 CHECK_PTHREAD_CALL(pthread_cond_wait, (&jni_on_load_cond_, jni_on_load_lock_.GetImpl()), "JNI_OnLoad");
Elliott Hughes79082e32011-08-25 12:07:32 -0700502 }
503
504 bool okay = (jni_on_load_result_ == kOkay);
505 if (vm->verbose_jni) {
506 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
507 << (okay ? "succeeded" : "failed") << "]";
508 }
509 return okay;
510 }
511
512 void SetResult(bool result) {
513 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700514 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700515
516 // Broadcast a wakeup to anybody sleeping on the condition variable.
517 MutexLock mu(jni_on_load_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700518 CHECK_PTHREAD_CALL(pthread_cond_broadcast, (&jni_on_load_cond_), "JNI_OnLoad");
Elliott Hughes79082e32011-08-25 12:07:32 -0700519 }
520
521 void* FindSymbol(const std::string& symbol_name) {
522 return dlsym(handle_, symbol_name.c_str());
523 }
524
525 private:
526 enum JNI_OnLoadState {
527 kPending,
528 kFailed,
529 kOkay,
530 };
531
532 // Path to library "/system/lib/libjni.so".
533 std::string path_;
534
535 // The void* returned by dlopen(3).
536 void* handle_;
537
538 // The ClassLoader this library is associated with.
539 Object* class_loader_;
540
541 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700542 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700543 // Wait for JNI_OnLoad in other thread.
544 pthread_cond_t jni_on_load_cond_;
545 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700546 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700547 // Result of earlier JNI_OnLoad call.
548 JNI_OnLoadState jni_on_load_result_;
549};
550
Elliott Hughescdf53122011-08-19 15:46:09 -0700551} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700552
Elliott Hughes79082e32011-08-25 12:07:32 -0700553// This exists mainly to keep implementation details out of the header file.
554class Libraries {
555 public:
556 Libraries() {
557 }
558
559 ~Libraries() {
560 // Delete our map values. (The keys will be cleaned up by the map itself.)
561 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
562 delete it->second;
563 }
564 }
565
566 SharedLibrary* Get(const std::string& path) {
567 return libraries_[path];
568 }
569
570 void Put(const std::string& path, SharedLibrary* library) {
571 libraries_[path] = library;
572 }
573
574 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700575 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700576 std::string jni_short_name(JniShortName(m));
577 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700578 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700579 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
580 SharedLibrary* library = it->second;
581 if (library->GetClassLoader() != declaring_class_loader) {
582 // We only search libraries loaded by the appropriate ClassLoader.
583 continue;
584 }
585 // Try the short name then the long name...
586 void* fn = library->FindSymbol(jni_short_name);
587 if (fn == NULL) {
588 fn = library->FindSymbol(jni_long_name);
589 }
590 if (fn != NULL) {
591 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700592 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700593 << " in \"" << library->GetPath() << "\"]";
594 }
595 return fn;
596 }
597 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700598 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700599 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700601 return NULL;
602 }
603
604 private:
605 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
606
607 std::map<std::string, SharedLibrary*> libraries_;
608};
609
Elliott Hughescdf53122011-08-19 15:46:09 -0700610class JNI {
611 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700612
Elliott Hughescdf53122011-08-19 15:46:09 -0700613 static jint GetVersion(JNIEnv* env) {
614 ScopedJniThreadState ts(env);
615 return JNI_VERSION_1_6;
616 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700617
Elliott Hughescdf53122011-08-19 15:46:09 -0700618 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
619 ScopedJniThreadState ts(env);
620 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700621 return NULL;
622 }
623
Elliott Hughescdf53122011-08-19 15:46:09 -0700624 static jclass FindClass(JNIEnv* env, const char* name) {
625 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700626 Runtime* runtime = Runtime::Current();
627 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700628 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700629 Class* c = NULL;
630 if (runtime->IsStarted()) {
631 // TODO: need to get the appropriate ClassLoader.
632 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
633 c = class_linker->FindClass(descriptor, cl);
634 } else {
635 c = class_linker->FindSystemClass(descriptor);
636 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700637 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700638 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700639
Elliott Hughescdf53122011-08-19 15:46:09 -0700640 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
641 ScopedJniThreadState ts(env);
642 Method* method = Decode<Method*>(ts, java_method);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700643 return reinterpret_cast<jmethodID>(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700644 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700645
Elliott Hughescdf53122011-08-19 15:46:09 -0700646 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
647 ScopedJniThreadState ts(env);
648 Field* field = Decode<Field*>(ts, java_field);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700649 return reinterpret_cast<jfieldID>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700650 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700651
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
653 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700654 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700655 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700656 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700657
Elliott Hughescdf53122011-08-19 15:46:09 -0700658 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
659 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700660 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700661 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700662 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700663
Elliott Hughes37f7a402011-08-22 18:56:01 -0700664 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
665 ScopedJniThreadState ts(env);
666 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700667 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700668 }
669
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700670 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700672 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700673 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700675
Elliott Hughes37f7a402011-08-22 18:56:01 -0700676 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700677 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 Class* c1 = Decode<Class*>(ts, java_class1);
679 Class* c2 = Decode<Class*>(ts, java_class2);
680 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700682
Elliott Hughes37f7a402011-08-22 18:56:01 -0700683 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700685 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700686 if (jobj == NULL) {
687 // NB. JNI is different from regular Java instanceof in this respect
688 return JNI_TRUE;
689 } else {
690 Object* obj = Decode<Object*>(ts, jobj);
691 Class* klass = Decode<Class*>(ts, clazz);
692 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
693 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700695
Elliott Hughes37f7a402011-08-22 18:56:01 -0700696 static jint Throw(JNIEnv* env, jthrowable java_exception) {
697 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700698 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700699 if (exception == NULL) {
700 return JNI_ERR;
701 }
702 ts.Self()->SetException(exception);
703 return JNI_OK;
704 }
705
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700706 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700707 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700708 // TODO: check for a pending exception to decide what constructor to call.
709 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
710 if (mid == NULL) {
711 return JNI_ERR;
712 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700713 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
714 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700715 return JNI_ERR;
716 }
717
718 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700719 args[0].l = s.get();
720 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
721 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700722 return JNI_ERR;
723 }
724
Elliott Hughes72025e52011-08-23 17:50:30 -0700725 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700726 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700727 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700728
Elliott Hughes37f7a402011-08-22 18:56:01 -0700729 return JNI_OK;
730 }
731
732 static jboolean ExceptionCheck(JNIEnv* env) {
733 ScopedJniThreadState ts(env);
734 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
735 }
736
737 static void ExceptionClear(JNIEnv* env) {
738 ScopedJniThreadState ts(env);
739 ts.Self()->ClearException();
740 }
741
742 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700744
745 Thread* self = ts.Self();
746 Throwable* original_exception = self->GetException();
747 self->ClearException();
748
Elliott Hughesbf86d042011-08-31 17:53:14 -0700749 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700750 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
751 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
752 if (mid == NULL) {
753 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
754 << PrettyType(original_exception);
755 } else {
756 env->CallVoidMethod(exception.get(), mid);
757 if (self->IsExceptionPending()) {
758 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
759 << " thrown while calling printStackTrace";
760 self->ClearException();
761 }
762 }
763
764 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700766
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 static jthrowable ExceptionOccurred(JNIEnv* env) {
768 ScopedJniThreadState ts(env);
769 Object* exception = ts.Self()->GetException();
770 if (exception == NULL) {
771 return NULL;
772 } else {
773 // TODO: if adding a local reference failing causes the VM to abort
774 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700775 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700776 if (localException == NULL) {
777 // We were unable to add a new local reference, and threw a new
778 // exception. We can't return "exception", because it's not a
779 // local reference. So we have to return NULL, indicating that
780 // there was no exception, even though it's pretty much raining
781 // exceptions in here.
782 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
783 }
784 return localException;
785 }
786 }
787
Elliott Hughescdf53122011-08-19 15:46:09 -0700788 static void FatalError(JNIEnv* env, const char* msg) {
789 ScopedJniThreadState ts(env);
790 LOG(FATAL) << "JNI FatalError called: " << msg;
791 }
792
793 static jint PushLocalFrame(JNIEnv* env, jint cap) {
794 ScopedJniThreadState ts(env);
795 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
796 return JNI_OK;
797 }
798
799 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
800 ScopedJniThreadState ts(env);
801 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
802 return res;
803 }
804
Elliott Hughes72025e52011-08-23 17:50:30 -0700805 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
806 ScopedJniThreadState ts(env);
807 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
808 return 0;
809 }
810
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
812 ScopedJniThreadState ts(env);
813 if (obj == NULL) {
814 return NULL;
815 }
816
Elliott Hughes75770752011-08-24 17:52:38 -0700817 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700818 IndirectReferenceTable& globals = vm->globals;
819 MutexLock mu(vm->globals_lock);
820 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
821 return reinterpret_cast<jobject>(ref);
822 }
823
824 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
825 ScopedJniThreadState ts(env);
826 if (obj == NULL) {
827 return;
828 }
829
Elliott Hughes75770752011-08-24 17:52:38 -0700830 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 IndirectReferenceTable& globals = vm->globals;
832 MutexLock mu(vm->globals_lock);
833
834 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
835 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
836 << "failed to find entry";
837 }
838 }
839
840 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
841 ScopedJniThreadState ts(env);
842 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
843 }
844
845 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
846 ScopedJniThreadState ts(env);
847 if (obj == NULL) {
848 return;
849 }
850
Elliott Hughes75770752011-08-24 17:52:38 -0700851 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 IndirectReferenceTable& weak_globals = vm->weak_globals;
853 MutexLock mu(vm->weak_globals_lock);
854
855 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
856 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
857 << "failed to find entry";
858 }
859 }
860
861 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
862 ScopedJniThreadState ts(env);
863 if (obj == NULL) {
864 return NULL;
865 }
866
867 IndirectReferenceTable& locals = ts.Env()->locals;
868
869 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
870 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
871 return reinterpret_cast<jobject>(ref);
872 }
873
874 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
875 ScopedJniThreadState ts(env);
876 if (obj == NULL) {
877 return;
878 }
879
880 IndirectReferenceTable& locals = ts.Env()->locals;
881
882 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
883 if (!locals.Remove(cookie, obj)) {
884 // Attempting to delete a local reference that is not in the
885 // topmost local reference frame is a no-op. DeleteLocalRef returns
886 // void and doesn't throw any exceptions, but we should probably
887 // complain about it so the user will notice that things aren't
888 // going quite the way they expect.
889 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
890 << "failed to find entry";
891 }
892 }
893
894 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
895 ScopedJniThreadState ts(env);
896 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
897 ? JNI_TRUE : JNI_FALSE;
898 }
899
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700900 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700902 Class* c = Decode<Class*>(ts, java_class);
903 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
904 return NULL;
905 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700906 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 }
908
Elliott Hughes72025e52011-08-23 17:50:30 -0700909 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 ScopedJniThreadState ts(env);
911 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 va_start(args, mid);
913 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 va_end(args);
915 return result;
916 }
917
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700920 Class* c = Decode<Class*>(ts, java_class);
921 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
922 return NULL;
923 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700924 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700925 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 return local_result;
928 }
929
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700932 Class* c = Decode<Class*>(ts, java_class);
933 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
934 return NULL;
935 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700936 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700937 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 return local_result;
940 }
941
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
943 ScopedJniThreadState ts(env);
944 return FindMethodID(ts, c, name, sig, false);
945 }
946
947 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
948 ScopedJniThreadState ts(env);
949 return FindMethodID(ts, c, name, sig, true);
950 }
951
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700954 va_list ap;
955 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700956 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700958 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700959 }
960
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700963 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700964 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700969 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700970 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 }
972
Elliott Hughes72025e52011-08-23 17:50:30 -0700973 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 va_list ap;
976 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700977 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 va_end(ap);
979 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 }
981
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700984 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 }
986
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700989 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 }
991
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 va_list ap;
995 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700996 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 va_end(ap);
998 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001003 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 }
1005
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001008 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 }
1010
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 va_list ap;
1014 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001015 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 va_end(ap);
1017 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 }
1019
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001022 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 }
1024
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001027 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 va_list ap;
1033 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001034 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 va_end(ap);
1036 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001041 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 }
1043
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001046 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 }
1048
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 va_list ap;
1052 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001053 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 va_end(ap);
1055 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001060 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001065 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 va_list ap;
1071 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001072 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 va_end(ap);
1074 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001079 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001084 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 va_list ap;
1090 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001091 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 va_end(ap);
1093 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001098 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001103 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 va_list ap;
1109 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001110 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 va_end(ap);
1112 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001117 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001122 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 va_list ap;
1128 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001129 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001135 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001140 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 }
1142
1143 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 ScopedJniThreadState ts(env);
1146 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_start(ap, mid);
1148 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001149 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 va_end(ap);
1151 return local_result;
1152 }
1153
1154 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001158 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
1161 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001165 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
1168 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 ScopedJniThreadState ts(env);
1171 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 va_start(ap, mid);
1173 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 va_end(ap);
1175 return result.z;
1176 }
1177
1178 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
1184 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
1190 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 ScopedJniThreadState ts(env);
1193 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 va_start(ap, mid);
1195 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 va_end(ap);
1197 return result.b;
1198 }
1199
1200 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
1205
1206 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
1212 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 ScopedJniThreadState ts(env);
1215 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 va_start(ap, mid);
1217 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 va_end(ap);
1219 return result.c;
1220 }
1221
1222 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
1228 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
1234 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 ScopedJniThreadState ts(env);
1237 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 va_start(ap, mid);
1239 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 va_end(ap);
1241 return result.s;
1242 }
1243
1244 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
1250 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 }
1255
1256 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 ScopedJniThreadState ts(env);
1259 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 va_start(ap, mid);
1261 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 va_end(ap);
1263 return result.i;
1264 }
1265
1266 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
1272 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
1278 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
1281 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 va_start(ap, mid);
1283 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 va_end(ap);
1285 return result.j;
1286 }
1287
1288 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001289 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
1294 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001295 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
1300 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
1303 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001304 va_start(ap, mid);
1305 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 va_end(ap);
1307 return result.f;
1308 }
1309
1310 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
1316 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001317 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001319 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
1322 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 ScopedJniThreadState ts(env);
1325 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 va_start(ap, mid);
1327 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 va_end(ap);
1329 return result.d;
1330 }
1331
1332 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001333 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 }
1337
1338 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001339 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
1344 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 ScopedJniThreadState ts(env);
1347 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001348 va_start(ap, mid);
1349 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 va_end(ap);
1351 }
1352
1353 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001356 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
1359 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001360 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001365 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 ScopedJniThreadState ts(env);
1367 return FindFieldID(ts, c, name, sig, false);
1368 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001369
1370
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001371 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 ScopedJniThreadState ts(env);
1373 return FindFieldID(ts, c, name, sig, true);
1374 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001375
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001376 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001378 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001379 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001380 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001382
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001383 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001385 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001386 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001391 Object* o = Decode<Object*>(ts, java_object);
1392 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001393 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
1396
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001397 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001400 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001401 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 }
1403
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404#define GET_PRIMITIVE_FIELD(fn, instance) \
1405 ScopedJniThreadState ts(env); \
1406 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001407 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001408 return f->fn(o)
1409
1410#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1411 ScopedJniThreadState ts(env); \
1412 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001413 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001414 f->fn(o, value)
1415
1416 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1417 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001420 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1421 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001424 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1425 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001428 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1429 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1433 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1437 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1441 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001444 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1445 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1449 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1453 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1457 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1461 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1465 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001468 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1469 GET_PRIMITIVE_FIELD(GetLong, NULL);
1470 }
1471
1472 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1473 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1474 }
1475
1476 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1477 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1478 }
1479
1480 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1481 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1482 }
1483
1484 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1485 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1486 }
1487
1488 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1489 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1490 }
1491
1492 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1493 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1494 }
1495
1496 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1497 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1498 }
1499
1500 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1501 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1502 }
1503
1504 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1505 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1506 }
1507
1508 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1509 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1510 }
1511
1512 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1513 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1514 }
1515
1516 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1517 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1518 }
1519
1520 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1521 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1522 }
1523
1524 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1525 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1526 }
1527
1528 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1529 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1530 }
1531
1532 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1533 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1534 }
1535
1536 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1537 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1538 }
1539
1540 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1541 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
1544 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001545 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 ScopedJniThreadState ts(env);
1547 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001548 va_start(ap, mid);
1549 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001550 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 va_end(ap);
1552 return local_result;
1553 }
1554
1555 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001556 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001558 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001559 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 }
1561
1562 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001563 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001564 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001565 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001566 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
1569 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001570 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 ScopedJniThreadState ts(env);
1572 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001573 va_start(ap, mid);
1574 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 va_end(ap);
1576 return result.z;
1577 }
1578
1579 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001580 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001582 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
1585 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001586 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001588 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes72025e52011-08-23 17:50:30 -07001591 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 ScopedJniThreadState ts(env);
1593 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001594 va_start(ap, mid);
1595 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 va_end(ap);
1597 return result.b;
1598 }
1599
1600 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001601 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001603 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 }
1605
1606 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001607 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001609 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes72025e52011-08-23 17:50:30 -07001612 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 ScopedJniThreadState ts(env);
1614 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 va_start(ap, mid);
1616 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 va_end(ap);
1618 return result.c;
1619 }
1620
1621 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001622 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001624 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 }
1626
1627 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001628 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001630 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 }
1632
Elliott Hughes72025e52011-08-23 17:50:30 -07001633 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 ScopedJniThreadState ts(env);
1635 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001636 va_start(ap, mid);
1637 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 va_end(ap);
1639 return result.s;
1640 }
1641
1642 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001643 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001645 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 }
1647
1648 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001649 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 }
1653
Elliott Hughes72025e52011-08-23 17:50:30 -07001654 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 ScopedJniThreadState ts(env);
1656 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001657 va_start(ap, mid);
1658 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 va_end(ap);
1660 return result.i;
1661 }
1662
1663 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001664 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001666 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
1669 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
Elliott Hughes72025e52011-08-23 17:50:30 -07001675 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 ScopedJniThreadState ts(env);
1677 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 va_start(ap, mid);
1679 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 va_end(ap);
1681 return result.j;
1682 }
1683
1684 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001685 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 }
1689
1690 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001693 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 }
1695
Elliott Hughes72025e52011-08-23 17:50:30 -07001696 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 ScopedJniThreadState ts(env);
1698 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 va_start(ap, mid);
1700 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 va_end(ap);
1702 return result.f;
1703 }
1704
1705 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001706 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
1711 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001712 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001714 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes72025e52011-08-23 17:50:30 -07001717 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 ScopedJniThreadState ts(env);
1719 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 va_start(ap, mid);
1721 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 va_end(ap);
1723 return result.d;
1724 }
1725
1726 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001727 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001729 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 }
1731
1732 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001733 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001735 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes72025e52011-08-23 17:50:30 -07001738 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 ScopedJniThreadState ts(env);
1740 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 va_start(ap, mid);
1742 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 va_end(ap);
1744 }
1745
1746 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001749 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
1752 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001753 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001755 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 }
1757
Elliott Hughes814e4032011-08-23 12:07:56 -07001758 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001760 if (chars == NULL && char_count == 0) {
1761 return NULL;
1762 }
1763 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001764 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
1767 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1768 ScopedJniThreadState ts(env);
1769 if (utf == NULL) {
1770 return NULL;
1771 }
1772 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001773 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes814e4032011-08-23 12:07:56 -07001776 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1777 ScopedJniThreadState ts(env);
1778 return Decode<String*>(ts, java_string)->GetLength();
1779 }
1780
1781 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1782 ScopedJniThreadState ts(env);
1783 return Decode<String*>(ts, java_string)->GetUtfLength();
1784 }
1785
Elliott Hughesb465ab02011-08-24 11:21:21 -07001786 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001788 String* s = Decode<String*>(ts, java_string);
1789 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1790 ThrowSIOOBE(ts, start, length, s->GetLength());
1791 } else {
1792 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1793 memcpy(buf, chars + start, length * sizeof(jchar));
1794 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001795 }
1796
Elliott Hughesb465ab02011-08-24 11:21:21 -07001797 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001798 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001799 String* s = Decode<String*>(ts, java_string);
1800 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1801 ThrowSIOOBE(ts, start, length, s->GetLength());
1802 } else {
1803 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1804 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1805 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001806 }
1807
Elliott Hughes75770752011-08-24 17:52:38 -07001808 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001809 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001810 String* s = Decode<String*>(ts, java_string);
1811 const CharArray* chars = s->GetCharArray();
1812 PinPrimitiveArray(ts, chars);
1813 if (is_copy != NULL) {
1814 *is_copy = JNI_FALSE;
1815 }
1816 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001817 }
1818
Elliott Hughes75770752011-08-24 17:52:38 -07001819 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001820 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001821 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 }
1823
Elliott Hughes75770752011-08-24 17:52:38 -07001824 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001826 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 }
1828
Elliott Hughes75770752011-08-24 17:52:38 -07001829 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001831 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
Elliott Hughes75770752011-08-24 17:52:38 -07001834 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001835 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001836 if (java_string == NULL) {
1837 return NULL;
1838 }
1839 if (is_copy != NULL) {
1840 *is_copy = JNI_TRUE;
1841 }
1842 String* s = Decode<String*>(ts, java_string);
1843 size_t byte_count = s->GetUtfLength();
1844 char* bytes = new char[byte_count + 1];
1845 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001846 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001847 return NULL;
1848 }
1849 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1850 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1851 bytes[byte_count] = '\0';
1852 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001853 }
1854
Elliott Hughes75770752011-08-24 17:52:38 -07001855 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001856 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001857 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001858 }
1859
Elliott Hughesbd935992011-08-22 11:59:34 -07001860 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001862 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001863 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001864 Array* array = obj->AsArray();
1865 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 }
1867
Elliott Hughes814e4032011-08-23 12:07:56 -07001868 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001870 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001871 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 }
1873
1874 static void SetObjectArrayElement(JNIEnv* env,
1875 jobjectArray java_array, jsize index, jobject java_value) {
1876 ScopedJniThreadState ts(env);
1877 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1878 Object* value = Decode<Object*>(ts, java_value);
1879 array->Set(index, value);
1880 }
1881
1882 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1883 ScopedJniThreadState ts(env);
1884 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1885 }
1886
1887 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1888 ScopedJniThreadState ts(env);
1889 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1890 }
1891
1892 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1893 ScopedJniThreadState ts(env);
1894 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1895 }
1896
1897 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1898 ScopedJniThreadState ts(env);
1899 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1900 }
1901
1902 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1903 ScopedJniThreadState ts(env);
1904 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1905 }
1906
1907 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1908 ScopedJniThreadState ts(env);
1909 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1910 }
1911
1912 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1913 ScopedJniThreadState ts(env);
1914 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1915 }
1916
1917 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1918 ScopedJniThreadState ts(env);
1919 CHECK_GE(length, 0); // TODO: ReportJniError
1920
1921 // Compute the array class corresponding to the given element class.
1922 Class* element_class = Decode<Class*>(ts, element_jclass);
1923 std::string descriptor;
1924 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001925 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001926
1927 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001928 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1929 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001930 return NULL;
1931 }
1932
Elliott Hughes75770752011-08-24 17:52:38 -07001933 // Allocate and initialize if necessary.
1934 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001936 if (initial_element != NULL) {
1937 Object* initial_object = Decode<Object*>(ts, initial_element);
1938 for (jsize i = 0; i < length; ++i) {
1939 result->Set(i, initial_object);
1940 }
1941 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001942 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
1945 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1946 ScopedJniThreadState ts(env);
1947 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1948 }
1949
Elliott Hughes75770752011-08-24 17:52:38 -07001950 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001951 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001952 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001953 }
1954
Elliott Hughes75770752011-08-24 17:52:38 -07001955 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001956 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001957 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001958 }
1959
Elliott Hughes75770752011-08-24 17:52:38 -07001960 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001962 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 }
1964
Elliott Hughes75770752011-08-24 17:52:38 -07001965 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001967 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes75770752011-08-24 17:52:38 -07001970 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001972 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 }
1974
Elliott Hughes75770752011-08-24 17:52:38 -07001975 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001977 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
Elliott Hughes75770752011-08-24 17:52:38 -07001980 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001982 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 }
1984
Elliott Hughes75770752011-08-24 17:52:38 -07001985 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001987 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Elliott Hughes75770752011-08-24 17:52:38 -07001990 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001992 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 }
1994
Elliott Hughes75770752011-08-24 17:52:38 -07001995 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001997 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Elliott Hughes75770752011-08-24 17:52:38 -07002000 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002002 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
Elliott Hughes75770752011-08-24 17:52:38 -07002005 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002007 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Elliott Hughes75770752011-08-24 17:52:38 -07002010 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002012 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Elliott Hughes75770752011-08-24 17:52:38 -07002015 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002017 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Elliott Hughes75770752011-08-24 17:52:38 -07002020 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002022 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes75770752011-08-24 17:52:38 -07002025 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002027 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes75770752011-08-24 17:52:38 -07002030 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002032 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
2034
Elliott Hughes75770752011-08-24 17:52:38 -07002035 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002037 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes814e4032011-08-23 12:07:56 -07002040 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002042 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes814e4032011-08-23 12:07:56 -07002045 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002047 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002052 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002057 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002062 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002067 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Elliott Hughes814e4032011-08-23 12:07:56 -07002070 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002072 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002077 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002082 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002087 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002092 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002097 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002102 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002107 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002112 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002117 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
Elliott Hughes5174fe62011-08-23 15:12:35 -07002120 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002122 Class* c = Decode<Class*>(ts, java_class);
2123
Elliott Hughes5174fe62011-08-23 15:12:35 -07002124 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 const char* name = methods[i].name;
2126 const char* sig = methods[i].signature;
2127
2128 if (*sig == '!') {
2129 // TODO: fast jni. it's too noisy to log all these.
2130 ++sig;
2131 }
2132
Elliott Hughes5174fe62011-08-23 15:12:35 -07002133 Method* m = c->FindDirectMethod(name, sig);
2134 if (m == NULL) {
2135 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002137 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002139 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2141 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002142 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002144 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002146 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2148 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002149 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 return JNI_ERR;
2151 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002152
Elliott Hughes75770752011-08-24 17:52:38 -07002153 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002154 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155 }
2156
2157 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 }
2159 return JNI_OK;
2160 }
2161
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002164 Class* c = Decode<Class*>(ts, java_class);
2165
Elliott Hughes75770752011-08-24 17:52:38 -07002166 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002167 LOG(INFO) << "[Unregistering JNI native methods for "
2168 << PrettyDescriptor(c->GetDescriptor()) << "]";
2169 }
2170
2171 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2172 Method* m = c->GetDirectMethod(i);
2173 if (m->IsNative()) {
2174 m->UnregisterNative();
2175 }
2176 }
2177 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2178 Method* m = c->GetVirtualMethod(i);
2179 if (m->IsNative()) {
2180 m->UnregisterNative();
2181 }
2182 }
2183
2184 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Elliott Hughes72025e52011-08-23 17:50:30 -07002187 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002189 Decode<Object*>(ts, java_object)->MonitorEnter();
2190 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Elliott Hughes72025e52011-08-23 17:50:30 -07002193 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 ScopedJniThreadState ts(env);
Elliott Hughes02b48d12011-09-07 17:15:51 -07002195 Decode<Object*>(ts, java_object)->MonitorExit();
Elliott Hughes72025e52011-08-23 17:50:30 -07002196 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 }
2198
2199 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2200 ScopedJniThreadState ts(env);
2201 Runtime* runtime = Runtime::Current();
2202 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002203 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 } else {
2205 *vm = NULL;
2206 }
2207 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2208 }
2209
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2211 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002212
2213 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002214 CHECK(address != NULL); // TODO: ReportJniError
2215 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002216
2217 jclass buffer_class = GetDirectByteBufferClass(env);
2218 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2219 if (mid == NULL) {
2220 return NULL;
2221 }
2222
2223 // At the moment, the Java side is limited to 32 bits.
2224 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2225 CHECK_LE(capacity, 0xffffffff);
2226 jint address_arg = reinterpret_cast<jint>(address);
2227 jint capacity_arg = static_cast<jint>(capacity);
2228
2229 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2230 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 }
2232
Elliott Hughesb465ab02011-08-24 11:21:21 -07002233 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002235 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2236 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 }
2238
Elliott Hughesb465ab02011-08-24 11:21:21 -07002239 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002240 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002241 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2242 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 }
2244
Elliott Hughesb465ab02011-08-24 11:21:21 -07002245 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247
Elliott Hughes75770752011-08-24 17:52:38 -07002248 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249
2250 // Do we definitely know what kind of reference this is?
2251 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2252 IndirectRefKind kind = GetIndirectRefKind(ref);
2253 switch (kind) {
2254 case kLocal:
2255 return JNILocalRefType;
2256 case kGlobal:
2257 return JNIGlobalRefType;
2258 case kWeakGlobal:
2259 return JNIWeakGlobalRefType;
2260 case kSirtOrInvalid:
2261 // Is it in a stack IRT?
2262 if (ts.Self()->SirtContains(java_object)) {
2263 return JNILocalRefType;
2264 }
2265
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002266 if (!ts.Env()->work_around_app_jni_bugs) {
2267 return JNIInvalidRefType;
2268 }
2269
Elliott Hughesb465ab02011-08-24 11:21:21 -07002270 // If we're handing out direct pointers, check whether it's a direct pointer
2271 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002272 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002273 if (ts.Env()->locals.Contains(java_object)) {
2274 return JNILocalRefType;
2275 }
2276 }
2277
2278 return JNIInvalidRefType;
2279 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 }
2281};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002282
Elliott Hughesa2501992011-08-26 19:39:54 -07002283const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002284 NULL, // reserved0.
2285 NULL, // reserved1.
2286 NULL, // reserved2.
2287 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002288 JNI::GetVersion,
2289 JNI::DefineClass,
2290 JNI::FindClass,
2291 JNI::FromReflectedMethod,
2292 JNI::FromReflectedField,
2293 JNI::ToReflectedMethod,
2294 JNI::GetSuperclass,
2295 JNI::IsAssignableFrom,
2296 JNI::ToReflectedField,
2297 JNI::Throw,
2298 JNI::ThrowNew,
2299 JNI::ExceptionOccurred,
2300 JNI::ExceptionDescribe,
2301 JNI::ExceptionClear,
2302 JNI::FatalError,
2303 JNI::PushLocalFrame,
2304 JNI::PopLocalFrame,
2305 JNI::NewGlobalRef,
2306 JNI::DeleteGlobalRef,
2307 JNI::DeleteLocalRef,
2308 JNI::IsSameObject,
2309 JNI::NewLocalRef,
2310 JNI::EnsureLocalCapacity,
2311 JNI::AllocObject,
2312 JNI::NewObject,
2313 JNI::NewObjectV,
2314 JNI::NewObjectA,
2315 JNI::GetObjectClass,
2316 JNI::IsInstanceOf,
2317 JNI::GetMethodID,
2318 JNI::CallObjectMethod,
2319 JNI::CallObjectMethodV,
2320 JNI::CallObjectMethodA,
2321 JNI::CallBooleanMethod,
2322 JNI::CallBooleanMethodV,
2323 JNI::CallBooleanMethodA,
2324 JNI::CallByteMethod,
2325 JNI::CallByteMethodV,
2326 JNI::CallByteMethodA,
2327 JNI::CallCharMethod,
2328 JNI::CallCharMethodV,
2329 JNI::CallCharMethodA,
2330 JNI::CallShortMethod,
2331 JNI::CallShortMethodV,
2332 JNI::CallShortMethodA,
2333 JNI::CallIntMethod,
2334 JNI::CallIntMethodV,
2335 JNI::CallIntMethodA,
2336 JNI::CallLongMethod,
2337 JNI::CallLongMethodV,
2338 JNI::CallLongMethodA,
2339 JNI::CallFloatMethod,
2340 JNI::CallFloatMethodV,
2341 JNI::CallFloatMethodA,
2342 JNI::CallDoubleMethod,
2343 JNI::CallDoubleMethodV,
2344 JNI::CallDoubleMethodA,
2345 JNI::CallVoidMethod,
2346 JNI::CallVoidMethodV,
2347 JNI::CallVoidMethodA,
2348 JNI::CallNonvirtualObjectMethod,
2349 JNI::CallNonvirtualObjectMethodV,
2350 JNI::CallNonvirtualObjectMethodA,
2351 JNI::CallNonvirtualBooleanMethod,
2352 JNI::CallNonvirtualBooleanMethodV,
2353 JNI::CallNonvirtualBooleanMethodA,
2354 JNI::CallNonvirtualByteMethod,
2355 JNI::CallNonvirtualByteMethodV,
2356 JNI::CallNonvirtualByteMethodA,
2357 JNI::CallNonvirtualCharMethod,
2358 JNI::CallNonvirtualCharMethodV,
2359 JNI::CallNonvirtualCharMethodA,
2360 JNI::CallNonvirtualShortMethod,
2361 JNI::CallNonvirtualShortMethodV,
2362 JNI::CallNonvirtualShortMethodA,
2363 JNI::CallNonvirtualIntMethod,
2364 JNI::CallNonvirtualIntMethodV,
2365 JNI::CallNonvirtualIntMethodA,
2366 JNI::CallNonvirtualLongMethod,
2367 JNI::CallNonvirtualLongMethodV,
2368 JNI::CallNonvirtualLongMethodA,
2369 JNI::CallNonvirtualFloatMethod,
2370 JNI::CallNonvirtualFloatMethodV,
2371 JNI::CallNonvirtualFloatMethodA,
2372 JNI::CallNonvirtualDoubleMethod,
2373 JNI::CallNonvirtualDoubleMethodV,
2374 JNI::CallNonvirtualDoubleMethodA,
2375 JNI::CallNonvirtualVoidMethod,
2376 JNI::CallNonvirtualVoidMethodV,
2377 JNI::CallNonvirtualVoidMethodA,
2378 JNI::GetFieldID,
2379 JNI::GetObjectField,
2380 JNI::GetBooleanField,
2381 JNI::GetByteField,
2382 JNI::GetCharField,
2383 JNI::GetShortField,
2384 JNI::GetIntField,
2385 JNI::GetLongField,
2386 JNI::GetFloatField,
2387 JNI::GetDoubleField,
2388 JNI::SetObjectField,
2389 JNI::SetBooleanField,
2390 JNI::SetByteField,
2391 JNI::SetCharField,
2392 JNI::SetShortField,
2393 JNI::SetIntField,
2394 JNI::SetLongField,
2395 JNI::SetFloatField,
2396 JNI::SetDoubleField,
2397 JNI::GetStaticMethodID,
2398 JNI::CallStaticObjectMethod,
2399 JNI::CallStaticObjectMethodV,
2400 JNI::CallStaticObjectMethodA,
2401 JNI::CallStaticBooleanMethod,
2402 JNI::CallStaticBooleanMethodV,
2403 JNI::CallStaticBooleanMethodA,
2404 JNI::CallStaticByteMethod,
2405 JNI::CallStaticByteMethodV,
2406 JNI::CallStaticByteMethodA,
2407 JNI::CallStaticCharMethod,
2408 JNI::CallStaticCharMethodV,
2409 JNI::CallStaticCharMethodA,
2410 JNI::CallStaticShortMethod,
2411 JNI::CallStaticShortMethodV,
2412 JNI::CallStaticShortMethodA,
2413 JNI::CallStaticIntMethod,
2414 JNI::CallStaticIntMethodV,
2415 JNI::CallStaticIntMethodA,
2416 JNI::CallStaticLongMethod,
2417 JNI::CallStaticLongMethodV,
2418 JNI::CallStaticLongMethodA,
2419 JNI::CallStaticFloatMethod,
2420 JNI::CallStaticFloatMethodV,
2421 JNI::CallStaticFloatMethodA,
2422 JNI::CallStaticDoubleMethod,
2423 JNI::CallStaticDoubleMethodV,
2424 JNI::CallStaticDoubleMethodA,
2425 JNI::CallStaticVoidMethod,
2426 JNI::CallStaticVoidMethodV,
2427 JNI::CallStaticVoidMethodA,
2428 JNI::GetStaticFieldID,
2429 JNI::GetStaticObjectField,
2430 JNI::GetStaticBooleanField,
2431 JNI::GetStaticByteField,
2432 JNI::GetStaticCharField,
2433 JNI::GetStaticShortField,
2434 JNI::GetStaticIntField,
2435 JNI::GetStaticLongField,
2436 JNI::GetStaticFloatField,
2437 JNI::GetStaticDoubleField,
2438 JNI::SetStaticObjectField,
2439 JNI::SetStaticBooleanField,
2440 JNI::SetStaticByteField,
2441 JNI::SetStaticCharField,
2442 JNI::SetStaticShortField,
2443 JNI::SetStaticIntField,
2444 JNI::SetStaticLongField,
2445 JNI::SetStaticFloatField,
2446 JNI::SetStaticDoubleField,
2447 JNI::NewString,
2448 JNI::GetStringLength,
2449 JNI::GetStringChars,
2450 JNI::ReleaseStringChars,
2451 JNI::NewStringUTF,
2452 JNI::GetStringUTFLength,
2453 JNI::GetStringUTFChars,
2454 JNI::ReleaseStringUTFChars,
2455 JNI::GetArrayLength,
2456 JNI::NewObjectArray,
2457 JNI::GetObjectArrayElement,
2458 JNI::SetObjectArrayElement,
2459 JNI::NewBooleanArray,
2460 JNI::NewByteArray,
2461 JNI::NewCharArray,
2462 JNI::NewShortArray,
2463 JNI::NewIntArray,
2464 JNI::NewLongArray,
2465 JNI::NewFloatArray,
2466 JNI::NewDoubleArray,
2467 JNI::GetBooleanArrayElements,
2468 JNI::GetByteArrayElements,
2469 JNI::GetCharArrayElements,
2470 JNI::GetShortArrayElements,
2471 JNI::GetIntArrayElements,
2472 JNI::GetLongArrayElements,
2473 JNI::GetFloatArrayElements,
2474 JNI::GetDoubleArrayElements,
2475 JNI::ReleaseBooleanArrayElements,
2476 JNI::ReleaseByteArrayElements,
2477 JNI::ReleaseCharArrayElements,
2478 JNI::ReleaseShortArrayElements,
2479 JNI::ReleaseIntArrayElements,
2480 JNI::ReleaseLongArrayElements,
2481 JNI::ReleaseFloatArrayElements,
2482 JNI::ReleaseDoubleArrayElements,
2483 JNI::GetBooleanArrayRegion,
2484 JNI::GetByteArrayRegion,
2485 JNI::GetCharArrayRegion,
2486 JNI::GetShortArrayRegion,
2487 JNI::GetIntArrayRegion,
2488 JNI::GetLongArrayRegion,
2489 JNI::GetFloatArrayRegion,
2490 JNI::GetDoubleArrayRegion,
2491 JNI::SetBooleanArrayRegion,
2492 JNI::SetByteArrayRegion,
2493 JNI::SetCharArrayRegion,
2494 JNI::SetShortArrayRegion,
2495 JNI::SetIntArrayRegion,
2496 JNI::SetLongArrayRegion,
2497 JNI::SetFloatArrayRegion,
2498 JNI::SetDoubleArrayRegion,
2499 JNI::RegisterNatives,
2500 JNI::UnregisterNatives,
2501 JNI::MonitorEnter,
2502 JNI::MonitorExit,
2503 JNI::GetJavaVM,
2504 JNI::GetStringRegion,
2505 JNI::GetStringUTFRegion,
2506 JNI::GetPrimitiveArrayCritical,
2507 JNI::ReleasePrimitiveArrayCritical,
2508 JNI::GetStringCritical,
2509 JNI::ReleaseStringCritical,
2510 JNI::NewWeakGlobalRef,
2511 JNI::DeleteWeakGlobalRef,
2512 JNI::ExceptionCheck,
2513 JNI::NewDirectByteBuffer,
2514 JNI::GetDirectBufferAddress,
2515 JNI::GetDirectBufferCapacity,
2516 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002517};
2518
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002519static const size_t kMonitorsInitial = 32; // Arbitrary.
2520static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2521
2522static const size_t kLocalsInitial = 64; // Arbitrary.
2523static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002524
Elliott Hughes75770752011-08-24 17:52:38 -07002525JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002526 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002527 vm(vm),
2528 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002529 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002530 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002531 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2532 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002533 functions = unchecked_functions = &gNativeInterface;
2534 if (check_jni) {
2535 functions = GetCheckJniNativeInterface();
2536 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002537}
2538
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002539JNIEnvExt::~JNIEnvExt() {
2540}
2541
Carl Shapiroea4dca82011-08-01 13:45:38 -07002542// JNI Invocation interface.
2543
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002544extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2545 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2546 if (args->version < JNI_VERSION_1_2) {
2547 return JNI_EVERSION;
2548 }
2549 Runtime::Options options;
2550 for (int i = 0; i < args->nOptions; ++i) {
2551 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002552 options.push_back(std::make_pair(StringPiece(option->optionString),
2553 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002554 }
2555 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002556 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002557 if (runtime == NULL) {
2558 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002559 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002560 runtime->Start();
2561 *p_env = Thread::Current()->GetJniEnv();
2562 *p_vm = runtime->GetJavaVM();
2563 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002564}
2565
Elliott Hughesf2682d52011-08-15 16:37:04 -07002566extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002567 Runtime* runtime = Runtime::Current();
2568 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002569 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002570 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002571 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002572 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002573 }
2574 return JNI_OK;
2575}
2576
2577// Historically unsupported.
2578extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2579 return JNI_ERR;
2580}
2581
Elliott Hughescdf53122011-08-19 15:46:09 -07002582class JII {
2583 public:
2584 static jint DestroyJavaVM(JavaVM* vm) {
2585 if (vm == NULL) {
2586 return JNI_ERR;
2587 } else {
2588 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2589 delete raw_vm->runtime;
2590 return JNI_OK;
2591 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002592 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002593
Elliott Hughescdf53122011-08-19 15:46:09 -07002594 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002595 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002596 }
2597
2598 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002599 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002600 }
2601
2602 static jint DetachCurrentThread(JavaVM* vm) {
2603 if (vm == NULL) {
2604 return JNI_ERR;
2605 } else {
2606 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2607 Runtime* runtime = raw_vm->runtime;
2608 runtime->DetachCurrentThread();
2609 return JNI_OK;
2610 }
2611 }
2612
2613 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2614 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2615 return JNI_EVERSION;
2616 }
2617 if (vm == NULL || env == NULL) {
2618 return JNI_ERR;
2619 }
2620 Thread* thread = Thread::Current();
2621 if (thread == NULL) {
2622 *env = NULL;
2623 return JNI_EDETACHED;
2624 }
2625 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002626 return JNI_OK;
2627 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002628};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002629
Elliott Hughesa2501992011-08-26 19:39:54 -07002630const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002631 NULL, // reserved0
2632 NULL, // reserved1
2633 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002634 JII::DestroyJavaVM,
2635 JII::AttachCurrentThread,
2636 JII::DetachCurrentThread,
2637 JII::GetEnv,
2638 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002639};
2640
Elliott Hughesbbd76712011-08-17 10:25:24 -07002641static const size_t kPinTableInitialSize = 16;
2642static const size_t kPinTableMaxSize = 1024;
2643
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002644static const size_t kGlobalsInitial = 512; // Arbitrary.
2645static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2646
2647static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2648static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2649
Elliott Hughesa0957642011-09-02 14:27:33 -07002650JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002651 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002652 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002653 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002654 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002655 verbose_jni(options->IsVerbose("jni")),
2656 log_third_party_jni(options->IsVerbose("third-party-jni")),
2657 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002658 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002659 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002660 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002661 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002662 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002663 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002664 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002665 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002666 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002667 functions = unchecked_functions = &gInvokeInterface;
2668 if (check_jni) {
2669 functions = GetCheckJniInvokeInterface();
2670 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002671}
2672
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002673JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002674 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002675}
2676
Elliott Hughes75770752011-08-24 17:52:38 -07002677bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2678 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002679
2680 // See if we've already loaded this library. If we have, and the class loader
2681 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002682 // TODO: for better results we should canonicalize the pathname (or even compare
2683 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002684 SharedLibrary* library;
2685 {
2686 // TODO: move the locking (and more of this logic) into Libraries.
2687 MutexLock mu(libraries_lock);
2688 library = libraries->Get(path);
2689 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002690 if (library != NULL) {
2691 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002692 // The library will be associated with class_loader. The JNI
2693 // spec says we can't load the same library into more than one
2694 // class loader.
2695 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2696 "ClassLoader %p; can't open in ClassLoader %p",
2697 path.c_str(), library->GetClassLoader(), class_loader);
2698 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002699 return false;
2700 }
2701 if (verbose_jni) {
2702 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2703 << "ClassLoader " << class_loader << "]";
2704 }
2705 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002706 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2707 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002708 return false;
2709 }
2710 return true;
2711 }
2712
2713 // Open the shared library. Because we're using a full path, the system
2714 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2715 // resolve this library's dependencies though.)
2716
2717 // Failures here are expected when java.library.path has several entries
2718 // and we have to hunt for the lib.
2719
2720 // The current version of the dynamic linker prints detailed information
2721 // about dlopen() failures. Some things to check if the message is
2722 // cryptic:
2723 // - make sure the library exists on the device
2724 // - verify that the right path is being opened (the debug log message
2725 // above can help with that)
2726 // - check to see if the library is valid (e.g. not zero bytes long)
2727 // - check config/prelink-linux-arm.map to ensure that the library
2728 // is listed and is not being overrun by the previous entry (if
2729 // loading suddenly stops working on a prelinked library, this is
2730 // a good one to check)
2731 // - write a trivial app that calls sleep() then dlopen(), attach
2732 // to it with "strace -p <pid>" while it sleeps, and watch for
2733 // attempts to open nonexistent dependent shared libs
2734
2735 // TODO: automate some of these checks!
2736
2737 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002738 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002739 // the GC to ignore us.
2740 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002741 void* handle = NULL;
2742 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002743 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002744 handle = dlopen(path.c_str(), RTLD_LAZY);
2745 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002746
2747 if (verbose_jni) {
2748 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2749 }
2750
2751 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002752 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002753 return false;
2754 }
2755
2756 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002757 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002758 // TODO: move the locking (and more of this logic) into Libraries.
2759 MutexLock mu(libraries_lock);
2760 library = libraries->Get(path);
2761 if (library != NULL) {
2762 LOG(INFO) << "WOW: we lost a race to add shared library: "
2763 << "\"" << path << "\" ClassLoader=" << class_loader;
2764 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002765 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002766 library = new SharedLibrary(path, handle, class_loader);
2767 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002768 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002769
2770 if (verbose_jni) {
2771 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2772 }
2773
2774 bool result = true;
2775 void* sym = dlsym(handle, "JNI_OnLoad");
2776 if (sym == NULL) {
2777 if (verbose_jni) {
2778 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2779 }
2780 } else {
2781 // Call JNI_OnLoad. We have to override the current class
2782 // loader, which will always be "null" since the stuff at the
2783 // top of the stack is around Runtime.loadLibrary(). (See
2784 // the comments in the JNI FindClass function.)
2785 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2786 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002787 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002788 self->SetClassLoaderOverride(class_loader);
2789
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002790 int version = 0;
2791 {
2792 ScopedThreadStateChange tsc(self, Thread::kNative);
2793 if (verbose_jni) {
2794 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2795 }
2796 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002797 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002798
2799 self->SetClassLoaderOverride(old_class_loader);;
2800
2801 if (version != JNI_VERSION_1_2 &&
2802 version != JNI_VERSION_1_4 &&
2803 version != JNI_VERSION_1_6) {
2804 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2805 << "bad version: " << version;
2806 // It's unwise to call dlclose() here, but we can mark it
2807 // as bad and ensure that future load attempts will fail.
2808 // We don't know how far JNI_OnLoad got, so there could
2809 // be some partially-initialized stuff accessible through
2810 // newly-registered native method calls. We could try to
2811 // unregister them, but that doesn't seem worthwhile.
2812 result = false;
2813 } else {
2814 if (verbose_jni) {
2815 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2816 << " from JNI_OnLoad in \"" << path << "\"]";
2817 }
2818 }
2819 }
2820
2821 library->SetResult(result);
2822 return result;
2823}
2824
2825void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2826 CHECK(m->IsNative());
2827
2828 Class* c = m->GetDeclaringClass();
2829
2830 // If this is a static method, it could be called before the class
2831 // has been initialized.
2832 if (m->IsStatic()) {
2833 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2834 return NULL;
2835 }
2836 } else {
2837 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2838 }
2839
Brian Carlstrom16192862011-09-12 17:50:06 -07002840 std::string detail;
2841 void* native_method;
2842 {
2843 MutexLock mu(libraries_lock);
2844 native_method = libraries->FindNativeMethod(m, detail);
2845 }
2846 // throwing can cause libraries_lock to be reacquired
2847 if (native_method == NULL) {
2848 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
2849 "%s", detail.c_str());
2850 }
2851 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002852}
2853
Elliott Hughes410c0c82011-09-01 17:58:25 -07002854void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2855 {
2856 MutexLock mu(globals_lock);
2857 globals.VisitRoots(visitor, arg);
2858 }
2859 {
2860 MutexLock mu(pins_lock);
2861 pin_table.VisitRoots(visitor, arg);
2862 }
2863 // The weak_globals table is visited by the GC itself (because it mutates the table).
2864}
2865
Ian Rogersdf20fe02011-07-20 20:34:16 -07002866} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002867
2868std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2869 switch (rhs) {
2870 case JNIInvalidRefType:
2871 os << "JNIInvalidRefType";
2872 return os;
2873 case JNILocalRefType:
2874 os << "JNILocalRefType";
2875 return os;
2876 case JNIGlobalRefType:
2877 os << "JNIGlobalRefType";
2878 return os;
2879 case JNIWeakGlobalRefType:
2880 os << "JNIWeakGlobalRefType";
2881 return os;
2882 }
2883}