blob: 5385ae2ad3f0bfcdb029ebbbb356368560ebbd85 [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 Hughesc2dc62d2012-01-17 20:06:12 -080097 if (env->vm->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}
Shih-wei Liao24782c62012-01-08 12:46:11 -0800129// TODO: Change to use template when Mac OS build server no longer uses GCC 4.2.*.
130Object* DecodeObj(JNIEnv* public_env, jobject obj) {
131 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
132 return reinterpret_cast<Object*>(env->self->DecodeJObject(obj));
133}
Elliott Hughesbf86d042011-08-31 17:53:14 -0700134// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700135template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700136template Class* Decode<Class*>(JNIEnv*, jobject);
137template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
138template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700139template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700140template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700141template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700142template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400143template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700144template String* Decode<String*>(JNIEnv*, jobject);
145template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700146
147namespace {
148
Elliott Hughescdf53122011-08-19 15:46:09 -0700149jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
150 if (obj == NULL) {
151 return NULL;
152 }
Elliott Hughes75770752011-08-24 17:52:38 -0700153 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700154 IndirectReferenceTable& weak_globals = vm->weak_globals;
155 MutexLock mu(vm->weak_globals_lock);
156 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
157 return reinterpret_cast<jweak>(ref);
158}
159
Elliott Hughesbf86d042011-08-31 17:53:14 -0700160// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700161template<typename T>
162T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700163 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700164}
165
Ian Rogers0571d352011-11-03 19:51:38 -0700166static byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700167 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800168 const char* shorty = MethodHelper(method).GetShorty();
169 size_t shorty_len = strlen(shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700170 size_t num_bytes = NumArgArrayBytes(shorty);
171 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800172 for (size_t i = 1, offset = 0; i < shorty_len; ++i) {
173 switch (shorty[i]) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700174 case 'Z':
175 case 'B':
176 case 'C':
177 case 'S':
178 case 'I':
179 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
180 offset += 4;
181 break;
182 case 'F':
183 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
184 offset += 4;
185 break;
186 case 'L': {
Shih-wei Liao24782c62012-01-08 12:46:11 -0800187 Object* obj = DecodeObj(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700188 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
189 offset += sizeof(Object*);
190 break;
191 }
192 case 'D':
193 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
194 offset += 8;
195 break;
196 case 'J':
197 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
198 offset += 8;
199 break;
200 }
201 }
202 return arg_array.release();
203}
204
Ian Rogers0571d352011-11-03 19:51:38 -0700205static byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700206 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800207 const char* shorty = MethodHelper(method).GetShorty();
208 size_t shorty_len = strlen(shorty);
209 size_t num_bytes = NumArgArrayBytes(shorty);
Elliott Hughes90a33692011-08-30 13:27:07 -0700210 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800211 for (size_t i = 1, offset = 0; i < shorty_len; ++i) {
212 switch (shorty[i]) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700213 case 'Z':
214 case 'B':
215 case 'C':
216 case 'S':
217 case 'I':
218 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
219 offset += 4;
220 break;
221 case 'F':
222 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
223 offset += 4;
224 break;
225 case 'L': {
Shih-wei Liao24782c62012-01-08 12:46:11 -0800226 Object* obj = DecodeObj(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700227 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
228 offset += sizeof(Object*);
229 break;
230 }
231 case 'D':
232 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
233 offset += 8;
234 break;
235 case 'J':
236 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
237 offset += 8;
238 break;
239 }
240 }
241 return arg_array.release();
242}
243
Elliott Hughesd07986f2011-12-06 18:27:45 -0800244static byte* CreateArgArray(Method* method, JValue* args) {
245 const char* shorty = MethodHelper(method).GetShorty();
246 size_t shorty_len = strlen(shorty);
247 size_t num_bytes = NumArgArrayBytes(shorty);
248 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
249 for (size_t i = 1, offset = 0; i < shorty_len; ++i) {
250 switch (shorty[i]) {
251 case 'Z':
252 case 'B':
253 case 'C':
254 case 'S':
255 case 'I':
256 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
257 offset += 4;
258 break;
259 case 'F':
260 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
261 offset += 4;
262 break;
263 case 'L':
264 *reinterpret_cast<Object**>(&arg_array[offset]) = args[i - 1].l;
265 offset += sizeof(Object*);
266 break;
267 case 'D':
268 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
269 offset += 8;
270 break;
271 case 'J':
272 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
273 offset += 8;
274 break;
275 }
276 }
277 return arg_array.release();
278}
279
Ian Rogers0571d352011-11-03 19:51:38 -0700280static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700281 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700282 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700283 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700284 return result;
285}
286
Ian Rogers0571d352011-11-03 19:51:38 -0700287static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700288 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800289 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700290 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700291 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
292 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700293}
294
Ian Rogers0571d352011-11-03 19:51:38 -0700295static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700296 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700297}
298
Ian Rogers0571d352011-11-03 19:51:38 -0700299static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
300 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700301 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800302 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700303 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700304 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
305 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700306}
307
Ian Rogers0571d352011-11-03 19:51:38 -0700308static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
309 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700310 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800311 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700312 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700313 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
314 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700315}
316
Elliott Hughes6b436852011-08-12 10:16:44 -0700317// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
318// separated with slashes but aren't wrapped with "L;" like regular descriptors
319// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
320// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
321// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700322static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700323 std::string result;
324 // Add the missing "L;" if necessary.
325 if (name[0] == '[') {
326 result = name;
327 } else {
328 result += 'L';
329 result += name;
330 result += ';';
331 }
332 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700333 if (result.find('.') != std::string::npos) {
334 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
335 << "\"" << name << "\"";
336 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700337 }
338 return result;
339}
340
Ian Rogers0571d352011-11-03 19:51:38 -0700341static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700342 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800343 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700344}
345
Ian Rogers0571d352011-11-03 19:51:38 -0700346static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700347 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700348 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700349 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700350 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700351
352 Method* method = NULL;
353 if (is_static) {
354 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700355 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700356 method = c->FindVirtualMethod(name, sig);
357 if (method == NULL) {
358 // No virtual method matching the signature. Search declared
359 // private methods and constructors.
360 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700361 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700362 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700363
Elliott Hughescdf53122011-08-19 15:46:09 -0700364 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700365 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700366 return NULL;
367 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700368
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700369 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700370}
371
Ian Rogers0571d352011-11-03 19:51:38 -0700372static const ClassLoader* GetClassLoader(Thread* self) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700373 Frame frame = self->GetTopOfStack();
374 Method* method = frame.GetMethod();
375 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
376 return self->GetClassLoaderOverride();
377 }
378 return method->GetDeclaringClass()->GetClassLoader();
379}
380
Ian Rogers0571d352011-11-03 19:51:38 -0700381static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700382 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700383 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700384 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700385 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700386
387 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 Class* field_type;
389 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
390 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700391 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700393 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700394 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700395 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700396 if (field_type == NULL) {
397 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700398 DCHECK(ts.Self()->IsExceptionPending());
399 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700400 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700401 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800402 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700403 return NULL;
404 }
405 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800406 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700407 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800408 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700409 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700410 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700411 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700412 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800413 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700414 return NULL;
415 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700416 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700417}
418
Ian Rogers0571d352011-11-03 19:51:38 -0700419static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700420 JavaVMExt* vm = ts.Vm();
421 MutexLock mu(vm->pins_lock);
422 vm->pin_table.Add(array);
423}
424
Ian Rogers0571d352011-11-03 19:51:38 -0700425static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700426 JavaVMExt* vm = ts.Vm();
427 MutexLock mu(vm->pins_lock);
428 vm->pin_table.Remove(array);
429}
430
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700431template<typename JniT, typename ArtT>
432JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
433 CHECK_GE(length, 0); // TODO: ReportJniError
434 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700435 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700436}
437
Elliott Hughes75770752011-08-24 17:52:38 -0700438template <typename ArrayT, typename CArrayT, typename ArtArrayT>
439CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
440 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
441 PinPrimitiveArray(ts, array);
442 if (is_copy != NULL) {
443 *is_copy = JNI_FALSE;
444 }
445 return array->GetData();
446}
447
448template <typename ArrayT>
449void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
450 if (mode != JNI_COMMIT) {
451 Array* array = Decode<Array*>(ts, java_array);
452 UnpinPrimitiveArray(ts, array);
453 }
454}
455
Ian Rogers0571d352011-11-03 19:51:38 -0700456static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700457 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700458 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700459 "%s offset=%d length=%d %s.length=%d",
460 type.c_str(), start, length, identifier, array->GetLength());
461}
Ian Rogers0571d352011-11-03 19:51:38 -0700462
463static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700464 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700465 "offset=%d length=%d string.length()=%d", start, length, array_length);
466}
Elliott Hughes814e4032011-08-23 12:07:56 -0700467
468template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700469void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700470 ArrayT* array = Decode<ArrayT*>(ts, java_array);
471 if (start < 0 || length < 0 || start + length > array->GetLength()) {
472 ThrowAIOOBE(ts, array, start, length, "src");
473 } else {
474 JavaT* data = array->GetData();
475 memcpy(buf, data + start, length * sizeof(JavaT));
476 }
477}
478
479template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700480void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700481 ArrayT* array = Decode<ArrayT*>(ts, java_array);
482 if (start < 0 || length < 0 || start + length > array->GetLength()) {
483 ThrowAIOOBE(ts, array, start, length, "dst");
484 } else {
485 JavaT* data = array->GetData();
486 memcpy(data + start, buf, length * sizeof(JavaT));
487 }
488}
489
Ian Rogers0571d352011-11-03 19:51:38 -0700490static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700491 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
492 CHECK(buffer_class.get() != NULL);
493 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
494}
495
Ian Rogers0571d352011-11-03 19:51:38 -0700496static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700497 static jclass buffer_class = InitDirectByteBufferClass(env);
498 return buffer_class;
499}
500
Ian Rogers0571d352011-11-03 19:51:38 -0700501static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700502 if (vm == NULL || p_env == NULL) {
503 return JNI_ERR;
504 }
505
Elliott Hughescac6cc72011-11-03 20:31:21 -0700506 // Return immediately if we're already one with the VM.
507 Thread* self = Thread::Current();
508 if (self != NULL) {
509 *p_env = self->GetJniEnv();
510 return JNI_OK;
511 }
512
513 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
514
515 // No threads allowed in zygote mode.
516 if (runtime->IsZygote()) {
517 LOG(ERROR) << "Attempt to attach a thread in the zygote";
518 return JNI_ERR;
519 }
520
Elliott Hughes75770752011-08-24 17:52:38 -0700521 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
522 JavaVMAttachArgs args;
523 if (thr_args == NULL) {
524 // Allow the v1.1 calling convention.
525 args.version = JNI_VERSION_1_2;
526 args.name = NULL;
527 args.group = NULL; // TODO: get "main" thread group
528 } else {
529 args.version = in_args->version;
530 args.name = in_args->name;
531 if (in_args->group != NULL) {
532 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
533 args.group = NULL; // TODO: decode in_args->group
534 } else {
535 args.group = NULL; // TODO: get "main" thread group
536 }
537 }
538 CHECK_GE(args.version, JNI_VERSION_1_2);
539
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700540 runtime->AttachCurrentThread(args.name, as_daemon);
541 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700542 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700543}
544
Elliott Hughes79082e32011-08-25 12:07:32 -0700545class SharedLibrary {
546 public:
547 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
548 : path_(path),
549 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700550 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700551 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700552 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700553 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700555 }
556
Elliott Hughes79082e32011-08-25 12:07:32 -0700557 Object* GetClassLoader() {
558 return class_loader_;
559 }
560
561 std::string GetPath() {
562 return path_;
563 }
564
565 /*
566 * Check the result of an earlier call to JNI_OnLoad on this library. If
567 * the call has not yet finished in another thread, wait for it.
568 */
569 bool CheckOnLoadResult(JavaVMExt* vm) {
570 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700571 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700572 // Check this so we don't end up waiting for ourselves. We need
573 // to return "true" so the caller can continue.
574 LOG(INFO) << *self << " recursive attempt to load library "
575 << "\"" << path_ << "\"";
576 return true;
577 }
578
579 MutexLock mu(jni_on_load_lock_);
580 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800581 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
582 << "JNI_OnLoad...]";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700583 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700584 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700585 }
586
587 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800588 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
589 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 return okay;
591 }
592
593 void SetResult(bool result) {
594 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700595 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700596
597 // Broadcast a wakeup to anybody sleeping on the condition variable.
598 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700599 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 }
601
602 void* FindSymbol(const std::string& symbol_name) {
603 return dlsym(handle_, symbol_name.c_str());
604 }
605
606 private:
607 enum JNI_OnLoadState {
608 kPending,
609 kFailed,
610 kOkay,
611 };
612
613 // Path to library "/system/lib/libjni.so".
614 std::string path_;
615
616 // The void* returned by dlopen(3).
617 void* handle_;
618
619 // The ClassLoader this library is associated with.
620 Object* class_loader_;
621
622 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700623 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700624 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700625 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700626 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700627 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700628 // Result of earlier JNI_OnLoad call.
629 JNI_OnLoadState jni_on_load_result_;
630};
631
Elliott Hughescdf53122011-08-19 15:46:09 -0700632} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700633
Elliott Hughes79082e32011-08-25 12:07:32 -0700634// This exists mainly to keep implementation details out of the header file.
635class Libraries {
636 public:
637 Libraries() {
638 }
639
640 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700641 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700642 }
643
644 SharedLibrary* Get(const std::string& path) {
645 return libraries_[path];
646 }
647
648 void Put(const std::string& path, SharedLibrary* library) {
649 libraries_[path] = library;
650 }
651
652 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700653 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700654 std::string jni_short_name(JniShortName(m));
655 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700656 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700657 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
658 SharedLibrary* library = it->second;
659 if (library->GetClassLoader() != declaring_class_loader) {
660 // We only search libraries loaded by the appropriate ClassLoader.
661 continue;
662 }
663 // Try the short name then the long name...
664 void* fn = library->FindSymbol(jni_short_name);
665 if (fn == NULL) {
666 fn = library->FindSymbol(jni_long_name);
667 }
668 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800669 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
670 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700671 return fn;
672 }
673 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700674 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700675 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700676 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700677 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700678 return NULL;
679 }
680
681 private:
682 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
683
684 std::map<std::string, SharedLibrary*> libraries_;
685};
686
Elliott Hughes418d20f2011-09-22 14:00:39 -0700687JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
688 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
689 Object* receiver = Decode<Object*>(env, obj);
690 Method* method = DecodeMethod(mid);
691 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
692 return InvokeWithArgArray(env, receiver, method, arg_array.get());
693}
694
Elliott Hughesd07986f2011-12-06 18:27:45 -0800695JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
696 UniquePtr<byte[]> arg_array(CreateArgArray(m, args));
697 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, arg_array.get());
698}
699
Elliott Hughescdf53122011-08-19 15:46:09 -0700700class JNI {
701 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700702
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 static jint GetVersion(JNIEnv* env) {
704 ScopedJniThreadState ts(env);
705 return JNI_VERSION_1_6;
706 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700707
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
709 ScopedJniThreadState ts(env);
710 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700711 return NULL;
712 }
713
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 static jclass FindClass(JNIEnv* env, const char* name) {
715 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700716 Runtime* runtime = Runtime::Current();
717 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700719 Class* c = NULL;
720 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700721 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800722 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700723 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800724 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700725 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700726 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700727 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700728
Elliott Hughescdf53122011-08-19 15:46:09 -0700729 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
730 ScopedJniThreadState ts(env);
731 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700732 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700733 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700734
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
736 ScopedJniThreadState ts(env);
737 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700738 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700740
Elliott Hughescdf53122011-08-19 15:46:09 -0700741 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
742 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700743 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700744 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700746
Elliott Hughescdf53122011-08-19 15:46:09 -0700747 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
748 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700749 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700750 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700752
Elliott Hughes37f7a402011-08-22 18:56:01 -0700753 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
754 ScopedJniThreadState ts(env);
755 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700756 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700757 }
758
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700759 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700760 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700761 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700762 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700763 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700764
Elliott Hughes37f7a402011-08-22 18:56:01 -0700765 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700767 Class* c1 = Decode<Class*>(ts, java_class1);
768 Class* c2 = Decode<Class*>(ts, java_class2);
769 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700770 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700771
Elliott Hughes37f7a402011-08-22 18:56:01 -0700772 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700774 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700775 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700776 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700777 return JNI_TRUE;
778 } else {
779 Object* obj = Decode<Object*>(ts, jobj);
780 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700781 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700782 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700784
Elliott Hughes37f7a402011-08-22 18:56:01 -0700785 static jint Throw(JNIEnv* env, jthrowable java_exception) {
786 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700787 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700788 if (exception == NULL) {
789 return JNI_ERR;
790 }
791 ts.Self()->SetException(exception);
792 return JNI_OK;
793 }
794
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700795 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700796 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700797 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700798 jmethodID mid = ((msg != NULL)
799 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
800 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700801 if (mid == NULL) {
802 return JNI_ERR;
803 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700804 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700805 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700806 return JNI_ERR;
807 }
808
809 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700810 args[0].l = s.get();
811 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
812 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700813 return JNI_ERR;
814 }
815
Elliott Hughes72025e52011-08-23 17:50:30 -0700816 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700817
Elliott Hughes37f7a402011-08-22 18:56:01 -0700818 return JNI_OK;
819 }
820
821 static jboolean ExceptionCheck(JNIEnv* env) {
822 ScopedJniThreadState ts(env);
823 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
824 }
825
826 static void ExceptionClear(JNIEnv* env) {
827 ScopedJniThreadState ts(env);
828 ts.Self()->ClearException();
829 }
830
831 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700833
834 Thread* self = ts.Self();
835 Throwable* original_exception = self->GetException();
836 self->ClearException();
837
Elliott Hughesbf86d042011-08-31 17:53:14 -0700838 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700839 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
840 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
841 if (mid == NULL) {
842 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700843 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700844 } else {
845 env->CallVoidMethod(exception.get(), mid);
846 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700847 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700848 << " thrown while calling printStackTrace";
849 self->ClearException();
850 }
851 }
852
853 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700855
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 static jthrowable ExceptionOccurred(JNIEnv* env) {
857 ScopedJniThreadState ts(env);
858 Object* exception = ts.Self()->GetException();
859 if (exception == NULL) {
860 return NULL;
861 } else {
862 // TODO: if adding a local reference failing causes the VM to abort
863 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700864 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 if (localException == NULL) {
866 // We were unable to add a new local reference, and threw a new
867 // exception. We can't return "exception", because it's not a
868 // local reference. So we have to return NULL, indicating that
869 // there was no exception, even though it's pretty much raining
870 // exceptions in here.
871 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
872 }
873 return localException;
874 }
875 }
876
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 static void FatalError(JNIEnv* env, const char* msg) {
878 ScopedJniThreadState ts(env);
879 LOG(FATAL) << "JNI FatalError called: " << msg;
880 }
881
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700882 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700883 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700884 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
885 return JNI_ERR;
886 }
887 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 return JNI_OK;
889 }
890
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700891 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700893 Object* survivor = Decode<Object*>(ts, java_survivor);
894 ts.Env()->PopFrame();
895 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 }
897
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700898 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700900 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
901 }
902
903 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
904 // TODO: we should try to expand the table if necessary.
905 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
906 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
907 return JNI_ERR;
908 }
909 // TODO: this isn't quite right, since "capacity" includes holes.
910 size_t capacity = ts.Env()->locals.Capacity();
911 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
912 if (!okay) {
913 ts.Self()->ThrowOutOfMemoryError(caller);
914 }
915 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700916 }
917
Elliott Hughescdf53122011-08-19 15:46:09 -0700918 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
919 ScopedJniThreadState ts(env);
920 if (obj == NULL) {
921 return NULL;
922 }
923
Elliott Hughes75770752011-08-24 17:52:38 -0700924 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 IndirectReferenceTable& globals = vm->globals;
926 MutexLock mu(vm->globals_lock);
927 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
928 return reinterpret_cast<jobject>(ref);
929 }
930
931 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
932 ScopedJniThreadState ts(env);
933 if (obj == NULL) {
934 return;
935 }
936
Elliott Hughes75770752011-08-24 17:52:38 -0700937 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 IndirectReferenceTable& globals = vm->globals;
939 MutexLock mu(vm->globals_lock);
940
941 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
942 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
943 << "failed to find entry";
944 }
945 }
946
947 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
948 ScopedJniThreadState ts(env);
949 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
950 }
951
952 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
953 ScopedJniThreadState ts(env);
954 if (obj == NULL) {
955 return;
956 }
957
Elliott Hughes75770752011-08-24 17:52:38 -0700958 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700959 IndirectReferenceTable& weak_globals = vm->weak_globals;
960 MutexLock mu(vm->weak_globals_lock);
961
962 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
963 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
964 << "failed to find entry";
965 }
966 }
967
968 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
969 ScopedJniThreadState ts(env);
970 if (obj == NULL) {
971 return NULL;
972 }
973
974 IndirectReferenceTable& locals = ts.Env()->locals;
975
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700976 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
978 return reinterpret_cast<jobject>(ref);
979 }
980
981 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
982 ScopedJniThreadState ts(env);
983 if (obj == NULL) {
984 return;
985 }
986
987 IndirectReferenceTable& locals = ts.Env()->locals;
988
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700989 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 if (!locals.Remove(cookie, obj)) {
991 // Attempting to delete a local reference that is not in the
992 // topmost local reference frame is a no-op. DeleteLocalRef returns
993 // void and doesn't throw any exceptions, but we should probably
994 // complain about it so the user will notice that things aren't
995 // going quite the way they expect.
996 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
997 << "failed to find entry";
998 }
999 }
1000
1001 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
1002 ScopedJniThreadState ts(env);
1003 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
1004 ? JNI_TRUE : JNI_FALSE;
1005 }
1006
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001007 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001009 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001010 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001011 return NULL;
1012 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001013 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 ScopedJniThreadState ts(env);
1018 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_start(args, mid);
1020 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 va_end(args);
1022 return result;
1023 }
1024
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001027 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001028 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001029 return NULL;
1030 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001031 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001032 if (result == NULL) {
1033 return NULL;
1034 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001035 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001037 if (!ts.Self()->IsExceptionPending()) {
1038 return local_result;
1039 } else {
1040 return NULL;
1041 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 }
1043
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001046 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001047 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001048 return NULL;
1049 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001050 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001051 if (result == NULL) {
1052 return NULL;
1053 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001054 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001056 if (!ts.Self()->IsExceptionPending()) {
1057 return local_result;
1058 } else {
1059 return NULL;
1060 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1064 ScopedJniThreadState ts(env);
1065 return FindMethodID(ts, c, name, sig, false);
1066 }
1067
1068 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1069 ScopedJniThreadState ts(env);
1070 return FindMethodID(ts, c, name, sig, true);
1071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jobject CallObjectMethod(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);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001079 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jobject CallObjectMethodV(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 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001085 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001090 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001091 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 va_list ap;
1097 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001098 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 va_end(ap);
1100 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001105 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001110 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_list ap;
1116 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001117 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 va_end(ap);
1119 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001124 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001129 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 va_list ap;
1135 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001136 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 va_end(ap);
1138 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001143 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001148 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 va_list ap;
1154 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001155 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 va_end(ap);
1157 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001162 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001167 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 va_list ap;
1173 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001174 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 va_end(ap);
1176 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001181 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001186 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 va_list ap;
1192 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001193 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 va_end(ap);
1195 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001200 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001205 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 va_list ap;
1211 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001212 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 va_end(ap);
1214 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001219 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001224 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
1226
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 va_list ap;
1230 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001231 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001232 va_end(ap);
1233 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001238 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001243 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001248 va_list ap;
1249 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001250 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001256 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001261 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 ScopedJniThreadState ts(env);
1267 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001269 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001270 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 va_end(ap);
1272 return local_result;
1273 }
1274
1275 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001276 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001278 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001279 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
1281
1282 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001283 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001285 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001286 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
1289 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
1292 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001294 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 va_end(ap);
1296 return result.z;
1297 }
1298
1299 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001300 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001302 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
1305 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001306 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001308 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
1311 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 ScopedJniThreadState ts(env);
1314 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001316 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 va_end(ap);
1318 return result.b;
1319 }
1320
1321 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001322 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001324 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
1327 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001330 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
1333 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 ScopedJniThreadState ts(env);
1336 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001338 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 va_end(ap);
1340 return result.c;
1341 }
1342
1343 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001346 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 }
1348
1349 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001350 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001352 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 }
1354
1355 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001356 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 ScopedJniThreadState ts(env);
1358 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001360 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 va_end(ap);
1362 return result.s;
1363 }
1364
1365 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001366 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001368 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
1371 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001372 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001374 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
Elliott Hughes418d20f2011-09-22 14:00:39 -07001377 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 ScopedJniThreadState ts(env);
1379 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001380 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001381 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 va_end(ap);
1383 return result.i;
1384 }
1385
1386 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001387 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001389 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
1392 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001393 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001395 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
1398 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001399 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 ScopedJniThreadState ts(env);
1401 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001402 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001403 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 va_end(ap);
1405 return result.j;
1406 }
1407
1408 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001409 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001411 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
1414 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001415 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001417 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
1420 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001421 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 ScopedJniThreadState ts(env);
1423 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001424 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001425 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 va_end(ap);
1427 return result.f;
1428 }
1429
1430 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001431 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001433 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
1436 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001437 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001439 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
1442 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001443 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 ScopedJniThreadState ts(env);
1445 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001446 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001447 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 va_end(ap);
1449 return result.d;
1450 }
1451
1452 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001453 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001455 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
1458 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001459 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001461 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
1464 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001465 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 ScopedJniThreadState ts(env);
1467 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001468 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001469 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 va_end(ap);
1471 }
1472
1473 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001474 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001476 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
1479 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001480 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001482 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 }
1484
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001485 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 ScopedJniThreadState ts(env);
1487 return FindFieldID(ts, c, name, sig, false);
1488 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001489
1490
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001491 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 ScopedJniThreadState ts(env);
1493 return FindFieldID(ts, c, name, sig, true);
1494 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001495
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001499 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001500 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001502
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001505 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001506 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 }
1508
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001509 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 Object* o = Decode<Object*>(ts, java_object);
1512 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001513 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 }
1516
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001517 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001520 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001521 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001524#define GET_PRIMITIVE_FIELD(fn, instance) \
1525 ScopedJniThreadState ts(env); \
1526 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001527 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 return f->fn(o)
1529
1530#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1531 ScopedJniThreadState ts(env); \
1532 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001533 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001534 f->fn(o, value)
1535
1536 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1537 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1541 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1545 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001548 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1549 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 }
1551
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001552 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1553 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001556 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1557 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 }
1559
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001560 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1561 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001564 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1565 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001568 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1569 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001572 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1573 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 }
1575
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001576 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1577 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001580 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1581 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001584 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1585 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001588 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1589 GET_PRIMITIVE_FIELD(GetLong, NULL);
1590 }
1591
1592 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1593 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1594 }
1595
1596 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1597 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1598 }
1599
1600 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1601 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1602 }
1603
1604 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1605 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1606 }
1607
1608 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1609 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1610 }
1611
1612 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1613 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1614 }
1615
1616 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1617 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1618 }
1619
1620 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1621 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1622 }
1623
1624 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1625 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1626 }
1627
1628 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1629 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1630 }
1631
1632 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1633 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1634 }
1635
1636 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1637 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1638 }
1639
1640 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1641 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1642 }
1643
1644 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1645 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1646 }
1647
1648 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1649 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1650 }
1651
1652 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1653 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1654 }
1655
1656 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1657 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1658 }
1659
1660 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1661 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
Elliott Hughes418d20f2011-09-22 14:00:39 -07001664 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 ScopedJniThreadState ts(env);
1666 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001668 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001669 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 va_end(ap);
1671 return local_result;
1672 }
1673
Elliott Hughes418d20f2011-09-22 14:00:39 -07001674 static jobject CallStaticObjectMethodV(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 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001677 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 }
1679
Elliott Hughes418d20f2011-09-22 14:00:39 -07001680 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001682 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001683 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
Elliott Hughes418d20f2011-09-22 14:00:39 -07001686 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 ScopedJniThreadState ts(env);
1688 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001690 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 va_end(ap);
1692 return result.z;
1693 }
1694
Elliott Hughes418d20f2011-09-22 14:00:39 -07001695 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 }
1699
Elliott Hughes418d20f2011-09-22 14:00:39 -07001700 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001702 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 }
1704
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
1707 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001709 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 va_end(ap);
1711 return result.b;
1712 }
1713
Elliott Hughes418d20f2011-09-22 14:00:39 -07001714 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes418d20f2011-09-22 14:00:39 -07001719 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001721 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 }
1723
Elliott Hughes72025e52011-08-23 17:50:30 -07001724 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 ScopedJniThreadState ts(env);
1726 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001727 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001728 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 va_end(ap);
1730 return result.c;
1731 }
1732
Elliott Hughes418d20f2011-09-22 14:00:39 -07001733 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001735 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes418d20f2011-09-22 14:00:39 -07001738 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001740 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 ScopedJniThreadState ts(env);
1745 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001746 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001747 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 va_end(ap);
1749 return result.s;
1750 }
1751
Elliott Hughes418d20f2011-09-22 14:00:39 -07001752 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001754 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes418d20f2011-09-22 14:00:39 -07001757 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001759 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes72025e52011-08-23 17:50:30 -07001762 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 ScopedJniThreadState ts(env);
1764 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001765 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001766 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 va_end(ap);
1768 return result.i;
1769 }
1770
Elliott Hughes418d20f2011-09-22 14:00:39 -07001771 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001773 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes418d20f2011-09-22 14:00:39 -07001776 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001778 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes72025e52011-08-23 17:50:30 -07001781 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 ScopedJniThreadState ts(env);
1783 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001784 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001785 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 va_end(ap);
1787 return result.j;
1788 }
1789
Elliott Hughes418d20f2011-09-22 14:00:39 -07001790 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001792 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes418d20f2011-09-22 14:00:39 -07001795 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001797 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
Elliott Hughes72025e52011-08-23 17:50:30 -07001800 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 ScopedJniThreadState ts(env);
1802 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001803 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001804 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 va_end(ap);
1806 return result.f;
1807 }
1808
Elliott Hughes418d20f2011-09-22 14:00:39 -07001809 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001811 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
Elliott Hughes418d20f2011-09-22 14:00:39 -07001814 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001816 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 }
1818
Elliott Hughes72025e52011-08-23 17:50:30 -07001819 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 ScopedJniThreadState ts(env);
1821 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001822 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001823 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 va_end(ap);
1825 return result.d;
1826 }
1827
Elliott Hughes418d20f2011-09-22 14:00:39 -07001828 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001830 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes418d20f2011-09-22 14:00:39 -07001833 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001835 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes72025e52011-08-23 17:50:30 -07001838 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 ScopedJniThreadState ts(env);
1840 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001841 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001842 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 va_end(ap);
1844 }
1845
Elliott Hughes418d20f2011-09-22 14:00:39 -07001846 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001848 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 }
1850
Elliott Hughes418d20f2011-09-22 14:00:39 -07001851 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001853 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 }
1855
Elliott Hughes814e4032011-08-23 12:07:56 -07001856 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001858 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001859 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 }
1861
1862 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1863 ScopedJniThreadState ts(env);
1864 if (utf == NULL) {
1865 return NULL;
1866 }
1867 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001868 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
Elliott Hughes814e4032011-08-23 12:07:56 -07001871 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1872 ScopedJniThreadState ts(env);
1873 return Decode<String*>(ts, java_string)->GetLength();
1874 }
1875
1876 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1877 ScopedJniThreadState ts(env);
1878 return Decode<String*>(ts, java_string)->GetUtfLength();
1879 }
1880
Elliott Hughesb465ab02011-08-24 11:21:21 -07001881 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001883 String* s = Decode<String*>(ts, java_string);
1884 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1885 ThrowSIOOBE(ts, start, length, s->GetLength());
1886 } else {
1887 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1888 memcpy(buf, chars + start, length * sizeof(jchar));
1889 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001890 }
1891
Elliott Hughesb465ab02011-08-24 11:21:21 -07001892 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001893 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001894 String* s = Decode<String*>(ts, java_string);
1895 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1896 ThrowSIOOBE(ts, start, length, s->GetLength());
1897 } else {
1898 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1899 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1900 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001901 }
1902
Elliott Hughes75770752011-08-24 17:52:38 -07001903 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001904 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001905 String* s = Decode<String*>(ts, java_string);
1906 const CharArray* chars = s->GetCharArray();
1907 PinPrimitiveArray(ts, chars);
1908 if (is_copy != NULL) {
1909 *is_copy = JNI_FALSE;
1910 }
1911 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001912 }
1913
Elliott Hughes75770752011-08-24 17:52:38 -07001914 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001915 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001916 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001917 }
1918
Elliott Hughes75770752011-08-24 17:52:38 -07001919 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001920 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001921 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 }
1923
Elliott Hughes75770752011-08-24 17:52:38 -07001924 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001925 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001926 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 }
1928
Elliott Hughes75770752011-08-24 17:52:38 -07001929 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001930 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001931 if (java_string == NULL) {
1932 return NULL;
1933 }
1934 if (is_copy != NULL) {
1935 *is_copy = JNI_TRUE;
1936 }
1937 String* s = Decode<String*>(ts, java_string);
1938 size_t byte_count = s->GetUtfLength();
1939 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001940 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001941 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1942 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1943 bytes[byte_count] = '\0';
1944 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001945 }
1946
Elliott Hughes75770752011-08-24 17:52:38 -07001947 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001948 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001949 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001950 }
1951
Elliott Hughesbd935992011-08-22 11:59:34 -07001952 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001954 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001955 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001956 Array* array = obj->AsArray();
1957 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Elliott Hughes814e4032011-08-23 12:07:56 -07001960 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001962 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001963 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
1966 static void SetObjectArrayElement(JNIEnv* env,
1967 jobjectArray java_array, jsize index, jobject java_value) {
1968 ScopedJniThreadState ts(env);
1969 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1970 Object* value = Decode<Object*>(ts, java_value);
1971 array->Set(index, value);
1972 }
1973
1974 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1975 ScopedJniThreadState ts(env);
1976 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1977 }
1978
1979 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1980 ScopedJniThreadState ts(env);
1981 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1982 }
1983
1984 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1985 ScopedJniThreadState ts(env);
1986 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1987 }
1988
1989 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1990 ScopedJniThreadState ts(env);
1991 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1992 }
1993
1994 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1995 ScopedJniThreadState ts(env);
1996 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1997 }
1998
1999 static jintArray NewIntArray(JNIEnv* env, jsize length) {
2000 ScopedJniThreadState ts(env);
2001 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
2002 }
2003
2004 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2005 ScopedJniThreadState ts(env);
2006 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
2007 }
2008
2009 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
2010 ScopedJniThreadState ts(env);
2011 CHECK_GE(length, 0); // TODO: ReportJniError
2012
2013 // Compute the array class corresponding to the given element class.
2014 Class* element_class = Decode<Class*>(ts, element_jclass);
2015 std::string descriptor;
2016 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002017 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002018
2019 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002020 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2021 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 return NULL;
2023 }
2024
Elliott Hughes75770752011-08-24 17:52:38 -07002025 // Allocate and initialize if necessary.
2026 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002028 if (initial_element != NULL) {
2029 Object* initial_object = Decode<Object*>(ts, initial_element);
2030 for (jsize i = 0; i < length; ++i) {
2031 result->Set(i, initial_object);
2032 }
2033 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002034 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
2037 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2038 ScopedJniThreadState ts(env);
2039 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2040 }
2041
Elliott Hughes75770752011-08-24 17:52:38 -07002042 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002043 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002044 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002045 }
2046
Elliott Hughes75770752011-08-24 17:52:38 -07002047 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002048 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002049 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002050 }
2051
Elliott Hughes75770752011-08-24 17:52:38 -07002052 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002054 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes75770752011-08-24 17:52:38 -07002057 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002059 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Elliott Hughes75770752011-08-24 17:52:38 -07002062 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002064 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes75770752011-08-24 17:52:38 -07002067 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002069 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Elliott Hughes75770752011-08-24 17:52:38 -07002072 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002074 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes75770752011-08-24 17:52:38 -07002077 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002079 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Elliott Hughes75770752011-08-24 17:52:38 -07002082 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002084 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Elliott Hughes75770752011-08-24 17:52:38 -07002087 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002089 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
Elliott Hughes75770752011-08-24 17:52:38 -07002092 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002094 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 }
2096
Elliott Hughes75770752011-08-24 17:52:38 -07002097 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002099 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 }
2101
Elliott Hughes75770752011-08-24 17:52:38 -07002102 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002104 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 }
2106
Elliott Hughes75770752011-08-24 17:52:38 -07002107 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002109 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
Elliott Hughes75770752011-08-24 17:52:38 -07002112 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002114 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 }
2116
Elliott Hughes75770752011-08-24 17:52:38 -07002117 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002119 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
Elliott Hughes75770752011-08-24 17:52:38 -07002122 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002124 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
Elliott Hughes75770752011-08-24 17:52:38 -07002127 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002129 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
Elliott Hughes814e4032011-08-23 12:07:56 -07002132 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002134 GetPrimitiveArrayRegion<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 GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002139 GetPrimitiveArrayRegion<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 GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002144 GetPrimitiveArrayRegion<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 GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002149 GetPrimitiveArrayRegion<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 GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002154 GetPrimitiveArrayRegion<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 GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002159 GetPrimitiveArrayRegion<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 GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002164 GetPrimitiveArrayRegion<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 GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002169 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Elliott Hughes814e4032011-08-23 12:07:56 -07002172 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002174 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
2176
Elliott Hughes814e4032011-08-23 12:07:56 -07002177 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002178 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002179 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
Elliott Hughes814e4032011-08-23 12:07:56 -07002182 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002184 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Elliott Hughes814e4032011-08-23 12:07:56 -07002187 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002189 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Elliott Hughes814e4032011-08-23 12:07:56 -07002192 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002194 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Elliott Hughes814e4032011-08-23 12:07:56 -07002197 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002199 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
Elliott Hughes814e4032011-08-23 12:07:56 -07002202 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002204 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 }
2206
Elliott Hughes814e4032011-08-23 12:07:56 -07002207 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002209 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 }
2211
Elliott Hughes5174fe62011-08-23 15:12:35 -07002212 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002214 Class* c = Decode<Class*>(ts, java_class);
2215
Elliott Hughes5174fe62011-08-23 15:12:35 -07002216 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 const char* name = methods[i].name;
2218 const char* sig = methods[i].signature;
2219
2220 if (*sig == '!') {
2221 // TODO: fast jni. it's too noisy to log all these.
2222 ++sig;
2223 }
2224
Elliott Hughes5174fe62011-08-23 15:12:35 -07002225 Method* m = c->FindDirectMethod(name, sig);
2226 if (m == NULL) {
2227 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002229 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002230 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002231 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002233 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002234 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002235 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 return JNI_ERR;
2237 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002238
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002239 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002240
2241 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243 return JNI_OK;
2244 }
2245
Elliott Hughes5174fe62011-08-23 15:12:35 -07002246 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002248 Class* c = Decode<Class*>(ts, java_class);
2249
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002250 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002251
2252 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2253 Method* m = c->GetDirectMethod(i);
2254 if (m->IsNative()) {
2255 m->UnregisterNative();
2256 }
2257 }
2258 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2259 Method* m = c->GetVirtualMethod(i);
2260 if (m->IsNative()) {
2261 m->UnregisterNative();
2262 }
2263 }
2264
2265 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002266 }
2267
Elliott Hughes72025e52011-08-23 17:50:30 -07002268 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002270 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002271 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273
Elliott Hughes72025e52011-08-23 17:50:30 -07002274 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002276 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002277 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 }
2279
2280 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2281 ScopedJniThreadState ts(env);
2282 Runtime* runtime = Runtime::Current();
2283 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002284 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 } else {
2286 *vm = NULL;
2287 }
2288 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2289 }
2290
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2292 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002293
2294 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002295 CHECK(address != NULL); // TODO: ReportJniError
2296 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002297
2298 jclass buffer_class = GetDirectByteBufferClass(env);
2299 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2300 if (mid == NULL) {
2301 return NULL;
2302 }
2303
2304 // At the moment, the Java side is limited to 32 bits.
2305 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2306 CHECK_LE(capacity, 0xffffffff);
2307 jint address_arg = reinterpret_cast<jint>(address);
2308 jint capacity_arg = static_cast<jint>(capacity);
2309
2310 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2311 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002312 }
2313
Elliott Hughesb465ab02011-08-24 11:21:21 -07002314 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002315 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002316 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2317 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 }
2319
Elliott Hughesb465ab02011-08-24 11:21:21 -07002320 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002321 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002322 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2323 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002324 }
2325
Elliott Hughesb465ab02011-08-24 11:21:21 -07002326 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002327 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002328
Elliott Hughes75770752011-08-24 17:52:38 -07002329 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002330
2331 // Do we definitely know what kind of reference this is?
2332 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2333 IndirectRefKind kind = GetIndirectRefKind(ref);
2334 switch (kind) {
2335 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002336 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2337 return JNILocalRefType;
2338 }
2339 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002340 case kGlobal:
2341 return JNIGlobalRefType;
2342 case kWeakGlobal:
2343 return JNIWeakGlobalRefType;
2344 case kSirtOrInvalid:
2345 // Is it in a stack IRT?
2346 if (ts.Self()->SirtContains(java_object)) {
2347 return JNILocalRefType;
2348 }
2349
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002350 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002351 return JNIInvalidRefType;
2352 }
2353
Elliott Hughesb465ab02011-08-24 11:21:21 -07002354 // If we're handing out direct pointers, check whether it's a direct pointer
2355 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002356 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002357 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002358 return JNILocalRefType;
2359 }
2360 }
2361
2362 return JNIInvalidRefType;
2363 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002364 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2365 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002366 }
2367};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002368
Elliott Hughesa2501992011-08-26 19:39:54 -07002369const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002370 NULL, // reserved0.
2371 NULL, // reserved1.
2372 NULL, // reserved2.
2373 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002374 JNI::GetVersion,
2375 JNI::DefineClass,
2376 JNI::FindClass,
2377 JNI::FromReflectedMethod,
2378 JNI::FromReflectedField,
2379 JNI::ToReflectedMethod,
2380 JNI::GetSuperclass,
2381 JNI::IsAssignableFrom,
2382 JNI::ToReflectedField,
2383 JNI::Throw,
2384 JNI::ThrowNew,
2385 JNI::ExceptionOccurred,
2386 JNI::ExceptionDescribe,
2387 JNI::ExceptionClear,
2388 JNI::FatalError,
2389 JNI::PushLocalFrame,
2390 JNI::PopLocalFrame,
2391 JNI::NewGlobalRef,
2392 JNI::DeleteGlobalRef,
2393 JNI::DeleteLocalRef,
2394 JNI::IsSameObject,
2395 JNI::NewLocalRef,
2396 JNI::EnsureLocalCapacity,
2397 JNI::AllocObject,
2398 JNI::NewObject,
2399 JNI::NewObjectV,
2400 JNI::NewObjectA,
2401 JNI::GetObjectClass,
2402 JNI::IsInstanceOf,
2403 JNI::GetMethodID,
2404 JNI::CallObjectMethod,
2405 JNI::CallObjectMethodV,
2406 JNI::CallObjectMethodA,
2407 JNI::CallBooleanMethod,
2408 JNI::CallBooleanMethodV,
2409 JNI::CallBooleanMethodA,
2410 JNI::CallByteMethod,
2411 JNI::CallByteMethodV,
2412 JNI::CallByteMethodA,
2413 JNI::CallCharMethod,
2414 JNI::CallCharMethodV,
2415 JNI::CallCharMethodA,
2416 JNI::CallShortMethod,
2417 JNI::CallShortMethodV,
2418 JNI::CallShortMethodA,
2419 JNI::CallIntMethod,
2420 JNI::CallIntMethodV,
2421 JNI::CallIntMethodA,
2422 JNI::CallLongMethod,
2423 JNI::CallLongMethodV,
2424 JNI::CallLongMethodA,
2425 JNI::CallFloatMethod,
2426 JNI::CallFloatMethodV,
2427 JNI::CallFloatMethodA,
2428 JNI::CallDoubleMethod,
2429 JNI::CallDoubleMethodV,
2430 JNI::CallDoubleMethodA,
2431 JNI::CallVoidMethod,
2432 JNI::CallVoidMethodV,
2433 JNI::CallVoidMethodA,
2434 JNI::CallNonvirtualObjectMethod,
2435 JNI::CallNonvirtualObjectMethodV,
2436 JNI::CallNonvirtualObjectMethodA,
2437 JNI::CallNonvirtualBooleanMethod,
2438 JNI::CallNonvirtualBooleanMethodV,
2439 JNI::CallNonvirtualBooleanMethodA,
2440 JNI::CallNonvirtualByteMethod,
2441 JNI::CallNonvirtualByteMethodV,
2442 JNI::CallNonvirtualByteMethodA,
2443 JNI::CallNonvirtualCharMethod,
2444 JNI::CallNonvirtualCharMethodV,
2445 JNI::CallNonvirtualCharMethodA,
2446 JNI::CallNonvirtualShortMethod,
2447 JNI::CallNonvirtualShortMethodV,
2448 JNI::CallNonvirtualShortMethodA,
2449 JNI::CallNonvirtualIntMethod,
2450 JNI::CallNonvirtualIntMethodV,
2451 JNI::CallNonvirtualIntMethodA,
2452 JNI::CallNonvirtualLongMethod,
2453 JNI::CallNonvirtualLongMethodV,
2454 JNI::CallNonvirtualLongMethodA,
2455 JNI::CallNonvirtualFloatMethod,
2456 JNI::CallNonvirtualFloatMethodV,
2457 JNI::CallNonvirtualFloatMethodA,
2458 JNI::CallNonvirtualDoubleMethod,
2459 JNI::CallNonvirtualDoubleMethodV,
2460 JNI::CallNonvirtualDoubleMethodA,
2461 JNI::CallNonvirtualVoidMethod,
2462 JNI::CallNonvirtualVoidMethodV,
2463 JNI::CallNonvirtualVoidMethodA,
2464 JNI::GetFieldID,
2465 JNI::GetObjectField,
2466 JNI::GetBooleanField,
2467 JNI::GetByteField,
2468 JNI::GetCharField,
2469 JNI::GetShortField,
2470 JNI::GetIntField,
2471 JNI::GetLongField,
2472 JNI::GetFloatField,
2473 JNI::GetDoubleField,
2474 JNI::SetObjectField,
2475 JNI::SetBooleanField,
2476 JNI::SetByteField,
2477 JNI::SetCharField,
2478 JNI::SetShortField,
2479 JNI::SetIntField,
2480 JNI::SetLongField,
2481 JNI::SetFloatField,
2482 JNI::SetDoubleField,
2483 JNI::GetStaticMethodID,
2484 JNI::CallStaticObjectMethod,
2485 JNI::CallStaticObjectMethodV,
2486 JNI::CallStaticObjectMethodA,
2487 JNI::CallStaticBooleanMethod,
2488 JNI::CallStaticBooleanMethodV,
2489 JNI::CallStaticBooleanMethodA,
2490 JNI::CallStaticByteMethod,
2491 JNI::CallStaticByteMethodV,
2492 JNI::CallStaticByteMethodA,
2493 JNI::CallStaticCharMethod,
2494 JNI::CallStaticCharMethodV,
2495 JNI::CallStaticCharMethodA,
2496 JNI::CallStaticShortMethod,
2497 JNI::CallStaticShortMethodV,
2498 JNI::CallStaticShortMethodA,
2499 JNI::CallStaticIntMethod,
2500 JNI::CallStaticIntMethodV,
2501 JNI::CallStaticIntMethodA,
2502 JNI::CallStaticLongMethod,
2503 JNI::CallStaticLongMethodV,
2504 JNI::CallStaticLongMethodA,
2505 JNI::CallStaticFloatMethod,
2506 JNI::CallStaticFloatMethodV,
2507 JNI::CallStaticFloatMethodA,
2508 JNI::CallStaticDoubleMethod,
2509 JNI::CallStaticDoubleMethodV,
2510 JNI::CallStaticDoubleMethodA,
2511 JNI::CallStaticVoidMethod,
2512 JNI::CallStaticVoidMethodV,
2513 JNI::CallStaticVoidMethodA,
2514 JNI::GetStaticFieldID,
2515 JNI::GetStaticObjectField,
2516 JNI::GetStaticBooleanField,
2517 JNI::GetStaticByteField,
2518 JNI::GetStaticCharField,
2519 JNI::GetStaticShortField,
2520 JNI::GetStaticIntField,
2521 JNI::GetStaticLongField,
2522 JNI::GetStaticFloatField,
2523 JNI::GetStaticDoubleField,
2524 JNI::SetStaticObjectField,
2525 JNI::SetStaticBooleanField,
2526 JNI::SetStaticByteField,
2527 JNI::SetStaticCharField,
2528 JNI::SetStaticShortField,
2529 JNI::SetStaticIntField,
2530 JNI::SetStaticLongField,
2531 JNI::SetStaticFloatField,
2532 JNI::SetStaticDoubleField,
2533 JNI::NewString,
2534 JNI::GetStringLength,
2535 JNI::GetStringChars,
2536 JNI::ReleaseStringChars,
2537 JNI::NewStringUTF,
2538 JNI::GetStringUTFLength,
2539 JNI::GetStringUTFChars,
2540 JNI::ReleaseStringUTFChars,
2541 JNI::GetArrayLength,
2542 JNI::NewObjectArray,
2543 JNI::GetObjectArrayElement,
2544 JNI::SetObjectArrayElement,
2545 JNI::NewBooleanArray,
2546 JNI::NewByteArray,
2547 JNI::NewCharArray,
2548 JNI::NewShortArray,
2549 JNI::NewIntArray,
2550 JNI::NewLongArray,
2551 JNI::NewFloatArray,
2552 JNI::NewDoubleArray,
2553 JNI::GetBooleanArrayElements,
2554 JNI::GetByteArrayElements,
2555 JNI::GetCharArrayElements,
2556 JNI::GetShortArrayElements,
2557 JNI::GetIntArrayElements,
2558 JNI::GetLongArrayElements,
2559 JNI::GetFloatArrayElements,
2560 JNI::GetDoubleArrayElements,
2561 JNI::ReleaseBooleanArrayElements,
2562 JNI::ReleaseByteArrayElements,
2563 JNI::ReleaseCharArrayElements,
2564 JNI::ReleaseShortArrayElements,
2565 JNI::ReleaseIntArrayElements,
2566 JNI::ReleaseLongArrayElements,
2567 JNI::ReleaseFloatArrayElements,
2568 JNI::ReleaseDoubleArrayElements,
2569 JNI::GetBooleanArrayRegion,
2570 JNI::GetByteArrayRegion,
2571 JNI::GetCharArrayRegion,
2572 JNI::GetShortArrayRegion,
2573 JNI::GetIntArrayRegion,
2574 JNI::GetLongArrayRegion,
2575 JNI::GetFloatArrayRegion,
2576 JNI::GetDoubleArrayRegion,
2577 JNI::SetBooleanArrayRegion,
2578 JNI::SetByteArrayRegion,
2579 JNI::SetCharArrayRegion,
2580 JNI::SetShortArrayRegion,
2581 JNI::SetIntArrayRegion,
2582 JNI::SetLongArrayRegion,
2583 JNI::SetFloatArrayRegion,
2584 JNI::SetDoubleArrayRegion,
2585 JNI::RegisterNatives,
2586 JNI::UnregisterNatives,
2587 JNI::MonitorEnter,
2588 JNI::MonitorExit,
2589 JNI::GetJavaVM,
2590 JNI::GetStringRegion,
2591 JNI::GetStringUTFRegion,
2592 JNI::GetPrimitiveArrayCritical,
2593 JNI::ReleasePrimitiveArrayCritical,
2594 JNI::GetStringCritical,
2595 JNI::ReleaseStringCritical,
2596 JNI::NewWeakGlobalRef,
2597 JNI::DeleteWeakGlobalRef,
2598 JNI::ExceptionCheck,
2599 JNI::NewDirectByteBuffer,
2600 JNI::GetDirectBufferAddress,
2601 JNI::GetDirectBufferCapacity,
2602 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002603};
2604
Elliott Hughes75770752011-08-24 17:52:38 -07002605JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002606 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002607 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002608 local_ref_cookie(IRT_FIRST_SEGMENT),
2609 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002610 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002611 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002612 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002613 functions = unchecked_functions = &gNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002614 if (vm->check_jni) {
2615 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002616 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002617 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2618 // errors will ensue.
2619 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2620 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002621}
2622
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002623JNIEnvExt::~JNIEnvExt() {
2624}
2625
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002626void JNIEnvExt::EnableCheckJni() {
2627 check_jni = true;
2628 functions = GetCheckJniNativeInterface();
2629}
2630
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002631void JNIEnvExt::DumpReferenceTables() {
2632 locals.Dump();
2633 monitors.Dump();
2634}
2635
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002636void JNIEnvExt::PushFrame(int capacity) {
2637 stacked_local_ref_cookies.push_back(local_ref_cookie);
2638 local_ref_cookie = locals.GetSegmentState();
2639}
2640
2641void JNIEnvExt::PopFrame() {
2642 locals.SetSegmentState(local_ref_cookie);
2643 local_ref_cookie = stacked_local_ref_cookies.back();
2644 stacked_local_ref_cookies.pop_back();
2645}
2646
Carl Shapiroea4dca82011-08-01 13:45:38 -07002647// JNI Invocation interface.
2648
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002649extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2650 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2651 if (args->version < JNI_VERSION_1_2) {
2652 return JNI_EVERSION;
2653 }
2654 Runtime::Options options;
2655 for (int i = 0; i < args->nOptions; ++i) {
2656 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002657 options.push_back(std::make_pair(StringPiece(option->optionString),
2658 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002659 }
2660 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002661 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002662 if (runtime == NULL) {
2663 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002664 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002665 runtime->Start();
2666 *p_env = Thread::Current()->GetJniEnv();
2667 *p_vm = runtime->GetJavaVM();
2668 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002669}
2670
Elliott Hughesf2682d52011-08-15 16:37:04 -07002671extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002672 Runtime* runtime = Runtime::Current();
2673 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002674 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002675 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002676 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002677 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002678 }
2679 return JNI_OK;
2680}
2681
2682// Historically unsupported.
2683extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2684 return JNI_ERR;
2685}
2686
Elliott Hughescdf53122011-08-19 15:46:09 -07002687class JII {
2688 public:
2689 static jint DestroyJavaVM(JavaVM* vm) {
2690 if (vm == NULL) {
2691 return JNI_ERR;
2692 } else {
2693 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2694 delete raw_vm->runtime;
2695 return JNI_OK;
2696 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002697 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002698
Elliott Hughescdf53122011-08-19 15:46:09 -07002699 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002700 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002701 }
2702
2703 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002704 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002705 }
2706
2707 static jint DetachCurrentThread(JavaVM* vm) {
2708 if (vm == NULL) {
2709 return JNI_ERR;
2710 } else {
2711 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2712 Runtime* runtime = raw_vm->runtime;
2713 runtime->DetachCurrentThread();
2714 return JNI_OK;
2715 }
2716 }
2717
2718 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2719 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2720 return JNI_EVERSION;
2721 }
2722 if (vm == NULL || env == NULL) {
2723 return JNI_ERR;
2724 }
2725 Thread* thread = Thread::Current();
2726 if (thread == NULL) {
2727 *env = NULL;
2728 return JNI_EDETACHED;
2729 }
2730 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002731 return JNI_OK;
2732 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002733};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002734
Elliott Hughesa2501992011-08-26 19:39:54 -07002735const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002736 NULL, // reserved0
2737 NULL, // reserved1
2738 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002739 JII::DestroyJavaVM,
2740 JII::AttachCurrentThread,
2741 JII::DetachCurrentThread,
2742 JII::GetEnv,
2743 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002744};
2745
Elliott Hughesa0957642011-09-02 14:27:33 -07002746JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002747 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002748 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002749 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002750 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002751 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002752 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002753 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002754 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002755 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002756 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002757 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002758 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002759 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002760 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002761 functions = unchecked_functions = &gInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002762 if (options->check_jni_) {
2763 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002764 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002765}
2766
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002767JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002768 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002769}
2770
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002771void JavaVMExt::EnableCheckJni() {
2772 check_jni = true;
2773 functions = GetCheckJniInvokeInterface();
2774}
2775
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002776void JavaVMExt::DumpReferenceTables() {
2777 {
2778 MutexLock mu(globals_lock);
2779 globals.Dump();
2780 }
2781 {
2782 MutexLock mu(weak_globals_lock);
2783 weak_globals.Dump();
2784 }
2785 {
2786 MutexLock mu(pins_lock);
2787 pin_table.Dump();
2788 }
2789}
2790
Elliott Hughes75770752011-08-24 17:52:38 -07002791bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2792 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002793
2794 // See if we've already loaded this library. If we have, and the class loader
2795 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002796 // TODO: for better results we should canonicalize the pathname (or even compare
2797 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002798 SharedLibrary* library;
2799 {
2800 // TODO: move the locking (and more of this logic) into Libraries.
2801 MutexLock mu(libraries_lock);
2802 library = libraries->Get(path);
2803 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002804 if (library != NULL) {
2805 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002806 // The library will be associated with class_loader. The JNI
2807 // spec says we can't load the same library into more than one
2808 // class loader.
2809 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2810 "ClassLoader %p; can't open in ClassLoader %p",
2811 path.c_str(), library->GetClassLoader(), class_loader);
2812 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002813 return false;
2814 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002815 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2816 << "ClassLoader " << class_loader << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002817 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002818 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2819 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002820 return false;
2821 }
2822 return true;
2823 }
2824
2825 // Open the shared library. Because we're using a full path, the system
2826 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2827 // resolve this library's dependencies though.)
2828
2829 // Failures here are expected when java.library.path has several entries
2830 // and we have to hunt for the lib.
2831
2832 // The current version of the dynamic linker prints detailed information
2833 // about dlopen() failures. Some things to check if the message is
2834 // cryptic:
2835 // - make sure the library exists on the device
2836 // - verify that the right path is being opened (the debug log message
2837 // above can help with that)
2838 // - check to see if the library is valid (e.g. not zero bytes long)
2839 // - check config/prelink-linux-arm.map to ensure that the library
2840 // is listed and is not being overrun by the previous entry (if
2841 // loading suddenly stops working on a prelinked library, this is
2842 // a good one to check)
2843 // - write a trivial app that calls sleep() then dlopen(), attach
2844 // to it with "strace -p <pid>" while it sleeps, and watch for
2845 // attempts to open nonexistent dependent shared libs
2846
2847 // TODO: automate some of these checks!
2848
2849 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002850 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002851 // the GC to ignore us.
2852 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002853 void* handle = NULL;
2854 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002855 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002856 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002857 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002858
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002859 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002860
2861 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002862 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002863 return false;
2864 }
2865
2866 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002867 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002868 // TODO: move the locking (and more of this logic) into Libraries.
2869 MutexLock mu(libraries_lock);
2870 library = libraries->Get(path);
2871 if (library != NULL) {
2872 LOG(INFO) << "WOW: we lost a race to add shared library: "
2873 << "\"" << path << "\" ClassLoader=" << class_loader;
2874 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002875 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002876 library = new SharedLibrary(path, handle, class_loader);
2877 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002878 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002879
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002880 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002881
2882 bool result = true;
2883 void* sym = dlsym(handle, "JNI_OnLoad");
2884 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002885 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002886 } else {
2887 // Call JNI_OnLoad. We have to override the current class
2888 // loader, which will always be "null" since the stuff at the
2889 // top of the stack is around Runtime.loadLibrary(). (See
2890 // the comments in the JNI FindClass function.)
2891 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2892 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002893 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002894 self->SetClassLoaderOverride(class_loader);
2895
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002896 int version = 0;
2897 {
2898 ScopedThreadStateChange tsc(self, Thread::kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002899 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002900 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002901 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002902
Brian Carlstromaded5f72011-10-07 17:15:04 -07002903 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002904
2905 if (version != JNI_VERSION_1_2 &&
2906 version != JNI_VERSION_1_4 &&
2907 version != JNI_VERSION_1_6) {
2908 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2909 << "bad version: " << version;
2910 // It's unwise to call dlclose() here, but we can mark it
2911 // as bad and ensure that future load attempts will fail.
2912 // We don't know how far JNI_OnLoad got, so there could
2913 // be some partially-initialized stuff accessible through
2914 // newly-registered native method calls. We could try to
2915 // unregister them, but that doesn't seem worthwhile.
2916 result = false;
2917 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002918 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2919 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002920 }
2921 }
2922
2923 library->SetResult(result);
2924 return result;
2925}
2926
2927void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2928 CHECK(m->IsNative());
2929
2930 Class* c = m->GetDeclaringClass();
2931
2932 // If this is a static method, it could be called before the class
2933 // has been initialized.
2934 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002935 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002936 return NULL;
2937 }
2938 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002939 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002940 }
2941
Brian Carlstrom16192862011-09-12 17:50:06 -07002942 std::string detail;
2943 void* native_method;
2944 {
2945 MutexLock mu(libraries_lock);
2946 native_method = libraries->FindNativeMethod(m, detail);
2947 }
2948 // throwing can cause libraries_lock to be reacquired
2949 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002950 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002951 }
2952 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002953}
2954
Elliott Hughes410c0c82011-09-01 17:58:25 -07002955void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2956 {
2957 MutexLock mu(globals_lock);
2958 globals.VisitRoots(visitor, arg);
2959 }
2960 {
2961 MutexLock mu(pins_lock);
2962 pin_table.VisitRoots(visitor, arg);
2963 }
2964 // The weak_globals table is visited by the GC itself (because it mutates the table).
2965}
2966
Elliott Hughes844f9a02012-01-24 20:19:58 -08002967jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
2968 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2969 if (c.get() == NULL) {
2970 return NULL;
2971 }
2972 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
2973}
2974
Ian Rogersdf20fe02011-07-20 20:34:16 -07002975} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002976
2977std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2978 switch (rhs) {
2979 case JNIInvalidRefType:
2980 os << "JNIInvalidRefType";
2981 return os;
2982 case JNILocalRefType:
2983 os << "JNILocalRefType";
2984 return os;
2985 case JNIGlobalRefType:
2986 os << "JNIGlobalRefType";
2987 return os;
2988 case JNIWeakGlobalRefType:
2989 os << "JNIWeakGlobalRefType";
2990 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002991 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002992 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002993 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002994 }
2995}