blob: 144ec22a202a34d432c77322be6d4e1c0cdcdb9c [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
Elliott Hughesc5f7c912011-08-18 14:00:42 -070029/*
30 * Add a local reference for an object to the current stack frame. When
31 * the native function returns, the reference will be discarded.
32 *
33 * We need to allow the same reference to be added multiple times.
34 *
35 * This will be called on otherwise unreferenced objects. We cannot do
36 * GC allocations here, and it's best if we don't grab a mutex.
37 *
38 * Returns the local reference (currently just the same pointer that was
39 * passed in), or NULL on failure.
40 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070041template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070042T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
43 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
44 Object* obj = const_cast<Object*>(const_obj);
45
Elliott Hughesc5f7c912011-08-18 14:00:42 -070046 if (obj == NULL) {
47 return NULL;
48 }
49
Elliott Hughesbf86d042011-08-31 17:53:14 -070050 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
51 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070052
Ian Rogers5a7a74a2011-09-26 16:32:29 -070053 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070054 IndirectRef ref = locals.Add(cookie, obj);
55 if (ref == NULL) {
56 // TODO: just change Add's DCHECK to CHECK and lose this?
57 locals.Dump();
58 LOG(FATAL) << "Failed adding to JNI local reference table "
59 << "(has " << locals.Capacity() << " entries)";
60 // TODO: dvmDumpThread(dvmThreadSelf(), false);
61 }
62
63#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070064 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070065 size_t entry_count = locals.Capacity();
66 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070067 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070068 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070069 locals.Dump();
70 // TODO: dvmDumpThread(dvmThreadSelf(), false);
71 // dvmAbort();
72 }
73 }
74#endif
75
Elliott Hughesbf86d042011-08-31 17:53:14 -070076 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070077 // Hand out direct pointers to support broken old apps.
78 return reinterpret_cast<T>(obj);
79 }
80
81 return reinterpret_cast<T>(ref);
82}
83
Elliott Hughesbf86d042011-08-31 17:53:14 -070084// For external use.
85template<typename T>
86T Decode(JNIEnv* public_env, jobject obj) {
87 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
88 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
89}
90// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -070091template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070092template Class* Decode<Class*>(JNIEnv*, jobject);
93template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
94template Object* Decode<Object*>(JNIEnv*, jobject);
95template String* Decode<String*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -070096template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
97template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070098template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070099
100namespace {
101
Elliott Hughescdf53122011-08-19 15:46:09 -0700102jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
103 if (obj == NULL) {
104 return NULL;
105 }
Elliott Hughes75770752011-08-24 17:52:38 -0700106 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700107 IndirectReferenceTable& weak_globals = vm->weak_globals;
108 MutexLock mu(vm->weak_globals_lock);
109 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
110 return reinterpret_cast<jweak>(ref);
111}
112
Elliott Hughesbf86d042011-08-31 17:53:14 -0700113// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700114template<typename T>
115T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700116 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700117}
118
Elliott Hughes418d20f2011-09-22 14:00:39 -0700119byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
120 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700121 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700122 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700123 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700124 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
125 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700126 case 'Z':
127 case 'B':
128 case 'C':
129 case 'S':
130 case 'I':
131 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
132 offset += 4;
133 break;
134 case 'F':
135 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
136 offset += 4;
137 break;
138 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700139 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700140 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
141 offset += sizeof(Object*);
142 break;
143 }
144 case 'D':
145 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
146 offset += 8;
147 break;
148 case 'J':
149 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
150 offset += 8;
151 break;
152 }
153 }
154 return arg_array.release();
155}
156
Elliott Hughes418d20f2011-09-22 14:00:39 -0700157byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
158 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700159 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700160 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700161 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700162 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
163 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700164 case 'Z':
165 case 'B':
166 case 'C':
167 case 'S':
168 case 'I':
169 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
170 offset += 4;
171 break;
172 case 'F':
173 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
174 offset += 4;
175 break;
176 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700177 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700178 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
179 offset += sizeof(Object*);
180 break;
181 }
182 case 'D':
183 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
184 offset += 8;
185 break;
186 case 'J':
187 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
188 offset += 8;
189 break;
190 }
191 }
192 return arg_array.release();
193}
194
Elliott Hughes418d20f2011-09-22 14:00:39 -0700195JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
196 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700197 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700198 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700199 return result;
200}
201
Elliott Hughes418d20f2011-09-22 14:00:39 -0700202JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
203 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
204 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700205 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700206 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
207 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700208}
209
Elliott Hughes75770752011-08-24 17:52:38 -0700210Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700211 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700212}
213
Elliott Hughes418d20f2011-09-22 14:00:39 -0700214JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
215 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
216 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700217 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700218 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
219 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700220}
221
Elliott Hughes418d20f2011-09-22 14:00:39 -0700222JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
223 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
224 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700225 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700226 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
227 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700228}
229
Elliott Hughes6b436852011-08-12 10:16:44 -0700230// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
231// separated with slashes but aren't wrapped with "L;" like regular descriptors
232// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
233// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
234// supported names with dots too (such as "a.b.C").
235std::string NormalizeJniClassDescriptor(const char* name) {
236 std::string result;
237 // Add the missing "L;" if necessary.
238 if (name[0] == '[') {
239 result = name;
240 } else {
241 result += 'L';
242 result += name;
243 result += ';';
244 }
245 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700246 if (result.find('.') != std::string::npos) {
247 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
248 << "\"" << name << "\"";
249 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700250 }
251 return result;
252}
253
Elliott Hughes14134a12011-09-30 16:55:51 -0700254void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
255 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
256 ts.Self()->ThrowNewException("Ljava/lang/NoSuchMethodError;",
257 "no %s method \"%s.%s%s\"", kind, class_descriptor.c_str(), name, sig);
258}
259
Elliott Hughescdf53122011-08-19 15:46:09 -0700260jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
261 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700262 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700263 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700264 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700265
266 Method* method = NULL;
267 if (is_static) {
268 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700269 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700270 method = c->FindVirtualMethod(name, sig);
271 if (method == NULL) {
272 // No virtual method matching the signature. Search declared
273 // private methods and constructors.
274 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700275 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700276 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700277
Elliott Hughescdf53122011-08-19 15:46:09 -0700278 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700279 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700280 return NULL;
281 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700282
Elliott Hughes418d20f2011-09-22 14:00:39 -0700283 method->InitJavaFields();
284
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700285 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700286}
287
Elliott Hughescdf53122011-08-19 15:46:09 -0700288jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
289 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700290 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700291 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700292 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700293
294 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700295 Class* field_type;
296 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
297 if (sig[1] != '\0') {
298 // TODO: need to get the appropriate ClassLoader.
299 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
300 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700301 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700302 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700303 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304 if (field_type == NULL) {
305 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700306 DCHECK(ts.Self()->IsExceptionPending());
307 ts.Self()->ClearException();
308 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
309 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
310 "no type \"%s\" found and so no field \"%s\" could be found in class "
311 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 return NULL;
313 }
314 if (is_static) {
315 field = c->FindStaticField(name, field_type);
316 } else {
317 field = c->FindInstanceField(name, field_type);
318 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700319 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700320 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700321 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700322 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700323 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700324 return NULL;
325 }
Elliott Hughes80609252011-09-23 17:24:51 -0700326 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700327 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700328}
329
Elliott Hughes75770752011-08-24 17:52:38 -0700330void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
331 JavaVMExt* vm = ts.Vm();
332 MutexLock mu(vm->pins_lock);
333 vm->pin_table.Add(array);
334}
335
336void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
337 JavaVMExt* vm = ts.Vm();
338 MutexLock mu(vm->pins_lock);
339 vm->pin_table.Remove(array);
340}
341
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700342template<typename JniT, typename ArtT>
343JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
344 CHECK_GE(length, 0); // TODO: ReportJniError
345 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700346 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700347}
348
Elliott Hughes75770752011-08-24 17:52:38 -0700349template <typename ArrayT, typename CArrayT, typename ArtArrayT>
350CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
351 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
352 PinPrimitiveArray(ts, array);
353 if (is_copy != NULL) {
354 *is_copy = JNI_FALSE;
355 }
356 return array->GetData();
357}
358
359template <typename ArrayT>
360void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
361 if (mode != JNI_COMMIT) {
362 Array* array = Decode<Array*>(ts, java_array);
363 UnpinPrimitiveArray(ts, array);
364 }
365}
366
Elliott Hughes814e4032011-08-23 12:07:56 -0700367void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700368 std::string type(PrettyTypeOf(array));
Elliott Hughes814e4032011-08-23 12:07:56 -0700369 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
370 "%s offset=%d length=%d %s.length=%d",
371 type.c_str(), start, length, identifier, array->GetLength());
372}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700373void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
374 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
375 "offset=%d length=%d string.length()=%d", start, length, array_length);
376}
Elliott Hughes814e4032011-08-23 12:07:56 -0700377
378template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700379void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700380 ArrayT* array = Decode<ArrayT*>(ts, java_array);
381 if (start < 0 || length < 0 || start + length > array->GetLength()) {
382 ThrowAIOOBE(ts, array, start, length, "src");
383 } else {
384 JavaT* data = array->GetData();
385 memcpy(buf, data + start, length * sizeof(JavaT));
386 }
387}
388
389template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700390void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700391 ArrayT* array = Decode<ArrayT*>(ts, java_array);
392 if (start < 0 || length < 0 || start + length > array->GetLength()) {
393 ThrowAIOOBE(ts, array, start, length, "dst");
394 } else {
395 JavaT* data = array->GetData();
396 memcpy(data + start, buf, length * sizeof(JavaT));
397 }
398}
399
Elliott Hughes75770752011-08-24 17:52:38 -0700400jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700401 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
402 CHECK(buffer_class.get() != NULL);
403 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
404}
405
Elliott Hughes75770752011-08-24 17:52:38 -0700406jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700407 static jclass buffer_class = InitDirectByteBufferClass(env);
408 return buffer_class;
409}
410
Elliott Hughes75770752011-08-24 17:52:38 -0700411jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
412 if (vm == NULL || p_env == NULL) {
413 return JNI_ERR;
414 }
415
416 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
417 JavaVMAttachArgs args;
418 if (thr_args == NULL) {
419 // Allow the v1.1 calling convention.
420 args.version = JNI_VERSION_1_2;
421 args.name = NULL;
422 args.group = NULL; // TODO: get "main" thread group
423 } else {
424 args.version = in_args->version;
425 args.name = in_args->name;
426 if (in_args->group != NULL) {
427 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
428 args.group = NULL; // TODO: decode in_args->group
429 } else {
430 args.group = NULL; // TODO: get "main" thread group
431 }
432 }
433 CHECK_GE(args.version, JNI_VERSION_1_2);
434
435 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700436 runtime->AttachCurrentThread(args.name, as_daemon);
437 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700438 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700439}
440
Elliott Hughes79082e32011-08-25 12:07:32 -0700441class SharedLibrary {
442 public:
443 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
444 : path_(path),
445 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700446 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700447 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700448 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700449 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700450 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700451 }
452
Elliott Hughes79082e32011-08-25 12:07:32 -0700453 Object* GetClassLoader() {
454 return class_loader_;
455 }
456
457 std::string GetPath() {
458 return path_;
459 }
460
461 /*
462 * Check the result of an earlier call to JNI_OnLoad on this library. If
463 * the call has not yet finished in another thread, wait for it.
464 */
465 bool CheckOnLoadResult(JavaVMExt* vm) {
466 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700467 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700468 // Check this so we don't end up waiting for ourselves. We need
469 // to return "true" so the caller can continue.
470 LOG(INFO) << *self << " recursive attempt to load library "
471 << "\"" << path_ << "\"";
472 return true;
473 }
474
475 MutexLock mu(jni_on_load_lock_);
476 while (jni_on_load_result_ == kPending) {
477 if (vm->verbose_jni) {
478 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
479 << "JNI_OnLoad...]";
480 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700481 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700482 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700483 }
484
485 bool okay = (jni_on_load_result_ == kOkay);
486 if (vm->verbose_jni) {
487 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
488 << (okay ? "succeeded" : "failed") << "]";
489 }
490 return okay;
491 }
492
493 void SetResult(bool result) {
494 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700495 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700496
497 // Broadcast a wakeup to anybody sleeping on the condition variable.
498 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700499 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700500 }
501
502 void* FindSymbol(const std::string& symbol_name) {
503 return dlsym(handle_, symbol_name.c_str());
504 }
505
506 private:
507 enum JNI_OnLoadState {
508 kPending,
509 kFailed,
510 kOkay,
511 };
512
513 // Path to library "/system/lib/libjni.so".
514 std::string path_;
515
516 // The void* returned by dlopen(3).
517 void* handle_;
518
519 // The ClassLoader this library is associated with.
520 Object* class_loader_;
521
522 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700523 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700524 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700525 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700526 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700527 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700528 // Result of earlier JNI_OnLoad call.
529 JNI_OnLoadState jni_on_load_result_;
530};
531
Elliott Hughescdf53122011-08-19 15:46:09 -0700532} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700533
Elliott Hughes79082e32011-08-25 12:07:32 -0700534// This exists mainly to keep implementation details out of the header file.
535class Libraries {
536 public:
537 Libraries() {
538 }
539
540 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700541 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700542 }
543
544 SharedLibrary* Get(const std::string& path) {
545 return libraries_[path];
546 }
547
548 void Put(const std::string& path, SharedLibrary* library) {
549 libraries_[path] = library;
550 }
551
552 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700553 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 std::string jni_short_name(JniShortName(m));
555 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700556 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700557 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
558 SharedLibrary* library = it->second;
559 if (library->GetClassLoader() != declaring_class_loader) {
560 // We only search libraries loaded by the appropriate ClassLoader.
561 continue;
562 }
563 // Try the short name then the long name...
564 void* fn = library->FindSymbol(jni_short_name);
565 if (fn == NULL) {
566 fn = library->FindSymbol(jni_long_name);
567 }
568 if (fn != NULL) {
569 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700570 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700571 << " in \"" << library->GetPath() << "\"]";
572 }
573 return fn;
574 }
575 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700576 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700577 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700578 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700579 return NULL;
580 }
581
582 private:
583 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
584
585 std::map<std::string, SharedLibrary*> libraries_;
586};
587
Elliott Hughes418d20f2011-09-22 14:00:39 -0700588JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
589 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
590 Object* receiver = Decode<Object*>(env, obj);
591 Method* method = DecodeMethod(mid);
592 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
593 return InvokeWithArgArray(env, receiver, method, arg_array.get());
594}
595
Elliott Hughescdf53122011-08-19 15:46:09 -0700596class JNI {
597 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700598
Elliott Hughescdf53122011-08-19 15:46:09 -0700599 static jint GetVersion(JNIEnv* env) {
600 ScopedJniThreadState ts(env);
601 return JNI_VERSION_1_6;
602 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700603
Elliott Hughescdf53122011-08-19 15:46:09 -0700604 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
605 ScopedJniThreadState ts(env);
606 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700607 return NULL;
608 }
609
Elliott Hughescdf53122011-08-19 15:46:09 -0700610 static jclass FindClass(JNIEnv* env, const char* name) {
611 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700612 Runtime* runtime = Runtime::Current();
613 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700615 Class* c = NULL;
616 if (runtime->IsStarted()) {
617 // TODO: need to get the appropriate ClassLoader.
618 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
619 c = class_linker->FindClass(descriptor, cl);
620 } else {
621 c = class_linker->FindSystemClass(descriptor);
622 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700623 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700624 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700625
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
627 ScopedJniThreadState ts(env);
628 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700629 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700630 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700631
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
633 ScopedJniThreadState ts(env);
634 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700635 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700637
Elliott Hughescdf53122011-08-19 15:46:09 -0700638 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
639 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700640 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700641 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700642 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700643
Elliott Hughescdf53122011-08-19 15:46:09 -0700644 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
645 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700646 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700647 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700648 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700649
Elliott Hughes37f7a402011-08-22 18:56:01 -0700650 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
651 ScopedJniThreadState ts(env);
652 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700653 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700654 }
655
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700656 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700658 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700659 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700661
Elliott Hughes37f7a402011-08-22 18:56:01 -0700662 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700664 Class* c1 = Decode<Class*>(ts, java_class1);
665 Class* c2 = Decode<Class*>(ts, java_class2);
666 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700668
Elliott Hughes37f7a402011-08-22 18:56:01 -0700669 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700671 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700672 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700673 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700674 return JNI_TRUE;
675 } else {
676 Object* obj = Decode<Object*>(ts, jobj);
677 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700678 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700679 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700681
Elliott Hughes37f7a402011-08-22 18:56:01 -0700682 static jint Throw(JNIEnv* env, jthrowable java_exception) {
683 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700684 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 if (exception == NULL) {
686 return JNI_ERR;
687 }
688 ts.Self()->SetException(exception);
689 return JNI_OK;
690 }
691
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700692 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700693 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700694 // TODO: check for a pending exception to decide what constructor to call.
695 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
696 if (mid == NULL) {
697 return JNI_ERR;
698 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700699 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
700 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700701 return JNI_ERR;
702 }
703
704 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700705 args[0].l = s.get();
706 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
707 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700708 return JNI_ERR;
709 }
710
Elliott Hughes54e7df12011-09-16 11:47:04 -0700711 LOG(INFO) << "Throwing " << PrettyTypeOf(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700712 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700713 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700714
Elliott Hughes37f7a402011-08-22 18:56:01 -0700715 return JNI_OK;
716 }
717
718 static jboolean ExceptionCheck(JNIEnv* env) {
719 ScopedJniThreadState ts(env);
720 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
721 }
722
723 static void ExceptionClear(JNIEnv* env) {
724 ScopedJniThreadState ts(env);
725 ts.Self()->ClearException();
726 }
727
728 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700729 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700730
731 Thread* self = ts.Self();
732 Throwable* original_exception = self->GetException();
733 self->ClearException();
734
Elliott Hughesbf86d042011-08-31 17:53:14 -0700735 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700736 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
737 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
738 if (mid == NULL) {
739 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700740 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700741 } else {
742 env->CallVoidMethod(exception.get(), mid);
743 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700744 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700745 << " thrown while calling printStackTrace";
746 self->ClearException();
747 }
748 }
749
750 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700752
Elliott Hughescdf53122011-08-19 15:46:09 -0700753 static jthrowable ExceptionOccurred(JNIEnv* env) {
754 ScopedJniThreadState ts(env);
755 Object* exception = ts.Self()->GetException();
756 if (exception == NULL) {
757 return NULL;
758 } else {
759 // TODO: if adding a local reference failing causes the VM to abort
760 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700761 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700762 if (localException == NULL) {
763 // We were unable to add a new local reference, and threw a new
764 // exception. We can't return "exception", because it's not a
765 // local reference. So we have to return NULL, indicating that
766 // there was no exception, even though it's pretty much raining
767 // exceptions in here.
768 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
769 }
770 return localException;
771 }
772 }
773
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 static void FatalError(JNIEnv* env, const char* msg) {
775 ScopedJniThreadState ts(env);
776 LOG(FATAL) << "JNI FatalError called: " << msg;
777 }
778
779 static jint PushLocalFrame(JNIEnv* env, jint cap) {
780 ScopedJniThreadState ts(env);
781 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
782 return JNI_OK;
783 }
784
785 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
786 ScopedJniThreadState ts(env);
787 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
788 return res;
789 }
790
Elliott Hughes72025e52011-08-23 17:50:30 -0700791 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
792 ScopedJniThreadState ts(env);
793 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
794 return 0;
795 }
796
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
798 ScopedJniThreadState ts(env);
799 if (obj == NULL) {
800 return NULL;
801 }
802
Elliott Hughes75770752011-08-24 17:52:38 -0700803 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 IndirectReferenceTable& globals = vm->globals;
805 MutexLock mu(vm->globals_lock);
806 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
807 return reinterpret_cast<jobject>(ref);
808 }
809
810 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
811 ScopedJniThreadState ts(env);
812 if (obj == NULL) {
813 return;
814 }
815
Elliott Hughes75770752011-08-24 17:52:38 -0700816 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 IndirectReferenceTable& globals = vm->globals;
818 MutexLock mu(vm->globals_lock);
819
820 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
821 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
822 << "failed to find entry";
823 }
824 }
825
826 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
827 ScopedJniThreadState ts(env);
828 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
829 }
830
831 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
832 ScopedJniThreadState ts(env);
833 if (obj == NULL) {
834 return;
835 }
836
Elliott Hughes75770752011-08-24 17:52:38 -0700837 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700838 IndirectReferenceTable& weak_globals = vm->weak_globals;
839 MutexLock mu(vm->weak_globals_lock);
840
841 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
842 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
843 << "failed to find entry";
844 }
845 }
846
847 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
848 ScopedJniThreadState ts(env);
849 if (obj == NULL) {
850 return NULL;
851 }
852
853 IndirectReferenceTable& locals = ts.Env()->locals;
854
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700855 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
857 return reinterpret_cast<jobject>(ref);
858 }
859
860 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
861 ScopedJniThreadState ts(env);
862 if (obj == NULL) {
863 return;
864 }
865
866 IndirectReferenceTable& locals = ts.Env()->locals;
867
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700868 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 if (!locals.Remove(cookie, obj)) {
870 // Attempting to delete a local reference that is not in the
871 // topmost local reference frame is a no-op. DeleteLocalRef returns
872 // void and doesn't throw any exceptions, but we should probably
873 // complain about it so the user will notice that things aren't
874 // going quite the way they expect.
875 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
876 << "failed to find entry";
877 }
878 }
879
880 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
881 ScopedJniThreadState ts(env);
882 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
883 ? JNI_TRUE : JNI_FALSE;
884 }
885
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700886 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700888 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700889 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700890 return NULL;
891 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700892 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 }
894
Elliott Hughes72025e52011-08-23 17:50:30 -0700895 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 ScopedJniThreadState ts(env);
897 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700898 va_start(args, mid);
899 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 va_end(args);
901 return result;
902 }
903
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
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 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700910 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700911 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 return local_result;
914 }
915
Elliott Hughes72025e52011-08-23 17:50:30 -0700916 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700918 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700919 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700920 return NULL;
921 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700922 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700923 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 return local_result;
926 }
927
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
929 ScopedJniThreadState ts(env);
930 return FindMethodID(ts, c, name, sig, false);
931 }
932
933 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
934 ScopedJniThreadState ts(env);
935 return FindMethodID(ts, c, name, sig, true);
936 }
937
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 va_list ap;
941 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700942 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700944 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 }
946
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700949 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700950 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 }
952
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700955 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700956 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 }
958
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 va_list ap;
962 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700963 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 va_end(ap);
965 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 }
967
Elliott Hughes72025e52011-08-23 17:50:30 -0700968 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700970 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 }
972
Elliott Hughes72025e52011-08-23 17:50:30 -0700973 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700975 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 va_list ap;
981 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700982 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 va_end(ap);
984 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 }
986
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700989 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 }
991
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700994 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 va_list ap;
1000 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001001 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 va_end(ap);
1003 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 }
1005
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001008 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 }
1010
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001013 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_list ap;
1019 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001020 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 va_end(ap);
1022 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 }
1024
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001027 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001032 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 va_list ap;
1038 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001039 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 va_end(ap);
1041 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 }
1043
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001046 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 }
1048
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001051 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 va_list ap;
1057 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001058 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 va_end(ap);
1060 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001065 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001070 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_list ap;
1076 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001077 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 va_end(ap);
1079 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001084 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001089 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 va_list ap;
1095 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001096 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_end(ap);
1098 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001103 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001108 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 va_list ap;
1114 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001115 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 static void CallVoidMethodV(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 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static void CallVoidMethodA(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 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
1129 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 ScopedJniThreadState ts(env);
1132 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001134 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001135 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 va_end(ap);
1137 return local_result;
1138 }
1139
1140 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001143 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001144 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
1147 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001150 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001151 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 }
1153
1154 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 ScopedJniThreadState ts(env);
1157 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001159 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 va_end(ap);
1161 return result.z;
1162 }
1163
1164 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001167 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
1170 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001173 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 }
1175
1176 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 ScopedJniThreadState ts(env);
1179 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001181 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 va_end(ap);
1183 return result.b;
1184 }
1185
1186 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001189 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
1192 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001195 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
1198 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 ScopedJniThreadState ts(env);
1201 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001203 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 va_end(ap);
1205 return result.c;
1206 }
1207
1208 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001211 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
1214 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001217 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
1220 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 ScopedJniThreadState ts(env);
1223 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001225 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 va_end(ap);
1227 return result.s;
1228 }
1229
1230 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001233 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
1236 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001239 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
Elliott Hughes418d20f2011-09-22 14:00:39 -07001242 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 ScopedJniThreadState ts(env);
1244 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001246 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 va_end(ap);
1248 return result.i;
1249 }
1250
1251 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001254 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 }
1256
1257 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001258 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001260 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 }
1262
1263 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001264 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 ScopedJniThreadState ts(env);
1266 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001268 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 va_end(ap);
1270 return result.j;
1271 }
1272
1273 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001276 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 }
1278
1279 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001280 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001282 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 }
1284
1285 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001286 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 ScopedJniThreadState ts(env);
1288 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001289 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001290 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 va_end(ap);
1292 return result.f;
1293 }
1294
1295 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001298 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
1301 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001302 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001304 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 }
1306
1307 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001308 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 ScopedJniThreadState ts(env);
1310 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001312 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 va_end(ap);
1314 return result.d;
1315 }
1316
1317 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001320 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
1323 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001324 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001326 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
1329 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001330 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 ScopedJniThreadState ts(env);
1332 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001333 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001334 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 va_end(ap);
1336 }
1337
1338 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001339 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001341 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
1344 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001347 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001350 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 ScopedJniThreadState ts(env);
1352 return FindFieldID(ts, c, name, sig, false);
1353 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001354
1355
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001356 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 ScopedJniThreadState ts(env);
1358 return FindFieldID(ts, c, name, sig, true);
1359 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001360
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001361 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001363 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001364 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001365 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001367
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001368 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001370 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001371 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001374 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001376 Object* o = Decode<Object*>(ts, java_object);
1377 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001378 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001379 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 }
1381
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001382 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001384 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001385 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389#define GET_PRIMITIVE_FIELD(fn, instance) \
1390 ScopedJniThreadState ts(env); \
1391 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001392 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001393 return f->fn(o)
1394
1395#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1396 ScopedJniThreadState ts(env); \
1397 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001398 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399 f->fn(o, value)
1400
1401 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1402 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 }
1404
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001405 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1406 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 }
1408
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001409 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1410 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 }
1412
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001413 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1414 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
1416
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001417 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1418 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001421 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1422 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001425 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1426 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 }
1428
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001429 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1430 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
1432
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001433 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1434 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
1436
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001437 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1438 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 }
1440
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001441 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1442 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
1444
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001445 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1446 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 }
1448
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001449 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1450 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 }
1452
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001453 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1454 GET_PRIMITIVE_FIELD(GetLong, NULL);
1455 }
1456
1457 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1458 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1459 }
1460
1461 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1462 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1463 }
1464
1465 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1466 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1467 }
1468
1469 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1470 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1471 }
1472
1473 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1474 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1475 }
1476
1477 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1478 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1479 }
1480
1481 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1482 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1483 }
1484
1485 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1486 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1487 }
1488
1489 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1490 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1491 }
1492
1493 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1494 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1495 }
1496
1497 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1498 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1499 }
1500
1501 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1502 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1503 }
1504
1505 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1506 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1507 }
1508
1509 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1510 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1511 }
1512
1513 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1514 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1515 }
1516
1517 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1518 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1519 }
1520
1521 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1522 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1523 }
1524
1525 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1526 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 }
1528
Elliott Hughes418d20f2011-09-22 14:00:39 -07001529 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 ScopedJniThreadState ts(env);
1531 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001532 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001533 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001534 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 va_end(ap);
1536 return local_result;
1537 }
1538
Elliott Hughes418d20f2011-09-22 14:00:39 -07001539 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001541 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001542 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes418d20f2011-09-22 14:00:39 -07001545 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001547 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001548 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 }
1550
Elliott Hughes418d20f2011-09-22 14:00:39 -07001551 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 ScopedJniThreadState ts(env);
1553 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001554 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001555 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 va_end(ap);
1557 return result.z;
1558 }
1559
Elliott Hughes418d20f2011-09-22 14:00:39 -07001560 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001562 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes418d20f2011-09-22 14:00:39 -07001565 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001567 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 }
1569
Elliott Hughes72025e52011-08-23 17:50:30 -07001570 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 ScopedJniThreadState ts(env);
1572 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001573 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001574 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 va_end(ap);
1576 return result.b;
1577 }
1578
Elliott Hughes418d20f2011-09-22 14:00:39 -07001579 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001581 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes418d20f2011-09-22 14:00:39 -07001584 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001586 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
Elliott Hughes72025e52011-08-23 17:50:30 -07001589 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 ScopedJniThreadState ts(env);
1591 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001592 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001593 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 va_end(ap);
1595 return result.c;
1596 }
1597
Elliott Hughes418d20f2011-09-22 14:00:39 -07001598 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001600 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes418d20f2011-09-22 14:00:39 -07001603 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001605 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes72025e52011-08-23 17:50:30 -07001608 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 ScopedJniThreadState ts(env);
1610 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001611 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001612 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 va_end(ap);
1614 return result.s;
1615 }
1616
Elliott Hughes418d20f2011-09-22 14:00:39 -07001617 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001619 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
Elliott Hughes418d20f2011-09-22 14:00:39 -07001622 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001624 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 }
1626
Elliott Hughes72025e52011-08-23 17:50:30 -07001627 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 ScopedJniThreadState ts(env);
1629 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001630 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001631 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 va_end(ap);
1633 return result.i;
1634 }
1635
Elliott Hughes418d20f2011-09-22 14:00:39 -07001636 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001638 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes418d20f2011-09-22 14:00:39 -07001641 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001643 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
1645
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 ScopedJniThreadState ts(env);
1648 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001649 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001650 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 va_end(ap);
1652 return result.j;
1653 }
1654
Elliott Hughes418d20f2011-09-22 14:00:39 -07001655 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001657 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 }
1659
Elliott Hughes418d20f2011-09-22 14:00:39 -07001660 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001662 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 }
1664
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 ScopedJniThreadState ts(env);
1667 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001669 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 va_end(ap);
1671 return result.f;
1672 }
1673
Elliott Hughes418d20f2011-09-22 14:00:39 -07001674 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001676 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes418d20f2011-09-22 14:00:39 -07001679 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001681 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 }
1683
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 ScopedJniThreadState ts(env);
1686 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001688 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 va_end(ap);
1690 return result.d;
1691 }
1692
Elliott Hughes418d20f2011-09-22 14:00:39 -07001693 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001695 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes418d20f2011-09-22 14:00:39 -07001698 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001700 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 }
1702
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 ScopedJniThreadState ts(env);
1705 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001706 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001707 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 va_end(ap);
1709 }
1710
Elliott Hughes418d20f2011-09-22 14:00:39 -07001711 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001713 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 }
1715
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001718 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes814e4032011-08-23 12:07:56 -07001721 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001723 if (chars == NULL && char_count == 0) {
1724 return NULL;
1725 }
1726 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001727 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
1730 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1731 ScopedJniThreadState ts(env);
1732 if (utf == NULL) {
1733 return NULL;
1734 }
1735 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001736 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes814e4032011-08-23 12:07:56 -07001739 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1740 ScopedJniThreadState ts(env);
1741 return Decode<String*>(ts, java_string)->GetLength();
1742 }
1743
1744 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1745 ScopedJniThreadState ts(env);
1746 return Decode<String*>(ts, java_string)->GetUtfLength();
1747 }
1748
Elliott Hughesb465ab02011-08-24 11:21:21 -07001749 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001751 String* s = Decode<String*>(ts, java_string);
1752 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1753 ThrowSIOOBE(ts, start, length, s->GetLength());
1754 } else {
1755 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1756 memcpy(buf, chars + start, length * sizeof(jchar));
1757 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001758 }
1759
Elliott Hughesb465ab02011-08-24 11:21:21 -07001760 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001761 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001762 String* s = Decode<String*>(ts, java_string);
1763 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1764 ThrowSIOOBE(ts, start, length, s->GetLength());
1765 } else {
1766 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1767 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1768 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001769 }
1770
Elliott Hughes75770752011-08-24 17:52:38 -07001771 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001772 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001773 String* s = Decode<String*>(ts, java_string);
1774 const CharArray* chars = s->GetCharArray();
1775 PinPrimitiveArray(ts, chars);
1776 if (is_copy != NULL) {
1777 *is_copy = JNI_FALSE;
1778 }
1779 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001780 }
1781
Elliott Hughes75770752011-08-24 17:52:38 -07001782 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001783 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001784 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
Elliott Hughes75770752011-08-24 17:52:38 -07001787 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001789 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 }
1791
Elliott Hughes75770752011-08-24 17:52:38 -07001792 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001794 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 }
1796
Elliott Hughes75770752011-08-24 17:52:38 -07001797 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001798 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001799 if (java_string == NULL) {
1800 return NULL;
1801 }
1802 if (is_copy != NULL) {
1803 *is_copy = JNI_TRUE;
1804 }
1805 String* s = Decode<String*>(ts, java_string);
1806 size_t byte_count = s->GetUtfLength();
1807 char* bytes = new char[byte_count + 1];
1808 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001809 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001810 return NULL;
1811 }
1812 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1813 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1814 bytes[byte_count] = '\0';
1815 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001816 }
1817
Elliott Hughes75770752011-08-24 17:52:38 -07001818 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001819 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001820 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001821 }
1822
Elliott Hughesbd935992011-08-22 11:59:34 -07001823 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001825 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001826 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001827 Array* array = obj->AsArray();
1828 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
Elliott Hughes814e4032011-08-23 12:07:56 -07001831 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001833 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001834 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
1837 static void SetObjectArrayElement(JNIEnv* env,
1838 jobjectArray java_array, jsize index, jobject java_value) {
1839 ScopedJniThreadState ts(env);
1840 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1841 Object* value = Decode<Object*>(ts, java_value);
1842 array->Set(index, value);
1843 }
1844
1845 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1846 ScopedJniThreadState ts(env);
1847 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1848 }
1849
1850 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1851 ScopedJniThreadState ts(env);
1852 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1853 }
1854
1855 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1856 ScopedJniThreadState ts(env);
1857 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1858 }
1859
1860 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1861 ScopedJniThreadState ts(env);
1862 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1863 }
1864
1865 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1866 ScopedJniThreadState ts(env);
1867 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1868 }
1869
1870 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1871 ScopedJniThreadState ts(env);
1872 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1873 }
1874
1875 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1876 ScopedJniThreadState ts(env);
1877 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1878 }
1879
1880 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1881 ScopedJniThreadState ts(env);
1882 CHECK_GE(length, 0); // TODO: ReportJniError
1883
1884 // Compute the array class corresponding to the given element class.
1885 Class* element_class = Decode<Class*>(ts, element_jclass);
1886 std::string descriptor;
1887 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001888 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001889
1890 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001891 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1892 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 return NULL;
1894 }
1895
Elliott Hughes75770752011-08-24 17:52:38 -07001896 // Allocate and initialize if necessary.
1897 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001899 if (initial_element != NULL) {
1900 Object* initial_object = Decode<Object*>(ts, initial_element);
1901 for (jsize i = 0; i < length; ++i) {
1902 result->Set(i, initial_object);
1903 }
1904 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001905 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 }
1907
1908 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1909 ScopedJniThreadState ts(env);
1910 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1911 }
1912
Elliott Hughes75770752011-08-24 17:52:38 -07001913 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001914 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001915 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001916 }
1917
Elliott Hughes75770752011-08-24 17:52:38 -07001918 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001919 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001920 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001921 }
1922
Elliott Hughes75770752011-08-24 17:52:38 -07001923 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001924 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001925 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 }
1927
Elliott Hughes75770752011-08-24 17:52:38 -07001928 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001929 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001930 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 }
1932
Elliott Hughes75770752011-08-24 17:52:38 -07001933 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001935 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 }
1937
Elliott Hughes75770752011-08-24 17:52:38 -07001938 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001940 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001945 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 }
1947
Elliott Hughes75770752011-08-24 17:52:38 -07001948 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001950 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 }
1952
Elliott Hughes75770752011-08-24 17:52:38 -07001953 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001955 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 }
1957
Elliott Hughes75770752011-08-24 17:52:38 -07001958 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001960 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 }
1962
Elliott Hughes75770752011-08-24 17:52:38 -07001963 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001965 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Elliott Hughes75770752011-08-24 17:52:38 -07001968 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001970 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Elliott Hughes75770752011-08-24 17:52:38 -07001973 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001975 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Elliott Hughes75770752011-08-24 17:52:38 -07001978 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001980 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Elliott Hughes75770752011-08-24 17:52:38 -07001983 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001985 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Elliott Hughes75770752011-08-24 17:52:38 -07001988 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001990 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001995 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes75770752011-08-24 17:52:38 -07001998 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002000 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes814e4032011-08-23 12:07:56 -07002003 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002005 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes814e4032011-08-23 12:07:56 -07002008 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002010 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes814e4032011-08-23 12:07:56 -07002013 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002015 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes814e4032011-08-23 12:07:56 -07002018 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002020 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes814e4032011-08-23 12:07:56 -07002023 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002025 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes814e4032011-08-23 12:07:56 -07002028 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002030 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes814e4032011-08-23 12:07:56 -07002033 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002035 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes814e4032011-08-23 12:07:56 -07002038 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002040 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes814e4032011-08-23 12:07:56 -07002043 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002045 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002070 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes5174fe62011-08-23 15:12:35 -07002083 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002085 Class* c = Decode<Class*>(ts, java_class);
2086
Elliott Hughes5174fe62011-08-23 15:12:35 -07002087 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 const char* name = methods[i].name;
2089 const char* sig = methods[i].signature;
2090
2091 if (*sig == '!') {
2092 // TODO: fast jni. it's too noisy to log all these.
2093 ++sig;
2094 }
2095
Elliott Hughes5174fe62011-08-23 15:12:35 -07002096 Method* m = c->FindDirectMethod(name, sig);
2097 if (m == NULL) {
2098 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002100 if (m == NULL) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002101 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002103 } else if (!m->IsNative()) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002104 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 return JNI_ERR;
2106 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002107
Elliott Hughes75770752011-08-24 17:52:38 -07002108 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002109 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002110 }
2111
2112 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114 return JNI_OK;
2115 }
2116
Elliott Hughes5174fe62011-08-23 15:12:35 -07002117 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002119 Class* c = Decode<Class*>(ts, java_class);
2120
Elliott Hughes75770752011-08-24 17:52:38 -07002121 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002122 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002123 }
2124
2125 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2126 Method* m = c->GetDirectMethod(i);
2127 if (m->IsNative()) {
2128 m->UnregisterNative();
2129 }
2130 }
2131 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2132 Method* m = c->GetVirtualMethod(i);
2133 if (m->IsNative()) {
2134 m->UnregisterNative();
2135 }
2136 }
2137
2138 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140
Elliott Hughes72025e52011-08-23 17:50:30 -07002141 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002143 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002144 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 }
2146
Elliott Hughes72025e52011-08-23 17:50:30 -07002147 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002149 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002150 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
2153 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2154 ScopedJniThreadState ts(env);
2155 Runtime* runtime = Runtime::Current();
2156 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002157 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 } else {
2159 *vm = NULL;
2160 }
2161 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2162 }
2163
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2165 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002166
2167 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002168 CHECK(address != NULL); // TODO: ReportJniError
2169 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002170
2171 jclass buffer_class = GetDirectByteBufferClass(env);
2172 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2173 if (mid == NULL) {
2174 return NULL;
2175 }
2176
2177 // At the moment, the Java side is limited to 32 bits.
2178 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2179 CHECK_LE(capacity, 0xffffffff);
2180 jint address_arg = reinterpret_cast<jint>(address);
2181 jint capacity_arg = static_cast<jint>(capacity);
2182
2183 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2184 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Elliott Hughesb465ab02011-08-24 11:21:21 -07002187 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002189 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2190 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Elliott Hughesb465ab02011-08-24 11:21:21 -07002193 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002195 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2196 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 }
2198
Elliott Hughesb465ab02011-08-24 11:21:21 -07002199 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002201
Elliott Hughes75770752011-08-24 17:52:38 -07002202 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002203
2204 // Do we definitely know what kind of reference this is?
2205 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2206 IndirectRefKind kind = GetIndirectRefKind(ref);
2207 switch (kind) {
2208 case kLocal:
2209 return JNILocalRefType;
2210 case kGlobal:
2211 return JNIGlobalRefType;
2212 case kWeakGlobal:
2213 return JNIWeakGlobalRefType;
2214 case kSirtOrInvalid:
2215 // Is it in a stack IRT?
2216 if (ts.Self()->SirtContains(java_object)) {
2217 return JNILocalRefType;
2218 }
2219
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002220 if (!ts.Env()->work_around_app_jni_bugs) {
2221 return JNIInvalidRefType;
2222 }
2223
Elliott Hughesb465ab02011-08-24 11:21:21 -07002224 // If we're handing out direct pointers, check whether it's a direct pointer
2225 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002226 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002227 if (ts.Env()->locals.Contains(java_object)) {
2228 return JNILocalRefType;
2229 }
2230 }
2231
2232 return JNIInvalidRefType;
2233 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002236
Elliott Hughesa2501992011-08-26 19:39:54 -07002237const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002238 NULL, // reserved0.
2239 NULL, // reserved1.
2240 NULL, // reserved2.
2241 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 JNI::GetVersion,
2243 JNI::DefineClass,
2244 JNI::FindClass,
2245 JNI::FromReflectedMethod,
2246 JNI::FromReflectedField,
2247 JNI::ToReflectedMethod,
2248 JNI::GetSuperclass,
2249 JNI::IsAssignableFrom,
2250 JNI::ToReflectedField,
2251 JNI::Throw,
2252 JNI::ThrowNew,
2253 JNI::ExceptionOccurred,
2254 JNI::ExceptionDescribe,
2255 JNI::ExceptionClear,
2256 JNI::FatalError,
2257 JNI::PushLocalFrame,
2258 JNI::PopLocalFrame,
2259 JNI::NewGlobalRef,
2260 JNI::DeleteGlobalRef,
2261 JNI::DeleteLocalRef,
2262 JNI::IsSameObject,
2263 JNI::NewLocalRef,
2264 JNI::EnsureLocalCapacity,
2265 JNI::AllocObject,
2266 JNI::NewObject,
2267 JNI::NewObjectV,
2268 JNI::NewObjectA,
2269 JNI::GetObjectClass,
2270 JNI::IsInstanceOf,
2271 JNI::GetMethodID,
2272 JNI::CallObjectMethod,
2273 JNI::CallObjectMethodV,
2274 JNI::CallObjectMethodA,
2275 JNI::CallBooleanMethod,
2276 JNI::CallBooleanMethodV,
2277 JNI::CallBooleanMethodA,
2278 JNI::CallByteMethod,
2279 JNI::CallByteMethodV,
2280 JNI::CallByteMethodA,
2281 JNI::CallCharMethod,
2282 JNI::CallCharMethodV,
2283 JNI::CallCharMethodA,
2284 JNI::CallShortMethod,
2285 JNI::CallShortMethodV,
2286 JNI::CallShortMethodA,
2287 JNI::CallIntMethod,
2288 JNI::CallIntMethodV,
2289 JNI::CallIntMethodA,
2290 JNI::CallLongMethod,
2291 JNI::CallLongMethodV,
2292 JNI::CallLongMethodA,
2293 JNI::CallFloatMethod,
2294 JNI::CallFloatMethodV,
2295 JNI::CallFloatMethodA,
2296 JNI::CallDoubleMethod,
2297 JNI::CallDoubleMethodV,
2298 JNI::CallDoubleMethodA,
2299 JNI::CallVoidMethod,
2300 JNI::CallVoidMethodV,
2301 JNI::CallVoidMethodA,
2302 JNI::CallNonvirtualObjectMethod,
2303 JNI::CallNonvirtualObjectMethodV,
2304 JNI::CallNonvirtualObjectMethodA,
2305 JNI::CallNonvirtualBooleanMethod,
2306 JNI::CallNonvirtualBooleanMethodV,
2307 JNI::CallNonvirtualBooleanMethodA,
2308 JNI::CallNonvirtualByteMethod,
2309 JNI::CallNonvirtualByteMethodV,
2310 JNI::CallNonvirtualByteMethodA,
2311 JNI::CallNonvirtualCharMethod,
2312 JNI::CallNonvirtualCharMethodV,
2313 JNI::CallNonvirtualCharMethodA,
2314 JNI::CallNonvirtualShortMethod,
2315 JNI::CallNonvirtualShortMethodV,
2316 JNI::CallNonvirtualShortMethodA,
2317 JNI::CallNonvirtualIntMethod,
2318 JNI::CallNonvirtualIntMethodV,
2319 JNI::CallNonvirtualIntMethodA,
2320 JNI::CallNonvirtualLongMethod,
2321 JNI::CallNonvirtualLongMethodV,
2322 JNI::CallNonvirtualLongMethodA,
2323 JNI::CallNonvirtualFloatMethod,
2324 JNI::CallNonvirtualFloatMethodV,
2325 JNI::CallNonvirtualFloatMethodA,
2326 JNI::CallNonvirtualDoubleMethod,
2327 JNI::CallNonvirtualDoubleMethodV,
2328 JNI::CallNonvirtualDoubleMethodA,
2329 JNI::CallNonvirtualVoidMethod,
2330 JNI::CallNonvirtualVoidMethodV,
2331 JNI::CallNonvirtualVoidMethodA,
2332 JNI::GetFieldID,
2333 JNI::GetObjectField,
2334 JNI::GetBooleanField,
2335 JNI::GetByteField,
2336 JNI::GetCharField,
2337 JNI::GetShortField,
2338 JNI::GetIntField,
2339 JNI::GetLongField,
2340 JNI::GetFloatField,
2341 JNI::GetDoubleField,
2342 JNI::SetObjectField,
2343 JNI::SetBooleanField,
2344 JNI::SetByteField,
2345 JNI::SetCharField,
2346 JNI::SetShortField,
2347 JNI::SetIntField,
2348 JNI::SetLongField,
2349 JNI::SetFloatField,
2350 JNI::SetDoubleField,
2351 JNI::GetStaticMethodID,
2352 JNI::CallStaticObjectMethod,
2353 JNI::CallStaticObjectMethodV,
2354 JNI::CallStaticObjectMethodA,
2355 JNI::CallStaticBooleanMethod,
2356 JNI::CallStaticBooleanMethodV,
2357 JNI::CallStaticBooleanMethodA,
2358 JNI::CallStaticByteMethod,
2359 JNI::CallStaticByteMethodV,
2360 JNI::CallStaticByteMethodA,
2361 JNI::CallStaticCharMethod,
2362 JNI::CallStaticCharMethodV,
2363 JNI::CallStaticCharMethodA,
2364 JNI::CallStaticShortMethod,
2365 JNI::CallStaticShortMethodV,
2366 JNI::CallStaticShortMethodA,
2367 JNI::CallStaticIntMethod,
2368 JNI::CallStaticIntMethodV,
2369 JNI::CallStaticIntMethodA,
2370 JNI::CallStaticLongMethod,
2371 JNI::CallStaticLongMethodV,
2372 JNI::CallStaticLongMethodA,
2373 JNI::CallStaticFloatMethod,
2374 JNI::CallStaticFloatMethodV,
2375 JNI::CallStaticFloatMethodA,
2376 JNI::CallStaticDoubleMethod,
2377 JNI::CallStaticDoubleMethodV,
2378 JNI::CallStaticDoubleMethodA,
2379 JNI::CallStaticVoidMethod,
2380 JNI::CallStaticVoidMethodV,
2381 JNI::CallStaticVoidMethodA,
2382 JNI::GetStaticFieldID,
2383 JNI::GetStaticObjectField,
2384 JNI::GetStaticBooleanField,
2385 JNI::GetStaticByteField,
2386 JNI::GetStaticCharField,
2387 JNI::GetStaticShortField,
2388 JNI::GetStaticIntField,
2389 JNI::GetStaticLongField,
2390 JNI::GetStaticFloatField,
2391 JNI::GetStaticDoubleField,
2392 JNI::SetStaticObjectField,
2393 JNI::SetStaticBooleanField,
2394 JNI::SetStaticByteField,
2395 JNI::SetStaticCharField,
2396 JNI::SetStaticShortField,
2397 JNI::SetStaticIntField,
2398 JNI::SetStaticLongField,
2399 JNI::SetStaticFloatField,
2400 JNI::SetStaticDoubleField,
2401 JNI::NewString,
2402 JNI::GetStringLength,
2403 JNI::GetStringChars,
2404 JNI::ReleaseStringChars,
2405 JNI::NewStringUTF,
2406 JNI::GetStringUTFLength,
2407 JNI::GetStringUTFChars,
2408 JNI::ReleaseStringUTFChars,
2409 JNI::GetArrayLength,
2410 JNI::NewObjectArray,
2411 JNI::GetObjectArrayElement,
2412 JNI::SetObjectArrayElement,
2413 JNI::NewBooleanArray,
2414 JNI::NewByteArray,
2415 JNI::NewCharArray,
2416 JNI::NewShortArray,
2417 JNI::NewIntArray,
2418 JNI::NewLongArray,
2419 JNI::NewFloatArray,
2420 JNI::NewDoubleArray,
2421 JNI::GetBooleanArrayElements,
2422 JNI::GetByteArrayElements,
2423 JNI::GetCharArrayElements,
2424 JNI::GetShortArrayElements,
2425 JNI::GetIntArrayElements,
2426 JNI::GetLongArrayElements,
2427 JNI::GetFloatArrayElements,
2428 JNI::GetDoubleArrayElements,
2429 JNI::ReleaseBooleanArrayElements,
2430 JNI::ReleaseByteArrayElements,
2431 JNI::ReleaseCharArrayElements,
2432 JNI::ReleaseShortArrayElements,
2433 JNI::ReleaseIntArrayElements,
2434 JNI::ReleaseLongArrayElements,
2435 JNI::ReleaseFloatArrayElements,
2436 JNI::ReleaseDoubleArrayElements,
2437 JNI::GetBooleanArrayRegion,
2438 JNI::GetByteArrayRegion,
2439 JNI::GetCharArrayRegion,
2440 JNI::GetShortArrayRegion,
2441 JNI::GetIntArrayRegion,
2442 JNI::GetLongArrayRegion,
2443 JNI::GetFloatArrayRegion,
2444 JNI::GetDoubleArrayRegion,
2445 JNI::SetBooleanArrayRegion,
2446 JNI::SetByteArrayRegion,
2447 JNI::SetCharArrayRegion,
2448 JNI::SetShortArrayRegion,
2449 JNI::SetIntArrayRegion,
2450 JNI::SetLongArrayRegion,
2451 JNI::SetFloatArrayRegion,
2452 JNI::SetDoubleArrayRegion,
2453 JNI::RegisterNatives,
2454 JNI::UnregisterNatives,
2455 JNI::MonitorEnter,
2456 JNI::MonitorExit,
2457 JNI::GetJavaVM,
2458 JNI::GetStringRegion,
2459 JNI::GetStringUTFRegion,
2460 JNI::GetPrimitiveArrayCritical,
2461 JNI::ReleasePrimitiveArrayCritical,
2462 JNI::GetStringCritical,
2463 JNI::ReleaseStringCritical,
2464 JNI::NewWeakGlobalRef,
2465 JNI::DeleteWeakGlobalRef,
2466 JNI::ExceptionCheck,
2467 JNI::NewDirectByteBuffer,
2468 JNI::GetDirectBufferAddress,
2469 JNI::GetDirectBufferCapacity,
2470 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002471};
2472
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002473static const size_t kMonitorsInitial = 32; // Arbitrary.
2474static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2475
2476static const size_t kLocalsInitial = 64; // Arbitrary.
2477static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002478
Elliott Hughes75770752011-08-24 17:52:38 -07002479JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002480 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002481 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002482 local_ref_cookie(IRT_FIRST_SEGMENT),
2483 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes75770752011-08-24 17:52:38 -07002484 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002485 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002486 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002487 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002488 functions = unchecked_functions = &gNativeInterface;
2489 if (check_jni) {
2490 functions = GetCheckJniNativeInterface();
2491 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002492 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2493 // errors will ensue.
2494 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2495 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002496}
2497
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002498JNIEnvExt::~JNIEnvExt() {
2499}
2500
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002501void JNIEnvExt::DumpReferenceTables() {
2502 locals.Dump();
2503 monitors.Dump();
2504}
2505
Carl Shapiroea4dca82011-08-01 13:45:38 -07002506// JNI Invocation interface.
2507
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002508extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2509 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2510 if (args->version < JNI_VERSION_1_2) {
2511 return JNI_EVERSION;
2512 }
2513 Runtime::Options options;
2514 for (int i = 0; i < args->nOptions; ++i) {
2515 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002516 options.push_back(std::make_pair(StringPiece(option->optionString),
2517 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002518 }
2519 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002520 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002521 if (runtime == NULL) {
2522 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002523 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002524 runtime->Start();
2525 *p_env = Thread::Current()->GetJniEnv();
2526 *p_vm = runtime->GetJavaVM();
2527 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002528}
2529
Elliott Hughesf2682d52011-08-15 16:37:04 -07002530extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002531 Runtime* runtime = Runtime::Current();
2532 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002533 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002534 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002535 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002536 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002537 }
2538 return JNI_OK;
2539}
2540
2541// Historically unsupported.
2542extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2543 return JNI_ERR;
2544}
2545
Elliott Hughescdf53122011-08-19 15:46:09 -07002546class JII {
2547 public:
2548 static jint DestroyJavaVM(JavaVM* vm) {
2549 if (vm == NULL) {
2550 return JNI_ERR;
2551 } else {
2552 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2553 delete raw_vm->runtime;
2554 return JNI_OK;
2555 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002556 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002557
Elliott Hughescdf53122011-08-19 15:46:09 -07002558 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002559 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002560 }
2561
2562 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002563 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002564 }
2565
2566 static jint DetachCurrentThread(JavaVM* vm) {
2567 if (vm == NULL) {
2568 return JNI_ERR;
2569 } else {
2570 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2571 Runtime* runtime = raw_vm->runtime;
2572 runtime->DetachCurrentThread();
2573 return JNI_OK;
2574 }
2575 }
2576
2577 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2578 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2579 return JNI_EVERSION;
2580 }
2581 if (vm == NULL || env == NULL) {
2582 return JNI_ERR;
2583 }
2584 Thread* thread = Thread::Current();
2585 if (thread == NULL) {
2586 *env = NULL;
2587 return JNI_EDETACHED;
2588 }
2589 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002590 return JNI_OK;
2591 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002592};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002593
Elliott Hughesa2501992011-08-26 19:39:54 -07002594const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002595 NULL, // reserved0
2596 NULL, // reserved1
2597 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002598 JII::DestroyJavaVM,
2599 JII::AttachCurrentThread,
2600 JII::DetachCurrentThread,
2601 JII::GetEnv,
2602 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002603};
2604
Elliott Hughesbbd76712011-08-17 10:25:24 -07002605static const size_t kPinTableInitialSize = 16;
2606static const size_t kPinTableMaxSize = 1024;
2607
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002608static const size_t kGlobalsInitial = 512; // Arbitrary.
2609static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2610
2611static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2612static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2613
Elliott Hughesa0957642011-09-02 14:27:33 -07002614JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002615 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002616 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002617 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002618 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002619 verbose_jni(options->IsVerbose("jni")),
2620 log_third_party_jni(options->IsVerbose("third-party-jni")),
2621 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002622 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002623 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002624 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002625 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002626 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002627 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002628 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002629 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002630 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002631 functions = unchecked_functions = &gInvokeInterface;
2632 if (check_jni) {
2633 functions = GetCheckJniInvokeInterface();
2634 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002635}
2636
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002637JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002638 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002639}
2640
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002641void JavaVMExt::DumpReferenceTables() {
2642 {
2643 MutexLock mu(globals_lock);
2644 globals.Dump();
2645 }
2646 {
2647 MutexLock mu(weak_globals_lock);
2648 weak_globals.Dump();
2649 }
2650 {
2651 MutexLock mu(pins_lock);
2652 pin_table.Dump();
2653 }
2654}
2655
Elliott Hughes75770752011-08-24 17:52:38 -07002656bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2657 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002658
2659 // See if we've already loaded this library. If we have, and the class loader
2660 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002661 // TODO: for better results we should canonicalize the pathname (or even compare
2662 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002663 SharedLibrary* library;
2664 {
2665 // TODO: move the locking (and more of this logic) into Libraries.
2666 MutexLock mu(libraries_lock);
2667 library = libraries->Get(path);
2668 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002669 if (library != NULL) {
2670 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002671 // The library will be associated with class_loader. The JNI
2672 // spec says we can't load the same library into more than one
2673 // class loader.
2674 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2675 "ClassLoader %p; can't open in ClassLoader %p",
2676 path.c_str(), library->GetClassLoader(), class_loader);
2677 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002678 return false;
2679 }
2680 if (verbose_jni) {
2681 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2682 << "ClassLoader " << class_loader << "]";
2683 }
2684 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002685 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2686 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002687 return false;
2688 }
2689 return true;
2690 }
2691
2692 // Open the shared library. Because we're using a full path, the system
2693 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2694 // resolve this library's dependencies though.)
2695
2696 // Failures here are expected when java.library.path has several entries
2697 // and we have to hunt for the lib.
2698
2699 // The current version of the dynamic linker prints detailed information
2700 // about dlopen() failures. Some things to check if the message is
2701 // cryptic:
2702 // - make sure the library exists on the device
2703 // - verify that the right path is being opened (the debug log message
2704 // above can help with that)
2705 // - check to see if the library is valid (e.g. not zero bytes long)
2706 // - check config/prelink-linux-arm.map to ensure that the library
2707 // is listed and is not being overrun by the previous entry (if
2708 // loading suddenly stops working on a prelinked library, this is
2709 // a good one to check)
2710 // - write a trivial app that calls sleep() then dlopen(), attach
2711 // to it with "strace -p <pid>" while it sleeps, and watch for
2712 // attempts to open nonexistent dependent shared libs
2713
2714 // TODO: automate some of these checks!
2715
2716 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002717 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002718 // the GC to ignore us.
2719 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002720 void* handle = NULL;
2721 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002722 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002723 handle = dlopen(path.c_str(), RTLD_LAZY);
2724 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002725
2726 if (verbose_jni) {
2727 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2728 }
2729
2730 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002731 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002732 return false;
2733 }
2734
2735 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002736 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002737 // TODO: move the locking (and more of this logic) into Libraries.
2738 MutexLock mu(libraries_lock);
2739 library = libraries->Get(path);
2740 if (library != NULL) {
2741 LOG(INFO) << "WOW: we lost a race to add shared library: "
2742 << "\"" << path << "\" ClassLoader=" << class_loader;
2743 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002744 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002745 library = new SharedLibrary(path, handle, class_loader);
2746 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002747 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002748
2749 if (verbose_jni) {
2750 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2751 }
2752
2753 bool result = true;
2754 void* sym = dlsym(handle, "JNI_OnLoad");
2755 if (sym == NULL) {
2756 if (verbose_jni) {
2757 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2758 }
2759 } else {
2760 // Call JNI_OnLoad. We have to override the current class
2761 // loader, which will always be "null" since the stuff at the
2762 // top of the stack is around Runtime.loadLibrary(). (See
2763 // the comments in the JNI FindClass function.)
2764 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2765 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002766 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002767 self->SetClassLoaderOverride(class_loader);
2768
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002769 int version = 0;
2770 {
2771 ScopedThreadStateChange tsc(self, Thread::kNative);
2772 if (verbose_jni) {
2773 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2774 }
2775 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002776 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002777
2778 self->SetClassLoaderOverride(old_class_loader);;
2779
2780 if (version != JNI_VERSION_1_2 &&
2781 version != JNI_VERSION_1_4 &&
2782 version != JNI_VERSION_1_6) {
2783 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2784 << "bad version: " << version;
2785 // It's unwise to call dlclose() here, but we can mark it
2786 // as bad and ensure that future load attempts will fail.
2787 // We don't know how far JNI_OnLoad got, so there could
2788 // be some partially-initialized stuff accessible through
2789 // newly-registered native method calls. We could try to
2790 // unregister them, but that doesn't seem worthwhile.
2791 result = false;
2792 } else {
2793 if (verbose_jni) {
2794 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2795 << " from JNI_OnLoad in \"" << path << "\"]";
2796 }
2797 }
2798 }
2799
2800 library->SetResult(result);
2801 return result;
2802}
2803
2804void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2805 CHECK(m->IsNative());
2806
2807 Class* c = m->GetDeclaringClass();
2808
2809 // If this is a static method, it could be called before the class
2810 // has been initialized.
2811 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002812 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002813 return NULL;
2814 }
2815 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002816 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002817 }
2818
Brian Carlstrom16192862011-09-12 17:50:06 -07002819 std::string detail;
2820 void* native_method;
2821 {
2822 MutexLock mu(libraries_lock);
2823 native_method = libraries->FindNativeMethod(m, detail);
2824 }
2825 // throwing can cause libraries_lock to be reacquired
2826 if (native_method == NULL) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002827 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", "%s", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002828 }
2829 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002830}
2831
Elliott Hughes410c0c82011-09-01 17:58:25 -07002832void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2833 {
2834 MutexLock mu(globals_lock);
2835 globals.VisitRoots(visitor, arg);
2836 }
2837 {
2838 MutexLock mu(pins_lock);
2839 pin_table.VisitRoots(visitor, arg);
2840 }
2841 // The weak_globals table is visited by the GC itself (because it mutates the table).
2842}
2843
Ian Rogersdf20fe02011-07-20 20:34:16 -07002844} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002845
2846std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2847 switch (rhs) {
2848 case JNIInvalidRefType:
2849 os << "JNIInvalidRefType";
2850 return os;
2851 case JNILocalRefType:
2852 os << "JNILocalRefType";
2853 return os;
2854 case JNIGlobalRefType:
2855 os << "JNIGlobalRefType";
2856 return os;
2857 case JNIWeakGlobalRefType:
2858 os << "JNIWeakGlobalRefType";
2859 return os;
2860 }
2861}