blob: 45eedb81ead4a0628acf197dffc425df2212a046 [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070015#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070016#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070018#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070021#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070022#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070023#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070024#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070026
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070027namespace art {
28
Elliott Hughes2ced6a52011-10-16 18:44:48 -070029static const size_t kMonitorsInitial = 32; // Arbitrary.
30static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
31
32static const size_t kLocalsInitial = 64; // Arbitrary.
33static const size_t kLocalsMax = 512; // Arbitrary sanity check.
34
35static const size_t kPinTableInitial = 16; // Arbitrary.
36static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
37
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070038static size_t gGlobalsInitial = 512; // Arbitrary.
39static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070040
41static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
42static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
43
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070044void SetJniGlobalsMax(size_t max) {
45 if (max != 0) {
46 gGlobalsMax = max;
47 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
48 }
49}
Ian Rogersdf20fe02011-07-20 20:34:16 -070050
Elliott Hughesc5f7c912011-08-18 14:00:42 -070051/*
52 * Add a local reference for an object to the current stack frame. When
53 * the native function returns, the reference will be discarded.
54 *
55 * We need to allow the same reference to be added multiple times.
56 *
57 * This will be called on otherwise unreferenced objects. We cannot do
58 * GC allocations here, and it's best if we don't grab a mutex.
59 *
60 * Returns the local reference (currently just the same pointer that was
61 * passed in), or NULL on failure.
62 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070063template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070064T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
65 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
66 Object* obj = const_cast<Object*>(const_obj);
67
Elliott Hughesc5f7c912011-08-18 14:00:42 -070068 if (obj == NULL) {
69 return NULL;
70 }
71
Elliott Hughesbf86d042011-08-31 17:53:14 -070072 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
73 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070074
Ian Rogers5a7a74a2011-09-26 16:32:29 -070075 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070076 IndirectRef ref = locals.Add(cookie, obj);
77 if (ref == NULL) {
78 // TODO: just change Add's DCHECK to CHECK and lose this?
79 locals.Dump();
80 LOG(FATAL) << "Failed adding to JNI local reference table "
81 << "(has " << locals.Capacity() << " entries)";
82 // TODO: dvmDumpThread(dvmThreadSelf(), false);
83 }
84
85#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070086 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070087 size_t entry_count = locals.Capacity();
88 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070089 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070090 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070091 locals.Dump();
92 // TODO: dvmDumpThread(dvmThreadSelf(), false);
93 // dvmAbort();
94 }
95 }
96#endif
97
Elliott Hughesbf86d042011-08-31 17:53:14 -070098 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070099 // Hand out direct pointers to support broken old apps.
100 return reinterpret_cast<T>(obj);
101 }
102
103 return reinterpret_cast<T>(ref);
104}
105
Elliott Hughesbf86d042011-08-31 17:53:14 -0700106// For external use.
107template<typename T>
108T Decode(JNIEnv* public_env, jobject obj) {
109 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
110 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
111}
112// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700113template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700114template Class* Decode<Class*>(JNIEnv*, jobject);
115template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
116template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700117template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700118template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700119template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700120template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400121template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700122template String* Decode<String*>(JNIEnv*, jobject);
123template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700124
125namespace {
126
Elliott Hughescdf53122011-08-19 15:46:09 -0700127jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
128 if (obj == NULL) {
129 return NULL;
130 }
Elliott Hughes75770752011-08-24 17:52:38 -0700131 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700132 IndirectReferenceTable& weak_globals = vm->weak_globals;
133 MutexLock mu(vm->weak_globals_lock);
134 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
135 return reinterpret_cast<jweak>(ref);
136}
137
Elliott Hughesbf86d042011-08-31 17:53:14 -0700138// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700139template<typename T>
140T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700141 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700142}
143
Elliott Hughes418d20f2011-09-22 14:00:39 -0700144byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
145 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700146 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700147 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700148 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700149 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
150 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700151 case 'Z':
152 case 'B':
153 case 'C':
154 case 'S':
155 case 'I':
156 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
157 offset += 4;
158 break;
159 case 'F':
160 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
161 offset += 4;
162 break;
163 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700164 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700165 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
166 offset += sizeof(Object*);
167 break;
168 }
169 case 'D':
170 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
171 offset += 8;
172 break;
173 case 'J':
174 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
175 offset += 8;
176 break;
177 }
178 }
179 return arg_array.release();
180}
181
Elliott Hughes418d20f2011-09-22 14:00:39 -0700182byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
183 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700184 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700185 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700186 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700187 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
188 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700189 case 'Z':
190 case 'B':
191 case 'C':
192 case 'S':
193 case 'I':
194 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
195 offset += 4;
196 break;
197 case 'F':
198 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
199 offset += 4;
200 break;
201 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700202 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700203 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
204 offset += sizeof(Object*);
205 break;
206 }
207 case 'D':
208 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
209 offset += 8;
210 break;
211 case 'J':
212 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
213 offset += 8;
214 break;
215 }
216 }
217 return arg_array.release();
218}
219
Elliott Hughes418d20f2011-09-22 14:00:39 -0700220JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
221 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700222 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700223 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700224 return result;
225}
226
Elliott Hughes418d20f2011-09-22 14:00:39 -0700227JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
228 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
229 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700230 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700231 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
232 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700233}
234
Elliott Hughes75770752011-08-24 17:52:38 -0700235Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700236 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700237}
238
Elliott Hughes418d20f2011-09-22 14:00:39 -0700239JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
240 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
241 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700242 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700243 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
244 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700245}
246
Elliott Hughes418d20f2011-09-22 14:00:39 -0700247JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
248 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
249 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700250 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700251 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
252 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700253}
254
Elliott Hughes6b436852011-08-12 10:16:44 -0700255// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
256// separated with slashes but aren't wrapped with "L;" like regular descriptors
257// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
258// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
259// supported names with dots too (such as "a.b.C").
260std::string NormalizeJniClassDescriptor(const char* name) {
261 std::string result;
262 // Add the missing "L;" if necessary.
263 if (name[0] == '[') {
264 result = name;
265 } else {
266 result += 'L';
267 result += name;
268 result += ';';
269 }
270 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700271 if (result.find('.') != std::string::npos) {
272 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
273 << "\"" << name << "\"";
274 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700275 }
276 return result;
277}
278
Elliott Hughes14134a12011-09-30 16:55:51 -0700279void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
280 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700281 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes14134a12011-09-30 16:55:51 -0700282 "no %s method \"%s.%s%s\"", kind, class_descriptor.c_str(), name, sig);
283}
284
Elliott Hughescdf53122011-08-19 15:46:09 -0700285jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
286 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700287 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700288 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700289 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700290
291 Method* method = NULL;
292 if (is_static) {
293 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700294 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700295 method = c->FindVirtualMethod(name, sig);
296 if (method == NULL) {
297 // No virtual method matching the signature. Search declared
298 // private methods and constructors.
299 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700300 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700301 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700302
Elliott Hughescdf53122011-08-19 15:46:09 -0700303 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700304 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700305 return NULL;
306 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700307
Elliott Hughes418d20f2011-09-22 14:00:39 -0700308 method->InitJavaFields();
309
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700310 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700311}
312
Brian Carlstrom00fae582011-10-28 01:16:28 -0700313const ClassLoader* GetClassLoader(Thread* self) {
314 Frame frame = self->GetTopOfStack();
315 Method* method = frame.GetMethod();
316 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
317 return self->GetClassLoaderOverride();
318 }
319 return method->GetDeclaringClass()->GetClassLoader();
320}
321
Elliott Hughescdf53122011-08-19 15:46:09 -0700322jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
323 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700324 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700325 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700326 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700327
328 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 Class* field_type;
330 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
331 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700332 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700333 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700334 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700336 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 if (field_type == NULL) {
338 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700339 DCHECK(ts.Self()->IsExceptionPending());
340 ts.Self()->ClearException();
341 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700342 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700343 "no type \"%s\" found and so no field \"%s\" could be found in class "
344 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700345 return NULL;
346 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700347 std::string field_type_descriptor = field_type->GetDescriptor()->ToModifiedUtf8();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 if (is_static) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700349 field = c->FindStaticField(name, field_type_descriptor);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700350 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700351 field = c->FindInstanceField(name, field_type_descriptor);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700352 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700353 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700354 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700355 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700356 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700357 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700358 return NULL;
359 }
Elliott Hughes80609252011-09-23 17:24:51 -0700360 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700361 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700362}
363
Elliott Hughes75770752011-08-24 17:52:38 -0700364void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
365 JavaVMExt* vm = ts.Vm();
366 MutexLock mu(vm->pins_lock);
367 vm->pin_table.Add(array);
368}
369
370void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
371 JavaVMExt* vm = ts.Vm();
372 MutexLock mu(vm->pins_lock);
373 vm->pin_table.Remove(array);
374}
375
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700376template<typename JniT, typename ArtT>
377JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
378 CHECK_GE(length, 0); // TODO: ReportJniError
379 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700380 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700381}
382
Elliott Hughes75770752011-08-24 17:52:38 -0700383template <typename ArrayT, typename CArrayT, typename ArtArrayT>
384CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
385 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
386 PinPrimitiveArray(ts, array);
387 if (is_copy != NULL) {
388 *is_copy = JNI_FALSE;
389 }
390 return array->GetData();
391}
392
393template <typename ArrayT>
394void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
395 if (mode != JNI_COMMIT) {
396 Array* array = Decode<Array*>(ts, java_array);
397 UnpinPrimitiveArray(ts, array);
398 }
399}
400
Elliott Hughes814e4032011-08-23 12:07:56 -0700401void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700402 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700403 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700404 "%s offset=%d length=%d %s.length=%d",
405 type.c_str(), start, length, identifier, array->GetLength());
406}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700407void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700408 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700409 "offset=%d length=%d string.length()=%d", start, length, array_length);
410}
Elliott Hughes814e4032011-08-23 12:07:56 -0700411
412template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700413void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700414 ArrayT* array = Decode<ArrayT*>(ts, java_array);
415 if (start < 0 || length < 0 || start + length > array->GetLength()) {
416 ThrowAIOOBE(ts, array, start, length, "src");
417 } else {
418 JavaT* data = array->GetData();
419 memcpy(buf, data + start, length * sizeof(JavaT));
420 }
421}
422
423template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700424void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700425 ArrayT* array = Decode<ArrayT*>(ts, java_array);
426 if (start < 0 || length < 0 || start + length > array->GetLength()) {
427 ThrowAIOOBE(ts, array, start, length, "dst");
428 } else {
429 JavaT* data = array->GetData();
430 memcpy(data + start, buf, length * sizeof(JavaT));
431 }
432}
433
Elliott Hughes75770752011-08-24 17:52:38 -0700434jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700435 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
436 CHECK(buffer_class.get() != NULL);
437 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
438}
439
Elliott Hughes75770752011-08-24 17:52:38 -0700440jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700441 static jclass buffer_class = InitDirectByteBufferClass(env);
442 return buffer_class;
443}
444
Elliott Hughes75770752011-08-24 17:52:38 -0700445jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
446 if (vm == NULL || p_env == NULL) {
447 return JNI_ERR;
448 }
449
Elliott Hughescac6cc72011-11-03 20:31:21 -0700450 // Return immediately if we're already one with the VM.
451 Thread* self = Thread::Current();
452 if (self != NULL) {
453 *p_env = self->GetJniEnv();
454 return JNI_OK;
455 }
456
457 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
458
459 // No threads allowed in zygote mode.
460 if (runtime->IsZygote()) {
461 LOG(ERROR) << "Attempt to attach a thread in the zygote";
462 return JNI_ERR;
463 }
464
Elliott Hughes75770752011-08-24 17:52:38 -0700465 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
466 JavaVMAttachArgs args;
467 if (thr_args == NULL) {
468 // Allow the v1.1 calling convention.
469 args.version = JNI_VERSION_1_2;
470 args.name = NULL;
471 args.group = NULL; // TODO: get "main" thread group
472 } else {
473 args.version = in_args->version;
474 args.name = in_args->name;
475 if (in_args->group != NULL) {
476 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
477 args.group = NULL; // TODO: decode in_args->group
478 } else {
479 args.group = NULL; // TODO: get "main" thread group
480 }
481 }
482 CHECK_GE(args.version, JNI_VERSION_1_2);
483
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700484 runtime->AttachCurrentThread(args.name, as_daemon);
485 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700486 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700487}
488
Elliott Hughes79082e32011-08-25 12:07:32 -0700489class SharedLibrary {
490 public:
491 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
492 : path_(path),
493 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700494 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700495 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700496 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700497 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700498 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700499 }
500
Elliott Hughes79082e32011-08-25 12:07:32 -0700501 Object* GetClassLoader() {
502 return class_loader_;
503 }
504
505 std::string GetPath() {
506 return path_;
507 }
508
509 /*
510 * Check the result of an earlier call to JNI_OnLoad on this library. If
511 * the call has not yet finished in another thread, wait for it.
512 */
513 bool CheckOnLoadResult(JavaVMExt* vm) {
514 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700515 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700516 // Check this so we don't end up waiting for ourselves. We need
517 // to return "true" so the caller can continue.
518 LOG(INFO) << *self << " recursive attempt to load library "
519 << "\"" << path_ << "\"";
520 return true;
521 }
522
523 MutexLock mu(jni_on_load_lock_);
524 while (jni_on_load_result_ == kPending) {
525 if (vm->verbose_jni) {
526 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
527 << "JNI_OnLoad...]";
528 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700529 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700530 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700531 }
532
533 bool okay = (jni_on_load_result_ == kOkay);
534 if (vm->verbose_jni) {
535 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
536 << (okay ? "succeeded" : "failed") << "]";
537 }
538 return okay;
539 }
540
541 void SetResult(bool result) {
542 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700543 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700544
545 // Broadcast a wakeup to anybody sleeping on the condition variable.
546 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700547 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700548 }
549
550 void* FindSymbol(const std::string& symbol_name) {
551 return dlsym(handle_, symbol_name.c_str());
552 }
553
554 private:
555 enum JNI_OnLoadState {
556 kPending,
557 kFailed,
558 kOkay,
559 };
560
561 // Path to library "/system/lib/libjni.so".
562 std::string path_;
563
564 // The void* returned by dlopen(3).
565 void* handle_;
566
567 // The ClassLoader this library is associated with.
568 Object* class_loader_;
569
570 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700571 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700572 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700573 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700574 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700575 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700576 // Result of earlier JNI_OnLoad call.
577 JNI_OnLoadState jni_on_load_result_;
578};
579
Elliott Hughescdf53122011-08-19 15:46:09 -0700580} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700581
Elliott Hughes79082e32011-08-25 12:07:32 -0700582// This exists mainly to keep implementation details out of the header file.
583class Libraries {
584 public:
585 Libraries() {
586 }
587
588 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700589 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 }
591
592 SharedLibrary* Get(const std::string& path) {
593 return libraries_[path];
594 }
595
596 void Put(const std::string& path, SharedLibrary* library) {
597 libraries_[path] = library;
598 }
599
600 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700601 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700602 std::string jni_short_name(JniShortName(m));
603 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700604 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700605 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
606 SharedLibrary* library = it->second;
607 if (library->GetClassLoader() != declaring_class_loader) {
608 // We only search libraries loaded by the appropriate ClassLoader.
609 continue;
610 }
611 // Try the short name then the long name...
612 void* fn = library->FindSymbol(jni_short_name);
613 if (fn == NULL) {
614 fn = library->FindSymbol(jni_long_name);
615 }
616 if (fn != NULL) {
617 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700618 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700619 << " in \"" << library->GetPath() << "\"]";
620 }
621 return fn;
622 }
623 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700624 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700625 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700626 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700627 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700628 return NULL;
629 }
630
631 private:
632 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
633
634 std::map<std::string, SharedLibrary*> libraries_;
635};
636
Elliott Hughes418d20f2011-09-22 14:00:39 -0700637JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
638 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
639 Object* receiver = Decode<Object*>(env, obj);
640 Method* method = DecodeMethod(mid);
641 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
642 return InvokeWithArgArray(env, receiver, method, arg_array.get());
643}
644
Elliott Hughescdf53122011-08-19 15:46:09 -0700645class JNI {
646 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700647
Elliott Hughescdf53122011-08-19 15:46:09 -0700648 static jint GetVersion(JNIEnv* env) {
649 ScopedJniThreadState ts(env);
650 return JNI_VERSION_1_6;
651 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700652
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
654 ScopedJniThreadState ts(env);
655 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700656 return NULL;
657 }
658
Elliott Hughescdf53122011-08-19 15:46:09 -0700659 static jclass FindClass(JNIEnv* env, const char* name) {
660 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700661 Runtime* runtime = Runtime::Current();
662 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700664 Class* c = NULL;
665 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700666 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700667 c = class_linker->FindClass(descriptor, cl);
668 } else {
669 c = class_linker->FindSystemClass(descriptor);
670 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700671 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700672 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700673
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
675 ScopedJniThreadState ts(env);
676 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700677 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700678 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700679
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
681 ScopedJniThreadState ts(env);
682 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700683 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700685
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
687 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700688 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700689 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700691
Elliott Hughescdf53122011-08-19 15:46:09 -0700692 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
693 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700694 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700695 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700697
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
699 ScopedJniThreadState ts(env);
700 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700701 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700702 }
703
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700704 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700706 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700707 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700709
Elliott Hughes37f7a402011-08-22 18:56:01 -0700710 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700712 Class* c1 = Decode<Class*>(ts, java_class1);
713 Class* c2 = Decode<Class*>(ts, java_class2);
714 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700715 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700716
Elliott Hughes37f7a402011-08-22 18:56:01 -0700717 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700719 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700720 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700721 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700722 return JNI_TRUE;
723 } else {
724 Object* obj = Decode<Object*>(ts, jobj);
725 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700726 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700727 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700729
Elliott Hughes37f7a402011-08-22 18:56:01 -0700730 static jint Throw(JNIEnv* env, jthrowable java_exception) {
731 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700732 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700733 if (exception == NULL) {
734 return JNI_ERR;
735 }
736 ts.Self()->SetException(exception);
737 return JNI_OK;
738 }
739
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700740 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700741 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700742 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700743 jmethodID mid = ((msg != NULL)
744 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
745 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700746 if (mid == NULL) {
747 return JNI_ERR;
748 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700749 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700750 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700751 return JNI_ERR;
752 }
753
754 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700755 args[0].l = s.get();
756 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
757 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700758 return JNI_ERR;
759 }
760
Elliott Hughes72025e52011-08-23 17:50:30 -0700761 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700762
Elliott Hughes37f7a402011-08-22 18:56:01 -0700763 return JNI_OK;
764 }
765
766 static jboolean ExceptionCheck(JNIEnv* env) {
767 ScopedJniThreadState ts(env);
768 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
769 }
770
771 static void ExceptionClear(JNIEnv* env) {
772 ScopedJniThreadState ts(env);
773 ts.Self()->ClearException();
774 }
775
776 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700778
779 Thread* self = ts.Self();
780 Throwable* original_exception = self->GetException();
781 self->ClearException();
782
Elliott Hughesbf86d042011-08-31 17:53:14 -0700783 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700784 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
785 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
786 if (mid == NULL) {
787 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700788 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700789 } else {
790 env->CallVoidMethod(exception.get(), mid);
791 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700792 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700793 << " thrown while calling printStackTrace";
794 self->ClearException();
795 }
796 }
797
798 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700799 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700800
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 static jthrowable ExceptionOccurred(JNIEnv* env) {
802 ScopedJniThreadState ts(env);
803 Object* exception = ts.Self()->GetException();
804 if (exception == NULL) {
805 return NULL;
806 } else {
807 // TODO: if adding a local reference failing causes the VM to abort
808 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700809 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 if (localException == NULL) {
811 // We were unable to add a new local reference, and threw a new
812 // exception. We can't return "exception", because it's not a
813 // local reference. So we have to return NULL, indicating that
814 // there was no exception, even though it's pretty much raining
815 // exceptions in here.
816 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
817 }
818 return localException;
819 }
820 }
821
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 static void FatalError(JNIEnv* env, const char* msg) {
823 ScopedJniThreadState ts(env);
824 LOG(FATAL) << "JNI FatalError called: " << msg;
825 }
826
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700827 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700829 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
830 return JNI_ERR;
831 }
832 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700833 return JNI_OK;
834 }
835
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700836 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700838 Object* survivor = Decode<Object*>(ts, java_survivor);
839 ts.Env()->PopFrame();
840 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 }
842
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700843 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700844 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700845 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
846 }
847
848 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
849 // TODO: we should try to expand the table if necessary.
850 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
851 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
852 return JNI_ERR;
853 }
854 // TODO: this isn't quite right, since "capacity" includes holes.
855 size_t capacity = ts.Env()->locals.Capacity();
856 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
857 if (!okay) {
858 ts.Self()->ThrowOutOfMemoryError(caller);
859 }
860 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700861 }
862
Elliott Hughescdf53122011-08-19 15:46:09 -0700863 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
864 ScopedJniThreadState ts(env);
865 if (obj == NULL) {
866 return NULL;
867 }
868
Elliott Hughes75770752011-08-24 17:52:38 -0700869 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 IndirectReferenceTable& globals = vm->globals;
871 MutexLock mu(vm->globals_lock);
872 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
873 return reinterpret_cast<jobject>(ref);
874 }
875
876 static void DeleteGlobalRef(JNIEnv* env, jobject 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& globals = vm->globals;
884 MutexLock mu(vm->globals_lock);
885
886 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
887 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
888 << "failed to find entry";
889 }
890 }
891
892 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
893 ScopedJniThreadState ts(env);
894 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
895 }
896
897 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
898 ScopedJniThreadState ts(env);
899 if (obj == NULL) {
900 return;
901 }
902
Elliott Hughes75770752011-08-24 17:52:38 -0700903 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 IndirectReferenceTable& weak_globals = vm->weak_globals;
905 MutexLock mu(vm->weak_globals_lock);
906
907 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
908 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
909 << "failed to find entry";
910 }
911 }
912
913 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
914 ScopedJniThreadState ts(env);
915 if (obj == NULL) {
916 return NULL;
917 }
918
919 IndirectReferenceTable& locals = ts.Env()->locals;
920
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700921 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
923 return reinterpret_cast<jobject>(ref);
924 }
925
926 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
927 ScopedJniThreadState ts(env);
928 if (obj == NULL) {
929 return;
930 }
931
932 IndirectReferenceTable& locals = ts.Env()->locals;
933
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700934 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 if (!locals.Remove(cookie, obj)) {
936 // Attempting to delete a local reference that is not in the
937 // topmost local reference frame is a no-op. DeleteLocalRef returns
938 // void and doesn't throw any exceptions, but we should probably
939 // complain about it so the user will notice that things aren't
940 // going quite the way they expect.
941 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
942 << "failed to find entry";
943 }
944 }
945
946 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
947 ScopedJniThreadState ts(env);
948 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
949 ? JNI_TRUE : JNI_FALSE;
950 }
951
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700952 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700954 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700955 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700956 return NULL;
957 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700958 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700959 }
960
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 ScopedJniThreadState ts(env);
963 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 va_start(args, mid);
965 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 va_end(args);
967 return result;
968 }
969
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700972 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700973 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700974 return NULL;
975 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700976 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700977 if (result == NULL) {
978 return NULL;
979 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700980 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700982 if (!ts.Self()->IsExceptionPending()) {
983 return local_result;
984 } else {
985 return NULL;
986 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700991 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700992 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700993 return NULL;
994 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700995 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700996 if (result == NULL) {
997 return NULL;
998 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700999 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001001 if (!ts.Self()->IsExceptionPending()) {
1002 return local_result;
1003 } else {
1004 return NULL;
1005 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1009 ScopedJniThreadState ts(env);
1010 return FindMethodID(ts, c, name, sig, false);
1011 }
1012
1013 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1014 ScopedJniThreadState ts(env);
1015 return FindMethodID(ts, c, name, sig, true);
1016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 va_list ap;
1021 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001022 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001024 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001029 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001030 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001035 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001036 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 va_list ap;
1042 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001043 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 va_end(ap);
1045 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001050 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001055 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_list ap;
1061 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001062 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_end(ap);
1064 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001069 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001074 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 va_list ap;
1080 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001081 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 va_end(ap);
1083 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001088 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001093 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 va_list ap;
1099 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001100 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_end(ap);
1102 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001107 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001112 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_list ap;
1118 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001119 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 va_end(ap);
1121 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001126 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001131 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 va_list ap;
1137 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001138 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 va_end(ap);
1140 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 }
1142
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001145 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 }
1147
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001150 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 va_list ap;
1156 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001157 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 va_end(ap);
1159 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 }
1161
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001164 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001169 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 }
1171
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 va_list ap;
1175 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001176 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 va_end(ap);
1178 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 }
1180
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001183 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001188 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 va_list ap;
1194 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001195 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001201 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001206 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 }
1208
1209 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 ScopedJniThreadState ts(env);
1212 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001214 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001215 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 va_end(ap);
1217 return local_result;
1218 }
1219
1220 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001223 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001224 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
1226
1227 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001230 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001231 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
1234 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 ScopedJniThreadState ts(env);
1237 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001239 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 va_end(ap);
1241 return result.z;
1242 }
1243
1244 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001247 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
1250 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001253 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 }
1255
1256 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 ScopedJniThreadState ts(env);
1259 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001261 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 va_end(ap);
1263 return result.b;
1264 }
1265
1266 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001269 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
1272 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001275 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
1278 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
1281 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001283 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 va_end(ap);
1285 return result.c;
1286 }
1287
1288 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001289 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001291 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
1294 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001295 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001297 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
1300 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
1303 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001304 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001305 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 va_end(ap);
1307 return result.s;
1308 }
1309
1310 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001313 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
1316 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001317 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001319 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
Elliott Hughes418d20f2011-09-22 14:00:39 -07001322 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 ScopedJniThreadState ts(env);
1324 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001326 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 va_end(ap);
1328 return result.i;
1329 }
1330
1331 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001334 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
1337 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001340 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
1343 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 ScopedJniThreadState ts(env);
1346 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001348 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 va_end(ap);
1350 return result.j;
1351 }
1352
1353 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001356 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
1359 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001360 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001362 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
1365 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001366 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 ScopedJniThreadState ts(env);
1368 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001369 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001370 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 va_end(ap);
1372 return result.f;
1373 }
1374
1375 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001376 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001378 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
1380
1381 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001382 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001384 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
1386
1387 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001388 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 ScopedJniThreadState ts(env);
1390 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001391 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001392 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 va_end(ap);
1394 return result.d;
1395 }
1396
1397 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001398 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001400 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 }
1402
1403 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001404 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001406 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 }
1408
1409 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001410 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 ScopedJniThreadState ts(env);
1412 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001413 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001414 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 va_end(ap);
1416 }
1417
1418 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001419 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001421 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
1424 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001425 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001427 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001430 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 ScopedJniThreadState ts(env);
1432 return FindFieldID(ts, c, name, sig, false);
1433 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001434
1435
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001436 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 ScopedJniThreadState ts(env);
1438 return FindFieldID(ts, c, name, sig, true);
1439 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001440
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001441 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001444 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001445 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001447
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001450 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001451 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 Object* o = Decode<Object*>(ts, java_object);
1457 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001458 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001465 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001466 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 }
1468
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001469#define GET_PRIMITIVE_FIELD(fn, instance) \
1470 ScopedJniThreadState ts(env); \
1471 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001472 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001473 return f->fn(o)
1474
1475#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1476 ScopedJniThreadState ts(env); \
1477 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001478 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 f->fn(o, value)
1480
1481 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1482 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 }
1484
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001485 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1486 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 }
1488
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001489 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1490 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 }
1492
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001493 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1494 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 }
1496
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001497 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1498 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 }
1500
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001501 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1502 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 }
1504
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001505 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1506 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 }
1508
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001509 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1510 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001511 }
1512
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001513 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1514 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 }
1516
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001517 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1518 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 }
1520
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001521 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1522 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 }
1524
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001525 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1526 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 }
1528
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001529 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1530 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001531 }
1532
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001533 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1534 GET_PRIMITIVE_FIELD(GetLong, NULL);
1535 }
1536
1537 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1538 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1539 }
1540
1541 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1542 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1543 }
1544
1545 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1546 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1547 }
1548
1549 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1550 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1551 }
1552
1553 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1554 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1555 }
1556
1557 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1558 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1559 }
1560
1561 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1562 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1563 }
1564
1565 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1566 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1567 }
1568
1569 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1570 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1571 }
1572
1573 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1574 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1575 }
1576
1577 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1578 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1579 }
1580
1581 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1582 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1583 }
1584
1585 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1586 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1587 }
1588
1589 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1590 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1591 }
1592
1593 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1594 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1595 }
1596
1597 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1598 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1599 }
1600
1601 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1602 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1603 }
1604
1605 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1606 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes418d20f2011-09-22 14:00:39 -07001609 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 ScopedJniThreadState ts(env);
1611 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001612 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001613 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001614 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 va_end(ap);
1616 return local_result;
1617 }
1618
Elliott Hughes418d20f2011-09-22 14:00:39 -07001619 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001621 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001622 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
Elliott Hughes418d20f2011-09-22 14:00:39 -07001625 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001627 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001628 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes418d20f2011-09-22 14:00:39 -07001631 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 ScopedJniThreadState ts(env);
1633 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001635 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 va_end(ap);
1637 return result.z;
1638 }
1639
Elliott Hughes418d20f2011-09-22 14:00:39 -07001640 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001642 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes418d20f2011-09-22 14:00:39 -07001645 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001647 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes72025e52011-08-23 17:50:30 -07001650 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 ScopedJniThreadState ts(env);
1652 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001654 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 va_end(ap);
1656 return result.b;
1657 }
1658
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001661 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
Elliott Hughes418d20f2011-09-22 14:00:39 -07001664 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001666 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes72025e52011-08-23 17:50:30 -07001669 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 ScopedJniThreadState ts(env);
1671 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001673 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 va_end(ap);
1675 return result.c;
1676 }
1677
Elliott Hughes418d20f2011-09-22 14:00:39 -07001678 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001680 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
Elliott Hughes418d20f2011-09-22 14:00:39 -07001683 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001685 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
Elliott Hughes72025e52011-08-23 17:50:30 -07001688 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 ScopedJniThreadState ts(env);
1690 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001692 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 va_end(ap);
1694 return result.s;
1695 }
1696
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001699 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 }
1701
Elliott Hughes418d20f2011-09-22 14:00:39 -07001702 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001704 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 ScopedJniThreadState ts(env);
1709 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001711 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 va_end(ap);
1713 return result.i;
1714 }
1715
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001718 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes418d20f2011-09-22 14:00:39 -07001721 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001723 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes72025e52011-08-23 17:50:30 -07001726 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 ScopedJniThreadState ts(env);
1728 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001729 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001730 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 va_end(ap);
1732 return result.j;
1733 }
1734
Elliott Hughes418d20f2011-09-22 14:00:39 -07001735 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001737 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes418d20f2011-09-22 14:00:39 -07001740 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001742 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes72025e52011-08-23 17:50:30 -07001745 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 ScopedJniThreadState ts(env);
1747 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001748 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001749 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 va_end(ap);
1751 return result.f;
1752 }
1753
Elliott Hughes418d20f2011-09-22 14:00:39 -07001754 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001756 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 }
1758
Elliott Hughes418d20f2011-09-22 14:00:39 -07001759 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001761 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 }
1763
Elliott Hughes72025e52011-08-23 17:50:30 -07001764 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 ScopedJniThreadState ts(env);
1766 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001767 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001768 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 va_end(ap);
1770 return result.d;
1771 }
1772
Elliott Hughes418d20f2011-09-22 14:00:39 -07001773 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001775 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 }
1777
Elliott Hughes418d20f2011-09-22 14:00:39 -07001778 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001780 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes72025e52011-08-23 17:50:30 -07001783 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 ScopedJniThreadState ts(env);
1785 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001786 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001787 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 va_end(ap);
1789 }
1790
Elliott Hughes418d20f2011-09-22 14:00:39 -07001791 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001793 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes418d20f2011-09-22 14:00:39 -07001796 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001798 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
Elliott Hughes814e4032011-08-23 12:07:56 -07001801 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001803 if (chars == NULL && char_count == 0) {
1804 return NULL;
1805 }
1806 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001807 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
1810 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1811 ScopedJniThreadState ts(env);
1812 if (utf == NULL) {
1813 return NULL;
1814 }
1815 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001816 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 }
1818
Elliott Hughes814e4032011-08-23 12:07:56 -07001819 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1820 ScopedJniThreadState ts(env);
1821 return Decode<String*>(ts, java_string)->GetLength();
1822 }
1823
1824 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1825 ScopedJniThreadState ts(env);
1826 return Decode<String*>(ts, java_string)->GetUtfLength();
1827 }
1828
Elliott Hughesb465ab02011-08-24 11:21:21 -07001829 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001831 String* s = Decode<String*>(ts, java_string);
1832 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1833 ThrowSIOOBE(ts, start, length, s->GetLength());
1834 } else {
1835 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1836 memcpy(buf, chars + start, length * sizeof(jchar));
1837 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001838 }
1839
Elliott Hughesb465ab02011-08-24 11:21:21 -07001840 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001841 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001842 String* s = Decode<String*>(ts, java_string);
1843 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1844 ThrowSIOOBE(ts, start, length, s->GetLength());
1845 } else {
1846 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1847 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1848 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001849 }
1850
Elliott Hughes75770752011-08-24 17:52:38 -07001851 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001852 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001853 String* s = Decode<String*>(ts, java_string);
1854 const CharArray* chars = s->GetCharArray();
1855 PinPrimitiveArray(ts, chars);
1856 if (is_copy != NULL) {
1857 *is_copy = JNI_FALSE;
1858 }
1859 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001860 }
1861
Elliott Hughes75770752011-08-24 17:52:38 -07001862 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001863 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001864 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
Elliott Hughes75770752011-08-24 17:52:38 -07001867 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001869 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 }
1871
Elliott Hughes75770752011-08-24 17:52:38 -07001872 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001874 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 }
1876
Elliott Hughes75770752011-08-24 17:52:38 -07001877 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001878 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001879 if (java_string == NULL) {
1880 return NULL;
1881 }
1882 if (is_copy != NULL) {
1883 *is_copy = JNI_TRUE;
1884 }
1885 String* s = Decode<String*>(ts, java_string);
1886 size_t byte_count = s->GetUtfLength();
1887 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001888 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001889 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1890 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1891 bytes[byte_count] = '\0';
1892 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001893 }
1894
Elliott Hughes75770752011-08-24 17:52:38 -07001895 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001896 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001897 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001898 }
1899
Elliott Hughesbd935992011-08-22 11:59:34 -07001900 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001902 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001903 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001904 Array* array = obj->AsArray();
1905 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 }
1907
Elliott Hughes814e4032011-08-23 12:07:56 -07001908 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001909 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001910 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001911 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001912 }
1913
1914 static void SetObjectArrayElement(JNIEnv* env,
1915 jobjectArray java_array, jsize index, jobject java_value) {
1916 ScopedJniThreadState ts(env);
1917 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1918 Object* value = Decode<Object*>(ts, java_value);
1919 array->Set(index, value);
1920 }
1921
1922 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1923 ScopedJniThreadState ts(env);
1924 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1925 }
1926
1927 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1928 ScopedJniThreadState ts(env);
1929 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1930 }
1931
1932 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1933 ScopedJniThreadState ts(env);
1934 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1935 }
1936
1937 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1938 ScopedJniThreadState ts(env);
1939 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1940 }
1941
1942 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1943 ScopedJniThreadState ts(env);
1944 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1945 }
1946
1947 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1948 ScopedJniThreadState ts(env);
1949 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1950 }
1951
1952 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1953 ScopedJniThreadState ts(env);
1954 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1955 }
1956
1957 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1958 ScopedJniThreadState ts(env);
1959 CHECK_GE(length, 0); // TODO: ReportJniError
1960
1961 // Compute the array class corresponding to the given element class.
1962 Class* element_class = Decode<Class*>(ts, element_jclass);
1963 std::string descriptor;
1964 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001965 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001966
1967 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001968 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1969 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 return NULL;
1971 }
1972
Elliott Hughes75770752011-08-24 17:52:38 -07001973 // Allocate and initialize if necessary.
1974 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001976 if (initial_element != NULL) {
1977 Object* initial_object = Decode<Object*>(ts, initial_element);
1978 for (jsize i = 0; i < length; ++i) {
1979 result->Set(i, initial_object);
1980 }
1981 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001982 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 }
1984
1985 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1986 ScopedJniThreadState ts(env);
1987 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1988 }
1989
Elliott Hughes75770752011-08-24 17:52:38 -07001990 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001991 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001992 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001993 }
1994
Elliott Hughes75770752011-08-24 17:52:38 -07001995 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001996 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001997 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001998 }
1999
Elliott Hughes75770752011-08-24 17:52:38 -07002000 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002002 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
Elliott Hughes75770752011-08-24 17:52:38 -07002005 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002007 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Elliott Hughes75770752011-08-24 17:52:38 -07002010 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002012 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Elliott Hughes75770752011-08-24 17:52:38 -07002015 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002017 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Elliott Hughes75770752011-08-24 17:52:38 -07002020 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002022 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes75770752011-08-24 17:52:38 -07002025 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002027 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes75770752011-08-24 17:52:38 -07002030 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002032 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
2034
Elliott Hughes75770752011-08-24 17:52:38 -07002035 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002037 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes75770752011-08-24 17:52:38 -07002040 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002042 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes75770752011-08-24 17:52:38 -07002045 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002047 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Elliott Hughes75770752011-08-24 17:52:38 -07002050 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002052 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes75770752011-08-24 17:52:38 -07002055 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002057 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
Elliott Hughes75770752011-08-24 17:52:38 -07002060 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002062 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Elliott Hughes75770752011-08-24 17:52:38 -07002065 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002067 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Elliott Hughes75770752011-08-24 17:52:38 -07002070 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002072 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Elliott Hughes75770752011-08-24 17:52:38 -07002075 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002077 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002082 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002087 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002092 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002097 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002102 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002107 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002112 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002117 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002122 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 }
2124
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002127 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
2129
Elliott Hughes814e4032011-08-23 12:07:56 -07002130 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002132 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 }
2134
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002137 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
2139
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002142 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002147 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 }
2149
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002152 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Elliott Hughes814e4032011-08-23 12:07:56 -07002155 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002157 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 }
2159
Elliott Hughes5174fe62011-08-23 15:12:35 -07002160 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162 Class* c = Decode<Class*>(ts, java_class);
2163
Elliott Hughes5174fe62011-08-23 15:12:35 -07002164 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 const char* name = methods[i].name;
2166 const char* sig = methods[i].signature;
2167
2168 if (*sig == '!') {
2169 // TODO: fast jni. it's too noisy to log all these.
2170 ++sig;
2171 }
2172
Elliott Hughes5174fe62011-08-23 15:12:35 -07002173 Method* m = c->FindDirectMethod(name, sig);
2174 if (m == NULL) {
2175 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002177 if (m == NULL) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002178 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002180 } else if (!m->IsNative()) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002181 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 return JNI_ERR;
2183 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002184
Elliott Hughes75770752011-08-24 17:52:38 -07002185 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002186 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002187 }
2188
2189 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191 return JNI_OK;
2192 }
2193
Elliott Hughes5174fe62011-08-23 15:12:35 -07002194 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002196 Class* c = Decode<Class*>(ts, java_class);
2197
Elliott Hughes75770752011-08-24 17:52:38 -07002198 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002199 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002200 }
2201
2202 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2203 Method* m = c->GetDirectMethod(i);
2204 if (m->IsNative()) {
2205 m->UnregisterNative();
2206 }
2207 }
2208 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2209 Method* m = c->GetVirtualMethod(i);
2210 if (m->IsNative()) {
2211 m->UnregisterNative();
2212 }
2213 }
2214
2215 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 }
2217
Elliott Hughes72025e52011-08-23 17:50:30 -07002218 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002220 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002221 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002222 }
2223
Elliott Hughes72025e52011-08-23 17:50:30 -07002224 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002225 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002226 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002227 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
2230 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2231 ScopedJniThreadState ts(env);
2232 Runtime* runtime = Runtime::Current();
2233 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002234 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 } else {
2236 *vm = NULL;
2237 }
2238 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2239 }
2240
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2242 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002243
2244 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002245 CHECK(address != NULL); // TODO: ReportJniError
2246 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247
2248 jclass buffer_class = GetDirectByteBufferClass(env);
2249 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2250 if (mid == NULL) {
2251 return NULL;
2252 }
2253
2254 // At the moment, the Java side is limited to 32 bits.
2255 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2256 CHECK_LE(capacity, 0xffffffff);
2257 jint address_arg = reinterpret_cast<jint>(address);
2258 jint capacity_arg = static_cast<jint>(capacity);
2259
2260 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2261 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 }
2263
Elliott Hughesb465ab02011-08-24 11:21:21 -07002264 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002265 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002266 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2267 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 }
2269
Elliott Hughesb465ab02011-08-24 11:21:21 -07002270 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002272 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2273 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002274 }
2275
Elliott Hughesb465ab02011-08-24 11:21:21 -07002276 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002278
Elliott Hughes75770752011-08-24 17:52:38 -07002279 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002280
2281 // Do we definitely know what kind of reference this is?
2282 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2283 IndirectRefKind kind = GetIndirectRefKind(ref);
2284 switch (kind) {
2285 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002286 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2287 return JNILocalRefType;
2288 }
2289 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002290 case kGlobal:
2291 return JNIGlobalRefType;
2292 case kWeakGlobal:
2293 return JNIWeakGlobalRefType;
2294 case kSirtOrInvalid:
2295 // Is it in a stack IRT?
2296 if (ts.Self()->SirtContains(java_object)) {
2297 return JNILocalRefType;
2298 }
2299
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002300 if (!ts.Env()->work_around_app_jni_bugs) {
2301 return JNIInvalidRefType;
2302 }
2303
Elliott Hughesb465ab02011-08-24 11:21:21 -07002304 // If we're handing out direct pointers, check whether it's a direct pointer
2305 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002306 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002307 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002308 return JNILocalRefType;
2309 }
2310 }
2311
2312 return JNIInvalidRefType;
2313 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002314 }
2315};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002316
Elliott Hughesa2501992011-08-26 19:39:54 -07002317const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002318 NULL, // reserved0.
2319 NULL, // reserved1.
2320 NULL, // reserved2.
2321 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002322 JNI::GetVersion,
2323 JNI::DefineClass,
2324 JNI::FindClass,
2325 JNI::FromReflectedMethod,
2326 JNI::FromReflectedField,
2327 JNI::ToReflectedMethod,
2328 JNI::GetSuperclass,
2329 JNI::IsAssignableFrom,
2330 JNI::ToReflectedField,
2331 JNI::Throw,
2332 JNI::ThrowNew,
2333 JNI::ExceptionOccurred,
2334 JNI::ExceptionDescribe,
2335 JNI::ExceptionClear,
2336 JNI::FatalError,
2337 JNI::PushLocalFrame,
2338 JNI::PopLocalFrame,
2339 JNI::NewGlobalRef,
2340 JNI::DeleteGlobalRef,
2341 JNI::DeleteLocalRef,
2342 JNI::IsSameObject,
2343 JNI::NewLocalRef,
2344 JNI::EnsureLocalCapacity,
2345 JNI::AllocObject,
2346 JNI::NewObject,
2347 JNI::NewObjectV,
2348 JNI::NewObjectA,
2349 JNI::GetObjectClass,
2350 JNI::IsInstanceOf,
2351 JNI::GetMethodID,
2352 JNI::CallObjectMethod,
2353 JNI::CallObjectMethodV,
2354 JNI::CallObjectMethodA,
2355 JNI::CallBooleanMethod,
2356 JNI::CallBooleanMethodV,
2357 JNI::CallBooleanMethodA,
2358 JNI::CallByteMethod,
2359 JNI::CallByteMethodV,
2360 JNI::CallByteMethodA,
2361 JNI::CallCharMethod,
2362 JNI::CallCharMethodV,
2363 JNI::CallCharMethodA,
2364 JNI::CallShortMethod,
2365 JNI::CallShortMethodV,
2366 JNI::CallShortMethodA,
2367 JNI::CallIntMethod,
2368 JNI::CallIntMethodV,
2369 JNI::CallIntMethodA,
2370 JNI::CallLongMethod,
2371 JNI::CallLongMethodV,
2372 JNI::CallLongMethodA,
2373 JNI::CallFloatMethod,
2374 JNI::CallFloatMethodV,
2375 JNI::CallFloatMethodA,
2376 JNI::CallDoubleMethod,
2377 JNI::CallDoubleMethodV,
2378 JNI::CallDoubleMethodA,
2379 JNI::CallVoidMethod,
2380 JNI::CallVoidMethodV,
2381 JNI::CallVoidMethodA,
2382 JNI::CallNonvirtualObjectMethod,
2383 JNI::CallNonvirtualObjectMethodV,
2384 JNI::CallNonvirtualObjectMethodA,
2385 JNI::CallNonvirtualBooleanMethod,
2386 JNI::CallNonvirtualBooleanMethodV,
2387 JNI::CallNonvirtualBooleanMethodA,
2388 JNI::CallNonvirtualByteMethod,
2389 JNI::CallNonvirtualByteMethodV,
2390 JNI::CallNonvirtualByteMethodA,
2391 JNI::CallNonvirtualCharMethod,
2392 JNI::CallNonvirtualCharMethodV,
2393 JNI::CallNonvirtualCharMethodA,
2394 JNI::CallNonvirtualShortMethod,
2395 JNI::CallNonvirtualShortMethodV,
2396 JNI::CallNonvirtualShortMethodA,
2397 JNI::CallNonvirtualIntMethod,
2398 JNI::CallNonvirtualIntMethodV,
2399 JNI::CallNonvirtualIntMethodA,
2400 JNI::CallNonvirtualLongMethod,
2401 JNI::CallNonvirtualLongMethodV,
2402 JNI::CallNonvirtualLongMethodA,
2403 JNI::CallNonvirtualFloatMethod,
2404 JNI::CallNonvirtualFloatMethodV,
2405 JNI::CallNonvirtualFloatMethodA,
2406 JNI::CallNonvirtualDoubleMethod,
2407 JNI::CallNonvirtualDoubleMethodV,
2408 JNI::CallNonvirtualDoubleMethodA,
2409 JNI::CallNonvirtualVoidMethod,
2410 JNI::CallNonvirtualVoidMethodV,
2411 JNI::CallNonvirtualVoidMethodA,
2412 JNI::GetFieldID,
2413 JNI::GetObjectField,
2414 JNI::GetBooleanField,
2415 JNI::GetByteField,
2416 JNI::GetCharField,
2417 JNI::GetShortField,
2418 JNI::GetIntField,
2419 JNI::GetLongField,
2420 JNI::GetFloatField,
2421 JNI::GetDoubleField,
2422 JNI::SetObjectField,
2423 JNI::SetBooleanField,
2424 JNI::SetByteField,
2425 JNI::SetCharField,
2426 JNI::SetShortField,
2427 JNI::SetIntField,
2428 JNI::SetLongField,
2429 JNI::SetFloatField,
2430 JNI::SetDoubleField,
2431 JNI::GetStaticMethodID,
2432 JNI::CallStaticObjectMethod,
2433 JNI::CallStaticObjectMethodV,
2434 JNI::CallStaticObjectMethodA,
2435 JNI::CallStaticBooleanMethod,
2436 JNI::CallStaticBooleanMethodV,
2437 JNI::CallStaticBooleanMethodA,
2438 JNI::CallStaticByteMethod,
2439 JNI::CallStaticByteMethodV,
2440 JNI::CallStaticByteMethodA,
2441 JNI::CallStaticCharMethod,
2442 JNI::CallStaticCharMethodV,
2443 JNI::CallStaticCharMethodA,
2444 JNI::CallStaticShortMethod,
2445 JNI::CallStaticShortMethodV,
2446 JNI::CallStaticShortMethodA,
2447 JNI::CallStaticIntMethod,
2448 JNI::CallStaticIntMethodV,
2449 JNI::CallStaticIntMethodA,
2450 JNI::CallStaticLongMethod,
2451 JNI::CallStaticLongMethodV,
2452 JNI::CallStaticLongMethodA,
2453 JNI::CallStaticFloatMethod,
2454 JNI::CallStaticFloatMethodV,
2455 JNI::CallStaticFloatMethodA,
2456 JNI::CallStaticDoubleMethod,
2457 JNI::CallStaticDoubleMethodV,
2458 JNI::CallStaticDoubleMethodA,
2459 JNI::CallStaticVoidMethod,
2460 JNI::CallStaticVoidMethodV,
2461 JNI::CallStaticVoidMethodA,
2462 JNI::GetStaticFieldID,
2463 JNI::GetStaticObjectField,
2464 JNI::GetStaticBooleanField,
2465 JNI::GetStaticByteField,
2466 JNI::GetStaticCharField,
2467 JNI::GetStaticShortField,
2468 JNI::GetStaticIntField,
2469 JNI::GetStaticLongField,
2470 JNI::GetStaticFloatField,
2471 JNI::GetStaticDoubleField,
2472 JNI::SetStaticObjectField,
2473 JNI::SetStaticBooleanField,
2474 JNI::SetStaticByteField,
2475 JNI::SetStaticCharField,
2476 JNI::SetStaticShortField,
2477 JNI::SetStaticIntField,
2478 JNI::SetStaticLongField,
2479 JNI::SetStaticFloatField,
2480 JNI::SetStaticDoubleField,
2481 JNI::NewString,
2482 JNI::GetStringLength,
2483 JNI::GetStringChars,
2484 JNI::ReleaseStringChars,
2485 JNI::NewStringUTF,
2486 JNI::GetStringUTFLength,
2487 JNI::GetStringUTFChars,
2488 JNI::ReleaseStringUTFChars,
2489 JNI::GetArrayLength,
2490 JNI::NewObjectArray,
2491 JNI::GetObjectArrayElement,
2492 JNI::SetObjectArrayElement,
2493 JNI::NewBooleanArray,
2494 JNI::NewByteArray,
2495 JNI::NewCharArray,
2496 JNI::NewShortArray,
2497 JNI::NewIntArray,
2498 JNI::NewLongArray,
2499 JNI::NewFloatArray,
2500 JNI::NewDoubleArray,
2501 JNI::GetBooleanArrayElements,
2502 JNI::GetByteArrayElements,
2503 JNI::GetCharArrayElements,
2504 JNI::GetShortArrayElements,
2505 JNI::GetIntArrayElements,
2506 JNI::GetLongArrayElements,
2507 JNI::GetFloatArrayElements,
2508 JNI::GetDoubleArrayElements,
2509 JNI::ReleaseBooleanArrayElements,
2510 JNI::ReleaseByteArrayElements,
2511 JNI::ReleaseCharArrayElements,
2512 JNI::ReleaseShortArrayElements,
2513 JNI::ReleaseIntArrayElements,
2514 JNI::ReleaseLongArrayElements,
2515 JNI::ReleaseFloatArrayElements,
2516 JNI::ReleaseDoubleArrayElements,
2517 JNI::GetBooleanArrayRegion,
2518 JNI::GetByteArrayRegion,
2519 JNI::GetCharArrayRegion,
2520 JNI::GetShortArrayRegion,
2521 JNI::GetIntArrayRegion,
2522 JNI::GetLongArrayRegion,
2523 JNI::GetFloatArrayRegion,
2524 JNI::GetDoubleArrayRegion,
2525 JNI::SetBooleanArrayRegion,
2526 JNI::SetByteArrayRegion,
2527 JNI::SetCharArrayRegion,
2528 JNI::SetShortArrayRegion,
2529 JNI::SetIntArrayRegion,
2530 JNI::SetLongArrayRegion,
2531 JNI::SetFloatArrayRegion,
2532 JNI::SetDoubleArrayRegion,
2533 JNI::RegisterNatives,
2534 JNI::UnregisterNatives,
2535 JNI::MonitorEnter,
2536 JNI::MonitorExit,
2537 JNI::GetJavaVM,
2538 JNI::GetStringRegion,
2539 JNI::GetStringUTFRegion,
2540 JNI::GetPrimitiveArrayCritical,
2541 JNI::ReleasePrimitiveArrayCritical,
2542 JNI::GetStringCritical,
2543 JNI::ReleaseStringCritical,
2544 JNI::NewWeakGlobalRef,
2545 JNI::DeleteWeakGlobalRef,
2546 JNI::ExceptionCheck,
2547 JNI::NewDirectByteBuffer,
2548 JNI::GetDirectBufferAddress,
2549 JNI::GetDirectBufferCapacity,
2550 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002551};
2552
Elliott Hughes75770752011-08-24 17:52:38 -07002553JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002554 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002555 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002556 local_ref_cookie(IRT_FIRST_SEGMENT),
2557 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002558 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002559 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002560 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002561 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002562 functions = unchecked_functions = &gNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002563 if (vm->check_jni) {
2564 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002565 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002566 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2567 // errors will ensue.
2568 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2569 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002570}
2571
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002572JNIEnvExt::~JNIEnvExt() {
2573}
2574
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002575void JNIEnvExt::EnableCheckJni() {
2576 check_jni = true;
2577 functions = GetCheckJniNativeInterface();
2578}
2579
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002580void JNIEnvExt::DumpReferenceTables() {
2581 locals.Dump();
2582 monitors.Dump();
2583}
2584
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002585void JNIEnvExt::PushFrame(int capacity) {
2586 stacked_local_ref_cookies.push_back(local_ref_cookie);
2587 local_ref_cookie = locals.GetSegmentState();
2588}
2589
2590void JNIEnvExt::PopFrame() {
2591 locals.SetSegmentState(local_ref_cookie);
2592 local_ref_cookie = stacked_local_ref_cookies.back();
2593 stacked_local_ref_cookies.pop_back();
2594}
2595
Carl Shapiroea4dca82011-08-01 13:45:38 -07002596// JNI Invocation interface.
2597
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002598extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2599 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2600 if (args->version < JNI_VERSION_1_2) {
2601 return JNI_EVERSION;
2602 }
2603 Runtime::Options options;
2604 for (int i = 0; i < args->nOptions; ++i) {
2605 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002606 options.push_back(std::make_pair(StringPiece(option->optionString),
2607 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002608 }
2609 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002610 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002611 if (runtime == NULL) {
2612 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002613 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002614 runtime->Start();
2615 *p_env = Thread::Current()->GetJniEnv();
2616 *p_vm = runtime->GetJavaVM();
2617 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002618}
2619
Elliott Hughesf2682d52011-08-15 16:37:04 -07002620extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002621 Runtime* runtime = Runtime::Current();
2622 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002623 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002624 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002625 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002626 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002627 }
2628 return JNI_OK;
2629}
2630
2631// Historically unsupported.
2632extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2633 return JNI_ERR;
2634}
2635
Elliott Hughescdf53122011-08-19 15:46:09 -07002636class JII {
2637 public:
2638 static jint DestroyJavaVM(JavaVM* vm) {
2639 if (vm == NULL) {
2640 return JNI_ERR;
2641 } else {
2642 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2643 delete raw_vm->runtime;
2644 return JNI_OK;
2645 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002646 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002647
Elliott Hughescdf53122011-08-19 15:46:09 -07002648 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002649 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002650 }
2651
2652 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002653 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002654 }
2655
2656 static jint DetachCurrentThread(JavaVM* vm) {
2657 if (vm == NULL) {
2658 return JNI_ERR;
2659 } else {
2660 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2661 Runtime* runtime = raw_vm->runtime;
2662 runtime->DetachCurrentThread();
2663 return JNI_OK;
2664 }
2665 }
2666
2667 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2668 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2669 return JNI_EVERSION;
2670 }
2671 if (vm == NULL || env == NULL) {
2672 return JNI_ERR;
2673 }
2674 Thread* thread = Thread::Current();
2675 if (thread == NULL) {
2676 *env = NULL;
2677 return JNI_EDETACHED;
2678 }
2679 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002680 return JNI_OK;
2681 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002682};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002683
Elliott Hughesa2501992011-08-26 19:39:54 -07002684const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002685 NULL, // reserved0
2686 NULL, // reserved1
2687 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002688 JII::DestroyJavaVM,
2689 JII::AttachCurrentThread,
2690 JII::DetachCurrentThread,
2691 JII::GetEnv,
2692 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002693};
2694
Elliott Hughesa0957642011-09-02 14:27:33 -07002695JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002696 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002697 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002698 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002699 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002700 verbose_jni(options->IsVerbose("jni")),
2701 log_third_party_jni(options->IsVerbose("third-party-jni")),
2702 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002703 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002704 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002705 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002706 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002707 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002708 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002709 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002710 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002711 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002712 functions = unchecked_functions = &gInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002713 if (options->check_jni_) {
2714 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002715 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002716}
2717
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002718JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002719 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002720}
2721
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002722void JavaVMExt::EnableCheckJni() {
2723 check_jni = true;
2724 functions = GetCheckJniInvokeInterface();
2725}
2726
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002727void JavaVMExt::DumpReferenceTables() {
2728 {
2729 MutexLock mu(globals_lock);
2730 globals.Dump();
2731 }
2732 {
2733 MutexLock mu(weak_globals_lock);
2734 weak_globals.Dump();
2735 }
2736 {
2737 MutexLock mu(pins_lock);
2738 pin_table.Dump();
2739 }
2740}
2741
Elliott Hughes75770752011-08-24 17:52:38 -07002742bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2743 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002744
2745 // See if we've already loaded this library. If we have, and the class loader
2746 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002747 // TODO: for better results we should canonicalize the pathname (or even compare
2748 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002749 SharedLibrary* library;
2750 {
2751 // TODO: move the locking (and more of this logic) into Libraries.
2752 MutexLock mu(libraries_lock);
2753 library = libraries->Get(path);
2754 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002755 if (library != NULL) {
2756 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002757 // The library will be associated with class_loader. The JNI
2758 // spec says we can't load the same library into more than one
2759 // class loader.
2760 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2761 "ClassLoader %p; can't open in ClassLoader %p",
2762 path.c_str(), library->GetClassLoader(), class_loader);
2763 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002764 return false;
2765 }
2766 if (verbose_jni) {
2767 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2768 << "ClassLoader " << class_loader << "]";
2769 }
2770 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002771 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2772 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002773 return false;
2774 }
2775 return true;
2776 }
2777
2778 // Open the shared library. Because we're using a full path, the system
2779 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2780 // resolve this library's dependencies though.)
2781
2782 // Failures here are expected when java.library.path has several entries
2783 // and we have to hunt for the lib.
2784
2785 // The current version of the dynamic linker prints detailed information
2786 // about dlopen() failures. Some things to check if the message is
2787 // cryptic:
2788 // - make sure the library exists on the device
2789 // - verify that the right path is being opened (the debug log message
2790 // above can help with that)
2791 // - check to see if the library is valid (e.g. not zero bytes long)
2792 // - check config/prelink-linux-arm.map to ensure that the library
2793 // is listed and is not being overrun by the previous entry (if
2794 // loading suddenly stops working on a prelinked library, this is
2795 // a good one to check)
2796 // - write a trivial app that calls sleep() then dlopen(), attach
2797 // to it with "strace -p <pid>" while it sleeps, and watch for
2798 // attempts to open nonexistent dependent shared libs
2799
2800 // TODO: automate some of these checks!
2801
2802 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002803 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002804 // the GC to ignore us.
2805 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002806 void* handle = NULL;
2807 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002808 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002809 handle = dlopen(path.c_str(), RTLD_LAZY);
2810 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002811
2812 if (verbose_jni) {
2813 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2814 }
2815
2816 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002817 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002818 return false;
2819 }
2820
2821 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002822 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002823 // TODO: move the locking (and more of this logic) into Libraries.
2824 MutexLock mu(libraries_lock);
2825 library = libraries->Get(path);
2826 if (library != NULL) {
2827 LOG(INFO) << "WOW: we lost a race to add shared library: "
2828 << "\"" << path << "\" ClassLoader=" << class_loader;
2829 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002830 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002831 library = new SharedLibrary(path, handle, class_loader);
2832 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002833 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002834
2835 if (verbose_jni) {
2836 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2837 }
2838
2839 bool result = true;
2840 void* sym = dlsym(handle, "JNI_OnLoad");
2841 if (sym == NULL) {
2842 if (verbose_jni) {
2843 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2844 }
2845 } else {
2846 // Call JNI_OnLoad. We have to override the current class
2847 // loader, which will always be "null" since the stuff at the
2848 // top of the stack is around Runtime.loadLibrary(). (See
2849 // the comments in the JNI FindClass function.)
2850 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2851 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002852 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002853 self->SetClassLoaderOverride(class_loader);
2854
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002855 int version = 0;
2856 {
2857 ScopedThreadStateChange tsc(self, Thread::kNative);
2858 if (verbose_jni) {
2859 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2860 }
2861 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002862 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002863
Brian Carlstromaded5f72011-10-07 17:15:04 -07002864 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002865
2866 if (version != JNI_VERSION_1_2 &&
2867 version != JNI_VERSION_1_4 &&
2868 version != JNI_VERSION_1_6) {
2869 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2870 << "bad version: " << version;
2871 // It's unwise to call dlclose() here, but we can mark it
2872 // as bad and ensure that future load attempts will fail.
2873 // We don't know how far JNI_OnLoad got, so there could
2874 // be some partially-initialized stuff accessible through
2875 // newly-registered native method calls. We could try to
2876 // unregister them, but that doesn't seem worthwhile.
2877 result = false;
2878 } else {
2879 if (verbose_jni) {
2880 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2881 << " from JNI_OnLoad in \"" << path << "\"]";
2882 }
2883 }
2884 }
2885
2886 library->SetResult(result);
2887 return result;
2888}
2889
2890void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2891 CHECK(m->IsNative());
2892
2893 Class* c = m->GetDeclaringClass();
2894
2895 // If this is a static method, it could be called before the class
2896 // has been initialized.
2897 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002898 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002899 return NULL;
2900 }
2901 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002902 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002903 }
2904
Brian Carlstrom16192862011-09-12 17:50:06 -07002905 std::string detail;
2906 void* native_method;
2907 {
2908 MutexLock mu(libraries_lock);
2909 native_method = libraries->FindNativeMethod(m, detail);
2910 }
2911 // throwing can cause libraries_lock to be reacquired
2912 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002913 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002914 }
2915 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002916}
2917
Elliott Hughes410c0c82011-09-01 17:58:25 -07002918void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2919 {
2920 MutexLock mu(globals_lock);
2921 globals.VisitRoots(visitor, arg);
2922 }
2923 {
2924 MutexLock mu(pins_lock);
2925 pin_table.VisitRoots(visitor, arg);
2926 }
2927 // The weak_globals table is visited by the GC itself (because it mutates the table).
2928}
2929
Ian Rogersdf20fe02011-07-20 20:34:16 -07002930} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002931
2932std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2933 switch (rhs) {
2934 case JNIInvalidRefType:
2935 os << "JNIInvalidRefType";
2936 return os;
2937 case JNILocalRefType:
2938 os << "JNILocalRefType";
2939 return os;
2940 case JNIGlobalRefType:
2941 os << "JNIGlobalRefType";
2942 return os;
2943 case JNIWeakGlobalRefType:
2944 os << "JNIWeakGlobalRefType";
2945 return os;
2946 }
2947}