blob: 2ea6f765145e16697258f5f2ce68207b23e3e4cb [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
Elliott Hughesc5f7c912011-08-18 14:00:42 -070028/*
29 * Add a local reference for an object to the current stack frame. When
30 * the native function returns, the reference will be discarded.
31 *
32 * We need to allow the same reference to be added multiple times.
33 *
34 * This will be called on otherwise unreferenced objects. We cannot do
35 * GC allocations here, and it's best if we don't grab a mutex.
36 *
37 * Returns the local reference (currently just the same pointer that was
38 * passed in), or NULL on failure.
39 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070040template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070041T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
42 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
43 Object* obj = const_cast<Object*>(const_obj);
44
Elliott Hughesc5f7c912011-08-18 14:00:42 -070045 if (obj == NULL) {
46 return NULL;
47 }
48
Elliott Hughesbf86d042011-08-31 17:53:14 -070049 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
50 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070051
52 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
53 IndirectRef ref = locals.Add(cookie, obj);
54 if (ref == NULL) {
55 // TODO: just change Add's DCHECK to CHECK and lose this?
56 locals.Dump();
57 LOG(FATAL) << "Failed adding to JNI local reference table "
58 << "(has " << locals.Capacity() << " entries)";
59 // TODO: dvmDumpThread(dvmThreadSelf(), false);
60 }
61
62#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070063 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070064 size_t entry_count = locals.Capacity();
65 if (entry_count > 16) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -070066 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -070067 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -070068 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070069 locals.Dump();
70 // TODO: dvmDumpThread(dvmThreadSelf(), false);
71 // dvmAbort();
72 }
73 }
74#endif
75
Elliott Hughesbf86d042011-08-31 17:53:14 -070076 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070077 // Hand out direct pointers to support broken old apps.
78 return reinterpret_cast<T>(obj);
79 }
80
81 return reinterpret_cast<T>(ref);
82}
83
Elliott Hughesbf86d042011-08-31 17:53:14 -070084// For external use.
85template<typename T>
86T Decode(JNIEnv* public_env, jobject obj) {
87 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
88 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
89}
90// Explicit instantiations.
91template Class* Decode<Class*>(JNIEnv*, jobject);
92template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
93template Object* Decode<Object*>(JNIEnv*, jobject);
94template String* Decode<String*>(JNIEnv*, jobject);
95
96namespace {
97
Elliott Hughescdf53122011-08-19 15:46:09 -070098jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
99 if (obj == NULL) {
100 return NULL;
101 }
Elliott Hughes75770752011-08-24 17:52:38 -0700102 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700103 IndirectReferenceTable& weak_globals = vm->weak_globals;
104 MutexLock mu(vm->weak_globals_lock);
105 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
106 return reinterpret_cast<jweak>(ref);
107}
108
Elliott Hughesbf86d042011-08-31 17:53:14 -0700109// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700110template<typename T>
111T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700112 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700113}
114
Elliott Hughescdf53122011-08-19 15:46:09 -0700115Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
116 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
117}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700118
Elliott Hughescdf53122011-08-19 15:46:09 -0700119Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
120 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700121}
122
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700123byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700124 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700125 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700126 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700127 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700128 switch (shorty[i]) {
129 case 'Z':
130 case 'B':
131 case 'C':
132 case 'S':
133 case 'I':
134 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
135 offset += 4;
136 break;
137 case 'F':
138 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
139 offset += 4;
140 break;
141 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700142 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700143 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
144 offset += sizeof(Object*);
145 break;
146 }
147 case 'D':
148 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
149 offset += 8;
150 break;
151 case 'J':
152 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
153 offset += 8;
154 break;
155 }
156 }
157 return arg_array.release();
158}
159
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700160byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700161 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700162 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700163 const StringPiece& shorty = method->GetShorty();
Ian Rogers4dd71f12011-08-16 14:16:02 -0700164 for (int i = 1, offset = 0; i < shorty.size(); ++i) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700165 switch (shorty[i]) {
166 case 'Z':
167 case 'B':
168 case 'C':
169 case 'S':
170 case 'I':
171 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
172 offset += 4;
173 break;
174 case 'F':
175 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
176 offset += 4;
177 break;
178 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700179 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700180 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
181 offset += sizeof(Object*);
182 break;
183 }
184 case 'D':
185 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
186 offset += 8;
187 break;
188 case 'J':
189 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
190 offset += 8;
191 break;
192 }
193 }
194 return arg_array.release();
195}
196
Elliott Hughes72025e52011-08-23 17:50:30 -0700197JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
198 Method* method, byte* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700199 JValue result;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700200 method->Invoke(ts.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700201 return result;
202}
203
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700204JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700205 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700206 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700207 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700208 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700209 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700210}
211
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700212JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700213 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700214 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700215 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700216 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700217 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
218}
219
Elliott Hughes75770752011-08-24 17:52:38 -0700220Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700221 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700222}
223
Brian Carlstrom30b94452011-08-25 21:35:26 -0700224JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700225 Object* receiver = Decode<Object*>(ts, obj);
226 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700227 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700228 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
229}
230
Brian Carlstrom30b94452011-08-25 21:35:26 -0700231JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700232 Object* receiver = Decode<Object*>(ts, obj);
233 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700234 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700235 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700236}
237
Elliott Hughes6b436852011-08-12 10:16:44 -0700238// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
239// separated with slashes but aren't wrapped with "L;" like regular descriptors
240// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
241// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
242// supported names with dots too (such as "a.b.C").
243std::string NormalizeJniClassDescriptor(const char* name) {
244 std::string result;
245 // Add the missing "L;" if necessary.
246 if (name[0] == '[') {
247 result = name;
248 } else {
249 result += 'L';
250 result += name;
251 result += ';';
252 }
253 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700254 if (result.find('.') != std::string::npos) {
255 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
256 << "\"" << name << "\"";
257 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700258 }
259 return result;
260}
261
Elliott Hughescdf53122011-08-19 15:46:09 -0700262jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
263 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700264 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
265 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700266 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700267
268 Method* method = NULL;
269 if (is_static) {
270 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700271 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700272 method = c->FindVirtualMethod(name, sig);
273 if (method == NULL) {
274 // No virtual method matching the signature. Search declared
275 // private methods and constructors.
276 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700277 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700278 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700279
Elliott Hughescdf53122011-08-19 15:46:09 -0700280 if (method == NULL || method->IsStatic() != is_static) {
281 Thread* self = Thread::Current();
Elliott Hughesa2501992011-08-26 19:39:54 -0700282 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700283 // TODO: try searching for the opposite kind of method from is_static
284 // for better diagnostics?
285 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700286 "no %s method %s", is_static ? "static" : "non-static",
287 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700288 return NULL;
289 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700290
Elliott Hughescdf53122011-08-19 15:46:09 -0700291 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700292}
293
Elliott Hughescdf53122011-08-19 15:46:09 -0700294jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
295 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700296 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
297 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700298 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700299
300 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 Class* field_type;
302 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
303 if (sig[1] != '\0') {
304 // TODO: need to get the appropriate ClassLoader.
305 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
306 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700307 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700308 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700309 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700310 if (field_type == NULL) {
311 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700312 DCHECK(ts.Self()->IsExceptionPending());
313 ts.Self()->ClearException();
314 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
315 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
316 "no type \"%s\" found and so no field \"%s\" could be found in class "
317 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700318 return NULL;
319 }
320 if (is_static) {
321 field = c->FindStaticField(name, field_type);
322 } else {
323 field = c->FindInstanceField(name, field_type);
324 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700325 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700326 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700327 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700328 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700329 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700330 return NULL;
331 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700332 // Check invariant that all jfieldIDs have resolved types (how else would
333 // the type equality in Find...Field hold?)
334 DCHECK(field->GetType() != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700335 jweak fid = AddWeakGlobalReference(ts, field);
336 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700337}
338
Elliott Hughes75770752011-08-24 17:52:38 -0700339void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
340 JavaVMExt* vm = ts.Vm();
341 MutexLock mu(vm->pins_lock);
342 vm->pin_table.Add(array);
343}
344
345void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
346 JavaVMExt* vm = ts.Vm();
347 MutexLock mu(vm->pins_lock);
348 vm->pin_table.Remove(array);
349}
350
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700351template<typename JniT, typename ArtT>
352JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
353 CHECK_GE(length, 0); // TODO: ReportJniError
354 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700355 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700356}
357
Elliott Hughes75770752011-08-24 17:52:38 -0700358template <typename ArrayT, typename CArrayT, typename ArtArrayT>
359CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
360 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
361 PinPrimitiveArray(ts, array);
362 if (is_copy != NULL) {
363 *is_copy = JNI_FALSE;
364 }
365 return array->GetData();
366}
367
368template <typename ArrayT>
369void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
370 if (mode != JNI_COMMIT) {
371 Array* array = Decode<Array*>(ts, java_array);
372 UnpinPrimitiveArray(ts, array);
373 }
374}
375
Elliott Hughes814e4032011-08-23 12:07:56 -0700376void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
377 std::string type(PrettyType(array));
378 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
379 "%s offset=%d length=%d %s.length=%d",
380 type.c_str(), start, length, identifier, array->GetLength());
381}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700382void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
383 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
384 "offset=%d length=%d string.length()=%d", start, length, array_length);
385}
Elliott Hughes814e4032011-08-23 12:07:56 -0700386
387template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700388void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700389 ArrayT* array = Decode<ArrayT*>(ts, java_array);
390 if (start < 0 || length < 0 || start + length > array->GetLength()) {
391 ThrowAIOOBE(ts, array, start, length, "src");
392 } else {
393 JavaT* data = array->GetData();
394 memcpy(buf, data + start, length * sizeof(JavaT));
395 }
396}
397
398template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700399void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700400 ArrayT* array = Decode<ArrayT*>(ts, java_array);
401 if (start < 0 || length < 0 || start + length > array->GetLength()) {
402 ThrowAIOOBE(ts, array, start, length, "dst");
403 } else {
404 JavaT* data = array->GetData();
405 memcpy(data + start, buf, length * sizeof(JavaT));
406 }
407}
408
Elliott Hughes75770752011-08-24 17:52:38 -0700409jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700410 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
411 CHECK(buffer_class.get() != NULL);
412 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
413}
414
Elliott Hughes75770752011-08-24 17:52:38 -0700415jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700416 static jclass buffer_class = InitDirectByteBufferClass(env);
417 return buffer_class;
418}
419
Elliott Hughes75770752011-08-24 17:52:38 -0700420jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
421 if (vm == NULL || p_env == NULL) {
422 return JNI_ERR;
423 }
424
425 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
426 JavaVMAttachArgs args;
427 if (thr_args == NULL) {
428 // Allow the v1.1 calling convention.
429 args.version = JNI_VERSION_1_2;
430 args.name = NULL;
431 args.group = NULL; // TODO: get "main" thread group
432 } else {
433 args.version = in_args->version;
434 args.name = in_args->name;
435 if (in_args->group != NULL) {
436 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
437 args.group = NULL; // TODO: decode in_args->group
438 } else {
439 args.group = NULL; // TODO: get "main" thread group
440 }
441 }
442 CHECK_GE(args.version, JNI_VERSION_1_2);
443
444 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700445 runtime->AttachCurrentThread(args.name, as_daemon);
446 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700447 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700448}
449
Elliott Hughes79082e32011-08-25 12:07:32 -0700450class SharedLibrary {
451 public:
452 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
453 : path_(path),
454 handle_(handle),
455 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700456 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700457 jni_on_load_result_(kPending) {
458 pthread_cond_init(&jni_on_load_cond_, NULL);
459 }
460
461 ~SharedLibrary() {
462 delete jni_on_load_lock_;
463 }
464
465 Object* GetClassLoader() {
466 return class_loader_;
467 }
468
469 std::string GetPath() {
470 return path_;
471 }
472
473 /*
474 * Check the result of an earlier call to JNI_OnLoad on this library. If
475 * the call has not yet finished in another thread, wait for it.
476 */
477 bool CheckOnLoadResult(JavaVMExt* vm) {
478 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700479 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700480 // Check this so we don't end up waiting for ourselves. We need
481 // to return "true" so the caller can continue.
482 LOG(INFO) << *self << " recursive attempt to load library "
483 << "\"" << path_ << "\"";
484 return true;
485 }
486
487 MutexLock mu(jni_on_load_lock_);
488 while (jni_on_load_result_ == kPending) {
489 if (vm->verbose_jni) {
490 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
491 << "JNI_OnLoad...]";
492 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700493 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes79082e32011-08-25 12:07:32 -0700494 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
Elliott Hughes79082e32011-08-25 12:07:32 -0700495 }
496
497 bool okay = (jni_on_load_result_ == kOkay);
498 if (vm->verbose_jni) {
499 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
500 << (okay ? "succeeded" : "failed") << "]";
501 }
502 return okay;
503 }
504
505 void SetResult(bool result) {
506 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700507 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700508
509 // Broadcast a wakeup to anybody sleeping on the condition variable.
510 MutexLock mu(jni_on_load_lock_);
511 pthread_cond_broadcast(&jni_on_load_cond_);
512 }
513
514 void* FindSymbol(const std::string& symbol_name) {
515 return dlsym(handle_, symbol_name.c_str());
516 }
517
518 private:
519 enum JNI_OnLoadState {
520 kPending,
521 kFailed,
522 kOkay,
523 };
524
525 // Path to library "/system/lib/libjni.so".
526 std::string path_;
527
528 // The void* returned by dlopen(3).
529 void* handle_;
530
531 // The ClassLoader this library is associated with.
532 Object* class_loader_;
533
534 // Guards remaining items.
535 Mutex* jni_on_load_lock_;
536 // Wait for JNI_OnLoad in other thread.
537 pthread_cond_t jni_on_load_cond_;
538 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700539 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700540 // Result of earlier JNI_OnLoad call.
541 JNI_OnLoadState jni_on_load_result_;
542};
543
Elliott Hughescdf53122011-08-19 15:46:09 -0700544} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700545
Elliott Hughes79082e32011-08-25 12:07:32 -0700546// This exists mainly to keep implementation details out of the header file.
547class Libraries {
548 public:
549 Libraries() {
550 }
551
552 ~Libraries() {
553 // Delete our map values. (The keys will be cleaned up by the map itself.)
554 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
555 delete it->second;
556 }
557 }
558
559 SharedLibrary* Get(const std::string& path) {
560 return libraries_[path];
561 }
562
563 void Put(const std::string& path, SharedLibrary* library) {
564 libraries_[path] = library;
565 }
566
567 // See section 11.3 "Linking Native Methods" of the JNI spec.
568 void* FindNativeMethod(const Method* m) {
569 std::string jni_short_name(JniShortName(m));
570 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700571 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700572 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
573 SharedLibrary* library = it->second;
574 if (library->GetClassLoader() != declaring_class_loader) {
575 // We only search libraries loaded by the appropriate ClassLoader.
576 continue;
577 }
578 // Try the short name then the long name...
579 void* fn = library->FindSymbol(jni_short_name);
580 if (fn == NULL) {
581 fn = library->FindSymbol(jni_long_name);
582 }
583 if (fn != NULL) {
584 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700585 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700586 << " in \"" << library->GetPath() << "\"]";
587 }
588 return fn;
589 }
590 }
591 std::string detail;
592 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700593 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700594 LOG(ERROR) << detail;
595 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
596 "%s", detail.c_str());
597 return NULL;
598 }
599
600 private:
601 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
602
603 std::map<std::string, SharedLibrary*> libraries_;
604};
605
Elliott Hughescdf53122011-08-19 15:46:09 -0700606class JNI {
607 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700608
Elliott Hughescdf53122011-08-19 15:46:09 -0700609 static jint GetVersion(JNIEnv* env) {
610 ScopedJniThreadState ts(env);
611 return JNI_VERSION_1_6;
612 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700613
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
615 ScopedJniThreadState ts(env);
616 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700617 return NULL;
618 }
619
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 static jclass FindClass(JNIEnv* env, const char* name) {
621 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700622 Runtime* runtime = Runtime::Current();
623 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700624 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700625 Class* c = NULL;
626 if (runtime->IsStarted()) {
627 // TODO: need to get the appropriate ClassLoader.
628 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
629 c = class_linker->FindClass(descriptor, cl);
630 } else {
631 c = class_linker->FindSystemClass(descriptor);
632 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700633 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700634 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700635
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
637 ScopedJniThreadState ts(env);
638 Method* method = Decode<Method*>(ts, java_method);
639 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700640 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700641
Elliott Hughescdf53122011-08-19 15:46:09 -0700642 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
643 ScopedJniThreadState ts(env);
644 Field* field = Decode<Field*>(ts, java_field);
645 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
646 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700647
Elliott Hughescdf53122011-08-19 15:46:09 -0700648 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
649 ScopedJniThreadState ts(env);
650 Method* method = DecodeMethod(ts, mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700651 return AddLocalReference<jobject>(env, method);
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 ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
655 ScopedJniThreadState ts(env);
656 Field* field = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700657 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700658 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700659
Elliott Hughes37f7a402011-08-22 18:56:01 -0700660 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
661 ScopedJniThreadState ts(env);
662 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700663 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700664 }
665
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700666 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700668 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700669 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700671
Elliott Hughes37f7a402011-08-22 18:56:01 -0700672 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700674 Class* c1 = Decode<Class*>(ts, java_class1);
675 Class* c2 = Decode<Class*>(ts, java_class2);
676 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700677 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700678
Elliott Hughes37f7a402011-08-22 18:56:01 -0700679 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700681 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700682 if (jobj == NULL) {
683 // NB. JNI is different from regular Java instanceof in this respect
684 return JNI_TRUE;
685 } else {
686 Object* obj = Decode<Object*>(ts, jobj);
687 Class* klass = Decode<Class*>(ts, clazz);
688 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
689 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700691
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 static jint Throw(JNIEnv* env, jthrowable java_exception) {
693 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700694 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700695 if (exception == NULL) {
696 return JNI_ERR;
697 }
698 ts.Self()->SetException(exception);
699 return JNI_OK;
700 }
701
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700702 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700703 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700704 // TODO: check for a pending exception to decide what constructor to call.
705 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
706 if (mid == NULL) {
707 return JNI_ERR;
708 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700709 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
710 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700711 return JNI_ERR;
712 }
713
714 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700715 args[0].l = s.get();
716 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
717 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700718 return JNI_ERR;
719 }
720
Elliott Hughes72025e52011-08-23 17:50:30 -0700721 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700722 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700723 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700724
Elliott Hughes37f7a402011-08-22 18:56:01 -0700725 return JNI_OK;
726 }
727
728 static jboolean ExceptionCheck(JNIEnv* env) {
729 ScopedJniThreadState ts(env);
730 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
731 }
732
733 static void ExceptionClear(JNIEnv* env) {
734 ScopedJniThreadState ts(env);
735 ts.Self()->ClearException();
736 }
737
738 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700740
741 Thread* self = ts.Self();
742 Throwable* original_exception = self->GetException();
743 self->ClearException();
744
Elliott Hughesbf86d042011-08-31 17:53:14 -0700745 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700746 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
747 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
748 if (mid == NULL) {
749 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
750 << PrettyType(original_exception);
751 } else {
752 env->CallVoidMethod(exception.get(), mid);
753 if (self->IsExceptionPending()) {
754 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
755 << " thrown while calling printStackTrace";
756 self->ClearException();
757 }
758 }
759
760 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700762
Elliott Hughescdf53122011-08-19 15:46:09 -0700763 static jthrowable ExceptionOccurred(JNIEnv* env) {
764 ScopedJniThreadState ts(env);
765 Object* exception = ts.Self()->GetException();
766 if (exception == NULL) {
767 return NULL;
768 } else {
769 // TODO: if adding a local reference failing causes the VM to abort
770 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700771 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 if (localException == NULL) {
773 // We were unable to add a new local reference, and threw a new
774 // exception. We can't return "exception", because it's not a
775 // local reference. So we have to return NULL, indicating that
776 // there was no exception, even though it's pretty much raining
777 // exceptions in here.
778 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
779 }
780 return localException;
781 }
782 }
783
Elliott Hughescdf53122011-08-19 15:46:09 -0700784 static void FatalError(JNIEnv* env, const char* msg) {
785 ScopedJniThreadState ts(env);
786 LOG(FATAL) << "JNI FatalError called: " << msg;
787 }
788
789 static jint PushLocalFrame(JNIEnv* env, jint cap) {
790 ScopedJniThreadState ts(env);
791 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
792 return JNI_OK;
793 }
794
795 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
796 ScopedJniThreadState ts(env);
797 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
798 return res;
799 }
800
Elliott Hughes72025e52011-08-23 17:50:30 -0700801 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
802 ScopedJniThreadState ts(env);
803 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
804 return 0;
805 }
806
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
808 ScopedJniThreadState ts(env);
809 if (obj == NULL) {
810 return NULL;
811 }
812
Elliott Hughes75770752011-08-24 17:52:38 -0700813 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 IndirectReferenceTable& globals = vm->globals;
815 MutexLock mu(vm->globals_lock);
816 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
817 return reinterpret_cast<jobject>(ref);
818 }
819
820 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
821 ScopedJniThreadState ts(env);
822 if (obj == NULL) {
823 return;
824 }
825
Elliott Hughes75770752011-08-24 17:52:38 -0700826 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 IndirectReferenceTable& globals = vm->globals;
828 MutexLock mu(vm->globals_lock);
829
830 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
831 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
832 << "failed to find entry";
833 }
834 }
835
836 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
837 ScopedJniThreadState ts(env);
838 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
839 }
840
841 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
842 ScopedJniThreadState ts(env);
843 if (obj == NULL) {
844 return;
845 }
846
Elliott Hughes75770752011-08-24 17:52:38 -0700847 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 IndirectReferenceTable& weak_globals = vm->weak_globals;
849 MutexLock mu(vm->weak_globals_lock);
850
851 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
852 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
853 << "failed to find entry";
854 }
855 }
856
857 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
858 ScopedJniThreadState ts(env);
859 if (obj == NULL) {
860 return NULL;
861 }
862
863 IndirectReferenceTable& locals = ts.Env()->locals;
864
865 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
866 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
867 return reinterpret_cast<jobject>(ref);
868 }
869
870 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
871 ScopedJniThreadState ts(env);
872 if (obj == NULL) {
873 return;
874 }
875
876 IndirectReferenceTable& locals = ts.Env()->locals;
877
878 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
879 if (!locals.Remove(cookie, obj)) {
880 // Attempting to delete a local reference that is not in the
881 // topmost local reference frame is a no-op. DeleteLocalRef returns
882 // void and doesn't throw any exceptions, but we should probably
883 // complain about it so the user will notice that things aren't
884 // going quite the way they expect.
885 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
886 << "failed to find entry";
887 }
888 }
889
890 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
891 ScopedJniThreadState ts(env);
892 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
893 ? JNI_TRUE : JNI_FALSE;
894 }
895
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700896 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700898 Class* c = Decode<Class*>(ts, java_class);
899 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
900 return NULL;
901 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700902 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 }
904
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 ScopedJniThreadState ts(env);
907 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700908 va_start(args, mid);
909 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 va_end(args);
911 return result;
912 }
913
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700916 Class* c = Decode<Class*>(ts, java_class);
917 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
918 return NULL;
919 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700920 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700921 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 return local_result;
924 }
925
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700928 Class* c = Decode<Class*>(ts, java_class);
929 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
930 return NULL;
931 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700932 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700933 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 return local_result;
936 }
937
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
939 ScopedJniThreadState ts(env);
940 return FindMethodID(ts, c, name, sig, false);
941 }
942
943 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
944 ScopedJniThreadState ts(env);
945 return FindMethodID(ts, c, name, sig, true);
946 }
947
Elliott Hughes72025e52011-08-23 17:50:30 -0700948 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 va_list ap;
951 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700952 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700954 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 }
956
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700959 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
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 CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700965 JValue result = InvokeVirtualOrInterfaceWithJValues(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 jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 va_list ap;
972 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700973 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 va_end(ap);
975 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700980 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700985 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 va_list ap;
991 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700992 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_end(ap);
994 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700999 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001004 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 va_list ap;
1010 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001011 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 va_end(ap);
1013 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001018 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001023 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 va_list ap;
1029 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001030 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 va_end(ap);
1032 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001037 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001042 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 va_list ap;
1048 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001049 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 va_end(ap);
1051 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001056 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001061 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 va_list ap;
1067 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001068 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_end(ap);
1070 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001075 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001080 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 va_list ap;
1086 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001087 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 va_end(ap);
1089 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001094 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001099 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 va_list ap;
1105 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001106 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 va_end(ap);
1108 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001113 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001118 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_list ap;
1124 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001125 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001131 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001136 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
1139 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 ScopedJniThreadState ts(env);
1142 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 va_start(ap, mid);
1144 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001145 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 va_end(ap);
1147 return local_result;
1148 }
1149
1150 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001154 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
1157 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001161 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
1164 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 ScopedJniThreadState ts(env);
1167 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 va_start(ap, mid);
1169 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 va_end(ap);
1171 return result.z;
1172 }
1173
1174 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
1180 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
1186 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
1189 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 va_start(ap, mid);
1191 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 va_end(ap);
1193 return result.b;
1194 }
1195
1196 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
1202 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
1208 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 ScopedJniThreadState ts(env);
1211 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 va_start(ap, mid);
1213 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 va_end(ap);
1215 return result.c;
1216 }
1217
1218 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
1224 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
1230 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
1233 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 va_start(ap, mid);
1235 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 va_end(ap);
1237 return result.s;
1238 }
1239
1240 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
1246 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
1252 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 ScopedJniThreadState ts(env);
1255 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001256 va_start(ap, mid);
1257 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 va_end(ap);
1259 return result.i;
1260 }
1261
1262 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
1268 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 }
1273
1274 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 ScopedJniThreadState ts(env);
1277 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 va_start(ap, mid);
1279 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 va_end(ap);
1281 return result.j;
1282 }
1283
1284 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
1290 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
1296 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 ScopedJniThreadState ts(env);
1299 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001300 va_start(ap, mid);
1301 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 va_end(ap);
1303 return result.f;
1304 }
1305
1306 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
1312 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
1318 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001319 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 ScopedJniThreadState ts(env);
1321 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001322 va_start(ap, mid);
1323 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 va_end(ap);
1325 return result.d;
1326 }
1327
1328 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001329 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
1333
1334 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 }
1339
1340 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 ScopedJniThreadState ts(env);
1343 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 va_start(ap, mid);
1345 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 va_end(ap);
1347 }
1348
1349 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001350 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001352 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 }
1354
1355 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001356 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001358 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 }
1360
1361 static jfieldID GetFieldID(JNIEnv* env,
1362 jclass c, const char* name, const char* sig) {
1363 ScopedJniThreadState ts(env);
1364 return FindFieldID(ts, c, name, sig, false);
1365 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001366
1367
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 static jfieldID GetStaticFieldID(JNIEnv* env,
1369 jclass c, const char* name, const char* sig) {
1370 ScopedJniThreadState ts(env);
1371 return FindFieldID(ts, c, name, sig, true);
1372 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001373
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001374 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001376 Object* o = Decode<Object*>(ts, obj);
1377 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001378 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001380
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001383 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001384 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 }
1386
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001387 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389 Object* o = Decode<Object*>(ts, java_object);
1390 Object* v = Decode<Object*>(ts, java_value);
1391 Field* f = DecodeField(ts, fid);
1392 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001395 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001397 Object* v = Decode<Object*>(ts, java_value);
1398 Field* f = DecodeField(ts, fid);
1399 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402#define GET_PRIMITIVE_FIELD(fn, instance) \
1403 ScopedJniThreadState ts(env); \
1404 Object* o = Decode<Object*>(ts, instance); \
1405 Field* f = DecodeField(ts, fid); \
1406 return f->fn(o)
1407
1408#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1409 ScopedJniThreadState ts(env); \
1410 Object* o = Decode<Object*>(ts, instance); \
1411 Field* f = DecodeField(ts, fid); \
1412 f->fn(o, value)
1413
1414 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1415 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
1417
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001418 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1419 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001422 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1423 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1427 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001430 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1431 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1435 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1439 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1443 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1447 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1451 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1455 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1459 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1463 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 }
1465
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001466 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1467 GET_PRIMITIVE_FIELD(GetLong, NULL);
1468 }
1469
1470 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1471 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1472 }
1473
1474 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1475 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1476 }
1477
1478 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1479 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1480 }
1481
1482 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1483 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1484 }
1485
1486 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1487 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1488 }
1489
1490 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1491 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1492 }
1493
1494 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1495 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1496 }
1497
1498 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1499 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1500 }
1501
1502 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1503 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1504 }
1505
1506 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1507 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1508 }
1509
1510 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1511 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1512 }
1513
1514 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1515 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1516 }
1517
1518 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1519 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1520 }
1521
1522 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1523 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1524 }
1525
1526 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1527 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1528 }
1529
1530 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1531 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1532 }
1533
1534 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1535 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1536 }
1537
1538 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1539 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
1542 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001543 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 ScopedJniThreadState ts(env);
1545 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001546 va_start(ap, mid);
1547 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001548 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 va_end(ap);
1550 return local_result;
1551 }
1552
1553 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001554 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001556 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001557 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 }
1559
1560 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001561 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001563 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001564 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 }
1566
1567 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001568 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 ScopedJniThreadState ts(env);
1570 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001571 va_start(ap, mid);
1572 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 va_end(ap);
1574 return result.z;
1575 }
1576
1577 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001578 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001580 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
1583 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001584 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001586 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
Elliott Hughes72025e52011-08-23 17:50:30 -07001589 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 ScopedJniThreadState ts(env);
1591 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001592 va_start(ap, mid);
1593 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 va_end(ap);
1595 return result.b;
1596 }
1597
1598 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001599 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001601 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
1604 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001607 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 }
1609
Elliott Hughes72025e52011-08-23 17:50:30 -07001610 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 ScopedJniThreadState ts(env);
1612 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 va_start(ap, mid);
1614 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 va_end(ap);
1616 return result.c;
1617 }
1618
1619 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001620 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001622 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
1625 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001628 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes72025e52011-08-23 17:50:30 -07001631 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 ScopedJniThreadState ts(env);
1633 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 va_start(ap, mid);
1635 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 va_end(ap);
1637 return result.s;
1638 }
1639
1640 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001641 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001643 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
1645
1646 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001649 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 }
1651
Elliott Hughes72025e52011-08-23 17:50:30 -07001652 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 ScopedJniThreadState ts(env);
1654 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001655 va_start(ap, mid);
1656 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 va_end(ap);
1658 return result.i;
1659 }
1660
1661 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001662 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001664 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
1667 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 }
1672
Elliott Hughes72025e52011-08-23 17:50:30 -07001673 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 ScopedJniThreadState ts(env);
1675 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001676 va_start(ap, mid);
1677 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 va_end(ap);
1679 return result.j;
1680 }
1681
1682 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001683 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001685 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
1688 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 }
1693
Elliott Hughes72025e52011-08-23 17:50:30 -07001694 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 ScopedJniThreadState ts(env);
1696 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001697 va_start(ap, mid);
1698 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 va_end(ap);
1700 return result.f;
1701 }
1702
1703 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001704 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001706 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 }
1708
1709 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001712 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 }
1714
Elliott Hughes72025e52011-08-23 17:50:30 -07001715 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 ScopedJniThreadState ts(env);
1717 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001718 va_start(ap, mid);
1719 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 va_end(ap);
1721 return result.d;
1722 }
1723
1724 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001725 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001727 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
1730 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001733 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes72025e52011-08-23 17:50:30 -07001736 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 ScopedJniThreadState ts(env);
1738 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001739 va_start(ap, mid);
1740 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 va_end(ap);
1742 }
1743
1744 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001745 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
1750 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001751 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001753 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 }
1755
Elliott Hughes814e4032011-08-23 12:07:56 -07001756 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001758 if (chars == NULL && char_count == 0) {
1759 return NULL;
1760 }
1761 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001762 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 }
1764
1765 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1766 ScopedJniThreadState ts(env);
1767 if (utf == NULL) {
1768 return NULL;
1769 }
1770 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001771 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 }
1773
Elliott Hughes814e4032011-08-23 12:07:56 -07001774 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1775 ScopedJniThreadState ts(env);
1776 return Decode<String*>(ts, java_string)->GetLength();
1777 }
1778
1779 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1780 ScopedJniThreadState ts(env);
1781 return Decode<String*>(ts, java_string)->GetUtfLength();
1782 }
1783
Elliott Hughesb465ab02011-08-24 11:21:21 -07001784 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001786 String* s = Decode<String*>(ts, java_string);
1787 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1788 ThrowSIOOBE(ts, start, length, s->GetLength());
1789 } else {
1790 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1791 memcpy(buf, chars + start, length * sizeof(jchar));
1792 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001793 }
1794
Elliott Hughesb465ab02011-08-24 11:21:21 -07001795 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001796 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001797 String* s = Decode<String*>(ts, java_string);
1798 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1799 ThrowSIOOBE(ts, start, length, s->GetLength());
1800 } else {
1801 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1802 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1803 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001804 }
1805
Elliott Hughes75770752011-08-24 17:52:38 -07001806 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001807 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001808 String* s = Decode<String*>(ts, java_string);
1809 const CharArray* chars = s->GetCharArray();
1810 PinPrimitiveArray(ts, chars);
1811 if (is_copy != NULL) {
1812 *is_copy = JNI_FALSE;
1813 }
1814 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001815 }
1816
Elliott Hughes75770752011-08-24 17:52:38 -07001817 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001818 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001819 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
Elliott Hughes75770752011-08-24 17:52:38 -07001822 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001824 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 }
1826
Elliott Hughes75770752011-08-24 17:52:38 -07001827 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001829 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 }
1831
Elliott Hughes75770752011-08-24 17:52:38 -07001832 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001833 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001834 if (java_string == NULL) {
1835 return NULL;
1836 }
1837 if (is_copy != NULL) {
1838 *is_copy = JNI_TRUE;
1839 }
1840 String* s = Decode<String*>(ts, java_string);
1841 size_t byte_count = s->GetUtfLength();
1842 char* bytes = new char[byte_count + 1];
1843 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001844 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001845 return NULL;
1846 }
1847 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1848 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1849 bytes[byte_count] = '\0';
1850 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001851 }
1852
Elliott Hughes75770752011-08-24 17:52:38 -07001853 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001854 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001855 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001856 }
1857
Elliott Hughesbd935992011-08-22 11:59:34 -07001858 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001860 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001861 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001862 Array* array = obj->AsArray();
1863 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 }
1865
Elliott Hughes814e4032011-08-23 12:07:56 -07001866 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001868 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001869 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 }
1871
1872 static void SetObjectArrayElement(JNIEnv* env,
1873 jobjectArray java_array, jsize index, jobject java_value) {
1874 ScopedJniThreadState ts(env);
1875 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1876 Object* value = Decode<Object*>(ts, java_value);
1877 array->Set(index, value);
1878 }
1879
1880 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1881 ScopedJniThreadState ts(env);
1882 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1883 }
1884
1885 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1886 ScopedJniThreadState ts(env);
1887 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1888 }
1889
1890 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1891 ScopedJniThreadState ts(env);
1892 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1893 }
1894
1895 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1896 ScopedJniThreadState ts(env);
1897 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1898 }
1899
1900 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1901 ScopedJniThreadState ts(env);
1902 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1903 }
1904
1905 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1906 ScopedJniThreadState ts(env);
1907 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1908 }
1909
1910 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1911 ScopedJniThreadState ts(env);
1912 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1913 }
1914
1915 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1916 ScopedJniThreadState ts(env);
1917 CHECK_GE(length, 0); // TODO: ReportJniError
1918
1919 // Compute the array class corresponding to the given element class.
1920 Class* element_class = Decode<Class*>(ts, element_jclass);
1921 std::string descriptor;
1922 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001923 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001924
1925 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001926 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1927 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 return NULL;
1929 }
1930
Elliott Hughes75770752011-08-24 17:52:38 -07001931 // Allocate and initialize if necessary.
1932 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001934 if (initial_element != NULL) {
1935 Object* initial_object = Decode<Object*>(ts, initial_element);
1936 for (jsize i = 0; i < length; ++i) {
1937 result->Set(i, initial_object);
1938 }
1939 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001940 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
1943 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1944 ScopedJniThreadState ts(env);
1945 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1946 }
1947
Elliott Hughes75770752011-08-24 17:52:38 -07001948 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001949 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001950 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001951 }
1952
Elliott Hughes75770752011-08-24 17:52:38 -07001953 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001954 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001955 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001956 }
1957
Elliott Hughes75770752011-08-24 17:52:38 -07001958 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001960 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001961 }
1962
Elliott Hughes75770752011-08-24 17:52:38 -07001963 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001965 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Elliott Hughes75770752011-08-24 17:52:38 -07001968 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001970 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 }
1972
Elliott Hughes75770752011-08-24 17:52:38 -07001973 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001975 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Elliott Hughes75770752011-08-24 17:52:38 -07001978 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001980 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Elliott Hughes75770752011-08-24 17:52:38 -07001983 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001985 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Elliott Hughes75770752011-08-24 17:52:38 -07001988 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001990 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Elliott Hughes75770752011-08-24 17:52:38 -07001993 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001995 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes75770752011-08-24 17:52:38 -07001998 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002000 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes75770752011-08-24 17:52:38 -07002003 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002005 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes75770752011-08-24 17:52:38 -07002008 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002010 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002015 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes75770752011-08-24 17:52:38 -07002018 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002020 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes75770752011-08-24 17:52:38 -07002023 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002025 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes75770752011-08-24 17:52:38 -07002028 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002030 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes75770752011-08-24 17:52:38 -07002033 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002035 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes814e4032011-08-23 12:07:56 -07002038 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002040 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes814e4032011-08-23 12:07:56 -07002043 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002045 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002070 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes5174fe62011-08-23 15:12:35 -07002118 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002120 Class* c = Decode<Class*>(ts, java_class);
2121
Elliott Hughes5174fe62011-08-23 15:12:35 -07002122 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 const char* name = methods[i].name;
2124 const char* sig = methods[i].signature;
2125
2126 if (*sig == '!') {
2127 // TODO: fast jni. it's too noisy to log all these.
2128 ++sig;
2129 }
2130
Elliott Hughes5174fe62011-08-23 15:12:35 -07002131 Method* m = c->FindDirectMethod(name, sig);
2132 if (m == NULL) {
2133 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002135 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002137 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2139 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002140 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002142 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002144 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2146 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002147 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 return JNI_ERR;
2149 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150
Elliott Hughes75770752011-08-24 17:52:38 -07002151 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002152 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002153 }
2154
2155 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157 return JNI_OK;
2158 }
2159
Elliott Hughes5174fe62011-08-23 15:12:35 -07002160 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162 Class* c = Decode<Class*>(ts, java_class);
2163
Elliott Hughes75770752011-08-24 17:52:38 -07002164 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002165 LOG(INFO) << "[Unregistering JNI native methods for "
2166 << PrettyDescriptor(c->GetDescriptor()) << "]";
2167 }
2168
2169 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2170 Method* m = c->GetDirectMethod(i);
2171 if (m->IsNative()) {
2172 m->UnregisterNative();
2173 }
2174 }
2175 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2176 Method* m = c->GetVirtualMethod(i);
2177 if (m->IsNative()) {
2178 m->UnregisterNative();
2179 }
2180 }
2181
2182 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184
Elliott Hughes72025e52011-08-23 17:50:30 -07002185 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002187 Decode<Object*>(ts, java_object)->MonitorEnter();
2188 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Elliott Hughes72025e52011-08-23 17:50:30 -07002191 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 ScopedJniThreadState ts(env);
Elliott Hughes02b48d12011-09-07 17:15:51 -07002193 Decode<Object*>(ts, java_object)->MonitorExit();
Elliott Hughes72025e52011-08-23 17:50:30 -07002194 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
2197 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2198 ScopedJniThreadState ts(env);
2199 Runtime* runtime = Runtime::Current();
2200 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002201 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 } else {
2203 *vm = NULL;
2204 }
2205 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2206 }
2207
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2209 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002210
2211 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002212 CHECK(address != NULL); // TODO: ReportJniError
2213 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002214
2215 jclass buffer_class = GetDirectByteBufferClass(env);
2216 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2217 if (mid == NULL) {
2218 return NULL;
2219 }
2220
2221 // At the moment, the Java side is limited to 32 bits.
2222 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2223 CHECK_LE(capacity, 0xffffffff);
2224 jint address_arg = reinterpret_cast<jint>(address);
2225 jint capacity_arg = static_cast<jint>(capacity);
2226
2227 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2228 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Elliott Hughesb465ab02011-08-24 11:21:21 -07002231 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002233 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2234 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 }
2236
Elliott Hughesb465ab02011-08-24 11:21:21 -07002237 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002239 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2240 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 }
2242
Elliott Hughesb465ab02011-08-24 11:21:21 -07002243 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002245
Elliott Hughes75770752011-08-24 17:52:38 -07002246 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247
2248 // Do we definitely know what kind of reference this is?
2249 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2250 IndirectRefKind kind = GetIndirectRefKind(ref);
2251 switch (kind) {
2252 case kLocal:
2253 return JNILocalRefType;
2254 case kGlobal:
2255 return JNIGlobalRefType;
2256 case kWeakGlobal:
2257 return JNIWeakGlobalRefType;
2258 case kSirtOrInvalid:
2259 // Is it in a stack IRT?
2260 if (ts.Self()->SirtContains(java_object)) {
2261 return JNILocalRefType;
2262 }
2263
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002264 if (!ts.Env()->work_around_app_jni_bugs) {
2265 return JNIInvalidRefType;
2266 }
2267
Elliott Hughesb465ab02011-08-24 11:21:21 -07002268 // If we're handing out direct pointers, check whether it's a direct pointer
2269 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002270 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002271 if (ts.Env()->locals.Contains(java_object)) {
2272 return JNILocalRefType;
2273 }
2274 }
2275
2276 return JNIInvalidRefType;
2277 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 }
2279};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002280
Elliott Hughesa2501992011-08-26 19:39:54 -07002281const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002282 NULL, // reserved0.
2283 NULL, // reserved1.
2284 NULL, // reserved2.
2285 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002286 JNI::GetVersion,
2287 JNI::DefineClass,
2288 JNI::FindClass,
2289 JNI::FromReflectedMethod,
2290 JNI::FromReflectedField,
2291 JNI::ToReflectedMethod,
2292 JNI::GetSuperclass,
2293 JNI::IsAssignableFrom,
2294 JNI::ToReflectedField,
2295 JNI::Throw,
2296 JNI::ThrowNew,
2297 JNI::ExceptionOccurred,
2298 JNI::ExceptionDescribe,
2299 JNI::ExceptionClear,
2300 JNI::FatalError,
2301 JNI::PushLocalFrame,
2302 JNI::PopLocalFrame,
2303 JNI::NewGlobalRef,
2304 JNI::DeleteGlobalRef,
2305 JNI::DeleteLocalRef,
2306 JNI::IsSameObject,
2307 JNI::NewLocalRef,
2308 JNI::EnsureLocalCapacity,
2309 JNI::AllocObject,
2310 JNI::NewObject,
2311 JNI::NewObjectV,
2312 JNI::NewObjectA,
2313 JNI::GetObjectClass,
2314 JNI::IsInstanceOf,
2315 JNI::GetMethodID,
2316 JNI::CallObjectMethod,
2317 JNI::CallObjectMethodV,
2318 JNI::CallObjectMethodA,
2319 JNI::CallBooleanMethod,
2320 JNI::CallBooleanMethodV,
2321 JNI::CallBooleanMethodA,
2322 JNI::CallByteMethod,
2323 JNI::CallByteMethodV,
2324 JNI::CallByteMethodA,
2325 JNI::CallCharMethod,
2326 JNI::CallCharMethodV,
2327 JNI::CallCharMethodA,
2328 JNI::CallShortMethod,
2329 JNI::CallShortMethodV,
2330 JNI::CallShortMethodA,
2331 JNI::CallIntMethod,
2332 JNI::CallIntMethodV,
2333 JNI::CallIntMethodA,
2334 JNI::CallLongMethod,
2335 JNI::CallLongMethodV,
2336 JNI::CallLongMethodA,
2337 JNI::CallFloatMethod,
2338 JNI::CallFloatMethodV,
2339 JNI::CallFloatMethodA,
2340 JNI::CallDoubleMethod,
2341 JNI::CallDoubleMethodV,
2342 JNI::CallDoubleMethodA,
2343 JNI::CallVoidMethod,
2344 JNI::CallVoidMethodV,
2345 JNI::CallVoidMethodA,
2346 JNI::CallNonvirtualObjectMethod,
2347 JNI::CallNonvirtualObjectMethodV,
2348 JNI::CallNonvirtualObjectMethodA,
2349 JNI::CallNonvirtualBooleanMethod,
2350 JNI::CallNonvirtualBooleanMethodV,
2351 JNI::CallNonvirtualBooleanMethodA,
2352 JNI::CallNonvirtualByteMethod,
2353 JNI::CallNonvirtualByteMethodV,
2354 JNI::CallNonvirtualByteMethodA,
2355 JNI::CallNonvirtualCharMethod,
2356 JNI::CallNonvirtualCharMethodV,
2357 JNI::CallNonvirtualCharMethodA,
2358 JNI::CallNonvirtualShortMethod,
2359 JNI::CallNonvirtualShortMethodV,
2360 JNI::CallNonvirtualShortMethodA,
2361 JNI::CallNonvirtualIntMethod,
2362 JNI::CallNonvirtualIntMethodV,
2363 JNI::CallNonvirtualIntMethodA,
2364 JNI::CallNonvirtualLongMethod,
2365 JNI::CallNonvirtualLongMethodV,
2366 JNI::CallNonvirtualLongMethodA,
2367 JNI::CallNonvirtualFloatMethod,
2368 JNI::CallNonvirtualFloatMethodV,
2369 JNI::CallNonvirtualFloatMethodA,
2370 JNI::CallNonvirtualDoubleMethod,
2371 JNI::CallNonvirtualDoubleMethodV,
2372 JNI::CallNonvirtualDoubleMethodA,
2373 JNI::CallNonvirtualVoidMethod,
2374 JNI::CallNonvirtualVoidMethodV,
2375 JNI::CallNonvirtualVoidMethodA,
2376 JNI::GetFieldID,
2377 JNI::GetObjectField,
2378 JNI::GetBooleanField,
2379 JNI::GetByteField,
2380 JNI::GetCharField,
2381 JNI::GetShortField,
2382 JNI::GetIntField,
2383 JNI::GetLongField,
2384 JNI::GetFloatField,
2385 JNI::GetDoubleField,
2386 JNI::SetObjectField,
2387 JNI::SetBooleanField,
2388 JNI::SetByteField,
2389 JNI::SetCharField,
2390 JNI::SetShortField,
2391 JNI::SetIntField,
2392 JNI::SetLongField,
2393 JNI::SetFloatField,
2394 JNI::SetDoubleField,
2395 JNI::GetStaticMethodID,
2396 JNI::CallStaticObjectMethod,
2397 JNI::CallStaticObjectMethodV,
2398 JNI::CallStaticObjectMethodA,
2399 JNI::CallStaticBooleanMethod,
2400 JNI::CallStaticBooleanMethodV,
2401 JNI::CallStaticBooleanMethodA,
2402 JNI::CallStaticByteMethod,
2403 JNI::CallStaticByteMethodV,
2404 JNI::CallStaticByteMethodA,
2405 JNI::CallStaticCharMethod,
2406 JNI::CallStaticCharMethodV,
2407 JNI::CallStaticCharMethodA,
2408 JNI::CallStaticShortMethod,
2409 JNI::CallStaticShortMethodV,
2410 JNI::CallStaticShortMethodA,
2411 JNI::CallStaticIntMethod,
2412 JNI::CallStaticIntMethodV,
2413 JNI::CallStaticIntMethodA,
2414 JNI::CallStaticLongMethod,
2415 JNI::CallStaticLongMethodV,
2416 JNI::CallStaticLongMethodA,
2417 JNI::CallStaticFloatMethod,
2418 JNI::CallStaticFloatMethodV,
2419 JNI::CallStaticFloatMethodA,
2420 JNI::CallStaticDoubleMethod,
2421 JNI::CallStaticDoubleMethodV,
2422 JNI::CallStaticDoubleMethodA,
2423 JNI::CallStaticVoidMethod,
2424 JNI::CallStaticVoidMethodV,
2425 JNI::CallStaticVoidMethodA,
2426 JNI::GetStaticFieldID,
2427 JNI::GetStaticObjectField,
2428 JNI::GetStaticBooleanField,
2429 JNI::GetStaticByteField,
2430 JNI::GetStaticCharField,
2431 JNI::GetStaticShortField,
2432 JNI::GetStaticIntField,
2433 JNI::GetStaticLongField,
2434 JNI::GetStaticFloatField,
2435 JNI::GetStaticDoubleField,
2436 JNI::SetStaticObjectField,
2437 JNI::SetStaticBooleanField,
2438 JNI::SetStaticByteField,
2439 JNI::SetStaticCharField,
2440 JNI::SetStaticShortField,
2441 JNI::SetStaticIntField,
2442 JNI::SetStaticLongField,
2443 JNI::SetStaticFloatField,
2444 JNI::SetStaticDoubleField,
2445 JNI::NewString,
2446 JNI::GetStringLength,
2447 JNI::GetStringChars,
2448 JNI::ReleaseStringChars,
2449 JNI::NewStringUTF,
2450 JNI::GetStringUTFLength,
2451 JNI::GetStringUTFChars,
2452 JNI::ReleaseStringUTFChars,
2453 JNI::GetArrayLength,
2454 JNI::NewObjectArray,
2455 JNI::GetObjectArrayElement,
2456 JNI::SetObjectArrayElement,
2457 JNI::NewBooleanArray,
2458 JNI::NewByteArray,
2459 JNI::NewCharArray,
2460 JNI::NewShortArray,
2461 JNI::NewIntArray,
2462 JNI::NewLongArray,
2463 JNI::NewFloatArray,
2464 JNI::NewDoubleArray,
2465 JNI::GetBooleanArrayElements,
2466 JNI::GetByteArrayElements,
2467 JNI::GetCharArrayElements,
2468 JNI::GetShortArrayElements,
2469 JNI::GetIntArrayElements,
2470 JNI::GetLongArrayElements,
2471 JNI::GetFloatArrayElements,
2472 JNI::GetDoubleArrayElements,
2473 JNI::ReleaseBooleanArrayElements,
2474 JNI::ReleaseByteArrayElements,
2475 JNI::ReleaseCharArrayElements,
2476 JNI::ReleaseShortArrayElements,
2477 JNI::ReleaseIntArrayElements,
2478 JNI::ReleaseLongArrayElements,
2479 JNI::ReleaseFloatArrayElements,
2480 JNI::ReleaseDoubleArrayElements,
2481 JNI::GetBooleanArrayRegion,
2482 JNI::GetByteArrayRegion,
2483 JNI::GetCharArrayRegion,
2484 JNI::GetShortArrayRegion,
2485 JNI::GetIntArrayRegion,
2486 JNI::GetLongArrayRegion,
2487 JNI::GetFloatArrayRegion,
2488 JNI::GetDoubleArrayRegion,
2489 JNI::SetBooleanArrayRegion,
2490 JNI::SetByteArrayRegion,
2491 JNI::SetCharArrayRegion,
2492 JNI::SetShortArrayRegion,
2493 JNI::SetIntArrayRegion,
2494 JNI::SetLongArrayRegion,
2495 JNI::SetFloatArrayRegion,
2496 JNI::SetDoubleArrayRegion,
2497 JNI::RegisterNatives,
2498 JNI::UnregisterNatives,
2499 JNI::MonitorEnter,
2500 JNI::MonitorExit,
2501 JNI::GetJavaVM,
2502 JNI::GetStringRegion,
2503 JNI::GetStringUTFRegion,
2504 JNI::GetPrimitiveArrayCritical,
2505 JNI::ReleasePrimitiveArrayCritical,
2506 JNI::GetStringCritical,
2507 JNI::ReleaseStringCritical,
2508 JNI::NewWeakGlobalRef,
2509 JNI::DeleteWeakGlobalRef,
2510 JNI::ExceptionCheck,
2511 JNI::NewDirectByteBuffer,
2512 JNI::GetDirectBufferAddress,
2513 JNI::GetDirectBufferCapacity,
2514 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002515};
2516
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002517static const size_t kMonitorsInitial = 32; // Arbitrary.
2518static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2519
2520static const size_t kLocalsInitial = 64; // Arbitrary.
2521static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002522
Elliott Hughes75770752011-08-24 17:52:38 -07002523JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002524 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002525 vm(vm),
2526 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002527 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002528 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002529 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2530 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002531 functions = unchecked_functions = &gNativeInterface;
2532 if (check_jni) {
2533 functions = GetCheckJniNativeInterface();
2534 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002535}
2536
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002537JNIEnvExt::~JNIEnvExt() {
2538}
2539
Carl Shapiroea4dca82011-08-01 13:45:38 -07002540// JNI Invocation interface.
2541
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002542extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2543 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2544 if (args->version < JNI_VERSION_1_2) {
2545 return JNI_EVERSION;
2546 }
2547 Runtime::Options options;
2548 for (int i = 0; i < args->nOptions; ++i) {
2549 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002550 options.push_back(std::make_pair(StringPiece(option->optionString),
2551 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002552 }
2553 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002554 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002555 if (runtime == NULL) {
2556 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002557 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002558 runtime->Start();
2559 *p_env = Thread::Current()->GetJniEnv();
2560 *p_vm = runtime->GetJavaVM();
2561 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002562}
2563
Elliott Hughesf2682d52011-08-15 16:37:04 -07002564extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002565 Runtime* runtime = Runtime::Current();
2566 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002567 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002568 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002569 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002570 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002571 }
2572 return JNI_OK;
2573}
2574
2575// Historically unsupported.
2576extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2577 return JNI_ERR;
2578}
2579
Elliott Hughescdf53122011-08-19 15:46:09 -07002580class JII {
2581 public:
2582 static jint DestroyJavaVM(JavaVM* vm) {
2583 if (vm == NULL) {
2584 return JNI_ERR;
2585 } else {
2586 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2587 delete raw_vm->runtime;
2588 return JNI_OK;
2589 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002590 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002591
Elliott Hughescdf53122011-08-19 15:46:09 -07002592 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002593 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002594 }
2595
2596 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002597 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002598 }
2599
2600 static jint DetachCurrentThread(JavaVM* vm) {
2601 if (vm == NULL) {
2602 return JNI_ERR;
2603 } else {
2604 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2605 Runtime* runtime = raw_vm->runtime;
2606 runtime->DetachCurrentThread();
2607 return JNI_OK;
2608 }
2609 }
2610
2611 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2612 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2613 return JNI_EVERSION;
2614 }
2615 if (vm == NULL || env == NULL) {
2616 return JNI_ERR;
2617 }
2618 Thread* thread = Thread::Current();
2619 if (thread == NULL) {
2620 *env = NULL;
2621 return JNI_EDETACHED;
2622 }
2623 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002624 return JNI_OK;
2625 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002626};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002627
Elliott Hughesa2501992011-08-26 19:39:54 -07002628const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002629 NULL, // reserved0
2630 NULL, // reserved1
2631 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002632 JII::DestroyJavaVM,
2633 JII::AttachCurrentThread,
2634 JII::DetachCurrentThread,
2635 JII::GetEnv,
2636 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002637};
2638
Elliott Hughesbbd76712011-08-17 10:25:24 -07002639static const size_t kPinTableInitialSize = 16;
2640static const size_t kPinTableMaxSize = 1024;
2641
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002642static const size_t kGlobalsInitial = 512; // Arbitrary.
2643static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2644
2645static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2646static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2647
Elliott Hughesa0957642011-09-02 14:27:33 -07002648JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002649 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002650 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002651 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002652 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002653 verbose_jni(options->IsVerbose("jni")),
2654 log_third_party_jni(options->IsVerbose("third-party-jni")),
2655 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002656 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes75770752011-08-24 17:52:38 -07002657 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002658 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002659 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002660 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002661 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002662 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2663 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2664 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002665 functions = unchecked_functions = &gInvokeInterface;
2666 if (check_jni) {
2667 functions = GetCheckJniInvokeInterface();
2668 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002669}
2670
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002671JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002672 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002673 delete globals_lock;
2674 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002675 delete libraries_lock;
2676 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
2740 // want to switch from RUNNING to VMWAIT while it executes. This allows
2741 // the GC to ignore us.
2742 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002743 void* handle = NULL;
2744 {
2745 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
2746 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}