blob: fce79fbd4f00676a790759c04ea9c0e985aae1da [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.
110template Class* Decode<Class*>(JNIEnv*, jobject);
111template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
112template Object* Decode<Object*>(JNIEnv*, jobject);
113template String* Decode<String*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700114template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700115
116namespace {
117
Elliott Hughescdf53122011-08-19 15:46:09 -0700118jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
119 if (obj == NULL) {
120 return NULL;
121 }
Elliott Hughes75770752011-08-24 17:52:38 -0700122 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700123 IndirectReferenceTable& weak_globals = vm->weak_globals;
124 MutexLock mu(vm->weak_globals_lock);
125 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
126 return reinterpret_cast<jweak>(ref);
127}
128
Elliott Hughesbf86d042011-08-31 17:53:14 -0700129// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700130template<typename T>
131T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700132 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700133}
134
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700135byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700136 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700137 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700138 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700139 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
140 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700141 case 'Z':
142 case 'B':
143 case 'C':
144 case 'S':
145 case 'I':
146 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
147 offset += 4;
148 break;
149 case 'F':
150 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
151 offset += 4;
152 break;
153 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700154 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700155 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
156 offset += sizeof(Object*);
157 break;
158 }
159 case 'D':
160 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
161 offset += 8;
162 break;
163 case 'J':
164 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
165 offset += 8;
166 break;
167 }
168 }
169 return arg_array.release();
170}
171
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700172byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700173 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700174 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700175 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700176 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
177 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700178 case 'Z':
179 case 'B':
180 case 'C':
181 case 'S':
182 case 'I':
183 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
184 offset += 4;
185 break;
186 case 'F':
187 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
188 offset += 4;
189 break;
190 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700191 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700192 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
193 offset += sizeof(Object*);
194 break;
195 }
196 case 'D':
197 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
198 offset += 8;
199 break;
200 case 'J':
201 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
202 offset += 8;
203 break;
204 }
205 }
206 return arg_array.release();
207}
208
Elliott Hughes72025e52011-08-23 17:50:30 -0700209JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
210 Method* method, byte* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700211 JValue result;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700212 method->Invoke(ts.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700213 return result;
214}
215
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700216JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700217 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700218 Method* method = DecodeMethod(mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700219 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700220 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700221}
222
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700223JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700224 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700225 Method* method = DecodeMethod(mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700226 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700227 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
228}
229
Elliott Hughes75770752011-08-24 17:52:38 -0700230Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700231 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700232}
233
Brian Carlstrom30b94452011-08-25 21:35:26 -0700234JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700235 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700236 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700237 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700238 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
239}
240
Brian Carlstrom30b94452011-08-25 21:35:26 -0700241JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700242 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700243 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700244 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700245 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700246}
247
Elliott Hughes6b436852011-08-12 10:16:44 -0700248// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
249// separated with slashes but aren't wrapped with "L;" like regular descriptors
250// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
251// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
252// supported names with dots too (such as "a.b.C").
253std::string NormalizeJniClassDescriptor(const char* name) {
254 std::string result;
255 // Add the missing "L;" if necessary.
256 if (name[0] == '[') {
257 result = name;
258 } else {
259 result += 'L';
260 result += name;
261 result += ';';
262 }
263 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700264 if (result.find('.') != std::string::npos) {
265 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
266 << "\"" << name << "\"";
267 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700268 }
269 return result;
270}
271
Elliott Hughescdf53122011-08-19 15:46:09 -0700272jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
273 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700274 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
275 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700276 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700277
278 Method* method = NULL;
279 if (is_static) {
280 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700281 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700282 method = c->FindVirtualMethod(name, sig);
283 if (method == NULL) {
284 // No virtual method matching the signature. Search declared
285 // private methods and constructors.
286 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700287 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700288 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700289
Elliott Hughescdf53122011-08-19 15:46:09 -0700290 if (method == NULL || method->IsStatic() != is_static) {
291 Thread* self = Thread::Current();
Elliott Hughesa2501992011-08-26 19:39:54 -0700292 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700293 // TODO: try searching for the opposite kind of method from is_static
294 // for better diagnostics?
295 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700296 "no %s method %s", is_static ? "static" : "non-static",
297 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700298 return NULL;
299 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700300
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700301 return reinterpret_cast<jmethodID>(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700302}
303
Elliott Hughescdf53122011-08-19 15:46:09 -0700304jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
305 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700306 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
307 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700308 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700309
310 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700311 Class* field_type;
312 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
313 if (sig[1] != '\0') {
314 // TODO: need to get the appropriate ClassLoader.
315 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
316 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700317 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700318 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700319 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 if (field_type == NULL) {
321 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700322 DCHECK(ts.Self()->IsExceptionPending());
323 ts.Self()->ClearException();
324 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
325 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
326 "no type \"%s\" found and so no field \"%s\" could be found in class "
327 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700328 return NULL;
329 }
330 if (is_static) {
331 field = c->FindStaticField(name, field_type);
332 } else {
333 field = c->FindInstanceField(name, field_type);
334 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700335 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700336 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700337 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700338 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700339 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700340 return NULL;
341 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700342 // Check invariant that all jfieldIDs have resolved types (how else would
343 // the type equality in Find...Field hold?)
344 DCHECK(field->GetType() != NULL);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700345 return reinterpret_cast<jfieldID>(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700346}
347
Elliott Hughes75770752011-08-24 17:52:38 -0700348void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
349 JavaVMExt* vm = ts.Vm();
350 MutexLock mu(vm->pins_lock);
351 vm->pin_table.Add(array);
352}
353
354void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
355 JavaVMExt* vm = ts.Vm();
356 MutexLock mu(vm->pins_lock);
357 vm->pin_table.Remove(array);
358}
359
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700360template<typename JniT, typename ArtT>
361JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
362 CHECK_GE(length, 0); // TODO: ReportJniError
363 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700364 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700365}
366
Elliott Hughes75770752011-08-24 17:52:38 -0700367template <typename ArrayT, typename CArrayT, typename ArtArrayT>
368CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
369 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
370 PinPrimitiveArray(ts, array);
371 if (is_copy != NULL) {
372 *is_copy = JNI_FALSE;
373 }
374 return array->GetData();
375}
376
377template <typename ArrayT>
378void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
379 if (mode != JNI_COMMIT) {
380 Array* array = Decode<Array*>(ts, java_array);
381 UnpinPrimitiveArray(ts, array);
382 }
383}
384
Elliott Hughes814e4032011-08-23 12:07:56 -0700385void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
386 std::string type(PrettyType(array));
387 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
388 "%s offset=%d length=%d %s.length=%d",
389 type.c_str(), start, length, identifier, array->GetLength());
390}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700391void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
392 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
393 "offset=%d length=%d string.length()=%d", start, length, array_length);
394}
Elliott Hughes814e4032011-08-23 12:07:56 -0700395
396template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700397void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700398 ArrayT* array = Decode<ArrayT*>(ts, java_array);
399 if (start < 0 || length < 0 || start + length > array->GetLength()) {
400 ThrowAIOOBE(ts, array, start, length, "src");
401 } else {
402 JavaT* data = array->GetData();
403 memcpy(buf, data + start, length * sizeof(JavaT));
404 }
405}
406
407template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700408void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700409 ArrayT* array = Decode<ArrayT*>(ts, java_array);
410 if (start < 0 || length < 0 || start + length > array->GetLength()) {
411 ThrowAIOOBE(ts, array, start, length, "dst");
412 } else {
413 JavaT* data = array->GetData();
414 memcpy(data + start, buf, length * sizeof(JavaT));
415 }
416}
417
Elliott Hughes75770752011-08-24 17:52:38 -0700418jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700419 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
420 CHECK(buffer_class.get() != NULL);
421 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
422}
423
Elliott Hughes75770752011-08-24 17:52:38 -0700424jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700425 static jclass buffer_class = InitDirectByteBufferClass(env);
426 return buffer_class;
427}
428
Elliott Hughes75770752011-08-24 17:52:38 -0700429jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
430 if (vm == NULL || p_env == NULL) {
431 return JNI_ERR;
432 }
433
434 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
435 JavaVMAttachArgs args;
436 if (thr_args == NULL) {
437 // Allow the v1.1 calling convention.
438 args.version = JNI_VERSION_1_2;
439 args.name = NULL;
440 args.group = NULL; // TODO: get "main" thread group
441 } else {
442 args.version = in_args->version;
443 args.name = in_args->name;
444 if (in_args->group != NULL) {
445 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
446 args.group = NULL; // TODO: decode in_args->group
447 } else {
448 args.group = NULL; // TODO: get "main" thread group
449 }
450 }
451 CHECK_GE(args.version, JNI_VERSION_1_2);
452
453 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700454 runtime->AttachCurrentThread(args.name, as_daemon);
455 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700456 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700457}
458
Elliott Hughes79082e32011-08-25 12:07:32 -0700459class SharedLibrary {
460 public:
461 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
462 : path_(path),
463 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700464 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700465 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700466 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700467 jni_on_load_result_(kPending) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700468 CHECK_PTHREAD_CALL(pthread_cond_init, (&jni_on_load_cond_, NULL), "jni_on_load_cond_");
Elliott Hughes79082e32011-08-25 12:07:32 -0700469 }
470
Elliott Hughes79082e32011-08-25 12:07:32 -0700471 Object* GetClassLoader() {
472 return class_loader_;
473 }
474
475 std::string GetPath() {
476 return path_;
477 }
478
479 /*
480 * Check the result of an earlier call to JNI_OnLoad on this library. If
481 * the call has not yet finished in another thread, wait for it.
482 */
483 bool CheckOnLoadResult(JavaVMExt* vm) {
484 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700485 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700486 // Check this so we don't end up waiting for ourselves. We need
487 // to return "true" so the caller can continue.
488 LOG(INFO) << *self << " recursive attempt to load library "
489 << "\"" << path_ << "\"";
490 return true;
491 }
492
493 MutexLock mu(jni_on_load_lock_);
494 while (jni_on_load_result_ == kPending) {
495 if (vm->verbose_jni) {
496 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
497 << "JNI_OnLoad...]";
498 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700499 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700500 CHECK_PTHREAD_CALL(pthread_cond_wait, (&jni_on_load_cond_, jni_on_load_lock_.GetImpl()), "JNI_OnLoad");
Elliott Hughes79082e32011-08-25 12:07:32 -0700501 }
502
503 bool okay = (jni_on_load_result_ == kOkay);
504 if (vm->verbose_jni) {
505 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
506 << (okay ? "succeeded" : "failed") << "]";
507 }
508 return okay;
509 }
510
511 void SetResult(bool result) {
512 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700513 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700514
515 // Broadcast a wakeup to anybody sleeping on the condition variable.
516 MutexLock mu(jni_on_load_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700517 CHECK_PTHREAD_CALL(pthread_cond_broadcast, (&jni_on_load_cond_), "JNI_OnLoad");
Elliott Hughes79082e32011-08-25 12:07:32 -0700518 }
519
520 void* FindSymbol(const std::string& symbol_name) {
521 return dlsym(handle_, symbol_name.c_str());
522 }
523
524 private:
525 enum JNI_OnLoadState {
526 kPending,
527 kFailed,
528 kOkay,
529 };
530
531 // Path to library "/system/lib/libjni.so".
532 std::string path_;
533
534 // The void* returned by dlopen(3).
535 void* handle_;
536
537 // The ClassLoader this library is associated with.
538 Object* class_loader_;
539
540 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700541 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700542 // Wait for JNI_OnLoad in other thread.
543 pthread_cond_t jni_on_load_cond_;
544 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700545 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 // Result of earlier JNI_OnLoad call.
547 JNI_OnLoadState jni_on_load_result_;
548};
549
Elliott Hughescdf53122011-08-19 15:46:09 -0700550} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700551
Elliott Hughes79082e32011-08-25 12:07:32 -0700552// This exists mainly to keep implementation details out of the header file.
553class Libraries {
554 public:
555 Libraries() {
556 }
557
558 ~Libraries() {
559 // Delete our map values. (The keys will be cleaned up by the map itself.)
560 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
561 delete it->second;
562 }
563 }
564
565 SharedLibrary* Get(const std::string& path) {
566 return libraries_[path];
567 }
568
569 void Put(const std::string& path, SharedLibrary* library) {
570 libraries_[path] = library;
571 }
572
573 // See section 11.3 "Linking Native Methods" of the JNI spec.
574 void* FindNativeMethod(const Method* m) {
575 std::string jni_short_name(JniShortName(m));
576 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700577 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700578 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
579 SharedLibrary* library = it->second;
580 if (library->GetClassLoader() != declaring_class_loader) {
581 // We only search libraries loaded by the appropriate ClassLoader.
582 continue;
583 }
584 // Try the short name then the long name...
585 void* fn = library->FindSymbol(jni_short_name);
586 if (fn == NULL) {
587 fn = library->FindSymbol(jni_long_name);
588 }
589 if (fn != NULL) {
590 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700591 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700592 << " in \"" << library->GetPath() << "\"]";
593 }
594 return fn;
595 }
596 }
597 std::string detail;
598 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700599 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 LOG(ERROR) << detail;
601 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
602 "%s", detail.c_str());
603 return NULL;
604 }
605
606 private:
607 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
608
609 std::map<std::string, SharedLibrary*> libraries_;
610};
611
Elliott Hughescdf53122011-08-19 15:46:09 -0700612class JNI {
613 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700614
Elliott Hughescdf53122011-08-19 15:46:09 -0700615 static jint GetVersion(JNIEnv* env) {
616 ScopedJniThreadState ts(env);
617 return JNI_VERSION_1_6;
618 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700619
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
621 ScopedJniThreadState ts(env);
622 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700623 return NULL;
624 }
625
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 static jclass FindClass(JNIEnv* env, const char* name) {
627 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700628 Runtime* runtime = Runtime::Current();
629 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700630 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700631 Class* c = NULL;
632 if (runtime->IsStarted()) {
633 // TODO: need to get the appropriate ClassLoader.
634 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
635 c = class_linker->FindClass(descriptor, cl);
636 } else {
637 c = class_linker->FindSystemClass(descriptor);
638 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700639 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700640 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700641
Elliott Hughescdf53122011-08-19 15:46:09 -0700642 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
643 ScopedJniThreadState ts(env);
644 Method* method = Decode<Method*>(ts, java_method);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700645 return reinterpret_cast<jmethodID>(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700646 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700647
Elliott Hughescdf53122011-08-19 15:46:09 -0700648 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
649 ScopedJniThreadState ts(env);
650 Field* field = Decode<Field*>(ts, java_field);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700651 return reinterpret_cast<jfieldID>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700653
Elliott Hughescdf53122011-08-19 15:46:09 -0700654 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
655 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700656 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700657 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700658 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700659
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
661 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700662 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700663 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700664 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700665
Elliott Hughes37f7a402011-08-22 18:56:01 -0700666 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
667 ScopedJniThreadState ts(env);
668 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700669 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700670 }
671
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700672 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700674 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700675 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700677
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700679 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700680 Class* c1 = Decode<Class*>(ts, java_class1);
681 Class* c2 = Decode<Class*>(ts, java_class2);
682 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700684
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700687 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 if (jobj == NULL) {
689 // NB. JNI is different from regular Java instanceof in this respect
690 return JNI_TRUE;
691 } else {
692 Object* obj = Decode<Object*>(ts, jobj);
693 Class* klass = Decode<Class*>(ts, clazz);
694 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
695 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700697
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 static jint Throw(JNIEnv* env, jthrowable java_exception) {
699 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700700 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700701 if (exception == NULL) {
702 return JNI_ERR;
703 }
704 ts.Self()->SetException(exception);
705 return JNI_OK;
706 }
707
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700708 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700709 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700710 // TODO: check for a pending exception to decide what constructor to call.
711 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
712 if (mid == NULL) {
713 return JNI_ERR;
714 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700715 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
716 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700717 return JNI_ERR;
718 }
719
720 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700721 args[0].l = s.get();
722 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
723 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700724 return JNI_ERR;
725 }
726
Elliott Hughes72025e52011-08-23 17:50:30 -0700727 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700728 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700729 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700730
Elliott Hughes37f7a402011-08-22 18:56:01 -0700731 return JNI_OK;
732 }
733
734 static jboolean ExceptionCheck(JNIEnv* env) {
735 ScopedJniThreadState ts(env);
736 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
737 }
738
739 static void ExceptionClear(JNIEnv* env) {
740 ScopedJniThreadState ts(env);
741 ts.Self()->ClearException();
742 }
743
744 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700746
747 Thread* self = ts.Self();
748 Throwable* original_exception = self->GetException();
749 self->ClearException();
750
Elliott Hughesbf86d042011-08-31 17:53:14 -0700751 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700752 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
753 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
754 if (mid == NULL) {
755 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
756 << PrettyType(original_exception);
757 } else {
758 env->CallVoidMethod(exception.get(), mid);
759 if (self->IsExceptionPending()) {
760 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
761 << " thrown while calling printStackTrace";
762 self->ClearException();
763 }
764 }
765
766 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700768
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 static jthrowable ExceptionOccurred(JNIEnv* env) {
770 ScopedJniThreadState ts(env);
771 Object* exception = ts.Self()->GetException();
772 if (exception == NULL) {
773 return NULL;
774 } else {
775 // TODO: if adding a local reference failing causes the VM to abort
776 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700777 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 if (localException == NULL) {
779 // We were unable to add a new local reference, and threw a new
780 // exception. We can't return "exception", because it's not a
781 // local reference. So we have to return NULL, indicating that
782 // there was no exception, even though it's pretty much raining
783 // exceptions in here.
784 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
785 }
786 return localException;
787 }
788 }
789
Elliott Hughescdf53122011-08-19 15:46:09 -0700790 static void FatalError(JNIEnv* env, const char* msg) {
791 ScopedJniThreadState ts(env);
792 LOG(FATAL) << "JNI FatalError called: " << msg;
793 }
794
795 static jint PushLocalFrame(JNIEnv* env, jint cap) {
796 ScopedJniThreadState ts(env);
797 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
798 return JNI_OK;
799 }
800
801 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
802 ScopedJniThreadState ts(env);
803 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
804 return res;
805 }
806
Elliott Hughes72025e52011-08-23 17:50:30 -0700807 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
808 ScopedJniThreadState ts(env);
809 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
810 return 0;
811 }
812
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
814 ScopedJniThreadState ts(env);
815 if (obj == NULL) {
816 return NULL;
817 }
818
Elliott Hughes75770752011-08-24 17:52:38 -0700819 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 IndirectReferenceTable& globals = vm->globals;
821 MutexLock mu(vm->globals_lock);
822 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
823 return reinterpret_cast<jobject>(ref);
824 }
825
826 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
827 ScopedJniThreadState ts(env);
828 if (obj == NULL) {
829 return;
830 }
831
Elliott Hughes75770752011-08-24 17:52:38 -0700832 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700833 IndirectReferenceTable& globals = vm->globals;
834 MutexLock mu(vm->globals_lock);
835
836 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
837 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
838 << "failed to find entry";
839 }
840 }
841
842 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
843 ScopedJniThreadState ts(env);
844 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
845 }
846
847 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
848 ScopedJniThreadState ts(env);
849 if (obj == NULL) {
850 return;
851 }
852
Elliott Hughes75770752011-08-24 17:52:38 -0700853 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 IndirectReferenceTable& weak_globals = vm->weak_globals;
855 MutexLock mu(vm->weak_globals_lock);
856
857 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
858 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
859 << "failed to find entry";
860 }
861 }
862
863 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
864 ScopedJniThreadState ts(env);
865 if (obj == NULL) {
866 return NULL;
867 }
868
869 IndirectReferenceTable& locals = ts.Env()->locals;
870
871 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
872 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
873 return reinterpret_cast<jobject>(ref);
874 }
875
876 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
877 ScopedJniThreadState ts(env);
878 if (obj == NULL) {
879 return;
880 }
881
882 IndirectReferenceTable& locals = ts.Env()->locals;
883
884 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
885 if (!locals.Remove(cookie, obj)) {
886 // Attempting to delete a local reference that is not in the
887 // topmost local reference frame is a no-op. DeleteLocalRef returns
888 // void and doesn't throw any exceptions, but we should probably
889 // complain about it so the user will notice that things aren't
890 // going quite the way they expect.
891 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
892 << "failed to find entry";
893 }
894 }
895
896 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
897 ScopedJniThreadState ts(env);
898 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
899 ? JNI_TRUE : JNI_FALSE;
900 }
901
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700902 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700904 Class* c = Decode<Class*>(ts, java_class);
905 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
906 return NULL;
907 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700908 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700909 }
910
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700912 ScopedJniThreadState ts(env);
913 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 va_start(args, mid);
915 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 va_end(args);
917 return result;
918 }
919
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700922 Class* c = Decode<Class*>(ts, java_class);
923 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
924 return NULL;
925 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700926 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700927 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700928 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 return local_result;
930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700934 Class* c = Decode<Class*>(ts, java_class);
935 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
936 return NULL;
937 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700938 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700939 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 return local_result;
942 }
943
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
945 ScopedJniThreadState ts(env);
946 return FindMethodID(ts, c, name, sig, false);
947 }
948
949 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
950 ScopedJniThreadState ts(env);
951 return FindMethodID(ts, c, name, sig, true);
952 }
953
Elliott Hughes72025e52011-08-23 17:50:30 -0700954 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 va_list ap;
957 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700958 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700960 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 }
962
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700965 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700966 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700971 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700972 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 va_list ap;
978 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700979 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 va_end(ap);
981 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 }
983
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700986 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 }
988
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700991 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 }
993
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 va_list ap;
997 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700998 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 va_end(ap);
1000 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 }
1002
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001005 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001010 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 }
1012
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 va_list ap;
1016 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001017 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_end(ap);
1019 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001024 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 }
1026
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001029 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 va_list ap;
1035 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001036 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 va_end(ap);
1038 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 }
1040
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001043 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001048 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 }
1050
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 va_list ap;
1054 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001055 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 va_end(ap);
1057 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001062 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 }
1064
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001067 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 va_list ap;
1073 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001074 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_end(ap);
1076 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 }
1078
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001081 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 }
1083
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001086 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_list ap;
1092 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001093 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 va_end(ap);
1095 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001100 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001105 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 va_list ap;
1111 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001112 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 va_end(ap);
1114 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001119 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001124 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_list ap;
1130 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001131 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001137 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001142 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
1145 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 ScopedJniThreadState ts(env);
1148 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 va_start(ap, mid);
1150 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001151 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 va_end(ap);
1153 return local_result;
1154 }
1155
1156 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001160 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
1163 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001167 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
1170 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 ScopedJniThreadState ts(env);
1173 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 va_start(ap, mid);
1175 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 va_end(ap);
1177 return result.z;
1178 }
1179
1180 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
1186 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
1192 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 ScopedJniThreadState ts(env);
1195 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 va_start(ap, mid);
1197 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 va_end(ap);
1199 return result.b;
1200 }
1201
1202 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
1208 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
1214 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 ScopedJniThreadState ts(env);
1217 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 va_start(ap, mid);
1219 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 va_end(ap);
1221 return result.c;
1222 }
1223
1224 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
1230 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
1236 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 ScopedJniThreadState ts(env);
1239 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 va_start(ap, mid);
1241 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 va_end(ap);
1243 return result.s;
1244 }
1245
1246 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
1252 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
1258 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
1261 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 va_start(ap, mid);
1263 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 va_end(ap);
1265 return result.i;
1266 }
1267
1268 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 }
1273
1274 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
1280 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 ScopedJniThreadState ts(env);
1283 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 va_start(ap, mid);
1285 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 va_end(ap);
1287 return result.j;
1288 }
1289
1290 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
1296 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
1302 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 ScopedJniThreadState ts(env);
1305 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001306 va_start(ap, mid);
1307 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 va_end(ap);
1309 return result.f;
1310 }
1311
1312 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
1318 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001319 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001321 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
1324 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 ScopedJniThreadState ts(env);
1327 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 va_start(ap, mid);
1329 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 va_end(ap);
1331 return result.d;
1332 }
1333
1334 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 }
1339
1340 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001343 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 }
1345
1346 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 ScopedJniThreadState ts(env);
1349 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001350 va_start(ap, mid);
1351 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 va_end(ap);
1353 }
1354
1355 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001356 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001358 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 }
1360
1361 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001364 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001367 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 ScopedJniThreadState ts(env);
1369 return FindFieldID(ts, c, name, sig, false);
1370 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001371
1372
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001373 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 ScopedJniThreadState ts(env);
1375 return FindFieldID(ts, c, name, sig, true);
1376 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001377
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001378 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001380 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001381 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001382 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001384
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001385 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001387 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001388 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001391 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001393 Object* o = Decode<Object*>(ts, java_object);
1394 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001395 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001401 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001402 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001403 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001406#define GET_PRIMITIVE_FIELD(fn, instance) \
1407 ScopedJniThreadState ts(env); \
1408 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001409 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001410 return f->fn(o)
1411
1412#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1413 ScopedJniThreadState ts(env); \
1414 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001415 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001416 f->fn(o, value)
1417
1418 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1419 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001422 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1423 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1427 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001430 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1431 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1435 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1439 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1443 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1447 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1451 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1455 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1459 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1463 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 }
1465
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001466 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1467 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1471 GET_PRIMITIVE_FIELD(GetLong, NULL);
1472 }
1473
1474 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1475 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1476 }
1477
1478 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1479 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1480 }
1481
1482 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1483 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1484 }
1485
1486 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1487 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1488 }
1489
1490 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1491 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1492 }
1493
1494 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1495 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1496 }
1497
1498 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1499 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1500 }
1501
1502 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1503 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1504 }
1505
1506 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1507 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1508 }
1509
1510 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1511 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1512 }
1513
1514 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1515 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1516 }
1517
1518 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1519 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1520 }
1521
1522 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1523 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1524 }
1525
1526 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1527 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1528 }
1529
1530 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1531 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1532 }
1533
1534 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1535 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1536 }
1537
1538 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1539 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1540 }
1541
1542 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1543 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
1546 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001547 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 ScopedJniThreadState ts(env);
1549 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001550 va_start(ap, mid);
1551 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001552 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 va_end(ap);
1554 return local_result;
1555 }
1556
1557 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001558 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001560 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001561 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
1564 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001565 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001567 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001568 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
1571 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001572 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 ScopedJniThreadState ts(env);
1574 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001575 va_start(ap, mid);
1576 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 va_end(ap);
1578 return result.z;
1579 }
1580
1581 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001582 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001584 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
1587 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001588 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001590 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes72025e52011-08-23 17:50:30 -07001593 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 ScopedJniThreadState ts(env);
1595 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001596 va_start(ap, mid);
1597 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 va_end(ap);
1599 return result.b;
1600 }
1601
1602 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001603 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
1608 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001609 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001611 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 }
1613
Elliott Hughes72025e52011-08-23 17:50:30 -07001614 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 ScopedJniThreadState ts(env);
1616 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001617 va_start(ap, mid);
1618 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 va_end(ap);
1620 return result.c;
1621 }
1622
1623 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001624 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
1629 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001630 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001632 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 }
1634
Elliott Hughes72025e52011-08-23 17:50:30 -07001635 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 ScopedJniThreadState ts(env);
1637 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001638 va_start(ap, mid);
1639 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 va_end(ap);
1641 return result.s;
1642 }
1643
1644 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001645 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
1650 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 }
1655
Elliott Hughes72025e52011-08-23 17:50:30 -07001656 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 ScopedJniThreadState ts(env);
1658 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 va_start(ap, mid);
1660 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 va_end(ap);
1662 return result.i;
1663 }
1664
1665 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001666 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 }
1670
1671 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001674 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 }
1676
Elliott Hughes72025e52011-08-23 17:50:30 -07001677 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 ScopedJniThreadState ts(env);
1679 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001680 va_start(ap, mid);
1681 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 va_end(ap);
1683 return result.j;
1684 }
1685
1686 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 }
1691
1692 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001693 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001695 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes72025e52011-08-23 17:50:30 -07001698 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 ScopedJniThreadState ts(env);
1700 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001701 va_start(ap, mid);
1702 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 va_end(ap);
1704 return result.f;
1705 }
1706
1707 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 }
1712
1713 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001714 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001716 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 ScopedJniThreadState ts(env);
1721 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 va_start(ap, mid);
1723 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 va_end(ap);
1725 return result.d;
1726 }
1727
1728 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001729 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
1734 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001735 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001737 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes72025e52011-08-23 17:50:30 -07001740 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 ScopedJniThreadState ts(env);
1742 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 va_start(ap, mid);
1744 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 va_end(ap);
1746 }
1747
1748 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001749 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001751 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 }
1753
1754 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001755 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 }
1759
Elliott Hughes814e4032011-08-23 12:07:56 -07001760 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001762 if (chars == NULL && char_count == 0) {
1763 return NULL;
1764 }
1765 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001766 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
1769 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1770 ScopedJniThreadState ts(env);
1771 if (utf == NULL) {
1772 return NULL;
1773 }
1774 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001775 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 }
1777
Elliott Hughes814e4032011-08-23 12:07:56 -07001778 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1779 ScopedJniThreadState ts(env);
1780 return Decode<String*>(ts, java_string)->GetLength();
1781 }
1782
1783 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1784 ScopedJniThreadState ts(env);
1785 return Decode<String*>(ts, java_string)->GetUtfLength();
1786 }
1787
Elliott Hughesb465ab02011-08-24 11:21:21 -07001788 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001790 String* s = Decode<String*>(ts, java_string);
1791 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1792 ThrowSIOOBE(ts, start, length, s->GetLength());
1793 } else {
1794 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1795 memcpy(buf, chars + start, length * sizeof(jchar));
1796 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001797 }
1798
Elliott Hughesb465ab02011-08-24 11:21:21 -07001799 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001800 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001801 String* s = Decode<String*>(ts, java_string);
1802 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1803 ThrowSIOOBE(ts, start, length, s->GetLength());
1804 } else {
1805 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1806 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1807 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001808 }
1809
Elliott Hughes75770752011-08-24 17:52:38 -07001810 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001811 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001812 String* s = Decode<String*>(ts, java_string);
1813 const CharArray* chars = s->GetCharArray();
1814 PinPrimitiveArray(ts, chars);
1815 if (is_copy != NULL) {
1816 *is_copy = JNI_FALSE;
1817 }
1818 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001819 }
1820
Elliott Hughes75770752011-08-24 17:52:38 -07001821 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001822 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001823 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
Elliott Hughes75770752011-08-24 17:52:38 -07001826 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001828 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
Elliott Hughes75770752011-08-24 17:52:38 -07001831 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001833 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 }
1835
Elliott Hughes75770752011-08-24 17:52:38 -07001836 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001837 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001838 if (java_string == NULL) {
1839 return NULL;
1840 }
1841 if (is_copy != NULL) {
1842 *is_copy = JNI_TRUE;
1843 }
1844 String* s = Decode<String*>(ts, java_string);
1845 size_t byte_count = s->GetUtfLength();
1846 char* bytes = new char[byte_count + 1];
1847 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001848 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001849 return NULL;
1850 }
1851 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1852 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1853 bytes[byte_count] = '\0';
1854 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001855 }
1856
Elliott Hughes75770752011-08-24 17:52:38 -07001857 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001858 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001859 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001860 }
1861
Elliott Hughesbd935992011-08-22 11:59:34 -07001862 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001864 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001865 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001866 Array* array = obj->AsArray();
1867 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 }
1869
Elliott Hughes814e4032011-08-23 12:07:56 -07001870 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001872 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001873 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 }
1875
1876 static void SetObjectArrayElement(JNIEnv* env,
1877 jobjectArray java_array, jsize index, jobject java_value) {
1878 ScopedJniThreadState ts(env);
1879 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1880 Object* value = Decode<Object*>(ts, java_value);
1881 array->Set(index, value);
1882 }
1883
1884 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1885 ScopedJniThreadState ts(env);
1886 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1887 }
1888
1889 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1890 ScopedJniThreadState ts(env);
1891 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1892 }
1893
1894 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1895 ScopedJniThreadState ts(env);
1896 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1897 }
1898
1899 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1900 ScopedJniThreadState ts(env);
1901 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1902 }
1903
1904 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1905 ScopedJniThreadState ts(env);
1906 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1907 }
1908
1909 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1910 ScopedJniThreadState ts(env);
1911 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1912 }
1913
1914 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1915 ScopedJniThreadState ts(env);
1916 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1917 }
1918
1919 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1920 ScopedJniThreadState ts(env);
1921 CHECK_GE(length, 0); // TODO: ReportJniError
1922
1923 // Compute the array class corresponding to the given element class.
1924 Class* element_class = Decode<Class*>(ts, element_jclass);
1925 std::string descriptor;
1926 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001927 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001928
1929 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001930 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1931 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 return NULL;
1933 }
1934
Elliott Hughes75770752011-08-24 17:52:38 -07001935 // Allocate and initialize if necessary.
1936 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001937 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001938 if (initial_element != NULL) {
1939 Object* initial_object = Decode<Object*>(ts, initial_element);
1940 for (jsize i = 0; i < length; ++i) {
1941 result->Set(i, initial_object);
1942 }
1943 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001944 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
1947 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1948 ScopedJniThreadState ts(env);
1949 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1950 }
1951
Elliott Hughes75770752011-08-24 17:52:38 -07001952 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001953 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001954 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001955 }
1956
Elliott Hughes75770752011-08-24 17:52:38 -07001957 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001958 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001959 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001960 }
1961
Elliott Hughes75770752011-08-24 17:52:38 -07001962 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001964 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 }
1966
Elliott Hughes75770752011-08-24 17:52:38 -07001967 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001969 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Elliott Hughes75770752011-08-24 17:52:38 -07001972 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001974 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
Elliott Hughes75770752011-08-24 17:52:38 -07001977 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001979 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
Elliott Hughes75770752011-08-24 17:52:38 -07001982 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001984 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 }
1986
Elliott Hughes75770752011-08-24 17:52:38 -07001987 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001989 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Elliott Hughes75770752011-08-24 17:52:38 -07001992 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001994 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
Elliott Hughes75770752011-08-24 17:52:38 -07001997 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001999 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
Elliott Hughes75770752011-08-24 17:52:38 -07002002 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002004 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 }
2006
Elliott Hughes75770752011-08-24 17:52:38 -07002007 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002009 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Elliott Hughes75770752011-08-24 17:52:38 -07002012 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002014 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 }
2016
Elliott Hughes75770752011-08-24 17:52:38 -07002017 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002019 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
Elliott Hughes75770752011-08-24 17:52:38 -07002022 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002024 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 }
2026
Elliott Hughes75770752011-08-24 17:52:38 -07002027 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002029 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Elliott Hughes75770752011-08-24 17:52:38 -07002032 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002034 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Elliott Hughes75770752011-08-24 17:52:38 -07002037 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002039 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Elliott Hughes814e4032011-08-23 12:07:56 -07002042 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002044 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Elliott Hughes814e4032011-08-23 12:07:56 -07002047 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002049 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Elliott Hughes814e4032011-08-23 12:07:56 -07002052 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002054 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes814e4032011-08-23 12:07:56 -07002057 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Elliott Hughes814e4032011-08-23 12:07:56 -07002062 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes814e4032011-08-23 12:07:56 -07002067 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Elliott Hughes814e4032011-08-23 12:07:56 -07002072 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes814e4032011-08-23 12:07:56 -07002077 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Elliott Hughes814e4032011-08-23 12:07:56 -07002082 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Elliott Hughes814e4032011-08-23 12:07:56 -07002087 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
Elliott Hughes814e4032011-08-23 12:07:56 -07002092 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 }
2096
Elliott Hughes814e4032011-08-23 12:07:56 -07002097 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 }
2101
Elliott Hughes814e4032011-08-23 12:07:56 -07002102 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 }
2106
Elliott Hughes814e4032011-08-23 12:07:56 -07002107 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
Elliott Hughes814e4032011-08-23 12:07:56 -07002112 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 }
2116
Elliott Hughes814e4032011-08-23 12:07:56 -07002117 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
Elliott Hughes5174fe62011-08-23 15:12:35 -07002122 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002124 Class* c = Decode<Class*>(ts, java_class);
2125
Elliott Hughes5174fe62011-08-23 15:12:35 -07002126 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 const char* name = methods[i].name;
2128 const char* sig = methods[i].signature;
2129
2130 if (*sig == '!') {
2131 // TODO: fast jni. it's too noisy to log all these.
2132 ++sig;
2133 }
2134
Elliott Hughes5174fe62011-08-23 15:12:35 -07002135 Method* m = c->FindDirectMethod(name, sig);
2136 if (m == NULL) {
2137 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002139 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002141 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2143 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002144 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002146 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002148 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2150 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002151 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 return JNI_ERR;
2153 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002154
Elliott Hughes75770752011-08-24 17:52:38 -07002155 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002156 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002157 }
2158
2159 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 }
2161 return JNI_OK;
2162 }
2163
Elliott Hughes5174fe62011-08-23 15:12:35 -07002164 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002166 Class* c = Decode<Class*>(ts, java_class);
2167
Elliott Hughes75770752011-08-24 17:52:38 -07002168 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002169 LOG(INFO) << "[Unregistering JNI native methods for "
2170 << PrettyDescriptor(c->GetDescriptor()) << "]";
2171 }
2172
2173 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2174 Method* m = c->GetDirectMethod(i);
2175 if (m->IsNative()) {
2176 m->UnregisterNative();
2177 }
2178 }
2179 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2180 Method* m = c->GetVirtualMethod(i);
2181 if (m->IsNative()) {
2182 m->UnregisterNative();
2183 }
2184 }
2185
2186 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 }
2188
Elliott Hughes72025e52011-08-23 17:50:30 -07002189 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002191 Decode<Object*>(ts, java_object)->MonitorEnter();
2192 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
Elliott Hughes72025e52011-08-23 17:50:30 -07002195 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 ScopedJniThreadState ts(env);
Elliott Hughes02b48d12011-09-07 17:15:51 -07002197 Decode<Object*>(ts, java_object)->MonitorExit();
Elliott Hughes72025e52011-08-23 17:50:30 -07002198 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 }
2200
2201 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2202 ScopedJniThreadState ts(env);
2203 Runtime* runtime = Runtime::Current();
2204 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002205 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 } else {
2207 *vm = NULL;
2208 }
2209 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2210 }
2211
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2213 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002214
2215 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002216 CHECK(address != NULL); // TODO: ReportJniError
2217 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002218
2219 jclass buffer_class = GetDirectByteBufferClass(env);
2220 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2221 if (mid == NULL) {
2222 return NULL;
2223 }
2224
2225 // At the moment, the Java side is limited to 32 bits.
2226 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2227 CHECK_LE(capacity, 0xffffffff);
2228 jint address_arg = reinterpret_cast<jint>(address);
2229 jint capacity_arg = static_cast<jint>(capacity);
2230
2231 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2232 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Elliott Hughesb465ab02011-08-24 11:21:21 -07002235 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002237 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2238 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Elliott Hughesb465ab02011-08-24 11:21:21 -07002241 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002243 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2244 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249
Elliott Hughes75770752011-08-24 17:52:38 -07002250 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002251
2252 // Do we definitely know what kind of reference this is?
2253 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2254 IndirectRefKind kind = GetIndirectRefKind(ref);
2255 switch (kind) {
2256 case kLocal:
2257 return JNILocalRefType;
2258 case kGlobal:
2259 return JNIGlobalRefType;
2260 case kWeakGlobal:
2261 return JNIWeakGlobalRefType;
2262 case kSirtOrInvalid:
2263 // Is it in a stack IRT?
2264 if (ts.Self()->SirtContains(java_object)) {
2265 return JNILocalRefType;
2266 }
2267
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002268 if (!ts.Env()->work_around_app_jni_bugs) {
2269 return JNIInvalidRefType;
2270 }
2271
Elliott Hughesb465ab02011-08-24 11:21:21 -07002272 // If we're handing out direct pointers, check whether it's a direct pointer
2273 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002274 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002275 if (ts.Env()->locals.Contains(java_object)) {
2276 return JNILocalRefType;
2277 }
2278 }
2279
2280 return JNIInvalidRefType;
2281 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002282 }
2283};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002284
Elliott Hughesa2501992011-08-26 19:39:54 -07002285const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002286 NULL, // reserved0.
2287 NULL, // reserved1.
2288 NULL, // reserved2.
2289 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 JNI::GetVersion,
2291 JNI::DefineClass,
2292 JNI::FindClass,
2293 JNI::FromReflectedMethod,
2294 JNI::FromReflectedField,
2295 JNI::ToReflectedMethod,
2296 JNI::GetSuperclass,
2297 JNI::IsAssignableFrom,
2298 JNI::ToReflectedField,
2299 JNI::Throw,
2300 JNI::ThrowNew,
2301 JNI::ExceptionOccurred,
2302 JNI::ExceptionDescribe,
2303 JNI::ExceptionClear,
2304 JNI::FatalError,
2305 JNI::PushLocalFrame,
2306 JNI::PopLocalFrame,
2307 JNI::NewGlobalRef,
2308 JNI::DeleteGlobalRef,
2309 JNI::DeleteLocalRef,
2310 JNI::IsSameObject,
2311 JNI::NewLocalRef,
2312 JNI::EnsureLocalCapacity,
2313 JNI::AllocObject,
2314 JNI::NewObject,
2315 JNI::NewObjectV,
2316 JNI::NewObjectA,
2317 JNI::GetObjectClass,
2318 JNI::IsInstanceOf,
2319 JNI::GetMethodID,
2320 JNI::CallObjectMethod,
2321 JNI::CallObjectMethodV,
2322 JNI::CallObjectMethodA,
2323 JNI::CallBooleanMethod,
2324 JNI::CallBooleanMethodV,
2325 JNI::CallBooleanMethodA,
2326 JNI::CallByteMethod,
2327 JNI::CallByteMethodV,
2328 JNI::CallByteMethodA,
2329 JNI::CallCharMethod,
2330 JNI::CallCharMethodV,
2331 JNI::CallCharMethodA,
2332 JNI::CallShortMethod,
2333 JNI::CallShortMethodV,
2334 JNI::CallShortMethodA,
2335 JNI::CallIntMethod,
2336 JNI::CallIntMethodV,
2337 JNI::CallIntMethodA,
2338 JNI::CallLongMethod,
2339 JNI::CallLongMethodV,
2340 JNI::CallLongMethodA,
2341 JNI::CallFloatMethod,
2342 JNI::CallFloatMethodV,
2343 JNI::CallFloatMethodA,
2344 JNI::CallDoubleMethod,
2345 JNI::CallDoubleMethodV,
2346 JNI::CallDoubleMethodA,
2347 JNI::CallVoidMethod,
2348 JNI::CallVoidMethodV,
2349 JNI::CallVoidMethodA,
2350 JNI::CallNonvirtualObjectMethod,
2351 JNI::CallNonvirtualObjectMethodV,
2352 JNI::CallNonvirtualObjectMethodA,
2353 JNI::CallNonvirtualBooleanMethod,
2354 JNI::CallNonvirtualBooleanMethodV,
2355 JNI::CallNonvirtualBooleanMethodA,
2356 JNI::CallNonvirtualByteMethod,
2357 JNI::CallNonvirtualByteMethodV,
2358 JNI::CallNonvirtualByteMethodA,
2359 JNI::CallNonvirtualCharMethod,
2360 JNI::CallNonvirtualCharMethodV,
2361 JNI::CallNonvirtualCharMethodA,
2362 JNI::CallNonvirtualShortMethod,
2363 JNI::CallNonvirtualShortMethodV,
2364 JNI::CallNonvirtualShortMethodA,
2365 JNI::CallNonvirtualIntMethod,
2366 JNI::CallNonvirtualIntMethodV,
2367 JNI::CallNonvirtualIntMethodA,
2368 JNI::CallNonvirtualLongMethod,
2369 JNI::CallNonvirtualLongMethodV,
2370 JNI::CallNonvirtualLongMethodA,
2371 JNI::CallNonvirtualFloatMethod,
2372 JNI::CallNonvirtualFloatMethodV,
2373 JNI::CallNonvirtualFloatMethodA,
2374 JNI::CallNonvirtualDoubleMethod,
2375 JNI::CallNonvirtualDoubleMethodV,
2376 JNI::CallNonvirtualDoubleMethodA,
2377 JNI::CallNonvirtualVoidMethod,
2378 JNI::CallNonvirtualVoidMethodV,
2379 JNI::CallNonvirtualVoidMethodA,
2380 JNI::GetFieldID,
2381 JNI::GetObjectField,
2382 JNI::GetBooleanField,
2383 JNI::GetByteField,
2384 JNI::GetCharField,
2385 JNI::GetShortField,
2386 JNI::GetIntField,
2387 JNI::GetLongField,
2388 JNI::GetFloatField,
2389 JNI::GetDoubleField,
2390 JNI::SetObjectField,
2391 JNI::SetBooleanField,
2392 JNI::SetByteField,
2393 JNI::SetCharField,
2394 JNI::SetShortField,
2395 JNI::SetIntField,
2396 JNI::SetLongField,
2397 JNI::SetFloatField,
2398 JNI::SetDoubleField,
2399 JNI::GetStaticMethodID,
2400 JNI::CallStaticObjectMethod,
2401 JNI::CallStaticObjectMethodV,
2402 JNI::CallStaticObjectMethodA,
2403 JNI::CallStaticBooleanMethod,
2404 JNI::CallStaticBooleanMethodV,
2405 JNI::CallStaticBooleanMethodA,
2406 JNI::CallStaticByteMethod,
2407 JNI::CallStaticByteMethodV,
2408 JNI::CallStaticByteMethodA,
2409 JNI::CallStaticCharMethod,
2410 JNI::CallStaticCharMethodV,
2411 JNI::CallStaticCharMethodA,
2412 JNI::CallStaticShortMethod,
2413 JNI::CallStaticShortMethodV,
2414 JNI::CallStaticShortMethodA,
2415 JNI::CallStaticIntMethod,
2416 JNI::CallStaticIntMethodV,
2417 JNI::CallStaticIntMethodA,
2418 JNI::CallStaticLongMethod,
2419 JNI::CallStaticLongMethodV,
2420 JNI::CallStaticLongMethodA,
2421 JNI::CallStaticFloatMethod,
2422 JNI::CallStaticFloatMethodV,
2423 JNI::CallStaticFloatMethodA,
2424 JNI::CallStaticDoubleMethod,
2425 JNI::CallStaticDoubleMethodV,
2426 JNI::CallStaticDoubleMethodA,
2427 JNI::CallStaticVoidMethod,
2428 JNI::CallStaticVoidMethodV,
2429 JNI::CallStaticVoidMethodA,
2430 JNI::GetStaticFieldID,
2431 JNI::GetStaticObjectField,
2432 JNI::GetStaticBooleanField,
2433 JNI::GetStaticByteField,
2434 JNI::GetStaticCharField,
2435 JNI::GetStaticShortField,
2436 JNI::GetStaticIntField,
2437 JNI::GetStaticLongField,
2438 JNI::GetStaticFloatField,
2439 JNI::GetStaticDoubleField,
2440 JNI::SetStaticObjectField,
2441 JNI::SetStaticBooleanField,
2442 JNI::SetStaticByteField,
2443 JNI::SetStaticCharField,
2444 JNI::SetStaticShortField,
2445 JNI::SetStaticIntField,
2446 JNI::SetStaticLongField,
2447 JNI::SetStaticFloatField,
2448 JNI::SetStaticDoubleField,
2449 JNI::NewString,
2450 JNI::GetStringLength,
2451 JNI::GetStringChars,
2452 JNI::ReleaseStringChars,
2453 JNI::NewStringUTF,
2454 JNI::GetStringUTFLength,
2455 JNI::GetStringUTFChars,
2456 JNI::ReleaseStringUTFChars,
2457 JNI::GetArrayLength,
2458 JNI::NewObjectArray,
2459 JNI::GetObjectArrayElement,
2460 JNI::SetObjectArrayElement,
2461 JNI::NewBooleanArray,
2462 JNI::NewByteArray,
2463 JNI::NewCharArray,
2464 JNI::NewShortArray,
2465 JNI::NewIntArray,
2466 JNI::NewLongArray,
2467 JNI::NewFloatArray,
2468 JNI::NewDoubleArray,
2469 JNI::GetBooleanArrayElements,
2470 JNI::GetByteArrayElements,
2471 JNI::GetCharArrayElements,
2472 JNI::GetShortArrayElements,
2473 JNI::GetIntArrayElements,
2474 JNI::GetLongArrayElements,
2475 JNI::GetFloatArrayElements,
2476 JNI::GetDoubleArrayElements,
2477 JNI::ReleaseBooleanArrayElements,
2478 JNI::ReleaseByteArrayElements,
2479 JNI::ReleaseCharArrayElements,
2480 JNI::ReleaseShortArrayElements,
2481 JNI::ReleaseIntArrayElements,
2482 JNI::ReleaseLongArrayElements,
2483 JNI::ReleaseFloatArrayElements,
2484 JNI::ReleaseDoubleArrayElements,
2485 JNI::GetBooleanArrayRegion,
2486 JNI::GetByteArrayRegion,
2487 JNI::GetCharArrayRegion,
2488 JNI::GetShortArrayRegion,
2489 JNI::GetIntArrayRegion,
2490 JNI::GetLongArrayRegion,
2491 JNI::GetFloatArrayRegion,
2492 JNI::GetDoubleArrayRegion,
2493 JNI::SetBooleanArrayRegion,
2494 JNI::SetByteArrayRegion,
2495 JNI::SetCharArrayRegion,
2496 JNI::SetShortArrayRegion,
2497 JNI::SetIntArrayRegion,
2498 JNI::SetLongArrayRegion,
2499 JNI::SetFloatArrayRegion,
2500 JNI::SetDoubleArrayRegion,
2501 JNI::RegisterNatives,
2502 JNI::UnregisterNatives,
2503 JNI::MonitorEnter,
2504 JNI::MonitorExit,
2505 JNI::GetJavaVM,
2506 JNI::GetStringRegion,
2507 JNI::GetStringUTFRegion,
2508 JNI::GetPrimitiveArrayCritical,
2509 JNI::ReleasePrimitiveArrayCritical,
2510 JNI::GetStringCritical,
2511 JNI::ReleaseStringCritical,
2512 JNI::NewWeakGlobalRef,
2513 JNI::DeleteWeakGlobalRef,
2514 JNI::ExceptionCheck,
2515 JNI::NewDirectByteBuffer,
2516 JNI::GetDirectBufferAddress,
2517 JNI::GetDirectBufferCapacity,
2518 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002519};
2520
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002521static const size_t kMonitorsInitial = 32; // Arbitrary.
2522static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2523
2524static const size_t kLocalsInitial = 64; // Arbitrary.
2525static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002526
Elliott Hughes75770752011-08-24 17:52:38 -07002527JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002528 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002529 vm(vm),
2530 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002531 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002532 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002533 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2534 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002535 functions = unchecked_functions = &gNativeInterface;
2536 if (check_jni) {
2537 functions = GetCheckJniNativeInterface();
2538 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002539}
2540
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002541JNIEnvExt::~JNIEnvExt() {
2542}
2543
Carl Shapiroea4dca82011-08-01 13:45:38 -07002544// JNI Invocation interface.
2545
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002546extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2547 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2548 if (args->version < JNI_VERSION_1_2) {
2549 return JNI_EVERSION;
2550 }
2551 Runtime::Options options;
2552 for (int i = 0; i < args->nOptions; ++i) {
2553 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002554 options.push_back(std::make_pair(StringPiece(option->optionString),
2555 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002556 }
2557 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002558 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002559 if (runtime == NULL) {
2560 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002561 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002562 runtime->Start();
2563 *p_env = Thread::Current()->GetJniEnv();
2564 *p_vm = runtime->GetJavaVM();
2565 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002566}
2567
Elliott Hughesf2682d52011-08-15 16:37:04 -07002568extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002569 Runtime* runtime = Runtime::Current();
2570 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002571 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002572 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002573 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002574 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002575 }
2576 return JNI_OK;
2577}
2578
2579// Historically unsupported.
2580extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2581 return JNI_ERR;
2582}
2583
Elliott Hughescdf53122011-08-19 15:46:09 -07002584class JII {
2585 public:
2586 static jint DestroyJavaVM(JavaVM* vm) {
2587 if (vm == NULL) {
2588 return JNI_ERR;
2589 } else {
2590 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2591 delete raw_vm->runtime;
2592 return JNI_OK;
2593 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002594 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002595
Elliott Hughescdf53122011-08-19 15:46:09 -07002596 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002597 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002598 }
2599
2600 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002601 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002602 }
2603
2604 static jint DetachCurrentThread(JavaVM* vm) {
2605 if (vm == NULL) {
2606 return JNI_ERR;
2607 } else {
2608 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2609 Runtime* runtime = raw_vm->runtime;
2610 runtime->DetachCurrentThread();
2611 return JNI_OK;
2612 }
2613 }
2614
2615 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2616 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2617 return JNI_EVERSION;
2618 }
2619 if (vm == NULL || env == NULL) {
2620 return JNI_ERR;
2621 }
2622 Thread* thread = Thread::Current();
2623 if (thread == NULL) {
2624 *env = NULL;
2625 return JNI_EDETACHED;
2626 }
2627 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002628 return JNI_OK;
2629 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002630};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002631
Elliott Hughesa2501992011-08-26 19:39:54 -07002632const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002633 NULL, // reserved0
2634 NULL, // reserved1
2635 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002636 JII::DestroyJavaVM,
2637 JII::AttachCurrentThread,
2638 JII::DetachCurrentThread,
2639 JII::GetEnv,
2640 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002641};
2642
Elliott Hughesbbd76712011-08-17 10:25:24 -07002643static const size_t kPinTableInitialSize = 16;
2644static const size_t kPinTableMaxSize = 1024;
2645
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002646static const size_t kGlobalsInitial = 512; // Arbitrary.
2647static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2648
2649static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2650static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2651
Elliott Hughesa0957642011-09-02 14:27:33 -07002652JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002653 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002654 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002655 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002656 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002657 verbose_jni(options->IsVerbose("jni")),
2658 log_third_party_jni(options->IsVerbose("third-party-jni")),
2659 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002660 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002661 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002662 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002663 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002664 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002665 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002666 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002667 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002668 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002669 functions = unchecked_functions = &gInvokeInterface;
2670 if (check_jni) {
2671 functions = GetCheckJniInvokeInterface();
2672 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002673}
2674
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002675JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002676 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002677}
2678
Elliott Hughes75770752011-08-24 17:52:38 -07002679bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2680 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002681
2682 // See if we've already loaded this library. If we have, and the class loader
2683 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002684 // TODO: for better results we should canonicalize the pathname (or even compare
2685 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002686 SharedLibrary* library;
2687 {
2688 // TODO: move the locking (and more of this logic) into Libraries.
2689 MutexLock mu(libraries_lock);
2690 library = libraries->Get(path);
2691 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002692 if (library != NULL) {
2693 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002694 // The library will be associated with class_loader. The JNI
2695 // spec says we can't load the same library into more than one
2696 // class loader.
2697 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2698 "ClassLoader %p; can't open in ClassLoader %p",
2699 path.c_str(), library->GetClassLoader(), class_loader);
2700 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002701 return false;
2702 }
2703 if (verbose_jni) {
2704 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2705 << "ClassLoader " << class_loader << "]";
2706 }
2707 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002708 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2709 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002710 return false;
2711 }
2712 return true;
2713 }
2714
2715 // Open the shared library. Because we're using a full path, the system
2716 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2717 // resolve this library's dependencies though.)
2718
2719 // Failures here are expected when java.library.path has several entries
2720 // and we have to hunt for the lib.
2721
2722 // The current version of the dynamic linker prints detailed information
2723 // about dlopen() failures. Some things to check if the message is
2724 // cryptic:
2725 // - make sure the library exists on the device
2726 // - verify that the right path is being opened (the debug log message
2727 // above can help with that)
2728 // - check to see if the library is valid (e.g. not zero bytes long)
2729 // - check config/prelink-linux-arm.map to ensure that the library
2730 // is listed and is not being overrun by the previous entry (if
2731 // loading suddenly stops working on a prelinked library, this is
2732 // a good one to check)
2733 // - write a trivial app that calls sleep() then dlopen(), attach
2734 // to it with "strace -p <pid>" while it sleeps, and watch for
2735 // attempts to open nonexistent dependent shared libs
2736
2737 // TODO: automate some of these checks!
2738
2739 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002740 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002741 // the GC to ignore us.
2742 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002743 void* handle = NULL;
2744 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002745 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002746 handle = dlopen(path.c_str(), RTLD_LAZY);
2747 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002748
2749 if (verbose_jni) {
2750 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2751 }
2752
2753 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002754 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002755 return false;
2756 }
2757
2758 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002759 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002760 // TODO: move the locking (and more of this logic) into Libraries.
2761 MutexLock mu(libraries_lock);
2762 library = libraries->Get(path);
2763 if (library != NULL) {
2764 LOG(INFO) << "WOW: we lost a race to add shared library: "
2765 << "\"" << path << "\" ClassLoader=" << class_loader;
2766 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002767 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002768 library = new SharedLibrary(path, handle, class_loader);
2769 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002770 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002771
2772 if (verbose_jni) {
2773 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2774 }
2775
2776 bool result = true;
2777 void* sym = dlsym(handle, "JNI_OnLoad");
2778 if (sym == NULL) {
2779 if (verbose_jni) {
2780 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2781 }
2782 } else {
2783 // Call JNI_OnLoad. We have to override the current class
2784 // loader, which will always be "null" since the stuff at the
2785 // top of the stack is around Runtime.loadLibrary(). (See
2786 // the comments in the JNI FindClass function.)
2787 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2788 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002789 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002790 self->SetClassLoaderOverride(class_loader);
2791
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002792 int version = 0;
2793 {
2794 ScopedThreadStateChange tsc(self, Thread::kNative);
2795 if (verbose_jni) {
2796 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2797 }
2798 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002799 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002800
2801 self->SetClassLoaderOverride(old_class_loader);;
2802
2803 if (version != JNI_VERSION_1_2 &&
2804 version != JNI_VERSION_1_4 &&
2805 version != JNI_VERSION_1_6) {
2806 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2807 << "bad version: " << version;
2808 // It's unwise to call dlclose() here, but we can mark it
2809 // as bad and ensure that future load attempts will fail.
2810 // We don't know how far JNI_OnLoad got, so there could
2811 // be some partially-initialized stuff accessible through
2812 // newly-registered native method calls. We could try to
2813 // unregister them, but that doesn't seem worthwhile.
2814 result = false;
2815 } else {
2816 if (verbose_jni) {
2817 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2818 << " from JNI_OnLoad in \"" << path << "\"]";
2819 }
2820 }
2821 }
2822
2823 library->SetResult(result);
2824 return result;
2825}
2826
2827void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2828 CHECK(m->IsNative());
2829
2830 Class* c = m->GetDeclaringClass();
2831
2832 // If this is a static method, it could be called before the class
2833 // has been initialized.
2834 if (m->IsStatic()) {
2835 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2836 return NULL;
2837 }
2838 } else {
2839 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2840 }
2841
2842 MutexLock mu(libraries_lock);
2843 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002844}
2845
Elliott Hughes410c0c82011-09-01 17:58:25 -07002846void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2847 {
2848 MutexLock mu(globals_lock);
2849 globals.VisitRoots(visitor, arg);
2850 }
2851 {
2852 MutexLock mu(pins_lock);
2853 pin_table.VisitRoots(visitor, arg);
2854 }
2855 // The weak_globals table is visited by the GC itself (because it mutates the table).
2856}
2857
Ian Rogersdf20fe02011-07-20 20:34:16 -07002858} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002859
2860std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2861 switch (rhs) {
2862 case JNIInvalidRefType:
2863 os << "JNIInvalidRefType";
2864 return os;
2865 case JNILocalRefType:
2866 os << "JNILocalRefType";
2867 return os;
2868 case JNIGlobalRefType:
2869 os << "JNIGlobalRefType";
2870 return os;
2871 case JNIWeakGlobalRefType:
2872 os << "JNIWeakGlobalRefType";
2873 return os;
2874 }
2875}