blob: 02c8513734bfb9604d34a2428391d40ba4d8217e [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
Elliott Hughescdf53122011-08-19 15:46:09 -0700313jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
314 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700315 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700316 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700317 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700318
319 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 Class* field_type;
321 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
322 if (sig[1] != '\0') {
323 // TODO: need to get the appropriate ClassLoader.
324 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
325 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700326 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700327 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700328 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 if (field_type == NULL) {
330 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700331 DCHECK(ts.Self()->IsExceptionPending());
332 ts.Self()->ClearException();
333 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700334 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700335 "no type \"%s\" found and so no field \"%s\" could be found in class "
336 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 return NULL;
338 }
339 if (is_static) {
340 field = c->FindStaticField(name, field_type);
341 } else {
342 field = c->FindInstanceField(name, field_type);
343 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700344 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700345 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700346 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700347 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700348 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700349 return NULL;
350 }
Elliott Hughes80609252011-09-23 17:24:51 -0700351 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700352 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700353}
354
Elliott Hughes75770752011-08-24 17:52:38 -0700355void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
356 JavaVMExt* vm = ts.Vm();
357 MutexLock mu(vm->pins_lock);
358 vm->pin_table.Add(array);
359}
360
361void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
362 JavaVMExt* vm = ts.Vm();
363 MutexLock mu(vm->pins_lock);
364 vm->pin_table.Remove(array);
365}
366
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700367template<typename JniT, typename ArtT>
368JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
369 CHECK_GE(length, 0); // TODO: ReportJniError
370 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700371 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700372}
373
Elliott Hughes75770752011-08-24 17:52:38 -0700374template <typename ArrayT, typename CArrayT, typename ArtArrayT>
375CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
376 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
377 PinPrimitiveArray(ts, array);
378 if (is_copy != NULL) {
379 *is_copy = JNI_FALSE;
380 }
381 return array->GetData();
382}
383
384template <typename ArrayT>
385void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
386 if (mode != JNI_COMMIT) {
387 Array* array = Decode<Array*>(ts, java_array);
388 UnpinPrimitiveArray(ts, array);
389 }
390}
391
Elliott Hughes814e4032011-08-23 12:07:56 -0700392void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700393 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700394 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700395 "%s offset=%d length=%d %s.length=%d",
396 type.c_str(), start, length, identifier, array->GetLength());
397}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700398void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700399 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700400 "offset=%d length=%d string.length()=%d", start, length, array_length);
401}
Elliott Hughes814e4032011-08-23 12:07:56 -0700402
403template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700404void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700405 ArrayT* array = Decode<ArrayT*>(ts, java_array);
406 if (start < 0 || length < 0 || start + length > array->GetLength()) {
407 ThrowAIOOBE(ts, array, start, length, "src");
408 } else {
409 JavaT* data = array->GetData();
410 memcpy(buf, data + start, length * sizeof(JavaT));
411 }
412}
413
414template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700415void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700416 ArrayT* array = Decode<ArrayT*>(ts, java_array);
417 if (start < 0 || length < 0 || start + length > array->GetLength()) {
418 ThrowAIOOBE(ts, array, start, length, "dst");
419 } else {
420 JavaT* data = array->GetData();
421 memcpy(data + start, buf, length * sizeof(JavaT));
422 }
423}
424
Elliott Hughes75770752011-08-24 17:52:38 -0700425jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700426 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
427 CHECK(buffer_class.get() != NULL);
428 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
429}
430
Elliott Hughes75770752011-08-24 17:52:38 -0700431jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700432 static jclass buffer_class = InitDirectByteBufferClass(env);
433 return buffer_class;
434}
435
Elliott Hughes75770752011-08-24 17:52:38 -0700436jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
437 if (vm == NULL || p_env == NULL) {
438 return JNI_ERR;
439 }
440
441 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
442 JavaVMAttachArgs args;
443 if (thr_args == NULL) {
444 // Allow the v1.1 calling convention.
445 args.version = JNI_VERSION_1_2;
446 args.name = NULL;
447 args.group = NULL; // TODO: get "main" thread group
448 } else {
449 args.version = in_args->version;
450 args.name = in_args->name;
451 if (in_args->group != NULL) {
452 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
453 args.group = NULL; // TODO: decode in_args->group
454 } else {
455 args.group = NULL; // TODO: get "main" thread group
456 }
457 }
458 CHECK_GE(args.version, JNI_VERSION_1_2);
459
460 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700461 runtime->AttachCurrentThread(args.name, as_daemon);
462 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700463 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700464}
465
Elliott Hughes79082e32011-08-25 12:07:32 -0700466class SharedLibrary {
467 public:
468 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
469 : path_(path),
470 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700471 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700472 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700473 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700474 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700475 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700476 }
477
Elliott Hughes79082e32011-08-25 12:07:32 -0700478 Object* GetClassLoader() {
479 return class_loader_;
480 }
481
482 std::string GetPath() {
483 return path_;
484 }
485
486 /*
487 * Check the result of an earlier call to JNI_OnLoad on this library. If
488 * the call has not yet finished in another thread, wait for it.
489 */
490 bool CheckOnLoadResult(JavaVMExt* vm) {
491 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700492 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700493 // Check this so we don't end up waiting for ourselves. We need
494 // to return "true" so the caller can continue.
495 LOG(INFO) << *self << " recursive attempt to load library "
496 << "\"" << path_ << "\"";
497 return true;
498 }
499
500 MutexLock mu(jni_on_load_lock_);
501 while (jni_on_load_result_ == kPending) {
502 if (vm->verbose_jni) {
503 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
504 << "JNI_OnLoad...]";
505 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700506 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700507 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700508 }
509
510 bool okay = (jni_on_load_result_ == kOkay);
511 if (vm->verbose_jni) {
512 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
513 << (okay ? "succeeded" : "failed") << "]";
514 }
515 return okay;
516 }
517
518 void SetResult(bool result) {
519 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700520 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700521
522 // Broadcast a wakeup to anybody sleeping on the condition variable.
523 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700524 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700525 }
526
527 void* FindSymbol(const std::string& symbol_name) {
528 return dlsym(handle_, symbol_name.c_str());
529 }
530
531 private:
532 enum JNI_OnLoadState {
533 kPending,
534 kFailed,
535 kOkay,
536 };
537
538 // Path to library "/system/lib/libjni.so".
539 std::string path_;
540
541 // The void* returned by dlopen(3).
542 void* handle_;
543
544 // The ClassLoader this library is associated with.
545 Object* class_loader_;
546
547 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700548 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700549 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700550 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700551 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700552 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700553 // Result of earlier JNI_OnLoad call.
554 JNI_OnLoadState jni_on_load_result_;
555};
556
Elliott Hughescdf53122011-08-19 15:46:09 -0700557} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700558
Elliott Hughes79082e32011-08-25 12:07:32 -0700559// This exists mainly to keep implementation details out of the header file.
560class Libraries {
561 public:
562 Libraries() {
563 }
564
565 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700566 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700567 }
568
569 SharedLibrary* Get(const std::string& path) {
570 return libraries_[path];
571 }
572
573 void Put(const std::string& path, SharedLibrary* library) {
574 libraries_[path] = library;
575 }
576
577 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700578 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700579 std::string jni_short_name(JniShortName(m));
580 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700581 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700582 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
583 SharedLibrary* library = it->second;
584 if (library->GetClassLoader() != declaring_class_loader) {
585 // We only search libraries loaded by the appropriate ClassLoader.
586 continue;
587 }
588 // Try the short name then the long name...
589 void* fn = library->FindSymbol(jni_short_name);
590 if (fn == NULL) {
591 fn = library->FindSymbol(jni_long_name);
592 }
593 if (fn != NULL) {
594 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700595 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700596 << " in \"" << library->GetPath() << "\"]";
597 }
598 return fn;
599 }
600 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700601 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700602 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700603 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700604 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700605 return NULL;
606 }
607
608 private:
609 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
610
611 std::map<std::string, SharedLibrary*> libraries_;
612};
613
Elliott Hughes418d20f2011-09-22 14:00:39 -0700614JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
615 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
616 Object* receiver = Decode<Object*>(env, obj);
617 Method* method = DecodeMethod(mid);
618 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
619 return InvokeWithArgArray(env, receiver, method, arg_array.get());
620}
621
Elliott Hughescdf53122011-08-19 15:46:09 -0700622class JNI {
623 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700624
Elliott Hughescdf53122011-08-19 15:46:09 -0700625 static jint GetVersion(JNIEnv* env) {
626 ScopedJniThreadState ts(env);
627 return JNI_VERSION_1_6;
628 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700629
Elliott Hughescdf53122011-08-19 15:46:09 -0700630 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
631 ScopedJniThreadState ts(env);
632 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700633 return NULL;
634 }
635
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 static jclass FindClass(JNIEnv* env, const char* name) {
637 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700638 Runtime* runtime = Runtime::Current();
639 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700640 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700641 Class* c = NULL;
642 if (runtime->IsStarted()) {
643 // TODO: need to get the appropriate ClassLoader.
644 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
645 c = class_linker->FindClass(descriptor, cl);
646 } else {
647 c = class_linker->FindSystemClass(descriptor);
648 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700649 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700650 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700651
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
653 ScopedJniThreadState ts(env);
654 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700655 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700656 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700657
Elliott Hughescdf53122011-08-19 15:46:09 -0700658 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
659 ScopedJniThreadState ts(env);
660 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700661 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700662 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700663
Elliott Hughescdf53122011-08-19 15:46:09 -0700664 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
665 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700666 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700667 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700668 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700669
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
671 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700672 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700673 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700675
Elliott Hughes37f7a402011-08-22 18:56:01 -0700676 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
677 ScopedJniThreadState ts(env);
678 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700679 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700680 }
681
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700682 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700684 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700685 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700687
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700690 Class* c1 = Decode<Class*>(ts, java_class1);
691 Class* c2 = Decode<Class*>(ts, java_class2);
692 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700694
Elliott Hughes37f7a402011-08-22 18:56:01 -0700695 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700697 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700699 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700700 return JNI_TRUE;
701 } else {
702 Object* obj = Decode<Object*>(ts, jobj);
703 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700704 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700705 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700706 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700707
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 static jint Throw(JNIEnv* env, jthrowable java_exception) {
709 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700710 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700711 if (exception == NULL) {
712 return JNI_ERR;
713 }
714 ts.Self()->SetException(exception);
715 return JNI_OK;
716 }
717
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700718 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700719 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700720 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700721 jmethodID mid = ((msg != NULL)
722 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
723 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700724 if (mid == NULL) {
725 return JNI_ERR;
726 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700727 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700728 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700729 return JNI_ERR;
730 }
731
732 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700733 args[0].l = s.get();
734 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
735 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700736 return JNI_ERR;
737 }
738
Elliott Hughes72025e52011-08-23 17:50:30 -0700739 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700740
Elliott Hughes37f7a402011-08-22 18:56:01 -0700741 return JNI_OK;
742 }
743
744 static jboolean ExceptionCheck(JNIEnv* env) {
745 ScopedJniThreadState ts(env);
746 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
747 }
748
749 static void ExceptionClear(JNIEnv* env) {
750 ScopedJniThreadState ts(env);
751 ts.Self()->ClearException();
752 }
753
754 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700756
757 Thread* self = ts.Self();
758 Throwable* original_exception = self->GetException();
759 self->ClearException();
760
Elliott Hughesbf86d042011-08-31 17:53:14 -0700761 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700762 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
763 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
764 if (mid == NULL) {
765 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700766 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700767 } else {
768 env->CallVoidMethod(exception.get(), mid);
769 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700770 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700771 << " thrown while calling printStackTrace";
772 self->ClearException();
773 }
774 }
775
776 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700778
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 static jthrowable ExceptionOccurred(JNIEnv* env) {
780 ScopedJniThreadState ts(env);
781 Object* exception = ts.Self()->GetException();
782 if (exception == NULL) {
783 return NULL;
784 } else {
785 // TODO: if adding a local reference failing causes the VM to abort
786 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700787 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700788 if (localException == NULL) {
789 // We were unable to add a new local reference, and threw a new
790 // exception. We can't return "exception", because it's not a
791 // local reference. So we have to return NULL, indicating that
792 // there was no exception, even though it's pretty much raining
793 // exceptions in here.
794 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
795 }
796 return localException;
797 }
798 }
799
Elliott Hughescdf53122011-08-19 15:46:09 -0700800 static void FatalError(JNIEnv* env, const char* msg) {
801 ScopedJniThreadState ts(env);
802 LOG(FATAL) << "JNI FatalError called: " << msg;
803 }
804
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700805 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700807 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
808 return JNI_ERR;
809 }
810 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 return JNI_OK;
812 }
813
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700814 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700816 Object* survivor = Decode<Object*>(ts, java_survivor);
817 ts.Env()->PopFrame();
818 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 }
820
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700821 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700822 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700823 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
824 }
825
826 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
827 // TODO: we should try to expand the table if necessary.
828 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
829 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
830 return JNI_ERR;
831 }
832 // TODO: this isn't quite right, since "capacity" includes holes.
833 size_t capacity = ts.Env()->locals.Capacity();
834 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
835 if (!okay) {
836 ts.Self()->ThrowOutOfMemoryError(caller);
837 }
838 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700839 }
840
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
842 ScopedJniThreadState ts(env);
843 if (obj == NULL) {
844 return NULL;
845 }
846
Elliott Hughes75770752011-08-24 17:52:38 -0700847 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 IndirectReferenceTable& globals = vm->globals;
849 MutexLock mu(vm->globals_lock);
850 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
851 return reinterpret_cast<jobject>(ref);
852 }
853
854 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
855 ScopedJniThreadState ts(env);
856 if (obj == NULL) {
857 return;
858 }
859
Elliott Hughes75770752011-08-24 17:52:38 -0700860 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 IndirectReferenceTable& globals = vm->globals;
862 MutexLock mu(vm->globals_lock);
863
864 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
865 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
866 << "failed to find entry";
867 }
868 }
869
870 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
871 ScopedJniThreadState ts(env);
872 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
873 }
874
875 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
876 ScopedJniThreadState ts(env);
877 if (obj == NULL) {
878 return;
879 }
880
Elliott Hughes75770752011-08-24 17:52:38 -0700881 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 IndirectReferenceTable& weak_globals = vm->weak_globals;
883 MutexLock mu(vm->weak_globals_lock);
884
885 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
886 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
887 << "failed to find entry";
888 }
889 }
890
891 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
892 ScopedJniThreadState ts(env);
893 if (obj == NULL) {
894 return NULL;
895 }
896
897 IndirectReferenceTable& locals = ts.Env()->locals;
898
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700899 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
901 return reinterpret_cast<jobject>(ref);
902 }
903
904 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
905 ScopedJniThreadState ts(env);
906 if (obj == NULL) {
907 return;
908 }
909
910 IndirectReferenceTable& locals = ts.Env()->locals;
911
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700912 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 if (!locals.Remove(cookie, obj)) {
914 // Attempting to delete a local reference that is not in the
915 // topmost local reference frame is a no-op. DeleteLocalRef returns
916 // void and doesn't throw any exceptions, but we should probably
917 // complain about it so the user will notice that things aren't
918 // going quite the way they expect.
919 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
920 << "failed to find entry";
921 }
922 }
923
924 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
925 ScopedJniThreadState ts(env);
926 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
927 ? JNI_TRUE : JNI_FALSE;
928 }
929
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700930 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700932 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700933 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700934 return NULL;
935 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700936 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 }
938
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 ScopedJniThreadState ts(env);
941 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 va_start(args, mid);
943 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 va_end(args);
945 return result;
946 }
947
Elliott Hughes72025e52011-08-23 17:50:30 -0700948 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700950 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700951 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700952 return NULL;
953 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700954 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700955 if (result == NULL) {
956 return NULL;
957 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700958 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 return local_result;
961 }
962
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700965 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700966 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700967 return NULL;
968 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700969 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700970 if (result == NULL) {
971 return NULL;
972 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700973 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 return local_result;
976 }
977
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
979 ScopedJniThreadState ts(env);
980 return FindMethodID(ts, c, name, sig, false);
981 }
982
983 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
984 ScopedJniThreadState ts(env);
985 return FindMethodID(ts, c, name, sig, true);
986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 va_list ap;
991 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700992 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700994 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700999 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001000 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001005 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001006 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 va_list ap;
1012 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001013 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 va_end(ap);
1015 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001020 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001025 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_list ap;
1031 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001032 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 va_end(ap);
1034 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001039 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001044 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_list ap;
1050 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001051 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 va_end(ap);
1053 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001058 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001063 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 va_list ap;
1069 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001070 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_end(ap);
1072 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001077 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001082 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 va_list ap;
1088 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001089 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 va_end(ap);
1091 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001096 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001101 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 va_list ap;
1107 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001108 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_end(ap);
1110 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001115 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001120 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 va_list ap;
1126 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001127 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 va_end(ap);
1129 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001134 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001139 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 va_list ap;
1145 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001146 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_end(ap);
1148 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001153 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 }
1155
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001158 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 va_list ap;
1164 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001165 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001171 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001176 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
1179 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 ScopedJniThreadState ts(env);
1182 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001184 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001185 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 va_end(ap);
1187 return local_result;
1188 }
1189
1190 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001193 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001194 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
1197 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001200 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001201 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
1204 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 ScopedJniThreadState ts(env);
1207 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001209 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 va_end(ap);
1211 return result.z;
1212 }
1213
1214 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001217 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
1220 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001223 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 }
1225
1226 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 ScopedJniThreadState ts(env);
1229 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001231 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 va_end(ap);
1233 return result.b;
1234 }
1235
1236 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001239 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
1242 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001245 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
1248 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 ScopedJniThreadState ts(env);
1251 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001253 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 va_end(ap);
1255 return result.c;
1256 }
1257
1258 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001261 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001267 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
1270 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 ScopedJniThreadState ts(env);
1273 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001275 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 va_end(ap);
1277 return result.s;
1278 }
1279
1280 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001283 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
1286 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001289 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
Elliott Hughes418d20f2011-09-22 14:00:39 -07001292 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 ScopedJniThreadState ts(env);
1294 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001295 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001296 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 va_end(ap);
1298 return result.i;
1299 }
1300
1301 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001302 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001304 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 }
1306
1307 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001308 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001310 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 }
1312
1313 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001314 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 ScopedJniThreadState ts(env);
1316 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001317 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001318 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 va_end(ap);
1320 return result.j;
1321 }
1322
1323 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001324 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001326 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
1329 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001330 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001332 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
1335 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001336 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 ScopedJniThreadState ts(env);
1338 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001339 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001340 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 va_end(ap);
1342 return result.f;
1343 }
1344
1345 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001346 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001348 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
1351 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001352 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001354 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
1357 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001358 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 ScopedJniThreadState ts(env);
1360 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001361 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001362 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 va_end(ap);
1364 return result.d;
1365 }
1366
1367 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001368 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001370 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
1373 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001374 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001376 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
1379 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001380 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 ScopedJniThreadState ts(env);
1382 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001383 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001384 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 va_end(ap);
1386 }
1387
1388 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001389 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001391 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
1393
1394 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001395 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001397 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001400 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 ScopedJniThreadState ts(env);
1402 return FindFieldID(ts, c, name, sig, false);
1403 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001404
1405
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001406 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 ScopedJniThreadState ts(env);
1408 return FindFieldID(ts, c, name, sig, true);
1409 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001410
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001411 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001413 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001414 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001415 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001417
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001418 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001420 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001421 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001424 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 Object* o = Decode<Object*>(ts, java_object);
1427 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001428 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001429 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 static void SetStaticObjectField(JNIEnv* env, jclass, 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* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001435 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439#define GET_PRIMITIVE_FIELD(fn, instance) \
1440 ScopedJniThreadState ts(env); \
1441 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001442 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 return f->fn(o)
1444
1445#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1446 ScopedJniThreadState ts(env); \
1447 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001448 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001449 f->fn(o, value)
1450
1451 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1476 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1480 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001483 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1484 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001487 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1488 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1492 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1496 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1500 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1504 GET_PRIMITIVE_FIELD(GetLong, NULL);
1505 }
1506
1507 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1508 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1509 }
1510
1511 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1512 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1513 }
1514
1515 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1516 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1517 }
1518
1519 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1520 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1521 }
1522
1523 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1524 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1525 }
1526
1527 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1528 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1529 }
1530
1531 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1532 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1533 }
1534
1535 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1536 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1537 }
1538
1539 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1540 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1541 }
1542
1543 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1544 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1545 }
1546
1547 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1548 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1549 }
1550
1551 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1552 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1553 }
1554
1555 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1556 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1557 }
1558
1559 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1560 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1561 }
1562
1563 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1564 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1565 }
1566
1567 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1568 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1569 }
1570
1571 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1572 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1573 }
1574
1575 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1576 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
Elliott Hughes418d20f2011-09-22 14:00:39 -07001579 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 ScopedJniThreadState ts(env);
1581 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001582 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001583 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001584 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 va_end(ap);
1586 return local_result;
1587 }
1588
Elliott Hughes418d20f2011-09-22 14:00:39 -07001589 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001591 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001592 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 }
1594
Elliott Hughes418d20f2011-09-22 14:00:39 -07001595 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001597 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001598 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes418d20f2011-09-22 14:00:39 -07001601 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 ScopedJniThreadState ts(env);
1603 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001604 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001605 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 va_end(ap);
1607 return result.z;
1608 }
1609
Elliott Hughes418d20f2011-09-22 14:00:39 -07001610 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001612 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes418d20f2011-09-22 14:00:39 -07001615 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001617 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes72025e52011-08-23 17:50:30 -07001620 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 ScopedJniThreadState ts(env);
1622 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001623 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001624 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 va_end(ap);
1626 return result.b;
1627 }
1628
Elliott Hughes418d20f2011-09-22 14:00:39 -07001629 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001631 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 }
1633
Elliott Hughes418d20f2011-09-22 14:00:39 -07001634 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001636 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 }
1638
Elliott Hughes72025e52011-08-23 17:50:30 -07001639 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 ScopedJniThreadState ts(env);
1641 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001642 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001643 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 va_end(ap);
1645 return result.c;
1646 }
1647
Elliott Hughes418d20f2011-09-22 14:00:39 -07001648 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001650 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
1652
Elliott Hughes418d20f2011-09-22 14:00:39 -07001653 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001655 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 }
1657
Elliott Hughes72025e52011-08-23 17:50:30 -07001658 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 ScopedJniThreadState ts(env);
1660 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001661 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001662 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 va_end(ap);
1664 return result.s;
1665 }
1666
Elliott Hughes418d20f2011-09-22 14:00:39 -07001667 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001669 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 }
1671
Elliott Hughes418d20f2011-09-22 14:00:39 -07001672 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001674 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 }
1676
Elliott Hughes72025e52011-08-23 17:50:30 -07001677 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 ScopedJniThreadState ts(env);
1679 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001680 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001681 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 va_end(ap);
1683 return result.i;
1684 }
1685
Elliott Hughes418d20f2011-09-22 14:00:39 -07001686 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001688 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 }
1690
Elliott Hughes418d20f2011-09-22 14:00:39 -07001691 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001693 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 }
1695
Elliott Hughes72025e52011-08-23 17:50:30 -07001696 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 ScopedJniThreadState ts(env);
1698 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001700 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 va_end(ap);
1702 return result.j;
1703 }
1704
Elliott Hughes418d20f2011-09-22 14:00:39 -07001705 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001707 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 }
1709
Elliott Hughes418d20f2011-09-22 14:00:39 -07001710 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001712 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 }
1714
Elliott Hughes72025e52011-08-23 17:50:30 -07001715 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 ScopedJniThreadState ts(env);
1717 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001718 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001719 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 va_end(ap);
1721 return result.f;
1722 }
1723
Elliott Hughes418d20f2011-09-22 14:00:39 -07001724 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001726 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes418d20f2011-09-22 14:00:39 -07001729 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001731 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes72025e52011-08-23 17:50:30 -07001734 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 ScopedJniThreadState ts(env);
1736 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001737 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001738 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 va_end(ap);
1740 return result.d;
1741 }
1742
Elliott Hughes418d20f2011-09-22 14:00:39 -07001743 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001745 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 }
1747
Elliott Hughes418d20f2011-09-22 14:00:39 -07001748 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001750 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 }
1752
Elliott Hughes72025e52011-08-23 17:50:30 -07001753 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 ScopedJniThreadState ts(env);
1755 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001756 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001757 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 va_end(ap);
1759 }
1760
Elliott Hughes418d20f2011-09-22 14:00:39 -07001761 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001763 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 }
1765
Elliott Hughes418d20f2011-09-22 14:00:39 -07001766 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001768 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes814e4032011-08-23 12:07:56 -07001771 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001773 if (chars == NULL && char_count == 0) {
1774 return NULL;
1775 }
1776 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001777 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 }
1779
1780 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1781 ScopedJniThreadState ts(env);
1782 if (utf == NULL) {
1783 return NULL;
1784 }
1785 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001786 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 }
1788
Elliott Hughes814e4032011-08-23 12:07:56 -07001789 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1790 ScopedJniThreadState ts(env);
1791 return Decode<String*>(ts, java_string)->GetLength();
1792 }
1793
1794 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1795 ScopedJniThreadState ts(env);
1796 return Decode<String*>(ts, java_string)->GetUtfLength();
1797 }
1798
Elliott Hughesb465ab02011-08-24 11:21:21 -07001799 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001801 String* s = Decode<String*>(ts, java_string);
1802 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1803 ThrowSIOOBE(ts, start, length, s->GetLength());
1804 } else {
1805 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1806 memcpy(buf, chars + start, length * sizeof(jchar));
1807 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001808 }
1809
Elliott Hughesb465ab02011-08-24 11:21:21 -07001810 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001811 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001812 String* s = Decode<String*>(ts, java_string);
1813 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1814 ThrowSIOOBE(ts, start, length, s->GetLength());
1815 } else {
1816 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1817 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1818 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001819 }
1820
Elliott Hughes75770752011-08-24 17:52:38 -07001821 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001822 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001823 String* s = Decode<String*>(ts, java_string);
1824 const CharArray* chars = s->GetCharArray();
1825 PinPrimitiveArray(ts, chars);
1826 if (is_copy != NULL) {
1827 *is_copy = JNI_FALSE;
1828 }
1829 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001830 }
1831
Elliott Hughes75770752011-08-24 17:52:38 -07001832 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001833 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001834 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
Elliott Hughes75770752011-08-24 17:52:38 -07001837 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001839 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 }
1841
Elliott Hughes75770752011-08-24 17:52:38 -07001842 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001844 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 }
1846
Elliott Hughes75770752011-08-24 17:52:38 -07001847 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001848 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001849 if (java_string == NULL) {
1850 return NULL;
1851 }
1852 if (is_copy != NULL) {
1853 *is_copy = JNI_TRUE;
1854 }
1855 String* s = Decode<String*>(ts, java_string);
1856 size_t byte_count = s->GetUtfLength();
1857 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001858 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001859 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1860 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1861 bytes[byte_count] = '\0';
1862 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001863 }
1864
Elliott Hughes75770752011-08-24 17:52:38 -07001865 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001866 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001867 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001868 }
1869
Elliott Hughesbd935992011-08-22 11:59:34 -07001870 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001872 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001873 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001874 Array* array = obj->AsArray();
1875 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 }
1877
Elliott Hughes814e4032011-08-23 12:07:56 -07001878 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001880 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001881 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 }
1883
1884 static void SetObjectArrayElement(JNIEnv* env,
1885 jobjectArray java_array, jsize index, jobject java_value) {
1886 ScopedJniThreadState ts(env);
1887 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1888 Object* value = Decode<Object*>(ts, java_value);
1889 array->Set(index, value);
1890 }
1891
1892 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1893 ScopedJniThreadState ts(env);
1894 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1895 }
1896
1897 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1898 ScopedJniThreadState ts(env);
1899 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1900 }
1901
1902 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1903 ScopedJniThreadState ts(env);
1904 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1905 }
1906
1907 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1908 ScopedJniThreadState ts(env);
1909 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1910 }
1911
1912 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1913 ScopedJniThreadState ts(env);
1914 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1915 }
1916
1917 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1918 ScopedJniThreadState ts(env);
1919 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1920 }
1921
1922 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1923 ScopedJniThreadState ts(env);
1924 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1925 }
1926
1927 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1928 ScopedJniThreadState ts(env);
1929 CHECK_GE(length, 0); // TODO: ReportJniError
1930
1931 // Compute the array class corresponding to the given element class.
1932 Class* element_class = Decode<Class*>(ts, element_jclass);
1933 std::string descriptor;
1934 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001935 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001936
1937 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001938 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1939 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 return NULL;
1941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 // Allocate and initialize if necessary.
1944 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001946 if (initial_element != NULL) {
1947 Object* initial_object = Decode<Object*>(ts, initial_element);
1948 for (jsize i = 0; i < length; ++i) {
1949 result->Set(i, initial_object);
1950 }
1951 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001952 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
1955 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1956 ScopedJniThreadState ts(env);
1957 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1958 }
1959
Elliott Hughes75770752011-08-24 17:52:38 -07001960 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001961 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001962 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001963 }
1964
Elliott Hughes75770752011-08-24 17:52:38 -07001965 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001966 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001967 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001968 }
1969
Elliott Hughes75770752011-08-24 17:52:38 -07001970 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001972 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 }
1974
Elliott Hughes75770752011-08-24 17:52:38 -07001975 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001977 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
Elliott Hughes75770752011-08-24 17:52:38 -07001980 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001982 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 }
1984
Elliott Hughes75770752011-08-24 17:52:38 -07001985 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001987 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Elliott Hughes75770752011-08-24 17:52:38 -07001990 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001992 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 }
1994
Elliott Hughes75770752011-08-24 17:52:38 -07001995 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001997 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Elliott Hughes75770752011-08-24 17:52:38 -07002000 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002002 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
Elliott Hughes75770752011-08-24 17:52:38 -07002005 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002007 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Elliott Hughes75770752011-08-24 17:52:38 -07002010 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002012 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Elliott Hughes75770752011-08-24 17:52:38 -07002015 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002017 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Elliott Hughes75770752011-08-24 17:52:38 -07002020 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002022 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes75770752011-08-24 17:52:38 -07002025 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002027 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes75770752011-08-24 17:52:38 -07002030 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002032 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
2034
Elliott Hughes75770752011-08-24 17:52:38 -07002035 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002037 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes75770752011-08-24 17:52:38 -07002040 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002042 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes75770752011-08-24 17:52:38 -07002045 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002047 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002052 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002057 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002062 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002067 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Elliott Hughes814e4032011-08-23 12:07:56 -07002070 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002072 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002077 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002082 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002087 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002092 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002097 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002102 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002107 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002112 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002117 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002122 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 }
2124
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002127 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
2129
Elliott Hughes5174fe62011-08-23 15:12:35 -07002130 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002132 Class* c = Decode<Class*>(ts, java_class);
2133
Elliott Hughes5174fe62011-08-23 15:12:35 -07002134 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 const char* name = methods[i].name;
2136 const char* sig = methods[i].signature;
2137
2138 if (*sig == '!') {
2139 // TODO: fast jni. it's too noisy to log all these.
2140 ++sig;
2141 }
2142
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143 Method* m = c->FindDirectMethod(name, sig);
2144 if (m == NULL) {
2145 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002147 if (m == NULL) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002148 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150 } else if (!m->IsNative()) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002151 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 return JNI_ERR;
2153 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002154
Elliott Hughes75770752011-08-24 17:52:38 -07002155 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002156 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002157 }
2158
2159 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 }
2161 return JNI_OK;
2162 }
2163
Elliott Hughes5174fe62011-08-23 15:12:35 -07002164 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002166 Class* c = Decode<Class*>(ts, java_class);
2167
Elliott Hughes75770752011-08-24 17:52:38 -07002168 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002169 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002170 }
2171
2172 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2173 Method* m = c->GetDirectMethod(i);
2174 if (m->IsNative()) {
2175 m->UnregisterNative();
2176 }
2177 }
2178 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2179 Method* m = c->GetVirtualMethod(i);
2180 if (m->IsNative()) {
2181 m->UnregisterNative();
2182 }
2183 }
2184
2185 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
Elliott Hughes72025e52011-08-23 17:50:30 -07002188 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002190 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002191 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 }
2193
Elliott Hughes72025e52011-08-23 17:50:30 -07002194 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002196 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002197 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 }
2199
2200 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2201 ScopedJniThreadState ts(env);
2202 Runtime* runtime = Runtime::Current();
2203 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002204 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 } else {
2206 *vm = NULL;
2207 }
2208 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2209 }
2210
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2212 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002213
2214 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002215 CHECK(address != NULL); // TODO: ReportJniError
2216 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002217
2218 jclass buffer_class = GetDirectByteBufferClass(env);
2219 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2220 if (mid == NULL) {
2221 return NULL;
2222 }
2223
2224 // At the moment, the Java side is limited to 32 bits.
2225 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2226 CHECK_LE(capacity, 0xffffffff);
2227 jint address_arg = reinterpret_cast<jint>(address);
2228 jint capacity_arg = static_cast<jint>(capacity);
2229
2230 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2231 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 }
2233
Elliott Hughesb465ab02011-08-24 11:21:21 -07002234 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002236 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2237 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239
Elliott Hughesb465ab02011-08-24 11:21:21 -07002240 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002242 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2243 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 }
2245
Elliott Hughesb465ab02011-08-24 11:21:21 -07002246 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002248
Elliott Hughes75770752011-08-24 17:52:38 -07002249 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002250
2251 // Do we definitely know what kind of reference this is?
2252 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2253 IndirectRefKind kind = GetIndirectRefKind(ref);
2254 switch (kind) {
2255 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002256 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2257 return JNILocalRefType;
2258 }
2259 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002260 case kGlobal:
2261 return JNIGlobalRefType;
2262 case kWeakGlobal:
2263 return JNIWeakGlobalRefType;
2264 case kSirtOrInvalid:
2265 // Is it in a stack IRT?
2266 if (ts.Self()->SirtContains(java_object)) {
2267 return JNILocalRefType;
2268 }
2269
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002270 if (!ts.Env()->work_around_app_jni_bugs) {
2271 return JNIInvalidRefType;
2272 }
2273
Elliott Hughesb465ab02011-08-24 11:21:21 -07002274 // If we're handing out direct pointers, check whether it's a direct pointer
2275 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002276 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002277 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002278 return JNILocalRefType;
2279 }
2280 }
2281
2282 return JNIInvalidRefType;
2283 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002284 }
2285};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002286
Elliott Hughesa2501992011-08-26 19:39:54 -07002287const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002288 NULL, // reserved0.
2289 NULL, // reserved1.
2290 NULL, // reserved2.
2291 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002292 JNI::GetVersion,
2293 JNI::DefineClass,
2294 JNI::FindClass,
2295 JNI::FromReflectedMethod,
2296 JNI::FromReflectedField,
2297 JNI::ToReflectedMethod,
2298 JNI::GetSuperclass,
2299 JNI::IsAssignableFrom,
2300 JNI::ToReflectedField,
2301 JNI::Throw,
2302 JNI::ThrowNew,
2303 JNI::ExceptionOccurred,
2304 JNI::ExceptionDescribe,
2305 JNI::ExceptionClear,
2306 JNI::FatalError,
2307 JNI::PushLocalFrame,
2308 JNI::PopLocalFrame,
2309 JNI::NewGlobalRef,
2310 JNI::DeleteGlobalRef,
2311 JNI::DeleteLocalRef,
2312 JNI::IsSameObject,
2313 JNI::NewLocalRef,
2314 JNI::EnsureLocalCapacity,
2315 JNI::AllocObject,
2316 JNI::NewObject,
2317 JNI::NewObjectV,
2318 JNI::NewObjectA,
2319 JNI::GetObjectClass,
2320 JNI::IsInstanceOf,
2321 JNI::GetMethodID,
2322 JNI::CallObjectMethod,
2323 JNI::CallObjectMethodV,
2324 JNI::CallObjectMethodA,
2325 JNI::CallBooleanMethod,
2326 JNI::CallBooleanMethodV,
2327 JNI::CallBooleanMethodA,
2328 JNI::CallByteMethod,
2329 JNI::CallByteMethodV,
2330 JNI::CallByteMethodA,
2331 JNI::CallCharMethod,
2332 JNI::CallCharMethodV,
2333 JNI::CallCharMethodA,
2334 JNI::CallShortMethod,
2335 JNI::CallShortMethodV,
2336 JNI::CallShortMethodA,
2337 JNI::CallIntMethod,
2338 JNI::CallIntMethodV,
2339 JNI::CallIntMethodA,
2340 JNI::CallLongMethod,
2341 JNI::CallLongMethodV,
2342 JNI::CallLongMethodA,
2343 JNI::CallFloatMethod,
2344 JNI::CallFloatMethodV,
2345 JNI::CallFloatMethodA,
2346 JNI::CallDoubleMethod,
2347 JNI::CallDoubleMethodV,
2348 JNI::CallDoubleMethodA,
2349 JNI::CallVoidMethod,
2350 JNI::CallVoidMethodV,
2351 JNI::CallVoidMethodA,
2352 JNI::CallNonvirtualObjectMethod,
2353 JNI::CallNonvirtualObjectMethodV,
2354 JNI::CallNonvirtualObjectMethodA,
2355 JNI::CallNonvirtualBooleanMethod,
2356 JNI::CallNonvirtualBooleanMethodV,
2357 JNI::CallNonvirtualBooleanMethodA,
2358 JNI::CallNonvirtualByteMethod,
2359 JNI::CallNonvirtualByteMethodV,
2360 JNI::CallNonvirtualByteMethodA,
2361 JNI::CallNonvirtualCharMethod,
2362 JNI::CallNonvirtualCharMethodV,
2363 JNI::CallNonvirtualCharMethodA,
2364 JNI::CallNonvirtualShortMethod,
2365 JNI::CallNonvirtualShortMethodV,
2366 JNI::CallNonvirtualShortMethodA,
2367 JNI::CallNonvirtualIntMethod,
2368 JNI::CallNonvirtualIntMethodV,
2369 JNI::CallNonvirtualIntMethodA,
2370 JNI::CallNonvirtualLongMethod,
2371 JNI::CallNonvirtualLongMethodV,
2372 JNI::CallNonvirtualLongMethodA,
2373 JNI::CallNonvirtualFloatMethod,
2374 JNI::CallNonvirtualFloatMethodV,
2375 JNI::CallNonvirtualFloatMethodA,
2376 JNI::CallNonvirtualDoubleMethod,
2377 JNI::CallNonvirtualDoubleMethodV,
2378 JNI::CallNonvirtualDoubleMethodA,
2379 JNI::CallNonvirtualVoidMethod,
2380 JNI::CallNonvirtualVoidMethodV,
2381 JNI::CallNonvirtualVoidMethodA,
2382 JNI::GetFieldID,
2383 JNI::GetObjectField,
2384 JNI::GetBooleanField,
2385 JNI::GetByteField,
2386 JNI::GetCharField,
2387 JNI::GetShortField,
2388 JNI::GetIntField,
2389 JNI::GetLongField,
2390 JNI::GetFloatField,
2391 JNI::GetDoubleField,
2392 JNI::SetObjectField,
2393 JNI::SetBooleanField,
2394 JNI::SetByteField,
2395 JNI::SetCharField,
2396 JNI::SetShortField,
2397 JNI::SetIntField,
2398 JNI::SetLongField,
2399 JNI::SetFloatField,
2400 JNI::SetDoubleField,
2401 JNI::GetStaticMethodID,
2402 JNI::CallStaticObjectMethod,
2403 JNI::CallStaticObjectMethodV,
2404 JNI::CallStaticObjectMethodA,
2405 JNI::CallStaticBooleanMethod,
2406 JNI::CallStaticBooleanMethodV,
2407 JNI::CallStaticBooleanMethodA,
2408 JNI::CallStaticByteMethod,
2409 JNI::CallStaticByteMethodV,
2410 JNI::CallStaticByteMethodA,
2411 JNI::CallStaticCharMethod,
2412 JNI::CallStaticCharMethodV,
2413 JNI::CallStaticCharMethodA,
2414 JNI::CallStaticShortMethod,
2415 JNI::CallStaticShortMethodV,
2416 JNI::CallStaticShortMethodA,
2417 JNI::CallStaticIntMethod,
2418 JNI::CallStaticIntMethodV,
2419 JNI::CallStaticIntMethodA,
2420 JNI::CallStaticLongMethod,
2421 JNI::CallStaticLongMethodV,
2422 JNI::CallStaticLongMethodA,
2423 JNI::CallStaticFloatMethod,
2424 JNI::CallStaticFloatMethodV,
2425 JNI::CallStaticFloatMethodA,
2426 JNI::CallStaticDoubleMethod,
2427 JNI::CallStaticDoubleMethodV,
2428 JNI::CallStaticDoubleMethodA,
2429 JNI::CallStaticVoidMethod,
2430 JNI::CallStaticVoidMethodV,
2431 JNI::CallStaticVoidMethodA,
2432 JNI::GetStaticFieldID,
2433 JNI::GetStaticObjectField,
2434 JNI::GetStaticBooleanField,
2435 JNI::GetStaticByteField,
2436 JNI::GetStaticCharField,
2437 JNI::GetStaticShortField,
2438 JNI::GetStaticIntField,
2439 JNI::GetStaticLongField,
2440 JNI::GetStaticFloatField,
2441 JNI::GetStaticDoubleField,
2442 JNI::SetStaticObjectField,
2443 JNI::SetStaticBooleanField,
2444 JNI::SetStaticByteField,
2445 JNI::SetStaticCharField,
2446 JNI::SetStaticShortField,
2447 JNI::SetStaticIntField,
2448 JNI::SetStaticLongField,
2449 JNI::SetStaticFloatField,
2450 JNI::SetStaticDoubleField,
2451 JNI::NewString,
2452 JNI::GetStringLength,
2453 JNI::GetStringChars,
2454 JNI::ReleaseStringChars,
2455 JNI::NewStringUTF,
2456 JNI::GetStringUTFLength,
2457 JNI::GetStringUTFChars,
2458 JNI::ReleaseStringUTFChars,
2459 JNI::GetArrayLength,
2460 JNI::NewObjectArray,
2461 JNI::GetObjectArrayElement,
2462 JNI::SetObjectArrayElement,
2463 JNI::NewBooleanArray,
2464 JNI::NewByteArray,
2465 JNI::NewCharArray,
2466 JNI::NewShortArray,
2467 JNI::NewIntArray,
2468 JNI::NewLongArray,
2469 JNI::NewFloatArray,
2470 JNI::NewDoubleArray,
2471 JNI::GetBooleanArrayElements,
2472 JNI::GetByteArrayElements,
2473 JNI::GetCharArrayElements,
2474 JNI::GetShortArrayElements,
2475 JNI::GetIntArrayElements,
2476 JNI::GetLongArrayElements,
2477 JNI::GetFloatArrayElements,
2478 JNI::GetDoubleArrayElements,
2479 JNI::ReleaseBooleanArrayElements,
2480 JNI::ReleaseByteArrayElements,
2481 JNI::ReleaseCharArrayElements,
2482 JNI::ReleaseShortArrayElements,
2483 JNI::ReleaseIntArrayElements,
2484 JNI::ReleaseLongArrayElements,
2485 JNI::ReleaseFloatArrayElements,
2486 JNI::ReleaseDoubleArrayElements,
2487 JNI::GetBooleanArrayRegion,
2488 JNI::GetByteArrayRegion,
2489 JNI::GetCharArrayRegion,
2490 JNI::GetShortArrayRegion,
2491 JNI::GetIntArrayRegion,
2492 JNI::GetLongArrayRegion,
2493 JNI::GetFloatArrayRegion,
2494 JNI::GetDoubleArrayRegion,
2495 JNI::SetBooleanArrayRegion,
2496 JNI::SetByteArrayRegion,
2497 JNI::SetCharArrayRegion,
2498 JNI::SetShortArrayRegion,
2499 JNI::SetIntArrayRegion,
2500 JNI::SetLongArrayRegion,
2501 JNI::SetFloatArrayRegion,
2502 JNI::SetDoubleArrayRegion,
2503 JNI::RegisterNatives,
2504 JNI::UnregisterNatives,
2505 JNI::MonitorEnter,
2506 JNI::MonitorExit,
2507 JNI::GetJavaVM,
2508 JNI::GetStringRegion,
2509 JNI::GetStringUTFRegion,
2510 JNI::GetPrimitiveArrayCritical,
2511 JNI::ReleasePrimitiveArrayCritical,
2512 JNI::GetStringCritical,
2513 JNI::ReleaseStringCritical,
2514 JNI::NewWeakGlobalRef,
2515 JNI::DeleteWeakGlobalRef,
2516 JNI::ExceptionCheck,
2517 JNI::NewDirectByteBuffer,
2518 JNI::GetDirectBufferAddress,
2519 JNI::GetDirectBufferCapacity,
2520 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002521};
2522
Elliott Hughes75770752011-08-24 17:52:38 -07002523JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002524 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002525 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002526 local_ref_cookie(IRT_FIRST_SEGMENT),
2527 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes75770752011-08-24 17:52:38 -07002528 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002529 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002530 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002531 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002532 functions = unchecked_functions = &gNativeInterface;
2533 if (check_jni) {
2534 functions = GetCheckJniNativeInterface();
2535 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002536 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2537 // errors will ensue.
2538 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2539 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002540}
2541
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002542JNIEnvExt::~JNIEnvExt() {
2543}
2544
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002545void JNIEnvExt::DumpReferenceTables() {
2546 locals.Dump();
2547 monitors.Dump();
2548}
2549
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002550void JNIEnvExt::PushFrame(int capacity) {
2551 stacked_local_ref_cookies.push_back(local_ref_cookie);
2552 local_ref_cookie = locals.GetSegmentState();
2553}
2554
2555void JNIEnvExt::PopFrame() {
2556 locals.SetSegmentState(local_ref_cookie);
2557 local_ref_cookie = stacked_local_ref_cookies.back();
2558 stacked_local_ref_cookies.pop_back();
2559}
2560
Carl Shapiroea4dca82011-08-01 13:45:38 -07002561// JNI Invocation interface.
2562
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002563extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2564 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2565 if (args->version < JNI_VERSION_1_2) {
2566 return JNI_EVERSION;
2567 }
2568 Runtime::Options options;
2569 for (int i = 0; i < args->nOptions; ++i) {
2570 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002571 options.push_back(std::make_pair(StringPiece(option->optionString),
2572 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002573 }
2574 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002575 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002576 if (runtime == NULL) {
2577 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002578 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002579 runtime->Start();
2580 *p_env = Thread::Current()->GetJniEnv();
2581 *p_vm = runtime->GetJavaVM();
2582 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002583}
2584
Elliott Hughesf2682d52011-08-15 16:37:04 -07002585extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002586 Runtime* runtime = Runtime::Current();
2587 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002588 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002589 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002590 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002591 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002592 }
2593 return JNI_OK;
2594}
2595
2596// Historically unsupported.
2597extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2598 return JNI_ERR;
2599}
2600
Elliott Hughescdf53122011-08-19 15:46:09 -07002601class JII {
2602 public:
2603 static jint DestroyJavaVM(JavaVM* vm) {
2604 if (vm == NULL) {
2605 return JNI_ERR;
2606 } else {
2607 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2608 delete raw_vm->runtime;
2609 return JNI_OK;
2610 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002611 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002612
Elliott Hughescdf53122011-08-19 15:46:09 -07002613 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002614 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002615 }
2616
2617 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002618 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002619 }
2620
2621 static jint DetachCurrentThread(JavaVM* vm) {
2622 if (vm == NULL) {
2623 return JNI_ERR;
2624 } else {
2625 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2626 Runtime* runtime = raw_vm->runtime;
2627 runtime->DetachCurrentThread();
2628 return JNI_OK;
2629 }
2630 }
2631
2632 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2633 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2634 return JNI_EVERSION;
2635 }
2636 if (vm == NULL || env == NULL) {
2637 return JNI_ERR;
2638 }
2639 Thread* thread = Thread::Current();
2640 if (thread == NULL) {
2641 *env = NULL;
2642 return JNI_EDETACHED;
2643 }
2644 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002645 return JNI_OK;
2646 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002647};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002648
Elliott Hughesa2501992011-08-26 19:39:54 -07002649const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002650 NULL, // reserved0
2651 NULL, // reserved1
2652 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002653 JII::DestroyJavaVM,
2654 JII::AttachCurrentThread,
2655 JII::DetachCurrentThread,
2656 JII::GetEnv,
2657 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002658};
2659
Elliott Hughesa0957642011-09-02 14:27:33 -07002660JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002661 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002662 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002663 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002664 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002665 verbose_jni(options->IsVerbose("jni")),
2666 log_third_party_jni(options->IsVerbose("third-party-jni")),
2667 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002668 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002669 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002670 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002671 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002672 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002673 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002674 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002675 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002676 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002677 functions = unchecked_functions = &gInvokeInterface;
2678 if (check_jni) {
2679 functions = GetCheckJniInvokeInterface();
2680 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002681}
2682
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002683JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002684 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002685}
2686
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002687void JavaVMExt::DumpReferenceTables() {
2688 {
2689 MutexLock mu(globals_lock);
2690 globals.Dump();
2691 }
2692 {
2693 MutexLock mu(weak_globals_lock);
2694 weak_globals.Dump();
2695 }
2696 {
2697 MutexLock mu(pins_lock);
2698 pin_table.Dump();
2699 }
2700}
2701
Elliott Hughes75770752011-08-24 17:52:38 -07002702bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2703 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002704
2705 // See if we've already loaded this library. If we have, and the class loader
2706 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002707 // TODO: for better results we should canonicalize the pathname (or even compare
2708 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002709 SharedLibrary* library;
2710 {
2711 // TODO: move the locking (and more of this logic) into Libraries.
2712 MutexLock mu(libraries_lock);
2713 library = libraries->Get(path);
2714 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002715 if (library != NULL) {
2716 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002717 // The library will be associated with class_loader. The JNI
2718 // spec says we can't load the same library into more than one
2719 // class loader.
2720 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2721 "ClassLoader %p; can't open in ClassLoader %p",
2722 path.c_str(), library->GetClassLoader(), class_loader);
2723 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002724 return false;
2725 }
2726 if (verbose_jni) {
2727 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2728 << "ClassLoader " << class_loader << "]";
2729 }
2730 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002731 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2732 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002733 return false;
2734 }
2735 return true;
2736 }
2737
2738 // Open the shared library. Because we're using a full path, the system
2739 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2740 // resolve this library's dependencies though.)
2741
2742 // Failures here are expected when java.library.path has several entries
2743 // and we have to hunt for the lib.
2744
2745 // The current version of the dynamic linker prints detailed information
2746 // about dlopen() failures. Some things to check if the message is
2747 // cryptic:
2748 // - make sure the library exists on the device
2749 // - verify that the right path is being opened (the debug log message
2750 // above can help with that)
2751 // - check to see if the library is valid (e.g. not zero bytes long)
2752 // - check config/prelink-linux-arm.map to ensure that the library
2753 // is listed and is not being overrun by the previous entry (if
2754 // loading suddenly stops working on a prelinked library, this is
2755 // a good one to check)
2756 // - write a trivial app that calls sleep() then dlopen(), attach
2757 // to it with "strace -p <pid>" while it sleeps, and watch for
2758 // attempts to open nonexistent dependent shared libs
2759
2760 // TODO: automate some of these checks!
2761
2762 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002763 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002764 // the GC to ignore us.
2765 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002766 void* handle = NULL;
2767 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002768 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002769 handle = dlopen(path.c_str(), RTLD_LAZY);
2770 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002771
2772 if (verbose_jni) {
2773 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2774 }
2775
2776 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002777 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002778 return false;
2779 }
2780
2781 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002782 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002783 // TODO: move the locking (and more of this logic) into Libraries.
2784 MutexLock mu(libraries_lock);
2785 library = libraries->Get(path);
2786 if (library != NULL) {
2787 LOG(INFO) << "WOW: we lost a race to add shared library: "
2788 << "\"" << path << "\" ClassLoader=" << class_loader;
2789 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002790 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002791 library = new SharedLibrary(path, handle, class_loader);
2792 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002793 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002794
2795 if (verbose_jni) {
2796 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2797 }
2798
2799 bool result = true;
2800 void* sym = dlsym(handle, "JNI_OnLoad");
2801 if (sym == NULL) {
2802 if (verbose_jni) {
2803 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2804 }
2805 } else {
2806 // Call JNI_OnLoad. We have to override the current class
2807 // loader, which will always be "null" since the stuff at the
2808 // top of the stack is around Runtime.loadLibrary(). (See
2809 // the comments in the JNI FindClass function.)
2810 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2811 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002812 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002813 self->SetClassLoaderOverride(class_loader);
2814
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002815 int version = 0;
2816 {
2817 ScopedThreadStateChange tsc(self, Thread::kNative);
2818 if (verbose_jni) {
2819 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2820 }
2821 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002822 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002823
Brian Carlstromaded5f72011-10-07 17:15:04 -07002824 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002825
2826 if (version != JNI_VERSION_1_2 &&
2827 version != JNI_VERSION_1_4 &&
2828 version != JNI_VERSION_1_6) {
2829 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2830 << "bad version: " << version;
2831 // It's unwise to call dlclose() here, but we can mark it
2832 // as bad and ensure that future load attempts will fail.
2833 // We don't know how far JNI_OnLoad got, so there could
2834 // be some partially-initialized stuff accessible through
2835 // newly-registered native method calls. We could try to
2836 // unregister them, but that doesn't seem worthwhile.
2837 result = false;
2838 } else {
2839 if (verbose_jni) {
2840 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2841 << " from JNI_OnLoad in \"" << path << "\"]";
2842 }
2843 }
2844 }
2845
2846 library->SetResult(result);
2847 return result;
2848}
2849
2850void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2851 CHECK(m->IsNative());
2852
2853 Class* c = m->GetDeclaringClass();
2854
2855 // If this is a static method, it could be called before the class
2856 // has been initialized.
2857 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002858 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002859 return NULL;
2860 }
2861 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002862 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002863 }
2864
Brian Carlstrom16192862011-09-12 17:50:06 -07002865 std::string detail;
2866 void* native_method;
2867 {
2868 MutexLock mu(libraries_lock);
2869 native_method = libraries->FindNativeMethod(m, detail);
2870 }
2871 // throwing can cause libraries_lock to be reacquired
2872 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002873 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002874 }
2875 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002876}
2877
Elliott Hughes410c0c82011-09-01 17:58:25 -07002878void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2879 {
2880 MutexLock mu(globals_lock);
2881 globals.VisitRoots(visitor, arg);
2882 }
2883 {
2884 MutexLock mu(pins_lock);
2885 pin_table.VisitRoots(visitor, arg);
2886 }
2887 // The weak_globals table is visited by the GC itself (because it mutates the table).
2888}
2889
Ian Rogersdf20fe02011-07-20 20:34:16 -07002890} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002891
2892std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2893 switch (rhs) {
2894 case JNIInvalidRefType:
2895 os << "JNIInvalidRefType";
2896 return os;
2897 case JNILocalRefType:
2898 os << "JNILocalRefType";
2899 return os;
2900 case JNIGlobalRefType:
2901 os << "JNIGlobalRefType";
2902 return os;
2903 case JNIWeakGlobalRefType:
2904 os << "JNIWeakGlobalRefType";
2905 return os;
2906 }
2907}