blob: 1d46526e734edc4df2ed95e7f014413cac0e2685 [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);
Elliott Hughes418d20f2011-09-22 14:00:39 -070095template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -070096template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -070097template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070098template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -040099template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700100template String* Decode<String*>(JNIEnv*, jobject);
101template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700102
103namespace {
104
Elliott Hughescdf53122011-08-19 15:46:09 -0700105jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
106 if (obj == NULL) {
107 return NULL;
108 }
Elliott Hughes75770752011-08-24 17:52:38 -0700109 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700110 IndirectReferenceTable& weak_globals = vm->weak_globals;
111 MutexLock mu(vm->weak_globals_lock);
112 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
113 return reinterpret_cast<jweak>(ref);
114}
115
Elliott Hughesbf86d042011-08-31 17:53:14 -0700116// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700117template<typename T>
118T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700119 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700120}
121
Elliott Hughes418d20f2011-09-22 14:00:39 -0700122byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
123 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700124 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700125 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700126 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700127 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
128 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700129 case 'Z':
130 case 'B':
131 case 'C':
132 case 'S':
133 case 'I':
134 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
135 offset += 4;
136 break;
137 case 'F':
138 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
139 offset += 4;
140 break;
141 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700142 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700143 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
144 offset += sizeof(Object*);
145 break;
146 }
147 case 'D':
148 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
149 offset += 8;
150 break;
151 case 'J':
152 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
153 offset += 8;
154 break;
155 }
156 }
157 return arg_array.release();
158}
159
Elliott Hughes418d20f2011-09-22 14:00:39 -0700160byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
161 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700162 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700163 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700164 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700165 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
166 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700167 case 'Z':
168 case 'B':
169 case 'C':
170 case 'S':
171 case 'I':
172 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
173 offset += 4;
174 break;
175 case 'F':
176 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
177 offset += 4;
178 break;
179 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700180 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700181 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
182 offset += sizeof(Object*);
183 break;
184 }
185 case 'D':
186 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
187 offset += 8;
188 break;
189 case 'J':
190 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
191 offset += 8;
192 break;
193 }
194 }
195 return arg_array.release();
196}
197
Elliott Hughes418d20f2011-09-22 14:00:39 -0700198JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
199 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700200 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700201 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700202 return result;
203}
204
Elliott Hughes418d20f2011-09-22 14:00:39 -0700205JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
206 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
207 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700208 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700209 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
210 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700211}
212
Elliott Hughes75770752011-08-24 17:52:38 -0700213Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700214 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700215}
216
Elliott Hughes418d20f2011-09-22 14:00:39 -0700217JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
218 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
219 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700220 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700221 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
222 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700223}
224
Elliott Hughes418d20f2011-09-22 14:00:39 -0700225JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
226 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
227 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700228 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700229 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
230 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700231}
232
Elliott Hughes6b436852011-08-12 10:16:44 -0700233// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
234// separated with slashes but aren't wrapped with "L;" like regular descriptors
235// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
236// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
237// supported names with dots too (such as "a.b.C").
238std::string NormalizeJniClassDescriptor(const char* name) {
239 std::string result;
240 // Add the missing "L;" if necessary.
241 if (name[0] == '[') {
242 result = name;
243 } else {
244 result += 'L';
245 result += name;
246 result += ';';
247 }
248 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700249 if (result.find('.') != std::string::npos) {
250 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
251 << "\"" << name << "\"";
252 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700253 }
254 return result;
255}
256
Elliott Hughes14134a12011-09-30 16:55:51 -0700257void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
258 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700259 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes14134a12011-09-30 16:55:51 -0700260 "no %s method \"%s.%s%s\"", kind, class_descriptor.c_str(), name, sig);
261}
262
Elliott Hughescdf53122011-08-19 15:46:09 -0700263jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
264 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700265 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700266 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700267 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700268
269 Method* method = NULL;
270 if (is_static) {
271 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700272 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700273 method = c->FindVirtualMethod(name, sig);
274 if (method == NULL) {
275 // No virtual method matching the signature. Search declared
276 // private methods and constructors.
277 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700278 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700279 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700280
Elliott Hughescdf53122011-08-19 15:46:09 -0700281 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700282 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700283 return NULL;
284 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700285
Elliott Hughes418d20f2011-09-22 14:00:39 -0700286 method->InitJavaFields();
287
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700288 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700289}
290
Elliott Hughescdf53122011-08-19 15:46:09 -0700291jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
292 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700293 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700294 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700295 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700296
297 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700298 Class* field_type;
299 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
300 if (sig[1] != '\0') {
301 // TODO: need to get the appropriate ClassLoader.
302 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
303 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700304 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700305 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700306 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700307 if (field_type == NULL) {
308 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700309 DCHECK(ts.Self()->IsExceptionPending());
310 ts.Self()->ClearException();
311 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700312 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700313 "no type \"%s\" found and so no field \"%s\" could be found in class "
314 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700315 return NULL;
316 }
317 if (is_static) {
318 field = c->FindStaticField(name, field_type);
319 } else {
320 field = c->FindInstanceField(name, field_type);
321 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700322 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700323 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700324 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700325 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700326 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700327 return NULL;
328 }
Elliott Hughes80609252011-09-23 17:24:51 -0700329 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700330 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700331}
332
Elliott Hughes75770752011-08-24 17:52:38 -0700333void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
334 JavaVMExt* vm = ts.Vm();
335 MutexLock mu(vm->pins_lock);
336 vm->pin_table.Add(array);
337}
338
339void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
340 JavaVMExt* vm = ts.Vm();
341 MutexLock mu(vm->pins_lock);
342 vm->pin_table.Remove(array);
343}
344
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700345template<typename JniT, typename ArtT>
346JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
347 CHECK_GE(length, 0); // TODO: ReportJniError
348 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700349 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700350}
351
Elliott Hughes75770752011-08-24 17:52:38 -0700352template <typename ArrayT, typename CArrayT, typename ArtArrayT>
353CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
354 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
355 PinPrimitiveArray(ts, array);
356 if (is_copy != NULL) {
357 *is_copy = JNI_FALSE;
358 }
359 return array->GetData();
360}
361
362template <typename ArrayT>
363void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
364 if (mode != JNI_COMMIT) {
365 Array* array = Decode<Array*>(ts, java_array);
366 UnpinPrimitiveArray(ts, array);
367 }
368}
369
Elliott Hughes814e4032011-08-23 12:07:56 -0700370void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700371 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700372 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700373 "%s offset=%d length=%d %s.length=%d",
374 type.c_str(), start, length, identifier, array->GetLength());
375}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700376void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700377 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700378 "offset=%d length=%d string.length()=%d", start, length, array_length);
379}
Elliott Hughes814e4032011-08-23 12:07:56 -0700380
381template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700382void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700383 ArrayT* array = Decode<ArrayT*>(ts, java_array);
384 if (start < 0 || length < 0 || start + length > array->GetLength()) {
385 ThrowAIOOBE(ts, array, start, length, "src");
386 } else {
387 JavaT* data = array->GetData();
388 memcpy(buf, data + start, length * sizeof(JavaT));
389 }
390}
391
392template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700393void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700394 ArrayT* array = Decode<ArrayT*>(ts, java_array);
395 if (start < 0 || length < 0 || start + length > array->GetLength()) {
396 ThrowAIOOBE(ts, array, start, length, "dst");
397 } else {
398 JavaT* data = array->GetData();
399 memcpy(data + start, buf, length * sizeof(JavaT));
400 }
401}
402
Elliott Hughes75770752011-08-24 17:52:38 -0700403jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700404 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
405 CHECK(buffer_class.get() != NULL);
406 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
407}
408
Elliott Hughes75770752011-08-24 17:52:38 -0700409jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700410 static jclass buffer_class = InitDirectByteBufferClass(env);
411 return buffer_class;
412}
413
Elliott Hughes75770752011-08-24 17:52:38 -0700414jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
415 if (vm == NULL || p_env == NULL) {
416 return JNI_ERR;
417 }
418
419 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
420 JavaVMAttachArgs args;
421 if (thr_args == NULL) {
422 // Allow the v1.1 calling convention.
423 args.version = JNI_VERSION_1_2;
424 args.name = NULL;
425 args.group = NULL; // TODO: get "main" thread group
426 } else {
427 args.version = in_args->version;
428 args.name = in_args->name;
429 if (in_args->group != NULL) {
430 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
431 args.group = NULL; // TODO: decode in_args->group
432 } else {
433 args.group = NULL; // TODO: get "main" thread group
434 }
435 }
436 CHECK_GE(args.version, JNI_VERSION_1_2);
437
438 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700439 runtime->AttachCurrentThread(args.name, as_daemon);
440 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700441 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700442}
443
Elliott Hughes79082e32011-08-25 12:07:32 -0700444class SharedLibrary {
445 public:
446 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
447 : path_(path),
448 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700449 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700450 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700451 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700452 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700453 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700454 }
455
Elliott Hughes79082e32011-08-25 12:07:32 -0700456 Object* GetClassLoader() {
457 return class_loader_;
458 }
459
460 std::string GetPath() {
461 return path_;
462 }
463
464 /*
465 * Check the result of an earlier call to JNI_OnLoad on this library. If
466 * the call has not yet finished in another thread, wait for it.
467 */
468 bool CheckOnLoadResult(JavaVMExt* vm) {
469 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700470 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700471 // Check this so we don't end up waiting for ourselves. We need
472 // to return "true" so the caller can continue.
473 LOG(INFO) << *self << " recursive attempt to load library "
474 << "\"" << path_ << "\"";
475 return true;
476 }
477
478 MutexLock mu(jni_on_load_lock_);
479 while (jni_on_load_result_ == kPending) {
480 if (vm->verbose_jni) {
481 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
482 << "JNI_OnLoad...]";
483 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700484 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700485 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700486 }
487
488 bool okay = (jni_on_load_result_ == kOkay);
489 if (vm->verbose_jni) {
490 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
491 << (okay ? "succeeded" : "failed") << "]";
492 }
493 return okay;
494 }
495
496 void SetResult(bool result) {
497 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700498 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700499
500 // Broadcast a wakeup to anybody sleeping on the condition variable.
501 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700502 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700503 }
504
505 void* FindSymbol(const std::string& symbol_name) {
506 return dlsym(handle_, symbol_name.c_str());
507 }
508
509 private:
510 enum JNI_OnLoadState {
511 kPending,
512 kFailed,
513 kOkay,
514 };
515
516 // Path to library "/system/lib/libjni.so".
517 std::string path_;
518
519 // The void* returned by dlopen(3).
520 void* handle_;
521
522 // The ClassLoader this library is associated with.
523 Object* class_loader_;
524
525 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700526 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700527 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700528 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700529 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700530 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700531 // Result of earlier JNI_OnLoad call.
532 JNI_OnLoadState jni_on_load_result_;
533};
534
Elliott Hughescdf53122011-08-19 15:46:09 -0700535} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700536
Elliott Hughes79082e32011-08-25 12:07:32 -0700537// This exists mainly to keep implementation details out of the header file.
538class Libraries {
539 public:
540 Libraries() {
541 }
542
543 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700544 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700545 }
546
547 SharedLibrary* Get(const std::string& path) {
548 return libraries_[path];
549 }
550
551 void Put(const std::string& path, SharedLibrary* library) {
552 libraries_[path] = library;
553 }
554
555 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700556 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700557 std::string jni_short_name(JniShortName(m));
558 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700559 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700560 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
561 SharedLibrary* library = it->second;
562 if (library->GetClassLoader() != declaring_class_loader) {
563 // We only search libraries loaded by the appropriate ClassLoader.
564 continue;
565 }
566 // Try the short name then the long name...
567 void* fn = library->FindSymbol(jni_short_name);
568 if (fn == NULL) {
569 fn = library->FindSymbol(jni_long_name);
570 }
571 if (fn != NULL) {
572 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700573 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700574 << " in \"" << library->GetPath() << "\"]";
575 }
576 return fn;
577 }
578 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700579 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700580 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700581 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700582 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 return NULL;
584 }
585
586 private:
587 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
588
589 std::map<std::string, SharedLibrary*> libraries_;
590};
591
Elliott Hughes418d20f2011-09-22 14:00:39 -0700592JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
593 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
594 Object* receiver = Decode<Object*>(env, obj);
595 Method* method = DecodeMethod(mid);
596 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
597 return InvokeWithArgArray(env, receiver, method, arg_array.get());
598}
599
Elliott Hughescdf53122011-08-19 15:46:09 -0700600class JNI {
601 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700602
Elliott Hughescdf53122011-08-19 15:46:09 -0700603 static jint GetVersion(JNIEnv* env) {
604 ScopedJniThreadState ts(env);
605 return JNI_VERSION_1_6;
606 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700607
Elliott Hughescdf53122011-08-19 15:46:09 -0700608 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
609 ScopedJniThreadState ts(env);
610 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700611 return NULL;
612 }
613
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 static jclass FindClass(JNIEnv* env, const char* name) {
615 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700616 Runtime* runtime = Runtime::Current();
617 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700618 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700619 Class* c = NULL;
620 if (runtime->IsStarted()) {
621 // TODO: need to get the appropriate ClassLoader.
622 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
623 c = class_linker->FindClass(descriptor, cl);
624 } else {
625 c = class_linker->FindSystemClass(descriptor);
626 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700627 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700628 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700629
Elliott Hughescdf53122011-08-19 15:46:09 -0700630 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
631 ScopedJniThreadState ts(env);
632 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700633 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700634 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700635
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
637 ScopedJniThreadState ts(env);
638 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700639 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700640 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700641
Elliott Hughescdf53122011-08-19 15:46:09 -0700642 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
643 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700644 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700645 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700646 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700647
Elliott Hughescdf53122011-08-19 15:46:09 -0700648 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
649 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700650 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700651 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700653
Elliott Hughes37f7a402011-08-22 18:56:01 -0700654 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
655 ScopedJniThreadState ts(env);
656 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700657 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700658 }
659
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700660 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700662 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700663 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700664 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700665
Elliott Hughes37f7a402011-08-22 18:56:01 -0700666 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700668 Class* c1 = Decode<Class*>(ts, java_class1);
669 Class* c2 = Decode<Class*>(ts, java_class2);
670 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700672
Elliott Hughes37f7a402011-08-22 18:56:01 -0700673 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700675 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700676 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700677 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 return JNI_TRUE;
679 } else {
680 Object* obj = Decode<Object*>(ts, jobj);
681 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700682 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700683 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700685
Elliott Hughes37f7a402011-08-22 18:56:01 -0700686 static jint Throw(JNIEnv* env, jthrowable java_exception) {
687 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700688 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700689 if (exception == NULL) {
690 return JNI_ERR;
691 }
692 ts.Self()->SetException(exception);
693 return JNI_OK;
694 }
695
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700696 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700697 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700698 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700699 jmethodID mid = ((msg != NULL)
700 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
701 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700702 if (mid == NULL) {
703 return JNI_ERR;
704 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700705 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700706 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700707 return JNI_ERR;
708 }
709
710 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700711 args[0].l = s.get();
712 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
713 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700714 return JNI_ERR;
715 }
716
Elliott Hughes72025e52011-08-23 17:50:30 -0700717 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700718
Elliott Hughes37f7a402011-08-22 18:56:01 -0700719 return JNI_OK;
720 }
721
722 static jboolean ExceptionCheck(JNIEnv* env) {
723 ScopedJniThreadState ts(env);
724 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
725 }
726
727 static void ExceptionClear(JNIEnv* env) {
728 ScopedJniThreadState ts(env);
729 ts.Self()->ClearException();
730 }
731
732 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700733 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700734
735 Thread* self = ts.Self();
736 Throwable* original_exception = self->GetException();
737 self->ClearException();
738
Elliott Hughesbf86d042011-08-31 17:53:14 -0700739 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700740 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
741 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
742 if (mid == NULL) {
743 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700744 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700745 } else {
746 env->CallVoidMethod(exception.get(), mid);
747 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700748 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700749 << " thrown while calling printStackTrace";
750 self->ClearException();
751 }
752 }
753
754 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700756
Elliott Hughescdf53122011-08-19 15:46:09 -0700757 static jthrowable ExceptionOccurred(JNIEnv* env) {
758 ScopedJniThreadState ts(env);
759 Object* exception = ts.Self()->GetException();
760 if (exception == NULL) {
761 return NULL;
762 } else {
763 // TODO: if adding a local reference failing causes the VM to abort
764 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700765 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 if (localException == NULL) {
767 // We were unable to add a new local reference, and threw a new
768 // exception. We can't return "exception", because it's not a
769 // local reference. So we have to return NULL, indicating that
770 // there was no exception, even though it's pretty much raining
771 // exceptions in here.
772 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
773 }
774 return localException;
775 }
776 }
777
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 static void FatalError(JNIEnv* env, const char* msg) {
779 ScopedJniThreadState ts(env);
780 LOG(FATAL) << "JNI FatalError called: " << msg;
781 }
782
783 static jint PushLocalFrame(JNIEnv* env, jint cap) {
784 ScopedJniThreadState ts(env);
785 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
786 return JNI_OK;
787 }
788
789 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
790 ScopedJniThreadState ts(env);
791 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
792 return res;
793 }
794
Elliott Hughes72025e52011-08-23 17:50:30 -0700795 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
796 ScopedJniThreadState ts(env);
797 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
798 return 0;
799 }
800
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
802 ScopedJniThreadState ts(env);
803 if (obj == NULL) {
804 return NULL;
805 }
806
Elliott Hughes75770752011-08-24 17:52:38 -0700807 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700808 IndirectReferenceTable& globals = vm->globals;
809 MutexLock mu(vm->globals_lock);
810 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
811 return reinterpret_cast<jobject>(ref);
812 }
813
814 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
815 ScopedJniThreadState ts(env);
816 if (obj == NULL) {
817 return;
818 }
819
Elliott Hughes75770752011-08-24 17:52:38 -0700820 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 IndirectReferenceTable& globals = vm->globals;
822 MutexLock mu(vm->globals_lock);
823
824 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
825 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
826 << "failed to find entry";
827 }
828 }
829
830 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
831 ScopedJniThreadState ts(env);
832 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
833 }
834
835 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
836 ScopedJniThreadState ts(env);
837 if (obj == NULL) {
838 return;
839 }
840
Elliott Hughes75770752011-08-24 17:52:38 -0700841 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 IndirectReferenceTable& weak_globals = vm->weak_globals;
843 MutexLock mu(vm->weak_globals_lock);
844
845 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
846 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
847 << "failed to find entry";
848 }
849 }
850
851 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
852 ScopedJniThreadState ts(env);
853 if (obj == NULL) {
854 return NULL;
855 }
856
857 IndirectReferenceTable& locals = ts.Env()->locals;
858
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700859 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
861 return reinterpret_cast<jobject>(ref);
862 }
863
864 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
865 ScopedJniThreadState ts(env);
866 if (obj == NULL) {
867 return;
868 }
869
870 IndirectReferenceTable& locals = ts.Env()->locals;
871
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700872 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 if (!locals.Remove(cookie, obj)) {
874 // Attempting to delete a local reference that is not in the
875 // topmost local reference frame is a no-op. DeleteLocalRef returns
876 // void and doesn't throw any exceptions, but we should probably
877 // complain about it so the user will notice that things aren't
878 // going quite the way they expect.
879 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
880 << "failed to find entry";
881 }
882 }
883
884 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
885 ScopedJniThreadState ts(env);
886 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
887 ? JNI_TRUE : JNI_FALSE;
888 }
889
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700890 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700891 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700892 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700893 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700894 return NULL;
895 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700896 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 }
898
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 ScopedJniThreadState ts(env);
901 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700902 va_start(args, mid);
903 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 va_end(args);
905 return result;
906 }
907
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700909 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700910 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700911 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700912 return NULL;
913 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700914 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700915 if (result == NULL) {
916 return NULL;
917 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700918 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700919 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 return local_result;
921 }
922
Elliott Hughes72025e52011-08-23 17:50:30 -0700923 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700925 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700926 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700927 return NULL;
928 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700929 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700930 if (result == NULL) {
931 return NULL;
932 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700933 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 return local_result;
936 }
937
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
939 ScopedJniThreadState ts(env);
940 return FindMethodID(ts, c, name, sig, false);
941 }
942
943 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
944 ScopedJniThreadState ts(env);
945 return FindMethodID(ts, c, name, sig, true);
946 }
947
Elliott Hughes72025e52011-08-23 17:50:30 -0700948 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 va_list ap;
951 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700952 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700954 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 }
956
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700959 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700960 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 }
962
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700965 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700966 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 va_list ap;
972 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700973 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 va_end(ap);
975 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700980 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700985 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 va_list ap;
991 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700992 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_end(ap);
994 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700999 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001004 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 va_list ap;
1010 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001011 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 va_end(ap);
1013 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001018 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001023 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 va_list ap;
1029 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001030 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 va_end(ap);
1032 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001037 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001042 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 va_list ap;
1048 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001049 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 va_end(ap);
1051 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001056 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001061 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 va_list ap;
1067 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001068 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_end(ap);
1070 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001075 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001080 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 va_list ap;
1086 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001087 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 va_end(ap);
1089 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001094 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001099 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 va_list ap;
1105 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001106 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 va_end(ap);
1108 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001113 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001118 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_list ap;
1124 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001125 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001131 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001136 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
1139 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 ScopedJniThreadState ts(env);
1142 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001144 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001145 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 va_end(ap);
1147 return local_result;
1148 }
1149
1150 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001153 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001154 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
1157 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001160 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001161 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
1164 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 ScopedJniThreadState ts(env);
1167 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001169 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 va_end(ap);
1171 return result.z;
1172 }
1173
1174 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001177 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
1180 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001183 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
1186 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
1189 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001191 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 va_end(ap);
1193 return result.b;
1194 }
1195
1196 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001199 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
1202 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001205 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
1208 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 ScopedJniThreadState ts(env);
1211 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001213 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 va_end(ap);
1215 return result.c;
1216 }
1217
1218 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001221 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
1224 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001227 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
1230 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
1233 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001235 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 va_end(ap);
1237 return result.s;
1238 }
1239
1240 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001243 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
1246 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001249 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
Elliott Hughes418d20f2011-09-22 14:00:39 -07001252 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 ScopedJniThreadState ts(env);
1254 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001256 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 va_end(ap);
1258 return result.i;
1259 }
1260
1261 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001264 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
1267 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001270 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 }
1272
1273 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 ScopedJniThreadState ts(env);
1276 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001278 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 va_end(ap);
1280 return result.j;
1281 }
1282
1283 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001286 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
1289 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001292 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
1295 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 ScopedJniThreadState ts(env);
1298 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001300 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 va_end(ap);
1302 return result.f;
1303 }
1304
1305 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001306 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001308 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
1311 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001314 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
1317 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 ScopedJniThreadState ts(env);
1320 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001321 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001322 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 va_end(ap);
1324 return result.d;
1325 }
1326
1327 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001330 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
1333 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001336 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 }
1338
1339 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001340 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 ScopedJniThreadState ts(env);
1342 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001343 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001344 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 va_end(ap);
1346 }
1347
1348 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001349 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001351 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 }
1353
1354 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001355 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001357 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 }
1359
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001360 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 ScopedJniThreadState ts(env);
1362 return FindFieldID(ts, c, name, sig, false);
1363 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001364
1365
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001366 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 ScopedJniThreadState ts(env);
1368 return FindFieldID(ts, c, name, sig, true);
1369 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001370
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001371 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001373 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001374 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001375 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001377
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001378 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001380 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001381 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 }
1383
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001384 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 Object* o = Decode<Object*>(ts, java_object);
1387 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001388 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001395 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399#define GET_PRIMITIVE_FIELD(fn, instance) \
1400 ScopedJniThreadState ts(env); \
1401 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001402 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001403 return f->fn(o)
1404
1405#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1406 ScopedJniThreadState ts(env); \
1407 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001408 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001409 f->fn(o, value)
1410
1411 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1412 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001415 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1416 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 }
1418
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001419 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1420 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001423 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1424 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 }
1426
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001427 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1428 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001431 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1432 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1436 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1440 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1444 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1448 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetLong, NULL);
1465 }
1466
1467 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1469 }
1470
1471 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1473 }
1474
1475 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1476 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1477 }
1478
1479 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1480 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1481 }
1482
1483 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1484 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1485 }
1486
1487 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1488 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1489 }
1490
1491 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1492 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1493 }
1494
1495 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1496 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1497 }
1498
1499 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1500 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1501 }
1502
1503 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1504 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1505 }
1506
1507 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1508 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1509 }
1510
1511 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1512 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1513 }
1514
1515 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1516 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1517 }
1518
1519 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1520 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1521 }
1522
1523 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1524 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1525 }
1526
1527 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1528 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1529 }
1530
1531 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1532 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1533 }
1534
1535 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1536 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Elliott Hughes418d20f2011-09-22 14:00:39 -07001539 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 ScopedJniThreadState ts(env);
1541 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001542 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001543 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001544 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 va_end(ap);
1546 return local_result;
1547 }
1548
Elliott Hughes418d20f2011-09-22 14:00:39 -07001549 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001551 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001552 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes418d20f2011-09-22 14:00:39 -07001555 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001557 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001558 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes418d20f2011-09-22 14:00:39 -07001561 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 ScopedJniThreadState ts(env);
1563 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001564 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001565 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 va_end(ap);
1567 return result.z;
1568 }
1569
Elliott Hughes418d20f2011-09-22 14:00:39 -07001570 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001572 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes418d20f2011-09-22 14:00:39 -07001575 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001577 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
Elliott Hughes72025e52011-08-23 17:50:30 -07001580 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 ScopedJniThreadState ts(env);
1582 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001583 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001584 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 va_end(ap);
1586 return result.b;
1587 }
1588
Elliott Hughes418d20f2011-09-22 14:00:39 -07001589 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001591 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes418d20f2011-09-22 14:00:39 -07001594 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001596 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
Elliott Hughes72025e52011-08-23 17:50:30 -07001599 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 ScopedJniThreadState ts(env);
1601 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001602 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001603 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 va_end(ap);
1605 return result.c;
1606 }
1607
Elliott Hughes418d20f2011-09-22 14:00:39 -07001608 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001610 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes418d20f2011-09-22 14:00:39 -07001613 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001615 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
Elliott Hughes72025e52011-08-23 17:50:30 -07001618 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 ScopedJniThreadState ts(env);
1620 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001621 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001622 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 va_end(ap);
1624 return result.s;
1625 }
1626
Elliott Hughes418d20f2011-09-22 14:00:39 -07001627 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001629 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes418d20f2011-09-22 14:00:39 -07001632 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001634 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
Elliott Hughes72025e52011-08-23 17:50:30 -07001637 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 ScopedJniThreadState ts(env);
1639 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001640 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001641 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 va_end(ap);
1643 return result.i;
1644 }
1645
Elliott Hughes418d20f2011-09-22 14:00:39 -07001646 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001648 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
Elliott Hughes418d20f2011-09-22 14:00:39 -07001651 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001653 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 }
1655
Elliott Hughes72025e52011-08-23 17:50:30 -07001656 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 ScopedJniThreadState ts(env);
1658 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001660 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 va_end(ap);
1662 return result.j;
1663 }
1664
Elliott Hughes418d20f2011-09-22 14:00:39 -07001665 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001667 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 }
1669
Elliott Hughes418d20f2011-09-22 14:00:39 -07001670 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001672 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
Elliott Hughes72025e52011-08-23 17:50:30 -07001675 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 ScopedJniThreadState ts(env);
1677 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001679 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 va_end(ap);
1681 return result.f;
1682 }
1683
Elliott Hughes418d20f2011-09-22 14:00:39 -07001684 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001686 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes418d20f2011-09-22 14:00:39 -07001689 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001691 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 }
1693
Elliott Hughes72025e52011-08-23 17:50:30 -07001694 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 ScopedJniThreadState ts(env);
1696 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001697 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001698 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 va_end(ap);
1700 return result.d;
1701 }
1702
Elliott Hughes418d20f2011-09-22 14:00:39 -07001703 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001705 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 }
1707
Elliott Hughes418d20f2011-09-22 14:00:39 -07001708 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001710 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 }
1712
Elliott Hughes72025e52011-08-23 17:50:30 -07001713 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 ScopedJniThreadState ts(env);
1715 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001716 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001717 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 va_end(ap);
1719 }
1720
Elliott Hughes418d20f2011-09-22 14:00:39 -07001721 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001723 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes418d20f2011-09-22 14:00:39 -07001726 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001728 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes814e4032011-08-23 12:07:56 -07001731 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001733 if (chars == NULL && char_count == 0) {
1734 return NULL;
1735 }
1736 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001737 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
1740 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1741 ScopedJniThreadState ts(env);
1742 if (utf == NULL) {
1743 return NULL;
1744 }
1745 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001746 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes814e4032011-08-23 12:07:56 -07001749 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1750 ScopedJniThreadState ts(env);
1751 return Decode<String*>(ts, java_string)->GetLength();
1752 }
1753
1754 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1755 ScopedJniThreadState ts(env);
1756 return Decode<String*>(ts, java_string)->GetUtfLength();
1757 }
1758
Elliott Hughesb465ab02011-08-24 11:21:21 -07001759 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001761 String* s = Decode<String*>(ts, java_string);
1762 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1763 ThrowSIOOBE(ts, start, length, s->GetLength());
1764 } else {
1765 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1766 memcpy(buf, chars + start, length * sizeof(jchar));
1767 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001768 }
1769
Elliott Hughesb465ab02011-08-24 11:21:21 -07001770 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001771 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001772 String* s = Decode<String*>(ts, java_string);
1773 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1774 ThrowSIOOBE(ts, start, length, s->GetLength());
1775 } else {
1776 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1777 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1778 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001779 }
1780
Elliott Hughes75770752011-08-24 17:52:38 -07001781 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001782 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001783 String* s = Decode<String*>(ts, java_string);
1784 const CharArray* chars = s->GetCharArray();
1785 PinPrimitiveArray(ts, chars);
1786 if (is_copy != NULL) {
1787 *is_copy = JNI_FALSE;
1788 }
1789 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001790 }
1791
Elliott Hughes75770752011-08-24 17:52:38 -07001792 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001793 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001794 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 }
1796
Elliott Hughes75770752011-08-24 17:52:38 -07001797 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001799 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
Elliott Hughes75770752011-08-24 17:52:38 -07001802 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001804 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
Elliott Hughes75770752011-08-24 17:52:38 -07001807 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001808 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001809 if (java_string == NULL) {
1810 return NULL;
1811 }
1812 if (is_copy != NULL) {
1813 *is_copy = JNI_TRUE;
1814 }
1815 String* s = Decode<String*>(ts, java_string);
1816 size_t byte_count = s->GetUtfLength();
1817 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001818 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001819 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1820 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1821 bytes[byte_count] = '\0';
1822 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001823 }
1824
Elliott Hughes75770752011-08-24 17:52:38 -07001825 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001826 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001827 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001828 }
1829
Elliott Hughesbd935992011-08-22 11:59:34 -07001830 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001832 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001833 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001834 Array* array = obj->AsArray();
1835 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes814e4032011-08-23 12:07:56 -07001838 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001840 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001841 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 }
1843
1844 static void SetObjectArrayElement(JNIEnv* env,
1845 jobjectArray java_array, jsize index, jobject java_value) {
1846 ScopedJniThreadState ts(env);
1847 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1848 Object* value = Decode<Object*>(ts, java_value);
1849 array->Set(index, value);
1850 }
1851
1852 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1853 ScopedJniThreadState ts(env);
1854 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1855 }
1856
1857 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1858 ScopedJniThreadState ts(env);
1859 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1860 }
1861
1862 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1863 ScopedJniThreadState ts(env);
1864 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1865 }
1866
1867 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1868 ScopedJniThreadState ts(env);
1869 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1870 }
1871
1872 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1873 ScopedJniThreadState ts(env);
1874 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1875 }
1876
1877 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1878 ScopedJniThreadState ts(env);
1879 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1880 }
1881
1882 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1883 ScopedJniThreadState ts(env);
1884 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1885 }
1886
1887 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1888 ScopedJniThreadState ts(env);
1889 CHECK_GE(length, 0); // TODO: ReportJniError
1890
1891 // Compute the array class corresponding to the given element class.
1892 Class* element_class = Decode<Class*>(ts, element_jclass);
1893 std::string descriptor;
1894 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001895 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001896
1897 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001898 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1899 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001900 return NULL;
1901 }
1902
Elliott Hughes75770752011-08-24 17:52:38 -07001903 // Allocate and initialize if necessary.
1904 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001906 if (initial_element != NULL) {
1907 Object* initial_object = Decode<Object*>(ts, initial_element);
1908 for (jsize i = 0; i < length; ++i) {
1909 result->Set(i, initial_object);
1910 }
1911 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001912 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 }
1914
1915 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1916 ScopedJniThreadState ts(env);
1917 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1918 }
1919
Elliott Hughes75770752011-08-24 17:52:38 -07001920 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001921 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001922 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001923 }
1924
Elliott Hughes75770752011-08-24 17:52:38 -07001925 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001926 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001927 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001928 }
1929
Elliott Hughes75770752011-08-24 17:52:38 -07001930 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001932 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes75770752011-08-24 17:52:38 -07001935 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001937 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 }
1939
Elliott Hughes75770752011-08-24 17:52:38 -07001940 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001942 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
Elliott Hughes75770752011-08-24 17:52:38 -07001945 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001947 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
1949
Elliott Hughes75770752011-08-24 17:52:38 -07001950 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001952 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Elliott Hughes75770752011-08-24 17:52:38 -07001955 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001957 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Elliott Hughes75770752011-08-24 17:52:38 -07001960 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001962 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 }
1964
Elliott Hughes75770752011-08-24 17:52:38 -07001965 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001967 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes75770752011-08-24 17:52:38 -07001970 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001972 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 }
1974
Elliott Hughes75770752011-08-24 17:52:38 -07001975 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001977 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
Elliott Hughes75770752011-08-24 17:52:38 -07001980 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001982 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 }
1984
Elliott Hughes75770752011-08-24 17:52:38 -07001985 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001987 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Elliott Hughes75770752011-08-24 17:52:38 -07001990 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001992 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 }
1994
Elliott Hughes75770752011-08-24 17:52:38 -07001995 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001997 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Elliott Hughes75770752011-08-24 17:52:38 -07002000 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002002 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
Elliott Hughes75770752011-08-24 17:52:38 -07002005 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002007 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Elliott Hughes814e4032011-08-23 12:07:56 -07002010 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002012 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Elliott Hughes814e4032011-08-23 12:07:56 -07002015 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002017 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Elliott Hughes814e4032011-08-23 12:07:56 -07002020 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002022 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes814e4032011-08-23 12:07:56 -07002025 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002027 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes814e4032011-08-23 12:07:56 -07002030 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002032 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
2034
Elliott Hughes814e4032011-08-23 12:07:56 -07002035 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002037 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes814e4032011-08-23 12:07:56 -07002040 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002042 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes814e4032011-08-23 12:07:56 -07002045 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002047 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002052 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002057 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002062 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002067 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Elliott Hughes814e4032011-08-23 12:07:56 -07002070 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002072 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002077 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002082 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002087 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
Elliott Hughes5174fe62011-08-23 15:12:35 -07002090 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002092 Class* c = Decode<Class*>(ts, java_class);
2093
Elliott Hughes5174fe62011-08-23 15:12:35 -07002094 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 const char* name = methods[i].name;
2096 const char* sig = methods[i].signature;
2097
2098 if (*sig == '!') {
2099 // TODO: fast jni. it's too noisy to log all these.
2100 ++sig;
2101 }
2102
Elliott Hughes5174fe62011-08-23 15:12:35 -07002103 Method* m = c->FindDirectMethod(name, sig);
2104 if (m == NULL) {
2105 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002107 if (m == NULL) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002108 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002110 } else if (!m->IsNative()) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002111 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 return JNI_ERR;
2113 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002114
Elliott Hughes75770752011-08-24 17:52:38 -07002115 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002116 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002117 }
2118
2119 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121 return JNI_OK;
2122 }
2123
Elliott Hughes5174fe62011-08-23 15:12:35 -07002124 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002126 Class* c = Decode<Class*>(ts, java_class);
2127
Elliott Hughes75770752011-08-24 17:52:38 -07002128 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002129 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002130 }
2131
2132 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2133 Method* m = c->GetDirectMethod(i);
2134 if (m->IsNative()) {
2135 m->UnregisterNative();
2136 }
2137 }
2138 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2139 Method* m = c->GetVirtualMethod(i);
2140 if (m->IsNative()) {
2141 m->UnregisterNative();
2142 }
2143 }
2144
2145 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes72025e52011-08-23 17:50:30 -07002148 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002150 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002151 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
2153
Elliott Hughes72025e52011-08-23 17:50:30 -07002154 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002156 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002157 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 }
2159
2160 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2161 ScopedJniThreadState ts(env);
2162 Runtime* runtime = Runtime::Current();
2163 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002164 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 } else {
2166 *vm = NULL;
2167 }
2168 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2169 }
2170
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2172 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002173
2174 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002175 CHECK(address != NULL); // TODO: ReportJniError
2176 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002177
2178 jclass buffer_class = GetDirectByteBufferClass(env);
2179 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2180 if (mid == NULL) {
2181 return NULL;
2182 }
2183
2184 // At the moment, the Java side is limited to 32 bits.
2185 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2186 CHECK_LE(capacity, 0xffffffff);
2187 jint address_arg = reinterpret_cast<jint>(address);
2188 jint capacity_arg = static_cast<jint>(capacity);
2189
2190 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2191 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
2193
Elliott Hughesb465ab02011-08-24 11:21:21 -07002194 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002196 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2197 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 }
2199
Elliott Hughesb465ab02011-08-24 11:21:21 -07002200 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002202 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2203 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 }
2205
Elliott Hughesb465ab02011-08-24 11:21:21 -07002206 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002208
Elliott Hughes75770752011-08-24 17:52:38 -07002209 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002210
2211 // Do we definitely know what kind of reference this is?
2212 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2213 IndirectRefKind kind = GetIndirectRefKind(ref);
2214 switch (kind) {
2215 case kLocal:
2216 return JNILocalRefType;
2217 case kGlobal:
2218 return JNIGlobalRefType;
2219 case kWeakGlobal:
2220 return JNIWeakGlobalRefType;
2221 case kSirtOrInvalid:
2222 // Is it in a stack IRT?
2223 if (ts.Self()->SirtContains(java_object)) {
2224 return JNILocalRefType;
2225 }
2226
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002227 if (!ts.Env()->work_around_app_jni_bugs) {
2228 return JNIInvalidRefType;
2229 }
2230
Elliott Hughesb465ab02011-08-24 11:21:21 -07002231 // If we're handing out direct pointers, check whether it's a direct pointer
2232 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002233 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002234 if (ts.Env()->locals.Contains(java_object)) {
2235 return JNILocalRefType;
2236 }
2237 }
2238
2239 return JNIInvalidRefType;
2240 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 }
2242};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002243
Elliott Hughesa2501992011-08-26 19:39:54 -07002244const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002245 NULL, // reserved0.
2246 NULL, // reserved1.
2247 NULL, // reserved2.
2248 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 JNI::GetVersion,
2250 JNI::DefineClass,
2251 JNI::FindClass,
2252 JNI::FromReflectedMethod,
2253 JNI::FromReflectedField,
2254 JNI::ToReflectedMethod,
2255 JNI::GetSuperclass,
2256 JNI::IsAssignableFrom,
2257 JNI::ToReflectedField,
2258 JNI::Throw,
2259 JNI::ThrowNew,
2260 JNI::ExceptionOccurred,
2261 JNI::ExceptionDescribe,
2262 JNI::ExceptionClear,
2263 JNI::FatalError,
2264 JNI::PushLocalFrame,
2265 JNI::PopLocalFrame,
2266 JNI::NewGlobalRef,
2267 JNI::DeleteGlobalRef,
2268 JNI::DeleteLocalRef,
2269 JNI::IsSameObject,
2270 JNI::NewLocalRef,
2271 JNI::EnsureLocalCapacity,
2272 JNI::AllocObject,
2273 JNI::NewObject,
2274 JNI::NewObjectV,
2275 JNI::NewObjectA,
2276 JNI::GetObjectClass,
2277 JNI::IsInstanceOf,
2278 JNI::GetMethodID,
2279 JNI::CallObjectMethod,
2280 JNI::CallObjectMethodV,
2281 JNI::CallObjectMethodA,
2282 JNI::CallBooleanMethod,
2283 JNI::CallBooleanMethodV,
2284 JNI::CallBooleanMethodA,
2285 JNI::CallByteMethod,
2286 JNI::CallByteMethodV,
2287 JNI::CallByteMethodA,
2288 JNI::CallCharMethod,
2289 JNI::CallCharMethodV,
2290 JNI::CallCharMethodA,
2291 JNI::CallShortMethod,
2292 JNI::CallShortMethodV,
2293 JNI::CallShortMethodA,
2294 JNI::CallIntMethod,
2295 JNI::CallIntMethodV,
2296 JNI::CallIntMethodA,
2297 JNI::CallLongMethod,
2298 JNI::CallLongMethodV,
2299 JNI::CallLongMethodA,
2300 JNI::CallFloatMethod,
2301 JNI::CallFloatMethodV,
2302 JNI::CallFloatMethodA,
2303 JNI::CallDoubleMethod,
2304 JNI::CallDoubleMethodV,
2305 JNI::CallDoubleMethodA,
2306 JNI::CallVoidMethod,
2307 JNI::CallVoidMethodV,
2308 JNI::CallVoidMethodA,
2309 JNI::CallNonvirtualObjectMethod,
2310 JNI::CallNonvirtualObjectMethodV,
2311 JNI::CallNonvirtualObjectMethodA,
2312 JNI::CallNonvirtualBooleanMethod,
2313 JNI::CallNonvirtualBooleanMethodV,
2314 JNI::CallNonvirtualBooleanMethodA,
2315 JNI::CallNonvirtualByteMethod,
2316 JNI::CallNonvirtualByteMethodV,
2317 JNI::CallNonvirtualByteMethodA,
2318 JNI::CallNonvirtualCharMethod,
2319 JNI::CallNonvirtualCharMethodV,
2320 JNI::CallNonvirtualCharMethodA,
2321 JNI::CallNonvirtualShortMethod,
2322 JNI::CallNonvirtualShortMethodV,
2323 JNI::CallNonvirtualShortMethodA,
2324 JNI::CallNonvirtualIntMethod,
2325 JNI::CallNonvirtualIntMethodV,
2326 JNI::CallNonvirtualIntMethodA,
2327 JNI::CallNonvirtualLongMethod,
2328 JNI::CallNonvirtualLongMethodV,
2329 JNI::CallNonvirtualLongMethodA,
2330 JNI::CallNonvirtualFloatMethod,
2331 JNI::CallNonvirtualFloatMethodV,
2332 JNI::CallNonvirtualFloatMethodA,
2333 JNI::CallNonvirtualDoubleMethod,
2334 JNI::CallNonvirtualDoubleMethodV,
2335 JNI::CallNonvirtualDoubleMethodA,
2336 JNI::CallNonvirtualVoidMethod,
2337 JNI::CallNonvirtualVoidMethodV,
2338 JNI::CallNonvirtualVoidMethodA,
2339 JNI::GetFieldID,
2340 JNI::GetObjectField,
2341 JNI::GetBooleanField,
2342 JNI::GetByteField,
2343 JNI::GetCharField,
2344 JNI::GetShortField,
2345 JNI::GetIntField,
2346 JNI::GetLongField,
2347 JNI::GetFloatField,
2348 JNI::GetDoubleField,
2349 JNI::SetObjectField,
2350 JNI::SetBooleanField,
2351 JNI::SetByteField,
2352 JNI::SetCharField,
2353 JNI::SetShortField,
2354 JNI::SetIntField,
2355 JNI::SetLongField,
2356 JNI::SetFloatField,
2357 JNI::SetDoubleField,
2358 JNI::GetStaticMethodID,
2359 JNI::CallStaticObjectMethod,
2360 JNI::CallStaticObjectMethodV,
2361 JNI::CallStaticObjectMethodA,
2362 JNI::CallStaticBooleanMethod,
2363 JNI::CallStaticBooleanMethodV,
2364 JNI::CallStaticBooleanMethodA,
2365 JNI::CallStaticByteMethod,
2366 JNI::CallStaticByteMethodV,
2367 JNI::CallStaticByteMethodA,
2368 JNI::CallStaticCharMethod,
2369 JNI::CallStaticCharMethodV,
2370 JNI::CallStaticCharMethodA,
2371 JNI::CallStaticShortMethod,
2372 JNI::CallStaticShortMethodV,
2373 JNI::CallStaticShortMethodA,
2374 JNI::CallStaticIntMethod,
2375 JNI::CallStaticIntMethodV,
2376 JNI::CallStaticIntMethodA,
2377 JNI::CallStaticLongMethod,
2378 JNI::CallStaticLongMethodV,
2379 JNI::CallStaticLongMethodA,
2380 JNI::CallStaticFloatMethod,
2381 JNI::CallStaticFloatMethodV,
2382 JNI::CallStaticFloatMethodA,
2383 JNI::CallStaticDoubleMethod,
2384 JNI::CallStaticDoubleMethodV,
2385 JNI::CallStaticDoubleMethodA,
2386 JNI::CallStaticVoidMethod,
2387 JNI::CallStaticVoidMethodV,
2388 JNI::CallStaticVoidMethodA,
2389 JNI::GetStaticFieldID,
2390 JNI::GetStaticObjectField,
2391 JNI::GetStaticBooleanField,
2392 JNI::GetStaticByteField,
2393 JNI::GetStaticCharField,
2394 JNI::GetStaticShortField,
2395 JNI::GetStaticIntField,
2396 JNI::GetStaticLongField,
2397 JNI::GetStaticFloatField,
2398 JNI::GetStaticDoubleField,
2399 JNI::SetStaticObjectField,
2400 JNI::SetStaticBooleanField,
2401 JNI::SetStaticByteField,
2402 JNI::SetStaticCharField,
2403 JNI::SetStaticShortField,
2404 JNI::SetStaticIntField,
2405 JNI::SetStaticLongField,
2406 JNI::SetStaticFloatField,
2407 JNI::SetStaticDoubleField,
2408 JNI::NewString,
2409 JNI::GetStringLength,
2410 JNI::GetStringChars,
2411 JNI::ReleaseStringChars,
2412 JNI::NewStringUTF,
2413 JNI::GetStringUTFLength,
2414 JNI::GetStringUTFChars,
2415 JNI::ReleaseStringUTFChars,
2416 JNI::GetArrayLength,
2417 JNI::NewObjectArray,
2418 JNI::GetObjectArrayElement,
2419 JNI::SetObjectArrayElement,
2420 JNI::NewBooleanArray,
2421 JNI::NewByteArray,
2422 JNI::NewCharArray,
2423 JNI::NewShortArray,
2424 JNI::NewIntArray,
2425 JNI::NewLongArray,
2426 JNI::NewFloatArray,
2427 JNI::NewDoubleArray,
2428 JNI::GetBooleanArrayElements,
2429 JNI::GetByteArrayElements,
2430 JNI::GetCharArrayElements,
2431 JNI::GetShortArrayElements,
2432 JNI::GetIntArrayElements,
2433 JNI::GetLongArrayElements,
2434 JNI::GetFloatArrayElements,
2435 JNI::GetDoubleArrayElements,
2436 JNI::ReleaseBooleanArrayElements,
2437 JNI::ReleaseByteArrayElements,
2438 JNI::ReleaseCharArrayElements,
2439 JNI::ReleaseShortArrayElements,
2440 JNI::ReleaseIntArrayElements,
2441 JNI::ReleaseLongArrayElements,
2442 JNI::ReleaseFloatArrayElements,
2443 JNI::ReleaseDoubleArrayElements,
2444 JNI::GetBooleanArrayRegion,
2445 JNI::GetByteArrayRegion,
2446 JNI::GetCharArrayRegion,
2447 JNI::GetShortArrayRegion,
2448 JNI::GetIntArrayRegion,
2449 JNI::GetLongArrayRegion,
2450 JNI::GetFloatArrayRegion,
2451 JNI::GetDoubleArrayRegion,
2452 JNI::SetBooleanArrayRegion,
2453 JNI::SetByteArrayRegion,
2454 JNI::SetCharArrayRegion,
2455 JNI::SetShortArrayRegion,
2456 JNI::SetIntArrayRegion,
2457 JNI::SetLongArrayRegion,
2458 JNI::SetFloatArrayRegion,
2459 JNI::SetDoubleArrayRegion,
2460 JNI::RegisterNatives,
2461 JNI::UnregisterNatives,
2462 JNI::MonitorEnter,
2463 JNI::MonitorExit,
2464 JNI::GetJavaVM,
2465 JNI::GetStringRegion,
2466 JNI::GetStringUTFRegion,
2467 JNI::GetPrimitiveArrayCritical,
2468 JNI::ReleasePrimitiveArrayCritical,
2469 JNI::GetStringCritical,
2470 JNI::ReleaseStringCritical,
2471 JNI::NewWeakGlobalRef,
2472 JNI::DeleteWeakGlobalRef,
2473 JNI::ExceptionCheck,
2474 JNI::NewDirectByteBuffer,
2475 JNI::GetDirectBufferAddress,
2476 JNI::GetDirectBufferCapacity,
2477 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002478};
2479
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002480static const size_t kMonitorsInitial = 32; // Arbitrary.
2481static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2482
2483static const size_t kLocalsInitial = 64; // Arbitrary.
2484static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002485
Elliott Hughes75770752011-08-24 17:52:38 -07002486JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002487 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002488 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002489 local_ref_cookie(IRT_FIRST_SEGMENT),
2490 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes75770752011-08-24 17:52:38 -07002491 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002492 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002493 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002494 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002495 functions = unchecked_functions = &gNativeInterface;
2496 if (check_jni) {
2497 functions = GetCheckJniNativeInterface();
2498 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002499 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2500 // errors will ensue.
2501 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2502 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002503}
2504
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002505JNIEnvExt::~JNIEnvExt() {
2506}
2507
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002508void JNIEnvExt::DumpReferenceTables() {
2509 locals.Dump();
2510 monitors.Dump();
2511}
2512
Carl Shapiroea4dca82011-08-01 13:45:38 -07002513// JNI Invocation interface.
2514
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002515extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2516 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2517 if (args->version < JNI_VERSION_1_2) {
2518 return JNI_EVERSION;
2519 }
2520 Runtime::Options options;
2521 for (int i = 0; i < args->nOptions; ++i) {
2522 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002523 options.push_back(std::make_pair(StringPiece(option->optionString),
2524 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002525 }
2526 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002527 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002528 if (runtime == NULL) {
2529 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002530 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002531 runtime->Start();
2532 *p_env = Thread::Current()->GetJniEnv();
2533 *p_vm = runtime->GetJavaVM();
2534 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002535}
2536
Elliott Hughesf2682d52011-08-15 16:37:04 -07002537extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002538 Runtime* runtime = Runtime::Current();
2539 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002540 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002541 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002542 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002543 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002544 }
2545 return JNI_OK;
2546}
2547
2548// Historically unsupported.
2549extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2550 return JNI_ERR;
2551}
2552
Elliott Hughescdf53122011-08-19 15:46:09 -07002553class JII {
2554 public:
2555 static jint DestroyJavaVM(JavaVM* vm) {
2556 if (vm == NULL) {
2557 return JNI_ERR;
2558 } else {
2559 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2560 delete raw_vm->runtime;
2561 return JNI_OK;
2562 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002563 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002564
Elliott Hughescdf53122011-08-19 15:46:09 -07002565 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002566 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002567 }
2568
2569 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002570 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002571 }
2572
2573 static jint DetachCurrentThread(JavaVM* vm) {
2574 if (vm == NULL) {
2575 return JNI_ERR;
2576 } else {
2577 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2578 Runtime* runtime = raw_vm->runtime;
2579 runtime->DetachCurrentThread();
2580 return JNI_OK;
2581 }
2582 }
2583
2584 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2585 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2586 return JNI_EVERSION;
2587 }
2588 if (vm == NULL || env == NULL) {
2589 return JNI_ERR;
2590 }
2591 Thread* thread = Thread::Current();
2592 if (thread == NULL) {
2593 *env = NULL;
2594 return JNI_EDETACHED;
2595 }
2596 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002597 return JNI_OK;
2598 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002599};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002600
Elliott Hughesa2501992011-08-26 19:39:54 -07002601const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002602 NULL, // reserved0
2603 NULL, // reserved1
2604 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002605 JII::DestroyJavaVM,
2606 JII::AttachCurrentThread,
2607 JII::DetachCurrentThread,
2608 JII::GetEnv,
2609 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002610};
2611
Elliott Hughesbbd76712011-08-17 10:25:24 -07002612static const size_t kPinTableInitialSize = 16;
2613static const size_t kPinTableMaxSize = 1024;
2614
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002615static const size_t kGlobalsInitial = 512; // Arbitrary.
2616static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2617
2618static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2619static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2620
Elliott Hughesa0957642011-09-02 14:27:33 -07002621JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002622 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002623 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002624 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002625 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002626 verbose_jni(options->IsVerbose("jni")),
2627 log_third_party_jni(options->IsVerbose("third-party-jni")),
2628 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002629 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002630 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002631 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002632 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002633 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002634 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002635 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002636 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002637 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002638 functions = unchecked_functions = &gInvokeInterface;
2639 if (check_jni) {
2640 functions = GetCheckJniInvokeInterface();
2641 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002642}
2643
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002644JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002645 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002646}
2647
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002648void JavaVMExt::DumpReferenceTables() {
2649 {
2650 MutexLock mu(globals_lock);
2651 globals.Dump();
2652 }
2653 {
2654 MutexLock mu(weak_globals_lock);
2655 weak_globals.Dump();
2656 }
2657 {
2658 MutexLock mu(pins_lock);
2659 pin_table.Dump();
2660 }
2661}
2662
Elliott Hughes75770752011-08-24 17:52:38 -07002663bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2664 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002665
2666 // See if we've already loaded this library. If we have, and the class loader
2667 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002668 // TODO: for better results we should canonicalize the pathname (or even compare
2669 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002670 SharedLibrary* library;
2671 {
2672 // TODO: move the locking (and more of this logic) into Libraries.
2673 MutexLock mu(libraries_lock);
2674 library = libraries->Get(path);
2675 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002676 if (library != NULL) {
2677 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002678 // The library will be associated with class_loader. The JNI
2679 // spec says we can't load the same library into more than one
2680 // class loader.
2681 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2682 "ClassLoader %p; can't open in ClassLoader %p",
2683 path.c_str(), library->GetClassLoader(), class_loader);
2684 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002685 return false;
2686 }
2687 if (verbose_jni) {
2688 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2689 << "ClassLoader " << class_loader << "]";
2690 }
2691 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002692 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2693 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002694 return false;
2695 }
2696 return true;
2697 }
2698
2699 // Open the shared library. Because we're using a full path, the system
2700 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2701 // resolve this library's dependencies though.)
2702
2703 // Failures here are expected when java.library.path has several entries
2704 // and we have to hunt for the lib.
2705
2706 // The current version of the dynamic linker prints detailed information
2707 // about dlopen() failures. Some things to check if the message is
2708 // cryptic:
2709 // - make sure the library exists on the device
2710 // - verify that the right path is being opened (the debug log message
2711 // above can help with that)
2712 // - check to see if the library is valid (e.g. not zero bytes long)
2713 // - check config/prelink-linux-arm.map to ensure that the library
2714 // is listed and is not being overrun by the previous entry (if
2715 // loading suddenly stops working on a prelinked library, this is
2716 // a good one to check)
2717 // - write a trivial app that calls sleep() then dlopen(), attach
2718 // to it with "strace -p <pid>" while it sleeps, and watch for
2719 // attempts to open nonexistent dependent shared libs
2720
2721 // TODO: automate some of these checks!
2722
2723 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002724 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002725 // the GC to ignore us.
2726 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002727 void* handle = NULL;
2728 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002729 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002730 handle = dlopen(path.c_str(), RTLD_LAZY);
2731 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002732
2733 if (verbose_jni) {
2734 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2735 }
2736
2737 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002738 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002739 return false;
2740 }
2741
2742 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002743 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002744 // TODO: move the locking (and more of this logic) into Libraries.
2745 MutexLock mu(libraries_lock);
2746 library = libraries->Get(path);
2747 if (library != NULL) {
2748 LOG(INFO) << "WOW: we lost a race to add shared library: "
2749 << "\"" << path << "\" ClassLoader=" << class_loader;
2750 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002751 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002752 library = new SharedLibrary(path, handle, class_loader);
2753 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002754 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002755
2756 if (verbose_jni) {
2757 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2758 }
2759
2760 bool result = true;
2761 void* sym = dlsym(handle, "JNI_OnLoad");
2762 if (sym == NULL) {
2763 if (verbose_jni) {
2764 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2765 }
2766 } else {
2767 // Call JNI_OnLoad. We have to override the current class
2768 // loader, which will always be "null" since the stuff at the
2769 // top of the stack is around Runtime.loadLibrary(). (See
2770 // the comments in the JNI FindClass function.)
2771 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2772 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002773 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002774 self->SetClassLoaderOverride(class_loader);
2775
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002776 int version = 0;
2777 {
2778 ScopedThreadStateChange tsc(self, Thread::kNative);
2779 if (verbose_jni) {
2780 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2781 }
2782 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002783 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002784
Brian Carlstromaded5f72011-10-07 17:15:04 -07002785 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002786
2787 if (version != JNI_VERSION_1_2 &&
2788 version != JNI_VERSION_1_4 &&
2789 version != JNI_VERSION_1_6) {
2790 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2791 << "bad version: " << version;
2792 // It's unwise to call dlclose() here, but we can mark it
2793 // as bad and ensure that future load attempts will fail.
2794 // We don't know how far JNI_OnLoad got, so there could
2795 // be some partially-initialized stuff accessible through
2796 // newly-registered native method calls. We could try to
2797 // unregister them, but that doesn't seem worthwhile.
2798 result = false;
2799 } else {
2800 if (verbose_jni) {
2801 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2802 << " from JNI_OnLoad in \"" << path << "\"]";
2803 }
2804 }
2805 }
2806
2807 library->SetResult(result);
2808 return result;
2809}
2810
2811void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2812 CHECK(m->IsNative());
2813
2814 Class* c = m->GetDeclaringClass();
2815
2816 // If this is a static method, it could be called before the class
2817 // has been initialized.
2818 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002819 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002820 return NULL;
2821 }
2822 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002823 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002824 }
2825
Brian Carlstrom16192862011-09-12 17:50:06 -07002826 std::string detail;
2827 void* native_method;
2828 {
2829 MutexLock mu(libraries_lock);
2830 native_method = libraries->FindNativeMethod(m, detail);
2831 }
2832 // throwing can cause libraries_lock to be reacquired
2833 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002834 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002835 }
2836 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002837}
2838
Elliott Hughes410c0c82011-09-01 17:58:25 -07002839void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2840 {
2841 MutexLock mu(globals_lock);
2842 globals.VisitRoots(visitor, arg);
2843 }
2844 {
2845 MutexLock mu(pins_lock);
2846 pin_table.VisitRoots(visitor, arg);
2847 }
2848 // The weak_globals table is visited by the GC itself (because it mutates the table).
2849}
2850
Ian Rogersdf20fe02011-07-20 20:34:16 -07002851} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002852
2853std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2854 switch (rhs) {
2855 case JNIInvalidRefType:
2856 os << "JNIInvalidRefType";
2857 return os;
2858 case JNILocalRefType:
2859 os << "JNILocalRefType";
2860 return os;
2861 case JNIGlobalRefType:
2862 os << "JNIGlobalRefType";
2863 return os;
2864 case JNIWeakGlobalRefType:
2865 os << "JNIWeakGlobalRefType";
2866 return os;
2867 }
2868}