blob: 7e145a8f1ec16eef1422e7e1ff1b5435133527f0 [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) {
576 if (vm->verbose_jni) {
577 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
578 << "JNI_OnLoad...]";
579 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700580 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700581 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700582 }
583
584 bool okay = (jni_on_load_result_ == kOkay);
585 if (vm->verbose_jni) {
586 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
587 << (okay ? "succeeded" : "failed") << "]";
588 }
589 return okay;
590 }
591
592 void SetResult(bool result) {
593 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700594 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700595
596 // Broadcast a wakeup to anybody sleeping on the condition variable.
597 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700598 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700599 }
600
601 void* FindSymbol(const std::string& symbol_name) {
602 return dlsym(handle_, symbol_name.c_str());
603 }
604
605 private:
606 enum JNI_OnLoadState {
607 kPending,
608 kFailed,
609 kOkay,
610 };
611
612 // Path to library "/system/lib/libjni.so".
613 std::string path_;
614
615 // The void* returned by dlopen(3).
616 void* handle_;
617
618 // The ClassLoader this library is associated with.
619 Object* class_loader_;
620
621 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700622 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700623 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700624 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700625 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700626 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700627 // Result of earlier JNI_OnLoad call.
628 JNI_OnLoadState jni_on_load_result_;
629};
630
Elliott Hughescdf53122011-08-19 15:46:09 -0700631} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700632
Elliott Hughes79082e32011-08-25 12:07:32 -0700633// This exists mainly to keep implementation details out of the header file.
634class Libraries {
635 public:
636 Libraries() {
637 }
638
639 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700640 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700641 }
642
643 SharedLibrary* Get(const std::string& path) {
644 return libraries_[path];
645 }
646
647 void Put(const std::string& path, SharedLibrary* library) {
648 libraries_[path] = library;
649 }
650
651 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700652 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700653 std::string jni_short_name(JniShortName(m));
654 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700655 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700656 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
657 SharedLibrary* library = it->second;
658 if (library->GetClassLoader() != declaring_class_loader) {
659 // We only search libraries loaded by the appropriate ClassLoader.
660 continue;
661 }
662 // Try the short name then the long name...
663 void* fn = library->FindSymbol(jni_short_name);
664 if (fn == NULL) {
665 fn = library->FindSymbol(jni_long_name);
666 }
667 if (fn != NULL) {
668 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700669 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700670 << " in \"" << library->GetPath() << "\"]";
671 }
672 return fn;
673 }
674 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700675 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700676 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700677 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700678 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700679 return NULL;
680 }
681
682 private:
683 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
684
685 std::map<std::string, SharedLibrary*> libraries_;
686};
687
Elliott Hughes418d20f2011-09-22 14:00:39 -0700688JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
689 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
690 Object* receiver = Decode<Object*>(env, obj);
691 Method* method = DecodeMethod(mid);
692 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
693 return InvokeWithArgArray(env, receiver, method, arg_array.get());
694}
695
Elliott Hughesd07986f2011-12-06 18:27:45 -0800696JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
697 UniquePtr<byte[]> arg_array(CreateArgArray(m, args));
698 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, arg_array.get());
699}
700
Elliott Hughescdf53122011-08-19 15:46:09 -0700701class JNI {
702 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700703
Elliott Hughescdf53122011-08-19 15:46:09 -0700704 static jint GetVersion(JNIEnv* env) {
705 ScopedJniThreadState ts(env);
706 return JNI_VERSION_1_6;
707 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700708
Elliott Hughescdf53122011-08-19 15:46:09 -0700709 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
710 ScopedJniThreadState ts(env);
711 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700712 return NULL;
713 }
714
Elliott Hughescdf53122011-08-19 15:46:09 -0700715 static jclass FindClass(JNIEnv* env, const char* name) {
716 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700717 Runtime* runtime = Runtime::Current();
718 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700720 Class* c = NULL;
721 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700722 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700723 c = class_linker->FindClass(descriptor, cl);
724 } else {
725 c = class_linker->FindSystemClass(descriptor);
726 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700727 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700728 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700729
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
731 ScopedJniThreadState ts(env);
732 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700733 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700734 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700735
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
737 ScopedJniThreadState ts(env);
738 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700739 return EncodeField(field);
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 ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
743 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700744 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700745 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700746 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700747
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
749 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700750 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700751 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700753
Elliott Hughes37f7a402011-08-22 18:56:01 -0700754 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
755 ScopedJniThreadState ts(env);
756 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700757 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700758 }
759
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700760 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700762 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700763 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700764 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700765
Elliott Hughes37f7a402011-08-22 18:56:01 -0700766 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700768 Class* c1 = Decode<Class*>(ts, java_class1);
769 Class* c2 = Decode<Class*>(ts, java_class2);
770 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700771 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700772
Elliott Hughes37f7a402011-08-22 18:56:01 -0700773 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700775 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700776 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700777 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700778 return JNI_TRUE;
779 } else {
780 Object* obj = Decode<Object*>(ts, jobj);
781 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700782 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700783 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700784 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700785
Elliott Hughes37f7a402011-08-22 18:56:01 -0700786 static jint Throw(JNIEnv* env, jthrowable java_exception) {
787 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700788 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700789 if (exception == NULL) {
790 return JNI_ERR;
791 }
792 ts.Self()->SetException(exception);
793 return JNI_OK;
794 }
795
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700796 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700797 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700798 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700799 jmethodID mid = ((msg != NULL)
800 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
801 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700802 if (mid == NULL) {
803 return JNI_ERR;
804 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700805 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700806 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700807 return JNI_ERR;
808 }
809
810 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700811 args[0].l = s.get();
812 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
813 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700814 return JNI_ERR;
815 }
816
Elliott Hughes72025e52011-08-23 17:50:30 -0700817 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700818
Elliott Hughes37f7a402011-08-22 18:56:01 -0700819 return JNI_OK;
820 }
821
822 static jboolean ExceptionCheck(JNIEnv* env) {
823 ScopedJniThreadState ts(env);
824 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
825 }
826
827 static void ExceptionClear(JNIEnv* env) {
828 ScopedJniThreadState ts(env);
829 ts.Self()->ClearException();
830 }
831
832 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700833 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700834
835 Thread* self = ts.Self();
836 Throwable* original_exception = self->GetException();
837 self->ClearException();
838
Elliott Hughesbf86d042011-08-31 17:53:14 -0700839 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700840 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
841 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
842 if (mid == NULL) {
843 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700844 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700845 } else {
846 env->CallVoidMethod(exception.get(), mid);
847 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700848 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700849 << " thrown while calling printStackTrace";
850 self->ClearException();
851 }
852 }
853
854 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700856
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 static jthrowable ExceptionOccurred(JNIEnv* env) {
858 ScopedJniThreadState ts(env);
859 Object* exception = ts.Self()->GetException();
860 if (exception == NULL) {
861 return NULL;
862 } else {
863 // TODO: if adding a local reference failing causes the VM to abort
864 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700865 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 if (localException == NULL) {
867 // We were unable to add a new local reference, and threw a new
868 // exception. We can't return "exception", because it's not a
869 // local reference. So we have to return NULL, indicating that
870 // there was no exception, even though it's pretty much raining
871 // exceptions in here.
872 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
873 }
874 return localException;
875 }
876 }
877
Elliott Hughescdf53122011-08-19 15:46:09 -0700878 static void FatalError(JNIEnv* env, const char* msg) {
879 ScopedJniThreadState ts(env);
880 LOG(FATAL) << "JNI FatalError called: " << msg;
881 }
882
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700883 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700885 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
886 return JNI_ERR;
887 }
888 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 return JNI_OK;
890 }
891
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700892 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700894 Object* survivor = Decode<Object*>(ts, java_survivor);
895 ts.Env()->PopFrame();
896 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 }
898
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700899 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700900 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700901 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
902 }
903
904 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
905 // TODO: we should try to expand the table if necessary.
906 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
907 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
908 return JNI_ERR;
909 }
910 // TODO: this isn't quite right, since "capacity" includes holes.
911 size_t capacity = ts.Env()->locals.Capacity();
912 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
913 if (!okay) {
914 ts.Self()->ThrowOutOfMemoryError(caller);
915 }
916 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700917 }
918
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
920 ScopedJniThreadState ts(env);
921 if (obj == NULL) {
922 return NULL;
923 }
924
Elliott Hughes75770752011-08-24 17:52:38 -0700925 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700926 IndirectReferenceTable& globals = vm->globals;
927 MutexLock mu(vm->globals_lock);
928 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
929 return reinterpret_cast<jobject>(ref);
930 }
931
932 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
933 ScopedJniThreadState ts(env);
934 if (obj == NULL) {
935 return;
936 }
937
Elliott Hughes75770752011-08-24 17:52:38 -0700938 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 IndirectReferenceTable& globals = vm->globals;
940 MutexLock mu(vm->globals_lock);
941
942 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
943 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
944 << "failed to find entry";
945 }
946 }
947
948 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
949 ScopedJniThreadState ts(env);
950 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
951 }
952
953 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
954 ScopedJniThreadState ts(env);
955 if (obj == NULL) {
956 return;
957 }
958
Elliott Hughes75770752011-08-24 17:52:38 -0700959 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 IndirectReferenceTable& weak_globals = vm->weak_globals;
961 MutexLock mu(vm->weak_globals_lock);
962
963 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
964 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
965 << "failed to find entry";
966 }
967 }
968
969 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
970 ScopedJniThreadState ts(env);
971 if (obj == NULL) {
972 return NULL;
973 }
974
975 IndirectReferenceTable& locals = ts.Env()->locals;
976
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700977 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
979 return reinterpret_cast<jobject>(ref);
980 }
981
982 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
983 ScopedJniThreadState ts(env);
984 if (obj == NULL) {
985 return;
986 }
987
988 IndirectReferenceTable& locals = ts.Env()->locals;
989
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700990 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 if (!locals.Remove(cookie, obj)) {
992 // Attempting to delete a local reference that is not in the
993 // topmost local reference frame is a no-op. DeleteLocalRef returns
994 // void and doesn't throw any exceptions, but we should probably
995 // complain about it so the user will notice that things aren't
996 // going quite the way they expect.
997 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
998 << "failed to find entry";
999 }
1000 }
1001
1002 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
1003 ScopedJniThreadState ts(env);
1004 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
1005 ? JNI_TRUE : JNI_FALSE;
1006 }
1007
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001008 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001010 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001011 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001012 return NULL;
1013 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001014 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 ScopedJniThreadState ts(env);
1019 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 va_start(args, mid);
1021 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 va_end(args);
1023 return result;
1024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001028 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001029 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001030 return NULL;
1031 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001032 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001033 if (result == NULL) {
1034 return NULL;
1035 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001036 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001038 if (!ts.Self()->IsExceptionPending()) {
1039 return local_result;
1040 } else {
1041 return NULL;
1042 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001047 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001048 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001049 return NULL;
1050 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001051 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001052 if (result == NULL) {
1053 return NULL;
1054 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001055 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001057 if (!ts.Self()->IsExceptionPending()) {
1058 return local_result;
1059 } else {
1060 return NULL;
1061 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1065 ScopedJniThreadState ts(env);
1066 return FindMethodID(ts, c, name, sig, false);
1067 }
1068
1069 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1070 ScopedJniThreadState ts(env);
1071 return FindMethodID(ts, c, name, sig, true);
1072 }
1073
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 va_list ap;
1077 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001078 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 va_end(ap);
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 CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001085 JValue result = InvokeVirtualOrInterfaceWithVarArgs(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 jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001091 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001092 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_list ap;
1098 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001099 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 va_end(ap);
1101 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001106 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001111 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 va_list ap;
1117 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001118 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 va_end(ap);
1120 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001125 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001130 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 va_list ap;
1136 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001137 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_end(ap);
1139 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001144 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001149 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_list ap;
1155 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001156 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 va_end(ap);
1158 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001163 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001168 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_list ap;
1174 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001175 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 va_end(ap);
1177 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001182 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001187 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 va_list ap;
1193 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001194 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 va_end(ap);
1196 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001201 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001206 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 }
1208
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 va_list ap;
1212 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001213 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 va_end(ap);
1215 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 }
1217
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001220 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001225 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 va_list ap;
1231 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001232 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 va_end(ap);
1234 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 }
1236
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001239 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
Elliott Hughes72025e52011-08-23 17:50:30 -07001242 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001244 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 }
1246
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 va_list ap;
1250 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001251 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 }
1254
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001257 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001262 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 }
1264
1265 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001266 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 ScopedJniThreadState ts(env);
1268 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001270 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001271 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 va_end(ap);
1273 return local_result;
1274 }
1275
1276 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001279 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001280 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
1283 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001286 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001287 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
1290 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 ScopedJniThreadState ts(env);
1293 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001295 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 va_end(ap);
1297 return result.z;
1298 }
1299
1300 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001303 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
1306 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001309 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
1312 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 ScopedJniThreadState ts(env);
1315 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001317 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 va_end(ap);
1319 return result.b;
1320 }
1321
1322 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001325 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
1328 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001329 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001331 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
1333
1334 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 ScopedJniThreadState ts(env);
1337 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001339 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 va_end(ap);
1341 return result.c;
1342 }
1343
1344 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001347 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
1350 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001351 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001353 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
1356 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001357 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 ScopedJniThreadState ts(env);
1359 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001360 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001361 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 va_end(ap);
1363 return result.s;
1364 }
1365
1366 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001367 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001369 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
1372 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001373 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001375 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
1377
Elliott Hughes418d20f2011-09-22 14:00:39 -07001378 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 ScopedJniThreadState ts(env);
1380 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001381 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001382 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 va_end(ap);
1384 return result.i;
1385 }
1386
1387 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001388 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001390 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
1392
1393 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001394 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001396 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
1399 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001400 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 ScopedJniThreadState ts(env);
1402 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001403 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001404 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 va_end(ap);
1406 return result.j;
1407 }
1408
1409 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001410 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001412 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
1415 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001416 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001418 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
1421 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001422 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 ScopedJniThreadState ts(env);
1424 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001425 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001426 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 va_end(ap);
1428 return result.f;
1429 }
1430
1431 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001432 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001434 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
1436
1437 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001438 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001440 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
1443 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001444 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 ScopedJniThreadState ts(env);
1446 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001447 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001448 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 va_end(ap);
1450 return result.d;
1451 }
1452
1453 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001454 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001456 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
1459 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001460 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001462 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 }
1464
1465 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001466 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 ScopedJniThreadState ts(env);
1468 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001469 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001470 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001471 va_end(ap);
1472 }
1473
1474 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001475 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001477 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
1480 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001481 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001483 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001486 static jfieldID GetFieldID(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, false);
1489 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001490
1491
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001492 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 ScopedJniThreadState ts(env);
1494 return FindFieldID(ts, c, name, sig, true);
1495 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001496
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001497 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 Object* o = Decode<Object*>(ts, obj);
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(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001503
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001506 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001507 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 }
1509
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001511 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 Object* o = Decode<Object*>(ts, java_object);
1513 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001514 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001515 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001518 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001521 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001522 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 }
1524
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001525#define GET_PRIMITIVE_FIELD(fn, instance) \
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 return f->fn(o)
1530
1531#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1532 ScopedJniThreadState ts(env); \
1533 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001534 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 f->fn(o, value)
1536
1537 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1538 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001539 }
1540
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001541 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1542 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1546 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001549 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1550 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001553 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1554 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 }
1556
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001557 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1558 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001561 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1562 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001565 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1566 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001569 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1570 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 }
1572
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001573 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1574 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001577 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1578 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 }
1580
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001581 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1582 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001585 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1586 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001589 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1590 GET_PRIMITIVE_FIELD(GetLong, NULL);
1591 }
1592
1593 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1594 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1595 }
1596
1597 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1598 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1599 }
1600
1601 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1602 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1603 }
1604
1605 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1606 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1607 }
1608
1609 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1610 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1611 }
1612
1613 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1614 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1615 }
1616
1617 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1618 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1619 }
1620
1621 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1622 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1623 }
1624
1625 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1626 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1627 }
1628
1629 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1630 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1631 }
1632
1633 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1634 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1635 }
1636
1637 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1638 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1639 }
1640
1641 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1642 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1643 }
1644
1645 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1646 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1647 }
1648
1649 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1650 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1651 }
1652
1653 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1654 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1655 }
1656
1657 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1658 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1659 }
1660
1661 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1662 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 }
1664
Elliott Hughes418d20f2011-09-22 14:00:39 -07001665 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 ScopedJniThreadState ts(env);
1667 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001669 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001670 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 va_end(ap);
1672 return local_result;
1673 }
1674
Elliott Hughes418d20f2011-09-22 14:00:39 -07001675 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001677 JValue result = InvokeWithVarArgs(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 jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001683 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001684 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 }
1686
Elliott Hughes418d20f2011-09-22 14:00:39 -07001687 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 ScopedJniThreadState ts(env);
1689 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001690 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001691 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 va_end(ap);
1693 return result.z;
1694 }
1695
Elliott Hughes418d20f2011-09-22 14:00:39 -07001696 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001698 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 }
1700
Elliott Hughes418d20f2011-09-22 14:00:39 -07001701 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001703 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 }
1705
Elliott Hughes72025e52011-08-23 17:50:30 -07001706 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 ScopedJniThreadState ts(env);
1708 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001709 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001710 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 va_end(ap);
1712 return result.b;
1713 }
1714
Elliott Hughes418d20f2011-09-22 14:00:39 -07001715 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001717 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes418d20f2011-09-22 14:00:39 -07001720 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001722 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
Elliott Hughes72025e52011-08-23 17:50:30 -07001725 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 ScopedJniThreadState ts(env);
1727 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001728 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001729 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 va_end(ap);
1731 return result.c;
1732 }
1733
Elliott Hughes418d20f2011-09-22 14:00:39 -07001734 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001736 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes418d20f2011-09-22 14:00:39 -07001739 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001741 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 }
1743
Elliott Hughes72025e52011-08-23 17:50:30 -07001744 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 ScopedJniThreadState ts(env);
1746 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001748 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 va_end(ap);
1750 return result.s;
1751 }
1752
Elliott Hughes418d20f2011-09-22 14:00:39 -07001753 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001755 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 }
1757
Elliott Hughes418d20f2011-09-22 14:00:39 -07001758 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001760 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes72025e52011-08-23 17:50:30 -07001763 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 ScopedJniThreadState ts(env);
1765 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001766 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001767 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 va_end(ap);
1769 return result.i;
1770 }
1771
Elliott Hughes418d20f2011-09-22 14:00:39 -07001772 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001774 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes418d20f2011-09-22 14:00:39 -07001777 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001779 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 }
1781
Elliott Hughes72025e52011-08-23 17:50:30 -07001782 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 ScopedJniThreadState ts(env);
1784 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001785 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001786 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 va_end(ap);
1788 return result.j;
1789 }
1790
Elliott Hughes418d20f2011-09-22 14:00:39 -07001791 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001793 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes418d20f2011-09-22 14:00:39 -07001796 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001798 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
Elliott Hughes72025e52011-08-23 17:50:30 -07001801 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 ScopedJniThreadState ts(env);
1803 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001804 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001805 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 va_end(ap);
1807 return result.f;
1808 }
1809
Elliott Hughes418d20f2011-09-22 14:00:39 -07001810 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001812 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes418d20f2011-09-22 14:00:39 -07001815 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001817 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 }
1819
Elliott Hughes72025e52011-08-23 17:50:30 -07001820 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 ScopedJniThreadState ts(env);
1822 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001823 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001824 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 va_end(ap);
1826 return result.d;
1827 }
1828
Elliott Hughes418d20f2011-09-22 14:00:39 -07001829 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001831 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
Elliott Hughes418d20f2011-09-22 14:00:39 -07001834 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001836 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
Elliott Hughes72025e52011-08-23 17:50:30 -07001839 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 ScopedJniThreadState ts(env);
1841 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001842 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001843 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 va_end(ap);
1845 }
1846
Elliott Hughes418d20f2011-09-22 14:00:39 -07001847 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001849 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 }
1851
Elliott Hughes418d20f2011-09-22 14:00:39 -07001852 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001854 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 }
1856
Elliott Hughes814e4032011-08-23 12:07:56 -07001857 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001859 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001860 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 }
1862
1863 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1864 ScopedJniThreadState ts(env);
1865 if (utf == NULL) {
1866 return NULL;
1867 }
1868 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001869 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 }
1871
Elliott Hughes814e4032011-08-23 12:07:56 -07001872 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1873 ScopedJniThreadState ts(env);
1874 return Decode<String*>(ts, java_string)->GetLength();
1875 }
1876
1877 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1878 ScopedJniThreadState ts(env);
1879 return Decode<String*>(ts, java_string)->GetUtfLength();
1880 }
1881
Elliott Hughesb465ab02011-08-24 11:21:21 -07001882 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001884 String* s = Decode<String*>(ts, java_string);
1885 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1886 ThrowSIOOBE(ts, start, length, s->GetLength());
1887 } else {
1888 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1889 memcpy(buf, chars + start, length * sizeof(jchar));
1890 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001891 }
1892
Elliott Hughesb465ab02011-08-24 11:21:21 -07001893 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001894 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001895 String* s = Decode<String*>(ts, java_string);
1896 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1897 ThrowSIOOBE(ts, start, length, s->GetLength());
1898 } else {
1899 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1900 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1901 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001902 }
1903
Elliott Hughes75770752011-08-24 17:52:38 -07001904 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001905 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001906 String* s = Decode<String*>(ts, java_string);
1907 const CharArray* chars = s->GetCharArray();
1908 PinPrimitiveArray(ts, chars);
1909 if (is_copy != NULL) {
1910 *is_copy = JNI_FALSE;
1911 }
1912 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001913 }
1914
Elliott Hughes75770752011-08-24 17:52:38 -07001915 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001916 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001917 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 }
1919
Elliott Hughes75770752011-08-24 17:52:38 -07001920 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001922 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 }
1924
Elliott Hughes75770752011-08-24 17:52:38 -07001925 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001927 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 }
1929
Elliott Hughes75770752011-08-24 17:52:38 -07001930 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001931 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001932 if (java_string == NULL) {
1933 return NULL;
1934 }
1935 if (is_copy != NULL) {
1936 *is_copy = JNI_TRUE;
1937 }
1938 String* s = Decode<String*>(ts, java_string);
1939 size_t byte_count = s->GetUtfLength();
1940 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001941 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001942 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1943 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1944 bytes[byte_count] = '\0';
1945 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001946 }
1947
Elliott Hughes75770752011-08-24 17:52:38 -07001948 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001949 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001950 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001951 }
1952
Elliott Hughesbd935992011-08-22 11:59:34 -07001953 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001955 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001956 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001957 Array* array = obj->AsArray();
1958 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 }
1960
Elliott Hughes814e4032011-08-23 12:07:56 -07001961 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001962 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001963 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001964 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 }
1966
1967 static void SetObjectArrayElement(JNIEnv* env,
1968 jobjectArray java_array, jsize index, jobject java_value) {
1969 ScopedJniThreadState ts(env);
1970 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1971 Object* value = Decode<Object*>(ts, java_value);
1972 array->Set(index, value);
1973 }
1974
1975 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1976 ScopedJniThreadState ts(env);
1977 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1978 }
1979
1980 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1981 ScopedJniThreadState ts(env);
1982 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1983 }
1984
1985 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1986 ScopedJniThreadState ts(env);
1987 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1988 }
1989
1990 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1991 ScopedJniThreadState ts(env);
1992 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1993 }
1994
1995 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1996 ScopedJniThreadState ts(env);
1997 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1998 }
1999
2000 static jintArray NewIntArray(JNIEnv* env, jsize length) {
2001 ScopedJniThreadState ts(env);
2002 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
2003 }
2004
2005 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2006 ScopedJniThreadState ts(env);
2007 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
2008 }
2009
2010 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
2011 ScopedJniThreadState ts(env);
2012 CHECK_GE(length, 0); // TODO: ReportJniError
2013
2014 // Compute the array class corresponding to the given element class.
2015 Class* element_class = Decode<Class*>(ts, element_jclass);
2016 std::string descriptor;
2017 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002018 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002019
2020 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002021 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2022 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 return NULL;
2024 }
2025
Elliott Hughes75770752011-08-24 17:52:38 -07002026 // Allocate and initialize if necessary.
2027 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002029 if (initial_element != NULL) {
2030 Object* initial_object = Decode<Object*>(ts, initial_element);
2031 for (jsize i = 0; i < length; ++i) {
2032 result->Set(i, initial_object);
2033 }
2034 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002035 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
2038 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2039 ScopedJniThreadState ts(env);
2040 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2041 }
2042
Elliott Hughes75770752011-08-24 17:52:38 -07002043 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002045 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002046 }
2047
Elliott Hughes75770752011-08-24 17:52:38 -07002048 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002050 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002051 }
2052
Elliott Hughes75770752011-08-24 17:52:38 -07002053 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002055 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes75770752011-08-24 17:52:38 -07002058 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002060 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes75770752011-08-24 17:52:38 -07002063 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002065 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes75770752011-08-24 17:52:38 -07002068 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002070 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes75770752011-08-24 17:52:38 -07002073 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002075 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes75770752011-08-24 17:52:38 -07002078 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002080 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes75770752011-08-24 17:52:38 -07002083 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002085 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes75770752011-08-24 17:52:38 -07002088 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002090 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes75770752011-08-24 17:52:38 -07002093 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002095 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes75770752011-08-24 17:52:38 -07002098 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002100 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes75770752011-08-24 17:52:38 -07002103 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002105 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes75770752011-08-24 17:52:38 -07002108 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002110 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes75770752011-08-24 17:52:38 -07002113 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002115 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes75770752011-08-24 17:52:38 -07002118 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002120 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes75770752011-08-24 17:52:38 -07002123 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002125 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes75770752011-08-24 17:52:38 -07002128 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002130 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes814e4032011-08-23 12:07:56 -07002138 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Elliott Hughes814e4032011-08-23 12:07:56 -07002143 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes814e4032011-08-23 12:07:56 -07002148 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Elliott Hughes814e4032011-08-23 12:07:56 -07002153 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002155 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Elliott Hughes814e4032011-08-23 12:07:56 -07002158 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002160 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Elliott Hughes814e4032011-08-23 12:07:56 -07002163 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002165 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 }
2167
Elliott Hughes814e4032011-08-23 12:07:56 -07002168 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002170 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Elliott Hughes814e4032011-08-23 12:07:56 -07002173 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002175 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes814e4032011-08-23 12:07:56 -07002178 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002180 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
Elliott Hughes814e4032011-08-23 12:07:56 -07002183 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002185 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
Elliott Hughes814e4032011-08-23 12:07:56 -07002188 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002190 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Elliott Hughes814e4032011-08-23 12:07:56 -07002193 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002195 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Elliott Hughes814e4032011-08-23 12:07:56 -07002198 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002200 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
Elliott Hughes814e4032011-08-23 12:07:56 -07002203 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002205 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
Elliott Hughes814e4032011-08-23 12:07:56 -07002208 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002210 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
Elliott Hughes5174fe62011-08-23 15:12:35 -07002213 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002215 Class* c = Decode<Class*>(ts, java_class);
2216
Elliott Hughes5174fe62011-08-23 15:12:35 -07002217 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 const char* name = methods[i].name;
2219 const char* sig = methods[i].signature;
2220
2221 if (*sig == '!') {
2222 // TODO: fast jni. it's too noisy to log all these.
2223 ++sig;
2224 }
2225
Elliott Hughes5174fe62011-08-23 15:12:35 -07002226 Method* m = c->FindDirectMethod(name, sig);
2227 if (m == NULL) {
2228 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002230 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002231 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002232 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002234 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002235 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002236 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 return JNI_ERR;
2238 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002239
Elliott Hughes75770752011-08-24 17:52:38 -07002240 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002241 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002242 }
2243
2244 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246 return JNI_OK;
2247 }
2248
Elliott Hughes5174fe62011-08-23 15:12:35 -07002249 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002251 Class* c = Decode<Class*>(ts, java_class);
2252
Elliott Hughes75770752011-08-24 17:52:38 -07002253 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002254 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002255 }
2256
2257 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2258 Method* m = c->GetDirectMethod(i);
2259 if (m->IsNative()) {
2260 m->UnregisterNative();
2261 }
2262 }
2263 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2264 Method* m = c->GetVirtualMethod(i);
2265 if (m->IsNative()) {
2266 m->UnregisterNative();
2267 }
2268 }
2269
2270 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 }
2272
Elliott Hughes72025e52011-08-23 17:50:30 -07002273 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002274 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002275 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002276 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 }
2278
Elliott Hughes72025e52011-08-23 17:50:30 -07002279 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002281 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002282 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002283 }
2284
2285 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2286 ScopedJniThreadState ts(env);
2287 Runtime* runtime = Runtime::Current();
2288 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002289 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 } else {
2291 *vm = NULL;
2292 }
2293 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2294 }
2295
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2297 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002298
2299 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002300 CHECK(address != NULL); // TODO: ReportJniError
2301 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002302
2303 jclass buffer_class = GetDirectByteBufferClass(env);
2304 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2305 if (mid == NULL) {
2306 return NULL;
2307 }
2308
2309 // At the moment, the Java side is limited to 32 bits.
2310 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2311 CHECK_LE(capacity, 0xffffffff);
2312 jint address_arg = reinterpret_cast<jint>(address);
2313 jint capacity_arg = static_cast<jint>(capacity);
2314
2315 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2316 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002317 }
2318
Elliott Hughesb465ab02011-08-24 11:21:21 -07002319 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002320 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002321 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2322 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002323 }
2324
Elliott Hughesb465ab02011-08-24 11:21:21 -07002325 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002326 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002327 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2328 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002329 }
2330
Elliott Hughesb465ab02011-08-24 11:21:21 -07002331 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002332 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002333
Elliott Hughes75770752011-08-24 17:52:38 -07002334 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002335
2336 // Do we definitely know what kind of reference this is?
2337 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2338 IndirectRefKind kind = GetIndirectRefKind(ref);
2339 switch (kind) {
2340 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002341 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2342 return JNILocalRefType;
2343 }
2344 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002345 case kGlobal:
2346 return JNIGlobalRefType;
2347 case kWeakGlobal:
2348 return JNIWeakGlobalRefType;
2349 case kSirtOrInvalid:
2350 // Is it in a stack IRT?
2351 if (ts.Self()->SirtContains(java_object)) {
2352 return JNILocalRefType;
2353 }
2354
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002355 if (!ts.Env()->work_around_app_jni_bugs) {
2356 return JNIInvalidRefType;
2357 }
2358
Elliott Hughesb465ab02011-08-24 11:21:21 -07002359 // If we're handing out direct pointers, check whether it's a direct pointer
2360 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002361 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002362 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002363 return JNILocalRefType;
2364 }
2365 }
2366
2367 return JNIInvalidRefType;
2368 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002369 }
2370};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002371
Elliott Hughesa2501992011-08-26 19:39:54 -07002372const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002373 NULL, // reserved0.
2374 NULL, // reserved1.
2375 NULL, // reserved2.
2376 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002377 JNI::GetVersion,
2378 JNI::DefineClass,
2379 JNI::FindClass,
2380 JNI::FromReflectedMethod,
2381 JNI::FromReflectedField,
2382 JNI::ToReflectedMethod,
2383 JNI::GetSuperclass,
2384 JNI::IsAssignableFrom,
2385 JNI::ToReflectedField,
2386 JNI::Throw,
2387 JNI::ThrowNew,
2388 JNI::ExceptionOccurred,
2389 JNI::ExceptionDescribe,
2390 JNI::ExceptionClear,
2391 JNI::FatalError,
2392 JNI::PushLocalFrame,
2393 JNI::PopLocalFrame,
2394 JNI::NewGlobalRef,
2395 JNI::DeleteGlobalRef,
2396 JNI::DeleteLocalRef,
2397 JNI::IsSameObject,
2398 JNI::NewLocalRef,
2399 JNI::EnsureLocalCapacity,
2400 JNI::AllocObject,
2401 JNI::NewObject,
2402 JNI::NewObjectV,
2403 JNI::NewObjectA,
2404 JNI::GetObjectClass,
2405 JNI::IsInstanceOf,
2406 JNI::GetMethodID,
2407 JNI::CallObjectMethod,
2408 JNI::CallObjectMethodV,
2409 JNI::CallObjectMethodA,
2410 JNI::CallBooleanMethod,
2411 JNI::CallBooleanMethodV,
2412 JNI::CallBooleanMethodA,
2413 JNI::CallByteMethod,
2414 JNI::CallByteMethodV,
2415 JNI::CallByteMethodA,
2416 JNI::CallCharMethod,
2417 JNI::CallCharMethodV,
2418 JNI::CallCharMethodA,
2419 JNI::CallShortMethod,
2420 JNI::CallShortMethodV,
2421 JNI::CallShortMethodA,
2422 JNI::CallIntMethod,
2423 JNI::CallIntMethodV,
2424 JNI::CallIntMethodA,
2425 JNI::CallLongMethod,
2426 JNI::CallLongMethodV,
2427 JNI::CallLongMethodA,
2428 JNI::CallFloatMethod,
2429 JNI::CallFloatMethodV,
2430 JNI::CallFloatMethodA,
2431 JNI::CallDoubleMethod,
2432 JNI::CallDoubleMethodV,
2433 JNI::CallDoubleMethodA,
2434 JNI::CallVoidMethod,
2435 JNI::CallVoidMethodV,
2436 JNI::CallVoidMethodA,
2437 JNI::CallNonvirtualObjectMethod,
2438 JNI::CallNonvirtualObjectMethodV,
2439 JNI::CallNonvirtualObjectMethodA,
2440 JNI::CallNonvirtualBooleanMethod,
2441 JNI::CallNonvirtualBooleanMethodV,
2442 JNI::CallNonvirtualBooleanMethodA,
2443 JNI::CallNonvirtualByteMethod,
2444 JNI::CallNonvirtualByteMethodV,
2445 JNI::CallNonvirtualByteMethodA,
2446 JNI::CallNonvirtualCharMethod,
2447 JNI::CallNonvirtualCharMethodV,
2448 JNI::CallNonvirtualCharMethodA,
2449 JNI::CallNonvirtualShortMethod,
2450 JNI::CallNonvirtualShortMethodV,
2451 JNI::CallNonvirtualShortMethodA,
2452 JNI::CallNonvirtualIntMethod,
2453 JNI::CallNonvirtualIntMethodV,
2454 JNI::CallNonvirtualIntMethodA,
2455 JNI::CallNonvirtualLongMethod,
2456 JNI::CallNonvirtualLongMethodV,
2457 JNI::CallNonvirtualLongMethodA,
2458 JNI::CallNonvirtualFloatMethod,
2459 JNI::CallNonvirtualFloatMethodV,
2460 JNI::CallNonvirtualFloatMethodA,
2461 JNI::CallNonvirtualDoubleMethod,
2462 JNI::CallNonvirtualDoubleMethodV,
2463 JNI::CallNonvirtualDoubleMethodA,
2464 JNI::CallNonvirtualVoidMethod,
2465 JNI::CallNonvirtualVoidMethodV,
2466 JNI::CallNonvirtualVoidMethodA,
2467 JNI::GetFieldID,
2468 JNI::GetObjectField,
2469 JNI::GetBooleanField,
2470 JNI::GetByteField,
2471 JNI::GetCharField,
2472 JNI::GetShortField,
2473 JNI::GetIntField,
2474 JNI::GetLongField,
2475 JNI::GetFloatField,
2476 JNI::GetDoubleField,
2477 JNI::SetObjectField,
2478 JNI::SetBooleanField,
2479 JNI::SetByteField,
2480 JNI::SetCharField,
2481 JNI::SetShortField,
2482 JNI::SetIntField,
2483 JNI::SetLongField,
2484 JNI::SetFloatField,
2485 JNI::SetDoubleField,
2486 JNI::GetStaticMethodID,
2487 JNI::CallStaticObjectMethod,
2488 JNI::CallStaticObjectMethodV,
2489 JNI::CallStaticObjectMethodA,
2490 JNI::CallStaticBooleanMethod,
2491 JNI::CallStaticBooleanMethodV,
2492 JNI::CallStaticBooleanMethodA,
2493 JNI::CallStaticByteMethod,
2494 JNI::CallStaticByteMethodV,
2495 JNI::CallStaticByteMethodA,
2496 JNI::CallStaticCharMethod,
2497 JNI::CallStaticCharMethodV,
2498 JNI::CallStaticCharMethodA,
2499 JNI::CallStaticShortMethod,
2500 JNI::CallStaticShortMethodV,
2501 JNI::CallStaticShortMethodA,
2502 JNI::CallStaticIntMethod,
2503 JNI::CallStaticIntMethodV,
2504 JNI::CallStaticIntMethodA,
2505 JNI::CallStaticLongMethod,
2506 JNI::CallStaticLongMethodV,
2507 JNI::CallStaticLongMethodA,
2508 JNI::CallStaticFloatMethod,
2509 JNI::CallStaticFloatMethodV,
2510 JNI::CallStaticFloatMethodA,
2511 JNI::CallStaticDoubleMethod,
2512 JNI::CallStaticDoubleMethodV,
2513 JNI::CallStaticDoubleMethodA,
2514 JNI::CallStaticVoidMethod,
2515 JNI::CallStaticVoidMethodV,
2516 JNI::CallStaticVoidMethodA,
2517 JNI::GetStaticFieldID,
2518 JNI::GetStaticObjectField,
2519 JNI::GetStaticBooleanField,
2520 JNI::GetStaticByteField,
2521 JNI::GetStaticCharField,
2522 JNI::GetStaticShortField,
2523 JNI::GetStaticIntField,
2524 JNI::GetStaticLongField,
2525 JNI::GetStaticFloatField,
2526 JNI::GetStaticDoubleField,
2527 JNI::SetStaticObjectField,
2528 JNI::SetStaticBooleanField,
2529 JNI::SetStaticByteField,
2530 JNI::SetStaticCharField,
2531 JNI::SetStaticShortField,
2532 JNI::SetStaticIntField,
2533 JNI::SetStaticLongField,
2534 JNI::SetStaticFloatField,
2535 JNI::SetStaticDoubleField,
2536 JNI::NewString,
2537 JNI::GetStringLength,
2538 JNI::GetStringChars,
2539 JNI::ReleaseStringChars,
2540 JNI::NewStringUTF,
2541 JNI::GetStringUTFLength,
2542 JNI::GetStringUTFChars,
2543 JNI::ReleaseStringUTFChars,
2544 JNI::GetArrayLength,
2545 JNI::NewObjectArray,
2546 JNI::GetObjectArrayElement,
2547 JNI::SetObjectArrayElement,
2548 JNI::NewBooleanArray,
2549 JNI::NewByteArray,
2550 JNI::NewCharArray,
2551 JNI::NewShortArray,
2552 JNI::NewIntArray,
2553 JNI::NewLongArray,
2554 JNI::NewFloatArray,
2555 JNI::NewDoubleArray,
2556 JNI::GetBooleanArrayElements,
2557 JNI::GetByteArrayElements,
2558 JNI::GetCharArrayElements,
2559 JNI::GetShortArrayElements,
2560 JNI::GetIntArrayElements,
2561 JNI::GetLongArrayElements,
2562 JNI::GetFloatArrayElements,
2563 JNI::GetDoubleArrayElements,
2564 JNI::ReleaseBooleanArrayElements,
2565 JNI::ReleaseByteArrayElements,
2566 JNI::ReleaseCharArrayElements,
2567 JNI::ReleaseShortArrayElements,
2568 JNI::ReleaseIntArrayElements,
2569 JNI::ReleaseLongArrayElements,
2570 JNI::ReleaseFloatArrayElements,
2571 JNI::ReleaseDoubleArrayElements,
2572 JNI::GetBooleanArrayRegion,
2573 JNI::GetByteArrayRegion,
2574 JNI::GetCharArrayRegion,
2575 JNI::GetShortArrayRegion,
2576 JNI::GetIntArrayRegion,
2577 JNI::GetLongArrayRegion,
2578 JNI::GetFloatArrayRegion,
2579 JNI::GetDoubleArrayRegion,
2580 JNI::SetBooleanArrayRegion,
2581 JNI::SetByteArrayRegion,
2582 JNI::SetCharArrayRegion,
2583 JNI::SetShortArrayRegion,
2584 JNI::SetIntArrayRegion,
2585 JNI::SetLongArrayRegion,
2586 JNI::SetFloatArrayRegion,
2587 JNI::SetDoubleArrayRegion,
2588 JNI::RegisterNatives,
2589 JNI::UnregisterNatives,
2590 JNI::MonitorEnter,
2591 JNI::MonitorExit,
2592 JNI::GetJavaVM,
2593 JNI::GetStringRegion,
2594 JNI::GetStringUTFRegion,
2595 JNI::GetPrimitiveArrayCritical,
2596 JNI::ReleasePrimitiveArrayCritical,
2597 JNI::GetStringCritical,
2598 JNI::ReleaseStringCritical,
2599 JNI::NewWeakGlobalRef,
2600 JNI::DeleteWeakGlobalRef,
2601 JNI::ExceptionCheck,
2602 JNI::NewDirectByteBuffer,
2603 JNI::GetDirectBufferAddress,
2604 JNI::GetDirectBufferCapacity,
2605 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002606};
2607
Elliott Hughes75770752011-08-24 17:52:38 -07002608JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002609 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002610 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002611 local_ref_cookie(IRT_FIRST_SEGMENT),
2612 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002613 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002614 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002615 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002616 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002617 functions = unchecked_functions = &gNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002618 if (vm->check_jni) {
2619 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002620 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002621 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2622 // errors will ensue.
2623 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2624 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002625}
2626
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002627JNIEnvExt::~JNIEnvExt() {
2628}
2629
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002630void JNIEnvExt::EnableCheckJni() {
2631 check_jni = true;
2632 functions = GetCheckJniNativeInterface();
2633}
2634
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002635void JNIEnvExt::DumpReferenceTables() {
2636 locals.Dump();
2637 monitors.Dump();
2638}
2639
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002640void JNIEnvExt::PushFrame(int capacity) {
2641 stacked_local_ref_cookies.push_back(local_ref_cookie);
2642 local_ref_cookie = locals.GetSegmentState();
2643}
2644
2645void JNIEnvExt::PopFrame() {
2646 locals.SetSegmentState(local_ref_cookie);
2647 local_ref_cookie = stacked_local_ref_cookies.back();
2648 stacked_local_ref_cookies.pop_back();
2649}
2650
Carl Shapiroea4dca82011-08-01 13:45:38 -07002651// JNI Invocation interface.
2652
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002653extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2654 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2655 if (args->version < JNI_VERSION_1_2) {
2656 return JNI_EVERSION;
2657 }
2658 Runtime::Options options;
2659 for (int i = 0; i < args->nOptions; ++i) {
2660 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002661 options.push_back(std::make_pair(StringPiece(option->optionString),
2662 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002663 }
2664 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002665 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002666 if (runtime == NULL) {
2667 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002668 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002669 runtime->Start();
2670 *p_env = Thread::Current()->GetJniEnv();
2671 *p_vm = runtime->GetJavaVM();
2672 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002673}
2674
Elliott Hughesf2682d52011-08-15 16:37:04 -07002675extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002676 Runtime* runtime = Runtime::Current();
2677 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002678 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002679 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002680 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002681 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002682 }
2683 return JNI_OK;
2684}
2685
2686// Historically unsupported.
2687extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2688 return JNI_ERR;
2689}
2690
Elliott Hughescdf53122011-08-19 15:46:09 -07002691class JII {
2692 public:
2693 static jint DestroyJavaVM(JavaVM* vm) {
2694 if (vm == NULL) {
2695 return JNI_ERR;
2696 } else {
2697 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2698 delete raw_vm->runtime;
2699 return JNI_OK;
2700 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002701 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002702
Elliott Hughescdf53122011-08-19 15:46:09 -07002703 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002704 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002705 }
2706
2707 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002708 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002709 }
2710
2711 static jint DetachCurrentThread(JavaVM* vm) {
2712 if (vm == NULL) {
2713 return JNI_ERR;
2714 } else {
2715 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2716 Runtime* runtime = raw_vm->runtime;
2717 runtime->DetachCurrentThread();
2718 return JNI_OK;
2719 }
2720 }
2721
2722 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2723 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2724 return JNI_EVERSION;
2725 }
2726 if (vm == NULL || env == NULL) {
2727 return JNI_ERR;
2728 }
2729 Thread* thread = Thread::Current();
2730 if (thread == NULL) {
2731 *env = NULL;
2732 return JNI_EDETACHED;
2733 }
2734 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002735 return JNI_OK;
2736 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002737};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002738
Elliott Hughesa2501992011-08-26 19:39:54 -07002739const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002740 NULL, // reserved0
2741 NULL, // reserved1
2742 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002743 JII::DestroyJavaVM,
2744 JII::AttachCurrentThread,
2745 JII::DetachCurrentThread,
2746 JII::GetEnv,
2747 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002748};
2749
Elliott Hughesa0957642011-09-02 14:27:33 -07002750JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002751 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002752 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002753 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002754 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002755 verbose_jni(options->IsVerbose("jni")),
2756 log_third_party_jni(options->IsVerbose("third-party-jni")),
2757 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002758 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002759 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002760 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002761 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002762 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002763 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002764 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002765 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002766 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002767 functions = unchecked_functions = &gInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002768 if (options->check_jni_) {
2769 EnableCheckJni();
Elliott Hughesa2501992011-08-26 19:39:54 -07002770 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002771}
2772
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002773JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002774 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002775}
2776
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002777void JavaVMExt::EnableCheckJni() {
2778 check_jni = true;
2779 functions = GetCheckJniInvokeInterface();
2780}
2781
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002782void JavaVMExt::DumpReferenceTables() {
2783 {
2784 MutexLock mu(globals_lock);
2785 globals.Dump();
2786 }
2787 {
2788 MutexLock mu(weak_globals_lock);
2789 weak_globals.Dump();
2790 }
2791 {
2792 MutexLock mu(pins_lock);
2793 pin_table.Dump();
2794 }
2795}
2796
Elliott Hughes75770752011-08-24 17:52:38 -07002797bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2798 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002799
2800 // See if we've already loaded this library. If we have, and the class loader
2801 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002802 // TODO: for better results we should canonicalize the pathname (or even compare
2803 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002804 SharedLibrary* library;
2805 {
2806 // TODO: move the locking (and more of this logic) into Libraries.
2807 MutexLock mu(libraries_lock);
2808 library = libraries->Get(path);
2809 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002810 if (library != NULL) {
2811 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002812 // The library will be associated with class_loader. The JNI
2813 // spec says we can't load the same library into more than one
2814 // class loader.
2815 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2816 "ClassLoader %p; can't open in ClassLoader %p",
2817 path.c_str(), library->GetClassLoader(), class_loader);
2818 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002819 return false;
2820 }
2821 if (verbose_jni) {
2822 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2823 << "ClassLoader " << class_loader << "]";
2824 }
2825 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002826 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2827 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002828 return false;
2829 }
2830 return true;
2831 }
2832
2833 // Open the shared library. Because we're using a full path, the system
2834 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2835 // resolve this library's dependencies though.)
2836
2837 // Failures here are expected when java.library.path has several entries
2838 // and we have to hunt for the lib.
2839
2840 // The current version of the dynamic linker prints detailed information
2841 // about dlopen() failures. Some things to check if the message is
2842 // cryptic:
2843 // - make sure the library exists on the device
2844 // - verify that the right path is being opened (the debug log message
2845 // above can help with that)
2846 // - check to see if the library is valid (e.g. not zero bytes long)
2847 // - check config/prelink-linux-arm.map to ensure that the library
2848 // is listed and is not being overrun by the previous entry (if
2849 // loading suddenly stops working on a prelinked library, this is
2850 // a good one to check)
2851 // - write a trivial app that calls sleep() then dlopen(), attach
2852 // to it with "strace -p <pid>" while it sleeps, and watch for
2853 // attempts to open nonexistent dependent shared libs
2854
2855 // TODO: automate some of these checks!
2856
2857 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002858 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002859 // the GC to ignore us.
2860 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002861 void* handle = NULL;
2862 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002863 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002864 handle = dlopen(path.c_str(), RTLD_LAZY);
2865 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002866
2867 if (verbose_jni) {
2868 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2869 }
2870
2871 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002872 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002873 return false;
2874 }
2875
2876 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002877 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002878 // TODO: move the locking (and more of this logic) into Libraries.
2879 MutexLock mu(libraries_lock);
2880 library = libraries->Get(path);
2881 if (library != NULL) {
2882 LOG(INFO) << "WOW: we lost a race to add shared library: "
2883 << "\"" << path << "\" ClassLoader=" << class_loader;
2884 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002885 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002886 library = new SharedLibrary(path, handle, class_loader);
2887 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002888 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002889
2890 if (verbose_jni) {
2891 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2892 }
2893
2894 bool result = true;
2895 void* sym = dlsym(handle, "JNI_OnLoad");
2896 if (sym == NULL) {
2897 if (verbose_jni) {
2898 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2899 }
2900 } else {
2901 // Call JNI_OnLoad. We have to override the current class
2902 // loader, which will always be "null" since the stuff at the
2903 // top of the stack is around Runtime.loadLibrary(). (See
2904 // the comments in the JNI FindClass function.)
2905 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2906 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002907 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002908 self->SetClassLoaderOverride(class_loader);
2909
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002910 int version = 0;
2911 {
2912 ScopedThreadStateChange tsc(self, Thread::kNative);
2913 if (verbose_jni) {
2914 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2915 }
2916 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002917 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002918
Brian Carlstromaded5f72011-10-07 17:15:04 -07002919 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002920
2921 if (version != JNI_VERSION_1_2 &&
2922 version != JNI_VERSION_1_4 &&
2923 version != JNI_VERSION_1_6) {
2924 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2925 << "bad version: " << version;
2926 // It's unwise to call dlclose() here, but we can mark it
2927 // as bad and ensure that future load attempts will fail.
2928 // We don't know how far JNI_OnLoad got, so there could
2929 // be some partially-initialized stuff accessible through
2930 // newly-registered native method calls. We could try to
2931 // unregister them, but that doesn't seem worthwhile.
2932 result = false;
2933 } else {
2934 if (verbose_jni) {
2935 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2936 << " from JNI_OnLoad in \"" << path << "\"]";
2937 }
2938 }
2939 }
2940
2941 library->SetResult(result);
2942 return result;
2943}
2944
2945void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2946 CHECK(m->IsNative());
2947
2948 Class* c = m->GetDeclaringClass();
2949
2950 // If this is a static method, it could be called before the class
2951 // has been initialized.
2952 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002953 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002954 return NULL;
2955 }
2956 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002957 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002958 }
2959
Brian Carlstrom16192862011-09-12 17:50:06 -07002960 std::string detail;
2961 void* native_method;
2962 {
2963 MutexLock mu(libraries_lock);
2964 native_method = libraries->FindNativeMethod(m, detail);
2965 }
2966 // throwing can cause libraries_lock to be reacquired
2967 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002968 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002969 }
2970 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002971}
2972
Elliott Hughes410c0c82011-09-01 17:58:25 -07002973void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2974 {
2975 MutexLock mu(globals_lock);
2976 globals.VisitRoots(visitor, arg);
2977 }
2978 {
2979 MutexLock mu(pins_lock);
2980 pin_table.VisitRoots(visitor, arg);
2981 }
2982 // The weak_globals table is visited by the GC itself (because it mutates the table).
2983}
2984
Ian Rogersdf20fe02011-07-20 20:34:16 -07002985} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002986
2987std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2988 switch (rhs) {
2989 case JNIInvalidRefType:
2990 os << "JNIInvalidRefType";
2991 return os;
2992 case JNILocalRefType:
2993 os << "JNILocalRefType";
2994 return os;
2995 case JNIGlobalRefType:
2996 os << "JNIGlobalRefType";
2997 return os;
2998 case JNIWeakGlobalRefType:
2999 os << "JNIWeakGlobalRefType";
3000 return os;
3001 }
3002}