blob: 9ace57a38c7d439f1a5a327eadf6905959f652ea [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070015#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070016#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070018#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070022#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070023#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070024#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070025#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070026#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070027
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070028namespace art {
29
Elliott Hughes2ced6a52011-10-16 18:44:48 -070030static const size_t kMonitorsInitial = 32; // Arbitrary.
31static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
32
33static const size_t kLocalsInitial = 64; // Arbitrary.
34static const size_t kLocalsMax = 512; // Arbitrary sanity check.
35
36static const size_t kPinTableInitial = 16; // Arbitrary.
37static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
38
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070039static size_t gGlobalsInitial = 512; // Arbitrary.
40static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070041
42static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
43static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
44
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070045void SetJniGlobalsMax(size_t max) {
46 if (max != 0) {
47 gGlobalsMax = max;
48 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
49 }
50}
Ian Rogersdf20fe02011-07-20 20:34:16 -070051
Elliott Hughesc5f7c912011-08-18 14:00:42 -070052/*
53 * Add a local reference for an object to the current stack frame. When
54 * the native function returns, the reference will be discarded.
55 *
56 * We need to allow the same reference to be added multiple times.
57 *
58 * This will be called on otherwise unreferenced objects. We cannot do
59 * GC allocations here, and it's best if we don't grab a mutex.
60 *
61 * Returns the local reference (currently just the same pointer that was
62 * passed in), or NULL on failure.
63 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070064template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070065T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
66 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
67 Object* obj = const_cast<Object*>(const_obj);
68
Elliott Hughesc5f7c912011-08-18 14:00:42 -070069 if (obj == NULL) {
70 return NULL;
71 }
72
Elliott Hughesbf86d042011-08-31 17:53:14 -070073 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
74 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070075
Ian Rogers5a7a74a2011-09-26 16:32:29 -070076 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070077 IndirectRef ref = locals.Add(cookie, obj);
78 if (ref == NULL) {
79 // TODO: just change Add's DCHECK to CHECK and lose this?
80 locals.Dump();
81 LOG(FATAL) << "Failed adding to JNI local reference table "
82 << "(has " << locals.Capacity() << " entries)";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070083 }
84
85#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070086 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070087 size_t entry_count = locals.Capacity();
88 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070089 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070090 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070091 locals.Dump();
Elliott Hughes82188472011-11-07 18:11:48 -080092 // TODO: LOG(FATAL) instead.
Elliott Hughesc5f7c912011-08-18 14:00:42 -070093 }
94 }
95#endif
96
Elliott Hughesbf86d042011-08-31 17:53:14 -070097 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070098 // Hand out direct pointers to support broken old apps.
99 return reinterpret_cast<T>(obj);
100 }
101
102 return reinterpret_cast<T>(ref);
103}
104
Ian Rogers0571d352011-11-03 19:51:38 -0700105size_t NumArgArrayBytes(const char* shorty) {
106 size_t num_bytes = 0;
107 size_t end = strlen(shorty);
108 for (size_t i = 1; i < end; ++i) {
109 char ch = shorty[i];
110 if (ch == 'D' || ch == 'J') {
111 num_bytes += 8;
112 } else if (ch == 'L') {
113 // Argument is a reference or an array. The shorty descriptor
114 // does not distinguish between these types.
115 num_bytes += sizeof(Object*);
116 } else {
117 num_bytes += 4;
118 }
119 }
120 return num_bytes;
121}
122
Elliott Hughesbf86d042011-08-31 17:53:14 -0700123// For external use.
124template<typename T>
125T Decode(JNIEnv* public_env, jobject obj) {
126 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
127 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
128}
129// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700130template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700131template Class* Decode<Class*>(JNIEnv*, jobject);
132template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
133template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700134template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700135template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700136template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700137template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400138template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700139template String* Decode<String*>(JNIEnv*, jobject);
140template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700141
142namespace {
143
Elliott Hughescdf53122011-08-19 15:46:09 -0700144jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
145 if (obj == NULL) {
146 return NULL;
147 }
Elliott Hughes75770752011-08-24 17:52:38 -0700148 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700149 IndirectReferenceTable& weak_globals = vm->weak_globals;
150 MutexLock mu(vm->weak_globals_lock);
151 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
152 return reinterpret_cast<jweak>(ref);
153}
154
Elliott Hughesbf86d042011-08-31 17:53:14 -0700155// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700156template<typename T>
157T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700158 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700159}
160
Ian Rogers0571d352011-11-03 19:51:38 -0700161static byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700162 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800163 const char* shorty = MethodHelper(method).GetShorty();
164 size_t shorty_len = strlen(shorty);
Ian Rogers0571d352011-11-03 19:51:38 -0700165 size_t num_bytes = NumArgArrayBytes(shorty);
166 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800167 for (size_t i = 1, offset = 0; i < shorty_len; ++i) {
168 switch (shorty[i]) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700169 case 'Z':
170 case 'B':
171 case 'C':
172 case 'S':
173 case 'I':
174 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
175 offset += 4;
176 break;
177 case 'F':
178 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
179 offset += 4;
180 break;
181 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700182 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700183 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
184 offset += sizeof(Object*);
185 break;
186 }
187 case 'D':
188 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
189 offset += 8;
190 break;
191 case 'J':
192 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
193 offset += 8;
194 break;
195 }
196 }
197 return arg_array.release();
198}
199
Ian Rogers0571d352011-11-03 19:51:38 -0700200static byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700201 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800202 const char* shorty = MethodHelper(method).GetShorty();
203 size_t shorty_len = strlen(shorty);
204 size_t num_bytes = NumArgArrayBytes(shorty);
Elliott Hughes90a33692011-08-30 13:27:07 -0700205 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800206 for (size_t i = 1, offset = 0; i < shorty_len; ++i) {
207 switch (shorty[i]) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700208 case 'Z':
209 case 'B':
210 case 'C':
211 case 'S':
212 case 'I':
213 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
214 offset += 4;
215 break;
216 case 'F':
217 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
218 offset += 4;
219 break;
220 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700221 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700222 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
223 offset += sizeof(Object*);
224 break;
225 }
226 case 'D':
227 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
228 offset += 8;
229 break;
230 case 'J':
231 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
232 offset += 8;
233 break;
234 }
235 }
236 return arg_array.release();
237}
238
Elliott Hughesd07986f2011-12-06 18:27:45 -0800239static byte* CreateArgArray(Method* method, JValue* args) {
240 const char* shorty = MethodHelper(method).GetShorty();
241 size_t shorty_len = strlen(shorty);
242 size_t num_bytes = NumArgArrayBytes(shorty);
243 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
244 for (size_t i = 1, offset = 0; i < shorty_len; ++i) {
245 switch (shorty[i]) {
246 case 'Z':
247 case 'B':
248 case 'C':
249 case 'S':
250 case 'I':
251 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
252 offset += 4;
253 break;
254 case 'F':
255 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
256 offset += 4;
257 break;
258 case 'L':
259 *reinterpret_cast<Object**>(&arg_array[offset]) = args[i - 1].l;
260 offset += sizeof(Object*);
261 break;
262 case 'D':
263 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
264 offset += 8;
265 break;
266 case 'J':
267 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
268 offset += 8;
269 break;
270 }
271 }
272 return arg_array.release();
273}
274
Ian Rogers0571d352011-11-03 19:51:38 -0700275static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700276 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700277 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700278 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700279 return result;
280}
281
Ian Rogers0571d352011-11-03 19:51:38 -0700282static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700283 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
284 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700285 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700286 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
287 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700288}
289
Ian Rogers0571d352011-11-03 19:51:38 -0700290static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700291 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700292}
293
Ian Rogers0571d352011-11-03 19:51:38 -0700294static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
295 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700296 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
297 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700298 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700299 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
300 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700301}
302
Ian Rogers0571d352011-11-03 19:51:38 -0700303static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
304 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700305 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
306 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700307 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700308 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
309 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700310}
311
Elliott Hughes6b436852011-08-12 10:16:44 -0700312// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
313// separated with slashes but aren't wrapped with "L;" like regular descriptors
314// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
315// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
316// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700317static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700318 std::string result;
319 // Add the missing "L;" if necessary.
320 if (name[0] == '[') {
321 result = name;
322 } else {
323 result += 'L';
324 result += name;
325 result += ';';
326 }
327 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700328 if (result.find('.') != std::string::npos) {
329 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
330 << "\"" << name << "\"";
331 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700332 }
333 return result;
334}
335
Ian Rogers0571d352011-11-03 19:51:38 -0700336static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700337 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800338 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor().c_str(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700339}
340
Ian Rogers0571d352011-11-03 19:51:38 -0700341static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700342 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700343 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700344 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700345 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700346
347 Method* method = NULL;
348 if (is_static) {
349 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700350 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700351 method = c->FindVirtualMethod(name, sig);
352 if (method == NULL) {
353 // No virtual method matching the signature. Search declared
354 // private methods and constructors.
355 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700356 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700357 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700358
Elliott Hughescdf53122011-08-19 15:46:09 -0700359 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700360 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700361 return NULL;
362 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700363
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700364 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700365}
366
Ian Rogers0571d352011-11-03 19:51:38 -0700367static const ClassLoader* GetClassLoader(Thread* self) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700368 Frame frame = self->GetTopOfStack();
369 Method* method = frame.GetMethod();
370 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
371 return self->GetClassLoaderOverride();
372 }
373 return method->GetDeclaringClass()->GetClassLoader();
374}
375
Ian Rogers0571d352011-11-03 19:51:38 -0700376static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700377 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700378 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700379 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700380 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700381
382 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383 Class* field_type;
384 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
385 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700386 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700387 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700388 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700389 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700390 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700391 if (field_type == NULL) {
392 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700393 DCHECK(ts.Self()->IsExceptionPending());
394 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700395 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700396 "no type \"%s\" found and so no field \"%s\" could be found in class "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800397 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor().c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700398 return NULL;
399 }
400 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800401 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700402 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800403 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700404 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700405 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700406 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700407 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800408 name, ClassHelper(c).GetDescriptor().c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700409 return NULL;
410 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700411 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700412}
413
Ian Rogers0571d352011-11-03 19:51:38 -0700414static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700415 JavaVMExt* vm = ts.Vm();
416 MutexLock mu(vm->pins_lock);
417 vm->pin_table.Add(array);
418}
419
Ian Rogers0571d352011-11-03 19:51:38 -0700420static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700421 JavaVMExt* vm = ts.Vm();
422 MutexLock mu(vm->pins_lock);
423 vm->pin_table.Remove(array);
424}
425
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700426template<typename JniT, typename ArtT>
427JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
428 CHECK_GE(length, 0); // TODO: ReportJniError
429 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700430 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700431}
432
Elliott Hughes75770752011-08-24 17:52:38 -0700433template <typename ArrayT, typename CArrayT, typename ArtArrayT>
434CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
435 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
436 PinPrimitiveArray(ts, array);
437 if (is_copy != NULL) {
438 *is_copy = JNI_FALSE;
439 }
440 return array->GetData();
441}
442
443template <typename ArrayT>
444void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
445 if (mode != JNI_COMMIT) {
446 Array* array = Decode<Array*>(ts, java_array);
447 UnpinPrimitiveArray(ts, array);
448 }
449}
450
Ian Rogers0571d352011-11-03 19:51:38 -0700451static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700452 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700453 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700454 "%s offset=%d length=%d %s.length=%d",
455 type.c_str(), start, length, identifier, array->GetLength());
456}
Ian Rogers0571d352011-11-03 19:51:38 -0700457
458static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700459 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700460 "offset=%d length=%d string.length()=%d", start, length, array_length);
461}
Elliott Hughes814e4032011-08-23 12:07:56 -0700462
463template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700464void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700465 ArrayT* array = Decode<ArrayT*>(ts, java_array);
466 if (start < 0 || length < 0 || start + length > array->GetLength()) {
467 ThrowAIOOBE(ts, array, start, length, "src");
468 } else {
469 JavaT* data = array->GetData();
470 memcpy(buf, data + start, length * sizeof(JavaT));
471 }
472}
473
474template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700475void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700476 ArrayT* array = Decode<ArrayT*>(ts, java_array);
477 if (start < 0 || length < 0 || start + length > array->GetLength()) {
478 ThrowAIOOBE(ts, array, start, length, "dst");
479 } else {
480 JavaT* data = array->GetData();
481 memcpy(data + start, buf, length * sizeof(JavaT));
482 }
483}
484
Ian Rogers0571d352011-11-03 19:51:38 -0700485static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700486 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
487 CHECK(buffer_class.get() != NULL);
488 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
489}
490
Ian Rogers0571d352011-11-03 19:51:38 -0700491static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700492 static jclass buffer_class = InitDirectByteBufferClass(env);
493 return buffer_class;
494}
495
Ian Rogers0571d352011-11-03 19:51:38 -0700496static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700497 if (vm == NULL || p_env == NULL) {
498 return JNI_ERR;
499 }
500
Elliott Hughescac6cc72011-11-03 20:31:21 -0700501 // Return immediately if we're already one with the VM.
502 Thread* self = Thread::Current();
503 if (self != NULL) {
504 *p_env = self->GetJniEnv();
505 return JNI_OK;
506 }
507
508 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
509
510 // No threads allowed in zygote mode.
511 if (runtime->IsZygote()) {
512 LOG(ERROR) << "Attempt to attach a thread in the zygote";
513 return JNI_ERR;
514 }
515
Elliott Hughes75770752011-08-24 17:52:38 -0700516 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
517 JavaVMAttachArgs args;
518 if (thr_args == NULL) {
519 // Allow the v1.1 calling convention.
520 args.version = JNI_VERSION_1_2;
521 args.name = NULL;
522 args.group = NULL; // TODO: get "main" thread group
523 } else {
524 args.version = in_args->version;
525 args.name = in_args->name;
526 if (in_args->group != NULL) {
527 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
528 args.group = NULL; // TODO: decode in_args->group
529 } else {
530 args.group = NULL; // TODO: get "main" thread group
531 }
532 }
533 CHECK_GE(args.version, JNI_VERSION_1_2);
534
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700535 runtime->AttachCurrentThread(args.name, as_daemon);
536 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700537 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700538}
539
Elliott Hughes79082e32011-08-25 12:07:32 -0700540class SharedLibrary {
541 public:
542 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
543 : path_(path),
544 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700545 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700546 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700547 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700548 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700549 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700550 }
551
Elliott Hughes79082e32011-08-25 12:07:32 -0700552 Object* GetClassLoader() {
553 return class_loader_;
554 }
555
556 std::string GetPath() {
557 return path_;
558 }
559
560 /*
561 * Check the result of an earlier call to JNI_OnLoad on this library. If
562 * the call has not yet finished in another thread, wait for it.
563 */
564 bool CheckOnLoadResult(JavaVMExt* vm) {
565 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700566 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700567 // Check this so we don't end up waiting for ourselves. We need
568 // to return "true" so the caller can continue.
569 LOG(INFO) << *self << " recursive attempt to load library "
570 << "\"" << path_ << "\"";
571 return true;
572 }
573
574 MutexLock mu(jni_on_load_lock_);
575 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800576 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
577 << "JNI_OnLoad...]";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700578 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700579 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700580 }
581
582 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800583 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
584 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700585 return okay;
586 }
587
588 void SetResult(bool result) {
589 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700590 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700591
592 // Broadcast a wakeup to anybody sleeping on the condition variable.
593 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700594 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700595 }
596
597 void* FindSymbol(const std::string& symbol_name) {
598 return dlsym(handle_, symbol_name.c_str());
599 }
600
601 private:
602 enum JNI_OnLoadState {
603 kPending,
604 kFailed,
605 kOkay,
606 };
607
608 // Path to library "/system/lib/libjni.so".
609 std::string path_;
610
611 // The void* returned by dlopen(3).
612 void* handle_;
613
614 // The ClassLoader this library is associated with.
615 Object* class_loader_;
616
617 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700618 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700619 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700620 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700621 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700622 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700623 // Result of earlier JNI_OnLoad call.
624 JNI_OnLoadState jni_on_load_result_;
625};
626
Elliott Hughescdf53122011-08-19 15:46:09 -0700627} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700628
Elliott Hughes79082e32011-08-25 12:07:32 -0700629// This exists mainly to keep implementation details out of the header file.
630class Libraries {
631 public:
632 Libraries() {
633 }
634
635 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700636 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700637 }
638
639 SharedLibrary* Get(const std::string& path) {
640 return libraries_[path];
641 }
642
643 void Put(const std::string& path, SharedLibrary* library) {
644 libraries_[path] = library;
645 }
646
647 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700648 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700649 std::string jni_short_name(JniShortName(m));
650 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700651 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700652 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
653 SharedLibrary* library = it->second;
654 if (library->GetClassLoader() != declaring_class_loader) {
655 // We only search libraries loaded by the appropriate ClassLoader.
656 continue;
657 }
658 // Try the short name then the long name...
659 void* fn = library->FindSymbol(jni_short_name);
660 if (fn == NULL) {
661 fn = library->FindSymbol(jni_long_name);
662 }
663 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800664 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
665 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700666 return fn;
667 }
668 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700669 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700670 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700671 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700672 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700673 return NULL;
674 }
675
676 private:
677 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
678
679 std::map<std::string, SharedLibrary*> libraries_;
680};
681
Elliott Hughes418d20f2011-09-22 14:00:39 -0700682JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
683 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
684 Object* receiver = Decode<Object*>(env, obj);
685 Method* method = DecodeMethod(mid);
686 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
687 return InvokeWithArgArray(env, receiver, method, arg_array.get());
688}
689
Elliott Hughesd07986f2011-12-06 18:27:45 -0800690JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
691 UniquePtr<byte[]> arg_array(CreateArgArray(m, args));
692 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, arg_array.get());
693}
694
Elliott Hughescdf53122011-08-19 15:46:09 -0700695class JNI {
696 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700697
Elliott Hughescdf53122011-08-19 15:46:09 -0700698 static jint GetVersion(JNIEnv* env) {
699 ScopedJniThreadState ts(env);
700 return JNI_VERSION_1_6;
701 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700702
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
704 ScopedJniThreadState ts(env);
705 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700706 return NULL;
707 }
708
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 static jclass FindClass(JNIEnv* env, const char* name) {
710 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700711 Runtime* runtime = Runtime::Current();
712 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700713 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700714 Class* c = NULL;
715 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700716 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700717 c = class_linker->FindClass(descriptor, cl);
718 } else {
719 c = class_linker->FindSystemClass(descriptor);
720 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700721 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700722 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700723
Elliott Hughescdf53122011-08-19 15:46:09 -0700724 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
725 ScopedJniThreadState ts(env);
726 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700727 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700728 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700729
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
731 ScopedJniThreadState ts(env);
732 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700733 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700734 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700735
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
737 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700738 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700739 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700740 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700741
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
743 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700744 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700745 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700746 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700747
Elliott Hughes37f7a402011-08-22 18:56:01 -0700748 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
749 ScopedJniThreadState ts(env);
750 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700751 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700752 }
753
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700754 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700756 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700757 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700759
Elliott Hughes37f7a402011-08-22 18:56:01 -0700760 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700762 Class* c1 = Decode<Class*>(ts, java_class1);
763 Class* c2 = Decode<Class*>(ts, java_class2);
764 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700766
Elliott Hughes37f7a402011-08-22 18:56:01 -0700767 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700768 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700769 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700770 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700771 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700772 return JNI_TRUE;
773 } else {
774 Object* obj = Decode<Object*>(ts, jobj);
775 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700776 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700777 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700779
Elliott Hughes37f7a402011-08-22 18:56:01 -0700780 static jint Throw(JNIEnv* env, jthrowable java_exception) {
781 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700782 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700783 if (exception == NULL) {
784 return JNI_ERR;
785 }
786 ts.Self()->SetException(exception);
787 return JNI_OK;
788 }
789
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700790 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700791 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700792 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700793 jmethodID mid = ((msg != NULL)
794 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
795 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700796 if (mid == NULL) {
797 return JNI_ERR;
798 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700799 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700800 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700801 return JNI_ERR;
802 }
803
804 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700805 args[0].l = s.get();
806 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
807 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700808 return JNI_ERR;
809 }
810
Elliott Hughes72025e52011-08-23 17:50:30 -0700811 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700812
Elliott Hughes37f7a402011-08-22 18:56:01 -0700813 return JNI_OK;
814 }
815
816 static jboolean ExceptionCheck(JNIEnv* env) {
817 ScopedJniThreadState ts(env);
818 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
819 }
820
821 static void ExceptionClear(JNIEnv* env) {
822 ScopedJniThreadState ts(env);
823 ts.Self()->ClearException();
824 }
825
826 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700828
829 Thread* self = ts.Self();
830 Throwable* original_exception = self->GetException();
831 self->ClearException();
832
Elliott Hughesbf86d042011-08-31 17:53:14 -0700833 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700834 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
835 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
836 if (mid == NULL) {
837 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700838 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700839 } else {
840 env->CallVoidMethod(exception.get(), mid);
841 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700842 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700843 << " thrown while calling printStackTrace";
844 self->ClearException();
845 }
846 }
847
848 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700850
Elliott Hughescdf53122011-08-19 15:46:09 -0700851 static jthrowable ExceptionOccurred(JNIEnv* env) {
852 ScopedJniThreadState ts(env);
853 Object* exception = ts.Self()->GetException();
854 if (exception == NULL) {
855 return NULL;
856 } else {
857 // TODO: if adding a local reference failing causes the VM to abort
858 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700859 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 if (localException == NULL) {
861 // We were unable to add a new local reference, and threw a new
862 // exception. We can't return "exception", because it's not a
863 // local reference. So we have to return NULL, indicating that
864 // there was no exception, even though it's pretty much raining
865 // exceptions in here.
866 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
867 }
868 return localException;
869 }
870 }
871
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 static void FatalError(JNIEnv* env, const char* msg) {
873 ScopedJniThreadState ts(env);
874 LOG(FATAL) << "JNI FatalError called: " << msg;
875 }
876
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700877 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700878 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700879 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
880 return JNI_ERR;
881 }
882 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700883 return JNI_OK;
884 }
885
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700886 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700888 Object* survivor = Decode<Object*>(ts, java_survivor);
889 ts.Env()->PopFrame();
890 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700891 }
892
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700893 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700894 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700895 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
896 }
897
898 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
899 // TODO: we should try to expand the table if necessary.
900 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
901 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
902 return JNI_ERR;
903 }
904 // TODO: this isn't quite right, since "capacity" includes holes.
905 size_t capacity = ts.Env()->locals.Capacity();
906 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
907 if (!okay) {
908 ts.Self()->ThrowOutOfMemoryError(caller);
909 }
910 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 }
912
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
914 ScopedJniThreadState ts(env);
915 if (obj == NULL) {
916 return NULL;
917 }
918
Elliott Hughes75770752011-08-24 17:52:38 -0700919 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 IndirectReferenceTable& globals = vm->globals;
921 MutexLock mu(vm->globals_lock);
922 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
923 return reinterpret_cast<jobject>(ref);
924 }
925
926 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
927 ScopedJniThreadState ts(env);
928 if (obj == NULL) {
929 return;
930 }
931
Elliott Hughes75770752011-08-24 17:52:38 -0700932 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 IndirectReferenceTable& globals = vm->globals;
934 MutexLock mu(vm->globals_lock);
935
936 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
937 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
938 << "failed to find entry";
939 }
940 }
941
942 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
943 ScopedJniThreadState ts(env);
944 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
945 }
946
947 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
948 ScopedJniThreadState ts(env);
949 if (obj == NULL) {
950 return;
951 }
952
Elliott Hughes75770752011-08-24 17:52:38 -0700953 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 IndirectReferenceTable& weak_globals = vm->weak_globals;
955 MutexLock mu(vm->weak_globals_lock);
956
957 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
958 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
959 << "failed to find entry";
960 }
961 }
962
963 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
964 ScopedJniThreadState ts(env);
965 if (obj == NULL) {
966 return NULL;
967 }
968
969 IndirectReferenceTable& locals = ts.Env()->locals;
970
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700971 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
973 return reinterpret_cast<jobject>(ref);
974 }
975
976 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
977 ScopedJniThreadState ts(env);
978 if (obj == NULL) {
979 return;
980 }
981
982 IndirectReferenceTable& locals = ts.Env()->locals;
983
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700984 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 if (!locals.Remove(cookie, obj)) {
986 // Attempting to delete a local reference that is not in the
987 // topmost local reference frame is a no-op. DeleteLocalRef returns
988 // void and doesn't throw any exceptions, but we should probably
989 // complain about it so the user will notice that things aren't
990 // going quite the way they expect.
991 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
992 << "failed to find entry";
993 }
994 }
995
996 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
997 ScopedJniThreadState ts(env);
998 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
999 ? JNI_TRUE : JNI_FALSE;
1000 }
1001
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001002 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001004 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001005 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001006 return NULL;
1007 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001008 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 }
1010
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 ScopedJniThreadState ts(env);
1013 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 va_start(args, mid);
1015 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 va_end(args);
1017 return result;
1018 }
1019
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001022 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001023 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001024 return NULL;
1025 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001026 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001027 if (result == NULL) {
1028 return NULL;
1029 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001030 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001032 if (!ts.Self()->IsExceptionPending()) {
1033 return local_result;
1034 } else {
1035 return NULL;
1036 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001041 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001042 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001043 return NULL;
1044 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001045 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001046 if (result == NULL) {
1047 return NULL;
1048 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001049 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001051 if (!ts.Self()->IsExceptionPending()) {
1052 return local_result;
1053 } else {
1054 return NULL;
1055 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1059 ScopedJniThreadState ts(env);
1060 return FindMethodID(ts, c, name, sig, false);
1061 }
1062
1063 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1064 ScopedJniThreadState ts(env);
1065 return FindMethodID(ts, c, name, sig, true);
1066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 va_list ap;
1071 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001072 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001074 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001079 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001080 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001085 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001086 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_list ap;
1092 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001093 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 va_end(ap);
1095 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001100 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001105 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 va_list ap;
1111 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001112 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 va_end(ap);
1114 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001119 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001124 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_list ap;
1130 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001131 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 va_end(ap);
1133 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001138 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001143 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_list ap;
1149 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001150 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 va_end(ap);
1152 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001157 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001162 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 va_list ap;
1168 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001169 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 va_end(ap);
1171 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001176 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001181 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 va_list ap;
1187 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001188 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 va_end(ap);
1190 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001195 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001200 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 va_list ap;
1206 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001207 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 va_end(ap);
1209 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001214 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001219 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 va_list ap;
1225 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001226 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 va_end(ap);
1228 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001233 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001238 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 va_list ap;
1244 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001245 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001251 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001256 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
1259 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
1262 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001264 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001265 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 va_end(ap);
1267 return local_result;
1268 }
1269
1270 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001273 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001274 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
1277 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001280 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001281 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
1284 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 ScopedJniThreadState ts(env);
1287 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001288 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001289 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 va_end(ap);
1291 return result.z;
1292 }
1293
1294 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001295 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001297 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
1300 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001303 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
1306 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 ScopedJniThreadState ts(env);
1309 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001310 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001311 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 va_end(ap);
1313 return result.b;
1314 }
1315
1316 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001317 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001319 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
1322 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001325 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
1328 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001329 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 ScopedJniThreadState ts(env);
1331 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001333 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 va_end(ap);
1335 return result.c;
1336 }
1337
1338 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001339 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001341 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
1344 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001347 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
1350 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001351 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 ScopedJniThreadState ts(env);
1353 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001355 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 va_end(ap);
1357 return result.s;
1358 }
1359
1360 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001361 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001363 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 }
1365
1366 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001367 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001369 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
Elliott Hughes418d20f2011-09-22 14:00:39 -07001372 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 ScopedJniThreadState ts(env);
1374 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001375 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001376 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 va_end(ap);
1378 return result.i;
1379 }
1380
1381 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001382 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001384 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
1386
1387 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001388 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001390 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
1392
1393 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001394 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 ScopedJniThreadState ts(env);
1396 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001397 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001398 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 va_end(ap);
1400 return result.j;
1401 }
1402
1403 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001404 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001406 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 }
1408
1409 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001410 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001412 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
1415 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001416 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 ScopedJniThreadState ts(env);
1418 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001419 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001420 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 va_end(ap);
1422 return result.f;
1423 }
1424
1425 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001426 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001428 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
1431 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001432 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001434 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
1436
1437 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001438 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 ScopedJniThreadState ts(env);
1440 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001441 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001442 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 va_end(ap);
1444 return result.d;
1445 }
1446
1447 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001448 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001450 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 }
1452
1453 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001454 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001456 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
1459 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001460 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 ScopedJniThreadState ts(env);
1462 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001463 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001464 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 va_end(ap);
1466 }
1467
1468 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001469 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001471 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
1474 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001475 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001477 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001480 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 ScopedJniThreadState ts(env);
1482 return FindFieldID(ts, c, name, sig, false);
1483 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001484
1485
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001486 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 ScopedJniThreadState ts(env);
1488 return FindFieldID(ts, c, name, sig, true);
1489 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001490
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001493 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001494 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001495 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001496 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001497
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001500 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001501 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506 Object* o = Decode<Object*>(ts, java_object);
1507 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001508 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001509 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
1511
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001513 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001515 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
1518
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519#define GET_PRIMITIVE_FIELD(fn, instance) \
1520 ScopedJniThreadState ts(env); \
1521 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001522 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001523 return f->fn(o)
1524
1525#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1526 ScopedJniThreadState ts(env); \
1527 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001528 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001529 f->fn(o, value)
1530
1531 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1532 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1536 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001539 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1540 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 }
1542
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001543 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1544 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001547 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1548 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 }
1550
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001551 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1552 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001555 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1556 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001559 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1560 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001563 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1564 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 }
1566
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001567 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1568 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001571 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1572 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001575 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1576 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001579 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1580 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001583 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1584 GET_PRIMITIVE_FIELD(GetLong, NULL);
1585 }
1586
1587 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1588 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1589 }
1590
1591 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1592 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1593 }
1594
1595 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1596 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1597 }
1598
1599 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1600 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1601 }
1602
1603 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1604 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1605 }
1606
1607 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1608 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1609 }
1610
1611 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1612 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1613 }
1614
1615 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1616 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1617 }
1618
1619 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1620 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1621 }
1622
1623 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1624 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1625 }
1626
1627 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1628 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1629 }
1630
1631 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1632 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1633 }
1634
1635 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1636 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1637 }
1638
1639 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1640 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1641 }
1642
1643 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1644 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1645 }
1646
1647 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1648 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1649 }
1650
1651 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1652 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1653 }
1654
1655 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1656 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 }
1658
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 ScopedJniThreadState ts(env);
1661 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001662 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001663 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001664 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 va_end(ap);
1666 return local_result;
1667 }
1668
Elliott Hughes418d20f2011-09-22 14:00:39 -07001669 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001671 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001672 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
Elliott Hughes418d20f2011-09-22 14:00:39 -07001675 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001677 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001678 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 }
1680
Elliott Hughes418d20f2011-09-22 14:00:39 -07001681 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 ScopedJniThreadState ts(env);
1683 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001685 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 va_end(ap);
1687 return result.z;
1688 }
1689
Elliott Hughes418d20f2011-09-22 14:00:39 -07001690 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001692 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 }
1694
Elliott Hughes418d20f2011-09-22 14:00:39 -07001695 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 }
1699
Elliott Hughes72025e52011-08-23 17:50:30 -07001700 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 ScopedJniThreadState ts(env);
1702 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001704 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 va_end(ap);
1706 return result.b;
1707 }
1708
Elliott Hughes418d20f2011-09-22 14:00:39 -07001709 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001711 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 }
1713
Elliott Hughes418d20f2011-09-22 14:00:39 -07001714 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 ScopedJniThreadState ts(env);
1721 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001723 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 va_end(ap);
1725 return result.c;
1726 }
1727
Elliott Hughes418d20f2011-09-22 14:00:39 -07001728 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001730 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes418d20f2011-09-22 14:00:39 -07001733 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001735 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes72025e52011-08-23 17:50:30 -07001738 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 ScopedJniThreadState ts(env);
1740 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001742 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 va_end(ap);
1744 return result.s;
1745 }
1746
Elliott Hughes418d20f2011-09-22 14:00:39 -07001747 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001749 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes418d20f2011-09-22 14:00:39 -07001752 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001754 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 ScopedJniThreadState ts(env);
1759 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001760 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001761 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 va_end(ap);
1763 return result.i;
1764 }
1765
Elliott Hughes418d20f2011-09-22 14:00:39 -07001766 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001768 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes418d20f2011-09-22 14:00:39 -07001771 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001773 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes72025e52011-08-23 17:50:30 -07001776 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 ScopedJniThreadState ts(env);
1778 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001779 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001780 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 va_end(ap);
1782 return result.j;
1783 }
1784
Elliott Hughes418d20f2011-09-22 14:00:39 -07001785 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001787 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
Elliott Hughes418d20f2011-09-22 14:00:39 -07001790 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001792 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes72025e52011-08-23 17:50:30 -07001795 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 ScopedJniThreadState ts(env);
1797 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001798 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001799 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 va_end(ap);
1801 return result.f;
1802 }
1803
Elliott Hughes418d20f2011-09-22 14:00:39 -07001804 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001806 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 }
1808
Elliott Hughes418d20f2011-09-22 14:00:39 -07001809 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001811 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
Elliott Hughes72025e52011-08-23 17:50:30 -07001814 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 ScopedJniThreadState ts(env);
1816 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001817 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001818 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 va_end(ap);
1820 return result.d;
1821 }
1822
Elliott Hughes418d20f2011-09-22 14:00:39 -07001823 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001825 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
Elliott Hughes418d20f2011-09-22 14:00:39 -07001828 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001830 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes72025e52011-08-23 17:50:30 -07001833 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 ScopedJniThreadState ts(env);
1835 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001836 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001837 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 va_end(ap);
1839 }
1840
Elliott Hughes418d20f2011-09-22 14:00:39 -07001841 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001843 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 }
1845
Elliott Hughes418d20f2011-09-22 14:00:39 -07001846 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001848 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 }
1850
Elliott Hughes814e4032011-08-23 12:07:56 -07001851 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001853 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001854 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 }
1856
1857 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1858 ScopedJniThreadState ts(env);
1859 if (utf == NULL) {
1860 return NULL;
1861 }
1862 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001863 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 }
1865
Elliott Hughes814e4032011-08-23 12:07:56 -07001866 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1867 ScopedJniThreadState ts(env);
1868 return Decode<String*>(ts, java_string)->GetLength();
1869 }
1870
1871 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1872 ScopedJniThreadState ts(env);
1873 return Decode<String*>(ts, java_string)->GetUtfLength();
1874 }
1875
Elliott Hughesb465ab02011-08-24 11:21:21 -07001876 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001878 String* s = Decode<String*>(ts, java_string);
1879 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1880 ThrowSIOOBE(ts, start, length, s->GetLength());
1881 } else {
1882 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1883 memcpy(buf, chars + start, length * sizeof(jchar));
1884 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001885 }
1886
Elliott Hughesb465ab02011-08-24 11:21:21 -07001887 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001888 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001889 String* s = Decode<String*>(ts, java_string);
1890 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1891 ThrowSIOOBE(ts, start, length, s->GetLength());
1892 } else {
1893 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1894 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1895 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001896 }
1897
Elliott Hughes75770752011-08-24 17:52:38 -07001898 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001899 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001900 String* s = Decode<String*>(ts, java_string);
1901 const CharArray* chars = s->GetCharArray();
1902 PinPrimitiveArray(ts, chars);
1903 if (is_copy != NULL) {
1904 *is_copy = JNI_FALSE;
1905 }
1906 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001907 }
1908
Elliott Hughes75770752011-08-24 17:52:38 -07001909 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001910 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001911 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001912 }
1913
Elliott Hughes75770752011-08-24 17:52:38 -07001914 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001915 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001916 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001917 }
1918
Elliott Hughes75770752011-08-24 17:52:38 -07001919 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001920 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001921 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 }
1923
Elliott Hughes75770752011-08-24 17:52:38 -07001924 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001925 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001926 if (java_string == NULL) {
1927 return NULL;
1928 }
1929 if (is_copy != NULL) {
1930 *is_copy = JNI_TRUE;
1931 }
1932 String* s = Decode<String*>(ts, java_string);
1933 size_t byte_count = s->GetUtfLength();
1934 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001935 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001936 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1937 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1938 bytes[byte_count] = '\0';
1939 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001940 }
1941
Elliott Hughes75770752011-08-24 17:52:38 -07001942 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001943 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001944 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001945 }
1946
Elliott Hughesbd935992011-08-22 11:59:34 -07001947 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001949 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001950 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001951 Array* array = obj->AsArray();
1952 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Elliott Hughes814e4032011-08-23 12:07:56 -07001955 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001957 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001958 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 }
1960
1961 static void SetObjectArrayElement(JNIEnv* env,
1962 jobjectArray java_array, jsize index, jobject java_value) {
1963 ScopedJniThreadState ts(env);
1964 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1965 Object* value = Decode<Object*>(ts, java_value);
1966 array->Set(index, value);
1967 }
1968
1969 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1970 ScopedJniThreadState ts(env);
1971 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1972 }
1973
1974 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1975 ScopedJniThreadState ts(env);
1976 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1977 }
1978
1979 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1980 ScopedJniThreadState ts(env);
1981 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1982 }
1983
1984 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1985 ScopedJniThreadState ts(env);
1986 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1987 }
1988
1989 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1990 ScopedJniThreadState ts(env);
1991 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1992 }
1993
1994 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1995 ScopedJniThreadState ts(env);
1996 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1997 }
1998
1999 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2000 ScopedJniThreadState ts(env);
2001 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
2002 }
2003
2004 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
2005 ScopedJniThreadState ts(env);
2006 CHECK_GE(length, 0); // TODO: ReportJniError
2007
2008 // Compute the array class corresponding to the given element class.
2009 Class* element_class = Decode<Class*>(ts, element_jclass);
2010 std::string descriptor;
2011 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002012 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002013
2014 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002015 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2016 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 return NULL;
2018 }
2019
Elliott Hughes75770752011-08-24 17:52:38 -07002020 // Allocate and initialize if necessary.
2021 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002023 if (initial_element != NULL) {
2024 Object* initial_object = Decode<Object*>(ts, initial_element);
2025 for (jsize i = 0; i < length; ++i) {
2026 result->Set(i, initial_object);
2027 }
2028 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002029 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
2032 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2033 ScopedJniThreadState ts(env);
2034 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2035 }
2036
Elliott Hughes75770752011-08-24 17:52:38 -07002037 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002038 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002039 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002040 }
2041
Elliott Hughes75770752011-08-24 17:52:38 -07002042 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002043 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002044 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002045 }
2046
Elliott Hughes75770752011-08-24 17:52:38 -07002047 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002049 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Elliott Hughes75770752011-08-24 17:52:38 -07002052 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray 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<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes75770752011-08-24 17:52:38 -07002057 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray 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<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Elliott Hughes75770752011-08-24 17:52:38 -07002062 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray 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<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes75770752011-08-24 17:52:38 -07002067 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray 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<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Elliott Hughes75770752011-08-24 17:52:38 -07002072 static jint* GetIntArrayElements(JNIEnv* env, jintArray 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<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes75770752011-08-24 17:52:38 -07002077 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray 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<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Elliott Hughes75770752011-08-24 17:52:38 -07002082 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray 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<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Elliott Hughes75770752011-08-24 17:52:38 -07002087 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002089 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
Elliott Hughes75770752011-08-24 17:52:38 -07002092 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* 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 ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* 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 ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* 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 ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* 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 ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* 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 ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* 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 ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* 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 Hughes814e4032011-08-23 12:07:56 -07002127 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002129 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
Elliott Hughes814e4032011-08-23 12:07:56 -07002132 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002134 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 }
2136
Elliott Hughes814e4032011-08-23 12:07:56 -07002137 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002139 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 }
2141
Elliott Hughes814e4032011-08-23 12:07:56 -07002142 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002144 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 }
2146
Elliott Hughes814e4032011-08-23 12:07:56 -07002147 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002149 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 }
2151
Elliott Hughes814e4032011-08-23 12:07:56 -07002152 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002154 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
2156
Elliott Hughes814e4032011-08-23 12:07:56 -07002157 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002159 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 }
2161
Elliott Hughes814e4032011-08-23 12:07:56 -07002162 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002164 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Elliott Hughes814e4032011-08-23 12:07:56 -07002167 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002169 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Elliott Hughes814e4032011-08-23 12:07:56 -07002172 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002174 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
2176
Elliott Hughes814e4032011-08-23 12:07:56 -07002177 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002178 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002179 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
Elliott Hughes814e4032011-08-23 12:07:56 -07002182 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002184 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Elliott Hughes814e4032011-08-23 12:07:56 -07002187 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002189 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Elliott Hughes814e4032011-08-23 12:07:56 -07002192 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002194 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Elliott Hughes814e4032011-08-23 12:07:56 -07002197 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002199 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
Elliott Hughes814e4032011-08-23 12:07:56 -07002202 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002204 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 }
2206
Elliott Hughes5174fe62011-08-23 15:12:35 -07002207 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002209 Class* c = Decode<Class*>(ts, java_class);
2210
Elliott Hughes5174fe62011-08-23 15:12:35 -07002211 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 const char* name = methods[i].name;
2213 const char* sig = methods[i].signature;
2214
2215 if (*sig == '!') {
2216 // TODO: fast jni. it's too noisy to log all these.
2217 ++sig;
2218 }
2219
Elliott Hughes5174fe62011-08-23 15:12:35 -07002220 Method* m = c->FindDirectMethod(name, sig);
2221 if (m == NULL) {
2222 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002224 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002225 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002226 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002228 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002229 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002230 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 return JNI_ERR;
2232 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002233
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002234 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002235
2236 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 }
2238 return JNI_OK;
2239 }
2240
Elliott Hughes5174fe62011-08-23 15:12:35 -07002241 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002243 Class* c = Decode<Class*>(ts, java_class);
2244
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002245 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002246
2247 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2248 Method* m = c->GetDirectMethod(i);
2249 if (m->IsNative()) {
2250 m->UnregisterNative();
2251 }
2252 }
2253 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2254 Method* m = c->GetVirtualMethod(i);
2255 if (m->IsNative()) {
2256 m->UnregisterNative();
2257 }
2258 }
2259
2260 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 }
2262
Elliott Hughes72025e52011-08-23 17:50:30 -07002263 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002264 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002265 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002266 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 }
2268
Elliott Hughes72025e52011-08-23 17:50:30 -07002269 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002270 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002271 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002272 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002273 }
2274
2275 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2276 ScopedJniThreadState ts(env);
2277 Runtime* runtime = Runtime::Current();
2278 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002279 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 } else {
2281 *vm = NULL;
2282 }
2283 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2284 }
2285
Elliott Hughescdf53122011-08-19 15:46:09 -07002286 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2287 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002288
2289 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002290 CHECK(address != NULL); // TODO: ReportJniError
2291 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002292
2293 jclass buffer_class = GetDirectByteBufferClass(env);
2294 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2295 if (mid == NULL) {
2296 return NULL;
2297 }
2298
2299 // At the moment, the Java side is limited to 32 bits.
2300 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2301 CHECK_LE(capacity, 0xffffffff);
2302 jint address_arg = reinterpret_cast<jint>(address);
2303 jint capacity_arg = static_cast<jint>(capacity);
2304
2305 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2306 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002307 }
2308
Elliott Hughesb465ab02011-08-24 11:21:21 -07002309 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002310 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002311 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2312 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002313 }
2314
Elliott Hughesb465ab02011-08-24 11:21:21 -07002315 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002316 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002317 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2318 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002319 }
2320
Elliott Hughesb465ab02011-08-24 11:21:21 -07002321 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002322 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002323
Elliott Hughes75770752011-08-24 17:52:38 -07002324 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002325
2326 // Do we definitely know what kind of reference this is?
2327 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2328 IndirectRefKind kind = GetIndirectRefKind(ref);
2329 switch (kind) {
2330 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002331 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2332 return JNILocalRefType;
2333 }
2334 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002335 case kGlobal:
2336 return JNIGlobalRefType;
2337 case kWeakGlobal:
2338 return JNIWeakGlobalRefType;
2339 case kSirtOrInvalid:
2340 // Is it in a stack IRT?
2341 if (ts.Self()->SirtContains(java_object)) {
2342 return JNILocalRefType;
2343 }
2344
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002345 if (!ts.Env()->work_around_app_jni_bugs) {
2346 return JNIInvalidRefType;
2347 }
2348
Elliott Hughesb465ab02011-08-24 11:21:21 -07002349 // If we're handing out direct pointers, check whether it's a direct pointer
2350 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002351 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002352 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002353 return JNILocalRefType;
2354 }
2355 }
2356
2357 return JNIInvalidRefType;
2358 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002359 }
2360};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002361
Elliott Hughesa2501992011-08-26 19:39:54 -07002362const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002363 NULL, // reserved0.
2364 NULL, // reserved1.
2365 NULL, // reserved2.
2366 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002367 JNI::GetVersion,
2368 JNI::DefineClass,
2369 JNI::FindClass,
2370 JNI::FromReflectedMethod,
2371 JNI::FromReflectedField,
2372 JNI::ToReflectedMethod,
2373 JNI::GetSuperclass,
2374 JNI::IsAssignableFrom,
2375 JNI::ToReflectedField,
2376 JNI::Throw,
2377 JNI::ThrowNew,
2378 JNI::ExceptionOccurred,
2379 JNI::ExceptionDescribe,
2380 JNI::ExceptionClear,
2381 JNI::FatalError,
2382 JNI::PushLocalFrame,
2383 JNI::PopLocalFrame,
2384 JNI::NewGlobalRef,
2385 JNI::DeleteGlobalRef,
2386 JNI::DeleteLocalRef,
2387 JNI::IsSameObject,
2388 JNI::NewLocalRef,
2389 JNI::EnsureLocalCapacity,
2390 JNI::AllocObject,
2391 JNI::NewObject,
2392 JNI::NewObjectV,
2393 JNI::NewObjectA,
2394 JNI::GetObjectClass,
2395 JNI::IsInstanceOf,
2396 JNI::GetMethodID,
2397 JNI::CallObjectMethod,
2398 JNI::CallObjectMethodV,
2399 JNI::CallObjectMethodA,
2400 JNI::CallBooleanMethod,
2401 JNI::CallBooleanMethodV,
2402 JNI::CallBooleanMethodA,
2403 JNI::CallByteMethod,
2404 JNI::CallByteMethodV,
2405 JNI::CallByteMethodA,
2406 JNI::CallCharMethod,
2407 JNI::CallCharMethodV,
2408 JNI::CallCharMethodA,
2409 JNI::CallShortMethod,
2410 JNI::CallShortMethodV,
2411 JNI::CallShortMethodA,
2412 JNI::CallIntMethod,
2413 JNI::CallIntMethodV,
2414 JNI::CallIntMethodA,
2415 JNI::CallLongMethod,
2416 JNI::CallLongMethodV,
2417 JNI::CallLongMethodA,
2418 JNI::CallFloatMethod,
2419 JNI::CallFloatMethodV,
2420 JNI::CallFloatMethodA,
2421 JNI::CallDoubleMethod,
2422 JNI::CallDoubleMethodV,
2423 JNI::CallDoubleMethodA,
2424 JNI::CallVoidMethod,
2425 JNI::CallVoidMethodV,
2426 JNI::CallVoidMethodA,
2427 JNI::CallNonvirtualObjectMethod,
2428 JNI::CallNonvirtualObjectMethodV,
2429 JNI::CallNonvirtualObjectMethodA,
2430 JNI::CallNonvirtualBooleanMethod,
2431 JNI::CallNonvirtualBooleanMethodV,
2432 JNI::CallNonvirtualBooleanMethodA,
2433 JNI::CallNonvirtualByteMethod,
2434 JNI::CallNonvirtualByteMethodV,
2435 JNI::CallNonvirtualByteMethodA,
2436 JNI::CallNonvirtualCharMethod,
2437 JNI::CallNonvirtualCharMethodV,
2438 JNI::CallNonvirtualCharMethodA,
2439 JNI::CallNonvirtualShortMethod,
2440 JNI::CallNonvirtualShortMethodV,
2441 JNI::CallNonvirtualShortMethodA,
2442 JNI::CallNonvirtualIntMethod,
2443 JNI::CallNonvirtualIntMethodV,
2444 JNI::CallNonvirtualIntMethodA,
2445 JNI::CallNonvirtualLongMethod,
2446 JNI::CallNonvirtualLongMethodV,
2447 JNI::CallNonvirtualLongMethodA,
2448 JNI::CallNonvirtualFloatMethod,
2449 JNI::CallNonvirtualFloatMethodV,
2450 JNI::CallNonvirtualFloatMethodA,
2451 JNI::CallNonvirtualDoubleMethod,
2452 JNI::CallNonvirtualDoubleMethodV,
2453 JNI::CallNonvirtualDoubleMethodA,
2454 JNI::CallNonvirtualVoidMethod,
2455 JNI::CallNonvirtualVoidMethodV,
2456 JNI::CallNonvirtualVoidMethodA,
2457 JNI::GetFieldID,
2458 JNI::GetObjectField,
2459 JNI::GetBooleanField,
2460 JNI::GetByteField,
2461 JNI::GetCharField,
2462 JNI::GetShortField,
2463 JNI::GetIntField,
2464 JNI::GetLongField,
2465 JNI::GetFloatField,
2466 JNI::GetDoubleField,
2467 JNI::SetObjectField,
2468 JNI::SetBooleanField,
2469 JNI::SetByteField,
2470 JNI::SetCharField,
2471 JNI::SetShortField,
2472 JNI::SetIntField,
2473 JNI::SetLongField,
2474 JNI::SetFloatField,
2475 JNI::SetDoubleField,
2476 JNI::GetStaticMethodID,
2477 JNI::CallStaticObjectMethod,
2478 JNI::CallStaticObjectMethodV,
2479 JNI::CallStaticObjectMethodA,
2480 JNI::CallStaticBooleanMethod,
2481 JNI::CallStaticBooleanMethodV,
2482 JNI::CallStaticBooleanMethodA,
2483 JNI::CallStaticByteMethod,
2484 JNI::CallStaticByteMethodV,
2485 JNI::CallStaticByteMethodA,
2486 JNI::CallStaticCharMethod,
2487 JNI::CallStaticCharMethodV,
2488 JNI::CallStaticCharMethodA,
2489 JNI::CallStaticShortMethod,
2490 JNI::CallStaticShortMethodV,
2491 JNI::CallStaticShortMethodA,
2492 JNI::CallStaticIntMethod,
2493 JNI::CallStaticIntMethodV,
2494 JNI::CallStaticIntMethodA,
2495 JNI::CallStaticLongMethod,
2496 JNI::CallStaticLongMethodV,
2497 JNI::CallStaticLongMethodA,
2498 JNI::CallStaticFloatMethod,
2499 JNI::CallStaticFloatMethodV,
2500 JNI::CallStaticFloatMethodA,
2501 JNI::CallStaticDoubleMethod,
2502 JNI::CallStaticDoubleMethodV,
2503 JNI::CallStaticDoubleMethodA,
2504 JNI::CallStaticVoidMethod,
2505 JNI::CallStaticVoidMethodV,
2506 JNI::CallStaticVoidMethodA,
2507 JNI::GetStaticFieldID,
2508 JNI::GetStaticObjectField,
2509 JNI::GetStaticBooleanField,
2510 JNI::GetStaticByteField,
2511 JNI::GetStaticCharField,
2512 JNI::GetStaticShortField,
2513 JNI::GetStaticIntField,
2514 JNI::GetStaticLongField,
2515 JNI::GetStaticFloatField,
2516 JNI::GetStaticDoubleField,
2517 JNI::SetStaticObjectField,
2518 JNI::SetStaticBooleanField,
2519 JNI::SetStaticByteField,
2520 JNI::SetStaticCharField,
2521 JNI::SetStaticShortField,
2522 JNI::SetStaticIntField,
2523 JNI::SetStaticLongField,
2524 JNI::SetStaticFloatField,
2525 JNI::SetStaticDoubleField,
2526 JNI::NewString,
2527 JNI::GetStringLength,
2528 JNI::GetStringChars,
2529 JNI::ReleaseStringChars,
2530 JNI::NewStringUTF,
2531 JNI::GetStringUTFLength,
2532 JNI::GetStringUTFChars,
2533 JNI::ReleaseStringUTFChars,
2534 JNI::GetArrayLength,
2535 JNI::NewObjectArray,
2536 JNI::GetObjectArrayElement,
2537 JNI::SetObjectArrayElement,
2538 JNI::NewBooleanArray,
2539 JNI::NewByteArray,
2540 JNI::NewCharArray,
2541 JNI::NewShortArray,
2542 JNI::NewIntArray,
2543 JNI::NewLongArray,
2544 JNI::NewFloatArray,
2545 JNI::NewDoubleArray,
2546 JNI::GetBooleanArrayElements,
2547 JNI::GetByteArrayElements,
2548 JNI::GetCharArrayElements,
2549 JNI::GetShortArrayElements,
2550 JNI::GetIntArrayElements,
2551 JNI::GetLongArrayElements,
2552 JNI::GetFloatArrayElements,
2553 JNI::GetDoubleArrayElements,
2554 JNI::ReleaseBooleanArrayElements,
2555 JNI::ReleaseByteArrayElements,
2556 JNI::ReleaseCharArrayElements,
2557 JNI::ReleaseShortArrayElements,
2558 JNI::ReleaseIntArrayElements,
2559 JNI::ReleaseLongArrayElements,
2560 JNI::ReleaseFloatArrayElements,
2561 JNI::ReleaseDoubleArrayElements,
2562 JNI::GetBooleanArrayRegion,
2563 JNI::GetByteArrayRegion,
2564 JNI::GetCharArrayRegion,
2565 JNI::GetShortArrayRegion,
2566 JNI::GetIntArrayRegion,
2567 JNI::GetLongArrayRegion,
2568 JNI::GetFloatArrayRegion,
2569 JNI::GetDoubleArrayRegion,
2570 JNI::SetBooleanArrayRegion,
2571 JNI::SetByteArrayRegion,
2572 JNI::SetCharArrayRegion,
2573 JNI::SetShortArrayRegion,
2574 JNI::SetIntArrayRegion,
2575 JNI::SetLongArrayRegion,
2576 JNI::SetFloatArrayRegion,
2577 JNI::SetDoubleArrayRegion,
2578 JNI::RegisterNatives,
2579 JNI::UnregisterNatives,
2580 JNI::MonitorEnter,
2581 JNI::MonitorExit,
2582 JNI::GetJavaVM,
2583 JNI::GetStringRegion,
2584 JNI::GetStringUTFRegion,
2585 JNI::GetPrimitiveArrayCritical,
2586 JNI::ReleasePrimitiveArrayCritical,
2587 JNI::GetStringCritical,
2588 JNI::ReleaseStringCritical,
2589 JNI::NewWeakGlobalRef,
2590 JNI::DeleteWeakGlobalRef,
2591 JNI::ExceptionCheck,
2592 JNI::NewDirectByteBuffer,
2593 JNI::GetDirectBufferAddress,
2594 JNI::GetDirectBufferCapacity,
2595 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002596};
2597
Elliott Hughes75770752011-08-24 17:52:38 -07002598JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002599 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002600 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002601 local_ref_cookie(IRT_FIRST_SEGMENT),
2602 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002603 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002604 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002605 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002606 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002607 functions = unchecked_functions = &gNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002608 if (vm->check_jni) {
2609 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002610 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002611 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2612 // errors will ensue.
2613 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2614 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002615}
2616
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002617JNIEnvExt::~JNIEnvExt() {
2618}
2619
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002620void JNIEnvExt::EnableCheckJni() {
2621 check_jni = true;
2622 functions = GetCheckJniNativeInterface();
2623}
2624
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002625void JNIEnvExt::DumpReferenceTables() {
2626 locals.Dump();
2627 monitors.Dump();
2628}
2629
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002630void JNIEnvExt::PushFrame(int capacity) {
2631 stacked_local_ref_cookies.push_back(local_ref_cookie);
2632 local_ref_cookie = locals.GetSegmentState();
2633}
2634
2635void JNIEnvExt::PopFrame() {
2636 locals.SetSegmentState(local_ref_cookie);
2637 local_ref_cookie = stacked_local_ref_cookies.back();
2638 stacked_local_ref_cookies.pop_back();
2639}
2640
Carl Shapiroea4dca82011-08-01 13:45:38 -07002641// JNI Invocation interface.
2642
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002643extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2644 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2645 if (args->version < JNI_VERSION_1_2) {
2646 return JNI_EVERSION;
2647 }
2648 Runtime::Options options;
2649 for (int i = 0; i < args->nOptions; ++i) {
2650 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002651 options.push_back(std::make_pair(StringPiece(option->optionString),
2652 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002653 }
2654 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002655 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002656 if (runtime == NULL) {
2657 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002658 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002659 runtime->Start();
2660 *p_env = Thread::Current()->GetJniEnv();
2661 *p_vm = runtime->GetJavaVM();
2662 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002663}
2664
Elliott Hughesf2682d52011-08-15 16:37:04 -07002665extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002666 Runtime* runtime = Runtime::Current();
2667 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002668 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002669 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002670 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002671 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002672 }
2673 return JNI_OK;
2674}
2675
2676// Historically unsupported.
2677extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2678 return JNI_ERR;
2679}
2680
Elliott Hughescdf53122011-08-19 15:46:09 -07002681class JII {
2682 public:
2683 static jint DestroyJavaVM(JavaVM* vm) {
2684 if (vm == NULL) {
2685 return JNI_ERR;
2686 } else {
2687 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2688 delete raw_vm->runtime;
2689 return JNI_OK;
2690 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002691 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002692
Elliott Hughescdf53122011-08-19 15:46:09 -07002693 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002694 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002695 }
2696
2697 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002698 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002699 }
2700
2701 static jint DetachCurrentThread(JavaVM* vm) {
2702 if (vm == NULL) {
2703 return JNI_ERR;
2704 } else {
2705 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2706 Runtime* runtime = raw_vm->runtime;
2707 runtime->DetachCurrentThread();
2708 return JNI_OK;
2709 }
2710 }
2711
2712 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2713 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2714 return JNI_EVERSION;
2715 }
2716 if (vm == NULL || env == NULL) {
2717 return JNI_ERR;
2718 }
2719 Thread* thread = Thread::Current();
2720 if (thread == NULL) {
2721 *env = NULL;
2722 return JNI_EDETACHED;
2723 }
2724 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002725 return JNI_OK;
2726 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002727};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002728
Elliott Hughesa2501992011-08-26 19:39:54 -07002729const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002730 NULL, // reserved0
2731 NULL, // reserved1
2732 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002733 JII::DestroyJavaVM,
2734 JII::AttachCurrentThread,
2735 JII::DetachCurrentThread,
2736 JII::GetEnv,
2737 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002738};
2739
Elliott Hughesa0957642011-09-02 14:27:33 -07002740JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002741 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002742 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002743 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002744 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002745 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002746 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002747 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002748 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002749 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002750 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002751 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002752 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002753 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002754 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002755 functions = unchecked_functions = &gInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002756 if (options->check_jni_) {
2757 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002758 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002759}
2760
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002761JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002762 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002763}
2764
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002765void JavaVMExt::EnableCheckJni() {
2766 check_jni = true;
2767 functions = GetCheckJniInvokeInterface();
2768}
2769
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002770void JavaVMExt::DumpReferenceTables() {
2771 {
2772 MutexLock mu(globals_lock);
2773 globals.Dump();
2774 }
2775 {
2776 MutexLock mu(weak_globals_lock);
2777 weak_globals.Dump();
2778 }
2779 {
2780 MutexLock mu(pins_lock);
2781 pin_table.Dump();
2782 }
2783}
2784
Elliott Hughes75770752011-08-24 17:52:38 -07002785bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2786 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002787
2788 // See if we've already loaded this library. If we have, and the class loader
2789 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002790 // TODO: for better results we should canonicalize the pathname (or even compare
2791 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002792 SharedLibrary* library;
2793 {
2794 // TODO: move the locking (and more of this logic) into Libraries.
2795 MutexLock mu(libraries_lock);
2796 library = libraries->Get(path);
2797 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002798 if (library != NULL) {
2799 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002800 // The library will be associated with class_loader. The JNI
2801 // spec says we can't load the same library into more than one
2802 // class loader.
2803 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2804 "ClassLoader %p; can't open in ClassLoader %p",
2805 path.c_str(), library->GetClassLoader(), class_loader);
2806 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002807 return false;
2808 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002809 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2810 << "ClassLoader " << class_loader << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002811 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002812 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2813 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002814 return false;
2815 }
2816 return true;
2817 }
2818
2819 // Open the shared library. Because we're using a full path, the system
2820 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2821 // resolve this library's dependencies though.)
2822
2823 // Failures here are expected when java.library.path has several entries
2824 // and we have to hunt for the lib.
2825
2826 // The current version of the dynamic linker prints detailed information
2827 // about dlopen() failures. Some things to check if the message is
2828 // cryptic:
2829 // - make sure the library exists on the device
2830 // - verify that the right path is being opened (the debug log message
2831 // above can help with that)
2832 // - check to see if the library is valid (e.g. not zero bytes long)
2833 // - check config/prelink-linux-arm.map to ensure that the library
2834 // is listed and is not being overrun by the previous entry (if
2835 // loading suddenly stops working on a prelinked library, this is
2836 // a good one to check)
2837 // - write a trivial app that calls sleep() then dlopen(), attach
2838 // to it with "strace -p <pid>" while it sleeps, and watch for
2839 // attempts to open nonexistent dependent shared libs
2840
2841 // TODO: automate some of these checks!
2842
2843 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002844 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002845 // the GC to ignore us.
2846 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002847 void* handle = NULL;
2848 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002849 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002850 handle = dlopen(path.c_str(), RTLD_LAZY);
2851 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002852
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002853 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002854
2855 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002856 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002857 return false;
2858 }
2859
2860 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002861 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002862 // TODO: move the locking (and more of this logic) into Libraries.
2863 MutexLock mu(libraries_lock);
2864 library = libraries->Get(path);
2865 if (library != NULL) {
2866 LOG(INFO) << "WOW: we lost a race to add shared library: "
2867 << "\"" << path << "\" ClassLoader=" << class_loader;
2868 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002869 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002870 library = new SharedLibrary(path, handle, class_loader);
2871 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002872 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002873
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002874 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002875
2876 bool result = true;
2877 void* sym = dlsym(handle, "JNI_OnLoad");
2878 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002879 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002880 } else {
2881 // Call JNI_OnLoad. We have to override the current class
2882 // loader, which will always be "null" since the stuff at the
2883 // top of the stack is around Runtime.loadLibrary(). (See
2884 // the comments in the JNI FindClass function.)
2885 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2886 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002887 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002888 self->SetClassLoaderOverride(class_loader);
2889
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002890 int version = 0;
2891 {
2892 ScopedThreadStateChange tsc(self, Thread::kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002893 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002894 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002895 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002896
Brian Carlstromaded5f72011-10-07 17:15:04 -07002897 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002898
2899 if (version != JNI_VERSION_1_2 &&
2900 version != JNI_VERSION_1_4 &&
2901 version != JNI_VERSION_1_6) {
2902 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2903 << "bad version: " << version;
2904 // It's unwise to call dlclose() here, but we can mark it
2905 // as bad and ensure that future load attempts will fail.
2906 // We don't know how far JNI_OnLoad got, so there could
2907 // be some partially-initialized stuff accessible through
2908 // newly-registered native method calls. We could try to
2909 // unregister them, but that doesn't seem worthwhile.
2910 result = false;
2911 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002912 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2913 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002914 }
2915 }
2916
2917 library->SetResult(result);
2918 return result;
2919}
2920
2921void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2922 CHECK(m->IsNative());
2923
2924 Class* c = m->GetDeclaringClass();
2925
2926 // If this is a static method, it could be called before the class
2927 // has been initialized.
2928 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002929 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002930 return NULL;
2931 }
2932 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002933 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002934 }
2935
Brian Carlstrom16192862011-09-12 17:50:06 -07002936 std::string detail;
2937 void* native_method;
2938 {
2939 MutexLock mu(libraries_lock);
2940 native_method = libraries->FindNativeMethod(m, detail);
2941 }
2942 // throwing can cause libraries_lock to be reacquired
2943 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002944 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002945 }
2946 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002947}
2948
Elliott Hughes410c0c82011-09-01 17:58:25 -07002949void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2950 {
2951 MutexLock mu(globals_lock);
2952 globals.VisitRoots(visitor, arg);
2953 }
2954 {
2955 MutexLock mu(pins_lock);
2956 pin_table.VisitRoots(visitor, arg);
2957 }
2958 // The weak_globals table is visited by the GC itself (because it mutates the table).
2959}
2960
Ian Rogersdf20fe02011-07-20 20:34:16 -07002961} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002962
2963std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2964 switch (rhs) {
2965 case JNIInvalidRefType:
2966 os << "JNIInvalidRefType";
2967 return os;
2968 case JNILocalRefType:
2969 os << "JNILocalRefType";
2970 return os;
2971 case JNIGlobalRefType:
2972 os << "JNIGlobalRefType";
2973 return os;
2974 case JNIWeakGlobalRefType:
2975 os << "JNIWeakGlobalRefType";
2976 return os;
2977 }
2978}