blob: 718659f393e92bbe7cd9411b3ca5be005cd4ab65 [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"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070022#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070023#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070024#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070025#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070026#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070027
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070028namespace art {
29
Elliott Hughes2ced6a52011-10-16 18:44:48 -070030static const size_t kMonitorsInitial = 32; // Arbitrary.
31static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
32
33static const size_t kLocalsInitial = 64; // Arbitrary.
34static const size_t kLocalsMax = 512; // Arbitrary sanity check.
35
36static const size_t kPinTableInitial = 16; // Arbitrary.
37static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
38
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070039static size_t gGlobalsInitial = 512; // Arbitrary.
40static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070041
42static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
43static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
44
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070045void SetJniGlobalsMax(size_t max) {
46 if (max != 0) {
47 gGlobalsMax = max;
48 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
49 }
50}
Ian Rogersdf20fe02011-07-20 20:34:16 -070051
Elliott Hughesc5f7c912011-08-18 14:00:42 -070052/*
53 * Add a local reference for an object to the current stack frame. When
54 * the native function returns, the reference will be discarded.
55 *
56 * We need to allow the same reference to be added multiple times.
57 *
58 * This will be called on otherwise unreferenced objects. We cannot do
59 * GC allocations here, and it's best if we don't grab a mutex.
60 *
61 * Returns the local reference (currently just the same pointer that was
62 * passed in), or NULL on failure.
63 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070064template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070065T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
66 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
67 Object* obj = const_cast<Object*>(const_obj);
68
Elliott Hughesc5f7c912011-08-18 14:00:42 -070069 if (obj == NULL) {
70 return NULL;
71 }
72
Elliott Hughesbf86d042011-08-31 17:53:14 -070073 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
74 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070075
Ian Rogers5a7a74a2011-09-26 16:32:29 -070076 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070077 IndirectRef ref = locals.Add(cookie, obj);
78 if (ref == NULL) {
79 // TODO: just change Add's DCHECK to CHECK and lose this?
80 locals.Dump();
81 LOG(FATAL) << "Failed adding to JNI local reference table "
82 << "(has " << locals.Capacity() << " entries)";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070083 }
84
85#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070086 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070087 size_t entry_count = locals.Capacity();
88 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070089 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070090 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070091 locals.Dump();
Elliott Hughes82188472011-11-07 18:11:48 -080092 // TODO: LOG(FATAL) instead.
Elliott Hughesc5f7c912011-08-18 14:00:42 -070093 }
94 }
95#endif
96
Elliott Hughesbf86d042011-08-31 17:53:14 -070097 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070098 // Hand out direct pointers to support broken old apps.
99 return reinterpret_cast<T>(obj);
100 }
101
102 return reinterpret_cast<T>(ref);
103}
104
Ian Rogers0571d352011-11-03 19:51:38 -0700105size_t NumArgArrayBytes(const char* shorty) {
106 size_t num_bytes = 0;
107 size_t end = strlen(shorty);
108 for (size_t i = 1; i < end; ++i) {
109 char ch = shorty[i];
110 if (ch == 'D' || ch == 'J') {
111 num_bytes += 8;
112 } else if (ch == 'L') {
113 // Argument is a reference or an array. The shorty descriptor
114 // does not distinguish between these types.
115 num_bytes += sizeof(Object*);
116 } else {
117 num_bytes += 4;
118 }
119 }
120 return num_bytes;
121}
122
Elliott Hughesbf86d042011-08-31 17:53:14 -0700123// For external use.
124template<typename T>
125T Decode(JNIEnv* public_env, jobject obj) {
126 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
127 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
128}
129// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700130template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700131template Class* Decode<Class*>(JNIEnv*, jobject);
132template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
133template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700134template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700135template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700136template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700137template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400138template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700139template String* Decode<String*>(JNIEnv*, jobject);
140template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700141
142namespace {
143
Elliott Hughescdf53122011-08-19 15:46:09 -0700144jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
145 if (obj == NULL) {
146 return NULL;
147 }
Elliott Hughes75770752011-08-24 17:52:38 -0700148 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700149 IndirectReferenceTable& weak_globals = vm->weak_globals;
150 MutexLock mu(vm->weak_globals_lock);
151 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
152 return reinterpret_cast<jweak>(ref);
153}
154
Elliott Hughesbf86d042011-08-31 17:53:14 -0700155// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700156template<typename T>
157T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700158 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700159}
160
Ian Rogers0571d352011-11-03 19:51:38 -0700161static byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700162 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800163 const char* shorty = MethodHelper(method).GetShorty();
164 size_t shorty_len = strlen(shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700165 size_t num_bytes = NumArgArrayBytes(shorty);
166 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800167 for (size_t i = 1, offset = 0; i < shorty_len; ++i) {
168 switch (shorty[i]) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700169 case 'Z':
170 case 'B':
171 case 'C':
172 case 'S':
173 case 'I':
174 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
175 offset += 4;
176 break;
177 case 'F':
178 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
179 offset += 4;
180 break;
181 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700182 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700183 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
184 offset += sizeof(Object*);
185 break;
186 }
187 case 'D':
188 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
189 offset += 8;
190 break;
191 case 'J':
192 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
193 offset += 8;
194 break;
195 }
196 }
197 return arg_array.release();
198}
199
Ian Rogers0571d352011-11-03 19:51:38 -0700200static byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700201 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800202 const char* shorty = MethodHelper(method).GetShorty();
203 size_t shorty_len = strlen(shorty);
204 size_t num_bytes = NumArgArrayBytes(shorty);
Elliott Hughes90a33692011-08-30 13:27:07 -0700205 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800206 for (size_t i = 1, offset = 0; i < shorty_len; ++i) {
207 switch (shorty[i]) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700208 case 'Z':
209 case 'B':
210 case 'C':
211 case 'S':
212 case 'I':
213 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
214 offset += 4;
215 break;
216 case 'F':
217 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
218 offset += 4;
219 break;
220 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700221 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700222 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
223 offset += sizeof(Object*);
224 break;
225 }
226 case 'D':
227 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
228 offset += 8;
229 break;
230 case 'J':
231 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
232 offset += 8;
233 break;
234 }
235 }
236 return arg_array.release();
237}
238
Ian Rogers0571d352011-11-03 19:51:38 -0700239static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700240 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700241 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700242 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700243 return result;
244}
245
Ian Rogers0571d352011-11-03 19:51:38 -0700246static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700247 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
248 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700249 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700250 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
251 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700252}
253
Ian Rogers0571d352011-11-03 19:51:38 -0700254static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700255 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700256}
257
Ian Rogers0571d352011-11-03 19:51:38 -0700258static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
259 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700260 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
261 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700262 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700263 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
264 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700265}
266
Ian Rogers0571d352011-11-03 19:51:38 -0700267static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
268 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700269 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
270 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700271 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700272 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
273 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700274}
275
Elliott Hughes6b436852011-08-12 10:16:44 -0700276// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
277// separated with slashes but aren't wrapped with "L;" like regular descriptors
278// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
279// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
280// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700281static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700282 std::string result;
283 // Add the missing "L;" if necessary.
284 if (name[0] == '[') {
285 result = name;
286 } else {
287 result += 'L';
288 result += name;
289 result += ';';
290 }
291 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700292 if (result.find('.') != std::string::npos) {
293 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
294 << "\"" << name << "\"";
295 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700296 }
297 return result;
298}
299
Ian Rogers0571d352011-11-03 19:51:38 -0700300static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700301 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800302 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor().c_str(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700303}
304
Ian Rogers0571d352011-11-03 19:51:38 -0700305static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700306 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700307 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700308 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700309 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700310
311 Method* method = NULL;
312 if (is_static) {
313 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700314 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700315 method = c->FindVirtualMethod(name, sig);
316 if (method == NULL) {
317 // No virtual method matching the signature. Search declared
318 // private methods and constructors.
319 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700320 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700321 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700322
Elliott Hughescdf53122011-08-19 15:46:09 -0700323 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700324 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700325 return NULL;
326 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700327
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700328 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700329}
330
Ian Rogers0571d352011-11-03 19:51:38 -0700331static const ClassLoader* GetClassLoader(Thread* self) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700332 Frame frame = self->GetTopOfStack();
333 Method* method = frame.GetMethod();
334 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
335 return self->GetClassLoaderOverride();
336 }
337 return method->GetDeclaringClass()->GetClassLoader();
338}
339
Ian Rogers0571d352011-11-03 19:51:38 -0700340static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700341 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700342 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700343 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700344 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700345
346 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700347 Class* field_type;
348 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
349 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700350 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700351 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700352 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700353 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700354 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355 if (field_type == NULL) {
356 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700357 DCHECK(ts.Self()->IsExceptionPending());
358 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700359 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700360 "no type \"%s\" found and so no field \"%s\" could be found in class "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800361 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700362 return NULL;
363 }
364 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800365 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700366 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800367 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700368 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700369 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700370 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700371 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800372 name, ClassHelper(c).GetDescriptor().c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700373 return NULL;
374 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700375 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700376}
377
Ian Rogers0571d352011-11-03 19:51:38 -0700378static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700379 JavaVMExt* vm = ts.Vm();
380 MutexLock mu(vm->pins_lock);
381 vm->pin_table.Add(array);
382}
383
Ian Rogers0571d352011-11-03 19:51:38 -0700384static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700385 JavaVMExt* vm = ts.Vm();
386 MutexLock mu(vm->pins_lock);
387 vm->pin_table.Remove(array);
388}
389
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700390template<typename JniT, typename ArtT>
391JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
392 CHECK_GE(length, 0); // TODO: ReportJniError
393 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700394 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700395}
396
Elliott Hughes75770752011-08-24 17:52:38 -0700397template <typename ArrayT, typename CArrayT, typename ArtArrayT>
398CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
399 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
400 PinPrimitiveArray(ts, array);
401 if (is_copy != NULL) {
402 *is_copy = JNI_FALSE;
403 }
404 return array->GetData();
405}
406
407template <typename ArrayT>
408void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
409 if (mode != JNI_COMMIT) {
410 Array* array = Decode<Array*>(ts, java_array);
411 UnpinPrimitiveArray(ts, array);
412 }
413}
414
Ian Rogers0571d352011-11-03 19:51:38 -0700415static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700416 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700417 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700418 "%s offset=%d length=%d %s.length=%d",
419 type.c_str(), start, length, identifier, array->GetLength());
420}
Ian Rogers0571d352011-11-03 19:51:38 -0700421
422static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700423 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700424 "offset=%d length=%d string.length()=%d", start, length, array_length);
425}
Elliott Hughes814e4032011-08-23 12:07:56 -0700426
427template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700428void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700429 ArrayT* array = Decode<ArrayT*>(ts, java_array);
430 if (start < 0 || length < 0 || start + length > array->GetLength()) {
431 ThrowAIOOBE(ts, array, start, length, "src");
432 } else {
433 JavaT* data = array->GetData();
434 memcpy(buf, data + start, length * sizeof(JavaT));
435 }
436}
437
438template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700439void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700440 ArrayT* array = Decode<ArrayT*>(ts, java_array);
441 if (start < 0 || length < 0 || start + length > array->GetLength()) {
442 ThrowAIOOBE(ts, array, start, length, "dst");
443 } else {
444 JavaT* data = array->GetData();
445 memcpy(data + start, buf, length * sizeof(JavaT));
446 }
447}
448
Ian Rogers0571d352011-11-03 19:51:38 -0700449static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700450 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
451 CHECK(buffer_class.get() != NULL);
452 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
453}
454
Ian Rogers0571d352011-11-03 19:51:38 -0700455static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700456 static jclass buffer_class = InitDirectByteBufferClass(env);
457 return buffer_class;
458}
459
Ian Rogers0571d352011-11-03 19:51:38 -0700460static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700461 if (vm == NULL || p_env == NULL) {
462 return JNI_ERR;
463 }
464
Elliott Hughescac6cc72011-11-03 20:31:21 -0700465 // Return immediately if we're already one with the VM.
466 Thread* self = Thread::Current();
467 if (self != NULL) {
468 *p_env = self->GetJniEnv();
469 return JNI_OK;
470 }
471
472 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
473
474 // No threads allowed in zygote mode.
475 if (runtime->IsZygote()) {
476 LOG(ERROR) << "Attempt to attach a thread in the zygote";
477 return JNI_ERR;
478 }
479
Elliott Hughes75770752011-08-24 17:52:38 -0700480 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
481 JavaVMAttachArgs args;
482 if (thr_args == NULL) {
483 // Allow the v1.1 calling convention.
484 args.version = JNI_VERSION_1_2;
485 args.name = NULL;
486 args.group = NULL; // TODO: get "main" thread group
487 } else {
488 args.version = in_args->version;
489 args.name = in_args->name;
490 if (in_args->group != NULL) {
491 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
492 args.group = NULL; // TODO: decode in_args->group
493 } else {
494 args.group = NULL; // TODO: get "main" thread group
495 }
496 }
497 CHECK_GE(args.version, JNI_VERSION_1_2);
498
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700499 runtime->AttachCurrentThread(args.name, as_daemon);
500 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700501 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700502}
503
Elliott Hughes79082e32011-08-25 12:07:32 -0700504class SharedLibrary {
505 public:
506 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
507 : path_(path),
508 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700509 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700510 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700511 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700512 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700513 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 }
515
Elliott Hughes79082e32011-08-25 12:07:32 -0700516 Object* GetClassLoader() {
517 return class_loader_;
518 }
519
520 std::string GetPath() {
521 return path_;
522 }
523
524 /*
525 * Check the result of an earlier call to JNI_OnLoad on this library. If
526 * the call has not yet finished in another thread, wait for it.
527 */
528 bool CheckOnLoadResult(JavaVMExt* vm) {
529 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700530 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700531 // Check this so we don't end up waiting for ourselves. We need
532 // to return "true" so the caller can continue.
533 LOG(INFO) << *self << " recursive attempt to load library "
534 << "\"" << path_ << "\"";
535 return true;
536 }
537
538 MutexLock mu(jni_on_load_lock_);
539 while (jni_on_load_result_ == kPending) {
540 if (vm->verbose_jni) {
541 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
542 << "JNI_OnLoad...]";
543 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700544 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700545 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 }
547
548 bool okay = (jni_on_load_result_ == kOkay);
549 if (vm->verbose_jni) {
550 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
551 << (okay ? "succeeded" : "failed") << "]";
552 }
553 return okay;
554 }
555
556 void SetResult(bool result) {
557 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700558 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700559
560 // Broadcast a wakeup to anybody sleeping on the condition variable.
561 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700562 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700563 }
564
565 void* FindSymbol(const std::string& symbol_name) {
566 return dlsym(handle_, symbol_name.c_str());
567 }
568
569 private:
570 enum JNI_OnLoadState {
571 kPending,
572 kFailed,
573 kOkay,
574 };
575
576 // Path to library "/system/lib/libjni.so".
577 std::string path_;
578
579 // The void* returned by dlopen(3).
580 void* handle_;
581
582 // The ClassLoader this library is associated with.
583 Object* class_loader_;
584
585 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700586 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700587 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700588 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700589 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700590 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700591 // Result of earlier JNI_OnLoad call.
592 JNI_OnLoadState jni_on_load_result_;
593};
594
Elliott Hughescdf53122011-08-19 15:46:09 -0700595} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700596
Elliott Hughes79082e32011-08-25 12:07:32 -0700597// This exists mainly to keep implementation details out of the header file.
598class Libraries {
599 public:
600 Libraries() {
601 }
602
603 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700604 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700605 }
606
607 SharedLibrary* Get(const std::string& path) {
608 return libraries_[path];
609 }
610
611 void Put(const std::string& path, SharedLibrary* library) {
612 libraries_[path] = library;
613 }
614
615 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700616 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700617 std::string jni_short_name(JniShortName(m));
618 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700619 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700620 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
621 SharedLibrary* library = it->second;
622 if (library->GetClassLoader() != declaring_class_loader) {
623 // We only search libraries loaded by the appropriate ClassLoader.
624 continue;
625 }
626 // Try the short name then the long name...
627 void* fn = library->FindSymbol(jni_short_name);
628 if (fn == NULL) {
629 fn = library->FindSymbol(jni_long_name);
630 }
631 if (fn != NULL) {
632 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700633 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700634 << " in \"" << library->GetPath() << "\"]";
635 }
636 return fn;
637 }
638 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700639 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700640 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700641 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700642 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700643 return NULL;
644 }
645
646 private:
647 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
648
649 std::map<std::string, SharedLibrary*> libraries_;
650};
651
Elliott Hughes418d20f2011-09-22 14:00:39 -0700652JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
653 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
654 Object* receiver = Decode<Object*>(env, obj);
655 Method* method = DecodeMethod(mid);
656 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
657 return InvokeWithArgArray(env, receiver, method, arg_array.get());
658}
659
Elliott Hughescdf53122011-08-19 15:46:09 -0700660class JNI {
661 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700662
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 static jint GetVersion(JNIEnv* env) {
664 ScopedJniThreadState ts(env);
665 return JNI_VERSION_1_6;
666 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700667
Elliott Hughescdf53122011-08-19 15:46:09 -0700668 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
669 ScopedJniThreadState ts(env);
670 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700671 return NULL;
672 }
673
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 static jclass FindClass(JNIEnv* env, const char* name) {
675 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700676 Runtime* runtime = Runtime::Current();
677 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700679 Class* c = NULL;
680 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700681 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700682 c = class_linker->FindClass(descriptor, cl);
683 } else {
684 c = class_linker->FindSystemClass(descriptor);
685 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700686 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700687 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700688
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
690 ScopedJniThreadState ts(env);
691 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700692 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700693 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700694
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
696 ScopedJniThreadState ts(env);
697 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700698 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700700
Elliott Hughescdf53122011-08-19 15:46:09 -0700701 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
702 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700703 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700704 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700706
Elliott Hughescdf53122011-08-19 15:46:09 -0700707 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
708 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700709 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700710 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700711 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700712
Elliott Hughes37f7a402011-08-22 18:56:01 -0700713 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
714 ScopedJniThreadState ts(env);
715 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700716 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700717 }
718
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700719 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700721 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700722 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700723 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700724
Elliott Hughes37f7a402011-08-22 18:56:01 -0700725 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700727 Class* c1 = Decode<Class*>(ts, java_class1);
728 Class* c2 = Decode<Class*>(ts, java_class2);
729 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700731
Elliott Hughes37f7a402011-08-22 18:56:01 -0700732 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700733 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700734 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700735 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700736 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700737 return JNI_TRUE;
738 } else {
739 Object* obj = Decode<Object*>(ts, jobj);
740 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700741 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700742 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700744
Elliott Hughes37f7a402011-08-22 18:56:01 -0700745 static jint Throw(JNIEnv* env, jthrowable java_exception) {
746 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700747 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700748 if (exception == NULL) {
749 return JNI_ERR;
750 }
751 ts.Self()->SetException(exception);
752 return JNI_OK;
753 }
754
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700755 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700756 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700757 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700758 jmethodID mid = ((msg != NULL)
759 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
760 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700761 if (mid == NULL) {
762 return JNI_ERR;
763 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700764 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700765 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700766 return JNI_ERR;
767 }
768
769 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700770 args[0].l = s.get();
771 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
772 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700773 return JNI_ERR;
774 }
775
Elliott Hughes72025e52011-08-23 17:50:30 -0700776 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700777
Elliott Hughes37f7a402011-08-22 18:56:01 -0700778 return JNI_OK;
779 }
780
781 static jboolean ExceptionCheck(JNIEnv* env) {
782 ScopedJniThreadState ts(env);
783 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
784 }
785
786 static void ExceptionClear(JNIEnv* env) {
787 ScopedJniThreadState ts(env);
788 ts.Self()->ClearException();
789 }
790
791 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700792 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700793
794 Thread* self = ts.Self();
795 Throwable* original_exception = self->GetException();
796 self->ClearException();
797
Elliott Hughesbf86d042011-08-31 17:53:14 -0700798 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700799 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
800 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
801 if (mid == NULL) {
802 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700803 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700804 } else {
805 env->CallVoidMethod(exception.get(), mid);
806 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700807 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700808 << " thrown while calling printStackTrace";
809 self->ClearException();
810 }
811 }
812
813 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700815
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 static jthrowable ExceptionOccurred(JNIEnv* env) {
817 ScopedJniThreadState ts(env);
818 Object* exception = ts.Self()->GetException();
819 if (exception == NULL) {
820 return NULL;
821 } else {
822 // TODO: if adding a local reference failing causes the VM to abort
823 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700824 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700825 if (localException == NULL) {
826 // We were unable to add a new local reference, and threw a new
827 // exception. We can't return "exception", because it's not a
828 // local reference. So we have to return NULL, indicating that
829 // there was no exception, even though it's pretty much raining
830 // exceptions in here.
831 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
832 }
833 return localException;
834 }
835 }
836
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 static void FatalError(JNIEnv* env, const char* msg) {
838 ScopedJniThreadState ts(env);
839 LOG(FATAL) << "JNI FatalError called: " << msg;
840 }
841
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700842 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700843 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700844 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
845 return JNI_ERR;
846 }
847 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 return JNI_OK;
849 }
850
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700851 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700853 Object* survivor = Decode<Object*>(ts, java_survivor);
854 ts.Env()->PopFrame();
855 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 }
857
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700858 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700859 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700860 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
861 }
862
863 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
864 // TODO: we should try to expand the table if necessary.
865 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
866 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
867 return JNI_ERR;
868 }
869 // TODO: this isn't quite right, since "capacity" includes holes.
870 size_t capacity = ts.Env()->locals.Capacity();
871 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
872 if (!okay) {
873 ts.Self()->ThrowOutOfMemoryError(caller);
874 }
875 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700876 }
877
Elliott Hughescdf53122011-08-19 15:46:09 -0700878 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
879 ScopedJniThreadState ts(env);
880 if (obj == NULL) {
881 return NULL;
882 }
883
Elliott Hughes75770752011-08-24 17:52:38 -0700884 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700885 IndirectReferenceTable& globals = vm->globals;
886 MutexLock mu(vm->globals_lock);
887 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
888 return reinterpret_cast<jobject>(ref);
889 }
890
891 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
892 ScopedJniThreadState ts(env);
893 if (obj == NULL) {
894 return;
895 }
896
Elliott Hughes75770752011-08-24 17:52:38 -0700897 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700898 IndirectReferenceTable& globals = vm->globals;
899 MutexLock mu(vm->globals_lock);
900
901 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
902 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
903 << "failed to find entry";
904 }
905 }
906
907 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
908 ScopedJniThreadState ts(env);
909 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
910 }
911
912 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
913 ScopedJniThreadState ts(env);
914 if (obj == NULL) {
915 return;
916 }
917
Elliott Hughes75770752011-08-24 17:52:38 -0700918 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 IndirectReferenceTable& weak_globals = vm->weak_globals;
920 MutexLock mu(vm->weak_globals_lock);
921
922 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
923 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
924 << "failed to find entry";
925 }
926 }
927
928 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
929 ScopedJniThreadState ts(env);
930 if (obj == NULL) {
931 return NULL;
932 }
933
934 IndirectReferenceTable& locals = ts.Env()->locals;
935
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700936 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
938 return reinterpret_cast<jobject>(ref);
939 }
940
941 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
942 ScopedJniThreadState ts(env);
943 if (obj == NULL) {
944 return;
945 }
946
947 IndirectReferenceTable& locals = ts.Env()->locals;
948
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700949 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 if (!locals.Remove(cookie, obj)) {
951 // Attempting to delete a local reference that is not in the
952 // topmost local reference frame is a no-op. DeleteLocalRef returns
953 // void and doesn't throw any exceptions, but we should probably
954 // complain about it so the user will notice that things aren't
955 // going quite the way they expect.
956 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
957 << "failed to find entry";
958 }
959 }
960
961 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
962 ScopedJniThreadState ts(env);
963 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
964 ? JNI_TRUE : JNI_FALSE;
965 }
966
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700967 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700969 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700970 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700971 return NULL;
972 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700973 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 ScopedJniThreadState ts(env);
978 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 va_start(args, mid);
980 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 va_end(args);
982 return result;
983 }
984
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700987 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700988 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700989 return NULL;
990 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700991 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700992 if (result == NULL) {
993 return NULL;
994 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700995 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700997 if (!ts.Self()->IsExceptionPending()) {
998 return local_result;
999 } else {
1000 return NULL;
1001 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001006 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001007 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001008 return NULL;
1009 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001010 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001011 if (result == NULL) {
1012 return NULL;
1013 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001014 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001016 if (!ts.Self()->IsExceptionPending()) {
1017 return local_result;
1018 } else {
1019 return NULL;
1020 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1024 ScopedJniThreadState ts(env);
1025 return FindMethodID(ts, c, name, sig, false);
1026 }
1027
1028 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1029 ScopedJniThreadState ts(env);
1030 return FindMethodID(ts, c, name, sig, true);
1031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 va_list ap;
1036 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001037 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001039 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001044 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001045 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001050 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001051 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 va_list ap;
1057 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001058 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 va_end(ap);
1060 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001065 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001070 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_list ap;
1076 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001077 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 va_end(ap);
1079 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001084 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001089 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 va_list ap;
1095 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001096 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_end(ap);
1098 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001103 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001108 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 va_list ap;
1114 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001115 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 va_end(ap);
1117 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001122 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001127 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 va_list ap;
1133 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001134 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 va_end(ap);
1136 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001141 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001146 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 va_list ap;
1152 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001153 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_end(ap);
1155 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001160 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001165 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 va_list ap;
1171 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001172 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_end(ap);
1174 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001179 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001184 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 va_list ap;
1190 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001191 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 va_end(ap);
1193 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001198 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001203 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
1205
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 va_list ap;
1209 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001210 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001216 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 }
1218
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001221 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
1224 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 ScopedJniThreadState ts(env);
1227 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001229 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001230 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 va_end(ap);
1232 return local_result;
1233 }
1234
1235 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001238 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001239 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
1242 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001245 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001246 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
1249 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 ScopedJniThreadState ts(env);
1252 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001254 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 va_end(ap);
1256 return result.z;
1257 }
1258
1259 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001262 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 }
1264
1265 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001266 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001268 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
1271 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 ScopedJniThreadState ts(env);
1274 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001276 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 va_end(ap);
1278 return result.b;
1279 }
1280
1281 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001284 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
1287 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001288 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001290 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
1293 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 ScopedJniThreadState ts(env);
1296 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001298 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 va_end(ap);
1300 return result.c;
1301 }
1302
1303 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001304 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001306 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
1309 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001310 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001312 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
1315 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 ScopedJniThreadState ts(env);
1318 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001319 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001320 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 va_end(ap);
1322 return result.s;
1323 }
1324
1325 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001328 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
1331 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001334 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
Elliott Hughes418d20f2011-09-22 14:00:39 -07001337 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 ScopedJniThreadState ts(env);
1339 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001340 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001341 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 va_end(ap);
1343 return result.i;
1344 }
1345
1346 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001349 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
1352 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001355 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
1358 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 ScopedJniThreadState ts(env);
1361 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001363 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 va_end(ap);
1365 return result.j;
1366 }
1367
1368 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001369 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001371 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
1374 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001375 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001377 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
1380 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001381 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 ScopedJniThreadState ts(env);
1383 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001384 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001385 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 va_end(ap);
1387 return result.f;
1388 }
1389
1390 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001391 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001393 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
1396 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001397 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001399 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
1402 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001403 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 ScopedJniThreadState ts(env);
1405 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001406 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001407 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 va_end(ap);
1409 return result.d;
1410 }
1411
1412 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001413 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001415 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
1417
1418 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001419 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001421 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
1424 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001425 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 ScopedJniThreadState ts(env);
1427 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001428 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001429 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 va_end(ap);
1431 }
1432
1433 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001434 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001436 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
1439 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001440 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001442 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
1444
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001445 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 ScopedJniThreadState ts(env);
1447 return FindFieldID(ts, c, name, sig, false);
1448 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001449
1450
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001451 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 ScopedJniThreadState ts(env);
1453 return FindFieldID(ts, c, name, sig, true);
1454 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001455
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001459 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001460 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001465 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001466 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 }
1468
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001469 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 Object* o = Decode<Object*>(ts, java_object);
1472 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001473 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 }
1476
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001477 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001480 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001481 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484#define GET_PRIMITIVE_FIELD(fn, instance) \
1485 ScopedJniThreadState ts(env); \
1486 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001487 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 return f->fn(o)
1489
1490#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1491 ScopedJniThreadState ts(env); \
1492 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001493 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494 f->fn(o, value)
1495
1496 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1497 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 }
1499
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1501 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1505 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1509 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
1511
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1513 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1517 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1521 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001524 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1525 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1529 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 }
1531
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001532 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1533 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001536 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1537 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1541 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1545 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001548 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1549 GET_PRIMITIVE_FIELD(GetLong, NULL);
1550 }
1551
1552 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1553 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1554 }
1555
1556 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1557 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1558 }
1559
1560 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1561 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1562 }
1563
1564 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1565 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1566 }
1567
1568 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1569 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1570 }
1571
1572 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1573 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1574 }
1575
1576 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1577 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1578 }
1579
1580 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1581 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1582 }
1583
1584 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1585 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1586 }
1587
1588 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1589 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1590 }
1591
1592 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1593 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1594 }
1595
1596 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1597 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1598 }
1599
1600 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1601 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1602 }
1603
1604 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1605 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1606 }
1607
1608 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1609 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1610 }
1611
1612 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1613 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1614 }
1615
1616 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1617 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1618 }
1619
1620 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1621 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes418d20f2011-09-22 14:00:39 -07001624 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 ScopedJniThreadState ts(env);
1626 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001627 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001628 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001629 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 va_end(ap);
1631 return local_result;
1632 }
1633
Elliott Hughes418d20f2011-09-22 14:00:39 -07001634 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001636 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001637 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 }
1639
Elliott Hughes418d20f2011-09-22 14:00:39 -07001640 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001642 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001643 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
1645
Elliott Hughes418d20f2011-09-22 14:00:39 -07001646 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 ScopedJniThreadState ts(env);
1648 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001649 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001650 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 va_end(ap);
1652 return result.z;
1653 }
1654
Elliott Hughes418d20f2011-09-22 14:00:39 -07001655 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001657 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 }
1659
Elliott Hughes418d20f2011-09-22 14:00:39 -07001660 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001662 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 }
1664
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 ScopedJniThreadState ts(env);
1667 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001669 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 va_end(ap);
1671 return result.b;
1672 }
1673
Elliott Hughes418d20f2011-09-22 14:00:39 -07001674 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001676 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes418d20f2011-09-22 14:00:39 -07001679 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001681 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 }
1683
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 ScopedJniThreadState ts(env);
1686 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001688 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 va_end(ap);
1690 return result.c;
1691 }
1692
Elliott Hughes418d20f2011-09-22 14:00:39 -07001693 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001695 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes418d20f2011-09-22 14:00:39 -07001698 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001700 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 }
1702
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 ScopedJniThreadState ts(env);
1705 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001706 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001707 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 va_end(ap);
1709 return result.s;
1710 }
1711
Elliott Hughes418d20f2011-09-22 14:00:39 -07001712 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001714 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes418d20f2011-09-22 14:00:39 -07001717 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001719 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 }
1721
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 ScopedJniThreadState ts(env);
1724 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001725 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001726 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 va_end(ap);
1728 return result.i;
1729 }
1730
Elliott Hughes418d20f2011-09-22 14:00:39 -07001731 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001733 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes418d20f2011-09-22 14:00:39 -07001736 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001738 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 }
1740
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 ScopedJniThreadState ts(env);
1743 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001744 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001745 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 va_end(ap);
1747 return result.j;
1748 }
1749
Elliott Hughes418d20f2011-09-22 14:00:39 -07001750 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001752 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes418d20f2011-09-22 14:00:39 -07001755 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001757 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 }
1759
Elliott Hughes72025e52011-08-23 17:50:30 -07001760 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 ScopedJniThreadState ts(env);
1762 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001763 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001764 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 va_end(ap);
1766 return result.f;
1767 }
1768
Elliott Hughes418d20f2011-09-22 14:00:39 -07001769 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001771 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 }
1773
Elliott Hughes418d20f2011-09-22 14:00:39 -07001774 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001776 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 }
1778
Elliott Hughes72025e52011-08-23 17:50:30 -07001779 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 ScopedJniThreadState ts(env);
1781 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001782 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001783 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 va_end(ap);
1785 return result.d;
1786 }
1787
Elliott Hughes418d20f2011-09-22 14:00:39 -07001788 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001790 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 }
1792
Elliott Hughes418d20f2011-09-22 14:00:39 -07001793 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001795 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 }
1797
Elliott Hughes72025e52011-08-23 17:50:30 -07001798 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 ScopedJniThreadState ts(env);
1800 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001801 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001802 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 va_end(ap);
1804 }
1805
Elliott Hughes418d20f2011-09-22 14:00:39 -07001806 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001808 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
Elliott Hughes418d20f2011-09-22 14:00:39 -07001811 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001813 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 }
1815
Elliott Hughes814e4032011-08-23 12:07:56 -07001816 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001818 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001819 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
1822 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1823 ScopedJniThreadState ts(env);
1824 if (utf == NULL) {
1825 return NULL;
1826 }
1827 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001828 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
Elliott Hughes814e4032011-08-23 12:07:56 -07001831 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1832 ScopedJniThreadState ts(env);
1833 return Decode<String*>(ts, java_string)->GetLength();
1834 }
1835
1836 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1837 ScopedJniThreadState ts(env);
1838 return Decode<String*>(ts, java_string)->GetUtfLength();
1839 }
1840
Elliott Hughesb465ab02011-08-24 11:21:21 -07001841 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001843 String* s = Decode<String*>(ts, java_string);
1844 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1845 ThrowSIOOBE(ts, start, length, s->GetLength());
1846 } else {
1847 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1848 memcpy(buf, chars + start, length * sizeof(jchar));
1849 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001850 }
1851
Elliott Hughesb465ab02011-08-24 11:21:21 -07001852 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001853 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001854 String* s = Decode<String*>(ts, java_string);
1855 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1856 ThrowSIOOBE(ts, start, length, s->GetLength());
1857 } else {
1858 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1859 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1860 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001861 }
1862
Elliott Hughes75770752011-08-24 17:52:38 -07001863 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001864 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001865 String* s = Decode<String*>(ts, java_string);
1866 const CharArray* chars = s->GetCharArray();
1867 PinPrimitiveArray(ts, chars);
1868 if (is_copy != NULL) {
1869 *is_copy = JNI_FALSE;
1870 }
1871 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001872 }
1873
Elliott Hughes75770752011-08-24 17:52:38 -07001874 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001875 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001876 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 }
1878
Elliott Hughes75770752011-08-24 17:52:38 -07001879 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001881 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 }
1883
Elliott Hughes75770752011-08-24 17:52:38 -07001884 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001886 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 }
1888
Elliott Hughes75770752011-08-24 17:52:38 -07001889 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001890 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001891 if (java_string == NULL) {
1892 return NULL;
1893 }
1894 if (is_copy != NULL) {
1895 *is_copy = JNI_TRUE;
1896 }
1897 String* s = Decode<String*>(ts, java_string);
1898 size_t byte_count = s->GetUtfLength();
1899 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001900 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001901 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1902 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1903 bytes[byte_count] = '\0';
1904 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001905 }
1906
Elliott Hughes75770752011-08-24 17:52:38 -07001907 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001908 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001909 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001910 }
1911
Elliott Hughesbd935992011-08-22 11:59:34 -07001912 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001914 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001915 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001916 Array* array = obj->AsArray();
1917 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 }
1919
Elliott Hughes814e4032011-08-23 12:07:56 -07001920 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001922 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001923 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001924 }
1925
1926 static void SetObjectArrayElement(JNIEnv* env,
1927 jobjectArray java_array, jsize index, jobject java_value) {
1928 ScopedJniThreadState ts(env);
1929 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1930 Object* value = Decode<Object*>(ts, java_value);
1931 array->Set(index, value);
1932 }
1933
1934 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1935 ScopedJniThreadState ts(env);
1936 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1937 }
1938
1939 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1940 ScopedJniThreadState ts(env);
1941 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1942 }
1943
1944 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1945 ScopedJniThreadState ts(env);
1946 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1947 }
1948
1949 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1950 ScopedJniThreadState ts(env);
1951 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1952 }
1953
1954 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1955 ScopedJniThreadState ts(env);
1956 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1957 }
1958
1959 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1960 ScopedJniThreadState ts(env);
1961 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1962 }
1963
1964 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1965 ScopedJniThreadState ts(env);
1966 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1967 }
1968
1969 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1970 ScopedJniThreadState ts(env);
1971 CHECK_GE(length, 0); // TODO: ReportJniError
1972
1973 // Compute the array class corresponding to the given element class.
1974 Class* element_class = Decode<Class*>(ts, element_jclass);
1975 std::string descriptor;
1976 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001977 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001978
1979 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001980 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1981 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 return NULL;
1983 }
1984
Elliott Hughes75770752011-08-24 17:52:38 -07001985 // Allocate and initialize if necessary.
1986 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001988 if (initial_element != NULL) {
1989 Object* initial_object = Decode<Object*>(ts, initial_element);
1990 for (jsize i = 0; i < length; ++i) {
1991 result->Set(i, initial_object);
1992 }
1993 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001994 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
1997 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1998 ScopedJniThreadState ts(env);
1999 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2000 }
2001
Elliott Hughes75770752011-08-24 17:52:38 -07002002 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002003 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002004 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002005 }
2006
Elliott Hughes75770752011-08-24 17:52:38 -07002007 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002008 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002009 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002010 }
2011
Elliott Hughes75770752011-08-24 17:52:38 -07002012 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002014 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 }
2016
Elliott Hughes75770752011-08-24 17:52:38 -07002017 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002019 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
Elliott Hughes75770752011-08-24 17:52:38 -07002022 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002024 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 }
2026
Elliott Hughes75770752011-08-24 17:52:38 -07002027 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002029 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Elliott Hughes75770752011-08-24 17:52:38 -07002032 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002034 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Elliott Hughes75770752011-08-24 17:52:38 -07002037 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002039 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Elliott Hughes75770752011-08-24 17:52:38 -07002042 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002044 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Elliott Hughes75770752011-08-24 17:52:38 -07002047 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002049 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Elliott Hughes75770752011-08-24 17:52:38 -07002052 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002054 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes75770752011-08-24 17:52:38 -07002057 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002059 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Elliott Hughes75770752011-08-24 17:52:38 -07002062 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002064 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes75770752011-08-24 17:52:38 -07002067 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002069 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Elliott Hughes75770752011-08-24 17:52:38 -07002072 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002074 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes75770752011-08-24 17:52:38 -07002077 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002079 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Elliott Hughes75770752011-08-24 17:52:38 -07002082 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002084 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Elliott Hughes75770752011-08-24 17:52:38 -07002087 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002089 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
Elliott Hughes814e4032011-08-23 12:07:56 -07002092 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 }
2096
Elliott Hughes814e4032011-08-23 12:07:56 -07002097 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 }
2101
Elliott Hughes814e4032011-08-23 12:07:56 -07002102 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 }
2106
Elliott Hughes814e4032011-08-23 12:07:56 -07002107 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
Elliott Hughes814e4032011-08-23 12:07:56 -07002112 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 }
2116
Elliott Hughes814e4032011-08-23 12:07:56 -07002117 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
Elliott Hughes814e4032011-08-23 12:07:56 -07002122 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002124 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
Elliott Hughes814e4032011-08-23 12:07:56 -07002127 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002129 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
Elliott Hughes814e4032011-08-23 12:07:56 -07002132 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002134 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 }
2136
Elliott Hughes814e4032011-08-23 12:07:56 -07002137 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002139 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 }
2141
Elliott Hughes814e4032011-08-23 12:07:56 -07002142 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002144 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 }
2146
Elliott Hughes814e4032011-08-23 12:07:56 -07002147 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002149 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 }
2151
Elliott Hughes814e4032011-08-23 12:07:56 -07002152 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002154 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
2156
Elliott Hughes814e4032011-08-23 12:07:56 -07002157 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002159 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 }
2161
Elliott Hughes814e4032011-08-23 12:07:56 -07002162 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002164 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Elliott Hughes814e4032011-08-23 12:07:56 -07002167 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002169 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Elliott Hughes5174fe62011-08-23 15:12:35 -07002172 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002174 Class* c = Decode<Class*>(ts, java_class);
2175
Elliott Hughes5174fe62011-08-23 15:12:35 -07002176 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 const char* name = methods[i].name;
2178 const char* sig = methods[i].signature;
2179
2180 if (*sig == '!') {
2181 // TODO: fast jni. it's too noisy to log all these.
2182 ++sig;
2183 }
2184
Elliott Hughes5174fe62011-08-23 15:12:35 -07002185 Method* m = c->FindDirectMethod(name, sig);
2186 if (m == NULL) {
2187 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002189 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002190 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002191 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002193 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002194 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002195 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 return JNI_ERR;
2197 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002198
Elliott Hughes75770752011-08-24 17:52:38 -07002199 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002200 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002201 }
2202
2203 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 }
2205 return JNI_OK;
2206 }
2207
Elliott Hughes5174fe62011-08-23 15:12:35 -07002208 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002210 Class* c = Decode<Class*>(ts, java_class);
2211
Elliott Hughes75770752011-08-24 17:52:38 -07002212 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002213 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002214 }
2215
2216 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2217 Method* m = c->GetDirectMethod(i);
2218 if (m->IsNative()) {
2219 m->UnregisterNative();
2220 }
2221 }
2222 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2223 Method* m = c->GetVirtualMethod(i);
2224 if (m->IsNative()) {
2225 m->UnregisterNative();
2226 }
2227 }
2228
2229 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
2231
Elliott Hughes72025e52011-08-23 17:50:30 -07002232 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002234 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002235 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 }
2237
Elliott Hughes72025e52011-08-23 17:50:30 -07002238 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002240 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002241 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
2244 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2245 ScopedJniThreadState ts(env);
2246 Runtime* runtime = Runtime::Current();
2247 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002248 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 } else {
2250 *vm = NULL;
2251 }
2252 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2253 }
2254
Elliott Hughescdf53122011-08-19 15:46:09 -07002255 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2256 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002257
2258 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002259 CHECK(address != NULL); // TODO: ReportJniError
2260 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002261
2262 jclass buffer_class = GetDirectByteBufferClass(env);
2263 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2264 if (mid == NULL) {
2265 return NULL;
2266 }
2267
2268 // At the moment, the Java side is limited to 32 bits.
2269 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2270 CHECK_LE(capacity, 0xffffffff);
2271 jint address_arg = reinterpret_cast<jint>(address);
2272 jint capacity_arg = static_cast<jint>(capacity);
2273
2274 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2275 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002276 }
2277
Elliott Hughesb465ab02011-08-24 11:21:21 -07002278 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002279 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002280 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2281 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002282 }
2283
Elliott Hughesb465ab02011-08-24 11:21:21 -07002284 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002286 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2287 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002288 }
2289
Elliott Hughesb465ab02011-08-24 11:21:21 -07002290 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002292
Elliott Hughes75770752011-08-24 17:52:38 -07002293 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002294
2295 // Do we definitely know what kind of reference this is?
2296 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2297 IndirectRefKind kind = GetIndirectRefKind(ref);
2298 switch (kind) {
2299 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002300 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2301 return JNILocalRefType;
2302 }
2303 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002304 case kGlobal:
2305 return JNIGlobalRefType;
2306 case kWeakGlobal:
2307 return JNIWeakGlobalRefType;
2308 case kSirtOrInvalid:
2309 // Is it in a stack IRT?
2310 if (ts.Self()->SirtContains(java_object)) {
2311 return JNILocalRefType;
2312 }
2313
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002314 if (!ts.Env()->work_around_app_jni_bugs) {
2315 return JNIInvalidRefType;
2316 }
2317
Elliott Hughesb465ab02011-08-24 11:21:21 -07002318 // If we're handing out direct pointers, check whether it's a direct pointer
2319 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002320 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002321 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002322 return JNILocalRefType;
2323 }
2324 }
2325
2326 return JNIInvalidRefType;
2327 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002328 }
2329};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002330
Elliott Hughesa2501992011-08-26 19:39:54 -07002331const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002332 NULL, // reserved0.
2333 NULL, // reserved1.
2334 NULL, // reserved2.
2335 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002336 JNI::GetVersion,
2337 JNI::DefineClass,
2338 JNI::FindClass,
2339 JNI::FromReflectedMethod,
2340 JNI::FromReflectedField,
2341 JNI::ToReflectedMethod,
2342 JNI::GetSuperclass,
2343 JNI::IsAssignableFrom,
2344 JNI::ToReflectedField,
2345 JNI::Throw,
2346 JNI::ThrowNew,
2347 JNI::ExceptionOccurred,
2348 JNI::ExceptionDescribe,
2349 JNI::ExceptionClear,
2350 JNI::FatalError,
2351 JNI::PushLocalFrame,
2352 JNI::PopLocalFrame,
2353 JNI::NewGlobalRef,
2354 JNI::DeleteGlobalRef,
2355 JNI::DeleteLocalRef,
2356 JNI::IsSameObject,
2357 JNI::NewLocalRef,
2358 JNI::EnsureLocalCapacity,
2359 JNI::AllocObject,
2360 JNI::NewObject,
2361 JNI::NewObjectV,
2362 JNI::NewObjectA,
2363 JNI::GetObjectClass,
2364 JNI::IsInstanceOf,
2365 JNI::GetMethodID,
2366 JNI::CallObjectMethod,
2367 JNI::CallObjectMethodV,
2368 JNI::CallObjectMethodA,
2369 JNI::CallBooleanMethod,
2370 JNI::CallBooleanMethodV,
2371 JNI::CallBooleanMethodA,
2372 JNI::CallByteMethod,
2373 JNI::CallByteMethodV,
2374 JNI::CallByteMethodA,
2375 JNI::CallCharMethod,
2376 JNI::CallCharMethodV,
2377 JNI::CallCharMethodA,
2378 JNI::CallShortMethod,
2379 JNI::CallShortMethodV,
2380 JNI::CallShortMethodA,
2381 JNI::CallIntMethod,
2382 JNI::CallIntMethodV,
2383 JNI::CallIntMethodA,
2384 JNI::CallLongMethod,
2385 JNI::CallLongMethodV,
2386 JNI::CallLongMethodA,
2387 JNI::CallFloatMethod,
2388 JNI::CallFloatMethodV,
2389 JNI::CallFloatMethodA,
2390 JNI::CallDoubleMethod,
2391 JNI::CallDoubleMethodV,
2392 JNI::CallDoubleMethodA,
2393 JNI::CallVoidMethod,
2394 JNI::CallVoidMethodV,
2395 JNI::CallVoidMethodA,
2396 JNI::CallNonvirtualObjectMethod,
2397 JNI::CallNonvirtualObjectMethodV,
2398 JNI::CallNonvirtualObjectMethodA,
2399 JNI::CallNonvirtualBooleanMethod,
2400 JNI::CallNonvirtualBooleanMethodV,
2401 JNI::CallNonvirtualBooleanMethodA,
2402 JNI::CallNonvirtualByteMethod,
2403 JNI::CallNonvirtualByteMethodV,
2404 JNI::CallNonvirtualByteMethodA,
2405 JNI::CallNonvirtualCharMethod,
2406 JNI::CallNonvirtualCharMethodV,
2407 JNI::CallNonvirtualCharMethodA,
2408 JNI::CallNonvirtualShortMethod,
2409 JNI::CallNonvirtualShortMethodV,
2410 JNI::CallNonvirtualShortMethodA,
2411 JNI::CallNonvirtualIntMethod,
2412 JNI::CallNonvirtualIntMethodV,
2413 JNI::CallNonvirtualIntMethodA,
2414 JNI::CallNonvirtualLongMethod,
2415 JNI::CallNonvirtualLongMethodV,
2416 JNI::CallNonvirtualLongMethodA,
2417 JNI::CallNonvirtualFloatMethod,
2418 JNI::CallNonvirtualFloatMethodV,
2419 JNI::CallNonvirtualFloatMethodA,
2420 JNI::CallNonvirtualDoubleMethod,
2421 JNI::CallNonvirtualDoubleMethodV,
2422 JNI::CallNonvirtualDoubleMethodA,
2423 JNI::CallNonvirtualVoidMethod,
2424 JNI::CallNonvirtualVoidMethodV,
2425 JNI::CallNonvirtualVoidMethodA,
2426 JNI::GetFieldID,
2427 JNI::GetObjectField,
2428 JNI::GetBooleanField,
2429 JNI::GetByteField,
2430 JNI::GetCharField,
2431 JNI::GetShortField,
2432 JNI::GetIntField,
2433 JNI::GetLongField,
2434 JNI::GetFloatField,
2435 JNI::GetDoubleField,
2436 JNI::SetObjectField,
2437 JNI::SetBooleanField,
2438 JNI::SetByteField,
2439 JNI::SetCharField,
2440 JNI::SetShortField,
2441 JNI::SetIntField,
2442 JNI::SetLongField,
2443 JNI::SetFloatField,
2444 JNI::SetDoubleField,
2445 JNI::GetStaticMethodID,
2446 JNI::CallStaticObjectMethod,
2447 JNI::CallStaticObjectMethodV,
2448 JNI::CallStaticObjectMethodA,
2449 JNI::CallStaticBooleanMethod,
2450 JNI::CallStaticBooleanMethodV,
2451 JNI::CallStaticBooleanMethodA,
2452 JNI::CallStaticByteMethod,
2453 JNI::CallStaticByteMethodV,
2454 JNI::CallStaticByteMethodA,
2455 JNI::CallStaticCharMethod,
2456 JNI::CallStaticCharMethodV,
2457 JNI::CallStaticCharMethodA,
2458 JNI::CallStaticShortMethod,
2459 JNI::CallStaticShortMethodV,
2460 JNI::CallStaticShortMethodA,
2461 JNI::CallStaticIntMethod,
2462 JNI::CallStaticIntMethodV,
2463 JNI::CallStaticIntMethodA,
2464 JNI::CallStaticLongMethod,
2465 JNI::CallStaticLongMethodV,
2466 JNI::CallStaticLongMethodA,
2467 JNI::CallStaticFloatMethod,
2468 JNI::CallStaticFloatMethodV,
2469 JNI::CallStaticFloatMethodA,
2470 JNI::CallStaticDoubleMethod,
2471 JNI::CallStaticDoubleMethodV,
2472 JNI::CallStaticDoubleMethodA,
2473 JNI::CallStaticVoidMethod,
2474 JNI::CallStaticVoidMethodV,
2475 JNI::CallStaticVoidMethodA,
2476 JNI::GetStaticFieldID,
2477 JNI::GetStaticObjectField,
2478 JNI::GetStaticBooleanField,
2479 JNI::GetStaticByteField,
2480 JNI::GetStaticCharField,
2481 JNI::GetStaticShortField,
2482 JNI::GetStaticIntField,
2483 JNI::GetStaticLongField,
2484 JNI::GetStaticFloatField,
2485 JNI::GetStaticDoubleField,
2486 JNI::SetStaticObjectField,
2487 JNI::SetStaticBooleanField,
2488 JNI::SetStaticByteField,
2489 JNI::SetStaticCharField,
2490 JNI::SetStaticShortField,
2491 JNI::SetStaticIntField,
2492 JNI::SetStaticLongField,
2493 JNI::SetStaticFloatField,
2494 JNI::SetStaticDoubleField,
2495 JNI::NewString,
2496 JNI::GetStringLength,
2497 JNI::GetStringChars,
2498 JNI::ReleaseStringChars,
2499 JNI::NewStringUTF,
2500 JNI::GetStringUTFLength,
2501 JNI::GetStringUTFChars,
2502 JNI::ReleaseStringUTFChars,
2503 JNI::GetArrayLength,
2504 JNI::NewObjectArray,
2505 JNI::GetObjectArrayElement,
2506 JNI::SetObjectArrayElement,
2507 JNI::NewBooleanArray,
2508 JNI::NewByteArray,
2509 JNI::NewCharArray,
2510 JNI::NewShortArray,
2511 JNI::NewIntArray,
2512 JNI::NewLongArray,
2513 JNI::NewFloatArray,
2514 JNI::NewDoubleArray,
2515 JNI::GetBooleanArrayElements,
2516 JNI::GetByteArrayElements,
2517 JNI::GetCharArrayElements,
2518 JNI::GetShortArrayElements,
2519 JNI::GetIntArrayElements,
2520 JNI::GetLongArrayElements,
2521 JNI::GetFloatArrayElements,
2522 JNI::GetDoubleArrayElements,
2523 JNI::ReleaseBooleanArrayElements,
2524 JNI::ReleaseByteArrayElements,
2525 JNI::ReleaseCharArrayElements,
2526 JNI::ReleaseShortArrayElements,
2527 JNI::ReleaseIntArrayElements,
2528 JNI::ReleaseLongArrayElements,
2529 JNI::ReleaseFloatArrayElements,
2530 JNI::ReleaseDoubleArrayElements,
2531 JNI::GetBooleanArrayRegion,
2532 JNI::GetByteArrayRegion,
2533 JNI::GetCharArrayRegion,
2534 JNI::GetShortArrayRegion,
2535 JNI::GetIntArrayRegion,
2536 JNI::GetLongArrayRegion,
2537 JNI::GetFloatArrayRegion,
2538 JNI::GetDoubleArrayRegion,
2539 JNI::SetBooleanArrayRegion,
2540 JNI::SetByteArrayRegion,
2541 JNI::SetCharArrayRegion,
2542 JNI::SetShortArrayRegion,
2543 JNI::SetIntArrayRegion,
2544 JNI::SetLongArrayRegion,
2545 JNI::SetFloatArrayRegion,
2546 JNI::SetDoubleArrayRegion,
2547 JNI::RegisterNatives,
2548 JNI::UnregisterNatives,
2549 JNI::MonitorEnter,
2550 JNI::MonitorExit,
2551 JNI::GetJavaVM,
2552 JNI::GetStringRegion,
2553 JNI::GetStringUTFRegion,
2554 JNI::GetPrimitiveArrayCritical,
2555 JNI::ReleasePrimitiveArrayCritical,
2556 JNI::GetStringCritical,
2557 JNI::ReleaseStringCritical,
2558 JNI::NewWeakGlobalRef,
2559 JNI::DeleteWeakGlobalRef,
2560 JNI::ExceptionCheck,
2561 JNI::NewDirectByteBuffer,
2562 JNI::GetDirectBufferAddress,
2563 JNI::GetDirectBufferCapacity,
2564 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002565};
2566
Elliott Hughes75770752011-08-24 17:52:38 -07002567JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002568 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002569 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002570 local_ref_cookie(IRT_FIRST_SEGMENT),
2571 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002572 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002573 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002574 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002575 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002576 functions = unchecked_functions = &gNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002577 if (vm->check_jni) {
2578 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002579 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002580 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2581 // errors will ensue.
2582 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2583 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002584}
2585
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002586JNIEnvExt::~JNIEnvExt() {
2587}
2588
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002589void JNIEnvExt::EnableCheckJni() {
2590 check_jni = true;
2591 functions = GetCheckJniNativeInterface();
2592}
2593
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002594void JNIEnvExt::DumpReferenceTables() {
2595 locals.Dump();
2596 monitors.Dump();
2597}
2598
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002599void JNIEnvExt::PushFrame(int capacity) {
2600 stacked_local_ref_cookies.push_back(local_ref_cookie);
2601 local_ref_cookie = locals.GetSegmentState();
2602}
2603
2604void JNIEnvExt::PopFrame() {
2605 locals.SetSegmentState(local_ref_cookie);
2606 local_ref_cookie = stacked_local_ref_cookies.back();
2607 stacked_local_ref_cookies.pop_back();
2608}
2609
Carl Shapiroea4dca82011-08-01 13:45:38 -07002610// JNI Invocation interface.
2611
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002612extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2613 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2614 if (args->version < JNI_VERSION_1_2) {
2615 return JNI_EVERSION;
2616 }
2617 Runtime::Options options;
2618 for (int i = 0; i < args->nOptions; ++i) {
2619 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002620 options.push_back(std::make_pair(StringPiece(option->optionString),
2621 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002622 }
2623 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002624 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002625 if (runtime == NULL) {
2626 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002627 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002628 runtime->Start();
2629 *p_env = Thread::Current()->GetJniEnv();
2630 *p_vm = runtime->GetJavaVM();
2631 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002632}
2633
Elliott Hughesf2682d52011-08-15 16:37:04 -07002634extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002635 Runtime* runtime = Runtime::Current();
2636 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002637 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002638 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002639 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002640 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002641 }
2642 return JNI_OK;
2643}
2644
2645// Historically unsupported.
2646extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2647 return JNI_ERR;
2648}
2649
Elliott Hughescdf53122011-08-19 15:46:09 -07002650class JII {
2651 public:
2652 static jint DestroyJavaVM(JavaVM* vm) {
2653 if (vm == NULL) {
2654 return JNI_ERR;
2655 } else {
2656 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2657 delete raw_vm->runtime;
2658 return JNI_OK;
2659 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002660 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002661
Elliott Hughescdf53122011-08-19 15:46:09 -07002662 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002663 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002664 }
2665
2666 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002667 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002668 }
2669
2670 static jint DetachCurrentThread(JavaVM* vm) {
2671 if (vm == NULL) {
2672 return JNI_ERR;
2673 } else {
2674 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2675 Runtime* runtime = raw_vm->runtime;
2676 runtime->DetachCurrentThread();
2677 return JNI_OK;
2678 }
2679 }
2680
2681 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2682 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2683 return JNI_EVERSION;
2684 }
2685 if (vm == NULL || env == NULL) {
2686 return JNI_ERR;
2687 }
2688 Thread* thread = Thread::Current();
2689 if (thread == NULL) {
2690 *env = NULL;
2691 return JNI_EDETACHED;
2692 }
2693 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002694 return JNI_OK;
2695 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002696};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002697
Elliott Hughesa2501992011-08-26 19:39:54 -07002698const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002699 NULL, // reserved0
2700 NULL, // reserved1
2701 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002702 JII::DestroyJavaVM,
2703 JII::AttachCurrentThread,
2704 JII::DetachCurrentThread,
2705 JII::GetEnv,
2706 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002707};
2708
Elliott Hughesa0957642011-09-02 14:27:33 -07002709JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002710 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002711 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002712 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002713 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002714 verbose_jni(options->IsVerbose("jni")),
2715 log_third_party_jni(options->IsVerbose("third-party-jni")),
2716 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002717 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002718 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002719 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002720 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002721 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002722 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002723 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002724 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002725 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002726 functions = unchecked_functions = &gInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002727 if (options->check_jni_) {
2728 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002729 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002730}
2731
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002732JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002733 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002734}
2735
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002736void JavaVMExt::EnableCheckJni() {
2737 check_jni = true;
2738 functions = GetCheckJniInvokeInterface();
2739}
2740
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002741void JavaVMExt::DumpReferenceTables() {
2742 {
2743 MutexLock mu(globals_lock);
2744 globals.Dump();
2745 }
2746 {
2747 MutexLock mu(weak_globals_lock);
2748 weak_globals.Dump();
2749 }
2750 {
2751 MutexLock mu(pins_lock);
2752 pin_table.Dump();
2753 }
2754}
2755
Elliott Hughes75770752011-08-24 17:52:38 -07002756bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2757 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002758
2759 // See if we've already loaded this library. If we have, and the class loader
2760 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002761 // TODO: for better results we should canonicalize the pathname (or even compare
2762 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002763 SharedLibrary* library;
2764 {
2765 // TODO: move the locking (and more of this logic) into Libraries.
2766 MutexLock mu(libraries_lock);
2767 library = libraries->Get(path);
2768 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002769 if (library != NULL) {
2770 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002771 // The library will be associated with class_loader. The JNI
2772 // spec says we can't load the same library into more than one
2773 // class loader.
2774 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2775 "ClassLoader %p; can't open in ClassLoader %p",
2776 path.c_str(), library->GetClassLoader(), class_loader);
2777 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002778 return false;
2779 }
2780 if (verbose_jni) {
2781 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2782 << "ClassLoader " << class_loader << "]";
2783 }
2784 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002785 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2786 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002787 return false;
2788 }
2789 return true;
2790 }
2791
2792 // Open the shared library. Because we're using a full path, the system
2793 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2794 // resolve this library's dependencies though.)
2795
2796 // Failures here are expected when java.library.path has several entries
2797 // and we have to hunt for the lib.
2798
2799 // The current version of the dynamic linker prints detailed information
2800 // about dlopen() failures. Some things to check if the message is
2801 // cryptic:
2802 // - make sure the library exists on the device
2803 // - verify that the right path is being opened (the debug log message
2804 // above can help with that)
2805 // - check to see if the library is valid (e.g. not zero bytes long)
2806 // - check config/prelink-linux-arm.map to ensure that the library
2807 // is listed and is not being overrun by the previous entry (if
2808 // loading suddenly stops working on a prelinked library, this is
2809 // a good one to check)
2810 // - write a trivial app that calls sleep() then dlopen(), attach
2811 // to it with "strace -p <pid>" while it sleeps, and watch for
2812 // attempts to open nonexistent dependent shared libs
2813
2814 // TODO: automate some of these checks!
2815
2816 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002817 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002818 // the GC to ignore us.
2819 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002820 void* handle = NULL;
2821 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002822 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002823 handle = dlopen(path.c_str(), RTLD_LAZY);
2824 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002825
2826 if (verbose_jni) {
2827 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2828 }
2829
2830 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002831 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002832 return false;
2833 }
2834
2835 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002836 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002837 // TODO: move the locking (and more of this logic) into Libraries.
2838 MutexLock mu(libraries_lock);
2839 library = libraries->Get(path);
2840 if (library != NULL) {
2841 LOG(INFO) << "WOW: we lost a race to add shared library: "
2842 << "\"" << path << "\" ClassLoader=" << class_loader;
2843 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002844 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002845 library = new SharedLibrary(path, handle, class_loader);
2846 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002847 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002848
2849 if (verbose_jni) {
2850 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2851 }
2852
2853 bool result = true;
2854 void* sym = dlsym(handle, "JNI_OnLoad");
2855 if (sym == NULL) {
2856 if (verbose_jni) {
2857 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2858 }
2859 } else {
2860 // Call JNI_OnLoad. We have to override the current class
2861 // loader, which will always be "null" since the stuff at the
2862 // top of the stack is around Runtime.loadLibrary(). (See
2863 // the comments in the JNI FindClass function.)
2864 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2865 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002866 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002867 self->SetClassLoaderOverride(class_loader);
2868
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002869 int version = 0;
2870 {
2871 ScopedThreadStateChange tsc(self, Thread::kNative);
2872 if (verbose_jni) {
2873 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2874 }
2875 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002876 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002877
Brian Carlstromaded5f72011-10-07 17:15:04 -07002878 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002879
2880 if (version != JNI_VERSION_1_2 &&
2881 version != JNI_VERSION_1_4 &&
2882 version != JNI_VERSION_1_6) {
2883 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2884 << "bad version: " << version;
2885 // It's unwise to call dlclose() here, but we can mark it
2886 // as bad and ensure that future load attempts will fail.
2887 // We don't know how far JNI_OnLoad got, so there could
2888 // be some partially-initialized stuff accessible through
2889 // newly-registered native method calls. We could try to
2890 // unregister them, but that doesn't seem worthwhile.
2891 result = false;
2892 } else {
2893 if (verbose_jni) {
2894 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2895 << " from JNI_OnLoad in \"" << path << "\"]";
2896 }
2897 }
2898 }
2899
2900 library->SetResult(result);
2901 return result;
2902}
2903
2904void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2905 CHECK(m->IsNative());
2906
2907 Class* c = m->GetDeclaringClass();
2908
2909 // If this is a static method, it could be called before the class
2910 // has been initialized.
2911 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002912 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002913 return NULL;
2914 }
2915 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002916 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002917 }
2918
Brian Carlstrom16192862011-09-12 17:50:06 -07002919 std::string detail;
2920 void* native_method;
2921 {
2922 MutexLock mu(libraries_lock);
2923 native_method = libraries->FindNativeMethod(m, detail);
2924 }
2925 // throwing can cause libraries_lock to be reacquired
2926 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002927 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002928 }
2929 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002930}
2931
Elliott Hughes410c0c82011-09-01 17:58:25 -07002932void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2933 {
2934 MutexLock mu(globals_lock);
2935 globals.VisitRoots(visitor, arg);
2936 }
2937 {
2938 MutexLock mu(pins_lock);
2939 pin_table.VisitRoots(visitor, arg);
2940 }
2941 // The weak_globals table is visited by the GC itself (because it mutates the table).
2942}
2943
Ian Rogersdf20fe02011-07-20 20:34:16 -07002944} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002945
2946std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2947 switch (rhs) {
2948 case JNIInvalidRefType:
2949 os << "JNIInvalidRefType";
2950 return os;
2951 case JNILocalRefType:
2952 os << "JNILocalRefType";
2953 return os;
2954 case JNIGlobalRefType:
2955 os << "JNIGlobalRefType";
2956 return os;
2957 case JNIWeakGlobalRefType:
2958 os << "JNIWeakGlobalRefType";
2959 return os;
2960 }
2961}