blob: 96b3b3449217e0671b41ac3bd7b8cd81323d85c6 [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 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700344 // Check invariant that all jfieldIDs have resolved types (how else would
345 // the type equality in Find...Field hold?)
346 DCHECK(field->GetType() != NULL);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700347 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700348}
349
Elliott Hughes75770752011-08-24 17:52:38 -0700350void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
351 JavaVMExt* vm = ts.Vm();
352 MutexLock mu(vm->pins_lock);
353 vm->pin_table.Add(array);
354}
355
356void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
357 JavaVMExt* vm = ts.Vm();
358 MutexLock mu(vm->pins_lock);
359 vm->pin_table.Remove(array);
360}
361
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700362template<typename JniT, typename ArtT>
363JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
364 CHECK_GE(length, 0); // TODO: ReportJniError
365 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700366 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700367}
368
Elliott Hughes75770752011-08-24 17:52:38 -0700369template <typename ArrayT, typename CArrayT, typename ArtArrayT>
370CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
371 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
372 PinPrimitiveArray(ts, array);
373 if (is_copy != NULL) {
374 *is_copy = JNI_FALSE;
375 }
376 return array->GetData();
377}
378
379template <typename ArrayT>
380void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
381 if (mode != JNI_COMMIT) {
382 Array* array = Decode<Array*>(ts, java_array);
383 UnpinPrimitiveArray(ts, array);
384 }
385}
386
Elliott Hughes814e4032011-08-23 12:07:56 -0700387void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700388 std::string type(PrettyTypeOf(array));
Elliott Hughes814e4032011-08-23 12:07:56 -0700389 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
390 "%s offset=%d length=%d %s.length=%d",
391 type.c_str(), start, length, identifier, array->GetLength());
392}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700393void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
394 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
395 "offset=%d length=%d string.length()=%d", start, length, array_length);
396}
Elliott Hughes814e4032011-08-23 12:07:56 -0700397
398template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700399void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700400 ArrayT* array = Decode<ArrayT*>(ts, java_array);
401 if (start < 0 || length < 0 || start + length > array->GetLength()) {
402 ThrowAIOOBE(ts, array, start, length, "src");
403 } else {
404 JavaT* data = array->GetData();
405 memcpy(buf, data + start, length * sizeof(JavaT));
406 }
407}
408
409template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700410void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700411 ArrayT* array = Decode<ArrayT*>(ts, java_array);
412 if (start < 0 || length < 0 || start + length > array->GetLength()) {
413 ThrowAIOOBE(ts, array, start, length, "dst");
414 } else {
415 JavaT* data = array->GetData();
416 memcpy(data + start, buf, length * sizeof(JavaT));
417 }
418}
419
Elliott Hughes75770752011-08-24 17:52:38 -0700420jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700421 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
422 CHECK(buffer_class.get() != NULL);
423 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
424}
425
Elliott Hughes75770752011-08-24 17:52:38 -0700426jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700427 static jclass buffer_class = InitDirectByteBufferClass(env);
428 return buffer_class;
429}
430
Elliott Hughes75770752011-08-24 17:52:38 -0700431jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
432 if (vm == NULL || p_env == NULL) {
433 return JNI_ERR;
434 }
435
436 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
437 JavaVMAttachArgs args;
438 if (thr_args == NULL) {
439 // Allow the v1.1 calling convention.
440 args.version = JNI_VERSION_1_2;
441 args.name = NULL;
442 args.group = NULL; // TODO: get "main" thread group
443 } else {
444 args.version = in_args->version;
445 args.name = in_args->name;
446 if (in_args->group != NULL) {
447 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
448 args.group = NULL; // TODO: decode in_args->group
449 } else {
450 args.group = NULL; // TODO: get "main" thread group
451 }
452 }
453 CHECK_GE(args.version, JNI_VERSION_1_2);
454
455 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700456 runtime->AttachCurrentThread(args.name, as_daemon);
457 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700458 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700459}
460
Elliott Hughes79082e32011-08-25 12:07:32 -0700461class SharedLibrary {
462 public:
463 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
464 : path_(path),
465 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700466 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700467 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700468 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700469 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700470 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700471 }
472
Elliott Hughes79082e32011-08-25 12:07:32 -0700473 Object* GetClassLoader() {
474 return class_loader_;
475 }
476
477 std::string GetPath() {
478 return path_;
479 }
480
481 /*
482 * Check the result of an earlier call to JNI_OnLoad on this library. If
483 * the call has not yet finished in another thread, wait for it.
484 */
485 bool CheckOnLoadResult(JavaVMExt* vm) {
486 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700487 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700488 // Check this so we don't end up waiting for ourselves. We need
489 // to return "true" so the caller can continue.
490 LOG(INFO) << *self << " recursive attempt to load library "
491 << "\"" << path_ << "\"";
492 return true;
493 }
494
495 MutexLock mu(jni_on_load_lock_);
496 while (jni_on_load_result_ == kPending) {
497 if (vm->verbose_jni) {
498 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
499 << "JNI_OnLoad...]";
500 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700501 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700502 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700503 }
504
505 bool okay = (jni_on_load_result_ == kOkay);
506 if (vm->verbose_jni) {
507 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
508 << (okay ? "succeeded" : "failed") << "]";
509 }
510 return okay;
511 }
512
513 void SetResult(bool result) {
514 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700515 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700516
517 // Broadcast a wakeup to anybody sleeping on the condition variable.
518 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700519 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700520 }
521
522 void* FindSymbol(const std::string& symbol_name) {
523 return dlsym(handle_, symbol_name.c_str());
524 }
525
526 private:
527 enum JNI_OnLoadState {
528 kPending,
529 kFailed,
530 kOkay,
531 };
532
533 // Path to library "/system/lib/libjni.so".
534 std::string path_;
535
536 // The void* returned by dlopen(3).
537 void* handle_;
538
539 // The ClassLoader this library is associated with.
540 Object* class_loader_;
541
542 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700543 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700544 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700545 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700547 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700548 // Result of earlier JNI_OnLoad call.
549 JNI_OnLoadState jni_on_load_result_;
550};
551
Elliott Hughescdf53122011-08-19 15:46:09 -0700552} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700553
Elliott Hughes79082e32011-08-25 12:07:32 -0700554// This exists mainly to keep implementation details out of the header file.
555class Libraries {
556 public:
557 Libraries() {
558 }
559
560 ~Libraries() {
561 // Delete our map values. (The keys will be cleaned up by the map itself.)
562 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
563 delete it->second;
564 }
565 }
566
567 SharedLibrary* Get(const std::string& path) {
568 return libraries_[path];
569 }
570
571 void Put(const std::string& path, SharedLibrary* library) {
572 libraries_[path] = library;
573 }
574
575 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700576 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700577 std::string jni_short_name(JniShortName(m));
578 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700579 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700580 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
581 SharedLibrary* library = it->second;
582 if (library->GetClassLoader() != declaring_class_loader) {
583 // We only search libraries loaded by the appropriate ClassLoader.
584 continue;
585 }
586 // Try the short name then the long name...
587 void* fn = library->FindSymbol(jni_short_name);
588 if (fn == NULL) {
589 fn = library->FindSymbol(jni_long_name);
590 }
591 if (fn != NULL) {
592 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700593 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700594 << " in \"" << library->GetPath() << "\"]";
595 }
596 return fn;
597 }
598 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700599 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700600 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700601 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700602 return NULL;
603 }
604
605 private:
606 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
607
608 std::map<std::string, SharedLibrary*> libraries_;
609};
610
Elliott Hughes418d20f2011-09-22 14:00:39 -0700611JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
612 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
613 Object* receiver = Decode<Object*>(env, obj);
614 Method* method = DecodeMethod(mid);
615 UniquePtr<byte[]> arg_array(CreateArgArray(env, method, args));
616 return InvokeWithArgArray(env, receiver, method, arg_array.get());
617}
618
Elliott Hughescdf53122011-08-19 15:46:09 -0700619class JNI {
620 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700621
Elliott Hughescdf53122011-08-19 15:46:09 -0700622 static jint GetVersion(JNIEnv* env) {
623 ScopedJniThreadState ts(env);
624 return JNI_VERSION_1_6;
625 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700626
Elliott Hughescdf53122011-08-19 15:46:09 -0700627 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
628 ScopedJniThreadState ts(env);
629 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700630 return NULL;
631 }
632
Elliott Hughescdf53122011-08-19 15:46:09 -0700633 static jclass FindClass(JNIEnv* env, const char* name) {
634 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700635 Runtime* runtime = Runtime::Current();
636 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700638 Class* c = NULL;
639 if (runtime->IsStarted()) {
640 // TODO: need to get the appropriate ClassLoader.
641 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
642 c = class_linker->FindClass(descriptor, cl);
643 } else {
644 c = class_linker->FindSystemClass(descriptor);
645 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700646 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700647 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700648
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
650 ScopedJniThreadState ts(env);
651 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700652 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700653 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700654
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
656 ScopedJniThreadState ts(env);
657 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700658 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700659 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700660
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
662 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700663 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700664 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700665 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700666
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
668 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700669 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700670 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700672
Elliott Hughes37f7a402011-08-22 18:56:01 -0700673 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
674 ScopedJniThreadState ts(env);
675 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700676 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700677 }
678
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700679 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700681 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700682 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700684
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700687 Class* c1 = Decode<Class*>(ts, java_class1);
688 Class* c2 = Decode<Class*>(ts, java_class2);
689 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700691
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700694 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700695 if (jobj == NULL) {
696 // NB. JNI is different from regular Java instanceof in this respect
697 return JNI_TRUE;
698 } else {
699 Object* obj = Decode<Object*>(ts, jobj);
700 Class* klass = Decode<Class*>(ts, clazz);
701 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
702 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700704
Elliott Hughes37f7a402011-08-22 18:56:01 -0700705 static jint Throw(JNIEnv* env, jthrowable java_exception) {
706 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700707 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 if (exception == NULL) {
709 return JNI_ERR;
710 }
711 ts.Self()->SetException(exception);
712 return JNI_OK;
713 }
714
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700715 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700716 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700717 // TODO: check for a pending exception to decide what constructor to call.
718 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
719 if (mid == NULL) {
720 return JNI_ERR;
721 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700722 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
723 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700724 return JNI_ERR;
725 }
726
727 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700728 args[0].l = s.get();
729 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
730 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700731 return JNI_ERR;
732 }
733
Elliott Hughes54e7df12011-09-16 11:47:04 -0700734 LOG(INFO) << "Throwing " << PrettyTypeOf(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700735 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700736 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700737
Elliott Hughes37f7a402011-08-22 18:56:01 -0700738 return JNI_OK;
739 }
740
741 static jboolean ExceptionCheck(JNIEnv* env) {
742 ScopedJniThreadState ts(env);
743 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
744 }
745
746 static void ExceptionClear(JNIEnv* env) {
747 ScopedJniThreadState ts(env);
748 ts.Self()->ClearException();
749 }
750
751 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700753
754 Thread* self = ts.Self();
755 Throwable* original_exception = self->GetException();
756 self->ClearException();
757
Elliott Hughesbf86d042011-08-31 17:53:14 -0700758 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700759 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
760 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
761 if (mid == NULL) {
762 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700763 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700764 } else {
765 env->CallVoidMethod(exception.get(), mid);
766 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700767 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700768 << " thrown while calling printStackTrace";
769 self->ClearException();
770 }
771 }
772
773 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700775
Elliott Hughescdf53122011-08-19 15:46:09 -0700776 static jthrowable ExceptionOccurred(JNIEnv* env) {
777 ScopedJniThreadState ts(env);
778 Object* exception = ts.Self()->GetException();
779 if (exception == NULL) {
780 return NULL;
781 } else {
782 // TODO: if adding a local reference failing causes the VM to abort
783 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700784 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 if (localException == NULL) {
786 // We were unable to add a new local reference, and threw a new
787 // exception. We can't return "exception", because it's not a
788 // local reference. So we have to return NULL, indicating that
789 // there was no exception, even though it's pretty much raining
790 // exceptions in here.
791 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
792 }
793 return localException;
794 }
795 }
796
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 static void FatalError(JNIEnv* env, const char* msg) {
798 ScopedJniThreadState ts(env);
799 LOG(FATAL) << "JNI FatalError called: " << msg;
800 }
801
802 static jint PushLocalFrame(JNIEnv* env, jint cap) {
803 ScopedJniThreadState ts(env);
804 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
805 return JNI_OK;
806 }
807
808 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
809 ScopedJniThreadState ts(env);
810 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
811 return res;
812 }
813
Elliott Hughes72025e52011-08-23 17:50:30 -0700814 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
815 ScopedJniThreadState ts(env);
816 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
817 return 0;
818 }
819
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
821 ScopedJniThreadState ts(env);
822 if (obj == NULL) {
823 return NULL;
824 }
825
Elliott Hughes75770752011-08-24 17:52:38 -0700826 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 IndirectReferenceTable& globals = vm->globals;
828 MutexLock mu(vm->globals_lock);
829 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
830 return reinterpret_cast<jobject>(ref);
831 }
832
833 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
834 ScopedJniThreadState ts(env);
835 if (obj == NULL) {
836 return;
837 }
838
Elliott Hughes75770752011-08-24 17:52:38 -0700839 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 IndirectReferenceTable& globals = vm->globals;
841 MutexLock mu(vm->globals_lock);
842
843 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
844 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
845 << "failed to find entry";
846 }
847 }
848
849 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
850 ScopedJniThreadState ts(env);
851 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
852 }
853
854 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
855 ScopedJniThreadState ts(env);
856 if (obj == NULL) {
857 return;
858 }
859
Elliott Hughes75770752011-08-24 17:52:38 -0700860 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 IndirectReferenceTable& weak_globals = vm->weak_globals;
862 MutexLock mu(vm->weak_globals_lock);
863
864 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
865 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
866 << "failed to find entry";
867 }
868 }
869
870 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
871 ScopedJniThreadState ts(env);
872 if (obj == NULL) {
873 return NULL;
874 }
875
876 IndirectReferenceTable& locals = ts.Env()->locals;
877
878 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
879 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
880 return reinterpret_cast<jobject>(ref);
881 }
882
883 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
884 ScopedJniThreadState ts(env);
885 if (obj == NULL) {
886 return;
887 }
888
889 IndirectReferenceTable& locals = ts.Env()->locals;
890
891 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
892 if (!locals.Remove(cookie, obj)) {
893 // Attempting to delete a local reference that is not in the
894 // topmost local reference frame is a no-op. DeleteLocalRef returns
895 // void and doesn't throw any exceptions, but we should probably
896 // complain about it so the user will notice that things aren't
897 // going quite the way they expect.
898 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
899 << "failed to find entry";
900 }
901 }
902
903 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
904 ScopedJniThreadState ts(env);
905 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
906 ? JNI_TRUE : JNI_FALSE;
907 }
908
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700909 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700911 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700912 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700913 return NULL;
914 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700915 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 }
917
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 ScopedJniThreadState ts(env);
920 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 va_start(args, mid);
922 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 va_end(args);
924 return result;
925 }
926
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700929 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700930 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700931 return NULL;
932 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700933 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700934 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700935 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 return local_result;
937 }
938
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700941 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700942 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700943 return NULL;
944 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700945 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700946 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 return local_result;
949 }
950
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
952 ScopedJniThreadState ts(env);
953 return FindMethodID(ts, c, name, sig, false);
954 }
955
956 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
957 ScopedJniThreadState ts(env);
958 return FindMethodID(ts, c, name, sig, true);
959 }
960
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 va_list ap;
964 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700965 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700967 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700972 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700973 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700978 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700979 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 }
981
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 va_list ap;
985 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700986 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 va_end(ap);
988 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 }
990
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700993 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700998 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 va_list ap;
1004 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001005 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 va_end(ap);
1007 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001012 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001017 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 }
1019
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_list ap;
1023 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001024 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 va_end(ap);
1026 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001031 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001036 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 va_list ap;
1042 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001043 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 va_end(ap);
1045 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001050 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001055 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_list ap;
1061 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001062 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_end(ap);
1064 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001069 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001074 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 va_list ap;
1080 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001081 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 va_end(ap);
1083 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001088 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001093 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 va_list ap;
1099 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001100 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_end(ap);
1102 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001107 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001112 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_list ap;
1118 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001119 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 va_end(ap);
1121 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001126 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001131 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 va_list ap;
1137 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001138 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001144 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001149 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
1152 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 ScopedJniThreadState ts(env);
1155 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001157 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001158 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 va_end(ap);
1160 return local_result;
1161 }
1162
1163 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001166 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001167 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
1170 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001173 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001174 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
1177 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 ScopedJniThreadState ts(env);
1180 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001182 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 va_end(ap);
1184 return result.z;
1185 }
1186
1187 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001190 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
1193 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001196 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
1199 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 ScopedJniThreadState ts(env);
1202 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001204 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 va_end(ap);
1206 return result.b;
1207 }
1208
1209 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001212 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
1215 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001218 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
1221 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
1224 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001226 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 va_end(ap);
1228 return result.c;
1229 }
1230
1231 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001232 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001234 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 }
1236
1237 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001240 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
1243 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001244 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 ScopedJniThreadState ts(env);
1246 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001248 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 va_end(ap);
1250 return result.s;
1251 }
1252
1253 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001256 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
1259 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001262 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 }
1264
Elliott Hughes418d20f2011-09-22 14:00:39 -07001265 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 ScopedJniThreadState ts(env);
1267 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001269 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 va_end(ap);
1271 return result.i;
1272 }
1273
1274 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001277 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
1280 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001283 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
1286 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 ScopedJniThreadState ts(env);
1289 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001291 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 va_end(ap);
1293 return result.j;
1294 }
1295
1296 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001299 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
1302 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001305 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
1308 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 ScopedJniThreadState ts(env);
1311 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001313 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 va_end(ap);
1315 return result.f;
1316 }
1317
1318 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001319 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001321 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
1324 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001327 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
1330 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 ScopedJniThreadState ts(env);
1333 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001335 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 va_end(ap);
1337 return result.d;
1338 }
1339
1340 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001343 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 }
1345
1346 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001349 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
1352 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 ScopedJniThreadState ts(env);
1355 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001356 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001357 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 va_end(ap);
1359 }
1360
1361 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001364 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
1367 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001368 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001370 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001373 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 ScopedJniThreadState ts(env);
1375 return FindFieldID(ts, c, name, sig, false);
1376 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001377
1378
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001379 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 ScopedJniThreadState ts(env);
1381 return FindFieldID(ts, c, name, sig, true);
1382 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001383
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001384 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001387 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001388 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001390
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001391 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001393 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001394 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
1396
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001397 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399 Object* o = Decode<Object*>(ts, java_object);
1400 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001401 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 }
1404
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001405 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001408 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001409 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001412#define GET_PRIMITIVE_FIELD(fn, instance) \
1413 ScopedJniThreadState ts(env); \
1414 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001415 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001416 return f->fn(o)
1417
1418#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1419 ScopedJniThreadState ts(env); \
1420 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001421 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001422 f->fn(o, value)
1423
1424 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1425 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001428 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1429 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1433 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1437 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1441 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001444 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1445 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1449 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1453 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1457 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1461 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1465 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001468 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1469 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 }
1471
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001472 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1473 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1477 GET_PRIMITIVE_FIELD(GetLong, NULL);
1478 }
1479
1480 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1481 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1482 }
1483
1484 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1485 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1486 }
1487
1488 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1489 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1490 }
1491
1492 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1493 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1494 }
1495
1496 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1497 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1498 }
1499
1500 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1501 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1502 }
1503
1504 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1505 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1506 }
1507
1508 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1509 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1510 }
1511
1512 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1513 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1514 }
1515
1516 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1517 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1518 }
1519
1520 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1521 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1522 }
1523
1524 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1525 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1526 }
1527
1528 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1529 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1530 }
1531
1532 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1533 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1534 }
1535
1536 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1537 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1538 }
1539
1540 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1541 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1542 }
1543
1544 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1545 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1546 }
1547
1548 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1549 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 }
1551
Elliott Hughes418d20f2011-09-22 14:00:39 -07001552 static jobject CallStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 ScopedJniThreadState ts(env);
1554 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001555 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001556 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001557 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 va_end(ap);
1559 return local_result;
1560 }
1561
Elliott Hughes418d20f2011-09-22 14:00:39 -07001562 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001564 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001565 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes418d20f2011-09-22 14:00:39 -07001568 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001570 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001571 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001572 }
1573
Elliott Hughes418d20f2011-09-22 14:00:39 -07001574 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 ScopedJniThreadState ts(env);
1576 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001577 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001578 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 va_end(ap);
1580 return result.z;
1581 }
1582
Elliott Hughes418d20f2011-09-22 14:00:39 -07001583 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001585 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
Elliott Hughes418d20f2011-09-22 14:00:39 -07001588 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001590 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes72025e52011-08-23 17:50:30 -07001593 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 ScopedJniThreadState ts(env);
1595 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001596 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001597 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 va_end(ap);
1599 return result.b;
1600 }
1601
Elliott Hughes418d20f2011-09-22 14:00:39 -07001602 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001604 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes418d20f2011-09-22 14:00:39 -07001607 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001609 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes72025e52011-08-23 17:50:30 -07001612 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 ScopedJniThreadState ts(env);
1614 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001616 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 va_end(ap);
1618 return result.c;
1619 }
1620
Elliott Hughes418d20f2011-09-22 14:00:39 -07001621 static jchar CallStaticCharMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001623 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes418d20f2011-09-22 14:00:39 -07001626 static jchar CallStaticCharMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001628 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes72025e52011-08-23 17:50:30 -07001631 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 ScopedJniThreadState ts(env);
1633 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001635 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 va_end(ap);
1637 return result.s;
1638 }
1639
Elliott Hughes418d20f2011-09-22 14:00:39 -07001640 static jshort CallStaticShortMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001642 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes418d20f2011-09-22 14:00:39 -07001645 static jshort CallStaticShortMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001647 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes72025e52011-08-23 17:50:30 -07001650 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 ScopedJniThreadState ts(env);
1652 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001654 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 va_end(ap);
1656 return result.i;
1657 }
1658
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 static jint CallStaticIntMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001661 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
Elliott Hughes418d20f2011-09-22 14:00:39 -07001664 static jint CallStaticIntMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001666 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes72025e52011-08-23 17:50:30 -07001669 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 ScopedJniThreadState ts(env);
1671 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001673 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 va_end(ap);
1675 return result.j;
1676 }
1677
Elliott Hughes418d20f2011-09-22 14:00:39 -07001678 static jlong CallStaticLongMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001680 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
Elliott Hughes418d20f2011-09-22 14:00:39 -07001683 static jlong CallStaticLongMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001685 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
Elliott Hughes72025e52011-08-23 17:50:30 -07001688 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 ScopedJniThreadState ts(env);
1690 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001692 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 va_end(ap);
1694 return result.f;
1695 }
1696
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001699 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 }
1701
Elliott Hughes418d20f2011-09-22 14:00:39 -07001702 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001704 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 ScopedJniThreadState ts(env);
1709 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001711 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 va_end(ap);
1713 return result.d;
1714 }
1715
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001718 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes418d20f2011-09-22 14:00:39 -07001721 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001723 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes72025e52011-08-23 17:50:30 -07001726 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 ScopedJniThreadState ts(env);
1728 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001729 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001730 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 va_end(ap);
1732 }
1733
Elliott Hughes418d20f2011-09-22 14:00:39 -07001734 static void CallStaticVoidMethodV(JNIEnv* env, jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001736 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes418d20f2011-09-22 14:00:39 -07001739 static void CallStaticVoidMethodA(JNIEnv* env, jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001741 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 }
1743
Elliott Hughes814e4032011-08-23 12:07:56 -07001744 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001746 if (chars == NULL && char_count == 0) {
1747 return NULL;
1748 }
1749 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001750 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 }
1752
1753 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1754 ScopedJniThreadState ts(env);
1755 if (utf == NULL) {
1756 return NULL;
1757 }
1758 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001759 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes814e4032011-08-23 12:07:56 -07001762 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1763 ScopedJniThreadState ts(env);
1764 return Decode<String*>(ts, java_string)->GetLength();
1765 }
1766
1767 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1768 ScopedJniThreadState ts(env);
1769 return Decode<String*>(ts, java_string)->GetUtfLength();
1770 }
1771
Elliott Hughesb465ab02011-08-24 11:21:21 -07001772 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001774 String* s = Decode<String*>(ts, java_string);
1775 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1776 ThrowSIOOBE(ts, start, length, s->GetLength());
1777 } else {
1778 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1779 memcpy(buf, chars + start, length * sizeof(jchar));
1780 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001781 }
1782
Elliott Hughesb465ab02011-08-24 11:21:21 -07001783 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001784 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001785 String* s = Decode<String*>(ts, java_string);
1786 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1787 ThrowSIOOBE(ts, start, length, s->GetLength());
1788 } else {
1789 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1790 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1791 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001792 }
1793
Elliott Hughes75770752011-08-24 17:52:38 -07001794 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001795 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001796 String* s = Decode<String*>(ts, java_string);
1797 const CharArray* chars = s->GetCharArray();
1798 PinPrimitiveArray(ts, chars);
1799 if (is_copy != NULL) {
1800 *is_copy = JNI_FALSE;
1801 }
1802 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001803 }
1804
Elliott Hughes75770752011-08-24 17:52:38 -07001805 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001806 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001807 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
Elliott Hughes75770752011-08-24 17:52:38 -07001810 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001812 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes75770752011-08-24 17:52:38 -07001815 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001817 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 }
1819
Elliott Hughes75770752011-08-24 17:52:38 -07001820 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001821 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001822 if (java_string == NULL) {
1823 return NULL;
1824 }
1825 if (is_copy != NULL) {
1826 *is_copy = JNI_TRUE;
1827 }
1828 String* s = Decode<String*>(ts, java_string);
1829 size_t byte_count = s->GetUtfLength();
1830 char* bytes = new char[byte_count + 1];
1831 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001832 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001833 return NULL;
1834 }
1835 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1836 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1837 bytes[byte_count] = '\0';
1838 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001839 }
1840
Elliott Hughes75770752011-08-24 17:52:38 -07001841 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001842 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001843 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001844 }
1845
Elliott Hughesbd935992011-08-22 11:59:34 -07001846 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001848 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001849 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001850 Array* array = obj->AsArray();
1851 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 }
1853
Elliott Hughes814e4032011-08-23 12:07:56 -07001854 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001856 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001857 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 }
1859
1860 static void SetObjectArrayElement(JNIEnv* env,
1861 jobjectArray java_array, jsize index, jobject java_value) {
1862 ScopedJniThreadState ts(env);
1863 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1864 Object* value = Decode<Object*>(ts, java_value);
1865 array->Set(index, value);
1866 }
1867
1868 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1869 ScopedJniThreadState ts(env);
1870 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1871 }
1872
1873 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1874 ScopedJniThreadState ts(env);
1875 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1876 }
1877
1878 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1879 ScopedJniThreadState ts(env);
1880 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1881 }
1882
1883 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1884 ScopedJniThreadState ts(env);
1885 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1886 }
1887
1888 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1889 ScopedJniThreadState ts(env);
1890 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1891 }
1892
1893 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1894 ScopedJniThreadState ts(env);
1895 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1896 }
1897
1898 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1899 ScopedJniThreadState ts(env);
1900 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1901 }
1902
1903 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1904 ScopedJniThreadState ts(env);
1905 CHECK_GE(length, 0); // TODO: ReportJniError
1906
1907 // Compute the array class corresponding to the given element class.
1908 Class* element_class = Decode<Class*>(ts, element_jclass);
1909 std::string descriptor;
1910 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001911 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001912
1913 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001914 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1915 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001916 return NULL;
1917 }
1918
Elliott Hughes75770752011-08-24 17:52:38 -07001919 // Allocate and initialize if necessary.
1920 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001922 if (initial_element != NULL) {
1923 Object* initial_object = Decode<Object*>(ts, initial_element);
1924 for (jsize i = 0; i < length; ++i) {
1925 result->Set(i, initial_object);
1926 }
1927 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001928 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001929 }
1930
1931 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1932 ScopedJniThreadState ts(env);
1933 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1934 }
1935
Elliott Hughes75770752011-08-24 17:52:38 -07001936 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001937 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001938 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001939 }
1940
Elliott Hughes75770752011-08-24 17:52:38 -07001941 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001942 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001943 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001944 }
1945
Elliott Hughes75770752011-08-24 17:52:38 -07001946 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001948 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
Elliott Hughes75770752011-08-24 17:52:38 -07001951 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001953 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Elliott Hughes75770752011-08-24 17:52:38 -07001956 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001958 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 }
1960
Elliott Hughes75770752011-08-24 17:52:38 -07001961 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001962 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001963 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Elliott Hughes75770752011-08-24 17:52:38 -07001966 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001968 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 }
1970
Elliott Hughes75770752011-08-24 17:52:38 -07001971 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001973 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Elliott Hughes75770752011-08-24 17:52:38 -07001976 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001978 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Elliott Hughes75770752011-08-24 17:52:38 -07001981 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001983 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Elliott Hughes75770752011-08-24 17:52:38 -07001986 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001988 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes75770752011-08-24 17:52:38 -07001991 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001993 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes75770752011-08-24 17:52:38 -07001996 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001998 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Elliott Hughes75770752011-08-24 17:52:38 -07002001 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002003 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Elliott Hughes75770752011-08-24 17:52:38 -07002006 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002008 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Elliott Hughes75770752011-08-24 17:52:38 -07002011 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002013 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes75770752011-08-24 17:52:38 -07002016 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002018 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes75770752011-08-24 17:52:38 -07002021 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002023 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes814e4032011-08-23 12:07:56 -07002026 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002028 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes814e4032011-08-23 12:07:56 -07002031 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002033 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes814e4032011-08-23 12:07:56 -07002036 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002038 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes814e4032011-08-23 12:07:56 -07002041 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002043 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Elliott Hughes814e4032011-08-23 12:07:56 -07002046 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Elliott Hughes814e4032011-08-23 12:07:56 -07002051 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Elliott Hughes5174fe62011-08-23 15:12:35 -07002106 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002108 Class* c = Decode<Class*>(ts, java_class);
2109
Elliott Hughes5174fe62011-08-23 15:12:35 -07002110 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 const char* name = methods[i].name;
2112 const char* sig = methods[i].signature;
2113
2114 if (*sig == '!') {
2115 // TODO: fast jni. it's too noisy to log all these.
2116 ++sig;
2117 }
2118
Elliott Hughes5174fe62011-08-23 15:12:35 -07002119 Method* m = c->FindDirectMethod(name, sig);
2120 if (m == NULL) {
2121 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002123 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002125 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2127 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002128 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002130 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002132 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2134 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002135 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 return JNI_ERR;
2137 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002138
Elliott Hughes75770752011-08-24 17:52:38 -07002139 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002140 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002141 }
2142
2143 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
2145 return JNI_OK;
2146 }
2147
Elliott Hughes5174fe62011-08-23 15:12:35 -07002148 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150 Class* c = Decode<Class*>(ts, java_class);
2151
Elliott Hughes75770752011-08-24 17:52:38 -07002152 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002153 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002154 }
2155
2156 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2157 Method* m = c->GetDirectMethod(i);
2158 if (m->IsNative()) {
2159 m->UnregisterNative();
2160 }
2161 }
2162 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2163 Method* m = c->GetVirtualMethod(i);
2164 if (m->IsNative()) {
2165 m->UnregisterNative();
2166 }
2167 }
2168
2169 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Elliott Hughes72025e52011-08-23 17:50:30 -07002172 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002174 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002175 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes72025e52011-08-23 17:50:30 -07002178 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002180 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002181 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 }
2183
2184 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2185 ScopedJniThreadState ts(env);
2186 Runtime* runtime = Runtime::Current();
2187 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002188 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 } else {
2190 *vm = NULL;
2191 }
2192 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2193 }
2194
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2196 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002197
2198 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002199 CHECK(address != NULL); // TODO: ReportJniError
2200 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002201
2202 jclass buffer_class = GetDirectByteBufferClass(env);
2203 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2204 if (mid == NULL) {
2205 return NULL;
2206 }
2207
2208 // At the moment, the Java side is limited to 32 bits.
2209 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2210 CHECK_LE(capacity, 0xffffffff);
2211 jint address_arg = reinterpret_cast<jint>(address);
2212 jint capacity_arg = static_cast<jint>(capacity);
2213
2214 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2215 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 }
2217
Elliott Hughesb465ab02011-08-24 11:21:21 -07002218 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002220 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2221 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002222 }
2223
Elliott Hughesb465ab02011-08-24 11:21:21 -07002224 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002225 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002226 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2227 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
Elliott Hughesb465ab02011-08-24 11:21:21 -07002230 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002232
Elliott Hughes75770752011-08-24 17:52:38 -07002233 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002234
2235 // Do we definitely know what kind of reference this is?
2236 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2237 IndirectRefKind kind = GetIndirectRefKind(ref);
2238 switch (kind) {
2239 case kLocal:
2240 return JNILocalRefType;
2241 case kGlobal:
2242 return JNIGlobalRefType;
2243 case kWeakGlobal:
2244 return JNIWeakGlobalRefType;
2245 case kSirtOrInvalid:
2246 // Is it in a stack IRT?
2247 if (ts.Self()->SirtContains(java_object)) {
2248 return JNILocalRefType;
2249 }
2250
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002251 if (!ts.Env()->work_around_app_jni_bugs) {
2252 return JNIInvalidRefType;
2253 }
2254
Elliott Hughesb465ab02011-08-24 11:21:21 -07002255 // If we're handing out direct pointers, check whether it's a direct pointer
2256 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002257 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002258 if (ts.Env()->locals.Contains(java_object)) {
2259 return JNILocalRefType;
2260 }
2261 }
2262
2263 return JNIInvalidRefType;
2264 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002265 }
2266};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002267
Elliott Hughesa2501992011-08-26 19:39:54 -07002268const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002269 NULL, // reserved0.
2270 NULL, // reserved1.
2271 NULL, // reserved2.
2272 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002273 JNI::GetVersion,
2274 JNI::DefineClass,
2275 JNI::FindClass,
2276 JNI::FromReflectedMethod,
2277 JNI::FromReflectedField,
2278 JNI::ToReflectedMethod,
2279 JNI::GetSuperclass,
2280 JNI::IsAssignableFrom,
2281 JNI::ToReflectedField,
2282 JNI::Throw,
2283 JNI::ThrowNew,
2284 JNI::ExceptionOccurred,
2285 JNI::ExceptionDescribe,
2286 JNI::ExceptionClear,
2287 JNI::FatalError,
2288 JNI::PushLocalFrame,
2289 JNI::PopLocalFrame,
2290 JNI::NewGlobalRef,
2291 JNI::DeleteGlobalRef,
2292 JNI::DeleteLocalRef,
2293 JNI::IsSameObject,
2294 JNI::NewLocalRef,
2295 JNI::EnsureLocalCapacity,
2296 JNI::AllocObject,
2297 JNI::NewObject,
2298 JNI::NewObjectV,
2299 JNI::NewObjectA,
2300 JNI::GetObjectClass,
2301 JNI::IsInstanceOf,
2302 JNI::GetMethodID,
2303 JNI::CallObjectMethod,
2304 JNI::CallObjectMethodV,
2305 JNI::CallObjectMethodA,
2306 JNI::CallBooleanMethod,
2307 JNI::CallBooleanMethodV,
2308 JNI::CallBooleanMethodA,
2309 JNI::CallByteMethod,
2310 JNI::CallByteMethodV,
2311 JNI::CallByteMethodA,
2312 JNI::CallCharMethod,
2313 JNI::CallCharMethodV,
2314 JNI::CallCharMethodA,
2315 JNI::CallShortMethod,
2316 JNI::CallShortMethodV,
2317 JNI::CallShortMethodA,
2318 JNI::CallIntMethod,
2319 JNI::CallIntMethodV,
2320 JNI::CallIntMethodA,
2321 JNI::CallLongMethod,
2322 JNI::CallLongMethodV,
2323 JNI::CallLongMethodA,
2324 JNI::CallFloatMethod,
2325 JNI::CallFloatMethodV,
2326 JNI::CallFloatMethodA,
2327 JNI::CallDoubleMethod,
2328 JNI::CallDoubleMethodV,
2329 JNI::CallDoubleMethodA,
2330 JNI::CallVoidMethod,
2331 JNI::CallVoidMethodV,
2332 JNI::CallVoidMethodA,
2333 JNI::CallNonvirtualObjectMethod,
2334 JNI::CallNonvirtualObjectMethodV,
2335 JNI::CallNonvirtualObjectMethodA,
2336 JNI::CallNonvirtualBooleanMethod,
2337 JNI::CallNonvirtualBooleanMethodV,
2338 JNI::CallNonvirtualBooleanMethodA,
2339 JNI::CallNonvirtualByteMethod,
2340 JNI::CallNonvirtualByteMethodV,
2341 JNI::CallNonvirtualByteMethodA,
2342 JNI::CallNonvirtualCharMethod,
2343 JNI::CallNonvirtualCharMethodV,
2344 JNI::CallNonvirtualCharMethodA,
2345 JNI::CallNonvirtualShortMethod,
2346 JNI::CallNonvirtualShortMethodV,
2347 JNI::CallNonvirtualShortMethodA,
2348 JNI::CallNonvirtualIntMethod,
2349 JNI::CallNonvirtualIntMethodV,
2350 JNI::CallNonvirtualIntMethodA,
2351 JNI::CallNonvirtualLongMethod,
2352 JNI::CallNonvirtualLongMethodV,
2353 JNI::CallNonvirtualLongMethodA,
2354 JNI::CallNonvirtualFloatMethod,
2355 JNI::CallNonvirtualFloatMethodV,
2356 JNI::CallNonvirtualFloatMethodA,
2357 JNI::CallNonvirtualDoubleMethod,
2358 JNI::CallNonvirtualDoubleMethodV,
2359 JNI::CallNonvirtualDoubleMethodA,
2360 JNI::CallNonvirtualVoidMethod,
2361 JNI::CallNonvirtualVoidMethodV,
2362 JNI::CallNonvirtualVoidMethodA,
2363 JNI::GetFieldID,
2364 JNI::GetObjectField,
2365 JNI::GetBooleanField,
2366 JNI::GetByteField,
2367 JNI::GetCharField,
2368 JNI::GetShortField,
2369 JNI::GetIntField,
2370 JNI::GetLongField,
2371 JNI::GetFloatField,
2372 JNI::GetDoubleField,
2373 JNI::SetObjectField,
2374 JNI::SetBooleanField,
2375 JNI::SetByteField,
2376 JNI::SetCharField,
2377 JNI::SetShortField,
2378 JNI::SetIntField,
2379 JNI::SetLongField,
2380 JNI::SetFloatField,
2381 JNI::SetDoubleField,
2382 JNI::GetStaticMethodID,
2383 JNI::CallStaticObjectMethod,
2384 JNI::CallStaticObjectMethodV,
2385 JNI::CallStaticObjectMethodA,
2386 JNI::CallStaticBooleanMethod,
2387 JNI::CallStaticBooleanMethodV,
2388 JNI::CallStaticBooleanMethodA,
2389 JNI::CallStaticByteMethod,
2390 JNI::CallStaticByteMethodV,
2391 JNI::CallStaticByteMethodA,
2392 JNI::CallStaticCharMethod,
2393 JNI::CallStaticCharMethodV,
2394 JNI::CallStaticCharMethodA,
2395 JNI::CallStaticShortMethod,
2396 JNI::CallStaticShortMethodV,
2397 JNI::CallStaticShortMethodA,
2398 JNI::CallStaticIntMethod,
2399 JNI::CallStaticIntMethodV,
2400 JNI::CallStaticIntMethodA,
2401 JNI::CallStaticLongMethod,
2402 JNI::CallStaticLongMethodV,
2403 JNI::CallStaticLongMethodA,
2404 JNI::CallStaticFloatMethod,
2405 JNI::CallStaticFloatMethodV,
2406 JNI::CallStaticFloatMethodA,
2407 JNI::CallStaticDoubleMethod,
2408 JNI::CallStaticDoubleMethodV,
2409 JNI::CallStaticDoubleMethodA,
2410 JNI::CallStaticVoidMethod,
2411 JNI::CallStaticVoidMethodV,
2412 JNI::CallStaticVoidMethodA,
2413 JNI::GetStaticFieldID,
2414 JNI::GetStaticObjectField,
2415 JNI::GetStaticBooleanField,
2416 JNI::GetStaticByteField,
2417 JNI::GetStaticCharField,
2418 JNI::GetStaticShortField,
2419 JNI::GetStaticIntField,
2420 JNI::GetStaticLongField,
2421 JNI::GetStaticFloatField,
2422 JNI::GetStaticDoubleField,
2423 JNI::SetStaticObjectField,
2424 JNI::SetStaticBooleanField,
2425 JNI::SetStaticByteField,
2426 JNI::SetStaticCharField,
2427 JNI::SetStaticShortField,
2428 JNI::SetStaticIntField,
2429 JNI::SetStaticLongField,
2430 JNI::SetStaticFloatField,
2431 JNI::SetStaticDoubleField,
2432 JNI::NewString,
2433 JNI::GetStringLength,
2434 JNI::GetStringChars,
2435 JNI::ReleaseStringChars,
2436 JNI::NewStringUTF,
2437 JNI::GetStringUTFLength,
2438 JNI::GetStringUTFChars,
2439 JNI::ReleaseStringUTFChars,
2440 JNI::GetArrayLength,
2441 JNI::NewObjectArray,
2442 JNI::GetObjectArrayElement,
2443 JNI::SetObjectArrayElement,
2444 JNI::NewBooleanArray,
2445 JNI::NewByteArray,
2446 JNI::NewCharArray,
2447 JNI::NewShortArray,
2448 JNI::NewIntArray,
2449 JNI::NewLongArray,
2450 JNI::NewFloatArray,
2451 JNI::NewDoubleArray,
2452 JNI::GetBooleanArrayElements,
2453 JNI::GetByteArrayElements,
2454 JNI::GetCharArrayElements,
2455 JNI::GetShortArrayElements,
2456 JNI::GetIntArrayElements,
2457 JNI::GetLongArrayElements,
2458 JNI::GetFloatArrayElements,
2459 JNI::GetDoubleArrayElements,
2460 JNI::ReleaseBooleanArrayElements,
2461 JNI::ReleaseByteArrayElements,
2462 JNI::ReleaseCharArrayElements,
2463 JNI::ReleaseShortArrayElements,
2464 JNI::ReleaseIntArrayElements,
2465 JNI::ReleaseLongArrayElements,
2466 JNI::ReleaseFloatArrayElements,
2467 JNI::ReleaseDoubleArrayElements,
2468 JNI::GetBooleanArrayRegion,
2469 JNI::GetByteArrayRegion,
2470 JNI::GetCharArrayRegion,
2471 JNI::GetShortArrayRegion,
2472 JNI::GetIntArrayRegion,
2473 JNI::GetLongArrayRegion,
2474 JNI::GetFloatArrayRegion,
2475 JNI::GetDoubleArrayRegion,
2476 JNI::SetBooleanArrayRegion,
2477 JNI::SetByteArrayRegion,
2478 JNI::SetCharArrayRegion,
2479 JNI::SetShortArrayRegion,
2480 JNI::SetIntArrayRegion,
2481 JNI::SetLongArrayRegion,
2482 JNI::SetFloatArrayRegion,
2483 JNI::SetDoubleArrayRegion,
2484 JNI::RegisterNatives,
2485 JNI::UnregisterNatives,
2486 JNI::MonitorEnter,
2487 JNI::MonitorExit,
2488 JNI::GetJavaVM,
2489 JNI::GetStringRegion,
2490 JNI::GetStringUTFRegion,
2491 JNI::GetPrimitiveArrayCritical,
2492 JNI::ReleasePrimitiveArrayCritical,
2493 JNI::GetStringCritical,
2494 JNI::ReleaseStringCritical,
2495 JNI::NewWeakGlobalRef,
2496 JNI::DeleteWeakGlobalRef,
2497 JNI::ExceptionCheck,
2498 JNI::NewDirectByteBuffer,
2499 JNI::GetDirectBufferAddress,
2500 JNI::GetDirectBufferCapacity,
2501 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002502};
2503
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002504static const size_t kMonitorsInitial = 32; // Arbitrary.
2505static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2506
2507static const size_t kLocalsInitial = 64; // Arbitrary.
2508static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002509
Elliott Hughes75770752011-08-24 17:52:38 -07002510JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002511 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002512 vm(vm),
2513 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002514 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002515 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002516 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2517 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002518 functions = unchecked_functions = &gNativeInterface;
2519 if (check_jni) {
2520 functions = GetCheckJniNativeInterface();
2521 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002522}
2523
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002524JNIEnvExt::~JNIEnvExt() {
2525}
2526
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002527void JNIEnvExt::DumpReferenceTables() {
2528 locals.Dump();
2529 monitors.Dump();
2530}
2531
Carl Shapiroea4dca82011-08-01 13:45:38 -07002532// JNI Invocation interface.
2533
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002534extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2535 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2536 if (args->version < JNI_VERSION_1_2) {
2537 return JNI_EVERSION;
2538 }
2539 Runtime::Options options;
2540 for (int i = 0; i < args->nOptions; ++i) {
2541 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002542 options.push_back(std::make_pair(StringPiece(option->optionString),
2543 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002544 }
2545 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002546 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002547 if (runtime == NULL) {
2548 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002549 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002550 runtime->Start();
2551 *p_env = Thread::Current()->GetJniEnv();
2552 *p_vm = runtime->GetJavaVM();
2553 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002554}
2555
Elliott Hughesf2682d52011-08-15 16:37:04 -07002556extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002557 Runtime* runtime = Runtime::Current();
2558 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002559 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002560 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002561 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002562 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002563 }
2564 return JNI_OK;
2565}
2566
2567// Historically unsupported.
2568extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2569 return JNI_ERR;
2570}
2571
Elliott Hughescdf53122011-08-19 15:46:09 -07002572class JII {
2573 public:
2574 static jint DestroyJavaVM(JavaVM* vm) {
2575 if (vm == NULL) {
2576 return JNI_ERR;
2577 } else {
2578 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2579 delete raw_vm->runtime;
2580 return JNI_OK;
2581 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002582 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002583
Elliott Hughescdf53122011-08-19 15:46:09 -07002584 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002585 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002586 }
2587
2588 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002589 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002590 }
2591
2592 static jint DetachCurrentThread(JavaVM* vm) {
2593 if (vm == NULL) {
2594 return JNI_ERR;
2595 } else {
2596 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2597 Runtime* runtime = raw_vm->runtime;
2598 runtime->DetachCurrentThread();
2599 return JNI_OK;
2600 }
2601 }
2602
2603 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2604 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2605 return JNI_EVERSION;
2606 }
2607 if (vm == NULL || env == NULL) {
2608 return JNI_ERR;
2609 }
2610 Thread* thread = Thread::Current();
2611 if (thread == NULL) {
2612 *env = NULL;
2613 return JNI_EDETACHED;
2614 }
2615 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002616 return JNI_OK;
2617 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002618};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002619
Elliott Hughesa2501992011-08-26 19:39:54 -07002620const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002621 NULL, // reserved0
2622 NULL, // reserved1
2623 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002624 JII::DestroyJavaVM,
2625 JII::AttachCurrentThread,
2626 JII::DetachCurrentThread,
2627 JII::GetEnv,
2628 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002629};
2630
Elliott Hughesbbd76712011-08-17 10:25:24 -07002631static const size_t kPinTableInitialSize = 16;
2632static const size_t kPinTableMaxSize = 1024;
2633
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002634static const size_t kGlobalsInitial = 512; // Arbitrary.
2635static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2636
2637static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2638static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2639
Elliott Hughesa0957642011-09-02 14:27:33 -07002640JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002641 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002642 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002643 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002644 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002645 verbose_jni(options->IsVerbose("jni")),
2646 log_third_party_jni(options->IsVerbose("third-party-jni")),
2647 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002648 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002649 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002650 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002651 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002652 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002653 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002654 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002655 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002656 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002657 functions = unchecked_functions = &gInvokeInterface;
2658 if (check_jni) {
2659 functions = GetCheckJniInvokeInterface();
2660 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002661}
2662
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002663JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002664 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002665}
2666
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002667void JavaVMExt::DumpReferenceTables() {
2668 {
2669 MutexLock mu(globals_lock);
2670 globals.Dump();
2671 }
2672 {
2673 MutexLock mu(weak_globals_lock);
2674 weak_globals.Dump();
2675 }
2676 {
2677 MutexLock mu(pins_lock);
2678 pin_table.Dump();
2679 }
2680}
2681
Elliott Hughes75770752011-08-24 17:52:38 -07002682bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2683 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002684
2685 // See if we've already loaded this library. If we have, and the class loader
2686 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002687 // TODO: for better results we should canonicalize the pathname (or even compare
2688 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002689 SharedLibrary* library;
2690 {
2691 // TODO: move the locking (and more of this logic) into Libraries.
2692 MutexLock mu(libraries_lock);
2693 library = libraries->Get(path);
2694 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002695 if (library != NULL) {
2696 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002697 // The library will be associated with class_loader. The JNI
2698 // spec says we can't load the same library into more than one
2699 // class loader.
2700 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2701 "ClassLoader %p; can't open in ClassLoader %p",
2702 path.c_str(), library->GetClassLoader(), class_loader);
2703 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002704 return false;
2705 }
2706 if (verbose_jni) {
2707 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2708 << "ClassLoader " << class_loader << "]";
2709 }
2710 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002711 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2712 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002713 return false;
2714 }
2715 return true;
2716 }
2717
2718 // Open the shared library. Because we're using a full path, the system
2719 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2720 // resolve this library's dependencies though.)
2721
2722 // Failures here are expected when java.library.path has several entries
2723 // and we have to hunt for the lib.
2724
2725 // The current version of the dynamic linker prints detailed information
2726 // about dlopen() failures. Some things to check if the message is
2727 // cryptic:
2728 // - make sure the library exists on the device
2729 // - verify that the right path is being opened (the debug log message
2730 // above can help with that)
2731 // - check to see if the library is valid (e.g. not zero bytes long)
2732 // - check config/prelink-linux-arm.map to ensure that the library
2733 // is listed and is not being overrun by the previous entry (if
2734 // loading suddenly stops working on a prelinked library, this is
2735 // a good one to check)
2736 // - write a trivial app that calls sleep() then dlopen(), attach
2737 // to it with "strace -p <pid>" while it sleeps, and watch for
2738 // attempts to open nonexistent dependent shared libs
2739
2740 // TODO: automate some of these checks!
2741
2742 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002743 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002744 // the GC to ignore us.
2745 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002746 void* handle = NULL;
2747 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002748 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002749 handle = dlopen(path.c_str(), RTLD_LAZY);
2750 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002751
2752 if (verbose_jni) {
2753 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2754 }
2755
2756 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002757 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002758 return false;
2759 }
2760
2761 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002762 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002763 // TODO: move the locking (and more of this logic) into Libraries.
2764 MutexLock mu(libraries_lock);
2765 library = libraries->Get(path);
2766 if (library != NULL) {
2767 LOG(INFO) << "WOW: we lost a race to add shared library: "
2768 << "\"" << path << "\" ClassLoader=" << class_loader;
2769 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002770 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002771 library = new SharedLibrary(path, handle, class_loader);
2772 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002773 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002774
2775 if (verbose_jni) {
2776 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2777 }
2778
2779 bool result = true;
2780 void* sym = dlsym(handle, "JNI_OnLoad");
2781 if (sym == NULL) {
2782 if (verbose_jni) {
2783 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2784 }
2785 } else {
2786 // Call JNI_OnLoad. We have to override the current class
2787 // loader, which will always be "null" since the stuff at the
2788 // top of the stack is around Runtime.loadLibrary(). (See
2789 // the comments in the JNI FindClass function.)
2790 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2791 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002792 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002793 self->SetClassLoaderOverride(class_loader);
2794
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002795 int version = 0;
2796 {
2797 ScopedThreadStateChange tsc(self, Thread::kNative);
2798 if (verbose_jni) {
2799 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2800 }
2801 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002802 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002803
2804 self->SetClassLoaderOverride(old_class_loader);;
2805
2806 if (version != JNI_VERSION_1_2 &&
2807 version != JNI_VERSION_1_4 &&
2808 version != JNI_VERSION_1_6) {
2809 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2810 << "bad version: " << version;
2811 // It's unwise to call dlclose() here, but we can mark it
2812 // as bad and ensure that future load attempts will fail.
2813 // We don't know how far JNI_OnLoad got, so there could
2814 // be some partially-initialized stuff accessible through
2815 // newly-registered native method calls. We could try to
2816 // unregister them, but that doesn't seem worthwhile.
2817 result = false;
2818 } else {
2819 if (verbose_jni) {
2820 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2821 << " from JNI_OnLoad in \"" << path << "\"]";
2822 }
2823 }
2824 }
2825
2826 library->SetResult(result);
2827 return result;
2828}
2829
2830void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2831 CHECK(m->IsNative());
2832
2833 Class* c = m->GetDeclaringClass();
2834
2835 // If this is a static method, it could be called before the class
2836 // has been initialized.
2837 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002838 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002839 return NULL;
2840 }
2841 } else {
2842 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2843 }
2844
Brian Carlstrom16192862011-09-12 17:50:06 -07002845 std::string detail;
2846 void* native_method;
2847 {
2848 MutexLock mu(libraries_lock);
2849 native_method = libraries->FindNativeMethod(m, detail);
2850 }
2851 // throwing can cause libraries_lock to be reacquired
2852 if (native_method == NULL) {
2853 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
2854 "%s", detail.c_str());
2855 }
2856 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002857}
2858
Elliott Hughes410c0c82011-09-01 17:58:25 -07002859void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2860 {
2861 MutexLock mu(globals_lock);
2862 globals.VisitRoots(visitor, arg);
2863 }
2864 {
2865 MutexLock mu(pins_lock);
2866 pin_table.VisitRoots(visitor, arg);
2867 }
2868 // The weak_globals table is visited by the GC itself (because it mutates the table).
2869}
2870
Ian Rogersdf20fe02011-07-20 20:34:16 -07002871} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002872
2873std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2874 switch (rhs) {
2875 case JNIInvalidRefType:
2876 os << "JNIInvalidRefType";
2877 return os;
2878 case JNILocalRefType:
2879 os << "JNILocalRefType";
2880 return os;
2881 case JNIGlobalRefType:
2882 os << "JNIGlobalRefType";
2883 return os;
2884 case JNIWeakGlobalRefType:
2885 os << "JNIWeakGlobalRefType";
2886 return os;
2887 }
2888}