blob: 5ab86920e1df142517504b6cd28fe00bd8d03c2d [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070015#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070016#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070018#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070021#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070022#include "scoped_jni_thread_state.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070023#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070025
26namespace art {
27
Shih-wei Liao31384c52011-09-06 15:27:45 -070028void* FindNativeMethod(Thread* thread) {
29 DCHECK(Thread::Current() == thread);
30
31 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
32 DCHECK(method != NULL);
33
34 // Lookup symbol address for method, on failure we'll return NULL with an
35 // exception set, otherwise we return the address of the method we found.
36 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
37 if (native_code == NULL) {
38 DCHECK(thread->IsExceptionPending());
39 return NULL;
40 } else {
41 // Register so that future calls don't come here
42 method->RegisterNative(native_code);
43 return native_code;
44 }
45}
46
Elliott Hughesc5f7c912011-08-18 14:00:42 -070047/*
48 * Add a local reference for an object to the current stack frame. When
49 * the native function returns, the reference will be discarded.
50 *
51 * We need to allow the same reference to be added multiple times.
52 *
53 * This will be called on otherwise unreferenced objects. We cannot do
54 * GC allocations here, and it's best if we don't grab a mutex.
55 *
56 * Returns the local reference (currently just the same pointer that was
57 * passed in), or NULL on failure.
58 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070059template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070060T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
61 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
62 Object* obj = const_cast<Object*>(const_obj);
63
Elliott Hughesc5f7c912011-08-18 14:00:42 -070064 if (obj == NULL) {
65 return NULL;
66 }
67
Elliott Hughesbf86d042011-08-31 17:53:14 -070068 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
69 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070070
Ian Rogers5a7a74a2011-09-26 16:32:29 -070071 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070072 IndirectRef ref = locals.Add(cookie, obj);
73 if (ref == NULL) {
74 // TODO: just change Add's DCHECK to CHECK and lose this?
75 locals.Dump();
76 LOG(FATAL) << "Failed adding to JNI local reference table "
77 << "(has " << locals.Capacity() << " entries)";
78 // TODO: dvmDumpThread(dvmThreadSelf(), false);
79 }
80
81#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070082 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070083 size_t entry_count = locals.Capacity();
84 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070085 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070086 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070087 locals.Dump();
88 // TODO: dvmDumpThread(dvmThreadSelf(), false);
89 // dvmAbort();
90 }
91 }
92#endif
93
Elliott Hughesbf86d042011-08-31 17:53:14 -070094 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070095 // Hand out direct pointers to support broken old apps.
96 return reinterpret_cast<T>(obj);
97 }
98
99 return reinterpret_cast<T>(ref);
100}
101
Elliott Hughesbf86d042011-08-31 17:53:14 -0700102// For external use.
103template<typename T>
104T Decode(JNIEnv* public_env, jobject obj) {
105 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
106 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
107}
108// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700109template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700110template Class* Decode<Class*>(JNIEnv*, jobject);
111template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
112template Object* Decode<Object*>(JNIEnv*, jobject);
113template String* Decode<String*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700114template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
115template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700116template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700117
118namespace {
119
Elliott Hughescdf53122011-08-19 15:46:09 -0700120jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
121 if (obj == NULL) {
122 return NULL;
123 }
Elliott Hughes75770752011-08-24 17:52:38 -0700124 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700125 IndirectReferenceTable& weak_globals = vm->weak_globals;
126 MutexLock mu(vm->weak_globals_lock);
127 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
128 return reinterpret_cast<jweak>(ref);
129}
130
Elliott Hughesbf86d042011-08-31 17:53:14 -0700131// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700132template<typename T>
133T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700134 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700135}
136
Elliott Hughes418d20f2011-09-22 14:00:39 -0700137byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
138 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700139 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700140 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700141 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700142 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
143 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700144 case 'Z':
145 case 'B':
146 case 'C':
147 case 'S':
148 case 'I':
149 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
150 offset += 4;
151 break;
152 case 'F':
153 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
154 offset += 4;
155 break;
156 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700157 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700158 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
159 offset += sizeof(Object*);
160 break;
161 }
162 case 'D':
163 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
164 offset += 8;
165 break;
166 case 'J':
167 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
168 offset += 8;
169 break;
170 }
171 }
172 return arg_array.release();
173}
174
Elliott Hughes418d20f2011-09-22 14:00:39 -0700175byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
176 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700177 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700178 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700179 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700180 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
181 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700182 case 'Z':
183 case 'B':
184 case 'C':
185 case 'S':
186 case 'I':
187 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
188 offset += 4;
189 break;
190 case 'F':
191 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
192 offset += 4;
193 break;
194 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700195 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700196 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
197 offset += sizeof(Object*);
198 break;
199 }
200 case 'D':
201 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
202 offset += 8;
203 break;
204 case 'J':
205 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
206 offset += 8;
207 break;
208 }
209 }
210 return arg_array.release();
211}
212
Elliott Hughes418d20f2011-09-22 14:00:39 -0700213JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
214 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700215 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700216 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700217 return result;
218}
219
Elliott Hughes418d20f2011-09-22 14:00:39 -0700220JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
221 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
222 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700223 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700224 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
225 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700226}
227
Elliott Hughes75770752011-08-24 17:52:38 -0700228Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700229 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700230}
231
Elliott Hughes418d20f2011-09-22 14:00:39 -0700232JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
233 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
234 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700235 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700236 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
237 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700238}
239
Elliott Hughes418d20f2011-09-22 14:00:39 -0700240JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
241 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
242 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700243 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700244 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
245 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700246}
247
Elliott Hughes6b436852011-08-12 10:16:44 -0700248// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
249// separated with slashes but aren't wrapped with "L;" like regular descriptors
250// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
251// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
252// supported names with dots too (such as "a.b.C").
253std::string NormalizeJniClassDescriptor(const char* name) {
254 std::string result;
255 // Add the missing "L;" if necessary.
256 if (name[0] == '[') {
257 result = name;
258 } else {
259 result += 'L';
260 result += name;
261 result += ';';
262 }
263 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700264 if (result.find('.') != std::string::npos) {
265 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
266 << "\"" << name << "\"";
267 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700268 }
269 return result;
270}
271
Elliott Hughescdf53122011-08-19 15:46:09 -0700272jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
273 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700274 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700275 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700276 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700277
278 Method* method = NULL;
279 if (is_static) {
280 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700281 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700282 method = c->FindVirtualMethod(name, sig);
283 if (method == NULL) {
284 // No virtual method matching the signature. Search declared
285 // private methods and constructors.
286 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700287 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700288 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700289
Elliott Hughescdf53122011-08-19 15:46:09 -0700290 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700291 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700292 // TODO: try searching for the opposite kind of method from is_static
293 // for better diagnostics?
Elliott Hughescc5f9a92011-09-28 19:17:29 -0700294 ts.Self()->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700295 "no %s method %s", is_static ? "static" : "non-static",
296 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700297 return NULL;
298 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700299
Elliott Hughes418d20f2011-09-22 14:00:39 -0700300 method->InitJavaFields();
301
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700302 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700303}
304
Elliott Hughescdf53122011-08-19 15:46:09 -0700305jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
306 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700307 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700308 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700309 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700310
311 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 Class* field_type;
313 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
314 if (sig[1] != '\0') {
315 // TODO: need to get the appropriate ClassLoader.
316 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
317 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700318 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700319 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700320 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700321 if (field_type == NULL) {
322 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700323 DCHECK(ts.Self()->IsExceptionPending());
324 ts.Self()->ClearException();
325 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
326 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
327 "no type \"%s\" found and so no field \"%s\" could be found in class "
328 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 return NULL;
330 }
331 if (is_static) {
332 field = c->FindStaticField(name, field_type);
333 } else {
334 field = c->FindInstanceField(name, field_type);
335 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700336 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700337 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700338 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700339 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700340 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700341 return NULL;
342 }
Elliott Hughes80609252011-09-23 17:24:51 -0700343 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700344 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700345}
346
Elliott Hughes75770752011-08-24 17:52:38 -0700347void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
348 JavaVMExt* vm = ts.Vm();
349 MutexLock mu(vm->pins_lock);
350 vm->pin_table.Add(array);
351}
352
353void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
354 JavaVMExt* vm = ts.Vm();
355 MutexLock mu(vm->pins_lock);
356 vm->pin_table.Remove(array);
357}
358
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700359template<typename JniT, typename ArtT>
360JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
361 CHECK_GE(length, 0); // TODO: ReportJniError
362 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700363 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700364}
365
Elliott Hughes75770752011-08-24 17:52:38 -0700366template <typename ArrayT, typename CArrayT, typename ArtArrayT>
367CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
368 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
369 PinPrimitiveArray(ts, array);
370 if (is_copy != NULL) {
371 *is_copy = JNI_FALSE;
372 }
373 return array->GetData();
374}
375
376template <typename ArrayT>
377void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
378 if (mode != JNI_COMMIT) {
379 Array* array = Decode<Array*>(ts, java_array);
380 UnpinPrimitiveArray(ts, array);
381 }
382}
383
Elliott Hughes814e4032011-08-23 12:07:56 -0700384void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700385 std::string type(PrettyTypeOf(array));
Elliott Hughes814e4032011-08-23 12:07:56 -0700386 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
387 "%s offset=%d length=%d %s.length=%d",
388 type.c_str(), start, length, identifier, array->GetLength());
389}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700390void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
391 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
392 "offset=%d length=%d string.length()=%d", start, length, array_length);
393}
Elliott Hughes814e4032011-08-23 12:07:56 -0700394
395template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700396void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700397 ArrayT* array = Decode<ArrayT*>(ts, java_array);
398 if (start < 0 || length < 0 || start + length > array->GetLength()) {
399 ThrowAIOOBE(ts, array, start, length, "src");
400 } else {
401 JavaT* data = array->GetData();
402 memcpy(buf, data + start, length * sizeof(JavaT));
403 }
404}
405
406template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700407void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700408 ArrayT* array = Decode<ArrayT*>(ts, java_array);
409 if (start < 0 || length < 0 || start + length > array->GetLength()) {
410 ThrowAIOOBE(ts, array, start, length, "dst");
411 } else {
412 JavaT* data = array->GetData();
413 memcpy(data + start, buf, length * sizeof(JavaT));
414 }
415}
416
Elliott Hughes75770752011-08-24 17:52:38 -0700417jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700418 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
419 CHECK(buffer_class.get() != NULL);
420 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
421}
422
Elliott Hughes75770752011-08-24 17:52:38 -0700423jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700424 static jclass buffer_class = InitDirectByteBufferClass(env);
425 return buffer_class;
426}
427
Elliott Hughes75770752011-08-24 17:52:38 -0700428jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
429 if (vm == NULL || p_env == NULL) {
430 return JNI_ERR;
431 }
432
433 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
434 JavaVMAttachArgs args;
435 if (thr_args == NULL) {
436 // Allow the v1.1 calling convention.
437 args.version = JNI_VERSION_1_2;
438 args.name = NULL;
439 args.group = NULL; // TODO: get "main" thread group
440 } else {
441 args.version = in_args->version;
442 args.name = in_args->name;
443 if (in_args->group != NULL) {
444 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
445 args.group = NULL; // TODO: decode in_args->group
446 } else {
447 args.group = NULL; // TODO: get "main" thread group
448 }
449 }
450 CHECK_GE(args.version, JNI_VERSION_1_2);
451
452 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700453 runtime->AttachCurrentThread(args.name, as_daemon);
454 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700455 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700456}
457
Elliott Hughes79082e32011-08-25 12:07:32 -0700458class SharedLibrary {
459 public:
460 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
461 : path_(path),
462 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700463 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700464 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700465 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700466 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700467 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700468 }
469
Elliott Hughes79082e32011-08-25 12:07:32 -0700470 Object* GetClassLoader() {
471 return class_loader_;
472 }
473
474 std::string GetPath() {
475 return path_;
476 }
477
478 /*
479 * Check the result of an earlier call to JNI_OnLoad on this library. If
480 * the call has not yet finished in another thread, wait for it.
481 */
482 bool CheckOnLoadResult(JavaVMExt* vm) {
483 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700484 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 // Check this so we don't end up waiting for ourselves. We need
486 // to return "true" so the caller can continue.
487 LOG(INFO) << *self << " recursive attempt to load library "
488 << "\"" << path_ << "\"";
489 return true;
490 }
491
492 MutexLock mu(jni_on_load_lock_);
493 while (jni_on_load_result_ == kPending) {
494 if (vm->verbose_jni) {
495 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
496 << "JNI_OnLoad...]";
497 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700498 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700499 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700500 }
501
502 bool okay = (jni_on_load_result_ == kOkay);
503 if (vm->verbose_jni) {
504 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
505 << (okay ? "succeeded" : "failed") << "]";
506 }
507 return okay;
508 }
509
510 void SetResult(bool result) {
511 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700512 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700513
514 // Broadcast a wakeup to anybody sleeping on the condition variable.
515 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700516 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700517 }
518
519 void* FindSymbol(const std::string& symbol_name) {
520 return dlsym(handle_, symbol_name.c_str());
521 }
522
523 private:
524 enum JNI_OnLoadState {
525 kPending,
526 kFailed,
527 kOkay,
528 };
529
530 // Path to library "/system/lib/libjni.so".
531 std::string path_;
532
533 // The void* returned by dlopen(3).
534 void* handle_;
535
536 // The ClassLoader this library is associated with.
537 Object* class_loader_;
538
539 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700540 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700541 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700542 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700543 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700544 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700545 // Result of earlier JNI_OnLoad call.
546 JNI_OnLoadState jni_on_load_result_;
547};
548
Elliott Hughescdf53122011-08-19 15:46:09 -0700549} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700550
Elliott Hughes79082e32011-08-25 12:07:32 -0700551// This exists mainly to keep implementation details out of the header file.
552class Libraries {
553 public:
554 Libraries() {
555 }
556
557 ~Libraries() {
558 // Delete our map values. (The keys will be cleaned up by the map itself.)
559 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
560 delete it->second;
561 }
562 }
563
564 SharedLibrary* Get(const std::string& path) {
565 return libraries_[path];
566 }
567
568 void Put(const std::string& path, SharedLibrary* library) {
569 libraries_[path] = library;
570 }
571
572 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700573 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700574 std::string jni_short_name(JniShortName(m));
575 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700576 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700577 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
578 SharedLibrary* library = it->second;
579 if (library->GetClassLoader() != declaring_class_loader) {
580 // We only search libraries loaded by the appropriate ClassLoader.
581 continue;
582 }
583 // Try the short name then the long name...
584 void* fn = library->FindSymbol(jni_short_name);
585 if (fn == NULL) {
586 fn = library->FindSymbol(jni_long_name);
587 }
588 if (fn != NULL) {
589 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700590 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700591 << " in \"" << library->GetPath() << "\"]";
592 }
593 return fn;
594 }
595 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700596 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700597 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700598 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700599 return NULL;
600 }
601
602 private:
603 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
604
605 std::map<std::string, SharedLibrary*> libraries_;
606};
607
Elliott Hughes418d20f2011-09-22 14:00:39 -0700608JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
609 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
610 Object* receiver = Decode<Object*>(env, obj);
611 Method* method = DecodeMethod(mid);
612 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
613 return InvokeWithArgArray(env, receiver, method, arg_array.get());
614}
615
Elliott Hughescdf53122011-08-19 15:46:09 -0700616class JNI {
617 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700618
Elliott Hughescdf53122011-08-19 15:46:09 -0700619 static jint GetVersion(JNIEnv* env) {
620 ScopedJniThreadState ts(env);
621 return JNI_VERSION_1_6;
622 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700623
Elliott Hughescdf53122011-08-19 15:46:09 -0700624 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
625 ScopedJniThreadState ts(env);
626 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700627 return NULL;
628 }
629
Elliott Hughescdf53122011-08-19 15:46:09 -0700630 static jclass FindClass(JNIEnv* env, const char* name) {
631 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700632 Runtime* runtime = Runtime::Current();
633 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700634 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700635 Class* c = NULL;
636 if (runtime->IsStarted()) {
637 // TODO: need to get the appropriate ClassLoader.
638 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
639 c = class_linker->FindClass(descriptor, cl);
640 } else {
641 c = class_linker->FindSystemClass(descriptor);
642 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700643 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700644 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700645
Elliott Hughescdf53122011-08-19 15:46:09 -0700646 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
647 ScopedJniThreadState ts(env);
648 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700649 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700650 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700651
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
653 ScopedJniThreadState ts(env);
654 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700655 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700656 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700657
Elliott Hughescdf53122011-08-19 15:46:09 -0700658 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
659 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700660 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700661 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700662 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700663
Elliott Hughescdf53122011-08-19 15:46:09 -0700664 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
665 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700666 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700667 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700668 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700669
Elliott Hughes37f7a402011-08-22 18:56:01 -0700670 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
671 ScopedJniThreadState ts(env);
672 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700673 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700674 }
675
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700676 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700677 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700678 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700679 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700681
Elliott Hughes37f7a402011-08-22 18:56:01 -0700682 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700684 Class* c1 = Decode<Class*>(ts, java_class1);
685 Class* c2 = Decode<Class*>(ts, java_class2);
686 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700687 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700688
Elliott Hughes37f7a402011-08-22 18:56:01 -0700689 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700691 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700693 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700694 return JNI_TRUE;
695 } else {
696 Object* obj = Decode<Object*>(ts, jobj);
697 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700698 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700699 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700700 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700701
Elliott Hughes37f7a402011-08-22 18:56:01 -0700702 static jint Throw(JNIEnv* env, jthrowable java_exception) {
703 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700704 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700705 if (exception == NULL) {
706 return JNI_ERR;
707 }
708 ts.Self()->SetException(exception);
709 return JNI_OK;
710 }
711
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700712 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700713 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700714 // TODO: check for a pending exception to decide what constructor to call.
715 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
716 if (mid == NULL) {
717 return JNI_ERR;
718 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700719 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
720 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700721 return JNI_ERR;
722 }
723
724 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700725 args[0].l = s.get();
726 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
727 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700728 return JNI_ERR;
729 }
730
Elliott Hughes54e7df12011-09-16 11:47:04 -0700731 LOG(INFO) << "Throwing " << PrettyTypeOf(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700732 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700733 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700734
Elliott Hughes37f7a402011-08-22 18:56:01 -0700735 return JNI_OK;
736 }
737
738 static jboolean ExceptionCheck(JNIEnv* env) {
739 ScopedJniThreadState ts(env);
740 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
741 }
742
743 static void ExceptionClear(JNIEnv* env) {
744 ScopedJniThreadState ts(env);
745 ts.Self()->ClearException();
746 }
747
748 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700749 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700750
751 Thread* self = ts.Self();
752 Throwable* original_exception = self->GetException();
753 self->ClearException();
754
Elliott Hughesbf86d042011-08-31 17:53:14 -0700755 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700756 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
757 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
758 if (mid == NULL) {
759 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700760 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700761 } else {
762 env->CallVoidMethod(exception.get(), mid);
763 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700764 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700765 << " thrown while calling printStackTrace";
766 self->ClearException();
767 }
768 }
769
770 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700771 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700772
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 static jthrowable ExceptionOccurred(JNIEnv* env) {
774 ScopedJniThreadState ts(env);
775 Object* exception = ts.Self()->GetException();
776 if (exception == NULL) {
777 return NULL;
778 } else {
779 // TODO: if adding a local reference failing causes the VM to abort
780 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700781 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 if (localException == NULL) {
783 // We were unable to add a new local reference, and threw a new
784 // exception. We can't return "exception", because it's not a
785 // local reference. So we have to return NULL, indicating that
786 // there was no exception, even though it's pretty much raining
787 // exceptions in here.
788 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
789 }
790 return localException;
791 }
792 }
793
Elliott Hughescdf53122011-08-19 15:46:09 -0700794 static void FatalError(JNIEnv* env, const char* msg) {
795 ScopedJniThreadState ts(env);
796 LOG(FATAL) << "JNI FatalError called: " << msg;
797 }
798
799 static jint PushLocalFrame(JNIEnv* env, jint cap) {
800 ScopedJniThreadState ts(env);
801 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
802 return JNI_OK;
803 }
804
805 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
806 ScopedJniThreadState ts(env);
807 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
808 return res;
809 }
810
Elliott Hughes72025e52011-08-23 17:50:30 -0700811 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
812 ScopedJniThreadState ts(env);
813 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
814 return 0;
815 }
816
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
818 ScopedJniThreadState ts(env);
819 if (obj == NULL) {
820 return NULL;
821 }
822
Elliott Hughes75770752011-08-24 17:52:38 -0700823 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 IndirectReferenceTable& globals = vm->globals;
825 MutexLock mu(vm->globals_lock);
826 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
827 return reinterpret_cast<jobject>(ref);
828 }
829
830 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
831 ScopedJniThreadState ts(env);
832 if (obj == NULL) {
833 return;
834 }
835
Elliott Hughes75770752011-08-24 17:52:38 -0700836 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 IndirectReferenceTable& globals = vm->globals;
838 MutexLock mu(vm->globals_lock);
839
840 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
841 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
842 << "failed to find entry";
843 }
844 }
845
846 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
847 ScopedJniThreadState ts(env);
848 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
849 }
850
851 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
852 ScopedJniThreadState ts(env);
853 if (obj == NULL) {
854 return;
855 }
856
Elliott Hughes75770752011-08-24 17:52:38 -0700857 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700858 IndirectReferenceTable& weak_globals = vm->weak_globals;
859 MutexLock mu(vm->weak_globals_lock);
860
861 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
862 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
863 << "failed to find entry";
864 }
865 }
866
867 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
868 ScopedJniThreadState ts(env);
869 if (obj == NULL) {
870 return NULL;
871 }
872
873 IndirectReferenceTable& locals = ts.Env()->locals;
874
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700875 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700876 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
877 return reinterpret_cast<jobject>(ref);
878 }
879
880 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
881 ScopedJniThreadState ts(env);
882 if (obj == NULL) {
883 return;
884 }
885
886 IndirectReferenceTable& locals = ts.Env()->locals;
887
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700888 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 if (!locals.Remove(cookie, obj)) {
890 // Attempting to delete a local reference that is not in the
891 // topmost local reference frame is a no-op. DeleteLocalRef returns
892 // void and doesn't throw any exceptions, but we should probably
893 // complain about it so the user will notice that things aren't
894 // going quite the way they expect.
895 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
896 << "failed to find entry";
897 }
898 }
899
900 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
901 ScopedJniThreadState ts(env);
902 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
903 ? JNI_TRUE : JNI_FALSE;
904 }
905
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700906 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700908 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700909 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700910 return NULL;
911 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700912 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 }
914
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 ScopedJniThreadState ts(env);
917 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 va_start(args, mid);
919 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 va_end(args);
921 return result;
922 }
923
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700926 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700927 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700928 return NULL;
929 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700930 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700931 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 return local_result;
934 }
935
Elliott Hughes72025e52011-08-23 17:50:30 -0700936 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700938 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700939 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700940 return NULL;
941 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700942 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700943 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700944 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 return local_result;
946 }
947
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
949 ScopedJniThreadState ts(env);
950 return FindMethodID(ts, c, name, sig, false);
951 }
952
953 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
954 ScopedJniThreadState ts(env);
955 return FindMethodID(ts, c, name, sig, true);
956 }
957
Elliott Hughes72025e52011-08-23 17:50:30 -0700958 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700959 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 va_list ap;
961 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700962 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700964 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700969 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700970 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 }
972
Elliott Hughes72025e52011-08-23 17:50:30 -0700973 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700975 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700976 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 va_list ap;
982 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700983 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 va_end(ap);
985 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700990 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700995 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 va_list ap;
1001 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001002 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 va_end(ap);
1004 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001009 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001014 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_list ap;
1020 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001021 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_end(ap);
1023 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001028 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001033 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 va_list ap;
1039 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001040 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 va_end(ap);
1042 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001047 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001052 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 va_list ap;
1058 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001059 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_end(ap);
1061 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001066 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 }
1068
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001071 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 }
1073
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 va_list ap;
1077 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001078 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 va_end(ap);
1080 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001085 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001090 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 }
1092
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 va_list ap;
1096 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001097 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 va_end(ap);
1099 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001104 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001109 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 va_list ap;
1115 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001116 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_end(ap);
1118 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001123 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001128 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 }
1130
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 va_list ap;
1134 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001135 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001141 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001146 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
1149 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 ScopedJniThreadState ts(env);
1152 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001154 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001155 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 va_end(ap);
1157 return local_result;
1158 }
1159
1160 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001163 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001164 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
1167 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001170 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001171 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
1174 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 ScopedJniThreadState ts(env);
1177 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001179 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 va_end(ap);
1181 return result.z;
1182 }
1183
1184 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001187 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
1190 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001193 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
1196 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 ScopedJniThreadState ts(env);
1199 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001201 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 va_end(ap);
1203 return result.b;
1204 }
1205
1206 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001209 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
1212 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001215 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 }
1217
1218 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 ScopedJniThreadState ts(env);
1221 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001223 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 va_end(ap);
1225 return result.c;
1226 }
1227
1228 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001231 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
1234 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001237 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
1240 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
1243 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001244 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001245 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 va_end(ap);
1247 return result.s;
1248 }
1249
1250 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001253 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 }
1255
1256 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001259 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
Elliott Hughes418d20f2011-09-22 14:00:39 -07001262 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 ScopedJniThreadState ts(env);
1264 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001266 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 va_end(ap);
1268 return result.i;
1269 }
1270
1271 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001274 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
1277 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001280 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
1283 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 ScopedJniThreadState ts(env);
1286 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001288 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 va_end(ap);
1290 return result.j;
1291 }
1292
1293 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001296 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
1298
1299 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001300 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001302 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
1305 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001306 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 ScopedJniThreadState ts(env);
1308 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001310 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 va_end(ap);
1312 return result.f;
1313 }
1314
1315 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001318 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
1321 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001322 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001324 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
1327 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 ScopedJniThreadState ts(env);
1330 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001332 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 va_end(ap);
1334 return result.d;
1335 }
1336
1337 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001340 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
1343 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001346 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 }
1348
1349 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001350 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 ScopedJniThreadState ts(env);
1352 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001354 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 va_end(ap);
1356 }
1357
1358 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001361 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
1364 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001365 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001367 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
1369
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001370 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 ScopedJniThreadState ts(env);
1372 return FindFieldID(ts, c, name, sig, false);
1373 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001374
1375
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001376 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 ScopedJniThreadState ts(env);
1378 return FindFieldID(ts, c, name, sig, true);
1379 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001380
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001383 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001384 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001385 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001387
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001390 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001391 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
1393
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 Object* o = Decode<Object*>(ts, java_object);
1397 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001398 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001405 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001406 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 }
1408
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001409#define GET_PRIMITIVE_FIELD(fn, instance) \
1410 ScopedJniThreadState ts(env); \
1411 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001412 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001413 return f->fn(o)
1414
1415#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1416 ScopedJniThreadState ts(env); \
1417 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001418 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001419 f->fn(o, value)
1420
1421 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1422 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001425 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1426 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 }
1428
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001429 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1430 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
1432
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001433 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1434 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
1436
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001437 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1438 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 }
1440
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001441 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1442 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
1444
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001445 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1446 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 }
1448
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001449 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1450 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 }
1452
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001453 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1454 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 }
1456
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001457 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1458 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
1460
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001461 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1462 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 }
1464
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001465 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1466 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 }
1468
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001469 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1470 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001471 }
1472
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001473 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1474 GET_PRIMITIVE_FIELD(GetLong, NULL);
1475 }
1476
1477 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1478 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1479 }
1480
1481 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1482 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1483 }
1484
1485 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1486 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1487 }
1488
1489 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1490 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1491 }
1492
1493 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1494 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1495 }
1496
1497 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1498 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1499 }
1500
1501 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1502 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1503 }
1504
1505 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1506 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1507 }
1508
1509 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1510 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1511 }
1512
1513 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1514 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1515 }
1516
1517 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1518 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1519 }
1520
1521 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1522 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1523 }
1524
1525 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1526 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1527 }
1528
1529 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1530 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1531 }
1532
1533 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1534 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1535 }
1536
1537 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1538 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1539 }
1540
1541 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1542 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1543 }
1544
1545 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1546 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes418d20f2011-09-22 14:00:39 -07001549 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 ScopedJniThreadState ts(env);
1551 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001552 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001553 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001554 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 va_end(ap);
1556 return local_result;
1557 }
1558
Elliott Hughes418d20f2011-09-22 14:00:39 -07001559 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001561 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001562 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes418d20f2011-09-22 14:00:39 -07001565 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001567 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001568 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes418d20f2011-09-22 14:00:39 -07001571 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001572 ScopedJniThreadState ts(env);
1573 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001574 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001575 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 va_end(ap);
1577 return result.z;
1578 }
1579
Elliott Hughes418d20f2011-09-22 14:00:39 -07001580 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001582 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes418d20f2011-09-22 14:00:39 -07001585 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001587 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 }
1589
Elliott Hughes72025e52011-08-23 17:50:30 -07001590 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 ScopedJniThreadState ts(env);
1592 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001593 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001594 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 va_end(ap);
1596 return result.b;
1597 }
1598
Elliott Hughes418d20f2011-09-22 14:00:39 -07001599 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001601 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes418d20f2011-09-22 14:00:39 -07001604 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001606 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes72025e52011-08-23 17:50:30 -07001609 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 ScopedJniThreadState ts(env);
1611 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001612 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001613 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 va_end(ap);
1615 return result.c;
1616 }
1617
Elliott Hughes418d20f2011-09-22 14:00:39 -07001618 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001620 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 }
1622
Elliott Hughes418d20f2011-09-22 14:00:39 -07001623 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001625 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 }
1627
Elliott Hughes72025e52011-08-23 17:50:30 -07001628 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 ScopedJniThreadState ts(env);
1630 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001631 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001632 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 va_end(ap);
1634 return result.s;
1635 }
1636
Elliott Hughes418d20f2011-09-22 14:00:39 -07001637 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001639 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
Elliott Hughes418d20f2011-09-22 14:00:39 -07001642 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001644 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 }
1646
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 ScopedJniThreadState ts(env);
1649 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001650 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001651 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 va_end(ap);
1653 return result.i;
1654 }
1655
Elliott Hughes418d20f2011-09-22 14:00:39 -07001656 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001658 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 }
1660
Elliott Hughes418d20f2011-09-22 14:00:39 -07001661 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001663 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 }
1665
Elliott Hughes72025e52011-08-23 17:50:30 -07001666 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 ScopedJniThreadState ts(env);
1668 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001669 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001670 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 va_end(ap);
1672 return result.j;
1673 }
1674
Elliott Hughes418d20f2011-09-22 14:00:39 -07001675 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001677 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 }
1679
Elliott Hughes418d20f2011-09-22 14:00:39 -07001680 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001682 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 }
1684
Elliott Hughes72025e52011-08-23 17:50:30 -07001685 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 ScopedJniThreadState ts(env);
1687 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001688 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001689 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 va_end(ap);
1691 return result.f;
1692 }
1693
Elliott Hughes418d20f2011-09-22 14:00:39 -07001694 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001696 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 }
1698
Elliott Hughes418d20f2011-09-22 14:00:39 -07001699 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001701 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 }
1703
Elliott Hughes72025e52011-08-23 17:50:30 -07001704 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 ScopedJniThreadState ts(env);
1706 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001708 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 va_end(ap);
1710 return result.d;
1711 }
1712
Elliott Hughes418d20f2011-09-22 14:00:39 -07001713 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001715 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 }
1717
Elliott Hughes418d20f2011-09-22 14:00:39 -07001718 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001720 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 }
1722
Elliott Hughes72025e52011-08-23 17:50:30 -07001723 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 ScopedJniThreadState ts(env);
1725 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001726 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001727 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 va_end(ap);
1729 }
1730
Elliott Hughes418d20f2011-09-22 14:00:39 -07001731 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001733 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes418d20f2011-09-22 14:00:39 -07001736 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001738 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 }
1740
Elliott Hughes814e4032011-08-23 12:07:56 -07001741 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001743 if (chars == NULL && char_count == 0) {
1744 return NULL;
1745 }
1746 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001747 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
1750 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1751 ScopedJniThreadState ts(env);
1752 if (utf == NULL) {
1753 return NULL;
1754 }
1755 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001756 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 }
1758
Elliott Hughes814e4032011-08-23 12:07:56 -07001759 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1760 ScopedJniThreadState ts(env);
1761 return Decode<String*>(ts, java_string)->GetLength();
1762 }
1763
1764 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1765 ScopedJniThreadState ts(env);
1766 return Decode<String*>(ts, java_string)->GetUtfLength();
1767 }
1768
Elliott Hughesb465ab02011-08-24 11:21:21 -07001769 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001771 String* s = Decode<String*>(ts, java_string);
1772 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1773 ThrowSIOOBE(ts, start, length, s->GetLength());
1774 } else {
1775 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1776 memcpy(buf, chars + start, length * sizeof(jchar));
1777 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001778 }
1779
Elliott Hughesb465ab02011-08-24 11:21:21 -07001780 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001781 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001782 String* s = Decode<String*>(ts, java_string);
1783 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1784 ThrowSIOOBE(ts, start, length, s->GetLength());
1785 } else {
1786 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1787 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1788 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001789 }
1790
Elliott Hughes75770752011-08-24 17:52:38 -07001791 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001792 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001793 String* s = Decode<String*>(ts, java_string);
1794 const CharArray* chars = s->GetCharArray();
1795 PinPrimitiveArray(ts, chars);
1796 if (is_copy != NULL) {
1797 *is_copy = JNI_FALSE;
1798 }
1799 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001800 }
1801
Elliott Hughes75770752011-08-24 17:52:38 -07001802 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001803 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001804 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
Elliott Hughes75770752011-08-24 17:52:38 -07001807 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001809 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 }
1811
Elliott Hughes75770752011-08-24 17:52:38 -07001812 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001814 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
Elliott Hughes75770752011-08-24 17:52:38 -07001817 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001818 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001819 if (java_string == NULL) {
1820 return NULL;
1821 }
1822 if (is_copy != NULL) {
1823 *is_copy = JNI_TRUE;
1824 }
1825 String* s = Decode<String*>(ts, java_string);
1826 size_t byte_count = s->GetUtfLength();
1827 char* bytes = new char[byte_count + 1];
1828 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001829 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001830 return NULL;
1831 }
1832 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1833 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1834 bytes[byte_count] = '\0';
1835 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001836 }
1837
Elliott Hughes75770752011-08-24 17:52:38 -07001838 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001839 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001840 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001841 }
1842
Elliott Hughesbd935992011-08-22 11:59:34 -07001843 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001845 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001846 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001847 Array* array = obj->AsArray();
1848 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 }
1850
Elliott Hughes814e4032011-08-23 12:07:56 -07001851 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001853 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001854 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 }
1856
1857 static void SetObjectArrayElement(JNIEnv* env,
1858 jobjectArray java_array, jsize index, jobject java_value) {
1859 ScopedJniThreadState ts(env);
1860 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1861 Object* value = Decode<Object*>(ts, java_value);
1862 array->Set(index, value);
1863 }
1864
1865 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1866 ScopedJniThreadState ts(env);
1867 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1868 }
1869
1870 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1871 ScopedJniThreadState ts(env);
1872 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1873 }
1874
1875 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1876 ScopedJniThreadState ts(env);
1877 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1878 }
1879
1880 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1881 ScopedJniThreadState ts(env);
1882 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1883 }
1884
1885 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1886 ScopedJniThreadState ts(env);
1887 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1888 }
1889
1890 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1891 ScopedJniThreadState ts(env);
1892 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1893 }
1894
1895 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1896 ScopedJniThreadState ts(env);
1897 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1898 }
1899
1900 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1901 ScopedJniThreadState ts(env);
1902 CHECK_GE(length, 0); // TODO: ReportJniError
1903
1904 // Compute the array class corresponding to the given element class.
1905 Class* element_class = Decode<Class*>(ts, element_jclass);
1906 std::string descriptor;
1907 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001908 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001909
1910 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001911 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1912 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 return NULL;
1914 }
1915
Elliott Hughes75770752011-08-24 17:52:38 -07001916 // Allocate and initialize if necessary.
1917 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001919 if (initial_element != NULL) {
1920 Object* initial_object = Decode<Object*>(ts, initial_element);
1921 for (jsize i = 0; i < length; ++i) {
1922 result->Set(i, initial_object);
1923 }
1924 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001925 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 }
1927
1928 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1929 ScopedJniThreadState ts(env);
1930 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1931 }
1932
Elliott Hughes75770752011-08-24 17:52:38 -07001933 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001934 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001935 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001936 }
1937
Elliott Hughes75770752011-08-24 17:52:38 -07001938 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001939 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001940 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001945 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 }
1947
Elliott Hughes75770752011-08-24 17:52:38 -07001948 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001950 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 }
1952
Elliott Hughes75770752011-08-24 17:52:38 -07001953 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001955 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 }
1957
Elliott Hughes75770752011-08-24 17:52:38 -07001958 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001960 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 }
1962
Elliott Hughes75770752011-08-24 17:52:38 -07001963 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001965 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Elliott Hughes75770752011-08-24 17:52:38 -07001968 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001970 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Elliott Hughes75770752011-08-24 17:52:38 -07001973 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001975 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Elliott Hughes75770752011-08-24 17:52:38 -07001978 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001980 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Elliott Hughes75770752011-08-24 17:52:38 -07001983 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001985 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Elliott Hughes75770752011-08-24 17:52:38 -07001988 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001990 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001995 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes75770752011-08-24 17:52:38 -07001998 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002000 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes75770752011-08-24 17:52:38 -07002003 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002005 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002010 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002015 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes75770752011-08-24 17:52:38 -07002018 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002020 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes814e4032011-08-23 12:07:56 -07002023 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002025 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes814e4032011-08-23 12:07:56 -07002028 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002030 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes814e4032011-08-23 12:07:56 -07002033 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002035 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes814e4032011-08-23 12:07:56 -07002038 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002040 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes814e4032011-08-23 12:07:56 -07002043 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002045 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002070 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes5174fe62011-08-23 15:12:35 -07002103 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002105 Class* c = Decode<Class*>(ts, java_class);
2106
Elliott Hughes5174fe62011-08-23 15:12:35 -07002107 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 const char* name = methods[i].name;
2109 const char* sig = methods[i].signature;
2110
2111 if (*sig == '!') {
2112 // TODO: fast jni. it's too noisy to log all these.
2113 ++sig;
2114 }
2115
Elliott Hughes5174fe62011-08-23 15:12:35 -07002116 Method* m = c->FindDirectMethod(name, sig);
2117 if (m == NULL) {
2118 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002120 if (m == NULL) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002121 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescc5f9a92011-09-28 19:17:29 -07002122 ts.Self()->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002124 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002126 } else if (!m->IsNative()) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002127 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescc5f9a92011-09-28 19:17:29 -07002128 ts.Self()->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002130 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 return JNI_ERR;
2132 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002133
Elliott Hughes75770752011-08-24 17:52:38 -07002134 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002135 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002136 }
2137
2138 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140 return JNI_OK;
2141 }
2142
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002145 Class* c = Decode<Class*>(ts, java_class);
2146
Elliott Hughes75770752011-08-24 17:52:38 -07002147 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002148 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002149 }
2150
2151 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2152 Method* m = c->GetDirectMethod(i);
2153 if (m->IsNative()) {
2154 m->UnregisterNative();
2155 }
2156 }
2157 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2158 Method* m = c->GetVirtualMethod(i);
2159 if (m->IsNative()) {
2160 m->UnregisterNative();
2161 }
2162 }
2163
2164 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Elliott Hughes72025e52011-08-23 17:50:30 -07002167 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002169 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002170 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Elliott Hughes72025e52011-08-23 17:50:30 -07002173 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002175 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002176 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178
2179 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2180 ScopedJniThreadState ts(env);
2181 Runtime* runtime = Runtime::Current();
2182 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002183 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 } else {
2185 *vm = NULL;
2186 }
2187 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2188 }
2189
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2191 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002192
2193 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002194 CHECK(address != NULL); // TODO: ReportJniError
2195 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002196
2197 jclass buffer_class = GetDirectByteBufferClass(env);
2198 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2199 if (mid == NULL) {
2200 return NULL;
2201 }
2202
2203 // At the moment, the Java side is limited to 32 bits.
2204 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2205 CHECK_LE(capacity, 0xffffffff);
2206 jint address_arg = reinterpret_cast<jint>(address);
2207 jint capacity_arg = static_cast<jint>(capacity);
2208
2209 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2210 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
Elliott Hughesb465ab02011-08-24 11:21:21 -07002213 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002215 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2216 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218
Elliott Hughesb465ab02011-08-24 11:21:21 -07002219 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002221 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2222 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Elliott Hughesb465ab02011-08-24 11:21:21 -07002225 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002227
Elliott Hughes75770752011-08-24 17:52:38 -07002228 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002229
2230 // Do we definitely know what kind of reference this is?
2231 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2232 IndirectRefKind kind = GetIndirectRefKind(ref);
2233 switch (kind) {
2234 case kLocal:
2235 return JNILocalRefType;
2236 case kGlobal:
2237 return JNIGlobalRefType;
2238 case kWeakGlobal:
2239 return JNIWeakGlobalRefType;
2240 case kSirtOrInvalid:
2241 // Is it in a stack IRT?
2242 if (ts.Self()->SirtContains(java_object)) {
2243 return JNILocalRefType;
2244 }
2245
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002246 if (!ts.Env()->work_around_app_jni_bugs) {
2247 return JNIInvalidRefType;
2248 }
2249
Elliott Hughesb465ab02011-08-24 11:21:21 -07002250 // If we're handing out direct pointers, check whether it's a direct pointer
2251 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002252 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002253 if (ts.Env()->locals.Contains(java_object)) {
2254 return JNILocalRefType;
2255 }
2256 }
2257
2258 return JNIInvalidRefType;
2259 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002260 }
2261};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002262
Elliott Hughesa2501992011-08-26 19:39:54 -07002263const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002264 NULL, // reserved0.
2265 NULL, // reserved1.
2266 NULL, // reserved2.
2267 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 JNI::GetVersion,
2269 JNI::DefineClass,
2270 JNI::FindClass,
2271 JNI::FromReflectedMethod,
2272 JNI::FromReflectedField,
2273 JNI::ToReflectedMethod,
2274 JNI::GetSuperclass,
2275 JNI::IsAssignableFrom,
2276 JNI::ToReflectedField,
2277 JNI::Throw,
2278 JNI::ThrowNew,
2279 JNI::ExceptionOccurred,
2280 JNI::ExceptionDescribe,
2281 JNI::ExceptionClear,
2282 JNI::FatalError,
2283 JNI::PushLocalFrame,
2284 JNI::PopLocalFrame,
2285 JNI::NewGlobalRef,
2286 JNI::DeleteGlobalRef,
2287 JNI::DeleteLocalRef,
2288 JNI::IsSameObject,
2289 JNI::NewLocalRef,
2290 JNI::EnsureLocalCapacity,
2291 JNI::AllocObject,
2292 JNI::NewObject,
2293 JNI::NewObjectV,
2294 JNI::NewObjectA,
2295 JNI::GetObjectClass,
2296 JNI::IsInstanceOf,
2297 JNI::GetMethodID,
2298 JNI::CallObjectMethod,
2299 JNI::CallObjectMethodV,
2300 JNI::CallObjectMethodA,
2301 JNI::CallBooleanMethod,
2302 JNI::CallBooleanMethodV,
2303 JNI::CallBooleanMethodA,
2304 JNI::CallByteMethod,
2305 JNI::CallByteMethodV,
2306 JNI::CallByteMethodA,
2307 JNI::CallCharMethod,
2308 JNI::CallCharMethodV,
2309 JNI::CallCharMethodA,
2310 JNI::CallShortMethod,
2311 JNI::CallShortMethodV,
2312 JNI::CallShortMethodA,
2313 JNI::CallIntMethod,
2314 JNI::CallIntMethodV,
2315 JNI::CallIntMethodA,
2316 JNI::CallLongMethod,
2317 JNI::CallLongMethodV,
2318 JNI::CallLongMethodA,
2319 JNI::CallFloatMethod,
2320 JNI::CallFloatMethodV,
2321 JNI::CallFloatMethodA,
2322 JNI::CallDoubleMethod,
2323 JNI::CallDoubleMethodV,
2324 JNI::CallDoubleMethodA,
2325 JNI::CallVoidMethod,
2326 JNI::CallVoidMethodV,
2327 JNI::CallVoidMethodA,
2328 JNI::CallNonvirtualObjectMethod,
2329 JNI::CallNonvirtualObjectMethodV,
2330 JNI::CallNonvirtualObjectMethodA,
2331 JNI::CallNonvirtualBooleanMethod,
2332 JNI::CallNonvirtualBooleanMethodV,
2333 JNI::CallNonvirtualBooleanMethodA,
2334 JNI::CallNonvirtualByteMethod,
2335 JNI::CallNonvirtualByteMethodV,
2336 JNI::CallNonvirtualByteMethodA,
2337 JNI::CallNonvirtualCharMethod,
2338 JNI::CallNonvirtualCharMethodV,
2339 JNI::CallNonvirtualCharMethodA,
2340 JNI::CallNonvirtualShortMethod,
2341 JNI::CallNonvirtualShortMethodV,
2342 JNI::CallNonvirtualShortMethodA,
2343 JNI::CallNonvirtualIntMethod,
2344 JNI::CallNonvirtualIntMethodV,
2345 JNI::CallNonvirtualIntMethodA,
2346 JNI::CallNonvirtualLongMethod,
2347 JNI::CallNonvirtualLongMethodV,
2348 JNI::CallNonvirtualLongMethodA,
2349 JNI::CallNonvirtualFloatMethod,
2350 JNI::CallNonvirtualFloatMethodV,
2351 JNI::CallNonvirtualFloatMethodA,
2352 JNI::CallNonvirtualDoubleMethod,
2353 JNI::CallNonvirtualDoubleMethodV,
2354 JNI::CallNonvirtualDoubleMethodA,
2355 JNI::CallNonvirtualVoidMethod,
2356 JNI::CallNonvirtualVoidMethodV,
2357 JNI::CallNonvirtualVoidMethodA,
2358 JNI::GetFieldID,
2359 JNI::GetObjectField,
2360 JNI::GetBooleanField,
2361 JNI::GetByteField,
2362 JNI::GetCharField,
2363 JNI::GetShortField,
2364 JNI::GetIntField,
2365 JNI::GetLongField,
2366 JNI::GetFloatField,
2367 JNI::GetDoubleField,
2368 JNI::SetObjectField,
2369 JNI::SetBooleanField,
2370 JNI::SetByteField,
2371 JNI::SetCharField,
2372 JNI::SetShortField,
2373 JNI::SetIntField,
2374 JNI::SetLongField,
2375 JNI::SetFloatField,
2376 JNI::SetDoubleField,
2377 JNI::GetStaticMethodID,
2378 JNI::CallStaticObjectMethod,
2379 JNI::CallStaticObjectMethodV,
2380 JNI::CallStaticObjectMethodA,
2381 JNI::CallStaticBooleanMethod,
2382 JNI::CallStaticBooleanMethodV,
2383 JNI::CallStaticBooleanMethodA,
2384 JNI::CallStaticByteMethod,
2385 JNI::CallStaticByteMethodV,
2386 JNI::CallStaticByteMethodA,
2387 JNI::CallStaticCharMethod,
2388 JNI::CallStaticCharMethodV,
2389 JNI::CallStaticCharMethodA,
2390 JNI::CallStaticShortMethod,
2391 JNI::CallStaticShortMethodV,
2392 JNI::CallStaticShortMethodA,
2393 JNI::CallStaticIntMethod,
2394 JNI::CallStaticIntMethodV,
2395 JNI::CallStaticIntMethodA,
2396 JNI::CallStaticLongMethod,
2397 JNI::CallStaticLongMethodV,
2398 JNI::CallStaticLongMethodA,
2399 JNI::CallStaticFloatMethod,
2400 JNI::CallStaticFloatMethodV,
2401 JNI::CallStaticFloatMethodA,
2402 JNI::CallStaticDoubleMethod,
2403 JNI::CallStaticDoubleMethodV,
2404 JNI::CallStaticDoubleMethodA,
2405 JNI::CallStaticVoidMethod,
2406 JNI::CallStaticVoidMethodV,
2407 JNI::CallStaticVoidMethodA,
2408 JNI::GetStaticFieldID,
2409 JNI::GetStaticObjectField,
2410 JNI::GetStaticBooleanField,
2411 JNI::GetStaticByteField,
2412 JNI::GetStaticCharField,
2413 JNI::GetStaticShortField,
2414 JNI::GetStaticIntField,
2415 JNI::GetStaticLongField,
2416 JNI::GetStaticFloatField,
2417 JNI::GetStaticDoubleField,
2418 JNI::SetStaticObjectField,
2419 JNI::SetStaticBooleanField,
2420 JNI::SetStaticByteField,
2421 JNI::SetStaticCharField,
2422 JNI::SetStaticShortField,
2423 JNI::SetStaticIntField,
2424 JNI::SetStaticLongField,
2425 JNI::SetStaticFloatField,
2426 JNI::SetStaticDoubleField,
2427 JNI::NewString,
2428 JNI::GetStringLength,
2429 JNI::GetStringChars,
2430 JNI::ReleaseStringChars,
2431 JNI::NewStringUTF,
2432 JNI::GetStringUTFLength,
2433 JNI::GetStringUTFChars,
2434 JNI::ReleaseStringUTFChars,
2435 JNI::GetArrayLength,
2436 JNI::NewObjectArray,
2437 JNI::GetObjectArrayElement,
2438 JNI::SetObjectArrayElement,
2439 JNI::NewBooleanArray,
2440 JNI::NewByteArray,
2441 JNI::NewCharArray,
2442 JNI::NewShortArray,
2443 JNI::NewIntArray,
2444 JNI::NewLongArray,
2445 JNI::NewFloatArray,
2446 JNI::NewDoubleArray,
2447 JNI::GetBooleanArrayElements,
2448 JNI::GetByteArrayElements,
2449 JNI::GetCharArrayElements,
2450 JNI::GetShortArrayElements,
2451 JNI::GetIntArrayElements,
2452 JNI::GetLongArrayElements,
2453 JNI::GetFloatArrayElements,
2454 JNI::GetDoubleArrayElements,
2455 JNI::ReleaseBooleanArrayElements,
2456 JNI::ReleaseByteArrayElements,
2457 JNI::ReleaseCharArrayElements,
2458 JNI::ReleaseShortArrayElements,
2459 JNI::ReleaseIntArrayElements,
2460 JNI::ReleaseLongArrayElements,
2461 JNI::ReleaseFloatArrayElements,
2462 JNI::ReleaseDoubleArrayElements,
2463 JNI::GetBooleanArrayRegion,
2464 JNI::GetByteArrayRegion,
2465 JNI::GetCharArrayRegion,
2466 JNI::GetShortArrayRegion,
2467 JNI::GetIntArrayRegion,
2468 JNI::GetLongArrayRegion,
2469 JNI::GetFloatArrayRegion,
2470 JNI::GetDoubleArrayRegion,
2471 JNI::SetBooleanArrayRegion,
2472 JNI::SetByteArrayRegion,
2473 JNI::SetCharArrayRegion,
2474 JNI::SetShortArrayRegion,
2475 JNI::SetIntArrayRegion,
2476 JNI::SetLongArrayRegion,
2477 JNI::SetFloatArrayRegion,
2478 JNI::SetDoubleArrayRegion,
2479 JNI::RegisterNatives,
2480 JNI::UnregisterNatives,
2481 JNI::MonitorEnter,
2482 JNI::MonitorExit,
2483 JNI::GetJavaVM,
2484 JNI::GetStringRegion,
2485 JNI::GetStringUTFRegion,
2486 JNI::GetPrimitiveArrayCritical,
2487 JNI::ReleasePrimitiveArrayCritical,
2488 JNI::GetStringCritical,
2489 JNI::ReleaseStringCritical,
2490 JNI::NewWeakGlobalRef,
2491 JNI::DeleteWeakGlobalRef,
2492 JNI::ExceptionCheck,
2493 JNI::NewDirectByteBuffer,
2494 JNI::GetDirectBufferAddress,
2495 JNI::GetDirectBufferCapacity,
2496 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002497};
2498
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002499static const size_t kMonitorsInitial = 32; // Arbitrary.
2500static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2501
2502static const size_t kLocalsInitial = 64; // Arbitrary.
2503static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002504
Elliott Hughes75770752011-08-24 17:52:38 -07002505JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002506 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002507 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002508 local_ref_cookie(IRT_FIRST_SEGMENT),
2509 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes75770752011-08-24 17:52:38 -07002510 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002511 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002512 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002513 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002514 functions = unchecked_functions = &gNativeInterface;
2515 if (check_jni) {
2516 functions = GetCheckJniNativeInterface();
2517 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002518 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2519 // errors will ensue.
2520 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2521 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002522}
2523
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002524JNIEnvExt::~JNIEnvExt() {
2525}
2526
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002527void JNIEnvExt::DumpReferenceTables() {
2528 locals.Dump();
2529 monitors.Dump();
2530}
2531
Carl Shapiroea4dca82011-08-01 13:45:38 -07002532// JNI Invocation interface.
2533
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002534extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2535 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2536 if (args->version < JNI_VERSION_1_2) {
2537 return JNI_EVERSION;
2538 }
2539 Runtime::Options options;
2540 for (int i = 0; i < args->nOptions; ++i) {
2541 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002542 options.push_back(std::make_pair(StringPiece(option->optionString),
2543 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002544 }
2545 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002546 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002547 if (runtime == NULL) {
2548 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002549 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002550 runtime->Start();
2551 *p_env = Thread::Current()->GetJniEnv();
2552 *p_vm = runtime->GetJavaVM();
2553 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002554}
2555
Elliott Hughesf2682d52011-08-15 16:37:04 -07002556extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002557 Runtime* runtime = Runtime::Current();
2558 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002559 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002560 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002561 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002562 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002563 }
2564 return JNI_OK;
2565}
2566
2567// Historically unsupported.
2568extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2569 return JNI_ERR;
2570}
2571
Elliott Hughescdf53122011-08-19 15:46:09 -07002572class JII {
2573 public:
2574 static jint DestroyJavaVM(JavaVM* vm) {
2575 if (vm == NULL) {
2576 return JNI_ERR;
2577 } else {
2578 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2579 delete raw_vm->runtime;
2580 return JNI_OK;
2581 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002582 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002583
Elliott Hughescdf53122011-08-19 15:46:09 -07002584 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002585 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002586 }
2587
2588 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002589 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002590 }
2591
2592 static jint DetachCurrentThread(JavaVM* vm) {
2593 if (vm == NULL) {
2594 return JNI_ERR;
2595 } else {
2596 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2597 Runtime* runtime = raw_vm->runtime;
2598 runtime->DetachCurrentThread();
2599 return JNI_OK;
2600 }
2601 }
2602
2603 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2604 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2605 return JNI_EVERSION;
2606 }
2607 if (vm == NULL || env == NULL) {
2608 return JNI_ERR;
2609 }
2610 Thread* thread = Thread::Current();
2611 if (thread == NULL) {
2612 *env = NULL;
2613 return JNI_EDETACHED;
2614 }
2615 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002616 return JNI_OK;
2617 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002618};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002619
Elliott Hughesa2501992011-08-26 19:39:54 -07002620const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002621 NULL, // reserved0
2622 NULL, // reserved1
2623 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002624 JII::DestroyJavaVM,
2625 JII::AttachCurrentThread,
2626 JII::DetachCurrentThread,
2627 JII::GetEnv,
2628 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002629};
2630
Elliott Hughesbbd76712011-08-17 10:25:24 -07002631static const size_t kPinTableInitialSize = 16;
2632static const size_t kPinTableMaxSize = 1024;
2633
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002634static const size_t kGlobalsInitial = 512; // Arbitrary.
2635static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2636
2637static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2638static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2639
Elliott Hughesa0957642011-09-02 14:27:33 -07002640JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002641 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002642 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002643 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002644 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002645 verbose_jni(options->IsVerbose("jni")),
2646 log_third_party_jni(options->IsVerbose("third-party-jni")),
2647 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002648 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002649 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002650 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002651 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002652 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002653 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002654 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002655 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002656 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002657 functions = unchecked_functions = &gInvokeInterface;
2658 if (check_jni) {
2659 functions = GetCheckJniInvokeInterface();
2660 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002661}
2662
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002663JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002664 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002665}
2666
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002667void JavaVMExt::DumpReferenceTables() {
2668 {
2669 MutexLock mu(globals_lock);
2670 globals.Dump();
2671 }
2672 {
2673 MutexLock mu(weak_globals_lock);
2674 weak_globals.Dump();
2675 }
2676 {
2677 MutexLock mu(pins_lock);
2678 pin_table.Dump();
2679 }
2680}
2681
Elliott Hughes75770752011-08-24 17:52:38 -07002682bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2683 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002684
2685 // See if we've already loaded this library. If we have, and the class loader
2686 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002687 // TODO: for better results we should canonicalize the pathname (or even compare
2688 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002689 SharedLibrary* library;
2690 {
2691 // TODO: move the locking (and more of this logic) into Libraries.
2692 MutexLock mu(libraries_lock);
2693 library = libraries->Get(path);
2694 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002695 if (library != NULL) {
2696 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002697 // The library will be associated with class_loader. The JNI
2698 // spec says we can't load the same library into more than one
2699 // class loader.
2700 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2701 "ClassLoader %p; can't open in ClassLoader %p",
2702 path.c_str(), library->GetClassLoader(), class_loader);
2703 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002704 return false;
2705 }
2706 if (verbose_jni) {
2707 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2708 << "ClassLoader " << class_loader << "]";
2709 }
2710 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002711 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2712 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002713 return false;
2714 }
2715 return true;
2716 }
2717
2718 // Open the shared library. Because we're using a full path, the system
2719 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2720 // resolve this library's dependencies though.)
2721
2722 // Failures here are expected when java.library.path has several entries
2723 // and we have to hunt for the lib.
2724
2725 // The current version of the dynamic linker prints detailed information
2726 // about dlopen() failures. Some things to check if the message is
2727 // cryptic:
2728 // - make sure the library exists on the device
2729 // - verify that the right path is being opened (the debug log message
2730 // above can help with that)
2731 // - check to see if the library is valid (e.g. not zero bytes long)
2732 // - check config/prelink-linux-arm.map to ensure that the library
2733 // is listed and is not being overrun by the previous entry (if
2734 // loading suddenly stops working on a prelinked library, this is
2735 // a good one to check)
2736 // - write a trivial app that calls sleep() then dlopen(), attach
2737 // to it with "strace -p <pid>" while it sleeps, and watch for
2738 // attempts to open nonexistent dependent shared libs
2739
2740 // TODO: automate some of these checks!
2741
2742 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002743 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002744 // the GC to ignore us.
2745 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002746 void* handle = NULL;
2747 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002748 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002749 handle = dlopen(path.c_str(), RTLD_LAZY);
2750 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002751
2752 if (verbose_jni) {
2753 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2754 }
2755
2756 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002757 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002758 return false;
2759 }
2760
2761 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002762 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002763 // TODO: move the locking (and more of this logic) into Libraries.
2764 MutexLock mu(libraries_lock);
2765 library = libraries->Get(path);
2766 if (library != NULL) {
2767 LOG(INFO) << "WOW: we lost a race to add shared library: "
2768 << "\"" << path << "\" ClassLoader=" << class_loader;
2769 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002770 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002771 library = new SharedLibrary(path, handle, class_loader);
2772 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002773 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002774
2775 if (verbose_jni) {
2776 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2777 }
2778
2779 bool result = true;
2780 void* sym = dlsym(handle, "JNI_OnLoad");
2781 if (sym == NULL) {
2782 if (verbose_jni) {
2783 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2784 }
2785 } else {
2786 // Call JNI_OnLoad. We have to override the current class
2787 // loader, which will always be "null" since the stuff at the
2788 // top of the stack is around Runtime.loadLibrary(). (See
2789 // the comments in the JNI FindClass function.)
2790 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2791 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002792 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002793 self->SetClassLoaderOverride(class_loader);
2794
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002795 int version = 0;
2796 {
2797 ScopedThreadStateChange tsc(self, Thread::kNative);
2798 if (verbose_jni) {
2799 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2800 }
2801 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002802 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002803
2804 self->SetClassLoaderOverride(old_class_loader);;
2805
2806 if (version != JNI_VERSION_1_2 &&
2807 version != JNI_VERSION_1_4 &&
2808 version != JNI_VERSION_1_6) {
2809 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2810 << "bad version: " << version;
2811 // It's unwise to call dlclose() here, but we can mark it
2812 // as bad and ensure that future load attempts will fail.
2813 // We don't know how far JNI_OnLoad got, so there could
2814 // be some partially-initialized stuff accessible through
2815 // newly-registered native method calls. We could try to
2816 // unregister them, but that doesn't seem worthwhile.
2817 result = false;
2818 } else {
2819 if (verbose_jni) {
2820 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2821 << " from JNI_OnLoad in \"" << path << "\"]";
2822 }
2823 }
2824 }
2825
2826 library->SetResult(result);
2827 return result;
2828}
2829
2830void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2831 CHECK(m->IsNative());
2832
2833 Class* c = m->GetDeclaringClass();
2834
2835 // If this is a static method, it could be called before the class
2836 // has been initialized.
2837 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002838 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002839 return NULL;
2840 }
2841 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002842 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002843 }
2844
Brian Carlstrom16192862011-09-12 17:50:06 -07002845 std::string detail;
2846 void* native_method;
2847 {
2848 MutexLock mu(libraries_lock);
2849 native_method = libraries->FindNativeMethod(m, detail);
2850 }
2851 // throwing can cause libraries_lock to be reacquired
2852 if (native_method == NULL) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002853 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", "%s", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002854 }
2855 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002856}
2857
Elliott Hughes410c0c82011-09-01 17:58:25 -07002858void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2859 {
2860 MutexLock mu(globals_lock);
2861 globals.VisitRoots(visitor, arg);
2862 }
2863 {
2864 MutexLock mu(pins_lock);
2865 pin_table.VisitRoots(visitor, arg);
2866 }
2867 // The weak_globals table is visited by the GC itself (because it mutates the table).
2868}
2869
Ian Rogersdf20fe02011-07-20 20:34:16 -07002870} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002871
2872std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2873 switch (rhs) {
2874 case JNIInvalidRefType:
2875 os << "JNIInvalidRefType";
2876 return os;
2877 case JNILocalRefType:
2878 os << "JNILocalRefType";
2879 return os;
2880 case JNIGlobalRefType:
2881 os << "JNIGlobalRefType";
2882 return os;
2883 case JNIWeakGlobalRefType:
2884 os << "JNIWeakGlobalRefType";
2885 return os;
2886 }
2887}