blob: 04365003362a8b63ddb394a9fadadb4619db7345 [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"
Carl Shapirofc322c72011-07-27 00:20:01 -070023#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070025
26namespace art {
27
Shih-wei Liao31384c52011-09-06 15:27:45 -070028void* FindNativeMethod(Thread* thread) {
29 DCHECK(Thread::Current() == thread);
30
31 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
32 DCHECK(method != NULL);
33
34 // Lookup symbol address for method, on failure we'll return NULL with an
35 // exception set, otherwise we return the address of the method we found.
36 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
37 if (native_code == NULL) {
38 DCHECK(thread->IsExceptionPending());
39 return NULL;
40 } else {
41 // Register so that future calls don't come here
42 method->RegisterNative(native_code);
43 return native_code;
44 }
45}
46
Elliott Hughesc5f7c912011-08-18 14:00:42 -070047/*
48 * Add a local reference for an object to the current stack frame. When
49 * the native function returns, the reference will be discarded.
50 *
51 * We need to allow the same reference to be added multiple times.
52 *
53 * This will be called on otherwise unreferenced objects. We cannot do
54 * GC allocations here, and it's best if we don't grab a mutex.
55 *
56 * Returns the local reference (currently just the same pointer that was
57 * passed in), or NULL on failure.
58 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070059template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070060T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
61 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
62 Object* obj = const_cast<Object*>(const_obj);
63
Elliott Hughesc5f7c912011-08-18 14:00:42 -070064 if (obj == NULL) {
65 return NULL;
66 }
67
Elliott Hughesbf86d042011-08-31 17:53:14 -070068 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
69 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070070
71 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
72 IndirectRef ref = locals.Add(cookie, obj);
73 if (ref == NULL) {
74 // TODO: just change Add's DCHECK to CHECK and lose this?
75 locals.Dump();
76 LOG(FATAL) << "Failed adding to JNI local reference table "
77 << "(has " << locals.Capacity() << " entries)";
78 // TODO: dvmDumpThread(dvmThreadSelf(), false);
79 }
80
81#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070082 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070083 size_t entry_count = locals.Capacity();
84 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070085 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -070086 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070087 locals.Dump();
88 // TODO: dvmDumpThread(dvmThreadSelf(), false);
89 // dvmAbort();
90 }
91 }
92#endif
93
Elliott Hughesbf86d042011-08-31 17:53:14 -070094 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070095 // Hand out direct pointers to support broken old apps.
96 return reinterpret_cast<T>(obj);
97 }
98
99 return reinterpret_cast<T>(ref);
100}
101
Elliott Hughesbf86d042011-08-31 17:53:14 -0700102// For external use.
103template<typename T>
104T Decode(JNIEnv* public_env, jobject obj) {
105 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
106 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
107}
108// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700109template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700110template Class* Decode<Class*>(JNIEnv*, jobject);
111template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
112template Object* Decode<Object*>(JNIEnv*, jobject);
113template String* Decode<String*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700114template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
115template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700116template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(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 Hughescdf53122011-08-19 15:46:09 -0700272jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
273 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700274 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700275 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700276 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700277
278 Method* method = NULL;
279 if (is_static) {
280 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700281 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700282 method = c->FindVirtualMethod(name, sig);
283 if (method == NULL) {
284 // No virtual method matching the signature. Search declared
285 // private methods and constructors.
286 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700287 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700288 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700289
Elliott Hughescdf53122011-08-19 15:46:09 -0700290 if (method == NULL || method->IsStatic() != is_static) {
291 Thread* self = Thread::Current();
Elliott Hughesa2501992011-08-26 19:39:54 -0700292 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700293 // TODO: try searching for the opposite kind of method from is_static
294 // for better diagnostics?
295 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700296 "no %s method %s", is_static ? "static" : "non-static",
297 method_name.c_str());
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());
327 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
328 "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());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700339 ts.Self()->ThrowNewException("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 Hughes814e4032011-08-23 12:07:56 -0700387 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
388 "%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) {
392 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
393 "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() {
559 // Delete our map values. (The keys will be cleaned up by the map itself.)
560 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
561 delete it->second;
562 }
563 }
564
565 SharedLibrary* Get(const std::string& path) {
566 return libraries_[path];
567 }
568
569 void Put(const std::string& path, SharedLibrary* library) {
570 libraries_[path] = library;
571 }
572
573 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700574 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700575 std::string jni_short_name(JniShortName(m));
576 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700577 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700578 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
579 SharedLibrary* library = it->second;
580 if (library->GetClassLoader() != declaring_class_loader) {
581 // We only search libraries loaded by the appropriate ClassLoader.
582 continue;
583 }
584 // Try the short name then the long name...
585 void* fn = library->FindSymbol(jni_short_name);
586 if (fn == NULL) {
587 fn = library->FindSymbol(jni_long_name);
588 }
589 if (fn != NULL) {
590 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700591 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700592 << " in \"" << library->GetPath() << "\"]";
593 }
594 return fn;
595 }
596 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700597 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700598 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700599 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 return NULL;
601 }
602
603 private:
604 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
605
606 std::map<std::string, SharedLibrary*> libraries_;
607};
608
Elliott Hughes418d20f2011-09-22 14:00:39 -0700609JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
610 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
611 Object* receiver = Decode<Object*>(env, obj);
612 Method* method = DecodeMethod(mid);
613 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
614 return InvokeWithArgArray(env, receiver, method, arg_array.get());
615}
616
Elliott Hughescdf53122011-08-19 15:46:09 -0700617class JNI {
618 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700619
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 static jint GetVersion(JNIEnv* env) {
621 ScopedJniThreadState ts(env);
622 return JNI_VERSION_1_6;
623 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700624
Elliott Hughescdf53122011-08-19 15:46:09 -0700625 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
626 ScopedJniThreadState ts(env);
627 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700628 return NULL;
629 }
630
Elliott Hughescdf53122011-08-19 15:46:09 -0700631 static jclass FindClass(JNIEnv* env, const char* name) {
632 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700633 Runtime* runtime = Runtime::Current();
634 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700636 Class* c = NULL;
637 if (runtime->IsStarted()) {
638 // TODO: need to get the appropriate ClassLoader.
639 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
640 c = class_linker->FindClass(descriptor, cl);
641 } else {
642 c = class_linker->FindSystemClass(descriptor);
643 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700644 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700645 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700646
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
648 ScopedJniThreadState ts(env);
649 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700650 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700651 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700652
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
654 ScopedJniThreadState ts(env);
655 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700656 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700658
Elliott Hughescdf53122011-08-19 15:46:09 -0700659 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
660 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700661 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700662 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700664
Elliott Hughescdf53122011-08-19 15:46:09 -0700665 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
666 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700667 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700668 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700669 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700670
Elliott Hughes37f7a402011-08-22 18:56:01 -0700671 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
672 ScopedJniThreadState ts(env);
673 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700674 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700675 }
676
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700677 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700679 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700680 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700682
Elliott Hughes37f7a402011-08-22 18:56:01 -0700683 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 Class* c1 = Decode<Class*>(ts, java_class1);
686 Class* c2 = Decode<Class*>(ts, java_class2);
687 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700688 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700689
Elliott Hughes37f7a402011-08-22 18:56:01 -0700690 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700692 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700693 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700694 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700695 return JNI_TRUE;
696 } else {
697 Object* obj = Decode<Object*>(ts, jobj);
698 Class* klass = Decode<Class*>(ts, clazz);
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700699 return obj->InstanceOf(klass) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700700 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700701 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700702
Elliott Hughes37f7a402011-08-22 18:56:01 -0700703 static jint Throw(JNIEnv* env, jthrowable java_exception) {
704 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700705 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700706 if (exception == NULL) {
707 return JNI_ERR;
708 }
709 ts.Self()->SetException(exception);
710 return JNI_OK;
711 }
712
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700713 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700714 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700715 // TODO: check for a pending exception to decide what constructor to call.
716 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
717 if (mid == NULL) {
718 return JNI_ERR;
719 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700720 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
721 if (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 Hughes54e7df12011-09-16 11:47:04 -0700732 LOG(INFO) << "Throwing " << PrettyTypeOf(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700733 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700734 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700735
Elliott Hughes37f7a402011-08-22 18:56:01 -0700736 return JNI_OK;
737 }
738
739 static jboolean ExceptionCheck(JNIEnv* env) {
740 ScopedJniThreadState ts(env);
741 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
742 }
743
744 static void ExceptionClear(JNIEnv* env) {
745 ScopedJniThreadState ts(env);
746 ts.Self()->ClearException();
747 }
748
749 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700750 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700751
752 Thread* self = ts.Self();
753 Throwable* original_exception = self->GetException();
754 self->ClearException();
755
Elliott Hughesbf86d042011-08-31 17:53:14 -0700756 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700757 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
758 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
759 if (mid == NULL) {
760 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700761 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700762 } else {
763 env->CallVoidMethod(exception.get(), mid);
764 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700765 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700766 << " thrown while calling printStackTrace";
767 self->ClearException();
768 }
769 }
770
771 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700773
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 static jthrowable ExceptionOccurred(JNIEnv* env) {
775 ScopedJniThreadState ts(env);
776 Object* exception = ts.Self()->GetException();
777 if (exception == NULL) {
778 return NULL;
779 } else {
780 // TODO: if adding a local reference failing causes the VM to abort
781 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700782 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 if (localException == NULL) {
784 // We were unable to add a new local reference, and threw a new
785 // exception. We can't return "exception", because it's not a
786 // local reference. So we have to return NULL, indicating that
787 // there was no exception, even though it's pretty much raining
788 // exceptions in here.
789 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
790 }
791 return localException;
792 }
793 }
794
Elliott Hughescdf53122011-08-19 15:46:09 -0700795 static void FatalError(JNIEnv* env, const char* msg) {
796 ScopedJniThreadState ts(env);
797 LOG(FATAL) << "JNI FatalError called: " << msg;
798 }
799
800 static jint PushLocalFrame(JNIEnv* env, jint cap) {
801 ScopedJniThreadState ts(env);
802 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
803 return JNI_OK;
804 }
805
806 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
807 ScopedJniThreadState ts(env);
808 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
809 return res;
810 }
811
Elliott Hughes72025e52011-08-23 17:50:30 -0700812 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
813 ScopedJniThreadState ts(env);
814 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
815 return 0;
816 }
817
Elliott Hughescdf53122011-08-19 15:46:09 -0700818 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
819 ScopedJniThreadState ts(env);
820 if (obj == NULL) {
821 return NULL;
822 }
823
Elliott Hughes75770752011-08-24 17:52:38 -0700824 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700825 IndirectReferenceTable& globals = vm->globals;
826 MutexLock mu(vm->globals_lock);
827 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
828 return reinterpret_cast<jobject>(ref);
829 }
830
831 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
832 ScopedJniThreadState ts(env);
833 if (obj == NULL) {
834 return;
835 }
836
Elliott Hughes75770752011-08-24 17:52:38 -0700837 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700838 IndirectReferenceTable& globals = vm->globals;
839 MutexLock mu(vm->globals_lock);
840
841 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
842 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
843 << "failed to find entry";
844 }
845 }
846
847 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
848 ScopedJniThreadState ts(env);
849 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
850 }
851
852 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
853 ScopedJniThreadState ts(env);
854 if (obj == NULL) {
855 return;
856 }
857
Elliott Hughes75770752011-08-24 17:52:38 -0700858 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700859 IndirectReferenceTable& weak_globals = vm->weak_globals;
860 MutexLock mu(vm->weak_globals_lock);
861
862 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
863 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
864 << "failed to find entry";
865 }
866 }
867
868 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
869 ScopedJniThreadState ts(env);
870 if (obj == NULL) {
871 return NULL;
872 }
873
874 IndirectReferenceTable& locals = ts.Env()->locals;
875
876 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
877 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
878 return reinterpret_cast<jobject>(ref);
879 }
880
881 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
882 ScopedJniThreadState ts(env);
883 if (obj == NULL) {
884 return;
885 }
886
887 IndirectReferenceTable& locals = ts.Env()->locals;
888
889 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
890 if (!locals.Remove(cookie, obj)) {
891 // Attempting to delete a local reference that is not in the
892 // topmost local reference frame is a no-op. DeleteLocalRef returns
893 // void and doesn't throw any exceptions, but we should probably
894 // complain about it so the user will notice that things aren't
895 // going quite the way they expect.
896 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
897 << "failed to find entry";
898 }
899 }
900
901 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
902 ScopedJniThreadState ts(env);
903 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
904 ? JNI_TRUE : JNI_FALSE;
905 }
906
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700907 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700909 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700910 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700911 return NULL;
912 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700913 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 }
915
Elliott Hughes72025e52011-08-23 17:50:30 -0700916 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 ScopedJniThreadState ts(env);
918 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700919 va_start(args, mid);
920 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 va_end(args);
922 return result;
923 }
924
Elliott Hughes72025e52011-08-23 17:50:30 -0700925 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700926 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700927 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700928 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700929 return NULL;
930 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700931 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700932 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 return local_result;
935 }
936
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700939 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700940 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700941 return NULL;
942 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700943 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700944 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700946 return local_result;
947 }
948
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
950 ScopedJniThreadState ts(env);
951 return FindMethodID(ts, c, name, sig, false);
952 }
953
954 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
955 ScopedJniThreadState ts(env);
956 return FindMethodID(ts, c, name, sig, true);
957 }
958
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 va_list ap;
962 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700963 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700965 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 }
967
Elliott Hughes72025e52011-08-23 17:50:30 -0700968 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700970 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700971 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700976 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700977 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 }
979
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 va_list ap;
983 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700984 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 va_end(ap);
986 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700991 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 }
993
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700996 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 va_list ap;
1002 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001003 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 va_end(ap);
1005 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001010 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 }
1012
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001015 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 va_list ap;
1021 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001022 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 va_end(ap);
1024 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001029 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001034 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 va_list ap;
1040 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001041 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 va_end(ap);
1043 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001048 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 }
1050
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001053 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 va_list ap;
1059 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001060 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 va_end(ap);
1062 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 }
1064
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001067 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001072 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 va_list ap;
1078 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001079 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 va_end(ap);
1081 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 }
1083
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001086 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001091 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 va_list ap;
1097 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001098 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 va_end(ap);
1100 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001105 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001110 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_list ap;
1116 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001117 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 va_end(ap);
1119 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001124 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001129 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 va_list ap;
1135 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001136 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001142 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001147 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
1150 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 ScopedJniThreadState ts(env);
1153 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001155 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001156 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 va_end(ap);
1158 return local_result;
1159 }
1160
1161 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001164 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001165 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
1168 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001171 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001172 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 }
1174
1175 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 ScopedJniThreadState ts(env);
1178 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001180 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 va_end(ap);
1182 return result.z;
1183 }
1184
1185 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001188 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
1191 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001194 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
1197 static jbyte CallNonvirtualByteMethod(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.b;
1205 }
1206
1207 static jbyte CallNonvirtualByteMethodV(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).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
1213 static jbyte CallNonvirtualByteMethodA(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).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 }
1218
1219 static jchar CallNonvirtualCharMethod(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.c;
1227 }
1228
1229 static jchar CallNonvirtualCharMethodV(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).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 }
1234
1235 static jchar CallNonvirtualCharMethodA(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).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
1241 static jshort CallNonvirtualShortMethod(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.s;
1249 }
1250
1251 static jshort CallNonvirtualShortMethodV(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).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 }
1256
1257 static jshort CallNonvirtualShortMethodA(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).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 }
1262
Elliott Hughes418d20f2011-09-22 14:00:39 -07001263 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 ScopedJniThreadState ts(env);
1265 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001266 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001267 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 va_end(ap);
1269 return result.i;
1270 }
1271
1272 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001275 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
1278 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001281 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
1284 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 ScopedJniThreadState ts(env);
1287 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001288 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001289 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 va_end(ap);
1291 return result.j;
1292 }
1293
1294 static jlong CallNonvirtualLongMethodV(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).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
1300 static jlong CallNonvirtualLongMethodA(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).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
1306 static jfloat CallNonvirtualFloatMethod(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.f;
1314 }
1315
1316 static jfloat CallNonvirtualFloatMethodV(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).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
1322 static jfloat CallNonvirtualFloatMethodA(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).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
1328 static jdouble CallNonvirtualDoubleMethod(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.d;
1336 }
1337
1338 static jdouble CallNonvirtualDoubleMethodV(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).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
1344 static jdouble CallNonvirtualDoubleMethodA(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).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
1350 static void CallNonvirtualVoidMethod(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 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 va_end(ap);
1357 }
1358
1359 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001360 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001362 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
1365 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001366 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001368 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001371 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 ScopedJniThreadState ts(env);
1373 return FindFieldID(ts, c, name, sig, false);
1374 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001375
1376
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001377 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 ScopedJniThreadState ts(env);
1379 return FindFieldID(ts, c, name, sig, true);
1380 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001381
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001382 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001384 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001385 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001386 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001388
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001391 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001392 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001395 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001397 Object* o = Decode<Object*>(ts, java_object);
1398 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001399 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 }
1402
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001403 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001405 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001406 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 }
1409
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001410#define GET_PRIMITIVE_FIELD(fn, instance) \
1411 ScopedJniThreadState ts(env); \
1412 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001413 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001414 return f->fn(o)
1415
1416#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1417 ScopedJniThreadState ts(env); \
1418 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001419 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001420 f->fn(o, value)
1421
1422 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1423 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1427 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001430 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1431 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1435 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1439 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1443 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1447 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1451 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1455 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1459 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1463 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 }
1465
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001466 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1467 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1471 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1475 GET_PRIMITIVE_FIELD(GetLong, NULL);
1476 }
1477
1478 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1479 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1480 }
1481
1482 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1483 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1484 }
1485
1486 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1487 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1488 }
1489
1490 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1491 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1492 }
1493
1494 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1495 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1496 }
1497
1498 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1499 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1500 }
1501
1502 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1503 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1504 }
1505
1506 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1507 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1508 }
1509
1510 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1511 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1512 }
1513
1514 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1515 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1516 }
1517
1518 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1519 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1520 }
1521
1522 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1523 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1524 }
1525
1526 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1527 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1528 }
1529
1530 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1531 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1532 }
1533
1534 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1535 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1536 }
1537
1538 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1539 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1540 }
1541
1542 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1543 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1544 }
1545
1546 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1547 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 }
1549
Elliott Hughes418d20f2011-09-22 14:00:39 -07001550 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 ScopedJniThreadState ts(env);
1552 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001553 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001554 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001555 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 va_end(ap);
1557 return local_result;
1558 }
1559
Elliott Hughes418d20f2011-09-22 14:00:39 -07001560 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001562 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001563 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001564 }
1565
Elliott Hughes418d20f2011-09-22 14:00:39 -07001566 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001568 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001569 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes418d20f2011-09-22 14:00:39 -07001572 static jboolean CallStaticBooleanMethod(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 Hughescdf53122011-08-19 15:46:09 -07001577 va_end(ap);
1578 return result.z;
1579 }
1580
Elliott Hughes418d20f2011-09-22 14:00:39 -07001581 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001583 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 }
1585
Elliott Hughes418d20f2011-09-22 14:00:39 -07001586 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001588 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes72025e52011-08-23 17:50:30 -07001591 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 ScopedJniThreadState ts(env);
1593 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001594 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001595 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 va_end(ap);
1597 return result.b;
1598 }
1599
Elliott Hughes418d20f2011-09-22 14:00:39 -07001600 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001602 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
Elliott Hughes418d20f2011-09-22 14:00:39 -07001605 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001607 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 }
1609
Elliott Hughes72025e52011-08-23 17:50:30 -07001610 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 ScopedJniThreadState ts(env);
1612 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001614 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 va_end(ap);
1616 return result.c;
1617 }
1618
Elliott Hughes418d20f2011-09-22 14:00:39 -07001619 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001621 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes418d20f2011-09-22 14:00:39 -07001624 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001626 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
Elliott Hughes72025e52011-08-23 17:50:30 -07001629 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 ScopedJniThreadState ts(env);
1631 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001632 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001633 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 va_end(ap);
1635 return result.s;
1636 }
1637
Elliott Hughes418d20f2011-09-22 14:00:39 -07001638 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001640 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 }
1642
Elliott Hughes418d20f2011-09-22 14:00:39 -07001643 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001645 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 }
1647
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 ScopedJniThreadState ts(env);
1650 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001652 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 va_end(ap);
1654 return result.i;
1655 }
1656
Elliott Hughes418d20f2011-09-22 14:00:39 -07001657 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
Elliott Hughes418d20f2011-09-22 14:00:39 -07001662 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001664 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 ScopedJniThreadState ts(env);
1669 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001671 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 va_end(ap);
1673 return result.j;
1674 }
1675
Elliott Hughes418d20f2011-09-22 14:00:39 -07001676 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001678 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 }
1680
Elliott Hughes418d20f2011-09-22 14:00:39 -07001681 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001683 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 ScopedJniThreadState ts(env);
1688 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001690 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 va_end(ap);
1692 return result.f;
1693 }
1694
Elliott Hughes418d20f2011-09-22 14:00:39 -07001695 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 }
1699
Elliott Hughes418d20f2011-09-22 14:00:39 -07001700 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001702 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 }
1704
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
1707 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001709 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 va_end(ap);
1711 return result.d;
1712 }
1713
Elliott Hughes418d20f2011-09-22 14:00:39 -07001714 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes418d20f2011-09-22 14:00:39 -07001719 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001721 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 }
1723
Elliott Hughes72025e52011-08-23 17:50:30 -07001724 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 ScopedJniThreadState ts(env);
1726 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001727 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001728 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 va_end(ap);
1730 }
1731
Elliott Hughes418d20f2011-09-22 14:00:39 -07001732 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001734 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 }
1736
Elliott Hughes418d20f2011-09-22 14:00:39 -07001737 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001739 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 }
1741
Elliott Hughes814e4032011-08-23 12:07:56 -07001742 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001744 if (chars == NULL && char_count == 0) {
1745 return NULL;
1746 }
1747 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001748 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
1751 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1752 ScopedJniThreadState ts(env);
1753 if (utf == NULL) {
1754 return NULL;
1755 }
1756 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001757 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 }
1759
Elliott Hughes814e4032011-08-23 12:07:56 -07001760 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1761 ScopedJniThreadState ts(env);
1762 return Decode<String*>(ts, java_string)->GetLength();
1763 }
1764
1765 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1766 ScopedJniThreadState ts(env);
1767 return Decode<String*>(ts, java_string)->GetUtfLength();
1768 }
1769
Elliott Hughesb465ab02011-08-24 11:21:21 -07001770 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001772 String* s = Decode<String*>(ts, java_string);
1773 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1774 ThrowSIOOBE(ts, start, length, s->GetLength());
1775 } else {
1776 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1777 memcpy(buf, chars + start, length * sizeof(jchar));
1778 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001779 }
1780
Elliott Hughesb465ab02011-08-24 11:21:21 -07001781 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001782 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001783 String* s = Decode<String*>(ts, java_string);
1784 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1785 ThrowSIOOBE(ts, start, length, s->GetLength());
1786 } else {
1787 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1788 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1789 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001790 }
1791
Elliott Hughes75770752011-08-24 17:52:38 -07001792 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001793 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001794 String* s = Decode<String*>(ts, java_string);
1795 const CharArray* chars = s->GetCharArray();
1796 PinPrimitiveArray(ts, chars);
1797 if (is_copy != NULL) {
1798 *is_copy = JNI_FALSE;
1799 }
1800 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001801 }
1802
Elliott Hughes75770752011-08-24 17:52:38 -07001803 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001804 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001805 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 }
1807
Elliott Hughes75770752011-08-24 17:52:38 -07001808 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001810 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 }
1812
Elliott Hughes75770752011-08-24 17:52:38 -07001813 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001815 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
Elliott Hughes75770752011-08-24 17:52:38 -07001818 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001819 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001820 if (java_string == NULL) {
1821 return NULL;
1822 }
1823 if (is_copy != NULL) {
1824 *is_copy = JNI_TRUE;
1825 }
1826 String* s = Decode<String*>(ts, java_string);
1827 size_t byte_count = s->GetUtfLength();
1828 char* bytes = new char[byte_count + 1];
1829 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001830 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001831 return NULL;
1832 }
1833 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1834 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1835 bytes[byte_count] = '\0';
1836 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001837 }
1838
Elliott Hughes75770752011-08-24 17:52:38 -07001839 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001840 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001841 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001842 }
1843
Elliott Hughesbd935992011-08-22 11:59:34 -07001844 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001846 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001847 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001848 Array* array = obj->AsArray();
1849 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 }
1851
Elliott Hughes814e4032011-08-23 12:07:56 -07001852 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001854 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001855 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001856 }
1857
1858 static void SetObjectArrayElement(JNIEnv* env,
1859 jobjectArray java_array, jsize index, jobject java_value) {
1860 ScopedJniThreadState ts(env);
1861 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1862 Object* value = Decode<Object*>(ts, java_value);
1863 array->Set(index, value);
1864 }
1865
1866 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1867 ScopedJniThreadState ts(env);
1868 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1869 }
1870
1871 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1872 ScopedJniThreadState ts(env);
1873 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1874 }
1875
1876 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1877 ScopedJniThreadState ts(env);
1878 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1879 }
1880
1881 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1882 ScopedJniThreadState ts(env);
1883 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1884 }
1885
1886 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1887 ScopedJniThreadState ts(env);
1888 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1889 }
1890
1891 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1892 ScopedJniThreadState ts(env);
1893 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1894 }
1895
1896 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1897 ScopedJniThreadState ts(env);
1898 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1899 }
1900
1901 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1902 ScopedJniThreadState ts(env);
1903 CHECK_GE(length, 0); // TODO: ReportJniError
1904
1905 // Compute the array class corresponding to the given element class.
1906 Class* element_class = Decode<Class*>(ts, element_jclass);
1907 std::string descriptor;
1908 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001909 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001910
1911 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001912 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1913 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001914 return NULL;
1915 }
1916
Elliott Hughes75770752011-08-24 17:52:38 -07001917 // Allocate and initialize if necessary.
1918 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001920 if (initial_element != NULL) {
1921 Object* initial_object = Decode<Object*>(ts, initial_element);
1922 for (jsize i = 0; i < length; ++i) {
1923 result->Set(i, initial_object);
1924 }
1925 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001926 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 }
1928
1929 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1930 ScopedJniThreadState ts(env);
1931 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1932 }
1933
Elliott Hughes75770752011-08-24 17:52:38 -07001934 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001935 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001936 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001937 }
1938
Elliott Hughes75770752011-08-24 17:52:38 -07001939 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001940 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001941 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001942 }
1943
Elliott Hughes75770752011-08-24 17:52:38 -07001944 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001946 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 }
1948
Elliott Hughes75770752011-08-24 17:52:38 -07001949 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001951 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 }
1953
Elliott Hughes75770752011-08-24 17:52:38 -07001954 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001955 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001956 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 }
1958
Elliott Hughes75770752011-08-24 17:52:38 -07001959 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001961 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001962 }
1963
Elliott Hughes75770752011-08-24 17:52:38 -07001964 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001966 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 }
1968
Elliott Hughes75770752011-08-24 17:52:38 -07001969 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001971 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Elliott Hughes75770752011-08-24 17:52:38 -07001974 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001976 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Elliott Hughes75770752011-08-24 17:52:38 -07001979 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001981 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes75770752011-08-24 17:52:38 -07001984 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001986 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Elliott Hughes75770752011-08-24 17:52:38 -07001989 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001991 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Elliott Hughes75770752011-08-24 17:52:38 -07001994 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001996 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Elliott Hughes75770752011-08-24 17:52:38 -07001999 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002001 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes75770752011-08-24 17:52:38 -07002004 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002006 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Elliott Hughes75770752011-08-24 17:52:38 -07002009 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002011 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes75770752011-08-24 17:52:38 -07002014 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002016 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes75770752011-08-24 17:52:38 -07002019 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002021 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes814e4032011-08-23 12:07:56 -07002024 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002026 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes814e4032011-08-23 12:07:56 -07002029 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002031 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes814e4032011-08-23 12:07:56 -07002034 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002036 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes814e4032011-08-23 12:07:56 -07002039 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002041 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes814e4032011-08-23 12:07:56 -07002044 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002046 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes814e4032011-08-23 12:07:56 -07002049 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002051 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes814e4032011-08-23 12:07:56 -07002054 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes5174fe62011-08-23 15:12:35 -07002104 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002106 Class* c = Decode<Class*>(ts, java_class);
2107
Elliott Hughes5174fe62011-08-23 15:12:35 -07002108 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 const char* name = methods[i].name;
2110 const char* sig = methods[i].signature;
2111
2112 if (*sig == '!') {
2113 // TODO: fast jni. it's too noisy to log all these.
2114 ++sig;
2115 }
2116
Elliott Hughes5174fe62011-08-23 15:12:35 -07002117 Method* m = c->FindDirectMethod(name, sig);
2118 if (m == NULL) {
2119 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002121 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002123 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2125 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002126 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002128 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002130 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2132 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002133 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 return JNI_ERR;
2135 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002136
Elliott Hughes75770752011-08-24 17:52:38 -07002137 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002138 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002139 }
2140
2141 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 }
2143 return JNI_OK;
2144 }
2145
Elliott Hughes5174fe62011-08-23 15:12:35 -07002146 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002148 Class* c = Decode<Class*>(ts, java_class);
2149
Elliott Hughes75770752011-08-24 17:52:38 -07002150 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002151 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002152 }
2153
2154 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2155 Method* m = c->GetDirectMethod(i);
2156 if (m->IsNative()) {
2157 m->UnregisterNative();
2158 }
2159 }
2160 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2161 Method* m = c->GetVirtualMethod(i);
2162 if (m->IsNative()) {
2163 m->UnregisterNative();
2164 }
2165 }
2166
2167 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 }
2169
Elliott Hughes72025e52011-08-23 17:50:30 -07002170 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002172 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002173 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
2175
Elliott Hughes72025e52011-08-23 17:50:30 -07002176 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002178 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002179 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
2182 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2183 ScopedJniThreadState ts(env);
2184 Runtime* runtime = Runtime::Current();
2185 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002186 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 } else {
2188 *vm = NULL;
2189 }
2190 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2191 }
2192
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2194 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002195
2196 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002197 CHECK(address != NULL); // TODO: ReportJniError
2198 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002199
2200 jclass buffer_class = GetDirectByteBufferClass(env);
2201 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2202 if (mid == NULL) {
2203 return NULL;
2204 }
2205
2206 // At the moment, the Java side is limited to 32 bits.
2207 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2208 CHECK_LE(capacity, 0xffffffff);
2209 jint address_arg = reinterpret_cast<jint>(address);
2210 jint capacity_arg = static_cast<jint>(capacity);
2211
2212 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2213 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 }
2215
Elliott Hughesb465ab02011-08-24 11:21:21 -07002216 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002218 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2219 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 }
2221
Elliott Hughesb465ab02011-08-24 11:21:21 -07002222 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002224 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2225 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227
Elliott Hughesb465ab02011-08-24 11:21:21 -07002228 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002230
Elliott Hughes75770752011-08-24 17:52:38 -07002231 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002232
2233 // Do we definitely know what kind of reference this is?
2234 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2235 IndirectRefKind kind = GetIndirectRefKind(ref);
2236 switch (kind) {
2237 case kLocal:
2238 return JNILocalRefType;
2239 case kGlobal:
2240 return JNIGlobalRefType;
2241 case kWeakGlobal:
2242 return JNIWeakGlobalRefType;
2243 case kSirtOrInvalid:
2244 // Is it in a stack IRT?
2245 if (ts.Self()->SirtContains(java_object)) {
2246 return JNILocalRefType;
2247 }
2248
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002249 if (!ts.Env()->work_around_app_jni_bugs) {
2250 return JNIInvalidRefType;
2251 }
2252
Elliott Hughesb465ab02011-08-24 11:21:21 -07002253 // If we're handing out direct pointers, check whether it's a direct pointer
2254 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002255 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002256 if (ts.Env()->locals.Contains(java_object)) {
2257 return JNILocalRefType;
2258 }
2259 }
2260
2261 return JNIInvalidRefType;
2262 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
2264};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002265
Elliott Hughesa2501992011-08-26 19:39:54 -07002266const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002267 NULL, // reserved0.
2268 NULL, // reserved1.
2269 NULL, // reserved2.
2270 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 JNI::GetVersion,
2272 JNI::DefineClass,
2273 JNI::FindClass,
2274 JNI::FromReflectedMethod,
2275 JNI::FromReflectedField,
2276 JNI::ToReflectedMethod,
2277 JNI::GetSuperclass,
2278 JNI::IsAssignableFrom,
2279 JNI::ToReflectedField,
2280 JNI::Throw,
2281 JNI::ThrowNew,
2282 JNI::ExceptionOccurred,
2283 JNI::ExceptionDescribe,
2284 JNI::ExceptionClear,
2285 JNI::FatalError,
2286 JNI::PushLocalFrame,
2287 JNI::PopLocalFrame,
2288 JNI::NewGlobalRef,
2289 JNI::DeleteGlobalRef,
2290 JNI::DeleteLocalRef,
2291 JNI::IsSameObject,
2292 JNI::NewLocalRef,
2293 JNI::EnsureLocalCapacity,
2294 JNI::AllocObject,
2295 JNI::NewObject,
2296 JNI::NewObjectV,
2297 JNI::NewObjectA,
2298 JNI::GetObjectClass,
2299 JNI::IsInstanceOf,
2300 JNI::GetMethodID,
2301 JNI::CallObjectMethod,
2302 JNI::CallObjectMethodV,
2303 JNI::CallObjectMethodA,
2304 JNI::CallBooleanMethod,
2305 JNI::CallBooleanMethodV,
2306 JNI::CallBooleanMethodA,
2307 JNI::CallByteMethod,
2308 JNI::CallByteMethodV,
2309 JNI::CallByteMethodA,
2310 JNI::CallCharMethod,
2311 JNI::CallCharMethodV,
2312 JNI::CallCharMethodA,
2313 JNI::CallShortMethod,
2314 JNI::CallShortMethodV,
2315 JNI::CallShortMethodA,
2316 JNI::CallIntMethod,
2317 JNI::CallIntMethodV,
2318 JNI::CallIntMethodA,
2319 JNI::CallLongMethod,
2320 JNI::CallLongMethodV,
2321 JNI::CallLongMethodA,
2322 JNI::CallFloatMethod,
2323 JNI::CallFloatMethodV,
2324 JNI::CallFloatMethodA,
2325 JNI::CallDoubleMethod,
2326 JNI::CallDoubleMethodV,
2327 JNI::CallDoubleMethodA,
2328 JNI::CallVoidMethod,
2329 JNI::CallVoidMethodV,
2330 JNI::CallVoidMethodA,
2331 JNI::CallNonvirtualObjectMethod,
2332 JNI::CallNonvirtualObjectMethodV,
2333 JNI::CallNonvirtualObjectMethodA,
2334 JNI::CallNonvirtualBooleanMethod,
2335 JNI::CallNonvirtualBooleanMethodV,
2336 JNI::CallNonvirtualBooleanMethodA,
2337 JNI::CallNonvirtualByteMethod,
2338 JNI::CallNonvirtualByteMethodV,
2339 JNI::CallNonvirtualByteMethodA,
2340 JNI::CallNonvirtualCharMethod,
2341 JNI::CallNonvirtualCharMethodV,
2342 JNI::CallNonvirtualCharMethodA,
2343 JNI::CallNonvirtualShortMethod,
2344 JNI::CallNonvirtualShortMethodV,
2345 JNI::CallNonvirtualShortMethodA,
2346 JNI::CallNonvirtualIntMethod,
2347 JNI::CallNonvirtualIntMethodV,
2348 JNI::CallNonvirtualIntMethodA,
2349 JNI::CallNonvirtualLongMethod,
2350 JNI::CallNonvirtualLongMethodV,
2351 JNI::CallNonvirtualLongMethodA,
2352 JNI::CallNonvirtualFloatMethod,
2353 JNI::CallNonvirtualFloatMethodV,
2354 JNI::CallNonvirtualFloatMethodA,
2355 JNI::CallNonvirtualDoubleMethod,
2356 JNI::CallNonvirtualDoubleMethodV,
2357 JNI::CallNonvirtualDoubleMethodA,
2358 JNI::CallNonvirtualVoidMethod,
2359 JNI::CallNonvirtualVoidMethodV,
2360 JNI::CallNonvirtualVoidMethodA,
2361 JNI::GetFieldID,
2362 JNI::GetObjectField,
2363 JNI::GetBooleanField,
2364 JNI::GetByteField,
2365 JNI::GetCharField,
2366 JNI::GetShortField,
2367 JNI::GetIntField,
2368 JNI::GetLongField,
2369 JNI::GetFloatField,
2370 JNI::GetDoubleField,
2371 JNI::SetObjectField,
2372 JNI::SetBooleanField,
2373 JNI::SetByteField,
2374 JNI::SetCharField,
2375 JNI::SetShortField,
2376 JNI::SetIntField,
2377 JNI::SetLongField,
2378 JNI::SetFloatField,
2379 JNI::SetDoubleField,
2380 JNI::GetStaticMethodID,
2381 JNI::CallStaticObjectMethod,
2382 JNI::CallStaticObjectMethodV,
2383 JNI::CallStaticObjectMethodA,
2384 JNI::CallStaticBooleanMethod,
2385 JNI::CallStaticBooleanMethodV,
2386 JNI::CallStaticBooleanMethodA,
2387 JNI::CallStaticByteMethod,
2388 JNI::CallStaticByteMethodV,
2389 JNI::CallStaticByteMethodA,
2390 JNI::CallStaticCharMethod,
2391 JNI::CallStaticCharMethodV,
2392 JNI::CallStaticCharMethodA,
2393 JNI::CallStaticShortMethod,
2394 JNI::CallStaticShortMethodV,
2395 JNI::CallStaticShortMethodA,
2396 JNI::CallStaticIntMethod,
2397 JNI::CallStaticIntMethodV,
2398 JNI::CallStaticIntMethodA,
2399 JNI::CallStaticLongMethod,
2400 JNI::CallStaticLongMethodV,
2401 JNI::CallStaticLongMethodA,
2402 JNI::CallStaticFloatMethod,
2403 JNI::CallStaticFloatMethodV,
2404 JNI::CallStaticFloatMethodA,
2405 JNI::CallStaticDoubleMethod,
2406 JNI::CallStaticDoubleMethodV,
2407 JNI::CallStaticDoubleMethodA,
2408 JNI::CallStaticVoidMethod,
2409 JNI::CallStaticVoidMethodV,
2410 JNI::CallStaticVoidMethodA,
2411 JNI::GetStaticFieldID,
2412 JNI::GetStaticObjectField,
2413 JNI::GetStaticBooleanField,
2414 JNI::GetStaticByteField,
2415 JNI::GetStaticCharField,
2416 JNI::GetStaticShortField,
2417 JNI::GetStaticIntField,
2418 JNI::GetStaticLongField,
2419 JNI::GetStaticFloatField,
2420 JNI::GetStaticDoubleField,
2421 JNI::SetStaticObjectField,
2422 JNI::SetStaticBooleanField,
2423 JNI::SetStaticByteField,
2424 JNI::SetStaticCharField,
2425 JNI::SetStaticShortField,
2426 JNI::SetStaticIntField,
2427 JNI::SetStaticLongField,
2428 JNI::SetStaticFloatField,
2429 JNI::SetStaticDoubleField,
2430 JNI::NewString,
2431 JNI::GetStringLength,
2432 JNI::GetStringChars,
2433 JNI::ReleaseStringChars,
2434 JNI::NewStringUTF,
2435 JNI::GetStringUTFLength,
2436 JNI::GetStringUTFChars,
2437 JNI::ReleaseStringUTFChars,
2438 JNI::GetArrayLength,
2439 JNI::NewObjectArray,
2440 JNI::GetObjectArrayElement,
2441 JNI::SetObjectArrayElement,
2442 JNI::NewBooleanArray,
2443 JNI::NewByteArray,
2444 JNI::NewCharArray,
2445 JNI::NewShortArray,
2446 JNI::NewIntArray,
2447 JNI::NewLongArray,
2448 JNI::NewFloatArray,
2449 JNI::NewDoubleArray,
2450 JNI::GetBooleanArrayElements,
2451 JNI::GetByteArrayElements,
2452 JNI::GetCharArrayElements,
2453 JNI::GetShortArrayElements,
2454 JNI::GetIntArrayElements,
2455 JNI::GetLongArrayElements,
2456 JNI::GetFloatArrayElements,
2457 JNI::GetDoubleArrayElements,
2458 JNI::ReleaseBooleanArrayElements,
2459 JNI::ReleaseByteArrayElements,
2460 JNI::ReleaseCharArrayElements,
2461 JNI::ReleaseShortArrayElements,
2462 JNI::ReleaseIntArrayElements,
2463 JNI::ReleaseLongArrayElements,
2464 JNI::ReleaseFloatArrayElements,
2465 JNI::ReleaseDoubleArrayElements,
2466 JNI::GetBooleanArrayRegion,
2467 JNI::GetByteArrayRegion,
2468 JNI::GetCharArrayRegion,
2469 JNI::GetShortArrayRegion,
2470 JNI::GetIntArrayRegion,
2471 JNI::GetLongArrayRegion,
2472 JNI::GetFloatArrayRegion,
2473 JNI::GetDoubleArrayRegion,
2474 JNI::SetBooleanArrayRegion,
2475 JNI::SetByteArrayRegion,
2476 JNI::SetCharArrayRegion,
2477 JNI::SetShortArrayRegion,
2478 JNI::SetIntArrayRegion,
2479 JNI::SetLongArrayRegion,
2480 JNI::SetFloatArrayRegion,
2481 JNI::SetDoubleArrayRegion,
2482 JNI::RegisterNatives,
2483 JNI::UnregisterNatives,
2484 JNI::MonitorEnter,
2485 JNI::MonitorExit,
2486 JNI::GetJavaVM,
2487 JNI::GetStringRegion,
2488 JNI::GetStringUTFRegion,
2489 JNI::GetPrimitiveArrayCritical,
2490 JNI::ReleasePrimitiveArrayCritical,
2491 JNI::GetStringCritical,
2492 JNI::ReleaseStringCritical,
2493 JNI::NewWeakGlobalRef,
2494 JNI::DeleteWeakGlobalRef,
2495 JNI::ExceptionCheck,
2496 JNI::NewDirectByteBuffer,
2497 JNI::GetDirectBufferAddress,
2498 JNI::GetDirectBufferCapacity,
2499 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002500};
2501
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002502static const size_t kMonitorsInitial = 32; // Arbitrary.
2503static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2504
2505static const size_t kLocalsInitial = 64; // Arbitrary.
2506static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002507
Elliott Hughes75770752011-08-24 17:52:38 -07002508JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002509 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002510 vm(vm),
2511 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002512 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002513 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002514 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2515 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002516 functions = unchecked_functions = &gNativeInterface;
2517 if (check_jni) {
2518 functions = GetCheckJniNativeInterface();
2519 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002520}
2521
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002522JNIEnvExt::~JNIEnvExt() {
2523}
2524
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002525void JNIEnvExt::DumpReferenceTables() {
2526 locals.Dump();
2527 monitors.Dump();
2528}
2529
Carl Shapiroea4dca82011-08-01 13:45:38 -07002530// JNI Invocation interface.
2531
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002532extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2533 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2534 if (args->version < JNI_VERSION_1_2) {
2535 return JNI_EVERSION;
2536 }
2537 Runtime::Options options;
2538 for (int i = 0; i < args->nOptions; ++i) {
2539 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002540 options.push_back(std::make_pair(StringPiece(option->optionString),
2541 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002542 }
2543 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002544 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002545 if (runtime == NULL) {
2546 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002547 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002548 runtime->Start();
2549 *p_env = Thread::Current()->GetJniEnv();
2550 *p_vm = runtime->GetJavaVM();
2551 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002552}
2553
Elliott Hughesf2682d52011-08-15 16:37:04 -07002554extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002555 Runtime* runtime = Runtime::Current();
2556 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002557 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002558 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002559 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002560 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002561 }
2562 return JNI_OK;
2563}
2564
2565// Historically unsupported.
2566extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2567 return JNI_ERR;
2568}
2569
Elliott Hughescdf53122011-08-19 15:46:09 -07002570class JII {
2571 public:
2572 static jint DestroyJavaVM(JavaVM* vm) {
2573 if (vm == NULL) {
2574 return JNI_ERR;
2575 } else {
2576 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2577 delete raw_vm->runtime;
2578 return JNI_OK;
2579 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002580 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002581
Elliott Hughescdf53122011-08-19 15:46:09 -07002582 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002583 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002584 }
2585
2586 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002587 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002588 }
2589
2590 static jint DetachCurrentThread(JavaVM* vm) {
2591 if (vm == NULL) {
2592 return JNI_ERR;
2593 } else {
2594 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2595 Runtime* runtime = raw_vm->runtime;
2596 runtime->DetachCurrentThread();
2597 return JNI_OK;
2598 }
2599 }
2600
2601 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2602 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2603 return JNI_EVERSION;
2604 }
2605 if (vm == NULL || env == NULL) {
2606 return JNI_ERR;
2607 }
2608 Thread* thread = Thread::Current();
2609 if (thread == NULL) {
2610 *env = NULL;
2611 return JNI_EDETACHED;
2612 }
2613 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002614 return JNI_OK;
2615 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002616};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002617
Elliott Hughesa2501992011-08-26 19:39:54 -07002618const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002619 NULL, // reserved0
2620 NULL, // reserved1
2621 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002622 JII::DestroyJavaVM,
2623 JII::AttachCurrentThread,
2624 JII::DetachCurrentThread,
2625 JII::GetEnv,
2626 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002627};
2628
Elliott Hughesbbd76712011-08-17 10:25:24 -07002629static const size_t kPinTableInitialSize = 16;
2630static const size_t kPinTableMaxSize = 1024;
2631
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002632static const size_t kGlobalsInitial = 512; // Arbitrary.
2633static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2634
2635static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2636static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2637
Elliott Hughesa0957642011-09-02 14:27:33 -07002638JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002639 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002640 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002641 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002642 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002643 verbose_jni(options->IsVerbose("jni")),
2644 log_third_party_jni(options->IsVerbose("third-party-jni")),
2645 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002646 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002647 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002648 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002649 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002650 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002651 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002652 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002653 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002654 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002655 functions = unchecked_functions = &gInvokeInterface;
2656 if (check_jni) {
2657 functions = GetCheckJniInvokeInterface();
2658 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002659}
2660
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002661JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002662 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002663}
2664
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002665void JavaVMExt::DumpReferenceTables() {
2666 {
2667 MutexLock mu(globals_lock);
2668 globals.Dump();
2669 }
2670 {
2671 MutexLock mu(weak_globals_lock);
2672 weak_globals.Dump();
2673 }
2674 {
2675 MutexLock mu(pins_lock);
2676 pin_table.Dump();
2677 }
2678}
2679
Elliott Hughes75770752011-08-24 17:52:38 -07002680bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2681 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002682
2683 // See if we've already loaded this library. If we have, and the class loader
2684 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002685 // TODO: for better results we should canonicalize the pathname (or even compare
2686 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002687 SharedLibrary* library;
2688 {
2689 // TODO: move the locking (and more of this logic) into Libraries.
2690 MutexLock mu(libraries_lock);
2691 library = libraries->Get(path);
2692 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002693 if (library != NULL) {
2694 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002695 // The library will be associated with class_loader. The JNI
2696 // spec says we can't load the same library into more than one
2697 // class loader.
2698 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2699 "ClassLoader %p; can't open in ClassLoader %p",
2700 path.c_str(), library->GetClassLoader(), class_loader);
2701 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002702 return false;
2703 }
2704 if (verbose_jni) {
2705 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2706 << "ClassLoader " << class_loader << "]";
2707 }
2708 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002709 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2710 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002711 return false;
2712 }
2713 return true;
2714 }
2715
2716 // Open the shared library. Because we're using a full path, the system
2717 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2718 // resolve this library's dependencies though.)
2719
2720 // Failures here are expected when java.library.path has several entries
2721 // and we have to hunt for the lib.
2722
2723 // The current version of the dynamic linker prints detailed information
2724 // about dlopen() failures. Some things to check if the message is
2725 // cryptic:
2726 // - make sure the library exists on the device
2727 // - verify that the right path is being opened (the debug log message
2728 // above can help with that)
2729 // - check to see if the library is valid (e.g. not zero bytes long)
2730 // - check config/prelink-linux-arm.map to ensure that the library
2731 // is listed and is not being overrun by the previous entry (if
2732 // loading suddenly stops working on a prelinked library, this is
2733 // a good one to check)
2734 // - write a trivial app that calls sleep() then dlopen(), attach
2735 // to it with "strace -p <pid>" while it sleeps, and watch for
2736 // attempts to open nonexistent dependent shared libs
2737
2738 // TODO: automate some of these checks!
2739
2740 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002741 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002742 // the GC to ignore us.
2743 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002744 void* handle = NULL;
2745 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002746 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002747 handle = dlopen(path.c_str(), RTLD_LAZY);
2748 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002749
2750 if (verbose_jni) {
2751 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2752 }
2753
2754 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002755 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002756 return false;
2757 }
2758
2759 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002760 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002761 // TODO: move the locking (and more of this logic) into Libraries.
2762 MutexLock mu(libraries_lock);
2763 library = libraries->Get(path);
2764 if (library != NULL) {
2765 LOG(INFO) << "WOW: we lost a race to add shared library: "
2766 << "\"" << path << "\" ClassLoader=" << class_loader;
2767 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002768 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002769 library = new SharedLibrary(path, handle, class_loader);
2770 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002771 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002772
2773 if (verbose_jni) {
2774 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2775 }
2776
2777 bool result = true;
2778 void* sym = dlsym(handle, "JNI_OnLoad");
2779 if (sym == NULL) {
2780 if (verbose_jni) {
2781 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2782 }
2783 } else {
2784 // Call JNI_OnLoad. We have to override the current class
2785 // loader, which will always be "null" since the stuff at the
2786 // top of the stack is around Runtime.loadLibrary(). (See
2787 // the comments in the JNI FindClass function.)
2788 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2789 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002790 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002791 self->SetClassLoaderOverride(class_loader);
2792
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002793 int version = 0;
2794 {
2795 ScopedThreadStateChange tsc(self, Thread::kNative);
2796 if (verbose_jni) {
2797 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2798 }
2799 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002800 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002801
2802 self->SetClassLoaderOverride(old_class_loader);;
2803
2804 if (version != JNI_VERSION_1_2 &&
2805 version != JNI_VERSION_1_4 &&
2806 version != JNI_VERSION_1_6) {
2807 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2808 << "bad version: " << version;
2809 // It's unwise to call dlclose() here, but we can mark it
2810 // as bad and ensure that future load attempts will fail.
2811 // We don't know how far JNI_OnLoad got, so there could
2812 // be some partially-initialized stuff accessible through
2813 // newly-registered native method calls. We could try to
2814 // unregister them, but that doesn't seem worthwhile.
2815 result = false;
2816 } else {
2817 if (verbose_jni) {
2818 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2819 << " from JNI_OnLoad in \"" << path << "\"]";
2820 }
2821 }
2822 }
2823
2824 library->SetResult(result);
2825 return result;
2826}
2827
2828void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2829 CHECK(m->IsNative());
2830
2831 Class* c = m->GetDeclaringClass();
2832
2833 // If this is a static method, it could be called before the class
2834 // has been initialized.
2835 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002836 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002837 return NULL;
2838 }
2839 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002840 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002841 }
2842
Brian Carlstrom16192862011-09-12 17:50:06 -07002843 std::string detail;
2844 void* native_method;
2845 {
2846 MutexLock mu(libraries_lock);
2847 native_method = libraries->FindNativeMethod(m, detail);
2848 }
2849 // throwing can cause libraries_lock to be reacquired
2850 if (native_method == NULL) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002851 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", "%s", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002852 }
2853 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002854}
2855
Elliott Hughes410c0c82011-09-01 17:58:25 -07002856void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2857 {
2858 MutexLock mu(globals_lock);
2859 globals.VisitRoots(visitor, arg);
2860 }
2861 {
2862 MutexLock mu(pins_lock);
2863 pin_table.VisitRoots(visitor, arg);
2864 }
2865 // The weak_globals table is visited by the GC itself (because it mutates the table).
2866}
2867
Ian Rogersdf20fe02011-07-20 20:34:16 -07002868} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002869
2870std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2871 switch (rhs) {
2872 case JNIInvalidRefType:
2873 os << "JNIInvalidRefType";
2874 return os;
2875 case JNILocalRefType:
2876 os << "JNILocalRefType";
2877 return os;
2878 case JNIGlobalRefType:
2879 os << "JNIGlobalRefType";
2880 return os;
2881 case JNIWeakGlobalRefType:
2882 os << "JNIWeakGlobalRefType";
2883 return os;
2884 }
2885}