blob: 9480644db5f88d08cb11230af1fb96a6087196bd [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
Elliott Hughes2ced6a52011-10-16 18:44:48 -070027static const size_t kMonitorsInitial = 32; // Arbitrary.
28static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
29
30static const size_t kLocalsInitial = 64; // Arbitrary.
31static const size_t kLocalsMax = 512; // Arbitrary sanity check.
32
33static const size_t kPinTableInitial = 16; // Arbitrary.
34static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
35
36static const size_t kGlobalsInitial = 512; // Arbitrary.
37static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
38
39static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
40static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
41
Ian Rogersdf20fe02011-07-20 20:34:16 -070042namespace art {
43
Elliott Hughesc5f7c912011-08-18 14:00:42 -070044/*
45 * Add a local reference for an object to the current stack frame. When
46 * the native function returns, the reference will be discarded.
47 *
48 * We need to allow the same reference to be added multiple times.
49 *
50 * This will be called on otherwise unreferenced objects. We cannot do
51 * GC allocations here, and it's best if we don't grab a mutex.
52 *
53 * Returns the local reference (currently just the same pointer that was
54 * passed in), or NULL on failure.
55 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070056template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070057T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
58 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
59 Object* obj = const_cast<Object*>(const_obj);
60
Elliott Hughesc5f7c912011-08-18 14:00:42 -070061 if (obj == NULL) {
62 return NULL;
63 }
64
Elliott Hughesbf86d042011-08-31 17:53:14 -070065 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
66 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070067
Ian Rogers5a7a74a2011-09-26 16:32:29 -070068 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070069 IndirectRef ref = locals.Add(cookie, obj);
70 if (ref == NULL) {
71 // TODO: just change Add's DCHECK to CHECK and lose this?
72 locals.Dump();
73 LOG(FATAL) << "Failed adding to JNI local reference table "
74 << "(has " << locals.Capacity() << " entries)";
75 // TODO: dvmDumpThread(dvmThreadSelf(), false);
76 }
77
78#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070079 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070080 size_t entry_count = locals.Capacity();
81 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070082 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070083 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070084 locals.Dump();
85 // TODO: dvmDumpThread(dvmThreadSelf(), false);
86 // dvmAbort();
87 }
88 }
89#endif
90
Elliott Hughesbf86d042011-08-31 17:53:14 -070091 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070092 // Hand out direct pointers to support broken old apps.
93 return reinterpret_cast<T>(obj);
94 }
95
96 return reinterpret_cast<T>(ref);
97}
98
Elliott Hughesbf86d042011-08-31 17:53:14 -070099// For external use.
100template<typename T>
101T Decode(JNIEnv* public_env, jobject obj) {
102 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
103 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
104}
105// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700106template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700107template Class* Decode<Class*>(JNIEnv*, jobject);
108template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
109template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700110template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700111template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700112template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700113template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400114template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700115template String* Decode<String*>(JNIEnv*, jobject);
116template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700117
118namespace {
119
Elliott Hughescdf53122011-08-19 15:46:09 -0700120jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
121 if (obj == NULL) {
122 return NULL;
123 }
Elliott Hughes75770752011-08-24 17:52:38 -0700124 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700125 IndirectReferenceTable& weak_globals = vm->weak_globals;
126 MutexLock mu(vm->weak_globals_lock);
127 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
128 return reinterpret_cast<jweak>(ref);
129}
130
Elliott Hughesbf86d042011-08-31 17:53:14 -0700131// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700132template<typename T>
133T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700134 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700135}
136
Elliott Hughes418d20f2011-09-22 14:00:39 -0700137byte* CreateArgArray(JNIEnv* public_env, Method* method, va_list ap) {
138 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700139 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700140 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700141 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700142 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
143 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700144 case 'Z':
145 case 'B':
146 case 'C':
147 case 'S':
148 case 'I':
149 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
150 offset += 4;
151 break;
152 case 'F':
153 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
154 offset += 4;
155 break;
156 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700157 Object* obj = Decode<Object*>(env, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700158 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
159 offset += sizeof(Object*);
160 break;
161 }
162 case 'D':
163 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
164 offset += 8;
165 break;
166 case 'J':
167 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
168 offset += 8;
169 break;
170 }
171 }
172 return arg_array.release();
173}
174
Elliott Hughes418d20f2011-09-22 14:00:39 -0700175byte* CreateArgArray(JNIEnv* public_env, Method* method, jvalue* args) {
176 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700177 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700178 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700179 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700180 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
181 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700182 case 'Z':
183 case 'B':
184 case 'C':
185 case 'S':
186 case 'I':
187 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
188 offset += 4;
189 break;
190 case 'F':
191 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
192 offset += 4;
193 break;
194 case 'L': {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700195 Object* obj = Decode<Object*>(env, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700196 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
197 offset += sizeof(Object*);
198 break;
199 }
200 case 'D':
201 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
202 offset += 8;
203 break;
204 case 'J':
205 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
206 offset += 8;
207 break;
208 }
209 }
210 return arg_array.release();
211}
212
Elliott Hughes418d20f2011-09-22 14:00:39 -0700213JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, byte* args) {
214 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700215 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700216 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700217 return result;
218}
219
Elliott Hughes418d20f2011-09-22 14:00:39 -0700220JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
221 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
222 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700223 Method* method = DecodeMethod(mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700224 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
225 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700226}
227
Elliott Hughes75770752011-08-24 17:52:38 -0700228Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700229 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700230}
231
Elliott Hughes418d20f2011-09-22 14:00:39 -0700232JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
233 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
234 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700235 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700236 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
237 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700238}
239
Elliott Hughes418d20f2011-09-22 14:00:39 -0700240JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
241 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
242 Object* receiver = Decode<Object*>(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700243 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700244 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
245 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700246}
247
Elliott Hughes6b436852011-08-12 10:16:44 -0700248// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
249// separated with slashes but aren't wrapped with "L;" like regular descriptors
250// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
251// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
252// supported names with dots too (such as "a.b.C").
253std::string NormalizeJniClassDescriptor(const char* name) {
254 std::string result;
255 // Add the missing "L;" if necessary.
256 if (name[0] == '[') {
257 result = name;
258 } else {
259 result += 'L';
260 result += name;
261 result += ';';
262 }
263 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700264 if (result.find('.') != std::string::npos) {
265 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
266 << "\"" << name << "\"";
267 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700268 }
269 return result;
270}
271
Elliott Hughes14134a12011-09-30 16:55:51 -0700272void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
273 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700274 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes14134a12011-09-30 16:55:51 -0700275 "no %s method \"%s.%s%s\"", kind, class_descriptor.c_str(), name, sig);
276}
277
Elliott Hughescdf53122011-08-19 15:46:09 -0700278jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
279 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700280 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700281 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700282 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700283
284 Method* method = NULL;
285 if (is_static) {
286 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700287 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700288 method = c->FindVirtualMethod(name, sig);
289 if (method == NULL) {
290 // No virtual method matching the signature. Search declared
291 // private methods and constructors.
292 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700293 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700294 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700295
Elliott Hughescdf53122011-08-19 15:46:09 -0700296 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700297 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700298 return NULL;
299 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700300
Elliott Hughes418d20f2011-09-22 14:00:39 -0700301 method->InitJavaFields();
302
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700303 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700304}
305
Elliott Hughescdf53122011-08-19 15:46:09 -0700306jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
307 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700308 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700309 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700310 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700311
312 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700313 Class* field_type;
314 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
315 if (sig[1] != '\0') {
316 // TODO: need to get the appropriate ClassLoader.
317 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
318 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700319 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700321 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700322 if (field_type == NULL) {
323 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700324 DCHECK(ts.Self()->IsExceptionPending());
325 ts.Self()->ClearException();
326 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700327 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700328 "no type \"%s\" found and so no field \"%s\" could be found in class "
329 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700330 return NULL;
331 }
332 if (is_static) {
333 field = c->FindStaticField(name, field_type);
334 } else {
335 field = c->FindInstanceField(name, field_type);
336 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700337 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700338 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700339 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700340 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700341 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700342 return NULL;
343 }
Elliott Hughes80609252011-09-23 17:24:51 -0700344 field->InitJavaFields();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700345 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700346}
347
Elliott Hughes75770752011-08-24 17:52:38 -0700348void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
349 JavaVMExt* vm = ts.Vm();
350 MutexLock mu(vm->pins_lock);
351 vm->pin_table.Add(array);
352}
353
354void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
355 JavaVMExt* vm = ts.Vm();
356 MutexLock mu(vm->pins_lock);
357 vm->pin_table.Remove(array);
358}
359
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700360template<typename JniT, typename ArtT>
361JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
362 CHECK_GE(length, 0); // TODO: ReportJniError
363 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700364 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700365}
366
Elliott Hughes75770752011-08-24 17:52:38 -0700367template <typename ArrayT, typename CArrayT, typename ArtArrayT>
368CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
369 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
370 PinPrimitiveArray(ts, array);
371 if (is_copy != NULL) {
372 *is_copy = JNI_FALSE;
373 }
374 return array->GetData();
375}
376
377template <typename ArrayT>
378void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
379 if (mode != JNI_COMMIT) {
380 Array* array = Decode<Array*>(ts, java_array);
381 UnpinPrimitiveArray(ts, array);
382 }
383}
384
Elliott Hughes814e4032011-08-23 12:07:56 -0700385void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700386 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700387 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700388 "%s offset=%d length=%d %s.length=%d",
389 type.c_str(), start, length, identifier, array->GetLength());
390}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700391void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700392 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700393 "offset=%d length=%d string.length()=%d", start, length, array_length);
394}
Elliott Hughes814e4032011-08-23 12:07:56 -0700395
396template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700397void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700398 ArrayT* array = Decode<ArrayT*>(ts, java_array);
399 if (start < 0 || length < 0 || start + length > array->GetLength()) {
400 ThrowAIOOBE(ts, array, start, length, "src");
401 } else {
402 JavaT* data = array->GetData();
403 memcpy(buf, data + start, length * sizeof(JavaT));
404 }
405}
406
407template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700408void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700409 ArrayT* array = Decode<ArrayT*>(ts, java_array);
410 if (start < 0 || length < 0 || start + length > array->GetLength()) {
411 ThrowAIOOBE(ts, array, start, length, "dst");
412 } else {
413 JavaT* data = array->GetData();
414 memcpy(data + start, buf, length * sizeof(JavaT));
415 }
416}
417
Elliott Hughes75770752011-08-24 17:52:38 -0700418jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700419 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
420 CHECK(buffer_class.get() != NULL);
421 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
422}
423
Elliott Hughes75770752011-08-24 17:52:38 -0700424jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700425 static jclass buffer_class = InitDirectByteBufferClass(env);
426 return buffer_class;
427}
428
Elliott Hughes75770752011-08-24 17:52:38 -0700429jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
430 if (vm == NULL || p_env == NULL) {
431 return JNI_ERR;
432 }
433
434 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
435 JavaVMAttachArgs args;
436 if (thr_args == NULL) {
437 // Allow the v1.1 calling convention.
438 args.version = JNI_VERSION_1_2;
439 args.name = NULL;
440 args.group = NULL; // TODO: get "main" thread group
441 } else {
442 args.version = in_args->version;
443 args.name = in_args->name;
444 if (in_args->group != NULL) {
445 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
446 args.group = NULL; // TODO: decode in_args->group
447 } else {
448 args.group = NULL; // TODO: get "main" thread group
449 }
450 }
451 CHECK_GE(args.version, JNI_VERSION_1_2);
452
453 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700454 runtime->AttachCurrentThread(args.name, as_daemon);
455 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700456 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700457}
458
Elliott Hughes79082e32011-08-25 12:07:32 -0700459class SharedLibrary {
460 public:
461 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
462 : path_(path),
463 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700464 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700465 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700466 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700467 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700468 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700469 }
470
Elliott Hughes79082e32011-08-25 12:07:32 -0700471 Object* GetClassLoader() {
472 return class_loader_;
473 }
474
475 std::string GetPath() {
476 return path_;
477 }
478
479 /*
480 * Check the result of an earlier call to JNI_OnLoad on this library. If
481 * the call has not yet finished in another thread, wait for it.
482 */
483 bool CheckOnLoadResult(JavaVMExt* vm) {
484 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700485 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700486 // Check this so we don't end up waiting for ourselves. We need
487 // to return "true" so the caller can continue.
488 LOG(INFO) << *self << " recursive attempt to load library "
489 << "\"" << path_ << "\"";
490 return true;
491 }
492
493 MutexLock mu(jni_on_load_lock_);
494 while (jni_on_load_result_ == kPending) {
495 if (vm->verbose_jni) {
496 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
497 << "JNI_OnLoad...]";
498 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700499 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700500 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700501 }
502
503 bool okay = (jni_on_load_result_ == kOkay);
504 if (vm->verbose_jni) {
505 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
506 << (okay ? "succeeded" : "failed") << "]";
507 }
508 return okay;
509 }
510
511 void SetResult(bool result) {
512 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700513 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700514
515 // Broadcast a wakeup to anybody sleeping on the condition variable.
516 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700517 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700518 }
519
520 void* FindSymbol(const std::string& symbol_name) {
521 return dlsym(handle_, symbol_name.c_str());
522 }
523
524 private:
525 enum JNI_OnLoadState {
526 kPending,
527 kFailed,
528 kOkay,
529 };
530
531 // Path to library "/system/lib/libjni.so".
532 std::string path_;
533
534 // The void* returned by dlopen(3).
535 void* handle_;
536
537 // The ClassLoader this library is associated with.
538 Object* class_loader_;
539
540 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700541 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700542 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700543 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700544 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700545 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 // Result of earlier JNI_OnLoad call.
547 JNI_OnLoadState jni_on_load_result_;
548};
549
Elliott Hughescdf53122011-08-19 15:46:09 -0700550} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700551
Elliott Hughes79082e32011-08-25 12:07:32 -0700552// This exists mainly to keep implementation details out of the header file.
553class Libraries {
554 public:
555 Libraries() {
556 }
557
558 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700559 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700560 }
561
562 SharedLibrary* Get(const std::string& path) {
563 return libraries_[path];
564 }
565
566 void Put(const std::string& path, SharedLibrary* library) {
567 libraries_[path] = library;
568 }
569
570 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700571 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700572 std::string jni_short_name(JniShortName(m));
573 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700574 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700575 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
576 SharedLibrary* library = it->second;
577 if (library->GetClassLoader() != declaring_class_loader) {
578 // We only search libraries loaded by the appropriate ClassLoader.
579 continue;
580 }
581 // Try the short name then the long name...
582 void* fn = library->FindSymbol(jni_short_name);
583 if (fn == NULL) {
584 fn = library->FindSymbol(jni_long_name);
585 }
586 if (fn != NULL) {
587 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700588 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700589 << " in \"" << library->GetPath() << "\"]";
590 }
591 return fn;
592 }
593 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700594 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700595 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700596 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700597 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700598 return NULL;
599 }
600
601 private:
602 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
603
604 std::map<std::string, SharedLibrary*> libraries_;
605};
606
Elliott Hughes418d20f2011-09-22 14:00:39 -0700607JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
608 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
609 Object* receiver = Decode<Object*>(env, obj);
610 Method* method = DecodeMethod(mid);
611 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
612 return InvokeWithArgArray(env, receiver, method, arg_array.get());
613}
614
Elliott Hughescdf53122011-08-19 15:46:09 -0700615class JNI {
616 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700617
Elliott Hughescdf53122011-08-19 15:46:09 -0700618 static jint GetVersion(JNIEnv* env) {
619 ScopedJniThreadState ts(env);
620 return JNI_VERSION_1_6;
621 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700622
Elliott Hughescdf53122011-08-19 15:46:09 -0700623 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
624 ScopedJniThreadState ts(env);
625 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700626 return NULL;
627 }
628
Elliott Hughescdf53122011-08-19 15:46:09 -0700629 static jclass FindClass(JNIEnv* env, const char* name) {
630 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700631 Runtime* runtime = Runtime::Current();
632 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700633 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700634 Class* c = NULL;
635 if (runtime->IsStarted()) {
636 // TODO: need to get the appropriate ClassLoader.
637 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
638 c = class_linker->FindClass(descriptor, cl);
639 } else {
640 c = class_linker->FindSystemClass(descriptor);
641 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700642 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700643 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700644
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
646 ScopedJniThreadState ts(env);
647 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700648 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700649 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700650
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
652 ScopedJniThreadState ts(env);
653 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700654 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700656
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
658 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700659 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700660 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700662
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
664 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700665 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700666 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700668
Elliott Hughes37f7a402011-08-22 18:56:01 -0700669 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
670 ScopedJniThreadState ts(env);
671 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700672 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700673 }
674
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700675 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700677 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700678 return AddLocalReference<jclass>(env, c->GetSuperClass());
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 jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700683 Class* c1 = Decode<Class*>(ts, java_class1);
684 Class* c2 = Decode<Class*>(ts, java_class2);
685 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700687
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700690 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700691 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700692 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700693 return JNI_TRUE;
694 } else {
695 Object* obj = Decode<Object*>(ts, jobj);
696 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700697 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700700
Elliott Hughes37f7a402011-08-22 18:56:01 -0700701 static jint Throw(JNIEnv* env, jthrowable java_exception) {
702 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700703 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700704 if (exception == NULL) {
705 return JNI_ERR;
706 }
707 ts.Self()->SetException(exception);
708 return JNI_OK;
709 }
710
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700711 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700712 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700713 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700714 jmethodID mid = ((msg != NULL)
715 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
716 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700717 if (mid == NULL) {
718 return JNI_ERR;
719 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700720 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700721 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700722 return JNI_ERR;
723 }
724
725 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700726 args[0].l = s.get();
727 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
728 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700729 return JNI_ERR;
730 }
731
Elliott Hughes72025e52011-08-23 17:50:30 -0700732 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700733
Elliott Hughes37f7a402011-08-22 18:56:01 -0700734 return JNI_OK;
735 }
736
737 static jboolean ExceptionCheck(JNIEnv* env) {
738 ScopedJniThreadState ts(env);
739 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
740 }
741
742 static void ExceptionClear(JNIEnv* env) {
743 ScopedJniThreadState ts(env);
744 ts.Self()->ClearException();
745 }
746
747 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700749
750 Thread* self = ts.Self();
751 Throwable* original_exception = self->GetException();
752 self->ClearException();
753
Elliott Hughesbf86d042011-08-31 17:53:14 -0700754 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700755 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
756 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
757 if (mid == NULL) {
758 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700759 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700760 } else {
761 env->CallVoidMethod(exception.get(), mid);
762 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700763 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700764 << " thrown while calling printStackTrace";
765 self->ClearException();
766 }
767 }
768
769 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700770 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700771
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 static jthrowable ExceptionOccurred(JNIEnv* env) {
773 ScopedJniThreadState ts(env);
774 Object* exception = ts.Self()->GetException();
775 if (exception == NULL) {
776 return NULL;
777 } else {
778 // TODO: if adding a local reference failing causes the VM to abort
779 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700780 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700781 if (localException == NULL) {
782 // We were unable to add a new local reference, and threw a new
783 // exception. We can't return "exception", because it's not a
784 // local reference. So we have to return NULL, indicating that
785 // there was no exception, even though it's pretty much raining
786 // exceptions in here.
787 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
788 }
789 return localException;
790 }
791 }
792
Elliott Hughescdf53122011-08-19 15:46:09 -0700793 static void FatalError(JNIEnv* env, const char* msg) {
794 ScopedJniThreadState ts(env);
795 LOG(FATAL) << "JNI FatalError called: " << msg;
796 }
797
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700798 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700799 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700800 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
801 return JNI_ERR;
802 }
803 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 return JNI_OK;
805 }
806
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700807 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700808 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700809 Object* survivor = Decode<Object*>(ts, java_survivor);
810 ts.Env()->PopFrame();
811 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 }
813
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700814 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700815 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700816 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
817 }
818
819 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
820 // TODO: we should try to expand the table if necessary.
821 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
822 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
823 return JNI_ERR;
824 }
825 // TODO: this isn't quite right, since "capacity" includes holes.
826 size_t capacity = ts.Env()->locals.Capacity();
827 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
828 if (!okay) {
829 ts.Self()->ThrowOutOfMemoryError(caller);
830 }
831 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700832 }
833
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
835 ScopedJniThreadState ts(env);
836 if (obj == NULL) {
837 return NULL;
838 }
839
Elliott Hughes75770752011-08-24 17:52:38 -0700840 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 IndirectReferenceTable& globals = vm->globals;
842 MutexLock mu(vm->globals_lock);
843 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
844 return reinterpret_cast<jobject>(ref);
845 }
846
847 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
848 ScopedJniThreadState ts(env);
849 if (obj == NULL) {
850 return;
851 }
852
Elliott Hughes75770752011-08-24 17:52:38 -0700853 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 IndirectReferenceTable& globals = vm->globals;
855 MutexLock mu(vm->globals_lock);
856
857 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
858 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
859 << "failed to find entry";
860 }
861 }
862
863 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
864 ScopedJniThreadState ts(env);
865 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
866 }
867
868 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
869 ScopedJniThreadState ts(env);
870 if (obj == NULL) {
871 return;
872 }
873
Elliott Hughes75770752011-08-24 17:52:38 -0700874 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 IndirectReferenceTable& weak_globals = vm->weak_globals;
876 MutexLock mu(vm->weak_globals_lock);
877
878 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
879 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
880 << "failed to find entry";
881 }
882 }
883
884 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
885 ScopedJniThreadState ts(env);
886 if (obj == NULL) {
887 return NULL;
888 }
889
890 IndirectReferenceTable& locals = ts.Env()->locals;
891
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700892 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
894 return reinterpret_cast<jobject>(ref);
895 }
896
897 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
898 ScopedJniThreadState ts(env);
899 if (obj == NULL) {
900 return;
901 }
902
903 IndirectReferenceTable& locals = ts.Env()->locals;
904
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700905 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 if (!locals.Remove(cookie, obj)) {
907 // Attempting to delete a local reference that is not in the
908 // topmost local reference frame is a no-op. DeleteLocalRef returns
909 // void and doesn't throw any exceptions, but we should probably
910 // complain about it so the user will notice that things aren't
911 // going quite the way they expect.
912 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
913 << "failed to find entry";
914 }
915 }
916
917 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
918 ScopedJniThreadState ts(env);
919 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
920 ? JNI_TRUE : JNI_FALSE;
921 }
922
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700923 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700925 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700926 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700927 return NULL;
928 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700929 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 ScopedJniThreadState ts(env);
934 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700935 va_start(args, mid);
936 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 va_end(args);
938 return result;
939 }
940
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700943 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700944 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700945 return NULL;
946 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700947 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700948 if (result == NULL) {
949 return NULL;
950 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700951 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 return local_result;
954 }
955
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700958 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700959 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700960 return NULL;
961 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700962 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700963 if (result == NULL) {
964 return NULL;
965 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700966 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 return local_result;
969 }
970
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
972 ScopedJniThreadState ts(env);
973 return FindMethodID(ts, c, name, sig, false);
974 }
975
976 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
977 ScopedJniThreadState ts(env);
978 return FindMethodID(ts, c, name, sig, true);
979 }
980
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 va_list ap;
984 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700985 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700987 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700992 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700993 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700998 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700999 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 va_list ap;
1005 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001006 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 va_end(ap);
1008 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 }
1010
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001013 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001018 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 va_list ap;
1024 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001025 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 va_end(ap);
1027 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001032 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001037 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 va_list ap;
1043 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001044 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 va_end(ap);
1046 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 }
1048
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001051 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001056 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 va_list ap;
1062 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001063 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 va_end(ap);
1065 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001070 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001075 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 va_list ap;
1081 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001082 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 va_end(ap);
1084 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001089 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001094 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 va_list ap;
1100 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001101 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 va_end(ap);
1103 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001108 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001113 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 va_list ap;
1119 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001120 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 va_end(ap);
1122 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001127 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001132 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 va_list ap;
1138 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001139 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 va_end(ap);
1141 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001146 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001151 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 }
1153
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 va_list ap;
1157 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001158 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 }
1161
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001164 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001169 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 }
1171
1172 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 ScopedJniThreadState ts(env);
1175 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001177 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001178 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 va_end(ap);
1180 return local_result;
1181 }
1182
1183 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001186 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001187 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
1190 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001193 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001194 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
1197 static jboolean CallNonvirtualBooleanMethod(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.z;
1205 }
1206
1207 static jboolean CallNonvirtualBooleanMethodV(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).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
1213 static jboolean CallNonvirtualBooleanMethodA(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).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 }
1218
1219 static jbyte CallNonvirtualByteMethod(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.b;
1227 }
1228
1229 static jbyte CallNonvirtualByteMethodV(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).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 }
1234
1235 static jbyte CallNonvirtualByteMethodA(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).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
1241 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001242 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 ScopedJniThreadState ts(env);
1244 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001246 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 va_end(ap);
1248 return result.c;
1249 }
1250
1251 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001254 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 }
1256
1257 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001258 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001260 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 }
1262
1263 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001264 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 ScopedJniThreadState ts(env);
1266 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001268 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 va_end(ap);
1270 return result.s;
1271 }
1272
1273 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001276 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 }
1278
1279 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001280 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001282 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 }
1284
Elliott Hughes418d20f2011-09-22 14:00:39 -07001285 static jint CallNonvirtualIntMethod(JNIEnv* env, 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.i;
1292 }
1293
1294 static jint CallNonvirtualIntMethodV(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).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
1300 static jint CallNonvirtualIntMethodA(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).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
1306 static jlong CallNonvirtualLongMethod(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.j;
1314 }
1315
1316 static jlong CallNonvirtualLongMethodV(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).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
1322 static jlong CallNonvirtualLongMethodA(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).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
1328 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001329 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 ScopedJniThreadState ts(env);
1331 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001333 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 va_end(ap);
1335 return result.f;
1336 }
1337
1338 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001339 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001341 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
1344 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001347 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
1350 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001351 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 ScopedJniThreadState ts(env);
1353 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001355 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 va_end(ap);
1357 return result.d;
1358 }
1359
1360 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001361 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001363 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 }
1365
1366 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001367 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001369 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
1372 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001373 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 ScopedJniThreadState ts(env);
1375 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001376 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001377 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 va_end(ap);
1379 }
1380
1381 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001382 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001384 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
1386
1387 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001388 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001390 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
1392
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001393 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 ScopedJniThreadState ts(env);
1395 return FindFieldID(ts, c, name, sig, false);
1396 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001397
1398
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001399 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 ScopedJniThreadState ts(env);
1401 return FindFieldID(ts, c, name, sig, true);
1402 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001403
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001406 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001407 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001408 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001410
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001411 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001413 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001414 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
1416
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001417 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001419 Object* o = Decode<Object*>(ts, java_object);
1420 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001421 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001422 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001425 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001427 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001428 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001429 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432#define GET_PRIMITIVE_FIELD(fn, instance) \
1433 ScopedJniThreadState ts(env); \
1434 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001435 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 return f->fn(o)
1437
1438#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1439 ScopedJniThreadState ts(env); \
1440 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001441 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 f->fn(o, value)
1443
1444 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1445 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1449 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1453 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1457 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1461 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1465 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001468 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1469 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 }
1471
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001472 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1473 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1477 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1481 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1485 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 }
1487
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1489 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 }
1491
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1493 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1497 GET_PRIMITIVE_FIELD(GetLong, NULL);
1498 }
1499
1500 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1501 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1502 }
1503
1504 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1505 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1506 }
1507
1508 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1509 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1510 }
1511
1512 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1513 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1514 }
1515
1516 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1517 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1518 }
1519
1520 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1521 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1522 }
1523
1524 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1525 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1526 }
1527
1528 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1529 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1530 }
1531
1532 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1533 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1534 }
1535
1536 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1537 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1538 }
1539
1540 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1541 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1542 }
1543
1544 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1545 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1546 }
1547
1548 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1549 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1550 }
1551
1552 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1553 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1554 }
1555
1556 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1557 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1558 }
1559
1560 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1561 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1562 }
1563
1564 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1565 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1566 }
1567
1568 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1569 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes418d20f2011-09-22 14:00:39 -07001572 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 ScopedJniThreadState ts(env);
1574 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001575 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001576 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001577 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 va_end(ap);
1579 return local_result;
1580 }
1581
Elliott Hughes418d20f2011-09-22 14:00:39 -07001582 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001584 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001585 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
Elliott Hughes418d20f2011-09-22 14:00:39 -07001588 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001590 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001591 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes418d20f2011-09-22 14:00:39 -07001594 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 ScopedJniThreadState ts(env);
1596 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001597 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001598 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 va_end(ap);
1600 return result.z;
1601 }
1602
Elliott Hughes418d20f2011-09-22 14:00:39 -07001603 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001605 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes418d20f2011-09-22 14:00:39 -07001608 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001610 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 ScopedJniThreadState ts(env);
1615 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001616 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001617 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 va_end(ap);
1619 return result.b;
1620 }
1621
Elliott Hughes418d20f2011-09-22 14:00:39 -07001622 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001624 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 }
1626
Elliott Hughes418d20f2011-09-22 14:00:39 -07001627 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001629 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes72025e52011-08-23 17:50:30 -07001632 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 ScopedJniThreadState ts(env);
1634 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001635 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001636 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 va_end(ap);
1638 return result.c;
1639 }
1640
Elliott Hughes418d20f2011-09-22 14:00:39 -07001641 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001643 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
1645
Elliott Hughes418d20f2011-09-22 14:00:39 -07001646 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001648 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 ScopedJniThreadState ts(env);
1653 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001654 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001655 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 va_end(ap);
1657 return result.s;
1658 }
1659
Elliott Hughes418d20f2011-09-22 14:00:39 -07001660 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001662 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 }
1664
Elliott Hughes418d20f2011-09-22 14:00:39 -07001665 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001667 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 }
1669
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 ScopedJniThreadState ts(env);
1672 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001673 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001674 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 va_end(ap);
1676 return result.i;
1677 }
1678
Elliott Hughes418d20f2011-09-22 14:00:39 -07001679 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001681 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 }
1683
Elliott Hughes418d20f2011-09-22 14:00:39 -07001684 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001686 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 ScopedJniThreadState ts(env);
1691 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001692 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001693 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 va_end(ap);
1695 return result.j;
1696 }
1697
Elliott Hughes418d20f2011-09-22 14:00:39 -07001698 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001700 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 }
1702
Elliott Hughes418d20f2011-09-22 14:00:39 -07001703 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001705 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 }
1707
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 ScopedJniThreadState ts(env);
1710 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001711 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001712 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 va_end(ap);
1714 return result.f;
1715 }
1716
Elliott Hughes418d20f2011-09-22 14:00:39 -07001717 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001719 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 }
1721
Elliott Hughes418d20f2011-09-22 14:00:39 -07001722 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001724 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
Elliott Hughes72025e52011-08-23 17:50:30 -07001727 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 ScopedJniThreadState ts(env);
1729 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001730 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001731 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 va_end(ap);
1733 return result.d;
1734 }
1735
Elliott Hughes418d20f2011-09-22 14:00:39 -07001736 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001738 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 }
1740
Elliott Hughes418d20f2011-09-22 14:00:39 -07001741 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001743 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
Elliott Hughes72025e52011-08-23 17:50:30 -07001746 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 ScopedJniThreadState ts(env);
1748 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001749 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001750 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 va_end(ap);
1752 }
1753
Elliott Hughes418d20f2011-09-22 14:00:39 -07001754 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001756 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 }
1758
Elliott Hughes418d20f2011-09-22 14:00:39 -07001759 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001761 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 }
1763
Elliott Hughes814e4032011-08-23 12:07:56 -07001764 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001766 if (chars == NULL && char_count == 0) {
1767 return NULL;
1768 }
1769 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001770 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
1773 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1774 ScopedJniThreadState ts(env);
1775 if (utf == NULL) {
1776 return NULL;
1777 }
1778 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001779 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 }
1781
Elliott Hughes814e4032011-08-23 12:07:56 -07001782 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1783 ScopedJniThreadState ts(env);
1784 return Decode<String*>(ts, java_string)->GetLength();
1785 }
1786
1787 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1788 ScopedJniThreadState ts(env);
1789 return Decode<String*>(ts, java_string)->GetUtfLength();
1790 }
1791
Elliott Hughesb465ab02011-08-24 11:21:21 -07001792 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001794 String* s = Decode<String*>(ts, java_string);
1795 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1796 ThrowSIOOBE(ts, start, length, s->GetLength());
1797 } else {
1798 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1799 memcpy(buf, chars + start, length * sizeof(jchar));
1800 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001801 }
1802
Elliott Hughesb465ab02011-08-24 11:21:21 -07001803 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001804 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001805 String* s = Decode<String*>(ts, java_string);
1806 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1807 ThrowSIOOBE(ts, start, length, s->GetLength());
1808 } else {
1809 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1810 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1811 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001812 }
1813
Elliott Hughes75770752011-08-24 17:52:38 -07001814 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001815 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001816 String* s = Decode<String*>(ts, java_string);
1817 const CharArray* chars = s->GetCharArray();
1818 PinPrimitiveArray(ts, chars);
1819 if (is_copy != NULL) {
1820 *is_copy = JNI_FALSE;
1821 }
1822 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001823 }
1824
Elliott Hughes75770752011-08-24 17:52:38 -07001825 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001826 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001827 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
Elliott Hughes75770752011-08-24 17:52:38 -07001830 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001832 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 }
1834
Elliott Hughes75770752011-08-24 17:52:38 -07001835 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001837 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 }
1839
Elliott Hughes75770752011-08-24 17:52:38 -07001840 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001841 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001842 if (java_string == NULL) {
1843 return NULL;
1844 }
1845 if (is_copy != NULL) {
1846 *is_copy = JNI_TRUE;
1847 }
1848 String* s = Decode<String*>(ts, java_string);
1849 size_t byte_count = s->GetUtfLength();
1850 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001851 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001852 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1853 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1854 bytes[byte_count] = '\0';
1855 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001856 }
1857
Elliott Hughes75770752011-08-24 17:52:38 -07001858 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001859 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001860 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001861 }
1862
Elliott Hughesbd935992011-08-22 11:59:34 -07001863 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001865 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001866 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001867 Array* array = obj->AsArray();
1868 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
Elliott Hughes814e4032011-08-23 12:07:56 -07001871 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001873 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001874 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 }
1876
1877 static void SetObjectArrayElement(JNIEnv* env,
1878 jobjectArray java_array, jsize index, jobject java_value) {
1879 ScopedJniThreadState ts(env);
1880 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1881 Object* value = Decode<Object*>(ts, java_value);
1882 array->Set(index, value);
1883 }
1884
1885 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1886 ScopedJniThreadState ts(env);
1887 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1888 }
1889
1890 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1891 ScopedJniThreadState ts(env);
1892 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1893 }
1894
1895 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1896 ScopedJniThreadState ts(env);
1897 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1898 }
1899
1900 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1901 ScopedJniThreadState ts(env);
1902 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1903 }
1904
1905 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1906 ScopedJniThreadState ts(env);
1907 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1908 }
1909
1910 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1911 ScopedJniThreadState ts(env);
1912 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1913 }
1914
1915 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1916 ScopedJniThreadState ts(env);
1917 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1918 }
1919
1920 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1921 ScopedJniThreadState ts(env);
1922 CHECK_GE(length, 0); // TODO: ReportJniError
1923
1924 // Compute the array class corresponding to the given element class.
1925 Class* element_class = Decode<Class*>(ts, element_jclass);
1926 std::string descriptor;
1927 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001928 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001929
1930 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001931 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1932 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 return NULL;
1934 }
1935
Elliott Hughes75770752011-08-24 17:52:38 -07001936 // Allocate and initialize if necessary.
1937 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001939 if (initial_element != NULL) {
1940 Object* initial_object = Decode<Object*>(ts, initial_element);
1941 for (jsize i = 0; i < length; ++i) {
1942 result->Set(i, initial_object);
1943 }
1944 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001945 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 }
1947
1948 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1949 ScopedJniThreadState ts(env);
1950 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1951 }
1952
Elliott Hughes75770752011-08-24 17:52:38 -07001953 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001954 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001955 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001956 }
1957
Elliott Hughes75770752011-08-24 17:52:38 -07001958 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001959 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001960 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001961 }
1962
Elliott Hughes75770752011-08-24 17:52:38 -07001963 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001965 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Elliott Hughes75770752011-08-24 17:52:38 -07001968 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001970 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Elliott Hughes75770752011-08-24 17:52:38 -07001973 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001975 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Elliott Hughes75770752011-08-24 17:52:38 -07001978 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001980 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Elliott Hughes75770752011-08-24 17:52:38 -07001983 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001985 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Elliott Hughes75770752011-08-24 17:52:38 -07001988 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001990 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001995 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes75770752011-08-24 17:52:38 -07001998 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002000 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes75770752011-08-24 17:52:38 -07002003 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002005 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002010 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002015 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes75770752011-08-24 17:52:38 -07002018 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002020 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes75770752011-08-24 17:52:38 -07002023 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002025 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes75770752011-08-24 17:52:38 -07002028 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002030 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes75770752011-08-24 17:52:38 -07002033 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002035 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes75770752011-08-24 17:52:38 -07002038 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002040 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes814e4032011-08-23 12:07:56 -07002043 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002045 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002070 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes5174fe62011-08-23 15:12:35 -07002123 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002125 Class* c = Decode<Class*>(ts, java_class);
2126
Elliott Hughes5174fe62011-08-23 15:12:35 -07002127 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 const char* name = methods[i].name;
2129 const char* sig = methods[i].signature;
2130
2131 if (*sig == '!') {
2132 // TODO: fast jni. it's too noisy to log all these.
2133 ++sig;
2134 }
2135
Elliott Hughes5174fe62011-08-23 15:12:35 -07002136 Method* m = c->FindDirectMethod(name, sig);
2137 if (m == NULL) {
2138 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002140 if (m == NULL) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002141 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143 } else if (!m->IsNative()) {
Elliott Hughes14134a12011-09-30 16:55:51 -07002144 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 return JNI_ERR;
2146 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002147
Elliott Hughes75770752011-08-24 17:52:38 -07002148 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002149 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150 }
2151
2152 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154 return JNI_OK;
2155 }
2156
Elliott Hughes5174fe62011-08-23 15:12:35 -07002157 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002159 Class* c = Decode<Class*>(ts, java_class);
2160
Elliott Hughes75770752011-08-24 17:52:38 -07002161 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002162 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002163 }
2164
2165 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2166 Method* m = c->GetDirectMethod(i);
2167 if (m->IsNative()) {
2168 m->UnregisterNative();
2169 }
2170 }
2171 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2172 Method* m = c->GetVirtualMethod(i);
2173 if (m->IsNative()) {
2174 m->UnregisterNative();
2175 }
2176 }
2177
2178 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
2180
Elliott Hughes72025e52011-08-23 17:50:30 -07002181 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002183 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002184 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Elliott Hughes72025e52011-08-23 17:50:30 -07002187 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002189 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002190 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
2193 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2194 ScopedJniThreadState ts(env);
2195 Runtime* runtime = Runtime::Current();
2196 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002197 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 } else {
2199 *vm = NULL;
2200 }
2201 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2202 }
2203
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2205 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002206
2207 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002208 CHECK(address != NULL); // TODO: ReportJniError
2209 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002210
2211 jclass buffer_class = GetDirectByteBufferClass(env);
2212 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2213 if (mid == NULL) {
2214 return NULL;
2215 }
2216
2217 // At the moment, the Java side is limited to 32 bits.
2218 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2219 CHECK_LE(capacity, 0xffffffff);
2220 jint address_arg = reinterpret_cast<jint>(address);
2221 jint capacity_arg = static_cast<jint>(capacity);
2222
2223 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2224 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002225 }
2226
Elliott Hughesb465ab02011-08-24 11:21:21 -07002227 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002229 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2230 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 }
2232
Elliott Hughesb465ab02011-08-24 11:21:21 -07002233 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002235 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2236 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 }
2238
Elliott Hughesb465ab02011-08-24 11:21:21 -07002239 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002240 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002241
Elliott Hughes75770752011-08-24 17:52:38 -07002242 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002243
2244 // Do we definitely know what kind of reference this is?
2245 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2246 IndirectRefKind kind = GetIndirectRefKind(ref);
2247 switch (kind) {
2248 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002249 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2250 return JNILocalRefType;
2251 }
2252 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002253 case kGlobal:
2254 return JNIGlobalRefType;
2255 case kWeakGlobal:
2256 return JNIWeakGlobalRefType;
2257 case kSirtOrInvalid:
2258 // Is it in a stack IRT?
2259 if (ts.Self()->SirtContains(java_object)) {
2260 return JNILocalRefType;
2261 }
2262
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002263 if (!ts.Env()->work_around_app_jni_bugs) {
2264 return JNIInvalidRefType;
2265 }
2266
Elliott Hughesb465ab02011-08-24 11:21:21 -07002267 // If we're handing out direct pointers, check whether it's a direct pointer
2268 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002269 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002270 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002271 return JNILocalRefType;
2272 }
2273 }
2274
2275 return JNIInvalidRefType;
2276 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 }
2278};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002279
Elliott Hughesa2501992011-08-26 19:39:54 -07002280const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002281 NULL, // reserved0.
2282 NULL, // reserved1.
2283 NULL, // reserved2.
2284 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 JNI::GetVersion,
2286 JNI::DefineClass,
2287 JNI::FindClass,
2288 JNI::FromReflectedMethod,
2289 JNI::FromReflectedField,
2290 JNI::ToReflectedMethod,
2291 JNI::GetSuperclass,
2292 JNI::IsAssignableFrom,
2293 JNI::ToReflectedField,
2294 JNI::Throw,
2295 JNI::ThrowNew,
2296 JNI::ExceptionOccurred,
2297 JNI::ExceptionDescribe,
2298 JNI::ExceptionClear,
2299 JNI::FatalError,
2300 JNI::PushLocalFrame,
2301 JNI::PopLocalFrame,
2302 JNI::NewGlobalRef,
2303 JNI::DeleteGlobalRef,
2304 JNI::DeleteLocalRef,
2305 JNI::IsSameObject,
2306 JNI::NewLocalRef,
2307 JNI::EnsureLocalCapacity,
2308 JNI::AllocObject,
2309 JNI::NewObject,
2310 JNI::NewObjectV,
2311 JNI::NewObjectA,
2312 JNI::GetObjectClass,
2313 JNI::IsInstanceOf,
2314 JNI::GetMethodID,
2315 JNI::CallObjectMethod,
2316 JNI::CallObjectMethodV,
2317 JNI::CallObjectMethodA,
2318 JNI::CallBooleanMethod,
2319 JNI::CallBooleanMethodV,
2320 JNI::CallBooleanMethodA,
2321 JNI::CallByteMethod,
2322 JNI::CallByteMethodV,
2323 JNI::CallByteMethodA,
2324 JNI::CallCharMethod,
2325 JNI::CallCharMethodV,
2326 JNI::CallCharMethodA,
2327 JNI::CallShortMethod,
2328 JNI::CallShortMethodV,
2329 JNI::CallShortMethodA,
2330 JNI::CallIntMethod,
2331 JNI::CallIntMethodV,
2332 JNI::CallIntMethodA,
2333 JNI::CallLongMethod,
2334 JNI::CallLongMethodV,
2335 JNI::CallLongMethodA,
2336 JNI::CallFloatMethod,
2337 JNI::CallFloatMethodV,
2338 JNI::CallFloatMethodA,
2339 JNI::CallDoubleMethod,
2340 JNI::CallDoubleMethodV,
2341 JNI::CallDoubleMethodA,
2342 JNI::CallVoidMethod,
2343 JNI::CallVoidMethodV,
2344 JNI::CallVoidMethodA,
2345 JNI::CallNonvirtualObjectMethod,
2346 JNI::CallNonvirtualObjectMethodV,
2347 JNI::CallNonvirtualObjectMethodA,
2348 JNI::CallNonvirtualBooleanMethod,
2349 JNI::CallNonvirtualBooleanMethodV,
2350 JNI::CallNonvirtualBooleanMethodA,
2351 JNI::CallNonvirtualByteMethod,
2352 JNI::CallNonvirtualByteMethodV,
2353 JNI::CallNonvirtualByteMethodA,
2354 JNI::CallNonvirtualCharMethod,
2355 JNI::CallNonvirtualCharMethodV,
2356 JNI::CallNonvirtualCharMethodA,
2357 JNI::CallNonvirtualShortMethod,
2358 JNI::CallNonvirtualShortMethodV,
2359 JNI::CallNonvirtualShortMethodA,
2360 JNI::CallNonvirtualIntMethod,
2361 JNI::CallNonvirtualIntMethodV,
2362 JNI::CallNonvirtualIntMethodA,
2363 JNI::CallNonvirtualLongMethod,
2364 JNI::CallNonvirtualLongMethodV,
2365 JNI::CallNonvirtualLongMethodA,
2366 JNI::CallNonvirtualFloatMethod,
2367 JNI::CallNonvirtualFloatMethodV,
2368 JNI::CallNonvirtualFloatMethodA,
2369 JNI::CallNonvirtualDoubleMethod,
2370 JNI::CallNonvirtualDoubleMethodV,
2371 JNI::CallNonvirtualDoubleMethodA,
2372 JNI::CallNonvirtualVoidMethod,
2373 JNI::CallNonvirtualVoidMethodV,
2374 JNI::CallNonvirtualVoidMethodA,
2375 JNI::GetFieldID,
2376 JNI::GetObjectField,
2377 JNI::GetBooleanField,
2378 JNI::GetByteField,
2379 JNI::GetCharField,
2380 JNI::GetShortField,
2381 JNI::GetIntField,
2382 JNI::GetLongField,
2383 JNI::GetFloatField,
2384 JNI::GetDoubleField,
2385 JNI::SetObjectField,
2386 JNI::SetBooleanField,
2387 JNI::SetByteField,
2388 JNI::SetCharField,
2389 JNI::SetShortField,
2390 JNI::SetIntField,
2391 JNI::SetLongField,
2392 JNI::SetFloatField,
2393 JNI::SetDoubleField,
2394 JNI::GetStaticMethodID,
2395 JNI::CallStaticObjectMethod,
2396 JNI::CallStaticObjectMethodV,
2397 JNI::CallStaticObjectMethodA,
2398 JNI::CallStaticBooleanMethod,
2399 JNI::CallStaticBooleanMethodV,
2400 JNI::CallStaticBooleanMethodA,
2401 JNI::CallStaticByteMethod,
2402 JNI::CallStaticByteMethodV,
2403 JNI::CallStaticByteMethodA,
2404 JNI::CallStaticCharMethod,
2405 JNI::CallStaticCharMethodV,
2406 JNI::CallStaticCharMethodA,
2407 JNI::CallStaticShortMethod,
2408 JNI::CallStaticShortMethodV,
2409 JNI::CallStaticShortMethodA,
2410 JNI::CallStaticIntMethod,
2411 JNI::CallStaticIntMethodV,
2412 JNI::CallStaticIntMethodA,
2413 JNI::CallStaticLongMethod,
2414 JNI::CallStaticLongMethodV,
2415 JNI::CallStaticLongMethodA,
2416 JNI::CallStaticFloatMethod,
2417 JNI::CallStaticFloatMethodV,
2418 JNI::CallStaticFloatMethodA,
2419 JNI::CallStaticDoubleMethod,
2420 JNI::CallStaticDoubleMethodV,
2421 JNI::CallStaticDoubleMethodA,
2422 JNI::CallStaticVoidMethod,
2423 JNI::CallStaticVoidMethodV,
2424 JNI::CallStaticVoidMethodA,
2425 JNI::GetStaticFieldID,
2426 JNI::GetStaticObjectField,
2427 JNI::GetStaticBooleanField,
2428 JNI::GetStaticByteField,
2429 JNI::GetStaticCharField,
2430 JNI::GetStaticShortField,
2431 JNI::GetStaticIntField,
2432 JNI::GetStaticLongField,
2433 JNI::GetStaticFloatField,
2434 JNI::GetStaticDoubleField,
2435 JNI::SetStaticObjectField,
2436 JNI::SetStaticBooleanField,
2437 JNI::SetStaticByteField,
2438 JNI::SetStaticCharField,
2439 JNI::SetStaticShortField,
2440 JNI::SetStaticIntField,
2441 JNI::SetStaticLongField,
2442 JNI::SetStaticFloatField,
2443 JNI::SetStaticDoubleField,
2444 JNI::NewString,
2445 JNI::GetStringLength,
2446 JNI::GetStringChars,
2447 JNI::ReleaseStringChars,
2448 JNI::NewStringUTF,
2449 JNI::GetStringUTFLength,
2450 JNI::GetStringUTFChars,
2451 JNI::ReleaseStringUTFChars,
2452 JNI::GetArrayLength,
2453 JNI::NewObjectArray,
2454 JNI::GetObjectArrayElement,
2455 JNI::SetObjectArrayElement,
2456 JNI::NewBooleanArray,
2457 JNI::NewByteArray,
2458 JNI::NewCharArray,
2459 JNI::NewShortArray,
2460 JNI::NewIntArray,
2461 JNI::NewLongArray,
2462 JNI::NewFloatArray,
2463 JNI::NewDoubleArray,
2464 JNI::GetBooleanArrayElements,
2465 JNI::GetByteArrayElements,
2466 JNI::GetCharArrayElements,
2467 JNI::GetShortArrayElements,
2468 JNI::GetIntArrayElements,
2469 JNI::GetLongArrayElements,
2470 JNI::GetFloatArrayElements,
2471 JNI::GetDoubleArrayElements,
2472 JNI::ReleaseBooleanArrayElements,
2473 JNI::ReleaseByteArrayElements,
2474 JNI::ReleaseCharArrayElements,
2475 JNI::ReleaseShortArrayElements,
2476 JNI::ReleaseIntArrayElements,
2477 JNI::ReleaseLongArrayElements,
2478 JNI::ReleaseFloatArrayElements,
2479 JNI::ReleaseDoubleArrayElements,
2480 JNI::GetBooleanArrayRegion,
2481 JNI::GetByteArrayRegion,
2482 JNI::GetCharArrayRegion,
2483 JNI::GetShortArrayRegion,
2484 JNI::GetIntArrayRegion,
2485 JNI::GetLongArrayRegion,
2486 JNI::GetFloatArrayRegion,
2487 JNI::GetDoubleArrayRegion,
2488 JNI::SetBooleanArrayRegion,
2489 JNI::SetByteArrayRegion,
2490 JNI::SetCharArrayRegion,
2491 JNI::SetShortArrayRegion,
2492 JNI::SetIntArrayRegion,
2493 JNI::SetLongArrayRegion,
2494 JNI::SetFloatArrayRegion,
2495 JNI::SetDoubleArrayRegion,
2496 JNI::RegisterNatives,
2497 JNI::UnregisterNatives,
2498 JNI::MonitorEnter,
2499 JNI::MonitorExit,
2500 JNI::GetJavaVM,
2501 JNI::GetStringRegion,
2502 JNI::GetStringUTFRegion,
2503 JNI::GetPrimitiveArrayCritical,
2504 JNI::ReleasePrimitiveArrayCritical,
2505 JNI::GetStringCritical,
2506 JNI::ReleaseStringCritical,
2507 JNI::NewWeakGlobalRef,
2508 JNI::DeleteWeakGlobalRef,
2509 JNI::ExceptionCheck,
2510 JNI::NewDirectByteBuffer,
2511 JNI::GetDirectBufferAddress,
2512 JNI::GetDirectBufferCapacity,
2513 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002514};
2515
Elliott Hughes75770752011-08-24 17:52:38 -07002516JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002517 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002518 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002519 local_ref_cookie(IRT_FIRST_SEGMENT),
2520 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes75770752011-08-24 17:52:38 -07002521 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002522 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002523 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002524 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002525 functions = unchecked_functions = &gNativeInterface;
2526 if (check_jni) {
2527 functions = GetCheckJniNativeInterface();
2528 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002529 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2530 // errors will ensue.
2531 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2532 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002533}
2534
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002535JNIEnvExt::~JNIEnvExt() {
2536}
2537
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002538void JNIEnvExt::DumpReferenceTables() {
2539 locals.Dump();
2540 monitors.Dump();
2541}
2542
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002543void JNIEnvExt::PushFrame(int capacity) {
2544 stacked_local_ref_cookies.push_back(local_ref_cookie);
2545 local_ref_cookie = locals.GetSegmentState();
2546}
2547
2548void JNIEnvExt::PopFrame() {
2549 locals.SetSegmentState(local_ref_cookie);
2550 local_ref_cookie = stacked_local_ref_cookies.back();
2551 stacked_local_ref_cookies.pop_back();
2552}
2553
Carl Shapiroea4dca82011-08-01 13:45:38 -07002554// JNI Invocation interface.
2555
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002556extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2557 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2558 if (args->version < JNI_VERSION_1_2) {
2559 return JNI_EVERSION;
2560 }
2561 Runtime::Options options;
2562 for (int i = 0; i < args->nOptions; ++i) {
2563 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002564 options.push_back(std::make_pair(StringPiece(option->optionString),
2565 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002566 }
2567 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002568 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002569 if (runtime == NULL) {
2570 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002571 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002572 runtime->Start();
2573 *p_env = Thread::Current()->GetJniEnv();
2574 *p_vm = runtime->GetJavaVM();
2575 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002576}
2577
Elliott Hughesf2682d52011-08-15 16:37:04 -07002578extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002579 Runtime* runtime = Runtime::Current();
2580 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002581 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002582 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002583 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002584 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002585 }
2586 return JNI_OK;
2587}
2588
2589// Historically unsupported.
2590extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2591 return JNI_ERR;
2592}
2593
Elliott Hughescdf53122011-08-19 15:46:09 -07002594class JII {
2595 public:
2596 static jint DestroyJavaVM(JavaVM* vm) {
2597 if (vm == NULL) {
2598 return JNI_ERR;
2599 } else {
2600 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2601 delete raw_vm->runtime;
2602 return JNI_OK;
2603 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002604 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002605
Elliott Hughescdf53122011-08-19 15:46:09 -07002606 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002607 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002608 }
2609
2610 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002611 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002612 }
2613
2614 static jint DetachCurrentThread(JavaVM* vm) {
2615 if (vm == NULL) {
2616 return JNI_ERR;
2617 } else {
2618 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2619 Runtime* runtime = raw_vm->runtime;
2620 runtime->DetachCurrentThread();
2621 return JNI_OK;
2622 }
2623 }
2624
2625 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2626 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2627 return JNI_EVERSION;
2628 }
2629 if (vm == NULL || env == NULL) {
2630 return JNI_ERR;
2631 }
2632 Thread* thread = Thread::Current();
2633 if (thread == NULL) {
2634 *env = NULL;
2635 return JNI_EDETACHED;
2636 }
2637 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002638 return JNI_OK;
2639 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002640};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002641
Elliott Hughesa2501992011-08-26 19:39:54 -07002642const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002643 NULL, // reserved0
2644 NULL, // reserved1
2645 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002646 JII::DestroyJavaVM,
2647 JII::AttachCurrentThread,
2648 JII::DetachCurrentThread,
2649 JII::GetEnv,
2650 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002651};
2652
Elliott Hughesa0957642011-09-02 14:27:33 -07002653JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002654 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002655 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002656 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002657 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002658 verbose_jni(options->IsVerbose("jni")),
2659 log_third_party_jni(options->IsVerbose("third-party-jni")),
2660 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002661 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002662 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002663 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002664 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002665 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002666 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002667 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002668 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002669 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002670 functions = unchecked_functions = &gInvokeInterface;
2671 if (check_jni) {
2672 functions = GetCheckJniInvokeInterface();
2673 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002674}
2675
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002676JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002677 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002678}
2679
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002680void JavaVMExt::DumpReferenceTables() {
2681 {
2682 MutexLock mu(globals_lock);
2683 globals.Dump();
2684 }
2685 {
2686 MutexLock mu(weak_globals_lock);
2687 weak_globals.Dump();
2688 }
2689 {
2690 MutexLock mu(pins_lock);
2691 pin_table.Dump();
2692 }
2693}
2694
Elliott Hughes75770752011-08-24 17:52:38 -07002695bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2696 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002697
2698 // See if we've already loaded this library. If we have, and the class loader
2699 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002700 // TODO: for better results we should canonicalize the pathname (or even compare
2701 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002702 SharedLibrary* library;
2703 {
2704 // TODO: move the locking (and more of this logic) into Libraries.
2705 MutexLock mu(libraries_lock);
2706 library = libraries->Get(path);
2707 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002708 if (library != NULL) {
2709 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002710 // The library will be associated with class_loader. The JNI
2711 // spec says we can't load the same library into more than one
2712 // class loader.
2713 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2714 "ClassLoader %p; can't open in ClassLoader %p",
2715 path.c_str(), library->GetClassLoader(), class_loader);
2716 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002717 return false;
2718 }
2719 if (verbose_jni) {
2720 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2721 << "ClassLoader " << class_loader << "]";
2722 }
2723 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002724 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2725 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002726 return false;
2727 }
2728 return true;
2729 }
2730
2731 // Open the shared library. Because we're using a full path, the system
2732 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2733 // resolve this library's dependencies though.)
2734
2735 // Failures here are expected when java.library.path has several entries
2736 // and we have to hunt for the lib.
2737
2738 // The current version of the dynamic linker prints detailed information
2739 // about dlopen() failures. Some things to check if the message is
2740 // cryptic:
2741 // - make sure the library exists on the device
2742 // - verify that the right path is being opened (the debug log message
2743 // above can help with that)
2744 // - check to see if the library is valid (e.g. not zero bytes long)
2745 // - check config/prelink-linux-arm.map to ensure that the library
2746 // is listed and is not being overrun by the previous entry (if
2747 // loading suddenly stops working on a prelinked library, this is
2748 // a good one to check)
2749 // - write a trivial app that calls sleep() then dlopen(), attach
2750 // to it with "strace -p <pid>" while it sleeps, and watch for
2751 // attempts to open nonexistent dependent shared libs
2752
2753 // TODO: automate some of these checks!
2754
2755 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002756 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002757 // the GC to ignore us.
2758 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002759 void* handle = NULL;
2760 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002761 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002762 handle = dlopen(path.c_str(), RTLD_LAZY);
2763 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002764
2765 if (verbose_jni) {
2766 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2767 }
2768
2769 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002770 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002771 return false;
2772 }
2773
2774 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002775 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002776 // TODO: move the locking (and more of this logic) into Libraries.
2777 MutexLock mu(libraries_lock);
2778 library = libraries->Get(path);
2779 if (library != NULL) {
2780 LOG(INFO) << "WOW: we lost a race to add shared library: "
2781 << "\"" << path << "\" ClassLoader=" << class_loader;
2782 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002783 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002784 library = new SharedLibrary(path, handle, class_loader);
2785 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002786 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002787
2788 if (verbose_jni) {
2789 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2790 }
2791
2792 bool result = true;
2793 void* sym = dlsym(handle, "JNI_OnLoad");
2794 if (sym == NULL) {
2795 if (verbose_jni) {
2796 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2797 }
2798 } else {
2799 // Call JNI_OnLoad. We have to override the current class
2800 // loader, which will always be "null" since the stuff at the
2801 // top of the stack is around Runtime.loadLibrary(). (See
2802 // the comments in the JNI FindClass function.)
2803 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2804 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002805 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002806 self->SetClassLoaderOverride(class_loader);
2807
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002808 int version = 0;
2809 {
2810 ScopedThreadStateChange tsc(self, Thread::kNative);
2811 if (verbose_jni) {
2812 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2813 }
2814 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002815 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002816
Brian Carlstromaded5f72011-10-07 17:15:04 -07002817 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002818
2819 if (version != JNI_VERSION_1_2 &&
2820 version != JNI_VERSION_1_4 &&
2821 version != JNI_VERSION_1_6) {
2822 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2823 << "bad version: " << version;
2824 // It's unwise to call dlclose() here, but we can mark it
2825 // as bad and ensure that future load attempts will fail.
2826 // We don't know how far JNI_OnLoad got, so there could
2827 // be some partially-initialized stuff accessible through
2828 // newly-registered native method calls. We could try to
2829 // unregister them, but that doesn't seem worthwhile.
2830 result = false;
2831 } else {
2832 if (verbose_jni) {
2833 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2834 << " from JNI_OnLoad in \"" << path << "\"]";
2835 }
2836 }
2837 }
2838
2839 library->SetResult(result);
2840 return result;
2841}
2842
2843void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2844 CHECK(m->IsNative());
2845
2846 Class* c = m->GetDeclaringClass();
2847
2848 // If this is a static method, it could be called before the class
2849 // has been initialized.
2850 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002851 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002852 return NULL;
2853 }
2854 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002855 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002856 }
2857
Brian Carlstrom16192862011-09-12 17:50:06 -07002858 std::string detail;
2859 void* native_method;
2860 {
2861 MutexLock mu(libraries_lock);
2862 native_method = libraries->FindNativeMethod(m, detail);
2863 }
2864 // throwing can cause libraries_lock to be reacquired
2865 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002866 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002867 }
2868 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002869}
2870
Elliott Hughes410c0c82011-09-01 17:58:25 -07002871void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2872 {
2873 MutexLock mu(globals_lock);
2874 globals.VisitRoots(visitor, arg);
2875 }
2876 {
2877 MutexLock mu(pins_lock);
2878 pin_table.VisitRoots(visitor, arg);
2879 }
2880 // The weak_globals table is visited by the GC itself (because it mutates the table).
2881}
2882
Ian Rogersdf20fe02011-07-20 20:34:16 -07002883} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002884
2885std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2886 switch (rhs) {
2887 case JNIInvalidRefType:
2888 os << "JNIInvalidRefType";
2889 return os;
2890 case JNILocalRefType:
2891 os << "JNILocalRefType";
2892 return os;
2893 case JNIGlobalRefType:
2894 os << "JNIGlobalRefType";
2895 return os;
2896 case JNIWeakGlobalRefType:
2897 os << "JNIWeakGlobalRefType";
2898 return os;
2899 }
2900}