blob: 5787c652b13bb51b466620aacb078a587dc52c72 [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070015#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070016#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070018#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070021#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070022#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070023#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070024#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070026
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070027namespace art {
28
Elliott Hughes2ced6a52011-10-16 18:44:48 -070029static const size_t kMonitorsInitial = 32; // Arbitrary.
30static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
31
32static const size_t kLocalsInitial = 64; // Arbitrary.
33static const size_t kLocalsMax = 512; // Arbitrary sanity check.
34
35static const size_t kPinTableInitial = 16; // Arbitrary.
36static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
37
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070038static size_t gGlobalsInitial = 512; // Arbitrary.
39static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070040
41static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
42static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
43
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070044void SetJniGlobalsMax(size_t max) {
45 if (max != 0) {
46 gGlobalsMax = max;
47 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
48 }
49}
Ian Rogersdf20fe02011-07-20 20:34:16 -070050
Elliott Hughesc5f7c912011-08-18 14:00:42 -070051/*
52 * Add a local reference for an object to the current stack frame. When
53 * the native function returns, the reference will be discarded.
54 *
55 * We need to allow the same reference to be added multiple times.
56 *
57 * This will be called on otherwise unreferenced objects. We cannot do
58 * GC allocations here, and it's best if we don't grab a mutex.
59 *
60 * Returns the local reference (currently just the same pointer that was
61 * passed in), or NULL on failure.
62 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070063template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070064T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
65 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
66 Object* obj = const_cast<Object*>(const_obj);
67
Elliott Hughesc5f7c912011-08-18 14:00:42 -070068 if (obj == NULL) {
69 return NULL;
70 }
71
Elliott Hughesbf86d042011-08-31 17:53:14 -070072 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
73 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070074
Ian Rogers5a7a74a2011-09-26 16:32:29 -070075 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070076 IndirectRef ref = locals.Add(cookie, obj);
77 if (ref == NULL) {
78 // TODO: just change Add's DCHECK to CHECK and lose this?
79 locals.Dump();
80 LOG(FATAL) << "Failed adding to JNI local reference table "
81 << "(has " << locals.Capacity() << " entries)";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070082 }
83
84#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070085 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070086 size_t entry_count = locals.Capacity();
87 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070088 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070089 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070090 locals.Dump();
Elliott Hughes82188472011-11-07 18:11:48 -080091 // TODO: LOG(FATAL) instead.
Elliott Hughesc5f7c912011-08-18 14:00:42 -070092 }
93 }
94#endif
95
Elliott Hughesbf86d042011-08-31 17:53:14 -070096 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070097 // Hand out direct pointers to support broken old apps.
98 return reinterpret_cast<T>(obj);
99 }
100
101 return reinterpret_cast<T>(ref);
102}
103
Elliott Hughesbf86d042011-08-31 17:53:14 -0700104// For external use.
105template<typename T>
106T Decode(JNIEnv* public_env, jobject obj) {
107 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
108 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
109}
110// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700111template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700112template Class* Decode<Class*>(JNIEnv*, jobject);
113template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
114template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700115template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700116template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700117template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700118template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400119template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700120template String* Decode<String*>(JNIEnv*, jobject);
121template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700122
123namespace {
124
Elliott Hughescdf53122011-08-19 15:46:09 -0700125jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
126 if (obj == NULL) {
127 return NULL;
128 }
Elliott Hughes75770752011-08-24 17:52:38 -0700129 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700130 IndirectReferenceTable& weak_globals = vm->weak_globals;
131 MutexLock mu(vm->weak_globals_lock);
132 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
133 return reinterpret_cast<jweak>(ref);
134}
135
Elliott Hughesbf86d042011-08-31 17:53:14 -0700136// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700137template<typename T>
138T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700139 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700140}
141
Elliott Hughes418d20f2011-09-22 14:00:39 -0700142byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
143 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700144 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700145 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700146 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700147 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
148 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700149 case 'Z':
150 case 'B':
151 case 'C':
152 case 'S':
153 case 'I':
154 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
155 offset += 4;
156 break;
157 case 'F':
158 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
159 offset += 4;
160 break;
161 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700162 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700163 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
164 offset += sizeof(Object*);
165 break;
166 }
167 case 'D':
168 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
169 offset += 8;
170 break;
171 case 'J':
172 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
173 offset += 8;
174 break;
175 }
176 }
177 return arg_array.release();
178}
179
Elliott Hughes418d20f2011-09-22 14:00:39 -0700180byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
181 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700182 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700183 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700184 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700185 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
186 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700187 case 'Z':
188 case 'B':
189 case 'C':
190 case 'S':
191 case 'I':
192 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
193 offset += 4;
194 break;
195 case 'F':
196 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
197 offset += 4;
198 break;
199 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700200 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700201 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
202 offset += sizeof(Object*);
203 break;
204 }
205 case 'D':
206 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
207 offset += 8;
208 break;
209 case 'J':
210 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
211 offset += 8;
212 break;
213 }
214 }
215 return arg_array.release();
216}
217
Elliott Hughes418d20f2011-09-22 14:00:39 -0700218JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
219 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700220 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700221 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700222 return result;
223}
224
Elliott Hughes418d20f2011-09-22 14:00:39 -0700225JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
226 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
227 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700228 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700229 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
230 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700231}
232
Elliott Hughes75770752011-08-24 17:52:38 -0700233Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700234 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700235}
236
Elliott Hughes418d20f2011-09-22 14:00:39 -0700237JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
238 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
239 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700240 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700241 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
242 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700243}
244
Elliott Hughes418d20f2011-09-22 14:00:39 -0700245JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
246 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
247 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700248 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700249 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
250 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700251}
252
Elliott Hughes6b436852011-08-12 10:16:44 -0700253// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
254// separated with slashes but aren't wrapped with "L;" like regular descriptors
255// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
256// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
257// supported names with dots too (such as "a.b.C").
258std::string NormalizeJniClassDescriptor(const char* name) {
259 std::string result;
260 // Add the missing "L;" if necessary.
261 if (name[0] == '[') {
262 result = name;
263 } else {
264 result += 'L';
265 result += name;
266 result += ';';
267 }
268 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700269 if (result.find('.') != std::string::npos) {
270 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
271 << "\"" << name << "\"";
272 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700273 }
274 return result;
275}
276
Elliott Hughes14134a12011-09-30 16:55:51 -0700277void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
278 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700279 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes14134a12011-09-30 16:55:51 -0700280 "no %s method \"%s.%s%s\"", kind, class_descriptor.c_str(), name, sig);
281}
282
Elliott Hughescdf53122011-08-19 15:46:09 -0700283jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
284 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700285 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700286 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700287 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700288
289 Method* method = NULL;
290 if (is_static) {
291 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700292 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700293 method = c->FindVirtualMethod(name, sig);
294 if (method == NULL) {
295 // No virtual method matching the signature. Search declared
296 // private methods and constructors.
297 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700298 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700299 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700300
Elliott Hughescdf53122011-08-19 15:46:09 -0700301 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700302 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700303 return NULL;
304 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700305
Elliott Hughes418d20f2011-09-22 14:00:39 -0700306 method->InitJavaFields();
307
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700308 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700309}
310
Brian Carlstrom00fae582011-10-28 01:16:28 -0700311const ClassLoader* GetClassLoader(Thread* self) {
312 Frame frame = self->GetTopOfStack();
313 Method* method = frame.GetMethod();
314 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
315 return self->GetClassLoaderOverride();
316 }
317 return method->GetDeclaringClass()->GetClassLoader();
318}
319
Elliott Hughescdf53122011-08-19 15:46:09 -0700320jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
321 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700322 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700323 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700324 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700325
326 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700327 Class* field_type;
328 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
329 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700330 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700331 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700332 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700333 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700334 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 if (field_type == NULL) {
336 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700337 DCHECK(ts.Self()->IsExceptionPending());
338 ts.Self()->ClearException();
339 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700340 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700341 "no type \"%s\" found and so no field \"%s\" could be found in class "
342 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343 return NULL;
344 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700345 std::string field_type_descriptor = field_type->GetDescriptor()->ToModifiedUtf8();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700346 if (is_static) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700347 field = c->FindStaticField(name, field_type_descriptor);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 } else {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700349 field = c->FindInstanceField(name, field_type_descriptor);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700350 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700351 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700352 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700353 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700354 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700355 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700356 return NULL;
357 }
Elliott Hughes80609252011-09-23 17:24:51 -0700358 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700359 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700360}
361
Elliott Hughes75770752011-08-24 17:52:38 -0700362void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
363 JavaVMExt* vm = ts.Vm();
364 MutexLock mu(vm->pins_lock);
365 vm->pin_table.Add(array);
366}
367
368void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
369 JavaVMExt* vm = ts.Vm();
370 MutexLock mu(vm->pins_lock);
371 vm->pin_table.Remove(array);
372}
373
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700374template<typename JniT, typename ArtT>
375JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
376 CHECK_GE(length, 0); // TODO: ReportJniError
377 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700378 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700379}
380
Elliott Hughes75770752011-08-24 17:52:38 -0700381template <typename ArrayT, typename CArrayT, typename ArtArrayT>
382CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
383 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
384 PinPrimitiveArray(ts, array);
385 if (is_copy != NULL) {
386 *is_copy = JNI_FALSE;
387 }
388 return array->GetData();
389}
390
391template <typename ArrayT>
392void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
393 if (mode != JNI_COMMIT) {
394 Array* array = Decode<Array*>(ts, java_array);
395 UnpinPrimitiveArray(ts, array);
396 }
397}
398
Elliott Hughes814e4032011-08-23 12:07:56 -0700399void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700400 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700401 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700402 "%s offset=%d length=%d %s.length=%d",
403 type.c_str(), start, length, identifier, array->GetLength());
404}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700405void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700406 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700407 "offset=%d length=%d string.length()=%d", start, length, array_length);
408}
Elliott Hughes814e4032011-08-23 12:07:56 -0700409
410template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700411void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700412 ArrayT* array = Decode<ArrayT*>(ts, java_array);
413 if (start < 0 || length < 0 || start + length > array->GetLength()) {
414 ThrowAIOOBE(ts, array, start, length, "src");
415 } else {
416 JavaT* data = array->GetData();
417 memcpy(buf, data + start, length * sizeof(JavaT));
418 }
419}
420
421template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700422void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700423 ArrayT* array = Decode<ArrayT*>(ts, java_array);
424 if (start < 0 || length < 0 || start + length > array->GetLength()) {
425 ThrowAIOOBE(ts, array, start, length, "dst");
426 } else {
427 JavaT* data = array->GetData();
428 memcpy(data + start, buf, length * sizeof(JavaT));
429 }
430}
431
Elliott Hughes75770752011-08-24 17:52:38 -0700432jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700433 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
434 CHECK(buffer_class.get() != NULL);
435 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
436}
437
Elliott Hughes75770752011-08-24 17:52:38 -0700438jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700439 static jclass buffer_class = InitDirectByteBufferClass(env);
440 return buffer_class;
441}
442
Elliott Hughes75770752011-08-24 17:52:38 -0700443jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
444 if (vm == NULL || p_env == NULL) {
445 return JNI_ERR;
446 }
447
Elliott Hughescac6cc72011-11-03 20:31:21 -0700448 // Return immediately if we're already one with the VM.
449 Thread* self = Thread::Current();
450 if (self != NULL) {
451 *p_env = self->GetJniEnv();
452 return JNI_OK;
453 }
454
455 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
456
457 // No threads allowed in zygote mode.
458 if (runtime->IsZygote()) {
459 LOG(ERROR) << "Attempt to attach a thread in the zygote";
460 return JNI_ERR;
461 }
462
Elliott Hughes75770752011-08-24 17:52:38 -0700463 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
464 JavaVMAttachArgs args;
465 if (thr_args == NULL) {
466 // Allow the v1.1 calling convention.
467 args.version = JNI_VERSION_1_2;
468 args.name = NULL;
469 args.group = NULL; // TODO: get "main" thread group
470 } else {
471 args.version = in_args->version;
472 args.name = in_args->name;
473 if (in_args->group != NULL) {
474 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
475 args.group = NULL; // TODO: decode in_args->group
476 } else {
477 args.group = NULL; // TODO: get "main" thread group
478 }
479 }
480 CHECK_GE(args.version, JNI_VERSION_1_2);
481
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700482 runtime->AttachCurrentThread(args.name, as_daemon);
483 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700484 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700485}
486
Elliott Hughes79082e32011-08-25 12:07:32 -0700487class SharedLibrary {
488 public:
489 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
490 : path_(path),
491 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700492 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700493 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700494 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700495 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700496 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 }
498
Elliott Hughes79082e32011-08-25 12:07:32 -0700499 Object* GetClassLoader() {
500 return class_loader_;
501 }
502
503 std::string GetPath() {
504 return path_;
505 }
506
507 /*
508 * Check the result of an earlier call to JNI_OnLoad on this library. If
509 * the call has not yet finished in another thread, wait for it.
510 */
511 bool CheckOnLoadResult(JavaVMExt* vm) {
512 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700513 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 // Check this so we don't end up waiting for ourselves. We need
515 // to return "true" so the caller can continue.
516 LOG(INFO) << *self << " recursive attempt to load library "
517 << "\"" << path_ << "\"";
518 return true;
519 }
520
521 MutexLock mu(jni_on_load_lock_);
522 while (jni_on_load_result_ == kPending) {
523 if (vm->verbose_jni) {
524 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
525 << "JNI_OnLoad...]";
526 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700527 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700528 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700529 }
530
531 bool okay = (jni_on_load_result_ == kOkay);
532 if (vm->verbose_jni) {
533 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
534 << (okay ? "succeeded" : "failed") << "]";
535 }
536 return okay;
537 }
538
539 void SetResult(bool result) {
540 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700541 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700542
543 // Broadcast a wakeup to anybody sleeping on the condition variable.
544 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700545 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 }
547
548 void* FindSymbol(const std::string& symbol_name) {
549 return dlsym(handle_, symbol_name.c_str());
550 }
551
552 private:
553 enum JNI_OnLoadState {
554 kPending,
555 kFailed,
556 kOkay,
557 };
558
559 // Path to library "/system/lib/libjni.so".
560 std::string path_;
561
562 // The void* returned by dlopen(3).
563 void* handle_;
564
565 // The ClassLoader this library is associated with.
566 Object* class_loader_;
567
568 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700569 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700570 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700571 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700572 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700573 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700574 // Result of earlier JNI_OnLoad call.
575 JNI_OnLoadState jni_on_load_result_;
576};
577
Elliott Hughescdf53122011-08-19 15:46:09 -0700578} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700579
Elliott Hughes79082e32011-08-25 12:07:32 -0700580// This exists mainly to keep implementation details out of the header file.
581class Libraries {
582 public:
583 Libraries() {
584 }
585
586 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700587 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700588 }
589
590 SharedLibrary* Get(const std::string& path) {
591 return libraries_[path];
592 }
593
594 void Put(const std::string& path, SharedLibrary* library) {
595 libraries_[path] = library;
596 }
597
598 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700599 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 std::string jni_short_name(JniShortName(m));
601 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700602 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700603 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
604 SharedLibrary* library = it->second;
605 if (library->GetClassLoader() != declaring_class_loader) {
606 // We only search libraries loaded by the appropriate ClassLoader.
607 continue;
608 }
609 // Try the short name then the long name...
610 void* fn = library->FindSymbol(jni_short_name);
611 if (fn == NULL) {
612 fn = library->FindSymbol(jni_long_name);
613 }
614 if (fn != NULL) {
615 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700616 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700617 << " in \"" << library->GetPath() << "\"]";
618 }
619 return fn;
620 }
621 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700622 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700623 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700624 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700625 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700626 return NULL;
627 }
628
629 private:
630 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
631
632 std::map<std::string, SharedLibrary*> libraries_;
633};
634
Elliott Hughes418d20f2011-09-22 14:00:39 -0700635JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
636 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
637 Object* receiver = Decode<Object*>(env, obj);
638 Method* method = DecodeMethod(mid);
639 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
640 return InvokeWithArgArray(env, receiver, method, arg_array.get());
641}
642
Elliott Hughescdf53122011-08-19 15:46:09 -0700643class JNI {
644 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700645
Elliott Hughescdf53122011-08-19 15:46:09 -0700646 static jint GetVersion(JNIEnv* env) {
647 ScopedJniThreadState ts(env);
648 return JNI_VERSION_1_6;
649 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700650
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
652 ScopedJniThreadState ts(env);
653 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700654 return NULL;
655 }
656
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 static jclass FindClass(JNIEnv* env, const char* name) {
658 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700659 Runtime* runtime = Runtime::Current();
660 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700662 Class* c = NULL;
663 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700664 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700665 c = class_linker->FindClass(descriptor, cl);
666 } else {
667 c = class_linker->FindSystemClass(descriptor);
668 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700669 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700670 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700671
Elliott Hughescdf53122011-08-19 15:46:09 -0700672 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
673 ScopedJniThreadState ts(env);
674 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700675 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700676 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700677
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
679 ScopedJniThreadState ts(env);
680 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700681 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700683
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
685 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700686 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700687 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700688 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700689
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
691 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700692 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700693 return AddLocalReference<jobject>(env, field);
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 jclass GetObjectClass(JNIEnv* env, jobject java_object) {
697 ScopedJniThreadState ts(env);
698 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700699 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700700 }
701
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700702 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700704 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700705 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700706 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700707
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700710 Class* c1 = Decode<Class*>(ts, java_class1);
711 Class* c2 = Decode<Class*>(ts, java_class2);
712 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700713 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700714
Elliott Hughes37f7a402011-08-22 18:56:01 -0700715 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700716 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700717 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700718 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700719 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700720 return JNI_TRUE;
721 } else {
722 Object* obj = Decode<Object*>(ts, jobj);
723 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700724 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700725 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700727
Elliott Hughes37f7a402011-08-22 18:56:01 -0700728 static jint Throw(JNIEnv* env, jthrowable java_exception) {
729 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700730 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700731 if (exception == NULL) {
732 return JNI_ERR;
733 }
734 ts.Self()->SetException(exception);
735 return JNI_OK;
736 }
737
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700738 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700739 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700740 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700741 jmethodID mid = ((msg != NULL)
742 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
743 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700744 if (mid == NULL) {
745 return JNI_ERR;
746 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700747 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700748 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700749 return JNI_ERR;
750 }
751
752 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700753 args[0].l = s.get();
754 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
755 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700756 return JNI_ERR;
757 }
758
Elliott Hughes72025e52011-08-23 17:50:30 -0700759 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700760
Elliott Hughes37f7a402011-08-22 18:56:01 -0700761 return JNI_OK;
762 }
763
764 static jboolean ExceptionCheck(JNIEnv* env) {
765 ScopedJniThreadState ts(env);
766 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
767 }
768
769 static void ExceptionClear(JNIEnv* env) {
770 ScopedJniThreadState ts(env);
771 ts.Self()->ClearException();
772 }
773
774 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700776
777 Thread* self = ts.Self();
778 Throwable* original_exception = self->GetException();
779 self->ClearException();
780
Elliott Hughesbf86d042011-08-31 17:53:14 -0700781 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700782 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
783 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
784 if (mid == NULL) {
785 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700786 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700787 } else {
788 env->CallVoidMethod(exception.get(), mid);
789 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700790 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700791 << " thrown while calling printStackTrace";
792 self->ClearException();
793 }
794 }
795
796 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700798
Elliott Hughescdf53122011-08-19 15:46:09 -0700799 static jthrowable ExceptionOccurred(JNIEnv* env) {
800 ScopedJniThreadState ts(env);
801 Object* exception = ts.Self()->GetException();
802 if (exception == NULL) {
803 return NULL;
804 } else {
805 // TODO: if adding a local reference failing causes the VM to abort
806 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700807 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700808 if (localException == NULL) {
809 // We were unable to add a new local reference, and threw a new
810 // exception. We can't return "exception", because it's not a
811 // local reference. So we have to return NULL, indicating that
812 // there was no exception, even though it's pretty much raining
813 // exceptions in here.
814 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
815 }
816 return localException;
817 }
818 }
819
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 static void FatalError(JNIEnv* env, const char* msg) {
821 ScopedJniThreadState ts(env);
822 LOG(FATAL) << "JNI FatalError called: " << msg;
823 }
824
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700825 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700826 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700827 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
828 return JNI_ERR;
829 }
830 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 return JNI_OK;
832 }
833
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700834 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700836 Object* survivor = Decode<Object*>(ts, java_survivor);
837 ts.Env()->PopFrame();
838 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 }
840
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700841 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700842 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700843 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
844 }
845
846 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
847 // TODO: we should try to expand the table if necessary.
848 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
849 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
850 return JNI_ERR;
851 }
852 // TODO: this isn't quite right, since "capacity" includes holes.
853 size_t capacity = ts.Env()->locals.Capacity();
854 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
855 if (!okay) {
856 ts.Self()->ThrowOutOfMemoryError(caller);
857 }
858 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700859 }
860
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
862 ScopedJniThreadState ts(env);
863 if (obj == NULL) {
864 return NULL;
865 }
866
Elliott Hughes75770752011-08-24 17:52:38 -0700867 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 IndirectReferenceTable& globals = vm->globals;
869 MutexLock mu(vm->globals_lock);
870 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
871 return reinterpret_cast<jobject>(ref);
872 }
873
874 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
875 ScopedJniThreadState ts(env);
876 if (obj == NULL) {
877 return;
878 }
879
Elliott Hughes75770752011-08-24 17:52:38 -0700880 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700881 IndirectReferenceTable& globals = vm->globals;
882 MutexLock mu(vm->globals_lock);
883
884 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
885 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
886 << "failed to find entry";
887 }
888 }
889
890 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
891 ScopedJniThreadState ts(env);
892 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
893 }
894
895 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
896 ScopedJniThreadState ts(env);
897 if (obj == NULL) {
898 return;
899 }
900
Elliott Hughes75770752011-08-24 17:52:38 -0700901 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 IndirectReferenceTable& weak_globals = vm->weak_globals;
903 MutexLock mu(vm->weak_globals_lock);
904
905 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
906 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
907 << "failed to find entry";
908 }
909 }
910
911 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
912 ScopedJniThreadState ts(env);
913 if (obj == NULL) {
914 return NULL;
915 }
916
917 IndirectReferenceTable& locals = ts.Env()->locals;
918
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700919 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
921 return reinterpret_cast<jobject>(ref);
922 }
923
924 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
925 ScopedJniThreadState ts(env);
926 if (obj == NULL) {
927 return;
928 }
929
930 IndirectReferenceTable& locals = ts.Env()->locals;
931
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700932 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 if (!locals.Remove(cookie, obj)) {
934 // Attempting to delete a local reference that is not in the
935 // topmost local reference frame is a no-op. DeleteLocalRef returns
936 // void and doesn't throw any exceptions, but we should probably
937 // complain about it so the user will notice that things aren't
938 // going quite the way they expect.
939 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
940 << "failed to find entry";
941 }
942 }
943
944 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
945 ScopedJniThreadState ts(env);
946 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
947 ? JNI_TRUE : JNI_FALSE;
948 }
949
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700950 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700952 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700953 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700954 return NULL;
955 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700956 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 }
958
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 ScopedJniThreadState ts(env);
961 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 va_start(args, mid);
963 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 va_end(args);
965 return result;
966 }
967
Elliott Hughes72025e52011-08-23 17:50:30 -0700968 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700970 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700971 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700972 return NULL;
973 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700974 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700975 if (result == NULL) {
976 return NULL;
977 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700978 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700980 if (!ts.Self()->IsExceptionPending()) {
981 return local_result;
982 } else {
983 return NULL;
984 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 }
986
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700989 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700990 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700991 return NULL;
992 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700993 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700994 if (result == NULL) {
995 return NULL;
996 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700997 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700999 if (!ts.Self()->IsExceptionPending()) {
1000 return local_result;
1001 } else {
1002 return NULL;
1003 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 }
1005
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1007 ScopedJniThreadState ts(env);
1008 return FindMethodID(ts, c, name, sig, false);
1009 }
1010
1011 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1012 ScopedJniThreadState ts(env);
1013 return FindMethodID(ts, c, name, sig, true);
1014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_list ap;
1019 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001020 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 va_end(ap);
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 jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001027 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001028 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001033 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001034 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 va_list ap;
1040 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001041 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 va_end(ap);
1043 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001048 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 }
1050
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001053 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 va_list ap;
1059 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001060 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 va_end(ap);
1062 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 }
1064
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001067 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001072 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 va_list ap;
1078 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001079 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 va_end(ap);
1081 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 }
1083
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001086 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001091 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 va_list ap;
1097 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001098 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 va_end(ap);
1100 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001105 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001110 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_list ap;
1116 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001117 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 va_end(ap);
1119 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001124 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001129 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 va_list ap;
1135 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001136 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 va_end(ap);
1138 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001143 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001148 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 va_list ap;
1154 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001155 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 va_end(ap);
1157 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001162 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001167 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 va_list ap;
1173 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001174 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 va_end(ap);
1176 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001181 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001186 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 va_list ap;
1192 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001193 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001199 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001204 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
1207 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 ScopedJniThreadState ts(env);
1210 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001212 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001213 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 va_end(ap);
1215 return local_result;
1216 }
1217
1218 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001221 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001222 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 }
1224
1225 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001228 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001229 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 }
1231
1232 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 ScopedJniThreadState ts(env);
1235 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001237 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 va_end(ap);
1239 return result.z;
1240 }
1241
1242 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001245 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
1248 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001251 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
1254 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 ScopedJniThreadState ts(env);
1257 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001258 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001259 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 va_end(ap);
1261 return result.b;
1262 }
1263
1264 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001267 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
1270 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001273 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 }
1275
1276 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 ScopedJniThreadState ts(env);
1279 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001280 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001281 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 va_end(ap);
1283 return result.c;
1284 }
1285
1286 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001289 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
1292 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001295 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 }
1297
1298 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 ScopedJniThreadState ts(env);
1301 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001302 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001303 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 va_end(ap);
1305 return result.s;
1306 }
1307
1308 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001311 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
1314 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001317 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
Elliott Hughes418d20f2011-09-22 14:00:39 -07001320 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 ScopedJniThreadState ts(env);
1322 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001324 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 va_end(ap);
1326 return result.i;
1327 }
1328
1329 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001330 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001332 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
1335 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001336 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001338 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
1341 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001342 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 ScopedJniThreadState ts(env);
1344 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001346 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 va_end(ap);
1348 return result.j;
1349 }
1350
1351 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001352 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001354 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
1357 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001358 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001360 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
1363 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001364 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 ScopedJniThreadState ts(env);
1366 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001367 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001368 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 va_end(ap);
1370 return result.f;
1371 }
1372
1373 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001374 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001376 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
1379 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001380 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001382 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
1384
1385 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001386 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 ScopedJniThreadState ts(env);
1388 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001389 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001390 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 va_end(ap);
1392 return result.d;
1393 }
1394
1395 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001396 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001398 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
1401 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001402 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001404 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
1407 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001408 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 ScopedJniThreadState ts(env);
1410 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001411 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001412 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 va_end(ap);
1414 }
1415
1416 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001417 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001419 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
1422 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001423 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001425 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001428 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 ScopedJniThreadState ts(env);
1430 return FindFieldID(ts, c, name, sig, false);
1431 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001432
1433
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001434 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 ScopedJniThreadState ts(env);
1436 return FindFieldID(ts, c, name, sig, true);
1437 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001441 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001442 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001443 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001445
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001448 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001449 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 Object* o = Decode<Object*>(ts, java_object);
1455 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001456 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001457 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001463 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467#define GET_PRIMITIVE_FIELD(fn, instance) \
1468 ScopedJniThreadState ts(env); \
1469 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001470 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 return f->fn(o)
1472
1473#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1474 ScopedJniThreadState ts(env); \
1475 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001476 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001477 f->fn(o, value)
1478
1479 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1480 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001483 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1484 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 }
1486
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001487 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1488 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 }
1490
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1492 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 }
1494
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1496 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
1498
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1500 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1504 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 }
1506
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001507 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1508 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
1510
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1512 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 }
1514
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001515 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1516 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
1518
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1520 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 }
1522
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001523 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1524 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
1526
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001527 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1528 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
1530
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001531 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1532 GET_PRIMITIVE_FIELD(GetLong, NULL);
1533 }
1534
1535 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1536 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1537 }
1538
1539 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1540 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1541 }
1542
1543 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1544 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1545 }
1546
1547 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1548 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1549 }
1550
1551 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1552 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1553 }
1554
1555 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1556 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1557 }
1558
1559 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1560 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1561 }
1562
1563 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1564 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1565 }
1566
1567 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1568 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1569 }
1570
1571 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1572 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1573 }
1574
1575 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1576 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1577 }
1578
1579 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1580 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1581 }
1582
1583 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1584 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1585 }
1586
1587 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1588 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1589 }
1590
1591 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1592 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1593 }
1594
1595 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1596 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1597 }
1598
1599 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1600 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1601 }
1602
1603 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1604 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes418d20f2011-09-22 14:00:39 -07001607 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 ScopedJniThreadState ts(env);
1609 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001610 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001611 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001612 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 va_end(ap);
1614 return local_result;
1615 }
1616
Elliott Hughes418d20f2011-09-22 14:00:39 -07001617 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001619 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001620 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 }
1622
Elliott Hughes418d20f2011-09-22 14:00:39 -07001623 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001625 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001626 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
Elliott Hughes418d20f2011-09-22 14:00:39 -07001629 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 ScopedJniThreadState ts(env);
1631 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001632 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001633 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 va_end(ap);
1635 return result.z;
1636 }
1637
Elliott Hughes418d20f2011-09-22 14:00:39 -07001638 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001640 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 }
1642
Elliott Hughes418d20f2011-09-22 14:00:39 -07001643 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001645 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 }
1647
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 ScopedJniThreadState ts(env);
1650 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001652 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 va_end(ap);
1654 return result.b;
1655 }
1656
Elliott Hughes418d20f2011-09-22 14:00:39 -07001657 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
Elliott Hughes418d20f2011-09-22 14:00:39 -07001662 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001664 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 ScopedJniThreadState ts(env);
1669 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001671 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 va_end(ap);
1673 return result.c;
1674 }
1675
Elliott Hughes418d20f2011-09-22 14:00:39 -07001676 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001678 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 }
1680
Elliott Hughes418d20f2011-09-22 14:00:39 -07001681 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001683 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 ScopedJniThreadState ts(env);
1688 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001690 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 va_end(ap);
1692 return result.s;
1693 }
1694
Elliott Hughes418d20f2011-09-22 14:00:39 -07001695 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 }
1699
Elliott Hughes418d20f2011-09-22 14:00:39 -07001700 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001702 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 }
1704
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
1707 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001709 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 va_end(ap);
1711 return result.i;
1712 }
1713
Elliott Hughes418d20f2011-09-22 14:00:39 -07001714 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes418d20f2011-09-22 14:00:39 -07001719 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001721 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 }
1723
Elliott Hughes72025e52011-08-23 17:50:30 -07001724 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 ScopedJniThreadState ts(env);
1726 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001727 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001728 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 va_end(ap);
1730 return result.j;
1731 }
1732
Elliott Hughes418d20f2011-09-22 14:00:39 -07001733 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001735 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes418d20f2011-09-22 14:00:39 -07001738 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001740 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 ScopedJniThreadState ts(env);
1745 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001746 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001747 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 va_end(ap);
1749 return result.f;
1750 }
1751
Elliott Hughes418d20f2011-09-22 14:00:39 -07001752 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001754 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes418d20f2011-09-22 14:00:39 -07001757 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001759 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes72025e52011-08-23 17:50:30 -07001762 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 ScopedJniThreadState ts(env);
1764 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001765 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001766 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 va_end(ap);
1768 return result.d;
1769 }
1770
Elliott Hughes418d20f2011-09-22 14:00:39 -07001771 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001773 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes418d20f2011-09-22 14:00:39 -07001776 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001778 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes72025e52011-08-23 17:50:30 -07001781 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 ScopedJniThreadState ts(env);
1783 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001784 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001785 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 va_end(ap);
1787 }
1788
Elliott Hughes418d20f2011-09-22 14:00:39 -07001789 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001791 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 }
1793
Elliott Hughes418d20f2011-09-22 14:00:39 -07001794 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001796 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Elliott Hughes814e4032011-08-23 12:07:56 -07001799 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001801 if (chars == NULL && char_count == 0) {
1802 return NULL;
1803 }
1804 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001805 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 }
1807
1808 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1809 ScopedJniThreadState ts(env);
1810 if (utf == NULL) {
1811 return NULL;
1812 }
1813 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001814 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
Elliott Hughes814e4032011-08-23 12:07:56 -07001817 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1818 ScopedJniThreadState ts(env);
1819 return Decode<String*>(ts, java_string)->GetLength();
1820 }
1821
1822 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1823 ScopedJniThreadState ts(env);
1824 return Decode<String*>(ts, java_string)->GetUtfLength();
1825 }
1826
Elliott Hughesb465ab02011-08-24 11:21:21 -07001827 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001829 String* s = Decode<String*>(ts, java_string);
1830 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1831 ThrowSIOOBE(ts, start, length, s->GetLength());
1832 } else {
1833 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1834 memcpy(buf, chars + start, length * sizeof(jchar));
1835 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001836 }
1837
Elliott Hughesb465ab02011-08-24 11:21:21 -07001838 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001839 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001840 String* s = Decode<String*>(ts, java_string);
1841 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1842 ThrowSIOOBE(ts, start, length, s->GetLength());
1843 } else {
1844 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1845 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1846 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001847 }
1848
Elliott Hughes75770752011-08-24 17:52:38 -07001849 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001850 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001851 String* s = Decode<String*>(ts, java_string);
1852 const CharArray* chars = s->GetCharArray();
1853 PinPrimitiveArray(ts, chars);
1854 if (is_copy != NULL) {
1855 *is_copy = JNI_FALSE;
1856 }
1857 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001858 }
1859
Elliott Hughes75770752011-08-24 17:52:38 -07001860 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001861 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001862 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
Elliott Hughes75770752011-08-24 17:52:38 -07001865 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001867 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 }
1869
Elliott Hughes75770752011-08-24 17:52:38 -07001870 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001872 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 }
1874
Elliott Hughes75770752011-08-24 17:52:38 -07001875 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001876 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001877 if (java_string == NULL) {
1878 return NULL;
1879 }
1880 if (is_copy != NULL) {
1881 *is_copy = JNI_TRUE;
1882 }
1883 String* s = Decode<String*>(ts, java_string);
1884 size_t byte_count = s->GetUtfLength();
1885 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001886 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001887 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1888 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1889 bytes[byte_count] = '\0';
1890 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001891 }
1892
Elliott Hughes75770752011-08-24 17:52:38 -07001893 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001894 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001895 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001896 }
1897
Elliott Hughesbd935992011-08-22 11:59:34 -07001898 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001899 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001900 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001901 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001902 Array* array = obj->AsArray();
1903 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 }
1905
Elliott Hughes814e4032011-08-23 12:07:56 -07001906 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001908 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001909 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 }
1911
1912 static void SetObjectArrayElement(JNIEnv* env,
1913 jobjectArray java_array, jsize index, jobject java_value) {
1914 ScopedJniThreadState ts(env);
1915 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1916 Object* value = Decode<Object*>(ts, java_value);
1917 array->Set(index, value);
1918 }
1919
1920 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1921 ScopedJniThreadState ts(env);
1922 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1923 }
1924
1925 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1926 ScopedJniThreadState ts(env);
1927 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1928 }
1929
1930 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1931 ScopedJniThreadState ts(env);
1932 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1933 }
1934
1935 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1936 ScopedJniThreadState ts(env);
1937 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1938 }
1939
1940 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1941 ScopedJniThreadState ts(env);
1942 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1943 }
1944
1945 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1946 ScopedJniThreadState ts(env);
1947 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1948 }
1949
1950 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1951 ScopedJniThreadState ts(env);
1952 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1953 }
1954
1955 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1956 ScopedJniThreadState ts(env);
1957 CHECK_GE(length, 0); // TODO: ReportJniError
1958
1959 // Compute the array class corresponding to the given element class.
1960 Class* element_class = Decode<Class*>(ts, element_jclass);
1961 std::string descriptor;
1962 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001963 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001964
1965 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001966 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1967 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 return NULL;
1969 }
1970
Elliott Hughes75770752011-08-24 17:52:38 -07001971 // Allocate and initialize if necessary.
1972 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001974 if (initial_element != NULL) {
1975 Object* initial_object = Decode<Object*>(ts, initial_element);
1976 for (jsize i = 0; i < length; ++i) {
1977 result->Set(i, initial_object);
1978 }
1979 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001980 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
1983 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1984 ScopedJniThreadState ts(env);
1985 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1986 }
1987
Elliott Hughes75770752011-08-24 17:52:38 -07001988 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001989 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001990 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001994 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001995 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001996 }
1997
Elliott Hughes75770752011-08-24 17:52:38 -07001998 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002000 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes75770752011-08-24 17:52:38 -07002003 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002005 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002010 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002015 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes75770752011-08-24 17:52:38 -07002018 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002020 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes75770752011-08-24 17:52:38 -07002023 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002025 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes75770752011-08-24 17:52:38 -07002028 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002030 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes75770752011-08-24 17:52:38 -07002033 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002035 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes75770752011-08-24 17:52:38 -07002038 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002040 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes75770752011-08-24 17:52:38 -07002043 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002045 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes75770752011-08-24 17:52:38 -07002048 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002050 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes75770752011-08-24 17:52:38 -07002053 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002055 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes75770752011-08-24 17:52:38 -07002058 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002060 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes75770752011-08-24 17:52:38 -07002063 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002065 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes75770752011-08-24 17:52:38 -07002068 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002070 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes75770752011-08-24 17:52:38 -07002073 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002075 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002130 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes814e4032011-08-23 12:07:56 -07002138 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Elliott Hughes814e4032011-08-23 12:07:56 -07002143 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes814e4032011-08-23 12:07:56 -07002148 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Elliott Hughes814e4032011-08-23 12:07:56 -07002153 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002155 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002160 Class* c = Decode<Class*>(ts, java_class);
2161
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 const char* name = methods[i].name;
2164 const char* sig = methods[i].signature;
2165
2166 if (*sig == '!') {
2167 // TODO: fast jni. it's too noisy to log all these.
2168 ++sig;
2169 }
2170
Elliott Hughes5174fe62011-08-23 15:12:35 -07002171 Method* m = c->FindDirectMethod(name, sig);
2172 if (m == NULL) {
2173 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002175 if (m == NULL) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002176 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002178 } else if (!m->IsNative()) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002179 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 return JNI_ERR;
2181 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002182
Elliott Hughes75770752011-08-24 17:52:38 -07002183 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002184 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002185 }
2186
2187 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 }
2189 return JNI_OK;
2190 }
2191
Elliott Hughes5174fe62011-08-23 15:12:35 -07002192 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002194 Class* c = Decode<Class*>(ts, java_class);
2195
Elliott Hughes75770752011-08-24 17:52:38 -07002196 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002197 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002198 }
2199
2200 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2201 Method* m = c->GetDirectMethod(i);
2202 if (m->IsNative()) {
2203 m->UnregisterNative();
2204 }
2205 }
2206 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2207 Method* m = c->GetVirtualMethod(i);
2208 if (m->IsNative()) {
2209 m->UnregisterNative();
2210 }
2211 }
2212
2213 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 }
2215
Elliott Hughes72025e52011-08-23 17:50:30 -07002216 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002218 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002219 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 }
2221
Elliott Hughes72025e52011-08-23 17:50:30 -07002222 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002224 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002225 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227
2228 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2229 ScopedJniThreadState ts(env);
2230 Runtime* runtime = Runtime::Current();
2231 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002232 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 } else {
2234 *vm = NULL;
2235 }
2236 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2237 }
2238
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2240 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002241
2242 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002243 CHECK(address != NULL); // TODO: ReportJniError
2244 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002245
2246 jclass buffer_class = GetDirectByteBufferClass(env);
2247 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2248 if (mid == NULL) {
2249 return NULL;
2250 }
2251
2252 // At the moment, the Java side is limited to 32 bits.
2253 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2254 CHECK_LE(capacity, 0xffffffff);
2255 jint address_arg = reinterpret_cast<jint>(address);
2256 jint capacity_arg = static_cast<jint>(capacity);
2257
2258 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2259 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002260 }
2261
Elliott Hughesb465ab02011-08-24 11:21:21 -07002262 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002264 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2265 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002266 }
2267
Elliott Hughesb465ab02011-08-24 11:21:21 -07002268 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002270 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2271 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273
Elliott Hughesb465ab02011-08-24 11:21:21 -07002274 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002276
Elliott Hughes75770752011-08-24 17:52:38 -07002277 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002278
2279 // Do we definitely know what kind of reference this is?
2280 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2281 IndirectRefKind kind = GetIndirectRefKind(ref);
2282 switch (kind) {
2283 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002284 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2285 return JNILocalRefType;
2286 }
2287 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002288 case kGlobal:
2289 return JNIGlobalRefType;
2290 case kWeakGlobal:
2291 return JNIWeakGlobalRefType;
2292 case kSirtOrInvalid:
2293 // Is it in a stack IRT?
2294 if (ts.Self()->SirtContains(java_object)) {
2295 return JNILocalRefType;
2296 }
2297
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002298 if (!ts.Env()->work_around_app_jni_bugs) {
2299 return JNIInvalidRefType;
2300 }
2301
Elliott Hughesb465ab02011-08-24 11:21:21 -07002302 // If we're handing out direct pointers, check whether it's a direct pointer
2303 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002304 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002305 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002306 return JNILocalRefType;
2307 }
2308 }
2309
2310 return JNIInvalidRefType;
2311 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002312 }
2313};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002314
Elliott Hughesa2501992011-08-26 19:39:54 -07002315const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002316 NULL, // reserved0.
2317 NULL, // reserved1.
2318 NULL, // reserved2.
2319 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002320 JNI::GetVersion,
2321 JNI::DefineClass,
2322 JNI::FindClass,
2323 JNI::FromReflectedMethod,
2324 JNI::FromReflectedField,
2325 JNI::ToReflectedMethod,
2326 JNI::GetSuperclass,
2327 JNI::IsAssignableFrom,
2328 JNI::ToReflectedField,
2329 JNI::Throw,
2330 JNI::ThrowNew,
2331 JNI::ExceptionOccurred,
2332 JNI::ExceptionDescribe,
2333 JNI::ExceptionClear,
2334 JNI::FatalError,
2335 JNI::PushLocalFrame,
2336 JNI::PopLocalFrame,
2337 JNI::NewGlobalRef,
2338 JNI::DeleteGlobalRef,
2339 JNI::DeleteLocalRef,
2340 JNI::IsSameObject,
2341 JNI::NewLocalRef,
2342 JNI::EnsureLocalCapacity,
2343 JNI::AllocObject,
2344 JNI::NewObject,
2345 JNI::NewObjectV,
2346 JNI::NewObjectA,
2347 JNI::GetObjectClass,
2348 JNI::IsInstanceOf,
2349 JNI::GetMethodID,
2350 JNI::CallObjectMethod,
2351 JNI::CallObjectMethodV,
2352 JNI::CallObjectMethodA,
2353 JNI::CallBooleanMethod,
2354 JNI::CallBooleanMethodV,
2355 JNI::CallBooleanMethodA,
2356 JNI::CallByteMethod,
2357 JNI::CallByteMethodV,
2358 JNI::CallByteMethodA,
2359 JNI::CallCharMethod,
2360 JNI::CallCharMethodV,
2361 JNI::CallCharMethodA,
2362 JNI::CallShortMethod,
2363 JNI::CallShortMethodV,
2364 JNI::CallShortMethodA,
2365 JNI::CallIntMethod,
2366 JNI::CallIntMethodV,
2367 JNI::CallIntMethodA,
2368 JNI::CallLongMethod,
2369 JNI::CallLongMethodV,
2370 JNI::CallLongMethodA,
2371 JNI::CallFloatMethod,
2372 JNI::CallFloatMethodV,
2373 JNI::CallFloatMethodA,
2374 JNI::CallDoubleMethod,
2375 JNI::CallDoubleMethodV,
2376 JNI::CallDoubleMethodA,
2377 JNI::CallVoidMethod,
2378 JNI::CallVoidMethodV,
2379 JNI::CallVoidMethodA,
2380 JNI::CallNonvirtualObjectMethod,
2381 JNI::CallNonvirtualObjectMethodV,
2382 JNI::CallNonvirtualObjectMethodA,
2383 JNI::CallNonvirtualBooleanMethod,
2384 JNI::CallNonvirtualBooleanMethodV,
2385 JNI::CallNonvirtualBooleanMethodA,
2386 JNI::CallNonvirtualByteMethod,
2387 JNI::CallNonvirtualByteMethodV,
2388 JNI::CallNonvirtualByteMethodA,
2389 JNI::CallNonvirtualCharMethod,
2390 JNI::CallNonvirtualCharMethodV,
2391 JNI::CallNonvirtualCharMethodA,
2392 JNI::CallNonvirtualShortMethod,
2393 JNI::CallNonvirtualShortMethodV,
2394 JNI::CallNonvirtualShortMethodA,
2395 JNI::CallNonvirtualIntMethod,
2396 JNI::CallNonvirtualIntMethodV,
2397 JNI::CallNonvirtualIntMethodA,
2398 JNI::CallNonvirtualLongMethod,
2399 JNI::CallNonvirtualLongMethodV,
2400 JNI::CallNonvirtualLongMethodA,
2401 JNI::CallNonvirtualFloatMethod,
2402 JNI::CallNonvirtualFloatMethodV,
2403 JNI::CallNonvirtualFloatMethodA,
2404 JNI::CallNonvirtualDoubleMethod,
2405 JNI::CallNonvirtualDoubleMethodV,
2406 JNI::CallNonvirtualDoubleMethodA,
2407 JNI::CallNonvirtualVoidMethod,
2408 JNI::CallNonvirtualVoidMethodV,
2409 JNI::CallNonvirtualVoidMethodA,
2410 JNI::GetFieldID,
2411 JNI::GetObjectField,
2412 JNI::GetBooleanField,
2413 JNI::GetByteField,
2414 JNI::GetCharField,
2415 JNI::GetShortField,
2416 JNI::GetIntField,
2417 JNI::GetLongField,
2418 JNI::GetFloatField,
2419 JNI::GetDoubleField,
2420 JNI::SetObjectField,
2421 JNI::SetBooleanField,
2422 JNI::SetByteField,
2423 JNI::SetCharField,
2424 JNI::SetShortField,
2425 JNI::SetIntField,
2426 JNI::SetLongField,
2427 JNI::SetFloatField,
2428 JNI::SetDoubleField,
2429 JNI::GetStaticMethodID,
2430 JNI::CallStaticObjectMethod,
2431 JNI::CallStaticObjectMethodV,
2432 JNI::CallStaticObjectMethodA,
2433 JNI::CallStaticBooleanMethod,
2434 JNI::CallStaticBooleanMethodV,
2435 JNI::CallStaticBooleanMethodA,
2436 JNI::CallStaticByteMethod,
2437 JNI::CallStaticByteMethodV,
2438 JNI::CallStaticByteMethodA,
2439 JNI::CallStaticCharMethod,
2440 JNI::CallStaticCharMethodV,
2441 JNI::CallStaticCharMethodA,
2442 JNI::CallStaticShortMethod,
2443 JNI::CallStaticShortMethodV,
2444 JNI::CallStaticShortMethodA,
2445 JNI::CallStaticIntMethod,
2446 JNI::CallStaticIntMethodV,
2447 JNI::CallStaticIntMethodA,
2448 JNI::CallStaticLongMethod,
2449 JNI::CallStaticLongMethodV,
2450 JNI::CallStaticLongMethodA,
2451 JNI::CallStaticFloatMethod,
2452 JNI::CallStaticFloatMethodV,
2453 JNI::CallStaticFloatMethodA,
2454 JNI::CallStaticDoubleMethod,
2455 JNI::CallStaticDoubleMethodV,
2456 JNI::CallStaticDoubleMethodA,
2457 JNI::CallStaticVoidMethod,
2458 JNI::CallStaticVoidMethodV,
2459 JNI::CallStaticVoidMethodA,
2460 JNI::GetStaticFieldID,
2461 JNI::GetStaticObjectField,
2462 JNI::GetStaticBooleanField,
2463 JNI::GetStaticByteField,
2464 JNI::GetStaticCharField,
2465 JNI::GetStaticShortField,
2466 JNI::GetStaticIntField,
2467 JNI::GetStaticLongField,
2468 JNI::GetStaticFloatField,
2469 JNI::GetStaticDoubleField,
2470 JNI::SetStaticObjectField,
2471 JNI::SetStaticBooleanField,
2472 JNI::SetStaticByteField,
2473 JNI::SetStaticCharField,
2474 JNI::SetStaticShortField,
2475 JNI::SetStaticIntField,
2476 JNI::SetStaticLongField,
2477 JNI::SetStaticFloatField,
2478 JNI::SetStaticDoubleField,
2479 JNI::NewString,
2480 JNI::GetStringLength,
2481 JNI::GetStringChars,
2482 JNI::ReleaseStringChars,
2483 JNI::NewStringUTF,
2484 JNI::GetStringUTFLength,
2485 JNI::GetStringUTFChars,
2486 JNI::ReleaseStringUTFChars,
2487 JNI::GetArrayLength,
2488 JNI::NewObjectArray,
2489 JNI::GetObjectArrayElement,
2490 JNI::SetObjectArrayElement,
2491 JNI::NewBooleanArray,
2492 JNI::NewByteArray,
2493 JNI::NewCharArray,
2494 JNI::NewShortArray,
2495 JNI::NewIntArray,
2496 JNI::NewLongArray,
2497 JNI::NewFloatArray,
2498 JNI::NewDoubleArray,
2499 JNI::GetBooleanArrayElements,
2500 JNI::GetByteArrayElements,
2501 JNI::GetCharArrayElements,
2502 JNI::GetShortArrayElements,
2503 JNI::GetIntArrayElements,
2504 JNI::GetLongArrayElements,
2505 JNI::GetFloatArrayElements,
2506 JNI::GetDoubleArrayElements,
2507 JNI::ReleaseBooleanArrayElements,
2508 JNI::ReleaseByteArrayElements,
2509 JNI::ReleaseCharArrayElements,
2510 JNI::ReleaseShortArrayElements,
2511 JNI::ReleaseIntArrayElements,
2512 JNI::ReleaseLongArrayElements,
2513 JNI::ReleaseFloatArrayElements,
2514 JNI::ReleaseDoubleArrayElements,
2515 JNI::GetBooleanArrayRegion,
2516 JNI::GetByteArrayRegion,
2517 JNI::GetCharArrayRegion,
2518 JNI::GetShortArrayRegion,
2519 JNI::GetIntArrayRegion,
2520 JNI::GetLongArrayRegion,
2521 JNI::GetFloatArrayRegion,
2522 JNI::GetDoubleArrayRegion,
2523 JNI::SetBooleanArrayRegion,
2524 JNI::SetByteArrayRegion,
2525 JNI::SetCharArrayRegion,
2526 JNI::SetShortArrayRegion,
2527 JNI::SetIntArrayRegion,
2528 JNI::SetLongArrayRegion,
2529 JNI::SetFloatArrayRegion,
2530 JNI::SetDoubleArrayRegion,
2531 JNI::RegisterNatives,
2532 JNI::UnregisterNatives,
2533 JNI::MonitorEnter,
2534 JNI::MonitorExit,
2535 JNI::GetJavaVM,
2536 JNI::GetStringRegion,
2537 JNI::GetStringUTFRegion,
2538 JNI::GetPrimitiveArrayCritical,
2539 JNI::ReleasePrimitiveArrayCritical,
2540 JNI::GetStringCritical,
2541 JNI::ReleaseStringCritical,
2542 JNI::NewWeakGlobalRef,
2543 JNI::DeleteWeakGlobalRef,
2544 JNI::ExceptionCheck,
2545 JNI::NewDirectByteBuffer,
2546 JNI::GetDirectBufferAddress,
2547 JNI::GetDirectBufferCapacity,
2548 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002549};
2550
Elliott Hughes75770752011-08-24 17:52:38 -07002551JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002552 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002553 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002554 local_ref_cookie(IRT_FIRST_SEGMENT),
2555 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002556 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002557 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002558 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002559 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002560 functions = unchecked_functions = &gNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002561 if (vm->check_jni) {
2562 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002563 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002564 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2565 // errors will ensue.
2566 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2567 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002568}
2569
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002570JNIEnvExt::~JNIEnvExt() {
2571}
2572
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002573void JNIEnvExt::EnableCheckJni() {
2574 check_jni = true;
2575 functions = GetCheckJniNativeInterface();
2576}
2577
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002578void JNIEnvExt::DumpReferenceTables() {
2579 locals.Dump();
2580 monitors.Dump();
2581}
2582
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002583void JNIEnvExt::PushFrame(int capacity) {
2584 stacked_local_ref_cookies.push_back(local_ref_cookie);
2585 local_ref_cookie = locals.GetSegmentState();
2586}
2587
2588void JNIEnvExt::PopFrame() {
2589 locals.SetSegmentState(local_ref_cookie);
2590 local_ref_cookie = stacked_local_ref_cookies.back();
2591 stacked_local_ref_cookies.pop_back();
2592}
2593
Carl Shapiroea4dca82011-08-01 13:45:38 -07002594// JNI Invocation interface.
2595
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002596extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2597 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2598 if (args->version < JNI_VERSION_1_2) {
2599 return JNI_EVERSION;
2600 }
2601 Runtime::Options options;
2602 for (int i = 0; i < args->nOptions; ++i) {
2603 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002604 options.push_back(std::make_pair(StringPiece(option->optionString),
2605 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002606 }
2607 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002608 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002609 if (runtime == NULL) {
2610 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002611 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002612 runtime->Start();
2613 *p_env = Thread::Current()->GetJniEnv();
2614 *p_vm = runtime->GetJavaVM();
2615 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002616}
2617
Elliott Hughesf2682d52011-08-15 16:37:04 -07002618extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002619 Runtime* runtime = Runtime::Current();
2620 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002621 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002622 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002623 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002624 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002625 }
2626 return JNI_OK;
2627}
2628
2629// Historically unsupported.
2630extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2631 return JNI_ERR;
2632}
2633
Elliott Hughescdf53122011-08-19 15:46:09 -07002634class JII {
2635 public:
2636 static jint DestroyJavaVM(JavaVM* vm) {
2637 if (vm == NULL) {
2638 return JNI_ERR;
2639 } else {
2640 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2641 delete raw_vm->runtime;
2642 return JNI_OK;
2643 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002644 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002645
Elliott Hughescdf53122011-08-19 15:46:09 -07002646 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002647 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002648 }
2649
2650 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002651 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002652 }
2653
2654 static jint DetachCurrentThread(JavaVM* vm) {
2655 if (vm == NULL) {
2656 return JNI_ERR;
2657 } else {
2658 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2659 Runtime* runtime = raw_vm->runtime;
2660 runtime->DetachCurrentThread();
2661 return JNI_OK;
2662 }
2663 }
2664
2665 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2666 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2667 return JNI_EVERSION;
2668 }
2669 if (vm == NULL || env == NULL) {
2670 return JNI_ERR;
2671 }
2672 Thread* thread = Thread::Current();
2673 if (thread == NULL) {
2674 *env = NULL;
2675 return JNI_EDETACHED;
2676 }
2677 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002678 return JNI_OK;
2679 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002680};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002681
Elliott Hughesa2501992011-08-26 19:39:54 -07002682const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002683 NULL, // reserved0
2684 NULL, // reserved1
2685 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002686 JII::DestroyJavaVM,
2687 JII::AttachCurrentThread,
2688 JII::DetachCurrentThread,
2689 JII::GetEnv,
2690 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002691};
2692
Elliott Hughesa0957642011-09-02 14:27:33 -07002693JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002694 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002695 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002696 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002697 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002698 verbose_jni(options->IsVerbose("jni")),
2699 log_third_party_jni(options->IsVerbose("third-party-jni")),
2700 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002701 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002702 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002703 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002704 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002705 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002706 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002707 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002708 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002709 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002710 functions = unchecked_functions = &gInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002711 if (options->check_jni_) {
2712 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002713 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002714}
2715
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002716JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002717 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002718}
2719
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002720void JavaVMExt::EnableCheckJni() {
2721 check_jni = true;
2722 functions = GetCheckJniInvokeInterface();
2723}
2724
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002725void JavaVMExt::DumpReferenceTables() {
2726 {
2727 MutexLock mu(globals_lock);
2728 globals.Dump();
2729 }
2730 {
2731 MutexLock mu(weak_globals_lock);
2732 weak_globals.Dump();
2733 }
2734 {
2735 MutexLock mu(pins_lock);
2736 pin_table.Dump();
2737 }
2738}
2739
Elliott Hughes75770752011-08-24 17:52:38 -07002740bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2741 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002742
2743 // See if we've already loaded this library. If we have, and the class loader
2744 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002745 // TODO: for better results we should canonicalize the pathname (or even compare
2746 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002747 SharedLibrary* library;
2748 {
2749 // TODO: move the locking (and more of this logic) into Libraries.
2750 MutexLock mu(libraries_lock);
2751 library = libraries->Get(path);
2752 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002753 if (library != NULL) {
2754 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002755 // The library will be associated with class_loader. The JNI
2756 // spec says we can't load the same library into more than one
2757 // class loader.
2758 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2759 "ClassLoader %p; can't open in ClassLoader %p",
2760 path.c_str(), library->GetClassLoader(), class_loader);
2761 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002762 return false;
2763 }
2764 if (verbose_jni) {
2765 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2766 << "ClassLoader " << class_loader << "]";
2767 }
2768 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002769 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2770 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002771 return false;
2772 }
2773 return true;
2774 }
2775
2776 // Open the shared library. Because we're using a full path, the system
2777 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2778 // resolve this library's dependencies though.)
2779
2780 // Failures here are expected when java.library.path has several entries
2781 // and we have to hunt for the lib.
2782
2783 // The current version of the dynamic linker prints detailed information
2784 // about dlopen() failures. Some things to check if the message is
2785 // cryptic:
2786 // - make sure the library exists on the device
2787 // - verify that the right path is being opened (the debug log message
2788 // above can help with that)
2789 // - check to see if the library is valid (e.g. not zero bytes long)
2790 // - check config/prelink-linux-arm.map to ensure that the library
2791 // is listed and is not being overrun by the previous entry (if
2792 // loading suddenly stops working on a prelinked library, this is
2793 // a good one to check)
2794 // - write a trivial app that calls sleep() then dlopen(), attach
2795 // to it with "strace -p <pid>" while it sleeps, and watch for
2796 // attempts to open nonexistent dependent shared libs
2797
2798 // TODO: automate some of these checks!
2799
2800 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002801 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002802 // the GC to ignore us.
2803 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002804 void* handle = NULL;
2805 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002806 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002807 handle = dlopen(path.c_str(), RTLD_LAZY);
2808 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002809
2810 if (verbose_jni) {
2811 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2812 }
2813
2814 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002815 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002816 return false;
2817 }
2818
2819 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002820 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002821 // TODO: move the locking (and more of this logic) into Libraries.
2822 MutexLock mu(libraries_lock);
2823 library = libraries->Get(path);
2824 if (library != NULL) {
2825 LOG(INFO) << "WOW: we lost a race to add shared library: "
2826 << "\"" << path << "\" ClassLoader=" << class_loader;
2827 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002828 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002829 library = new SharedLibrary(path, handle, class_loader);
2830 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002831 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002832
2833 if (verbose_jni) {
2834 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2835 }
2836
2837 bool result = true;
2838 void* sym = dlsym(handle, "JNI_OnLoad");
2839 if (sym == NULL) {
2840 if (verbose_jni) {
2841 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2842 }
2843 } else {
2844 // Call JNI_OnLoad. We have to override the current class
2845 // loader, which will always be "null" since the stuff at the
2846 // top of the stack is around Runtime.loadLibrary(). (See
2847 // the comments in the JNI FindClass function.)
2848 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2849 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002850 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002851 self->SetClassLoaderOverride(class_loader);
2852
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002853 int version = 0;
2854 {
2855 ScopedThreadStateChange tsc(self, Thread::kNative);
2856 if (verbose_jni) {
2857 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2858 }
2859 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002860 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002861
Brian Carlstromaded5f72011-10-07 17:15:04 -07002862 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002863
2864 if (version != JNI_VERSION_1_2 &&
2865 version != JNI_VERSION_1_4 &&
2866 version != JNI_VERSION_1_6) {
2867 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2868 << "bad version: " << version;
2869 // It's unwise to call dlclose() here, but we can mark it
2870 // as bad and ensure that future load attempts will fail.
2871 // We don't know how far JNI_OnLoad got, so there could
2872 // be some partially-initialized stuff accessible through
2873 // newly-registered native method calls. We could try to
2874 // unregister them, but that doesn't seem worthwhile.
2875 result = false;
2876 } else {
2877 if (verbose_jni) {
2878 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2879 << " from JNI_OnLoad in \"" << path << "\"]";
2880 }
2881 }
2882 }
2883
2884 library->SetResult(result);
2885 return result;
2886}
2887
2888void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2889 CHECK(m->IsNative());
2890
2891 Class* c = m->GetDeclaringClass();
2892
2893 // If this is a static method, it could be called before the class
2894 // has been initialized.
2895 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002896 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002897 return NULL;
2898 }
2899 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002900 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002901 }
2902
Brian Carlstrom16192862011-09-12 17:50:06 -07002903 std::string detail;
2904 void* native_method;
2905 {
2906 MutexLock mu(libraries_lock);
2907 native_method = libraries->FindNativeMethod(m, detail);
2908 }
2909 // throwing can cause libraries_lock to be reacquired
2910 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002911 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002912 }
2913 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002914}
2915
Elliott Hughes410c0c82011-09-01 17:58:25 -07002916void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2917 {
2918 MutexLock mu(globals_lock);
2919 globals.VisitRoots(visitor, arg);
2920 }
2921 {
2922 MutexLock mu(pins_lock);
2923 pin_table.VisitRoots(visitor, arg);
2924 }
2925 // The weak_globals table is visited by the GC itself (because it mutates the table).
2926}
2927
Ian Rogersdf20fe02011-07-20 20:34:16 -07002928} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002929
2930std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2931 switch (rhs) {
2932 case JNIInvalidRefType:
2933 os << "JNIInvalidRefType";
2934 return os;
2935 case JNILocalRefType:
2936 os << "JNILocalRefType";
2937 return os;
2938 case JNIGlobalRefType:
2939 os << "JNIGlobalRefType";
2940 return os;
2941 case JNIWeakGlobalRefType:
2942 os << "JNIWeakGlobalRefType";
2943 return os;
2944 }
2945}