blob: a75a365e6a53a0091182bf3941197bc13e352514 [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) {
Ian Rogers6de08602011-08-19 14:52:39 -0700199 Thread* self = ts.Self();
200
201 // Push a transition back into managed code onto the linked list in thread
202 CHECK_EQ(Thread::kRunnable, self->GetState());
203 NativeToManagedRecord record;
204 self->PushNativeToManagedRecord(&record);
205
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700206 // Call the invoke stub associated with the method
207 // Pass everything as arguments
208 const Method::InvokeStub* stub = method->GetInvokeStub();
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700209 JValue result;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700210 if (method->HasCode() && stub != NULL) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700211 (*stub)(method, receiver, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700212 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700213 LOG(WARNING) << "Not invoking method with no associated code: "
Elliott Hughesa2501992011-08-26 19:39:54 -0700214 << PrettyMethod(method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700215 result.j = 0;
216 }
buzbeec143c552011-08-20 17:38:58 -0700217
Ian Rogers6de08602011-08-19 14:52:39 -0700218 // Pop transition
219 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700220 return result;
221}
222
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700223JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700224 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700225 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700226 Method* method = 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());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700229}
230
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700231JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700232 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700233 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700234 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700235 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700236 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
237}
238
Elliott Hughes75770752011-08-24 17:52:38 -0700239Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700240 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700241}
242
Brian Carlstrom30b94452011-08-25 21:35:26 -0700243JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700244 Object* receiver = Decode<Object*>(ts, obj);
245 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700246 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700247 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
248}
249
Brian Carlstrom30b94452011-08-25 21:35:26 -0700250JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700251 Object* receiver = Decode<Object*>(ts, obj);
252 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700253 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700254 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700255}
256
Elliott Hughes6b436852011-08-12 10:16:44 -0700257// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
258// separated with slashes but aren't wrapped with "L;" like regular descriptors
259// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
260// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
261// supported names with dots too (such as "a.b.C").
262std::string NormalizeJniClassDescriptor(const char* name) {
263 std::string result;
264 // Add the missing "L;" if necessary.
265 if (name[0] == '[') {
266 result = name;
267 } else {
268 result += 'L';
269 result += name;
270 result += ';';
271 }
272 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700273 if (result.find('.') != std::string::npos) {
274 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
275 << "\"" << name << "\"";
276 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700277 }
278 return result;
279}
280
Elliott Hughescdf53122011-08-19 15:46:09 -0700281jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
282 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700283 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
284 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700285 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700286
287 Method* method = NULL;
288 if (is_static) {
289 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700290 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700291 method = c->FindVirtualMethod(name, sig);
292 if (method == NULL) {
293 // No virtual method matching the signature. Search declared
294 // private methods and constructors.
295 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700296 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700297 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700298
Elliott Hughescdf53122011-08-19 15:46:09 -0700299 if (method == NULL || method->IsStatic() != is_static) {
300 Thread* self = Thread::Current();
Elliott Hughesa2501992011-08-26 19:39:54 -0700301 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700302 // TODO: try searching for the opposite kind of method from is_static
303 // for better diagnostics?
304 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700305 "no %s method %s", is_static ? "static" : "non-static",
306 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700307 return NULL;
308 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700309
Elliott Hughescdf53122011-08-19 15:46:09 -0700310 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700311}
312
Elliott Hughescdf53122011-08-19 15:46:09 -0700313jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
314 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700315 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
316 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700317 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700318
319 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 Class* field_type;
321 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
322 if (sig[1] != '\0') {
323 // TODO: need to get the appropriate ClassLoader.
324 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
325 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700326 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700327 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700328 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 if (field_type == NULL) {
330 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700331 DCHECK(ts.Self()->IsExceptionPending());
332 ts.Self()->ClearException();
333 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
334 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
335 "no type \"%s\" found and so no field \"%s\" could be found in class "
336 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 return NULL;
338 }
339 if (is_static) {
340 field = c->FindStaticField(name, field_type);
341 } else {
342 field = c->FindInstanceField(name, field_type);
343 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700344 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700345 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700346 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700347 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700348 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700349 return NULL;
350 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700351 // Check invariant that all jfieldIDs have resolved types (how else would
352 // the type equality in Find...Field hold?)
353 DCHECK(field->GetType() != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700354 jweak fid = AddWeakGlobalReference(ts, field);
355 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700356}
357
Elliott Hughes75770752011-08-24 17:52:38 -0700358void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
359 JavaVMExt* vm = ts.Vm();
360 MutexLock mu(vm->pins_lock);
361 vm->pin_table.Add(array);
362}
363
364void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
365 JavaVMExt* vm = ts.Vm();
366 MutexLock mu(vm->pins_lock);
367 vm->pin_table.Remove(array);
368}
369
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700370template<typename JniT, typename ArtT>
371JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
372 CHECK_GE(length, 0); // TODO: ReportJniError
373 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700374 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700375}
376
Elliott Hughes75770752011-08-24 17:52:38 -0700377template <typename ArrayT, typename CArrayT, typename ArtArrayT>
378CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
379 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
380 PinPrimitiveArray(ts, array);
381 if (is_copy != NULL) {
382 *is_copy = JNI_FALSE;
383 }
384 return array->GetData();
385}
386
387template <typename ArrayT>
388void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
389 if (mode != JNI_COMMIT) {
390 Array* array = Decode<Array*>(ts, java_array);
391 UnpinPrimitiveArray(ts, array);
392 }
393}
394
Elliott Hughes814e4032011-08-23 12:07:56 -0700395void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
396 std::string type(PrettyType(array));
397 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
398 "%s offset=%d length=%d %s.length=%d",
399 type.c_str(), start, length, identifier, array->GetLength());
400}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700401void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
402 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
403 "offset=%d length=%d string.length()=%d", start, length, array_length);
404}
Elliott Hughes814e4032011-08-23 12:07:56 -0700405
406template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700407void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700408 ArrayT* array = Decode<ArrayT*>(ts, java_array);
409 if (start < 0 || length < 0 || start + length > array->GetLength()) {
410 ThrowAIOOBE(ts, array, start, length, "src");
411 } else {
412 JavaT* data = array->GetData();
413 memcpy(buf, data + start, length * sizeof(JavaT));
414 }
415}
416
417template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700418void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700419 ArrayT* array = Decode<ArrayT*>(ts, java_array);
420 if (start < 0 || length < 0 || start + length > array->GetLength()) {
421 ThrowAIOOBE(ts, array, start, length, "dst");
422 } else {
423 JavaT* data = array->GetData();
424 memcpy(data + start, buf, length * sizeof(JavaT));
425 }
426}
427
Elliott Hughes75770752011-08-24 17:52:38 -0700428jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700429 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
430 CHECK(buffer_class.get() != NULL);
431 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
432}
433
Elliott Hughes75770752011-08-24 17:52:38 -0700434jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700435 static jclass buffer_class = InitDirectByteBufferClass(env);
436 return buffer_class;
437}
438
Elliott Hughes75770752011-08-24 17:52:38 -0700439jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
440 if (vm == NULL || p_env == NULL) {
441 return JNI_ERR;
442 }
443
444 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
445 JavaVMAttachArgs args;
446 if (thr_args == NULL) {
447 // Allow the v1.1 calling convention.
448 args.version = JNI_VERSION_1_2;
449 args.name = NULL;
450 args.group = NULL; // TODO: get "main" thread group
451 } else {
452 args.version = in_args->version;
453 args.name = in_args->name;
454 if (in_args->group != NULL) {
455 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
456 args.group = NULL; // TODO: decode in_args->group
457 } else {
458 args.group = NULL; // TODO: get "main" thread group
459 }
460 }
461 CHECK_GE(args.version, JNI_VERSION_1_2);
462
463 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700464 runtime->AttachCurrentThread(args.name, p_env, as_daemon);
465 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700466}
467
Elliott Hughes79082e32011-08-25 12:07:32 -0700468class SharedLibrary {
469 public:
470 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
471 : path_(path),
472 handle_(handle),
473 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
474 jni_on_load_tid_(Thread::Current()->GetId()),
475 jni_on_load_result_(kPending) {
476 pthread_cond_init(&jni_on_load_cond_, NULL);
477 }
478
479 ~SharedLibrary() {
480 delete jni_on_load_lock_;
481 }
482
483 Object* GetClassLoader() {
484 return class_loader_;
485 }
486
487 std::string GetPath() {
488 return path_;
489 }
490
491 /*
492 * Check the result of an earlier call to JNI_OnLoad on this library. If
493 * the call has not yet finished in another thread, wait for it.
494 */
495 bool CheckOnLoadResult(JavaVMExt* vm) {
496 Thread* self = Thread::Current();
497 if (jni_on_load_tid_ == self->GetId()) {
498 // Check this so we don't end up waiting for ourselves. We need
499 // to return "true" so the caller can continue.
500 LOG(INFO) << *self << " recursive attempt to load library "
501 << "\"" << path_ << "\"";
502 return true;
503 }
504
505 MutexLock mu(jni_on_load_lock_);
506 while (jni_on_load_result_ == kPending) {
507 if (vm->verbose_jni) {
508 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
509 << "JNI_OnLoad...]";
510 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700511 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes79082e32011-08-25 12:07:32 -0700512 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
Elliott Hughes79082e32011-08-25 12:07:32 -0700513 }
514
515 bool okay = (jni_on_load_result_ == kOkay);
516 if (vm->verbose_jni) {
517 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
518 << (okay ? "succeeded" : "failed") << "]";
519 }
520 return okay;
521 }
522
523 void SetResult(bool result) {
524 jni_on_load_result_ = result ? kOkay : kFailed;
525 jni_on_load_tid_ = 0;
526
527 // Broadcast a wakeup to anybody sleeping on the condition variable.
528 MutexLock mu(jni_on_load_lock_);
529 pthread_cond_broadcast(&jni_on_load_cond_);
530 }
531
532 void* FindSymbol(const std::string& symbol_name) {
533 return dlsym(handle_, symbol_name.c_str());
534 }
535
536 private:
537 enum JNI_OnLoadState {
538 kPending,
539 kFailed,
540 kOkay,
541 };
542
543 // Path to library "/system/lib/libjni.so".
544 std::string path_;
545
546 // The void* returned by dlopen(3).
547 void* handle_;
548
549 // The ClassLoader this library is associated with.
550 Object* class_loader_;
551
552 // Guards remaining items.
553 Mutex* jni_on_load_lock_;
554 // Wait for JNI_OnLoad in other thread.
555 pthread_cond_t jni_on_load_cond_;
556 // Recursive invocation guard.
557 uint32_t jni_on_load_tid_;
558 // Result of earlier JNI_OnLoad call.
559 JNI_OnLoadState jni_on_load_result_;
560};
561
Elliott Hughescdf53122011-08-19 15:46:09 -0700562} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700563
Elliott Hughes79082e32011-08-25 12:07:32 -0700564// This exists mainly to keep implementation details out of the header file.
565class Libraries {
566 public:
567 Libraries() {
568 }
569
570 ~Libraries() {
571 // Delete our map values. (The keys will be cleaned up by the map itself.)
572 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
573 delete it->second;
574 }
575 }
576
577 SharedLibrary* Get(const std::string& path) {
578 return libraries_[path];
579 }
580
581 void Put(const std::string& path, SharedLibrary* library) {
582 libraries_[path] = library;
583 }
584
585 // See section 11.3 "Linking Native Methods" of the JNI spec.
586 void* FindNativeMethod(const Method* m) {
587 std::string jni_short_name(JniShortName(m));
588 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700589 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
591 SharedLibrary* library = it->second;
592 if (library->GetClassLoader() != declaring_class_loader) {
593 // We only search libraries loaded by the appropriate ClassLoader.
594 continue;
595 }
596 // Try the short name then the long name...
597 void* fn = library->FindSymbol(jni_short_name);
598 if (fn == NULL) {
599 fn = library->FindSymbol(jni_long_name);
600 }
601 if (fn != NULL) {
602 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700603 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700604 << " in \"" << library->GetPath() << "\"]";
605 }
606 return fn;
607 }
608 }
609 std::string detail;
610 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700611 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700612 LOG(ERROR) << detail;
613 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
614 "%s", detail.c_str());
615 return NULL;
616 }
617
618 private:
619 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
620
621 std::map<std::string, SharedLibrary*> libraries_;
622};
623
Elliott Hughescdf53122011-08-19 15:46:09 -0700624class JNI {
625 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700626
Elliott Hughescdf53122011-08-19 15:46:09 -0700627 static jint GetVersion(JNIEnv* env) {
628 ScopedJniThreadState ts(env);
629 return JNI_VERSION_1_6;
630 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700631
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
633 ScopedJniThreadState ts(env);
634 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700635 return NULL;
636 }
637
Elliott Hughescdf53122011-08-19 15:46:09 -0700638 static jclass FindClass(JNIEnv* env, const char* name) {
639 ScopedJniThreadState ts(env);
640 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
641 std::string descriptor(NormalizeJniClassDescriptor(name));
642 // TODO: need to get the appropriate ClassLoader.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700643 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700644 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700645 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700646 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700647
Elliott Hughescdf53122011-08-19 15:46:09 -0700648 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
649 ScopedJniThreadState ts(env);
650 Method* method = Decode<Method*>(ts, java_method);
651 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700652 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700653
Elliott Hughescdf53122011-08-19 15:46:09 -0700654 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
655 ScopedJniThreadState ts(env);
656 Field* field = Decode<Field*>(ts, java_field);
657 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
658 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700659
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
661 ScopedJniThreadState ts(env);
662 Method* method = DecodeMethod(ts, mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700663 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700664 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700665
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
667 ScopedJniThreadState ts(env);
668 Field* field = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700669 return AddLocalReference<jobject>(env, field);
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 jclass GetObjectClass(JNIEnv* env, jobject java_object) {
673 ScopedJniThreadState ts(env);
674 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700675 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700676 }
677
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700678 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700679 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700680 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700681 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700683
Elliott Hughes37f7a402011-08-22 18:56:01 -0700684 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700685 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700686 Class* c1 = Decode<Class*>(ts, java_class1);
687 Class* c2 = Decode<Class*>(ts, java_class2);
688 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700690
Elliott Hughes37f7a402011-08-22 18:56:01 -0700691 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700692 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700693 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700694 if (jobj == NULL) {
695 // NB. JNI is different from regular Java instanceof in this respect
696 return JNI_TRUE;
697 } else {
698 Object* obj = Decode<Object*>(ts, jobj);
699 Class* klass = Decode<Class*>(ts, clazz);
700 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
701 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700702 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700703
Elliott Hughes37f7a402011-08-22 18:56:01 -0700704 static jint Throw(JNIEnv* env, jthrowable java_exception) {
705 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700706 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700707 if (exception == NULL) {
708 return JNI_ERR;
709 }
710 ts.Self()->SetException(exception);
711 return JNI_OK;
712 }
713
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700714 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700715 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700716 // TODO: check for a pending exception to decide what constructor to call.
717 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
718 if (mid == NULL) {
719 return JNI_ERR;
720 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700721 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
722 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700723 return JNI_ERR;
724 }
725
726 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700727 args[0].l = s.get();
728 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
729 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700730 return JNI_ERR;
731 }
732
Elliott Hughes72025e52011-08-23 17:50:30 -0700733 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700734 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700735 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700736
Elliott Hughes37f7a402011-08-22 18:56:01 -0700737 return JNI_OK;
738 }
739
740 static jboolean ExceptionCheck(JNIEnv* env) {
741 ScopedJniThreadState ts(env);
742 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
743 }
744
745 static void ExceptionClear(JNIEnv* env) {
746 ScopedJniThreadState ts(env);
747 ts.Self()->ClearException();
748 }
749
750 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700752
753 Thread* self = ts.Self();
754 Throwable* original_exception = self->GetException();
755 self->ClearException();
756
Elliott Hughesbf86d042011-08-31 17:53:14 -0700757 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700758 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
759 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
760 if (mid == NULL) {
761 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
762 << PrettyType(original_exception);
763 } else {
764 env->CallVoidMethod(exception.get(), mid);
765 if (self->IsExceptionPending()) {
766 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
767 << " thrown while calling printStackTrace";
768 self->ClearException();
769 }
770 }
771
772 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700774
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 static jthrowable ExceptionOccurred(JNIEnv* env) {
776 ScopedJniThreadState ts(env);
777 Object* exception = ts.Self()->GetException();
778 if (exception == NULL) {
779 return NULL;
780 } else {
781 // TODO: if adding a local reference failing causes the VM to abort
782 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700783 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700784 if (localException == NULL) {
785 // We were unable to add a new local reference, and threw a new
786 // exception. We can't return "exception", because it's not a
787 // local reference. So we have to return NULL, indicating that
788 // there was no exception, even though it's pretty much raining
789 // exceptions in here.
790 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
791 }
792 return localException;
793 }
794 }
795
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 static void FatalError(JNIEnv* env, const char* msg) {
797 ScopedJniThreadState ts(env);
798 LOG(FATAL) << "JNI FatalError called: " << msg;
799 }
800
801 static jint PushLocalFrame(JNIEnv* env, jint cap) {
802 ScopedJniThreadState ts(env);
803 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
804 return JNI_OK;
805 }
806
807 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
808 ScopedJniThreadState ts(env);
809 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
810 return res;
811 }
812
Elliott Hughes72025e52011-08-23 17:50:30 -0700813 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
814 ScopedJniThreadState ts(env);
815 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
816 return 0;
817 }
818
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
820 ScopedJniThreadState ts(env);
821 if (obj == NULL) {
822 return NULL;
823 }
824
Elliott Hughes75770752011-08-24 17:52:38 -0700825 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700826 IndirectReferenceTable& globals = vm->globals;
827 MutexLock mu(vm->globals_lock);
828 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
829 return reinterpret_cast<jobject>(ref);
830 }
831
832 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
833 ScopedJniThreadState ts(env);
834 if (obj == NULL) {
835 return;
836 }
837
Elliott Hughes75770752011-08-24 17:52:38 -0700838 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 IndirectReferenceTable& globals = vm->globals;
840 MutexLock mu(vm->globals_lock);
841
842 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
843 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
844 << "failed to find entry";
845 }
846 }
847
848 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
849 ScopedJniThreadState ts(env);
850 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
851 }
852
853 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
854 ScopedJniThreadState ts(env);
855 if (obj == NULL) {
856 return;
857 }
858
Elliott Hughes75770752011-08-24 17:52:38 -0700859 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 IndirectReferenceTable& weak_globals = vm->weak_globals;
861 MutexLock mu(vm->weak_globals_lock);
862
863 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
864 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
865 << "failed to find entry";
866 }
867 }
868
869 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
870 ScopedJniThreadState ts(env);
871 if (obj == NULL) {
872 return NULL;
873 }
874
875 IndirectReferenceTable& locals = ts.Env()->locals;
876
877 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
878 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
879 return reinterpret_cast<jobject>(ref);
880 }
881
882 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
883 ScopedJniThreadState ts(env);
884 if (obj == NULL) {
885 return;
886 }
887
888 IndirectReferenceTable& locals = ts.Env()->locals;
889
890 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
891 if (!locals.Remove(cookie, obj)) {
892 // Attempting to delete a local reference that is not in the
893 // topmost local reference frame is a no-op. DeleteLocalRef returns
894 // void and doesn't throw any exceptions, but we should probably
895 // complain about it so the user will notice that things aren't
896 // going quite the way they expect.
897 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
898 << "failed to find entry";
899 }
900 }
901
902 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
903 ScopedJniThreadState ts(env);
904 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
905 ? JNI_TRUE : JNI_FALSE;
906 }
907
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700908 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700909 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700910 Class* c = Decode<Class*>(ts, java_class);
911 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
912 return NULL;
913 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700914 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 }
916
Elliott Hughes72025e52011-08-23 17:50:30 -0700917 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700918 ScopedJniThreadState ts(env);
919 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 va_start(args, mid);
921 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 va_end(args);
923 return result;
924 }
925
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700928 Class* c = Decode<Class*>(ts, java_class);
929 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
930 return NULL;
931 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700932 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700933 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 return local_result;
936 }
937
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700940 Class* c = Decode<Class*>(ts, java_class);
941 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
942 return NULL;
943 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700944 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700945 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 return local_result;
948 }
949
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
951 ScopedJniThreadState ts(env);
952 return FindMethodID(ts, c, name, sig, false);
953 }
954
955 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
956 ScopedJniThreadState ts(env);
957 return FindMethodID(ts, c, name, sig, true);
958 }
959
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700961 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 va_list ap;
963 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700964 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700966 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700971 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700972 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700977 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700978 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 static jboolean CallBooleanMethod(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.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jboolean CallBooleanMethodV(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).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 }
994
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 static jboolean CallBooleanMethodA(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).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 }
999
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 static jbyte CallByteMethod(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.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jbyte CallByteMethodV(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).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jbyte CallByteMethodA(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).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 static jchar CallCharMethod(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.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jchar CallCharMethodV(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).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jchar CallCharMethodA(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).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jdouble CallDoubleMethod(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.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jdouble CallDoubleMethodV(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).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jdouble CallDoubleMethodA(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).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jfloat CallFloatMethod(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.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jfloat CallFloatMethodV(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).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jfloat CallFloatMethodA(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).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jint CallIntMethod(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.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jint CallIntMethodV(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).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jint CallIntMethodA(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).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jlong CallLongMethod(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.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jlong CallLongMethodV(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).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jlong CallLongMethodA(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).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jshort CallShortMethod(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);
1120 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001125 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001130 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 va_list ap;
1136 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001137 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001143 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001148 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
1151 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 ScopedJniThreadState ts(env);
1154 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 va_start(ap, mid);
1156 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001157 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 va_end(ap);
1159 return local_result;
1160 }
1161
1162 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001166 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
1169 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001173 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 }
1175
1176 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 ScopedJniThreadState ts(env);
1179 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 va_start(ap, mid);
1181 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 va_end(ap);
1183 return result.z;
1184 }
1185
1186 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
1192 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
1198 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 ScopedJniThreadState ts(env);
1201 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 va_start(ap, mid);
1203 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 va_end(ap);
1205 return result.b;
1206 }
1207
1208 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001209 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
1214 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
1220 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 ScopedJniThreadState ts(env);
1223 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 va_start(ap, mid);
1225 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 va_end(ap);
1227 return result.c;
1228 }
1229
1230 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
1236 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001239 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
1242 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 ScopedJniThreadState ts(env);
1245 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 va_start(ap, mid);
1247 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 va_end(ap);
1249 return result.s;
1250 }
1251
1252 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
1258 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001261 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 ScopedJniThreadState ts(env);
1267 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 va_start(ap, mid);
1269 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 va_end(ap);
1271 return result.i;
1272 }
1273
1274 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
1280 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001283 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
1286 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 ScopedJniThreadState ts(env);
1289 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 va_start(ap, mid);
1291 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 va_end(ap);
1293 return result.j;
1294 }
1295
1296 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
1302 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001305 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
1308 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 ScopedJniThreadState ts(env);
1311 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001312 va_start(ap, mid);
1313 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 va_end(ap);
1315 return result.f;
1316 }
1317
1318 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001319 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001321 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
1324 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001327 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
1330 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 ScopedJniThreadState ts(env);
1333 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001334 va_start(ap, mid);
1335 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 va_end(ap);
1337 return result.d;
1338 }
1339
1340 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001343 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 }
1345
1346 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001349 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
1352 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 ScopedJniThreadState ts(env);
1355 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001356 va_start(ap, mid);
1357 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 va_end(ap);
1359 }
1360
1361 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001364 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
1367 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001368 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001370 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
1373 static jfieldID GetFieldID(JNIEnv* env,
1374 jclass c, const char* name, const char* sig) {
1375 ScopedJniThreadState ts(env);
1376 return FindFieldID(ts, c, name, sig, false);
1377 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001378
1379
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 static jfieldID GetStaticFieldID(JNIEnv* env,
1381 jclass c, const char* name, const char* sig) {
1382 ScopedJniThreadState ts(env);
1383 return FindFieldID(ts, c, name, sig, true);
1384 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001385
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 Object* o = Decode<Object*>(ts, obj);
1389 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001390 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001392
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001393 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001395 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001396 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001401 Object* o = Decode<Object*>(ts, java_object);
1402 Object* v = Decode<Object*>(ts, java_value);
1403 Field* f = DecodeField(ts, fid);
1404 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001409 Object* v = Decode<Object*>(ts, java_value);
1410 Field* f = DecodeField(ts, fid);
1411 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001414#define GET_PRIMITIVE_FIELD(fn, instance) \
1415 ScopedJniThreadState ts(env); \
1416 Object* o = Decode<Object*>(ts, instance); \
1417 Field* f = DecodeField(ts, fid); \
1418 return f->fn(o)
1419
1420#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1421 ScopedJniThreadState ts(env); \
1422 Object* o = Decode<Object*>(ts, instance); \
1423 Field* f = DecodeField(ts, fid); \
1424 f->fn(o, value)
1425
1426 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1427 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001430 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1431 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1435 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1439 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1443 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1447 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1451 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1455 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1459 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1463 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 }
1465
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001466 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1467 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1471 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1475 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001478 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1479 GET_PRIMITIVE_FIELD(GetLong, NULL);
1480 }
1481
1482 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1483 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1484 }
1485
1486 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1487 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1488 }
1489
1490 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1491 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1492 }
1493
1494 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1495 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1496 }
1497
1498 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1499 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1500 }
1501
1502 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1503 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1504 }
1505
1506 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1507 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1508 }
1509
1510 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1511 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1512 }
1513
1514 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1515 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1516 }
1517
1518 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1519 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1520 }
1521
1522 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1523 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1524 }
1525
1526 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1527 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1528 }
1529
1530 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1531 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1532 }
1533
1534 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1535 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1536 }
1537
1538 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1539 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1540 }
1541
1542 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1543 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1544 }
1545
1546 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1547 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1548 }
1549
1550 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1551 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 }
1553
1554 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001555 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 ScopedJniThreadState ts(env);
1557 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001558 va_start(ap, mid);
1559 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001560 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 va_end(ap);
1562 return local_result;
1563 }
1564
1565 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001566 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001568 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001569 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
1572 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001573 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001575 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001576 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
1579 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001580 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 ScopedJniThreadState ts(env);
1582 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001583 va_start(ap, mid);
1584 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 va_end(ap);
1586 return result.z;
1587 }
1588
1589 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001590 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001592 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 }
1594
1595 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001596 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001598 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes72025e52011-08-23 17:50:30 -07001601 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 ScopedJniThreadState ts(env);
1603 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001604 va_start(ap, mid);
1605 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 va_end(ap);
1607 return result.b;
1608 }
1609
1610 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001611 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001613 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
1616 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001617 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001619 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
Elliott Hughes72025e52011-08-23 17:50:30 -07001622 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 ScopedJniThreadState ts(env);
1624 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001625 va_start(ap, mid);
1626 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 va_end(ap);
1628 return result.c;
1629 }
1630
1631 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001632 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
1637 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001638 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001640 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 }
1642
Elliott Hughes72025e52011-08-23 17:50:30 -07001643 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 ScopedJniThreadState ts(env);
1645 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 va_start(ap, mid);
1647 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 va_end(ap);
1649 return result.s;
1650 }
1651
1652 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001653 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001655 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 }
1657
1658 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001661 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
Elliott Hughes72025e52011-08-23 17:50:30 -07001664 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 ScopedJniThreadState ts(env);
1666 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 va_start(ap, mid);
1668 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 va_end(ap);
1670 return result.i;
1671 }
1672
1673 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001674 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001676 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
1679 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001680 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001682 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 }
1684
Elliott Hughes72025e52011-08-23 17:50:30 -07001685 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 ScopedJniThreadState ts(env);
1687 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001688 va_start(ap, mid);
1689 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 va_end(ap);
1691 return result.j;
1692 }
1693
1694 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001695 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001697 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 }
1699
1700 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001701 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 }
1705
Elliott Hughes72025e52011-08-23 17:50:30 -07001706 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 ScopedJniThreadState ts(env);
1708 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001709 va_start(ap, mid);
1710 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 va_end(ap);
1712 return result.f;
1713 }
1714
1715 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001716 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001718 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
1721 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001724 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
Elliott Hughes72025e52011-08-23 17:50:30 -07001727 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 ScopedJniThreadState ts(env);
1729 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001730 va_start(ap, mid);
1731 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 va_end(ap);
1733 return result.d;
1734 }
1735
1736 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001737 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001739 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 }
1741
1742 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001745 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 }
1747
Elliott Hughes72025e52011-08-23 17:50:30 -07001748 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 ScopedJniThreadState ts(env);
1750 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001751 va_start(ap, mid);
1752 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 va_end(ap);
1754 }
1755
1756 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001759 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
1762 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001763 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001765 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 }
1767
Elliott Hughes814e4032011-08-23 12:07:56 -07001768 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001770 if (chars == NULL && char_count == 0) {
1771 return NULL;
1772 }
1773 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001774 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
1777 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1778 ScopedJniThreadState ts(env);
1779 if (utf == NULL) {
1780 return NULL;
1781 }
1782 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001783 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 }
1785
Elliott Hughes814e4032011-08-23 12:07:56 -07001786 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1787 ScopedJniThreadState ts(env);
1788 return Decode<String*>(ts, java_string)->GetLength();
1789 }
1790
1791 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1792 ScopedJniThreadState ts(env);
1793 return Decode<String*>(ts, java_string)->GetUtfLength();
1794 }
1795
Elliott Hughesb465ab02011-08-24 11:21:21 -07001796 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001798 String* s = Decode<String*>(ts, java_string);
1799 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1800 ThrowSIOOBE(ts, start, length, s->GetLength());
1801 } else {
1802 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1803 memcpy(buf, chars + start, length * sizeof(jchar));
1804 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001805 }
1806
Elliott Hughesb465ab02011-08-24 11:21:21 -07001807 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001808 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001809 String* s = Decode<String*>(ts, java_string);
1810 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1811 ThrowSIOOBE(ts, start, length, s->GetLength());
1812 } else {
1813 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1814 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1815 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001816 }
1817
Elliott Hughes75770752011-08-24 17:52:38 -07001818 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001819 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001820 String* s = Decode<String*>(ts, java_string);
1821 const CharArray* chars = s->GetCharArray();
1822 PinPrimitiveArray(ts, chars);
1823 if (is_copy != NULL) {
1824 *is_copy = JNI_FALSE;
1825 }
1826 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001827 }
1828
Elliott Hughes75770752011-08-24 17:52:38 -07001829 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001830 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001831 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
Elliott Hughes75770752011-08-24 17:52:38 -07001834 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001836 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
Elliott Hughes75770752011-08-24 17:52:38 -07001839 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001841 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 }
1843
Elliott Hughes75770752011-08-24 17:52:38 -07001844 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001845 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001846 if (java_string == NULL) {
1847 return NULL;
1848 }
1849 if (is_copy != NULL) {
1850 *is_copy = JNI_TRUE;
1851 }
1852 String* s = Decode<String*>(ts, java_string);
1853 size_t byte_count = s->GetUtfLength();
1854 char* bytes = new char[byte_count + 1];
1855 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001856 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001857 return NULL;
1858 }
1859 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1860 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1861 bytes[byte_count] = '\0';
1862 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001863 }
1864
Elliott Hughes75770752011-08-24 17:52:38 -07001865 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001866 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001867 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001868 }
1869
Elliott Hughesbd935992011-08-22 11:59:34 -07001870 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001872 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001873 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001874 Array* array = obj->AsArray();
1875 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 }
1877
Elliott Hughes814e4032011-08-23 12:07:56 -07001878 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001880 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001881 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 }
1883
1884 static void SetObjectArrayElement(JNIEnv* env,
1885 jobjectArray java_array, jsize index, jobject java_value) {
1886 ScopedJniThreadState ts(env);
1887 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1888 Object* value = Decode<Object*>(ts, java_value);
1889 array->Set(index, value);
1890 }
1891
1892 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1893 ScopedJniThreadState ts(env);
1894 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1895 }
1896
1897 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1898 ScopedJniThreadState ts(env);
1899 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1900 }
1901
1902 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1903 ScopedJniThreadState ts(env);
1904 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1905 }
1906
1907 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1908 ScopedJniThreadState ts(env);
1909 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1910 }
1911
1912 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1913 ScopedJniThreadState ts(env);
1914 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1915 }
1916
1917 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1918 ScopedJniThreadState ts(env);
1919 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1920 }
1921
1922 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1923 ScopedJniThreadState ts(env);
1924 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1925 }
1926
1927 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1928 ScopedJniThreadState ts(env);
1929 CHECK_GE(length, 0); // TODO: ReportJniError
1930
1931 // Compute the array class corresponding to the given element class.
1932 Class* element_class = Decode<Class*>(ts, element_jclass);
1933 std::string descriptor;
1934 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001935 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001936
1937 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001938 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1939 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 return NULL;
1941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 // Allocate and initialize if necessary.
1944 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001946 if (initial_element != NULL) {
1947 Object* initial_object = Decode<Object*>(ts, initial_element);
1948 for (jsize i = 0; i < length; ++i) {
1949 result->Set(i, initial_object);
1950 }
1951 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001952 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
1955 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1956 ScopedJniThreadState ts(env);
1957 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1958 }
1959
Elliott Hughes75770752011-08-24 17:52:38 -07001960 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001961 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001962 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001963 }
1964
Elliott Hughes75770752011-08-24 17:52:38 -07001965 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001966 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001967 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001968 }
1969
Elliott Hughes75770752011-08-24 17:52:38 -07001970 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001971 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001972 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 }
1974
Elliott Hughes75770752011-08-24 17:52:38 -07001975 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001977 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
Elliott Hughes75770752011-08-24 17:52:38 -07001980 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001982 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 }
1984
Elliott Hughes75770752011-08-24 17:52:38 -07001985 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001987 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Elliott Hughes75770752011-08-24 17:52:38 -07001990 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001992 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 }
1994
Elliott Hughes75770752011-08-24 17:52:38 -07001995 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001997 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Elliott Hughes75770752011-08-24 17:52:38 -07002000 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002002 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
Elliott Hughes75770752011-08-24 17:52:38 -07002005 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002007 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Elliott Hughes75770752011-08-24 17:52:38 -07002010 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002012 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Elliott Hughes75770752011-08-24 17:52:38 -07002015 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002017 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Elliott Hughes75770752011-08-24 17:52:38 -07002020 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002022 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes75770752011-08-24 17:52:38 -07002025 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002027 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes75770752011-08-24 17:52:38 -07002030 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002032 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
2034
Elliott Hughes75770752011-08-24 17:52:38 -07002035 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002037 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes75770752011-08-24 17:52:38 -07002040 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002042 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes75770752011-08-24 17:52:38 -07002045 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002047 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002052 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002057 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002062 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002067 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Elliott Hughes814e4032011-08-23 12:07:56 -07002070 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002072 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002077 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 }
2079
Elliott Hughes814e4032011-08-23 12:07:56 -07002080 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002082 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Elliott Hughes814e4032011-08-23 12:07:56 -07002085 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002087 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
Elliott Hughes814e4032011-08-23 12:07:56 -07002090 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002092 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
Elliott Hughes814e4032011-08-23 12:07:56 -07002095 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002097 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
Elliott Hughes814e4032011-08-23 12:07:56 -07002100 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002102 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
Elliott Hughes814e4032011-08-23 12:07:56 -07002105 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002107 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
Elliott Hughes814e4032011-08-23 12:07:56 -07002110 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002112 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
Elliott Hughes814e4032011-08-23 12:07:56 -07002115 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002117 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002122 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 }
2124
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002127 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
2129
Elliott Hughes5174fe62011-08-23 15:12:35 -07002130 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002132 Class* c = Decode<Class*>(ts, java_class);
2133
Elliott Hughes5174fe62011-08-23 15:12:35 -07002134 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 const char* name = methods[i].name;
2136 const char* sig = methods[i].signature;
2137
2138 if (*sig == '!') {
2139 // TODO: fast jni. it's too noisy to log all these.
2140 ++sig;
2141 }
2142
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143 Method* m = c->FindDirectMethod(name, sig);
2144 if (m == NULL) {
2145 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002147 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002149 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2151 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002152 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002154 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002156 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2158 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002159 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 return JNI_ERR;
2161 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162
Elliott Hughes75770752011-08-24 17:52:38 -07002163 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002164 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002165 }
2166
2167 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 }
2169 return JNI_OK;
2170 }
2171
Elliott Hughes5174fe62011-08-23 15:12:35 -07002172 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002174 Class* c = Decode<Class*>(ts, java_class);
2175
Elliott Hughes75770752011-08-24 17:52:38 -07002176 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002177 LOG(INFO) << "[Unregistering JNI native methods for "
2178 << PrettyDescriptor(c->GetDescriptor()) << "]";
2179 }
2180
2181 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2182 Method* m = c->GetDirectMethod(i);
2183 if (m->IsNative()) {
2184 m->UnregisterNative();
2185 }
2186 }
2187 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2188 Method* m = c->GetVirtualMethod(i);
2189 if (m->IsNative()) {
2190 m->UnregisterNative();
2191 }
2192 }
2193
2194 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Elliott Hughes72025e52011-08-23 17:50:30 -07002197 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002199 Decode<Object*>(ts, java_object)->MonitorEnter();
2200 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
Elliott Hughes72025e52011-08-23 17:50:30 -07002203 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002205 Decode<Object*>(ts, java_object)->MonitorEnter();
2206 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
2209 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2210 ScopedJniThreadState ts(env);
2211 Runtime* runtime = Runtime::Current();
2212 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002213 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 } else {
2215 *vm = NULL;
2216 }
2217 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2218 }
2219
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2221 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002222
2223 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002224 CHECK(address != NULL); // TODO: ReportJniError
2225 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002226
2227 jclass buffer_class = GetDirectByteBufferClass(env);
2228 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2229 if (mid == NULL) {
2230 return NULL;
2231 }
2232
2233 // At the moment, the Java side is limited to 32 bits.
2234 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2235 CHECK_LE(capacity, 0xffffffff);
2236 jint address_arg = reinterpret_cast<jint>(address);
2237 jint capacity_arg = static_cast<jint>(capacity);
2238
2239 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2240 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 }
2242
Elliott Hughesb465ab02011-08-24 11:21:21 -07002243 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002245 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2246 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 }
2248
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002251 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2252 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 }
2254
Elliott Hughesb465ab02011-08-24 11:21:21 -07002255 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002257
Elliott Hughes75770752011-08-24 17:52:38 -07002258 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002259
2260 // Do we definitely know what kind of reference this is?
2261 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2262 IndirectRefKind kind = GetIndirectRefKind(ref);
2263 switch (kind) {
2264 case kLocal:
2265 return JNILocalRefType;
2266 case kGlobal:
2267 return JNIGlobalRefType;
2268 case kWeakGlobal:
2269 return JNIWeakGlobalRefType;
2270 case kSirtOrInvalid:
2271 // Is it in a stack IRT?
2272 if (ts.Self()->SirtContains(java_object)) {
2273 return JNILocalRefType;
2274 }
2275
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002276 if (!ts.Env()->work_around_app_jni_bugs) {
2277 return JNIInvalidRefType;
2278 }
2279
Elliott Hughesb465ab02011-08-24 11:21:21 -07002280 // If we're handing out direct pointers, check whether it's a direct pointer
2281 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002282 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002283 if (ts.Env()->locals.Contains(java_object)) {
2284 return JNILocalRefType;
2285 }
2286 }
2287
2288 return JNIInvalidRefType;
2289 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 }
2291};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002292
Elliott Hughesa2501992011-08-26 19:39:54 -07002293const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002294 NULL, // reserved0.
2295 NULL, // reserved1.
2296 NULL, // reserved2.
2297 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002298 JNI::GetVersion,
2299 JNI::DefineClass,
2300 JNI::FindClass,
2301 JNI::FromReflectedMethod,
2302 JNI::FromReflectedField,
2303 JNI::ToReflectedMethod,
2304 JNI::GetSuperclass,
2305 JNI::IsAssignableFrom,
2306 JNI::ToReflectedField,
2307 JNI::Throw,
2308 JNI::ThrowNew,
2309 JNI::ExceptionOccurred,
2310 JNI::ExceptionDescribe,
2311 JNI::ExceptionClear,
2312 JNI::FatalError,
2313 JNI::PushLocalFrame,
2314 JNI::PopLocalFrame,
2315 JNI::NewGlobalRef,
2316 JNI::DeleteGlobalRef,
2317 JNI::DeleteLocalRef,
2318 JNI::IsSameObject,
2319 JNI::NewLocalRef,
2320 JNI::EnsureLocalCapacity,
2321 JNI::AllocObject,
2322 JNI::NewObject,
2323 JNI::NewObjectV,
2324 JNI::NewObjectA,
2325 JNI::GetObjectClass,
2326 JNI::IsInstanceOf,
2327 JNI::GetMethodID,
2328 JNI::CallObjectMethod,
2329 JNI::CallObjectMethodV,
2330 JNI::CallObjectMethodA,
2331 JNI::CallBooleanMethod,
2332 JNI::CallBooleanMethodV,
2333 JNI::CallBooleanMethodA,
2334 JNI::CallByteMethod,
2335 JNI::CallByteMethodV,
2336 JNI::CallByteMethodA,
2337 JNI::CallCharMethod,
2338 JNI::CallCharMethodV,
2339 JNI::CallCharMethodA,
2340 JNI::CallShortMethod,
2341 JNI::CallShortMethodV,
2342 JNI::CallShortMethodA,
2343 JNI::CallIntMethod,
2344 JNI::CallIntMethodV,
2345 JNI::CallIntMethodA,
2346 JNI::CallLongMethod,
2347 JNI::CallLongMethodV,
2348 JNI::CallLongMethodA,
2349 JNI::CallFloatMethod,
2350 JNI::CallFloatMethodV,
2351 JNI::CallFloatMethodA,
2352 JNI::CallDoubleMethod,
2353 JNI::CallDoubleMethodV,
2354 JNI::CallDoubleMethodA,
2355 JNI::CallVoidMethod,
2356 JNI::CallVoidMethodV,
2357 JNI::CallVoidMethodA,
2358 JNI::CallNonvirtualObjectMethod,
2359 JNI::CallNonvirtualObjectMethodV,
2360 JNI::CallNonvirtualObjectMethodA,
2361 JNI::CallNonvirtualBooleanMethod,
2362 JNI::CallNonvirtualBooleanMethodV,
2363 JNI::CallNonvirtualBooleanMethodA,
2364 JNI::CallNonvirtualByteMethod,
2365 JNI::CallNonvirtualByteMethodV,
2366 JNI::CallNonvirtualByteMethodA,
2367 JNI::CallNonvirtualCharMethod,
2368 JNI::CallNonvirtualCharMethodV,
2369 JNI::CallNonvirtualCharMethodA,
2370 JNI::CallNonvirtualShortMethod,
2371 JNI::CallNonvirtualShortMethodV,
2372 JNI::CallNonvirtualShortMethodA,
2373 JNI::CallNonvirtualIntMethod,
2374 JNI::CallNonvirtualIntMethodV,
2375 JNI::CallNonvirtualIntMethodA,
2376 JNI::CallNonvirtualLongMethod,
2377 JNI::CallNonvirtualLongMethodV,
2378 JNI::CallNonvirtualLongMethodA,
2379 JNI::CallNonvirtualFloatMethod,
2380 JNI::CallNonvirtualFloatMethodV,
2381 JNI::CallNonvirtualFloatMethodA,
2382 JNI::CallNonvirtualDoubleMethod,
2383 JNI::CallNonvirtualDoubleMethodV,
2384 JNI::CallNonvirtualDoubleMethodA,
2385 JNI::CallNonvirtualVoidMethod,
2386 JNI::CallNonvirtualVoidMethodV,
2387 JNI::CallNonvirtualVoidMethodA,
2388 JNI::GetFieldID,
2389 JNI::GetObjectField,
2390 JNI::GetBooleanField,
2391 JNI::GetByteField,
2392 JNI::GetCharField,
2393 JNI::GetShortField,
2394 JNI::GetIntField,
2395 JNI::GetLongField,
2396 JNI::GetFloatField,
2397 JNI::GetDoubleField,
2398 JNI::SetObjectField,
2399 JNI::SetBooleanField,
2400 JNI::SetByteField,
2401 JNI::SetCharField,
2402 JNI::SetShortField,
2403 JNI::SetIntField,
2404 JNI::SetLongField,
2405 JNI::SetFloatField,
2406 JNI::SetDoubleField,
2407 JNI::GetStaticMethodID,
2408 JNI::CallStaticObjectMethod,
2409 JNI::CallStaticObjectMethodV,
2410 JNI::CallStaticObjectMethodA,
2411 JNI::CallStaticBooleanMethod,
2412 JNI::CallStaticBooleanMethodV,
2413 JNI::CallStaticBooleanMethodA,
2414 JNI::CallStaticByteMethod,
2415 JNI::CallStaticByteMethodV,
2416 JNI::CallStaticByteMethodA,
2417 JNI::CallStaticCharMethod,
2418 JNI::CallStaticCharMethodV,
2419 JNI::CallStaticCharMethodA,
2420 JNI::CallStaticShortMethod,
2421 JNI::CallStaticShortMethodV,
2422 JNI::CallStaticShortMethodA,
2423 JNI::CallStaticIntMethod,
2424 JNI::CallStaticIntMethodV,
2425 JNI::CallStaticIntMethodA,
2426 JNI::CallStaticLongMethod,
2427 JNI::CallStaticLongMethodV,
2428 JNI::CallStaticLongMethodA,
2429 JNI::CallStaticFloatMethod,
2430 JNI::CallStaticFloatMethodV,
2431 JNI::CallStaticFloatMethodA,
2432 JNI::CallStaticDoubleMethod,
2433 JNI::CallStaticDoubleMethodV,
2434 JNI::CallStaticDoubleMethodA,
2435 JNI::CallStaticVoidMethod,
2436 JNI::CallStaticVoidMethodV,
2437 JNI::CallStaticVoidMethodA,
2438 JNI::GetStaticFieldID,
2439 JNI::GetStaticObjectField,
2440 JNI::GetStaticBooleanField,
2441 JNI::GetStaticByteField,
2442 JNI::GetStaticCharField,
2443 JNI::GetStaticShortField,
2444 JNI::GetStaticIntField,
2445 JNI::GetStaticLongField,
2446 JNI::GetStaticFloatField,
2447 JNI::GetStaticDoubleField,
2448 JNI::SetStaticObjectField,
2449 JNI::SetStaticBooleanField,
2450 JNI::SetStaticByteField,
2451 JNI::SetStaticCharField,
2452 JNI::SetStaticShortField,
2453 JNI::SetStaticIntField,
2454 JNI::SetStaticLongField,
2455 JNI::SetStaticFloatField,
2456 JNI::SetStaticDoubleField,
2457 JNI::NewString,
2458 JNI::GetStringLength,
2459 JNI::GetStringChars,
2460 JNI::ReleaseStringChars,
2461 JNI::NewStringUTF,
2462 JNI::GetStringUTFLength,
2463 JNI::GetStringUTFChars,
2464 JNI::ReleaseStringUTFChars,
2465 JNI::GetArrayLength,
2466 JNI::NewObjectArray,
2467 JNI::GetObjectArrayElement,
2468 JNI::SetObjectArrayElement,
2469 JNI::NewBooleanArray,
2470 JNI::NewByteArray,
2471 JNI::NewCharArray,
2472 JNI::NewShortArray,
2473 JNI::NewIntArray,
2474 JNI::NewLongArray,
2475 JNI::NewFloatArray,
2476 JNI::NewDoubleArray,
2477 JNI::GetBooleanArrayElements,
2478 JNI::GetByteArrayElements,
2479 JNI::GetCharArrayElements,
2480 JNI::GetShortArrayElements,
2481 JNI::GetIntArrayElements,
2482 JNI::GetLongArrayElements,
2483 JNI::GetFloatArrayElements,
2484 JNI::GetDoubleArrayElements,
2485 JNI::ReleaseBooleanArrayElements,
2486 JNI::ReleaseByteArrayElements,
2487 JNI::ReleaseCharArrayElements,
2488 JNI::ReleaseShortArrayElements,
2489 JNI::ReleaseIntArrayElements,
2490 JNI::ReleaseLongArrayElements,
2491 JNI::ReleaseFloatArrayElements,
2492 JNI::ReleaseDoubleArrayElements,
2493 JNI::GetBooleanArrayRegion,
2494 JNI::GetByteArrayRegion,
2495 JNI::GetCharArrayRegion,
2496 JNI::GetShortArrayRegion,
2497 JNI::GetIntArrayRegion,
2498 JNI::GetLongArrayRegion,
2499 JNI::GetFloatArrayRegion,
2500 JNI::GetDoubleArrayRegion,
2501 JNI::SetBooleanArrayRegion,
2502 JNI::SetByteArrayRegion,
2503 JNI::SetCharArrayRegion,
2504 JNI::SetShortArrayRegion,
2505 JNI::SetIntArrayRegion,
2506 JNI::SetLongArrayRegion,
2507 JNI::SetFloatArrayRegion,
2508 JNI::SetDoubleArrayRegion,
2509 JNI::RegisterNatives,
2510 JNI::UnregisterNatives,
2511 JNI::MonitorEnter,
2512 JNI::MonitorExit,
2513 JNI::GetJavaVM,
2514 JNI::GetStringRegion,
2515 JNI::GetStringUTFRegion,
2516 JNI::GetPrimitiveArrayCritical,
2517 JNI::ReleasePrimitiveArrayCritical,
2518 JNI::GetStringCritical,
2519 JNI::ReleaseStringCritical,
2520 JNI::NewWeakGlobalRef,
2521 JNI::DeleteWeakGlobalRef,
2522 JNI::ExceptionCheck,
2523 JNI::NewDirectByteBuffer,
2524 JNI::GetDirectBufferAddress,
2525 JNI::GetDirectBufferCapacity,
2526 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002527};
2528
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002529static const size_t kMonitorsInitial = 32; // Arbitrary.
2530static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2531
2532static const size_t kLocalsInitial = 64; // Arbitrary.
2533static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002534
Elliott Hughes75770752011-08-24 17:52:38 -07002535JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002536 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002537 vm(vm),
2538 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002539 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002540 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002541 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2542 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002543 functions = unchecked_functions = &gNativeInterface;
2544 if (check_jni) {
2545 functions = GetCheckJniNativeInterface();
2546 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002547}
2548
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002549JNIEnvExt::~JNIEnvExt() {
2550}
2551
Carl Shapiroea4dca82011-08-01 13:45:38 -07002552// JNI Invocation interface.
2553
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002554extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2555 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2556 if (args->version < JNI_VERSION_1_2) {
2557 return JNI_EVERSION;
2558 }
2559 Runtime::Options options;
2560 for (int i = 0; i < args->nOptions; ++i) {
2561 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002562 options.push_back(std::make_pair(StringPiece(option->optionString),
2563 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002564 }
2565 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002566 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002567 if (runtime == NULL) {
2568 return JNI_ERR;
2569 } else {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002570 *p_env = Thread::Current()->GetJniEnv();
2571 *p_vm = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002572 return JNI_OK;
2573 }
2574}
2575
Elliott Hughesf2682d52011-08-15 16:37:04 -07002576extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002577 Runtime* runtime = Runtime::Current();
2578 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002579 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002580 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002581 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002582 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002583 }
2584 return JNI_OK;
2585}
2586
2587// Historically unsupported.
2588extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2589 return JNI_ERR;
2590}
2591
Elliott Hughescdf53122011-08-19 15:46:09 -07002592class JII {
2593 public:
2594 static jint DestroyJavaVM(JavaVM* vm) {
2595 if (vm == NULL) {
2596 return JNI_ERR;
2597 } else {
2598 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2599 delete raw_vm->runtime;
2600 return JNI_OK;
2601 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002602 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002603
Elliott Hughescdf53122011-08-19 15:46:09 -07002604 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002605 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002606 }
2607
2608 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002609 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002610 }
2611
2612 static jint DetachCurrentThread(JavaVM* vm) {
2613 if (vm == NULL) {
2614 return JNI_ERR;
2615 } else {
2616 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2617 Runtime* runtime = raw_vm->runtime;
2618 runtime->DetachCurrentThread();
2619 return JNI_OK;
2620 }
2621 }
2622
2623 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2624 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2625 return JNI_EVERSION;
2626 }
2627 if (vm == NULL || env == NULL) {
2628 return JNI_ERR;
2629 }
2630 Thread* thread = Thread::Current();
2631 if (thread == NULL) {
2632 *env = NULL;
2633 return JNI_EDETACHED;
2634 }
2635 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002636 return JNI_OK;
2637 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002638};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002639
Elliott Hughesa2501992011-08-26 19:39:54 -07002640const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002641 NULL, // reserved0
2642 NULL, // reserved1
2643 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002644 JII::DestroyJavaVM,
2645 JII::AttachCurrentThread,
2646 JII::DetachCurrentThread,
2647 JII::GetEnv,
2648 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002649};
2650
Elliott Hughesbbd76712011-08-17 10:25:24 -07002651static const size_t kPinTableInitialSize = 16;
2652static const size_t kPinTableMaxSize = 1024;
2653
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002654static const size_t kGlobalsInitial = 512; // Arbitrary.
2655static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2656
2657static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2658static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2659
Elliott Hughesa0957642011-09-02 14:27:33 -07002660JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002661 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002662 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002663 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002664 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002665 verbose_jni(options->IsVerbose("jni")),
2666 log_third_party_jni(options->IsVerbose("third-party-jni")),
2667 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002668 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes75770752011-08-24 17:52:38 -07002669 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002670 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002671 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002672 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002673 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002674 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2675 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2676 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002677 functions = unchecked_functions = &gInvokeInterface;
2678 if (check_jni) {
2679 functions = GetCheckJniInvokeInterface();
2680 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002681}
2682
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002683JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002684 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002685 delete globals_lock;
2686 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002687 delete libraries_lock;
2688 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002689}
2690
Elliott Hughes75770752011-08-24 17:52:38 -07002691bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2692 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002693
2694 // See if we've already loaded this library. If we have, and the class loader
2695 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002696 // TODO: for better results we should canonicalize the pathname (or even compare
2697 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002698 SharedLibrary* library;
2699 {
2700 // TODO: move the locking (and more of this logic) into Libraries.
2701 MutexLock mu(libraries_lock);
2702 library = libraries->Get(path);
2703 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002704 if (library != NULL) {
2705 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002706 // The library will be associated with class_loader. The JNI
2707 // spec says we can't load the same library into more than one
2708 // class loader.
2709 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2710 "ClassLoader %p; can't open in ClassLoader %p",
2711 path.c_str(), library->GetClassLoader(), class_loader);
2712 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002713 return false;
2714 }
2715 if (verbose_jni) {
2716 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2717 << "ClassLoader " << class_loader << "]";
2718 }
2719 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002720 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2721 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002722 return false;
2723 }
2724 return true;
2725 }
2726
2727 // Open the shared library. Because we're using a full path, the system
2728 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2729 // resolve this library's dependencies though.)
2730
2731 // Failures here are expected when java.library.path has several entries
2732 // and we have to hunt for the lib.
2733
2734 // The current version of the dynamic linker prints detailed information
2735 // about dlopen() failures. Some things to check if the message is
2736 // cryptic:
2737 // - make sure the library exists on the device
2738 // - verify that the right path is being opened (the debug log message
2739 // above can help with that)
2740 // - check to see if the library is valid (e.g. not zero bytes long)
2741 // - check config/prelink-linux-arm.map to ensure that the library
2742 // is listed and is not being overrun by the previous entry (if
2743 // loading suddenly stops working on a prelinked library, this is
2744 // a good one to check)
2745 // - write a trivial app that calls sleep() then dlopen(), attach
2746 // to it with "strace -p <pid>" while it sleeps, and watch for
2747 // attempts to open nonexistent dependent shared libs
2748
2749 // TODO: automate some of these checks!
2750
2751 // This can execute slowly for a large library on a busy system, so we
2752 // want to switch from RUNNING to VMWAIT while it executes. This allows
2753 // the GC to ignore us.
2754 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002755 void* handle = NULL;
2756 {
2757 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
2758 handle = dlopen(path.c_str(), RTLD_LAZY);
2759 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002760
2761 if (verbose_jni) {
2762 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2763 }
2764
2765 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002766 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002767 return false;
2768 }
2769
2770 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002771 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002772 // TODO: move the locking (and more of this logic) into Libraries.
2773 MutexLock mu(libraries_lock);
2774 library = libraries->Get(path);
2775 if (library != NULL) {
2776 LOG(INFO) << "WOW: we lost a race to add shared library: "
2777 << "\"" << path << "\" ClassLoader=" << class_loader;
2778 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002779 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002780 library = new SharedLibrary(path, handle, class_loader);
2781 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002782 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002783
2784 if (verbose_jni) {
2785 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2786 }
2787
2788 bool result = true;
2789 void* sym = dlsym(handle, "JNI_OnLoad");
2790 if (sym == NULL) {
2791 if (verbose_jni) {
2792 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2793 }
2794 } else {
2795 // Call JNI_OnLoad. We have to override the current class
2796 // loader, which will always be "null" since the stuff at the
2797 // top of the stack is around Runtime.loadLibrary(). (See
2798 // the comments in the JNI FindClass function.)
2799 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2800 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002801 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002802 self->SetClassLoaderOverride(class_loader);
2803
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002804 int version = 0;
2805 {
2806 ScopedThreadStateChange tsc(self, Thread::kNative);
2807 if (verbose_jni) {
2808 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2809 }
2810 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002811 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002812
2813 self->SetClassLoaderOverride(old_class_loader);;
2814
2815 if (version != JNI_VERSION_1_2 &&
2816 version != JNI_VERSION_1_4 &&
2817 version != JNI_VERSION_1_6) {
2818 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2819 << "bad version: " << version;
2820 // It's unwise to call dlclose() here, but we can mark it
2821 // as bad and ensure that future load attempts will fail.
2822 // We don't know how far JNI_OnLoad got, so there could
2823 // be some partially-initialized stuff accessible through
2824 // newly-registered native method calls. We could try to
2825 // unregister them, but that doesn't seem worthwhile.
2826 result = false;
2827 } else {
2828 if (verbose_jni) {
2829 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2830 << " from JNI_OnLoad in \"" << path << "\"]";
2831 }
2832 }
2833 }
2834
2835 library->SetResult(result);
2836 return result;
2837}
2838
2839void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2840 CHECK(m->IsNative());
2841
2842 Class* c = m->GetDeclaringClass();
2843
2844 // If this is a static method, it could be called before the class
2845 // has been initialized.
2846 if (m->IsStatic()) {
2847 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2848 return NULL;
2849 }
2850 } else {
2851 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2852 }
2853
2854 MutexLock mu(libraries_lock);
2855 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002856}
2857
Elliott Hughes410c0c82011-09-01 17:58:25 -07002858void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2859 {
2860 MutexLock mu(globals_lock);
2861 globals.VisitRoots(visitor, arg);
2862 }
2863 {
2864 MutexLock mu(pins_lock);
2865 pin_table.VisitRoots(visitor, arg);
2866 }
2867 // The weak_globals table is visited by the GC itself (because it mutates the table).
2868}
2869
Ian Rogersdf20fe02011-07-20 20:34:16 -07002870} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002871
2872std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2873 switch (rhs) {
2874 case JNIInvalidRefType:
2875 os << "JNIInvalidRefType";
2876 return os;
2877 case JNILocalRefType:
2878 os << "JNILocalRefType";
2879 return os;
2880 case JNIGlobalRefType:
2881 os << "JNIGlobalRefType";
2882 return os;
2883 case JNIWeakGlobalRefType:
2884 os << "JNIWeakGlobalRefType";
2885 return os;
2886 }
2887}