blob: 292d1e463a54bff133ee43e54016102fd8b4c242 [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"
Elliott Hughesc31664f2011-09-29 15:58:28 -070023#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070024#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070026
27namespace art {
28
Shih-wei Liao31384c52011-09-06 15:27:45 -070029void* FindNativeMethod(Thread* thread) {
30 DCHECK(Thread::Current() == thread);
31
32 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
33 DCHECK(method != NULL);
34
35 // Lookup symbol address for method, on failure we'll return NULL with an
36 // exception set, otherwise we return the address of the method we found.
37 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
38 if (native_code == NULL) {
39 DCHECK(thread->IsExceptionPending());
40 return NULL;
41 } else {
42 // Register so that future calls don't come here
43 method->RegisterNative(native_code);
44 return native_code;
45 }
46}
47
Elliott Hughesc5f7c912011-08-18 14:00:42 -070048/*
49 * Add a local reference for an object to the current stack frame. When
50 * the native function returns, the reference will be discarded.
51 *
52 * We need to allow the same reference to be added multiple times.
53 *
54 * This will be called on otherwise unreferenced objects. We cannot do
55 * GC allocations here, and it's best if we don't grab a mutex.
56 *
57 * Returns the local reference (currently just the same pointer that was
58 * passed in), or NULL on failure.
59 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070060template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070061T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
62 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
63 Object* obj = const_cast<Object*>(const_obj);
64
Elliott Hughesc5f7c912011-08-18 14:00:42 -070065 if (obj == NULL) {
66 return NULL;
67 }
68
Elliott Hughesbf86d042011-08-31 17:53:14 -070069 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
70 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070071
Ian Rogers5a7a74a2011-09-26 16:32:29 -070072 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070073 IndirectRef ref = locals.Add(cookie, obj);
74 if (ref == NULL) {
75 // TODO: just change Add's DCHECK to CHECK and lose this?
76 locals.Dump();
77 LOG(FATAL) << "Failed adding to JNI local reference table "
78 << "(has " << locals.Capacity() << " entries)";
79 // TODO: dvmDumpThread(dvmThreadSelf(), false);
80 }
81
82#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070083 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070084 size_t entry_count = locals.Capacity();
85 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070086 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070087 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
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 Hughes418d20f2011-09-22 14:00:39 -0700115template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
116template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700117template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700118
119namespace {
120
Elliott Hughescdf53122011-08-19 15:46:09 -0700121jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
122 if (obj == NULL) {
123 return NULL;
124 }
Elliott Hughes75770752011-08-24 17:52:38 -0700125 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700126 IndirectReferenceTable& weak_globals = vm->weak_globals;
127 MutexLock mu(vm->weak_globals_lock);
128 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
129 return reinterpret_cast<jweak>(ref);
130}
131
Elliott Hughesbf86d042011-08-31 17:53:14 -0700132// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700133template<typename T>
134T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700135 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700136}
137
Elliott Hughes418d20f2011-09-22 14:00:39 -0700138byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
139 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700140 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700141 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700142 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700143 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
144 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700145 case 'Z':
146 case 'B':
147 case 'C':
148 case 'S':
149 case 'I':
150 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
151 offset += 4;
152 break;
153 case 'F':
154 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
155 offset += 4;
156 break;
157 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700158 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700159 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
160 offset += sizeof(Object*);
161 break;
162 }
163 case 'D':
164 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
165 offset += 8;
166 break;
167 case 'J':
168 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
169 offset += 8;
170 break;
171 }
172 }
173 return arg_array.release();
174}
175
Elliott Hughes418d20f2011-09-22 14:00:39 -0700176byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
177 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700178 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700179 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700180 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700181 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
182 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700183 case 'Z':
184 case 'B':
185 case 'C':
186 case 'S':
187 case 'I':
188 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
189 offset += 4;
190 break;
191 case 'F':
192 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
193 offset += 4;
194 break;
195 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700196 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700197 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
198 offset += sizeof(Object*);
199 break;
200 }
201 case 'D':
202 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
203 offset += 8;
204 break;
205 case 'J':
206 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
207 offset += 8;
208 break;
209 }
210 }
211 return arg_array.release();
212}
213
Elliott Hughes418d20f2011-09-22 14:00:39 -0700214JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
215 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700216 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700217 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700218 return result;
219}
220
Elliott Hughes418d20f2011-09-22 14:00:39 -0700221JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
222 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
223 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700224 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700225 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
226 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700227}
228
Elliott Hughes75770752011-08-24 17:52:38 -0700229Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700230 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700231}
232
Elliott Hughes418d20f2011-09-22 14:00:39 -0700233JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
234 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
235 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700236 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700237 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
238 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700239}
240
Elliott Hughes418d20f2011-09-22 14:00:39 -0700241JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
242 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
243 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700244 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700245 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
246 return InvokeWithArgArray(env, 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);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700275 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700276 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) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700292 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700293 // TODO: try searching for the opposite kind of method from is_static
294 // for better diagnostics?
Elliott Hughescc5f9a92011-09-28 19:17:29 -0700295 ts.Self()->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700296 "no %s method %s", is_static ? "static" : "non-static",
297 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700298 return NULL;
299 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700300
Elliott Hughes418d20f2011-09-22 14:00:39 -0700301 method->InitJavaFields();
302
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700303 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700304}
305
Elliott Hughescdf53122011-08-19 15:46:09 -0700306jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
307 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700308 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700309 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700310 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700311
312 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700313 Class* field_type;
314 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
315 if (sig[1] != '\0') {
316 // TODO: need to get the appropriate ClassLoader.
317 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
318 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700319 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700321 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700322 if (field_type == NULL) {
323 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700324 DCHECK(ts.Self()->IsExceptionPending());
325 ts.Self()->ClearException();
326 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
327 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
328 "no type \"%s\" found and so no field \"%s\" could be found in class "
329 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700330 return NULL;
331 }
332 if (is_static) {
333 field = c->FindStaticField(name, field_type);
334 } else {
335 field = c->FindInstanceField(name, field_type);
336 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700337 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700338 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700339 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700340 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700341 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700342 return NULL;
343 }
Elliott Hughes80609252011-09-23 17:24:51 -0700344 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700345 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700346}
347
Elliott Hughes75770752011-08-24 17:52:38 -0700348void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
349 JavaVMExt* vm = ts.Vm();
350 MutexLock mu(vm->pins_lock);
351 vm->pin_table.Add(array);
352}
353
354void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
355 JavaVMExt* vm = ts.Vm();
356 MutexLock mu(vm->pins_lock);
357 vm->pin_table.Remove(array);
358}
359
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700360template<typename JniT, typename ArtT>
361JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
362 CHECK_GE(length, 0); // TODO: ReportJniError
363 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700364 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700365}
366
Elliott Hughes75770752011-08-24 17:52:38 -0700367template <typename ArrayT, typename CArrayT, typename ArtArrayT>
368CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
369 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
370 PinPrimitiveArray(ts, array);
371 if (is_copy != NULL) {
372 *is_copy = JNI_FALSE;
373 }
374 return array->GetData();
375}
376
377template <typename ArrayT>
378void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
379 if (mode != JNI_COMMIT) {
380 Array* array = Decode<Array*>(ts, java_array);
381 UnpinPrimitiveArray(ts, array);
382 }
383}
384
Elliott Hughes814e4032011-08-23 12:07:56 -0700385void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700386 std::string type(PrettyTypeOf(array));
Elliott Hughes814e4032011-08-23 12:07:56 -0700387 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
388 "%s offset=%d length=%d %s.length=%d",
389 type.c_str(), start, length, identifier, array->GetLength());
390}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700391void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
392 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
393 "offset=%d length=%d string.length()=%d", start, length, array_length);
394}
Elliott Hughes814e4032011-08-23 12:07:56 -0700395
396template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700397void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700398 ArrayT* array = Decode<ArrayT*>(ts, java_array);
399 if (start < 0 || length < 0 || start + length > array->GetLength()) {
400 ThrowAIOOBE(ts, array, start, length, "src");
401 } else {
402 JavaT* data = array->GetData();
403 memcpy(buf, data + start, length * sizeof(JavaT));
404 }
405}
406
407template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700408void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700409 ArrayT* array = Decode<ArrayT*>(ts, java_array);
410 if (start < 0 || length < 0 || start + length > array->GetLength()) {
411 ThrowAIOOBE(ts, array, start, length, "dst");
412 } else {
413 JavaT* data = array->GetData();
414 memcpy(data + start, buf, length * sizeof(JavaT));
415 }
416}
417
Elliott Hughes75770752011-08-24 17:52:38 -0700418jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700419 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
420 CHECK(buffer_class.get() != NULL);
421 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
422}
423
Elliott Hughes75770752011-08-24 17:52:38 -0700424jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700425 static jclass buffer_class = InitDirectByteBufferClass(env);
426 return buffer_class;
427}
428
Elliott Hughes75770752011-08-24 17:52:38 -0700429jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
430 if (vm == NULL || p_env == NULL) {
431 return JNI_ERR;
432 }
433
434 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
435 JavaVMAttachArgs args;
436 if (thr_args == NULL) {
437 // Allow the v1.1 calling convention.
438 args.version = JNI_VERSION_1_2;
439 args.name = NULL;
440 args.group = NULL; // TODO: get "main" thread group
441 } else {
442 args.version = in_args->version;
443 args.name = in_args->name;
444 if (in_args->group != NULL) {
445 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
446 args.group = NULL; // TODO: decode in_args->group
447 } else {
448 args.group = NULL; // TODO: get "main" thread group
449 }
450 }
451 CHECK_GE(args.version, JNI_VERSION_1_2);
452
453 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700454 runtime->AttachCurrentThread(args.name, as_daemon);
455 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700456 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700457}
458
Elliott Hughes79082e32011-08-25 12:07:32 -0700459class SharedLibrary {
460 public:
461 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
462 : path_(path),
463 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700464 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700465 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700466 jni_on_load_cond_("JNI_OnLoad"),
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 Hughes79082e32011-08-25 12:07:32 -0700469 }
470
Elliott Hughes79082e32011-08-25 12:07:32 -0700471 Object* GetClassLoader() {
472 return class_loader_;
473 }
474
475 std::string GetPath() {
476 return path_;
477 }
478
479 /*
480 * Check the result of an earlier call to JNI_OnLoad on this library. If
481 * the call has not yet finished in another thread, wait for it.
482 */
483 bool CheckOnLoadResult(JavaVMExt* vm) {
484 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700485 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700486 // Check this so we don't end up waiting for ourselves. We need
487 // to return "true" so the caller can continue.
488 LOG(INFO) << *self << " recursive attempt to load library "
489 << "\"" << path_ << "\"";
490 return true;
491 }
492
493 MutexLock mu(jni_on_load_lock_);
494 while (jni_on_load_result_ == kPending) {
495 if (vm->verbose_jni) {
496 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
497 << "JNI_OnLoad...]";
498 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700499 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700500 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700501 }
502
503 bool okay = (jni_on_load_result_ == kOkay);
504 if (vm->verbose_jni) {
505 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
506 << (okay ? "succeeded" : "failed") << "]";
507 }
508 return okay;
509 }
510
511 void SetResult(bool result) {
512 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700513 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700514
515 // Broadcast a wakeup to anybody sleeping on the condition variable.
516 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700517 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700518 }
519
520 void* FindSymbol(const std::string& symbol_name) {
521 return dlsym(handle_, symbol_name.c_str());
522 }
523
524 private:
525 enum JNI_OnLoadState {
526 kPending,
527 kFailed,
528 kOkay,
529 };
530
531 // Path to library "/system/lib/libjni.so".
532 std::string path_;
533
534 // The void* returned by dlopen(3).
535 void* handle_;
536
537 // The ClassLoader this library is associated with.
538 Object* class_loader_;
539
540 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700541 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700542 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700543 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700544 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700545 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 // Result of earlier JNI_OnLoad call.
547 JNI_OnLoadState jni_on_load_result_;
548};
549
Elliott Hughescdf53122011-08-19 15:46:09 -0700550} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700551
Elliott Hughes79082e32011-08-25 12:07:32 -0700552// This exists mainly to keep implementation details out of the header file.
553class Libraries {
554 public:
555 Libraries() {
556 }
557
558 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700559 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700560 }
561
562 SharedLibrary* Get(const std::string& path) {
563 return libraries_[path];
564 }
565
566 void Put(const std::string& path, SharedLibrary* library) {
567 libraries_[path] = library;
568 }
569
570 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700571 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700572 std::string jni_short_name(JniShortName(m));
573 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700574 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700575 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
576 SharedLibrary* library = it->second;
577 if (library->GetClassLoader() != declaring_class_loader) {
578 // We only search libraries loaded by the appropriate ClassLoader.
579 continue;
580 }
581 // Try the short name then the long name...
582 void* fn = library->FindSymbol(jni_short_name);
583 if (fn == NULL) {
584 fn = library->FindSymbol(jni_long_name);
585 }
586 if (fn != NULL) {
587 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700588 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700589 << " in \"" << library->GetPath() << "\"]";
590 }
591 return fn;
592 }
593 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700594 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700595 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700596 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700597 return NULL;
598 }
599
600 private:
601 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
602
603 std::map<std::string, SharedLibrary*> libraries_;
604};
605
Elliott Hughes418d20f2011-09-22 14:00:39 -0700606JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
607 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
608 Object* receiver = Decode<Object*>(env, obj);
609 Method* method = DecodeMethod(mid);
610 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
611 return InvokeWithArgArray(env, receiver, method, arg_array.get());
612}
613
Elliott Hughescdf53122011-08-19 15:46:09 -0700614class JNI {
615 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700616
Elliott Hughescdf53122011-08-19 15:46:09 -0700617 static jint GetVersion(JNIEnv* env) {
618 ScopedJniThreadState ts(env);
619 return JNI_VERSION_1_6;
620 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700621
Elliott Hughescdf53122011-08-19 15:46:09 -0700622 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
623 ScopedJniThreadState ts(env);
624 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700625 return NULL;
626 }
627
Elliott Hughescdf53122011-08-19 15:46:09 -0700628 static jclass FindClass(JNIEnv* env, const char* name) {
629 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700630 Runtime* runtime = Runtime::Current();
631 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700633 Class* c = NULL;
634 if (runtime->IsStarted()) {
635 // TODO: need to get the appropriate ClassLoader.
636 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
637 c = class_linker->FindClass(descriptor, cl);
638 } else {
639 c = class_linker->FindSystemClass(descriptor);
640 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700641 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700642 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700643
Elliott Hughescdf53122011-08-19 15:46:09 -0700644 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
645 ScopedJniThreadState ts(env);
646 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700647 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700648 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700649
Elliott Hughescdf53122011-08-19 15:46:09 -0700650 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
651 ScopedJniThreadState ts(env);
652 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700653 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700654 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700655
Elliott Hughescdf53122011-08-19 15:46:09 -0700656 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
657 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700658 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700659 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700661
Elliott Hughescdf53122011-08-19 15:46:09 -0700662 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
663 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700664 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700665 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700667
Elliott Hughes37f7a402011-08-22 18:56:01 -0700668 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
669 ScopedJniThreadState ts(env);
670 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700671 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700672 }
673
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700674 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700675 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700676 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700677 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700679
Elliott Hughes37f7a402011-08-22 18:56:01 -0700680 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700682 Class* c1 = Decode<Class*>(ts, java_class1);
683 Class* c2 = Decode<Class*>(ts, java_class2);
684 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700685 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700686
Elliott Hughes37f7a402011-08-22 18:56:01 -0700687 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700688 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700689 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700690 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700691 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 return JNI_TRUE;
693 } else {
694 Object* obj = Decode<Object*>(ts, jobj);
695 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700696 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700697 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700698 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700699
Elliott Hughes37f7a402011-08-22 18:56:01 -0700700 static jint Throw(JNIEnv* env, jthrowable java_exception) {
701 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700702 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700703 if (exception == NULL) {
704 return JNI_ERR;
705 }
706 ts.Self()->SetException(exception);
707 return JNI_OK;
708 }
709
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700710 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700711 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700712 // TODO: check for a pending exception to decide what constructor to call.
713 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
714 if (mid == NULL) {
715 return JNI_ERR;
716 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700717 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
718 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700719 return JNI_ERR;
720 }
721
722 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700723 args[0].l = s.get();
724 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
725 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700726 return JNI_ERR;
727 }
728
Elliott Hughes54e7df12011-09-16 11:47:04 -0700729 LOG(INFO) << "Throwing " << PrettyTypeOf(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700730 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700731 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700732
Elliott Hughes37f7a402011-08-22 18:56:01 -0700733 return JNI_OK;
734 }
735
736 static jboolean ExceptionCheck(JNIEnv* env) {
737 ScopedJniThreadState ts(env);
738 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
739 }
740
741 static void ExceptionClear(JNIEnv* env) {
742 ScopedJniThreadState ts(env);
743 ts.Self()->ClearException();
744 }
745
746 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700747 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700748
749 Thread* self = ts.Self();
750 Throwable* original_exception = self->GetException();
751 self->ClearException();
752
Elliott Hughesbf86d042011-08-31 17:53:14 -0700753 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700754 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
755 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
756 if (mid == NULL) {
757 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700758 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700759 } else {
760 env->CallVoidMethod(exception.get(), mid);
761 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700762 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700763 << " thrown while calling printStackTrace";
764 self->ClearException();
765 }
766 }
767
768 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700770
Elliott Hughescdf53122011-08-19 15:46:09 -0700771 static jthrowable ExceptionOccurred(JNIEnv* env) {
772 ScopedJniThreadState ts(env);
773 Object* exception = ts.Self()->GetException();
774 if (exception == NULL) {
775 return NULL;
776 } else {
777 // TODO: if adding a local reference failing causes the VM to abort
778 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700779 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 if (localException == NULL) {
781 // We were unable to add a new local reference, and threw a new
782 // exception. We can't return "exception", because it's not a
783 // local reference. So we have to return NULL, indicating that
784 // there was no exception, even though it's pretty much raining
785 // exceptions in here.
786 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
787 }
788 return localException;
789 }
790 }
791
Elliott Hughescdf53122011-08-19 15:46:09 -0700792 static void FatalError(JNIEnv* env, const char* msg) {
793 ScopedJniThreadState ts(env);
794 LOG(FATAL) << "JNI FatalError called: " << msg;
795 }
796
797 static jint PushLocalFrame(JNIEnv* env, jint cap) {
798 ScopedJniThreadState ts(env);
799 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
800 return JNI_OK;
801 }
802
803 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
804 ScopedJniThreadState ts(env);
805 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
806 return res;
807 }
808
Elliott Hughes72025e52011-08-23 17:50:30 -0700809 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
810 ScopedJniThreadState ts(env);
811 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
812 return 0;
813 }
814
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
816 ScopedJniThreadState ts(env);
817 if (obj == NULL) {
818 return NULL;
819 }
820
Elliott Hughes75770752011-08-24 17:52:38 -0700821 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 IndirectReferenceTable& globals = vm->globals;
823 MutexLock mu(vm->globals_lock);
824 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
825 return reinterpret_cast<jobject>(ref);
826 }
827
828 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
829 ScopedJniThreadState ts(env);
830 if (obj == NULL) {
831 return;
832 }
833
Elliott Hughes75770752011-08-24 17:52:38 -0700834 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 IndirectReferenceTable& globals = vm->globals;
836 MutexLock mu(vm->globals_lock);
837
838 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
839 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
840 << "failed to find entry";
841 }
842 }
843
844 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
845 ScopedJniThreadState ts(env);
846 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
847 }
848
849 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
850 ScopedJniThreadState ts(env);
851 if (obj == NULL) {
852 return;
853 }
854
Elliott Hughes75770752011-08-24 17:52:38 -0700855 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 IndirectReferenceTable& weak_globals = vm->weak_globals;
857 MutexLock mu(vm->weak_globals_lock);
858
859 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
860 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
861 << "failed to find entry";
862 }
863 }
864
865 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
866 ScopedJniThreadState ts(env);
867 if (obj == NULL) {
868 return NULL;
869 }
870
871 IndirectReferenceTable& locals = ts.Env()->locals;
872
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700873 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
875 return reinterpret_cast<jobject>(ref);
876 }
877
878 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
879 ScopedJniThreadState ts(env);
880 if (obj == NULL) {
881 return;
882 }
883
884 IndirectReferenceTable& locals = ts.Env()->locals;
885
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700886 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 if (!locals.Remove(cookie, obj)) {
888 // Attempting to delete a local reference that is not in the
889 // topmost local reference frame is a no-op. DeleteLocalRef returns
890 // void and doesn't throw any exceptions, but we should probably
891 // complain about it so the user will notice that things aren't
892 // going quite the way they expect.
893 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
894 << "failed to find entry";
895 }
896 }
897
898 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
899 ScopedJniThreadState ts(env);
900 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
901 ? JNI_TRUE : JNI_FALSE;
902 }
903
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700904 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700905 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700906 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700907 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700908 return NULL;
909 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700910 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700911 }
912
Elliott Hughes72025e52011-08-23 17:50:30 -0700913 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 ScopedJniThreadState ts(env);
915 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700916 va_start(args, mid);
917 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700918 va_end(args);
919 return result;
920 }
921
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700924 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700925 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700926 return NULL;
927 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700928 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700929 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 return local_result;
932 }
933
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700936 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700937 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700938 return NULL;
939 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700940 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700941 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 return local_result;
944 }
945
Elliott Hughescdf53122011-08-19 15:46:09 -0700946 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
947 ScopedJniThreadState ts(env);
948 return FindMethodID(ts, c, name, sig, false);
949 }
950
951 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
952 ScopedJniThreadState ts(env);
953 return FindMethodID(ts, c, name, sig, true);
954 }
955
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700958 va_list ap;
959 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700960 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700962 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 }
964
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700967 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700968 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 }
970
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700973 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700974 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 }
976
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 va_list ap;
980 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700981 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 va_end(ap);
983 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 }
985
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700988 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 }
990
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700993 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 va_list ap;
999 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001000 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 va_end(ap);
1002 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 }
1004
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001007 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001012 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 va_list ap;
1018 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001019 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 va_end(ap);
1021 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 }
1023
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001026 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001031 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 va_list ap;
1037 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001038 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 va_end(ap);
1040 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001045 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001050 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 va_list ap;
1056 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001057 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 va_end(ap);
1059 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001064 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001069 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 va_list ap;
1075 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001076 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 va_end(ap);
1078 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001083 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001088 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 va_list ap;
1094 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001095 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 va_end(ap);
1097 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001102 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001107 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 va_list ap;
1113 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001114 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_end(ap);
1116 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001121 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001126 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 va_list ap;
1132 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001133 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001139 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001144 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
1147 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 ScopedJniThreadState ts(env);
1150 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001152 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001153 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 va_end(ap);
1155 return local_result;
1156 }
1157
1158 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001161 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001162 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
1165 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001168 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001169 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 }
1171
1172 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 ScopedJniThreadState ts(env);
1175 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001177 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 va_end(ap);
1179 return result.z;
1180 }
1181
1182 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001185 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 }
1187
1188 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001191 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
1194 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 ScopedJniThreadState ts(env);
1197 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001199 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 va_end(ap);
1201 return result.b;
1202 }
1203
1204 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001207 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
1210 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001213 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
1215
1216 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 ScopedJniThreadState ts(env);
1219 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001220 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001221 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 va_end(ap);
1223 return result.c;
1224 }
1225
1226 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001229 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 }
1231
1232 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001235 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 }
1237
1238 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001239 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 ScopedJniThreadState ts(env);
1241 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001242 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001243 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 va_end(ap);
1245 return result.s;
1246 }
1247
1248 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001251 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
1254 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001257 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
Elliott Hughes418d20f2011-09-22 14:00:39 -07001260 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
1262 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001264 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 va_end(ap);
1266 return result.i;
1267 }
1268
1269 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001270 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001272 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 }
1274
1275 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001276 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001278 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 }
1280
1281 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 ScopedJniThreadState ts(env);
1284 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001286 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 va_end(ap);
1288 return result.j;
1289 }
1290
1291 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001292 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001294 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
1297 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001298 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001300 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
1303 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001304 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 ScopedJniThreadState ts(env);
1306 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001308 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 va_end(ap);
1310 return result.f;
1311 }
1312
1313 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001314 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001316 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 }
1318
1319 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001320 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001322 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
1325 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 ScopedJniThreadState ts(env);
1328 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001329 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001330 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 va_end(ap);
1332 return result.d;
1333 }
1334
1335 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001336 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001338 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
1341 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001342 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001344 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 }
1346
1347 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001348 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 ScopedJniThreadState ts(env);
1350 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001351 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001352 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 va_end(ap);
1354 }
1355
1356 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001357 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001359 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 }
1361
1362 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001363 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001365 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
1367
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001368 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 ScopedJniThreadState ts(env);
1370 return FindFieldID(ts, c, name, sig, false);
1371 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001372
1373
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001374 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 ScopedJniThreadState ts(env);
1376 return FindFieldID(ts, c, name, sig, true);
1377 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001378
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001379 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001382 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001383 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001385
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001388 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001389 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 Object* o = Decode<Object*>(ts, java_object);
1395 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001396 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001397 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001403 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407#define GET_PRIMITIVE_FIELD(fn, instance) \
1408 ScopedJniThreadState ts(env); \
1409 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001410 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001411 return f->fn(o)
1412
1413#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1414 ScopedJniThreadState ts(env); \
1415 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001416 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001417 f->fn(o, value)
1418
1419 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1420 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001423 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1424 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 }
1426
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001427 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1428 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001431 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1432 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1436 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1440 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1444 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1448 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetLong, NULL);
1473 }
1474
1475 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1476 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1477 }
1478
1479 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1480 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1481 }
1482
1483 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1484 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1485 }
1486
1487 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1488 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1489 }
1490
1491 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1492 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1493 }
1494
1495 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1496 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1497 }
1498
1499 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1500 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1501 }
1502
1503 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1504 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1505 }
1506
1507 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1508 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1509 }
1510
1511 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1512 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1513 }
1514
1515 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1516 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1517 }
1518
1519 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1520 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1521 }
1522
1523 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1524 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1525 }
1526
1527 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1528 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1529 }
1530
1531 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1532 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1533 }
1534
1535 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1536 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1537 }
1538
1539 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1540 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1541 }
1542
1543 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1544 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes418d20f2011-09-22 14:00:39 -07001547 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 ScopedJniThreadState ts(env);
1549 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001550 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001551 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001552 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 va_end(ap);
1554 return local_result;
1555 }
1556
Elliott Hughes418d20f2011-09-22 14:00:39 -07001557 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001559 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001560 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes418d20f2011-09-22 14:00:39 -07001563 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001564 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001565 JValue result = InvokeWithJValues(env, 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
Elliott Hughes418d20f2011-09-22 14:00:39 -07001569 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 ScopedJniThreadState ts(env);
1571 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001572 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001573 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 va_end(ap);
1575 return result.z;
1576 }
1577
Elliott Hughes418d20f2011-09-22 14:00:39 -07001578 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001580 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
Elliott Hughes418d20f2011-09-22 14:00:39 -07001583 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001585 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
Elliott Hughes72025e52011-08-23 17:50:30 -07001588 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 ScopedJniThreadState ts(env);
1590 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001591 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001592 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 va_end(ap);
1594 return result.b;
1595 }
1596
Elliott Hughes418d20f2011-09-22 14:00:39 -07001597 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001599 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 }
1601
Elliott Hughes418d20f2011-09-22 14:00:39 -07001602 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001604 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes72025e52011-08-23 17:50:30 -07001607 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 ScopedJniThreadState ts(env);
1609 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001610 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001611 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 va_end(ap);
1613 return result.c;
1614 }
1615
Elliott Hughes418d20f2011-09-22 14:00:39 -07001616 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001618 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes418d20f2011-09-22 14:00:39 -07001621 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001623 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 ScopedJniThreadState ts(env);
1628 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001629 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001630 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 va_end(ap);
1632 return result.s;
1633 }
1634
Elliott Hughes418d20f2011-09-22 14:00:39 -07001635 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001637 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 }
1639
Elliott Hughes418d20f2011-09-22 14:00:39 -07001640 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001642 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes72025e52011-08-23 17:50:30 -07001645 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
1647 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001649 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 va_end(ap);
1651 return result.i;
1652 }
1653
Elliott Hughes418d20f2011-09-22 14:00:39 -07001654 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001656 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 }
1658
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001661 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
Elliott Hughes72025e52011-08-23 17:50:30 -07001664 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 ScopedJniThreadState ts(env);
1666 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001668 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 va_end(ap);
1670 return result.j;
1671 }
1672
Elliott Hughes418d20f2011-09-22 14:00:39 -07001673 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001675 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 }
1677
Elliott Hughes418d20f2011-09-22 14:00:39 -07001678 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001680 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
Elliott Hughes72025e52011-08-23 17:50:30 -07001683 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 ScopedJniThreadState ts(env);
1685 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001687 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 va_end(ap);
1689 return result.f;
1690 }
1691
Elliott Hughes418d20f2011-09-22 14:00:39 -07001692 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001694 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 }
1696
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001699 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 }
1701
Elliott Hughes72025e52011-08-23 17:50:30 -07001702 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 ScopedJniThreadState ts(env);
1704 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001706 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 va_end(ap);
1708 return result.d;
1709 }
1710
Elliott Hughes418d20f2011-09-22 14:00:39 -07001711 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001713 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 }
1715
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001718 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes72025e52011-08-23 17:50:30 -07001721 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 ScopedJniThreadState ts(env);
1723 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001724 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001725 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 va_end(ap);
1727 }
1728
Elliott Hughes418d20f2011-09-22 14:00:39 -07001729 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001731 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes418d20f2011-09-22 14:00:39 -07001734 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001736 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes814e4032011-08-23 12:07:56 -07001739 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001741 if (chars == NULL && char_count == 0) {
1742 return NULL;
1743 }
1744 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001745 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 }
1747
1748 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1749 ScopedJniThreadState ts(env);
1750 if (utf == NULL) {
1751 return NULL;
1752 }
1753 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001754 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes814e4032011-08-23 12:07:56 -07001757 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1758 ScopedJniThreadState ts(env);
1759 return Decode<String*>(ts, java_string)->GetLength();
1760 }
1761
1762 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1763 ScopedJniThreadState ts(env);
1764 return Decode<String*>(ts, java_string)->GetUtfLength();
1765 }
1766
Elliott Hughesb465ab02011-08-24 11:21:21 -07001767 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001769 String* s = Decode<String*>(ts, java_string);
1770 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1771 ThrowSIOOBE(ts, start, length, s->GetLength());
1772 } else {
1773 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1774 memcpy(buf, chars + start, length * sizeof(jchar));
1775 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001776 }
1777
Elliott Hughesb465ab02011-08-24 11:21:21 -07001778 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001779 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001780 String* s = Decode<String*>(ts, java_string);
1781 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1782 ThrowSIOOBE(ts, start, length, s->GetLength());
1783 } else {
1784 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1785 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1786 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001787 }
1788
Elliott Hughes75770752011-08-24 17:52:38 -07001789 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001790 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001791 String* s = Decode<String*>(ts, java_string);
1792 const CharArray* chars = s->GetCharArray();
1793 PinPrimitiveArray(ts, chars);
1794 if (is_copy != NULL) {
1795 *is_copy = JNI_FALSE;
1796 }
1797 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001798 }
1799
Elliott Hughes75770752011-08-24 17:52:38 -07001800 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001801 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001802 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes75770752011-08-24 17:52:38 -07001805 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001807 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
Elliott Hughes75770752011-08-24 17:52:38 -07001810 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001812 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes75770752011-08-24 17:52:38 -07001815 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001816 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001817 if (java_string == NULL) {
1818 return NULL;
1819 }
1820 if (is_copy != NULL) {
1821 *is_copy = JNI_TRUE;
1822 }
1823 String* s = Decode<String*>(ts, java_string);
1824 size_t byte_count = s->GetUtfLength();
1825 char* bytes = new char[byte_count + 1];
1826 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001827 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001828 return NULL;
1829 }
1830 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1831 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1832 bytes[byte_count] = '\0';
1833 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001834 }
1835
Elliott Hughes75770752011-08-24 17:52:38 -07001836 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001837 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001838 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001839 }
1840
Elliott Hughesbd935992011-08-22 11:59:34 -07001841 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001843 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001844 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001845 Array* array = obj->AsArray();
1846 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 }
1848
Elliott Hughes814e4032011-08-23 12:07:56 -07001849 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001851 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001852 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 }
1854
1855 static void SetObjectArrayElement(JNIEnv* env,
1856 jobjectArray java_array, jsize index, jobject java_value) {
1857 ScopedJniThreadState ts(env);
1858 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1859 Object* value = Decode<Object*>(ts, java_value);
1860 array->Set(index, value);
1861 }
1862
1863 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1864 ScopedJniThreadState ts(env);
1865 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1866 }
1867
1868 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1869 ScopedJniThreadState ts(env);
1870 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1871 }
1872
1873 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1874 ScopedJniThreadState ts(env);
1875 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1876 }
1877
1878 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1879 ScopedJniThreadState ts(env);
1880 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1881 }
1882
1883 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1884 ScopedJniThreadState ts(env);
1885 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1886 }
1887
1888 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1889 ScopedJniThreadState ts(env);
1890 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1891 }
1892
1893 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1894 ScopedJniThreadState ts(env);
1895 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1896 }
1897
1898 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1899 ScopedJniThreadState ts(env);
1900 CHECK_GE(length, 0); // TODO: ReportJniError
1901
1902 // Compute the array class corresponding to the given element class.
1903 Class* element_class = Decode<Class*>(ts, element_jclass);
1904 std::string descriptor;
1905 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001906 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001907
1908 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001909 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1910 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 return NULL;
1912 }
1913
Elliott Hughes75770752011-08-24 17:52:38 -07001914 // Allocate and initialize if necessary.
1915 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001916 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001917 if (initial_element != NULL) {
1918 Object* initial_object = Decode<Object*>(ts, initial_element);
1919 for (jsize i = 0; i < length; ++i) {
1920 result->Set(i, initial_object);
1921 }
1922 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001923 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001924 }
1925
1926 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1927 ScopedJniThreadState ts(env);
1928 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1929 }
1930
Elliott Hughes75770752011-08-24 17:52:38 -07001931 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001932 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001933 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001934 }
1935
Elliott Hughes75770752011-08-24 17:52:38 -07001936 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001937 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001938 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001939 }
1940
Elliott Hughes75770752011-08-24 17:52:38 -07001941 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001942 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001943 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 }
1945
Elliott Hughes75770752011-08-24 17:52:38 -07001946 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001948 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
Elliott Hughes75770752011-08-24 17:52:38 -07001951 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001953 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Elliott Hughes75770752011-08-24 17:52:38 -07001956 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001958 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 }
1960
Elliott Hughes75770752011-08-24 17:52:38 -07001961 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001962 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001963 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Elliott Hughes75770752011-08-24 17:52:38 -07001966 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001968 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 }
1970
Elliott Hughes75770752011-08-24 17:52:38 -07001971 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001973 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Elliott Hughes75770752011-08-24 17:52:38 -07001976 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001978 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Elliott Hughes75770752011-08-24 17:52:38 -07001981 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001983 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Elliott Hughes75770752011-08-24 17:52:38 -07001986 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001988 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes75770752011-08-24 17:52:38 -07001991 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001993 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes75770752011-08-24 17:52:38 -07001996 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001998 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Elliott Hughes75770752011-08-24 17:52:38 -07002001 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002003 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Elliott Hughes75770752011-08-24 17:52:38 -07002006 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002008 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Elliott Hughes75770752011-08-24 17:52:38 -07002011 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002013 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes75770752011-08-24 17:52:38 -07002016 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002018 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes814e4032011-08-23 12:07:56 -07002021 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002023 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes814e4032011-08-23 12:07:56 -07002026 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002028 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes814e4032011-08-23 12:07:56 -07002031 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002033 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes814e4032011-08-23 12:07:56 -07002036 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002038 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes814e4032011-08-23 12:07:56 -07002041 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002043 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Elliott Hughes814e4032011-08-23 12:07:56 -07002046 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Elliott Hughes814e4032011-08-23 12:07:56 -07002051 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Elliott Hughes5174fe62011-08-23 15:12:35 -07002101 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002103 Class* c = Decode<Class*>(ts, java_class);
2104
Elliott Hughes5174fe62011-08-23 15:12:35 -07002105 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 const char* name = methods[i].name;
2107 const char* sig = methods[i].signature;
2108
2109 if (*sig == '!') {
2110 // TODO: fast jni. it's too noisy to log all these.
2111 ++sig;
2112 }
2113
Elliott Hughes5174fe62011-08-23 15:12:35 -07002114 Method* m = c->FindDirectMethod(name, sig);
2115 if (m == NULL) {
2116 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002118 if (m == NULL) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002119 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescc5f9a92011-09-28 19:17:29 -07002120 ts.Self()->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002122 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002124 } else if (!m->IsNative()) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002125 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescc5f9a92011-09-28 19:17:29 -07002126 ts.Self()->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002128 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 return JNI_ERR;
2130 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002131
Elliott Hughes75770752011-08-24 17:52:38 -07002132 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002133 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002134 }
2135
2136 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138 return JNI_OK;
2139 }
2140
Elliott Hughes5174fe62011-08-23 15:12:35 -07002141 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143 Class* c = Decode<Class*>(ts, java_class);
2144
Elliott Hughes75770752011-08-24 17:52:38 -07002145 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002146 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002147 }
2148
2149 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2150 Method* m = c->GetDirectMethod(i);
2151 if (m->IsNative()) {
2152 m->UnregisterNative();
2153 }
2154 }
2155 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2156 Method* m = c->GetVirtualMethod(i);
2157 if (m->IsNative()) {
2158 m->UnregisterNative();
2159 }
2160 }
2161
2162 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 }
2164
Elliott Hughes72025e52011-08-23 17:50:30 -07002165 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002167 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002168 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
Elliott Hughes72025e52011-08-23 17:50:30 -07002171 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002173 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002174 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
2176
2177 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2178 ScopedJniThreadState ts(env);
2179 Runtime* runtime = Runtime::Current();
2180 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002181 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 } else {
2183 *vm = NULL;
2184 }
2185 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2186 }
2187
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2189 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002190
2191 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002192 CHECK(address != NULL); // TODO: ReportJniError
2193 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002194
2195 jclass buffer_class = GetDirectByteBufferClass(env);
2196 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2197 if (mid == NULL) {
2198 return NULL;
2199 }
2200
2201 // At the moment, the Java side is limited to 32 bits.
2202 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2203 CHECK_LE(capacity, 0xffffffff);
2204 jint address_arg = reinterpret_cast<jint>(address);
2205 jint capacity_arg = static_cast<jint>(capacity);
2206
2207 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2208 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210
Elliott Hughesb465ab02011-08-24 11:21:21 -07002211 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002213 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2214 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 }
2216
Elliott Hughesb465ab02011-08-24 11:21:21 -07002217 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002219 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2220 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 }
2222
Elliott Hughesb465ab02011-08-24 11:21:21 -07002223 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002225
Elliott Hughes75770752011-08-24 17:52:38 -07002226 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002227
2228 // Do we definitely know what kind of reference this is?
2229 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2230 IndirectRefKind kind = GetIndirectRefKind(ref);
2231 switch (kind) {
2232 case kLocal:
2233 return JNILocalRefType;
2234 case kGlobal:
2235 return JNIGlobalRefType;
2236 case kWeakGlobal:
2237 return JNIWeakGlobalRefType;
2238 case kSirtOrInvalid:
2239 // Is it in a stack IRT?
2240 if (ts.Self()->SirtContains(java_object)) {
2241 return JNILocalRefType;
2242 }
2243
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002244 if (!ts.Env()->work_around_app_jni_bugs) {
2245 return JNIInvalidRefType;
2246 }
2247
Elliott Hughesb465ab02011-08-24 11:21:21 -07002248 // If we're handing out direct pointers, check whether it's a direct pointer
2249 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002250 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002251 if (ts.Env()->locals.Contains(java_object)) {
2252 return JNILocalRefType;
2253 }
2254 }
2255
2256 return JNIInvalidRefType;
2257 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002258 }
2259};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002260
Elliott Hughesa2501992011-08-26 19:39:54 -07002261const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002262 NULL, // reserved0.
2263 NULL, // reserved1.
2264 NULL, // reserved2.
2265 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002266 JNI::GetVersion,
2267 JNI::DefineClass,
2268 JNI::FindClass,
2269 JNI::FromReflectedMethod,
2270 JNI::FromReflectedField,
2271 JNI::ToReflectedMethod,
2272 JNI::GetSuperclass,
2273 JNI::IsAssignableFrom,
2274 JNI::ToReflectedField,
2275 JNI::Throw,
2276 JNI::ThrowNew,
2277 JNI::ExceptionOccurred,
2278 JNI::ExceptionDescribe,
2279 JNI::ExceptionClear,
2280 JNI::FatalError,
2281 JNI::PushLocalFrame,
2282 JNI::PopLocalFrame,
2283 JNI::NewGlobalRef,
2284 JNI::DeleteGlobalRef,
2285 JNI::DeleteLocalRef,
2286 JNI::IsSameObject,
2287 JNI::NewLocalRef,
2288 JNI::EnsureLocalCapacity,
2289 JNI::AllocObject,
2290 JNI::NewObject,
2291 JNI::NewObjectV,
2292 JNI::NewObjectA,
2293 JNI::GetObjectClass,
2294 JNI::IsInstanceOf,
2295 JNI::GetMethodID,
2296 JNI::CallObjectMethod,
2297 JNI::CallObjectMethodV,
2298 JNI::CallObjectMethodA,
2299 JNI::CallBooleanMethod,
2300 JNI::CallBooleanMethodV,
2301 JNI::CallBooleanMethodA,
2302 JNI::CallByteMethod,
2303 JNI::CallByteMethodV,
2304 JNI::CallByteMethodA,
2305 JNI::CallCharMethod,
2306 JNI::CallCharMethodV,
2307 JNI::CallCharMethodA,
2308 JNI::CallShortMethod,
2309 JNI::CallShortMethodV,
2310 JNI::CallShortMethodA,
2311 JNI::CallIntMethod,
2312 JNI::CallIntMethodV,
2313 JNI::CallIntMethodA,
2314 JNI::CallLongMethod,
2315 JNI::CallLongMethodV,
2316 JNI::CallLongMethodA,
2317 JNI::CallFloatMethod,
2318 JNI::CallFloatMethodV,
2319 JNI::CallFloatMethodA,
2320 JNI::CallDoubleMethod,
2321 JNI::CallDoubleMethodV,
2322 JNI::CallDoubleMethodA,
2323 JNI::CallVoidMethod,
2324 JNI::CallVoidMethodV,
2325 JNI::CallVoidMethodA,
2326 JNI::CallNonvirtualObjectMethod,
2327 JNI::CallNonvirtualObjectMethodV,
2328 JNI::CallNonvirtualObjectMethodA,
2329 JNI::CallNonvirtualBooleanMethod,
2330 JNI::CallNonvirtualBooleanMethodV,
2331 JNI::CallNonvirtualBooleanMethodA,
2332 JNI::CallNonvirtualByteMethod,
2333 JNI::CallNonvirtualByteMethodV,
2334 JNI::CallNonvirtualByteMethodA,
2335 JNI::CallNonvirtualCharMethod,
2336 JNI::CallNonvirtualCharMethodV,
2337 JNI::CallNonvirtualCharMethodA,
2338 JNI::CallNonvirtualShortMethod,
2339 JNI::CallNonvirtualShortMethodV,
2340 JNI::CallNonvirtualShortMethodA,
2341 JNI::CallNonvirtualIntMethod,
2342 JNI::CallNonvirtualIntMethodV,
2343 JNI::CallNonvirtualIntMethodA,
2344 JNI::CallNonvirtualLongMethod,
2345 JNI::CallNonvirtualLongMethodV,
2346 JNI::CallNonvirtualLongMethodA,
2347 JNI::CallNonvirtualFloatMethod,
2348 JNI::CallNonvirtualFloatMethodV,
2349 JNI::CallNonvirtualFloatMethodA,
2350 JNI::CallNonvirtualDoubleMethod,
2351 JNI::CallNonvirtualDoubleMethodV,
2352 JNI::CallNonvirtualDoubleMethodA,
2353 JNI::CallNonvirtualVoidMethod,
2354 JNI::CallNonvirtualVoidMethodV,
2355 JNI::CallNonvirtualVoidMethodA,
2356 JNI::GetFieldID,
2357 JNI::GetObjectField,
2358 JNI::GetBooleanField,
2359 JNI::GetByteField,
2360 JNI::GetCharField,
2361 JNI::GetShortField,
2362 JNI::GetIntField,
2363 JNI::GetLongField,
2364 JNI::GetFloatField,
2365 JNI::GetDoubleField,
2366 JNI::SetObjectField,
2367 JNI::SetBooleanField,
2368 JNI::SetByteField,
2369 JNI::SetCharField,
2370 JNI::SetShortField,
2371 JNI::SetIntField,
2372 JNI::SetLongField,
2373 JNI::SetFloatField,
2374 JNI::SetDoubleField,
2375 JNI::GetStaticMethodID,
2376 JNI::CallStaticObjectMethod,
2377 JNI::CallStaticObjectMethodV,
2378 JNI::CallStaticObjectMethodA,
2379 JNI::CallStaticBooleanMethod,
2380 JNI::CallStaticBooleanMethodV,
2381 JNI::CallStaticBooleanMethodA,
2382 JNI::CallStaticByteMethod,
2383 JNI::CallStaticByteMethodV,
2384 JNI::CallStaticByteMethodA,
2385 JNI::CallStaticCharMethod,
2386 JNI::CallStaticCharMethodV,
2387 JNI::CallStaticCharMethodA,
2388 JNI::CallStaticShortMethod,
2389 JNI::CallStaticShortMethodV,
2390 JNI::CallStaticShortMethodA,
2391 JNI::CallStaticIntMethod,
2392 JNI::CallStaticIntMethodV,
2393 JNI::CallStaticIntMethodA,
2394 JNI::CallStaticLongMethod,
2395 JNI::CallStaticLongMethodV,
2396 JNI::CallStaticLongMethodA,
2397 JNI::CallStaticFloatMethod,
2398 JNI::CallStaticFloatMethodV,
2399 JNI::CallStaticFloatMethodA,
2400 JNI::CallStaticDoubleMethod,
2401 JNI::CallStaticDoubleMethodV,
2402 JNI::CallStaticDoubleMethodA,
2403 JNI::CallStaticVoidMethod,
2404 JNI::CallStaticVoidMethodV,
2405 JNI::CallStaticVoidMethodA,
2406 JNI::GetStaticFieldID,
2407 JNI::GetStaticObjectField,
2408 JNI::GetStaticBooleanField,
2409 JNI::GetStaticByteField,
2410 JNI::GetStaticCharField,
2411 JNI::GetStaticShortField,
2412 JNI::GetStaticIntField,
2413 JNI::GetStaticLongField,
2414 JNI::GetStaticFloatField,
2415 JNI::GetStaticDoubleField,
2416 JNI::SetStaticObjectField,
2417 JNI::SetStaticBooleanField,
2418 JNI::SetStaticByteField,
2419 JNI::SetStaticCharField,
2420 JNI::SetStaticShortField,
2421 JNI::SetStaticIntField,
2422 JNI::SetStaticLongField,
2423 JNI::SetStaticFloatField,
2424 JNI::SetStaticDoubleField,
2425 JNI::NewString,
2426 JNI::GetStringLength,
2427 JNI::GetStringChars,
2428 JNI::ReleaseStringChars,
2429 JNI::NewStringUTF,
2430 JNI::GetStringUTFLength,
2431 JNI::GetStringUTFChars,
2432 JNI::ReleaseStringUTFChars,
2433 JNI::GetArrayLength,
2434 JNI::NewObjectArray,
2435 JNI::GetObjectArrayElement,
2436 JNI::SetObjectArrayElement,
2437 JNI::NewBooleanArray,
2438 JNI::NewByteArray,
2439 JNI::NewCharArray,
2440 JNI::NewShortArray,
2441 JNI::NewIntArray,
2442 JNI::NewLongArray,
2443 JNI::NewFloatArray,
2444 JNI::NewDoubleArray,
2445 JNI::GetBooleanArrayElements,
2446 JNI::GetByteArrayElements,
2447 JNI::GetCharArrayElements,
2448 JNI::GetShortArrayElements,
2449 JNI::GetIntArrayElements,
2450 JNI::GetLongArrayElements,
2451 JNI::GetFloatArrayElements,
2452 JNI::GetDoubleArrayElements,
2453 JNI::ReleaseBooleanArrayElements,
2454 JNI::ReleaseByteArrayElements,
2455 JNI::ReleaseCharArrayElements,
2456 JNI::ReleaseShortArrayElements,
2457 JNI::ReleaseIntArrayElements,
2458 JNI::ReleaseLongArrayElements,
2459 JNI::ReleaseFloatArrayElements,
2460 JNI::ReleaseDoubleArrayElements,
2461 JNI::GetBooleanArrayRegion,
2462 JNI::GetByteArrayRegion,
2463 JNI::GetCharArrayRegion,
2464 JNI::GetShortArrayRegion,
2465 JNI::GetIntArrayRegion,
2466 JNI::GetLongArrayRegion,
2467 JNI::GetFloatArrayRegion,
2468 JNI::GetDoubleArrayRegion,
2469 JNI::SetBooleanArrayRegion,
2470 JNI::SetByteArrayRegion,
2471 JNI::SetCharArrayRegion,
2472 JNI::SetShortArrayRegion,
2473 JNI::SetIntArrayRegion,
2474 JNI::SetLongArrayRegion,
2475 JNI::SetFloatArrayRegion,
2476 JNI::SetDoubleArrayRegion,
2477 JNI::RegisterNatives,
2478 JNI::UnregisterNatives,
2479 JNI::MonitorEnter,
2480 JNI::MonitorExit,
2481 JNI::GetJavaVM,
2482 JNI::GetStringRegion,
2483 JNI::GetStringUTFRegion,
2484 JNI::GetPrimitiveArrayCritical,
2485 JNI::ReleasePrimitiveArrayCritical,
2486 JNI::GetStringCritical,
2487 JNI::ReleaseStringCritical,
2488 JNI::NewWeakGlobalRef,
2489 JNI::DeleteWeakGlobalRef,
2490 JNI::ExceptionCheck,
2491 JNI::NewDirectByteBuffer,
2492 JNI::GetDirectBufferAddress,
2493 JNI::GetDirectBufferCapacity,
2494 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002495};
2496
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002497static const size_t kMonitorsInitial = 32; // Arbitrary.
2498static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2499
2500static const size_t kLocalsInitial = 64; // Arbitrary.
2501static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002502
Elliott Hughes75770752011-08-24 17:52:38 -07002503JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002504 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002505 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002506 local_ref_cookie(IRT_FIRST_SEGMENT),
2507 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes75770752011-08-24 17:52:38 -07002508 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002509 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002510 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002511 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002512 functions = unchecked_functions = &gNativeInterface;
2513 if (check_jni) {
2514 functions = GetCheckJniNativeInterface();
2515 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002516 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2517 // errors will ensue.
2518 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2519 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002520}
2521
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002522JNIEnvExt::~JNIEnvExt() {
2523}
2524
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002525void JNIEnvExt::DumpReferenceTables() {
2526 locals.Dump();
2527 monitors.Dump();
2528}
2529
Carl Shapiroea4dca82011-08-01 13:45:38 -07002530// JNI Invocation interface.
2531
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002532extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2533 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2534 if (args->version < JNI_VERSION_1_2) {
2535 return JNI_EVERSION;
2536 }
2537 Runtime::Options options;
2538 for (int i = 0; i < args->nOptions; ++i) {
2539 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002540 options.push_back(std::make_pair(StringPiece(option->optionString),
2541 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002542 }
2543 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002544 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002545 if (runtime == NULL) {
2546 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002547 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002548 runtime->Start();
2549 *p_env = Thread::Current()->GetJniEnv();
2550 *p_vm = runtime->GetJavaVM();
2551 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002552}
2553
Elliott Hughesf2682d52011-08-15 16:37:04 -07002554extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002555 Runtime* runtime = Runtime::Current();
2556 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002557 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002558 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002559 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002560 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002561 }
2562 return JNI_OK;
2563}
2564
2565// Historically unsupported.
2566extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2567 return JNI_ERR;
2568}
2569
Elliott Hughescdf53122011-08-19 15:46:09 -07002570class JII {
2571 public:
2572 static jint DestroyJavaVM(JavaVM* vm) {
2573 if (vm == NULL) {
2574 return JNI_ERR;
2575 } else {
2576 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2577 delete raw_vm->runtime;
2578 return JNI_OK;
2579 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002580 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002581
Elliott Hughescdf53122011-08-19 15:46:09 -07002582 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002583 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002584 }
2585
2586 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002587 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002588 }
2589
2590 static jint DetachCurrentThread(JavaVM* vm) {
2591 if (vm == NULL) {
2592 return JNI_ERR;
2593 } else {
2594 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2595 Runtime* runtime = raw_vm->runtime;
2596 runtime->DetachCurrentThread();
2597 return JNI_OK;
2598 }
2599 }
2600
2601 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2602 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2603 return JNI_EVERSION;
2604 }
2605 if (vm == NULL || env == NULL) {
2606 return JNI_ERR;
2607 }
2608 Thread* thread = Thread::Current();
2609 if (thread == NULL) {
2610 *env = NULL;
2611 return JNI_EDETACHED;
2612 }
2613 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002614 return JNI_OK;
2615 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002616};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002617
Elliott Hughesa2501992011-08-26 19:39:54 -07002618const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002619 NULL, // reserved0
2620 NULL, // reserved1
2621 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002622 JII::DestroyJavaVM,
2623 JII::AttachCurrentThread,
2624 JII::DetachCurrentThread,
2625 JII::GetEnv,
2626 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002627};
2628
Elliott Hughesbbd76712011-08-17 10:25:24 -07002629static const size_t kPinTableInitialSize = 16;
2630static const size_t kPinTableMaxSize = 1024;
2631
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002632static const size_t kGlobalsInitial = 512; // Arbitrary.
2633static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2634
2635static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2636static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2637
Elliott Hughesa0957642011-09-02 14:27:33 -07002638JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002639 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002640 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002641 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002642 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002643 verbose_jni(options->IsVerbose("jni")),
2644 log_third_party_jni(options->IsVerbose("third-party-jni")),
2645 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002646 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002647 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002648 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002649 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002650 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002651 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002652 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002653 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002654 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002655 functions = unchecked_functions = &gInvokeInterface;
2656 if (check_jni) {
2657 functions = GetCheckJniInvokeInterface();
2658 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002659}
2660
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002661JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002662 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002663}
2664
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002665void JavaVMExt::DumpReferenceTables() {
2666 {
2667 MutexLock mu(globals_lock);
2668 globals.Dump();
2669 }
2670 {
2671 MutexLock mu(weak_globals_lock);
2672 weak_globals.Dump();
2673 }
2674 {
2675 MutexLock mu(pins_lock);
2676 pin_table.Dump();
2677 }
2678}
2679
Elliott Hughes75770752011-08-24 17:52:38 -07002680bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2681 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002682
2683 // See if we've already loaded this library. If we have, and the class loader
2684 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002685 // TODO: for better results we should canonicalize the pathname (or even compare
2686 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002687 SharedLibrary* library;
2688 {
2689 // TODO: move the locking (and more of this logic) into Libraries.
2690 MutexLock mu(libraries_lock);
2691 library = libraries->Get(path);
2692 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002693 if (library != NULL) {
2694 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002695 // The library will be associated with class_loader. The JNI
2696 // spec says we can't load the same library into more than one
2697 // class loader.
2698 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2699 "ClassLoader %p; can't open in ClassLoader %p",
2700 path.c_str(), library->GetClassLoader(), class_loader);
2701 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002702 return false;
2703 }
2704 if (verbose_jni) {
2705 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2706 << "ClassLoader " << class_loader << "]";
2707 }
2708 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002709 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2710 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002711 return false;
2712 }
2713 return true;
2714 }
2715
2716 // Open the shared library. Because we're using a full path, the system
2717 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2718 // resolve this library's dependencies though.)
2719
2720 // Failures here are expected when java.library.path has several entries
2721 // and we have to hunt for the lib.
2722
2723 // The current version of the dynamic linker prints detailed information
2724 // about dlopen() failures. Some things to check if the message is
2725 // cryptic:
2726 // - make sure the library exists on the device
2727 // - verify that the right path is being opened (the debug log message
2728 // above can help with that)
2729 // - check to see if the library is valid (e.g. not zero bytes long)
2730 // - check config/prelink-linux-arm.map to ensure that the library
2731 // is listed and is not being overrun by the previous entry (if
2732 // loading suddenly stops working on a prelinked library, this is
2733 // a good one to check)
2734 // - write a trivial app that calls sleep() then dlopen(), attach
2735 // to it with "strace -p <pid>" while it sleeps, and watch for
2736 // attempts to open nonexistent dependent shared libs
2737
2738 // TODO: automate some of these checks!
2739
2740 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002741 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002742 // the GC to ignore us.
2743 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002744 void* handle = NULL;
2745 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002746 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002747 handle = dlopen(path.c_str(), RTLD_LAZY);
2748 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002749
2750 if (verbose_jni) {
2751 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2752 }
2753
2754 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002755 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002756 return false;
2757 }
2758
2759 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002760 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002761 // TODO: move the locking (and more of this logic) into Libraries.
2762 MutexLock mu(libraries_lock);
2763 library = libraries->Get(path);
2764 if (library != NULL) {
2765 LOG(INFO) << "WOW: we lost a race to add shared library: "
2766 << "\"" << path << "\" ClassLoader=" << class_loader;
2767 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002768 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002769 library = new SharedLibrary(path, handle, class_loader);
2770 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002771 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002772
2773 if (verbose_jni) {
2774 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2775 }
2776
2777 bool result = true;
2778 void* sym = dlsym(handle, "JNI_OnLoad");
2779 if (sym == NULL) {
2780 if (verbose_jni) {
2781 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2782 }
2783 } else {
2784 // Call JNI_OnLoad. We have to override the current class
2785 // loader, which will always be "null" since the stuff at the
2786 // top of the stack is around Runtime.loadLibrary(). (See
2787 // the comments in the JNI FindClass function.)
2788 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2789 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002790 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002791 self->SetClassLoaderOverride(class_loader);
2792
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002793 int version = 0;
2794 {
2795 ScopedThreadStateChange tsc(self, Thread::kNative);
2796 if (verbose_jni) {
2797 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2798 }
2799 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002800 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002801
2802 self->SetClassLoaderOverride(old_class_loader);;
2803
2804 if (version != JNI_VERSION_1_2 &&
2805 version != JNI_VERSION_1_4 &&
2806 version != JNI_VERSION_1_6) {
2807 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2808 << "bad version: " << version;
2809 // It's unwise to call dlclose() here, but we can mark it
2810 // as bad and ensure that future load attempts will fail.
2811 // We don't know how far JNI_OnLoad got, so there could
2812 // be some partially-initialized stuff accessible through
2813 // newly-registered native method calls. We could try to
2814 // unregister them, but that doesn't seem worthwhile.
2815 result = false;
2816 } else {
2817 if (verbose_jni) {
2818 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2819 << " from JNI_OnLoad in \"" << path << "\"]";
2820 }
2821 }
2822 }
2823
2824 library->SetResult(result);
2825 return result;
2826}
2827
2828void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2829 CHECK(m->IsNative());
2830
2831 Class* c = m->GetDeclaringClass();
2832
2833 // If this is a static method, it could be called before the class
2834 // has been initialized.
2835 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002836 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002837 return NULL;
2838 }
2839 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002840 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002841 }
2842
Brian Carlstrom16192862011-09-12 17:50:06 -07002843 std::string detail;
2844 void* native_method;
2845 {
2846 MutexLock mu(libraries_lock);
2847 native_method = libraries->FindNativeMethod(m, detail);
2848 }
2849 // throwing can cause libraries_lock to be reacquired
2850 if (native_method == NULL) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002851 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", "%s", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002852 }
2853 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002854}
2855
Elliott Hughes410c0c82011-09-01 17:58:25 -07002856void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2857 {
2858 MutexLock mu(globals_lock);
2859 globals.VisitRoots(visitor, arg);
2860 }
2861 {
2862 MutexLock mu(pins_lock);
2863 pin_table.VisitRoots(visitor, arg);
2864 }
2865 // The weak_globals table is visited by the GC itself (because it mutates the table).
2866}
2867
Ian Rogersdf20fe02011-07-20 20:34:16 -07002868} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002869
2870std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2871 switch (rhs) {
2872 case JNIInvalidRefType:
2873 os << "JNIInvalidRefType";
2874 return os;
2875 case JNILocalRefType:
2876 os << "JNILocalRefType";
2877 return os;
2878 case JNIGlobalRefType:
2879 os << "JNIGlobalRefType";
2880 return os;
2881 case JNIWeakGlobalRefType:
2882 os << "JNIWeakGlobalRefType";
2883 return os;
2884 }
2885}