blob: 94ef729a7918c5a425f3ffd1133b496c77e9efa6 [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 Hughes5ee7a8b2011-09-13 16:40:07 -0700114template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700115
116namespace {
117
Elliott Hughescdf53122011-08-19 15:46:09 -0700118jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
119 if (obj == NULL) {
120 return NULL;
121 }
Elliott Hughes75770752011-08-24 17:52:38 -0700122 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700123 IndirectReferenceTable& weak_globals = vm->weak_globals;
124 MutexLock mu(vm->weak_globals_lock);
125 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
126 return reinterpret_cast<jweak>(ref);
127}
128
Elliott Hughesbf86d042011-08-31 17:53:14 -0700129// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700130template<typename T>
131T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700132 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700133}
134
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700135byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700136 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700137 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700138 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700139 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
140 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700141 case 'Z':
142 case 'B':
143 case 'C':
144 case 'S':
145 case 'I':
146 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
147 offset += 4;
148 break;
149 case 'F':
150 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
151 offset += 4;
152 break;
153 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700154 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700155 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
156 offset += sizeof(Object*);
157 break;
158 }
159 case 'D':
160 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
161 offset += 8;
162 break;
163 case 'J':
164 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
165 offset += 8;
166 break;
167 }
168 }
169 return arg_array.release();
170}
171
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700172byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700173 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700174 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700175 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700176 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
177 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700178 case 'Z':
179 case 'B':
180 case 'C':
181 case 'S':
182 case 'I':
183 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
184 offset += 4;
185 break;
186 case 'F':
187 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
188 offset += 4;
189 break;
190 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700191 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700192 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
193 offset += sizeof(Object*);
194 break;
195 }
196 case 'D':
197 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
198 offset += 8;
199 break;
200 case 'J':
201 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
202 offset += 8;
203 break;
204 }
205 }
206 return arg_array.release();
207}
208
Elliott Hughes72025e52011-08-23 17:50:30 -0700209JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
210 Method* method, byte* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700211 JValue result;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700212 method->Invoke(ts.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700213 return result;
214}
215
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700216JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700217 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700218 Method* method = DecodeMethod(mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700219 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700220 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700221}
222
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700223JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700224 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700225 Method* method = DecodeMethod(mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700226 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700227 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
228}
229
Elliott Hughes75770752011-08-24 17:52:38 -0700230Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700231 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700232}
233
Brian Carlstrom30b94452011-08-25 21:35:26 -0700234JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700235 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700236 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700237 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700238 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
239}
240
Brian Carlstrom30b94452011-08-25 21:35:26 -0700241JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700242 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700243 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700244 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700245 return InvokeWithArgArray(ts, 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);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700274 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
275 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 Hughes5ee7a8b2011-09-13 16:40:07 -0700301 return reinterpret_cast<jmethodID>(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700302}
303
Elliott Hughescdf53122011-08-19 15:46:09 -0700304jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
305 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700306 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
307 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700308 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700309
310 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700311 Class* field_type;
312 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
313 if (sig[1] != '\0') {
314 // TODO: need to get the appropriate ClassLoader.
315 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
316 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700317 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700318 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700319 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 if (field_type == NULL) {
321 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700322 DCHECK(ts.Self()->IsExceptionPending());
323 ts.Self()->ClearException();
324 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
325 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
326 "no type \"%s\" found and so no field \"%s\" could be found in class "
327 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700328 return NULL;
329 }
330 if (is_static) {
331 field = c->FindStaticField(name, field_type);
332 } else {
333 field = c->FindInstanceField(name, field_type);
334 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700335 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700336 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700337 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700338 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700339 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700340 return NULL;
341 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700342 // Check invariant that all jfieldIDs have resolved types (how else would
343 // the type equality in Find...Field hold?)
344 DCHECK(field->GetType() != NULL);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700345 return reinterpret_cast<jfieldID>(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700346}
347
Elliott Hughes75770752011-08-24 17:52:38 -0700348void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
349 JavaVMExt* vm = ts.Vm();
350 MutexLock mu(vm->pins_lock);
351 vm->pin_table.Add(array);
352}
353
354void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
355 JavaVMExt* vm = ts.Vm();
356 MutexLock mu(vm->pins_lock);
357 vm->pin_table.Remove(array);
358}
359
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700360template<typename JniT, typename ArtT>
361JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
362 CHECK_GE(length, 0); // TODO: ReportJniError
363 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700364 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700365}
366
Elliott Hughes75770752011-08-24 17:52:38 -0700367template <typename ArrayT, typename CArrayT, typename ArtArrayT>
368CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
369 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
370 PinPrimitiveArray(ts, array);
371 if (is_copy != NULL) {
372 *is_copy = JNI_FALSE;
373 }
374 return array->GetData();
375}
376
377template <typename ArrayT>
378void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
379 if (mode != JNI_COMMIT) {
380 Array* array = Decode<Array*>(ts, java_array);
381 UnpinPrimitiveArray(ts, array);
382 }
383}
384
Elliott Hughes814e4032011-08-23 12:07:56 -0700385void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700386 std::string type(PrettyTypeOf(array));
Elliott Hughes814e4032011-08-23 12:07:56 -0700387 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
388 "%s offset=%d length=%d %s.length=%d",
389 type.c_str(), start, length, identifier, array->GetLength());
390}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700391void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
392 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
393 "offset=%d length=%d string.length()=%d", start, length, array_length);
394}
Elliott Hughes814e4032011-08-23 12:07:56 -0700395
396template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700397void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700398 ArrayT* array = Decode<ArrayT*>(ts, java_array);
399 if (start < 0 || length < 0 || start + length > array->GetLength()) {
400 ThrowAIOOBE(ts, array, start, length, "src");
401 } else {
402 JavaT* data = array->GetData();
403 memcpy(buf, data + start, length * sizeof(JavaT));
404 }
405}
406
407template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700408void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700409 ArrayT* array = Decode<ArrayT*>(ts, java_array);
410 if (start < 0 || length < 0 || start + length > array->GetLength()) {
411 ThrowAIOOBE(ts, array, start, length, "dst");
412 } else {
413 JavaT* data = array->GetData();
414 memcpy(data + start, buf, length * sizeof(JavaT));
415 }
416}
417
Elliott Hughes75770752011-08-24 17:52:38 -0700418jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700419 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
420 CHECK(buffer_class.get() != NULL);
421 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
422}
423
Elliott Hughes75770752011-08-24 17:52:38 -0700424jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700425 static jclass buffer_class = InitDirectByteBufferClass(env);
426 return buffer_class;
427}
428
Elliott Hughes75770752011-08-24 17:52:38 -0700429jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
430 if (vm == NULL || p_env == NULL) {
431 return JNI_ERR;
432 }
433
434 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
435 JavaVMAttachArgs args;
436 if (thr_args == NULL) {
437 // Allow the v1.1 calling convention.
438 args.version = JNI_VERSION_1_2;
439 args.name = NULL;
440 args.group = NULL; // TODO: get "main" thread group
441 } else {
442 args.version = in_args->version;
443 args.name = in_args->name;
444 if (in_args->group != NULL) {
445 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
446 args.group = NULL; // TODO: decode in_args->group
447 } else {
448 args.group = NULL; // TODO: get "main" thread group
449 }
450 }
451 CHECK_GE(args.version, JNI_VERSION_1_2);
452
453 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700454 runtime->AttachCurrentThread(args.name, as_daemon);
455 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700456 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700457}
458
Elliott Hughes79082e32011-08-25 12:07:32 -0700459class SharedLibrary {
460 public:
461 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
462 : path_(path),
463 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700464 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700465 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700466 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700467 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700468 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700469 }
470
Elliott Hughes79082e32011-08-25 12:07:32 -0700471 Object* GetClassLoader() {
472 return class_loader_;
473 }
474
475 std::string GetPath() {
476 return path_;
477 }
478
479 /*
480 * Check the result of an earlier call to JNI_OnLoad on this library. If
481 * the call has not yet finished in another thread, wait for it.
482 */
483 bool CheckOnLoadResult(JavaVMExt* vm) {
484 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700485 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700486 // Check this so we don't end up waiting for ourselves. We need
487 // to return "true" so the caller can continue.
488 LOG(INFO) << *self << " recursive attempt to load library "
489 << "\"" << path_ << "\"";
490 return true;
491 }
492
493 MutexLock mu(jni_on_load_lock_);
494 while (jni_on_load_result_ == kPending) {
495 if (vm->verbose_jni) {
496 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
497 << "JNI_OnLoad...]";
498 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700499 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700500 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700501 }
502
503 bool okay = (jni_on_load_result_ == kOkay);
504 if (vm->verbose_jni) {
505 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
506 << (okay ? "succeeded" : "failed") << "]";
507 }
508 return okay;
509 }
510
511 void SetResult(bool result) {
512 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700513 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700514
515 // Broadcast a wakeup to anybody sleeping on the condition variable.
516 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700517 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700518 }
519
520 void* FindSymbol(const std::string& symbol_name) {
521 return dlsym(handle_, symbol_name.c_str());
522 }
523
524 private:
525 enum JNI_OnLoadState {
526 kPending,
527 kFailed,
528 kOkay,
529 };
530
531 // Path to library "/system/lib/libjni.so".
532 std::string path_;
533
534 // The void* returned by dlopen(3).
535 void* handle_;
536
537 // The ClassLoader this library is associated with.
538 Object* class_loader_;
539
540 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700541 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700542 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700543 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700544 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700545 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 // Result of earlier JNI_OnLoad call.
547 JNI_OnLoadState jni_on_load_result_;
548};
549
Elliott Hughescdf53122011-08-19 15:46:09 -0700550} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700551
Elliott Hughes79082e32011-08-25 12:07:32 -0700552// This exists mainly to keep implementation details out of the header file.
553class Libraries {
554 public:
555 Libraries() {
556 }
557
558 ~Libraries() {
559 // Delete our map values. (The keys will be cleaned up by the map itself.)
560 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
561 delete it->second;
562 }
563 }
564
565 SharedLibrary* Get(const std::string& path) {
566 return libraries_[path];
567 }
568
569 void Put(const std::string& path, SharedLibrary* library) {
570 libraries_[path] = library;
571 }
572
573 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700574 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700575 std::string jni_short_name(JniShortName(m));
576 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700577 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700578 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
579 SharedLibrary* library = it->second;
580 if (library->GetClassLoader() != declaring_class_loader) {
581 // We only search libraries loaded by the appropriate ClassLoader.
582 continue;
583 }
584 // Try the short name then the long name...
585 void* fn = library->FindSymbol(jni_short_name);
586 if (fn == NULL) {
587 fn = library->FindSymbol(jni_long_name);
588 }
589 if (fn != NULL) {
590 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700591 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700592 << " in \"" << library->GetPath() << "\"]";
593 }
594 return fn;
595 }
596 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700597 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700598 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700599 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 return NULL;
601 }
602
603 private:
604 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
605
606 std::map<std::string, SharedLibrary*> libraries_;
607};
608
Elliott Hughescdf53122011-08-19 15:46:09 -0700609class JNI {
610 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700611
Elliott Hughescdf53122011-08-19 15:46:09 -0700612 static jint GetVersion(JNIEnv* env) {
613 ScopedJniThreadState ts(env);
614 return JNI_VERSION_1_6;
615 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700616
Elliott Hughescdf53122011-08-19 15:46:09 -0700617 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
618 ScopedJniThreadState ts(env);
619 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700620 return NULL;
621 }
622
Elliott Hughescdf53122011-08-19 15:46:09 -0700623 static jclass FindClass(JNIEnv* env, const char* name) {
624 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700625 Runtime* runtime = Runtime::Current();
626 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700627 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700628 Class* c = NULL;
629 if (runtime->IsStarted()) {
630 // TODO: need to get the appropriate ClassLoader.
631 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
632 c = class_linker->FindClass(descriptor, cl);
633 } else {
634 c = class_linker->FindSystemClass(descriptor);
635 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700636 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700637 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700638
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
640 ScopedJniThreadState ts(env);
641 Method* method = Decode<Method*>(ts, java_method);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700642 return reinterpret_cast<jmethodID>(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700643 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700644
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
646 ScopedJniThreadState ts(env);
647 Field* field = Decode<Field*>(ts, java_field);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700648 return reinterpret_cast<jfieldID>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700650
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
652 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700653 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700654 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700656
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
658 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700659 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700660 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700662
Elliott Hughes37f7a402011-08-22 18:56:01 -0700663 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
664 ScopedJniThreadState ts(env);
665 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700666 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700667 }
668
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700669 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700671 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700672 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700674
Elliott Hughes37f7a402011-08-22 18:56:01 -0700675 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700677 Class* c1 = Decode<Class*>(ts, java_class1);
678 Class* c2 = Decode<Class*>(ts, java_class2);
679 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700681
Elliott Hughes37f7a402011-08-22 18:56:01 -0700682 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700684 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 if (jobj == NULL) {
686 // NB. JNI is different from regular Java instanceof in this respect
687 return JNI_TRUE;
688 } else {
689 Object* obj = Decode<Object*>(ts, jobj);
690 Class* klass = Decode<Class*>(ts, clazz);
691 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
692 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700694
Elliott Hughes37f7a402011-08-22 18:56:01 -0700695 static jint Throw(JNIEnv* env, jthrowable java_exception) {
696 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700697 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 if (exception == NULL) {
699 return JNI_ERR;
700 }
701 ts.Self()->SetException(exception);
702 return JNI_OK;
703 }
704
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700705 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700706 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700707 // TODO: check for a pending exception to decide what constructor to call.
708 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
709 if (mid == NULL) {
710 return JNI_ERR;
711 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700712 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
713 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700714 return JNI_ERR;
715 }
716
717 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700718 args[0].l = s.get();
719 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
720 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700721 return JNI_ERR;
722 }
723
Elliott Hughes54e7df12011-09-16 11:47:04 -0700724 LOG(INFO) << "Throwing " << PrettyTypeOf(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700725 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700726 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700727
Elliott Hughes37f7a402011-08-22 18:56:01 -0700728 return JNI_OK;
729 }
730
731 static jboolean ExceptionCheck(JNIEnv* env) {
732 ScopedJniThreadState ts(env);
733 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
734 }
735
736 static void ExceptionClear(JNIEnv* env) {
737 ScopedJniThreadState ts(env);
738 ts.Self()->ClearException();
739 }
740
741 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700743
744 Thread* self = ts.Self();
745 Throwable* original_exception = self->GetException();
746 self->ClearException();
747
Elliott Hughesbf86d042011-08-31 17:53:14 -0700748 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700749 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
750 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
751 if (mid == NULL) {
752 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700753 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700754 } else {
755 env->CallVoidMethod(exception.get(), mid);
756 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700757 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700758 << " thrown while calling printStackTrace";
759 self->ClearException();
760 }
761 }
762
763 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700764 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700765
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 static jthrowable ExceptionOccurred(JNIEnv* env) {
767 ScopedJniThreadState ts(env);
768 Object* exception = ts.Self()->GetException();
769 if (exception == NULL) {
770 return NULL;
771 } else {
772 // TODO: if adding a local reference failing causes the VM to abort
773 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700774 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 if (localException == NULL) {
776 // We were unable to add a new local reference, and threw a new
777 // exception. We can't return "exception", because it's not a
778 // local reference. So we have to return NULL, indicating that
779 // there was no exception, even though it's pretty much raining
780 // exceptions in here.
781 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
782 }
783 return localException;
784 }
785 }
786
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 static void FatalError(JNIEnv* env, const char* msg) {
788 ScopedJniThreadState ts(env);
789 LOG(FATAL) << "JNI FatalError called: " << msg;
790 }
791
792 static jint PushLocalFrame(JNIEnv* env, jint cap) {
793 ScopedJniThreadState ts(env);
794 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
795 return JNI_OK;
796 }
797
798 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
799 ScopedJniThreadState ts(env);
800 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
801 return res;
802 }
803
Elliott Hughes72025e52011-08-23 17:50:30 -0700804 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
805 ScopedJniThreadState ts(env);
806 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
807 return 0;
808 }
809
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
811 ScopedJniThreadState ts(env);
812 if (obj == NULL) {
813 return NULL;
814 }
815
Elliott Hughes75770752011-08-24 17:52:38 -0700816 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 IndirectReferenceTable& globals = vm->globals;
818 MutexLock mu(vm->globals_lock);
819 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
820 return reinterpret_cast<jobject>(ref);
821 }
822
823 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
824 ScopedJniThreadState ts(env);
825 if (obj == NULL) {
826 return;
827 }
828
Elliott Hughes75770752011-08-24 17:52:38 -0700829 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 IndirectReferenceTable& globals = vm->globals;
831 MutexLock mu(vm->globals_lock);
832
833 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
834 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
835 << "failed to find entry";
836 }
837 }
838
839 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
840 ScopedJniThreadState ts(env);
841 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
842 }
843
844 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
845 ScopedJniThreadState ts(env);
846 if (obj == NULL) {
847 return;
848 }
849
Elliott Hughes75770752011-08-24 17:52:38 -0700850 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700851 IndirectReferenceTable& weak_globals = vm->weak_globals;
852 MutexLock mu(vm->weak_globals_lock);
853
854 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
855 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
856 << "failed to find entry";
857 }
858 }
859
860 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
861 ScopedJniThreadState ts(env);
862 if (obj == NULL) {
863 return NULL;
864 }
865
866 IndirectReferenceTable& locals = ts.Env()->locals;
867
868 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
869 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
870 return reinterpret_cast<jobject>(ref);
871 }
872
873 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
874 ScopedJniThreadState ts(env);
875 if (obj == NULL) {
876 return;
877 }
878
879 IndirectReferenceTable& locals = ts.Env()->locals;
880
881 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
882 if (!locals.Remove(cookie, obj)) {
883 // Attempting to delete a local reference that is not in the
884 // topmost local reference frame is a no-op. DeleteLocalRef returns
885 // void and doesn't throw any exceptions, but we should probably
886 // complain about it so the user will notice that things aren't
887 // going quite the way they expect.
888 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
889 << "failed to find entry";
890 }
891 }
892
893 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
894 ScopedJniThreadState ts(env);
895 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
896 ? JNI_TRUE : JNI_FALSE;
897 }
898
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700899 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700901 Class* c = Decode<Class*>(ts, java_class);
902 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
903 return NULL;
904 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700905 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 }
907
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700909 ScopedJniThreadState ts(env);
910 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 va_start(args, mid);
912 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 va_end(args);
914 return result;
915 }
916
Elliott Hughes72025e52011-08-23 17:50:30 -0700917 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700918 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700919 Class* c = Decode<Class*>(ts, java_class);
920 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
921 return NULL;
922 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700923 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700924 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700925 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700926 return local_result;
927 }
928
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700931 Class* c = Decode<Class*>(ts, java_class);
932 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
933 return NULL;
934 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700935 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700936 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 return local_result;
939 }
940
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
942 ScopedJniThreadState ts(env);
943 return FindMethodID(ts, c, name, sig, false);
944 }
945
946 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
947 ScopedJniThreadState ts(env);
948 return FindMethodID(ts, c, name, sig, true);
949 }
950
Elliott Hughes72025e52011-08-23 17:50:30 -0700951 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 va_list ap;
954 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700955 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700957 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 }
959
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700962 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700963 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 }
965
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700968 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700969 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 }
971
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 va_list ap;
975 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700976 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 va_end(ap);
978 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700983 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 }
985
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700988 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 }
990
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_list ap;
994 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700995 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 va_end(ap);
997 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 }
999
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001002 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 }
1004
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001007 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 va_list ap;
1013 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001014 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 va_end(ap);
1016 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001021 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 }
1023
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001026 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 va_list ap;
1032 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001033 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 va_end(ap);
1035 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001040 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001045 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 va_list ap;
1051 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001052 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 va_end(ap);
1054 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001059 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001064 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_list ap;
1070 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001071 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 va_end(ap);
1073 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001078 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001083 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 va_list ap;
1089 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001090 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_end(ap);
1092 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001097 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001102 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 va_list ap;
1108 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001109 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 va_end(ap);
1111 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001116 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001121 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 va_list ap;
1127 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001128 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001134 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001139 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
1142 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 ScopedJniThreadState ts(env);
1145 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 va_start(ap, mid);
1147 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001148 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 va_end(ap);
1150 return local_result;
1151 }
1152
1153 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001157 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
1160 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001164 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
1167 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 ScopedJniThreadState ts(env);
1170 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 va_start(ap, mid);
1172 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 va_end(ap);
1174 return result.z;
1175 }
1176
1177 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 }
1182
1183 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
1189 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 ScopedJniThreadState ts(env);
1192 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 va_start(ap, mid);
1194 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 va_end(ap);
1196 return result.b;
1197 }
1198
1199 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
1205 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 }
1210
1211 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 ScopedJniThreadState ts(env);
1214 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 va_start(ap, mid);
1216 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 va_end(ap);
1218 return result.c;
1219 }
1220
1221 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
1226
1227 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 }
1232
1233 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 ScopedJniThreadState ts(env);
1236 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 va_start(ap, mid);
1238 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 va_end(ap);
1240 return result.s;
1241 }
1242
1243 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001244 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
1249 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 }
1254
1255 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001256 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 ScopedJniThreadState ts(env);
1258 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 va_start(ap, mid);
1260 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 va_end(ap);
1262 return result.i;
1263 }
1264
1265 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001266 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
1271 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
1277 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 ScopedJniThreadState ts(env);
1280 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 va_start(ap, mid);
1282 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 va_end(ap);
1284 return result.j;
1285 }
1286
1287 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001288 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
1293 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
1298
1299 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001300 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 ScopedJniThreadState ts(env);
1302 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 va_start(ap, mid);
1304 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 va_end(ap);
1306 return result.f;
1307 }
1308
1309 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001310 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
1315 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
1321 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001322 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 ScopedJniThreadState ts(env);
1324 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 va_start(ap, mid);
1326 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 va_end(ap);
1328 return result.d;
1329 }
1330
1331 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
1337 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001340 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
1343 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 ScopedJniThreadState ts(env);
1346 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 va_start(ap, mid);
1348 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 va_end(ap);
1350 }
1351
1352 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001355 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
1358 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001361 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001364 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 ScopedJniThreadState ts(env);
1366 return FindFieldID(ts, c, name, sig, false);
1367 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001368
1369
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001370 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 ScopedJniThreadState ts(env);
1372 return FindFieldID(ts, c, name, sig, true);
1373 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001374
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001375 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001377 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001378 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001379 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001381
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001382 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001384 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001385 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
1387
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001390 Object* o = Decode<Object*>(ts, java_object);
1391 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001392 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001393 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001398 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001399 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 }
1402
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001403#define GET_PRIMITIVE_FIELD(fn, instance) \
1404 ScopedJniThreadState ts(env); \
1405 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001406 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407 return f->fn(o)
1408
1409#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1410 ScopedJniThreadState ts(env); \
1411 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001412 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001413 f->fn(o, value)
1414
1415 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1416 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 }
1418
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001419 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1420 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001423 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1424 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 }
1426
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001427 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1428 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001431 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1432 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1436 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1440 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1444 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1448 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetLong, NULL);
1469 }
1470
1471 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1473 }
1474
1475 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1476 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1477 }
1478
1479 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1480 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1481 }
1482
1483 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1484 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1485 }
1486
1487 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1488 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1489 }
1490
1491 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1492 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1493 }
1494
1495 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1496 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1497 }
1498
1499 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1500 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1501 }
1502
1503 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1504 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1505 }
1506
1507 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1508 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1509 }
1510
1511 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1512 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1513 }
1514
1515 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1516 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1517 }
1518
1519 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1520 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1521 }
1522
1523 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1524 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1525 }
1526
1527 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1528 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1529 }
1530
1531 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1532 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1533 }
1534
1535 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1536 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1537 }
1538
1539 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1540 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 }
1542
1543 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001544 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 ScopedJniThreadState ts(env);
1546 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001547 va_start(ap, mid);
1548 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001549 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 va_end(ap);
1551 return local_result;
1552 }
1553
1554 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001555 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001557 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001558 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
1561 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001562 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001564 JValue result = InvokeWithJValues(ts, 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
1568 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001569 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 ScopedJniThreadState ts(env);
1571 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001572 va_start(ap, mid);
1573 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 va_end(ap);
1575 return result.z;
1576 }
1577
1578 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001579 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001581 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
1584 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001585 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001587 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 }
1589
Elliott Hughes72025e52011-08-23 17:50:30 -07001590 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 ScopedJniThreadState ts(env);
1592 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001593 va_start(ap, mid);
1594 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 va_end(ap);
1596 return result.b;
1597 }
1598
1599 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001600 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001602 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
1605 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001606 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001608 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 }
1610
Elliott Hughes72025e52011-08-23 17:50:30 -07001611 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 ScopedJniThreadState ts(env);
1613 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001614 va_start(ap, mid);
1615 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 va_end(ap);
1617 return result.c;
1618 }
1619
1620 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001621 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001623 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
1626 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001627 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001629 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes72025e52011-08-23 17:50:30 -07001632 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 ScopedJniThreadState ts(env);
1634 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001635 va_start(ap, mid);
1636 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 va_end(ap);
1638 return result.s;
1639 }
1640
1641 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001642 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 }
1646
1647 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001650 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
1652
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 ScopedJniThreadState ts(env);
1655 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001656 va_start(ap, mid);
1657 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 va_end(ap);
1659 return result.i;
1660 }
1661
1662 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001663 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 }
1667
1668 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001669 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001671 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 }
1673
Elliott Hughes72025e52011-08-23 17:50:30 -07001674 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 ScopedJniThreadState ts(env);
1676 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001677 va_start(ap, mid);
1678 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 va_end(ap);
1680 return result.j;
1681 }
1682
1683 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
1689 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001690 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001692 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 }
1694
Elliott Hughes72025e52011-08-23 17:50:30 -07001695 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 ScopedJniThreadState ts(env);
1697 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001698 va_start(ap, mid);
1699 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 va_end(ap);
1701 return result.f;
1702 }
1703
1704 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 }
1709
1710 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001711 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001713 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 }
1715
Elliott Hughes72025e52011-08-23 17:50:30 -07001716 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 ScopedJniThreadState ts(env);
1718 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 va_start(ap, mid);
1720 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 va_end(ap);
1722 return result.d;
1723 }
1724
1725 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001726 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001728 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
1731 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001732 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001734 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 }
1736
Elliott Hughes72025e52011-08-23 17:50:30 -07001737 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 ScopedJniThreadState ts(env);
1739 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001740 va_start(ap, mid);
1741 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 va_end(ap);
1743 }
1744
1745 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001746 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001748 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
1751 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001752 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001754 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes814e4032011-08-23 12:07:56 -07001757 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001759 if (chars == NULL && char_count == 0) {
1760 return NULL;
1761 }
1762 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001763 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 }
1765
1766 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1767 ScopedJniThreadState ts(env);
1768 if (utf == NULL) {
1769 return NULL;
1770 }
1771 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001772 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 }
1774
Elliott Hughes814e4032011-08-23 12:07:56 -07001775 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1776 ScopedJniThreadState ts(env);
1777 return Decode<String*>(ts, java_string)->GetLength();
1778 }
1779
1780 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1781 ScopedJniThreadState ts(env);
1782 return Decode<String*>(ts, java_string)->GetUtfLength();
1783 }
1784
Elliott Hughesb465ab02011-08-24 11:21:21 -07001785 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001787 String* s = Decode<String*>(ts, java_string);
1788 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1789 ThrowSIOOBE(ts, start, length, s->GetLength());
1790 } else {
1791 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1792 memcpy(buf, chars + start, length * sizeof(jchar));
1793 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001794 }
1795
Elliott Hughesb465ab02011-08-24 11:21:21 -07001796 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001797 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001798 String* s = Decode<String*>(ts, java_string);
1799 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1800 ThrowSIOOBE(ts, start, length, s->GetLength());
1801 } else {
1802 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1803 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1804 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001805 }
1806
Elliott Hughes75770752011-08-24 17:52:38 -07001807 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001808 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001809 String* s = Decode<String*>(ts, java_string);
1810 const CharArray* chars = s->GetCharArray();
1811 PinPrimitiveArray(ts, chars);
1812 if (is_copy != NULL) {
1813 *is_copy = JNI_FALSE;
1814 }
1815 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001816 }
1817
Elliott Hughes75770752011-08-24 17:52:38 -07001818 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001819 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001820 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
Elliott Hughes75770752011-08-24 17:52:38 -07001823 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001825 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
Elliott Hughes75770752011-08-24 17:52:38 -07001828 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001830 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes75770752011-08-24 17:52:38 -07001833 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001834 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001835 if (java_string == NULL) {
1836 return NULL;
1837 }
1838 if (is_copy != NULL) {
1839 *is_copy = JNI_TRUE;
1840 }
1841 String* s = Decode<String*>(ts, java_string);
1842 size_t byte_count = s->GetUtfLength();
1843 char* bytes = new char[byte_count + 1];
1844 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001845 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001846 return NULL;
1847 }
1848 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1849 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1850 bytes[byte_count] = '\0';
1851 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001852 }
1853
Elliott Hughes75770752011-08-24 17:52:38 -07001854 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001855 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001856 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001857 }
1858
Elliott Hughesbd935992011-08-22 11:59:34 -07001859 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001861 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001862 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001863 Array* array = obj->AsArray();
1864 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
Elliott Hughes814e4032011-08-23 12:07:56 -07001867 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001869 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001870 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 }
1872
1873 static void SetObjectArrayElement(JNIEnv* env,
1874 jobjectArray java_array, jsize index, jobject java_value) {
1875 ScopedJniThreadState ts(env);
1876 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1877 Object* value = Decode<Object*>(ts, java_value);
1878 array->Set(index, value);
1879 }
1880
1881 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1882 ScopedJniThreadState ts(env);
1883 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1884 }
1885
1886 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1887 ScopedJniThreadState ts(env);
1888 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1889 }
1890
1891 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1892 ScopedJniThreadState ts(env);
1893 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1894 }
1895
1896 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1897 ScopedJniThreadState ts(env);
1898 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1899 }
1900
1901 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1902 ScopedJniThreadState ts(env);
1903 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1904 }
1905
1906 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1907 ScopedJniThreadState ts(env);
1908 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1909 }
1910
1911 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1912 ScopedJniThreadState ts(env);
1913 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1914 }
1915
1916 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1917 ScopedJniThreadState ts(env);
1918 CHECK_GE(length, 0); // TODO: ReportJniError
1919
1920 // Compute the array class corresponding to the given element class.
1921 Class* element_class = Decode<Class*>(ts, element_jclass);
1922 std::string descriptor;
1923 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001924 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001925
1926 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001927 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1928 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001929 return NULL;
1930 }
1931
Elliott Hughes75770752011-08-24 17:52:38 -07001932 // Allocate and initialize if necessary.
1933 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001935 if (initial_element != NULL) {
1936 Object* initial_object = Decode<Object*>(ts, initial_element);
1937 for (jsize i = 0; i < length; ++i) {
1938 result->Set(i, initial_object);
1939 }
1940 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001941 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001942 }
1943
1944 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1945 ScopedJniThreadState ts(env);
1946 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1947 }
1948
Elliott Hughes75770752011-08-24 17:52:38 -07001949 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001950 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001951 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001952 }
1953
Elliott Hughes75770752011-08-24 17:52:38 -07001954 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001955 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001956 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001957 }
1958
Elliott Hughes75770752011-08-24 17:52:38 -07001959 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001961 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001962 }
1963
Elliott Hughes75770752011-08-24 17:52:38 -07001964 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001966 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 }
1968
Elliott Hughes75770752011-08-24 17:52:38 -07001969 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001971 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Elliott Hughes75770752011-08-24 17:52:38 -07001974 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001976 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Elliott Hughes75770752011-08-24 17:52:38 -07001979 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001981 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes75770752011-08-24 17:52:38 -07001984 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001986 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Elliott Hughes75770752011-08-24 17:52:38 -07001989 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001991 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Elliott Hughes75770752011-08-24 17:52:38 -07001994 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001996 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Elliott Hughes75770752011-08-24 17:52:38 -07001999 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002001 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes75770752011-08-24 17:52:38 -07002004 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002006 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Elliott Hughes75770752011-08-24 17:52:38 -07002009 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002011 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes75770752011-08-24 17:52:38 -07002014 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002016 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes75770752011-08-24 17:52:38 -07002019 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002021 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes75770752011-08-24 17:52:38 -07002024 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002026 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes75770752011-08-24 17:52:38 -07002029 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002031 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes75770752011-08-24 17:52:38 -07002034 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002036 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes814e4032011-08-23 12:07:56 -07002039 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002041 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes814e4032011-08-23 12:07:56 -07002044 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002046 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes814e4032011-08-23 12:07:56 -07002049 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002051 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes814e4032011-08-23 12:07:56 -07002054 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
Elliott Hughes5174fe62011-08-23 15:12:35 -07002119 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002121 Class* c = Decode<Class*>(ts, java_class);
2122
Elliott Hughes5174fe62011-08-23 15:12:35 -07002123 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 const char* name = methods[i].name;
2125 const char* sig = methods[i].signature;
2126
2127 if (*sig == '!') {
2128 // TODO: fast jni. it's too noisy to log all these.
2129 ++sig;
2130 }
2131
Elliott Hughes5174fe62011-08-23 15:12:35 -07002132 Method* m = c->FindDirectMethod(name, sig);
2133 if (m == NULL) {
2134 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002136 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002138 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2140 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002141 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002145 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2147 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002148 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 return JNI_ERR;
2150 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002151
Elliott Hughes75770752011-08-24 17:52:38 -07002152 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002153 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002154 }
2155
2156 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 }
2158 return JNI_OK;
2159 }
2160
Elliott Hughes5174fe62011-08-23 15:12:35 -07002161 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002163 Class* c = Decode<Class*>(ts, java_class);
2164
Elliott Hughes75770752011-08-24 17:52:38 -07002165 if (ts.Vm()->verbose_jni) {
Elliott Hughes54e7df12011-09-16 11:47:04 -07002166 LOG(INFO) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002167 }
2168
2169 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2170 Method* m = c->GetDirectMethod(i);
2171 if (m->IsNative()) {
2172 m->UnregisterNative();
2173 }
2174 }
2175 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2176 Method* m = c->GetVirtualMethod(i);
2177 if (m->IsNative()) {
2178 m->UnregisterNative();
2179 }
2180 }
2181
2182 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184
Elliott Hughes72025e52011-08-23 17:50:30 -07002185 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002187 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002188 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Elliott Hughes72025e52011-08-23 17:50:30 -07002191 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002193 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002194 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
2197 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2198 ScopedJniThreadState ts(env);
2199 Runtime* runtime = Runtime::Current();
2200 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002201 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 } else {
2203 *vm = NULL;
2204 }
2205 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2206 }
2207
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2209 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002210
2211 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002212 CHECK(address != NULL); // TODO: ReportJniError
2213 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002214
2215 jclass buffer_class = GetDirectByteBufferClass(env);
2216 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2217 if (mid == NULL) {
2218 return NULL;
2219 }
2220
2221 // At the moment, the Java side is limited to 32 bits.
2222 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2223 CHECK_LE(capacity, 0xffffffff);
2224 jint address_arg = reinterpret_cast<jint>(address);
2225 jint capacity_arg = static_cast<jint>(capacity);
2226
2227 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2228 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Elliott Hughesb465ab02011-08-24 11:21:21 -07002231 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002233 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2234 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 }
2236
Elliott Hughesb465ab02011-08-24 11:21:21 -07002237 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002239 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2240 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 }
2242
Elliott Hughesb465ab02011-08-24 11:21:21 -07002243 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002245
Elliott Hughes75770752011-08-24 17:52:38 -07002246 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247
2248 // Do we definitely know what kind of reference this is?
2249 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2250 IndirectRefKind kind = GetIndirectRefKind(ref);
2251 switch (kind) {
2252 case kLocal:
2253 return JNILocalRefType;
2254 case kGlobal:
2255 return JNIGlobalRefType;
2256 case kWeakGlobal:
2257 return JNIWeakGlobalRefType;
2258 case kSirtOrInvalid:
2259 // Is it in a stack IRT?
2260 if (ts.Self()->SirtContains(java_object)) {
2261 return JNILocalRefType;
2262 }
2263
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002264 if (!ts.Env()->work_around_app_jni_bugs) {
2265 return JNIInvalidRefType;
2266 }
2267
Elliott Hughesb465ab02011-08-24 11:21:21 -07002268 // If we're handing out direct pointers, check whether it's a direct pointer
2269 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002270 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002271 if (ts.Env()->locals.Contains(java_object)) {
2272 return JNILocalRefType;
2273 }
2274 }
2275
2276 return JNIInvalidRefType;
2277 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 }
2279};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002280
Elliott Hughesa2501992011-08-26 19:39:54 -07002281const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002282 NULL, // reserved0.
2283 NULL, // reserved1.
2284 NULL, // reserved2.
2285 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002286 JNI::GetVersion,
2287 JNI::DefineClass,
2288 JNI::FindClass,
2289 JNI::FromReflectedMethod,
2290 JNI::FromReflectedField,
2291 JNI::ToReflectedMethod,
2292 JNI::GetSuperclass,
2293 JNI::IsAssignableFrom,
2294 JNI::ToReflectedField,
2295 JNI::Throw,
2296 JNI::ThrowNew,
2297 JNI::ExceptionOccurred,
2298 JNI::ExceptionDescribe,
2299 JNI::ExceptionClear,
2300 JNI::FatalError,
2301 JNI::PushLocalFrame,
2302 JNI::PopLocalFrame,
2303 JNI::NewGlobalRef,
2304 JNI::DeleteGlobalRef,
2305 JNI::DeleteLocalRef,
2306 JNI::IsSameObject,
2307 JNI::NewLocalRef,
2308 JNI::EnsureLocalCapacity,
2309 JNI::AllocObject,
2310 JNI::NewObject,
2311 JNI::NewObjectV,
2312 JNI::NewObjectA,
2313 JNI::GetObjectClass,
2314 JNI::IsInstanceOf,
2315 JNI::GetMethodID,
2316 JNI::CallObjectMethod,
2317 JNI::CallObjectMethodV,
2318 JNI::CallObjectMethodA,
2319 JNI::CallBooleanMethod,
2320 JNI::CallBooleanMethodV,
2321 JNI::CallBooleanMethodA,
2322 JNI::CallByteMethod,
2323 JNI::CallByteMethodV,
2324 JNI::CallByteMethodA,
2325 JNI::CallCharMethod,
2326 JNI::CallCharMethodV,
2327 JNI::CallCharMethodA,
2328 JNI::CallShortMethod,
2329 JNI::CallShortMethodV,
2330 JNI::CallShortMethodA,
2331 JNI::CallIntMethod,
2332 JNI::CallIntMethodV,
2333 JNI::CallIntMethodA,
2334 JNI::CallLongMethod,
2335 JNI::CallLongMethodV,
2336 JNI::CallLongMethodA,
2337 JNI::CallFloatMethod,
2338 JNI::CallFloatMethodV,
2339 JNI::CallFloatMethodA,
2340 JNI::CallDoubleMethod,
2341 JNI::CallDoubleMethodV,
2342 JNI::CallDoubleMethodA,
2343 JNI::CallVoidMethod,
2344 JNI::CallVoidMethodV,
2345 JNI::CallVoidMethodA,
2346 JNI::CallNonvirtualObjectMethod,
2347 JNI::CallNonvirtualObjectMethodV,
2348 JNI::CallNonvirtualObjectMethodA,
2349 JNI::CallNonvirtualBooleanMethod,
2350 JNI::CallNonvirtualBooleanMethodV,
2351 JNI::CallNonvirtualBooleanMethodA,
2352 JNI::CallNonvirtualByteMethod,
2353 JNI::CallNonvirtualByteMethodV,
2354 JNI::CallNonvirtualByteMethodA,
2355 JNI::CallNonvirtualCharMethod,
2356 JNI::CallNonvirtualCharMethodV,
2357 JNI::CallNonvirtualCharMethodA,
2358 JNI::CallNonvirtualShortMethod,
2359 JNI::CallNonvirtualShortMethodV,
2360 JNI::CallNonvirtualShortMethodA,
2361 JNI::CallNonvirtualIntMethod,
2362 JNI::CallNonvirtualIntMethodV,
2363 JNI::CallNonvirtualIntMethodA,
2364 JNI::CallNonvirtualLongMethod,
2365 JNI::CallNonvirtualLongMethodV,
2366 JNI::CallNonvirtualLongMethodA,
2367 JNI::CallNonvirtualFloatMethod,
2368 JNI::CallNonvirtualFloatMethodV,
2369 JNI::CallNonvirtualFloatMethodA,
2370 JNI::CallNonvirtualDoubleMethod,
2371 JNI::CallNonvirtualDoubleMethodV,
2372 JNI::CallNonvirtualDoubleMethodA,
2373 JNI::CallNonvirtualVoidMethod,
2374 JNI::CallNonvirtualVoidMethodV,
2375 JNI::CallNonvirtualVoidMethodA,
2376 JNI::GetFieldID,
2377 JNI::GetObjectField,
2378 JNI::GetBooleanField,
2379 JNI::GetByteField,
2380 JNI::GetCharField,
2381 JNI::GetShortField,
2382 JNI::GetIntField,
2383 JNI::GetLongField,
2384 JNI::GetFloatField,
2385 JNI::GetDoubleField,
2386 JNI::SetObjectField,
2387 JNI::SetBooleanField,
2388 JNI::SetByteField,
2389 JNI::SetCharField,
2390 JNI::SetShortField,
2391 JNI::SetIntField,
2392 JNI::SetLongField,
2393 JNI::SetFloatField,
2394 JNI::SetDoubleField,
2395 JNI::GetStaticMethodID,
2396 JNI::CallStaticObjectMethod,
2397 JNI::CallStaticObjectMethodV,
2398 JNI::CallStaticObjectMethodA,
2399 JNI::CallStaticBooleanMethod,
2400 JNI::CallStaticBooleanMethodV,
2401 JNI::CallStaticBooleanMethodA,
2402 JNI::CallStaticByteMethod,
2403 JNI::CallStaticByteMethodV,
2404 JNI::CallStaticByteMethodA,
2405 JNI::CallStaticCharMethod,
2406 JNI::CallStaticCharMethodV,
2407 JNI::CallStaticCharMethodA,
2408 JNI::CallStaticShortMethod,
2409 JNI::CallStaticShortMethodV,
2410 JNI::CallStaticShortMethodA,
2411 JNI::CallStaticIntMethod,
2412 JNI::CallStaticIntMethodV,
2413 JNI::CallStaticIntMethodA,
2414 JNI::CallStaticLongMethod,
2415 JNI::CallStaticLongMethodV,
2416 JNI::CallStaticLongMethodA,
2417 JNI::CallStaticFloatMethod,
2418 JNI::CallStaticFloatMethodV,
2419 JNI::CallStaticFloatMethodA,
2420 JNI::CallStaticDoubleMethod,
2421 JNI::CallStaticDoubleMethodV,
2422 JNI::CallStaticDoubleMethodA,
2423 JNI::CallStaticVoidMethod,
2424 JNI::CallStaticVoidMethodV,
2425 JNI::CallStaticVoidMethodA,
2426 JNI::GetStaticFieldID,
2427 JNI::GetStaticObjectField,
2428 JNI::GetStaticBooleanField,
2429 JNI::GetStaticByteField,
2430 JNI::GetStaticCharField,
2431 JNI::GetStaticShortField,
2432 JNI::GetStaticIntField,
2433 JNI::GetStaticLongField,
2434 JNI::GetStaticFloatField,
2435 JNI::GetStaticDoubleField,
2436 JNI::SetStaticObjectField,
2437 JNI::SetStaticBooleanField,
2438 JNI::SetStaticByteField,
2439 JNI::SetStaticCharField,
2440 JNI::SetStaticShortField,
2441 JNI::SetStaticIntField,
2442 JNI::SetStaticLongField,
2443 JNI::SetStaticFloatField,
2444 JNI::SetStaticDoubleField,
2445 JNI::NewString,
2446 JNI::GetStringLength,
2447 JNI::GetStringChars,
2448 JNI::ReleaseStringChars,
2449 JNI::NewStringUTF,
2450 JNI::GetStringUTFLength,
2451 JNI::GetStringUTFChars,
2452 JNI::ReleaseStringUTFChars,
2453 JNI::GetArrayLength,
2454 JNI::NewObjectArray,
2455 JNI::GetObjectArrayElement,
2456 JNI::SetObjectArrayElement,
2457 JNI::NewBooleanArray,
2458 JNI::NewByteArray,
2459 JNI::NewCharArray,
2460 JNI::NewShortArray,
2461 JNI::NewIntArray,
2462 JNI::NewLongArray,
2463 JNI::NewFloatArray,
2464 JNI::NewDoubleArray,
2465 JNI::GetBooleanArrayElements,
2466 JNI::GetByteArrayElements,
2467 JNI::GetCharArrayElements,
2468 JNI::GetShortArrayElements,
2469 JNI::GetIntArrayElements,
2470 JNI::GetLongArrayElements,
2471 JNI::GetFloatArrayElements,
2472 JNI::GetDoubleArrayElements,
2473 JNI::ReleaseBooleanArrayElements,
2474 JNI::ReleaseByteArrayElements,
2475 JNI::ReleaseCharArrayElements,
2476 JNI::ReleaseShortArrayElements,
2477 JNI::ReleaseIntArrayElements,
2478 JNI::ReleaseLongArrayElements,
2479 JNI::ReleaseFloatArrayElements,
2480 JNI::ReleaseDoubleArrayElements,
2481 JNI::GetBooleanArrayRegion,
2482 JNI::GetByteArrayRegion,
2483 JNI::GetCharArrayRegion,
2484 JNI::GetShortArrayRegion,
2485 JNI::GetIntArrayRegion,
2486 JNI::GetLongArrayRegion,
2487 JNI::GetFloatArrayRegion,
2488 JNI::GetDoubleArrayRegion,
2489 JNI::SetBooleanArrayRegion,
2490 JNI::SetByteArrayRegion,
2491 JNI::SetCharArrayRegion,
2492 JNI::SetShortArrayRegion,
2493 JNI::SetIntArrayRegion,
2494 JNI::SetLongArrayRegion,
2495 JNI::SetFloatArrayRegion,
2496 JNI::SetDoubleArrayRegion,
2497 JNI::RegisterNatives,
2498 JNI::UnregisterNatives,
2499 JNI::MonitorEnter,
2500 JNI::MonitorExit,
2501 JNI::GetJavaVM,
2502 JNI::GetStringRegion,
2503 JNI::GetStringUTFRegion,
2504 JNI::GetPrimitiveArrayCritical,
2505 JNI::ReleasePrimitiveArrayCritical,
2506 JNI::GetStringCritical,
2507 JNI::ReleaseStringCritical,
2508 JNI::NewWeakGlobalRef,
2509 JNI::DeleteWeakGlobalRef,
2510 JNI::ExceptionCheck,
2511 JNI::NewDirectByteBuffer,
2512 JNI::GetDirectBufferAddress,
2513 JNI::GetDirectBufferCapacity,
2514 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002515};
2516
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002517static const size_t kMonitorsInitial = 32; // Arbitrary.
2518static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2519
2520static const size_t kLocalsInitial = 64; // Arbitrary.
2521static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002522
Elliott Hughes75770752011-08-24 17:52:38 -07002523JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002524 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002525 vm(vm),
2526 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002527 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002528 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002529 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2530 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002531 functions = unchecked_functions = &gNativeInterface;
2532 if (check_jni) {
2533 functions = GetCheckJniNativeInterface();
2534 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002535}
2536
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002537JNIEnvExt::~JNIEnvExt() {
2538}
2539
Carl Shapiroea4dca82011-08-01 13:45:38 -07002540// JNI Invocation interface.
2541
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002542extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2543 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2544 if (args->version < JNI_VERSION_1_2) {
2545 return JNI_EVERSION;
2546 }
2547 Runtime::Options options;
2548 for (int i = 0; i < args->nOptions; ++i) {
2549 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002550 options.push_back(std::make_pair(StringPiece(option->optionString),
2551 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002552 }
2553 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002554 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002555 if (runtime == NULL) {
2556 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002557 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002558 runtime->Start();
2559 *p_env = Thread::Current()->GetJniEnv();
2560 *p_vm = runtime->GetJavaVM();
2561 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002562}
2563
Elliott Hughesf2682d52011-08-15 16:37:04 -07002564extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002565 Runtime* runtime = Runtime::Current();
2566 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002567 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002568 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002569 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002570 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002571 }
2572 return JNI_OK;
2573}
2574
2575// Historically unsupported.
2576extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2577 return JNI_ERR;
2578}
2579
Elliott Hughescdf53122011-08-19 15:46:09 -07002580class JII {
2581 public:
2582 static jint DestroyJavaVM(JavaVM* vm) {
2583 if (vm == NULL) {
2584 return JNI_ERR;
2585 } else {
2586 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2587 delete raw_vm->runtime;
2588 return JNI_OK;
2589 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002590 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002591
Elliott Hughescdf53122011-08-19 15:46:09 -07002592 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002593 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002594 }
2595
2596 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002597 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002598 }
2599
2600 static jint DetachCurrentThread(JavaVM* vm) {
2601 if (vm == NULL) {
2602 return JNI_ERR;
2603 } else {
2604 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2605 Runtime* runtime = raw_vm->runtime;
2606 runtime->DetachCurrentThread();
2607 return JNI_OK;
2608 }
2609 }
2610
2611 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2612 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2613 return JNI_EVERSION;
2614 }
2615 if (vm == NULL || env == NULL) {
2616 return JNI_ERR;
2617 }
2618 Thread* thread = Thread::Current();
2619 if (thread == NULL) {
2620 *env = NULL;
2621 return JNI_EDETACHED;
2622 }
2623 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002624 return JNI_OK;
2625 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002626};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002627
Elliott Hughesa2501992011-08-26 19:39:54 -07002628const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002629 NULL, // reserved0
2630 NULL, // reserved1
2631 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002632 JII::DestroyJavaVM,
2633 JII::AttachCurrentThread,
2634 JII::DetachCurrentThread,
2635 JII::GetEnv,
2636 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002637};
2638
Elliott Hughesbbd76712011-08-17 10:25:24 -07002639static const size_t kPinTableInitialSize = 16;
2640static const size_t kPinTableMaxSize = 1024;
2641
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002642static const size_t kGlobalsInitial = 512; // Arbitrary.
2643static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2644
2645static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2646static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2647
Elliott Hughesa0957642011-09-02 14:27:33 -07002648JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002649 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002650 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002651 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002652 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002653 verbose_jni(options->IsVerbose("jni")),
2654 log_third_party_jni(options->IsVerbose("third-party-jni")),
2655 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002656 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002657 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002658 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002659 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002660 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002661 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002662 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002663 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002664 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002665 functions = unchecked_functions = &gInvokeInterface;
2666 if (check_jni) {
2667 functions = GetCheckJniInvokeInterface();
2668 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002669}
2670
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002671JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002672 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002673}
2674
Elliott Hughes75770752011-08-24 17:52:38 -07002675bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2676 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002677
2678 // See if we've already loaded this library. If we have, and the class loader
2679 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002680 // TODO: for better results we should canonicalize the pathname (or even compare
2681 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002682 SharedLibrary* library;
2683 {
2684 // TODO: move the locking (and more of this logic) into Libraries.
2685 MutexLock mu(libraries_lock);
2686 library = libraries->Get(path);
2687 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002688 if (library != NULL) {
2689 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002690 // The library will be associated with class_loader. The JNI
2691 // spec says we can't load the same library into more than one
2692 // class loader.
2693 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2694 "ClassLoader %p; can't open in ClassLoader %p",
2695 path.c_str(), library->GetClassLoader(), class_loader);
2696 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002697 return false;
2698 }
2699 if (verbose_jni) {
2700 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2701 << "ClassLoader " << class_loader << "]";
2702 }
2703 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002704 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2705 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002706 return false;
2707 }
2708 return true;
2709 }
2710
2711 // Open the shared library. Because we're using a full path, the system
2712 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2713 // resolve this library's dependencies though.)
2714
2715 // Failures here are expected when java.library.path has several entries
2716 // and we have to hunt for the lib.
2717
2718 // The current version of the dynamic linker prints detailed information
2719 // about dlopen() failures. Some things to check if the message is
2720 // cryptic:
2721 // - make sure the library exists on the device
2722 // - verify that the right path is being opened (the debug log message
2723 // above can help with that)
2724 // - check to see if the library is valid (e.g. not zero bytes long)
2725 // - check config/prelink-linux-arm.map to ensure that the library
2726 // is listed and is not being overrun by the previous entry (if
2727 // loading suddenly stops working on a prelinked library, this is
2728 // a good one to check)
2729 // - write a trivial app that calls sleep() then dlopen(), attach
2730 // to it with "strace -p <pid>" while it sleeps, and watch for
2731 // attempts to open nonexistent dependent shared libs
2732
2733 // TODO: automate some of these checks!
2734
2735 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002736 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002737 // the GC to ignore us.
2738 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002739 void* handle = NULL;
2740 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002741 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002742 handle = dlopen(path.c_str(), RTLD_LAZY);
2743 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002744
2745 if (verbose_jni) {
2746 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2747 }
2748
2749 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002750 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002751 return false;
2752 }
2753
2754 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002755 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002756 // TODO: move the locking (and more of this logic) into Libraries.
2757 MutexLock mu(libraries_lock);
2758 library = libraries->Get(path);
2759 if (library != NULL) {
2760 LOG(INFO) << "WOW: we lost a race to add shared library: "
2761 << "\"" << path << "\" ClassLoader=" << class_loader;
2762 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002763 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002764 library = new SharedLibrary(path, handle, class_loader);
2765 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002766 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002767
2768 if (verbose_jni) {
2769 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2770 }
2771
2772 bool result = true;
2773 void* sym = dlsym(handle, "JNI_OnLoad");
2774 if (sym == NULL) {
2775 if (verbose_jni) {
2776 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2777 }
2778 } else {
2779 // Call JNI_OnLoad. We have to override the current class
2780 // loader, which will always be "null" since the stuff at the
2781 // top of the stack is around Runtime.loadLibrary(). (See
2782 // the comments in the JNI FindClass function.)
2783 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2784 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002785 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002786 self->SetClassLoaderOverride(class_loader);
2787
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002788 int version = 0;
2789 {
2790 ScopedThreadStateChange tsc(self, Thread::kNative);
2791 if (verbose_jni) {
2792 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2793 }
2794 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002795 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002796
2797 self->SetClassLoaderOverride(old_class_loader);;
2798
2799 if (version != JNI_VERSION_1_2 &&
2800 version != JNI_VERSION_1_4 &&
2801 version != JNI_VERSION_1_6) {
2802 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2803 << "bad version: " << version;
2804 // It's unwise to call dlclose() here, but we can mark it
2805 // as bad and ensure that future load attempts will fail.
2806 // We don't know how far JNI_OnLoad got, so there could
2807 // be some partially-initialized stuff accessible through
2808 // newly-registered native method calls. We could try to
2809 // unregister them, but that doesn't seem worthwhile.
2810 result = false;
2811 } else {
2812 if (verbose_jni) {
2813 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2814 << " from JNI_OnLoad in \"" << path << "\"]";
2815 }
2816 }
2817 }
2818
2819 library->SetResult(result);
2820 return result;
2821}
2822
2823void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2824 CHECK(m->IsNative());
2825
2826 Class* c = m->GetDeclaringClass();
2827
2828 // If this is a static method, it could be called before the class
2829 // has been initialized.
2830 if (m->IsStatic()) {
2831 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2832 return NULL;
2833 }
2834 } else {
2835 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2836 }
2837
Brian Carlstrom16192862011-09-12 17:50:06 -07002838 std::string detail;
2839 void* native_method;
2840 {
2841 MutexLock mu(libraries_lock);
2842 native_method = libraries->FindNativeMethod(m, detail);
2843 }
2844 // throwing can cause libraries_lock to be reacquired
2845 if (native_method == NULL) {
2846 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
2847 "%s", detail.c_str());
2848 }
2849 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002850}
2851
Elliott Hughes410c0c82011-09-01 17:58:25 -07002852void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2853 {
2854 MutexLock mu(globals_lock);
2855 globals.VisitRoots(visitor, arg);
2856 }
2857 {
2858 MutexLock mu(pins_lock);
2859 pin_table.VisitRoots(visitor, arg);
2860 }
2861 // The weak_globals table is visited by the GC itself (because it mutates the table).
2862}
2863
Ian Rogersdf20fe02011-07-20 20:34:16 -07002864} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002865
2866std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2867 switch (rhs) {
2868 case JNIInvalidRefType:
2869 os << "JNIInvalidRefType";
2870 return os;
2871 case JNILocalRefType:
2872 os << "JNILocalRefType";
2873 return os;
2874 case JNIGlobalRefType:
2875 os << "JNIGlobalRefType";
2876 return os;
2877 case JNIWeakGlobalRefType:
2878 os << "JNIWeakGlobalRefType";
2879 return os;
2880 }
2881}