blob: bd1ccecdb2ff6aa42a04e49f03d8fec76c9823c1 [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 Carlstrom69b15fb2011-09-03 12:25:21 -0700210
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700211 if (method->HasCode() && stub != NULL) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700212 (*stub)(method, receiver, self, args, &result);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700213 } else {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700214 LOG(WARNING) << "Not invoking method with no associated code: "
Elliott Hughesa2501992011-08-26 19:39:54 -0700215 << PrettyMethod(method);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700216 result.j = 0;
217 }
buzbeec143c552011-08-20 17:38:58 -0700218
Ian Rogers6de08602011-08-19 14:52:39 -0700219 // Pop transition
220 self->PopNativeToManagedRecord(record);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700221 return result;
222}
223
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700224JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700225 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700226 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700227 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700228 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700229 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700230}
231
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700232JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700233 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700234 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700235 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700236 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700237 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
238}
239
Elliott Hughes75770752011-08-24 17:52:38 -0700240Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700241 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700242}
243
Brian Carlstrom30b94452011-08-25 21:35:26 -0700244JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700245 Object* receiver = Decode<Object*>(ts, obj);
246 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700247 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700248 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
249}
250
Brian Carlstrom30b94452011-08-25 21:35:26 -0700251JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700252 Object* receiver = Decode<Object*>(ts, obj);
253 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700254 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700255 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700256}
257
Elliott Hughes6b436852011-08-12 10:16:44 -0700258// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
259// separated with slashes but aren't wrapped with "L;" like regular descriptors
260// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
261// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
262// supported names with dots too (such as "a.b.C").
263std::string NormalizeJniClassDescriptor(const char* name) {
264 std::string result;
265 // Add the missing "L;" if necessary.
266 if (name[0] == '[') {
267 result = name;
268 } else {
269 result += 'L';
270 result += name;
271 result += ';';
272 }
273 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700274 if (result.find('.') != std::string::npos) {
275 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
276 << "\"" << name << "\"";
277 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700278 }
279 return result;
280}
281
Elliott Hughescdf53122011-08-19 15:46:09 -0700282jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
283 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700284 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
285 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700286 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700287
288 Method* method = NULL;
289 if (is_static) {
290 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700291 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700292 method = c->FindVirtualMethod(name, sig);
293 if (method == NULL) {
294 // No virtual method matching the signature. Search declared
295 // private methods and constructors.
296 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700297 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700298 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700299
Elliott Hughescdf53122011-08-19 15:46:09 -0700300 if (method == NULL || method->IsStatic() != is_static) {
301 Thread* self = Thread::Current();
Elliott Hughesa2501992011-08-26 19:39:54 -0700302 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700303 // TODO: try searching for the opposite kind of method from is_static
304 // for better diagnostics?
305 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700306 "no %s method %s", is_static ? "static" : "non-static",
307 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700308 return NULL;
309 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700310
Elliott Hughescdf53122011-08-19 15:46:09 -0700311 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700312}
313
Elliott Hughescdf53122011-08-19 15:46:09 -0700314jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
315 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700316 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
317 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700318 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700319
320 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700321 Class* field_type;
322 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
323 if (sig[1] != '\0') {
324 // TODO: need to get the appropriate ClassLoader.
325 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
326 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700327 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700328 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700329 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700330 if (field_type == NULL) {
331 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700332 DCHECK(ts.Self()->IsExceptionPending());
333 ts.Self()->ClearException();
334 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
335 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
336 "no type \"%s\" found and so no field \"%s\" could be found in class "
337 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700338 return NULL;
339 }
340 if (is_static) {
341 field = c->FindStaticField(name, field_type);
342 } else {
343 field = c->FindInstanceField(name, field_type);
344 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700345 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700346 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700347 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700348 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700349 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700350 return NULL;
351 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700352 // Check invariant that all jfieldIDs have resolved types (how else would
353 // the type equality in Find...Field hold?)
354 DCHECK(field->GetType() != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700355 jweak fid = AddWeakGlobalReference(ts, field);
356 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700357}
358
Elliott Hughes75770752011-08-24 17:52:38 -0700359void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
360 JavaVMExt* vm = ts.Vm();
361 MutexLock mu(vm->pins_lock);
362 vm->pin_table.Add(array);
363}
364
365void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
366 JavaVMExt* vm = ts.Vm();
367 MutexLock mu(vm->pins_lock);
368 vm->pin_table.Remove(array);
369}
370
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700371template<typename JniT, typename ArtT>
372JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
373 CHECK_GE(length, 0); // TODO: ReportJniError
374 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700375 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700376}
377
Elliott Hughes75770752011-08-24 17:52:38 -0700378template <typename ArrayT, typename CArrayT, typename ArtArrayT>
379CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
380 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
381 PinPrimitiveArray(ts, array);
382 if (is_copy != NULL) {
383 *is_copy = JNI_FALSE;
384 }
385 return array->GetData();
386}
387
388template <typename ArrayT>
389void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
390 if (mode != JNI_COMMIT) {
391 Array* array = Decode<Array*>(ts, java_array);
392 UnpinPrimitiveArray(ts, array);
393 }
394}
395
Elliott Hughes814e4032011-08-23 12:07:56 -0700396void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
397 std::string type(PrettyType(array));
398 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
399 "%s offset=%d length=%d %s.length=%d",
400 type.c_str(), start, length, identifier, array->GetLength());
401}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700402void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
403 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
404 "offset=%d length=%d string.length()=%d", start, length, array_length);
405}
Elliott Hughes814e4032011-08-23 12:07:56 -0700406
407template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700408void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700409 ArrayT* array = Decode<ArrayT*>(ts, java_array);
410 if (start < 0 || length < 0 || start + length > array->GetLength()) {
411 ThrowAIOOBE(ts, array, start, length, "src");
412 } else {
413 JavaT* data = array->GetData();
414 memcpy(buf, data + start, length * sizeof(JavaT));
415 }
416}
417
418template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700419void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700420 ArrayT* array = Decode<ArrayT*>(ts, java_array);
421 if (start < 0 || length < 0 || start + length > array->GetLength()) {
422 ThrowAIOOBE(ts, array, start, length, "dst");
423 } else {
424 JavaT* data = array->GetData();
425 memcpy(data + start, buf, length * sizeof(JavaT));
426 }
427}
428
Elliott Hughes75770752011-08-24 17:52:38 -0700429jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700430 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
431 CHECK(buffer_class.get() != NULL);
432 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
433}
434
Elliott Hughes75770752011-08-24 17:52:38 -0700435jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700436 static jclass buffer_class = InitDirectByteBufferClass(env);
437 return buffer_class;
438}
439
Elliott Hughes75770752011-08-24 17:52:38 -0700440jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
441 if (vm == NULL || p_env == NULL) {
442 return JNI_ERR;
443 }
444
445 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
446 JavaVMAttachArgs args;
447 if (thr_args == NULL) {
448 // Allow the v1.1 calling convention.
449 args.version = JNI_VERSION_1_2;
450 args.name = NULL;
451 args.group = NULL; // TODO: get "main" thread group
452 } else {
453 args.version = in_args->version;
454 args.name = in_args->name;
455 if (in_args->group != NULL) {
456 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
457 args.group = NULL; // TODO: decode in_args->group
458 } else {
459 args.group = NULL; // TODO: get "main" thread group
460 }
461 }
462 CHECK_GE(args.version, JNI_VERSION_1_2);
463
464 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700465 runtime->AttachCurrentThread(args.name, p_env, as_daemon);
466 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700467}
468
Elliott Hughes79082e32011-08-25 12:07:32 -0700469class SharedLibrary {
470 public:
471 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
472 : path_(path),
473 handle_(handle),
474 jni_on_load_lock_(Mutex::Create("JNI_OnLoad lock")),
475 jni_on_load_tid_(Thread::Current()->GetId()),
476 jni_on_load_result_(kPending) {
477 pthread_cond_init(&jni_on_load_cond_, NULL);
478 }
479
480 ~SharedLibrary() {
481 delete jni_on_load_lock_;
482 }
483
484 Object* GetClassLoader() {
485 return class_loader_;
486 }
487
488 std::string GetPath() {
489 return path_;
490 }
491
492 /*
493 * Check the result of an earlier call to JNI_OnLoad on this library. If
494 * the call has not yet finished in another thread, wait for it.
495 */
496 bool CheckOnLoadResult(JavaVMExt* vm) {
497 Thread* self = Thread::Current();
498 if (jni_on_load_tid_ == self->GetId()) {
499 // Check this so we don't end up waiting for ourselves. We need
500 // to return "true" so the caller can continue.
501 LOG(INFO) << *self << " recursive attempt to load library "
502 << "\"" << path_ << "\"";
503 return true;
504 }
505
506 MutexLock mu(jni_on_load_lock_);
507 while (jni_on_load_result_ == kPending) {
508 if (vm->verbose_jni) {
509 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
510 << "JNI_OnLoad...]";
511 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700512 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes79082e32011-08-25 12:07:32 -0700513 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_->GetImpl());
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 }
515
516 bool okay = (jni_on_load_result_ == kOkay);
517 if (vm->verbose_jni) {
518 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
519 << (okay ? "succeeded" : "failed") << "]";
520 }
521 return okay;
522 }
523
524 void SetResult(bool result) {
525 jni_on_load_result_ = result ? kOkay : kFailed;
526 jni_on_load_tid_ = 0;
527
528 // Broadcast a wakeup to anybody sleeping on the condition variable.
529 MutexLock mu(jni_on_load_lock_);
530 pthread_cond_broadcast(&jni_on_load_cond_);
531 }
532
533 void* FindSymbol(const std::string& symbol_name) {
534 return dlsym(handle_, symbol_name.c_str());
535 }
536
537 private:
538 enum JNI_OnLoadState {
539 kPending,
540 kFailed,
541 kOkay,
542 };
543
544 // Path to library "/system/lib/libjni.so".
545 std::string path_;
546
547 // The void* returned by dlopen(3).
548 void* handle_;
549
550 // The ClassLoader this library is associated with.
551 Object* class_loader_;
552
553 // Guards remaining items.
554 Mutex* jni_on_load_lock_;
555 // Wait for JNI_OnLoad in other thread.
556 pthread_cond_t jni_on_load_cond_;
557 // Recursive invocation guard.
558 uint32_t jni_on_load_tid_;
559 // Result of earlier JNI_OnLoad call.
560 JNI_OnLoadState jni_on_load_result_;
561};
562
Elliott Hughescdf53122011-08-19 15:46:09 -0700563} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700564
Elliott Hughes79082e32011-08-25 12:07:32 -0700565// This exists mainly to keep implementation details out of the header file.
566class Libraries {
567 public:
568 Libraries() {
569 }
570
571 ~Libraries() {
572 // Delete our map values. (The keys will be cleaned up by the map itself.)
573 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
574 delete it->second;
575 }
576 }
577
578 SharedLibrary* Get(const std::string& path) {
579 return libraries_[path];
580 }
581
582 void Put(const std::string& path, SharedLibrary* library) {
583 libraries_[path] = library;
584 }
585
586 // See section 11.3 "Linking Native Methods" of the JNI spec.
587 void* FindNativeMethod(const Method* m) {
588 std::string jni_short_name(JniShortName(m));
589 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700590 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700591 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
592 SharedLibrary* library = it->second;
593 if (library->GetClassLoader() != declaring_class_loader) {
594 // We only search libraries loaded by the appropriate ClassLoader.
595 continue;
596 }
597 // Try the short name then the long name...
598 void* fn = library->FindSymbol(jni_short_name);
599 if (fn == NULL) {
600 fn = library->FindSymbol(jni_long_name);
601 }
602 if (fn != NULL) {
603 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700604 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700605 << " in \"" << library->GetPath() << "\"]";
606 }
607 return fn;
608 }
609 }
610 std::string detail;
611 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700612 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700613 LOG(ERROR) << detail;
614 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
615 "%s", detail.c_str());
616 return NULL;
617 }
618
619 private:
620 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
621
622 std::map<std::string, SharedLibrary*> libraries_;
623};
624
Elliott Hughescdf53122011-08-19 15:46:09 -0700625class JNI {
626 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700627
Elliott Hughescdf53122011-08-19 15:46:09 -0700628 static jint GetVersion(JNIEnv* env) {
629 ScopedJniThreadState ts(env);
630 return JNI_VERSION_1_6;
631 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700632
Elliott Hughescdf53122011-08-19 15:46:09 -0700633 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
634 ScopedJniThreadState ts(env);
635 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700636 return NULL;
637 }
638
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 static jclass FindClass(JNIEnv* env, const char* name) {
640 ScopedJniThreadState ts(env);
641 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
642 std::string descriptor(NormalizeJniClassDescriptor(name));
643 // TODO: need to get the appropriate ClassLoader.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700644 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
buzbeec143c552011-08-20 17:38:58 -0700645 Class* c = class_linker->FindClass(descriptor, cl);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700646 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700647 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700648
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
650 ScopedJniThreadState ts(env);
651 Method* method = Decode<Method*>(ts, java_method);
652 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700653 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700654
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
656 ScopedJniThreadState ts(env);
657 Field* field = Decode<Field*>(ts, java_field);
658 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
659 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700660
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
662 ScopedJniThreadState ts(env);
663 Method* method = DecodeMethod(ts, mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700664 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700665 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700666
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
668 ScopedJniThreadState ts(env);
669 Field* field = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700670 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700672
Elliott Hughes37f7a402011-08-22 18:56:01 -0700673 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
674 ScopedJniThreadState ts(env);
675 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700676 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700677 }
678
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700679 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700681 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700682 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700684
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700687 Class* c1 = Decode<Class*>(ts, java_class1);
688 Class* c2 = Decode<Class*>(ts, java_class2);
689 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700691
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700694 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700695 if (jobj == NULL) {
696 // NB. JNI is different from regular Java instanceof in this respect
697 return JNI_TRUE;
698 } else {
699 Object* obj = Decode<Object*>(ts, jobj);
700 Class* klass = Decode<Class*>(ts, clazz);
701 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
702 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700704
Elliott Hughes37f7a402011-08-22 18:56:01 -0700705 static jint Throw(JNIEnv* env, jthrowable java_exception) {
706 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700707 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 if (exception == NULL) {
709 return JNI_ERR;
710 }
711 ts.Self()->SetException(exception);
712 return JNI_OK;
713 }
714
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700715 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700716 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700717 // TODO: check for a pending exception to decide what constructor to call.
718 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
719 if (mid == NULL) {
720 return JNI_ERR;
721 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700722 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
723 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700724 return JNI_ERR;
725 }
726
727 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700728 args[0].l = s.get();
729 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
730 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700731 return JNI_ERR;
732 }
733
Elliott Hughes72025e52011-08-23 17:50:30 -0700734 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700735 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700736 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700737
Elliott Hughes37f7a402011-08-22 18:56:01 -0700738 return JNI_OK;
739 }
740
741 static jboolean ExceptionCheck(JNIEnv* env) {
742 ScopedJniThreadState ts(env);
743 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
744 }
745
746 static void ExceptionClear(JNIEnv* env) {
747 ScopedJniThreadState ts(env);
748 ts.Self()->ClearException();
749 }
750
751 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700753
754 Thread* self = ts.Self();
755 Throwable* original_exception = self->GetException();
756 self->ClearException();
757
Elliott Hughesbf86d042011-08-31 17:53:14 -0700758 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700759 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
760 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
761 if (mid == NULL) {
762 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
763 << PrettyType(original_exception);
764 } else {
765 env->CallVoidMethod(exception.get(), mid);
766 if (self->IsExceptionPending()) {
767 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
768 << " thrown while calling printStackTrace";
769 self->ClearException();
770 }
771 }
772
773 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700775
Elliott Hughescdf53122011-08-19 15:46:09 -0700776 static jthrowable ExceptionOccurred(JNIEnv* env) {
777 ScopedJniThreadState ts(env);
778 Object* exception = ts.Self()->GetException();
779 if (exception == NULL) {
780 return NULL;
781 } else {
782 // TODO: if adding a local reference failing causes the VM to abort
783 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700784 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 if (localException == NULL) {
786 // We were unable to add a new local reference, and threw a new
787 // exception. We can't return "exception", because it's not a
788 // local reference. So we have to return NULL, indicating that
789 // there was no exception, even though it's pretty much raining
790 // exceptions in here.
791 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
792 }
793 return localException;
794 }
795 }
796
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 static void FatalError(JNIEnv* env, const char* msg) {
798 ScopedJniThreadState ts(env);
799 LOG(FATAL) << "JNI FatalError called: " << msg;
800 }
801
802 static jint PushLocalFrame(JNIEnv* env, jint cap) {
803 ScopedJniThreadState ts(env);
804 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
805 return JNI_OK;
806 }
807
808 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
809 ScopedJniThreadState ts(env);
810 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
811 return res;
812 }
813
Elliott Hughes72025e52011-08-23 17:50:30 -0700814 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
815 ScopedJniThreadState ts(env);
816 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
817 return 0;
818 }
819
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
821 ScopedJniThreadState ts(env);
822 if (obj == NULL) {
823 return NULL;
824 }
825
Elliott Hughes75770752011-08-24 17:52:38 -0700826 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 IndirectReferenceTable& globals = vm->globals;
828 MutexLock mu(vm->globals_lock);
829 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
830 return reinterpret_cast<jobject>(ref);
831 }
832
833 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
834 ScopedJniThreadState ts(env);
835 if (obj == NULL) {
836 return;
837 }
838
Elliott Hughes75770752011-08-24 17:52:38 -0700839 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 IndirectReferenceTable& globals = vm->globals;
841 MutexLock mu(vm->globals_lock);
842
843 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
844 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
845 << "failed to find entry";
846 }
847 }
848
849 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
850 ScopedJniThreadState ts(env);
851 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
852 }
853
854 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
855 ScopedJniThreadState ts(env);
856 if (obj == NULL) {
857 return;
858 }
859
Elliott Hughes75770752011-08-24 17:52:38 -0700860 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 IndirectReferenceTable& weak_globals = vm->weak_globals;
862 MutexLock mu(vm->weak_globals_lock);
863
864 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
865 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
866 << "failed to find entry";
867 }
868 }
869
870 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
871 ScopedJniThreadState ts(env);
872 if (obj == NULL) {
873 return NULL;
874 }
875
876 IndirectReferenceTable& locals = ts.Env()->locals;
877
878 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
879 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
880 return reinterpret_cast<jobject>(ref);
881 }
882
883 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
884 ScopedJniThreadState ts(env);
885 if (obj == NULL) {
886 return;
887 }
888
889 IndirectReferenceTable& locals = ts.Env()->locals;
890
891 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
892 if (!locals.Remove(cookie, obj)) {
893 // Attempting to delete a local reference that is not in the
894 // topmost local reference frame is a no-op. DeleteLocalRef returns
895 // void and doesn't throw any exceptions, but we should probably
896 // complain about it so the user will notice that things aren't
897 // going quite the way they expect.
898 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
899 << "failed to find entry";
900 }
901 }
902
903 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
904 ScopedJniThreadState ts(env);
905 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
906 ? JNI_TRUE : JNI_FALSE;
907 }
908
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700909 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700911 Class* c = Decode<Class*>(ts, java_class);
912 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
913 return NULL;
914 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700915 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 }
917
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 ScopedJniThreadState ts(env);
920 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 va_start(args, mid);
922 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 va_end(args);
924 return result;
925 }
926
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700929 Class* c = Decode<Class*>(ts, java_class);
930 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
931 return NULL;
932 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700933 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700934 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700935 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 return local_result;
937 }
938
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700941 Class* c = Decode<Class*>(ts, java_class);
942 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
943 return NULL;
944 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700945 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700946 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 return local_result;
949 }
950
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
952 ScopedJniThreadState ts(env);
953 return FindMethodID(ts, c, name, sig, false);
954 }
955
956 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
957 ScopedJniThreadState ts(env);
958 return FindMethodID(ts, c, name, sig, true);
959 }
960
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 va_list ap;
964 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700965 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700967 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700972 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700973 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700978 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700979 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 }
981
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700984 va_list ap;
985 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700986 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 va_end(ap);
988 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 }
990
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700993 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700998 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001003 va_list ap;
1004 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001005 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 va_end(ap);
1007 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001012 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001017 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 }
1019
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_list ap;
1023 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001024 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 va_end(ap);
1026 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001031 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001036 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 va_list ap;
1042 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001043 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 va_end(ap);
1045 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001050 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001055 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_list ap;
1061 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001062 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_end(ap);
1064 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001069 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001074 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 va_list ap;
1080 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001081 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 va_end(ap);
1083 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001088 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001093 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 va_list ap;
1099 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001100 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_end(ap);
1102 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001107 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001112 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_list ap;
1118 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001119 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 va_end(ap);
1121 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 }
1123
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001126 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001131 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 va_list ap;
1137 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001138 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001144 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001149 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
1152 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 ScopedJniThreadState ts(env);
1155 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 va_start(ap, mid);
1157 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001158 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 va_end(ap);
1160 return local_result;
1161 }
1162
1163 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001167 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
1170 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001174 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
1177 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 ScopedJniThreadState ts(env);
1180 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 va_start(ap, mid);
1182 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 va_end(ap);
1184 return result.z;
1185 }
1186
1187 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
1193 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
1199 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 ScopedJniThreadState ts(env);
1202 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 va_start(ap, mid);
1204 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 va_end(ap);
1206 return result.b;
1207 }
1208
1209 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
1215 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
1221 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
1224 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 va_start(ap, mid);
1226 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 va_end(ap);
1228 return result.c;
1229 }
1230
1231 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001232 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 }
1236
1237 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
1243 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001244 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 ScopedJniThreadState ts(env);
1246 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 va_start(ap, mid);
1248 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 va_end(ap);
1250 return result.s;
1251 }
1252
1253 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001256 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
1259 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 }
1264
1265 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001266 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 ScopedJniThreadState ts(env);
1268 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 va_start(ap, mid);
1270 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 va_end(ap);
1272 return result.i;
1273 }
1274
1275 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001276 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 }
1280
1281 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
1287 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001288 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 ScopedJniThreadState ts(env);
1290 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 va_start(ap, mid);
1292 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 va_end(ap);
1294 return result.j;
1295 }
1296
1297 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001298 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001300 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
1303 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001304 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001306 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
1309 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001310 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 ScopedJniThreadState ts(env);
1312 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 va_start(ap, mid);
1314 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 va_end(ap);
1316 return result.f;
1317 }
1318
1319 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001320 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001322 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
1325 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
1331 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 ScopedJniThreadState ts(env);
1334 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 va_start(ap, mid);
1336 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 va_end(ap);
1338 return result.d;
1339 }
1340
1341 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001342 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 }
1346
1347 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001348 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001350 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
1353 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 ScopedJniThreadState ts(env);
1356 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001357 va_start(ap, mid);
1358 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 va_end(ap);
1360 }
1361
1362 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001363 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001365 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
1367
1368 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001369 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001371 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
1374 static jfieldID GetFieldID(JNIEnv* env,
1375 jclass c, const char* name, const char* sig) {
1376 ScopedJniThreadState ts(env);
1377 return FindFieldID(ts, c, name, sig, false);
1378 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001379
1380
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 static jfieldID GetStaticFieldID(JNIEnv* env,
1382 jclass c, const char* name, const char* sig) {
1383 ScopedJniThreadState ts(env);
1384 return FindFieldID(ts, c, name, sig, true);
1385 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001386
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001387 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389 Object* o = Decode<Object*>(ts, obj);
1390 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001391 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001393
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001397 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 Object* o = Decode<Object*>(ts, java_object);
1403 Object* v = Decode<Object*>(ts, java_value);
1404 Field* f = DecodeField(ts, fid);
1405 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001408 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001410 Object* v = Decode<Object*>(ts, java_value);
1411 Field* f = DecodeField(ts, fid);
1412 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001415#define GET_PRIMITIVE_FIELD(fn, instance) \
1416 ScopedJniThreadState ts(env); \
1417 Object* o = Decode<Object*>(ts, instance); \
1418 Field* f = DecodeField(ts, fid); \
1419 return f->fn(o)
1420
1421#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1422 ScopedJniThreadState ts(env); \
1423 Object* o = Decode<Object*>(ts, instance); \
1424 Field* f = DecodeField(ts, fid); \
1425 f->fn(o, value)
1426
1427 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1428 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001431 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1432 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001435 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1436 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 }
1438
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001439 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1440 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001443 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1444 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001447 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1448 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001451 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1452 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1456 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001459 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1460 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1464 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1468 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1472 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1476 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1480 GET_PRIMITIVE_FIELD(GetLong, NULL);
1481 }
1482
1483 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1484 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1485 }
1486
1487 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1488 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1489 }
1490
1491 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1492 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1493 }
1494
1495 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1496 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1497 }
1498
1499 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1500 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1501 }
1502
1503 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1504 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1505 }
1506
1507 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1508 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1509 }
1510
1511 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1512 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1513 }
1514
1515 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1516 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1517 }
1518
1519 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1520 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1521 }
1522
1523 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1524 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1525 }
1526
1527 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1528 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1529 }
1530
1531 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1532 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1533 }
1534
1535 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1536 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1537 }
1538
1539 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1540 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1541 }
1542
1543 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1544 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1545 }
1546
1547 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1548 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1549 }
1550
1551 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1552 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
1555 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001556 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 ScopedJniThreadState ts(env);
1558 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001559 va_start(ap, mid);
1560 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001561 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 va_end(ap);
1563 return local_result;
1564 }
1565
1566 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001567 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001569 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001570 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 }
1572
1573 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001574 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001576 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001577 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
1580 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001581 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 ScopedJniThreadState ts(env);
1583 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001584 va_start(ap, mid);
1585 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 va_end(ap);
1587 return result.z;
1588 }
1589
1590 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001591 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001593 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
1596 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001597 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001599 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 }
1601
Elliott Hughes72025e52011-08-23 17:50:30 -07001602 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 ScopedJniThreadState ts(env);
1604 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 va_start(ap, mid);
1606 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 va_end(ap);
1608 return result.b;
1609 }
1610
1611 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001612 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001614 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 }
1616
1617 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001618 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001620 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 }
1622
Elliott Hughes72025e52011-08-23 17:50:30 -07001623 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 ScopedJniThreadState ts(env);
1625 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 va_start(ap, mid);
1627 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 va_end(ap);
1629 return result.c;
1630 }
1631
1632 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001633 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001635 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 }
1637
1638 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001639 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001641 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 }
1643
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 ScopedJniThreadState ts(env);
1646 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 va_start(ap, mid);
1648 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 va_end(ap);
1650 return result.s;
1651 }
1652
1653 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001654 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001656 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 }
1658
1659 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001660 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001662 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 }
1664
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 ScopedJniThreadState ts(env);
1667 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 va_start(ap, mid);
1669 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 va_end(ap);
1671 return result.i;
1672 }
1673
1674 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001675 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001677 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 }
1679
1680 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001681 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001683 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 ScopedJniThreadState ts(env);
1688 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 va_start(ap, mid);
1690 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 va_end(ap);
1692 return result.j;
1693 }
1694
1695 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001696 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001698 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 }
1700
1701 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001702 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001704 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 ScopedJniThreadState ts(env);
1709 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 va_start(ap, mid);
1711 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 va_end(ap);
1713 return result.f;
1714 }
1715
1716 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001717 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 }
1721
1722 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001723 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001725 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 }
1727
Elliott Hughes72025e52011-08-23 17:50:30 -07001728 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 ScopedJniThreadState ts(env);
1730 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 va_start(ap, mid);
1732 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 va_end(ap);
1734 return result.d;
1735 }
1736
1737 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001738 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001740 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
1743 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001744 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001746 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes72025e52011-08-23 17:50:30 -07001749 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 ScopedJniThreadState ts(env);
1751 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001752 va_start(ap, mid);
1753 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 va_end(ap);
1755 }
1756
1757 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001758 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001760 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
1763 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001764 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001766 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 }
1768
Elliott Hughes814e4032011-08-23 12:07:56 -07001769 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001771 if (chars == NULL && char_count == 0) {
1772 return NULL;
1773 }
1774 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001775 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 }
1777
1778 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1779 ScopedJniThreadState ts(env);
1780 if (utf == NULL) {
1781 return NULL;
1782 }
1783 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001784 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
Elliott Hughes814e4032011-08-23 12:07:56 -07001787 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1788 ScopedJniThreadState ts(env);
1789 return Decode<String*>(ts, java_string)->GetLength();
1790 }
1791
1792 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1793 ScopedJniThreadState ts(env);
1794 return Decode<String*>(ts, java_string)->GetUtfLength();
1795 }
1796
Elliott Hughesb465ab02011-08-24 11:21:21 -07001797 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001799 String* s = Decode<String*>(ts, java_string);
1800 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1801 ThrowSIOOBE(ts, start, length, s->GetLength());
1802 } else {
1803 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1804 memcpy(buf, chars + start, length * sizeof(jchar));
1805 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001806 }
1807
Elliott Hughesb465ab02011-08-24 11:21:21 -07001808 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001809 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001810 String* s = Decode<String*>(ts, java_string);
1811 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1812 ThrowSIOOBE(ts, start, length, s->GetLength());
1813 } else {
1814 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1815 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1816 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001817 }
1818
Elliott Hughes75770752011-08-24 17:52:38 -07001819 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001820 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001821 String* s = Decode<String*>(ts, java_string);
1822 const CharArray* chars = s->GetCharArray();
1823 PinPrimitiveArray(ts, chars);
1824 if (is_copy != NULL) {
1825 *is_copy = JNI_FALSE;
1826 }
1827 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001828 }
1829
Elliott Hughes75770752011-08-24 17:52:38 -07001830 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001831 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001832 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 }
1834
Elliott Hughes75770752011-08-24 17:52:38 -07001835 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001837 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 }
1839
Elliott Hughes75770752011-08-24 17:52:38 -07001840 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001842 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 }
1844
Elliott Hughes75770752011-08-24 17:52:38 -07001845 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001846 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001847 if (java_string == NULL) {
1848 return NULL;
1849 }
1850 if (is_copy != NULL) {
1851 *is_copy = JNI_TRUE;
1852 }
1853 String* s = Decode<String*>(ts, java_string);
1854 size_t byte_count = s->GetUtfLength();
1855 char* bytes = new char[byte_count + 1];
1856 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001857 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001858 return NULL;
1859 }
1860 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1861 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1862 bytes[byte_count] = '\0';
1863 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001864 }
1865
Elliott Hughes75770752011-08-24 17:52:38 -07001866 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001867 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001868 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001869 }
1870
Elliott Hughesbd935992011-08-22 11:59:34 -07001871 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001873 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001874 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001875 Array* array = obj->AsArray();
1876 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 }
1878
Elliott Hughes814e4032011-08-23 12:07:56 -07001879 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001881 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001882 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
1884
1885 static void SetObjectArrayElement(JNIEnv* env,
1886 jobjectArray java_array, jsize index, jobject java_value) {
1887 ScopedJniThreadState ts(env);
1888 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1889 Object* value = Decode<Object*>(ts, java_value);
1890 array->Set(index, value);
1891 }
1892
1893 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1894 ScopedJniThreadState ts(env);
1895 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1896 }
1897
1898 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1899 ScopedJniThreadState ts(env);
1900 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1901 }
1902
1903 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1904 ScopedJniThreadState ts(env);
1905 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1906 }
1907
1908 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1909 ScopedJniThreadState ts(env);
1910 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1911 }
1912
1913 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1914 ScopedJniThreadState ts(env);
1915 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1916 }
1917
1918 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1919 ScopedJniThreadState ts(env);
1920 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1921 }
1922
1923 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1924 ScopedJniThreadState ts(env);
1925 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1926 }
1927
1928 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1929 ScopedJniThreadState ts(env);
1930 CHECK_GE(length, 0); // TODO: ReportJniError
1931
1932 // Compute the array class corresponding to the given element class.
1933 Class* element_class = Decode<Class*>(ts, element_jclass);
1934 std::string descriptor;
1935 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001936 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001937
1938 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001939 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1940 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 return NULL;
1942 }
1943
Elliott Hughes75770752011-08-24 17:52:38 -07001944 // Allocate and initialize if necessary.
1945 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001947 if (initial_element != NULL) {
1948 Object* initial_object = Decode<Object*>(ts, initial_element);
1949 for (jsize i = 0; i < length; ++i) {
1950 result->Set(i, initial_object);
1951 }
1952 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001953 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
1956 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1957 ScopedJniThreadState ts(env);
1958 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1959 }
1960
Elliott Hughes75770752011-08-24 17:52:38 -07001961 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001962 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001963 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001964 }
1965
Elliott Hughes75770752011-08-24 17:52:38 -07001966 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001967 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001968 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001969 }
1970
Elliott Hughes75770752011-08-24 17:52:38 -07001971 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001973 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Elliott Hughes75770752011-08-24 17:52:38 -07001976 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001978 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Elliott Hughes75770752011-08-24 17:52:38 -07001981 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001983 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Elliott Hughes75770752011-08-24 17:52:38 -07001986 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001988 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes75770752011-08-24 17:52:38 -07001991 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001993 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes75770752011-08-24 17:52:38 -07001996 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001998 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Elliott Hughes75770752011-08-24 17:52:38 -07002001 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002003 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Elliott Hughes75770752011-08-24 17:52:38 -07002006 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002008 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Elliott Hughes75770752011-08-24 17:52:38 -07002011 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002013 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes75770752011-08-24 17:52:38 -07002016 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002018 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes75770752011-08-24 17:52:38 -07002021 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002023 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes75770752011-08-24 17:52:38 -07002026 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002028 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes75770752011-08-24 17:52:38 -07002031 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002033 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes75770752011-08-24 17:52:38 -07002036 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002038 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes75770752011-08-24 17:52:38 -07002041 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002043 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Elliott Hughes75770752011-08-24 17:52:38 -07002046 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002048 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Elliott Hughes814e4032011-08-23 12:07:56 -07002051 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Elliott Hughes814e4032011-08-23 12:07:56 -07002126 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
Elliott Hughes5174fe62011-08-23 15:12:35 -07002131 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002133 Class* c = Decode<Class*>(ts, java_class);
2134
Elliott Hughes5174fe62011-08-23 15:12:35 -07002135 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 const char* name = methods[i].name;
2137 const char* sig = methods[i].signature;
2138
2139 if (*sig == '!') {
2140 // TODO: fast jni. it's too noisy to log all these.
2141 ++sig;
2142 }
2143
Elliott Hughes5174fe62011-08-23 15:12:35 -07002144 Method* m = c->FindDirectMethod(name, sig);
2145 if (m == NULL) {
2146 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002148 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2152 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002153 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002157 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2159 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002160 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 return JNI_ERR;
2162 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002163
Elliott Hughes75770752011-08-24 17:52:38 -07002164 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002165 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002166 }
2167
2168 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170 return JNI_OK;
2171 }
2172
Elliott Hughes5174fe62011-08-23 15:12:35 -07002173 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002175 Class* c = Decode<Class*>(ts, java_class);
2176
Elliott Hughes75770752011-08-24 17:52:38 -07002177 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002178 LOG(INFO) << "[Unregistering JNI native methods for "
2179 << PrettyDescriptor(c->GetDescriptor()) << "]";
2180 }
2181
2182 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2183 Method* m = c->GetDirectMethod(i);
2184 if (m->IsNative()) {
2185 m->UnregisterNative();
2186 }
2187 }
2188 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2189 Method* m = c->GetVirtualMethod(i);
2190 if (m->IsNative()) {
2191 m->UnregisterNative();
2192 }
2193 }
2194
2195 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Elliott Hughes72025e52011-08-23 17:50:30 -07002198 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002200 Decode<Object*>(ts, java_object)->MonitorEnter();
2201 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 }
2203
Elliott Hughes72025e52011-08-23 17:50:30 -07002204 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002206 Decode<Object*>(ts, java_object)->MonitorEnter();
2207 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 }
2209
2210 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2211 ScopedJniThreadState ts(env);
2212 Runtime* runtime = Runtime::Current();
2213 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002214 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 } else {
2216 *vm = NULL;
2217 }
2218 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2219 }
2220
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2222 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002223
2224 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002225 CHECK(address != NULL); // TODO: ReportJniError
2226 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002227
2228 jclass buffer_class = GetDirectByteBufferClass(env);
2229 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2230 if (mid == NULL) {
2231 return NULL;
2232 }
2233
2234 // At the moment, the Java side is limited to 32 bits.
2235 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2236 CHECK_LE(capacity, 0xffffffff);
2237 jint address_arg = reinterpret_cast<jint>(address);
2238 jint capacity_arg = static_cast<jint>(capacity);
2239
2240 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2241 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Elliott Hughesb465ab02011-08-24 11:21:21 -07002244 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002246 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2247 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 }
2249
Elliott Hughesb465ab02011-08-24 11:21:21 -07002250 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002252 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2253 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
Elliott Hughesb465ab02011-08-24 11:21:21 -07002256 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002258
Elliott Hughes75770752011-08-24 17:52:38 -07002259 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002260
2261 // Do we definitely know what kind of reference this is?
2262 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2263 IndirectRefKind kind = GetIndirectRefKind(ref);
2264 switch (kind) {
2265 case kLocal:
2266 return JNILocalRefType;
2267 case kGlobal:
2268 return JNIGlobalRefType;
2269 case kWeakGlobal:
2270 return JNIWeakGlobalRefType;
2271 case kSirtOrInvalid:
2272 // Is it in a stack IRT?
2273 if (ts.Self()->SirtContains(java_object)) {
2274 return JNILocalRefType;
2275 }
2276
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002277 if (!ts.Env()->work_around_app_jni_bugs) {
2278 return JNIInvalidRefType;
2279 }
2280
Elliott Hughesb465ab02011-08-24 11:21:21 -07002281 // If we're handing out direct pointers, check whether it's a direct pointer
2282 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002283 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002284 if (ts.Env()->locals.Contains(java_object)) {
2285 return JNILocalRefType;
2286 }
2287 }
2288
2289 return JNIInvalidRefType;
2290 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 }
2292};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002293
Elliott Hughesa2501992011-08-26 19:39:54 -07002294const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002295 NULL, // reserved0.
2296 NULL, // reserved1.
2297 NULL, // reserved2.
2298 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002299 JNI::GetVersion,
2300 JNI::DefineClass,
2301 JNI::FindClass,
2302 JNI::FromReflectedMethod,
2303 JNI::FromReflectedField,
2304 JNI::ToReflectedMethod,
2305 JNI::GetSuperclass,
2306 JNI::IsAssignableFrom,
2307 JNI::ToReflectedField,
2308 JNI::Throw,
2309 JNI::ThrowNew,
2310 JNI::ExceptionOccurred,
2311 JNI::ExceptionDescribe,
2312 JNI::ExceptionClear,
2313 JNI::FatalError,
2314 JNI::PushLocalFrame,
2315 JNI::PopLocalFrame,
2316 JNI::NewGlobalRef,
2317 JNI::DeleteGlobalRef,
2318 JNI::DeleteLocalRef,
2319 JNI::IsSameObject,
2320 JNI::NewLocalRef,
2321 JNI::EnsureLocalCapacity,
2322 JNI::AllocObject,
2323 JNI::NewObject,
2324 JNI::NewObjectV,
2325 JNI::NewObjectA,
2326 JNI::GetObjectClass,
2327 JNI::IsInstanceOf,
2328 JNI::GetMethodID,
2329 JNI::CallObjectMethod,
2330 JNI::CallObjectMethodV,
2331 JNI::CallObjectMethodA,
2332 JNI::CallBooleanMethod,
2333 JNI::CallBooleanMethodV,
2334 JNI::CallBooleanMethodA,
2335 JNI::CallByteMethod,
2336 JNI::CallByteMethodV,
2337 JNI::CallByteMethodA,
2338 JNI::CallCharMethod,
2339 JNI::CallCharMethodV,
2340 JNI::CallCharMethodA,
2341 JNI::CallShortMethod,
2342 JNI::CallShortMethodV,
2343 JNI::CallShortMethodA,
2344 JNI::CallIntMethod,
2345 JNI::CallIntMethodV,
2346 JNI::CallIntMethodA,
2347 JNI::CallLongMethod,
2348 JNI::CallLongMethodV,
2349 JNI::CallLongMethodA,
2350 JNI::CallFloatMethod,
2351 JNI::CallFloatMethodV,
2352 JNI::CallFloatMethodA,
2353 JNI::CallDoubleMethod,
2354 JNI::CallDoubleMethodV,
2355 JNI::CallDoubleMethodA,
2356 JNI::CallVoidMethod,
2357 JNI::CallVoidMethodV,
2358 JNI::CallVoidMethodA,
2359 JNI::CallNonvirtualObjectMethod,
2360 JNI::CallNonvirtualObjectMethodV,
2361 JNI::CallNonvirtualObjectMethodA,
2362 JNI::CallNonvirtualBooleanMethod,
2363 JNI::CallNonvirtualBooleanMethodV,
2364 JNI::CallNonvirtualBooleanMethodA,
2365 JNI::CallNonvirtualByteMethod,
2366 JNI::CallNonvirtualByteMethodV,
2367 JNI::CallNonvirtualByteMethodA,
2368 JNI::CallNonvirtualCharMethod,
2369 JNI::CallNonvirtualCharMethodV,
2370 JNI::CallNonvirtualCharMethodA,
2371 JNI::CallNonvirtualShortMethod,
2372 JNI::CallNonvirtualShortMethodV,
2373 JNI::CallNonvirtualShortMethodA,
2374 JNI::CallNonvirtualIntMethod,
2375 JNI::CallNonvirtualIntMethodV,
2376 JNI::CallNonvirtualIntMethodA,
2377 JNI::CallNonvirtualLongMethod,
2378 JNI::CallNonvirtualLongMethodV,
2379 JNI::CallNonvirtualLongMethodA,
2380 JNI::CallNonvirtualFloatMethod,
2381 JNI::CallNonvirtualFloatMethodV,
2382 JNI::CallNonvirtualFloatMethodA,
2383 JNI::CallNonvirtualDoubleMethod,
2384 JNI::CallNonvirtualDoubleMethodV,
2385 JNI::CallNonvirtualDoubleMethodA,
2386 JNI::CallNonvirtualVoidMethod,
2387 JNI::CallNonvirtualVoidMethodV,
2388 JNI::CallNonvirtualVoidMethodA,
2389 JNI::GetFieldID,
2390 JNI::GetObjectField,
2391 JNI::GetBooleanField,
2392 JNI::GetByteField,
2393 JNI::GetCharField,
2394 JNI::GetShortField,
2395 JNI::GetIntField,
2396 JNI::GetLongField,
2397 JNI::GetFloatField,
2398 JNI::GetDoubleField,
2399 JNI::SetObjectField,
2400 JNI::SetBooleanField,
2401 JNI::SetByteField,
2402 JNI::SetCharField,
2403 JNI::SetShortField,
2404 JNI::SetIntField,
2405 JNI::SetLongField,
2406 JNI::SetFloatField,
2407 JNI::SetDoubleField,
2408 JNI::GetStaticMethodID,
2409 JNI::CallStaticObjectMethod,
2410 JNI::CallStaticObjectMethodV,
2411 JNI::CallStaticObjectMethodA,
2412 JNI::CallStaticBooleanMethod,
2413 JNI::CallStaticBooleanMethodV,
2414 JNI::CallStaticBooleanMethodA,
2415 JNI::CallStaticByteMethod,
2416 JNI::CallStaticByteMethodV,
2417 JNI::CallStaticByteMethodA,
2418 JNI::CallStaticCharMethod,
2419 JNI::CallStaticCharMethodV,
2420 JNI::CallStaticCharMethodA,
2421 JNI::CallStaticShortMethod,
2422 JNI::CallStaticShortMethodV,
2423 JNI::CallStaticShortMethodA,
2424 JNI::CallStaticIntMethod,
2425 JNI::CallStaticIntMethodV,
2426 JNI::CallStaticIntMethodA,
2427 JNI::CallStaticLongMethod,
2428 JNI::CallStaticLongMethodV,
2429 JNI::CallStaticLongMethodA,
2430 JNI::CallStaticFloatMethod,
2431 JNI::CallStaticFloatMethodV,
2432 JNI::CallStaticFloatMethodA,
2433 JNI::CallStaticDoubleMethod,
2434 JNI::CallStaticDoubleMethodV,
2435 JNI::CallStaticDoubleMethodA,
2436 JNI::CallStaticVoidMethod,
2437 JNI::CallStaticVoidMethodV,
2438 JNI::CallStaticVoidMethodA,
2439 JNI::GetStaticFieldID,
2440 JNI::GetStaticObjectField,
2441 JNI::GetStaticBooleanField,
2442 JNI::GetStaticByteField,
2443 JNI::GetStaticCharField,
2444 JNI::GetStaticShortField,
2445 JNI::GetStaticIntField,
2446 JNI::GetStaticLongField,
2447 JNI::GetStaticFloatField,
2448 JNI::GetStaticDoubleField,
2449 JNI::SetStaticObjectField,
2450 JNI::SetStaticBooleanField,
2451 JNI::SetStaticByteField,
2452 JNI::SetStaticCharField,
2453 JNI::SetStaticShortField,
2454 JNI::SetStaticIntField,
2455 JNI::SetStaticLongField,
2456 JNI::SetStaticFloatField,
2457 JNI::SetStaticDoubleField,
2458 JNI::NewString,
2459 JNI::GetStringLength,
2460 JNI::GetStringChars,
2461 JNI::ReleaseStringChars,
2462 JNI::NewStringUTF,
2463 JNI::GetStringUTFLength,
2464 JNI::GetStringUTFChars,
2465 JNI::ReleaseStringUTFChars,
2466 JNI::GetArrayLength,
2467 JNI::NewObjectArray,
2468 JNI::GetObjectArrayElement,
2469 JNI::SetObjectArrayElement,
2470 JNI::NewBooleanArray,
2471 JNI::NewByteArray,
2472 JNI::NewCharArray,
2473 JNI::NewShortArray,
2474 JNI::NewIntArray,
2475 JNI::NewLongArray,
2476 JNI::NewFloatArray,
2477 JNI::NewDoubleArray,
2478 JNI::GetBooleanArrayElements,
2479 JNI::GetByteArrayElements,
2480 JNI::GetCharArrayElements,
2481 JNI::GetShortArrayElements,
2482 JNI::GetIntArrayElements,
2483 JNI::GetLongArrayElements,
2484 JNI::GetFloatArrayElements,
2485 JNI::GetDoubleArrayElements,
2486 JNI::ReleaseBooleanArrayElements,
2487 JNI::ReleaseByteArrayElements,
2488 JNI::ReleaseCharArrayElements,
2489 JNI::ReleaseShortArrayElements,
2490 JNI::ReleaseIntArrayElements,
2491 JNI::ReleaseLongArrayElements,
2492 JNI::ReleaseFloatArrayElements,
2493 JNI::ReleaseDoubleArrayElements,
2494 JNI::GetBooleanArrayRegion,
2495 JNI::GetByteArrayRegion,
2496 JNI::GetCharArrayRegion,
2497 JNI::GetShortArrayRegion,
2498 JNI::GetIntArrayRegion,
2499 JNI::GetLongArrayRegion,
2500 JNI::GetFloatArrayRegion,
2501 JNI::GetDoubleArrayRegion,
2502 JNI::SetBooleanArrayRegion,
2503 JNI::SetByteArrayRegion,
2504 JNI::SetCharArrayRegion,
2505 JNI::SetShortArrayRegion,
2506 JNI::SetIntArrayRegion,
2507 JNI::SetLongArrayRegion,
2508 JNI::SetFloatArrayRegion,
2509 JNI::SetDoubleArrayRegion,
2510 JNI::RegisterNatives,
2511 JNI::UnregisterNatives,
2512 JNI::MonitorEnter,
2513 JNI::MonitorExit,
2514 JNI::GetJavaVM,
2515 JNI::GetStringRegion,
2516 JNI::GetStringUTFRegion,
2517 JNI::GetPrimitiveArrayCritical,
2518 JNI::ReleasePrimitiveArrayCritical,
2519 JNI::GetStringCritical,
2520 JNI::ReleaseStringCritical,
2521 JNI::NewWeakGlobalRef,
2522 JNI::DeleteWeakGlobalRef,
2523 JNI::ExceptionCheck,
2524 JNI::NewDirectByteBuffer,
2525 JNI::GetDirectBufferAddress,
2526 JNI::GetDirectBufferCapacity,
2527 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002528};
2529
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002530static const size_t kMonitorsInitial = 32; // Arbitrary.
2531static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2532
2533static const size_t kLocalsInitial = 64; // Arbitrary.
2534static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002535
Elliott Hughes75770752011-08-24 17:52:38 -07002536JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002537 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002538 vm(vm),
2539 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002540 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002541 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002542 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2543 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002544 functions = unchecked_functions = &gNativeInterface;
2545 if (check_jni) {
2546 functions = GetCheckJniNativeInterface();
2547 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002548}
2549
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002550JNIEnvExt::~JNIEnvExt() {
2551}
2552
Carl Shapiroea4dca82011-08-01 13:45:38 -07002553// JNI Invocation interface.
2554
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002555extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2556 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2557 if (args->version < JNI_VERSION_1_2) {
2558 return JNI_EVERSION;
2559 }
2560 Runtime::Options options;
2561 for (int i = 0; i < args->nOptions; ++i) {
2562 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002563 options.push_back(std::make_pair(StringPiece(option->optionString),
2564 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002565 }
2566 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002567 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002568 if (runtime == NULL) {
2569 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002570 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002571 runtime->Start();
2572 *p_env = Thread::Current()->GetJniEnv();
2573 *p_vm = runtime->GetJavaVM();
2574 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002575}
2576
Elliott Hughesf2682d52011-08-15 16:37:04 -07002577extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002578 Runtime* runtime = Runtime::Current();
2579 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002580 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002581 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002582 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002583 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002584 }
2585 return JNI_OK;
2586}
2587
2588// Historically unsupported.
2589extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2590 return JNI_ERR;
2591}
2592
Elliott Hughescdf53122011-08-19 15:46:09 -07002593class JII {
2594 public:
2595 static jint DestroyJavaVM(JavaVM* vm) {
2596 if (vm == NULL) {
2597 return JNI_ERR;
2598 } else {
2599 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2600 delete raw_vm->runtime;
2601 return JNI_OK;
2602 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002603 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002604
Elliott Hughescdf53122011-08-19 15:46:09 -07002605 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002606 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002607 }
2608
2609 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002610 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002611 }
2612
2613 static jint DetachCurrentThread(JavaVM* vm) {
2614 if (vm == NULL) {
2615 return JNI_ERR;
2616 } else {
2617 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2618 Runtime* runtime = raw_vm->runtime;
2619 runtime->DetachCurrentThread();
2620 return JNI_OK;
2621 }
2622 }
2623
2624 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2625 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2626 return JNI_EVERSION;
2627 }
2628 if (vm == NULL || env == NULL) {
2629 return JNI_ERR;
2630 }
2631 Thread* thread = Thread::Current();
2632 if (thread == NULL) {
2633 *env = NULL;
2634 return JNI_EDETACHED;
2635 }
2636 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002637 return JNI_OK;
2638 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002639};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002640
Elliott Hughesa2501992011-08-26 19:39:54 -07002641const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002642 NULL, // reserved0
2643 NULL, // reserved1
2644 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002645 JII::DestroyJavaVM,
2646 JII::AttachCurrentThread,
2647 JII::DetachCurrentThread,
2648 JII::GetEnv,
2649 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002650};
2651
Elliott Hughesbbd76712011-08-17 10:25:24 -07002652static const size_t kPinTableInitialSize = 16;
2653static const size_t kPinTableMaxSize = 1024;
2654
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002655static const size_t kGlobalsInitial = 512; // Arbitrary.
2656static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2657
2658static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2659static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2660
Elliott Hughesa0957642011-09-02 14:27:33 -07002661JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002662 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002663 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002664 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002665 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002666 verbose_jni(options->IsVerbose("jni")),
2667 log_third_party_jni(options->IsVerbose("third-party-jni")),
2668 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002669 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes75770752011-08-24 17:52:38 -07002670 pins_lock(Mutex::Create("JNI pin table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002671 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes18c07532011-08-18 15:50:51 -07002672 globals_lock(Mutex::Create("JNI global reference table lock")),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002673 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes18c07532011-08-18 15:50:51 -07002674 weak_globals_lock(Mutex::Create("JNI weak global reference table lock")),
Elliott Hughes79082e32011-08-25 12:07:32 -07002675 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
2676 libraries_lock(Mutex::Create("JNI shared libraries map lock")),
2677 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002678 functions = unchecked_functions = &gInvokeInterface;
2679 if (check_jni) {
2680 functions = GetCheckJniInvokeInterface();
2681 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002682}
2683
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002684JavaVMExt::~JavaVMExt() {
Elliott Hughes75770752011-08-24 17:52:38 -07002685 delete pins_lock;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002686 delete globals_lock;
2687 delete weak_globals_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -07002688 delete libraries_lock;
2689 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002690}
2691
Elliott Hughes75770752011-08-24 17:52:38 -07002692bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2693 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002694
2695 // See if we've already loaded this library. If we have, and the class loader
2696 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002697 // TODO: for better results we should canonicalize the pathname (or even compare
2698 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002699 SharedLibrary* library;
2700 {
2701 // TODO: move the locking (and more of this logic) into Libraries.
2702 MutexLock mu(libraries_lock);
2703 library = libraries->Get(path);
2704 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002705 if (library != NULL) {
2706 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002707 // The library will be associated with class_loader. The JNI
2708 // spec says we can't load the same library into more than one
2709 // class loader.
2710 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2711 "ClassLoader %p; can't open in ClassLoader %p",
2712 path.c_str(), library->GetClassLoader(), class_loader);
2713 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002714 return false;
2715 }
2716 if (verbose_jni) {
2717 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2718 << "ClassLoader " << class_loader << "]";
2719 }
2720 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002721 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2722 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002723 return false;
2724 }
2725 return true;
2726 }
2727
2728 // Open the shared library. Because we're using a full path, the system
2729 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2730 // resolve this library's dependencies though.)
2731
2732 // Failures here are expected when java.library.path has several entries
2733 // and we have to hunt for the lib.
2734
2735 // The current version of the dynamic linker prints detailed information
2736 // about dlopen() failures. Some things to check if the message is
2737 // cryptic:
2738 // - make sure the library exists on the device
2739 // - verify that the right path is being opened (the debug log message
2740 // above can help with that)
2741 // - check to see if the library is valid (e.g. not zero bytes long)
2742 // - check config/prelink-linux-arm.map to ensure that the library
2743 // is listed and is not being overrun by the previous entry (if
2744 // loading suddenly stops working on a prelinked library, this is
2745 // a good one to check)
2746 // - write a trivial app that calls sleep() then dlopen(), attach
2747 // to it with "strace -p <pid>" while it sleeps, and watch for
2748 // attempts to open nonexistent dependent shared libs
2749
2750 // TODO: automate some of these checks!
2751
2752 // This can execute slowly for a large library on a busy system, so we
2753 // want to switch from RUNNING to VMWAIT while it executes. This allows
2754 // the GC to ignore us.
2755 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002756 void* handle = NULL;
2757 {
2758 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
2759 handle = dlopen(path.c_str(), RTLD_LAZY);
2760 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002761
2762 if (verbose_jni) {
2763 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2764 }
2765
2766 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002767 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002768 return false;
2769 }
2770
2771 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002772 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002773 // TODO: move the locking (and more of this logic) into Libraries.
2774 MutexLock mu(libraries_lock);
2775 library = libraries->Get(path);
2776 if (library != NULL) {
2777 LOG(INFO) << "WOW: we lost a race to add shared library: "
2778 << "\"" << path << "\" ClassLoader=" << class_loader;
2779 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002780 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002781 library = new SharedLibrary(path, handle, class_loader);
2782 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002783 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002784
2785 if (verbose_jni) {
2786 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2787 }
2788
2789 bool result = true;
2790 void* sym = dlsym(handle, "JNI_OnLoad");
2791 if (sym == NULL) {
2792 if (verbose_jni) {
2793 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2794 }
2795 } else {
2796 // Call JNI_OnLoad. We have to override the current class
2797 // loader, which will always be "null" since the stuff at the
2798 // top of the stack is around Runtime.loadLibrary(). (See
2799 // the comments in the JNI FindClass function.)
2800 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2801 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002802 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002803 self->SetClassLoaderOverride(class_loader);
2804
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002805 int version = 0;
2806 {
2807 ScopedThreadStateChange tsc(self, Thread::kNative);
2808 if (verbose_jni) {
2809 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2810 }
2811 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002812 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002813
2814 self->SetClassLoaderOverride(old_class_loader);;
2815
2816 if (version != JNI_VERSION_1_2 &&
2817 version != JNI_VERSION_1_4 &&
2818 version != JNI_VERSION_1_6) {
2819 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2820 << "bad version: " << version;
2821 // It's unwise to call dlclose() here, but we can mark it
2822 // as bad and ensure that future load attempts will fail.
2823 // We don't know how far JNI_OnLoad got, so there could
2824 // be some partially-initialized stuff accessible through
2825 // newly-registered native method calls. We could try to
2826 // unregister them, but that doesn't seem worthwhile.
2827 result = false;
2828 } else {
2829 if (verbose_jni) {
2830 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2831 << " from JNI_OnLoad in \"" << path << "\"]";
2832 }
2833 }
2834 }
2835
2836 library->SetResult(result);
2837 return result;
2838}
2839
2840void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2841 CHECK(m->IsNative());
2842
2843 Class* c = m->GetDeclaringClass();
2844
2845 // If this is a static method, it could be called before the class
2846 // has been initialized.
2847 if (m->IsStatic()) {
2848 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2849 return NULL;
2850 }
2851 } else {
2852 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2853 }
2854
2855 MutexLock mu(libraries_lock);
2856 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002857}
2858
Elliott Hughes410c0c82011-09-01 17:58:25 -07002859void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2860 {
2861 MutexLock mu(globals_lock);
2862 globals.VisitRoots(visitor, arg);
2863 }
2864 {
2865 MutexLock mu(pins_lock);
2866 pin_table.VisitRoots(visitor, arg);
2867 }
2868 // The weak_globals table is visited by the GC itself (because it mutates the table).
2869}
2870
Ian Rogersdf20fe02011-07-20 20:34:16 -07002871} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002872
2873std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2874 switch (rhs) {
2875 case JNIInvalidRefType:
2876 os << "JNIInvalidRefType";
2877 return os;
2878 case JNILocalRefType:
2879 os << "JNILocalRefType";
2880 return os;
2881 case JNIGlobalRefType:
2882 os << "JNIGlobalRefType";
2883 return os;
2884 case JNIWeakGlobalRefType:
2885 os << "JNIWeakGlobalRefType";
2886 return os;
2887 }
2888}