blob: 9936f259f19a461834962bd5cac41b3b03814700 [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)";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070082 }
83
84#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070085 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070086 size_t entry_count = locals.Capacity();
87 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070088 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070089 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070090 locals.Dump();
Elliott Hughes82188472011-11-07 18:11:48 -080091 // TODO: LOG(FATAL) instead.
Elliott Hughesc5f7c912011-08-18 14:00:42 -070092 }
93 }
94#endif
95
Elliott Hughesbf86d042011-08-31 17:53:14 -070096 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070097 // Hand out direct pointers to support broken old apps.
98 return reinterpret_cast<T>(obj);
99 }
100
101 return reinterpret_cast<T>(ref);
102}
103
Ian Rogers0571d352011-11-03 19:51:38 -0700104size_t NumArgArrayBytes(const char* shorty) {
105 size_t num_bytes = 0;
106 size_t end = strlen(shorty);
107 for (size_t i = 1; i < end; ++i) {
108 char ch = shorty[i];
109 if (ch == 'D' || ch == 'J') {
110 num_bytes += 8;
111 } else if (ch == 'L') {
112 // Argument is a reference or an array. The shorty descriptor
113 // does not distinguish between these types.
114 num_bytes += sizeof(Object*);
115 } else {
116 num_bytes += 4;
117 }
118 }
119 return num_bytes;
120}
121
122static size_t NumArgArrayBytes(const String* shorty) {
123 size_t num_bytes = 0;
124 size_t end = shorty->GetLength();
125 for (size_t i = 1; i < end; ++i) {
126 char ch = shorty->CharAt(i);
127 if (ch == 'D' || ch == 'J') {
128 num_bytes += 8;
129 } else if (ch == 'L') {
130 // Argument is a reference or an array. The shorty descriptor
131 // does not distinguish between these types.
132 num_bytes += sizeof(Object*);
133 } else {
134 num_bytes += 4;
135 }
136 }
137 return num_bytes;
138}
139
Elliott Hughesbf86d042011-08-31 17:53:14 -0700140// For external use.
141template<typename T>
142T Decode(JNIEnv* public_env, jobject obj) {
143 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
144 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
145}
146// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700147template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700148template Class* Decode<Class*>(JNIEnv*, jobject);
149template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
150template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700151template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700152template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700153template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700154template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400155template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700156template String* Decode<String*>(JNIEnv*, jobject);
157template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700158
159namespace {
160
Elliott Hughescdf53122011-08-19 15:46:09 -0700161jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
162 if (obj == NULL) {
163 return NULL;
164 }
Elliott Hughes75770752011-08-24 17:52:38 -0700165 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700166 IndirectReferenceTable& weak_globals = vm->weak_globals;
167 MutexLock mu(vm->weak_globals_lock);
168 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
169 return reinterpret_cast<jweak>(ref);
170}
171
Elliott Hughesbf86d042011-08-31 17:53:14 -0700172// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700173template<typename T>
174T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700175 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700176}
177
Ian Rogers0571d352011-11-03 19:51:38 -0700178static byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700179 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700180 const String* shorty = method->GetShorty();
Ian Rogers0571d352011-11-03 19:51:38 -0700181 size_t num_bytes = NumArgArrayBytes(shorty);
182 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700183 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
184 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700185 case 'Z':
186 case 'B':
187 case 'C':
188 case 'S':
189 case 'I':
190 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
191 offset += 4;
192 break;
193 case 'F':
194 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
195 offset += 4;
196 break;
197 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700198 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700199 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
200 offset += sizeof(Object*);
201 break;
202 }
203 case 'D':
204 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
205 offset += 8;
206 break;
207 case 'J':
208 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
209 offset += 8;
210 break;
211 }
212 }
213 return arg_array.release();
214}
215
Ian Rogers0571d352011-11-03 19:51:38 -0700216static byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700217 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Ian Rogers0571d352011-11-03 19:51:38 -0700218 size_t num_bytes = NumArgArrayBytes(method->GetShorty());
Elliott Hughes90a33692011-08-30 13:27:07 -0700219 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700220 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700221 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
222 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700223 case 'Z':
224 case 'B':
225 case 'C':
226 case 'S':
227 case 'I':
228 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
229 offset += 4;
230 break;
231 case 'F':
232 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
233 offset += 4;
234 break;
235 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700236 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700237 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
238 offset += sizeof(Object*);
239 break;
240 }
241 case 'D':
242 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
243 offset += 8;
244 break;
245 case 'J':
246 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
247 offset += 8;
248 break;
249 }
250 }
251 return arg_array.release();
252}
253
Ian Rogers0571d352011-11-03 19:51:38 -0700254static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700255 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700256 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700257 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700258 return result;
259}
260
Ian Rogers0571d352011-11-03 19:51:38 -0700261static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700262 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
263 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700264 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700265 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
266 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700267}
268
Ian Rogers0571d352011-11-03 19:51:38 -0700269static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700270 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700271}
272
Ian Rogers0571d352011-11-03 19:51:38 -0700273static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
274 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700275 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
276 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700277 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700278 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
279 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700280}
281
Ian Rogers0571d352011-11-03 19:51:38 -0700282static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
283 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700284 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
285 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700286 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700287 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
288 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700289}
290
Elliott Hughes6b436852011-08-12 10:16:44 -0700291// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
292// separated with slashes but aren't wrapped with "L;" like regular descriptors
293// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
294// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
295// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700296static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700297 std::string result;
298 // Add the missing "L;" if necessary.
299 if (name[0] == '[') {
300 result = name;
301 } else {
302 result += 'L';
303 result += name;
304 result += ';';
305 }
306 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700307 if (result.find('.') != std::string::npos) {
308 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
309 << "\"" << name << "\"";
310 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700311 }
312 return result;
313}
314
Ian Rogers0571d352011-11-03 19:51:38 -0700315static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700316 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700317 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes14134a12011-09-30 16:55:51 -0700318 "no %s method \"%s.%s%s\"", kind, class_descriptor.c_str(), name, sig);
319}
320
Ian Rogers0571d352011-11-03 19:51:38 -0700321static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700322 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700323 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700324 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700325 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700326
327 Method* method = NULL;
328 if (is_static) {
329 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700330 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700331 method = c->FindVirtualMethod(name, sig);
332 if (method == NULL) {
333 // No virtual method matching the signature. Search declared
334 // private methods and constructors.
335 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700336 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700337 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700338
Elliott Hughescdf53122011-08-19 15:46:09 -0700339 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700340 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700341 return NULL;
342 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700343
Elliott Hughes418d20f2011-09-22 14:00:39 -0700344 method->InitJavaFields();
345
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700346 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700347}
348
Ian Rogers0571d352011-11-03 19:51:38 -0700349static const ClassLoader* GetClassLoader(Thread* self) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700350 Frame frame = self->GetTopOfStack();
351 Method* method = frame.GetMethod();
352 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
353 return self->GetClassLoaderOverride();
354 }
355 return method->GetDeclaringClass()->GetClassLoader();
356}
357
Ian Rogers0571d352011-11-03 19:51:38 -0700358static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700359 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700360 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700361 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700362 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700363
364 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700365 Class* field_type;
366 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
367 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700368 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700369 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700370 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700371 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700372 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700373 if (field_type == NULL) {
374 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700375 DCHECK(ts.Self()->IsExceptionPending());
376 ts.Self()->ClearException();
377 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700378 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700379 "no type \"%s\" found and so no field \"%s\" could be found in class "
380 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381 return NULL;
382 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700383 std::string field_type_descriptor = field_type->GetDescriptor()->ToModifiedUtf8();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 if (is_static) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700385 field = c->FindStaticField(name, field_type_descriptor);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700387 field = c->FindInstanceField(name, field_type_descriptor);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700389 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700390 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700391 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700392 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700393 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700394 return NULL;
395 }
Elliott Hughes80609252011-09-23 17:24:51 -0700396 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700397 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700398}
399
Ian Rogers0571d352011-11-03 19:51:38 -0700400static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700401 JavaVMExt* vm = ts.Vm();
402 MutexLock mu(vm->pins_lock);
403 vm->pin_table.Add(array);
404}
405
Ian Rogers0571d352011-11-03 19:51:38 -0700406static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700407 JavaVMExt* vm = ts.Vm();
408 MutexLock mu(vm->pins_lock);
409 vm->pin_table.Remove(array);
410}
411
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700412template<typename JniT, typename ArtT>
413JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
414 CHECK_GE(length, 0); // TODO: ReportJniError
415 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700416 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700417}
418
Elliott Hughes75770752011-08-24 17:52:38 -0700419template <typename ArrayT, typename CArrayT, typename ArtArrayT>
420CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
421 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
422 PinPrimitiveArray(ts, array);
423 if (is_copy != NULL) {
424 *is_copy = JNI_FALSE;
425 }
426 return array->GetData();
427}
428
429template <typename ArrayT>
430void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
431 if (mode != JNI_COMMIT) {
432 Array* array = Decode<Array*>(ts, java_array);
433 UnpinPrimitiveArray(ts, array);
434 }
435}
436
Ian Rogers0571d352011-11-03 19:51:38 -0700437static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700438 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700439 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700440 "%s offset=%d length=%d %s.length=%d",
441 type.c_str(), start, length, identifier, array->GetLength());
442}
Ian Rogers0571d352011-11-03 19:51:38 -0700443
444static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700445 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700446 "offset=%d length=%d string.length()=%d", start, length, array_length);
447}
Elliott Hughes814e4032011-08-23 12:07:56 -0700448
449template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700450void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700451 ArrayT* array = Decode<ArrayT*>(ts, java_array);
452 if (start < 0 || length < 0 || start + length > array->GetLength()) {
453 ThrowAIOOBE(ts, array, start, length, "src");
454 } else {
455 JavaT* data = array->GetData();
456 memcpy(buf, data + start, length * sizeof(JavaT));
457 }
458}
459
460template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700461void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700462 ArrayT* array = Decode<ArrayT*>(ts, java_array);
463 if (start < 0 || length < 0 || start + length > array->GetLength()) {
464 ThrowAIOOBE(ts, array, start, length, "dst");
465 } else {
466 JavaT* data = array->GetData();
467 memcpy(data + start, buf, length * sizeof(JavaT));
468 }
469}
470
Ian Rogers0571d352011-11-03 19:51:38 -0700471static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700472 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
473 CHECK(buffer_class.get() != NULL);
474 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
475}
476
Ian Rogers0571d352011-11-03 19:51:38 -0700477static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700478 static jclass buffer_class = InitDirectByteBufferClass(env);
479 return buffer_class;
480}
481
Ian Rogers0571d352011-11-03 19:51:38 -0700482static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700483 if (vm == NULL || p_env == NULL) {
484 return JNI_ERR;
485 }
486
Elliott Hughescac6cc72011-11-03 20:31:21 -0700487 // Return immediately if we're already one with the VM.
488 Thread* self = Thread::Current();
489 if (self != NULL) {
490 *p_env = self->GetJniEnv();
491 return JNI_OK;
492 }
493
494 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
495
496 // No threads allowed in zygote mode.
497 if (runtime->IsZygote()) {
498 LOG(ERROR) << "Attempt to attach a thread in the zygote";
499 return JNI_ERR;
500 }
501
Elliott Hughes75770752011-08-24 17:52:38 -0700502 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
503 JavaVMAttachArgs args;
504 if (thr_args == NULL) {
505 // Allow the v1.1 calling convention.
506 args.version = JNI_VERSION_1_2;
507 args.name = NULL;
508 args.group = NULL; // TODO: get "main" thread group
509 } else {
510 args.version = in_args->version;
511 args.name = in_args->name;
512 if (in_args->group != NULL) {
513 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
514 args.group = NULL; // TODO: decode in_args->group
515 } else {
516 args.group = NULL; // TODO: get "main" thread group
517 }
518 }
519 CHECK_GE(args.version, JNI_VERSION_1_2);
520
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700521 runtime->AttachCurrentThread(args.name, as_daemon);
522 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700523 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700524}
525
Elliott Hughes79082e32011-08-25 12:07:32 -0700526class SharedLibrary {
527 public:
528 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
529 : path_(path),
530 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700531 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700532 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700533 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700534 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700535 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700536 }
537
Elliott Hughes79082e32011-08-25 12:07:32 -0700538 Object* GetClassLoader() {
539 return class_loader_;
540 }
541
542 std::string GetPath() {
543 return path_;
544 }
545
546 /*
547 * Check the result of an earlier call to JNI_OnLoad on this library. If
548 * the call has not yet finished in another thread, wait for it.
549 */
550 bool CheckOnLoadResult(JavaVMExt* vm) {
551 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700552 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700553 // Check this so we don't end up waiting for ourselves. We need
554 // to return "true" so the caller can continue.
555 LOG(INFO) << *self << " recursive attempt to load library "
556 << "\"" << path_ << "\"";
557 return true;
558 }
559
560 MutexLock mu(jni_on_load_lock_);
561 while (jni_on_load_result_ == kPending) {
562 if (vm->verbose_jni) {
563 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
564 << "JNI_OnLoad...]";
565 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700566 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700567 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700568 }
569
570 bool okay = (jni_on_load_result_ == kOkay);
571 if (vm->verbose_jni) {
572 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
573 << (okay ? "succeeded" : "failed") << "]";
574 }
575 return okay;
576 }
577
578 void SetResult(bool result) {
579 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700580 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700581
582 // Broadcast a wakeup to anybody sleeping on the condition variable.
583 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700584 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700585 }
586
587 void* FindSymbol(const std::string& symbol_name) {
588 return dlsym(handle_, symbol_name.c_str());
589 }
590
591 private:
592 enum JNI_OnLoadState {
593 kPending,
594 kFailed,
595 kOkay,
596 };
597
598 // Path to library "/system/lib/libjni.so".
599 std::string path_;
600
601 // The void* returned by dlopen(3).
602 void* handle_;
603
604 // The ClassLoader this library is associated with.
605 Object* class_loader_;
606
607 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700608 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700609 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700610 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700611 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700612 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700613 // Result of earlier JNI_OnLoad call.
614 JNI_OnLoadState jni_on_load_result_;
615};
616
Elliott Hughescdf53122011-08-19 15:46:09 -0700617} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700618
Elliott Hughes79082e32011-08-25 12:07:32 -0700619// This exists mainly to keep implementation details out of the header file.
620class Libraries {
621 public:
622 Libraries() {
623 }
624
625 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700626 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700627 }
628
629 SharedLibrary* Get(const std::string& path) {
630 return libraries_[path];
631 }
632
633 void Put(const std::string& path, SharedLibrary* library) {
634 libraries_[path] = library;
635 }
636
637 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700638 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700639 std::string jni_short_name(JniShortName(m));
640 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700641 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700642 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
643 SharedLibrary* library = it->second;
644 if (library->GetClassLoader() != declaring_class_loader) {
645 // We only search libraries loaded by the appropriate ClassLoader.
646 continue;
647 }
648 // Try the short name then the long name...
649 void* fn = library->FindSymbol(jni_short_name);
650 if (fn == NULL) {
651 fn = library->FindSymbol(jni_long_name);
652 }
653 if (fn != NULL) {
654 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700655 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700656 << " in \"" << library->GetPath() << "\"]";
657 }
658 return fn;
659 }
660 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700661 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700662 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700663 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700664 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700665 return NULL;
666 }
667
668 private:
669 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
670
671 std::map<std::string, SharedLibrary*> libraries_;
672};
673
Elliott Hughes418d20f2011-09-22 14:00:39 -0700674JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
675 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
676 Object* receiver = Decode<Object*>(env, obj);
677 Method* method = DecodeMethod(mid);
678 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
679 return InvokeWithArgArray(env, receiver, method, arg_array.get());
680}
681
Elliott Hughescdf53122011-08-19 15:46:09 -0700682class JNI {
683 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700684
Elliott Hughescdf53122011-08-19 15:46:09 -0700685 static jint GetVersion(JNIEnv* env) {
686 ScopedJniThreadState ts(env);
687 return JNI_VERSION_1_6;
688 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700689
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
691 ScopedJniThreadState ts(env);
692 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700693 return NULL;
694 }
695
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 static jclass FindClass(JNIEnv* env, const char* name) {
697 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700698 Runtime* runtime = Runtime::Current();
699 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700700 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700701 Class* c = NULL;
702 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700703 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700704 c = class_linker->FindClass(descriptor, cl);
705 } else {
706 c = class_linker->FindSystemClass(descriptor);
707 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700708 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700709 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700710
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
712 ScopedJniThreadState ts(env);
713 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700714 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700715 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700716
Elliott Hughescdf53122011-08-19 15:46:09 -0700717 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
718 ScopedJniThreadState ts(env);
719 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700720 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700721 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700722
Elliott Hughescdf53122011-08-19 15:46:09 -0700723 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
724 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700725 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700726 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700727 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700728
Elliott Hughescdf53122011-08-19 15:46:09 -0700729 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
730 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700731 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700732 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700733 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700734
Elliott Hughes37f7a402011-08-22 18:56:01 -0700735 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
736 ScopedJniThreadState ts(env);
737 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700738 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700739 }
740
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700741 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700743 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700744 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700746
Elliott Hughes37f7a402011-08-22 18:56:01 -0700747 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700749 Class* c1 = Decode<Class*>(ts, java_class1);
750 Class* c2 = Decode<Class*>(ts, java_class2);
751 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700753
Elliott Hughes37f7a402011-08-22 18:56:01 -0700754 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700756 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700757 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700758 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700759 return JNI_TRUE;
760 } else {
761 Object* obj = Decode<Object*>(ts, jobj);
762 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700763 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700764 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700766
Elliott Hughes37f7a402011-08-22 18:56:01 -0700767 static jint Throw(JNIEnv* env, jthrowable java_exception) {
768 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700769 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700770 if (exception == NULL) {
771 return JNI_ERR;
772 }
773 ts.Self()->SetException(exception);
774 return JNI_OK;
775 }
776
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700777 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700778 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700779 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700780 jmethodID mid = ((msg != NULL)
781 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
782 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700783 if (mid == NULL) {
784 return JNI_ERR;
785 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700786 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700787 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700788 return JNI_ERR;
789 }
790
791 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700792 args[0].l = s.get();
793 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
794 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700795 return JNI_ERR;
796 }
797
Elliott Hughes72025e52011-08-23 17:50:30 -0700798 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700799
Elliott Hughes37f7a402011-08-22 18:56:01 -0700800 return JNI_OK;
801 }
802
803 static jboolean ExceptionCheck(JNIEnv* env) {
804 ScopedJniThreadState ts(env);
805 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
806 }
807
808 static void ExceptionClear(JNIEnv* env) {
809 ScopedJniThreadState ts(env);
810 ts.Self()->ClearException();
811 }
812
813 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700815
816 Thread* self = ts.Self();
817 Throwable* original_exception = self->GetException();
818 self->ClearException();
819
Elliott Hughesbf86d042011-08-31 17:53:14 -0700820 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700821 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
822 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
823 if (mid == NULL) {
824 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700825 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700826 } else {
827 env->CallVoidMethod(exception.get(), mid);
828 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700829 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700830 << " thrown while calling printStackTrace";
831 self->ClearException();
832 }
833 }
834
835 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700837
Elliott Hughescdf53122011-08-19 15:46:09 -0700838 static jthrowable ExceptionOccurred(JNIEnv* env) {
839 ScopedJniThreadState ts(env);
840 Object* exception = ts.Self()->GetException();
841 if (exception == NULL) {
842 return NULL;
843 } else {
844 // TODO: if adding a local reference failing causes the VM to abort
845 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700846 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 if (localException == NULL) {
848 // We were unable to add a new local reference, and threw a new
849 // exception. We can't return "exception", because it's not a
850 // local reference. So we have to return NULL, indicating that
851 // there was no exception, even though it's pretty much raining
852 // exceptions in here.
853 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
854 }
855 return localException;
856 }
857 }
858
Elliott Hughescdf53122011-08-19 15:46:09 -0700859 static void FatalError(JNIEnv* env, const char* msg) {
860 ScopedJniThreadState ts(env);
861 LOG(FATAL) << "JNI FatalError called: " << msg;
862 }
863
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700864 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700866 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
867 return JNI_ERR;
868 }
869 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 return JNI_OK;
871 }
872
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700873 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700875 Object* survivor = Decode<Object*>(ts, java_survivor);
876 ts.Env()->PopFrame();
877 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700878 }
879
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700880 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700881 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700882 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
883 }
884
885 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
886 // TODO: we should try to expand the table if necessary.
887 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
888 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
889 return JNI_ERR;
890 }
891 // TODO: this isn't quite right, since "capacity" includes holes.
892 size_t capacity = ts.Env()->locals.Capacity();
893 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
894 if (!okay) {
895 ts.Self()->ThrowOutOfMemoryError(caller);
896 }
897 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700898 }
899
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
901 ScopedJniThreadState ts(env);
902 if (obj == NULL) {
903 return NULL;
904 }
905
Elliott Hughes75770752011-08-24 17:52:38 -0700906 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 IndirectReferenceTable& globals = vm->globals;
908 MutexLock mu(vm->globals_lock);
909 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
910 return reinterpret_cast<jobject>(ref);
911 }
912
913 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
914 ScopedJniThreadState ts(env);
915 if (obj == NULL) {
916 return;
917 }
918
Elliott Hughes75770752011-08-24 17:52:38 -0700919 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 IndirectReferenceTable& globals = vm->globals;
921 MutexLock mu(vm->globals_lock);
922
923 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
924 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
925 << "failed to find entry";
926 }
927 }
928
929 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
930 ScopedJniThreadState ts(env);
931 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
932 }
933
934 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
935 ScopedJniThreadState ts(env);
936 if (obj == NULL) {
937 return;
938 }
939
Elliott Hughes75770752011-08-24 17:52:38 -0700940 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 IndirectReferenceTable& weak_globals = vm->weak_globals;
942 MutexLock mu(vm->weak_globals_lock);
943
944 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
945 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
946 << "failed to find entry";
947 }
948 }
949
950 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
951 ScopedJniThreadState ts(env);
952 if (obj == NULL) {
953 return NULL;
954 }
955
956 IndirectReferenceTable& locals = ts.Env()->locals;
957
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700958 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700959 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
960 return reinterpret_cast<jobject>(ref);
961 }
962
963 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
964 ScopedJniThreadState ts(env);
965 if (obj == NULL) {
966 return;
967 }
968
969 IndirectReferenceTable& locals = ts.Env()->locals;
970
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700971 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 if (!locals.Remove(cookie, obj)) {
973 // Attempting to delete a local reference that is not in the
974 // topmost local reference frame is a no-op. DeleteLocalRef returns
975 // void and doesn't throw any exceptions, but we should probably
976 // complain about it so the user will notice that things aren't
977 // going quite the way they expect.
978 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
979 << "failed to find entry";
980 }
981 }
982
983 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
984 ScopedJniThreadState ts(env);
985 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
986 ? JNI_TRUE : JNI_FALSE;
987 }
988
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700989 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700991 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700992 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700993 return NULL;
994 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700995 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 ScopedJniThreadState ts(env);
1000 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 va_start(args, mid);
1002 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 va_end(args);
1004 return result;
1005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001009 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001010 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001011 return NULL;
1012 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001013 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001014 if (result == NULL) {
1015 return NULL;
1016 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001017 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001019 if (!ts.Self()->IsExceptionPending()) {
1020 return local_result;
1021 } else {
1022 return NULL;
1023 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001028 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001029 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001030 return NULL;
1031 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001032 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001033 if (result == NULL) {
1034 return NULL;
1035 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001036 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001038 if (!ts.Self()->IsExceptionPending()) {
1039 return local_result;
1040 } else {
1041 return NULL;
1042 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1046 ScopedJniThreadState ts(env);
1047 return FindMethodID(ts, c, name, sig, false);
1048 }
1049
1050 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1051 ScopedJniThreadState ts(env);
1052 return FindMethodID(ts, c, name, sig, true);
1053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 va_list ap;
1058 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001059 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001061 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001066 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001067 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001072 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001073 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 va_list ap;
1079 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001080 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 va_end(ap);
1082 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001087 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001092 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_list ap;
1098 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001099 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 va_end(ap);
1101 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001106 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001111 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 va_list ap;
1117 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001118 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 va_end(ap);
1120 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001125 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001130 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 va_list ap;
1136 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001137 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_end(ap);
1139 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001144 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001149 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_list ap;
1155 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001156 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 va_end(ap);
1158 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001163 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001168 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_list ap;
1174 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001175 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 va_end(ap);
1177 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001182 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001187 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 va_list ap;
1193 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001194 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 va_end(ap);
1196 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001201 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001206 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 }
1208
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 va_list ap;
1212 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001213 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 va_end(ap);
1215 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 }
1217
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001220 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001225 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 va_list ap;
1231 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001232 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001238 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001243 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
1246 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 ScopedJniThreadState ts(env);
1249 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001251 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001252 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 va_end(ap);
1254 return local_result;
1255 }
1256
1257 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001258 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001260 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001261 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jobject CallNonvirtualObjectMethodA(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 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001268 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
1271 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 ScopedJniThreadState ts(env);
1274 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001276 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 va_end(ap);
1278 return result.z;
1279 }
1280
1281 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001284 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
1287 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001288 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001290 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
1293 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 ScopedJniThreadState ts(env);
1296 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001298 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 va_end(ap);
1300 return result.b;
1301 }
1302
1303 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001304 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001306 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
1309 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001310 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001312 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
1315 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 ScopedJniThreadState ts(env);
1318 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001319 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001320 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 va_end(ap);
1322 return result.c;
1323 }
1324
1325 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001328 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
1331 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001334 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
1337 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 ScopedJniThreadState ts(env);
1340 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001342 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 va_end(ap);
1344 return result.s;
1345 }
1346
1347 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001348 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001350 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
1353 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001356 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
Elliott Hughes418d20f2011-09-22 14:00:39 -07001359 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 ScopedJniThreadState ts(env);
1361 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001363 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 va_end(ap);
1365 return result.i;
1366 }
1367
1368 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001369 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001371 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
1374 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001375 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001377 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
1380 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001381 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 ScopedJniThreadState ts(env);
1383 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001384 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001385 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 va_end(ap);
1387 return result.j;
1388 }
1389
1390 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001391 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001393 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
1396 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001397 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001399 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
1402 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001403 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 ScopedJniThreadState ts(env);
1405 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001406 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001407 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 va_end(ap);
1409 return result.f;
1410 }
1411
1412 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001413 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001415 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
1417
1418 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001419 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001421 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
1424 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001425 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 ScopedJniThreadState ts(env);
1427 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001428 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001429 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 va_end(ap);
1431 return result.d;
1432 }
1433
1434 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001435 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001437 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
1440 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001441 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001443 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
1446 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001447 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 ScopedJniThreadState ts(env);
1449 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001450 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001451 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 va_end(ap);
1453 }
1454
1455 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001456 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001458 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
1460
1461 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001462 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001464 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001467 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 ScopedJniThreadState ts(env);
1469 return FindFieldID(ts, c, name, sig, false);
1470 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001471
1472
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001473 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 ScopedJniThreadState ts(env);
1475 return FindFieldID(ts, c, name, sig, true);
1476 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001477
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001478 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001479 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001481 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001482 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001484
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001485 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001487 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001488 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001493 Object* o = Decode<Object*>(ts, java_object);
1494 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001495 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001501 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001502 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506#define GET_PRIMITIVE_FIELD(fn, instance) \
1507 ScopedJniThreadState ts(env); \
1508 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001509 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 return f->fn(o)
1511
1512#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1513 ScopedJniThreadState ts(env); \
1514 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001515 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 f->fn(o, value)
1517
1518 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1519 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
1521
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001522 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1523 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
1525
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001526 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1527 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
1529
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001530 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1531 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
1533
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001534 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1535 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001538 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1539 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001542 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1543 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001546 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1547 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 }
1549
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001550 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1551 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 }
1553
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001554 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1555 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 }
1557
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001558 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1559 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 }
1561
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001562 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1563 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001564 }
1565
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001566 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1567 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 }
1569
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001570 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1571 GET_PRIMITIVE_FIELD(GetLong, NULL);
1572 }
1573
1574 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1575 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1576 }
1577
1578 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1579 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1580 }
1581
1582 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1583 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1584 }
1585
1586 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1587 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1588 }
1589
1590 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1591 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1592 }
1593
1594 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1595 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1596 }
1597
1598 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1599 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1600 }
1601
1602 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1603 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1604 }
1605
1606 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1607 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1608 }
1609
1610 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1611 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1612 }
1613
1614 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1615 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1616 }
1617
1618 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1619 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1620 }
1621
1622 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1623 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1624 }
1625
1626 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1627 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1628 }
1629
1630 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1631 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1632 }
1633
1634 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1635 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1636 }
1637
1638 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1639 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1640 }
1641
1642 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1643 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
1645
Elliott Hughes418d20f2011-09-22 14:00:39 -07001646 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 ScopedJniThreadState ts(env);
1648 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001649 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001650 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001651 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 va_end(ap);
1653 return local_result;
1654 }
1655
Elliott Hughes418d20f2011-09-22 14:00:39 -07001656 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001658 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001659 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
Elliott Hughes418d20f2011-09-22 14:00:39 -07001662 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001664 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001665 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 }
1667
Elliott Hughes418d20f2011-09-22 14:00:39 -07001668 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 ScopedJniThreadState ts(env);
1670 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001671 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001672 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 va_end(ap);
1674 return result.z;
1675 }
1676
Elliott Hughes418d20f2011-09-22 14:00:39 -07001677 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001679 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 }
1681
Elliott Hughes418d20f2011-09-22 14:00:39 -07001682 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001684 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 }
1686
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 ScopedJniThreadState ts(env);
1689 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001690 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001691 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 va_end(ap);
1693 return result.b;
1694 }
1695
Elliott Hughes418d20f2011-09-22 14:00:39 -07001696 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001698 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 }
1700
Elliott Hughes418d20f2011-09-22 14:00:39 -07001701 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001703 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 }
1705
Elliott Hughes72025e52011-08-23 17:50:30 -07001706 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 ScopedJniThreadState ts(env);
1708 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001709 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001710 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 va_end(ap);
1712 return result.c;
1713 }
1714
Elliott Hughes418d20f2011-09-22 14:00:39 -07001715 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001717 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes418d20f2011-09-22 14:00:39 -07001720 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001722 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
Elliott Hughes72025e52011-08-23 17:50:30 -07001725 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 ScopedJniThreadState ts(env);
1727 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001728 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001729 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 va_end(ap);
1731 return result.s;
1732 }
1733
Elliott Hughes418d20f2011-09-22 14:00:39 -07001734 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001736 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes418d20f2011-09-22 14:00:39 -07001739 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001741 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 }
1743
Elliott Hughes72025e52011-08-23 17:50:30 -07001744 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 ScopedJniThreadState ts(env);
1746 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001748 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 va_end(ap);
1750 return result.i;
1751 }
1752
Elliott Hughes418d20f2011-09-22 14:00:39 -07001753 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001755 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 }
1757
Elliott Hughes418d20f2011-09-22 14:00:39 -07001758 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001760 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes72025e52011-08-23 17:50:30 -07001763 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 ScopedJniThreadState ts(env);
1765 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001766 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001767 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 va_end(ap);
1769 return result.j;
1770 }
1771
Elliott Hughes418d20f2011-09-22 14:00:39 -07001772 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001774 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes418d20f2011-09-22 14:00:39 -07001777 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001779 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 }
1781
Elliott Hughes72025e52011-08-23 17:50:30 -07001782 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 ScopedJniThreadState ts(env);
1784 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001785 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001786 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 va_end(ap);
1788 return result.f;
1789 }
1790
Elliott Hughes418d20f2011-09-22 14:00:39 -07001791 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001793 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes418d20f2011-09-22 14:00:39 -07001796 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001798 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
Elliott Hughes72025e52011-08-23 17:50:30 -07001801 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 ScopedJniThreadState ts(env);
1803 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001804 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001805 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 va_end(ap);
1807 return result.d;
1808 }
1809
Elliott Hughes418d20f2011-09-22 14:00:39 -07001810 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001812 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes418d20f2011-09-22 14:00:39 -07001815 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001817 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 }
1819
Elliott Hughes72025e52011-08-23 17:50:30 -07001820 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 ScopedJniThreadState ts(env);
1822 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001823 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001824 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 va_end(ap);
1826 }
1827
Elliott Hughes418d20f2011-09-22 14:00:39 -07001828 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001830 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes418d20f2011-09-22 14:00:39 -07001833 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001835 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes814e4032011-08-23 12:07:56 -07001838 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001840 if (chars == NULL && char_count == 0) {
1841 return NULL;
1842 }
1843 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001844 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 }
1846
1847 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1848 ScopedJniThreadState ts(env);
1849 if (utf == NULL) {
1850 return NULL;
1851 }
1852 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001853 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 }
1855
Elliott Hughes814e4032011-08-23 12:07:56 -07001856 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1857 ScopedJniThreadState ts(env);
1858 return Decode<String*>(ts, java_string)->GetLength();
1859 }
1860
1861 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1862 ScopedJniThreadState ts(env);
1863 return Decode<String*>(ts, java_string)->GetUtfLength();
1864 }
1865
Elliott Hughesb465ab02011-08-24 11:21:21 -07001866 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001868 String* s = Decode<String*>(ts, java_string);
1869 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1870 ThrowSIOOBE(ts, start, length, s->GetLength());
1871 } else {
1872 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1873 memcpy(buf, chars + start, length * sizeof(jchar));
1874 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001875 }
1876
Elliott Hughesb465ab02011-08-24 11:21:21 -07001877 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001878 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001879 String* s = Decode<String*>(ts, java_string);
1880 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1881 ThrowSIOOBE(ts, start, length, s->GetLength());
1882 } else {
1883 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1884 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1885 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001886 }
1887
Elliott Hughes75770752011-08-24 17:52:38 -07001888 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001889 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001890 String* s = Decode<String*>(ts, java_string);
1891 const CharArray* chars = s->GetCharArray();
1892 PinPrimitiveArray(ts, chars);
1893 if (is_copy != NULL) {
1894 *is_copy = JNI_FALSE;
1895 }
1896 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001897 }
1898
Elliott Hughes75770752011-08-24 17:52:38 -07001899 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001900 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001901 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001902 }
1903
Elliott Hughes75770752011-08-24 17:52:38 -07001904 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001906 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 }
1908
Elliott Hughes75770752011-08-24 17:52:38 -07001909 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001911 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001912 }
1913
Elliott Hughes75770752011-08-24 17:52:38 -07001914 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001915 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001916 if (java_string == NULL) {
1917 return NULL;
1918 }
1919 if (is_copy != NULL) {
1920 *is_copy = JNI_TRUE;
1921 }
1922 String* s = Decode<String*>(ts, java_string);
1923 size_t byte_count = s->GetUtfLength();
1924 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001925 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001926 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1927 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1928 bytes[byte_count] = '\0';
1929 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001930 }
1931
Elliott Hughes75770752011-08-24 17:52:38 -07001932 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001933 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001934 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001935 }
1936
Elliott Hughesbd935992011-08-22 11:59:34 -07001937 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001939 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001940 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001941 Array* array = obj->AsArray();
1942 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
Elliott Hughes814e4032011-08-23 12:07:56 -07001945 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001947 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001948 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
1951 static void SetObjectArrayElement(JNIEnv* env,
1952 jobjectArray java_array, jsize index, jobject java_value) {
1953 ScopedJniThreadState ts(env);
1954 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1955 Object* value = Decode<Object*>(ts, java_value);
1956 array->Set(index, value);
1957 }
1958
1959 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1960 ScopedJniThreadState ts(env);
1961 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1962 }
1963
1964 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1965 ScopedJniThreadState ts(env);
1966 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1967 }
1968
1969 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1970 ScopedJniThreadState ts(env);
1971 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1972 }
1973
1974 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1975 ScopedJniThreadState ts(env);
1976 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1977 }
1978
1979 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1980 ScopedJniThreadState ts(env);
1981 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1982 }
1983
1984 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1985 ScopedJniThreadState ts(env);
1986 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1987 }
1988
1989 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1990 ScopedJniThreadState ts(env);
1991 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1992 }
1993
1994 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1995 ScopedJniThreadState ts(env);
1996 CHECK_GE(length, 0); // TODO: ReportJniError
1997
1998 // Compute the array class corresponding to the given element class.
1999 Class* element_class = Decode<Class*>(ts, element_jclass);
2000 std::string descriptor;
2001 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07002002 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07002003
2004 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002005 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2006 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 return NULL;
2008 }
2009
Elliott Hughes75770752011-08-24 17:52:38 -07002010 // Allocate and initialize if necessary.
2011 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002013 if (initial_element != NULL) {
2014 Object* initial_object = Decode<Object*>(ts, initial_element);
2015 for (jsize i = 0; i < length; ++i) {
2016 result->Set(i, initial_object);
2017 }
2018 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002019 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
2022 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2023 ScopedJniThreadState ts(env);
2024 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2025 }
2026
Elliott Hughes75770752011-08-24 17:52:38 -07002027 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002028 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002029 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002030 }
2031
Elliott Hughes75770752011-08-24 17:52:38 -07002032 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002033 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002034 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002035 }
2036
Elliott Hughes75770752011-08-24 17:52:38 -07002037 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002039 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Elliott Hughes75770752011-08-24 17:52:38 -07002042 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002044 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Elliott Hughes75770752011-08-24 17:52:38 -07002047 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002049 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Elliott Hughes75770752011-08-24 17:52:38 -07002052 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002054 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes75770752011-08-24 17:52:38 -07002057 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002059 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Elliott Hughes75770752011-08-24 17:52:38 -07002062 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002064 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes75770752011-08-24 17:52:38 -07002067 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002069 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Elliott Hughes75770752011-08-24 17:52:38 -07002072 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002074 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes75770752011-08-24 17:52:38 -07002077 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002079 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Elliott Hughes75770752011-08-24 17:52:38 -07002082 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002084 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Elliott Hughes75770752011-08-24 17:52:38 -07002087 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002089 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
Elliott Hughes75770752011-08-24 17:52:38 -07002092 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002094 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 }
2096
Elliott Hughes75770752011-08-24 17:52:38 -07002097 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002099 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 }
2101
Elliott Hughes75770752011-08-24 17:52:38 -07002102 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002104 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 }
2106
Elliott Hughes75770752011-08-24 17:52:38 -07002107 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002109 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
Elliott Hughes75770752011-08-24 17:52:38 -07002112 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002114 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 }
2116
Elliott Hughes814e4032011-08-23 12:07:56 -07002117 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
Elliott Hughes814e4032011-08-23 12:07:56 -07002122 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002124 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
Elliott Hughes814e4032011-08-23 12:07:56 -07002127 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002129 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
Elliott Hughes814e4032011-08-23 12:07:56 -07002132 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002134 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 }
2136
Elliott Hughes814e4032011-08-23 12:07:56 -07002137 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002139 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 }
2141
Elliott Hughes814e4032011-08-23 12:07:56 -07002142 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002144 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 }
2146
Elliott Hughes814e4032011-08-23 12:07:56 -07002147 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002149 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 }
2151
Elliott Hughes814e4032011-08-23 12:07:56 -07002152 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002154 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
2156
Elliott Hughes814e4032011-08-23 12:07:56 -07002157 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002159 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 }
2161
Elliott Hughes814e4032011-08-23 12:07:56 -07002162 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002164 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Elliott Hughes814e4032011-08-23 12:07:56 -07002167 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002169 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Elliott Hughes814e4032011-08-23 12:07:56 -07002172 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002174 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
2176
Elliott Hughes814e4032011-08-23 12:07:56 -07002177 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002178 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002179 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
Elliott Hughes814e4032011-08-23 12:07:56 -07002182 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002184 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Elliott Hughes814e4032011-08-23 12:07:56 -07002187 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002189 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Elliott Hughes814e4032011-08-23 12:07:56 -07002192 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002194 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Elliott Hughes5174fe62011-08-23 15:12:35 -07002197 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002199 Class* c = Decode<Class*>(ts, java_class);
2200
Elliott Hughes5174fe62011-08-23 15:12:35 -07002201 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 const char* name = methods[i].name;
2203 const char* sig = methods[i].signature;
2204
2205 if (*sig == '!') {
2206 // TODO: fast jni. it's too noisy to log all these.
2207 ++sig;
2208 }
2209
Elliott Hughes5174fe62011-08-23 15:12:35 -07002210 Method* m = c->FindDirectMethod(name, sig);
2211 if (m == NULL) {
2212 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002214 if (m == NULL) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002215 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002217 } else if (!m->IsNative()) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002218 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 return JNI_ERR;
2220 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002221
Elliott Hughes75770752011-08-24 17:52:38 -07002222 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002223 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002224 }
2225
2226 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 }
2228 return JNI_OK;
2229 }
2230
Elliott Hughes5174fe62011-08-23 15:12:35 -07002231 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002233 Class* c = Decode<Class*>(ts, java_class);
2234
Elliott Hughes75770752011-08-24 17:52:38 -07002235 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002236 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002237 }
2238
2239 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2240 Method* m = c->GetDirectMethod(i);
2241 if (m->IsNative()) {
2242 m->UnregisterNative();
2243 }
2244 }
2245 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2246 Method* m = c->GetVirtualMethod(i);
2247 if (m->IsNative()) {
2248 m->UnregisterNative();
2249 }
2250 }
2251
2252 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 }
2254
Elliott Hughes72025e52011-08-23 17:50:30 -07002255 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002257 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002258 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002259 }
2260
Elliott Hughes72025e52011-08-23 17:50:30 -07002261 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002263 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002264 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002265 }
2266
2267 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2268 ScopedJniThreadState ts(env);
2269 Runtime* runtime = Runtime::Current();
2270 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002271 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 } else {
2273 *vm = NULL;
2274 }
2275 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2276 }
2277
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2279 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002280
2281 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002282 CHECK(address != NULL); // TODO: ReportJniError
2283 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002284
2285 jclass buffer_class = GetDirectByteBufferClass(env);
2286 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2287 if (mid == NULL) {
2288 return NULL;
2289 }
2290
2291 // At the moment, the Java side is limited to 32 bits.
2292 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2293 CHECK_LE(capacity, 0xffffffff);
2294 jint address_arg = reinterpret_cast<jint>(address);
2295 jint capacity_arg = static_cast<jint>(capacity);
2296
2297 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2298 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002299 }
2300
Elliott Hughesb465ab02011-08-24 11:21:21 -07002301 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002303 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2304 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002305 }
2306
Elliott Hughesb465ab02011-08-24 11:21:21 -07002307 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002309 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2310 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 }
2312
Elliott Hughesb465ab02011-08-24 11:21:21 -07002313 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002314 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002315
Elliott Hughes75770752011-08-24 17:52:38 -07002316 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002317
2318 // Do we definitely know what kind of reference this is?
2319 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2320 IndirectRefKind kind = GetIndirectRefKind(ref);
2321 switch (kind) {
2322 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002323 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2324 return JNILocalRefType;
2325 }
2326 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002327 case kGlobal:
2328 return JNIGlobalRefType;
2329 case kWeakGlobal:
2330 return JNIWeakGlobalRefType;
2331 case kSirtOrInvalid:
2332 // Is it in a stack IRT?
2333 if (ts.Self()->SirtContains(java_object)) {
2334 return JNILocalRefType;
2335 }
2336
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002337 if (!ts.Env()->work_around_app_jni_bugs) {
2338 return JNIInvalidRefType;
2339 }
2340
Elliott Hughesb465ab02011-08-24 11:21:21 -07002341 // If we're handing out direct pointers, check whether it's a direct pointer
2342 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002343 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002344 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002345 return JNILocalRefType;
2346 }
2347 }
2348
2349 return JNIInvalidRefType;
2350 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002351 }
2352};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002353
Elliott Hughesa2501992011-08-26 19:39:54 -07002354const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002355 NULL, // reserved0.
2356 NULL, // reserved1.
2357 NULL, // reserved2.
2358 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002359 JNI::GetVersion,
2360 JNI::DefineClass,
2361 JNI::FindClass,
2362 JNI::FromReflectedMethod,
2363 JNI::FromReflectedField,
2364 JNI::ToReflectedMethod,
2365 JNI::GetSuperclass,
2366 JNI::IsAssignableFrom,
2367 JNI::ToReflectedField,
2368 JNI::Throw,
2369 JNI::ThrowNew,
2370 JNI::ExceptionOccurred,
2371 JNI::ExceptionDescribe,
2372 JNI::ExceptionClear,
2373 JNI::FatalError,
2374 JNI::PushLocalFrame,
2375 JNI::PopLocalFrame,
2376 JNI::NewGlobalRef,
2377 JNI::DeleteGlobalRef,
2378 JNI::DeleteLocalRef,
2379 JNI::IsSameObject,
2380 JNI::NewLocalRef,
2381 JNI::EnsureLocalCapacity,
2382 JNI::AllocObject,
2383 JNI::NewObject,
2384 JNI::NewObjectV,
2385 JNI::NewObjectA,
2386 JNI::GetObjectClass,
2387 JNI::IsInstanceOf,
2388 JNI::GetMethodID,
2389 JNI::CallObjectMethod,
2390 JNI::CallObjectMethodV,
2391 JNI::CallObjectMethodA,
2392 JNI::CallBooleanMethod,
2393 JNI::CallBooleanMethodV,
2394 JNI::CallBooleanMethodA,
2395 JNI::CallByteMethod,
2396 JNI::CallByteMethodV,
2397 JNI::CallByteMethodA,
2398 JNI::CallCharMethod,
2399 JNI::CallCharMethodV,
2400 JNI::CallCharMethodA,
2401 JNI::CallShortMethod,
2402 JNI::CallShortMethodV,
2403 JNI::CallShortMethodA,
2404 JNI::CallIntMethod,
2405 JNI::CallIntMethodV,
2406 JNI::CallIntMethodA,
2407 JNI::CallLongMethod,
2408 JNI::CallLongMethodV,
2409 JNI::CallLongMethodA,
2410 JNI::CallFloatMethod,
2411 JNI::CallFloatMethodV,
2412 JNI::CallFloatMethodA,
2413 JNI::CallDoubleMethod,
2414 JNI::CallDoubleMethodV,
2415 JNI::CallDoubleMethodA,
2416 JNI::CallVoidMethod,
2417 JNI::CallVoidMethodV,
2418 JNI::CallVoidMethodA,
2419 JNI::CallNonvirtualObjectMethod,
2420 JNI::CallNonvirtualObjectMethodV,
2421 JNI::CallNonvirtualObjectMethodA,
2422 JNI::CallNonvirtualBooleanMethod,
2423 JNI::CallNonvirtualBooleanMethodV,
2424 JNI::CallNonvirtualBooleanMethodA,
2425 JNI::CallNonvirtualByteMethod,
2426 JNI::CallNonvirtualByteMethodV,
2427 JNI::CallNonvirtualByteMethodA,
2428 JNI::CallNonvirtualCharMethod,
2429 JNI::CallNonvirtualCharMethodV,
2430 JNI::CallNonvirtualCharMethodA,
2431 JNI::CallNonvirtualShortMethod,
2432 JNI::CallNonvirtualShortMethodV,
2433 JNI::CallNonvirtualShortMethodA,
2434 JNI::CallNonvirtualIntMethod,
2435 JNI::CallNonvirtualIntMethodV,
2436 JNI::CallNonvirtualIntMethodA,
2437 JNI::CallNonvirtualLongMethod,
2438 JNI::CallNonvirtualLongMethodV,
2439 JNI::CallNonvirtualLongMethodA,
2440 JNI::CallNonvirtualFloatMethod,
2441 JNI::CallNonvirtualFloatMethodV,
2442 JNI::CallNonvirtualFloatMethodA,
2443 JNI::CallNonvirtualDoubleMethod,
2444 JNI::CallNonvirtualDoubleMethodV,
2445 JNI::CallNonvirtualDoubleMethodA,
2446 JNI::CallNonvirtualVoidMethod,
2447 JNI::CallNonvirtualVoidMethodV,
2448 JNI::CallNonvirtualVoidMethodA,
2449 JNI::GetFieldID,
2450 JNI::GetObjectField,
2451 JNI::GetBooleanField,
2452 JNI::GetByteField,
2453 JNI::GetCharField,
2454 JNI::GetShortField,
2455 JNI::GetIntField,
2456 JNI::GetLongField,
2457 JNI::GetFloatField,
2458 JNI::GetDoubleField,
2459 JNI::SetObjectField,
2460 JNI::SetBooleanField,
2461 JNI::SetByteField,
2462 JNI::SetCharField,
2463 JNI::SetShortField,
2464 JNI::SetIntField,
2465 JNI::SetLongField,
2466 JNI::SetFloatField,
2467 JNI::SetDoubleField,
2468 JNI::GetStaticMethodID,
2469 JNI::CallStaticObjectMethod,
2470 JNI::CallStaticObjectMethodV,
2471 JNI::CallStaticObjectMethodA,
2472 JNI::CallStaticBooleanMethod,
2473 JNI::CallStaticBooleanMethodV,
2474 JNI::CallStaticBooleanMethodA,
2475 JNI::CallStaticByteMethod,
2476 JNI::CallStaticByteMethodV,
2477 JNI::CallStaticByteMethodA,
2478 JNI::CallStaticCharMethod,
2479 JNI::CallStaticCharMethodV,
2480 JNI::CallStaticCharMethodA,
2481 JNI::CallStaticShortMethod,
2482 JNI::CallStaticShortMethodV,
2483 JNI::CallStaticShortMethodA,
2484 JNI::CallStaticIntMethod,
2485 JNI::CallStaticIntMethodV,
2486 JNI::CallStaticIntMethodA,
2487 JNI::CallStaticLongMethod,
2488 JNI::CallStaticLongMethodV,
2489 JNI::CallStaticLongMethodA,
2490 JNI::CallStaticFloatMethod,
2491 JNI::CallStaticFloatMethodV,
2492 JNI::CallStaticFloatMethodA,
2493 JNI::CallStaticDoubleMethod,
2494 JNI::CallStaticDoubleMethodV,
2495 JNI::CallStaticDoubleMethodA,
2496 JNI::CallStaticVoidMethod,
2497 JNI::CallStaticVoidMethodV,
2498 JNI::CallStaticVoidMethodA,
2499 JNI::GetStaticFieldID,
2500 JNI::GetStaticObjectField,
2501 JNI::GetStaticBooleanField,
2502 JNI::GetStaticByteField,
2503 JNI::GetStaticCharField,
2504 JNI::GetStaticShortField,
2505 JNI::GetStaticIntField,
2506 JNI::GetStaticLongField,
2507 JNI::GetStaticFloatField,
2508 JNI::GetStaticDoubleField,
2509 JNI::SetStaticObjectField,
2510 JNI::SetStaticBooleanField,
2511 JNI::SetStaticByteField,
2512 JNI::SetStaticCharField,
2513 JNI::SetStaticShortField,
2514 JNI::SetStaticIntField,
2515 JNI::SetStaticLongField,
2516 JNI::SetStaticFloatField,
2517 JNI::SetStaticDoubleField,
2518 JNI::NewString,
2519 JNI::GetStringLength,
2520 JNI::GetStringChars,
2521 JNI::ReleaseStringChars,
2522 JNI::NewStringUTF,
2523 JNI::GetStringUTFLength,
2524 JNI::GetStringUTFChars,
2525 JNI::ReleaseStringUTFChars,
2526 JNI::GetArrayLength,
2527 JNI::NewObjectArray,
2528 JNI::GetObjectArrayElement,
2529 JNI::SetObjectArrayElement,
2530 JNI::NewBooleanArray,
2531 JNI::NewByteArray,
2532 JNI::NewCharArray,
2533 JNI::NewShortArray,
2534 JNI::NewIntArray,
2535 JNI::NewLongArray,
2536 JNI::NewFloatArray,
2537 JNI::NewDoubleArray,
2538 JNI::GetBooleanArrayElements,
2539 JNI::GetByteArrayElements,
2540 JNI::GetCharArrayElements,
2541 JNI::GetShortArrayElements,
2542 JNI::GetIntArrayElements,
2543 JNI::GetLongArrayElements,
2544 JNI::GetFloatArrayElements,
2545 JNI::GetDoubleArrayElements,
2546 JNI::ReleaseBooleanArrayElements,
2547 JNI::ReleaseByteArrayElements,
2548 JNI::ReleaseCharArrayElements,
2549 JNI::ReleaseShortArrayElements,
2550 JNI::ReleaseIntArrayElements,
2551 JNI::ReleaseLongArrayElements,
2552 JNI::ReleaseFloatArrayElements,
2553 JNI::ReleaseDoubleArrayElements,
2554 JNI::GetBooleanArrayRegion,
2555 JNI::GetByteArrayRegion,
2556 JNI::GetCharArrayRegion,
2557 JNI::GetShortArrayRegion,
2558 JNI::GetIntArrayRegion,
2559 JNI::GetLongArrayRegion,
2560 JNI::GetFloatArrayRegion,
2561 JNI::GetDoubleArrayRegion,
2562 JNI::SetBooleanArrayRegion,
2563 JNI::SetByteArrayRegion,
2564 JNI::SetCharArrayRegion,
2565 JNI::SetShortArrayRegion,
2566 JNI::SetIntArrayRegion,
2567 JNI::SetLongArrayRegion,
2568 JNI::SetFloatArrayRegion,
2569 JNI::SetDoubleArrayRegion,
2570 JNI::RegisterNatives,
2571 JNI::UnregisterNatives,
2572 JNI::MonitorEnter,
2573 JNI::MonitorExit,
2574 JNI::GetJavaVM,
2575 JNI::GetStringRegion,
2576 JNI::GetStringUTFRegion,
2577 JNI::GetPrimitiveArrayCritical,
2578 JNI::ReleasePrimitiveArrayCritical,
2579 JNI::GetStringCritical,
2580 JNI::ReleaseStringCritical,
2581 JNI::NewWeakGlobalRef,
2582 JNI::DeleteWeakGlobalRef,
2583 JNI::ExceptionCheck,
2584 JNI::NewDirectByteBuffer,
2585 JNI::GetDirectBufferAddress,
2586 JNI::GetDirectBufferCapacity,
2587 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002588};
2589
Elliott Hughes75770752011-08-24 17:52:38 -07002590JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002591 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002592 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002593 local_ref_cookie(IRT_FIRST_SEGMENT),
2594 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002595 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002596 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002597 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002598 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002599 functions = unchecked_functions = &gNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002600 if (vm->check_jni) {
2601 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002602 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002603 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2604 // errors will ensue.
2605 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2606 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002607}
2608
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002609JNIEnvExt::~JNIEnvExt() {
2610}
2611
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002612void JNIEnvExt::EnableCheckJni() {
2613 check_jni = true;
2614 functions = GetCheckJniNativeInterface();
2615}
2616
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002617void JNIEnvExt::DumpReferenceTables() {
2618 locals.Dump();
2619 monitors.Dump();
2620}
2621
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002622void JNIEnvExt::PushFrame(int capacity) {
2623 stacked_local_ref_cookies.push_back(local_ref_cookie);
2624 local_ref_cookie = locals.GetSegmentState();
2625}
2626
2627void JNIEnvExt::PopFrame() {
2628 locals.SetSegmentState(local_ref_cookie);
2629 local_ref_cookie = stacked_local_ref_cookies.back();
2630 stacked_local_ref_cookies.pop_back();
2631}
2632
Carl Shapiroea4dca82011-08-01 13:45:38 -07002633// JNI Invocation interface.
2634
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002635extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2636 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2637 if (args->version < JNI_VERSION_1_2) {
2638 return JNI_EVERSION;
2639 }
2640 Runtime::Options options;
2641 for (int i = 0; i < args->nOptions; ++i) {
2642 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002643 options.push_back(std::make_pair(StringPiece(option->optionString),
2644 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002645 }
2646 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002647 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002648 if (runtime == NULL) {
2649 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002650 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002651 runtime->Start();
2652 *p_env = Thread::Current()->GetJniEnv();
2653 *p_vm = runtime->GetJavaVM();
2654 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002655}
2656
Elliott Hughesf2682d52011-08-15 16:37:04 -07002657extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002658 Runtime* runtime = Runtime::Current();
2659 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002660 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002661 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002662 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002663 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002664 }
2665 return JNI_OK;
2666}
2667
2668// Historically unsupported.
2669extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2670 return JNI_ERR;
2671}
2672
Elliott Hughescdf53122011-08-19 15:46:09 -07002673class JII {
2674 public:
2675 static jint DestroyJavaVM(JavaVM* vm) {
2676 if (vm == NULL) {
2677 return JNI_ERR;
2678 } else {
2679 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2680 delete raw_vm->runtime;
2681 return JNI_OK;
2682 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002683 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002684
Elliott Hughescdf53122011-08-19 15:46:09 -07002685 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002686 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002687 }
2688
2689 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002690 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002691 }
2692
2693 static jint DetachCurrentThread(JavaVM* vm) {
2694 if (vm == NULL) {
2695 return JNI_ERR;
2696 } else {
2697 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2698 Runtime* runtime = raw_vm->runtime;
2699 runtime->DetachCurrentThread();
2700 return JNI_OK;
2701 }
2702 }
2703
2704 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2705 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2706 return JNI_EVERSION;
2707 }
2708 if (vm == NULL || env == NULL) {
2709 return JNI_ERR;
2710 }
2711 Thread* thread = Thread::Current();
2712 if (thread == NULL) {
2713 *env = NULL;
2714 return JNI_EDETACHED;
2715 }
2716 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002717 return JNI_OK;
2718 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002719};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002720
Elliott Hughesa2501992011-08-26 19:39:54 -07002721const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002722 NULL, // reserved0
2723 NULL, // reserved1
2724 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002725 JII::DestroyJavaVM,
2726 JII::AttachCurrentThread,
2727 JII::DetachCurrentThread,
2728 JII::GetEnv,
2729 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002730};
2731
Elliott Hughesa0957642011-09-02 14:27:33 -07002732JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002733 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002734 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002735 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002736 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002737 verbose_jni(options->IsVerbose("jni")),
2738 log_third_party_jni(options->IsVerbose("third-party-jni")),
2739 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002740 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002741 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002742 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002743 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002744 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002745 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002746 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002747 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002748 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002749 functions = unchecked_functions = &gInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002750 if (options->check_jni_) {
2751 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002752 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002753}
2754
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002755JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002756 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002757}
2758
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002759void JavaVMExt::EnableCheckJni() {
2760 check_jni = true;
2761 functions = GetCheckJniInvokeInterface();
2762}
2763
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002764void JavaVMExt::DumpReferenceTables() {
2765 {
2766 MutexLock mu(globals_lock);
2767 globals.Dump();
2768 }
2769 {
2770 MutexLock mu(weak_globals_lock);
2771 weak_globals.Dump();
2772 }
2773 {
2774 MutexLock mu(pins_lock);
2775 pin_table.Dump();
2776 }
2777}
2778
Elliott Hughes75770752011-08-24 17:52:38 -07002779bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2780 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002781
2782 // See if we've already loaded this library. If we have, and the class loader
2783 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002784 // TODO: for better results we should canonicalize the pathname (or even compare
2785 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002786 SharedLibrary* library;
2787 {
2788 // TODO: move the locking (and more of this logic) into Libraries.
2789 MutexLock mu(libraries_lock);
2790 library = libraries->Get(path);
2791 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002792 if (library != NULL) {
2793 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002794 // The library will be associated with class_loader. The JNI
2795 // spec says we can't load the same library into more than one
2796 // class loader.
2797 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2798 "ClassLoader %p; can't open in ClassLoader %p",
2799 path.c_str(), library->GetClassLoader(), class_loader);
2800 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002801 return false;
2802 }
2803 if (verbose_jni) {
2804 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2805 << "ClassLoader " << class_loader << "]";
2806 }
2807 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002808 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2809 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002810 return false;
2811 }
2812 return true;
2813 }
2814
2815 // Open the shared library. Because we're using a full path, the system
2816 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2817 // resolve this library's dependencies though.)
2818
2819 // Failures here are expected when java.library.path has several entries
2820 // and we have to hunt for the lib.
2821
2822 // The current version of the dynamic linker prints detailed information
2823 // about dlopen() failures. Some things to check if the message is
2824 // cryptic:
2825 // - make sure the library exists on the device
2826 // - verify that the right path is being opened (the debug log message
2827 // above can help with that)
2828 // - check to see if the library is valid (e.g. not zero bytes long)
2829 // - check config/prelink-linux-arm.map to ensure that the library
2830 // is listed and is not being overrun by the previous entry (if
2831 // loading suddenly stops working on a prelinked library, this is
2832 // a good one to check)
2833 // - write a trivial app that calls sleep() then dlopen(), attach
2834 // to it with "strace -p <pid>" while it sleeps, and watch for
2835 // attempts to open nonexistent dependent shared libs
2836
2837 // TODO: automate some of these checks!
2838
2839 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002840 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002841 // the GC to ignore us.
2842 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002843 void* handle = NULL;
2844 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002845 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002846 handle = dlopen(path.c_str(), RTLD_LAZY);
2847 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002848
2849 if (verbose_jni) {
2850 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2851 }
2852
2853 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002854 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002855 return false;
2856 }
2857
2858 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002859 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002860 // TODO: move the locking (and more of this logic) into Libraries.
2861 MutexLock mu(libraries_lock);
2862 library = libraries->Get(path);
2863 if (library != NULL) {
2864 LOG(INFO) << "WOW: we lost a race to add shared library: "
2865 << "\"" << path << "\" ClassLoader=" << class_loader;
2866 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002867 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002868 library = new SharedLibrary(path, handle, class_loader);
2869 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002870 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002871
2872 if (verbose_jni) {
2873 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2874 }
2875
2876 bool result = true;
2877 void* sym = dlsym(handle, "JNI_OnLoad");
2878 if (sym == NULL) {
2879 if (verbose_jni) {
2880 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2881 }
2882 } else {
2883 // Call JNI_OnLoad. We have to override the current class
2884 // loader, which will always be "null" since the stuff at the
2885 // top of the stack is around Runtime.loadLibrary(). (See
2886 // the comments in the JNI FindClass function.)
2887 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2888 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002889 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002890 self->SetClassLoaderOverride(class_loader);
2891
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002892 int version = 0;
2893 {
2894 ScopedThreadStateChange tsc(self, Thread::kNative);
2895 if (verbose_jni) {
2896 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2897 }
2898 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002899 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002900
Brian Carlstromaded5f72011-10-07 17:15:04 -07002901 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002902
2903 if (version != JNI_VERSION_1_2 &&
2904 version != JNI_VERSION_1_4 &&
2905 version != JNI_VERSION_1_6) {
2906 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2907 << "bad version: " << version;
2908 // It's unwise to call dlclose() here, but we can mark it
2909 // as bad and ensure that future load attempts will fail.
2910 // We don't know how far JNI_OnLoad got, so there could
2911 // be some partially-initialized stuff accessible through
2912 // newly-registered native method calls. We could try to
2913 // unregister them, but that doesn't seem worthwhile.
2914 result = false;
2915 } else {
2916 if (verbose_jni) {
2917 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2918 << " from JNI_OnLoad in \"" << path << "\"]";
2919 }
2920 }
2921 }
2922
2923 library->SetResult(result);
2924 return result;
2925}
2926
2927void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2928 CHECK(m->IsNative());
2929
2930 Class* c = m->GetDeclaringClass();
2931
2932 // If this is a static method, it could be called before the class
2933 // has been initialized.
2934 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002935 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002936 return NULL;
2937 }
2938 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002939 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002940 }
2941
Brian Carlstrom16192862011-09-12 17:50:06 -07002942 std::string detail;
2943 void* native_method;
2944 {
2945 MutexLock mu(libraries_lock);
2946 native_method = libraries->FindNativeMethod(m, detail);
2947 }
2948 // throwing can cause libraries_lock to be reacquired
2949 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002950 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002951 }
2952 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002953}
2954
Elliott Hughes410c0c82011-09-01 17:58:25 -07002955void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2956 {
2957 MutexLock mu(globals_lock);
2958 globals.VisitRoots(visitor, arg);
2959 }
2960 {
2961 MutexLock mu(pins_lock);
2962 pin_table.VisitRoots(visitor, arg);
2963 }
2964 // The weak_globals table is visited by the GC itself (because it mutates the table).
2965}
2966
Ian Rogersdf20fe02011-07-20 20:34:16 -07002967} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002968
2969std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2970 switch (rhs) {
2971 case JNIInvalidRefType:
2972 os << "JNIInvalidRefType";
2973 return os;
2974 case JNILocalRefType:
2975 os << "JNILocalRefType";
2976 return os;
2977 case JNIGlobalRefType:
2978 os << "JNIGlobalRefType";
2979 return os;
2980 case JNIWeakGlobalRefType:
2981 os << "JNIWeakGlobalRefType";
2982 return os;
2983 }
2984}