blob: 5ea25e899fa0a4965522e5d46dc0ecc8acfdcede [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 Hughesd92bec42011-09-02 17:04:36 -0700445 runtime->AttachCurrentThread(args.name, p_env, as_daemon);
446 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700447}
448
Elliott Hughes79082e32011-08-25 12:07:32 -0700449class SharedLibrary {
450 public:
451 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
452 : path_(path),
453 handle_(handle),
454 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700455 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700456 jni_on_load_result_(kPending) {
457 pthread_cond_init(&jni_on_load_cond_, NULL);
458 }
459
460 ~SharedLibrary() {
461 delete jni_on_load_lock_;
462 }
463
464 Object* GetClassLoader() {
465 return class_loader_;
466 }
467
468 std::string GetPath() {
469 return path_;
470 }
471
472 /*
473 * Check the result of an earlier call to JNI_OnLoad on this library. If
474 * the call has not yet finished in another thread, wait for it.
475 */
476 bool CheckOnLoadResult(JavaVMExt* vm) {
477 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700478 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700479 // Check this so we don't end up waiting for ourselves. We need
480 // to return "true" so the caller can continue.
481 LOG(INFO) << *self << " recursive attempt to load library "
482 << "\"" << path_ << "\"";
483 return true;
484 }
485
486 MutexLock mu(jni_on_load_lock_);
487 while (jni_on_load_result_ == kPending) {
488 if (vm->verbose_jni) {
489 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
490 << "JNI_OnLoad...]";
491 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700492 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes79082e32011-08-25 12:07:32 -0700493 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
Elliott Hughes79082e32011-08-25 12:07:32 -0700494 }
495
496 bool okay = (jni_on_load_result_ == kOkay);
497 if (vm->verbose_jni) {
498 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
499 << (okay ? "succeeded" : "failed") << "]";
500 }
501 return okay;
502 }
503
504 void SetResult(bool result) {
505 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700506 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700507
508 // Broadcast a wakeup to anybody sleeping on the condition variable.
509 MutexLock mu(jni_on_load_lock_);
510 pthread_cond_broadcast(&jni_on_load_cond_);
511 }
512
513 void* FindSymbol(const std::string& symbol_name) {
514 return dlsym(handle_, symbol_name.c_str());
515 }
516
517 private:
518 enum JNI_OnLoadState {
519 kPending,
520 kFailed,
521 kOkay,
522 };
523
524 // Path to library "/system/lib/libjni.so".
525 std::string path_;
526
527 // The void* returned by dlopen(3).
528 void* handle_;
529
530 // The ClassLoader this library is associated with.
531 Object* class_loader_;
532
533 // Guards remaining items.
534 Mutex* jni_on_load_lock_;
535 // Wait for JNI_OnLoad in other thread.
536 pthread_cond_t jni_on_load_cond_;
537 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700538 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700539 // Result of earlier JNI_OnLoad call.
540 JNI_OnLoadState jni_on_load_result_;
541};
542
Elliott Hughescdf53122011-08-19 15:46:09 -0700543} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700544
Elliott Hughes79082e32011-08-25 12:07:32 -0700545// This exists mainly to keep implementation details out of the header file.
546class Libraries {
547 public:
548 Libraries() {
549 }
550
551 ~Libraries() {
552 // Delete our map values. (The keys will be cleaned up by the map itself.)
553 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
554 delete it->second;
555 }
556 }
557
558 SharedLibrary* Get(const std::string& path) {
559 return libraries_[path];
560 }
561
562 void Put(const std::string& path, SharedLibrary* library) {
563 libraries_[path] = library;
564 }
565
566 // See section 11.3 "Linking Native Methods" of the JNI spec.
567 void* FindNativeMethod(const Method* m) {
568 std::string jni_short_name(JniShortName(m));
569 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700570 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700571 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
572 SharedLibrary* library = it->second;
573 if (library->GetClassLoader() != declaring_class_loader) {
574 // We only search libraries loaded by the appropriate ClassLoader.
575 continue;
576 }
577 // Try the short name then the long name...
578 void* fn = library->FindSymbol(jni_short_name);
579 if (fn == NULL) {
580 fn = library->FindSymbol(jni_long_name);
581 }
582 if (fn != NULL) {
583 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700584 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700585 << " in \"" << library->GetPath() << "\"]";
586 }
587 return fn;
588 }
589 }
590 std::string detail;
591 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700592 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700593 LOG(ERROR) << detail;
594 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
595 "%s", detail.c_str());
596 return NULL;
597 }
598
599 private:
600 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
601
602 std::map<std::string, SharedLibrary*> libraries_;
603};
604
Elliott Hughescdf53122011-08-19 15:46:09 -0700605class JNI {
606 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700607
Elliott Hughescdf53122011-08-19 15:46:09 -0700608 static jint GetVersion(JNIEnv* env) {
609 ScopedJniThreadState ts(env);
610 return JNI_VERSION_1_6;
611 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700612
Elliott Hughescdf53122011-08-19 15:46:09 -0700613 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
614 ScopedJniThreadState ts(env);
615 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700616 return NULL;
617 }
618
Elliott Hughescdf53122011-08-19 15:46:09 -0700619 static jclass FindClass(JNIEnv* env, const char* name) {
620 ScopedJniThreadState ts(env);
621 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
622 std::string descriptor(NormalizeJniClassDescriptor(name));
623 // TODO: need to get the appropriate ClassLoader.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700624 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700625 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700626 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700627 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700628
Elliott Hughescdf53122011-08-19 15:46:09 -0700629 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
630 ScopedJniThreadState ts(env);
631 Method* method = Decode<Method*>(ts, java_method);
632 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700633 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700634
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
636 ScopedJniThreadState ts(env);
637 Field* field = Decode<Field*>(ts, java_field);
638 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
639 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700640
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
642 ScopedJniThreadState ts(env);
643 Method* method = DecodeMethod(ts, mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700644 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700646
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
648 ScopedJniThreadState ts(env);
649 Field* field = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700650 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700652
Elliott Hughes37f7a402011-08-22 18:56:01 -0700653 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
654 ScopedJniThreadState ts(env);
655 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700656 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700657 }
658
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700659 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700661 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700662 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700664
Elliott Hughes37f7a402011-08-22 18:56:01 -0700665 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700667 Class* c1 = Decode<Class*>(ts, java_class1);
668 Class* c2 = Decode<Class*>(ts, java_class2);
669 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
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 IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700674 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700675 if (jobj == NULL) {
676 // NB. JNI is different from regular Java instanceof in this respect
677 return JNI_TRUE;
678 } else {
679 Object* obj = Decode<Object*>(ts, jobj);
680 Class* klass = Decode<Class*>(ts, clazz);
681 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
682 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700684
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 static jint Throw(JNIEnv* env, jthrowable java_exception) {
686 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700687 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 if (exception == NULL) {
689 return JNI_ERR;
690 }
691 ts.Self()->SetException(exception);
692 return JNI_OK;
693 }
694
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700695 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700696 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700697 // TODO: check for a pending exception to decide what constructor to call.
698 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
699 if (mid == NULL) {
700 return JNI_ERR;
701 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700702 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
703 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700704 return JNI_ERR;
705 }
706
707 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700708 args[0].l = s.get();
709 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
710 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700711 return JNI_ERR;
712 }
713
Elliott Hughes72025e52011-08-23 17:50:30 -0700714 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700715 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700716 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700717
Elliott Hughes37f7a402011-08-22 18:56:01 -0700718 return JNI_OK;
719 }
720
721 static jboolean ExceptionCheck(JNIEnv* env) {
722 ScopedJniThreadState ts(env);
723 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
724 }
725
726 static void ExceptionClear(JNIEnv* env) {
727 ScopedJniThreadState ts(env);
728 ts.Self()->ClearException();
729 }
730
731 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700732 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700733
734 Thread* self = ts.Self();
735 Throwable* original_exception = self->GetException();
736 self->ClearException();
737
Elliott Hughesbf86d042011-08-31 17:53:14 -0700738 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700739 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
740 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
741 if (mid == NULL) {
742 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
743 << PrettyType(original_exception);
744 } else {
745 env->CallVoidMethod(exception.get(), mid);
746 if (self->IsExceptionPending()) {
747 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
748 << " thrown while calling printStackTrace";
749 self->ClearException();
750 }
751 }
752
753 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700755
Elliott Hughescdf53122011-08-19 15:46:09 -0700756 static jthrowable ExceptionOccurred(JNIEnv* env) {
757 ScopedJniThreadState ts(env);
758 Object* exception = ts.Self()->GetException();
759 if (exception == NULL) {
760 return NULL;
761 } else {
762 // TODO: if adding a local reference failing causes the VM to abort
763 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700764 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 if (localException == NULL) {
766 // We were unable to add a new local reference, and threw a new
767 // exception. We can't return "exception", because it's not a
768 // local reference. So we have to return NULL, indicating that
769 // there was no exception, even though it's pretty much raining
770 // exceptions in here.
771 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
772 }
773 return localException;
774 }
775 }
776
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 static void FatalError(JNIEnv* env, const char* msg) {
778 ScopedJniThreadState ts(env);
779 LOG(FATAL) << "JNI FatalError called: " << msg;
780 }
781
782 static jint PushLocalFrame(JNIEnv* env, jint cap) {
783 ScopedJniThreadState ts(env);
784 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
785 return JNI_OK;
786 }
787
788 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
789 ScopedJniThreadState ts(env);
790 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
791 return res;
792 }
793
Elliott Hughes72025e52011-08-23 17:50:30 -0700794 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
795 ScopedJniThreadState ts(env);
796 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
797 return 0;
798 }
799
Elliott Hughescdf53122011-08-19 15:46:09 -0700800 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
801 ScopedJniThreadState ts(env);
802 if (obj == NULL) {
803 return NULL;
804 }
805
Elliott Hughes75770752011-08-24 17:52:38 -0700806 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 IndirectReferenceTable& globals = vm->globals;
808 MutexLock mu(vm->globals_lock);
809 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
810 return reinterpret_cast<jobject>(ref);
811 }
812
813 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
814 ScopedJniThreadState ts(env);
815 if (obj == NULL) {
816 return;
817 }
818
Elliott Hughes75770752011-08-24 17:52:38 -0700819 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 IndirectReferenceTable& globals = vm->globals;
821 MutexLock mu(vm->globals_lock);
822
823 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
824 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
825 << "failed to find entry";
826 }
827 }
828
829 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
830 ScopedJniThreadState ts(env);
831 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
832 }
833
834 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
835 ScopedJniThreadState ts(env);
836 if (obj == NULL) {
837 return;
838 }
839
Elliott Hughes75770752011-08-24 17:52:38 -0700840 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 IndirectReferenceTable& weak_globals = vm->weak_globals;
842 MutexLock mu(vm->weak_globals_lock);
843
844 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
845 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
846 << "failed to find entry";
847 }
848 }
849
850 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
851 ScopedJniThreadState ts(env);
852 if (obj == NULL) {
853 return NULL;
854 }
855
856 IndirectReferenceTable& locals = ts.Env()->locals;
857
858 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
859 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
860 return reinterpret_cast<jobject>(ref);
861 }
862
863 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
864 ScopedJniThreadState ts(env);
865 if (obj == NULL) {
866 return;
867 }
868
869 IndirectReferenceTable& locals = ts.Env()->locals;
870
871 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
872 if (!locals.Remove(cookie, obj)) {
873 // Attempting to delete a local reference that is not in the
874 // topmost local reference frame is a no-op. DeleteLocalRef returns
875 // void and doesn't throw any exceptions, but we should probably
876 // complain about it so the user will notice that things aren't
877 // going quite the way they expect.
878 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
879 << "failed to find entry";
880 }
881 }
882
883 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
884 ScopedJniThreadState ts(env);
885 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
886 ? JNI_TRUE : JNI_FALSE;
887 }
888
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700889 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700891 Class* c = Decode<Class*>(ts, java_class);
892 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
893 return NULL;
894 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700895 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 }
897
Elliott Hughes72025e52011-08-23 17:50:30 -0700898 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700899 ScopedJniThreadState ts(env);
900 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700901 va_start(args, mid);
902 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 va_end(args);
904 return result;
905 }
906
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700909 Class* c = Decode<Class*>(ts, java_class);
910 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
911 return NULL;
912 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700913 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700914 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 return local_result;
917 }
918
Elliott Hughes72025e52011-08-23 17:50:30 -0700919 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700921 Class* c = Decode<Class*>(ts, java_class);
922 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
923 return NULL;
924 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700925 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700926 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 return local_result;
929 }
930
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
932 ScopedJniThreadState ts(env);
933 return FindMethodID(ts, c, name, sig, false);
934 }
935
936 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
937 ScopedJniThreadState ts(env);
938 return FindMethodID(ts, c, name, sig, true);
939 }
940
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 va_list ap;
944 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700945 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700947 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700952 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700953 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 }
955
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700958 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700959 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 }
961
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 va_list ap;
965 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700966 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 va_end(ap);
968 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 }
970
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700973 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700978 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 va_list ap;
984 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700985 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 va_end(ap);
987 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700992 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 }
994
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700997 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 }
999
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001001 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 va_list ap;
1003 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001004 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 va_end(ap);
1006 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001011 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001016 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 va_list ap;
1022 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001023 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 va_end(ap);
1025 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001030 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001035 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 va_list ap;
1041 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001042 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 va_end(ap);
1044 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001049 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001054 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 va_list ap;
1060 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001061 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 va_end(ap);
1063 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001068 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001073 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 va_list ap;
1079 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001080 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 va_end(ap);
1082 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001087 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001091 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001092 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_list ap;
1098 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001099 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 va_end(ap);
1101 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001106 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001111 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 va_list ap;
1117 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001118 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001124 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001129 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
1132 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 ScopedJniThreadState ts(env);
1135 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 va_start(ap, mid);
1137 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001138 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 va_end(ap);
1140 return local_result;
1141 }
1142
1143 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001147 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
1150 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 JValue result = InvokeWithJValues(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 jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 ScopedJniThreadState ts(env);
1160 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 va_start(ap, mid);
1162 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 va_end(ap);
1164 return result.z;
1165 }
1166
1167 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
1173 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
1179 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 ScopedJniThreadState ts(env);
1182 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 va_start(ap, mid);
1184 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 va_end(ap);
1186 return result.b;
1187 }
1188
1189 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 }
1194
1195 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
1201 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 ScopedJniThreadState ts(env);
1204 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 va_start(ap, mid);
1206 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 va_end(ap);
1208 return result.c;
1209 }
1210
1211 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
1217 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001220 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
1223 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 ScopedJniThreadState ts(env);
1226 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 va_start(ap, mid);
1228 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 va_end(ap);
1230 return result.s;
1231 }
1232
1233 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
1239 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001242 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
1244
1245 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 ScopedJniThreadState ts(env);
1248 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 va_start(ap, mid);
1250 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 va_end(ap);
1252 return result.i;
1253 }
1254
1255 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001256 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001258 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 }
1260
1261 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001264 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
1267 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 ScopedJniThreadState ts(env);
1270 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 va_start(ap, mid);
1272 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 va_end(ap);
1274 return result.j;
1275 }
1276
1277 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001280 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
1283 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001286 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
1289 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
1292 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 va_start(ap, mid);
1294 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 va_end(ap);
1296 return result.f;
1297 }
1298
1299 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001300 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001302 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
1305 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001306 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001308 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
1311 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 ScopedJniThreadState ts(env);
1314 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 va_start(ap, mid);
1316 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 va_end(ap);
1318 return result.d;
1319 }
1320
1321 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001322 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001324 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
1327 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001330 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
1333 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 ScopedJniThreadState ts(env);
1336 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 va_start(ap, mid);
1338 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 va_end(ap);
1340 }
1341
1342 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001343 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 }
1347
1348 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001349 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001351 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 }
1353
1354 static jfieldID GetFieldID(JNIEnv* env,
1355 jclass c, const char* name, const char* sig) {
1356 ScopedJniThreadState ts(env);
1357 return FindFieldID(ts, c, name, sig, false);
1358 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001359
1360
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 static jfieldID GetStaticFieldID(JNIEnv* env,
1362 jclass c, const char* name, const char* sig) {
1363 ScopedJniThreadState ts(env);
1364 return FindFieldID(ts, c, name, sig, true);
1365 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001366
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001367 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001369 Object* o = Decode<Object*>(ts, obj);
1370 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001371 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001373
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001374 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001376 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001377 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001380 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001382 Object* o = Decode<Object*>(ts, java_object);
1383 Object* v = Decode<Object*>(ts, java_value);
1384 Field* f = DecodeField(ts, fid);
1385 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
1387
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001390 Object* v = Decode<Object*>(ts, java_value);
1391 Field* f = DecodeField(ts, fid);
1392 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001395#define GET_PRIMITIVE_FIELD(fn, instance) \
1396 ScopedJniThreadState ts(env); \
1397 Object* o = Decode<Object*>(ts, instance); \
1398 Field* f = DecodeField(ts, fid); \
1399 return f->fn(o)
1400
1401#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1402 ScopedJniThreadState ts(env); \
1403 Object* o = Decode<Object*>(ts, instance); \
1404 Field* f = DecodeField(ts, fid); \
1405 f->fn(o, value)
1406
1407 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1408 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 }
1410
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001411 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1412 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001415 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1416 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 }
1418
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001419 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1420 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001423 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1424 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 }
1426
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001427 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1428 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001431 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1432 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1436 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1440 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1444 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1448 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetLong, NULL);
1461 }
1462
1463 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1465 }
1466
1467 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1469 }
1470
1471 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1472 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1473 }
1474
1475 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1476 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1477 }
1478
1479 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1480 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1481 }
1482
1483 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1484 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1485 }
1486
1487 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1488 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1489 }
1490
1491 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1492 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1493 }
1494
1495 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1496 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1497 }
1498
1499 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1500 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1501 }
1502
1503 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1504 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1505 }
1506
1507 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1508 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1509 }
1510
1511 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1512 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1513 }
1514
1515 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1516 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1517 }
1518
1519 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1520 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1521 }
1522
1523 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1524 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1525 }
1526
1527 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1528 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1529 }
1530
1531 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1532 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
1535 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001536 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 ScopedJniThreadState ts(env);
1538 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001539 va_start(ap, mid);
1540 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001541 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 va_end(ap);
1543 return local_result;
1544 }
1545
1546 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001547 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001549 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001550 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
1553 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001554 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001556 JValue result = InvokeWithJValues(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 jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001561 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 ScopedJniThreadState ts(env);
1563 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001564 va_start(ap, mid);
1565 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 va_end(ap);
1567 return result.z;
1568 }
1569
1570 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001571 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001572 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001573 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 }
1575
1576 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001577 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001579 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
Elliott Hughes72025e52011-08-23 17:50:30 -07001582 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 ScopedJniThreadState ts(env);
1584 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001585 va_start(ap, mid);
1586 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 va_end(ap);
1588 return result.b;
1589 }
1590
1591 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001592 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001594 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
1597 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001598 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001600 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes72025e52011-08-23 17:50:30 -07001603 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 ScopedJniThreadState ts(env);
1605 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001606 va_start(ap, mid);
1607 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 va_end(ap);
1609 return result.c;
1610 }
1611
1612 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
1618 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001619 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001621 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes72025e52011-08-23 17:50:30 -07001624 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 ScopedJniThreadState ts(env);
1626 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001627 va_start(ap, mid);
1628 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 va_end(ap);
1630 return result.s;
1631 }
1632
1633 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001636 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 }
1638
1639 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001640 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001642 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes72025e52011-08-23 17:50:30 -07001645 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
1647 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 va_start(ap, mid);
1649 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 va_end(ap);
1651 return result.i;
1652 }
1653
1654 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001655 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001657 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 }
1659
1660 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001661 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001663 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 }
1665
Elliott Hughes72025e52011-08-23 17:50:30 -07001666 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 ScopedJniThreadState ts(env);
1668 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001669 va_start(ap, mid);
1670 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 va_end(ap);
1672 return result.j;
1673 }
1674
1675 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001676 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 }
1680
1681 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001682 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 }
1686
Elliott Hughes72025e52011-08-23 17:50:30 -07001687 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 ScopedJniThreadState ts(env);
1689 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001690 va_start(ap, mid);
1691 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 va_end(ap);
1693 return result.f;
1694 }
1695
1696 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001697 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 }
1701
1702 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 }
1707
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 ScopedJniThreadState ts(env);
1710 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001711 va_start(ap, mid);
1712 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 va_end(ap);
1714 return result.d;
1715 }
1716
1717 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001718 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 }
1722
1723 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001724 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001726 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes72025e52011-08-23 17:50:30 -07001729 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 ScopedJniThreadState ts(env);
1731 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001732 va_start(ap, mid);
1733 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 va_end(ap);
1735 }
1736
1737 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001738 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001740 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
1743 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001744 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001746 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes814e4032011-08-23 12:07:56 -07001749 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001751 if (chars == NULL && char_count == 0) {
1752 return NULL;
1753 }
1754 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001755 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 }
1757
1758 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1759 ScopedJniThreadState ts(env);
1760 if (utf == NULL) {
1761 return NULL;
1762 }
1763 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001764 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes814e4032011-08-23 12:07:56 -07001767 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1768 ScopedJniThreadState ts(env);
1769 return Decode<String*>(ts, java_string)->GetLength();
1770 }
1771
1772 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1773 ScopedJniThreadState ts(env);
1774 return Decode<String*>(ts, java_string)->GetUtfLength();
1775 }
1776
Elliott Hughesb465ab02011-08-24 11:21:21 -07001777 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001779 String* s = Decode<String*>(ts, java_string);
1780 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1781 ThrowSIOOBE(ts, start, length, s->GetLength());
1782 } else {
1783 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1784 memcpy(buf, chars + start, length * sizeof(jchar));
1785 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001786 }
1787
Elliott Hughesb465ab02011-08-24 11:21:21 -07001788 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001789 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001790 String* s = Decode<String*>(ts, java_string);
1791 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1792 ThrowSIOOBE(ts, start, length, s->GetLength());
1793 } else {
1794 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1795 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1796 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001797 }
1798
Elliott Hughes75770752011-08-24 17:52:38 -07001799 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001800 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001801 String* s = Decode<String*>(ts, java_string);
1802 const CharArray* chars = s->GetCharArray();
1803 PinPrimitiveArray(ts, chars);
1804 if (is_copy != NULL) {
1805 *is_copy = JNI_FALSE;
1806 }
1807 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001808 }
1809
Elliott Hughes75770752011-08-24 17:52:38 -07001810 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001811 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001812 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes75770752011-08-24 17:52:38 -07001815 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001817 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 }
1819
Elliott Hughes75770752011-08-24 17:52:38 -07001820 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001822 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
Elliott Hughes75770752011-08-24 17:52:38 -07001825 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001826 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001827 if (java_string == NULL) {
1828 return NULL;
1829 }
1830 if (is_copy != NULL) {
1831 *is_copy = JNI_TRUE;
1832 }
1833 String* s = Decode<String*>(ts, java_string);
1834 size_t byte_count = s->GetUtfLength();
1835 char* bytes = new char[byte_count + 1];
1836 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001837 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001838 return NULL;
1839 }
1840 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1841 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1842 bytes[byte_count] = '\0';
1843 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001844 }
1845
Elliott Hughes75770752011-08-24 17:52:38 -07001846 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001847 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001848 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001849 }
1850
Elliott Hughesbd935992011-08-22 11:59:34 -07001851 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001853 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001854 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001855 Array* array = obj->AsArray();
1856 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 }
1858
Elliott Hughes814e4032011-08-23 12:07:56 -07001859 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001861 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001862 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
1865 static void SetObjectArrayElement(JNIEnv* env,
1866 jobjectArray java_array, jsize index, jobject java_value) {
1867 ScopedJniThreadState ts(env);
1868 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1869 Object* value = Decode<Object*>(ts, java_value);
1870 array->Set(index, value);
1871 }
1872
1873 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1874 ScopedJniThreadState ts(env);
1875 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1876 }
1877
1878 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1879 ScopedJniThreadState ts(env);
1880 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1881 }
1882
1883 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1884 ScopedJniThreadState ts(env);
1885 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1886 }
1887
1888 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1889 ScopedJniThreadState ts(env);
1890 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1891 }
1892
1893 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1894 ScopedJniThreadState ts(env);
1895 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1896 }
1897
1898 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1899 ScopedJniThreadState ts(env);
1900 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1901 }
1902
1903 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1904 ScopedJniThreadState ts(env);
1905 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1906 }
1907
1908 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1909 ScopedJniThreadState ts(env);
1910 CHECK_GE(length, 0); // TODO: ReportJniError
1911
1912 // Compute the array class corresponding to the given element class.
1913 Class* element_class = Decode<Class*>(ts, element_jclass);
1914 std::string descriptor;
1915 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001916 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001917
1918 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001919 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1920 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 return NULL;
1922 }
1923
Elliott Hughes75770752011-08-24 17:52:38 -07001924 // Allocate and initialize if necessary.
1925 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001927 if (initial_element != NULL) {
1928 Object* initial_object = Decode<Object*>(ts, initial_element);
1929 for (jsize i = 0; i < length; ++i) {
1930 result->Set(i, initial_object);
1931 }
1932 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001933 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 }
1935
1936 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1937 ScopedJniThreadState ts(env);
1938 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1939 }
1940
Elliott Hughes75770752011-08-24 17:52:38 -07001941 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001942 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001943 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001944 }
1945
Elliott Hughes75770752011-08-24 17:52:38 -07001946 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001947 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001948 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001949 }
1950
Elliott Hughes75770752011-08-24 17:52:38 -07001951 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001953 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Elliott Hughes75770752011-08-24 17:52:38 -07001956 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001958 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 }
1960
Elliott Hughes75770752011-08-24 17:52:38 -07001961 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001962 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001963 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Elliott Hughes75770752011-08-24 17:52:38 -07001966 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001967 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001968 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 }
1970
Elliott Hughes75770752011-08-24 17:52:38 -07001971 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001973 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Elliott Hughes75770752011-08-24 17:52:38 -07001976 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001978 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Elliott Hughes75770752011-08-24 17:52:38 -07001981 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001983 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Elliott Hughes75770752011-08-24 17:52:38 -07001986 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001988 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes75770752011-08-24 17:52:38 -07001991 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001993 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes75770752011-08-24 17:52:38 -07001996 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001998 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Elliott Hughes75770752011-08-24 17:52:38 -07002001 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002003 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Elliott Hughes75770752011-08-24 17:52:38 -07002006 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002008 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Elliott Hughes75770752011-08-24 17:52:38 -07002011 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002013 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes75770752011-08-24 17:52:38 -07002016 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002018 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes75770752011-08-24 17:52:38 -07002021 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002023 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes75770752011-08-24 17:52:38 -07002026 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002028 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes814e4032011-08-23 12:07:56 -07002031 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002033 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes814e4032011-08-23 12:07:56 -07002036 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002038 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes814e4032011-08-23 12:07:56 -07002041 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002043 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Elliott Hughes814e4032011-08-23 12:07:56 -07002046 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Elliott Hughes814e4032011-08-23 12:07:56 -07002051 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
Elliott Hughes5174fe62011-08-23 15:12:35 -07002111 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002113 Class* c = Decode<Class*>(ts, java_class);
2114
Elliott Hughes5174fe62011-08-23 15:12:35 -07002115 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 const char* name = methods[i].name;
2117 const char* sig = methods[i].signature;
2118
2119 if (*sig == '!') {
2120 // TODO: fast jni. it's too noisy to log all these.
2121 ++sig;
2122 }
2123
Elliott Hughes5174fe62011-08-23 15:12:35 -07002124 Method* m = c->FindDirectMethod(name, sig);
2125 if (m == NULL) {
2126 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002128 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002130 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2132 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002133 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002135 } else if (!m->IsNative()) {
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 "method \"%s.%s%s\" is not native",
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;
2142 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143
Elliott Hughes75770752011-08-24 17:52:38 -07002144 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002145 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002146 }
2147
2148 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 }
2150 return JNI_OK;
2151 }
2152
Elliott Hughes5174fe62011-08-23 15:12:35 -07002153 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155 Class* c = Decode<Class*>(ts, java_class);
2156
Elliott Hughes75770752011-08-24 17:52:38 -07002157 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158 LOG(INFO) << "[Unregistering JNI native methods for "
2159 << PrettyDescriptor(c->GetDescriptor()) << "]";
2160 }
2161
2162 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2163 Method* m = c->GetDirectMethod(i);
2164 if (m->IsNative()) {
2165 m->UnregisterNative();
2166 }
2167 }
2168 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2169 Method* m = c->GetVirtualMethod(i);
2170 if (m->IsNative()) {
2171 m->UnregisterNative();
2172 }
2173 }
2174
2175 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes72025e52011-08-23 17:50:30 -07002178 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002180 Decode<Object*>(ts, java_object)->MonitorEnter();
2181 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 }
2183
Elliott Hughes72025e52011-08-23 17:50:30 -07002184 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002186 Decode<Object*>(ts, java_object)->MonitorEnter();
2187 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 }
2189
2190 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2191 ScopedJniThreadState ts(env);
2192 Runtime* runtime = Runtime::Current();
2193 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002194 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 } else {
2196 *vm = NULL;
2197 }
2198 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2199 }
2200
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2202 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002203
2204 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002205 CHECK(address != NULL); // TODO: ReportJniError
2206 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002207
2208 jclass buffer_class = GetDirectByteBufferClass(env);
2209 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2210 if (mid == NULL) {
2211 return NULL;
2212 }
2213
2214 // At the moment, the Java side is limited to 32 bits.
2215 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2216 CHECK_LE(capacity, 0xffffffff);
2217 jint address_arg = reinterpret_cast<jint>(address);
2218 jint capacity_arg = static_cast<jint>(capacity);
2219
2220 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2221 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002222 }
2223
Elliott Hughesb465ab02011-08-24 11:21:21 -07002224 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002225 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002226 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2227 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
Elliott Hughesb465ab02011-08-24 11:21:21 -07002230 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002232 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2233 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Elliott Hughesb465ab02011-08-24 11:21:21 -07002236 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002238
Elliott Hughes75770752011-08-24 17:52:38 -07002239 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002240
2241 // Do we definitely know what kind of reference this is?
2242 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2243 IndirectRefKind kind = GetIndirectRefKind(ref);
2244 switch (kind) {
2245 case kLocal:
2246 return JNILocalRefType;
2247 case kGlobal:
2248 return JNIGlobalRefType;
2249 case kWeakGlobal:
2250 return JNIWeakGlobalRefType;
2251 case kSirtOrInvalid:
2252 // Is it in a stack IRT?
2253 if (ts.Self()->SirtContains(java_object)) {
2254 return JNILocalRefType;
2255 }
2256
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002257 if (!ts.Env()->work_around_app_jni_bugs) {
2258 return JNIInvalidRefType;
2259 }
2260
Elliott Hughesb465ab02011-08-24 11:21:21 -07002261 // If we're handing out direct pointers, check whether it's a direct pointer
2262 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002263 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002264 if (ts.Env()->locals.Contains(java_object)) {
2265 return JNILocalRefType;
2266 }
2267 }
2268
2269 return JNIInvalidRefType;
2270 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 }
2272};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002273
Elliott Hughesa2501992011-08-26 19:39:54 -07002274const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002275 NULL, // reserved0.
2276 NULL, // reserved1.
2277 NULL, // reserved2.
2278 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002279 JNI::GetVersion,
2280 JNI::DefineClass,
2281 JNI::FindClass,
2282 JNI::FromReflectedMethod,
2283 JNI::FromReflectedField,
2284 JNI::ToReflectedMethod,
2285 JNI::GetSuperclass,
2286 JNI::IsAssignableFrom,
2287 JNI::ToReflectedField,
2288 JNI::Throw,
2289 JNI::ThrowNew,
2290 JNI::ExceptionOccurred,
2291 JNI::ExceptionDescribe,
2292 JNI::ExceptionClear,
2293 JNI::FatalError,
2294 JNI::PushLocalFrame,
2295 JNI::PopLocalFrame,
2296 JNI::NewGlobalRef,
2297 JNI::DeleteGlobalRef,
2298 JNI::DeleteLocalRef,
2299 JNI::IsSameObject,
2300 JNI::NewLocalRef,
2301 JNI::EnsureLocalCapacity,
2302 JNI::AllocObject,
2303 JNI::NewObject,
2304 JNI::NewObjectV,
2305 JNI::NewObjectA,
2306 JNI::GetObjectClass,
2307 JNI::IsInstanceOf,
2308 JNI::GetMethodID,
2309 JNI::CallObjectMethod,
2310 JNI::CallObjectMethodV,
2311 JNI::CallObjectMethodA,
2312 JNI::CallBooleanMethod,
2313 JNI::CallBooleanMethodV,
2314 JNI::CallBooleanMethodA,
2315 JNI::CallByteMethod,
2316 JNI::CallByteMethodV,
2317 JNI::CallByteMethodA,
2318 JNI::CallCharMethod,
2319 JNI::CallCharMethodV,
2320 JNI::CallCharMethodA,
2321 JNI::CallShortMethod,
2322 JNI::CallShortMethodV,
2323 JNI::CallShortMethodA,
2324 JNI::CallIntMethod,
2325 JNI::CallIntMethodV,
2326 JNI::CallIntMethodA,
2327 JNI::CallLongMethod,
2328 JNI::CallLongMethodV,
2329 JNI::CallLongMethodA,
2330 JNI::CallFloatMethod,
2331 JNI::CallFloatMethodV,
2332 JNI::CallFloatMethodA,
2333 JNI::CallDoubleMethod,
2334 JNI::CallDoubleMethodV,
2335 JNI::CallDoubleMethodA,
2336 JNI::CallVoidMethod,
2337 JNI::CallVoidMethodV,
2338 JNI::CallVoidMethodA,
2339 JNI::CallNonvirtualObjectMethod,
2340 JNI::CallNonvirtualObjectMethodV,
2341 JNI::CallNonvirtualObjectMethodA,
2342 JNI::CallNonvirtualBooleanMethod,
2343 JNI::CallNonvirtualBooleanMethodV,
2344 JNI::CallNonvirtualBooleanMethodA,
2345 JNI::CallNonvirtualByteMethod,
2346 JNI::CallNonvirtualByteMethodV,
2347 JNI::CallNonvirtualByteMethodA,
2348 JNI::CallNonvirtualCharMethod,
2349 JNI::CallNonvirtualCharMethodV,
2350 JNI::CallNonvirtualCharMethodA,
2351 JNI::CallNonvirtualShortMethod,
2352 JNI::CallNonvirtualShortMethodV,
2353 JNI::CallNonvirtualShortMethodA,
2354 JNI::CallNonvirtualIntMethod,
2355 JNI::CallNonvirtualIntMethodV,
2356 JNI::CallNonvirtualIntMethodA,
2357 JNI::CallNonvirtualLongMethod,
2358 JNI::CallNonvirtualLongMethodV,
2359 JNI::CallNonvirtualLongMethodA,
2360 JNI::CallNonvirtualFloatMethod,
2361 JNI::CallNonvirtualFloatMethodV,
2362 JNI::CallNonvirtualFloatMethodA,
2363 JNI::CallNonvirtualDoubleMethod,
2364 JNI::CallNonvirtualDoubleMethodV,
2365 JNI::CallNonvirtualDoubleMethodA,
2366 JNI::CallNonvirtualVoidMethod,
2367 JNI::CallNonvirtualVoidMethodV,
2368 JNI::CallNonvirtualVoidMethodA,
2369 JNI::GetFieldID,
2370 JNI::GetObjectField,
2371 JNI::GetBooleanField,
2372 JNI::GetByteField,
2373 JNI::GetCharField,
2374 JNI::GetShortField,
2375 JNI::GetIntField,
2376 JNI::GetLongField,
2377 JNI::GetFloatField,
2378 JNI::GetDoubleField,
2379 JNI::SetObjectField,
2380 JNI::SetBooleanField,
2381 JNI::SetByteField,
2382 JNI::SetCharField,
2383 JNI::SetShortField,
2384 JNI::SetIntField,
2385 JNI::SetLongField,
2386 JNI::SetFloatField,
2387 JNI::SetDoubleField,
2388 JNI::GetStaticMethodID,
2389 JNI::CallStaticObjectMethod,
2390 JNI::CallStaticObjectMethodV,
2391 JNI::CallStaticObjectMethodA,
2392 JNI::CallStaticBooleanMethod,
2393 JNI::CallStaticBooleanMethodV,
2394 JNI::CallStaticBooleanMethodA,
2395 JNI::CallStaticByteMethod,
2396 JNI::CallStaticByteMethodV,
2397 JNI::CallStaticByteMethodA,
2398 JNI::CallStaticCharMethod,
2399 JNI::CallStaticCharMethodV,
2400 JNI::CallStaticCharMethodA,
2401 JNI::CallStaticShortMethod,
2402 JNI::CallStaticShortMethodV,
2403 JNI::CallStaticShortMethodA,
2404 JNI::CallStaticIntMethod,
2405 JNI::CallStaticIntMethodV,
2406 JNI::CallStaticIntMethodA,
2407 JNI::CallStaticLongMethod,
2408 JNI::CallStaticLongMethodV,
2409 JNI::CallStaticLongMethodA,
2410 JNI::CallStaticFloatMethod,
2411 JNI::CallStaticFloatMethodV,
2412 JNI::CallStaticFloatMethodA,
2413 JNI::CallStaticDoubleMethod,
2414 JNI::CallStaticDoubleMethodV,
2415 JNI::CallStaticDoubleMethodA,
2416 JNI::CallStaticVoidMethod,
2417 JNI::CallStaticVoidMethodV,
2418 JNI::CallStaticVoidMethodA,
2419 JNI::GetStaticFieldID,
2420 JNI::GetStaticObjectField,
2421 JNI::GetStaticBooleanField,
2422 JNI::GetStaticByteField,
2423 JNI::GetStaticCharField,
2424 JNI::GetStaticShortField,
2425 JNI::GetStaticIntField,
2426 JNI::GetStaticLongField,
2427 JNI::GetStaticFloatField,
2428 JNI::GetStaticDoubleField,
2429 JNI::SetStaticObjectField,
2430 JNI::SetStaticBooleanField,
2431 JNI::SetStaticByteField,
2432 JNI::SetStaticCharField,
2433 JNI::SetStaticShortField,
2434 JNI::SetStaticIntField,
2435 JNI::SetStaticLongField,
2436 JNI::SetStaticFloatField,
2437 JNI::SetStaticDoubleField,
2438 JNI::NewString,
2439 JNI::GetStringLength,
2440 JNI::GetStringChars,
2441 JNI::ReleaseStringChars,
2442 JNI::NewStringUTF,
2443 JNI::GetStringUTFLength,
2444 JNI::GetStringUTFChars,
2445 JNI::ReleaseStringUTFChars,
2446 JNI::GetArrayLength,
2447 JNI::NewObjectArray,
2448 JNI::GetObjectArrayElement,
2449 JNI::SetObjectArrayElement,
2450 JNI::NewBooleanArray,
2451 JNI::NewByteArray,
2452 JNI::NewCharArray,
2453 JNI::NewShortArray,
2454 JNI::NewIntArray,
2455 JNI::NewLongArray,
2456 JNI::NewFloatArray,
2457 JNI::NewDoubleArray,
2458 JNI::GetBooleanArrayElements,
2459 JNI::GetByteArrayElements,
2460 JNI::GetCharArrayElements,
2461 JNI::GetShortArrayElements,
2462 JNI::GetIntArrayElements,
2463 JNI::GetLongArrayElements,
2464 JNI::GetFloatArrayElements,
2465 JNI::GetDoubleArrayElements,
2466 JNI::ReleaseBooleanArrayElements,
2467 JNI::ReleaseByteArrayElements,
2468 JNI::ReleaseCharArrayElements,
2469 JNI::ReleaseShortArrayElements,
2470 JNI::ReleaseIntArrayElements,
2471 JNI::ReleaseLongArrayElements,
2472 JNI::ReleaseFloatArrayElements,
2473 JNI::ReleaseDoubleArrayElements,
2474 JNI::GetBooleanArrayRegion,
2475 JNI::GetByteArrayRegion,
2476 JNI::GetCharArrayRegion,
2477 JNI::GetShortArrayRegion,
2478 JNI::GetIntArrayRegion,
2479 JNI::GetLongArrayRegion,
2480 JNI::GetFloatArrayRegion,
2481 JNI::GetDoubleArrayRegion,
2482 JNI::SetBooleanArrayRegion,
2483 JNI::SetByteArrayRegion,
2484 JNI::SetCharArrayRegion,
2485 JNI::SetShortArrayRegion,
2486 JNI::SetIntArrayRegion,
2487 JNI::SetLongArrayRegion,
2488 JNI::SetFloatArrayRegion,
2489 JNI::SetDoubleArrayRegion,
2490 JNI::RegisterNatives,
2491 JNI::UnregisterNatives,
2492 JNI::MonitorEnter,
2493 JNI::MonitorExit,
2494 JNI::GetJavaVM,
2495 JNI::GetStringRegion,
2496 JNI::GetStringUTFRegion,
2497 JNI::GetPrimitiveArrayCritical,
2498 JNI::ReleasePrimitiveArrayCritical,
2499 JNI::GetStringCritical,
2500 JNI::ReleaseStringCritical,
2501 JNI::NewWeakGlobalRef,
2502 JNI::DeleteWeakGlobalRef,
2503 JNI::ExceptionCheck,
2504 JNI::NewDirectByteBuffer,
2505 JNI::GetDirectBufferAddress,
2506 JNI::GetDirectBufferCapacity,
2507 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002508};
2509
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002510static const size_t kMonitorsInitial = 32; // Arbitrary.
2511static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2512
2513static const size_t kLocalsInitial = 64; // Arbitrary.
2514static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002515
Elliott Hughes75770752011-08-24 17:52:38 -07002516JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002517 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002518 vm(vm),
2519 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002520 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002521 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002522 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2523 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002524 functions = unchecked_functions = &gNativeInterface;
2525 if (check_jni) {
2526 functions = GetCheckJniNativeInterface();
2527 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002528}
2529
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002530JNIEnvExt::~JNIEnvExt() {
2531}
2532
Carl Shapiroea4dca82011-08-01 13:45:38 -07002533// JNI Invocation interface.
2534
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002535extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2536 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2537 if (args->version < JNI_VERSION_1_2) {
2538 return JNI_EVERSION;
2539 }
2540 Runtime::Options options;
2541 for (int i = 0; i < args->nOptions; ++i) {
2542 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002543 options.push_back(std::make_pair(StringPiece(option->optionString),
2544 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002545 }
2546 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002547 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002548 if (runtime == NULL) {
2549 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002550 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002551 runtime->Start();
2552 *p_env = Thread::Current()->GetJniEnv();
2553 *p_vm = runtime->GetJavaVM();
2554 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002555}
2556
Elliott Hughesf2682d52011-08-15 16:37:04 -07002557extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002558 Runtime* runtime = Runtime::Current();
2559 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002560 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002561 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002562 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002563 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002564 }
2565 return JNI_OK;
2566}
2567
2568// Historically unsupported.
2569extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2570 return JNI_ERR;
2571}
2572
Elliott Hughescdf53122011-08-19 15:46:09 -07002573class JII {
2574 public:
2575 static jint DestroyJavaVM(JavaVM* vm) {
2576 if (vm == NULL) {
2577 return JNI_ERR;
2578 } else {
2579 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2580 delete raw_vm->runtime;
2581 return JNI_OK;
2582 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002583 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002584
Elliott Hughescdf53122011-08-19 15:46:09 -07002585 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002586 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002587 }
2588
2589 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002590 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002591 }
2592
2593 static jint DetachCurrentThread(JavaVM* vm) {
2594 if (vm == NULL) {
2595 return JNI_ERR;
2596 } else {
2597 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2598 Runtime* runtime = raw_vm->runtime;
2599 runtime->DetachCurrentThread();
2600 return JNI_OK;
2601 }
2602 }
2603
2604 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2605 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2606 return JNI_EVERSION;
2607 }
2608 if (vm == NULL || env == NULL) {
2609 return JNI_ERR;
2610 }
2611 Thread* thread = Thread::Current();
2612 if (thread == NULL) {
2613 *env = NULL;
2614 return JNI_EDETACHED;
2615 }
2616 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002617 return JNI_OK;
2618 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002619};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002620
Elliott Hughesa2501992011-08-26 19:39:54 -07002621const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002622 NULL, // reserved0
2623 NULL, // reserved1
2624 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002625 JII::DestroyJavaVM,
2626 JII::AttachCurrentThread,
2627 JII::DetachCurrentThread,
2628 JII::GetEnv,
2629 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002630};
2631
Elliott Hughesbbd76712011-08-17 10:25:24 -07002632static const size_t kPinTableInitialSize = 16;
2633static const size_t kPinTableMaxSize = 1024;
2634
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002635static const size_t kGlobalsInitial = 512; // Arbitrary.
2636static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2637
2638static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2639static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2640
Elliott Hughesa0957642011-09-02 14:27:33 -07002641JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002642 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002643 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002644 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002645 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002646 verbose_jni(options->IsVerbose("jni")),
2647 log_third_party_jni(options->IsVerbose("third-party-jni")),
2648 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002649 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes75770752011-08-24 17:52:38 -07002650 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002651 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002652 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002653 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002654 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002655 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2656 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2657 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002658 functions = unchecked_functions = &gInvokeInterface;
2659 if (check_jni) {
2660 functions = GetCheckJniInvokeInterface();
2661 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002662}
2663
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002664JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002665 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002666 delete globals_lock;
2667 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002668 delete libraries_lock;
2669 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002670}
2671
Elliott Hughes75770752011-08-24 17:52:38 -07002672bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2673 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002674
2675 // See if we've already loaded this library. If we have, and the class loader
2676 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002677 // TODO: for better results we should canonicalize the pathname (or even compare
2678 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002679 SharedLibrary* library;
2680 {
2681 // TODO: move the locking (and more of this logic) into Libraries.
2682 MutexLock mu(libraries_lock);
2683 library = libraries->Get(path);
2684 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002685 if (library != NULL) {
2686 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002687 // The library will be associated with class_loader. The JNI
2688 // spec says we can't load the same library into more than one
2689 // class loader.
2690 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2691 "ClassLoader %p; can't open in ClassLoader %p",
2692 path.c_str(), library->GetClassLoader(), class_loader);
2693 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002694 return false;
2695 }
2696 if (verbose_jni) {
2697 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2698 << "ClassLoader " << class_loader << "]";
2699 }
2700 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002701 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2702 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002703 return false;
2704 }
2705 return true;
2706 }
2707
2708 // Open the shared library. Because we're using a full path, the system
2709 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2710 // resolve this library's dependencies though.)
2711
2712 // Failures here are expected when java.library.path has several entries
2713 // and we have to hunt for the lib.
2714
2715 // The current version of the dynamic linker prints detailed information
2716 // about dlopen() failures. Some things to check if the message is
2717 // cryptic:
2718 // - make sure the library exists on the device
2719 // - verify that the right path is being opened (the debug log message
2720 // above can help with that)
2721 // - check to see if the library is valid (e.g. not zero bytes long)
2722 // - check config/prelink-linux-arm.map to ensure that the library
2723 // is listed and is not being overrun by the previous entry (if
2724 // loading suddenly stops working on a prelinked library, this is
2725 // a good one to check)
2726 // - write a trivial app that calls sleep() then dlopen(), attach
2727 // to it with "strace -p <pid>" while it sleeps, and watch for
2728 // attempts to open nonexistent dependent shared libs
2729
2730 // TODO: automate some of these checks!
2731
2732 // This can execute slowly for a large library on a busy system, so we
2733 // want to switch from RUNNING to VMWAIT while it executes. This allows
2734 // the GC to ignore us.
2735 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002736 void* handle = NULL;
2737 {
2738 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
2739 handle = dlopen(path.c_str(), RTLD_LAZY);
2740 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002741
2742 if (verbose_jni) {
2743 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2744 }
2745
2746 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002747 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002748 return false;
2749 }
2750
2751 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002752 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002753 // TODO: move the locking (and more of this logic) into Libraries.
2754 MutexLock mu(libraries_lock);
2755 library = libraries->Get(path);
2756 if (library != NULL) {
2757 LOG(INFO) << "WOW: we lost a race to add shared library: "
2758 << "\"" << path << "\" ClassLoader=" << class_loader;
2759 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002760 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002761 library = new SharedLibrary(path, handle, class_loader);
2762 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002763 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002764
2765 if (verbose_jni) {
2766 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2767 }
2768
2769 bool result = true;
2770 void* sym = dlsym(handle, "JNI_OnLoad");
2771 if (sym == NULL) {
2772 if (verbose_jni) {
2773 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2774 }
2775 } else {
2776 // Call JNI_OnLoad. We have to override the current class
2777 // loader, which will always be "null" since the stuff at the
2778 // top of the stack is around Runtime.loadLibrary(). (See
2779 // the comments in the JNI FindClass function.)
2780 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2781 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002782 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002783 self->SetClassLoaderOverride(class_loader);
2784
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002785 int version = 0;
2786 {
2787 ScopedThreadStateChange tsc(self, Thread::kNative);
2788 if (verbose_jni) {
2789 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2790 }
2791 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002792 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002793
2794 self->SetClassLoaderOverride(old_class_loader);;
2795
2796 if (version != JNI_VERSION_1_2 &&
2797 version != JNI_VERSION_1_4 &&
2798 version != JNI_VERSION_1_6) {
2799 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2800 << "bad version: " << version;
2801 // It's unwise to call dlclose() here, but we can mark it
2802 // as bad and ensure that future load attempts will fail.
2803 // We don't know how far JNI_OnLoad got, so there could
2804 // be some partially-initialized stuff accessible through
2805 // newly-registered native method calls. We could try to
2806 // unregister them, but that doesn't seem worthwhile.
2807 result = false;
2808 } else {
2809 if (verbose_jni) {
2810 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2811 << " from JNI_OnLoad in \"" << path << "\"]";
2812 }
2813 }
2814 }
2815
2816 library->SetResult(result);
2817 return result;
2818}
2819
2820void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2821 CHECK(m->IsNative());
2822
2823 Class* c = m->GetDeclaringClass();
2824
2825 // If this is a static method, it could be called before the class
2826 // has been initialized.
2827 if (m->IsStatic()) {
2828 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2829 return NULL;
2830 }
2831 } else {
2832 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2833 }
2834
2835 MutexLock mu(libraries_lock);
2836 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002837}
2838
Elliott Hughes410c0c82011-09-01 17:58:25 -07002839void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2840 {
2841 MutexLock mu(globals_lock);
2842 globals.VisitRoots(visitor, arg);
2843 }
2844 {
2845 MutexLock mu(pins_lock);
2846 pin_table.VisitRoots(visitor, arg);
2847 }
2848 // The weak_globals table is visited by the GC itself (because it mutates the table).
2849}
2850
Ian Rogersdf20fe02011-07-20 20:34:16 -07002851} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002852
2853std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2854 switch (rhs) {
2855 case JNIInvalidRefType:
2856 os << "JNIInvalidRefType";
2857 return os;
2858 case JNILocalRefType:
2859 os << "JNILocalRefType";
2860 return os;
2861 case JNIGlobalRefType:
2862 os << "JNIGlobalRefType";
2863 return os;
2864 case JNIWeakGlobalRefType:
2865 os << "JNIWeakGlobalRefType";
2866 return os;
2867 }
2868}