blob: 56be2f4acde4b9c943373b906780449ae5719553 [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 Hughese5b0dc82011-08-23 09:59:02 -070085 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -070086 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -070087 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070088 locals.Dump();
89 // TODO: dvmDumpThread(dvmThreadSelf(), false);
90 // dvmAbort();
91 }
92 }
93#endif
94
Elliott Hughesbf86d042011-08-31 17:53:14 -070095 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070096 // Hand out direct pointers to support broken old apps.
97 return reinterpret_cast<T>(obj);
98 }
99
100 return reinterpret_cast<T>(ref);
101}
102
Elliott Hughesbf86d042011-08-31 17:53:14 -0700103// For external use.
104template<typename T>
105T Decode(JNIEnv* public_env, jobject obj) {
106 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
107 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
108}
109// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700110template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700111template Class* Decode<Class*>(JNIEnv*, jobject);
112template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
113template Object* Decode<Object*>(JNIEnv*, jobject);
114template String* Decode<String*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700115template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700116
117namespace {
118
Elliott Hughescdf53122011-08-19 15:46:09 -0700119jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
120 if (obj == NULL) {
121 return NULL;
122 }
Elliott Hughes75770752011-08-24 17:52:38 -0700123 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700124 IndirectReferenceTable& weak_globals = vm->weak_globals;
125 MutexLock mu(vm->weak_globals_lock);
126 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
127 return reinterpret_cast<jweak>(ref);
128}
129
Elliott Hughesbf86d042011-08-31 17:53:14 -0700130// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700131template<typename T>
132T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700133 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700134}
135
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700136byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700137 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700138 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700139 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700140 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
141 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700142 case 'Z':
143 case 'B':
144 case 'C':
145 case 'S':
146 case 'I':
147 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
148 offset += 4;
149 break;
150 case 'F':
151 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
152 offset += 4;
153 break;
154 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700155 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700156 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
157 offset += sizeof(Object*);
158 break;
159 }
160 case 'D':
161 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
162 offset += 8;
163 break;
164 case 'J':
165 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
166 offset += 8;
167 break;
168 }
169 }
170 return arg_array.release();
171}
172
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700173byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700174 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700175 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700176 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700177 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
178 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700179 case 'Z':
180 case 'B':
181 case 'C':
182 case 'S':
183 case 'I':
184 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
185 offset += 4;
186 break;
187 case 'F':
188 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
189 offset += 4;
190 break;
191 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700192 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700193 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
194 offset += sizeof(Object*);
195 break;
196 }
197 case 'D':
198 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
199 offset += 8;
200 break;
201 case 'J':
202 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
203 offset += 8;
204 break;
205 }
206 }
207 return arg_array.release();
208}
209
Elliott Hughes72025e52011-08-23 17:50:30 -0700210JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
211 Method* method, byte* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700212 JValue result;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700213 method->Invoke(ts.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700214 return result;
215}
216
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700217JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700218 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700219 Method* method = DecodeMethod(mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700220 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700221 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700222}
223
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700224JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700225 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700226 Method* method = DecodeMethod(mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700227 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700228 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
229}
230
Elliott Hughes75770752011-08-24 17:52:38 -0700231Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700232 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700233}
234
Brian Carlstrom30b94452011-08-25 21:35:26 -0700235JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700236 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700237 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700238 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700239 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
240}
241
Brian Carlstrom30b94452011-08-25 21:35:26 -0700242JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700243 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700244 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700245 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700246 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700247}
248
Elliott Hughes6b436852011-08-12 10:16:44 -0700249// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
250// separated with slashes but aren't wrapped with "L;" like regular descriptors
251// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
252// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
253// supported names with dots too (such as "a.b.C").
254std::string NormalizeJniClassDescriptor(const char* name) {
255 std::string result;
256 // Add the missing "L;" if necessary.
257 if (name[0] == '[') {
258 result = name;
259 } else {
260 result += 'L';
261 result += name;
262 result += ';';
263 }
264 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700265 if (result.find('.') != std::string::npos) {
266 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
267 << "\"" << name << "\"";
268 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700269 }
270 return result;
271}
272
Elliott Hughescdf53122011-08-19 15:46:09 -0700273jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
274 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700275 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
276 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700277 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700278
279 Method* method = NULL;
280 if (is_static) {
281 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700282 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700283 method = c->FindVirtualMethod(name, sig);
284 if (method == NULL) {
285 // No virtual method matching the signature. Search declared
286 // private methods and constructors.
287 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700288 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700289 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700290
Elliott Hughescdf53122011-08-19 15:46:09 -0700291 if (method == NULL || method->IsStatic() != is_static) {
292 Thread* self = Thread::Current();
Elliott Hughesa2501992011-08-26 19:39:54 -0700293 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700294 // TODO: try searching for the opposite kind of method from is_static
295 // for better diagnostics?
296 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700297 "no %s method %s", is_static ? "static" : "non-static",
298 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700299 return NULL;
300 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700301
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700302 return reinterpret_cast<jmethodID>(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700303}
304
Elliott Hughescdf53122011-08-19 15:46:09 -0700305jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
306 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700307 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
308 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700309 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700310
311 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 Class* field_type;
313 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
314 if (sig[1] != '\0') {
315 // TODO: need to get the appropriate ClassLoader.
316 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
317 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700318 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700319 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700320 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700321 if (field_type == NULL) {
322 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700323 DCHECK(ts.Self()->IsExceptionPending());
324 ts.Self()->ClearException();
325 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
326 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
327 "no type \"%s\" found and so no field \"%s\" could be found in class "
328 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 return NULL;
330 }
331 if (is_static) {
332 field = c->FindStaticField(name, field_type);
333 } else {
334 field = c->FindInstanceField(name, field_type);
335 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700336 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700337 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700338 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700339 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700340 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700341 return NULL;
342 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343 // Check invariant that all jfieldIDs have resolved types (how else would
344 // the type equality in Find...Field hold?)
345 DCHECK(field->GetType() != NULL);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700346 return reinterpret_cast<jfieldID>(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700347}
348
Elliott Hughes75770752011-08-24 17:52:38 -0700349void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
350 JavaVMExt* vm = ts.Vm();
351 MutexLock mu(vm->pins_lock);
352 vm->pin_table.Add(array);
353}
354
355void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
356 JavaVMExt* vm = ts.Vm();
357 MutexLock mu(vm->pins_lock);
358 vm->pin_table.Remove(array);
359}
360
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700361template<typename JniT, typename ArtT>
362JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
363 CHECK_GE(length, 0); // TODO: ReportJniError
364 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700365 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700366}
367
Elliott Hughes75770752011-08-24 17:52:38 -0700368template <typename ArrayT, typename CArrayT, typename ArtArrayT>
369CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
370 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
371 PinPrimitiveArray(ts, array);
372 if (is_copy != NULL) {
373 *is_copy = JNI_FALSE;
374 }
375 return array->GetData();
376}
377
378template <typename ArrayT>
379void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
380 if (mode != JNI_COMMIT) {
381 Array* array = Decode<Array*>(ts, java_array);
382 UnpinPrimitiveArray(ts, array);
383 }
384}
385
Elliott Hughes814e4032011-08-23 12:07:56 -0700386void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
387 std::string type(PrettyType(array));
388 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
389 "%s offset=%d length=%d %s.length=%d",
390 type.c_str(), start, length, identifier, array->GetLength());
391}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700392void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
393 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
394 "offset=%d length=%d string.length()=%d", start, length, array_length);
395}
Elliott Hughes814e4032011-08-23 12:07:56 -0700396
397template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700398void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700399 ArrayT* array = Decode<ArrayT*>(ts, java_array);
400 if (start < 0 || length < 0 || start + length > array->GetLength()) {
401 ThrowAIOOBE(ts, array, start, length, "src");
402 } else {
403 JavaT* data = array->GetData();
404 memcpy(buf, data + start, length * sizeof(JavaT));
405 }
406}
407
408template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700409void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700410 ArrayT* array = Decode<ArrayT*>(ts, java_array);
411 if (start < 0 || length < 0 || start + length > array->GetLength()) {
412 ThrowAIOOBE(ts, array, start, length, "dst");
413 } else {
414 JavaT* data = array->GetData();
415 memcpy(data + start, buf, length * sizeof(JavaT));
416 }
417}
418
Elliott Hughes75770752011-08-24 17:52:38 -0700419jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700420 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
421 CHECK(buffer_class.get() != NULL);
422 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
423}
424
Elliott Hughes75770752011-08-24 17:52:38 -0700425jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700426 static jclass buffer_class = InitDirectByteBufferClass(env);
427 return buffer_class;
428}
429
Elliott Hughes75770752011-08-24 17:52:38 -0700430jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
431 if (vm == NULL || p_env == NULL) {
432 return JNI_ERR;
433 }
434
435 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
436 JavaVMAttachArgs args;
437 if (thr_args == NULL) {
438 // Allow the v1.1 calling convention.
439 args.version = JNI_VERSION_1_2;
440 args.name = NULL;
441 args.group = NULL; // TODO: get "main" thread group
442 } else {
443 args.version = in_args->version;
444 args.name = in_args->name;
445 if (in_args->group != NULL) {
446 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
447 args.group = NULL; // TODO: decode in_args->group
448 } else {
449 args.group = NULL; // TODO: get "main" thread group
450 }
451 }
452 CHECK_GE(args.version, JNI_VERSION_1_2);
453
454 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700455 runtime->AttachCurrentThread(args.name, as_daemon);
456 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700457 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700458}
459
Elliott Hughes79082e32011-08-25 12:07:32 -0700460class SharedLibrary {
461 public:
462 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
463 : path_(path),
464 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700465 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700466 jni_on_load_lock_("JNI_OnLoad lock"),
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 Hughes8d768a92011-09-14 16:35:25 -0700469 CHECK_PTHREAD_CALL(pthread_cond_init, (&jni_on_load_cond_, NULL), "jni_on_load_cond_");
Elliott Hughes79082e32011-08-25 12:07:32 -0700470 }
471
Elliott Hughes79082e32011-08-25 12:07:32 -0700472 Object* GetClassLoader() {
473 return class_loader_;
474 }
475
476 std::string GetPath() {
477 return path_;
478 }
479
480 /*
481 * Check the result of an earlier call to JNI_OnLoad on this library. If
482 * the call has not yet finished in another thread, wait for it.
483 */
484 bool CheckOnLoadResult(JavaVMExt* vm) {
485 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700486 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700487 // Check this so we don't end up waiting for ourselves. We need
488 // to return "true" so the caller can continue.
489 LOG(INFO) << *self << " recursive attempt to load library "
490 << "\"" << path_ << "\"";
491 return true;
492 }
493
494 MutexLock mu(jni_on_load_lock_);
495 while (jni_on_load_result_ == kPending) {
496 if (vm->verbose_jni) {
497 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
498 << "JNI_OnLoad...]";
499 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700500 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700501 CHECK_PTHREAD_CALL(pthread_cond_wait, (&jni_on_load_cond_, jni_on_load_lock_.GetImpl()), "JNI_OnLoad");
Elliott Hughes79082e32011-08-25 12:07:32 -0700502 }
503
504 bool okay = (jni_on_load_result_ == kOkay);
505 if (vm->verbose_jni) {
506 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
507 << (okay ? "succeeded" : "failed") << "]";
508 }
509 return okay;
510 }
511
512 void SetResult(bool result) {
513 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700514 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700515
516 // Broadcast a wakeup to anybody sleeping on the condition variable.
517 MutexLock mu(jni_on_load_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700518 CHECK_PTHREAD_CALL(pthread_cond_broadcast, (&jni_on_load_cond_), "JNI_OnLoad");
Elliott Hughes79082e32011-08-25 12:07:32 -0700519 }
520
521 void* FindSymbol(const std::string& symbol_name) {
522 return dlsym(handle_, symbol_name.c_str());
523 }
524
525 private:
526 enum JNI_OnLoadState {
527 kPending,
528 kFailed,
529 kOkay,
530 };
531
532 // Path to library "/system/lib/libjni.so".
533 std::string path_;
534
535 // The void* returned by dlopen(3).
536 void* handle_;
537
538 // The ClassLoader this library is associated with.
539 Object* class_loader_;
540
541 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700542 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700543 // Wait for JNI_OnLoad in other thread.
544 pthread_cond_t jni_on_load_cond_;
545 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700546 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700547 // Result of earlier JNI_OnLoad call.
548 JNI_OnLoadState jni_on_load_result_;
549};
550
Elliott Hughescdf53122011-08-19 15:46:09 -0700551} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700552
Elliott Hughes79082e32011-08-25 12:07:32 -0700553// This exists mainly to keep implementation details out of the header file.
554class Libraries {
555 public:
556 Libraries() {
557 }
558
559 ~Libraries() {
560 // Delete our map values. (The keys will be cleaned up by the map itself.)
561 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
562 delete it->second;
563 }
564 }
565
566 SharedLibrary* Get(const std::string& path) {
567 return libraries_[path];
568 }
569
570 void Put(const std::string& path, SharedLibrary* library) {
571 libraries_[path] = library;
572 }
573
574 // See section 11.3 "Linking Native Methods" of the JNI spec.
575 void* FindNativeMethod(const Method* m) {
576 std::string jni_short_name(JniShortName(m));
577 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700578 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700579 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
580 SharedLibrary* library = it->second;
581 if (library->GetClassLoader() != declaring_class_loader) {
582 // We only search libraries loaded by the appropriate ClassLoader.
583 continue;
584 }
585 // Try the short name then the long name...
586 void* fn = library->FindSymbol(jni_short_name);
587 if (fn == NULL) {
588 fn = library->FindSymbol(jni_long_name);
589 }
590 if (fn != NULL) {
591 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700592 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700593 << " in \"" << library->GetPath() << "\"]";
594 }
595 return fn;
596 }
597 }
598 std::string detail;
599 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700600 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700601 LOG(ERROR) << detail;
602 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
603 "%s", detail.c_str());
604 return NULL;
605 }
606
607 private:
608 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
609
610 std::map<std::string, SharedLibrary*> libraries_;
611};
612
Elliott Hughescdf53122011-08-19 15:46:09 -0700613class JNI {
614 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700615
Elliott Hughescdf53122011-08-19 15:46:09 -0700616 static jint GetVersion(JNIEnv* env) {
617 ScopedJniThreadState ts(env);
618 return JNI_VERSION_1_6;
619 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700620
Elliott Hughescdf53122011-08-19 15:46:09 -0700621 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
622 ScopedJniThreadState ts(env);
623 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700624 return NULL;
625 }
626
Elliott Hughescdf53122011-08-19 15:46:09 -0700627 static jclass FindClass(JNIEnv* env, const char* name) {
628 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700629 Runtime* runtime = Runtime::Current();
630 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700631 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700632 Class* c = NULL;
633 if (runtime->IsStarted()) {
634 // TODO: need to get the appropriate ClassLoader.
635 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
636 c = class_linker->FindClass(descriptor, cl);
637 } else {
638 c = class_linker->FindSystemClass(descriptor);
639 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700640 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700641 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700642
Elliott Hughescdf53122011-08-19 15:46:09 -0700643 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
644 ScopedJniThreadState ts(env);
645 Method* method = Decode<Method*>(ts, java_method);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700646 return reinterpret_cast<jmethodID>(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700647 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700648
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
650 ScopedJniThreadState ts(env);
651 Field* field = Decode<Field*>(ts, java_field);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700652 return reinterpret_cast<jfieldID>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700654
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
656 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700657 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700658 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700659 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700660
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
662 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700663 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700664 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700665 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700666
Elliott Hughes37f7a402011-08-22 18:56:01 -0700667 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
668 ScopedJniThreadState ts(env);
669 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700670 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700671 }
672
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700673 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700675 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700676 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700677 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700678
Elliott Hughes37f7a402011-08-22 18:56:01 -0700679 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700681 Class* c1 = Decode<Class*>(ts, java_class1);
682 Class* c2 = Decode<Class*>(ts, java_class2);
683 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700684 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700685
Elliott Hughes37f7a402011-08-22 18:56:01 -0700686 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700687 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700688 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700689 if (jobj == NULL) {
690 // NB. JNI is different from regular Java instanceof in this respect
691 return JNI_TRUE;
692 } else {
693 Object* obj = Decode<Object*>(ts, jobj);
694 Class* klass = Decode<Class*>(ts, clazz);
695 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
696 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700697 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700698
Elliott Hughes37f7a402011-08-22 18:56:01 -0700699 static jint Throw(JNIEnv* env, jthrowable java_exception) {
700 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700701 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700702 if (exception == NULL) {
703 return JNI_ERR;
704 }
705 ts.Self()->SetException(exception);
706 return JNI_OK;
707 }
708
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700709 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700710 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700711 // TODO: check for a pending exception to decide what constructor to call.
712 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
713 if (mid == NULL) {
714 return JNI_ERR;
715 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700716 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
717 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700718 return JNI_ERR;
719 }
720
721 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700722 args[0].l = s.get();
723 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
724 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700725 return JNI_ERR;
726 }
727
Elliott Hughes72025e52011-08-23 17:50:30 -0700728 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700729 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700730 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700731
Elliott Hughes37f7a402011-08-22 18:56:01 -0700732 return JNI_OK;
733 }
734
735 static jboolean ExceptionCheck(JNIEnv* env) {
736 ScopedJniThreadState ts(env);
737 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
738 }
739
740 static void ExceptionClear(JNIEnv* env) {
741 ScopedJniThreadState ts(env);
742 ts.Self()->ClearException();
743 }
744
745 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700746 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700747
748 Thread* self = ts.Self();
749 Throwable* original_exception = self->GetException();
750 self->ClearException();
751
Elliott Hughesbf86d042011-08-31 17:53:14 -0700752 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700753 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
754 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
755 if (mid == NULL) {
756 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
757 << PrettyType(original_exception);
758 } else {
759 env->CallVoidMethod(exception.get(), mid);
760 if (self->IsExceptionPending()) {
761 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
762 << " thrown while calling printStackTrace";
763 self->ClearException();
764 }
765 }
766
767 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700768 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700769
Elliott Hughescdf53122011-08-19 15:46:09 -0700770 static jthrowable ExceptionOccurred(JNIEnv* env) {
771 ScopedJniThreadState ts(env);
772 Object* exception = ts.Self()->GetException();
773 if (exception == NULL) {
774 return NULL;
775 } else {
776 // TODO: if adding a local reference failing causes the VM to abort
777 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700778 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 if (localException == NULL) {
780 // We were unable to add a new local reference, and threw a new
781 // exception. We can't return "exception", because it's not a
782 // local reference. So we have to return NULL, indicating that
783 // there was no exception, even though it's pretty much raining
784 // exceptions in here.
785 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
786 }
787 return localException;
788 }
789 }
790
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 static void FatalError(JNIEnv* env, const char* msg) {
792 ScopedJniThreadState ts(env);
793 LOG(FATAL) << "JNI FatalError called: " << msg;
794 }
795
796 static jint PushLocalFrame(JNIEnv* env, jint cap) {
797 ScopedJniThreadState ts(env);
798 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
799 return JNI_OK;
800 }
801
802 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
803 ScopedJniThreadState ts(env);
804 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
805 return res;
806 }
807
Elliott Hughes72025e52011-08-23 17:50:30 -0700808 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
809 ScopedJniThreadState ts(env);
810 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
811 return 0;
812 }
813
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
815 ScopedJniThreadState ts(env);
816 if (obj == NULL) {
817 return NULL;
818 }
819
Elliott Hughes75770752011-08-24 17:52:38 -0700820 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 IndirectReferenceTable& globals = vm->globals;
822 MutexLock mu(vm->globals_lock);
823 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
824 return reinterpret_cast<jobject>(ref);
825 }
826
827 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
828 ScopedJniThreadState ts(env);
829 if (obj == NULL) {
830 return;
831 }
832
Elliott Hughes75770752011-08-24 17:52:38 -0700833 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 IndirectReferenceTable& globals = vm->globals;
835 MutexLock mu(vm->globals_lock);
836
837 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
838 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
839 << "failed to find entry";
840 }
841 }
842
843 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
844 ScopedJniThreadState ts(env);
845 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
846 }
847
848 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
849 ScopedJniThreadState ts(env);
850 if (obj == NULL) {
851 return;
852 }
853
Elliott Hughes75770752011-08-24 17:52:38 -0700854 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 IndirectReferenceTable& weak_globals = vm->weak_globals;
856 MutexLock mu(vm->weak_globals_lock);
857
858 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
859 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
860 << "failed to find entry";
861 }
862 }
863
864 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
865 ScopedJniThreadState ts(env);
866 if (obj == NULL) {
867 return NULL;
868 }
869
870 IndirectReferenceTable& locals = ts.Env()->locals;
871
872 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
873 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
874 return reinterpret_cast<jobject>(ref);
875 }
876
877 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
878 ScopedJniThreadState ts(env);
879 if (obj == NULL) {
880 return;
881 }
882
883 IndirectReferenceTable& locals = ts.Env()->locals;
884
885 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
886 if (!locals.Remove(cookie, obj)) {
887 // Attempting to delete a local reference that is not in the
888 // topmost local reference frame is a no-op. DeleteLocalRef returns
889 // void and doesn't throw any exceptions, but we should probably
890 // complain about it so the user will notice that things aren't
891 // going quite the way they expect.
892 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
893 << "failed to find entry";
894 }
895 }
896
897 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
898 ScopedJniThreadState ts(env);
899 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
900 ? JNI_TRUE : JNI_FALSE;
901 }
902
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700903 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700905 Class* c = Decode<Class*>(ts, java_class);
906 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
907 return NULL;
908 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700909 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 }
911
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 ScopedJniThreadState ts(env);
914 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 va_start(args, mid);
916 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 va_end(args);
918 return result;
919 }
920
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700923 Class* c = Decode<Class*>(ts, java_class);
924 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
925 return NULL;
926 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700927 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700928 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700930 return local_result;
931 }
932
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700935 Class* c = Decode<Class*>(ts, java_class);
936 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
937 return NULL;
938 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700939 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700940 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 return local_result;
943 }
944
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
946 ScopedJniThreadState ts(env);
947 return FindMethodID(ts, c, name, sig, false);
948 }
949
950 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
951 ScopedJniThreadState ts(env);
952 return FindMethodID(ts, c, name, sig, true);
953 }
954
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 va_list ap;
958 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700959 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700961 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700966 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700967 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700972 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700973 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 va_list ap;
979 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700980 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 va_end(ap);
982 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 }
984
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700987 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700992 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 }
994
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 va_list ap;
998 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700999 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 va_end(ap);
1001 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001006 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001011 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 va_list ap;
1017 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001018 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_end(ap);
1020 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001025 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001030 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 va_list ap;
1036 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001037 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 va_end(ap);
1039 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001044 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001049 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 va_list ap;
1055 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001056 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 va_end(ap);
1058 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001063 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001068 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 va_list ap;
1074 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001075 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 va_end(ap);
1077 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001082 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001087 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 va_list ap;
1093 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001094 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 va_end(ap);
1096 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001101 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001106 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 va_list ap;
1112 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001113 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 va_end(ap);
1115 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001120 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001125 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 va_list ap;
1131 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001132 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001138 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001143 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
1146 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 ScopedJniThreadState ts(env);
1149 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 va_start(ap, mid);
1151 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001152 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 va_end(ap);
1154 return local_result;
1155 }
1156
1157 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001161 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
1164 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001168 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
1171 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 ScopedJniThreadState ts(env);
1174 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 va_start(ap, mid);
1176 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 va_end(ap);
1178 return result.z;
1179 }
1180
1181 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
1187 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
1193 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 ScopedJniThreadState ts(env);
1196 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 va_start(ap, mid);
1198 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 va_end(ap);
1200 return result.b;
1201 }
1202
1203 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 }
1208
1209 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
1215 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 ScopedJniThreadState ts(env);
1218 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 va_start(ap, mid);
1220 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 va_end(ap);
1222 return result.c;
1223 }
1224
1225 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
1231 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001232 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 }
1236
1237 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 ScopedJniThreadState ts(env);
1240 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 va_start(ap, mid);
1242 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 va_end(ap);
1244 return result.s;
1245 }
1246
1247 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001248 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 }
1252
1253 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001256 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
1259 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
1262 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 va_start(ap, mid);
1264 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 va_end(ap);
1266 return result.i;
1267 }
1268
1269 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001270 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 }
1274
1275 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001276 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 }
1280
1281 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 ScopedJniThreadState ts(env);
1284 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 va_start(ap, mid);
1286 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 va_end(ap);
1288 return result.j;
1289 }
1290
1291 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001292 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
1297 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001298 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001300 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
1303 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001304 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 ScopedJniThreadState ts(env);
1306 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 va_start(ap, mid);
1308 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 va_end(ap);
1310 return result.f;
1311 }
1312
1313 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001314 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 }
1318
1319 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001320 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001322 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
1325 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 ScopedJniThreadState ts(env);
1328 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001329 va_start(ap, mid);
1330 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 va_end(ap);
1332 return result.d;
1333 }
1334
1335 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001336 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
1341 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001342 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 }
1346
1347 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001348 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 ScopedJniThreadState ts(env);
1350 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001351 va_start(ap, mid);
1352 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 va_end(ap);
1354 }
1355
1356 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001357 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 }
1361
1362 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001363 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001365 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
1367
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001368 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 ScopedJniThreadState ts(env);
1370 return FindFieldID(ts, c, name, sig, false);
1371 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001372
1373
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001374 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 ScopedJniThreadState ts(env);
1376 return FindFieldID(ts, c, name, sig, true);
1377 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001378
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001379 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001382 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001383 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001385
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001388 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001389 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 Object* o = Decode<Object*>(ts, java_object);
1395 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001396 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001397 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001403 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407#define GET_PRIMITIVE_FIELD(fn, instance) \
1408 ScopedJniThreadState ts(env); \
1409 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001410 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001411 return f->fn(o)
1412
1413#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1414 ScopedJniThreadState ts(env); \
1415 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001416 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001417 f->fn(o, value)
1418
1419 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1420 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001423 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1424 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 }
1426
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001427 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1428 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001431 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1432 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1436 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1440 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1444 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1448 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetLong, NULL);
1473 }
1474
1475 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1476 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1477 }
1478
1479 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1480 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1481 }
1482
1483 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1484 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1485 }
1486
1487 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1488 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1489 }
1490
1491 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1492 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1493 }
1494
1495 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1496 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1497 }
1498
1499 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1500 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1501 }
1502
1503 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1504 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1505 }
1506
1507 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1508 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1509 }
1510
1511 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1512 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1513 }
1514
1515 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1516 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1517 }
1518
1519 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1520 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1521 }
1522
1523 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1524 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1525 }
1526
1527 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1528 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1529 }
1530
1531 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1532 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1533 }
1534
1535 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1536 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1537 }
1538
1539 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1540 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1541 }
1542
1543 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1544 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
1547 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001548 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 ScopedJniThreadState ts(env);
1550 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001551 va_start(ap, mid);
1552 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001553 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 va_end(ap);
1555 return local_result;
1556 }
1557
1558 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001559 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001561 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001562 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
1565 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001566 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001568 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001569 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
1572 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001573 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 ScopedJniThreadState ts(env);
1575 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001576 va_start(ap, mid);
1577 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 va_end(ap);
1579 return result.z;
1580 }
1581
1582 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001583 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001584 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001585 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
1588 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001589 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001591 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes72025e52011-08-23 17:50:30 -07001594 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 ScopedJniThreadState ts(env);
1596 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001597 va_start(ap, mid);
1598 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 va_end(ap);
1600 return result.b;
1601 }
1602
1603 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001604 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001606 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
1609 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001610 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001612 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 ScopedJniThreadState ts(env);
1617 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001618 va_start(ap, mid);
1619 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 va_end(ap);
1621 return result.c;
1622 }
1623
1624 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001625 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001627 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
1630 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001631 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001633 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes72025e52011-08-23 17:50:30 -07001636 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 ScopedJniThreadState ts(env);
1638 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001639 va_start(ap, mid);
1640 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 va_end(ap);
1642 return result.s;
1643 }
1644
1645 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
1651 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001652 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001654 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 }
1656
Elliott Hughes72025e52011-08-23 17:50:30 -07001657 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
1659 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001660 va_start(ap, mid);
1661 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 va_end(ap);
1663 return result.i;
1664 }
1665
1666 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001669 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 }
1671
1672 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001673 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001675 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 }
1677
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
1680 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001681 va_start(ap, mid);
1682 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 va_end(ap);
1684 return result.j;
1685 }
1686
1687 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001688 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001690 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
1693 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001694 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001696 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 }
1698
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 ScopedJniThreadState ts(env);
1701 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001702 va_start(ap, mid);
1703 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 va_end(ap);
1705 return result.f;
1706 }
1707
1708 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001709 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001711 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 }
1713
1714 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001715 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001717 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 ScopedJniThreadState ts(env);
1722 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001723 va_start(ap, mid);
1724 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 va_end(ap);
1726 return result.d;
1727 }
1728
1729 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001730 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001732 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 }
1734
1735 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001736 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001738 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 }
1740
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 ScopedJniThreadState ts(env);
1743 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001744 va_start(ap, mid);
1745 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 va_end(ap);
1747 }
1748
1749 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001750 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001752 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
1755 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001756 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001758 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes814e4032011-08-23 12:07:56 -07001761 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001763 if (chars == NULL && char_count == 0) {
1764 return NULL;
1765 }
1766 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001767 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 }
1769
1770 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1771 ScopedJniThreadState ts(env);
1772 if (utf == NULL) {
1773 return NULL;
1774 }
1775 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001776 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 }
1778
Elliott Hughes814e4032011-08-23 12:07:56 -07001779 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1780 ScopedJniThreadState ts(env);
1781 return Decode<String*>(ts, java_string)->GetLength();
1782 }
1783
1784 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1785 ScopedJniThreadState ts(env);
1786 return Decode<String*>(ts, java_string)->GetUtfLength();
1787 }
1788
Elliott Hughesb465ab02011-08-24 11:21:21 -07001789 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001791 String* s = Decode<String*>(ts, java_string);
1792 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1793 ThrowSIOOBE(ts, start, length, s->GetLength());
1794 } else {
1795 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1796 memcpy(buf, chars + start, length * sizeof(jchar));
1797 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001798 }
1799
Elliott Hughesb465ab02011-08-24 11:21:21 -07001800 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001801 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001802 String* s = Decode<String*>(ts, java_string);
1803 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1804 ThrowSIOOBE(ts, start, length, s->GetLength());
1805 } else {
1806 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1807 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1808 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001809 }
1810
Elliott Hughes75770752011-08-24 17:52:38 -07001811 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001812 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001813 String* s = Decode<String*>(ts, java_string);
1814 const CharArray* chars = s->GetCharArray();
1815 PinPrimitiveArray(ts, chars);
1816 if (is_copy != NULL) {
1817 *is_copy = JNI_FALSE;
1818 }
1819 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001820 }
1821
Elliott Hughes75770752011-08-24 17:52:38 -07001822 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001823 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001824 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes75770752011-08-24 17:52:38 -07001827 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001829 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 }
1831
Elliott Hughes75770752011-08-24 17:52:38 -07001832 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001834 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
Elliott Hughes75770752011-08-24 17:52:38 -07001837 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001838 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001839 if (java_string == NULL) {
1840 return NULL;
1841 }
1842 if (is_copy != NULL) {
1843 *is_copy = JNI_TRUE;
1844 }
1845 String* s = Decode<String*>(ts, java_string);
1846 size_t byte_count = s->GetUtfLength();
1847 char* bytes = new char[byte_count + 1];
1848 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001849 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001850 return NULL;
1851 }
1852 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1853 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1854 bytes[byte_count] = '\0';
1855 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001856 }
1857
Elliott Hughes75770752011-08-24 17:52:38 -07001858 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001859 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001860 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001861 }
1862
Elliott Hughesbd935992011-08-22 11:59:34 -07001863 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001865 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001866 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001867 Array* array = obj->AsArray();
1868 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
Elliott Hughes814e4032011-08-23 12:07:56 -07001871 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001873 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001874 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 }
1876
1877 static void SetObjectArrayElement(JNIEnv* env,
1878 jobjectArray java_array, jsize index, jobject java_value) {
1879 ScopedJniThreadState ts(env);
1880 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1881 Object* value = Decode<Object*>(ts, java_value);
1882 array->Set(index, value);
1883 }
1884
1885 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1886 ScopedJniThreadState ts(env);
1887 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1888 }
1889
1890 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1891 ScopedJniThreadState ts(env);
1892 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1893 }
1894
1895 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1896 ScopedJniThreadState ts(env);
1897 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1898 }
1899
1900 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1901 ScopedJniThreadState ts(env);
1902 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1903 }
1904
1905 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1906 ScopedJniThreadState ts(env);
1907 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1908 }
1909
1910 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1911 ScopedJniThreadState ts(env);
1912 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1913 }
1914
1915 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1916 ScopedJniThreadState ts(env);
1917 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1918 }
1919
1920 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1921 ScopedJniThreadState ts(env);
1922 CHECK_GE(length, 0); // TODO: ReportJniError
1923
1924 // Compute the array class corresponding to the given element class.
1925 Class* element_class = Decode<Class*>(ts, element_jclass);
1926 std::string descriptor;
1927 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001928 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001929
1930 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001931 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1932 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 return NULL;
1934 }
1935
Elliott Hughes75770752011-08-24 17:52:38 -07001936 // Allocate and initialize if necessary.
1937 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001939 if (initial_element != NULL) {
1940 Object* initial_object = Decode<Object*>(ts, initial_element);
1941 for (jsize i = 0; i < length; ++i) {
1942 result->Set(i, initial_object);
1943 }
1944 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001945 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 }
1947
1948 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1949 ScopedJniThreadState ts(env);
1950 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1951 }
1952
Elliott Hughes75770752011-08-24 17:52:38 -07001953 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001954 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001955 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001956 }
1957
Elliott Hughes75770752011-08-24 17:52:38 -07001958 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001959 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001960 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001961 }
1962
Elliott Hughes75770752011-08-24 17:52:38 -07001963 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001965 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Elliott Hughes75770752011-08-24 17:52:38 -07001968 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001970 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Elliott Hughes75770752011-08-24 17:52:38 -07001973 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001975 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Elliott Hughes75770752011-08-24 17:52:38 -07001978 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001980 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Elliott Hughes75770752011-08-24 17:52:38 -07001983 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001985 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Elliott Hughes75770752011-08-24 17:52:38 -07001988 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001990 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001995 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes75770752011-08-24 17:52:38 -07001998 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002000 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes75770752011-08-24 17:52:38 -07002003 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002005 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002010 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002015 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes75770752011-08-24 17:52:38 -07002018 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002020 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes75770752011-08-24 17:52:38 -07002023 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002025 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes75770752011-08-24 17:52:38 -07002028 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002030 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes75770752011-08-24 17:52:38 -07002033 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002035 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes75770752011-08-24 17:52:38 -07002038 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002040 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes814e4032011-08-23 12:07:56 -07002043 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002045 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002070 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes5174fe62011-08-23 15:12:35 -07002123 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002125 Class* c = Decode<Class*>(ts, java_class);
2126
Elliott Hughes5174fe62011-08-23 15:12:35 -07002127 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 const char* name = methods[i].name;
2129 const char* sig = methods[i].signature;
2130
2131 if (*sig == '!') {
2132 // TODO: fast jni. it's too noisy to log all these.
2133 ++sig;
2134 }
2135
Elliott Hughes5174fe62011-08-23 15:12:35 -07002136 Method* m = c->FindDirectMethod(name, sig);
2137 if (m == NULL) {
2138 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002140 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002142 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2144 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002145 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002147 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002149 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2151 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002152 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 return JNI_ERR;
2154 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155
Elliott Hughes75770752011-08-24 17:52:38 -07002156 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002157 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158 }
2159
2160 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162 return JNI_OK;
2163 }
2164
Elliott Hughes5174fe62011-08-23 15:12:35 -07002165 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002167 Class* c = Decode<Class*>(ts, java_class);
2168
Elliott Hughes75770752011-08-24 17:52:38 -07002169 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002170 LOG(INFO) << "[Unregistering JNI native methods for "
2171 << PrettyDescriptor(c->GetDescriptor()) << "]";
2172 }
2173
2174 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2175 Method* m = c->GetDirectMethod(i);
2176 if (m->IsNative()) {
2177 m->UnregisterNative();
2178 }
2179 }
2180 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2181 Method* m = c->GetVirtualMethod(i);
2182 if (m->IsNative()) {
2183 m->UnregisterNative();
2184 }
2185 }
2186
2187 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 }
2189
Elliott Hughes72025e52011-08-23 17:50:30 -07002190 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002192 Decode<Object*>(ts, java_object)->MonitorEnter();
2193 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
2195
Elliott Hughes72025e52011-08-23 17:50:30 -07002196 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 ScopedJniThreadState ts(env);
Elliott Hughes02b48d12011-09-07 17:15:51 -07002198 Decode<Object*>(ts, java_object)->MonitorExit();
Elliott Hughes72025e52011-08-23 17:50:30 -07002199 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
2202 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2203 ScopedJniThreadState ts(env);
2204 Runtime* runtime = Runtime::Current();
2205 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002206 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 } else {
2208 *vm = NULL;
2209 }
2210 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2211 }
2212
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2214 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002215
2216 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002217 CHECK(address != NULL); // TODO: ReportJniError
2218 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002219
2220 jclass buffer_class = GetDirectByteBufferClass(env);
2221 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2222 if (mid == NULL) {
2223 return NULL;
2224 }
2225
2226 // At the moment, the Java side is limited to 32 bits.
2227 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2228 CHECK_LE(capacity, 0xffffffff);
2229 jint address_arg = reinterpret_cast<jint>(address);
2230 jint capacity_arg = static_cast<jint>(capacity);
2231
2232 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2233 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Elliott Hughesb465ab02011-08-24 11:21:21 -07002236 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002238 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2239 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002240 }
2241
Elliott Hughesb465ab02011-08-24 11:21:21 -07002242 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002244 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2245 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 }
2247
Elliott Hughesb465ab02011-08-24 11:21:21 -07002248 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002250
Elliott Hughes75770752011-08-24 17:52:38 -07002251 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002252
2253 // Do we definitely know what kind of reference this is?
2254 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2255 IndirectRefKind kind = GetIndirectRefKind(ref);
2256 switch (kind) {
2257 case kLocal:
2258 return JNILocalRefType;
2259 case kGlobal:
2260 return JNIGlobalRefType;
2261 case kWeakGlobal:
2262 return JNIWeakGlobalRefType;
2263 case kSirtOrInvalid:
2264 // Is it in a stack IRT?
2265 if (ts.Self()->SirtContains(java_object)) {
2266 return JNILocalRefType;
2267 }
2268
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002269 if (!ts.Env()->work_around_app_jni_bugs) {
2270 return JNIInvalidRefType;
2271 }
2272
Elliott Hughesb465ab02011-08-24 11:21:21 -07002273 // If we're handing out direct pointers, check whether it's a direct pointer
2274 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002275 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002276 if (ts.Env()->locals.Contains(java_object)) {
2277 return JNILocalRefType;
2278 }
2279 }
2280
2281 return JNIInvalidRefType;
2282 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002283 }
2284};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002285
Elliott Hughesa2501992011-08-26 19:39:54 -07002286const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002287 NULL, // reserved0.
2288 NULL, // reserved1.
2289 NULL, // reserved2.
2290 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 JNI::GetVersion,
2292 JNI::DefineClass,
2293 JNI::FindClass,
2294 JNI::FromReflectedMethod,
2295 JNI::FromReflectedField,
2296 JNI::ToReflectedMethod,
2297 JNI::GetSuperclass,
2298 JNI::IsAssignableFrom,
2299 JNI::ToReflectedField,
2300 JNI::Throw,
2301 JNI::ThrowNew,
2302 JNI::ExceptionOccurred,
2303 JNI::ExceptionDescribe,
2304 JNI::ExceptionClear,
2305 JNI::FatalError,
2306 JNI::PushLocalFrame,
2307 JNI::PopLocalFrame,
2308 JNI::NewGlobalRef,
2309 JNI::DeleteGlobalRef,
2310 JNI::DeleteLocalRef,
2311 JNI::IsSameObject,
2312 JNI::NewLocalRef,
2313 JNI::EnsureLocalCapacity,
2314 JNI::AllocObject,
2315 JNI::NewObject,
2316 JNI::NewObjectV,
2317 JNI::NewObjectA,
2318 JNI::GetObjectClass,
2319 JNI::IsInstanceOf,
2320 JNI::GetMethodID,
2321 JNI::CallObjectMethod,
2322 JNI::CallObjectMethodV,
2323 JNI::CallObjectMethodA,
2324 JNI::CallBooleanMethod,
2325 JNI::CallBooleanMethodV,
2326 JNI::CallBooleanMethodA,
2327 JNI::CallByteMethod,
2328 JNI::CallByteMethodV,
2329 JNI::CallByteMethodA,
2330 JNI::CallCharMethod,
2331 JNI::CallCharMethodV,
2332 JNI::CallCharMethodA,
2333 JNI::CallShortMethod,
2334 JNI::CallShortMethodV,
2335 JNI::CallShortMethodA,
2336 JNI::CallIntMethod,
2337 JNI::CallIntMethodV,
2338 JNI::CallIntMethodA,
2339 JNI::CallLongMethod,
2340 JNI::CallLongMethodV,
2341 JNI::CallLongMethodA,
2342 JNI::CallFloatMethod,
2343 JNI::CallFloatMethodV,
2344 JNI::CallFloatMethodA,
2345 JNI::CallDoubleMethod,
2346 JNI::CallDoubleMethodV,
2347 JNI::CallDoubleMethodA,
2348 JNI::CallVoidMethod,
2349 JNI::CallVoidMethodV,
2350 JNI::CallVoidMethodA,
2351 JNI::CallNonvirtualObjectMethod,
2352 JNI::CallNonvirtualObjectMethodV,
2353 JNI::CallNonvirtualObjectMethodA,
2354 JNI::CallNonvirtualBooleanMethod,
2355 JNI::CallNonvirtualBooleanMethodV,
2356 JNI::CallNonvirtualBooleanMethodA,
2357 JNI::CallNonvirtualByteMethod,
2358 JNI::CallNonvirtualByteMethodV,
2359 JNI::CallNonvirtualByteMethodA,
2360 JNI::CallNonvirtualCharMethod,
2361 JNI::CallNonvirtualCharMethodV,
2362 JNI::CallNonvirtualCharMethodA,
2363 JNI::CallNonvirtualShortMethod,
2364 JNI::CallNonvirtualShortMethodV,
2365 JNI::CallNonvirtualShortMethodA,
2366 JNI::CallNonvirtualIntMethod,
2367 JNI::CallNonvirtualIntMethodV,
2368 JNI::CallNonvirtualIntMethodA,
2369 JNI::CallNonvirtualLongMethod,
2370 JNI::CallNonvirtualLongMethodV,
2371 JNI::CallNonvirtualLongMethodA,
2372 JNI::CallNonvirtualFloatMethod,
2373 JNI::CallNonvirtualFloatMethodV,
2374 JNI::CallNonvirtualFloatMethodA,
2375 JNI::CallNonvirtualDoubleMethod,
2376 JNI::CallNonvirtualDoubleMethodV,
2377 JNI::CallNonvirtualDoubleMethodA,
2378 JNI::CallNonvirtualVoidMethod,
2379 JNI::CallNonvirtualVoidMethodV,
2380 JNI::CallNonvirtualVoidMethodA,
2381 JNI::GetFieldID,
2382 JNI::GetObjectField,
2383 JNI::GetBooleanField,
2384 JNI::GetByteField,
2385 JNI::GetCharField,
2386 JNI::GetShortField,
2387 JNI::GetIntField,
2388 JNI::GetLongField,
2389 JNI::GetFloatField,
2390 JNI::GetDoubleField,
2391 JNI::SetObjectField,
2392 JNI::SetBooleanField,
2393 JNI::SetByteField,
2394 JNI::SetCharField,
2395 JNI::SetShortField,
2396 JNI::SetIntField,
2397 JNI::SetLongField,
2398 JNI::SetFloatField,
2399 JNI::SetDoubleField,
2400 JNI::GetStaticMethodID,
2401 JNI::CallStaticObjectMethod,
2402 JNI::CallStaticObjectMethodV,
2403 JNI::CallStaticObjectMethodA,
2404 JNI::CallStaticBooleanMethod,
2405 JNI::CallStaticBooleanMethodV,
2406 JNI::CallStaticBooleanMethodA,
2407 JNI::CallStaticByteMethod,
2408 JNI::CallStaticByteMethodV,
2409 JNI::CallStaticByteMethodA,
2410 JNI::CallStaticCharMethod,
2411 JNI::CallStaticCharMethodV,
2412 JNI::CallStaticCharMethodA,
2413 JNI::CallStaticShortMethod,
2414 JNI::CallStaticShortMethodV,
2415 JNI::CallStaticShortMethodA,
2416 JNI::CallStaticIntMethod,
2417 JNI::CallStaticIntMethodV,
2418 JNI::CallStaticIntMethodA,
2419 JNI::CallStaticLongMethod,
2420 JNI::CallStaticLongMethodV,
2421 JNI::CallStaticLongMethodA,
2422 JNI::CallStaticFloatMethod,
2423 JNI::CallStaticFloatMethodV,
2424 JNI::CallStaticFloatMethodA,
2425 JNI::CallStaticDoubleMethod,
2426 JNI::CallStaticDoubleMethodV,
2427 JNI::CallStaticDoubleMethodA,
2428 JNI::CallStaticVoidMethod,
2429 JNI::CallStaticVoidMethodV,
2430 JNI::CallStaticVoidMethodA,
2431 JNI::GetStaticFieldID,
2432 JNI::GetStaticObjectField,
2433 JNI::GetStaticBooleanField,
2434 JNI::GetStaticByteField,
2435 JNI::GetStaticCharField,
2436 JNI::GetStaticShortField,
2437 JNI::GetStaticIntField,
2438 JNI::GetStaticLongField,
2439 JNI::GetStaticFloatField,
2440 JNI::GetStaticDoubleField,
2441 JNI::SetStaticObjectField,
2442 JNI::SetStaticBooleanField,
2443 JNI::SetStaticByteField,
2444 JNI::SetStaticCharField,
2445 JNI::SetStaticShortField,
2446 JNI::SetStaticIntField,
2447 JNI::SetStaticLongField,
2448 JNI::SetStaticFloatField,
2449 JNI::SetStaticDoubleField,
2450 JNI::NewString,
2451 JNI::GetStringLength,
2452 JNI::GetStringChars,
2453 JNI::ReleaseStringChars,
2454 JNI::NewStringUTF,
2455 JNI::GetStringUTFLength,
2456 JNI::GetStringUTFChars,
2457 JNI::ReleaseStringUTFChars,
2458 JNI::GetArrayLength,
2459 JNI::NewObjectArray,
2460 JNI::GetObjectArrayElement,
2461 JNI::SetObjectArrayElement,
2462 JNI::NewBooleanArray,
2463 JNI::NewByteArray,
2464 JNI::NewCharArray,
2465 JNI::NewShortArray,
2466 JNI::NewIntArray,
2467 JNI::NewLongArray,
2468 JNI::NewFloatArray,
2469 JNI::NewDoubleArray,
2470 JNI::GetBooleanArrayElements,
2471 JNI::GetByteArrayElements,
2472 JNI::GetCharArrayElements,
2473 JNI::GetShortArrayElements,
2474 JNI::GetIntArrayElements,
2475 JNI::GetLongArrayElements,
2476 JNI::GetFloatArrayElements,
2477 JNI::GetDoubleArrayElements,
2478 JNI::ReleaseBooleanArrayElements,
2479 JNI::ReleaseByteArrayElements,
2480 JNI::ReleaseCharArrayElements,
2481 JNI::ReleaseShortArrayElements,
2482 JNI::ReleaseIntArrayElements,
2483 JNI::ReleaseLongArrayElements,
2484 JNI::ReleaseFloatArrayElements,
2485 JNI::ReleaseDoubleArrayElements,
2486 JNI::GetBooleanArrayRegion,
2487 JNI::GetByteArrayRegion,
2488 JNI::GetCharArrayRegion,
2489 JNI::GetShortArrayRegion,
2490 JNI::GetIntArrayRegion,
2491 JNI::GetLongArrayRegion,
2492 JNI::GetFloatArrayRegion,
2493 JNI::GetDoubleArrayRegion,
2494 JNI::SetBooleanArrayRegion,
2495 JNI::SetByteArrayRegion,
2496 JNI::SetCharArrayRegion,
2497 JNI::SetShortArrayRegion,
2498 JNI::SetIntArrayRegion,
2499 JNI::SetLongArrayRegion,
2500 JNI::SetFloatArrayRegion,
2501 JNI::SetDoubleArrayRegion,
2502 JNI::RegisterNatives,
2503 JNI::UnregisterNatives,
2504 JNI::MonitorEnter,
2505 JNI::MonitorExit,
2506 JNI::GetJavaVM,
2507 JNI::GetStringRegion,
2508 JNI::GetStringUTFRegion,
2509 JNI::GetPrimitiveArrayCritical,
2510 JNI::ReleasePrimitiveArrayCritical,
2511 JNI::GetStringCritical,
2512 JNI::ReleaseStringCritical,
2513 JNI::NewWeakGlobalRef,
2514 JNI::DeleteWeakGlobalRef,
2515 JNI::ExceptionCheck,
2516 JNI::NewDirectByteBuffer,
2517 JNI::GetDirectBufferAddress,
2518 JNI::GetDirectBufferCapacity,
2519 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002520};
2521
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002522static const size_t kMonitorsInitial = 32; // Arbitrary.
2523static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2524
2525static const size_t kLocalsInitial = 64; // Arbitrary.
2526static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002527
Elliott Hughes75770752011-08-24 17:52:38 -07002528JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002529 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002530 vm(vm),
2531 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002532 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002533 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002534 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2535 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002536 functions = unchecked_functions = &gNativeInterface;
2537 if (check_jni) {
2538 functions = GetCheckJniNativeInterface();
2539 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002540}
2541
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002542JNIEnvExt::~JNIEnvExt() {
2543}
2544
Carl Shapiroea4dca82011-08-01 13:45:38 -07002545// JNI Invocation interface.
2546
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002547extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2548 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2549 if (args->version < JNI_VERSION_1_2) {
2550 return JNI_EVERSION;
2551 }
2552 Runtime::Options options;
2553 for (int i = 0; i < args->nOptions; ++i) {
2554 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002555 options.push_back(std::make_pair(StringPiece(option->optionString),
2556 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002557 }
2558 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002559 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002560 if (runtime == NULL) {
2561 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002562 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002563 runtime->Start();
2564 *p_env = Thread::Current()->GetJniEnv();
2565 *p_vm = runtime->GetJavaVM();
2566 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002567}
2568
Elliott Hughesf2682d52011-08-15 16:37:04 -07002569extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002570 Runtime* runtime = Runtime::Current();
2571 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002572 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002573 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002574 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002575 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002576 }
2577 return JNI_OK;
2578}
2579
2580// Historically unsupported.
2581extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2582 return JNI_ERR;
2583}
2584
Elliott Hughescdf53122011-08-19 15:46:09 -07002585class JII {
2586 public:
2587 static jint DestroyJavaVM(JavaVM* vm) {
2588 if (vm == NULL) {
2589 return JNI_ERR;
2590 } else {
2591 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2592 delete raw_vm->runtime;
2593 return JNI_OK;
2594 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002595 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002596
Elliott Hughescdf53122011-08-19 15:46:09 -07002597 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002598 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002599 }
2600
2601 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002602 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002603 }
2604
2605 static jint DetachCurrentThread(JavaVM* vm) {
2606 if (vm == NULL) {
2607 return JNI_ERR;
2608 } else {
2609 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2610 Runtime* runtime = raw_vm->runtime;
2611 runtime->DetachCurrentThread();
2612 return JNI_OK;
2613 }
2614 }
2615
2616 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2617 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2618 return JNI_EVERSION;
2619 }
2620 if (vm == NULL || env == NULL) {
2621 return JNI_ERR;
2622 }
2623 Thread* thread = Thread::Current();
2624 if (thread == NULL) {
2625 *env = NULL;
2626 return JNI_EDETACHED;
2627 }
2628 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002629 return JNI_OK;
2630 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002631};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002632
Elliott Hughesa2501992011-08-26 19:39:54 -07002633const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002634 NULL, // reserved0
2635 NULL, // reserved1
2636 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002637 JII::DestroyJavaVM,
2638 JII::AttachCurrentThread,
2639 JII::DetachCurrentThread,
2640 JII::GetEnv,
2641 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002642};
2643
Elliott Hughesbbd76712011-08-17 10:25:24 -07002644static const size_t kPinTableInitialSize = 16;
2645static const size_t kPinTableMaxSize = 1024;
2646
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002647static const size_t kGlobalsInitial = 512; // Arbitrary.
2648static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2649
2650static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2651static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2652
Elliott Hughesa0957642011-09-02 14:27:33 -07002653JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002654 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002655 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002656 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002657 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002658 verbose_jni(options->IsVerbose("jni")),
2659 log_third_party_jni(options->IsVerbose("third-party-jni")),
2660 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002661 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002662 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002663 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002664 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002665 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002666 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002667 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002668 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002669 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002670 functions = unchecked_functions = &gInvokeInterface;
2671 if (check_jni) {
2672 functions = GetCheckJniInvokeInterface();
2673 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002674}
2675
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002676JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002677 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002678}
2679
Elliott Hughes75770752011-08-24 17:52:38 -07002680bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2681 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002682
2683 // See if we've already loaded this library. If we have, and the class loader
2684 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002685 // TODO: for better results we should canonicalize the pathname (or even compare
2686 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002687 SharedLibrary* library;
2688 {
2689 // TODO: move the locking (and more of this logic) into Libraries.
2690 MutexLock mu(libraries_lock);
2691 library = libraries->Get(path);
2692 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002693 if (library != NULL) {
2694 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002695 // The library will be associated with class_loader. The JNI
2696 // spec says we can't load the same library into more than one
2697 // class loader.
2698 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2699 "ClassLoader %p; can't open in ClassLoader %p",
2700 path.c_str(), library->GetClassLoader(), class_loader);
2701 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002702 return false;
2703 }
2704 if (verbose_jni) {
2705 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2706 << "ClassLoader " << class_loader << "]";
2707 }
2708 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002709 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2710 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002711 return false;
2712 }
2713 return true;
2714 }
2715
2716 // Open the shared library. Because we're using a full path, the system
2717 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2718 // resolve this library's dependencies though.)
2719
2720 // Failures here are expected when java.library.path has several entries
2721 // and we have to hunt for the lib.
2722
2723 // The current version of the dynamic linker prints detailed information
2724 // about dlopen() failures. Some things to check if the message is
2725 // cryptic:
2726 // - make sure the library exists on the device
2727 // - verify that the right path is being opened (the debug log message
2728 // above can help with that)
2729 // - check to see if the library is valid (e.g. not zero bytes long)
2730 // - check config/prelink-linux-arm.map to ensure that the library
2731 // is listed and is not being overrun by the previous entry (if
2732 // loading suddenly stops working on a prelinked library, this is
2733 // a good one to check)
2734 // - write a trivial app that calls sleep() then dlopen(), attach
2735 // to it with "strace -p <pid>" while it sleeps, and watch for
2736 // attempts to open nonexistent dependent shared libs
2737
2738 // TODO: automate some of these checks!
2739
2740 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002741 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002742 // the GC to ignore us.
2743 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002744 void* handle = NULL;
2745 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002746 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002747 handle = dlopen(path.c_str(), RTLD_LAZY);
2748 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002749
2750 if (verbose_jni) {
2751 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2752 }
2753
2754 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002755 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002756 return false;
2757 }
2758
2759 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002760 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002761 // TODO: move the locking (and more of this logic) into Libraries.
2762 MutexLock mu(libraries_lock);
2763 library = libraries->Get(path);
2764 if (library != NULL) {
2765 LOG(INFO) << "WOW: we lost a race to add shared library: "
2766 << "\"" << path << "\" ClassLoader=" << class_loader;
2767 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002768 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002769 library = new SharedLibrary(path, handle, class_loader);
2770 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002771 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002772
2773 if (verbose_jni) {
2774 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2775 }
2776
2777 bool result = true;
2778 void* sym = dlsym(handle, "JNI_OnLoad");
2779 if (sym == NULL) {
2780 if (verbose_jni) {
2781 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2782 }
2783 } else {
2784 // Call JNI_OnLoad. We have to override the current class
2785 // loader, which will always be "null" since the stuff at the
2786 // top of the stack is around Runtime.loadLibrary(). (See
2787 // the comments in the JNI FindClass function.)
2788 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2789 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002790 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002791 self->SetClassLoaderOverride(class_loader);
2792
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002793 int version = 0;
2794 {
2795 ScopedThreadStateChange tsc(self, Thread::kNative);
2796 if (verbose_jni) {
2797 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2798 }
2799 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002800 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002801
2802 self->SetClassLoaderOverride(old_class_loader);;
2803
2804 if (version != JNI_VERSION_1_2 &&
2805 version != JNI_VERSION_1_4 &&
2806 version != JNI_VERSION_1_6) {
2807 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2808 << "bad version: " << version;
2809 // It's unwise to call dlclose() here, but we can mark it
2810 // as bad and ensure that future load attempts will fail.
2811 // We don't know how far JNI_OnLoad got, so there could
2812 // be some partially-initialized stuff accessible through
2813 // newly-registered native method calls. We could try to
2814 // unregister them, but that doesn't seem worthwhile.
2815 result = false;
2816 } else {
2817 if (verbose_jni) {
2818 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2819 << " from JNI_OnLoad in \"" << path << "\"]";
2820 }
2821 }
2822 }
2823
2824 library->SetResult(result);
2825 return result;
2826}
2827
2828void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2829 CHECK(m->IsNative());
2830
2831 Class* c = m->GetDeclaringClass();
2832
2833 // If this is a static method, it could be called before the class
2834 // has been initialized.
2835 if (m->IsStatic()) {
2836 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2837 return NULL;
2838 }
2839 } else {
2840 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2841 }
2842
2843 MutexLock mu(libraries_lock);
2844 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002845}
2846
Elliott Hughes410c0c82011-09-01 17:58:25 -07002847void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2848 {
2849 MutexLock mu(globals_lock);
2850 globals.VisitRoots(visitor, arg);
2851 }
2852 {
2853 MutexLock mu(pins_lock);
2854 pin_table.VisitRoots(visitor, arg);
2855 }
2856 // The weak_globals table is visited by the GC itself (because it mutates the table).
2857}
2858
Ian Rogersdf20fe02011-07-20 20:34:16 -07002859} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002860
2861std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2862 switch (rhs) {
2863 case JNIInvalidRefType:
2864 os << "JNIInvalidRefType";
2865 return os;
2866 case JNILocalRefType:
2867 os << "JNILocalRefType";
2868 return os;
2869 case JNIGlobalRefType:
2870 os << "JNIGlobalRefType";
2871 return os;
2872 case JNIWeakGlobalRefType:
2873 os << "JNIWeakGlobalRefType";
2874 return os;
2875 }
2876}