blob: 1f3124c0a2ef1cf4fab0d614e5b9b27ac67fc15a [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070015#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070016#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070018#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070021#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070022#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070023#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070024#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070026
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070027namespace art {
28
Elliott Hughes2ced6a52011-10-16 18:44:48 -070029static const size_t kMonitorsInitial = 32; // Arbitrary.
30static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
31
32static const size_t kLocalsInitial = 64; // Arbitrary.
33static const size_t kLocalsMax = 512; // Arbitrary sanity check.
34
35static const size_t kPinTableInitial = 16; // Arbitrary.
36static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
37
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070038static size_t gGlobalsInitial = 512; // Arbitrary.
39static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070040
41static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
42static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
43
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070044void SetJniGlobalsMax(size_t max) {
45 if (max != 0) {
46 gGlobalsMax = max;
47 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
48 }
49}
Ian Rogersdf20fe02011-07-20 20:34:16 -070050
Elliott Hughesc5f7c912011-08-18 14:00:42 -070051/*
52 * Add a local reference for an object to the current stack frame. When
53 * the native function returns, the reference will be discarded.
54 *
55 * We need to allow the same reference to be added multiple times.
56 *
57 * This will be called on otherwise unreferenced objects. We cannot do
58 * GC allocations here, and it's best if we don't grab a mutex.
59 *
60 * Returns the local reference (currently just the same pointer that was
61 * passed in), or NULL on failure.
62 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070063template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070064T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
65 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
66 Object* obj = const_cast<Object*>(const_obj);
67
Elliott Hughesc5f7c912011-08-18 14:00:42 -070068 if (obj == NULL) {
69 return NULL;
70 }
71
Elliott Hughesbf86d042011-08-31 17:53:14 -070072 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
73 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070074
Ian Rogers5a7a74a2011-09-26 16:32:29 -070075 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070076 IndirectRef ref = locals.Add(cookie, obj);
77 if (ref == NULL) {
78 // TODO: just change Add's DCHECK to CHECK and lose this?
79 locals.Dump();
80 LOG(FATAL) << "Failed adding to JNI local reference table "
81 << "(has " << locals.Capacity() << " entries)";
82 // TODO: dvmDumpThread(dvmThreadSelf(), false);
83 }
84
85#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070086 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070087 size_t entry_count = locals.Capacity();
88 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070089 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070090 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070091 locals.Dump();
92 // TODO: dvmDumpThread(dvmThreadSelf(), false);
93 // dvmAbort();
94 }
95 }
96#endif
97
Elliott Hughesbf86d042011-08-31 17:53:14 -070098 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070099 // Hand out direct pointers to support broken old apps.
100 return reinterpret_cast<T>(obj);
101 }
102
103 return reinterpret_cast<T>(ref);
104}
105
Elliott Hughesbf86d042011-08-31 17:53:14 -0700106// For external use.
107template<typename T>
108T Decode(JNIEnv* public_env, jobject obj) {
109 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
110 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
111}
112// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700113template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700114template Class* Decode<Class*>(JNIEnv*, jobject);
115template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
116template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700117template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700118template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700119template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700120template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400121template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700122template String* Decode<String*>(JNIEnv*, jobject);
123template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700124
125namespace {
126
Elliott Hughescdf53122011-08-19 15:46:09 -0700127jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
128 if (obj == NULL) {
129 return NULL;
130 }
Elliott Hughes75770752011-08-24 17:52:38 -0700131 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700132 IndirectReferenceTable& weak_globals = vm->weak_globals;
133 MutexLock mu(vm->weak_globals_lock);
134 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
135 return reinterpret_cast<jweak>(ref);
136}
137
Elliott Hughesbf86d042011-08-31 17:53:14 -0700138// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700139template<typename T>
140T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700141 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700142}
143
Elliott Hughes418d20f2011-09-22 14:00:39 -0700144byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
145 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700146 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700147 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700148 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700149 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
150 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700151 case 'Z':
152 case 'B':
153 case 'C':
154 case 'S':
155 case 'I':
156 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
157 offset += 4;
158 break;
159 case 'F':
160 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
161 offset += 4;
162 break;
163 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700164 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700165 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
166 offset += sizeof(Object*);
167 break;
168 }
169 case 'D':
170 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
171 offset += 8;
172 break;
173 case 'J':
174 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
175 offset += 8;
176 break;
177 }
178 }
179 return arg_array.release();
180}
181
Elliott Hughes418d20f2011-09-22 14:00:39 -0700182byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
183 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700184 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700185 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700186 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700187 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
188 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700189 case 'Z':
190 case 'B':
191 case 'C':
192 case 'S':
193 case 'I':
194 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
195 offset += 4;
196 break;
197 case 'F':
198 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
199 offset += 4;
200 break;
201 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700202 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700203 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
204 offset += sizeof(Object*);
205 break;
206 }
207 case 'D':
208 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
209 offset += 8;
210 break;
211 case 'J':
212 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
213 offset += 8;
214 break;
215 }
216 }
217 return arg_array.release();
218}
219
Elliott Hughes418d20f2011-09-22 14:00:39 -0700220JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
221 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700222 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700223 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700224 return result;
225}
226
Elliott Hughes418d20f2011-09-22 14:00:39 -0700227JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
228 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
229 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700230 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700231 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
232 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700233}
234
Elliott Hughes75770752011-08-24 17:52:38 -0700235Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700236 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700237}
238
Elliott Hughes418d20f2011-09-22 14:00:39 -0700239JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
240 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
241 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700242 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700243 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
244 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700245}
246
Elliott Hughes418d20f2011-09-22 14:00:39 -0700247JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
248 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
249 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700250 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700251 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
252 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700253}
254
Elliott Hughes6b436852011-08-12 10:16:44 -0700255// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
256// separated with slashes but aren't wrapped with "L;" like regular descriptors
257// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
258// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
259// supported names with dots too (such as "a.b.C").
260std::string NormalizeJniClassDescriptor(const char* name) {
261 std::string result;
262 // Add the missing "L;" if necessary.
263 if (name[0] == '[') {
264 result = name;
265 } else {
266 result += 'L';
267 result += name;
268 result += ';';
269 }
270 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700271 if (result.find('.') != std::string::npos) {
272 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
273 << "\"" << name << "\"";
274 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700275 }
276 return result;
277}
278
Elliott Hughes14134a12011-09-30 16:55:51 -0700279void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
280 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700281 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes14134a12011-09-30 16:55:51 -0700282 "no %s method \"%s.%s%s\"", kind, class_descriptor.c_str(), name, sig);
283}
284
Elliott Hughescdf53122011-08-19 15:46:09 -0700285jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
286 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700287 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700288 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700289 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700290
291 Method* method = NULL;
292 if (is_static) {
293 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700294 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700295 method = c->FindVirtualMethod(name, sig);
296 if (method == NULL) {
297 // No virtual method matching the signature. Search declared
298 // private methods and constructors.
299 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700300 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700301 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700302
Elliott Hughescdf53122011-08-19 15:46:09 -0700303 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700304 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700305 return NULL;
306 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700307
Elliott Hughes418d20f2011-09-22 14:00:39 -0700308 method->InitJavaFields();
309
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700310 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700311}
312
Brian Carlstrom00fae582011-10-28 01:16:28 -0700313const ClassLoader* GetClassLoader(Thread* self) {
314 Frame frame = self->GetTopOfStack();
315 Method* method = frame.GetMethod();
316 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
317 return self->GetClassLoaderOverride();
318 }
319 return method->GetDeclaringClass()->GetClassLoader();
320}
321
Elliott Hughescdf53122011-08-19 15:46:09 -0700322jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
323 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700324 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700325 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700326 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700327
328 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 Class* field_type;
330 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
331 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700332 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700333 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700334 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700336 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 if (field_type == NULL) {
338 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700339 DCHECK(ts.Self()->IsExceptionPending());
340 ts.Self()->ClearException();
341 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700342 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700343 "no type \"%s\" found and so no field \"%s\" could be found in class "
344 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700345 return NULL;
346 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700347 std::string field_type_descriptor = field_type->GetDescriptor()->ToModifiedUtf8();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 if (is_static) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700349 field = c->FindStaticField(name, field_type_descriptor);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700350 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700351 field = c->FindInstanceField(name, field_type_descriptor);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700352 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700353 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700354 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700355 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700356 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700357 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700358 return NULL;
359 }
Elliott Hughes80609252011-09-23 17:24:51 -0700360 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700361 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700362}
363
Elliott Hughes75770752011-08-24 17:52:38 -0700364void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
365 JavaVMExt* vm = ts.Vm();
366 MutexLock mu(vm->pins_lock);
367 vm->pin_table.Add(array);
368}
369
370void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
371 JavaVMExt* vm = ts.Vm();
372 MutexLock mu(vm->pins_lock);
373 vm->pin_table.Remove(array);
374}
375
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700376template<typename JniT, typename ArtT>
377JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
378 CHECK_GE(length, 0); // TODO: ReportJniError
379 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700380 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700381}
382
Elliott Hughes75770752011-08-24 17:52:38 -0700383template <typename ArrayT, typename CArrayT, typename ArtArrayT>
384CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
385 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
386 PinPrimitiveArray(ts, array);
387 if (is_copy != NULL) {
388 *is_copy = JNI_FALSE;
389 }
390 return array->GetData();
391}
392
393template <typename ArrayT>
394void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
395 if (mode != JNI_COMMIT) {
396 Array* array = Decode<Array*>(ts, java_array);
397 UnpinPrimitiveArray(ts, array);
398 }
399}
400
Elliott Hughes814e4032011-08-23 12:07:56 -0700401void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700402 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700403 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700404 "%s offset=%d length=%d %s.length=%d",
405 type.c_str(), start, length, identifier, array->GetLength());
406}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700407void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700408 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700409 "offset=%d length=%d string.length()=%d", start, length, array_length);
410}
Elliott Hughes814e4032011-08-23 12:07:56 -0700411
412template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700413void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700414 ArrayT* array = Decode<ArrayT*>(ts, java_array);
415 if (start < 0 || length < 0 || start + length > array->GetLength()) {
416 ThrowAIOOBE(ts, array, start, length, "src");
417 } else {
418 JavaT* data = array->GetData();
419 memcpy(buf, data + start, length * sizeof(JavaT));
420 }
421}
422
423template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700424void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700425 ArrayT* array = Decode<ArrayT*>(ts, java_array);
426 if (start < 0 || length < 0 || start + length > array->GetLength()) {
427 ThrowAIOOBE(ts, array, start, length, "dst");
428 } else {
429 JavaT* data = array->GetData();
430 memcpy(data + start, buf, length * sizeof(JavaT));
431 }
432}
433
Elliott Hughes75770752011-08-24 17:52:38 -0700434jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700435 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
436 CHECK(buffer_class.get() != NULL);
437 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
438}
439
Elliott Hughes75770752011-08-24 17:52:38 -0700440jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700441 static jclass buffer_class = InitDirectByteBufferClass(env);
442 return buffer_class;
443}
444
Elliott Hughes75770752011-08-24 17:52:38 -0700445jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
446 if (vm == NULL || p_env == NULL) {
447 return JNI_ERR;
448 }
449
450 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
451 JavaVMAttachArgs args;
452 if (thr_args == NULL) {
453 // Allow the v1.1 calling convention.
454 args.version = JNI_VERSION_1_2;
455 args.name = NULL;
456 args.group = NULL; // TODO: get "main" thread group
457 } else {
458 args.version = in_args->version;
459 args.name = in_args->name;
460 if (in_args->group != NULL) {
461 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
462 args.group = NULL; // TODO: decode in_args->group
463 } else {
464 args.group = NULL; // TODO: get "main" thread group
465 }
466 }
467 CHECK_GE(args.version, JNI_VERSION_1_2);
468
469 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700470 runtime->AttachCurrentThread(args.name, as_daemon);
471 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700472 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700473}
474
Elliott Hughes79082e32011-08-25 12:07:32 -0700475class SharedLibrary {
476 public:
477 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
478 : path_(path),
479 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700480 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700481 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700482 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700483 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700484 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 }
486
Elliott Hughes79082e32011-08-25 12:07:32 -0700487 Object* GetClassLoader() {
488 return class_loader_;
489 }
490
491 std::string GetPath() {
492 return path_;
493 }
494
495 /*
496 * Check the result of an earlier call to JNI_OnLoad on this library. If
497 * the call has not yet finished in another thread, wait for it.
498 */
499 bool CheckOnLoadResult(JavaVMExt* vm) {
500 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700501 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700502 // Check this so we don't end up waiting for ourselves. We need
503 // to return "true" so the caller can continue.
504 LOG(INFO) << *self << " recursive attempt to load library "
505 << "\"" << path_ << "\"";
506 return true;
507 }
508
509 MutexLock mu(jni_on_load_lock_);
510 while (jni_on_load_result_ == kPending) {
511 if (vm->verbose_jni) {
512 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
513 << "JNI_OnLoad...]";
514 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700515 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700516 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700517 }
518
519 bool okay = (jni_on_load_result_ == kOkay);
520 if (vm->verbose_jni) {
521 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
522 << (okay ? "succeeded" : "failed") << "]";
523 }
524 return okay;
525 }
526
527 void SetResult(bool result) {
528 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700529 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700530
531 // Broadcast a wakeup to anybody sleeping on the condition variable.
532 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700533 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700534 }
535
536 void* FindSymbol(const std::string& symbol_name) {
537 return dlsym(handle_, symbol_name.c_str());
538 }
539
540 private:
541 enum JNI_OnLoadState {
542 kPending,
543 kFailed,
544 kOkay,
545 };
546
547 // Path to library "/system/lib/libjni.so".
548 std::string path_;
549
550 // The void* returned by dlopen(3).
551 void* handle_;
552
553 // The ClassLoader this library is associated with.
554 Object* class_loader_;
555
556 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700557 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700558 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700559 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700560 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700561 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700562 // Result of earlier JNI_OnLoad call.
563 JNI_OnLoadState jni_on_load_result_;
564};
565
Elliott Hughescdf53122011-08-19 15:46:09 -0700566} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700567
Elliott Hughes79082e32011-08-25 12:07:32 -0700568// This exists mainly to keep implementation details out of the header file.
569class Libraries {
570 public:
571 Libraries() {
572 }
573
574 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700575 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700576 }
577
578 SharedLibrary* Get(const std::string& path) {
579 return libraries_[path];
580 }
581
582 void Put(const std::string& path, SharedLibrary* library) {
583 libraries_[path] = library;
584 }
585
586 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700587 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700588 std::string jni_short_name(JniShortName(m));
589 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700590 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700591 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
592 SharedLibrary* library = it->second;
593 if (library->GetClassLoader() != declaring_class_loader) {
594 // We only search libraries loaded by the appropriate ClassLoader.
595 continue;
596 }
597 // Try the short name then the long name...
598 void* fn = library->FindSymbol(jni_short_name);
599 if (fn == NULL) {
600 fn = library->FindSymbol(jni_long_name);
601 }
602 if (fn != NULL) {
603 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700604 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700605 << " in \"" << library->GetPath() << "\"]";
606 }
607 return fn;
608 }
609 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700610 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700611 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700612 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700613 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700614 return NULL;
615 }
616
617 private:
618 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
619
620 std::map<std::string, SharedLibrary*> libraries_;
621};
622
Elliott Hughes418d20f2011-09-22 14:00:39 -0700623JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
624 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
625 Object* receiver = Decode<Object*>(env, obj);
626 Method* method = DecodeMethod(mid);
627 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
628 return InvokeWithArgArray(env, receiver, method, arg_array.get());
629}
630
Elliott Hughescdf53122011-08-19 15:46:09 -0700631class JNI {
632 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700633
Elliott Hughescdf53122011-08-19 15:46:09 -0700634 static jint GetVersion(JNIEnv* env) {
635 ScopedJniThreadState ts(env);
636 return JNI_VERSION_1_6;
637 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700638
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
640 ScopedJniThreadState ts(env);
641 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700642 return NULL;
643 }
644
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 static jclass FindClass(JNIEnv* env, const char* name) {
646 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700647 Runtime* runtime = Runtime::Current();
648 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700650 Class* c = NULL;
651 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700652 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700653 c = class_linker->FindClass(descriptor, cl);
654 } else {
655 c = class_linker->FindSystemClass(descriptor);
656 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700657 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700658 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700659
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
661 ScopedJniThreadState ts(env);
662 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700663 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700664 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700665
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
667 ScopedJniThreadState ts(env);
668 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700669 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700671
Elliott Hughescdf53122011-08-19 15:46:09 -0700672 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
673 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700674 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700675 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700677
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
679 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700680 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700681 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700683
Elliott Hughes37f7a402011-08-22 18:56:01 -0700684 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
685 ScopedJniThreadState ts(env);
686 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700687 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 }
689
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700690 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700692 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700693 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700695
Elliott Hughes37f7a402011-08-22 18:56:01 -0700696 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700697 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 Class* c1 = Decode<Class*>(ts, java_class1);
699 Class* c2 = Decode<Class*>(ts, java_class2);
700 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700701 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700702
Elliott Hughes37f7a402011-08-22 18:56:01 -0700703 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700704 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700705 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700706 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700707 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 return JNI_TRUE;
709 } else {
710 Object* obj = Decode<Object*>(ts, jobj);
711 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700712 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700713 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700715
Elliott Hughes37f7a402011-08-22 18:56:01 -0700716 static jint Throw(JNIEnv* env, jthrowable java_exception) {
717 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700718 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700719 if (exception == NULL) {
720 return JNI_ERR;
721 }
722 ts.Self()->SetException(exception);
723 return JNI_OK;
724 }
725
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700726 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700727 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700728 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700729 jmethodID mid = ((msg != NULL)
730 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
731 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700732 if (mid == NULL) {
733 return JNI_ERR;
734 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700735 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700736 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700737 return JNI_ERR;
738 }
739
740 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700741 args[0].l = s.get();
742 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
743 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700744 return JNI_ERR;
745 }
746
Elliott Hughes72025e52011-08-23 17:50:30 -0700747 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700748
Elliott Hughes37f7a402011-08-22 18:56:01 -0700749 return JNI_OK;
750 }
751
752 static jboolean ExceptionCheck(JNIEnv* env) {
753 ScopedJniThreadState ts(env);
754 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
755 }
756
757 static void ExceptionClear(JNIEnv* env) {
758 ScopedJniThreadState ts(env);
759 ts.Self()->ClearException();
760 }
761
762 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700763 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700764
765 Thread* self = ts.Self();
766 Throwable* original_exception = self->GetException();
767 self->ClearException();
768
Elliott Hughesbf86d042011-08-31 17:53:14 -0700769 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700770 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
771 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
772 if (mid == NULL) {
773 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700774 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700775 } else {
776 env->CallVoidMethod(exception.get(), mid);
777 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700778 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700779 << " thrown while calling printStackTrace";
780 self->ClearException();
781 }
782 }
783
784 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700786
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 static jthrowable ExceptionOccurred(JNIEnv* env) {
788 ScopedJniThreadState ts(env);
789 Object* exception = ts.Self()->GetException();
790 if (exception == NULL) {
791 return NULL;
792 } else {
793 // TODO: if adding a local reference failing causes the VM to abort
794 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700795 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 if (localException == NULL) {
797 // We were unable to add a new local reference, and threw a new
798 // exception. We can't return "exception", because it's not a
799 // local reference. So we have to return NULL, indicating that
800 // there was no exception, even though it's pretty much raining
801 // exceptions in here.
802 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
803 }
804 return localException;
805 }
806 }
807
Elliott Hughescdf53122011-08-19 15:46:09 -0700808 static void FatalError(JNIEnv* env, const char* msg) {
809 ScopedJniThreadState ts(env);
810 LOG(FATAL) << "JNI FatalError called: " << msg;
811 }
812
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700813 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700815 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
816 return JNI_ERR;
817 }
818 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 return JNI_OK;
820 }
821
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700822 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700824 Object* survivor = Decode<Object*>(ts, java_survivor);
825 ts.Env()->PopFrame();
826 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 }
828
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700829 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700830 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700831 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
832 }
833
834 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
835 // TODO: we should try to expand the table if necessary.
836 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
837 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
838 return JNI_ERR;
839 }
840 // TODO: this isn't quite right, since "capacity" includes holes.
841 size_t capacity = ts.Env()->locals.Capacity();
842 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
843 if (!okay) {
844 ts.Self()->ThrowOutOfMemoryError(caller);
845 }
846 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700847 }
848
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
850 ScopedJniThreadState ts(env);
851 if (obj == NULL) {
852 return NULL;
853 }
854
Elliott Hughes75770752011-08-24 17:52:38 -0700855 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 IndirectReferenceTable& globals = vm->globals;
857 MutexLock mu(vm->globals_lock);
858 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
859 return reinterpret_cast<jobject>(ref);
860 }
861
862 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
863 ScopedJniThreadState ts(env);
864 if (obj == NULL) {
865 return;
866 }
867
Elliott Hughes75770752011-08-24 17:52:38 -0700868 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 IndirectReferenceTable& globals = vm->globals;
870 MutexLock mu(vm->globals_lock);
871
872 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
873 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
874 << "failed to find entry";
875 }
876 }
877
878 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
879 ScopedJniThreadState ts(env);
880 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
881 }
882
883 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
884 ScopedJniThreadState ts(env);
885 if (obj == NULL) {
886 return;
887 }
888
Elliott Hughes75770752011-08-24 17:52:38 -0700889 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 IndirectReferenceTable& weak_globals = vm->weak_globals;
891 MutexLock mu(vm->weak_globals_lock);
892
893 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
894 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
895 << "failed to find entry";
896 }
897 }
898
899 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
900 ScopedJniThreadState ts(env);
901 if (obj == NULL) {
902 return NULL;
903 }
904
905 IndirectReferenceTable& locals = ts.Env()->locals;
906
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700907 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
909 return reinterpret_cast<jobject>(ref);
910 }
911
912 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
913 ScopedJniThreadState ts(env);
914 if (obj == NULL) {
915 return;
916 }
917
918 IndirectReferenceTable& locals = ts.Env()->locals;
919
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700920 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 if (!locals.Remove(cookie, obj)) {
922 // Attempting to delete a local reference that is not in the
923 // topmost local reference frame is a no-op. DeleteLocalRef returns
924 // void and doesn't throw any exceptions, but we should probably
925 // complain about it so the user will notice that things aren't
926 // going quite the way they expect.
927 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
928 << "failed to find entry";
929 }
930 }
931
932 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
933 ScopedJniThreadState ts(env);
934 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
935 ? JNI_TRUE : JNI_FALSE;
936 }
937
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700938 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700940 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700941 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700942 return NULL;
943 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700944 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 }
946
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 ScopedJniThreadState ts(env);
949 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 va_start(args, mid);
951 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 va_end(args);
953 return result;
954 }
955
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700958 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700959 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700960 return NULL;
961 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700962 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700963 if (result == NULL) {
964 return NULL;
965 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700966 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700968 if (!ts.Self()->IsExceptionPending()) {
969 return local_result;
970 } else {
971 return NULL;
972 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700977 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700978 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700979 return NULL;
980 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700981 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700982 if (result == NULL) {
983 return NULL;
984 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700985 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700987 if (!ts.Self()->IsExceptionPending()) {
988 return local_result;
989 } else {
990 return NULL;
991 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 }
993
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
995 ScopedJniThreadState ts(env);
996 return FindMethodID(ts, c, name, sig, false);
997 }
998
999 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1000 ScopedJniThreadState ts(env);
1001 return FindMethodID(ts, c, name, sig, true);
1002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 va_list ap;
1007 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001008 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001010 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 }
1012
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001015 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001016 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001021 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001022 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 }
1024
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 va_list ap;
1028 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001029 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_end(ap);
1031 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001036 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001041 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 }
1043
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 va_list ap;
1047 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001048 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_end(ap);
1050 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001055 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001060 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 va_list ap;
1066 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001067 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 va_end(ap);
1069 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001074 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001079 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 va_list ap;
1085 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001086 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 va_end(ap);
1088 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001093 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001098 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 va_list ap;
1104 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001105 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 va_end(ap);
1107 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001112 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001117 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 va_list ap;
1123 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001124 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 va_end(ap);
1126 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001131 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001136 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 va_list ap;
1142 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001143 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 va_end(ap);
1145 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 }
1147
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001150 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001155 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 va_list ap;
1161 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001162 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 va_end(ap);
1164 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001169 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 }
1171
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001174 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 va_list ap;
1180 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001181 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001187 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001192 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 }
1194
1195 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 ScopedJniThreadState ts(env);
1198 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001200 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001201 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 va_end(ap);
1203 return local_result;
1204 }
1205
1206 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001209 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001210 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
1213 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001216 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001217 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
1220 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 ScopedJniThreadState ts(env);
1223 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001225 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 va_end(ap);
1227 return result.z;
1228 }
1229
1230 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001233 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
1236 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001239 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
1242 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 ScopedJniThreadState ts(env);
1245 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001247 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 va_end(ap);
1249 return result.b;
1250 }
1251
1252 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001255 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
1258 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001261 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 ScopedJniThreadState ts(env);
1267 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001269 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 va_end(ap);
1271 return result.c;
1272 }
1273
1274 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001277 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
1280 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001283 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
1286 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 ScopedJniThreadState ts(env);
1289 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001291 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 va_end(ap);
1293 return result.s;
1294 }
1295
1296 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001299 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
1302 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001305 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
Elliott Hughes418d20f2011-09-22 14:00:39 -07001308 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 ScopedJniThreadState ts(env);
1310 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001312 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 va_end(ap);
1314 return result.i;
1315 }
1316
1317 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001320 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
1323 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001324 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001326 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
1329 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001330 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 ScopedJniThreadState ts(env);
1332 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001333 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001334 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 va_end(ap);
1336 return result.j;
1337 }
1338
1339 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001340 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001342 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 }
1344
1345 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001346 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001348 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
1351 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001352 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 ScopedJniThreadState ts(env);
1354 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001355 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001356 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 va_end(ap);
1358 return result.f;
1359 }
1360
1361 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001364 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
1367 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001368 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001370 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
1373 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001374 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 ScopedJniThreadState ts(env);
1376 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001377 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001378 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 va_end(ap);
1380 return result.d;
1381 }
1382
1383 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001384 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001386 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
1389 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001390 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001392 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
1395 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001396 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 ScopedJniThreadState ts(env);
1398 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001399 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001400 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 va_end(ap);
1402 }
1403
1404 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001405 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001407 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 }
1409
1410 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001411 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001413 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001416 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 ScopedJniThreadState ts(env);
1418 return FindFieldID(ts, c, name, sig, false);
1419 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001420
1421
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001422 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 ScopedJniThreadState ts(env);
1424 return FindFieldID(ts, c, name, sig, true);
1425 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001426
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001427 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001429 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001430 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001431 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001433
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001436 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001437 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 Object* o = Decode<Object*>(ts, java_object);
1443 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001444 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001445 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001451 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455#define GET_PRIMITIVE_FIELD(fn, instance) \
1456 ScopedJniThreadState ts(env); \
1457 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001458 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 return f->fn(o)
1460
1461#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1462 ScopedJniThreadState ts(env); \
1463 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001464 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001465 f->fn(o, value)
1466
1467 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1476 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1480 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001483 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1484 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001487 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1488 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1492 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1496 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1500 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1504 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 }
1506
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001507 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1508 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
1510
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1512 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 }
1514
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001515 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1516 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
1518
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1520 GET_PRIMITIVE_FIELD(GetLong, NULL);
1521 }
1522
1523 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1524 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1525 }
1526
1527 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1528 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1529 }
1530
1531 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1532 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1533 }
1534
1535 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1536 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1537 }
1538
1539 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1540 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1541 }
1542
1543 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1544 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1545 }
1546
1547 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1548 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1549 }
1550
1551 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1552 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1553 }
1554
1555 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1556 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1557 }
1558
1559 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1560 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1561 }
1562
1563 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1564 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1565 }
1566
1567 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1568 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1569 }
1570
1571 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1572 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1573 }
1574
1575 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1576 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1577 }
1578
1579 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1580 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1581 }
1582
1583 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1584 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1585 }
1586
1587 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1588 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1589 }
1590
1591 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1592 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 }
1594
Elliott Hughes418d20f2011-09-22 14:00:39 -07001595 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 ScopedJniThreadState ts(env);
1597 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001598 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001599 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001600 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 va_end(ap);
1602 return local_result;
1603 }
1604
Elliott Hughes418d20f2011-09-22 14:00:39 -07001605 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001607 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001608 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 }
1610
Elliott Hughes418d20f2011-09-22 14:00:39 -07001611 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001613 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001614 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 }
1616
Elliott Hughes418d20f2011-09-22 14:00:39 -07001617 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 ScopedJniThreadState ts(env);
1619 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001620 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001621 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 va_end(ap);
1623 return result.z;
1624 }
1625
Elliott Hughes418d20f2011-09-22 14:00:39 -07001626 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001628 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes418d20f2011-09-22 14:00:39 -07001631 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001633 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes72025e52011-08-23 17:50:30 -07001636 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 ScopedJniThreadState ts(env);
1638 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001639 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001640 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 va_end(ap);
1642 return result.b;
1643 }
1644
Elliott Hughes418d20f2011-09-22 14:00:39 -07001645 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001647 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes418d20f2011-09-22 14:00:39 -07001650 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001652 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 }
1654
Elliott Hughes72025e52011-08-23 17:50:30 -07001655 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 ScopedJniThreadState ts(env);
1657 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001658 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 va_end(ap);
1661 return result.c;
1662 }
1663
Elliott Hughes418d20f2011-09-22 14:00:39 -07001664 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001666 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes418d20f2011-09-22 14:00:39 -07001669 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001671 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 }
1673
Elliott Hughes72025e52011-08-23 17:50:30 -07001674 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 ScopedJniThreadState ts(env);
1676 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001677 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001678 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 va_end(ap);
1680 return result.s;
1681 }
1682
Elliott Hughes418d20f2011-09-22 14:00:39 -07001683 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001685 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
Elliott Hughes418d20f2011-09-22 14:00:39 -07001688 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001690 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
Elliott Hughes72025e52011-08-23 17:50:30 -07001693 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 ScopedJniThreadState ts(env);
1695 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001696 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 va_end(ap);
1699 return result.i;
1700 }
1701
Elliott Hughes418d20f2011-09-22 14:00:39 -07001702 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001704 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes418d20f2011-09-22 14:00:39 -07001707 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001709 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 }
1711
Elliott Hughes72025e52011-08-23 17:50:30 -07001712 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 ScopedJniThreadState ts(env);
1714 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001715 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 va_end(ap);
1718 return result.j;
1719 }
1720
Elliott Hughes418d20f2011-09-22 14:00:39 -07001721 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001723 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes418d20f2011-09-22 14:00:39 -07001726 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001728 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
1733 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001734 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001735 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 va_end(ap);
1737 return result.f;
1738 }
1739
Elliott Hughes418d20f2011-09-22 14:00:39 -07001740 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001742 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes418d20f2011-09-22 14:00:39 -07001745 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001747 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
Elliott Hughes72025e52011-08-23 17:50:30 -07001750 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 ScopedJniThreadState ts(env);
1752 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001753 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001754 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 va_end(ap);
1756 return result.d;
1757 }
1758
Elliott Hughes418d20f2011-09-22 14:00:39 -07001759 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001761 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 }
1763
Elliott Hughes418d20f2011-09-22 14:00:39 -07001764 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001766 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
Elliott Hughes72025e52011-08-23 17:50:30 -07001769 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 ScopedJniThreadState ts(env);
1771 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001772 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001773 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 va_end(ap);
1775 }
1776
Elliott Hughes418d20f2011-09-22 14:00:39 -07001777 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001779 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 }
1781
Elliott Hughes418d20f2011-09-22 14:00:39 -07001782 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001784 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
Elliott Hughes814e4032011-08-23 12:07:56 -07001787 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001789 if (chars == NULL && char_count == 0) {
1790 return NULL;
1791 }
1792 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001793 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
1796 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1797 ScopedJniThreadState ts(env);
1798 if (utf == NULL) {
1799 return NULL;
1800 }
1801 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001802 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes814e4032011-08-23 12:07:56 -07001805 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1806 ScopedJniThreadState ts(env);
1807 return Decode<String*>(ts, java_string)->GetLength();
1808 }
1809
1810 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1811 ScopedJniThreadState ts(env);
1812 return Decode<String*>(ts, java_string)->GetUtfLength();
1813 }
1814
Elliott Hughesb465ab02011-08-24 11:21:21 -07001815 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001817 String* s = Decode<String*>(ts, java_string);
1818 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1819 ThrowSIOOBE(ts, start, length, s->GetLength());
1820 } else {
1821 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1822 memcpy(buf, chars + start, length * sizeof(jchar));
1823 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001824 }
1825
Elliott Hughesb465ab02011-08-24 11:21:21 -07001826 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001827 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001828 String* s = Decode<String*>(ts, java_string);
1829 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1830 ThrowSIOOBE(ts, start, length, s->GetLength());
1831 } else {
1832 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1833 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1834 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001835 }
1836
Elliott Hughes75770752011-08-24 17:52:38 -07001837 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001838 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001839 String* s = Decode<String*>(ts, java_string);
1840 const CharArray* chars = s->GetCharArray();
1841 PinPrimitiveArray(ts, chars);
1842 if (is_copy != NULL) {
1843 *is_copy = JNI_FALSE;
1844 }
1845 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001846 }
1847
Elliott Hughes75770752011-08-24 17:52:38 -07001848 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001849 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001850 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 }
1852
Elliott Hughes75770752011-08-24 17:52:38 -07001853 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001855 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001856 }
1857
Elliott Hughes75770752011-08-24 17:52:38 -07001858 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001860 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 }
1862
Elliott Hughes75770752011-08-24 17:52:38 -07001863 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001864 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001865 if (java_string == NULL) {
1866 return NULL;
1867 }
1868 if (is_copy != NULL) {
1869 *is_copy = JNI_TRUE;
1870 }
1871 String* s = Decode<String*>(ts, java_string);
1872 size_t byte_count = s->GetUtfLength();
1873 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001874 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001875 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1876 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1877 bytes[byte_count] = '\0';
1878 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001879 }
1880
Elliott Hughes75770752011-08-24 17:52:38 -07001881 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001882 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001883 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001884 }
1885
Elliott Hughesbd935992011-08-22 11:59:34 -07001886 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001888 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001889 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001890 Array* array = obj->AsArray();
1891 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 }
1893
Elliott Hughes814e4032011-08-23 12:07:56 -07001894 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001895 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001896 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001897 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 }
1899
1900 static void SetObjectArrayElement(JNIEnv* env,
1901 jobjectArray java_array, jsize index, jobject java_value) {
1902 ScopedJniThreadState ts(env);
1903 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1904 Object* value = Decode<Object*>(ts, java_value);
1905 array->Set(index, value);
1906 }
1907
1908 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1909 ScopedJniThreadState ts(env);
1910 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1911 }
1912
1913 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1914 ScopedJniThreadState ts(env);
1915 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1916 }
1917
1918 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1919 ScopedJniThreadState ts(env);
1920 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1921 }
1922
1923 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1924 ScopedJniThreadState ts(env);
1925 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1926 }
1927
1928 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1929 ScopedJniThreadState ts(env);
1930 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1931 }
1932
1933 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1934 ScopedJniThreadState ts(env);
1935 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1936 }
1937
1938 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1939 ScopedJniThreadState ts(env);
1940 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1941 }
1942
1943 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1944 ScopedJniThreadState ts(env);
1945 CHECK_GE(length, 0); // TODO: ReportJniError
1946
1947 // Compute the array class corresponding to the given element class.
1948 Class* element_class = Decode<Class*>(ts, element_jclass);
1949 std::string descriptor;
1950 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001951 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001952
1953 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001954 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1955 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 return NULL;
1957 }
1958
Elliott Hughes75770752011-08-24 17:52:38 -07001959 // Allocate and initialize if necessary.
1960 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001962 if (initial_element != NULL) {
1963 Object* initial_object = Decode<Object*>(ts, initial_element);
1964 for (jsize i = 0; i < length; ++i) {
1965 result->Set(i, initial_object);
1966 }
1967 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001968 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 }
1970
1971 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1972 ScopedJniThreadState ts(env);
1973 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1974 }
1975
Elliott Hughes75770752011-08-24 17:52:38 -07001976 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001977 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001978 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001979 }
1980
Elliott Hughes75770752011-08-24 17:52:38 -07001981 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001982 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001983 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001984 }
1985
Elliott Hughes75770752011-08-24 17:52:38 -07001986 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001988 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes75770752011-08-24 17:52:38 -07001991 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001993 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes75770752011-08-24 17:52:38 -07001996 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001998 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Elliott Hughes75770752011-08-24 17:52:38 -07002001 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002003 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Elliott Hughes75770752011-08-24 17:52:38 -07002006 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002008 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Elliott Hughes75770752011-08-24 17:52:38 -07002011 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002013 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes75770752011-08-24 17:52:38 -07002016 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002018 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes75770752011-08-24 17:52:38 -07002021 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002023 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes75770752011-08-24 17:52:38 -07002026 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002028 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes75770752011-08-24 17:52:38 -07002031 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002033 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes75770752011-08-24 17:52:38 -07002036 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002038 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes75770752011-08-24 17:52:38 -07002041 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002043 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Elliott Hughes75770752011-08-24 17:52:38 -07002046 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002048 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Elliott Hughes75770752011-08-24 17:52:38 -07002051 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002053 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Elliott Hughes75770752011-08-24 17:52:38 -07002056 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002058 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes75770752011-08-24 17:52:38 -07002061 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002063 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Elliott Hughes814e4032011-08-23 12:07:56 -07002126 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
Elliott Hughes814e4032011-08-23 12:07:56 -07002131 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
2135
Elliott Hughes814e4032011-08-23 12:07:56 -07002136 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002138 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140
Elliott Hughes814e4032011-08-23 12:07:56 -07002141 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002143 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
2145
Elliott Hughes5174fe62011-08-23 15:12:35 -07002146 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002148 Class* c = Decode<Class*>(ts, java_class);
2149
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 const char* name = methods[i].name;
2152 const char* sig = methods[i].signature;
2153
2154 if (*sig == '!') {
2155 // TODO: fast jni. it's too noisy to log all these.
2156 ++sig;
2157 }
2158
Elliott Hughes5174fe62011-08-23 15:12:35 -07002159 Method* m = c->FindDirectMethod(name, sig);
2160 if (m == NULL) {
2161 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002163 if (m == NULL) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002164 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002166 } else if (!m->IsNative()) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002167 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 return JNI_ERR;
2169 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002170
Elliott Hughes75770752011-08-24 17:52:38 -07002171 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002172 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002173 }
2174
2175 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177 return JNI_OK;
2178 }
2179
Elliott Hughes5174fe62011-08-23 15:12:35 -07002180 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002182 Class* c = Decode<Class*>(ts, java_class);
2183
Elliott Hughes75770752011-08-24 17:52:38 -07002184 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002185 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002186 }
2187
2188 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2189 Method* m = c->GetDirectMethod(i);
2190 if (m->IsNative()) {
2191 m->UnregisterNative();
2192 }
2193 }
2194 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2195 Method* m = c->GetVirtualMethod(i);
2196 if (m->IsNative()) {
2197 m->UnregisterNative();
2198 }
2199 }
2200
2201 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 }
2203
Elliott Hughes72025e52011-08-23 17:50:30 -07002204 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002206 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002207 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 }
2209
Elliott Hughes72025e52011-08-23 17:50:30 -07002210 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002212 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002213 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 }
2215
2216 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2217 ScopedJniThreadState ts(env);
2218 Runtime* runtime = Runtime::Current();
2219 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002220 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 } else {
2222 *vm = NULL;
2223 }
2224 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2225 }
2226
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2228 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002229
2230 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002231 CHECK(address != NULL); // TODO: ReportJniError
2232 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002233
2234 jclass buffer_class = GetDirectByteBufferClass(env);
2235 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2236 if (mid == NULL) {
2237 return NULL;
2238 }
2239
2240 // At the moment, the Java side is limited to 32 bits.
2241 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2242 CHECK_LE(capacity, 0xffffffff);
2243 jint address_arg = reinterpret_cast<jint>(address);
2244 jint capacity_arg = static_cast<jint>(capacity);
2245
2246 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2247 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 }
2249
Elliott Hughesb465ab02011-08-24 11:21:21 -07002250 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002252 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2253 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
Elliott Hughesb465ab02011-08-24 11:21:21 -07002256 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002258 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2259 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002260 }
2261
Elliott Hughesb465ab02011-08-24 11:21:21 -07002262 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002264
Elliott Hughes75770752011-08-24 17:52:38 -07002265 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002266
2267 // Do we definitely know what kind of reference this is?
2268 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2269 IndirectRefKind kind = GetIndirectRefKind(ref);
2270 switch (kind) {
2271 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002272 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2273 return JNILocalRefType;
2274 }
2275 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002276 case kGlobal:
2277 return JNIGlobalRefType;
2278 case kWeakGlobal:
2279 return JNIWeakGlobalRefType;
2280 case kSirtOrInvalid:
2281 // Is it in a stack IRT?
2282 if (ts.Self()->SirtContains(java_object)) {
2283 return JNILocalRefType;
2284 }
2285
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002286 if (!ts.Env()->work_around_app_jni_bugs) {
2287 return JNIInvalidRefType;
2288 }
2289
Elliott Hughesb465ab02011-08-24 11:21:21 -07002290 // If we're handing out direct pointers, check whether it's a direct pointer
2291 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002292 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002293 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002294 return JNILocalRefType;
2295 }
2296 }
2297
2298 return JNIInvalidRefType;
2299 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002300 }
2301};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002302
Elliott Hughesa2501992011-08-26 19:39:54 -07002303const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002304 NULL, // reserved0.
2305 NULL, // reserved1.
2306 NULL, // reserved2.
2307 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 JNI::GetVersion,
2309 JNI::DefineClass,
2310 JNI::FindClass,
2311 JNI::FromReflectedMethod,
2312 JNI::FromReflectedField,
2313 JNI::ToReflectedMethod,
2314 JNI::GetSuperclass,
2315 JNI::IsAssignableFrom,
2316 JNI::ToReflectedField,
2317 JNI::Throw,
2318 JNI::ThrowNew,
2319 JNI::ExceptionOccurred,
2320 JNI::ExceptionDescribe,
2321 JNI::ExceptionClear,
2322 JNI::FatalError,
2323 JNI::PushLocalFrame,
2324 JNI::PopLocalFrame,
2325 JNI::NewGlobalRef,
2326 JNI::DeleteGlobalRef,
2327 JNI::DeleteLocalRef,
2328 JNI::IsSameObject,
2329 JNI::NewLocalRef,
2330 JNI::EnsureLocalCapacity,
2331 JNI::AllocObject,
2332 JNI::NewObject,
2333 JNI::NewObjectV,
2334 JNI::NewObjectA,
2335 JNI::GetObjectClass,
2336 JNI::IsInstanceOf,
2337 JNI::GetMethodID,
2338 JNI::CallObjectMethod,
2339 JNI::CallObjectMethodV,
2340 JNI::CallObjectMethodA,
2341 JNI::CallBooleanMethod,
2342 JNI::CallBooleanMethodV,
2343 JNI::CallBooleanMethodA,
2344 JNI::CallByteMethod,
2345 JNI::CallByteMethodV,
2346 JNI::CallByteMethodA,
2347 JNI::CallCharMethod,
2348 JNI::CallCharMethodV,
2349 JNI::CallCharMethodA,
2350 JNI::CallShortMethod,
2351 JNI::CallShortMethodV,
2352 JNI::CallShortMethodA,
2353 JNI::CallIntMethod,
2354 JNI::CallIntMethodV,
2355 JNI::CallIntMethodA,
2356 JNI::CallLongMethod,
2357 JNI::CallLongMethodV,
2358 JNI::CallLongMethodA,
2359 JNI::CallFloatMethod,
2360 JNI::CallFloatMethodV,
2361 JNI::CallFloatMethodA,
2362 JNI::CallDoubleMethod,
2363 JNI::CallDoubleMethodV,
2364 JNI::CallDoubleMethodA,
2365 JNI::CallVoidMethod,
2366 JNI::CallVoidMethodV,
2367 JNI::CallVoidMethodA,
2368 JNI::CallNonvirtualObjectMethod,
2369 JNI::CallNonvirtualObjectMethodV,
2370 JNI::CallNonvirtualObjectMethodA,
2371 JNI::CallNonvirtualBooleanMethod,
2372 JNI::CallNonvirtualBooleanMethodV,
2373 JNI::CallNonvirtualBooleanMethodA,
2374 JNI::CallNonvirtualByteMethod,
2375 JNI::CallNonvirtualByteMethodV,
2376 JNI::CallNonvirtualByteMethodA,
2377 JNI::CallNonvirtualCharMethod,
2378 JNI::CallNonvirtualCharMethodV,
2379 JNI::CallNonvirtualCharMethodA,
2380 JNI::CallNonvirtualShortMethod,
2381 JNI::CallNonvirtualShortMethodV,
2382 JNI::CallNonvirtualShortMethodA,
2383 JNI::CallNonvirtualIntMethod,
2384 JNI::CallNonvirtualIntMethodV,
2385 JNI::CallNonvirtualIntMethodA,
2386 JNI::CallNonvirtualLongMethod,
2387 JNI::CallNonvirtualLongMethodV,
2388 JNI::CallNonvirtualLongMethodA,
2389 JNI::CallNonvirtualFloatMethod,
2390 JNI::CallNonvirtualFloatMethodV,
2391 JNI::CallNonvirtualFloatMethodA,
2392 JNI::CallNonvirtualDoubleMethod,
2393 JNI::CallNonvirtualDoubleMethodV,
2394 JNI::CallNonvirtualDoubleMethodA,
2395 JNI::CallNonvirtualVoidMethod,
2396 JNI::CallNonvirtualVoidMethodV,
2397 JNI::CallNonvirtualVoidMethodA,
2398 JNI::GetFieldID,
2399 JNI::GetObjectField,
2400 JNI::GetBooleanField,
2401 JNI::GetByteField,
2402 JNI::GetCharField,
2403 JNI::GetShortField,
2404 JNI::GetIntField,
2405 JNI::GetLongField,
2406 JNI::GetFloatField,
2407 JNI::GetDoubleField,
2408 JNI::SetObjectField,
2409 JNI::SetBooleanField,
2410 JNI::SetByteField,
2411 JNI::SetCharField,
2412 JNI::SetShortField,
2413 JNI::SetIntField,
2414 JNI::SetLongField,
2415 JNI::SetFloatField,
2416 JNI::SetDoubleField,
2417 JNI::GetStaticMethodID,
2418 JNI::CallStaticObjectMethod,
2419 JNI::CallStaticObjectMethodV,
2420 JNI::CallStaticObjectMethodA,
2421 JNI::CallStaticBooleanMethod,
2422 JNI::CallStaticBooleanMethodV,
2423 JNI::CallStaticBooleanMethodA,
2424 JNI::CallStaticByteMethod,
2425 JNI::CallStaticByteMethodV,
2426 JNI::CallStaticByteMethodA,
2427 JNI::CallStaticCharMethod,
2428 JNI::CallStaticCharMethodV,
2429 JNI::CallStaticCharMethodA,
2430 JNI::CallStaticShortMethod,
2431 JNI::CallStaticShortMethodV,
2432 JNI::CallStaticShortMethodA,
2433 JNI::CallStaticIntMethod,
2434 JNI::CallStaticIntMethodV,
2435 JNI::CallStaticIntMethodA,
2436 JNI::CallStaticLongMethod,
2437 JNI::CallStaticLongMethodV,
2438 JNI::CallStaticLongMethodA,
2439 JNI::CallStaticFloatMethod,
2440 JNI::CallStaticFloatMethodV,
2441 JNI::CallStaticFloatMethodA,
2442 JNI::CallStaticDoubleMethod,
2443 JNI::CallStaticDoubleMethodV,
2444 JNI::CallStaticDoubleMethodA,
2445 JNI::CallStaticVoidMethod,
2446 JNI::CallStaticVoidMethodV,
2447 JNI::CallStaticVoidMethodA,
2448 JNI::GetStaticFieldID,
2449 JNI::GetStaticObjectField,
2450 JNI::GetStaticBooleanField,
2451 JNI::GetStaticByteField,
2452 JNI::GetStaticCharField,
2453 JNI::GetStaticShortField,
2454 JNI::GetStaticIntField,
2455 JNI::GetStaticLongField,
2456 JNI::GetStaticFloatField,
2457 JNI::GetStaticDoubleField,
2458 JNI::SetStaticObjectField,
2459 JNI::SetStaticBooleanField,
2460 JNI::SetStaticByteField,
2461 JNI::SetStaticCharField,
2462 JNI::SetStaticShortField,
2463 JNI::SetStaticIntField,
2464 JNI::SetStaticLongField,
2465 JNI::SetStaticFloatField,
2466 JNI::SetStaticDoubleField,
2467 JNI::NewString,
2468 JNI::GetStringLength,
2469 JNI::GetStringChars,
2470 JNI::ReleaseStringChars,
2471 JNI::NewStringUTF,
2472 JNI::GetStringUTFLength,
2473 JNI::GetStringUTFChars,
2474 JNI::ReleaseStringUTFChars,
2475 JNI::GetArrayLength,
2476 JNI::NewObjectArray,
2477 JNI::GetObjectArrayElement,
2478 JNI::SetObjectArrayElement,
2479 JNI::NewBooleanArray,
2480 JNI::NewByteArray,
2481 JNI::NewCharArray,
2482 JNI::NewShortArray,
2483 JNI::NewIntArray,
2484 JNI::NewLongArray,
2485 JNI::NewFloatArray,
2486 JNI::NewDoubleArray,
2487 JNI::GetBooleanArrayElements,
2488 JNI::GetByteArrayElements,
2489 JNI::GetCharArrayElements,
2490 JNI::GetShortArrayElements,
2491 JNI::GetIntArrayElements,
2492 JNI::GetLongArrayElements,
2493 JNI::GetFloatArrayElements,
2494 JNI::GetDoubleArrayElements,
2495 JNI::ReleaseBooleanArrayElements,
2496 JNI::ReleaseByteArrayElements,
2497 JNI::ReleaseCharArrayElements,
2498 JNI::ReleaseShortArrayElements,
2499 JNI::ReleaseIntArrayElements,
2500 JNI::ReleaseLongArrayElements,
2501 JNI::ReleaseFloatArrayElements,
2502 JNI::ReleaseDoubleArrayElements,
2503 JNI::GetBooleanArrayRegion,
2504 JNI::GetByteArrayRegion,
2505 JNI::GetCharArrayRegion,
2506 JNI::GetShortArrayRegion,
2507 JNI::GetIntArrayRegion,
2508 JNI::GetLongArrayRegion,
2509 JNI::GetFloatArrayRegion,
2510 JNI::GetDoubleArrayRegion,
2511 JNI::SetBooleanArrayRegion,
2512 JNI::SetByteArrayRegion,
2513 JNI::SetCharArrayRegion,
2514 JNI::SetShortArrayRegion,
2515 JNI::SetIntArrayRegion,
2516 JNI::SetLongArrayRegion,
2517 JNI::SetFloatArrayRegion,
2518 JNI::SetDoubleArrayRegion,
2519 JNI::RegisterNatives,
2520 JNI::UnregisterNatives,
2521 JNI::MonitorEnter,
2522 JNI::MonitorExit,
2523 JNI::GetJavaVM,
2524 JNI::GetStringRegion,
2525 JNI::GetStringUTFRegion,
2526 JNI::GetPrimitiveArrayCritical,
2527 JNI::ReleasePrimitiveArrayCritical,
2528 JNI::GetStringCritical,
2529 JNI::ReleaseStringCritical,
2530 JNI::NewWeakGlobalRef,
2531 JNI::DeleteWeakGlobalRef,
2532 JNI::ExceptionCheck,
2533 JNI::NewDirectByteBuffer,
2534 JNI::GetDirectBufferAddress,
2535 JNI::GetDirectBufferCapacity,
2536 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002537};
2538
Elliott Hughes75770752011-08-24 17:52:38 -07002539JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002540 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002541 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002542 local_ref_cookie(IRT_FIRST_SEGMENT),
2543 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002544 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002545 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002546 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002547 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002548 functions = unchecked_functions = &gNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002549 if (vm->check_jni) {
2550 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002551 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002552 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2553 // errors will ensue.
2554 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2555 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002556}
2557
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002558JNIEnvExt::~JNIEnvExt() {
2559}
2560
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002561void JNIEnvExt::EnableCheckJni() {
2562 check_jni = true;
2563 functions = GetCheckJniNativeInterface();
2564}
2565
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002566void JNIEnvExt::DumpReferenceTables() {
2567 locals.Dump();
2568 monitors.Dump();
2569}
2570
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002571void JNIEnvExt::PushFrame(int capacity) {
2572 stacked_local_ref_cookies.push_back(local_ref_cookie);
2573 local_ref_cookie = locals.GetSegmentState();
2574}
2575
2576void JNIEnvExt::PopFrame() {
2577 locals.SetSegmentState(local_ref_cookie);
2578 local_ref_cookie = stacked_local_ref_cookies.back();
2579 stacked_local_ref_cookies.pop_back();
2580}
2581
Carl Shapiroea4dca82011-08-01 13:45:38 -07002582// JNI Invocation interface.
2583
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002584extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2585 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2586 if (args->version < JNI_VERSION_1_2) {
2587 return JNI_EVERSION;
2588 }
2589 Runtime::Options options;
2590 for (int i = 0; i < args->nOptions; ++i) {
2591 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002592 options.push_back(std::make_pair(StringPiece(option->optionString),
2593 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002594 }
2595 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002596 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002597 if (runtime == NULL) {
2598 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002599 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002600 runtime->Start();
2601 *p_env = Thread::Current()->GetJniEnv();
2602 *p_vm = runtime->GetJavaVM();
2603 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002604}
2605
Elliott Hughesf2682d52011-08-15 16:37:04 -07002606extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002607 Runtime* runtime = Runtime::Current();
2608 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002609 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002610 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002611 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002612 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002613 }
2614 return JNI_OK;
2615}
2616
2617// Historically unsupported.
2618extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2619 return JNI_ERR;
2620}
2621
Elliott Hughescdf53122011-08-19 15:46:09 -07002622class JII {
2623 public:
2624 static jint DestroyJavaVM(JavaVM* vm) {
2625 if (vm == NULL) {
2626 return JNI_ERR;
2627 } else {
2628 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2629 delete raw_vm->runtime;
2630 return JNI_OK;
2631 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002632 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002633
Elliott Hughescdf53122011-08-19 15:46:09 -07002634 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002635 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002636 }
2637
2638 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002639 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002640 }
2641
2642 static jint DetachCurrentThread(JavaVM* vm) {
2643 if (vm == NULL) {
2644 return JNI_ERR;
2645 } else {
2646 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2647 Runtime* runtime = raw_vm->runtime;
2648 runtime->DetachCurrentThread();
2649 return JNI_OK;
2650 }
2651 }
2652
2653 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2654 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2655 return JNI_EVERSION;
2656 }
2657 if (vm == NULL || env == NULL) {
2658 return JNI_ERR;
2659 }
2660 Thread* thread = Thread::Current();
2661 if (thread == NULL) {
2662 *env = NULL;
2663 return JNI_EDETACHED;
2664 }
2665 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002666 return JNI_OK;
2667 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002668};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002669
Elliott Hughesa2501992011-08-26 19:39:54 -07002670const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002671 NULL, // reserved0
2672 NULL, // reserved1
2673 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002674 JII::DestroyJavaVM,
2675 JII::AttachCurrentThread,
2676 JII::DetachCurrentThread,
2677 JII::GetEnv,
2678 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002679};
2680
Elliott Hughesa0957642011-09-02 14:27:33 -07002681JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002682 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002683 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002684 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002685 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002686 verbose_jni(options->IsVerbose("jni")),
2687 log_third_party_jni(options->IsVerbose("third-party-jni")),
2688 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002689 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002690 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002691 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002692 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002693 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002694 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002695 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002696 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002697 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002698 functions = unchecked_functions = &gInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002699 if (options->check_jni_) {
2700 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002701 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002702}
2703
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002704JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002705 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002706}
2707
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002708void JavaVMExt::EnableCheckJni() {
2709 check_jni = true;
2710 functions = GetCheckJniInvokeInterface();
2711}
2712
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002713void JavaVMExt::DumpReferenceTables() {
2714 {
2715 MutexLock mu(globals_lock);
2716 globals.Dump();
2717 }
2718 {
2719 MutexLock mu(weak_globals_lock);
2720 weak_globals.Dump();
2721 }
2722 {
2723 MutexLock mu(pins_lock);
2724 pin_table.Dump();
2725 }
2726}
2727
Elliott Hughes75770752011-08-24 17:52:38 -07002728bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2729 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002730
2731 // See if we've already loaded this library. If we have, and the class loader
2732 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002733 // TODO: for better results we should canonicalize the pathname (or even compare
2734 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002735 SharedLibrary* library;
2736 {
2737 // TODO: move the locking (and more of this logic) into Libraries.
2738 MutexLock mu(libraries_lock);
2739 library = libraries->Get(path);
2740 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002741 if (library != NULL) {
2742 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002743 // The library will be associated with class_loader. The JNI
2744 // spec says we can't load the same library into more than one
2745 // class loader.
2746 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2747 "ClassLoader %p; can't open in ClassLoader %p",
2748 path.c_str(), library->GetClassLoader(), class_loader);
2749 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002750 return false;
2751 }
2752 if (verbose_jni) {
2753 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2754 << "ClassLoader " << class_loader << "]";
2755 }
2756 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002757 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2758 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002759 return false;
2760 }
2761 return true;
2762 }
2763
2764 // Open the shared library. Because we're using a full path, the system
2765 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2766 // resolve this library's dependencies though.)
2767
2768 // Failures here are expected when java.library.path has several entries
2769 // and we have to hunt for the lib.
2770
2771 // The current version of the dynamic linker prints detailed information
2772 // about dlopen() failures. Some things to check if the message is
2773 // cryptic:
2774 // - make sure the library exists on the device
2775 // - verify that the right path is being opened (the debug log message
2776 // above can help with that)
2777 // - check to see if the library is valid (e.g. not zero bytes long)
2778 // - check config/prelink-linux-arm.map to ensure that the library
2779 // is listed and is not being overrun by the previous entry (if
2780 // loading suddenly stops working on a prelinked library, this is
2781 // a good one to check)
2782 // - write a trivial app that calls sleep() then dlopen(), attach
2783 // to it with "strace -p <pid>" while it sleeps, and watch for
2784 // attempts to open nonexistent dependent shared libs
2785
2786 // TODO: automate some of these checks!
2787
2788 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002789 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002790 // the GC to ignore us.
2791 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002792 void* handle = NULL;
2793 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002794 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002795 handle = dlopen(path.c_str(), RTLD_LAZY);
2796 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002797
2798 if (verbose_jni) {
2799 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2800 }
2801
2802 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002803 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002804 return false;
2805 }
2806
2807 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002808 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002809 // TODO: move the locking (and more of this logic) into Libraries.
2810 MutexLock mu(libraries_lock);
2811 library = libraries->Get(path);
2812 if (library != NULL) {
2813 LOG(INFO) << "WOW: we lost a race to add shared library: "
2814 << "\"" << path << "\" ClassLoader=" << class_loader;
2815 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002816 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002817 library = new SharedLibrary(path, handle, class_loader);
2818 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002819 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002820
2821 if (verbose_jni) {
2822 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2823 }
2824
2825 bool result = true;
2826 void* sym = dlsym(handle, "JNI_OnLoad");
2827 if (sym == NULL) {
2828 if (verbose_jni) {
2829 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2830 }
2831 } else {
2832 // Call JNI_OnLoad. We have to override the current class
2833 // loader, which will always be "null" since the stuff at the
2834 // top of the stack is around Runtime.loadLibrary(). (See
2835 // the comments in the JNI FindClass function.)
2836 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2837 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002838 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002839 self->SetClassLoaderOverride(class_loader);
2840
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002841 int version = 0;
2842 {
2843 ScopedThreadStateChange tsc(self, Thread::kNative);
2844 if (verbose_jni) {
2845 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2846 }
2847 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002848 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002849
Brian Carlstromaded5f72011-10-07 17:15:04 -07002850 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002851
2852 if (version != JNI_VERSION_1_2 &&
2853 version != JNI_VERSION_1_4 &&
2854 version != JNI_VERSION_1_6) {
2855 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2856 << "bad version: " << version;
2857 // It's unwise to call dlclose() here, but we can mark it
2858 // as bad and ensure that future load attempts will fail.
2859 // We don't know how far JNI_OnLoad got, so there could
2860 // be some partially-initialized stuff accessible through
2861 // newly-registered native method calls. We could try to
2862 // unregister them, but that doesn't seem worthwhile.
2863 result = false;
2864 } else {
2865 if (verbose_jni) {
2866 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2867 << " from JNI_OnLoad in \"" << path << "\"]";
2868 }
2869 }
2870 }
2871
2872 library->SetResult(result);
2873 return result;
2874}
2875
2876void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2877 CHECK(m->IsNative());
2878
2879 Class* c = m->GetDeclaringClass();
2880
2881 // If this is a static method, it could be called before the class
2882 // has been initialized.
2883 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002884 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002885 return NULL;
2886 }
2887 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002888 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002889 }
2890
Brian Carlstrom16192862011-09-12 17:50:06 -07002891 std::string detail;
2892 void* native_method;
2893 {
2894 MutexLock mu(libraries_lock);
2895 native_method = libraries->FindNativeMethod(m, detail);
2896 }
2897 // throwing can cause libraries_lock to be reacquired
2898 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002899 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002900 }
2901 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002902}
2903
Elliott Hughes410c0c82011-09-01 17:58:25 -07002904void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2905 {
2906 MutexLock mu(globals_lock);
2907 globals.VisitRoots(visitor, arg);
2908 }
2909 {
2910 MutexLock mu(pins_lock);
2911 pin_table.VisitRoots(visitor, arg);
2912 }
2913 // The weak_globals table is visited by the GC itself (because it mutates the table).
2914}
2915
Ian Rogersdf20fe02011-07-20 20:34:16 -07002916} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002917
2918std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2919 switch (rhs) {
2920 case JNIInvalidRefType:
2921 os << "JNIInvalidRefType";
2922 return os;
2923 case JNILocalRefType:
2924 os << "JNILocalRefType";
2925 return os;
2926 case JNIGlobalRefType:
2927 os << "JNIGlobalRefType";
2928 return os;
2929 case JNIWeakGlobalRefType:
2930 os << "JNIWeakGlobalRefType";
2931 return os;
2932 }
2933}