blob: 954fafd47ce7d7a538f424304dea472914725949 [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -07004
Elliott Hughes0af55432011-08-17 18:37:28 -07005#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -07006#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -07007
8#include <cstdarg>
9#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070010#include <utility>
11#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012
Elliott Hughes72025e52011-08-23 17:50:30 -070013#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070015#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070016#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070018#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include "object.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070021#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070022#include "scoped_jni_thread_state.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070023#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070025
26namespace art {
27
Shih-wei Liao31384c52011-09-06 15:27:45 -070028void* FindNativeMethod(Thread* thread) {
29 DCHECK(Thread::Current() == thread);
30
31 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
32 DCHECK(method != NULL);
33
34 // Lookup symbol address for method, on failure we'll return NULL with an
35 // exception set, otherwise we return the address of the method we found.
36 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
37 if (native_code == NULL) {
38 DCHECK(thread->IsExceptionPending());
39 return NULL;
40 } else {
41 // Register so that future calls don't come here
42 method->RegisterNative(native_code);
43 return native_code;
44 }
45}
46
Elliott Hughesc5f7c912011-08-18 14:00:42 -070047/*
48 * Add a local reference for an object to the current stack frame. When
49 * the native function returns, the reference will be discarded.
50 *
51 * We need to allow the same reference to be added multiple times.
52 *
53 * This will be called on otherwise unreferenced objects. We cannot do
54 * GC allocations here, and it's best if we don't grab a mutex.
55 *
56 * Returns the local reference (currently just the same pointer that was
57 * passed in), or NULL on failure.
58 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070059template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070060T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
61 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
62 Object* obj = const_cast<Object*>(const_obj);
63
Elliott Hughesc5f7c912011-08-18 14:00:42 -070064 if (obj == NULL) {
65 return NULL;
66 }
67
Elliott Hughesbf86d042011-08-31 17:53:14 -070068 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
69 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070070
71 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
72 IndirectRef ref = locals.Add(cookie, obj);
73 if (ref == NULL) {
74 // TODO: just change Add's DCHECK to CHECK and lose this?
75 locals.Dump();
76 LOG(FATAL) << "Failed adding to JNI local reference table "
77 << "(has " << locals.Capacity() << " entries)";
78 // TODO: dvmDumpThread(dvmThreadSelf(), false);
79 }
80
81#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -070082 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070083 size_t entry_count = locals.Capacity();
84 if (entry_count > 16) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -070085 std::string class_descriptor(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughesc5f7c912011-08-18 14:00:42 -070086 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughese5b0dc82011-08-23 09:59:02 -070087 << entry_count << " (most recent was a " << class_descriptor << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070088 locals.Dump();
89 // TODO: dvmDumpThread(dvmThreadSelf(), false);
90 // dvmAbort();
91 }
92 }
93#endif
94
Elliott Hughesbf86d042011-08-31 17:53:14 -070095 if (env->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070096 // Hand out direct pointers to support broken old apps.
97 return reinterpret_cast<T>(obj);
98 }
99
100 return reinterpret_cast<T>(ref);
101}
102
Elliott Hughesbf86d042011-08-31 17:53:14 -0700103// For external use.
104template<typename T>
105T Decode(JNIEnv* public_env, jobject obj) {
106 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
107 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
108}
109// Explicit instantiations.
110template Class* Decode<Class*>(JNIEnv*, jobject);
111template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
112template Object* Decode<Object*>(JNIEnv*, jobject);
113template String* Decode<String*>(JNIEnv*, jobject);
114
115namespace {
116
Elliott Hughescdf53122011-08-19 15:46:09 -0700117jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
118 if (obj == NULL) {
119 return NULL;
120 }
Elliott Hughes75770752011-08-24 17:52:38 -0700121 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700122 IndirectReferenceTable& weak_globals = vm->weak_globals;
123 MutexLock mu(vm->weak_globals_lock);
124 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
125 return reinterpret_cast<jweak>(ref);
126}
127
Elliott Hughesbf86d042011-08-31 17:53:14 -0700128// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700129template<typename T>
130T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700131 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700132}
133
Elliott Hughescdf53122011-08-19 15:46:09 -0700134Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
135 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
136}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700137
Elliott Hughescdf53122011-08-19 15:46:09 -0700138Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
139 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700140}
141
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700142byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700143 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700144 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700145 String* shorty = method->GetShorty();
146 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
147 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700148 case 'Z':
149 case 'B':
150 case 'C':
151 case 'S':
152 case 'I':
153 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
154 offset += 4;
155 break;
156 case 'F':
157 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
158 offset += 4;
159 break;
160 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700161 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700162 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
163 offset += sizeof(Object*);
164 break;
165 }
166 case 'D':
167 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
168 offset += 8;
169 break;
170 case 'J':
171 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
172 offset += 8;
173 break;
174 }
175 }
176 return arg_array.release();
177}
178
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700179byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700180 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700181 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700182 String* shorty = method->GetShorty();
183 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
184 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700185 case 'Z':
186 case 'B':
187 case 'C':
188 case 'S':
189 case 'I':
190 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
191 offset += 4;
192 break;
193 case 'F':
194 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
195 offset += 4;
196 break;
197 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700198 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700199 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
200 offset += sizeof(Object*);
201 break;
202 }
203 case 'D':
204 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
205 offset += 8;
206 break;
207 case 'J':
208 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
209 offset += 8;
210 break;
211 }
212 }
213 return arg_array.release();
214}
215
Elliott Hughes72025e52011-08-23 17:50:30 -0700216JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
217 Method* method, byte* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700218 JValue result;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700219 method->Invoke(ts.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700220 return result;
221}
222
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700223JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700224 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700225 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700226 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700227 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700228 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700229}
230
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700231JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700232 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700233 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700234 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700235 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700236 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
237}
238
Elliott Hughes75770752011-08-24 17:52:38 -0700239Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700240 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700241}
242
Brian Carlstrom30b94452011-08-25 21:35:26 -0700243JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700244 Object* receiver = Decode<Object*>(ts, obj);
245 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700246 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700247 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
248}
249
Brian Carlstrom30b94452011-08-25 21:35:26 -0700250JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700251 Object* receiver = Decode<Object*>(ts, obj);
252 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700253 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700254 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700255}
256
Elliott Hughes6b436852011-08-12 10:16:44 -0700257// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
258// separated with slashes but aren't wrapped with "L;" like regular descriptors
259// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
260// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
261// supported names with dots too (such as "a.b.C").
262std::string NormalizeJniClassDescriptor(const char* name) {
263 std::string result;
264 // Add the missing "L;" if necessary.
265 if (name[0] == '[') {
266 result = name;
267 } else {
268 result += 'L';
269 result += name;
270 result += ';';
271 }
272 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700273 if (result.find('.') != std::string::npos) {
274 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
275 << "\"" << name << "\"";
276 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700277 }
278 return result;
279}
280
Elliott Hughescdf53122011-08-19 15:46:09 -0700281jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
282 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700283 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
284 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700285 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700286
287 Method* method = NULL;
288 if (is_static) {
289 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700290 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700291 method = c->FindVirtualMethod(name, sig);
292 if (method == NULL) {
293 // No virtual method matching the signature. Search declared
294 // private methods and constructors.
295 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700296 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700297 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700298
Elliott Hughescdf53122011-08-19 15:46:09 -0700299 if (method == NULL || method->IsStatic() != is_static) {
300 Thread* self = Thread::Current();
Elliott Hughesa2501992011-08-26 19:39:54 -0700301 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700302 // TODO: try searching for the opposite kind of method from is_static
303 // for better diagnostics?
304 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700305 "no %s method %s", is_static ? "static" : "non-static",
306 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700307 return NULL;
308 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700309
Elliott Hughescdf53122011-08-19 15:46:09 -0700310 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700311}
312
Elliott Hughescdf53122011-08-19 15:46:09 -0700313jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
314 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700315 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
316 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700317 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700318
319 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 Class* field_type;
321 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
322 if (sig[1] != '\0') {
323 // TODO: need to get the appropriate ClassLoader.
324 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
325 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700326 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700327 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700328 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 if (field_type == NULL) {
330 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700331 DCHECK(ts.Self()->IsExceptionPending());
332 ts.Self()->ClearException();
333 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
334 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
335 "no type \"%s\" found and so no field \"%s\" could be found in class "
336 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 return NULL;
338 }
339 if (is_static) {
340 field = c->FindStaticField(name, field_type);
341 } else {
342 field = c->FindInstanceField(name, field_type);
343 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700344 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700345 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700346 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700347 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700348 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700349 return NULL;
350 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700351 // Check invariant that all jfieldIDs have resolved types (how else would
352 // the type equality in Find...Field hold?)
353 DCHECK(field->GetType() != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700354 jweak fid = AddWeakGlobalReference(ts, field);
355 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700356}
357
Elliott Hughes75770752011-08-24 17:52:38 -0700358void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
359 JavaVMExt* vm = ts.Vm();
360 MutexLock mu(vm->pins_lock);
361 vm->pin_table.Add(array);
362}
363
364void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
365 JavaVMExt* vm = ts.Vm();
366 MutexLock mu(vm->pins_lock);
367 vm->pin_table.Remove(array);
368}
369
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700370template<typename JniT, typename ArtT>
371JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
372 CHECK_GE(length, 0); // TODO: ReportJniError
373 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700374 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700375}
376
Elliott Hughes75770752011-08-24 17:52:38 -0700377template <typename ArrayT, typename CArrayT, typename ArtArrayT>
378CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
379 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
380 PinPrimitiveArray(ts, array);
381 if (is_copy != NULL) {
382 *is_copy = JNI_FALSE;
383 }
384 return array->GetData();
385}
386
387template <typename ArrayT>
388void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
389 if (mode != JNI_COMMIT) {
390 Array* array = Decode<Array*>(ts, java_array);
391 UnpinPrimitiveArray(ts, array);
392 }
393}
394
Elliott Hughes814e4032011-08-23 12:07:56 -0700395void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
396 std::string type(PrettyType(array));
397 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
398 "%s offset=%d length=%d %s.length=%d",
399 type.c_str(), start, length, identifier, array->GetLength());
400}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700401void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
402 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
403 "offset=%d length=%d string.length()=%d", start, length, array_length);
404}
Elliott Hughes814e4032011-08-23 12:07:56 -0700405
406template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700407void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700408 ArrayT* array = Decode<ArrayT*>(ts, java_array);
409 if (start < 0 || length < 0 || start + length > array->GetLength()) {
410 ThrowAIOOBE(ts, array, start, length, "src");
411 } else {
412 JavaT* data = array->GetData();
413 memcpy(buf, data + start, length * sizeof(JavaT));
414 }
415}
416
417template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700418void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700419 ArrayT* array = Decode<ArrayT*>(ts, java_array);
420 if (start < 0 || length < 0 || start + length > array->GetLength()) {
421 ThrowAIOOBE(ts, array, start, length, "dst");
422 } else {
423 JavaT* data = array->GetData();
424 memcpy(data + start, buf, length * sizeof(JavaT));
425 }
426}
427
Elliott Hughes75770752011-08-24 17:52:38 -0700428jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700429 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
430 CHECK(buffer_class.get() != NULL);
431 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
432}
433
Elliott Hughes75770752011-08-24 17:52:38 -0700434jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700435 static jclass buffer_class = InitDirectByteBufferClass(env);
436 return buffer_class;
437}
438
Elliott Hughes75770752011-08-24 17:52:38 -0700439jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
440 if (vm == NULL || p_env == NULL) {
441 return JNI_ERR;
442 }
443
444 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
445 JavaVMAttachArgs args;
446 if (thr_args == NULL) {
447 // Allow the v1.1 calling convention.
448 args.version = JNI_VERSION_1_2;
449 args.name = NULL;
450 args.group = NULL; // TODO: get "main" thread group
451 } else {
452 args.version = in_args->version;
453 args.name = in_args->name;
454 if (in_args->group != NULL) {
455 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
456 args.group = NULL; // TODO: decode in_args->group
457 } else {
458 args.group = NULL; // TODO: get "main" thread group
459 }
460 }
461 CHECK_GE(args.version, JNI_VERSION_1_2);
462
463 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700464 runtime->AttachCurrentThread(args.name, as_daemon);
465 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700466 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),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700474 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700475 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700476 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700477 jni_on_load_result_(kPending) {
478 pthread_cond_init(&jni_on_load_cond_, NULL);
479 }
480
Elliott Hughes79082e32011-08-25 12:07:32 -0700481 Object* GetClassLoader() {
482 return class_loader_;
483 }
484
485 std::string GetPath() {
486 return path_;
487 }
488
489 /*
490 * Check the result of an earlier call to JNI_OnLoad on this library. If
491 * the call has not yet finished in another thread, wait for it.
492 */
493 bool CheckOnLoadResult(JavaVMExt* vm) {
494 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700495 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700496 // Check this so we don't end up waiting for ourselves. We need
497 // to return "true" so the caller can continue.
498 LOG(INFO) << *self << " recursive attempt to load library "
499 << "\"" << path_ << "\"";
500 return true;
501 }
502
503 MutexLock mu(jni_on_load_lock_);
504 while (jni_on_load_result_ == kPending) {
505 if (vm->verbose_jni) {
506 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
507 << "JNI_OnLoad...]";
508 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700509 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes8daa0922011-09-11 13:46:25 -0700510 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_.GetImpl());
Elliott Hughes79082e32011-08-25 12:07:32 -0700511 }
512
513 bool okay = (jni_on_load_result_ == kOkay);
514 if (vm->verbose_jni) {
515 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
516 << (okay ? "succeeded" : "failed") << "]";
517 }
518 return okay;
519 }
520
521 void SetResult(bool result) {
522 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700523 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700524
525 // Broadcast a wakeup to anybody sleeping on the condition variable.
526 MutexLock mu(jni_on_load_lock_);
527 pthread_cond_broadcast(&jni_on_load_cond_);
528 }
529
530 void* FindSymbol(const std::string& symbol_name) {
531 return dlsym(handle_, symbol_name.c_str());
532 }
533
534 private:
535 enum JNI_OnLoadState {
536 kPending,
537 kFailed,
538 kOkay,
539 };
540
541 // Path to library "/system/lib/libjni.so".
542 std::string path_;
543
544 // The void* returned by dlopen(3).
545 void* handle_;
546
547 // The ClassLoader this library is associated with.
548 Object* class_loader_;
549
550 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700551 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700552 // Wait for JNI_OnLoad in other thread.
553 pthread_cond_t jni_on_load_cond_;
554 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700555 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700556 // Result of earlier JNI_OnLoad call.
557 JNI_OnLoadState jni_on_load_result_;
558};
559
Elliott Hughescdf53122011-08-19 15:46:09 -0700560} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700561
Elliott Hughes79082e32011-08-25 12:07:32 -0700562// This exists mainly to keep implementation details out of the header file.
563class Libraries {
564 public:
565 Libraries() {
566 }
567
568 ~Libraries() {
569 // Delete our map values. (The keys will be cleaned up by the map itself.)
570 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
571 delete it->second;
572 }
573 }
574
575 SharedLibrary* Get(const std::string& path) {
576 return libraries_[path];
577 }
578
579 void Put(const std::string& path, SharedLibrary* library) {
580 libraries_[path] = library;
581 }
582
583 // See section 11.3 "Linking Native Methods" of the JNI spec.
584 void* FindNativeMethod(const Method* m) {
585 std::string jni_short_name(JniShortName(m));
586 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700587 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700588 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
589 SharedLibrary* library = it->second;
590 if (library->GetClassLoader() != declaring_class_loader) {
591 // We only search libraries loaded by the appropriate ClassLoader.
592 continue;
593 }
594 // Try the short name then the long name...
595 void* fn = library->FindSymbol(jni_short_name);
596 if (fn == NULL) {
597 fn = library->FindSymbol(jni_long_name);
598 }
599 if (fn != NULL) {
600 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700601 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700602 << " in \"" << library->GetPath() << "\"]";
603 }
604 return fn;
605 }
606 }
607 std::string detail;
608 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700609 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700610 LOG(ERROR) << detail;
611 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
612 "%s", detail.c_str());
613 return NULL;
614 }
615
616 private:
617 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
618
619 std::map<std::string, SharedLibrary*> libraries_;
620};
621
Elliott Hughescdf53122011-08-19 15:46:09 -0700622class JNI {
623 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700624
Elliott Hughescdf53122011-08-19 15:46:09 -0700625 static jint GetVersion(JNIEnv* env) {
626 ScopedJniThreadState ts(env);
627 return JNI_VERSION_1_6;
628 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700629
Elliott Hughescdf53122011-08-19 15:46:09 -0700630 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
631 ScopedJniThreadState ts(env);
632 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700633 return NULL;
634 }
635
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 static jclass FindClass(JNIEnv* env, const char* name) {
637 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700638 Runtime* runtime = Runtime::Current();
639 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700640 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700641 Class* c = NULL;
642 if (runtime->IsStarted()) {
643 // TODO: need to get the appropriate ClassLoader.
644 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
645 c = class_linker->FindClass(descriptor, cl);
646 } else {
647 c = class_linker->FindSystemClass(descriptor);
648 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700649 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700650 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700651
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
653 ScopedJniThreadState ts(env);
654 Method* method = Decode<Method*>(ts, java_method);
655 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700656 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700657
Elliott Hughescdf53122011-08-19 15:46:09 -0700658 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
659 ScopedJniThreadState ts(env);
660 Field* field = Decode<Field*>(ts, java_field);
661 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
662 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700663
Elliott Hughescdf53122011-08-19 15:46:09 -0700664 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
665 ScopedJniThreadState ts(env);
666 Method* method = DecodeMethod(ts, mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700667 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700668 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700669
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
671 ScopedJniThreadState ts(env);
672 Field* field = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700673 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700675
Elliott Hughes37f7a402011-08-22 18:56:01 -0700676 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
677 ScopedJniThreadState ts(env);
678 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700679 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700680 }
681
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700682 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700684 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700685 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700687
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700690 Class* c1 = Decode<Class*>(ts, java_class1);
691 Class* c2 = Decode<Class*>(ts, java_class2);
692 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700694
Elliott Hughes37f7a402011-08-22 18:56:01 -0700695 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700697 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 if (jobj == NULL) {
699 // NB. JNI is different from regular Java instanceof in this respect
700 return JNI_TRUE;
701 } else {
702 Object* obj = Decode<Object*>(ts, jobj);
703 Class* klass = Decode<Class*>(ts, clazz);
704 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
705 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700706 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700707
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 static jint Throw(JNIEnv* env, jthrowable java_exception) {
709 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700710 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700711 if (exception == NULL) {
712 return JNI_ERR;
713 }
714 ts.Self()->SetException(exception);
715 return JNI_OK;
716 }
717
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700718 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700719 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700720 // TODO: check for a pending exception to decide what constructor to call.
721 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
722 if (mid == NULL) {
723 return JNI_ERR;
724 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700725 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
726 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700727 return JNI_ERR;
728 }
729
730 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700731 args[0].l = s.get();
732 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
733 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700734 return JNI_ERR;
735 }
736
Elliott Hughes72025e52011-08-23 17:50:30 -0700737 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700738 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700739 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700740
Elliott Hughes37f7a402011-08-22 18:56:01 -0700741 return JNI_OK;
742 }
743
744 static jboolean ExceptionCheck(JNIEnv* env) {
745 ScopedJniThreadState ts(env);
746 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
747 }
748
749 static void ExceptionClear(JNIEnv* env) {
750 ScopedJniThreadState ts(env);
751 ts.Self()->ClearException();
752 }
753
754 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700756
757 Thread* self = ts.Self();
758 Throwable* original_exception = self->GetException();
759 self->ClearException();
760
Elliott Hughesbf86d042011-08-31 17:53:14 -0700761 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700762 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
763 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
764 if (mid == NULL) {
765 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
766 << PrettyType(original_exception);
767 } else {
768 env->CallVoidMethod(exception.get(), mid);
769 if (self->IsExceptionPending()) {
770 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
771 << " thrown while calling printStackTrace";
772 self->ClearException();
773 }
774 }
775
776 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700778
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 static jthrowable ExceptionOccurred(JNIEnv* env) {
780 ScopedJniThreadState ts(env);
781 Object* exception = ts.Self()->GetException();
782 if (exception == NULL) {
783 return NULL;
784 } else {
785 // TODO: if adding a local reference failing causes the VM to abort
786 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700787 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700788 if (localException == NULL) {
789 // We were unable to add a new local reference, and threw a new
790 // exception. We can't return "exception", because it's not a
791 // local reference. So we have to return NULL, indicating that
792 // there was no exception, even though it's pretty much raining
793 // exceptions in here.
794 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
795 }
796 return localException;
797 }
798 }
799
Elliott Hughescdf53122011-08-19 15:46:09 -0700800 static void FatalError(JNIEnv* env, const char* msg) {
801 ScopedJniThreadState ts(env);
802 LOG(FATAL) << "JNI FatalError called: " << msg;
803 }
804
805 static jint PushLocalFrame(JNIEnv* env, jint cap) {
806 ScopedJniThreadState ts(env);
807 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
808 return JNI_OK;
809 }
810
811 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
812 ScopedJniThreadState ts(env);
813 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
814 return res;
815 }
816
Elliott Hughes72025e52011-08-23 17:50:30 -0700817 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
818 ScopedJniThreadState ts(env);
819 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
820 return 0;
821 }
822
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
824 ScopedJniThreadState ts(env);
825 if (obj == NULL) {
826 return NULL;
827 }
828
Elliott Hughes75770752011-08-24 17:52:38 -0700829 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 IndirectReferenceTable& globals = vm->globals;
831 MutexLock mu(vm->globals_lock);
832 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
833 return reinterpret_cast<jobject>(ref);
834 }
835
836 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
837 ScopedJniThreadState ts(env);
838 if (obj == NULL) {
839 return;
840 }
841
Elliott Hughes75770752011-08-24 17:52:38 -0700842 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700843 IndirectReferenceTable& globals = vm->globals;
844 MutexLock mu(vm->globals_lock);
845
846 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
847 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
848 << "failed to find entry";
849 }
850 }
851
852 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
853 ScopedJniThreadState ts(env);
854 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
855 }
856
857 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
858 ScopedJniThreadState ts(env);
859 if (obj == NULL) {
860 return;
861 }
862
Elliott Hughes75770752011-08-24 17:52:38 -0700863 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700864 IndirectReferenceTable& weak_globals = vm->weak_globals;
865 MutexLock mu(vm->weak_globals_lock);
866
867 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
868 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
869 << "failed to find entry";
870 }
871 }
872
873 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
874 ScopedJniThreadState ts(env);
875 if (obj == NULL) {
876 return NULL;
877 }
878
879 IndirectReferenceTable& locals = ts.Env()->locals;
880
881 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
882 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
883 return reinterpret_cast<jobject>(ref);
884 }
885
886 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
887 ScopedJniThreadState ts(env);
888 if (obj == NULL) {
889 return;
890 }
891
892 IndirectReferenceTable& locals = ts.Env()->locals;
893
894 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
895 if (!locals.Remove(cookie, obj)) {
896 // Attempting to delete a local reference that is not in the
897 // topmost local reference frame is a no-op. DeleteLocalRef returns
898 // void and doesn't throw any exceptions, but we should probably
899 // complain about it so the user will notice that things aren't
900 // going quite the way they expect.
901 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
902 << "failed to find entry";
903 }
904 }
905
906 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
907 ScopedJniThreadState ts(env);
908 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
909 ? JNI_TRUE : JNI_FALSE;
910 }
911
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700912 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700914 Class* c = Decode<Class*>(ts, java_class);
915 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
916 return NULL;
917 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700918 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 }
920
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 ScopedJniThreadState ts(env);
923 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 va_start(args, mid);
925 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700926 va_end(args);
927 return result;
928 }
929
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700932 Class* c = Decode<Class*>(ts, java_class);
933 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
934 return NULL;
935 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700936 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700937 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700939 return local_result;
940 }
941
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700944 Class* c = Decode<Class*>(ts, java_class);
945 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
946 return NULL;
947 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700948 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700949 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 return local_result;
952 }
953
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
955 ScopedJniThreadState ts(env);
956 return FindMethodID(ts, c, name, sig, false);
957 }
958
959 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
960 ScopedJniThreadState ts(env);
961 return FindMethodID(ts, c, name, sig, true);
962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 va_list ap;
967 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700968 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700970 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 }
972
Elliott Hughes72025e52011-08-23 17:50:30 -0700973 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700975 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700976 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700980 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700981 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700982 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 }
984
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 va_list ap;
988 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700989 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 va_end(ap);
991 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 }
993
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700996 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001001 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 va_list ap;
1007 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001008 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 va_end(ap);
1010 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 }
1012
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001015 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001020 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 va_list ap;
1026 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001027 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 va_end(ap);
1029 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001034 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001039 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 va_list ap;
1045 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001046 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 va_end(ap);
1048 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 }
1050
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001053 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001058 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_list ap;
1064 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001065 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 va_end(ap);
1067 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001072 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001077 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 va_list ap;
1083 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001084 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 va_end(ap);
1086 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001091 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001096 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_list ap;
1102 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001103 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 va_end(ap);
1105 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001110 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001115 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 va_list ap;
1121 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001122 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_end(ap);
1124 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001129 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001134 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 va_list ap;
1140 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001141 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001147 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001152 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
1155 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 ScopedJniThreadState ts(env);
1158 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 va_start(ap, mid);
1160 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001161 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 va_end(ap);
1163 return local_result;
1164 }
1165
1166 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001170 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
1173 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001177 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
1180 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 ScopedJniThreadState ts(env);
1183 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 va_start(ap, mid);
1185 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 va_end(ap);
1187 return result.z;
1188 }
1189
1190 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
1196 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
1202 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
1205 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 va_start(ap, mid);
1207 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 va_end(ap);
1209 return result.b;
1210 }
1211
1212 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 }
1217
1218 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
1224 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 ScopedJniThreadState ts(env);
1227 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 va_start(ap, mid);
1229 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_end(ap);
1231 return result.c;
1232 }
1233
1234 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
1240 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
1246 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 ScopedJniThreadState ts(env);
1249 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 va_start(ap, mid);
1251 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 va_end(ap);
1253 return result.s;
1254 }
1255
1256 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
1262 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
1268 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
1271 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 va_start(ap, mid);
1273 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 va_end(ap);
1275 return result.i;
1276 }
1277
1278 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
1284 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
1290 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 ScopedJniThreadState ts(env);
1293 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 va_start(ap, mid);
1295 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 va_end(ap);
1297 return result.j;
1298 }
1299
1300 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
1306 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001307 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
1312 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 ScopedJniThreadState ts(env);
1315 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 va_start(ap, mid);
1317 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 va_end(ap);
1319 return result.f;
1320 }
1321
1322 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
1327
1328 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001329 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
1333
1334 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001335 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 ScopedJniThreadState ts(env);
1337 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 va_start(ap, mid);
1339 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 va_end(ap);
1341 return result.d;
1342 }
1343
1344 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
1350 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001351 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
1356 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001357 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 ScopedJniThreadState ts(env);
1359 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001360 va_start(ap, mid);
1361 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 va_end(ap);
1363 }
1364
1365 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001366 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001368 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
1371 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001372 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001374 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
1377 static jfieldID GetFieldID(JNIEnv* env,
1378 jclass c, const char* name, const char* sig) {
1379 ScopedJniThreadState ts(env);
1380 return FindFieldID(ts, c, name, sig, false);
1381 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001382
1383
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 static jfieldID GetStaticFieldID(JNIEnv* env,
1385 jclass c, const char* name, const char* sig) {
1386 ScopedJniThreadState ts(env);
1387 return FindFieldID(ts, c, name, sig, true);
1388 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001389
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001390 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 Object* o = Decode<Object*>(ts, obj);
1393 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001394 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001396
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001397 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001400 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 }
1402
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001403 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001405 Object* o = Decode<Object*>(ts, java_object);
1406 Object* v = Decode<Object*>(ts, java_value);
1407 Field* f = DecodeField(ts, fid);
1408 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 }
1410
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001411 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001413 Object* v = Decode<Object*>(ts, java_value);
1414 Field* f = DecodeField(ts, fid);
1415 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
1417
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001418#define GET_PRIMITIVE_FIELD(fn, instance) \
1419 ScopedJniThreadState ts(env); \
1420 Object* o = Decode<Object*>(ts, instance); \
1421 Field* f = DecodeField(ts, fid); \
1422 return f->fn(o)
1423
1424#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1425 ScopedJniThreadState ts(env); \
1426 Object* o = Decode<Object*>(ts, instance); \
1427 Field* f = DecodeField(ts, fid); \
1428 f->fn(o, value)
1429
1430 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1431 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1435 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1439 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1443 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1447 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1451 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1455 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1459 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001462 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1463 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 }
1465
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001466 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1467 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001470 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1471 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 }
1473
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1475 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001478 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1479 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 }
1481
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001482 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1483 GET_PRIMITIVE_FIELD(GetLong, NULL);
1484 }
1485
1486 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1487 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1488 }
1489
1490 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1491 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1492 }
1493
1494 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1495 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1496 }
1497
1498 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1499 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1500 }
1501
1502 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1503 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1504 }
1505
1506 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1507 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1508 }
1509
1510 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1511 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1512 }
1513
1514 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1515 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1516 }
1517
1518 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1519 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1520 }
1521
1522 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1523 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1524 }
1525
1526 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1527 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1528 }
1529
1530 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1531 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1532 }
1533
1534 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1535 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1536 }
1537
1538 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1539 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1540 }
1541
1542 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1543 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1544 }
1545
1546 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1547 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1548 }
1549
1550 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1551 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1552 }
1553
1554 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1555 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 }
1557
1558 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001559 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 ScopedJniThreadState ts(env);
1561 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001562 va_start(ap, mid);
1563 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001564 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 va_end(ap);
1566 return local_result;
1567 }
1568
1569 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001570 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001572 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001573 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 }
1575
1576 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001577 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001579 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001580 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
1583 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001584 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 ScopedJniThreadState ts(env);
1586 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001587 va_start(ap, mid);
1588 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 va_end(ap);
1590 return result.z;
1591 }
1592
1593 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001594 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001596 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
1599 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001600 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001602 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 ScopedJniThreadState ts(env);
1607 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001608 va_start(ap, mid);
1609 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 va_end(ap);
1611 return result.b;
1612 }
1613
1614 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001615 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001617 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
1620 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001621 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001623 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes72025e52011-08-23 17:50:30 -07001626 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 ScopedJniThreadState ts(env);
1628 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001629 va_start(ap, mid);
1630 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 va_end(ap);
1632 return result.c;
1633 }
1634
1635 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001636 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001638 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
1641 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001642 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 }
1646
Elliott Hughes72025e52011-08-23 17:50:30 -07001647 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 ScopedJniThreadState ts(env);
1649 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001650 va_start(ap, mid);
1651 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 va_end(ap);
1653 return result.s;
1654 }
1655
1656 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001657 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
1662 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001663 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 }
1667
Elliott Hughes72025e52011-08-23 17:50:30 -07001668 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 ScopedJniThreadState ts(env);
1670 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001671 va_start(ap, mid);
1672 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 va_end(ap);
1674 return result.i;
1675 }
1676
1677 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001680 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
1683 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 ScopedJniThreadState ts(env);
1691 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001692 va_start(ap, mid);
1693 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 va_end(ap);
1695 return result.j;
1696 }
1697
1698 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001701 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 }
1703
1704 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 }
1709
Elliott Hughes72025e52011-08-23 17:50:30 -07001710 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 ScopedJniThreadState ts(env);
1712 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001713 va_start(ap, mid);
1714 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 va_end(ap);
1716 return result.f;
1717 }
1718
1719 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
1725 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001726 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001728 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 ScopedJniThreadState ts(env);
1733 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001734 va_start(ap, mid);
1735 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 va_end(ap);
1737 return result.d;
1738 }
1739
1740 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001741 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
1746 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001749 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes72025e52011-08-23 17:50:30 -07001752 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
1754 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001755 va_start(ap, mid);
1756 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 va_end(ap);
1758 }
1759
1760 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001761 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001763 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 }
1765
1766 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001767 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001769 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 }
1771
Elliott Hughes814e4032011-08-23 12:07:56 -07001772 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001774 if (chars == NULL && char_count == 0) {
1775 return NULL;
1776 }
1777 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001778 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
1781 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1782 ScopedJniThreadState ts(env);
1783 if (utf == NULL) {
1784 return NULL;
1785 }
1786 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001787 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
Elliott Hughes814e4032011-08-23 12:07:56 -07001790 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1791 ScopedJniThreadState ts(env);
1792 return Decode<String*>(ts, java_string)->GetLength();
1793 }
1794
1795 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1796 ScopedJniThreadState ts(env);
1797 return Decode<String*>(ts, java_string)->GetUtfLength();
1798 }
1799
Elliott Hughesb465ab02011-08-24 11:21:21 -07001800 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001802 String* s = Decode<String*>(ts, java_string);
1803 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1804 ThrowSIOOBE(ts, start, length, s->GetLength());
1805 } else {
1806 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1807 memcpy(buf, chars + start, length * sizeof(jchar));
1808 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001809 }
1810
Elliott Hughesb465ab02011-08-24 11:21:21 -07001811 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001812 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001813 String* s = Decode<String*>(ts, java_string);
1814 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1815 ThrowSIOOBE(ts, start, length, s->GetLength());
1816 } else {
1817 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1818 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1819 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001820 }
1821
Elliott Hughes75770752011-08-24 17:52:38 -07001822 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001823 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001824 String* s = Decode<String*>(ts, java_string);
1825 const CharArray* chars = s->GetCharArray();
1826 PinPrimitiveArray(ts, chars);
1827 if (is_copy != NULL) {
1828 *is_copy = JNI_FALSE;
1829 }
1830 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001831 }
1832
Elliott Hughes75770752011-08-24 17:52:38 -07001833 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001834 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001835 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes75770752011-08-24 17:52:38 -07001838 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001840 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
Elliott Hughes75770752011-08-24 17:52:38 -07001843 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001845 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 }
1847
Elliott Hughes75770752011-08-24 17:52:38 -07001848 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001849 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001850 if (java_string == NULL) {
1851 return NULL;
1852 }
1853 if (is_copy != NULL) {
1854 *is_copy = JNI_TRUE;
1855 }
1856 String* s = Decode<String*>(ts, java_string);
1857 size_t byte_count = s->GetUtfLength();
1858 char* bytes = new char[byte_count + 1];
1859 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001860 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001861 return NULL;
1862 }
1863 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1864 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1865 bytes[byte_count] = '\0';
1866 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001867 }
1868
Elliott Hughes75770752011-08-24 17:52:38 -07001869 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001870 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001871 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001872 }
1873
Elliott Hughesbd935992011-08-22 11:59:34 -07001874 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001876 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001877 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001878 Array* array = obj->AsArray();
1879 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 }
1881
Elliott Hughes814e4032011-08-23 12:07:56 -07001882 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001884 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001885 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001886 }
1887
1888 static void SetObjectArrayElement(JNIEnv* env,
1889 jobjectArray java_array, jsize index, jobject java_value) {
1890 ScopedJniThreadState ts(env);
1891 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1892 Object* value = Decode<Object*>(ts, java_value);
1893 array->Set(index, value);
1894 }
1895
1896 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1897 ScopedJniThreadState ts(env);
1898 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1899 }
1900
1901 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1902 ScopedJniThreadState ts(env);
1903 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1904 }
1905
1906 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1907 ScopedJniThreadState ts(env);
1908 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1909 }
1910
1911 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1912 ScopedJniThreadState ts(env);
1913 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1914 }
1915
1916 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1917 ScopedJniThreadState ts(env);
1918 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1919 }
1920
1921 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1922 ScopedJniThreadState ts(env);
1923 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1924 }
1925
1926 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1927 ScopedJniThreadState ts(env);
1928 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1929 }
1930
1931 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1932 ScopedJniThreadState ts(env);
1933 CHECK_GE(length, 0); // TODO: ReportJniError
1934
1935 // Compute the array class corresponding to the given element class.
1936 Class* element_class = Decode<Class*>(ts, element_jclass);
1937 std::string descriptor;
1938 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001939 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001940
1941 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001942 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1943 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 return NULL;
1945 }
1946
Elliott Hughes75770752011-08-24 17:52:38 -07001947 // Allocate and initialize if necessary.
1948 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001950 if (initial_element != NULL) {
1951 Object* initial_object = Decode<Object*>(ts, initial_element);
1952 for (jsize i = 0; i < length; ++i) {
1953 result->Set(i, initial_object);
1954 }
1955 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001956 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 }
1958
1959 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1960 ScopedJniThreadState ts(env);
1961 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1962 }
1963
Elliott Hughes75770752011-08-24 17:52:38 -07001964 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001965 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001966 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001967 }
1968
Elliott Hughes75770752011-08-24 17:52:38 -07001969 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001970 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001971 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001972 }
1973
Elliott Hughes75770752011-08-24 17:52:38 -07001974 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001976 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Elliott Hughes75770752011-08-24 17:52:38 -07001979 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001981 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes75770752011-08-24 17:52:38 -07001984 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001986 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Elliott Hughes75770752011-08-24 17:52:38 -07001989 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001991 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Elliott Hughes75770752011-08-24 17:52:38 -07001994 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001996 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Elliott Hughes75770752011-08-24 17:52:38 -07001999 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002001 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes75770752011-08-24 17:52:38 -07002004 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002006 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Elliott Hughes75770752011-08-24 17:52:38 -07002009 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002011 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes75770752011-08-24 17:52:38 -07002014 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002016 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes75770752011-08-24 17:52:38 -07002019 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002021 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes75770752011-08-24 17:52:38 -07002024 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002026 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes75770752011-08-24 17:52:38 -07002029 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002031 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes75770752011-08-24 17:52:38 -07002034 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002036 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes75770752011-08-24 17:52:38 -07002039 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002041 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes75770752011-08-24 17:52:38 -07002044 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002046 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes75770752011-08-24 17:52:38 -07002049 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002051 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes814e4032011-08-23 12:07:56 -07002054 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughes814e4032011-08-23 12:07:56 -07002124 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002126 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Elliott Hughes814e4032011-08-23 12:07:56 -07002129 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002131 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
2133
Elliott Hughes5174fe62011-08-23 15:12:35 -07002134 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002136 Class* c = Decode<Class*>(ts, java_class);
2137
Elliott Hughes5174fe62011-08-23 15:12:35 -07002138 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 const char* name = methods[i].name;
2140 const char* sig = methods[i].signature;
2141
2142 if (*sig == '!') {
2143 // TODO: fast jni. it's too noisy to log all these.
2144 ++sig;
2145 }
2146
Elliott Hughes5174fe62011-08-23 15:12:35 -07002147 Method* m = c->FindDirectMethod(name, sig);
2148 if (m == NULL) {
2149 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002151 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002153 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2155 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002156 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002158 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002160 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2162 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002163 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 return JNI_ERR;
2165 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002166
Elliott Hughes75770752011-08-24 17:52:38 -07002167 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002168 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002169 }
2170
2171 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 }
2173 return JNI_OK;
2174 }
2175
Elliott Hughes5174fe62011-08-23 15:12:35 -07002176 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002178 Class* c = Decode<Class*>(ts, java_class);
2179
Elliott Hughes75770752011-08-24 17:52:38 -07002180 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002181 LOG(INFO) << "[Unregistering JNI native methods for "
2182 << PrettyDescriptor(c->GetDescriptor()) << "]";
2183 }
2184
2185 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2186 Method* m = c->GetDirectMethod(i);
2187 if (m->IsNative()) {
2188 m->UnregisterNative();
2189 }
2190 }
2191 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2192 Method* m = c->GetVirtualMethod(i);
2193 if (m->IsNative()) {
2194 m->UnregisterNative();
2195 }
2196 }
2197
2198 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 }
2200
Elliott Hughes72025e52011-08-23 17:50:30 -07002201 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002203 Decode<Object*>(ts, java_object)->MonitorEnter();
2204 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 }
2206
Elliott Hughes72025e52011-08-23 17:50:30 -07002207 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 ScopedJniThreadState ts(env);
Elliott Hughes02b48d12011-09-07 17:15:51 -07002209 Decode<Object*>(ts, java_object)->MonitorExit();
Elliott Hughes72025e52011-08-23 17:50:30 -07002210 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
2213 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2214 ScopedJniThreadState ts(env);
2215 Runtime* runtime = Runtime::Current();
2216 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002217 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 } else {
2219 *vm = NULL;
2220 }
2221 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2222 }
2223
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2225 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002226
2227 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002228 CHECK(address != NULL); // TODO: ReportJniError
2229 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002230
2231 jclass buffer_class = GetDirectByteBufferClass(env);
2232 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2233 if (mid == NULL) {
2234 return NULL;
2235 }
2236
2237 // At the moment, the Java side is limited to 32 bits.
2238 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2239 CHECK_LE(capacity, 0xffffffff);
2240 jint address_arg = reinterpret_cast<jint>(address);
2241 jint capacity_arg = static_cast<jint>(capacity);
2242
2243 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2244 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2250 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 }
2252
Elliott Hughesb465ab02011-08-24 11:21:21 -07002253 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002255 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2256 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 }
2258
Elliott Hughesb465ab02011-08-24 11:21:21 -07002259 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002260 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002261
Elliott Hughes75770752011-08-24 17:52:38 -07002262 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002263
2264 // Do we definitely know what kind of reference this is?
2265 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2266 IndirectRefKind kind = GetIndirectRefKind(ref);
2267 switch (kind) {
2268 case kLocal:
2269 return JNILocalRefType;
2270 case kGlobal:
2271 return JNIGlobalRefType;
2272 case kWeakGlobal:
2273 return JNIWeakGlobalRefType;
2274 case kSirtOrInvalid:
2275 // Is it in a stack IRT?
2276 if (ts.Self()->SirtContains(java_object)) {
2277 return JNILocalRefType;
2278 }
2279
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002280 if (!ts.Env()->work_around_app_jni_bugs) {
2281 return JNIInvalidRefType;
2282 }
2283
Elliott Hughesb465ab02011-08-24 11:21:21 -07002284 // If we're handing out direct pointers, check whether it's a direct pointer
2285 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002286 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002287 if (ts.Env()->locals.Contains(java_object)) {
2288 return JNILocalRefType;
2289 }
2290 }
2291
2292 return JNIInvalidRefType;
2293 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002294 }
2295};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002296
Elliott Hughesa2501992011-08-26 19:39:54 -07002297const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002298 NULL, // reserved0.
2299 NULL, // reserved1.
2300 NULL, // reserved2.
2301 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 JNI::GetVersion,
2303 JNI::DefineClass,
2304 JNI::FindClass,
2305 JNI::FromReflectedMethod,
2306 JNI::FromReflectedField,
2307 JNI::ToReflectedMethod,
2308 JNI::GetSuperclass,
2309 JNI::IsAssignableFrom,
2310 JNI::ToReflectedField,
2311 JNI::Throw,
2312 JNI::ThrowNew,
2313 JNI::ExceptionOccurred,
2314 JNI::ExceptionDescribe,
2315 JNI::ExceptionClear,
2316 JNI::FatalError,
2317 JNI::PushLocalFrame,
2318 JNI::PopLocalFrame,
2319 JNI::NewGlobalRef,
2320 JNI::DeleteGlobalRef,
2321 JNI::DeleteLocalRef,
2322 JNI::IsSameObject,
2323 JNI::NewLocalRef,
2324 JNI::EnsureLocalCapacity,
2325 JNI::AllocObject,
2326 JNI::NewObject,
2327 JNI::NewObjectV,
2328 JNI::NewObjectA,
2329 JNI::GetObjectClass,
2330 JNI::IsInstanceOf,
2331 JNI::GetMethodID,
2332 JNI::CallObjectMethod,
2333 JNI::CallObjectMethodV,
2334 JNI::CallObjectMethodA,
2335 JNI::CallBooleanMethod,
2336 JNI::CallBooleanMethodV,
2337 JNI::CallBooleanMethodA,
2338 JNI::CallByteMethod,
2339 JNI::CallByteMethodV,
2340 JNI::CallByteMethodA,
2341 JNI::CallCharMethod,
2342 JNI::CallCharMethodV,
2343 JNI::CallCharMethodA,
2344 JNI::CallShortMethod,
2345 JNI::CallShortMethodV,
2346 JNI::CallShortMethodA,
2347 JNI::CallIntMethod,
2348 JNI::CallIntMethodV,
2349 JNI::CallIntMethodA,
2350 JNI::CallLongMethod,
2351 JNI::CallLongMethodV,
2352 JNI::CallLongMethodA,
2353 JNI::CallFloatMethod,
2354 JNI::CallFloatMethodV,
2355 JNI::CallFloatMethodA,
2356 JNI::CallDoubleMethod,
2357 JNI::CallDoubleMethodV,
2358 JNI::CallDoubleMethodA,
2359 JNI::CallVoidMethod,
2360 JNI::CallVoidMethodV,
2361 JNI::CallVoidMethodA,
2362 JNI::CallNonvirtualObjectMethod,
2363 JNI::CallNonvirtualObjectMethodV,
2364 JNI::CallNonvirtualObjectMethodA,
2365 JNI::CallNonvirtualBooleanMethod,
2366 JNI::CallNonvirtualBooleanMethodV,
2367 JNI::CallNonvirtualBooleanMethodA,
2368 JNI::CallNonvirtualByteMethod,
2369 JNI::CallNonvirtualByteMethodV,
2370 JNI::CallNonvirtualByteMethodA,
2371 JNI::CallNonvirtualCharMethod,
2372 JNI::CallNonvirtualCharMethodV,
2373 JNI::CallNonvirtualCharMethodA,
2374 JNI::CallNonvirtualShortMethod,
2375 JNI::CallNonvirtualShortMethodV,
2376 JNI::CallNonvirtualShortMethodA,
2377 JNI::CallNonvirtualIntMethod,
2378 JNI::CallNonvirtualIntMethodV,
2379 JNI::CallNonvirtualIntMethodA,
2380 JNI::CallNonvirtualLongMethod,
2381 JNI::CallNonvirtualLongMethodV,
2382 JNI::CallNonvirtualLongMethodA,
2383 JNI::CallNonvirtualFloatMethod,
2384 JNI::CallNonvirtualFloatMethodV,
2385 JNI::CallNonvirtualFloatMethodA,
2386 JNI::CallNonvirtualDoubleMethod,
2387 JNI::CallNonvirtualDoubleMethodV,
2388 JNI::CallNonvirtualDoubleMethodA,
2389 JNI::CallNonvirtualVoidMethod,
2390 JNI::CallNonvirtualVoidMethodV,
2391 JNI::CallNonvirtualVoidMethodA,
2392 JNI::GetFieldID,
2393 JNI::GetObjectField,
2394 JNI::GetBooleanField,
2395 JNI::GetByteField,
2396 JNI::GetCharField,
2397 JNI::GetShortField,
2398 JNI::GetIntField,
2399 JNI::GetLongField,
2400 JNI::GetFloatField,
2401 JNI::GetDoubleField,
2402 JNI::SetObjectField,
2403 JNI::SetBooleanField,
2404 JNI::SetByteField,
2405 JNI::SetCharField,
2406 JNI::SetShortField,
2407 JNI::SetIntField,
2408 JNI::SetLongField,
2409 JNI::SetFloatField,
2410 JNI::SetDoubleField,
2411 JNI::GetStaticMethodID,
2412 JNI::CallStaticObjectMethod,
2413 JNI::CallStaticObjectMethodV,
2414 JNI::CallStaticObjectMethodA,
2415 JNI::CallStaticBooleanMethod,
2416 JNI::CallStaticBooleanMethodV,
2417 JNI::CallStaticBooleanMethodA,
2418 JNI::CallStaticByteMethod,
2419 JNI::CallStaticByteMethodV,
2420 JNI::CallStaticByteMethodA,
2421 JNI::CallStaticCharMethod,
2422 JNI::CallStaticCharMethodV,
2423 JNI::CallStaticCharMethodA,
2424 JNI::CallStaticShortMethod,
2425 JNI::CallStaticShortMethodV,
2426 JNI::CallStaticShortMethodA,
2427 JNI::CallStaticIntMethod,
2428 JNI::CallStaticIntMethodV,
2429 JNI::CallStaticIntMethodA,
2430 JNI::CallStaticLongMethod,
2431 JNI::CallStaticLongMethodV,
2432 JNI::CallStaticLongMethodA,
2433 JNI::CallStaticFloatMethod,
2434 JNI::CallStaticFloatMethodV,
2435 JNI::CallStaticFloatMethodA,
2436 JNI::CallStaticDoubleMethod,
2437 JNI::CallStaticDoubleMethodV,
2438 JNI::CallStaticDoubleMethodA,
2439 JNI::CallStaticVoidMethod,
2440 JNI::CallStaticVoidMethodV,
2441 JNI::CallStaticVoidMethodA,
2442 JNI::GetStaticFieldID,
2443 JNI::GetStaticObjectField,
2444 JNI::GetStaticBooleanField,
2445 JNI::GetStaticByteField,
2446 JNI::GetStaticCharField,
2447 JNI::GetStaticShortField,
2448 JNI::GetStaticIntField,
2449 JNI::GetStaticLongField,
2450 JNI::GetStaticFloatField,
2451 JNI::GetStaticDoubleField,
2452 JNI::SetStaticObjectField,
2453 JNI::SetStaticBooleanField,
2454 JNI::SetStaticByteField,
2455 JNI::SetStaticCharField,
2456 JNI::SetStaticShortField,
2457 JNI::SetStaticIntField,
2458 JNI::SetStaticLongField,
2459 JNI::SetStaticFloatField,
2460 JNI::SetStaticDoubleField,
2461 JNI::NewString,
2462 JNI::GetStringLength,
2463 JNI::GetStringChars,
2464 JNI::ReleaseStringChars,
2465 JNI::NewStringUTF,
2466 JNI::GetStringUTFLength,
2467 JNI::GetStringUTFChars,
2468 JNI::ReleaseStringUTFChars,
2469 JNI::GetArrayLength,
2470 JNI::NewObjectArray,
2471 JNI::GetObjectArrayElement,
2472 JNI::SetObjectArrayElement,
2473 JNI::NewBooleanArray,
2474 JNI::NewByteArray,
2475 JNI::NewCharArray,
2476 JNI::NewShortArray,
2477 JNI::NewIntArray,
2478 JNI::NewLongArray,
2479 JNI::NewFloatArray,
2480 JNI::NewDoubleArray,
2481 JNI::GetBooleanArrayElements,
2482 JNI::GetByteArrayElements,
2483 JNI::GetCharArrayElements,
2484 JNI::GetShortArrayElements,
2485 JNI::GetIntArrayElements,
2486 JNI::GetLongArrayElements,
2487 JNI::GetFloatArrayElements,
2488 JNI::GetDoubleArrayElements,
2489 JNI::ReleaseBooleanArrayElements,
2490 JNI::ReleaseByteArrayElements,
2491 JNI::ReleaseCharArrayElements,
2492 JNI::ReleaseShortArrayElements,
2493 JNI::ReleaseIntArrayElements,
2494 JNI::ReleaseLongArrayElements,
2495 JNI::ReleaseFloatArrayElements,
2496 JNI::ReleaseDoubleArrayElements,
2497 JNI::GetBooleanArrayRegion,
2498 JNI::GetByteArrayRegion,
2499 JNI::GetCharArrayRegion,
2500 JNI::GetShortArrayRegion,
2501 JNI::GetIntArrayRegion,
2502 JNI::GetLongArrayRegion,
2503 JNI::GetFloatArrayRegion,
2504 JNI::GetDoubleArrayRegion,
2505 JNI::SetBooleanArrayRegion,
2506 JNI::SetByteArrayRegion,
2507 JNI::SetCharArrayRegion,
2508 JNI::SetShortArrayRegion,
2509 JNI::SetIntArrayRegion,
2510 JNI::SetLongArrayRegion,
2511 JNI::SetFloatArrayRegion,
2512 JNI::SetDoubleArrayRegion,
2513 JNI::RegisterNatives,
2514 JNI::UnregisterNatives,
2515 JNI::MonitorEnter,
2516 JNI::MonitorExit,
2517 JNI::GetJavaVM,
2518 JNI::GetStringRegion,
2519 JNI::GetStringUTFRegion,
2520 JNI::GetPrimitiveArrayCritical,
2521 JNI::ReleasePrimitiveArrayCritical,
2522 JNI::GetStringCritical,
2523 JNI::ReleaseStringCritical,
2524 JNI::NewWeakGlobalRef,
2525 JNI::DeleteWeakGlobalRef,
2526 JNI::ExceptionCheck,
2527 JNI::NewDirectByteBuffer,
2528 JNI::GetDirectBufferAddress,
2529 JNI::GetDirectBufferCapacity,
2530 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002531};
2532
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002533static const size_t kMonitorsInitial = 32; // Arbitrary.
2534static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2535
2536static const size_t kLocalsInitial = 64; // Arbitrary.
2537static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002538
Elliott Hughes75770752011-08-24 17:52:38 -07002539JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002540 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002541 vm(vm),
2542 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002543 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002544 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002545 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2546 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002547 functions = unchecked_functions = &gNativeInterface;
2548 if (check_jni) {
2549 functions = GetCheckJniNativeInterface();
2550 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002551}
2552
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002553JNIEnvExt::~JNIEnvExt() {
2554}
2555
Carl Shapiroea4dca82011-08-01 13:45:38 -07002556// JNI Invocation interface.
2557
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002558extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2559 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2560 if (args->version < JNI_VERSION_1_2) {
2561 return JNI_EVERSION;
2562 }
2563 Runtime::Options options;
2564 for (int i = 0; i < args->nOptions; ++i) {
2565 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002566 options.push_back(std::make_pair(StringPiece(option->optionString),
2567 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002568 }
2569 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002570 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002571 if (runtime == NULL) {
2572 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002573 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002574 runtime->Start();
2575 *p_env = Thread::Current()->GetJniEnv();
2576 *p_vm = runtime->GetJavaVM();
2577 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002578}
2579
Elliott Hughesf2682d52011-08-15 16:37:04 -07002580extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002581 Runtime* runtime = Runtime::Current();
2582 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002583 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002584 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002585 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002586 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002587 }
2588 return JNI_OK;
2589}
2590
2591// Historically unsupported.
2592extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2593 return JNI_ERR;
2594}
2595
Elliott Hughescdf53122011-08-19 15:46:09 -07002596class JII {
2597 public:
2598 static jint DestroyJavaVM(JavaVM* vm) {
2599 if (vm == NULL) {
2600 return JNI_ERR;
2601 } else {
2602 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2603 delete raw_vm->runtime;
2604 return JNI_OK;
2605 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002606 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002607
Elliott Hughescdf53122011-08-19 15:46:09 -07002608 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002609 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002610 }
2611
2612 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002613 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002614 }
2615
2616 static jint DetachCurrentThread(JavaVM* vm) {
2617 if (vm == NULL) {
2618 return JNI_ERR;
2619 } else {
2620 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2621 Runtime* runtime = raw_vm->runtime;
2622 runtime->DetachCurrentThread();
2623 return JNI_OK;
2624 }
2625 }
2626
2627 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2628 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2629 return JNI_EVERSION;
2630 }
2631 if (vm == NULL || env == NULL) {
2632 return JNI_ERR;
2633 }
2634 Thread* thread = Thread::Current();
2635 if (thread == NULL) {
2636 *env = NULL;
2637 return JNI_EDETACHED;
2638 }
2639 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002640 return JNI_OK;
2641 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002642};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002643
Elliott Hughesa2501992011-08-26 19:39:54 -07002644const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002645 NULL, // reserved0
2646 NULL, // reserved1
2647 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002648 JII::DestroyJavaVM,
2649 JII::AttachCurrentThread,
2650 JII::DetachCurrentThread,
2651 JII::GetEnv,
2652 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002653};
2654
Elliott Hughesbbd76712011-08-17 10:25:24 -07002655static const size_t kPinTableInitialSize = 16;
2656static const size_t kPinTableMaxSize = 1024;
2657
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002658static const size_t kGlobalsInitial = 512; // Arbitrary.
2659static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2660
2661static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2662static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2663
Elliott Hughesa0957642011-09-02 14:27:33 -07002664JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002665 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002666 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002667 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002668 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002669 verbose_jni(options->IsVerbose("jni")),
2670 log_third_party_jni(options->IsVerbose("third-party-jni")),
2671 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002672 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002673 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002674 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002675 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002676 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002677 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002678 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002679 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002680 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002681 functions = unchecked_functions = &gInvokeInterface;
2682 if (check_jni) {
2683 functions = GetCheckJniInvokeInterface();
2684 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002685}
2686
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002687JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002688 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002689}
2690
Elliott Hughes75770752011-08-24 17:52:38 -07002691bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2692 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002693
2694 // See if we've already loaded this library. If we have, and the class loader
2695 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002696 // TODO: for better results we should canonicalize the pathname (or even compare
2697 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002698 SharedLibrary* library;
2699 {
2700 // TODO: move the locking (and more of this logic) into Libraries.
2701 MutexLock mu(libraries_lock);
2702 library = libraries->Get(path);
2703 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002704 if (library != NULL) {
2705 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002706 // The library will be associated with class_loader. The JNI
2707 // spec says we can't load the same library into more than one
2708 // class loader.
2709 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2710 "ClassLoader %p; can't open in ClassLoader %p",
2711 path.c_str(), library->GetClassLoader(), class_loader);
2712 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002713 return false;
2714 }
2715 if (verbose_jni) {
2716 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2717 << "ClassLoader " << class_loader << "]";
2718 }
2719 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002720 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2721 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002722 return false;
2723 }
2724 return true;
2725 }
2726
2727 // Open the shared library. Because we're using a full path, the system
2728 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2729 // resolve this library's dependencies though.)
2730
2731 // Failures here are expected when java.library.path has several entries
2732 // and we have to hunt for the lib.
2733
2734 // The current version of the dynamic linker prints detailed information
2735 // about dlopen() failures. Some things to check if the message is
2736 // cryptic:
2737 // - make sure the library exists on the device
2738 // - verify that the right path is being opened (the debug log message
2739 // above can help with that)
2740 // - check to see if the library is valid (e.g. not zero bytes long)
2741 // - check config/prelink-linux-arm.map to ensure that the library
2742 // is listed and is not being overrun by the previous entry (if
2743 // loading suddenly stops working on a prelinked library, this is
2744 // a good one to check)
2745 // - write a trivial app that calls sleep() then dlopen(), attach
2746 // to it with "strace -p <pid>" while it sleeps, and watch for
2747 // attempts to open nonexistent dependent shared libs
2748
2749 // TODO: automate some of these checks!
2750
2751 // This can execute slowly for a large library on a busy system, so we
2752 // want to switch from RUNNING to VMWAIT while it executes. This allows
2753 // the GC to ignore us.
2754 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002755 void* handle = NULL;
2756 {
2757 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
2758 handle = dlopen(path.c_str(), RTLD_LAZY);
2759 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002760
2761 if (verbose_jni) {
2762 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2763 }
2764
2765 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002766 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002767 return false;
2768 }
2769
2770 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002771 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002772 // TODO: move the locking (and more of this logic) into Libraries.
2773 MutexLock mu(libraries_lock);
2774 library = libraries->Get(path);
2775 if (library != NULL) {
2776 LOG(INFO) << "WOW: we lost a race to add shared library: "
2777 << "\"" << path << "\" ClassLoader=" << class_loader;
2778 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002779 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002780 library = new SharedLibrary(path, handle, class_loader);
2781 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002782 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002783
2784 if (verbose_jni) {
2785 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2786 }
2787
2788 bool result = true;
2789 void* sym = dlsym(handle, "JNI_OnLoad");
2790 if (sym == NULL) {
2791 if (verbose_jni) {
2792 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2793 }
2794 } else {
2795 // Call JNI_OnLoad. We have to override the current class
2796 // loader, which will always be "null" since the stuff at the
2797 // top of the stack is around Runtime.loadLibrary(). (See
2798 // the comments in the JNI FindClass function.)
2799 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2800 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002801 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002802 self->SetClassLoaderOverride(class_loader);
2803
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002804 int version = 0;
2805 {
2806 ScopedThreadStateChange tsc(self, Thread::kNative);
2807 if (verbose_jni) {
2808 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2809 }
2810 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002811 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002812
2813 self->SetClassLoaderOverride(old_class_loader);;
2814
2815 if (version != JNI_VERSION_1_2 &&
2816 version != JNI_VERSION_1_4 &&
2817 version != JNI_VERSION_1_6) {
2818 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2819 << "bad version: " << version;
2820 // It's unwise to call dlclose() here, but we can mark it
2821 // as bad and ensure that future load attempts will fail.
2822 // We don't know how far JNI_OnLoad got, so there could
2823 // be some partially-initialized stuff accessible through
2824 // newly-registered native method calls. We could try to
2825 // unregister them, but that doesn't seem worthwhile.
2826 result = false;
2827 } else {
2828 if (verbose_jni) {
2829 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2830 << " from JNI_OnLoad in \"" << path << "\"]";
2831 }
2832 }
2833 }
2834
2835 library->SetResult(result);
2836 return result;
2837}
2838
2839void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2840 CHECK(m->IsNative());
2841
2842 Class* c = m->GetDeclaringClass();
2843
2844 // If this is a static method, it could be called before the class
2845 // has been initialized.
2846 if (m->IsStatic()) {
2847 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2848 return NULL;
2849 }
2850 } else {
2851 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2852 }
2853
2854 MutexLock mu(libraries_lock);
2855 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002856}
2857
Elliott Hughes410c0c82011-09-01 17:58:25 -07002858void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2859 {
2860 MutexLock mu(globals_lock);
2861 globals.VisitRoots(visitor, arg);
2862 }
2863 {
2864 MutexLock mu(pins_lock);
2865 pin_table.VisitRoots(visitor, arg);
2866 }
2867 // The weak_globals table is visited by the GC itself (because it mutates the table).
2868}
2869
Ian Rogersdf20fe02011-07-20 20:34:16 -07002870} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002871
2872std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2873 switch (rhs) {
2874 case JNIInvalidRefType:
2875 os << "JNIInvalidRefType";
2876 return os;
2877 case JNILocalRefType:
2878 os << "JNILocalRefType";
2879 return os;
2880 case JNIGlobalRefType:
2881 os << "JNIGlobalRefType";
2882 return os;
2883 case JNIWeakGlobalRefType:
2884 os << "JNIWeakGlobalRefType";
2885 return os;
2886 }
2887}