blob: 7e75dfffe9c8b17c7aa29429f9607e7e6a4e2986 [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070015#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070016#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070018#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070021#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070022#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070023#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070024#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070026
27namespace art {
28
Elliott Hughesc5f7c912011-08-18 14:00:42 -070029/*
30 * Add a local reference for an object to the current stack frame. When
31 * the native function returns, the reference will be discarded.
32 *
33 * We need to allow the same reference to be added multiple times.
34 *
35 * This will be called on otherwise unreferenced objects. We cannot do
36 * GC allocations here, and it's best if we don't grab a mutex.
37 *
38 * Returns the local reference (currently just the same pointer that was
39 * passed in), or NULL on failure.
40 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070041template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070042T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
43 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
44 Object* obj = const_cast<Object*>(const_obj);
45
Elliott Hughesc5f7c912011-08-18 14:00:42 -070046 if (obj == NULL) {
47 return NULL;
48 }
49
Elliott Hughesbf86d042011-08-31 17:53:14 -070050 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
51 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070052
Ian Rogers5a7a74a2011-09-26 16:32:29 -070053 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070054 IndirectRef ref = locals.Add(cookie, obj);
55 if (ref == NULL) {
56 // TODO: just change Add's DCHECK to CHECK and lose this?
57 locals.Dump();
58 LOG(FATAL) << "Failed adding to JNI local reference table "
59 << "(has " << locals.Capacity() << " entries)";
60 // TODO: dvmDumpThread(dvmThreadSelf(), false);
61 }
62
63#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070064 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070065 size_t entry_count = locals.Capacity();
66 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070067 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070068 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070069 locals.Dump();
70 // TODO: dvmDumpThread(dvmThreadSelf(), false);
71 // dvmAbort();
72 }
73 }
74#endif
75
Elliott Hughesbf86d042011-08-31 17:53:14 -070076 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070077 // Hand out direct pointers to support broken old apps.
78 return reinterpret_cast<T>(obj);
79 }
80
81 return reinterpret_cast<T>(ref);
82}
83
Elliott Hughesbf86d042011-08-31 17:53:14 -070084// For external use.
85template<typename T>
86T Decode(JNIEnv* public_env, jobject obj) {
87 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
88 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
89}
90// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -070091template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070092template Class* Decode<Class*>(JNIEnv*, jobject);
93template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
94template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -070095template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
96template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070097template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -040098template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -070099template String* Decode<String*>(JNIEnv*, jobject);
100template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700101
102namespace {
103
Elliott Hughescdf53122011-08-19 15:46:09 -0700104jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
105 if (obj == NULL) {
106 return NULL;
107 }
Elliott Hughes75770752011-08-24 17:52:38 -0700108 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700109 IndirectReferenceTable& weak_globals = vm->weak_globals;
110 MutexLock mu(vm->weak_globals_lock);
111 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
112 return reinterpret_cast<jweak>(ref);
113}
114
Elliott Hughesbf86d042011-08-31 17:53:14 -0700115// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700116template<typename T>
117T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700118 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700119}
120
Elliott Hughes418d20f2011-09-22 14:00:39 -0700121byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
122 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700123 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700124 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700125 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700126 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
127 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700128 case 'Z':
129 case 'B':
130 case 'C':
131 case 'S':
132 case 'I':
133 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
134 offset += 4;
135 break;
136 case 'F':
137 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
138 offset += 4;
139 break;
140 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700141 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700142 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
143 offset += sizeof(Object*);
144 break;
145 }
146 case 'D':
147 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
148 offset += 8;
149 break;
150 case 'J':
151 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
152 offset += 8;
153 break;
154 }
155 }
156 return arg_array.release();
157}
158
Elliott Hughes418d20f2011-09-22 14:00:39 -0700159byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
160 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700161 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700162 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700163 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700164 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
165 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700166 case 'Z':
167 case 'B':
168 case 'C':
169 case 'S':
170 case 'I':
171 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
172 offset += 4;
173 break;
174 case 'F':
175 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
176 offset += 4;
177 break;
178 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700179 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700180 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
181 offset += sizeof(Object*);
182 break;
183 }
184 case 'D':
185 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
186 offset += 8;
187 break;
188 case 'J':
189 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
190 offset += 8;
191 break;
192 }
193 }
194 return arg_array.release();
195}
196
Elliott Hughes418d20f2011-09-22 14:00:39 -0700197JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
198 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700199 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700200 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700201 return result;
202}
203
Elliott Hughes418d20f2011-09-22 14:00:39 -0700204JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
205 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
206 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700207 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700208 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
209 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700210}
211
Elliott Hughes75770752011-08-24 17:52:38 -0700212Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700213 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700214}
215
Elliott Hughes418d20f2011-09-22 14:00:39 -0700216JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
217 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
218 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700219 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700220 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
221 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700222}
223
Elliott Hughes418d20f2011-09-22 14:00:39 -0700224JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
225 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
226 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700227 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700228 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
229 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700230}
231
Elliott Hughes6b436852011-08-12 10:16:44 -0700232// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
233// separated with slashes but aren't wrapped with "L;" like regular descriptors
234// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
235// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
236// supported names with dots too (such as "a.b.C").
237std::string NormalizeJniClassDescriptor(const char* name) {
238 std::string result;
239 // Add the missing "L;" if necessary.
240 if (name[0] == '[') {
241 result = name;
242 } else {
243 result += 'L';
244 result += name;
245 result += ';';
246 }
247 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700248 if (result.find('.') != std::string::npos) {
249 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
250 << "\"" << name << "\"";
251 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700252 }
253 return result;
254}
255
Elliott Hughes14134a12011-09-30 16:55:51 -0700256void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
257 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700258 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes14134a12011-09-30 16:55:51 -0700259 "no %s method \"%s.%s%s\"", kind, class_descriptor.c_str(), name, sig);
260}
261
Elliott Hughescdf53122011-08-19 15:46:09 -0700262jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
263 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700264 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700265 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700266 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700267
268 Method* method = NULL;
269 if (is_static) {
270 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700271 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700272 method = c->FindVirtualMethod(name, sig);
273 if (method == NULL) {
274 // No virtual method matching the signature. Search declared
275 // private methods and constructors.
276 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700277 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700278 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700279
Elliott Hughescdf53122011-08-19 15:46:09 -0700280 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700281 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700282 return NULL;
283 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700284
Elliott Hughes418d20f2011-09-22 14:00:39 -0700285 method->InitJavaFields();
286
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700287 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700288}
289
Elliott Hughescdf53122011-08-19 15:46:09 -0700290jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
291 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700292 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700293 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700294 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700295
296 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700297 Class* field_type;
298 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
299 if (sig[1] != '\0') {
300 // TODO: need to get the appropriate ClassLoader.
301 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
302 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700303 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700305 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 if (field_type == NULL) {
307 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700308 DCHECK(ts.Self()->IsExceptionPending());
309 ts.Self()->ClearException();
310 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700311 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700312 "no type \"%s\" found and so no field \"%s\" could be found in class "
313 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700314 return NULL;
315 }
316 if (is_static) {
317 field = c->FindStaticField(name, field_type);
318 } else {
319 field = c->FindInstanceField(name, field_type);
320 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700321 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700322 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700323 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700324 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700325 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700326 return NULL;
327 }
Elliott Hughes80609252011-09-23 17:24:51 -0700328 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700329 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700330}
331
Elliott Hughes75770752011-08-24 17:52:38 -0700332void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
333 JavaVMExt* vm = ts.Vm();
334 MutexLock mu(vm->pins_lock);
335 vm->pin_table.Add(array);
336}
337
338void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
339 JavaVMExt* vm = ts.Vm();
340 MutexLock mu(vm->pins_lock);
341 vm->pin_table.Remove(array);
342}
343
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700344template<typename JniT, typename ArtT>
345JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
346 CHECK_GE(length, 0); // TODO: ReportJniError
347 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700348 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700349}
350
Elliott Hughes75770752011-08-24 17:52:38 -0700351template <typename ArrayT, typename CArrayT, typename ArtArrayT>
352CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
353 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
354 PinPrimitiveArray(ts, array);
355 if (is_copy != NULL) {
356 *is_copy = JNI_FALSE;
357 }
358 return array->GetData();
359}
360
361template <typename ArrayT>
362void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
363 if (mode != JNI_COMMIT) {
364 Array* array = Decode<Array*>(ts, java_array);
365 UnpinPrimitiveArray(ts, array);
366 }
367}
368
Elliott Hughes814e4032011-08-23 12:07:56 -0700369void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700370 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700371 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700372 "%s offset=%d length=%d %s.length=%d",
373 type.c_str(), start, length, identifier, array->GetLength());
374}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700375void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700376 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700377 "offset=%d length=%d string.length()=%d", start, length, array_length);
378}
Elliott Hughes814e4032011-08-23 12:07:56 -0700379
380template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700381void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700382 ArrayT* array = Decode<ArrayT*>(ts, java_array);
383 if (start < 0 || length < 0 || start + length > array->GetLength()) {
384 ThrowAIOOBE(ts, array, start, length, "src");
385 } else {
386 JavaT* data = array->GetData();
387 memcpy(buf, data + start, length * sizeof(JavaT));
388 }
389}
390
391template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700392void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700393 ArrayT* array = Decode<ArrayT*>(ts, java_array);
394 if (start < 0 || length < 0 || start + length > array->GetLength()) {
395 ThrowAIOOBE(ts, array, start, length, "dst");
396 } else {
397 JavaT* data = array->GetData();
398 memcpy(data + start, buf, length * sizeof(JavaT));
399 }
400}
401
Elliott Hughes75770752011-08-24 17:52:38 -0700402jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700403 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
404 CHECK(buffer_class.get() != NULL);
405 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
406}
407
Elliott Hughes75770752011-08-24 17:52:38 -0700408jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700409 static jclass buffer_class = InitDirectByteBufferClass(env);
410 return buffer_class;
411}
412
Elliott Hughes75770752011-08-24 17:52:38 -0700413jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
414 if (vm == NULL || p_env == NULL) {
415 return JNI_ERR;
416 }
417
418 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
419 JavaVMAttachArgs args;
420 if (thr_args == NULL) {
421 // Allow the v1.1 calling convention.
422 args.version = JNI_VERSION_1_2;
423 args.name = NULL;
424 args.group = NULL; // TODO: get "main" thread group
425 } else {
426 args.version = in_args->version;
427 args.name = in_args->name;
428 if (in_args->group != NULL) {
429 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
430 args.group = NULL; // TODO: decode in_args->group
431 } else {
432 args.group = NULL; // TODO: get "main" thread group
433 }
434 }
435 CHECK_GE(args.version, JNI_VERSION_1_2);
436
437 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700438 runtime->AttachCurrentThread(args.name, as_daemon);
439 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700440 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700441}
442
Elliott Hughes79082e32011-08-25 12:07:32 -0700443class SharedLibrary {
444 public:
445 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
446 : path_(path),
447 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700448 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700449 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700450 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700451 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700452 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700453 }
454
Elliott Hughes79082e32011-08-25 12:07:32 -0700455 Object* GetClassLoader() {
456 return class_loader_;
457 }
458
459 std::string GetPath() {
460 return path_;
461 }
462
463 /*
464 * Check the result of an earlier call to JNI_OnLoad on this library. If
465 * the call has not yet finished in another thread, wait for it.
466 */
467 bool CheckOnLoadResult(JavaVMExt* vm) {
468 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700469 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700470 // Check this so we don't end up waiting for ourselves. We need
471 // to return "true" so the caller can continue.
472 LOG(INFO) << *self << " recursive attempt to load library "
473 << "\"" << path_ << "\"";
474 return true;
475 }
476
477 MutexLock mu(jni_on_load_lock_);
478 while (jni_on_load_result_ == kPending) {
479 if (vm->verbose_jni) {
480 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
481 << "JNI_OnLoad...]";
482 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700483 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700484 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 }
486
487 bool okay = (jni_on_load_result_ == kOkay);
488 if (vm->verbose_jni) {
489 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
490 << (okay ? "succeeded" : "failed") << "]";
491 }
492 return okay;
493 }
494
495 void SetResult(bool result) {
496 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700497 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700498
499 // Broadcast a wakeup to anybody sleeping on the condition variable.
500 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700501 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700502 }
503
504 void* FindSymbol(const std::string& symbol_name) {
505 return dlsym(handle_, symbol_name.c_str());
506 }
507
508 private:
509 enum JNI_OnLoadState {
510 kPending,
511 kFailed,
512 kOkay,
513 };
514
515 // Path to library "/system/lib/libjni.so".
516 std::string path_;
517
518 // The void* returned by dlopen(3).
519 void* handle_;
520
521 // The ClassLoader this library is associated with.
522 Object* class_loader_;
523
524 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700525 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700526 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700527 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700528 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700529 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700530 // Result of earlier JNI_OnLoad call.
531 JNI_OnLoadState jni_on_load_result_;
532};
533
Elliott Hughescdf53122011-08-19 15:46:09 -0700534} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700535
Elliott Hughes79082e32011-08-25 12:07:32 -0700536// This exists mainly to keep implementation details out of the header file.
537class Libraries {
538 public:
539 Libraries() {
540 }
541
542 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700543 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700544 }
545
546 SharedLibrary* Get(const std::string& path) {
547 return libraries_[path];
548 }
549
550 void Put(const std::string& path, SharedLibrary* library) {
551 libraries_[path] = library;
552 }
553
554 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700555 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700556 std::string jni_short_name(JniShortName(m));
557 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700558 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700559 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
560 SharedLibrary* library = it->second;
561 if (library->GetClassLoader() != declaring_class_loader) {
562 // We only search libraries loaded by the appropriate ClassLoader.
563 continue;
564 }
565 // Try the short name then the long name...
566 void* fn = library->FindSymbol(jni_short_name);
567 if (fn == NULL) {
568 fn = library->FindSymbol(jni_long_name);
569 }
570 if (fn != NULL) {
571 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700572 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700573 << " in \"" << library->GetPath() << "\"]";
574 }
575 return fn;
576 }
577 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700578 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700579 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700580 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700581 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700582 return NULL;
583 }
584
585 private:
586 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
587
588 std::map<std::string, SharedLibrary*> libraries_;
589};
590
Elliott Hughes418d20f2011-09-22 14:00:39 -0700591JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
592 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
593 Object* receiver = Decode<Object*>(env, obj);
594 Method* method = DecodeMethod(mid);
595 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
596 return InvokeWithArgArray(env, receiver, method, arg_array.get());
597}
598
Elliott Hughescdf53122011-08-19 15:46:09 -0700599class JNI {
600 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700601
Elliott Hughescdf53122011-08-19 15:46:09 -0700602 static jint GetVersion(JNIEnv* env) {
603 ScopedJniThreadState ts(env);
604 return JNI_VERSION_1_6;
605 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700606
Elliott Hughescdf53122011-08-19 15:46:09 -0700607 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
608 ScopedJniThreadState ts(env);
609 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700610 return NULL;
611 }
612
Elliott Hughescdf53122011-08-19 15:46:09 -0700613 static jclass FindClass(JNIEnv* env, const char* name) {
614 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700615 Runtime* runtime = Runtime::Current();
616 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700617 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700618 Class* c = NULL;
619 if (runtime->IsStarted()) {
620 // TODO: need to get the appropriate ClassLoader.
621 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
622 c = class_linker->FindClass(descriptor, cl);
623 } else {
624 c = class_linker->FindSystemClass(descriptor);
625 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700626 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700627 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700628
Elliott Hughescdf53122011-08-19 15:46:09 -0700629 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
630 ScopedJniThreadState ts(env);
631 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700632 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700633 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700634
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
636 ScopedJniThreadState ts(env);
637 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700638 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700640
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
642 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700643 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700644 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700646
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
648 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700649 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700650 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700652
Elliott Hughes37f7a402011-08-22 18:56:01 -0700653 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
654 ScopedJniThreadState ts(env);
655 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700656 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700657 }
658
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700659 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700661 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700662 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700664
Elliott Hughes37f7a402011-08-22 18:56:01 -0700665 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700667 Class* c1 = Decode<Class*>(ts, java_class1);
668 Class* c2 = Decode<Class*>(ts, java_class2);
669 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700671
Elliott Hughes37f7a402011-08-22 18:56:01 -0700672 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700674 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700675 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700676 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700677 return JNI_TRUE;
678 } else {
679 Object* obj = Decode<Object*>(ts, jobj);
680 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700681 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700682 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700684
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 static jint Throw(JNIEnv* env, jthrowable java_exception) {
686 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700687 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 if (exception == NULL) {
689 return JNI_ERR;
690 }
691 ts.Self()->SetException(exception);
692 return JNI_OK;
693 }
694
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700695 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700696 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700697 // TODO: check for a pending exception to decide what constructor to call.
698 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
699 if (mid == NULL) {
700 return JNI_ERR;
701 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700702 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700703 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700704 return JNI_ERR;
705 }
706
707 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700708 args[0].l = s.get();
709 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
710 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700711 return JNI_ERR;
712 }
713
Elliott Hughes72025e52011-08-23 17:50:30 -0700714 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700715
Elliott Hughes37f7a402011-08-22 18:56:01 -0700716 return JNI_OK;
717 }
718
719 static jboolean ExceptionCheck(JNIEnv* env) {
720 ScopedJniThreadState ts(env);
721 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
722 }
723
724 static void ExceptionClear(JNIEnv* env) {
725 ScopedJniThreadState ts(env);
726 ts.Self()->ClearException();
727 }
728
729 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700731
732 Thread* self = ts.Self();
733 Throwable* original_exception = self->GetException();
734 self->ClearException();
735
Elliott Hughesbf86d042011-08-31 17:53:14 -0700736 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700737 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
738 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
739 if (mid == NULL) {
740 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700741 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700742 } else {
743 env->CallVoidMethod(exception.get(), mid);
744 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700745 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700746 << " thrown while calling printStackTrace";
747 self->ClearException();
748 }
749 }
750
751 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700753
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 static jthrowable ExceptionOccurred(JNIEnv* env) {
755 ScopedJniThreadState ts(env);
756 Object* exception = ts.Self()->GetException();
757 if (exception == NULL) {
758 return NULL;
759 } else {
760 // TODO: if adding a local reference failing causes the VM to abort
761 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700762 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700763 if (localException == NULL) {
764 // We were unable to add a new local reference, and threw a new
765 // exception. We can't return "exception", because it's not a
766 // local reference. So we have to return NULL, indicating that
767 // there was no exception, even though it's pretty much raining
768 // exceptions in here.
769 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
770 }
771 return localException;
772 }
773 }
774
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 static void FatalError(JNIEnv* env, const char* msg) {
776 ScopedJniThreadState ts(env);
777 LOG(FATAL) << "JNI FatalError called: " << msg;
778 }
779
780 static jint PushLocalFrame(JNIEnv* env, jint cap) {
781 ScopedJniThreadState ts(env);
782 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
783 return JNI_OK;
784 }
785
786 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
787 ScopedJniThreadState ts(env);
788 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
789 return res;
790 }
791
Elliott Hughes72025e52011-08-23 17:50:30 -0700792 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
793 ScopedJniThreadState ts(env);
794 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
795 return 0;
796 }
797
Elliott Hughescdf53122011-08-19 15:46:09 -0700798 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
799 ScopedJniThreadState ts(env);
800 if (obj == NULL) {
801 return NULL;
802 }
803
Elliott Hughes75770752011-08-24 17:52:38 -0700804 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 IndirectReferenceTable& globals = vm->globals;
806 MutexLock mu(vm->globals_lock);
807 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
808 return reinterpret_cast<jobject>(ref);
809 }
810
811 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
812 ScopedJniThreadState ts(env);
813 if (obj == NULL) {
814 return;
815 }
816
Elliott Hughes75770752011-08-24 17:52:38 -0700817 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700818 IndirectReferenceTable& globals = vm->globals;
819 MutexLock mu(vm->globals_lock);
820
821 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
822 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
823 << "failed to find entry";
824 }
825 }
826
827 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
828 ScopedJniThreadState ts(env);
829 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
830 }
831
832 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
833 ScopedJniThreadState ts(env);
834 if (obj == NULL) {
835 return;
836 }
837
Elliott Hughes75770752011-08-24 17:52:38 -0700838 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 IndirectReferenceTable& weak_globals = vm->weak_globals;
840 MutexLock mu(vm->weak_globals_lock);
841
842 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
843 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
844 << "failed to find entry";
845 }
846 }
847
848 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
849 ScopedJniThreadState ts(env);
850 if (obj == NULL) {
851 return NULL;
852 }
853
854 IndirectReferenceTable& locals = ts.Env()->locals;
855
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700856 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
858 return reinterpret_cast<jobject>(ref);
859 }
860
861 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
862 ScopedJniThreadState ts(env);
863 if (obj == NULL) {
864 return;
865 }
866
867 IndirectReferenceTable& locals = ts.Env()->locals;
868
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700869 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 if (!locals.Remove(cookie, obj)) {
871 // Attempting to delete a local reference that is not in the
872 // topmost local reference frame is a no-op. DeleteLocalRef returns
873 // void and doesn't throw any exceptions, but we should probably
874 // complain about it so the user will notice that things aren't
875 // going quite the way they expect.
876 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
877 << "failed to find entry";
878 }
879 }
880
881 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
882 ScopedJniThreadState ts(env);
883 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
884 ? JNI_TRUE : JNI_FALSE;
885 }
886
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700887 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700889 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700890 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700891 return NULL;
892 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700893 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700894 }
895
Elliott Hughes72025e52011-08-23 17:50:30 -0700896 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 ScopedJniThreadState ts(env);
898 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 va_start(args, mid);
900 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 va_end(args);
902 return result;
903 }
904
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700907 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700908 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700909 return NULL;
910 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700911 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700912 if (result == NULL) {
913 return NULL;
914 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700915 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700916 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 return local_result;
918 }
919
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700922 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700923 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700924 return NULL;
925 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700926 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700927 if (result == NULL) {
928 return NULL;
929 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700930 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700932 return local_result;
933 }
934
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
936 ScopedJniThreadState ts(env);
937 return FindMethodID(ts, c, name, sig, false);
938 }
939
940 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
941 ScopedJniThreadState ts(env);
942 return FindMethodID(ts, c, name, sig, true);
943 }
944
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700946 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 va_list ap;
948 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700949 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700951 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 }
953
Elliott Hughes72025e52011-08-23 17:50:30 -0700954 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700956 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700957 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700962 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700963 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 }
965
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700968 va_list ap;
969 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700970 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 va_end(ap);
972 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700977 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 }
979
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700982 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 }
984
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 va_list ap;
988 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700989 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 va_end(ap);
991 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 }
993
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700996 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001001 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 va_list ap;
1007 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001008 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 va_end(ap);
1010 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 }
1012
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001015 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001020 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 va_list ap;
1026 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001027 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 va_end(ap);
1029 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001034 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001039 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 va_list ap;
1045 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001046 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 va_end(ap);
1048 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 }
1050
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001053 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001058 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_list ap;
1064 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001065 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 va_end(ap);
1067 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001072 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001077 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 va_list ap;
1083 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001084 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 va_end(ap);
1086 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001091 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001096 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_list ap;
1102 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001103 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 va_end(ap);
1105 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001110 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001115 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 va_list ap;
1121 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001122 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001128 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 }
1130
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001133 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
1136 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 ScopedJniThreadState ts(env);
1139 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001141 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001142 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 va_end(ap);
1144 return local_result;
1145 }
1146
1147 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001150 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001151 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 }
1153
1154 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001157 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001158 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
1161 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 ScopedJniThreadState ts(env);
1164 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001166 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 va_end(ap);
1168 return result.z;
1169 }
1170
1171 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001174 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
1177 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001180 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 }
1182
1183 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 ScopedJniThreadState ts(env);
1186 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001188 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 va_end(ap);
1190 return result.b;
1191 }
1192
1193 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001196 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
1199 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001202 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
1205 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 ScopedJniThreadState ts(env);
1208 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001210 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 va_end(ap);
1212 return result.c;
1213 }
1214
1215 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001218 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
1221 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001224 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
1226
1227 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 ScopedJniThreadState ts(env);
1230 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001232 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 va_end(ap);
1234 return result.s;
1235 }
1236
1237 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001240 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
1243 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001244 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001246 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
Elliott Hughes418d20f2011-09-22 14:00:39 -07001249 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 ScopedJniThreadState ts(env);
1251 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001253 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 va_end(ap);
1255 return result.i;
1256 }
1257
1258 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001261 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001267 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
1270 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 ScopedJniThreadState ts(env);
1273 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001275 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 va_end(ap);
1277 return result.j;
1278 }
1279
1280 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001283 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
1286 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001289 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
1292 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 ScopedJniThreadState ts(env);
1295 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001297 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_end(ap);
1299 return result.f;
1300 }
1301
1302 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001305 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
1308 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001311 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
1314 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 ScopedJniThreadState ts(env);
1317 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001319 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 va_end(ap);
1321 return result.d;
1322 }
1323
1324 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001327 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
1330 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001333 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
1336 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 ScopedJniThreadState ts(env);
1339 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001340 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001341 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 va_end(ap);
1343 }
1344
1345 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001346 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001348 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
1351 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001352 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001354 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001357 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 ScopedJniThreadState ts(env);
1359 return FindFieldID(ts, c, name, sig, false);
1360 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001361
1362
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001363 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 ScopedJniThreadState ts(env);
1365 return FindFieldID(ts, c, name, sig, true);
1366 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001367
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001368 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001370 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001371 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001372 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001374
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001375 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001377 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001378 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
1380
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001383 Object* o = Decode<Object*>(ts, java_object);
1384 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001385 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001391 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001392 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001393 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396#define GET_PRIMITIVE_FIELD(fn, instance) \
1397 ScopedJniThreadState ts(env); \
1398 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001399 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 return f->fn(o)
1401
1402#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1403 ScopedJniThreadState ts(env); \
1404 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001405 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001406 f->fn(o, value)
1407
1408 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1409 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001412 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1413 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001416 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1417 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001420 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1421 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001424 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1425 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001428 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1429 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1433 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1437 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1441 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001444 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1445 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1449 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1453 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1457 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1461 GET_PRIMITIVE_FIELD(GetLong, NULL);
1462 }
1463
1464 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1465 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1466 }
1467
1468 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1469 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1470 }
1471
1472 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1473 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1474 }
1475
1476 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1477 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1478 }
1479
1480 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1481 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1482 }
1483
1484 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1485 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1486 }
1487
1488 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1489 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1490 }
1491
1492 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1493 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1494 }
1495
1496 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1497 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1498 }
1499
1500 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1501 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1502 }
1503
1504 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1505 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1506 }
1507
1508 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1509 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1510 }
1511
1512 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1513 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1514 }
1515
1516 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1517 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1518 }
1519
1520 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1521 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1522 }
1523
1524 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1525 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1526 }
1527
1528 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1529 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1530 }
1531
1532 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1533 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes418d20f2011-09-22 14:00:39 -07001536 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 ScopedJniThreadState ts(env);
1538 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001539 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001540 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001541 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 va_end(ap);
1543 return local_result;
1544 }
1545
Elliott Hughes418d20f2011-09-22 14:00:39 -07001546 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001548 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001549 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 }
1551
Elliott Hughes418d20f2011-09-22 14:00:39 -07001552 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001554 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001555 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 }
1557
Elliott Hughes418d20f2011-09-22 14:00:39 -07001558 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 ScopedJniThreadState ts(env);
1560 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001561 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001562 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 va_end(ap);
1564 return result.z;
1565 }
1566
Elliott Hughes418d20f2011-09-22 14:00:39 -07001567 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001569 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes418d20f2011-09-22 14:00:39 -07001572 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001574 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes72025e52011-08-23 17:50:30 -07001577 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 ScopedJniThreadState ts(env);
1579 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001580 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001581 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 va_end(ap);
1583 return result.b;
1584 }
1585
Elliott Hughes418d20f2011-09-22 14:00:39 -07001586 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001588 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes418d20f2011-09-22 14:00:39 -07001591 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001593 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes72025e52011-08-23 17:50:30 -07001596 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 ScopedJniThreadState ts(env);
1598 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001599 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001600 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 va_end(ap);
1602 return result.c;
1603 }
1604
Elliott Hughes418d20f2011-09-22 14:00:39 -07001605 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001607 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 }
1609
Elliott Hughes418d20f2011-09-22 14:00:39 -07001610 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001612 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 ScopedJniThreadState ts(env);
1617 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001618 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001619 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 va_end(ap);
1621 return result.s;
1622 }
1623
Elliott Hughes418d20f2011-09-22 14:00:39 -07001624 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001626 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
Elliott Hughes418d20f2011-09-22 14:00:39 -07001629 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001631 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 }
1633
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 ScopedJniThreadState ts(env);
1636 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001637 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001638 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 va_end(ap);
1640 return result.i;
1641 }
1642
Elliott Hughes418d20f2011-09-22 14:00:39 -07001643 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001645 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 }
1647
Elliott Hughes418d20f2011-09-22 14:00:39 -07001648 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001650 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
1652
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 ScopedJniThreadState ts(env);
1655 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001656 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001657 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 va_end(ap);
1659 return result.j;
1660 }
1661
Elliott Hughes418d20f2011-09-22 14:00:39 -07001662 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001664 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
Elliott Hughes418d20f2011-09-22 14:00:39 -07001667 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001669 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 }
1671
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 ScopedJniThreadState ts(env);
1674 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001675 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001676 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 va_end(ap);
1678 return result.f;
1679 }
1680
Elliott Hughes418d20f2011-09-22 14:00:39 -07001681 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001683 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
Elliott Hughes418d20f2011-09-22 14:00:39 -07001686 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001688 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 }
1690
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 ScopedJniThreadState ts(env);
1693 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001694 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001695 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 va_end(ap);
1697 return result.d;
1698 }
1699
Elliott Hughes418d20f2011-09-22 14:00:39 -07001700 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001702 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 }
1704
Elliott Hughes418d20f2011-09-22 14:00:39 -07001705 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001707 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 }
1709
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 ScopedJniThreadState ts(env);
1712 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001713 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001714 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 va_end(ap);
1716 }
1717
Elliott Hughes418d20f2011-09-22 14:00:39 -07001718 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001720 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 }
1722
Elliott Hughes418d20f2011-09-22 14:00:39 -07001723 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001725 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 }
1727
Elliott Hughes814e4032011-08-23 12:07:56 -07001728 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001730 if (chars == NULL && char_count == 0) {
1731 return NULL;
1732 }
1733 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001734 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 }
1736
1737 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1738 ScopedJniThreadState ts(env);
1739 if (utf == NULL) {
1740 return NULL;
1741 }
1742 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001743 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
Elliott Hughes814e4032011-08-23 12:07:56 -07001746 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1747 ScopedJniThreadState ts(env);
1748 return Decode<String*>(ts, java_string)->GetLength();
1749 }
1750
1751 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1752 ScopedJniThreadState ts(env);
1753 return Decode<String*>(ts, java_string)->GetUtfLength();
1754 }
1755
Elliott Hughesb465ab02011-08-24 11:21:21 -07001756 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001758 String* s = Decode<String*>(ts, java_string);
1759 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1760 ThrowSIOOBE(ts, start, length, s->GetLength());
1761 } else {
1762 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1763 memcpy(buf, chars + start, length * sizeof(jchar));
1764 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001765 }
1766
Elliott Hughesb465ab02011-08-24 11:21:21 -07001767 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001768 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001769 String* s = Decode<String*>(ts, java_string);
1770 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1771 ThrowSIOOBE(ts, start, length, s->GetLength());
1772 } else {
1773 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1774 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1775 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001776 }
1777
Elliott Hughes75770752011-08-24 17:52:38 -07001778 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001779 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001780 String* s = Decode<String*>(ts, java_string);
1781 const CharArray* chars = s->GetCharArray();
1782 PinPrimitiveArray(ts, chars);
1783 if (is_copy != NULL) {
1784 *is_copy = JNI_FALSE;
1785 }
1786 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001787 }
1788
Elliott Hughes75770752011-08-24 17:52:38 -07001789 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001790 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001791 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 }
1793
Elliott Hughes75770752011-08-24 17:52:38 -07001794 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001796 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Elliott Hughes75770752011-08-24 17:52:38 -07001799 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001801 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 }
1803
Elliott Hughes75770752011-08-24 17:52:38 -07001804 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001805 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001806 if (java_string == NULL) {
1807 return NULL;
1808 }
1809 if (is_copy != NULL) {
1810 *is_copy = JNI_TRUE;
1811 }
1812 String* s = Decode<String*>(ts, java_string);
1813 size_t byte_count = s->GetUtfLength();
1814 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001815 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001816 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1817 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1818 bytes[byte_count] = '\0';
1819 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001820 }
1821
Elliott Hughes75770752011-08-24 17:52:38 -07001822 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001823 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001824 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001825 }
1826
Elliott Hughesbd935992011-08-22 11:59:34 -07001827 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001829 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001830 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001831 Array* array = obj->AsArray();
1832 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 }
1834
Elliott Hughes814e4032011-08-23 12:07:56 -07001835 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001837 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001838 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 }
1840
1841 static void SetObjectArrayElement(JNIEnv* env,
1842 jobjectArray java_array, jsize index, jobject java_value) {
1843 ScopedJniThreadState ts(env);
1844 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1845 Object* value = Decode<Object*>(ts, java_value);
1846 array->Set(index, value);
1847 }
1848
1849 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1850 ScopedJniThreadState ts(env);
1851 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1852 }
1853
1854 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1855 ScopedJniThreadState ts(env);
1856 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1857 }
1858
1859 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1860 ScopedJniThreadState ts(env);
1861 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1862 }
1863
1864 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1865 ScopedJniThreadState ts(env);
1866 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1867 }
1868
1869 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1870 ScopedJniThreadState ts(env);
1871 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1872 }
1873
1874 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1875 ScopedJniThreadState ts(env);
1876 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1877 }
1878
1879 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1880 ScopedJniThreadState ts(env);
1881 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1882 }
1883
1884 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1885 ScopedJniThreadState ts(env);
1886 CHECK_GE(length, 0); // TODO: ReportJniError
1887
1888 // Compute the array class corresponding to the given element class.
1889 Class* element_class = Decode<Class*>(ts, element_jclass);
1890 std::string descriptor;
1891 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001892 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001893
1894 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001895 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1896 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001897 return NULL;
1898 }
1899
Elliott Hughes75770752011-08-24 17:52:38 -07001900 // Allocate and initialize if necessary.
1901 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001902 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001903 if (initial_element != NULL) {
1904 Object* initial_object = Decode<Object*>(ts, initial_element);
1905 for (jsize i = 0; i < length; ++i) {
1906 result->Set(i, initial_object);
1907 }
1908 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001909 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 }
1911
1912 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1913 ScopedJniThreadState ts(env);
1914 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1915 }
1916
Elliott Hughes75770752011-08-24 17:52:38 -07001917 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001918 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001919 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001920 }
1921
Elliott Hughes75770752011-08-24 17:52:38 -07001922 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001923 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001924 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001925 }
1926
Elliott Hughes75770752011-08-24 17:52:38 -07001927 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001929 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001930 }
1931
Elliott Hughes75770752011-08-24 17:52:38 -07001932 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001934 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes75770752011-08-24 17:52:38 -07001937 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001939 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
Elliott Hughes75770752011-08-24 17:52:38 -07001942 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001944 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes75770752011-08-24 17:52:38 -07001947 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001949 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 }
1951
Elliott Hughes75770752011-08-24 17:52:38 -07001952 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001954 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001955 }
1956
Elliott Hughes75770752011-08-24 17:52:38 -07001957 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001959 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
Elliott Hughes75770752011-08-24 17:52:38 -07001962 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001964 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 }
1966
Elliott Hughes75770752011-08-24 17:52:38 -07001967 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001969 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Elliott Hughes75770752011-08-24 17:52:38 -07001972 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001974 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
Elliott Hughes75770752011-08-24 17:52:38 -07001977 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001979 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
Elliott Hughes75770752011-08-24 17:52:38 -07001982 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001984 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 }
1986
Elliott Hughes75770752011-08-24 17:52:38 -07001987 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001989 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Elliott Hughes75770752011-08-24 17:52:38 -07001992 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001994 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
Elliott Hughes75770752011-08-24 17:52:38 -07001997 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001999 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
Elliott Hughes75770752011-08-24 17:52:38 -07002002 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002004 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 }
2006
Elliott Hughes814e4032011-08-23 12:07:56 -07002007 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002009 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Elliott Hughes814e4032011-08-23 12:07:56 -07002012 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002014 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 }
2016
Elliott Hughes814e4032011-08-23 12:07:56 -07002017 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002019 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
Elliott Hughes814e4032011-08-23 12:07:56 -07002022 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002024 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 }
2026
Elliott Hughes814e4032011-08-23 12:07:56 -07002027 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002029 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Elliott Hughes814e4032011-08-23 12:07:56 -07002032 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002034 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Elliott Hughes814e4032011-08-23 12:07:56 -07002037 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002039 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Elliott Hughes814e4032011-08-23 12:07:56 -07002042 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002044 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Elliott Hughes814e4032011-08-23 12:07:56 -07002047 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002049 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Elliott Hughes814e4032011-08-23 12:07:56 -07002052 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002054 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes814e4032011-08-23 12:07:56 -07002057 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Elliott Hughes814e4032011-08-23 12:07:56 -07002062 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes814e4032011-08-23 12:07:56 -07002067 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Elliott Hughes814e4032011-08-23 12:07:56 -07002072 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes814e4032011-08-23 12:07:56 -07002077 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Elliott Hughes814e4032011-08-23 12:07:56 -07002082 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Elliott Hughes5174fe62011-08-23 15:12:35 -07002087 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002089 Class* c = Decode<Class*>(ts, java_class);
2090
Elliott Hughes5174fe62011-08-23 15:12:35 -07002091 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 const char* name = methods[i].name;
2093 const char* sig = methods[i].signature;
2094
2095 if (*sig == '!') {
2096 // TODO: fast jni. it's too noisy to log all these.
2097 ++sig;
2098 }
2099
Elliott Hughes5174fe62011-08-23 15:12:35 -07002100 Method* m = c->FindDirectMethod(name, sig);
2101 if (m == NULL) {
2102 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002104 if (m == NULL) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002105 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002107 } else if (!m->IsNative()) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002108 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 return JNI_ERR;
2110 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002111
Elliott Hughes75770752011-08-24 17:52:38 -07002112 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002113 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002114 }
2115
2116 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118 return JNI_OK;
2119 }
2120
Elliott Hughes5174fe62011-08-23 15:12:35 -07002121 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002123 Class* c = Decode<Class*>(ts, java_class);
2124
Elliott Hughes75770752011-08-24 17:52:38 -07002125 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002126 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002127 }
2128
2129 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2130 Method* m = c->GetDirectMethod(i);
2131 if (m->IsNative()) {
2132 m->UnregisterNative();
2133 }
2134 }
2135 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2136 Method* m = c->GetVirtualMethod(i);
2137 if (m->IsNative()) {
2138 m->UnregisterNative();
2139 }
2140 }
2141
2142 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Elliott Hughes72025e52011-08-23 17:50:30 -07002145 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002147 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002148 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 }
2150
Elliott Hughes72025e52011-08-23 17:50:30 -07002151 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002153 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002154 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
2156
2157 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2158 ScopedJniThreadState ts(env);
2159 Runtime* runtime = Runtime::Current();
2160 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002161 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 } else {
2163 *vm = NULL;
2164 }
2165 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2166 }
2167
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2169 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002170
2171 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002172 CHECK(address != NULL); // TODO: ReportJniError
2173 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002174
2175 jclass buffer_class = GetDirectByteBufferClass(env);
2176 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2177 if (mid == NULL) {
2178 return NULL;
2179 }
2180
2181 // At the moment, the Java side is limited to 32 bits.
2182 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2183 CHECK_LE(capacity, 0xffffffff);
2184 jint address_arg = reinterpret_cast<jint>(address);
2185 jint capacity_arg = static_cast<jint>(capacity);
2186
2187 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2188 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Elliott Hughesb465ab02011-08-24 11:21:21 -07002191 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002193 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2194 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Elliott Hughesb465ab02011-08-24 11:21:21 -07002197 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002199 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2200 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
Elliott Hughesb465ab02011-08-24 11:21:21 -07002203 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002205
Elliott Hughes75770752011-08-24 17:52:38 -07002206 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002207
2208 // Do we definitely know what kind of reference this is?
2209 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2210 IndirectRefKind kind = GetIndirectRefKind(ref);
2211 switch (kind) {
2212 case kLocal:
2213 return JNILocalRefType;
2214 case kGlobal:
2215 return JNIGlobalRefType;
2216 case kWeakGlobal:
2217 return JNIWeakGlobalRefType;
2218 case kSirtOrInvalid:
2219 // Is it in a stack IRT?
2220 if (ts.Self()->SirtContains(java_object)) {
2221 return JNILocalRefType;
2222 }
2223
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002224 if (!ts.Env()->work_around_app_jni_bugs) {
2225 return JNIInvalidRefType;
2226 }
2227
Elliott Hughesb465ab02011-08-24 11:21:21 -07002228 // If we're handing out direct pointers, check whether it's a direct pointer
2229 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002230 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002231 if (ts.Env()->locals.Contains(java_object)) {
2232 return JNILocalRefType;
2233 }
2234 }
2235
2236 return JNIInvalidRefType;
2237 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002240
Elliott Hughesa2501992011-08-26 19:39:54 -07002241const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002242 NULL, // reserved0.
2243 NULL, // reserved1.
2244 NULL, // reserved2.
2245 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 JNI::GetVersion,
2247 JNI::DefineClass,
2248 JNI::FindClass,
2249 JNI::FromReflectedMethod,
2250 JNI::FromReflectedField,
2251 JNI::ToReflectedMethod,
2252 JNI::GetSuperclass,
2253 JNI::IsAssignableFrom,
2254 JNI::ToReflectedField,
2255 JNI::Throw,
2256 JNI::ThrowNew,
2257 JNI::ExceptionOccurred,
2258 JNI::ExceptionDescribe,
2259 JNI::ExceptionClear,
2260 JNI::FatalError,
2261 JNI::PushLocalFrame,
2262 JNI::PopLocalFrame,
2263 JNI::NewGlobalRef,
2264 JNI::DeleteGlobalRef,
2265 JNI::DeleteLocalRef,
2266 JNI::IsSameObject,
2267 JNI::NewLocalRef,
2268 JNI::EnsureLocalCapacity,
2269 JNI::AllocObject,
2270 JNI::NewObject,
2271 JNI::NewObjectV,
2272 JNI::NewObjectA,
2273 JNI::GetObjectClass,
2274 JNI::IsInstanceOf,
2275 JNI::GetMethodID,
2276 JNI::CallObjectMethod,
2277 JNI::CallObjectMethodV,
2278 JNI::CallObjectMethodA,
2279 JNI::CallBooleanMethod,
2280 JNI::CallBooleanMethodV,
2281 JNI::CallBooleanMethodA,
2282 JNI::CallByteMethod,
2283 JNI::CallByteMethodV,
2284 JNI::CallByteMethodA,
2285 JNI::CallCharMethod,
2286 JNI::CallCharMethodV,
2287 JNI::CallCharMethodA,
2288 JNI::CallShortMethod,
2289 JNI::CallShortMethodV,
2290 JNI::CallShortMethodA,
2291 JNI::CallIntMethod,
2292 JNI::CallIntMethodV,
2293 JNI::CallIntMethodA,
2294 JNI::CallLongMethod,
2295 JNI::CallLongMethodV,
2296 JNI::CallLongMethodA,
2297 JNI::CallFloatMethod,
2298 JNI::CallFloatMethodV,
2299 JNI::CallFloatMethodA,
2300 JNI::CallDoubleMethod,
2301 JNI::CallDoubleMethodV,
2302 JNI::CallDoubleMethodA,
2303 JNI::CallVoidMethod,
2304 JNI::CallVoidMethodV,
2305 JNI::CallVoidMethodA,
2306 JNI::CallNonvirtualObjectMethod,
2307 JNI::CallNonvirtualObjectMethodV,
2308 JNI::CallNonvirtualObjectMethodA,
2309 JNI::CallNonvirtualBooleanMethod,
2310 JNI::CallNonvirtualBooleanMethodV,
2311 JNI::CallNonvirtualBooleanMethodA,
2312 JNI::CallNonvirtualByteMethod,
2313 JNI::CallNonvirtualByteMethodV,
2314 JNI::CallNonvirtualByteMethodA,
2315 JNI::CallNonvirtualCharMethod,
2316 JNI::CallNonvirtualCharMethodV,
2317 JNI::CallNonvirtualCharMethodA,
2318 JNI::CallNonvirtualShortMethod,
2319 JNI::CallNonvirtualShortMethodV,
2320 JNI::CallNonvirtualShortMethodA,
2321 JNI::CallNonvirtualIntMethod,
2322 JNI::CallNonvirtualIntMethodV,
2323 JNI::CallNonvirtualIntMethodA,
2324 JNI::CallNonvirtualLongMethod,
2325 JNI::CallNonvirtualLongMethodV,
2326 JNI::CallNonvirtualLongMethodA,
2327 JNI::CallNonvirtualFloatMethod,
2328 JNI::CallNonvirtualFloatMethodV,
2329 JNI::CallNonvirtualFloatMethodA,
2330 JNI::CallNonvirtualDoubleMethod,
2331 JNI::CallNonvirtualDoubleMethodV,
2332 JNI::CallNonvirtualDoubleMethodA,
2333 JNI::CallNonvirtualVoidMethod,
2334 JNI::CallNonvirtualVoidMethodV,
2335 JNI::CallNonvirtualVoidMethodA,
2336 JNI::GetFieldID,
2337 JNI::GetObjectField,
2338 JNI::GetBooleanField,
2339 JNI::GetByteField,
2340 JNI::GetCharField,
2341 JNI::GetShortField,
2342 JNI::GetIntField,
2343 JNI::GetLongField,
2344 JNI::GetFloatField,
2345 JNI::GetDoubleField,
2346 JNI::SetObjectField,
2347 JNI::SetBooleanField,
2348 JNI::SetByteField,
2349 JNI::SetCharField,
2350 JNI::SetShortField,
2351 JNI::SetIntField,
2352 JNI::SetLongField,
2353 JNI::SetFloatField,
2354 JNI::SetDoubleField,
2355 JNI::GetStaticMethodID,
2356 JNI::CallStaticObjectMethod,
2357 JNI::CallStaticObjectMethodV,
2358 JNI::CallStaticObjectMethodA,
2359 JNI::CallStaticBooleanMethod,
2360 JNI::CallStaticBooleanMethodV,
2361 JNI::CallStaticBooleanMethodA,
2362 JNI::CallStaticByteMethod,
2363 JNI::CallStaticByteMethodV,
2364 JNI::CallStaticByteMethodA,
2365 JNI::CallStaticCharMethod,
2366 JNI::CallStaticCharMethodV,
2367 JNI::CallStaticCharMethodA,
2368 JNI::CallStaticShortMethod,
2369 JNI::CallStaticShortMethodV,
2370 JNI::CallStaticShortMethodA,
2371 JNI::CallStaticIntMethod,
2372 JNI::CallStaticIntMethodV,
2373 JNI::CallStaticIntMethodA,
2374 JNI::CallStaticLongMethod,
2375 JNI::CallStaticLongMethodV,
2376 JNI::CallStaticLongMethodA,
2377 JNI::CallStaticFloatMethod,
2378 JNI::CallStaticFloatMethodV,
2379 JNI::CallStaticFloatMethodA,
2380 JNI::CallStaticDoubleMethod,
2381 JNI::CallStaticDoubleMethodV,
2382 JNI::CallStaticDoubleMethodA,
2383 JNI::CallStaticVoidMethod,
2384 JNI::CallStaticVoidMethodV,
2385 JNI::CallStaticVoidMethodA,
2386 JNI::GetStaticFieldID,
2387 JNI::GetStaticObjectField,
2388 JNI::GetStaticBooleanField,
2389 JNI::GetStaticByteField,
2390 JNI::GetStaticCharField,
2391 JNI::GetStaticShortField,
2392 JNI::GetStaticIntField,
2393 JNI::GetStaticLongField,
2394 JNI::GetStaticFloatField,
2395 JNI::GetStaticDoubleField,
2396 JNI::SetStaticObjectField,
2397 JNI::SetStaticBooleanField,
2398 JNI::SetStaticByteField,
2399 JNI::SetStaticCharField,
2400 JNI::SetStaticShortField,
2401 JNI::SetStaticIntField,
2402 JNI::SetStaticLongField,
2403 JNI::SetStaticFloatField,
2404 JNI::SetStaticDoubleField,
2405 JNI::NewString,
2406 JNI::GetStringLength,
2407 JNI::GetStringChars,
2408 JNI::ReleaseStringChars,
2409 JNI::NewStringUTF,
2410 JNI::GetStringUTFLength,
2411 JNI::GetStringUTFChars,
2412 JNI::ReleaseStringUTFChars,
2413 JNI::GetArrayLength,
2414 JNI::NewObjectArray,
2415 JNI::GetObjectArrayElement,
2416 JNI::SetObjectArrayElement,
2417 JNI::NewBooleanArray,
2418 JNI::NewByteArray,
2419 JNI::NewCharArray,
2420 JNI::NewShortArray,
2421 JNI::NewIntArray,
2422 JNI::NewLongArray,
2423 JNI::NewFloatArray,
2424 JNI::NewDoubleArray,
2425 JNI::GetBooleanArrayElements,
2426 JNI::GetByteArrayElements,
2427 JNI::GetCharArrayElements,
2428 JNI::GetShortArrayElements,
2429 JNI::GetIntArrayElements,
2430 JNI::GetLongArrayElements,
2431 JNI::GetFloatArrayElements,
2432 JNI::GetDoubleArrayElements,
2433 JNI::ReleaseBooleanArrayElements,
2434 JNI::ReleaseByteArrayElements,
2435 JNI::ReleaseCharArrayElements,
2436 JNI::ReleaseShortArrayElements,
2437 JNI::ReleaseIntArrayElements,
2438 JNI::ReleaseLongArrayElements,
2439 JNI::ReleaseFloatArrayElements,
2440 JNI::ReleaseDoubleArrayElements,
2441 JNI::GetBooleanArrayRegion,
2442 JNI::GetByteArrayRegion,
2443 JNI::GetCharArrayRegion,
2444 JNI::GetShortArrayRegion,
2445 JNI::GetIntArrayRegion,
2446 JNI::GetLongArrayRegion,
2447 JNI::GetFloatArrayRegion,
2448 JNI::GetDoubleArrayRegion,
2449 JNI::SetBooleanArrayRegion,
2450 JNI::SetByteArrayRegion,
2451 JNI::SetCharArrayRegion,
2452 JNI::SetShortArrayRegion,
2453 JNI::SetIntArrayRegion,
2454 JNI::SetLongArrayRegion,
2455 JNI::SetFloatArrayRegion,
2456 JNI::SetDoubleArrayRegion,
2457 JNI::RegisterNatives,
2458 JNI::UnregisterNatives,
2459 JNI::MonitorEnter,
2460 JNI::MonitorExit,
2461 JNI::GetJavaVM,
2462 JNI::GetStringRegion,
2463 JNI::GetStringUTFRegion,
2464 JNI::GetPrimitiveArrayCritical,
2465 JNI::ReleasePrimitiveArrayCritical,
2466 JNI::GetStringCritical,
2467 JNI::ReleaseStringCritical,
2468 JNI::NewWeakGlobalRef,
2469 JNI::DeleteWeakGlobalRef,
2470 JNI::ExceptionCheck,
2471 JNI::NewDirectByteBuffer,
2472 JNI::GetDirectBufferAddress,
2473 JNI::GetDirectBufferCapacity,
2474 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002475};
2476
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002477static const size_t kMonitorsInitial = 32; // Arbitrary.
2478static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2479
2480static const size_t kLocalsInitial = 64; // Arbitrary.
2481static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002482
Elliott Hughes75770752011-08-24 17:52:38 -07002483JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002484 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002485 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002486 local_ref_cookie(IRT_FIRST_SEGMENT),
2487 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes75770752011-08-24 17:52:38 -07002488 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002489 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002490 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002491 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002492 functions = unchecked_functions = &gNativeInterface;
2493 if (check_jni) {
2494 functions = GetCheckJniNativeInterface();
2495 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002496 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2497 // errors will ensue.
2498 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2499 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002500}
2501
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002502JNIEnvExt::~JNIEnvExt() {
2503}
2504
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002505void JNIEnvExt::DumpReferenceTables() {
2506 locals.Dump();
2507 monitors.Dump();
2508}
2509
Carl Shapiroea4dca82011-08-01 13:45:38 -07002510// JNI Invocation interface.
2511
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002512extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2513 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2514 if (args->version < JNI_VERSION_1_2) {
2515 return JNI_EVERSION;
2516 }
2517 Runtime::Options options;
2518 for (int i = 0; i < args->nOptions; ++i) {
2519 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002520 options.push_back(std::make_pair(StringPiece(option->optionString),
2521 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002522 }
2523 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002524 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002525 if (runtime == NULL) {
2526 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002527 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002528 runtime->Start();
2529 *p_env = Thread::Current()->GetJniEnv();
2530 *p_vm = runtime->GetJavaVM();
2531 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002532}
2533
Elliott Hughesf2682d52011-08-15 16:37:04 -07002534extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002535 Runtime* runtime = Runtime::Current();
2536 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002537 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002538 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002539 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002540 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002541 }
2542 return JNI_OK;
2543}
2544
2545// Historically unsupported.
2546extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2547 return JNI_ERR;
2548}
2549
Elliott Hughescdf53122011-08-19 15:46:09 -07002550class JII {
2551 public:
2552 static jint DestroyJavaVM(JavaVM* vm) {
2553 if (vm == NULL) {
2554 return JNI_ERR;
2555 } else {
2556 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2557 delete raw_vm->runtime;
2558 return JNI_OK;
2559 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002560 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002561
Elliott Hughescdf53122011-08-19 15:46:09 -07002562 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002563 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002564 }
2565
2566 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002567 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002568 }
2569
2570 static jint DetachCurrentThread(JavaVM* vm) {
2571 if (vm == NULL) {
2572 return JNI_ERR;
2573 } else {
2574 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2575 Runtime* runtime = raw_vm->runtime;
2576 runtime->DetachCurrentThread();
2577 return JNI_OK;
2578 }
2579 }
2580
2581 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2582 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2583 return JNI_EVERSION;
2584 }
2585 if (vm == NULL || env == NULL) {
2586 return JNI_ERR;
2587 }
2588 Thread* thread = Thread::Current();
2589 if (thread == NULL) {
2590 *env = NULL;
2591 return JNI_EDETACHED;
2592 }
2593 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002594 return JNI_OK;
2595 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002596};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002597
Elliott Hughesa2501992011-08-26 19:39:54 -07002598const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002599 NULL, // reserved0
2600 NULL, // reserved1
2601 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002602 JII::DestroyJavaVM,
2603 JII::AttachCurrentThread,
2604 JII::DetachCurrentThread,
2605 JII::GetEnv,
2606 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002607};
2608
Elliott Hughesbbd76712011-08-17 10:25:24 -07002609static const size_t kPinTableInitialSize = 16;
2610static const size_t kPinTableMaxSize = 1024;
2611
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002612static const size_t kGlobalsInitial = 512; // Arbitrary.
2613static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2614
2615static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2616static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2617
Elliott Hughesa0957642011-09-02 14:27:33 -07002618JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002619 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002620 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002621 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002622 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002623 verbose_jni(options->IsVerbose("jni")),
2624 log_third_party_jni(options->IsVerbose("third-party-jni")),
2625 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002626 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002627 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002628 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002629 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002630 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002631 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002632 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002633 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002634 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002635 functions = unchecked_functions = &gInvokeInterface;
2636 if (check_jni) {
2637 functions = GetCheckJniInvokeInterface();
2638 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002639}
2640
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002641JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002642 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002643}
2644
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002645void JavaVMExt::DumpReferenceTables() {
2646 {
2647 MutexLock mu(globals_lock);
2648 globals.Dump();
2649 }
2650 {
2651 MutexLock mu(weak_globals_lock);
2652 weak_globals.Dump();
2653 }
2654 {
2655 MutexLock mu(pins_lock);
2656 pin_table.Dump();
2657 }
2658}
2659
Elliott Hughes75770752011-08-24 17:52:38 -07002660bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2661 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002662
2663 // See if we've already loaded this library. If we have, and the class loader
2664 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002665 // TODO: for better results we should canonicalize the pathname (or even compare
2666 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002667 SharedLibrary* library;
2668 {
2669 // TODO: move the locking (and more of this logic) into Libraries.
2670 MutexLock mu(libraries_lock);
2671 library = libraries->Get(path);
2672 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002673 if (library != NULL) {
2674 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002675 // The library will be associated with class_loader. The JNI
2676 // spec says we can't load the same library into more than one
2677 // class loader.
2678 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2679 "ClassLoader %p; can't open in ClassLoader %p",
2680 path.c_str(), library->GetClassLoader(), class_loader);
2681 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002682 return false;
2683 }
2684 if (verbose_jni) {
2685 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2686 << "ClassLoader " << class_loader << "]";
2687 }
2688 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002689 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2690 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002691 return false;
2692 }
2693 return true;
2694 }
2695
2696 // Open the shared library. Because we're using a full path, the system
2697 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2698 // resolve this library's dependencies though.)
2699
2700 // Failures here are expected when java.library.path has several entries
2701 // and we have to hunt for the lib.
2702
2703 // The current version of the dynamic linker prints detailed information
2704 // about dlopen() failures. Some things to check if the message is
2705 // cryptic:
2706 // - make sure the library exists on the device
2707 // - verify that the right path is being opened (the debug log message
2708 // above can help with that)
2709 // - check to see if the library is valid (e.g. not zero bytes long)
2710 // - check config/prelink-linux-arm.map to ensure that the library
2711 // is listed and is not being overrun by the previous entry (if
2712 // loading suddenly stops working on a prelinked library, this is
2713 // a good one to check)
2714 // - write a trivial app that calls sleep() then dlopen(), attach
2715 // to it with "strace -p <pid>" while it sleeps, and watch for
2716 // attempts to open nonexistent dependent shared libs
2717
2718 // TODO: automate some of these checks!
2719
2720 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002721 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002722 // the GC to ignore us.
2723 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002724 void* handle = NULL;
2725 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002726 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002727 handle = dlopen(path.c_str(), RTLD_LAZY);
2728 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002729
2730 if (verbose_jni) {
2731 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2732 }
2733
2734 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002735 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002736 return false;
2737 }
2738
2739 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002740 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002741 // TODO: move the locking (and more of this logic) into Libraries.
2742 MutexLock mu(libraries_lock);
2743 library = libraries->Get(path);
2744 if (library != NULL) {
2745 LOG(INFO) << "WOW: we lost a race to add shared library: "
2746 << "\"" << path << "\" ClassLoader=" << class_loader;
2747 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002748 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002749 library = new SharedLibrary(path, handle, class_loader);
2750 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002751 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002752
2753 if (verbose_jni) {
2754 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2755 }
2756
2757 bool result = true;
2758 void* sym = dlsym(handle, "JNI_OnLoad");
2759 if (sym == NULL) {
2760 if (verbose_jni) {
2761 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2762 }
2763 } else {
2764 // Call JNI_OnLoad. We have to override the current class
2765 // loader, which will always be "null" since the stuff at the
2766 // top of the stack is around Runtime.loadLibrary(). (See
2767 // the comments in the JNI FindClass function.)
2768 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2769 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002770 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002771 self->SetClassLoaderOverride(class_loader);
2772
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002773 int version = 0;
2774 {
2775 ScopedThreadStateChange tsc(self, Thread::kNative);
2776 if (verbose_jni) {
2777 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2778 }
2779 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002780 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002781
Brian Carlstromaded5f72011-10-07 17:15:04 -07002782 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002783
2784 if (version != JNI_VERSION_1_2 &&
2785 version != JNI_VERSION_1_4 &&
2786 version != JNI_VERSION_1_6) {
2787 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2788 << "bad version: " << version;
2789 // It's unwise to call dlclose() here, but we can mark it
2790 // as bad and ensure that future load attempts will fail.
2791 // We don't know how far JNI_OnLoad got, so there could
2792 // be some partially-initialized stuff accessible through
2793 // newly-registered native method calls. We could try to
2794 // unregister them, but that doesn't seem worthwhile.
2795 result = false;
2796 } else {
2797 if (verbose_jni) {
2798 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2799 << " from JNI_OnLoad in \"" << path << "\"]";
2800 }
2801 }
2802 }
2803
2804 library->SetResult(result);
2805 return result;
2806}
2807
2808void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2809 CHECK(m->IsNative());
2810
2811 Class* c = m->GetDeclaringClass();
2812
2813 // If this is a static method, it could be called before the class
2814 // has been initialized.
2815 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002816 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002817 return NULL;
2818 }
2819 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002820 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002821 }
2822
Brian Carlstrom16192862011-09-12 17:50:06 -07002823 std::string detail;
2824 void* native_method;
2825 {
2826 MutexLock mu(libraries_lock);
2827 native_method = libraries->FindNativeMethod(m, detail);
2828 }
2829 // throwing can cause libraries_lock to be reacquired
2830 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002831 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002832 }
2833 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002834}
2835
Elliott Hughes410c0c82011-09-01 17:58:25 -07002836void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2837 {
2838 MutexLock mu(globals_lock);
2839 globals.VisitRoots(visitor, arg);
2840 }
2841 {
2842 MutexLock mu(pins_lock);
2843 pin_table.VisitRoots(visitor, arg);
2844 }
2845 // The weak_globals table is visited by the GC itself (because it mutates the table).
2846}
2847
Ian Rogersdf20fe02011-07-20 20:34:16 -07002848} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002849
2850std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2851 switch (rhs) {
2852 case JNIInvalidRefType:
2853 os << "JNIInvalidRefType";
2854 return os;
2855 case JNILocalRefType:
2856 os << "JNILocalRefType";
2857 return os;
2858 case JNIGlobalRefType:
2859 os << "JNIGlobalRefType";
2860 return os;
2861 case JNIWeakGlobalRefType:
2862 os << "JNIWeakGlobalRefType";
2863 return os;
2864 }
2865}