blob: d01d841979892e5f636fc956929c3376d442e446 [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070015#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070016#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070018#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070021#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070022#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070023#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070024#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070026
27namespace art {
28
Elliott Hughesc5f7c912011-08-18 14:00:42 -070029/*
30 * Add a local reference for an object to the current stack frame. When
31 * the native function returns, the reference will be discarded.
32 *
33 * We need to allow the same reference to be added multiple times.
34 *
35 * This will be called on otherwise unreferenced objects. We cannot do
36 * GC allocations here, and it's best if we don't grab a mutex.
37 *
38 * Returns the local reference (currently just the same pointer that was
39 * passed in), or NULL on failure.
40 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070041template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070042T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
43 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
44 Object* obj = const_cast<Object*>(const_obj);
45
Elliott Hughesc5f7c912011-08-18 14:00:42 -070046 if (obj == NULL) {
47 return NULL;
48 }
49
Elliott Hughesbf86d042011-08-31 17:53:14 -070050 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
51 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070052
Ian Rogers5a7a74a2011-09-26 16:32:29 -070053 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070054 IndirectRef ref = locals.Add(cookie, obj);
55 if (ref == NULL) {
56 // TODO: just change Add's DCHECK to CHECK and lose this?
57 locals.Dump();
58 LOG(FATAL) << "Failed adding to JNI local reference table "
59 << "(has " << locals.Capacity() << " entries)";
60 // TODO: dvmDumpThread(dvmThreadSelf(), false);
61 }
62
63#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070064 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070065 size_t entry_count = locals.Capacity();
66 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070067 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070068 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070069 locals.Dump();
70 // TODO: dvmDumpThread(dvmThreadSelf(), false);
71 // dvmAbort();
72 }
73 }
74#endif
75
Elliott Hughesbf86d042011-08-31 17:53:14 -070076 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070077 // Hand out direct pointers to support broken old apps.
78 return reinterpret_cast<T>(obj);
79 }
80
81 return reinterpret_cast<T>(ref);
82}
83
Elliott Hughesbf86d042011-08-31 17:53:14 -070084// For external use.
85template<typename T>
86T Decode(JNIEnv* public_env, jobject obj) {
87 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
88 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
89}
90// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -070091template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070092template Class* Decode<Class*>(JNIEnv*, jobject);
93template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
94template Object* Decode<Object*>(JNIEnv*, jobject);
95template String* Decode<String*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -070096template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
97template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070098template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070099
100namespace {
101
Elliott Hughescdf53122011-08-19 15:46:09 -0700102jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
103 if (obj == NULL) {
104 return NULL;
105 }
Elliott Hughes75770752011-08-24 17:52:38 -0700106 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700107 IndirectReferenceTable& weak_globals = vm->weak_globals;
108 MutexLock mu(vm->weak_globals_lock);
109 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
110 return reinterpret_cast<jweak>(ref);
111}
112
Elliott Hughesbf86d042011-08-31 17:53:14 -0700113// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700114template<typename T>
115T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700116 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700117}
118
Elliott Hughes418d20f2011-09-22 14:00:39 -0700119byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
120 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700121 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700122 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700123 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700124 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
125 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700126 case 'Z':
127 case 'B':
128 case 'C':
129 case 'S':
130 case 'I':
131 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
132 offset += 4;
133 break;
134 case 'F':
135 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
136 offset += 4;
137 break;
138 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700139 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700140 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
141 offset += sizeof(Object*);
142 break;
143 }
144 case 'D':
145 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
146 offset += 8;
147 break;
148 case 'J':
149 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
150 offset += 8;
151 break;
152 }
153 }
154 return arg_array.release();
155}
156
Elliott Hughes418d20f2011-09-22 14:00:39 -0700157byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
158 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700159 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700160 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700161 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700162 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
163 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700164 case 'Z':
165 case 'B':
166 case 'C':
167 case 'S':
168 case 'I':
169 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
170 offset += 4;
171 break;
172 case 'F':
173 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
174 offset += 4;
175 break;
176 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700177 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700178 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
179 offset += sizeof(Object*);
180 break;
181 }
182 case 'D':
183 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
184 offset += 8;
185 break;
186 case 'J':
187 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
188 offset += 8;
189 break;
190 }
191 }
192 return arg_array.release();
193}
194
Elliott Hughes418d20f2011-09-22 14:00:39 -0700195JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
196 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700197 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700198 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700199 return result;
200}
201
Elliott Hughes418d20f2011-09-22 14:00:39 -0700202JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
203 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
204 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700205 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700206 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
207 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700208}
209
Elliott Hughes75770752011-08-24 17:52:38 -0700210Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700211 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700212}
213
Elliott Hughes418d20f2011-09-22 14:00:39 -0700214JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
215 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
216 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700217 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700218 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
219 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700220}
221
Elliott Hughes418d20f2011-09-22 14:00:39 -0700222JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
223 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
224 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700225 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700226 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
227 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700228}
229
Elliott Hughes6b436852011-08-12 10:16:44 -0700230// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
231// separated with slashes but aren't wrapped with "L;" like regular descriptors
232// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
233// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
234// supported names with dots too (such as "a.b.C").
235std::string NormalizeJniClassDescriptor(const char* name) {
236 std::string result;
237 // Add the missing "L;" if necessary.
238 if (name[0] == '[') {
239 result = name;
240 } else {
241 result += 'L';
242 result += name;
243 result += ';';
244 }
245 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700246 if (result.find('.') != std::string::npos) {
247 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
248 << "\"" << name << "\"";
249 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700250 }
251 return result;
252}
253
Elliott Hughescdf53122011-08-19 15:46:09 -0700254jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
255 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700256 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700257 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700258 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700259
260 Method* method = NULL;
261 if (is_static) {
262 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700263 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700264 method = c->FindVirtualMethod(name, sig);
265 if (method == NULL) {
266 // No virtual method matching the signature. Search declared
267 // private methods and constructors.
268 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700269 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700270 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700271
Elliott Hughescdf53122011-08-19 15:46:09 -0700272 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700273 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700274 // TODO: try searching for the opposite kind of method from is_static
275 // for better diagnostics?
Elliott Hughescc5f9a92011-09-28 19:17:29 -0700276 ts.Self()->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700277 "no %s method %s", is_static ? "static" : "non-static",
278 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700279 return NULL;
280 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700281
Elliott Hughes418d20f2011-09-22 14:00:39 -0700282 method->InitJavaFields();
283
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700284 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700285}
286
Elliott Hughescdf53122011-08-19 15:46:09 -0700287jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
288 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700289 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700290 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700291 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700292
293 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294 Class* field_type;
295 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
296 if (sig[1] != '\0') {
297 // TODO: need to get the appropriate ClassLoader.
298 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
299 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700300 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700302 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700303 if (field_type == NULL) {
304 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700305 DCHECK(ts.Self()->IsExceptionPending());
306 ts.Self()->ClearException();
307 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
308 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
309 "no type \"%s\" found and so no field \"%s\" could be found in class "
310 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700311 return NULL;
312 }
313 if (is_static) {
314 field = c->FindStaticField(name, field_type);
315 } else {
316 field = c->FindInstanceField(name, field_type);
317 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700318 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700319 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700320 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700321 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700322 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700323 return NULL;
324 }
Elliott Hughes80609252011-09-23 17:24:51 -0700325 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700326 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700327}
328
Elliott Hughes75770752011-08-24 17:52:38 -0700329void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
330 JavaVMExt* vm = ts.Vm();
331 MutexLock mu(vm->pins_lock);
332 vm->pin_table.Add(array);
333}
334
335void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
336 JavaVMExt* vm = ts.Vm();
337 MutexLock mu(vm->pins_lock);
338 vm->pin_table.Remove(array);
339}
340
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700341template<typename JniT, typename ArtT>
342JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
343 CHECK_GE(length, 0); // TODO: ReportJniError
344 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700345 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700346}
347
Elliott Hughes75770752011-08-24 17:52:38 -0700348template <typename ArrayT, typename CArrayT, typename ArtArrayT>
349CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
350 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
351 PinPrimitiveArray(ts, array);
352 if (is_copy != NULL) {
353 *is_copy = JNI_FALSE;
354 }
355 return array->GetData();
356}
357
358template <typename ArrayT>
359void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
360 if (mode != JNI_COMMIT) {
361 Array* array = Decode<Array*>(ts, java_array);
362 UnpinPrimitiveArray(ts, array);
363 }
364}
365
Elliott Hughes814e4032011-08-23 12:07:56 -0700366void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700367 std::string type(PrettyTypeOf(array));
Elliott Hughes814e4032011-08-23 12:07:56 -0700368 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
369 "%s offset=%d length=%d %s.length=%d",
370 type.c_str(), start, length, identifier, array->GetLength());
371}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700372void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
373 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
374 "offset=%d length=%d string.length()=%d", start, length, array_length);
375}
Elliott Hughes814e4032011-08-23 12:07:56 -0700376
377template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700378void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700379 ArrayT* array = Decode<ArrayT*>(ts, java_array);
380 if (start < 0 || length < 0 || start + length > array->GetLength()) {
381 ThrowAIOOBE(ts, array, start, length, "src");
382 } else {
383 JavaT* data = array->GetData();
384 memcpy(buf, data + start, length * sizeof(JavaT));
385 }
386}
387
388template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700389void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700390 ArrayT* array = Decode<ArrayT*>(ts, java_array);
391 if (start < 0 || length < 0 || start + length > array->GetLength()) {
392 ThrowAIOOBE(ts, array, start, length, "dst");
393 } else {
394 JavaT* data = array->GetData();
395 memcpy(data + start, buf, length * sizeof(JavaT));
396 }
397}
398
Elliott Hughes75770752011-08-24 17:52:38 -0700399jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700400 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
401 CHECK(buffer_class.get() != NULL);
402 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
403}
404
Elliott Hughes75770752011-08-24 17:52:38 -0700405jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700406 static jclass buffer_class = InitDirectByteBufferClass(env);
407 return buffer_class;
408}
409
Elliott Hughes75770752011-08-24 17:52:38 -0700410jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
411 if (vm == NULL || p_env == NULL) {
412 return JNI_ERR;
413 }
414
415 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
416 JavaVMAttachArgs args;
417 if (thr_args == NULL) {
418 // Allow the v1.1 calling convention.
419 args.version = JNI_VERSION_1_2;
420 args.name = NULL;
421 args.group = NULL; // TODO: get "main" thread group
422 } else {
423 args.version = in_args->version;
424 args.name = in_args->name;
425 if (in_args->group != NULL) {
426 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
427 args.group = NULL; // TODO: decode in_args->group
428 } else {
429 args.group = NULL; // TODO: get "main" thread group
430 }
431 }
432 CHECK_GE(args.version, JNI_VERSION_1_2);
433
434 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700435 runtime->AttachCurrentThread(args.name, as_daemon);
436 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700437 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700438}
439
Elliott Hughes79082e32011-08-25 12:07:32 -0700440class SharedLibrary {
441 public:
442 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
443 : path_(path),
444 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700445 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700446 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700447 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700448 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700449 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700450 }
451
Elliott Hughes79082e32011-08-25 12:07:32 -0700452 Object* GetClassLoader() {
453 return class_loader_;
454 }
455
456 std::string GetPath() {
457 return path_;
458 }
459
460 /*
461 * Check the result of an earlier call to JNI_OnLoad on this library. If
462 * the call has not yet finished in another thread, wait for it.
463 */
464 bool CheckOnLoadResult(JavaVMExt* vm) {
465 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700466 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700467 // Check this so we don't end up waiting for ourselves. We need
468 // to return "true" so the caller can continue.
469 LOG(INFO) << *self << " recursive attempt to load library "
470 << "\"" << path_ << "\"";
471 return true;
472 }
473
474 MutexLock mu(jni_on_load_lock_);
475 while (jni_on_load_result_ == kPending) {
476 if (vm->verbose_jni) {
477 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
478 << "JNI_OnLoad...]";
479 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700480 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700481 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700482 }
483
484 bool okay = (jni_on_load_result_ == kOkay);
485 if (vm->verbose_jni) {
486 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
487 << (okay ? "succeeded" : "failed") << "]";
488 }
489 return okay;
490 }
491
492 void SetResult(bool result) {
493 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700494 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700495
496 // Broadcast a wakeup to anybody sleeping on the condition variable.
497 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700498 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700499 }
500
501 void* FindSymbol(const std::string& symbol_name) {
502 return dlsym(handle_, symbol_name.c_str());
503 }
504
505 private:
506 enum JNI_OnLoadState {
507 kPending,
508 kFailed,
509 kOkay,
510 };
511
512 // Path to library "/system/lib/libjni.so".
513 std::string path_;
514
515 // The void* returned by dlopen(3).
516 void* handle_;
517
518 // The ClassLoader this library is associated with.
519 Object* class_loader_;
520
521 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700522 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700523 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700524 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700525 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700526 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700527 // Result of earlier JNI_OnLoad call.
528 JNI_OnLoadState jni_on_load_result_;
529};
530
Elliott Hughescdf53122011-08-19 15:46:09 -0700531} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700532
Elliott Hughes79082e32011-08-25 12:07:32 -0700533// This exists mainly to keep implementation details out of the header file.
534class Libraries {
535 public:
536 Libraries() {
537 }
538
539 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700540 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700541 }
542
543 SharedLibrary* Get(const std::string& path) {
544 return libraries_[path];
545 }
546
547 void Put(const std::string& path, SharedLibrary* library) {
548 libraries_[path] = library;
549 }
550
551 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700552 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700553 std::string jni_short_name(JniShortName(m));
554 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700555 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700556 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
557 SharedLibrary* library = it->second;
558 if (library->GetClassLoader() != declaring_class_loader) {
559 // We only search libraries loaded by the appropriate ClassLoader.
560 continue;
561 }
562 // Try the short name then the long name...
563 void* fn = library->FindSymbol(jni_short_name);
564 if (fn == NULL) {
565 fn = library->FindSymbol(jni_long_name);
566 }
567 if (fn != NULL) {
568 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700569 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700570 << " in \"" << library->GetPath() << "\"]";
571 }
572 return fn;
573 }
574 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700575 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700576 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700577 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700578 return NULL;
579 }
580
581 private:
582 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
583
584 std::map<std::string, SharedLibrary*> libraries_;
585};
586
Elliott Hughes418d20f2011-09-22 14:00:39 -0700587JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
588 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
589 Object* receiver = Decode<Object*>(env, obj);
590 Method* method = DecodeMethod(mid);
591 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
592 return InvokeWithArgArray(env, receiver, method, arg_array.get());
593}
594
Elliott Hughescdf53122011-08-19 15:46:09 -0700595class JNI {
596 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700597
Elliott Hughescdf53122011-08-19 15:46:09 -0700598 static jint GetVersion(JNIEnv* env) {
599 ScopedJniThreadState ts(env);
600 return JNI_VERSION_1_6;
601 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700602
Elliott Hughescdf53122011-08-19 15:46:09 -0700603 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
604 ScopedJniThreadState ts(env);
605 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700606 return NULL;
607 }
608
Elliott Hughescdf53122011-08-19 15:46:09 -0700609 static jclass FindClass(JNIEnv* env, const char* name) {
610 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700611 Runtime* runtime = Runtime::Current();
612 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700613 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700614 Class* c = NULL;
615 if (runtime->IsStarted()) {
616 // TODO: need to get the appropriate ClassLoader.
617 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
618 c = class_linker->FindClass(descriptor, cl);
619 } else {
620 c = class_linker->FindSystemClass(descriptor);
621 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700622 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700623 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700624
Elliott Hughescdf53122011-08-19 15:46:09 -0700625 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
626 ScopedJniThreadState ts(env);
627 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700628 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700629 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700630
Elliott Hughescdf53122011-08-19 15:46:09 -0700631 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
632 ScopedJniThreadState ts(env);
633 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700634 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700636
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
638 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700639 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700640 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700642
Elliott Hughescdf53122011-08-19 15:46:09 -0700643 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
644 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700645 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700646 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700648
Elliott Hughes37f7a402011-08-22 18:56:01 -0700649 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
650 ScopedJniThreadState ts(env);
651 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700652 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700653 }
654
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700655 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700656 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700657 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700658 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700659 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700660
Elliott Hughes37f7a402011-08-22 18:56:01 -0700661 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700662 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700663 Class* c1 = Decode<Class*>(ts, java_class1);
664 Class* c2 = Decode<Class*>(ts, java_class2);
665 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700667
Elliott Hughes37f7a402011-08-22 18:56:01 -0700668 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700669 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700670 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700671 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700672 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700673 return JNI_TRUE;
674 } else {
675 Object* obj = Decode<Object*>(ts, jobj);
676 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700677 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700679 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700680
Elliott Hughes37f7a402011-08-22 18:56:01 -0700681 static jint Throw(JNIEnv* env, jthrowable java_exception) {
682 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700683 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700684 if (exception == NULL) {
685 return JNI_ERR;
686 }
687 ts.Self()->SetException(exception);
688 return JNI_OK;
689 }
690
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700691 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700693 // TODO: check for a pending exception to decide what constructor to call.
694 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
695 if (mid == NULL) {
696 return JNI_ERR;
697 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700698 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
699 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700700 return JNI_ERR;
701 }
702
703 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700704 args[0].l = s.get();
705 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
706 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700707 return JNI_ERR;
708 }
709
Elliott Hughes54e7df12011-09-16 11:47:04 -0700710 LOG(INFO) << "Throwing " << PrettyTypeOf(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700711 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700712 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700713
Elliott Hughes37f7a402011-08-22 18:56:01 -0700714 return JNI_OK;
715 }
716
717 static jboolean ExceptionCheck(JNIEnv* env) {
718 ScopedJniThreadState ts(env);
719 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
720 }
721
722 static void ExceptionClear(JNIEnv* env) {
723 ScopedJniThreadState ts(env);
724 ts.Self()->ClearException();
725 }
726
727 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700729
730 Thread* self = ts.Self();
731 Throwable* original_exception = self->GetException();
732 self->ClearException();
733
Elliott Hughesbf86d042011-08-31 17:53:14 -0700734 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700735 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
736 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
737 if (mid == NULL) {
738 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700739 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700740 } else {
741 env->CallVoidMethod(exception.get(), mid);
742 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700743 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700744 << " thrown while calling printStackTrace";
745 self->ClearException();
746 }
747 }
748
749 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700750 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700751
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 static jthrowable ExceptionOccurred(JNIEnv* env) {
753 ScopedJniThreadState ts(env);
754 Object* exception = ts.Self()->GetException();
755 if (exception == NULL) {
756 return NULL;
757 } else {
758 // TODO: if adding a local reference failing causes the VM to abort
759 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700760 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 if (localException == NULL) {
762 // We were unable to add a new local reference, and threw a new
763 // exception. We can't return "exception", because it's not a
764 // local reference. So we have to return NULL, indicating that
765 // there was no exception, even though it's pretty much raining
766 // exceptions in here.
767 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
768 }
769 return localException;
770 }
771 }
772
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 static void FatalError(JNIEnv* env, const char* msg) {
774 ScopedJniThreadState ts(env);
775 LOG(FATAL) << "JNI FatalError called: " << msg;
776 }
777
778 static jint PushLocalFrame(JNIEnv* env, jint cap) {
779 ScopedJniThreadState ts(env);
780 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
781 return JNI_OK;
782 }
783
784 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
785 ScopedJniThreadState ts(env);
786 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
787 return res;
788 }
789
Elliott Hughes72025e52011-08-23 17:50:30 -0700790 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
791 ScopedJniThreadState ts(env);
792 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
793 return 0;
794 }
795
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
797 ScopedJniThreadState ts(env);
798 if (obj == NULL) {
799 return NULL;
800 }
801
Elliott Hughes75770752011-08-24 17:52:38 -0700802 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 IndirectReferenceTable& globals = vm->globals;
804 MutexLock mu(vm->globals_lock);
805 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
806 return reinterpret_cast<jobject>(ref);
807 }
808
809 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
810 ScopedJniThreadState ts(env);
811 if (obj == NULL) {
812 return;
813 }
814
Elliott Hughes75770752011-08-24 17:52:38 -0700815 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 IndirectReferenceTable& globals = vm->globals;
817 MutexLock mu(vm->globals_lock);
818
819 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
820 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
821 << "failed to find entry";
822 }
823 }
824
825 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
826 ScopedJniThreadState ts(env);
827 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
828 }
829
830 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
831 ScopedJniThreadState ts(env);
832 if (obj == NULL) {
833 return;
834 }
835
Elliott Hughes75770752011-08-24 17:52:38 -0700836 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 IndirectReferenceTable& weak_globals = vm->weak_globals;
838 MutexLock mu(vm->weak_globals_lock);
839
840 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
841 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
842 << "failed to find entry";
843 }
844 }
845
846 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
847 ScopedJniThreadState ts(env);
848 if (obj == NULL) {
849 return NULL;
850 }
851
852 IndirectReferenceTable& locals = ts.Env()->locals;
853
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700854 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
856 return reinterpret_cast<jobject>(ref);
857 }
858
859 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
860 ScopedJniThreadState ts(env);
861 if (obj == NULL) {
862 return;
863 }
864
865 IndirectReferenceTable& locals = ts.Env()->locals;
866
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700867 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 if (!locals.Remove(cookie, obj)) {
869 // Attempting to delete a local reference that is not in the
870 // topmost local reference frame is a no-op. DeleteLocalRef returns
871 // void and doesn't throw any exceptions, but we should probably
872 // complain about it so the user will notice that things aren't
873 // going quite the way they expect.
874 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
875 << "failed to find entry";
876 }
877 }
878
879 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
880 ScopedJniThreadState ts(env);
881 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
882 ? JNI_TRUE : JNI_FALSE;
883 }
884
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700885 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700886 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700887 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700888 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700889 return NULL;
890 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700891 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 }
893
Elliott Hughes72025e52011-08-23 17:50:30 -0700894 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 ScopedJniThreadState ts(env);
896 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700897 va_start(args, mid);
898 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 va_end(args);
900 return result;
901 }
902
Elliott Hughes72025e52011-08-23 17:50:30 -0700903 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700905 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700906 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700907 return NULL;
908 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700909 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700910 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700912 return local_result;
913 }
914
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700917 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700918 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700919 return NULL;
920 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700921 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700922 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700923 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 return local_result;
925 }
926
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
928 ScopedJniThreadState ts(env);
929 return FindMethodID(ts, c, name, sig, false);
930 }
931
932 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
933 ScopedJniThreadState ts(env);
934 return FindMethodID(ts, c, name, sig, true);
935 }
936
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 va_list ap;
940 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700941 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700943 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 }
945
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700948 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700949 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 }
951
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700954 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700955 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 }
957
Elliott Hughes72025e52011-08-23 17:50:30 -0700958 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700959 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 va_list ap;
961 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700962 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 va_end(ap);
964 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700969 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 }
971
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700974 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 }
976
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 va_list ap;
980 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700981 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 va_end(ap);
983 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 }
985
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700988 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 }
990
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700993 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 va_list ap;
999 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001000 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 va_end(ap);
1002 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 }
1004
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001007 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001012 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 va_list ap;
1018 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001019 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 va_end(ap);
1021 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 }
1023
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001026 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001031 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 va_list ap;
1037 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001038 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 va_end(ap);
1040 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001045 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001050 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 va_list ap;
1056 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001057 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 va_end(ap);
1059 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001064 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001069 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 va_list ap;
1075 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001076 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 va_end(ap);
1078 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001083 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001088 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 va_list ap;
1094 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001095 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 va_end(ap);
1097 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001102 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001107 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 va_list ap;
1113 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001114 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001120 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001125 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
1128 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 ScopedJniThreadState ts(env);
1131 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001133 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001134 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 va_end(ap);
1136 return local_result;
1137 }
1138
1139 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001142 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001143 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
1146 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001149 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001150 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
1153 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 ScopedJniThreadState ts(env);
1156 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001158 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 va_end(ap);
1160 return result.z;
1161 }
1162
1163 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001166 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
1169 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001172 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 }
1174
1175 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 ScopedJniThreadState ts(env);
1178 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001180 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 va_end(ap);
1182 return result.b;
1183 }
1184
1185 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001188 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
1191 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001194 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
1197 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 ScopedJniThreadState ts(env);
1200 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001202 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 va_end(ap);
1204 return result.c;
1205 }
1206
1207 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001210 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
1213 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001216 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 }
1218
1219 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001220 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 ScopedJniThreadState ts(env);
1222 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001224 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 va_end(ap);
1226 return result.s;
1227 }
1228
1229 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001232 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 }
1234
1235 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001238 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes418d20f2011-09-22 14:00:39 -07001241 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
1243 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001244 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001245 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 va_end(ap);
1247 return result.i;
1248 }
1249
1250 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001253 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 }
1255
1256 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001259 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
1262 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 ScopedJniThreadState ts(env);
1265 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001266 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001267 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 va_end(ap);
1269 return result.j;
1270 }
1271
1272 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001275 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
1278 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001281 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
1284 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 ScopedJniThreadState ts(env);
1287 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001288 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001289 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 va_end(ap);
1291 return result.f;
1292 }
1293
1294 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001295 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001297 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
1300 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001303 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
1306 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 ScopedJniThreadState ts(env);
1309 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001310 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001311 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 va_end(ap);
1313 return result.d;
1314 }
1315
1316 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001317 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001319 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
1322 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001325 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
1328 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001329 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 ScopedJniThreadState ts(env);
1331 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001333 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 va_end(ap);
1335 }
1336
1337 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001340 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
1343 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001346 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 }
1348
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001349 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 ScopedJniThreadState ts(env);
1351 return FindFieldID(ts, c, name, sig, false);
1352 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001353
1354
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001355 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 ScopedJniThreadState ts(env);
1357 return FindFieldID(ts, c, name, sig, true);
1358 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001359
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001360 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001362 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001363 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001364 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001366
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001367 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001369 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001370 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001373 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001375 Object* o = Decode<Object*>(ts, java_object);
1376 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001377 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001378 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
1380
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001383 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001384 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001385 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
1387
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388#define GET_PRIMITIVE_FIELD(fn, instance) \
1389 ScopedJniThreadState ts(env); \
1390 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001391 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 return f->fn(o)
1393
1394#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1395 ScopedJniThreadState ts(env); \
1396 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001397 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001398 f->fn(o, value)
1399
1400 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1401 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 }
1403
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1405 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001408 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1409 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001412 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1413 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001416 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1417 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001420 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1421 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001424 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1425 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001428 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1429 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1433 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1437 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1441 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001444 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1445 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1449 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1453 GET_PRIMITIVE_FIELD(GetLong, NULL);
1454 }
1455
1456 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1457 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1458 }
1459
1460 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1461 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1462 }
1463
1464 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1465 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1466 }
1467
1468 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1469 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1470 }
1471
1472 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1473 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1474 }
1475
1476 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1477 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1478 }
1479
1480 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1481 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1482 }
1483
1484 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1485 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1486 }
1487
1488 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1489 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1490 }
1491
1492 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1493 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1494 }
1495
1496 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1497 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1498 }
1499
1500 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1501 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1502 }
1503
1504 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1505 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1506 }
1507
1508 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1509 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1510 }
1511
1512 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1513 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1514 }
1515
1516 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1517 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1518 }
1519
1520 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1521 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1522 }
1523
1524 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1525 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes418d20f2011-09-22 14:00:39 -07001528 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 ScopedJniThreadState ts(env);
1530 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001531 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001532 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001533 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 va_end(ap);
1535 return local_result;
1536 }
1537
Elliott Hughes418d20f2011-09-22 14:00:39 -07001538 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001539 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001540 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001541 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes418d20f2011-09-22 14:00:39 -07001544 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001546 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001547 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 }
1549
Elliott Hughes418d20f2011-09-22 14:00:39 -07001550 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 ScopedJniThreadState ts(env);
1552 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001553 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001554 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 va_end(ap);
1556 return result.z;
1557 }
1558
Elliott Hughes418d20f2011-09-22 14:00:39 -07001559 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001561 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes418d20f2011-09-22 14:00:39 -07001564 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001566 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
Elliott Hughes72025e52011-08-23 17:50:30 -07001569 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 ScopedJniThreadState ts(env);
1571 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001572 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001573 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 va_end(ap);
1575 return result.b;
1576 }
1577
Elliott Hughes418d20f2011-09-22 14:00:39 -07001578 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001580 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
Elliott Hughes418d20f2011-09-22 14:00:39 -07001583 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001585 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
Elliott Hughes72025e52011-08-23 17:50:30 -07001588 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 ScopedJniThreadState ts(env);
1590 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001591 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001592 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 va_end(ap);
1594 return result.c;
1595 }
1596
Elliott Hughes418d20f2011-09-22 14:00:39 -07001597 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001599 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 }
1601
Elliott Hughes418d20f2011-09-22 14:00:39 -07001602 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001604 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes72025e52011-08-23 17:50:30 -07001607 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 ScopedJniThreadState ts(env);
1609 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001610 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001611 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 va_end(ap);
1613 return result.s;
1614 }
1615
Elliott Hughes418d20f2011-09-22 14:00:39 -07001616 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001618 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes418d20f2011-09-22 14:00:39 -07001621 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001623 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 ScopedJniThreadState ts(env);
1628 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001629 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001630 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 va_end(ap);
1632 return result.i;
1633 }
1634
Elliott Hughes418d20f2011-09-22 14:00:39 -07001635 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001637 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 }
1639
Elliott Hughes418d20f2011-09-22 14:00:39 -07001640 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001642 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes72025e52011-08-23 17:50:30 -07001645 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
1647 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001649 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 va_end(ap);
1651 return result.j;
1652 }
1653
Elliott Hughes418d20f2011-09-22 14:00:39 -07001654 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001656 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 }
1658
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001661 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
Elliott Hughes72025e52011-08-23 17:50:30 -07001664 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 ScopedJniThreadState ts(env);
1666 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001668 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 va_end(ap);
1670 return result.f;
1671 }
1672
Elliott Hughes418d20f2011-09-22 14:00:39 -07001673 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001675 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 }
1677
Elliott Hughes418d20f2011-09-22 14:00:39 -07001678 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001680 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
Elliott Hughes72025e52011-08-23 17:50:30 -07001683 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 ScopedJniThreadState ts(env);
1685 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001687 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 va_end(ap);
1689 return result.d;
1690 }
1691
Elliott Hughes418d20f2011-09-22 14:00:39 -07001692 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001694 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 }
1696
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001699 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 }
1701
Elliott Hughes72025e52011-08-23 17:50:30 -07001702 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 ScopedJniThreadState ts(env);
1704 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001706 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 va_end(ap);
1708 }
1709
Elliott Hughes418d20f2011-09-22 14:00:39 -07001710 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001712 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 }
1714
Elliott Hughes418d20f2011-09-22 14:00:39 -07001715 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001717 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes814e4032011-08-23 12:07:56 -07001720 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001722 if (chars == NULL && char_count == 0) {
1723 return NULL;
1724 }
1725 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001726 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
1729 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1730 ScopedJniThreadState ts(env);
1731 if (utf == NULL) {
1732 return NULL;
1733 }
1734 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001735 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes814e4032011-08-23 12:07:56 -07001738 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1739 ScopedJniThreadState ts(env);
1740 return Decode<String*>(ts, java_string)->GetLength();
1741 }
1742
1743 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1744 ScopedJniThreadState ts(env);
1745 return Decode<String*>(ts, java_string)->GetUtfLength();
1746 }
1747
Elliott Hughesb465ab02011-08-24 11:21:21 -07001748 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001750 String* s = Decode<String*>(ts, java_string);
1751 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1752 ThrowSIOOBE(ts, start, length, s->GetLength());
1753 } else {
1754 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1755 memcpy(buf, chars + start, length * sizeof(jchar));
1756 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001757 }
1758
Elliott Hughesb465ab02011-08-24 11:21:21 -07001759 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001760 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001761 String* s = Decode<String*>(ts, java_string);
1762 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1763 ThrowSIOOBE(ts, start, length, s->GetLength());
1764 } else {
1765 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1766 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1767 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001768 }
1769
Elliott Hughes75770752011-08-24 17:52:38 -07001770 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001771 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001772 String* s = Decode<String*>(ts, java_string);
1773 const CharArray* chars = s->GetCharArray();
1774 PinPrimitiveArray(ts, chars);
1775 if (is_copy != NULL) {
1776 *is_copy = JNI_FALSE;
1777 }
1778 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001779 }
1780
Elliott Hughes75770752011-08-24 17:52:38 -07001781 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001782 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001783 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 }
1785
Elliott Hughes75770752011-08-24 17:52:38 -07001786 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001788 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 }
1790
Elliott Hughes75770752011-08-24 17:52:38 -07001791 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001793 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes75770752011-08-24 17:52:38 -07001796 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001797 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001798 if (java_string == NULL) {
1799 return NULL;
1800 }
1801 if (is_copy != NULL) {
1802 *is_copy = JNI_TRUE;
1803 }
1804 String* s = Decode<String*>(ts, java_string);
1805 size_t byte_count = s->GetUtfLength();
1806 char* bytes = new char[byte_count + 1];
1807 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001808 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001809 return NULL;
1810 }
1811 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1812 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1813 bytes[byte_count] = '\0';
1814 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001815 }
1816
Elliott Hughes75770752011-08-24 17:52:38 -07001817 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001818 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001819 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001820 }
1821
Elliott Hughesbd935992011-08-22 11:59:34 -07001822 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001824 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001825 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001826 Array* array = obj->AsArray();
1827 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
Elliott Hughes814e4032011-08-23 12:07:56 -07001830 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001832 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001833 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 }
1835
1836 static void SetObjectArrayElement(JNIEnv* env,
1837 jobjectArray java_array, jsize index, jobject java_value) {
1838 ScopedJniThreadState ts(env);
1839 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1840 Object* value = Decode<Object*>(ts, java_value);
1841 array->Set(index, value);
1842 }
1843
1844 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1845 ScopedJniThreadState ts(env);
1846 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1847 }
1848
1849 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1850 ScopedJniThreadState ts(env);
1851 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1852 }
1853
1854 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1855 ScopedJniThreadState ts(env);
1856 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1857 }
1858
1859 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1860 ScopedJniThreadState ts(env);
1861 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1862 }
1863
1864 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1865 ScopedJniThreadState ts(env);
1866 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1867 }
1868
1869 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1870 ScopedJniThreadState ts(env);
1871 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1872 }
1873
1874 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1875 ScopedJniThreadState ts(env);
1876 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1877 }
1878
1879 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1880 ScopedJniThreadState ts(env);
1881 CHECK_GE(length, 0); // TODO: ReportJniError
1882
1883 // Compute the array class corresponding to the given element class.
1884 Class* element_class = Decode<Class*>(ts, element_jclass);
1885 std::string descriptor;
1886 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001887 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001888
1889 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001890 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1891 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 return NULL;
1893 }
1894
Elliott Hughes75770752011-08-24 17:52:38 -07001895 // Allocate and initialize if necessary.
1896 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001897 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001898 if (initial_element != NULL) {
1899 Object* initial_object = Decode<Object*>(ts, initial_element);
1900 for (jsize i = 0; i < length; ++i) {
1901 result->Set(i, initial_object);
1902 }
1903 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001904 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 }
1906
1907 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1908 ScopedJniThreadState ts(env);
1909 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1910 }
1911
Elliott Hughes75770752011-08-24 17:52:38 -07001912 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001913 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001914 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001915 }
1916
Elliott Hughes75770752011-08-24 17:52:38 -07001917 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001918 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001919 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001920 }
1921
Elliott Hughes75770752011-08-24 17:52:38 -07001922 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001924 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001925 }
1926
Elliott Hughes75770752011-08-24 17:52:38 -07001927 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001929 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001930 }
1931
Elliott Hughes75770752011-08-24 17:52:38 -07001932 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001934 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes75770752011-08-24 17:52:38 -07001937 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001939 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
Elliott Hughes75770752011-08-24 17:52:38 -07001942 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001944 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes75770752011-08-24 17:52:38 -07001947 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001949 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 }
1951
Elliott Hughes75770752011-08-24 17:52:38 -07001952 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001954 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001955 }
1956
Elliott Hughes75770752011-08-24 17:52:38 -07001957 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001959 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
Elliott Hughes75770752011-08-24 17:52:38 -07001962 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001964 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 }
1966
Elliott Hughes75770752011-08-24 17:52:38 -07001967 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001969 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Elliott Hughes75770752011-08-24 17:52:38 -07001972 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001974 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
Elliott Hughes75770752011-08-24 17:52:38 -07001977 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001979 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
Elliott Hughes75770752011-08-24 17:52:38 -07001982 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001984 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 }
1986
Elliott Hughes75770752011-08-24 17:52:38 -07001987 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001989 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Elliott Hughes75770752011-08-24 17:52:38 -07001992 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001994 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
Elliott Hughes75770752011-08-24 17:52:38 -07001997 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001999 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
Elliott Hughes814e4032011-08-23 12:07:56 -07002002 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002004 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 }
2006
Elliott Hughes814e4032011-08-23 12:07:56 -07002007 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002009 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Elliott Hughes814e4032011-08-23 12:07:56 -07002012 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002014 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 }
2016
Elliott Hughes814e4032011-08-23 12:07:56 -07002017 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002019 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
Elliott Hughes814e4032011-08-23 12:07:56 -07002022 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002024 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 }
2026
Elliott Hughes814e4032011-08-23 12:07:56 -07002027 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002029 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Elliott Hughes814e4032011-08-23 12:07:56 -07002032 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002034 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Elliott Hughes814e4032011-08-23 12:07:56 -07002037 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002039 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Elliott Hughes814e4032011-08-23 12:07:56 -07002042 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002044 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Elliott Hughes814e4032011-08-23 12:07:56 -07002047 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002049 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Elliott Hughes814e4032011-08-23 12:07:56 -07002052 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002054 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes814e4032011-08-23 12:07:56 -07002057 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Elliott Hughes814e4032011-08-23 12:07:56 -07002062 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes814e4032011-08-23 12:07:56 -07002067 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Elliott Hughes814e4032011-08-23 12:07:56 -07002072 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes814e4032011-08-23 12:07:56 -07002077 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Elliott Hughes5174fe62011-08-23 15:12:35 -07002082 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002084 Class* c = Decode<Class*>(ts, java_class);
2085
Elliott Hughes5174fe62011-08-23 15:12:35 -07002086 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 const char* name = methods[i].name;
2088 const char* sig = methods[i].signature;
2089
2090 if (*sig == '!') {
2091 // TODO: fast jni. it's too noisy to log all these.
2092 ++sig;
2093 }
2094
Elliott Hughes5174fe62011-08-23 15:12:35 -07002095 Method* m = c->FindDirectMethod(name, sig);
2096 if (m == NULL) {
2097 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002099 if (m == NULL) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002100 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescc5f9a92011-09-28 19:17:29 -07002101 ts.Self()->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002103 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002105 } else if (!m->IsNative()) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002106 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescc5f9a92011-09-28 19:17:29 -07002107 ts.Self()->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002109 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 return JNI_ERR;
2111 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002112
Elliott Hughes75770752011-08-24 17:52:38 -07002113 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002114 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002115 }
2116
2117 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119 return JNI_OK;
2120 }
2121
Elliott Hughes5174fe62011-08-23 15:12:35 -07002122 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002124 Class* c = Decode<Class*>(ts, java_class);
2125
Elliott Hughes75770752011-08-24 17:52:38 -07002126 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002127 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002128 }
2129
2130 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2131 Method* m = c->GetDirectMethod(i);
2132 if (m->IsNative()) {
2133 m->UnregisterNative();
2134 }
2135 }
2136 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2137 Method* m = c->GetVirtualMethod(i);
2138 if (m->IsNative()) {
2139 m->UnregisterNative();
2140 }
2141 }
2142
2143 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
2145
Elliott Hughes72025e52011-08-23 17:50:30 -07002146 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002148 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002149 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 }
2151
Elliott Hughes72025e52011-08-23 17:50:30 -07002152 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002154 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002155 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
2158 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2159 ScopedJniThreadState ts(env);
2160 Runtime* runtime = Runtime::Current();
2161 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002162 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 } else {
2164 *vm = NULL;
2165 }
2166 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2167 }
2168
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2170 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002171
2172 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002173 CHECK(address != NULL); // TODO: ReportJniError
2174 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002175
2176 jclass buffer_class = GetDirectByteBufferClass(env);
2177 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2178 if (mid == NULL) {
2179 return NULL;
2180 }
2181
2182 // At the moment, the Java side is limited to 32 bits.
2183 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2184 CHECK_LE(capacity, 0xffffffff);
2185 jint address_arg = reinterpret_cast<jint>(address);
2186 jint capacity_arg = static_cast<jint>(capacity);
2187
2188 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2189 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Elliott Hughesb465ab02011-08-24 11:21:21 -07002192 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002194 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2195 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Elliott Hughesb465ab02011-08-24 11:21:21 -07002198 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002200 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2201 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 }
2203
Elliott Hughesb465ab02011-08-24 11:21:21 -07002204 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002206
Elliott Hughes75770752011-08-24 17:52:38 -07002207 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002208
2209 // Do we definitely know what kind of reference this is?
2210 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2211 IndirectRefKind kind = GetIndirectRefKind(ref);
2212 switch (kind) {
2213 case kLocal:
2214 return JNILocalRefType;
2215 case kGlobal:
2216 return JNIGlobalRefType;
2217 case kWeakGlobal:
2218 return JNIWeakGlobalRefType;
2219 case kSirtOrInvalid:
2220 // Is it in a stack IRT?
2221 if (ts.Self()->SirtContains(java_object)) {
2222 return JNILocalRefType;
2223 }
2224
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002225 if (!ts.Env()->work_around_app_jni_bugs) {
2226 return JNIInvalidRefType;
2227 }
2228
Elliott Hughesb465ab02011-08-24 11:21:21 -07002229 // If we're handing out direct pointers, check whether it's a direct pointer
2230 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002231 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002232 if (ts.Env()->locals.Contains(java_object)) {
2233 return JNILocalRefType;
2234 }
2235 }
2236
2237 return JNIInvalidRefType;
2238 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002241
Elliott Hughesa2501992011-08-26 19:39:54 -07002242const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002243 NULL, // reserved0.
2244 NULL, // reserved1.
2245 NULL, // reserved2.
2246 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 JNI::GetVersion,
2248 JNI::DefineClass,
2249 JNI::FindClass,
2250 JNI::FromReflectedMethod,
2251 JNI::FromReflectedField,
2252 JNI::ToReflectedMethod,
2253 JNI::GetSuperclass,
2254 JNI::IsAssignableFrom,
2255 JNI::ToReflectedField,
2256 JNI::Throw,
2257 JNI::ThrowNew,
2258 JNI::ExceptionOccurred,
2259 JNI::ExceptionDescribe,
2260 JNI::ExceptionClear,
2261 JNI::FatalError,
2262 JNI::PushLocalFrame,
2263 JNI::PopLocalFrame,
2264 JNI::NewGlobalRef,
2265 JNI::DeleteGlobalRef,
2266 JNI::DeleteLocalRef,
2267 JNI::IsSameObject,
2268 JNI::NewLocalRef,
2269 JNI::EnsureLocalCapacity,
2270 JNI::AllocObject,
2271 JNI::NewObject,
2272 JNI::NewObjectV,
2273 JNI::NewObjectA,
2274 JNI::GetObjectClass,
2275 JNI::IsInstanceOf,
2276 JNI::GetMethodID,
2277 JNI::CallObjectMethod,
2278 JNI::CallObjectMethodV,
2279 JNI::CallObjectMethodA,
2280 JNI::CallBooleanMethod,
2281 JNI::CallBooleanMethodV,
2282 JNI::CallBooleanMethodA,
2283 JNI::CallByteMethod,
2284 JNI::CallByteMethodV,
2285 JNI::CallByteMethodA,
2286 JNI::CallCharMethod,
2287 JNI::CallCharMethodV,
2288 JNI::CallCharMethodA,
2289 JNI::CallShortMethod,
2290 JNI::CallShortMethodV,
2291 JNI::CallShortMethodA,
2292 JNI::CallIntMethod,
2293 JNI::CallIntMethodV,
2294 JNI::CallIntMethodA,
2295 JNI::CallLongMethod,
2296 JNI::CallLongMethodV,
2297 JNI::CallLongMethodA,
2298 JNI::CallFloatMethod,
2299 JNI::CallFloatMethodV,
2300 JNI::CallFloatMethodA,
2301 JNI::CallDoubleMethod,
2302 JNI::CallDoubleMethodV,
2303 JNI::CallDoubleMethodA,
2304 JNI::CallVoidMethod,
2305 JNI::CallVoidMethodV,
2306 JNI::CallVoidMethodA,
2307 JNI::CallNonvirtualObjectMethod,
2308 JNI::CallNonvirtualObjectMethodV,
2309 JNI::CallNonvirtualObjectMethodA,
2310 JNI::CallNonvirtualBooleanMethod,
2311 JNI::CallNonvirtualBooleanMethodV,
2312 JNI::CallNonvirtualBooleanMethodA,
2313 JNI::CallNonvirtualByteMethod,
2314 JNI::CallNonvirtualByteMethodV,
2315 JNI::CallNonvirtualByteMethodA,
2316 JNI::CallNonvirtualCharMethod,
2317 JNI::CallNonvirtualCharMethodV,
2318 JNI::CallNonvirtualCharMethodA,
2319 JNI::CallNonvirtualShortMethod,
2320 JNI::CallNonvirtualShortMethodV,
2321 JNI::CallNonvirtualShortMethodA,
2322 JNI::CallNonvirtualIntMethod,
2323 JNI::CallNonvirtualIntMethodV,
2324 JNI::CallNonvirtualIntMethodA,
2325 JNI::CallNonvirtualLongMethod,
2326 JNI::CallNonvirtualLongMethodV,
2327 JNI::CallNonvirtualLongMethodA,
2328 JNI::CallNonvirtualFloatMethod,
2329 JNI::CallNonvirtualFloatMethodV,
2330 JNI::CallNonvirtualFloatMethodA,
2331 JNI::CallNonvirtualDoubleMethod,
2332 JNI::CallNonvirtualDoubleMethodV,
2333 JNI::CallNonvirtualDoubleMethodA,
2334 JNI::CallNonvirtualVoidMethod,
2335 JNI::CallNonvirtualVoidMethodV,
2336 JNI::CallNonvirtualVoidMethodA,
2337 JNI::GetFieldID,
2338 JNI::GetObjectField,
2339 JNI::GetBooleanField,
2340 JNI::GetByteField,
2341 JNI::GetCharField,
2342 JNI::GetShortField,
2343 JNI::GetIntField,
2344 JNI::GetLongField,
2345 JNI::GetFloatField,
2346 JNI::GetDoubleField,
2347 JNI::SetObjectField,
2348 JNI::SetBooleanField,
2349 JNI::SetByteField,
2350 JNI::SetCharField,
2351 JNI::SetShortField,
2352 JNI::SetIntField,
2353 JNI::SetLongField,
2354 JNI::SetFloatField,
2355 JNI::SetDoubleField,
2356 JNI::GetStaticMethodID,
2357 JNI::CallStaticObjectMethod,
2358 JNI::CallStaticObjectMethodV,
2359 JNI::CallStaticObjectMethodA,
2360 JNI::CallStaticBooleanMethod,
2361 JNI::CallStaticBooleanMethodV,
2362 JNI::CallStaticBooleanMethodA,
2363 JNI::CallStaticByteMethod,
2364 JNI::CallStaticByteMethodV,
2365 JNI::CallStaticByteMethodA,
2366 JNI::CallStaticCharMethod,
2367 JNI::CallStaticCharMethodV,
2368 JNI::CallStaticCharMethodA,
2369 JNI::CallStaticShortMethod,
2370 JNI::CallStaticShortMethodV,
2371 JNI::CallStaticShortMethodA,
2372 JNI::CallStaticIntMethod,
2373 JNI::CallStaticIntMethodV,
2374 JNI::CallStaticIntMethodA,
2375 JNI::CallStaticLongMethod,
2376 JNI::CallStaticLongMethodV,
2377 JNI::CallStaticLongMethodA,
2378 JNI::CallStaticFloatMethod,
2379 JNI::CallStaticFloatMethodV,
2380 JNI::CallStaticFloatMethodA,
2381 JNI::CallStaticDoubleMethod,
2382 JNI::CallStaticDoubleMethodV,
2383 JNI::CallStaticDoubleMethodA,
2384 JNI::CallStaticVoidMethod,
2385 JNI::CallStaticVoidMethodV,
2386 JNI::CallStaticVoidMethodA,
2387 JNI::GetStaticFieldID,
2388 JNI::GetStaticObjectField,
2389 JNI::GetStaticBooleanField,
2390 JNI::GetStaticByteField,
2391 JNI::GetStaticCharField,
2392 JNI::GetStaticShortField,
2393 JNI::GetStaticIntField,
2394 JNI::GetStaticLongField,
2395 JNI::GetStaticFloatField,
2396 JNI::GetStaticDoubleField,
2397 JNI::SetStaticObjectField,
2398 JNI::SetStaticBooleanField,
2399 JNI::SetStaticByteField,
2400 JNI::SetStaticCharField,
2401 JNI::SetStaticShortField,
2402 JNI::SetStaticIntField,
2403 JNI::SetStaticLongField,
2404 JNI::SetStaticFloatField,
2405 JNI::SetStaticDoubleField,
2406 JNI::NewString,
2407 JNI::GetStringLength,
2408 JNI::GetStringChars,
2409 JNI::ReleaseStringChars,
2410 JNI::NewStringUTF,
2411 JNI::GetStringUTFLength,
2412 JNI::GetStringUTFChars,
2413 JNI::ReleaseStringUTFChars,
2414 JNI::GetArrayLength,
2415 JNI::NewObjectArray,
2416 JNI::GetObjectArrayElement,
2417 JNI::SetObjectArrayElement,
2418 JNI::NewBooleanArray,
2419 JNI::NewByteArray,
2420 JNI::NewCharArray,
2421 JNI::NewShortArray,
2422 JNI::NewIntArray,
2423 JNI::NewLongArray,
2424 JNI::NewFloatArray,
2425 JNI::NewDoubleArray,
2426 JNI::GetBooleanArrayElements,
2427 JNI::GetByteArrayElements,
2428 JNI::GetCharArrayElements,
2429 JNI::GetShortArrayElements,
2430 JNI::GetIntArrayElements,
2431 JNI::GetLongArrayElements,
2432 JNI::GetFloatArrayElements,
2433 JNI::GetDoubleArrayElements,
2434 JNI::ReleaseBooleanArrayElements,
2435 JNI::ReleaseByteArrayElements,
2436 JNI::ReleaseCharArrayElements,
2437 JNI::ReleaseShortArrayElements,
2438 JNI::ReleaseIntArrayElements,
2439 JNI::ReleaseLongArrayElements,
2440 JNI::ReleaseFloatArrayElements,
2441 JNI::ReleaseDoubleArrayElements,
2442 JNI::GetBooleanArrayRegion,
2443 JNI::GetByteArrayRegion,
2444 JNI::GetCharArrayRegion,
2445 JNI::GetShortArrayRegion,
2446 JNI::GetIntArrayRegion,
2447 JNI::GetLongArrayRegion,
2448 JNI::GetFloatArrayRegion,
2449 JNI::GetDoubleArrayRegion,
2450 JNI::SetBooleanArrayRegion,
2451 JNI::SetByteArrayRegion,
2452 JNI::SetCharArrayRegion,
2453 JNI::SetShortArrayRegion,
2454 JNI::SetIntArrayRegion,
2455 JNI::SetLongArrayRegion,
2456 JNI::SetFloatArrayRegion,
2457 JNI::SetDoubleArrayRegion,
2458 JNI::RegisterNatives,
2459 JNI::UnregisterNatives,
2460 JNI::MonitorEnter,
2461 JNI::MonitorExit,
2462 JNI::GetJavaVM,
2463 JNI::GetStringRegion,
2464 JNI::GetStringUTFRegion,
2465 JNI::GetPrimitiveArrayCritical,
2466 JNI::ReleasePrimitiveArrayCritical,
2467 JNI::GetStringCritical,
2468 JNI::ReleaseStringCritical,
2469 JNI::NewWeakGlobalRef,
2470 JNI::DeleteWeakGlobalRef,
2471 JNI::ExceptionCheck,
2472 JNI::NewDirectByteBuffer,
2473 JNI::GetDirectBufferAddress,
2474 JNI::GetDirectBufferCapacity,
2475 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002476};
2477
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002478static const size_t kMonitorsInitial = 32; // Arbitrary.
2479static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2480
2481static const size_t kLocalsInitial = 64; // Arbitrary.
2482static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002483
Elliott Hughes75770752011-08-24 17:52:38 -07002484JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002485 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002486 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002487 local_ref_cookie(IRT_FIRST_SEGMENT),
2488 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes75770752011-08-24 17:52:38 -07002489 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002490 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002491 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002492 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002493 functions = unchecked_functions = &gNativeInterface;
2494 if (check_jni) {
2495 functions = GetCheckJniNativeInterface();
2496 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002497 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2498 // errors will ensue.
2499 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2500 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002501}
2502
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002503JNIEnvExt::~JNIEnvExt() {
2504}
2505
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002506void JNIEnvExt::DumpReferenceTables() {
2507 locals.Dump();
2508 monitors.Dump();
2509}
2510
Carl Shapiroea4dca82011-08-01 13:45:38 -07002511// JNI Invocation interface.
2512
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002513extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2514 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2515 if (args->version < JNI_VERSION_1_2) {
2516 return JNI_EVERSION;
2517 }
2518 Runtime::Options options;
2519 for (int i = 0; i < args->nOptions; ++i) {
2520 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002521 options.push_back(std::make_pair(StringPiece(option->optionString),
2522 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002523 }
2524 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002525 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002526 if (runtime == NULL) {
2527 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002528 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002529 runtime->Start();
2530 *p_env = Thread::Current()->GetJniEnv();
2531 *p_vm = runtime->GetJavaVM();
2532 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002533}
2534
Elliott Hughesf2682d52011-08-15 16:37:04 -07002535extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002536 Runtime* runtime = Runtime::Current();
2537 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002538 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002539 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002540 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002541 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002542 }
2543 return JNI_OK;
2544}
2545
2546// Historically unsupported.
2547extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2548 return JNI_ERR;
2549}
2550
Elliott Hughescdf53122011-08-19 15:46:09 -07002551class JII {
2552 public:
2553 static jint DestroyJavaVM(JavaVM* vm) {
2554 if (vm == NULL) {
2555 return JNI_ERR;
2556 } else {
2557 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2558 delete raw_vm->runtime;
2559 return JNI_OK;
2560 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002561 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002562
Elliott Hughescdf53122011-08-19 15:46:09 -07002563 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002564 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002565 }
2566
2567 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002568 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002569 }
2570
2571 static jint DetachCurrentThread(JavaVM* vm) {
2572 if (vm == NULL) {
2573 return JNI_ERR;
2574 } else {
2575 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2576 Runtime* runtime = raw_vm->runtime;
2577 runtime->DetachCurrentThread();
2578 return JNI_OK;
2579 }
2580 }
2581
2582 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2583 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2584 return JNI_EVERSION;
2585 }
2586 if (vm == NULL || env == NULL) {
2587 return JNI_ERR;
2588 }
2589 Thread* thread = Thread::Current();
2590 if (thread == NULL) {
2591 *env = NULL;
2592 return JNI_EDETACHED;
2593 }
2594 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002595 return JNI_OK;
2596 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002597};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002598
Elliott Hughesa2501992011-08-26 19:39:54 -07002599const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002600 NULL, // reserved0
2601 NULL, // reserved1
2602 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002603 JII::DestroyJavaVM,
2604 JII::AttachCurrentThread,
2605 JII::DetachCurrentThread,
2606 JII::GetEnv,
2607 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002608};
2609
Elliott Hughesbbd76712011-08-17 10:25:24 -07002610static const size_t kPinTableInitialSize = 16;
2611static const size_t kPinTableMaxSize = 1024;
2612
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002613static const size_t kGlobalsInitial = 512; // Arbitrary.
2614static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2615
2616static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2617static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2618
Elliott Hughesa0957642011-09-02 14:27:33 -07002619JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002620 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002621 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002622 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002623 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002624 verbose_jni(options->IsVerbose("jni")),
2625 log_third_party_jni(options->IsVerbose("third-party-jni")),
2626 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002627 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002628 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002629 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002630 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002631 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002632 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002633 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002634 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002635 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002636 functions = unchecked_functions = &gInvokeInterface;
2637 if (check_jni) {
2638 functions = GetCheckJniInvokeInterface();
2639 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002640}
2641
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002642JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002643 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002644}
2645
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002646void JavaVMExt::DumpReferenceTables() {
2647 {
2648 MutexLock mu(globals_lock);
2649 globals.Dump();
2650 }
2651 {
2652 MutexLock mu(weak_globals_lock);
2653 weak_globals.Dump();
2654 }
2655 {
2656 MutexLock mu(pins_lock);
2657 pin_table.Dump();
2658 }
2659}
2660
Elliott Hughes75770752011-08-24 17:52:38 -07002661bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2662 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002663
2664 // See if we've already loaded this library. If we have, and the class loader
2665 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002666 // TODO: for better results we should canonicalize the pathname (or even compare
2667 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002668 SharedLibrary* library;
2669 {
2670 // TODO: move the locking (and more of this logic) into Libraries.
2671 MutexLock mu(libraries_lock);
2672 library = libraries->Get(path);
2673 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002674 if (library != NULL) {
2675 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002676 // The library will be associated with class_loader. The JNI
2677 // spec says we can't load the same library into more than one
2678 // class loader.
2679 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2680 "ClassLoader %p; can't open in ClassLoader %p",
2681 path.c_str(), library->GetClassLoader(), class_loader);
2682 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002683 return false;
2684 }
2685 if (verbose_jni) {
2686 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2687 << "ClassLoader " << class_loader << "]";
2688 }
2689 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002690 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2691 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002692 return false;
2693 }
2694 return true;
2695 }
2696
2697 // Open the shared library. Because we're using a full path, the system
2698 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2699 // resolve this library's dependencies though.)
2700
2701 // Failures here are expected when java.library.path has several entries
2702 // and we have to hunt for the lib.
2703
2704 // The current version of the dynamic linker prints detailed information
2705 // about dlopen() failures. Some things to check if the message is
2706 // cryptic:
2707 // - make sure the library exists on the device
2708 // - verify that the right path is being opened (the debug log message
2709 // above can help with that)
2710 // - check to see if the library is valid (e.g. not zero bytes long)
2711 // - check config/prelink-linux-arm.map to ensure that the library
2712 // is listed and is not being overrun by the previous entry (if
2713 // loading suddenly stops working on a prelinked library, this is
2714 // a good one to check)
2715 // - write a trivial app that calls sleep() then dlopen(), attach
2716 // to it with "strace -p <pid>" while it sleeps, and watch for
2717 // attempts to open nonexistent dependent shared libs
2718
2719 // TODO: automate some of these checks!
2720
2721 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002722 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002723 // the GC to ignore us.
2724 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002725 void* handle = NULL;
2726 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002727 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002728 handle = dlopen(path.c_str(), RTLD_LAZY);
2729 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002730
2731 if (verbose_jni) {
2732 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2733 }
2734
2735 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002736 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002737 return false;
2738 }
2739
2740 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002741 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002742 // TODO: move the locking (and more of this logic) into Libraries.
2743 MutexLock mu(libraries_lock);
2744 library = libraries->Get(path);
2745 if (library != NULL) {
2746 LOG(INFO) << "WOW: we lost a race to add shared library: "
2747 << "\"" << path << "\" ClassLoader=" << class_loader;
2748 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002749 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002750 library = new SharedLibrary(path, handle, class_loader);
2751 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002752 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002753
2754 if (verbose_jni) {
2755 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2756 }
2757
2758 bool result = true;
2759 void* sym = dlsym(handle, "JNI_OnLoad");
2760 if (sym == NULL) {
2761 if (verbose_jni) {
2762 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2763 }
2764 } else {
2765 // Call JNI_OnLoad. We have to override the current class
2766 // loader, which will always be "null" since the stuff at the
2767 // top of the stack is around Runtime.loadLibrary(). (See
2768 // the comments in the JNI FindClass function.)
2769 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2770 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002771 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002772 self->SetClassLoaderOverride(class_loader);
2773
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002774 int version = 0;
2775 {
2776 ScopedThreadStateChange tsc(self, Thread::kNative);
2777 if (verbose_jni) {
2778 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2779 }
2780 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002781 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002782
2783 self->SetClassLoaderOverride(old_class_loader);;
2784
2785 if (version != JNI_VERSION_1_2 &&
2786 version != JNI_VERSION_1_4 &&
2787 version != JNI_VERSION_1_6) {
2788 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2789 << "bad version: " << version;
2790 // It's unwise to call dlclose() here, but we can mark it
2791 // as bad and ensure that future load attempts will fail.
2792 // We don't know how far JNI_OnLoad got, so there could
2793 // be some partially-initialized stuff accessible through
2794 // newly-registered native method calls. We could try to
2795 // unregister them, but that doesn't seem worthwhile.
2796 result = false;
2797 } else {
2798 if (verbose_jni) {
2799 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2800 << " from JNI_OnLoad in \"" << path << "\"]";
2801 }
2802 }
2803 }
2804
2805 library->SetResult(result);
2806 return result;
2807}
2808
2809void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2810 CHECK(m->IsNative());
2811
2812 Class* c = m->GetDeclaringClass();
2813
2814 // If this is a static method, it could be called before the class
2815 // has been initialized.
2816 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002817 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002818 return NULL;
2819 }
2820 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002821 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002822 }
2823
Brian Carlstrom16192862011-09-12 17:50:06 -07002824 std::string detail;
2825 void* native_method;
2826 {
2827 MutexLock mu(libraries_lock);
2828 native_method = libraries->FindNativeMethod(m, detail);
2829 }
2830 // throwing can cause libraries_lock to be reacquired
2831 if (native_method == NULL) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002832 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", "%s", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002833 }
2834 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002835}
2836
Elliott Hughes410c0c82011-09-01 17:58:25 -07002837void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2838 {
2839 MutexLock mu(globals_lock);
2840 globals.VisitRoots(visitor, arg);
2841 }
2842 {
2843 MutexLock mu(pins_lock);
2844 pin_table.VisitRoots(visitor, arg);
2845 }
2846 // The weak_globals table is visited by the GC itself (because it mutates the table).
2847}
2848
Ian Rogersdf20fe02011-07-20 20:34:16 -07002849} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002850
2851std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2852 switch (rhs) {
2853 case JNIInvalidRefType:
2854 os << "JNIInvalidRefType";
2855 return os;
2856 case JNILocalRefType:
2857 os << "JNILocalRefType";
2858 return os;
2859 case JNIGlobalRefType:
2860 os << "JNIGlobalRefType";
2861 return os;
2862 case JNIWeakGlobalRefType:
2863 os << "JNIWeakGlobalRefType";
2864 return os;
2865 }
2866}