blob: 0982f8385075ab48259e51c3df5c4a3d70d47103 [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070015#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070016#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070018#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070021#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070022#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070023#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070024#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070026
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070027namespace art {
28
Elliott Hughes2ced6a52011-10-16 18:44:48 -070029static const size_t kMonitorsInitial = 32; // Arbitrary.
30static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
31
32static const size_t kLocalsInitial = 64; // Arbitrary.
33static const size_t kLocalsMax = 512; // Arbitrary sanity check.
34
35static const size_t kPinTableInitial = 16; // Arbitrary.
36static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
37
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070038static size_t gGlobalsInitial = 512; // Arbitrary.
39static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070040
41static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
42static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
43
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070044void SetJniGlobalsMax(size_t max) {
45 if (max != 0) {
46 gGlobalsMax = max;
47 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
48 }
49}
Ian Rogersdf20fe02011-07-20 20:34:16 -070050
Elliott Hughesc5f7c912011-08-18 14:00:42 -070051/*
52 * Add a local reference for an object to the current stack frame. When
53 * the native function returns, the reference will be discarded.
54 *
55 * We need to allow the same reference to be added multiple times.
56 *
57 * This will be called on otherwise unreferenced objects. We cannot do
58 * GC allocations here, and it's best if we don't grab a mutex.
59 *
60 * Returns the local reference (currently just the same pointer that was
61 * passed in), or NULL on failure.
62 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070063template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070064T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
65 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
66 Object* obj = const_cast<Object*>(const_obj);
67
Elliott Hughesc5f7c912011-08-18 14:00:42 -070068 if (obj == NULL) {
69 return NULL;
70 }
71
Elliott Hughesbf86d042011-08-31 17:53:14 -070072 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
73 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070074
Ian Rogers5a7a74a2011-09-26 16:32:29 -070075 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070076 IndirectRef ref = locals.Add(cookie, obj);
77 if (ref == NULL) {
78 // TODO: just change Add's DCHECK to CHECK and lose this?
79 locals.Dump();
80 LOG(FATAL) << "Failed adding to JNI local reference table "
81 << "(has " << locals.Capacity() << " entries)";
82 // TODO: dvmDumpThread(dvmThreadSelf(), false);
83 }
84
85#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070086 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070087 size_t entry_count = locals.Capacity();
88 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070089 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070090 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070091 locals.Dump();
92 // TODO: dvmDumpThread(dvmThreadSelf(), false);
93 // dvmAbort();
94 }
95 }
96#endif
97
Elliott Hughesbf86d042011-08-31 17:53:14 -070098 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070099 // Hand out direct pointers to support broken old apps.
100 return reinterpret_cast<T>(obj);
101 }
102
103 return reinterpret_cast<T>(ref);
104}
105
Elliott Hughesbf86d042011-08-31 17:53:14 -0700106// For external use.
107template<typename T>
108T Decode(JNIEnv* public_env, jobject obj) {
109 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
110 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
111}
112// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700113template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700114template Class* Decode<Class*>(JNIEnv*, jobject);
115template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
116template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700117template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700118template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700119template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700120template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400121template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700122template String* Decode<String*>(JNIEnv*, jobject);
123template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700124
125namespace {
126
Elliott Hughescdf53122011-08-19 15:46:09 -0700127jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
128 if (obj == NULL) {
129 return NULL;
130 }
Elliott Hughes75770752011-08-24 17:52:38 -0700131 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700132 IndirectReferenceTable& weak_globals = vm->weak_globals;
133 MutexLock mu(vm->weak_globals_lock);
134 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
135 return reinterpret_cast<jweak>(ref);
136}
137
Elliott Hughesbf86d042011-08-31 17:53:14 -0700138// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700139template<typename T>
140T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700141 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700142}
143
Elliott Hughes418d20f2011-09-22 14:00:39 -0700144byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
145 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700146 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700147 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700148 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700149 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
150 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700151 case 'Z':
152 case 'B':
153 case 'C':
154 case 'S':
155 case 'I':
156 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
157 offset += 4;
158 break;
159 case 'F':
160 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
161 offset += 4;
162 break;
163 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700164 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700165 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
166 offset += sizeof(Object*);
167 break;
168 }
169 case 'D':
170 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
171 offset += 8;
172 break;
173 case 'J':
174 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
175 offset += 8;
176 break;
177 }
178 }
179 return arg_array.release();
180}
181
Elliott Hughes418d20f2011-09-22 14:00:39 -0700182byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
183 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700184 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700185 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700186 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700187 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
188 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700189 case 'Z':
190 case 'B':
191 case 'C':
192 case 'S':
193 case 'I':
194 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
195 offset += 4;
196 break;
197 case 'F':
198 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
199 offset += 4;
200 break;
201 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700202 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700203 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
204 offset += sizeof(Object*);
205 break;
206 }
207 case 'D':
208 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
209 offset += 8;
210 break;
211 case 'J':
212 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
213 offset += 8;
214 break;
215 }
216 }
217 return arg_array.release();
218}
219
Elliott Hughes418d20f2011-09-22 14:00:39 -0700220JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
221 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700222 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700223 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700224 return result;
225}
226
Elliott Hughes418d20f2011-09-22 14:00:39 -0700227JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
228 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
229 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700230 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700231 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
232 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700233}
234
Elliott Hughes75770752011-08-24 17:52:38 -0700235Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700236 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700237}
238
Elliott Hughes418d20f2011-09-22 14:00:39 -0700239JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
240 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
241 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700242 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700243 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
244 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700245}
246
Elliott Hughes418d20f2011-09-22 14:00:39 -0700247JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
248 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
249 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700250 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700251 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
252 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700253}
254
Elliott Hughes6b436852011-08-12 10:16:44 -0700255// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
256// separated with slashes but aren't wrapped with "L;" like regular descriptors
257// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
258// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
259// supported names with dots too (such as "a.b.C").
260std::string NormalizeJniClassDescriptor(const char* name) {
261 std::string result;
262 // Add the missing "L;" if necessary.
263 if (name[0] == '[') {
264 result = name;
265 } else {
266 result += 'L';
267 result += name;
268 result += ';';
269 }
270 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700271 if (result.find('.') != std::string::npos) {
272 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
273 << "\"" << name << "\"";
274 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700275 }
276 return result;
277}
278
Elliott Hughes14134a12011-09-30 16:55:51 -0700279void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
280 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700281 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes14134a12011-09-30 16:55:51 -0700282 "no %s method \"%s.%s%s\"", kind, class_descriptor.c_str(), name, sig);
283}
284
Elliott Hughescdf53122011-08-19 15:46:09 -0700285jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
286 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700287 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700288 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700289 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700290
291 Method* method = NULL;
292 if (is_static) {
293 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700294 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700295 method = c->FindVirtualMethod(name, sig);
296 if (method == NULL) {
297 // No virtual method matching the signature. Search declared
298 // private methods and constructors.
299 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700300 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700301 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700302
Elliott Hughescdf53122011-08-19 15:46:09 -0700303 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700304 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700305 return NULL;
306 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700307
Elliott Hughes418d20f2011-09-22 14:00:39 -0700308 method->InitJavaFields();
309
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700310 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700311}
312
Elliott Hughescdf53122011-08-19 15:46:09 -0700313jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
314 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700315 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700316 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700317 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700318
319 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 Class* field_type;
321 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
322 if (sig[1] != '\0') {
323 // TODO: need to get the appropriate ClassLoader.
324 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
325 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700326 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700327 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700328 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 if (field_type == NULL) {
330 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700331 DCHECK(ts.Self()->IsExceptionPending());
332 ts.Self()->ClearException();
333 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700334 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700335 "no type \"%s\" found and so no field \"%s\" could be found in class "
336 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 return NULL;
338 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700339 std::string field_type_descriptor = field_type->GetDescriptor()->ToModifiedUtf8();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700340 if (is_static) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700341 field = c->FindStaticField(name, field_type_descriptor);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700342 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700343 field = c->FindInstanceField(name, field_type_descriptor);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700344 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700345 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700346 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700347 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700348 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700349 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700350 return NULL;
351 }
Elliott Hughes80609252011-09-23 17:24:51 -0700352 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700353 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700354}
355
Elliott Hughes75770752011-08-24 17:52:38 -0700356void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
357 JavaVMExt* vm = ts.Vm();
358 MutexLock mu(vm->pins_lock);
359 vm->pin_table.Add(array);
360}
361
362void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
363 JavaVMExt* vm = ts.Vm();
364 MutexLock mu(vm->pins_lock);
365 vm->pin_table.Remove(array);
366}
367
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700368template<typename JniT, typename ArtT>
369JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
370 CHECK_GE(length, 0); // TODO: ReportJniError
371 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700372 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700373}
374
Elliott Hughes75770752011-08-24 17:52:38 -0700375template <typename ArrayT, typename CArrayT, typename ArtArrayT>
376CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
377 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
378 PinPrimitiveArray(ts, array);
379 if (is_copy != NULL) {
380 *is_copy = JNI_FALSE;
381 }
382 return array->GetData();
383}
384
385template <typename ArrayT>
386void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
387 if (mode != JNI_COMMIT) {
388 Array* array = Decode<Array*>(ts, java_array);
389 UnpinPrimitiveArray(ts, array);
390 }
391}
392
Elliott Hughes814e4032011-08-23 12:07:56 -0700393void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700394 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700395 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700396 "%s offset=%d length=%d %s.length=%d",
397 type.c_str(), start, length, identifier, array->GetLength());
398}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700399void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700400 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700401 "offset=%d length=%d string.length()=%d", start, length, array_length);
402}
Elliott Hughes814e4032011-08-23 12:07:56 -0700403
404template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700405void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700406 ArrayT* array = Decode<ArrayT*>(ts, java_array);
407 if (start < 0 || length < 0 || start + length > array->GetLength()) {
408 ThrowAIOOBE(ts, array, start, length, "src");
409 } else {
410 JavaT* data = array->GetData();
411 memcpy(buf, data + start, length * sizeof(JavaT));
412 }
413}
414
415template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700416void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700417 ArrayT* array = Decode<ArrayT*>(ts, java_array);
418 if (start < 0 || length < 0 || start + length > array->GetLength()) {
419 ThrowAIOOBE(ts, array, start, length, "dst");
420 } else {
421 JavaT* data = array->GetData();
422 memcpy(data + start, buf, length * sizeof(JavaT));
423 }
424}
425
Elliott Hughes75770752011-08-24 17:52:38 -0700426jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700427 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
428 CHECK(buffer_class.get() != NULL);
429 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
430}
431
Elliott Hughes75770752011-08-24 17:52:38 -0700432jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700433 static jclass buffer_class = InitDirectByteBufferClass(env);
434 return buffer_class;
435}
436
Elliott Hughes75770752011-08-24 17:52:38 -0700437jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
438 if (vm == NULL || p_env == NULL) {
439 return JNI_ERR;
440 }
441
442 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
443 JavaVMAttachArgs args;
444 if (thr_args == NULL) {
445 // Allow the v1.1 calling convention.
446 args.version = JNI_VERSION_1_2;
447 args.name = NULL;
448 args.group = NULL; // TODO: get "main" thread group
449 } else {
450 args.version = in_args->version;
451 args.name = in_args->name;
452 if (in_args->group != NULL) {
453 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
454 args.group = NULL; // TODO: decode in_args->group
455 } else {
456 args.group = NULL; // TODO: get "main" thread group
457 }
458 }
459 CHECK_GE(args.version, JNI_VERSION_1_2);
460
461 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700462 runtime->AttachCurrentThread(args.name, as_daemon);
463 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700464 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700465}
466
Elliott Hughes79082e32011-08-25 12:07:32 -0700467class SharedLibrary {
468 public:
469 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
470 : path_(path),
471 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700472 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700473 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700474 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700475 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700476 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700477 }
478
Elliott Hughes79082e32011-08-25 12:07:32 -0700479 Object* GetClassLoader() {
480 return class_loader_;
481 }
482
483 std::string GetPath() {
484 return path_;
485 }
486
487 /*
488 * Check the result of an earlier call to JNI_OnLoad on this library. If
489 * the call has not yet finished in another thread, wait for it.
490 */
491 bool CheckOnLoadResult(JavaVMExt* vm) {
492 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700493 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700494 // Check this so we don't end up waiting for ourselves. We need
495 // to return "true" so the caller can continue.
496 LOG(INFO) << *self << " recursive attempt to load library "
497 << "\"" << path_ << "\"";
498 return true;
499 }
500
501 MutexLock mu(jni_on_load_lock_);
502 while (jni_on_load_result_ == kPending) {
503 if (vm->verbose_jni) {
504 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
505 << "JNI_OnLoad...]";
506 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700507 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700508 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700509 }
510
511 bool okay = (jni_on_load_result_ == kOkay);
512 if (vm->verbose_jni) {
513 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
514 << (okay ? "succeeded" : "failed") << "]";
515 }
516 return okay;
517 }
518
519 void SetResult(bool result) {
520 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700521 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700522
523 // Broadcast a wakeup to anybody sleeping on the condition variable.
524 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700525 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700526 }
527
528 void* FindSymbol(const std::string& symbol_name) {
529 return dlsym(handle_, symbol_name.c_str());
530 }
531
532 private:
533 enum JNI_OnLoadState {
534 kPending,
535 kFailed,
536 kOkay,
537 };
538
539 // Path to library "/system/lib/libjni.so".
540 std::string path_;
541
542 // The void* returned by dlopen(3).
543 void* handle_;
544
545 // The ClassLoader this library is associated with.
546 Object* class_loader_;
547
548 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700549 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700550 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700551 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700552 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700553 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 // Result of earlier JNI_OnLoad call.
555 JNI_OnLoadState jni_on_load_result_;
556};
557
Elliott Hughescdf53122011-08-19 15:46:09 -0700558} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700559
Elliott Hughes79082e32011-08-25 12:07:32 -0700560// This exists mainly to keep implementation details out of the header file.
561class Libraries {
562 public:
563 Libraries() {
564 }
565
566 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700567 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700568 }
569
570 SharedLibrary* Get(const std::string& path) {
571 return libraries_[path];
572 }
573
574 void Put(const std::string& path, SharedLibrary* library) {
575 libraries_[path] = library;
576 }
577
578 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700579 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700580 std::string jni_short_name(JniShortName(m));
581 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700582 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
584 SharedLibrary* library = it->second;
585 if (library->GetClassLoader() != declaring_class_loader) {
586 // We only search libraries loaded by the appropriate ClassLoader.
587 continue;
588 }
589 // Try the short name then the long name...
590 void* fn = library->FindSymbol(jni_short_name);
591 if (fn == NULL) {
592 fn = library->FindSymbol(jni_long_name);
593 }
594 if (fn != NULL) {
595 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700596 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700597 << " in \"" << library->GetPath() << "\"]";
598 }
599 return fn;
600 }
601 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700602 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700603 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700604 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700605 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700606 return NULL;
607 }
608
609 private:
610 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
611
612 std::map<std::string, SharedLibrary*> libraries_;
613};
614
Elliott Hughes418d20f2011-09-22 14:00:39 -0700615JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
616 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
617 Object* receiver = Decode<Object*>(env, obj);
618 Method* method = DecodeMethod(mid);
619 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
620 return InvokeWithArgArray(env, receiver, method, arg_array.get());
621}
622
Elliott Hughescdf53122011-08-19 15:46:09 -0700623class JNI {
624 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700625
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 static jint GetVersion(JNIEnv* env) {
627 ScopedJniThreadState ts(env);
628 return JNI_VERSION_1_6;
629 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700630
Elliott Hughescdf53122011-08-19 15:46:09 -0700631 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
632 ScopedJniThreadState ts(env);
633 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700634 return NULL;
635 }
636
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 static jclass FindClass(JNIEnv* env, const char* name) {
638 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700639 Runtime* runtime = Runtime::Current();
640 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700642 Class* c = NULL;
643 if (runtime->IsStarted()) {
644 // TODO: need to get the appropriate ClassLoader.
645 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
646 c = class_linker->FindClass(descriptor, cl);
647 } else {
648 c = class_linker->FindSystemClass(descriptor);
649 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700650 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700651 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700652
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
654 ScopedJniThreadState ts(env);
655 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700656 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700657 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700658
Elliott Hughescdf53122011-08-19 15:46:09 -0700659 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
660 ScopedJniThreadState ts(env);
661 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700662 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700664
Elliott Hughescdf53122011-08-19 15:46:09 -0700665 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
666 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700667 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700668 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700669 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700670
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
672 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700673 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700674 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700675 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700676
Elliott Hughes37f7a402011-08-22 18:56:01 -0700677 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
678 ScopedJniThreadState ts(env);
679 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700680 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700681 }
682
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700683 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700685 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700686 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700687 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700688
Elliott Hughes37f7a402011-08-22 18:56:01 -0700689 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700691 Class* c1 = Decode<Class*>(ts, java_class1);
692 Class* c2 = Decode<Class*>(ts, java_class2);
693 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700695
Elliott Hughes37f7a402011-08-22 18:56:01 -0700696 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700697 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700698 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700699 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700700 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700701 return JNI_TRUE;
702 } else {
703 Object* obj = Decode<Object*>(ts, jobj);
704 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700705 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700706 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700707 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700708
Elliott Hughes37f7a402011-08-22 18:56:01 -0700709 static jint Throw(JNIEnv* env, jthrowable java_exception) {
710 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700711 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700712 if (exception == NULL) {
713 return JNI_ERR;
714 }
715 ts.Self()->SetException(exception);
716 return JNI_OK;
717 }
718
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700719 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700720 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700721 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700722 jmethodID mid = ((msg != NULL)
723 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
724 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700725 if (mid == NULL) {
726 return JNI_ERR;
727 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700728 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700729 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700730 return JNI_ERR;
731 }
732
733 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700734 args[0].l = s.get();
735 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
736 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700737 return JNI_ERR;
738 }
739
Elliott Hughes72025e52011-08-23 17:50:30 -0700740 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700741
Elliott Hughes37f7a402011-08-22 18:56:01 -0700742 return JNI_OK;
743 }
744
745 static jboolean ExceptionCheck(JNIEnv* env) {
746 ScopedJniThreadState ts(env);
747 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
748 }
749
750 static void ExceptionClear(JNIEnv* env) {
751 ScopedJniThreadState ts(env);
752 ts.Self()->ClearException();
753 }
754
755 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700756 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700757
758 Thread* self = ts.Self();
759 Throwable* original_exception = self->GetException();
760 self->ClearException();
761
Elliott Hughesbf86d042011-08-31 17:53:14 -0700762 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700763 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
764 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
765 if (mid == NULL) {
766 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700767 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700768 } else {
769 env->CallVoidMethod(exception.get(), mid);
770 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700771 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700772 << " thrown while calling printStackTrace";
773 self->ClearException();
774 }
775 }
776
777 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700779
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 static jthrowable ExceptionOccurred(JNIEnv* env) {
781 ScopedJniThreadState ts(env);
782 Object* exception = ts.Self()->GetException();
783 if (exception == NULL) {
784 return NULL;
785 } else {
786 // TODO: if adding a local reference failing causes the VM to abort
787 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700788 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700789 if (localException == NULL) {
790 // We were unable to add a new local reference, and threw a new
791 // exception. We can't return "exception", because it's not a
792 // local reference. So we have to return NULL, indicating that
793 // there was no exception, even though it's pretty much raining
794 // exceptions in here.
795 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
796 }
797 return localException;
798 }
799 }
800
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 static void FatalError(JNIEnv* env, const char* msg) {
802 ScopedJniThreadState ts(env);
803 LOG(FATAL) << "JNI FatalError called: " << msg;
804 }
805
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700806 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700808 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
809 return JNI_ERR;
810 }
811 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 return JNI_OK;
813 }
814
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700815 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700817 Object* survivor = Decode<Object*>(ts, java_survivor);
818 ts.Env()->PopFrame();
819 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 }
821
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700822 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700823 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700824 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
825 }
826
827 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
828 // TODO: we should try to expand the table if necessary.
829 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
830 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
831 return JNI_ERR;
832 }
833 // TODO: this isn't quite right, since "capacity" includes holes.
834 size_t capacity = ts.Env()->locals.Capacity();
835 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
836 if (!okay) {
837 ts.Self()->ThrowOutOfMemoryError(caller);
838 }
839 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700840 }
841
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
843 ScopedJniThreadState ts(env);
844 if (obj == NULL) {
845 return NULL;
846 }
847
Elliott Hughes75770752011-08-24 17:52:38 -0700848 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 IndirectReferenceTable& globals = vm->globals;
850 MutexLock mu(vm->globals_lock);
851 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
852 return reinterpret_cast<jobject>(ref);
853 }
854
855 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
856 ScopedJniThreadState ts(env);
857 if (obj == NULL) {
858 return;
859 }
860
Elliott Hughes75770752011-08-24 17:52:38 -0700861 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 IndirectReferenceTable& globals = vm->globals;
863 MutexLock mu(vm->globals_lock);
864
865 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
866 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
867 << "failed to find entry";
868 }
869 }
870
871 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
872 ScopedJniThreadState ts(env);
873 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
874 }
875
876 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
877 ScopedJniThreadState ts(env);
878 if (obj == NULL) {
879 return;
880 }
881
Elliott Hughes75770752011-08-24 17:52:38 -0700882 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700883 IndirectReferenceTable& weak_globals = vm->weak_globals;
884 MutexLock mu(vm->weak_globals_lock);
885
886 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
887 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
888 << "failed to find entry";
889 }
890 }
891
892 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
893 ScopedJniThreadState ts(env);
894 if (obj == NULL) {
895 return NULL;
896 }
897
898 IndirectReferenceTable& locals = ts.Env()->locals;
899
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700900 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
902 return reinterpret_cast<jobject>(ref);
903 }
904
905 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
906 ScopedJniThreadState ts(env);
907 if (obj == NULL) {
908 return;
909 }
910
911 IndirectReferenceTable& locals = ts.Env()->locals;
912
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700913 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 if (!locals.Remove(cookie, obj)) {
915 // Attempting to delete a local reference that is not in the
916 // topmost local reference frame is a no-op. DeleteLocalRef returns
917 // void and doesn't throw any exceptions, but we should probably
918 // complain about it so the user will notice that things aren't
919 // going quite the way they expect.
920 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
921 << "failed to find entry";
922 }
923 }
924
925 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
926 ScopedJniThreadState ts(env);
927 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
928 ? JNI_TRUE : JNI_FALSE;
929 }
930
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700931 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700932 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700933 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700934 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700935 return NULL;
936 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700937 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 }
939
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 ScopedJniThreadState ts(env);
942 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 va_start(args, mid);
944 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 va_end(args);
946 return result;
947 }
948
Elliott Hughes72025e52011-08-23 17:50:30 -0700949 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700951 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700952 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700953 return NULL;
954 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700955 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700956 if (result == NULL) {
957 return NULL;
958 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700959 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 return local_result;
962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700966 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700967 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700968 return NULL;
969 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700970 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700971 if (result == NULL) {
972 return NULL;
973 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700974 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 return local_result;
977 }
978
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
980 ScopedJniThreadState ts(env);
981 return FindMethodID(ts, c, name, sig, false);
982 }
983
984 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
985 ScopedJniThreadState ts(env);
986 return FindMethodID(ts, c, name, sig, true);
987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 va_list ap;
992 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700993 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700995 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001000 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001001 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001006 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001007 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 va_list ap;
1013 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001014 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 va_end(ap);
1016 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001021 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 }
1023
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001026 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 va_list ap;
1032 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001033 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 va_end(ap);
1035 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001040 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001045 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 va_list ap;
1051 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001052 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 va_end(ap);
1054 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001059 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001064 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_list ap;
1070 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001071 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 va_end(ap);
1073 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001078 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001083 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 va_list ap;
1089 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001090 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_end(ap);
1092 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001097 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001102 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 va_list ap;
1108 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001109 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 va_end(ap);
1111 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001116 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001121 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 va_list ap;
1127 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001128 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_end(ap);
1130 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001135 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001140 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 }
1142
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 va_list ap;
1146 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001147 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_end(ap);
1149 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001154 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001159 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 }
1161
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 va_list ap;
1165 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001166 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001172 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 }
1174
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001177 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
1180 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
1183 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001185 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001186 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 va_end(ap);
1188 return local_result;
1189 }
1190
1191 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001194 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001195 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
1198 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001201 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001202 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
1205 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 ScopedJniThreadState ts(env);
1208 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001210 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 va_end(ap);
1212 return result.z;
1213 }
1214
1215 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001218 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
1221 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001224 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
1226
1227 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 ScopedJniThreadState ts(env);
1230 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001232 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 va_end(ap);
1234 return result.b;
1235 }
1236
1237 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001240 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
1243 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001244 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001246 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
1249 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 ScopedJniThreadState ts(env);
1252 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001254 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 va_end(ap);
1256 return result.c;
1257 }
1258
1259 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001262 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 }
1264
1265 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001266 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001268 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
1271 static jshort CallNonvirtualShortMethod(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.s;
1279 }
1280
1281 static jshort CallNonvirtualShortMethodV(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).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
1287 static jshort CallNonvirtualShortMethodA(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).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
Elliott Hughes418d20f2011-09-22 14:00:39 -07001293 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 ScopedJniThreadState ts(env);
1295 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001297 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_end(ap);
1299 return result.i;
1300 }
1301
1302 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001305 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
1308 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001311 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
1314 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 ScopedJniThreadState ts(env);
1317 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001319 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 va_end(ap);
1321 return result.j;
1322 }
1323
1324 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001327 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
1330 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001333 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
1336 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 ScopedJniThreadState ts(env);
1339 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001340 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001341 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 va_end(ap);
1343 return result.f;
1344 }
1345
1346 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001349 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
1352 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001355 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
1358 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 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.d;
1366 }
1367
1368 static jdouble CallNonvirtualDoubleMethodV(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).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
1374 static jdouble CallNonvirtualDoubleMethodA(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).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
1380 static void CallNonvirtualVoidMethod(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 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 va_end(ap);
1387 }
1388
1389 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001390 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001392 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
1395 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001396 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001398 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001401 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 ScopedJniThreadState ts(env);
1403 return FindFieldID(ts, c, name, sig, false);
1404 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001405
1406
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001407 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 ScopedJniThreadState ts(env);
1409 return FindFieldID(ts, c, name, sig, true);
1410 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001411
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001412 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001414 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001415 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001416 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001418
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001419 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001421 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001422 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001425 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001427 Object* o = Decode<Object*>(ts, java_object);
1428 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001429 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001430 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
1432
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001433 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001436 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001437 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440#define GET_PRIMITIVE_FIELD(fn, instance) \
1441 ScopedJniThreadState ts(env); \
1442 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001443 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001444 return f->fn(o)
1445
1446#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1447 ScopedJniThreadState ts(env); \
1448 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001449 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 f->fn(o, value)
1451
1452 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1453 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1457 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1461 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1465 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001468 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1469 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 }
1471
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001472 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1473 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1477 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1481 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1485 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 }
1487
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1489 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 }
1491
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1493 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1497 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 }
1499
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1501 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1505 GET_PRIMITIVE_FIELD(GetLong, NULL);
1506 }
1507
1508 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1509 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1510 }
1511
1512 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1513 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1514 }
1515
1516 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1517 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1518 }
1519
1520 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1521 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1522 }
1523
1524 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1525 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1526 }
1527
1528 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1529 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1530 }
1531
1532 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1533 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1534 }
1535
1536 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1537 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1538 }
1539
1540 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1541 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1542 }
1543
1544 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1545 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1546 }
1547
1548 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1549 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1550 }
1551
1552 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1553 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1554 }
1555
1556 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1557 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1558 }
1559
1560 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1561 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1562 }
1563
1564 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1565 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1566 }
1567
1568 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1569 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1570 }
1571
1572 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1573 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1574 }
1575
1576 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1577 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
Elliott Hughes418d20f2011-09-22 14:00:39 -07001580 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 ScopedJniThreadState ts(env);
1582 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001583 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001584 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001585 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 va_end(ap);
1587 return local_result;
1588 }
1589
Elliott Hughes418d20f2011-09-22 14:00:39 -07001590 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001592 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001593 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes418d20f2011-09-22 14:00:39 -07001596 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001598 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001599 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 }
1601
Elliott Hughes418d20f2011-09-22 14:00:39 -07001602 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 ScopedJniThreadState ts(env);
1604 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001606 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 va_end(ap);
1608 return result.z;
1609 }
1610
Elliott Hughes418d20f2011-09-22 14:00:39 -07001611 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001613 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes418d20f2011-09-22 14:00:39 -07001616 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001618 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes72025e52011-08-23 17:50:30 -07001621 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 ScopedJniThreadState ts(env);
1623 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001624 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001625 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 va_end(ap);
1627 return result.b;
1628 }
1629
Elliott Hughes418d20f2011-09-22 14:00:39 -07001630 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001632 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 }
1634
Elliott Hughes418d20f2011-09-22 14:00:39 -07001635 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001637 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 }
1639
Elliott Hughes72025e52011-08-23 17:50:30 -07001640 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 ScopedJniThreadState ts(env);
1642 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001643 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001644 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 va_end(ap);
1646 return result.c;
1647 }
1648
Elliott Hughes418d20f2011-09-22 14:00:39 -07001649 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001651 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 }
1653
Elliott Hughes418d20f2011-09-22 14:00:39 -07001654 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001656 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 }
1658
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 ScopedJniThreadState ts(env);
1661 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001662 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001663 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 va_end(ap);
1665 return result.s;
1666 }
1667
Elliott Hughes418d20f2011-09-22 14:00:39 -07001668 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001670 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
Elliott Hughes418d20f2011-09-22 14:00:39 -07001673 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001675 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 }
1677
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
1680 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001681 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001682 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 va_end(ap);
1684 return result.i;
1685 }
1686
Elliott Hughes418d20f2011-09-22 14:00:39 -07001687 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001689 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 }
1691
Elliott Hughes418d20f2011-09-22 14:00:39 -07001692 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001694 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 }
1696
Elliott Hughes72025e52011-08-23 17:50:30 -07001697 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 ScopedJniThreadState ts(env);
1699 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001700 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001701 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 va_end(ap);
1703 return result.j;
1704 }
1705
Elliott Hughes418d20f2011-09-22 14:00:39 -07001706 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001708 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes418d20f2011-09-22 14:00:39 -07001711 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001713 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 }
1715
Elliott Hughes72025e52011-08-23 17:50:30 -07001716 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 ScopedJniThreadState ts(env);
1718 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001720 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 va_end(ap);
1722 return result.f;
1723 }
1724
Elliott Hughes418d20f2011-09-22 14:00:39 -07001725 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001727 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes418d20f2011-09-22 14:00:39 -07001730 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001732 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 }
1734
Elliott Hughes72025e52011-08-23 17:50:30 -07001735 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 ScopedJniThreadState ts(env);
1737 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001738 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001739 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 va_end(ap);
1741 return result.d;
1742 }
1743
Elliott Hughes418d20f2011-09-22 14:00:39 -07001744 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001746 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes418d20f2011-09-22 14:00:39 -07001749 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001751 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 }
1753
Elliott Hughes72025e52011-08-23 17:50:30 -07001754 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 ScopedJniThreadState ts(env);
1756 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001758 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 va_end(ap);
1760 }
1761
Elliott Hughes418d20f2011-09-22 14:00:39 -07001762 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001764 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes418d20f2011-09-22 14:00:39 -07001767 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001769 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 }
1771
Elliott Hughes814e4032011-08-23 12:07:56 -07001772 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001774 if (chars == NULL && char_count == 0) {
1775 return NULL;
1776 }
1777 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001778 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
1781 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1782 ScopedJniThreadState ts(env);
1783 if (utf == NULL) {
1784 return NULL;
1785 }
1786 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001787 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
Elliott Hughes814e4032011-08-23 12:07:56 -07001790 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1791 ScopedJniThreadState ts(env);
1792 return Decode<String*>(ts, java_string)->GetLength();
1793 }
1794
1795 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1796 ScopedJniThreadState ts(env);
1797 return Decode<String*>(ts, java_string)->GetUtfLength();
1798 }
1799
Elliott Hughesb465ab02011-08-24 11:21:21 -07001800 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001802 String* s = Decode<String*>(ts, java_string);
1803 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1804 ThrowSIOOBE(ts, start, length, s->GetLength());
1805 } else {
1806 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1807 memcpy(buf, chars + start, length * sizeof(jchar));
1808 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001809 }
1810
Elliott Hughesb465ab02011-08-24 11:21:21 -07001811 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001812 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001813 String* s = Decode<String*>(ts, java_string);
1814 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1815 ThrowSIOOBE(ts, start, length, s->GetLength());
1816 } else {
1817 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1818 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1819 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001820 }
1821
Elliott Hughes75770752011-08-24 17:52:38 -07001822 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001823 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001824 String* s = Decode<String*>(ts, java_string);
1825 const CharArray* chars = s->GetCharArray();
1826 PinPrimitiveArray(ts, chars);
1827 if (is_copy != NULL) {
1828 *is_copy = JNI_FALSE;
1829 }
1830 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001831 }
1832
Elliott Hughes75770752011-08-24 17:52:38 -07001833 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001834 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001835 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes75770752011-08-24 17:52:38 -07001838 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001840 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
Elliott Hughes75770752011-08-24 17:52:38 -07001843 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001845 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 }
1847
Elliott Hughes75770752011-08-24 17:52:38 -07001848 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001849 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001850 if (java_string == NULL) {
1851 return NULL;
1852 }
1853 if (is_copy != NULL) {
1854 *is_copy = JNI_TRUE;
1855 }
1856 String* s = Decode<String*>(ts, java_string);
1857 size_t byte_count = s->GetUtfLength();
1858 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001859 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001860 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1861 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1862 bytes[byte_count] = '\0';
1863 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001864 }
1865
Elliott Hughes75770752011-08-24 17:52:38 -07001866 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001867 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001868 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001869 }
1870
Elliott Hughesbd935992011-08-22 11:59:34 -07001871 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001873 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001874 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001875 Array* array = obj->AsArray();
1876 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 }
1878
Elliott Hughes814e4032011-08-23 12:07:56 -07001879 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001881 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001882 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
1884
1885 static void SetObjectArrayElement(JNIEnv* env,
1886 jobjectArray java_array, jsize index, jobject java_value) {
1887 ScopedJniThreadState ts(env);
1888 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1889 Object* value = Decode<Object*>(ts, java_value);
1890 array->Set(index, value);
1891 }
1892
1893 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1894 ScopedJniThreadState ts(env);
1895 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1896 }
1897
1898 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1899 ScopedJniThreadState ts(env);
1900 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1901 }
1902
1903 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1904 ScopedJniThreadState ts(env);
1905 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1906 }
1907
1908 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1909 ScopedJniThreadState ts(env);
1910 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1911 }
1912
1913 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1914 ScopedJniThreadState ts(env);
1915 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1916 }
1917
1918 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1919 ScopedJniThreadState ts(env);
1920 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1921 }
1922
1923 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1924 ScopedJniThreadState ts(env);
1925 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1926 }
1927
1928 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1929 ScopedJniThreadState ts(env);
1930 CHECK_GE(length, 0); // TODO: ReportJniError
1931
1932 // Compute the array class corresponding to the given element class.
1933 Class* element_class = Decode<Class*>(ts, element_jclass);
1934 std::string descriptor;
1935 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001936 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001937
1938 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001939 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1940 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 return NULL;
1942 }
1943
Elliott Hughes75770752011-08-24 17:52:38 -07001944 // Allocate and initialize if necessary.
1945 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001947 if (initial_element != NULL) {
1948 Object* initial_object = Decode<Object*>(ts, initial_element);
1949 for (jsize i = 0; i < length; ++i) {
1950 result->Set(i, initial_object);
1951 }
1952 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001953 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
1956 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1957 ScopedJniThreadState ts(env);
1958 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1959 }
1960
Elliott Hughes75770752011-08-24 17:52:38 -07001961 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001962 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001963 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001964 }
1965
Elliott Hughes75770752011-08-24 17:52:38 -07001966 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001967 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001968 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001969 }
1970
Elliott Hughes75770752011-08-24 17:52:38 -07001971 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001973 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Elliott Hughes75770752011-08-24 17:52:38 -07001976 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001978 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Elliott Hughes75770752011-08-24 17:52:38 -07001981 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001983 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Elliott Hughes75770752011-08-24 17:52:38 -07001986 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001988 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes75770752011-08-24 17:52:38 -07001991 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001993 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes75770752011-08-24 17:52:38 -07001996 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001998 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Elliott Hughes75770752011-08-24 17:52:38 -07002001 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002003 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Elliott Hughes75770752011-08-24 17:52:38 -07002006 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002008 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Elliott Hughes75770752011-08-24 17:52:38 -07002011 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002013 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes75770752011-08-24 17:52:38 -07002016 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002018 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes75770752011-08-24 17:52:38 -07002021 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002023 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes75770752011-08-24 17:52:38 -07002026 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002028 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes75770752011-08-24 17:52:38 -07002031 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002033 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes75770752011-08-24 17:52:38 -07002036 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002038 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes75770752011-08-24 17:52:38 -07002041 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002043 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Elliott Hughes75770752011-08-24 17:52:38 -07002046 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002048 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Elliott Hughes814e4032011-08-23 12:07:56 -07002051 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Elliott Hughes814e4032011-08-23 12:07:56 -07002126 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
Elliott Hughes5174fe62011-08-23 15:12:35 -07002131 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002133 Class* c = Decode<Class*>(ts, java_class);
2134
Elliott Hughes5174fe62011-08-23 15:12:35 -07002135 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 const char* name = methods[i].name;
2137 const char* sig = methods[i].signature;
2138
2139 if (*sig == '!') {
2140 // TODO: fast jni. it's too noisy to log all these.
2141 ++sig;
2142 }
2143
Elliott Hughes5174fe62011-08-23 15:12:35 -07002144 Method* m = c->FindDirectMethod(name, sig);
2145 if (m == NULL) {
2146 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002148 if (m == NULL) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002149 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002151 } else if (!m->IsNative()) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002152 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 return JNI_ERR;
2154 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155
Elliott Hughes75770752011-08-24 17:52:38 -07002156 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002157 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158 }
2159
2160 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162 return JNI_OK;
2163 }
2164
Elliott Hughes5174fe62011-08-23 15:12:35 -07002165 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002167 Class* c = Decode<Class*>(ts, java_class);
2168
Elliott Hughes75770752011-08-24 17:52:38 -07002169 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002170 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002171 }
2172
2173 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2174 Method* m = c->GetDirectMethod(i);
2175 if (m->IsNative()) {
2176 m->UnregisterNative();
2177 }
2178 }
2179 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2180 Method* m = c->GetVirtualMethod(i);
2181 if (m->IsNative()) {
2182 m->UnregisterNative();
2183 }
2184 }
2185
2186 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 }
2188
Elliott Hughes72025e52011-08-23 17:50:30 -07002189 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002191 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002192 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
Elliott Hughes72025e52011-08-23 17:50:30 -07002195 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002197 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002198 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 }
2200
2201 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2202 ScopedJniThreadState ts(env);
2203 Runtime* runtime = Runtime::Current();
2204 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002205 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 } else {
2207 *vm = NULL;
2208 }
2209 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2210 }
2211
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2213 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002214
2215 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002216 CHECK(address != NULL); // TODO: ReportJniError
2217 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002218
2219 jclass buffer_class = GetDirectByteBufferClass(env);
2220 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2221 if (mid == NULL) {
2222 return NULL;
2223 }
2224
2225 // At the moment, the Java side is limited to 32 bits.
2226 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2227 CHECK_LE(capacity, 0xffffffff);
2228 jint address_arg = reinterpret_cast<jint>(address);
2229 jint capacity_arg = static_cast<jint>(capacity);
2230
2231 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2232 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Elliott Hughesb465ab02011-08-24 11:21:21 -07002235 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002237 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2238 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Elliott Hughesb465ab02011-08-24 11:21:21 -07002241 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002243 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2244 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249
Elliott Hughes75770752011-08-24 17:52:38 -07002250 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002251
2252 // Do we definitely know what kind of reference this is?
2253 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2254 IndirectRefKind kind = GetIndirectRefKind(ref);
2255 switch (kind) {
2256 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002257 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2258 return JNILocalRefType;
2259 }
2260 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002261 case kGlobal:
2262 return JNIGlobalRefType;
2263 case kWeakGlobal:
2264 return JNIWeakGlobalRefType;
2265 case kSirtOrInvalid:
2266 // Is it in a stack IRT?
2267 if (ts.Self()->SirtContains(java_object)) {
2268 return JNILocalRefType;
2269 }
2270
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002271 if (!ts.Env()->work_around_app_jni_bugs) {
2272 return JNIInvalidRefType;
2273 }
2274
Elliott Hughesb465ab02011-08-24 11:21:21 -07002275 // If we're handing out direct pointers, check whether it's a direct pointer
2276 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002277 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002278 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002279 return JNILocalRefType;
2280 }
2281 }
2282
2283 return JNIInvalidRefType;
2284 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 }
2286};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002287
Elliott Hughesa2501992011-08-26 19:39:54 -07002288const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002289 NULL, // reserved0.
2290 NULL, // reserved1.
2291 NULL, // reserved2.
2292 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002293 JNI::GetVersion,
2294 JNI::DefineClass,
2295 JNI::FindClass,
2296 JNI::FromReflectedMethod,
2297 JNI::FromReflectedField,
2298 JNI::ToReflectedMethod,
2299 JNI::GetSuperclass,
2300 JNI::IsAssignableFrom,
2301 JNI::ToReflectedField,
2302 JNI::Throw,
2303 JNI::ThrowNew,
2304 JNI::ExceptionOccurred,
2305 JNI::ExceptionDescribe,
2306 JNI::ExceptionClear,
2307 JNI::FatalError,
2308 JNI::PushLocalFrame,
2309 JNI::PopLocalFrame,
2310 JNI::NewGlobalRef,
2311 JNI::DeleteGlobalRef,
2312 JNI::DeleteLocalRef,
2313 JNI::IsSameObject,
2314 JNI::NewLocalRef,
2315 JNI::EnsureLocalCapacity,
2316 JNI::AllocObject,
2317 JNI::NewObject,
2318 JNI::NewObjectV,
2319 JNI::NewObjectA,
2320 JNI::GetObjectClass,
2321 JNI::IsInstanceOf,
2322 JNI::GetMethodID,
2323 JNI::CallObjectMethod,
2324 JNI::CallObjectMethodV,
2325 JNI::CallObjectMethodA,
2326 JNI::CallBooleanMethod,
2327 JNI::CallBooleanMethodV,
2328 JNI::CallBooleanMethodA,
2329 JNI::CallByteMethod,
2330 JNI::CallByteMethodV,
2331 JNI::CallByteMethodA,
2332 JNI::CallCharMethod,
2333 JNI::CallCharMethodV,
2334 JNI::CallCharMethodA,
2335 JNI::CallShortMethod,
2336 JNI::CallShortMethodV,
2337 JNI::CallShortMethodA,
2338 JNI::CallIntMethod,
2339 JNI::CallIntMethodV,
2340 JNI::CallIntMethodA,
2341 JNI::CallLongMethod,
2342 JNI::CallLongMethodV,
2343 JNI::CallLongMethodA,
2344 JNI::CallFloatMethod,
2345 JNI::CallFloatMethodV,
2346 JNI::CallFloatMethodA,
2347 JNI::CallDoubleMethod,
2348 JNI::CallDoubleMethodV,
2349 JNI::CallDoubleMethodA,
2350 JNI::CallVoidMethod,
2351 JNI::CallVoidMethodV,
2352 JNI::CallVoidMethodA,
2353 JNI::CallNonvirtualObjectMethod,
2354 JNI::CallNonvirtualObjectMethodV,
2355 JNI::CallNonvirtualObjectMethodA,
2356 JNI::CallNonvirtualBooleanMethod,
2357 JNI::CallNonvirtualBooleanMethodV,
2358 JNI::CallNonvirtualBooleanMethodA,
2359 JNI::CallNonvirtualByteMethod,
2360 JNI::CallNonvirtualByteMethodV,
2361 JNI::CallNonvirtualByteMethodA,
2362 JNI::CallNonvirtualCharMethod,
2363 JNI::CallNonvirtualCharMethodV,
2364 JNI::CallNonvirtualCharMethodA,
2365 JNI::CallNonvirtualShortMethod,
2366 JNI::CallNonvirtualShortMethodV,
2367 JNI::CallNonvirtualShortMethodA,
2368 JNI::CallNonvirtualIntMethod,
2369 JNI::CallNonvirtualIntMethodV,
2370 JNI::CallNonvirtualIntMethodA,
2371 JNI::CallNonvirtualLongMethod,
2372 JNI::CallNonvirtualLongMethodV,
2373 JNI::CallNonvirtualLongMethodA,
2374 JNI::CallNonvirtualFloatMethod,
2375 JNI::CallNonvirtualFloatMethodV,
2376 JNI::CallNonvirtualFloatMethodA,
2377 JNI::CallNonvirtualDoubleMethod,
2378 JNI::CallNonvirtualDoubleMethodV,
2379 JNI::CallNonvirtualDoubleMethodA,
2380 JNI::CallNonvirtualVoidMethod,
2381 JNI::CallNonvirtualVoidMethodV,
2382 JNI::CallNonvirtualVoidMethodA,
2383 JNI::GetFieldID,
2384 JNI::GetObjectField,
2385 JNI::GetBooleanField,
2386 JNI::GetByteField,
2387 JNI::GetCharField,
2388 JNI::GetShortField,
2389 JNI::GetIntField,
2390 JNI::GetLongField,
2391 JNI::GetFloatField,
2392 JNI::GetDoubleField,
2393 JNI::SetObjectField,
2394 JNI::SetBooleanField,
2395 JNI::SetByteField,
2396 JNI::SetCharField,
2397 JNI::SetShortField,
2398 JNI::SetIntField,
2399 JNI::SetLongField,
2400 JNI::SetFloatField,
2401 JNI::SetDoubleField,
2402 JNI::GetStaticMethodID,
2403 JNI::CallStaticObjectMethod,
2404 JNI::CallStaticObjectMethodV,
2405 JNI::CallStaticObjectMethodA,
2406 JNI::CallStaticBooleanMethod,
2407 JNI::CallStaticBooleanMethodV,
2408 JNI::CallStaticBooleanMethodA,
2409 JNI::CallStaticByteMethod,
2410 JNI::CallStaticByteMethodV,
2411 JNI::CallStaticByteMethodA,
2412 JNI::CallStaticCharMethod,
2413 JNI::CallStaticCharMethodV,
2414 JNI::CallStaticCharMethodA,
2415 JNI::CallStaticShortMethod,
2416 JNI::CallStaticShortMethodV,
2417 JNI::CallStaticShortMethodA,
2418 JNI::CallStaticIntMethod,
2419 JNI::CallStaticIntMethodV,
2420 JNI::CallStaticIntMethodA,
2421 JNI::CallStaticLongMethod,
2422 JNI::CallStaticLongMethodV,
2423 JNI::CallStaticLongMethodA,
2424 JNI::CallStaticFloatMethod,
2425 JNI::CallStaticFloatMethodV,
2426 JNI::CallStaticFloatMethodA,
2427 JNI::CallStaticDoubleMethod,
2428 JNI::CallStaticDoubleMethodV,
2429 JNI::CallStaticDoubleMethodA,
2430 JNI::CallStaticVoidMethod,
2431 JNI::CallStaticVoidMethodV,
2432 JNI::CallStaticVoidMethodA,
2433 JNI::GetStaticFieldID,
2434 JNI::GetStaticObjectField,
2435 JNI::GetStaticBooleanField,
2436 JNI::GetStaticByteField,
2437 JNI::GetStaticCharField,
2438 JNI::GetStaticShortField,
2439 JNI::GetStaticIntField,
2440 JNI::GetStaticLongField,
2441 JNI::GetStaticFloatField,
2442 JNI::GetStaticDoubleField,
2443 JNI::SetStaticObjectField,
2444 JNI::SetStaticBooleanField,
2445 JNI::SetStaticByteField,
2446 JNI::SetStaticCharField,
2447 JNI::SetStaticShortField,
2448 JNI::SetStaticIntField,
2449 JNI::SetStaticLongField,
2450 JNI::SetStaticFloatField,
2451 JNI::SetStaticDoubleField,
2452 JNI::NewString,
2453 JNI::GetStringLength,
2454 JNI::GetStringChars,
2455 JNI::ReleaseStringChars,
2456 JNI::NewStringUTF,
2457 JNI::GetStringUTFLength,
2458 JNI::GetStringUTFChars,
2459 JNI::ReleaseStringUTFChars,
2460 JNI::GetArrayLength,
2461 JNI::NewObjectArray,
2462 JNI::GetObjectArrayElement,
2463 JNI::SetObjectArrayElement,
2464 JNI::NewBooleanArray,
2465 JNI::NewByteArray,
2466 JNI::NewCharArray,
2467 JNI::NewShortArray,
2468 JNI::NewIntArray,
2469 JNI::NewLongArray,
2470 JNI::NewFloatArray,
2471 JNI::NewDoubleArray,
2472 JNI::GetBooleanArrayElements,
2473 JNI::GetByteArrayElements,
2474 JNI::GetCharArrayElements,
2475 JNI::GetShortArrayElements,
2476 JNI::GetIntArrayElements,
2477 JNI::GetLongArrayElements,
2478 JNI::GetFloatArrayElements,
2479 JNI::GetDoubleArrayElements,
2480 JNI::ReleaseBooleanArrayElements,
2481 JNI::ReleaseByteArrayElements,
2482 JNI::ReleaseCharArrayElements,
2483 JNI::ReleaseShortArrayElements,
2484 JNI::ReleaseIntArrayElements,
2485 JNI::ReleaseLongArrayElements,
2486 JNI::ReleaseFloatArrayElements,
2487 JNI::ReleaseDoubleArrayElements,
2488 JNI::GetBooleanArrayRegion,
2489 JNI::GetByteArrayRegion,
2490 JNI::GetCharArrayRegion,
2491 JNI::GetShortArrayRegion,
2492 JNI::GetIntArrayRegion,
2493 JNI::GetLongArrayRegion,
2494 JNI::GetFloatArrayRegion,
2495 JNI::GetDoubleArrayRegion,
2496 JNI::SetBooleanArrayRegion,
2497 JNI::SetByteArrayRegion,
2498 JNI::SetCharArrayRegion,
2499 JNI::SetShortArrayRegion,
2500 JNI::SetIntArrayRegion,
2501 JNI::SetLongArrayRegion,
2502 JNI::SetFloatArrayRegion,
2503 JNI::SetDoubleArrayRegion,
2504 JNI::RegisterNatives,
2505 JNI::UnregisterNatives,
2506 JNI::MonitorEnter,
2507 JNI::MonitorExit,
2508 JNI::GetJavaVM,
2509 JNI::GetStringRegion,
2510 JNI::GetStringUTFRegion,
2511 JNI::GetPrimitiveArrayCritical,
2512 JNI::ReleasePrimitiveArrayCritical,
2513 JNI::GetStringCritical,
2514 JNI::ReleaseStringCritical,
2515 JNI::NewWeakGlobalRef,
2516 JNI::DeleteWeakGlobalRef,
2517 JNI::ExceptionCheck,
2518 JNI::NewDirectByteBuffer,
2519 JNI::GetDirectBufferAddress,
2520 JNI::GetDirectBufferCapacity,
2521 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002522};
2523
Elliott Hughes75770752011-08-24 17:52:38 -07002524JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002525 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002526 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002527 local_ref_cookie(IRT_FIRST_SEGMENT),
2528 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002529 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002530 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002531 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002532 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002533 functions = unchecked_functions = &gNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002534 if (vm->check_jni) {
2535 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002536 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002537 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2538 // errors will ensue.
2539 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2540 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002541}
2542
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002543JNIEnvExt::~JNIEnvExt() {
2544}
2545
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002546void JNIEnvExt::EnableCheckJni() {
2547 check_jni = true;
2548 functions = GetCheckJniNativeInterface();
2549}
2550
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002551void JNIEnvExt::DumpReferenceTables() {
2552 locals.Dump();
2553 monitors.Dump();
2554}
2555
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002556void JNIEnvExt::PushFrame(int capacity) {
2557 stacked_local_ref_cookies.push_back(local_ref_cookie);
2558 local_ref_cookie = locals.GetSegmentState();
2559}
2560
2561void JNIEnvExt::PopFrame() {
2562 locals.SetSegmentState(local_ref_cookie);
2563 local_ref_cookie = stacked_local_ref_cookies.back();
2564 stacked_local_ref_cookies.pop_back();
2565}
2566
Carl Shapiroea4dca82011-08-01 13:45:38 -07002567// JNI Invocation interface.
2568
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002569extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2570 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2571 if (args->version < JNI_VERSION_1_2) {
2572 return JNI_EVERSION;
2573 }
2574 Runtime::Options options;
2575 for (int i = 0; i < args->nOptions; ++i) {
2576 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002577 options.push_back(std::make_pair(StringPiece(option->optionString),
2578 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002579 }
2580 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002581 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002582 if (runtime == NULL) {
2583 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002584 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002585 runtime->Start();
2586 *p_env = Thread::Current()->GetJniEnv();
2587 *p_vm = runtime->GetJavaVM();
2588 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002589}
2590
Elliott Hughesf2682d52011-08-15 16:37:04 -07002591extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002592 Runtime* runtime = Runtime::Current();
2593 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002594 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002595 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002596 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002597 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002598 }
2599 return JNI_OK;
2600}
2601
2602// Historically unsupported.
2603extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2604 return JNI_ERR;
2605}
2606
Elliott Hughescdf53122011-08-19 15:46:09 -07002607class JII {
2608 public:
2609 static jint DestroyJavaVM(JavaVM* vm) {
2610 if (vm == NULL) {
2611 return JNI_ERR;
2612 } else {
2613 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2614 delete raw_vm->runtime;
2615 return JNI_OK;
2616 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002617 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002618
Elliott Hughescdf53122011-08-19 15:46:09 -07002619 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002620 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002621 }
2622
2623 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002624 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002625 }
2626
2627 static jint DetachCurrentThread(JavaVM* vm) {
2628 if (vm == NULL) {
2629 return JNI_ERR;
2630 } else {
2631 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2632 Runtime* runtime = raw_vm->runtime;
2633 runtime->DetachCurrentThread();
2634 return JNI_OK;
2635 }
2636 }
2637
2638 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2639 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2640 return JNI_EVERSION;
2641 }
2642 if (vm == NULL || env == NULL) {
2643 return JNI_ERR;
2644 }
2645 Thread* thread = Thread::Current();
2646 if (thread == NULL) {
2647 *env = NULL;
2648 return JNI_EDETACHED;
2649 }
2650 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002651 return JNI_OK;
2652 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002653};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002654
Elliott Hughesa2501992011-08-26 19:39:54 -07002655const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002656 NULL, // reserved0
2657 NULL, // reserved1
2658 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002659 JII::DestroyJavaVM,
2660 JII::AttachCurrentThread,
2661 JII::DetachCurrentThread,
2662 JII::GetEnv,
2663 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002664};
2665
Elliott Hughesa0957642011-09-02 14:27:33 -07002666JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002667 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002668 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002669 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002670 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002671 verbose_jni(options->IsVerbose("jni")),
2672 log_third_party_jni(options->IsVerbose("third-party-jni")),
2673 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002674 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002675 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002676 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002677 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002678 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002679 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002680 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002681 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002682 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002683 functions = unchecked_functions = &gInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002684 if (options->check_jni_) {
2685 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002686 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002687}
2688
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002689JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002690 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002691}
2692
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002693void JavaVMExt::EnableCheckJni() {
2694 check_jni = true;
2695 functions = GetCheckJniInvokeInterface();
2696}
2697
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002698void JavaVMExt::DumpReferenceTables() {
2699 {
2700 MutexLock mu(globals_lock);
2701 globals.Dump();
2702 }
2703 {
2704 MutexLock mu(weak_globals_lock);
2705 weak_globals.Dump();
2706 }
2707 {
2708 MutexLock mu(pins_lock);
2709 pin_table.Dump();
2710 }
2711}
2712
Elliott Hughes75770752011-08-24 17:52:38 -07002713bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2714 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002715
2716 // See if we've already loaded this library. If we have, and the class loader
2717 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002718 // TODO: for better results we should canonicalize the pathname (or even compare
2719 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002720 SharedLibrary* library;
2721 {
2722 // TODO: move the locking (and more of this logic) into Libraries.
2723 MutexLock mu(libraries_lock);
2724 library = libraries->Get(path);
2725 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002726 if (library != NULL) {
2727 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002728 // The library will be associated with class_loader. The JNI
2729 // spec says we can't load the same library into more than one
2730 // class loader.
2731 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2732 "ClassLoader %p; can't open in ClassLoader %p",
2733 path.c_str(), library->GetClassLoader(), class_loader);
2734 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002735 return false;
2736 }
2737 if (verbose_jni) {
2738 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2739 << "ClassLoader " << class_loader << "]";
2740 }
2741 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002742 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2743 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002744 return false;
2745 }
2746 return true;
2747 }
2748
2749 // Open the shared library. Because we're using a full path, the system
2750 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2751 // resolve this library's dependencies though.)
2752
2753 // Failures here are expected when java.library.path has several entries
2754 // and we have to hunt for the lib.
2755
2756 // The current version of the dynamic linker prints detailed information
2757 // about dlopen() failures. Some things to check if the message is
2758 // cryptic:
2759 // - make sure the library exists on the device
2760 // - verify that the right path is being opened (the debug log message
2761 // above can help with that)
2762 // - check to see if the library is valid (e.g. not zero bytes long)
2763 // - check config/prelink-linux-arm.map to ensure that the library
2764 // is listed and is not being overrun by the previous entry (if
2765 // loading suddenly stops working on a prelinked library, this is
2766 // a good one to check)
2767 // - write a trivial app that calls sleep() then dlopen(), attach
2768 // to it with "strace -p <pid>" while it sleeps, and watch for
2769 // attempts to open nonexistent dependent shared libs
2770
2771 // TODO: automate some of these checks!
2772
2773 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002774 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002775 // the GC to ignore us.
2776 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002777 void* handle = NULL;
2778 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002779 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002780 handle = dlopen(path.c_str(), RTLD_LAZY);
2781 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002782
2783 if (verbose_jni) {
2784 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2785 }
2786
2787 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002788 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002789 return false;
2790 }
2791
2792 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002793 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002794 // TODO: move the locking (and more of this logic) into Libraries.
2795 MutexLock mu(libraries_lock);
2796 library = libraries->Get(path);
2797 if (library != NULL) {
2798 LOG(INFO) << "WOW: we lost a race to add shared library: "
2799 << "\"" << path << "\" ClassLoader=" << class_loader;
2800 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002801 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002802 library = new SharedLibrary(path, handle, class_loader);
2803 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002804 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002805
2806 if (verbose_jni) {
2807 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2808 }
2809
2810 bool result = true;
2811 void* sym = dlsym(handle, "JNI_OnLoad");
2812 if (sym == NULL) {
2813 if (verbose_jni) {
2814 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2815 }
2816 } else {
2817 // Call JNI_OnLoad. We have to override the current class
2818 // loader, which will always be "null" since the stuff at the
2819 // top of the stack is around Runtime.loadLibrary(). (See
2820 // the comments in the JNI FindClass function.)
2821 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2822 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002823 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002824 self->SetClassLoaderOverride(class_loader);
2825
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002826 int version = 0;
2827 {
2828 ScopedThreadStateChange tsc(self, Thread::kNative);
2829 if (verbose_jni) {
2830 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2831 }
2832 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002833 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002834
Brian Carlstromaded5f72011-10-07 17:15:04 -07002835 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002836
2837 if (version != JNI_VERSION_1_2 &&
2838 version != JNI_VERSION_1_4 &&
2839 version != JNI_VERSION_1_6) {
2840 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2841 << "bad version: " << version;
2842 // It's unwise to call dlclose() here, but we can mark it
2843 // as bad and ensure that future load attempts will fail.
2844 // We don't know how far JNI_OnLoad got, so there could
2845 // be some partially-initialized stuff accessible through
2846 // newly-registered native method calls. We could try to
2847 // unregister them, but that doesn't seem worthwhile.
2848 result = false;
2849 } else {
2850 if (verbose_jni) {
2851 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2852 << " from JNI_OnLoad in \"" << path << "\"]";
2853 }
2854 }
2855 }
2856
2857 library->SetResult(result);
2858 return result;
2859}
2860
2861void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2862 CHECK(m->IsNative());
2863
2864 Class* c = m->GetDeclaringClass();
2865
2866 // If this is a static method, it could be called before the class
2867 // has been initialized.
2868 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002869 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002870 return NULL;
2871 }
2872 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002873 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002874 }
2875
Brian Carlstrom16192862011-09-12 17:50:06 -07002876 std::string detail;
2877 void* native_method;
2878 {
2879 MutexLock mu(libraries_lock);
2880 native_method = libraries->FindNativeMethod(m, detail);
2881 }
2882 // throwing can cause libraries_lock to be reacquired
2883 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002884 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002885 }
2886 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002887}
2888
Elliott Hughes410c0c82011-09-01 17:58:25 -07002889void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2890 {
2891 MutexLock mu(globals_lock);
2892 globals.VisitRoots(visitor, arg);
2893 }
2894 {
2895 MutexLock mu(pins_lock);
2896 pin_table.VisitRoots(visitor, arg);
2897 }
2898 // The weak_globals table is visited by the GC itself (because it mutates the table).
2899}
2900
Ian Rogersdf20fe02011-07-20 20:34:16 -07002901} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002902
2903std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2904 switch (rhs) {
2905 case JNIInvalidRefType:
2906 os << "JNIInvalidRefType";
2907 return os;
2908 case JNILocalRefType:
2909 os << "JNILocalRefType";
2910 return os;
2911 case JNIGlobalRefType:
2912 os << "JNIGlobalRefType";
2913 return os;
2914 case JNIWeakGlobalRefType:
2915 os << "JNIWeakGlobalRefType";
2916 return os;
2917 }
2918}