blob: 5052773875d9bec945f43e1d437119024e47bb0a [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
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070027namespace art {
28
Elliott Hughes2ced6a52011-10-16 18:44:48 -070029static const size_t kMonitorsInitial = 32; // Arbitrary.
30static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
31
32static const size_t kLocalsInitial = 64; // Arbitrary.
33static const size_t kLocalsMax = 512; // Arbitrary sanity check.
34
35static const size_t kPinTableInitial = 16; // Arbitrary.
36static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
37
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070038static size_t gGlobalsInitial = 512; // Arbitrary.
39static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070040
41static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
42static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
43
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070044void SetJniGlobalsMax(size_t max) {
45 if (max != 0) {
46 gGlobalsMax = max;
47 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
48 }
49}
Ian Rogersdf20fe02011-07-20 20:34:16 -070050
Elliott Hughesc5f7c912011-08-18 14:00:42 -070051/*
52 * Add a local reference for an object to the current stack frame. When
53 * the native function returns, the reference will be discarded.
54 *
55 * We need to allow the same reference to be added multiple times.
56 *
57 * This will be called on otherwise unreferenced objects. We cannot do
58 * GC allocations here, and it's best if we don't grab a mutex.
59 *
60 * Returns the local reference (currently just the same pointer that was
61 * passed in), or NULL on failure.
62 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070063template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070064T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
65 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
66 Object* obj = const_cast<Object*>(const_obj);
67
Elliott Hughesc5f7c912011-08-18 14:00:42 -070068 if (obj == NULL) {
69 return NULL;
70 }
71
Elliott Hughesbf86d042011-08-31 17:53:14 -070072 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
73 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070074
Ian Rogers5a7a74a2011-09-26 16:32:29 -070075 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070076 IndirectRef ref = locals.Add(cookie, obj);
77 if (ref == NULL) {
78 // TODO: just change Add's DCHECK to CHECK and lose this?
79 locals.Dump();
80 LOG(FATAL) << "Failed adding to JNI local reference table "
81 << "(has " << locals.Capacity() << " entries)";
82 // TODO: dvmDumpThread(dvmThreadSelf(), false);
83 }
84
85#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070086 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070087 size_t entry_count = locals.Capacity();
88 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070089 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070090 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070091 locals.Dump();
92 // TODO: dvmDumpThread(dvmThreadSelf(), false);
93 // dvmAbort();
94 }
95 }
96#endif
97
Elliott Hughesbf86d042011-08-31 17:53:14 -070098 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070099 // Hand out direct pointers to support broken old apps.
100 return reinterpret_cast<T>(obj);
101 }
102
103 return reinterpret_cast<T>(ref);
104}
105
Elliott Hughesbf86d042011-08-31 17:53:14 -0700106// For external use.
107template<typename T>
108T Decode(JNIEnv* public_env, jobject obj) {
109 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
110 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
111}
112// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700113template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700114template Class* Decode<Class*>(JNIEnv*, jobject);
115template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
116template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700117template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700118template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700119template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700120template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400121template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700122template String* Decode<String*>(JNIEnv*, jobject);
123template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700124
125namespace {
126
Elliott Hughescdf53122011-08-19 15:46:09 -0700127jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
128 if (obj == NULL) {
129 return NULL;
130 }
Elliott Hughes75770752011-08-24 17:52:38 -0700131 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700132 IndirectReferenceTable& weak_globals = vm->weak_globals;
133 MutexLock mu(vm->weak_globals_lock);
134 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
135 return reinterpret_cast<jweak>(ref);
136}
137
Elliott Hughesbf86d042011-08-31 17:53:14 -0700138// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700139template<typename T>
140T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700141 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700142}
143
Elliott Hughes418d20f2011-09-22 14:00:39 -0700144byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
145 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700146 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700147 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700148 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700149 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
150 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700151 case 'Z':
152 case 'B':
153 case 'C':
154 case 'S':
155 case 'I':
156 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
157 offset += 4;
158 break;
159 case 'F':
160 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
161 offset += 4;
162 break;
163 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700164 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700165 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
166 offset += sizeof(Object*);
167 break;
168 }
169 case 'D':
170 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
171 offset += 8;
172 break;
173 case 'J':
174 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
175 offset += 8;
176 break;
177 }
178 }
179 return arg_array.release();
180}
181
Elliott Hughes418d20f2011-09-22 14:00:39 -0700182byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
183 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700184 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700185 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700186 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700187 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
188 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700189 case 'Z':
190 case 'B':
191 case 'C':
192 case 'S':
193 case 'I':
194 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
195 offset += 4;
196 break;
197 case 'F':
198 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
199 offset += 4;
200 break;
201 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700202 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700203 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
204 offset += sizeof(Object*);
205 break;
206 }
207 case 'D':
208 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
209 offset += 8;
210 break;
211 case 'J':
212 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
213 offset += 8;
214 break;
215 }
216 }
217 return arg_array.release();
218}
219
Elliott Hughes418d20f2011-09-22 14:00:39 -0700220JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
221 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700222 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700223 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700224 return result;
225}
226
Elliott Hughes418d20f2011-09-22 14:00:39 -0700227JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
228 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
229 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700230 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700231 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
232 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700233}
234
Elliott Hughes75770752011-08-24 17:52:38 -0700235Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700236 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700237}
238
Elliott Hughes418d20f2011-09-22 14:00:39 -0700239JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
240 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
241 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700242 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700243 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
244 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700245}
246
Elliott Hughes418d20f2011-09-22 14:00:39 -0700247JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
248 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
249 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700250 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700251 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
252 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700253}
254
Elliott Hughes6b436852011-08-12 10:16:44 -0700255// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
256// separated with slashes but aren't wrapped with "L;" like regular descriptors
257// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
258// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
259// supported names with dots too (such as "a.b.C").
260std::string NormalizeJniClassDescriptor(const char* name) {
261 std::string result;
262 // Add the missing "L;" if necessary.
263 if (name[0] == '[') {
264 result = name;
265 } else {
266 result += 'L';
267 result += name;
268 result += ';';
269 }
270 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700271 if (result.find('.') != std::string::npos) {
272 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
273 << "\"" << name << "\"";
274 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700275 }
276 return result;
277}
278
Elliott Hughes14134a12011-09-30 16:55:51 -0700279void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
280 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700281 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes14134a12011-09-30 16:55:51 -0700282 "no %s method \"%s.%s%s\"", kind, class_descriptor.c_str(), name, sig);
283}
284
Elliott Hughescdf53122011-08-19 15:46:09 -0700285jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
286 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700287 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700288 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700289 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700290
291 Method* method = NULL;
292 if (is_static) {
293 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700294 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700295 method = c->FindVirtualMethod(name, sig);
296 if (method == NULL) {
297 // No virtual method matching the signature. Search declared
298 // private methods and constructors.
299 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700300 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700301 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700302
Elliott Hughescdf53122011-08-19 15:46:09 -0700303 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700304 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700305 return NULL;
306 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700307
Elliott Hughes418d20f2011-09-22 14:00:39 -0700308 method->InitJavaFields();
309
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700310 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700311}
312
Brian Carlstrom00fae582011-10-28 01:16:28 -0700313const ClassLoader* GetClassLoader(Thread* self) {
314 Frame frame = self->GetTopOfStack();
315 Method* method = frame.GetMethod();
316 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
317 return self->GetClassLoaderOverride();
318 }
319 return method->GetDeclaringClass()->GetClassLoader();
320}
321
Elliott Hughescdf53122011-08-19 15:46:09 -0700322jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
323 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700324 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700325 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700326 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700327
328 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 Class* field_type;
330 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
331 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700332 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700333 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700334 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700336 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 if (field_type == NULL) {
338 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700339 DCHECK(ts.Self()->IsExceptionPending());
340 ts.Self()->ClearException();
341 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700342 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700343 "no type \"%s\" found and so no field \"%s\" could be found in class "
344 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700345 return NULL;
346 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700347 std::string field_type_descriptor = field_type->GetDescriptor()->ToModifiedUtf8();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 if (is_static) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700349 field = c->FindStaticField(name, field_type_descriptor);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700350 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700351 field = c->FindInstanceField(name, field_type_descriptor);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700352 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700353 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700354 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700355 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700356 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700357 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700358 return NULL;
359 }
Elliott Hughes80609252011-09-23 17:24:51 -0700360 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700361 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700362}
363
Elliott Hughes75770752011-08-24 17:52:38 -0700364void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
365 JavaVMExt* vm = ts.Vm();
366 MutexLock mu(vm->pins_lock);
367 vm->pin_table.Add(array);
368}
369
370void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
371 JavaVMExt* vm = ts.Vm();
372 MutexLock mu(vm->pins_lock);
373 vm->pin_table.Remove(array);
374}
375
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700376template<typename JniT, typename ArtT>
377JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
378 CHECK_GE(length, 0); // TODO: ReportJniError
379 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700380 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700381}
382
Elliott Hughes75770752011-08-24 17:52:38 -0700383template <typename ArrayT, typename CArrayT, typename ArtArrayT>
384CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
385 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
386 PinPrimitiveArray(ts, array);
387 if (is_copy != NULL) {
388 *is_copy = JNI_FALSE;
389 }
390 return array->GetData();
391}
392
393template <typename ArrayT>
394void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
395 if (mode != JNI_COMMIT) {
396 Array* array = Decode<Array*>(ts, java_array);
397 UnpinPrimitiveArray(ts, array);
398 }
399}
400
Elliott Hughes814e4032011-08-23 12:07:56 -0700401void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700402 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700403 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700404 "%s offset=%d length=%d %s.length=%d",
405 type.c_str(), start, length, identifier, array->GetLength());
406}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700407void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700408 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700409 "offset=%d length=%d string.length()=%d", start, length, array_length);
410}
Elliott Hughes814e4032011-08-23 12:07:56 -0700411
412template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700413void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700414 ArrayT* array = Decode<ArrayT*>(ts, java_array);
415 if (start < 0 || length < 0 || start + length > array->GetLength()) {
416 ThrowAIOOBE(ts, array, start, length, "src");
417 } else {
418 JavaT* data = array->GetData();
419 memcpy(buf, data + start, length * sizeof(JavaT));
420 }
421}
422
423template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700424void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700425 ArrayT* array = Decode<ArrayT*>(ts, java_array);
426 if (start < 0 || length < 0 || start + length > array->GetLength()) {
427 ThrowAIOOBE(ts, array, start, length, "dst");
428 } else {
429 JavaT* data = array->GetData();
430 memcpy(data + start, buf, length * sizeof(JavaT));
431 }
432}
433
Elliott Hughes75770752011-08-24 17:52:38 -0700434jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700435 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
436 CHECK(buffer_class.get() != NULL);
437 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
438}
439
Elliott Hughes75770752011-08-24 17:52:38 -0700440jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700441 static jclass buffer_class = InitDirectByteBufferClass(env);
442 return buffer_class;
443}
444
Elliott Hughes75770752011-08-24 17:52:38 -0700445jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
446 if (vm == NULL || p_env == NULL) {
447 return JNI_ERR;
448 }
449
450 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
451 JavaVMAttachArgs args;
452 if (thr_args == NULL) {
453 // Allow the v1.1 calling convention.
454 args.version = JNI_VERSION_1_2;
455 args.name = NULL;
456 args.group = NULL; // TODO: get "main" thread group
457 } else {
458 args.version = in_args->version;
459 args.name = in_args->name;
460 if (in_args->group != NULL) {
461 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
462 args.group = NULL; // TODO: decode in_args->group
463 } else {
464 args.group = NULL; // TODO: get "main" thread group
465 }
466 }
467 CHECK_GE(args.version, JNI_VERSION_1_2);
468
469 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700470 runtime->AttachCurrentThread(args.name, as_daemon);
471 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700472 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700473}
474
Elliott Hughes79082e32011-08-25 12:07:32 -0700475class SharedLibrary {
476 public:
477 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
478 : path_(path),
479 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700480 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700481 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700482 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700483 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700484 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 }
486
Elliott Hughes79082e32011-08-25 12:07:32 -0700487 Object* GetClassLoader() {
488 return class_loader_;
489 }
490
491 std::string GetPath() {
492 return path_;
493 }
494
495 /*
496 * Check the result of an earlier call to JNI_OnLoad on this library. If
497 * the call has not yet finished in another thread, wait for it.
498 */
499 bool CheckOnLoadResult(JavaVMExt* vm) {
500 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700501 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700502 // Check this so we don't end up waiting for ourselves. We need
503 // to return "true" so the caller can continue.
504 LOG(INFO) << *self << " recursive attempt to load library "
505 << "\"" << path_ << "\"";
506 return true;
507 }
508
509 MutexLock mu(jni_on_load_lock_);
510 while (jni_on_load_result_ == kPending) {
511 if (vm->verbose_jni) {
512 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
513 << "JNI_OnLoad...]";
514 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700515 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700516 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700517 }
518
519 bool okay = (jni_on_load_result_ == kOkay);
520 if (vm->verbose_jni) {
521 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
522 << (okay ? "succeeded" : "failed") << "]";
523 }
524 return okay;
525 }
526
527 void SetResult(bool result) {
528 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700529 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700530
531 // Broadcast a wakeup to anybody sleeping on the condition variable.
532 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700533 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700534 }
535
536 void* FindSymbol(const std::string& symbol_name) {
537 return dlsym(handle_, symbol_name.c_str());
538 }
539
540 private:
541 enum JNI_OnLoadState {
542 kPending,
543 kFailed,
544 kOkay,
545 };
546
547 // Path to library "/system/lib/libjni.so".
548 std::string path_;
549
550 // The void* returned by dlopen(3).
551 void* handle_;
552
553 // The ClassLoader this library is associated with.
554 Object* class_loader_;
555
556 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700557 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700558 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700559 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700560 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700561 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700562 // Result of earlier JNI_OnLoad call.
563 JNI_OnLoadState jni_on_load_result_;
564};
565
Elliott Hughescdf53122011-08-19 15:46:09 -0700566} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700567
Elliott Hughes79082e32011-08-25 12:07:32 -0700568// This exists mainly to keep implementation details out of the header file.
569class Libraries {
570 public:
571 Libraries() {
572 }
573
574 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700575 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700576 }
577
578 SharedLibrary* Get(const std::string& path) {
579 return libraries_[path];
580 }
581
582 void Put(const std::string& path, SharedLibrary* library) {
583 libraries_[path] = library;
584 }
585
586 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700587 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700588 std::string jni_short_name(JniShortName(m));
589 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700590 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700591 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
592 SharedLibrary* library = it->second;
593 if (library->GetClassLoader() != declaring_class_loader) {
594 // We only search libraries loaded by the appropriate ClassLoader.
595 continue;
596 }
597 // Try the short name then the long name...
598 void* fn = library->FindSymbol(jni_short_name);
599 if (fn == NULL) {
600 fn = library->FindSymbol(jni_long_name);
601 }
602 if (fn != NULL) {
603 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700604 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700605 << " in \"" << library->GetPath() << "\"]";
606 }
607 return fn;
608 }
609 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700610 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700611 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700612 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700613 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700614 return NULL;
615 }
616
617 private:
618 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
619
620 std::map<std::string, SharedLibrary*> libraries_;
621};
622
Elliott Hughes418d20f2011-09-22 14:00:39 -0700623JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
624 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
625 Object* receiver = Decode<Object*>(env, obj);
626 Method* method = DecodeMethod(mid);
627 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
628 return InvokeWithArgArray(env, receiver, method, arg_array.get());
629}
630
Elliott Hughescdf53122011-08-19 15:46:09 -0700631class JNI {
632 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700633
Elliott Hughescdf53122011-08-19 15:46:09 -0700634 static jint GetVersion(JNIEnv* env) {
635 ScopedJniThreadState ts(env);
636 return JNI_VERSION_1_6;
637 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700638
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
640 ScopedJniThreadState ts(env);
641 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700642 return NULL;
643 }
644
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 static jclass FindClass(JNIEnv* env, const char* name) {
646 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700647 Runtime* runtime = Runtime::Current();
648 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700650 Class* c = NULL;
651 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700652 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700653 c = class_linker->FindClass(descriptor, cl);
654 } else {
655 c = class_linker->FindSystemClass(descriptor);
656 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700657 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700658 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700659
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
661 ScopedJniThreadState ts(env);
662 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700663 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700664 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700665
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
667 ScopedJniThreadState ts(env);
668 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700669 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700671
Elliott Hughescdf53122011-08-19 15:46:09 -0700672 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
673 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700674 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700675 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700677
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
679 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700680 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700681 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700683
Elliott Hughes37f7a402011-08-22 18:56:01 -0700684 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
685 ScopedJniThreadState ts(env);
686 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700687 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 }
689
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700690 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700692 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700693 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700695
Elliott Hughes37f7a402011-08-22 18:56:01 -0700696 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700697 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 Class* c1 = Decode<Class*>(ts, java_class1);
699 Class* c2 = Decode<Class*>(ts, java_class2);
700 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700701 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700702
Elliott Hughes37f7a402011-08-22 18:56:01 -0700703 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700704 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700705 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700706 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700707 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 return JNI_TRUE;
709 } else {
710 Object* obj = Decode<Object*>(ts, jobj);
711 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700712 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700713 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700715
Elliott Hughes37f7a402011-08-22 18:56:01 -0700716 static jint Throw(JNIEnv* env, jthrowable java_exception) {
717 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700718 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700719 if (exception == NULL) {
720 return JNI_ERR;
721 }
722 ts.Self()->SetException(exception);
723 return JNI_OK;
724 }
725
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700726 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700727 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700728 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700729 jmethodID mid = ((msg != NULL)
730 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
731 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700732 if (mid == NULL) {
733 return JNI_ERR;
734 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700735 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700736 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700737 return JNI_ERR;
738 }
739
740 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700741 args[0].l = s.get();
742 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
743 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700744 return JNI_ERR;
745 }
746
Elliott Hughes72025e52011-08-23 17:50:30 -0700747 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700748
Elliott Hughes37f7a402011-08-22 18:56:01 -0700749 return JNI_OK;
750 }
751
752 static jboolean ExceptionCheck(JNIEnv* env) {
753 ScopedJniThreadState ts(env);
754 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
755 }
756
757 static void ExceptionClear(JNIEnv* env) {
758 ScopedJniThreadState ts(env);
759 ts.Self()->ClearException();
760 }
761
762 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700763 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700764
765 Thread* self = ts.Self();
766 Throwable* original_exception = self->GetException();
767 self->ClearException();
768
Elliott Hughesbf86d042011-08-31 17:53:14 -0700769 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700770 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
771 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
772 if (mid == NULL) {
773 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700774 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700775 } else {
776 env->CallVoidMethod(exception.get(), mid);
777 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700778 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700779 << " thrown while calling printStackTrace";
780 self->ClearException();
781 }
782 }
783
784 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700786
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 static jthrowable ExceptionOccurred(JNIEnv* env) {
788 ScopedJniThreadState ts(env);
789 Object* exception = ts.Self()->GetException();
790 if (exception == NULL) {
791 return NULL;
792 } else {
793 // TODO: if adding a local reference failing causes the VM to abort
794 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700795 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 if (localException == NULL) {
797 // We were unable to add a new local reference, and threw a new
798 // exception. We can't return "exception", because it's not a
799 // local reference. So we have to return NULL, indicating that
800 // there was no exception, even though it's pretty much raining
801 // exceptions in here.
802 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
803 }
804 return localException;
805 }
806 }
807
Elliott Hughescdf53122011-08-19 15:46:09 -0700808 static void FatalError(JNIEnv* env, const char* msg) {
809 ScopedJniThreadState ts(env);
810 LOG(FATAL) << "JNI FatalError called: " << msg;
811 }
812
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700813 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700815 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
816 return JNI_ERR;
817 }
818 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 return JNI_OK;
820 }
821
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700822 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700824 Object* survivor = Decode<Object*>(ts, java_survivor);
825 ts.Env()->PopFrame();
826 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 }
828
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700829 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700830 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700831 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
832 }
833
834 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
835 // TODO: we should try to expand the table if necessary.
836 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
837 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
838 return JNI_ERR;
839 }
840 // TODO: this isn't quite right, since "capacity" includes holes.
841 size_t capacity = ts.Env()->locals.Capacity();
842 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
843 if (!okay) {
844 ts.Self()->ThrowOutOfMemoryError(caller);
845 }
846 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700847 }
848
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
850 ScopedJniThreadState ts(env);
851 if (obj == NULL) {
852 return NULL;
853 }
854
Elliott Hughes75770752011-08-24 17:52:38 -0700855 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 IndirectReferenceTable& globals = vm->globals;
857 MutexLock mu(vm->globals_lock);
858 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
859 return reinterpret_cast<jobject>(ref);
860 }
861
862 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
863 ScopedJniThreadState ts(env);
864 if (obj == NULL) {
865 return;
866 }
867
Elliott Hughes75770752011-08-24 17:52:38 -0700868 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 IndirectReferenceTable& globals = vm->globals;
870 MutexLock mu(vm->globals_lock);
871
872 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
873 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
874 << "failed to find entry";
875 }
876 }
877
878 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
879 ScopedJniThreadState ts(env);
880 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
881 }
882
883 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
884 ScopedJniThreadState ts(env);
885 if (obj == NULL) {
886 return;
887 }
888
Elliott Hughes75770752011-08-24 17:52:38 -0700889 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 IndirectReferenceTable& weak_globals = vm->weak_globals;
891 MutexLock mu(vm->weak_globals_lock);
892
893 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
894 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
895 << "failed to find entry";
896 }
897 }
898
899 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
900 ScopedJniThreadState ts(env);
901 if (obj == NULL) {
902 return NULL;
903 }
904
905 IndirectReferenceTable& locals = ts.Env()->locals;
906
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700907 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
909 return reinterpret_cast<jobject>(ref);
910 }
911
912 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
913 ScopedJniThreadState ts(env);
914 if (obj == NULL) {
915 return;
916 }
917
918 IndirectReferenceTable& locals = ts.Env()->locals;
919
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700920 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 if (!locals.Remove(cookie, obj)) {
922 // Attempting to delete a local reference that is not in the
923 // topmost local reference frame is a no-op. DeleteLocalRef returns
924 // void and doesn't throw any exceptions, but we should probably
925 // complain about it so the user will notice that things aren't
926 // going quite the way they expect.
927 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
928 << "failed to find entry";
929 }
930 }
931
932 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
933 ScopedJniThreadState ts(env);
934 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
935 ? JNI_TRUE : JNI_FALSE;
936 }
937
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700938 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700940 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700941 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700942 return NULL;
943 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700944 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 }
946
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 ScopedJniThreadState ts(env);
949 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 va_start(args, mid);
951 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 va_end(args);
953 return result;
954 }
955
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700958 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700959 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700960 return NULL;
961 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700962 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700963 if (result == NULL) {
964 return NULL;
965 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700966 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 return local_result;
969 }
970
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700973 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700974 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700975 return NULL;
976 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700977 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700978 if (result == NULL) {
979 return NULL;
980 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700981 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 return local_result;
984 }
985
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
987 ScopedJniThreadState ts(env);
988 return FindMethodID(ts, c, name, sig, false);
989 }
990
991 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
992 ScopedJniThreadState ts(env);
993 return FindMethodID(ts, c, name, sig, true);
994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 va_list ap;
999 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001000 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001002 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 }
1004
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001007 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001008 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 }
1010
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001013 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001014 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_list ap;
1020 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001021 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_end(ap);
1023 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001028 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001033 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 }
1035
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 va_list ap;
1039 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001040 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 va_end(ap);
1042 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001047 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001052 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 va_list ap;
1058 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001059 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_end(ap);
1061 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001066 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 }
1068
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001071 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 }
1073
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 va_list ap;
1077 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001078 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 va_end(ap);
1080 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001085 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001090 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 }
1092
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 va_list ap;
1096 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001097 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 va_end(ap);
1099 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001104 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001109 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 va_list ap;
1115 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001116 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_end(ap);
1118 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001123 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001128 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 }
1130
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 va_list ap;
1134 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001135 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 va_end(ap);
1137 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001142 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001147 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 va_list ap;
1153 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001154 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 va_end(ap);
1156 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001161 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001166 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 va_list ap;
1172 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001173 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001179 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001184 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
1187 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 ScopedJniThreadState ts(env);
1190 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001192 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001193 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 va_end(ap);
1195 return local_result;
1196 }
1197
1198 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001201 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001202 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
1205 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001208 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001209 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
1212 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 ScopedJniThreadState ts(env);
1215 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001217 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 va_end(ap);
1219 return result.z;
1220 }
1221
1222 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001225 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
1228 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001231 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
1234 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 ScopedJniThreadState ts(env);
1237 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001239 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 va_end(ap);
1241 return result.b;
1242 }
1243
1244 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001247 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
1250 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001253 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 }
1255
1256 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 ScopedJniThreadState ts(env);
1259 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001261 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 va_end(ap);
1263 return result.c;
1264 }
1265
1266 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001269 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
1272 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001275 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
1278 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
1281 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001283 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 va_end(ap);
1285 return result.s;
1286 }
1287
1288 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001289 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001291 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
1294 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001295 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001297 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
Elliott Hughes418d20f2011-09-22 14:00:39 -07001300 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 ScopedJniThreadState ts(env);
1302 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001304 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 va_end(ap);
1306 return result.i;
1307 }
1308
1309 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001310 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001312 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
1315 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001318 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
1321 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001322 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 ScopedJniThreadState ts(env);
1324 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001326 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 va_end(ap);
1328 return result.j;
1329 }
1330
1331 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001334 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
1337 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001340 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
1343 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 ScopedJniThreadState ts(env);
1346 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001348 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 va_end(ap);
1350 return result.f;
1351 }
1352
1353 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001356 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
1359 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001360 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001362 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
1365 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001366 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 ScopedJniThreadState ts(env);
1368 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001369 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001370 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 va_end(ap);
1372 return result.d;
1373 }
1374
1375 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001376 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001378 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
1380
1381 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001382 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001384 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
1386
1387 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001388 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 ScopedJniThreadState ts(env);
1390 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001391 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001392 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 va_end(ap);
1394 }
1395
1396 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001397 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001399 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
1402 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001403 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001405 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001408 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 ScopedJniThreadState ts(env);
1410 return FindFieldID(ts, c, name, sig, false);
1411 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001412
1413
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001414 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 ScopedJniThreadState ts(env);
1416 return FindFieldID(ts, c, name, sig, true);
1417 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001418
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001419 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001421 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001422 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001423 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001425
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001428 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001429 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 Object* o = Decode<Object*>(ts, java_object);
1435 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001436 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001437 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001443 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001444 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447#define GET_PRIMITIVE_FIELD(fn, instance) \
1448 ScopedJniThreadState ts(env); \
1449 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001450 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 return f->fn(o)
1452
1453#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1454 ScopedJniThreadState ts(env); \
1455 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001456 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001457 f->fn(o, value)
1458
1459 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1476 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1480 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001483 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1484 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001487 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1488 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1492 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1496 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1500 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1504 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 }
1506
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001507 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1508 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
1510
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1512 GET_PRIMITIVE_FIELD(GetLong, NULL);
1513 }
1514
1515 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1516 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1517 }
1518
1519 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1520 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1521 }
1522
1523 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1524 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1525 }
1526
1527 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1528 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1529 }
1530
1531 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1532 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1533 }
1534
1535 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1536 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1537 }
1538
1539 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1540 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1541 }
1542
1543 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1544 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1545 }
1546
1547 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1548 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1549 }
1550
1551 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1552 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1553 }
1554
1555 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1556 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1557 }
1558
1559 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1560 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1561 }
1562
1563 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1564 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1565 }
1566
1567 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1568 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1569 }
1570
1571 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1572 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1573 }
1574
1575 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1576 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1577 }
1578
1579 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1580 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1581 }
1582
1583 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1584 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes418d20f2011-09-22 14:00:39 -07001587 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 ScopedJniThreadState ts(env);
1589 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001590 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001591 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001592 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 va_end(ap);
1594 return local_result;
1595 }
1596
Elliott Hughes418d20f2011-09-22 14:00:39 -07001597 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001599 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001600 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes418d20f2011-09-22 14:00:39 -07001603 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001605 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001606 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes418d20f2011-09-22 14:00:39 -07001609 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 ScopedJniThreadState ts(env);
1611 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001612 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001613 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 va_end(ap);
1615 return result.z;
1616 }
1617
Elliott Hughes418d20f2011-09-22 14:00:39 -07001618 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001620 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 }
1622
Elliott Hughes418d20f2011-09-22 14:00:39 -07001623 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001625 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 }
1627
Elliott Hughes72025e52011-08-23 17:50:30 -07001628 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 ScopedJniThreadState ts(env);
1630 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001631 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001632 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 va_end(ap);
1634 return result.b;
1635 }
1636
Elliott Hughes418d20f2011-09-22 14:00:39 -07001637 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001639 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
Elliott Hughes418d20f2011-09-22 14:00:39 -07001642 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001644 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 }
1646
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 ScopedJniThreadState ts(env);
1649 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001650 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001651 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 va_end(ap);
1653 return result.c;
1654 }
1655
Elliott Hughes418d20f2011-09-22 14:00:39 -07001656 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001658 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 }
1660
Elliott Hughes418d20f2011-09-22 14:00:39 -07001661 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001663 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 }
1665
Elliott Hughes72025e52011-08-23 17:50:30 -07001666 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 ScopedJniThreadState ts(env);
1668 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001669 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001670 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 va_end(ap);
1672 return result.s;
1673 }
1674
Elliott Hughes418d20f2011-09-22 14:00:39 -07001675 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001677 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 }
1679
Elliott Hughes418d20f2011-09-22 14:00:39 -07001680 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001682 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 }
1684
Elliott Hughes72025e52011-08-23 17:50:30 -07001685 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 ScopedJniThreadState ts(env);
1687 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001688 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001689 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 va_end(ap);
1691 return result.i;
1692 }
1693
Elliott Hughes418d20f2011-09-22 14:00:39 -07001694 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001696 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 }
1698
Elliott Hughes418d20f2011-09-22 14:00:39 -07001699 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001701 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 }
1703
Elliott Hughes72025e52011-08-23 17:50:30 -07001704 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 ScopedJniThreadState ts(env);
1706 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001708 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 va_end(ap);
1710 return result.j;
1711 }
1712
Elliott Hughes418d20f2011-09-22 14:00:39 -07001713 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001715 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 }
1717
Elliott Hughes418d20f2011-09-22 14:00:39 -07001718 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001720 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 }
1722
Elliott Hughes72025e52011-08-23 17:50:30 -07001723 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 ScopedJniThreadState ts(env);
1725 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001726 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001727 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 va_end(ap);
1729 return result.f;
1730 }
1731
Elliott Hughes418d20f2011-09-22 14:00:39 -07001732 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001734 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 }
1736
Elliott Hughes418d20f2011-09-22 14:00:39 -07001737 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001739 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 }
1741
Elliott Hughes72025e52011-08-23 17:50:30 -07001742 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 ScopedJniThreadState ts(env);
1744 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001745 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001746 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 va_end(ap);
1748 return result.d;
1749 }
1750
Elliott Hughes418d20f2011-09-22 14:00:39 -07001751 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001753 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 }
1755
Elliott Hughes418d20f2011-09-22 14:00:39 -07001756 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001758 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes72025e52011-08-23 17:50:30 -07001761 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 ScopedJniThreadState ts(env);
1763 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001764 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001765 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 va_end(ap);
1767 }
1768
Elliott Hughes418d20f2011-09-22 14:00:39 -07001769 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001771 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 }
1773
Elliott Hughes418d20f2011-09-22 14:00:39 -07001774 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001776 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 }
1778
Elliott Hughes814e4032011-08-23 12:07:56 -07001779 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001781 if (chars == NULL && char_count == 0) {
1782 return NULL;
1783 }
1784 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001785 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 }
1787
1788 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1789 ScopedJniThreadState ts(env);
1790 if (utf == NULL) {
1791 return NULL;
1792 }
1793 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001794 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 }
1796
Elliott Hughes814e4032011-08-23 12:07:56 -07001797 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1798 ScopedJniThreadState ts(env);
1799 return Decode<String*>(ts, java_string)->GetLength();
1800 }
1801
1802 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1803 ScopedJniThreadState ts(env);
1804 return Decode<String*>(ts, java_string)->GetUtfLength();
1805 }
1806
Elliott Hughesb465ab02011-08-24 11:21:21 -07001807 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001809 String* s = Decode<String*>(ts, java_string);
1810 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1811 ThrowSIOOBE(ts, start, length, s->GetLength());
1812 } else {
1813 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1814 memcpy(buf, chars + start, length * sizeof(jchar));
1815 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001816 }
1817
Elliott Hughesb465ab02011-08-24 11:21:21 -07001818 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001819 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001820 String* s = Decode<String*>(ts, java_string);
1821 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1822 ThrowSIOOBE(ts, start, length, s->GetLength());
1823 } else {
1824 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1825 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1826 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001827 }
1828
Elliott Hughes75770752011-08-24 17:52:38 -07001829 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001830 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001831 String* s = Decode<String*>(ts, java_string);
1832 const CharArray* chars = s->GetCharArray();
1833 PinPrimitiveArray(ts, chars);
1834 if (is_copy != NULL) {
1835 *is_copy = JNI_FALSE;
1836 }
1837 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001838 }
1839
Elliott Hughes75770752011-08-24 17:52:38 -07001840 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001841 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001842 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 }
1844
Elliott Hughes75770752011-08-24 17:52:38 -07001845 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001847 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
Elliott Hughes75770752011-08-24 17:52:38 -07001850 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001852 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 }
1854
Elliott Hughes75770752011-08-24 17:52:38 -07001855 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001856 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001857 if (java_string == NULL) {
1858 return NULL;
1859 }
1860 if (is_copy != NULL) {
1861 *is_copy = JNI_TRUE;
1862 }
1863 String* s = Decode<String*>(ts, java_string);
1864 size_t byte_count = s->GetUtfLength();
1865 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001866 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001867 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1868 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1869 bytes[byte_count] = '\0';
1870 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001871 }
1872
Elliott Hughes75770752011-08-24 17:52:38 -07001873 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001874 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001875 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001876 }
1877
Elliott Hughesbd935992011-08-22 11:59:34 -07001878 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001880 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001881 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001882 Array* array = obj->AsArray();
1883 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001884 }
1885
Elliott Hughes814e4032011-08-23 12:07:56 -07001886 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001888 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001889 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001890 }
1891
1892 static void SetObjectArrayElement(JNIEnv* env,
1893 jobjectArray java_array, jsize index, jobject java_value) {
1894 ScopedJniThreadState ts(env);
1895 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1896 Object* value = Decode<Object*>(ts, java_value);
1897 array->Set(index, value);
1898 }
1899
1900 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1901 ScopedJniThreadState ts(env);
1902 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1903 }
1904
1905 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1906 ScopedJniThreadState ts(env);
1907 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1908 }
1909
1910 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1911 ScopedJniThreadState ts(env);
1912 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1913 }
1914
1915 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1916 ScopedJniThreadState ts(env);
1917 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1918 }
1919
1920 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1921 ScopedJniThreadState ts(env);
1922 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1923 }
1924
1925 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1926 ScopedJniThreadState ts(env);
1927 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1928 }
1929
1930 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1931 ScopedJniThreadState ts(env);
1932 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1933 }
1934
1935 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1936 ScopedJniThreadState ts(env);
1937 CHECK_GE(length, 0); // TODO: ReportJniError
1938
1939 // Compute the array class corresponding to the given element class.
1940 Class* element_class = Decode<Class*>(ts, element_jclass);
1941 std::string descriptor;
1942 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001943 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001944
1945 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001946 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1947 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 return NULL;
1949 }
1950
Elliott Hughes75770752011-08-24 17:52:38 -07001951 // Allocate and initialize if necessary.
1952 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001954 if (initial_element != NULL) {
1955 Object* initial_object = Decode<Object*>(ts, initial_element);
1956 for (jsize i = 0; i < length; ++i) {
1957 result->Set(i, initial_object);
1958 }
1959 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001960 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 }
1962
1963 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1964 ScopedJniThreadState ts(env);
1965 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1966 }
1967
Elliott Hughes75770752011-08-24 17:52:38 -07001968 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001969 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001970 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001971 }
1972
Elliott Hughes75770752011-08-24 17:52:38 -07001973 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001974 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001975 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001976 }
1977
Elliott Hughes75770752011-08-24 17:52:38 -07001978 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001980 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Elliott Hughes75770752011-08-24 17:52:38 -07001983 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001985 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Elliott Hughes75770752011-08-24 17:52:38 -07001988 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001990 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001995 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes75770752011-08-24 17:52:38 -07001998 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002000 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes75770752011-08-24 17:52:38 -07002003 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002005 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002010 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002015 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes75770752011-08-24 17:52:38 -07002018 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002020 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes75770752011-08-24 17:52:38 -07002023 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002025 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes75770752011-08-24 17:52:38 -07002028 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002030 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes75770752011-08-24 17:52:38 -07002033 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002035 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes75770752011-08-24 17:52:38 -07002038 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002040 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes75770752011-08-24 17:52:38 -07002043 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002045 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes75770752011-08-24 17:52:38 -07002048 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002050 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes75770752011-08-24 17:52:38 -07002053 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002055 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002070 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002130 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes5174fe62011-08-23 15:12:35 -07002138 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002140 Class* c = Decode<Class*>(ts, java_class);
2141
Elliott Hughes5174fe62011-08-23 15:12:35 -07002142 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 const char* name = methods[i].name;
2144 const char* sig = methods[i].signature;
2145
2146 if (*sig == '!') {
2147 // TODO: fast jni. it's too noisy to log all these.
2148 ++sig;
2149 }
2150
Elliott Hughes5174fe62011-08-23 15:12:35 -07002151 Method* m = c->FindDirectMethod(name, sig);
2152 if (m == NULL) {
2153 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155 if (m == NULL) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002156 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158 } else if (!m->IsNative()) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002159 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 return JNI_ERR;
2161 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162
Elliott Hughes75770752011-08-24 17:52:38 -07002163 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002164 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002165 }
2166
2167 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 }
2169 return JNI_OK;
2170 }
2171
Elliott Hughes5174fe62011-08-23 15:12:35 -07002172 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002174 Class* c = Decode<Class*>(ts, java_class);
2175
Elliott Hughes75770752011-08-24 17:52:38 -07002176 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002177 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002178 }
2179
2180 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2181 Method* m = c->GetDirectMethod(i);
2182 if (m->IsNative()) {
2183 m->UnregisterNative();
2184 }
2185 }
2186 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2187 Method* m = c->GetVirtualMethod(i);
2188 if (m->IsNative()) {
2189 m->UnregisterNative();
2190 }
2191 }
2192
2193 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
2195
Elliott Hughes72025e52011-08-23 17:50:30 -07002196 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002198 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002199 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
Elliott Hughes72025e52011-08-23 17:50:30 -07002202 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002204 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002205 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
2208 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2209 ScopedJniThreadState ts(env);
2210 Runtime* runtime = Runtime::Current();
2211 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002212 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 } else {
2214 *vm = NULL;
2215 }
2216 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2217 }
2218
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2220 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002221
2222 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002223 CHECK(address != NULL); // TODO: ReportJniError
2224 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002225
2226 jclass buffer_class = GetDirectByteBufferClass(env);
2227 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2228 if (mid == NULL) {
2229 return NULL;
2230 }
2231
2232 // At the moment, the Java side is limited to 32 bits.
2233 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2234 CHECK_LE(capacity, 0xffffffff);
2235 jint address_arg = reinterpret_cast<jint>(address);
2236 jint capacity_arg = static_cast<jint>(capacity);
2237
2238 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2239 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002240 }
2241
Elliott Hughesb465ab02011-08-24 11:21:21 -07002242 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002244 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2245 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 }
2247
Elliott Hughesb465ab02011-08-24 11:21:21 -07002248 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002250 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2251 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002252 }
2253
Elliott Hughesb465ab02011-08-24 11:21:21 -07002254 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002255 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002256
Elliott Hughes75770752011-08-24 17:52:38 -07002257 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002258
2259 // Do we definitely know what kind of reference this is?
2260 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2261 IndirectRefKind kind = GetIndirectRefKind(ref);
2262 switch (kind) {
2263 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002264 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2265 return JNILocalRefType;
2266 }
2267 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002268 case kGlobal:
2269 return JNIGlobalRefType;
2270 case kWeakGlobal:
2271 return JNIWeakGlobalRefType;
2272 case kSirtOrInvalid:
2273 // Is it in a stack IRT?
2274 if (ts.Self()->SirtContains(java_object)) {
2275 return JNILocalRefType;
2276 }
2277
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002278 if (!ts.Env()->work_around_app_jni_bugs) {
2279 return JNIInvalidRefType;
2280 }
2281
Elliott Hughesb465ab02011-08-24 11:21:21 -07002282 // If we're handing out direct pointers, check whether it's a direct pointer
2283 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002284 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002285 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002286 return JNILocalRefType;
2287 }
2288 }
2289
2290 return JNIInvalidRefType;
2291 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002292 }
2293};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002294
Elliott Hughesa2501992011-08-26 19:39:54 -07002295const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002296 NULL, // reserved0.
2297 NULL, // reserved1.
2298 NULL, // reserved2.
2299 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002300 JNI::GetVersion,
2301 JNI::DefineClass,
2302 JNI::FindClass,
2303 JNI::FromReflectedMethod,
2304 JNI::FromReflectedField,
2305 JNI::ToReflectedMethod,
2306 JNI::GetSuperclass,
2307 JNI::IsAssignableFrom,
2308 JNI::ToReflectedField,
2309 JNI::Throw,
2310 JNI::ThrowNew,
2311 JNI::ExceptionOccurred,
2312 JNI::ExceptionDescribe,
2313 JNI::ExceptionClear,
2314 JNI::FatalError,
2315 JNI::PushLocalFrame,
2316 JNI::PopLocalFrame,
2317 JNI::NewGlobalRef,
2318 JNI::DeleteGlobalRef,
2319 JNI::DeleteLocalRef,
2320 JNI::IsSameObject,
2321 JNI::NewLocalRef,
2322 JNI::EnsureLocalCapacity,
2323 JNI::AllocObject,
2324 JNI::NewObject,
2325 JNI::NewObjectV,
2326 JNI::NewObjectA,
2327 JNI::GetObjectClass,
2328 JNI::IsInstanceOf,
2329 JNI::GetMethodID,
2330 JNI::CallObjectMethod,
2331 JNI::CallObjectMethodV,
2332 JNI::CallObjectMethodA,
2333 JNI::CallBooleanMethod,
2334 JNI::CallBooleanMethodV,
2335 JNI::CallBooleanMethodA,
2336 JNI::CallByteMethod,
2337 JNI::CallByteMethodV,
2338 JNI::CallByteMethodA,
2339 JNI::CallCharMethod,
2340 JNI::CallCharMethodV,
2341 JNI::CallCharMethodA,
2342 JNI::CallShortMethod,
2343 JNI::CallShortMethodV,
2344 JNI::CallShortMethodA,
2345 JNI::CallIntMethod,
2346 JNI::CallIntMethodV,
2347 JNI::CallIntMethodA,
2348 JNI::CallLongMethod,
2349 JNI::CallLongMethodV,
2350 JNI::CallLongMethodA,
2351 JNI::CallFloatMethod,
2352 JNI::CallFloatMethodV,
2353 JNI::CallFloatMethodA,
2354 JNI::CallDoubleMethod,
2355 JNI::CallDoubleMethodV,
2356 JNI::CallDoubleMethodA,
2357 JNI::CallVoidMethod,
2358 JNI::CallVoidMethodV,
2359 JNI::CallVoidMethodA,
2360 JNI::CallNonvirtualObjectMethod,
2361 JNI::CallNonvirtualObjectMethodV,
2362 JNI::CallNonvirtualObjectMethodA,
2363 JNI::CallNonvirtualBooleanMethod,
2364 JNI::CallNonvirtualBooleanMethodV,
2365 JNI::CallNonvirtualBooleanMethodA,
2366 JNI::CallNonvirtualByteMethod,
2367 JNI::CallNonvirtualByteMethodV,
2368 JNI::CallNonvirtualByteMethodA,
2369 JNI::CallNonvirtualCharMethod,
2370 JNI::CallNonvirtualCharMethodV,
2371 JNI::CallNonvirtualCharMethodA,
2372 JNI::CallNonvirtualShortMethod,
2373 JNI::CallNonvirtualShortMethodV,
2374 JNI::CallNonvirtualShortMethodA,
2375 JNI::CallNonvirtualIntMethod,
2376 JNI::CallNonvirtualIntMethodV,
2377 JNI::CallNonvirtualIntMethodA,
2378 JNI::CallNonvirtualLongMethod,
2379 JNI::CallNonvirtualLongMethodV,
2380 JNI::CallNonvirtualLongMethodA,
2381 JNI::CallNonvirtualFloatMethod,
2382 JNI::CallNonvirtualFloatMethodV,
2383 JNI::CallNonvirtualFloatMethodA,
2384 JNI::CallNonvirtualDoubleMethod,
2385 JNI::CallNonvirtualDoubleMethodV,
2386 JNI::CallNonvirtualDoubleMethodA,
2387 JNI::CallNonvirtualVoidMethod,
2388 JNI::CallNonvirtualVoidMethodV,
2389 JNI::CallNonvirtualVoidMethodA,
2390 JNI::GetFieldID,
2391 JNI::GetObjectField,
2392 JNI::GetBooleanField,
2393 JNI::GetByteField,
2394 JNI::GetCharField,
2395 JNI::GetShortField,
2396 JNI::GetIntField,
2397 JNI::GetLongField,
2398 JNI::GetFloatField,
2399 JNI::GetDoubleField,
2400 JNI::SetObjectField,
2401 JNI::SetBooleanField,
2402 JNI::SetByteField,
2403 JNI::SetCharField,
2404 JNI::SetShortField,
2405 JNI::SetIntField,
2406 JNI::SetLongField,
2407 JNI::SetFloatField,
2408 JNI::SetDoubleField,
2409 JNI::GetStaticMethodID,
2410 JNI::CallStaticObjectMethod,
2411 JNI::CallStaticObjectMethodV,
2412 JNI::CallStaticObjectMethodA,
2413 JNI::CallStaticBooleanMethod,
2414 JNI::CallStaticBooleanMethodV,
2415 JNI::CallStaticBooleanMethodA,
2416 JNI::CallStaticByteMethod,
2417 JNI::CallStaticByteMethodV,
2418 JNI::CallStaticByteMethodA,
2419 JNI::CallStaticCharMethod,
2420 JNI::CallStaticCharMethodV,
2421 JNI::CallStaticCharMethodA,
2422 JNI::CallStaticShortMethod,
2423 JNI::CallStaticShortMethodV,
2424 JNI::CallStaticShortMethodA,
2425 JNI::CallStaticIntMethod,
2426 JNI::CallStaticIntMethodV,
2427 JNI::CallStaticIntMethodA,
2428 JNI::CallStaticLongMethod,
2429 JNI::CallStaticLongMethodV,
2430 JNI::CallStaticLongMethodA,
2431 JNI::CallStaticFloatMethod,
2432 JNI::CallStaticFloatMethodV,
2433 JNI::CallStaticFloatMethodA,
2434 JNI::CallStaticDoubleMethod,
2435 JNI::CallStaticDoubleMethodV,
2436 JNI::CallStaticDoubleMethodA,
2437 JNI::CallStaticVoidMethod,
2438 JNI::CallStaticVoidMethodV,
2439 JNI::CallStaticVoidMethodA,
2440 JNI::GetStaticFieldID,
2441 JNI::GetStaticObjectField,
2442 JNI::GetStaticBooleanField,
2443 JNI::GetStaticByteField,
2444 JNI::GetStaticCharField,
2445 JNI::GetStaticShortField,
2446 JNI::GetStaticIntField,
2447 JNI::GetStaticLongField,
2448 JNI::GetStaticFloatField,
2449 JNI::GetStaticDoubleField,
2450 JNI::SetStaticObjectField,
2451 JNI::SetStaticBooleanField,
2452 JNI::SetStaticByteField,
2453 JNI::SetStaticCharField,
2454 JNI::SetStaticShortField,
2455 JNI::SetStaticIntField,
2456 JNI::SetStaticLongField,
2457 JNI::SetStaticFloatField,
2458 JNI::SetStaticDoubleField,
2459 JNI::NewString,
2460 JNI::GetStringLength,
2461 JNI::GetStringChars,
2462 JNI::ReleaseStringChars,
2463 JNI::NewStringUTF,
2464 JNI::GetStringUTFLength,
2465 JNI::GetStringUTFChars,
2466 JNI::ReleaseStringUTFChars,
2467 JNI::GetArrayLength,
2468 JNI::NewObjectArray,
2469 JNI::GetObjectArrayElement,
2470 JNI::SetObjectArrayElement,
2471 JNI::NewBooleanArray,
2472 JNI::NewByteArray,
2473 JNI::NewCharArray,
2474 JNI::NewShortArray,
2475 JNI::NewIntArray,
2476 JNI::NewLongArray,
2477 JNI::NewFloatArray,
2478 JNI::NewDoubleArray,
2479 JNI::GetBooleanArrayElements,
2480 JNI::GetByteArrayElements,
2481 JNI::GetCharArrayElements,
2482 JNI::GetShortArrayElements,
2483 JNI::GetIntArrayElements,
2484 JNI::GetLongArrayElements,
2485 JNI::GetFloatArrayElements,
2486 JNI::GetDoubleArrayElements,
2487 JNI::ReleaseBooleanArrayElements,
2488 JNI::ReleaseByteArrayElements,
2489 JNI::ReleaseCharArrayElements,
2490 JNI::ReleaseShortArrayElements,
2491 JNI::ReleaseIntArrayElements,
2492 JNI::ReleaseLongArrayElements,
2493 JNI::ReleaseFloatArrayElements,
2494 JNI::ReleaseDoubleArrayElements,
2495 JNI::GetBooleanArrayRegion,
2496 JNI::GetByteArrayRegion,
2497 JNI::GetCharArrayRegion,
2498 JNI::GetShortArrayRegion,
2499 JNI::GetIntArrayRegion,
2500 JNI::GetLongArrayRegion,
2501 JNI::GetFloatArrayRegion,
2502 JNI::GetDoubleArrayRegion,
2503 JNI::SetBooleanArrayRegion,
2504 JNI::SetByteArrayRegion,
2505 JNI::SetCharArrayRegion,
2506 JNI::SetShortArrayRegion,
2507 JNI::SetIntArrayRegion,
2508 JNI::SetLongArrayRegion,
2509 JNI::SetFloatArrayRegion,
2510 JNI::SetDoubleArrayRegion,
2511 JNI::RegisterNatives,
2512 JNI::UnregisterNatives,
2513 JNI::MonitorEnter,
2514 JNI::MonitorExit,
2515 JNI::GetJavaVM,
2516 JNI::GetStringRegion,
2517 JNI::GetStringUTFRegion,
2518 JNI::GetPrimitiveArrayCritical,
2519 JNI::ReleasePrimitiveArrayCritical,
2520 JNI::GetStringCritical,
2521 JNI::ReleaseStringCritical,
2522 JNI::NewWeakGlobalRef,
2523 JNI::DeleteWeakGlobalRef,
2524 JNI::ExceptionCheck,
2525 JNI::NewDirectByteBuffer,
2526 JNI::GetDirectBufferAddress,
2527 JNI::GetDirectBufferCapacity,
2528 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002529};
2530
Elliott Hughes75770752011-08-24 17:52:38 -07002531JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002532 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002533 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002534 local_ref_cookie(IRT_FIRST_SEGMENT),
2535 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002536 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002537 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002538 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002539 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002540 functions = unchecked_functions = &gNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002541 if (vm->check_jni) {
2542 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002543 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002544 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2545 // errors will ensue.
2546 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2547 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002548}
2549
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002550JNIEnvExt::~JNIEnvExt() {
2551}
2552
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002553void JNIEnvExt::EnableCheckJni() {
2554 check_jni = true;
2555 functions = GetCheckJniNativeInterface();
2556}
2557
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002558void JNIEnvExt::DumpReferenceTables() {
2559 locals.Dump();
2560 monitors.Dump();
2561}
2562
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002563void JNIEnvExt::PushFrame(int capacity) {
2564 stacked_local_ref_cookies.push_back(local_ref_cookie);
2565 local_ref_cookie = locals.GetSegmentState();
2566}
2567
2568void JNIEnvExt::PopFrame() {
2569 locals.SetSegmentState(local_ref_cookie);
2570 local_ref_cookie = stacked_local_ref_cookies.back();
2571 stacked_local_ref_cookies.pop_back();
2572}
2573
Carl Shapiroea4dca82011-08-01 13:45:38 -07002574// JNI Invocation interface.
2575
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002576extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2577 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2578 if (args->version < JNI_VERSION_1_2) {
2579 return JNI_EVERSION;
2580 }
2581 Runtime::Options options;
2582 for (int i = 0; i < args->nOptions; ++i) {
2583 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002584 options.push_back(std::make_pair(StringPiece(option->optionString),
2585 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002586 }
2587 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002588 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002589 if (runtime == NULL) {
2590 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002591 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002592 runtime->Start();
2593 *p_env = Thread::Current()->GetJniEnv();
2594 *p_vm = runtime->GetJavaVM();
2595 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002596}
2597
Elliott Hughesf2682d52011-08-15 16:37:04 -07002598extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002599 Runtime* runtime = Runtime::Current();
2600 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002601 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002602 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002603 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002604 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002605 }
2606 return JNI_OK;
2607}
2608
2609// Historically unsupported.
2610extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2611 return JNI_ERR;
2612}
2613
Elliott Hughescdf53122011-08-19 15:46:09 -07002614class JII {
2615 public:
2616 static jint DestroyJavaVM(JavaVM* vm) {
2617 if (vm == NULL) {
2618 return JNI_ERR;
2619 } else {
2620 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2621 delete raw_vm->runtime;
2622 return JNI_OK;
2623 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002624 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002625
Elliott Hughescdf53122011-08-19 15:46:09 -07002626 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002627 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002628 }
2629
2630 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002631 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002632 }
2633
2634 static jint DetachCurrentThread(JavaVM* vm) {
2635 if (vm == NULL) {
2636 return JNI_ERR;
2637 } else {
2638 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2639 Runtime* runtime = raw_vm->runtime;
2640 runtime->DetachCurrentThread();
2641 return JNI_OK;
2642 }
2643 }
2644
2645 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2646 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2647 return JNI_EVERSION;
2648 }
2649 if (vm == NULL || env == NULL) {
2650 return JNI_ERR;
2651 }
2652 Thread* thread = Thread::Current();
2653 if (thread == NULL) {
2654 *env = NULL;
2655 return JNI_EDETACHED;
2656 }
2657 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002658 return JNI_OK;
2659 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002660};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002661
Elliott Hughesa2501992011-08-26 19:39:54 -07002662const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002663 NULL, // reserved0
2664 NULL, // reserved1
2665 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002666 JII::DestroyJavaVM,
2667 JII::AttachCurrentThread,
2668 JII::DetachCurrentThread,
2669 JII::GetEnv,
2670 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002671};
2672
Elliott Hughesa0957642011-09-02 14:27:33 -07002673JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002674 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002675 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002676 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002677 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002678 verbose_jni(options->IsVerbose("jni")),
2679 log_third_party_jni(options->IsVerbose("third-party-jni")),
2680 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002681 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002682 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002683 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002684 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002685 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002686 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002687 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002688 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002689 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002690 functions = unchecked_functions = &gInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002691 if (options->check_jni_) {
2692 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002693 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002694}
2695
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002696JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002697 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002698}
2699
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002700void JavaVMExt::EnableCheckJni() {
2701 check_jni = true;
2702 functions = GetCheckJniInvokeInterface();
2703}
2704
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002705void JavaVMExt::DumpReferenceTables() {
2706 {
2707 MutexLock mu(globals_lock);
2708 globals.Dump();
2709 }
2710 {
2711 MutexLock mu(weak_globals_lock);
2712 weak_globals.Dump();
2713 }
2714 {
2715 MutexLock mu(pins_lock);
2716 pin_table.Dump();
2717 }
2718}
2719
Elliott Hughes75770752011-08-24 17:52:38 -07002720bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2721 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002722
2723 // See if we've already loaded this library. If we have, and the class loader
2724 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002725 // TODO: for better results we should canonicalize the pathname (or even compare
2726 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002727 SharedLibrary* library;
2728 {
2729 // TODO: move the locking (and more of this logic) into Libraries.
2730 MutexLock mu(libraries_lock);
2731 library = libraries->Get(path);
2732 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002733 if (library != NULL) {
2734 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002735 // The library will be associated with class_loader. The JNI
2736 // spec says we can't load the same library into more than one
2737 // class loader.
2738 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2739 "ClassLoader %p; can't open in ClassLoader %p",
2740 path.c_str(), library->GetClassLoader(), class_loader);
2741 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002742 return false;
2743 }
2744 if (verbose_jni) {
2745 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2746 << "ClassLoader " << class_loader << "]";
2747 }
2748 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002749 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2750 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002751 return false;
2752 }
2753 return true;
2754 }
2755
2756 // Open the shared library. Because we're using a full path, the system
2757 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2758 // resolve this library's dependencies though.)
2759
2760 // Failures here are expected when java.library.path has several entries
2761 // and we have to hunt for the lib.
2762
2763 // The current version of the dynamic linker prints detailed information
2764 // about dlopen() failures. Some things to check if the message is
2765 // cryptic:
2766 // - make sure the library exists on the device
2767 // - verify that the right path is being opened (the debug log message
2768 // above can help with that)
2769 // - check to see if the library is valid (e.g. not zero bytes long)
2770 // - check config/prelink-linux-arm.map to ensure that the library
2771 // is listed and is not being overrun by the previous entry (if
2772 // loading suddenly stops working on a prelinked library, this is
2773 // a good one to check)
2774 // - write a trivial app that calls sleep() then dlopen(), attach
2775 // to it with "strace -p <pid>" while it sleeps, and watch for
2776 // attempts to open nonexistent dependent shared libs
2777
2778 // TODO: automate some of these checks!
2779
2780 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002781 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002782 // the GC to ignore us.
2783 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002784 void* handle = NULL;
2785 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002786 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002787 handle = dlopen(path.c_str(), RTLD_LAZY);
2788 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002789
2790 if (verbose_jni) {
2791 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2792 }
2793
2794 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002795 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002796 return false;
2797 }
2798
2799 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002800 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002801 // TODO: move the locking (and more of this logic) into Libraries.
2802 MutexLock mu(libraries_lock);
2803 library = libraries->Get(path);
2804 if (library != NULL) {
2805 LOG(INFO) << "WOW: we lost a race to add shared library: "
2806 << "\"" << path << "\" ClassLoader=" << class_loader;
2807 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002808 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002809 library = new SharedLibrary(path, handle, class_loader);
2810 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002811 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002812
2813 if (verbose_jni) {
2814 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2815 }
2816
2817 bool result = true;
2818 void* sym = dlsym(handle, "JNI_OnLoad");
2819 if (sym == NULL) {
2820 if (verbose_jni) {
2821 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2822 }
2823 } else {
2824 // Call JNI_OnLoad. We have to override the current class
2825 // loader, which will always be "null" since the stuff at the
2826 // top of the stack is around Runtime.loadLibrary(). (See
2827 // the comments in the JNI FindClass function.)
2828 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2829 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002830 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002831 self->SetClassLoaderOverride(class_loader);
2832
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002833 int version = 0;
2834 {
2835 ScopedThreadStateChange tsc(self, Thread::kNative);
2836 if (verbose_jni) {
2837 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2838 }
2839 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002840 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002841
Brian Carlstromaded5f72011-10-07 17:15:04 -07002842 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002843
2844 if (version != JNI_VERSION_1_2 &&
2845 version != JNI_VERSION_1_4 &&
2846 version != JNI_VERSION_1_6) {
2847 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2848 << "bad version: " << version;
2849 // It's unwise to call dlclose() here, but we can mark it
2850 // as bad and ensure that future load attempts will fail.
2851 // We don't know how far JNI_OnLoad got, so there could
2852 // be some partially-initialized stuff accessible through
2853 // newly-registered native method calls. We could try to
2854 // unregister them, but that doesn't seem worthwhile.
2855 result = false;
2856 } else {
2857 if (verbose_jni) {
2858 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2859 << " from JNI_OnLoad in \"" << path << "\"]";
2860 }
2861 }
2862 }
2863
2864 library->SetResult(result);
2865 return result;
2866}
2867
2868void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2869 CHECK(m->IsNative());
2870
2871 Class* c = m->GetDeclaringClass();
2872
2873 // If this is a static method, it could be called before the class
2874 // has been initialized.
2875 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002876 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002877 return NULL;
2878 }
2879 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002880 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002881 }
2882
Brian Carlstrom16192862011-09-12 17:50:06 -07002883 std::string detail;
2884 void* native_method;
2885 {
2886 MutexLock mu(libraries_lock);
2887 native_method = libraries->FindNativeMethod(m, detail);
2888 }
2889 // throwing can cause libraries_lock to be reacquired
2890 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002891 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002892 }
2893 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002894}
2895
Elliott Hughes410c0c82011-09-01 17:58:25 -07002896void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2897 {
2898 MutexLock mu(globals_lock);
2899 globals.VisitRoots(visitor, arg);
2900 }
2901 {
2902 MutexLock mu(pins_lock);
2903 pin_table.VisitRoots(visitor, arg);
2904 }
2905 // The weak_globals table is visited by the GC itself (because it mutates the table).
2906}
2907
Ian Rogersdf20fe02011-07-20 20:34:16 -07002908} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002909
2910std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2911 switch (rhs) {
2912 case JNIInvalidRefType:
2913 os << "JNIInvalidRefType";
2914 return os;
2915 case JNILocalRefType:
2916 os << "JNILocalRefType";
2917 return os;
2918 case JNIGlobalRefType:
2919 os << "JNIGlobalRefType";
2920 return os;
2921 case JNIWeakGlobalRefType:
2922 os << "JNIWeakGlobalRefType";
2923 return os;
2924 }
2925}