blob: 0129fc5fbb36f1cc2a36b1744e5f9f19f1d3be61 [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);
Ian Rogersaaa20802011-09-11 21:47:37 -0700114template ObjectArray<StackTraceElement>*
115 Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700116
117namespace {
118
Elliott Hughescdf53122011-08-19 15:46:09 -0700119jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
120 if (obj == NULL) {
121 return NULL;
122 }
Elliott Hughes75770752011-08-24 17:52:38 -0700123 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700124 IndirectReferenceTable& weak_globals = vm->weak_globals;
125 MutexLock mu(vm->weak_globals_lock);
126 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
127 return reinterpret_cast<jweak>(ref);
128}
129
Elliott Hughesbf86d042011-08-31 17:53:14 -0700130// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700131template<typename T>
132T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700133 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700134}
135
Elliott Hughescdf53122011-08-19 15:46:09 -0700136Field* DecodeField(ScopedJniThreadState& ts, jfieldID fid) {
137 return Decode<Field*>(ts, reinterpret_cast<jweak>(fid));
138}
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700139
Elliott Hughescdf53122011-08-19 15:46:09 -0700140Method* DecodeMethod(ScopedJniThreadState& ts, jmethodID mid) {
141 return Decode<Method*>(ts, reinterpret_cast<jweak>(mid));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700142}
143
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700144byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, va_list ap) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700145 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700146 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700147 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700148 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
149 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700150 case 'Z':
151 case 'B':
152 case 'C':
153 case 'S':
154 case 'I':
155 *reinterpret_cast<int32_t*>(&arg_array[offset]) = va_arg(ap, jint);
156 offset += 4;
157 break;
158 case 'F':
159 *reinterpret_cast<float*>(&arg_array[offset]) = va_arg(ap, jdouble);
160 offset += 4;
161 break;
162 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700163 Object* obj = Decode<Object*>(ts, va_arg(ap, jobject));
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700164 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
165 offset += sizeof(Object*);
166 break;
167 }
168 case 'D':
169 *reinterpret_cast<double*>(&arg_array[offset]) = va_arg(ap, jdouble);
170 offset += 8;
171 break;
172 case 'J':
173 *reinterpret_cast<int64_t*>(&arg_array[offset]) = va_arg(ap, jlong);
174 offset += 8;
175 break;
176 }
177 }
178 return arg_array.release();
179}
180
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700181byte* CreateArgArray(ScopedJniThreadState& ts, Method* method, jvalue* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700182 size_t num_bytes = method->NumArgArrayBytes();
Elliott Hughes90a33692011-08-30 13:27:07 -0700183 UniquePtr<byte[]> arg_array(new byte[num_bytes]);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700184 const String* shorty = method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700185 for (int i = 1, offset = 0; i < shorty->GetLength(); ++i) {
186 switch (shorty->CharAt(i)) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700187 case 'Z':
188 case 'B':
189 case 'C':
190 case 'S':
191 case 'I':
192 *reinterpret_cast<uint32_t*>(&arg_array[offset]) = args[i - 1].i;
193 offset += 4;
194 break;
195 case 'F':
196 *reinterpret_cast<float*>(&arg_array[offset]) = args[i - 1].f;
197 offset += 4;
198 break;
199 case 'L': {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700200 Object* obj = Decode<Object*>(ts, args[i - 1].l);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700201 *reinterpret_cast<Object**>(&arg_array[offset]) = obj;
202 offset += sizeof(Object*);
203 break;
204 }
205 case 'D':
206 *reinterpret_cast<double*>(&arg_array[offset]) = args[i - 1].d;
207 offset += 8;
208 break;
209 case 'J':
210 *reinterpret_cast<uint64_t*>(&arg_array[offset]) = args[i - 1].j;
211 offset += 8;
212 break;
213 }
214 }
215 return arg_array.release();
216}
217
Elliott Hughes72025e52011-08-23 17:50:30 -0700218JValue InvokeWithArgArray(ScopedJniThreadState& ts, Object* receiver,
219 Method* method, byte* args) {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700220 JValue result;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700221 method->Invoke(ts.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700222 return result;
223}
224
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700225JValue InvokeWithJValues(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700226 jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700227 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700228 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700229 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700230 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700231}
232
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700233JValue InvokeWithVarArgs(ScopedJniThreadState& ts, jobject obj,
Elliott Hughescdf53122011-08-19 15:46:09 -0700234 jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700235 Object* receiver = Decode<Object*>(ts, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700236 Method* method = DecodeMethod(ts, mid);
Elliott Hughes90a33692011-08-30 13:27:07 -0700237 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700238 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
239}
240
Elliott Hughes75770752011-08-24 17:52:38 -0700241Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700242 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700243}
244
Brian Carlstrom30b94452011-08-25 21:35:26 -0700245JValue InvokeVirtualOrInterfaceWithJValues(ScopedJniThreadState& ts, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700246 Object* receiver = Decode<Object*>(ts, obj);
247 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700248 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700249 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
250}
251
Brian Carlstrom30b94452011-08-25 21:35:26 -0700252JValue InvokeVirtualOrInterfaceWithVarArgs(ScopedJniThreadState& ts, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700253 Object* receiver = Decode<Object*>(ts, obj);
254 Method* method = FindVirtualMethod(receiver, DecodeMethod(ts, mid));
Elliott Hughes90a33692011-08-30 13:27:07 -0700255 UniquePtr<byte[]> arg_array(CreateArgArray(ts, method, args));
Elliott Hughes72025e52011-08-23 17:50:30 -0700256 return InvokeWithArgArray(ts, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700257}
258
Elliott Hughes6b436852011-08-12 10:16:44 -0700259// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
260// separated with slashes but aren't wrapped with "L;" like regular descriptors
261// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
262// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
263// supported names with dots too (such as "a.b.C").
264std::string NormalizeJniClassDescriptor(const char* name) {
265 std::string result;
266 // Add the missing "L;" if necessary.
267 if (name[0] == '[') {
268 result = name;
269 } else {
270 result += 'L';
271 result += name;
272 result += ';';
273 }
274 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700275 if (result.find('.') != std::string::npos) {
276 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
277 << "\"" << name << "\"";
278 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700279 }
280 return result;
281}
282
Elliott Hughescdf53122011-08-19 15:46:09 -0700283jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
284 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700285 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
286 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700287 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700288
289 Method* method = NULL;
290 if (is_static) {
291 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700292 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700293 method = c->FindVirtualMethod(name, sig);
294 if (method == NULL) {
295 // No virtual method matching the signature. Search declared
296 // private methods and constructors.
297 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700298 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700299 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700300
Elliott Hughescdf53122011-08-19 15:46:09 -0700301 if (method == NULL || method->IsStatic() != is_static) {
302 Thread* self = Thread::Current();
Elliott Hughesa2501992011-08-26 19:39:54 -0700303 std::string method_name(PrettyMethod(method));
Elliott Hughescdf53122011-08-19 15:46:09 -0700304 // TODO: try searching for the opposite kind of method from is_static
305 // for better diagnostics?
306 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700307 "no %s method %s", is_static ? "static" : "non-static",
308 method_name.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -0700309 return NULL;
310 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700311
Elliott Hughescdf53122011-08-19 15:46:09 -0700312 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Carl Shapiroea4dca82011-08-01 13:45:38 -0700313}
314
Elliott Hughescdf53122011-08-19 15:46:09 -0700315jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
316 Class* c = Decode<Class*>(ts, jni_class);
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700317 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
318 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700319 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700320
321 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700322 Class* field_type;
323 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
324 if (sig[1] != '\0') {
325 // TODO: need to get the appropriate ClassLoader.
326 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
327 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700328 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700330 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700331 if (field_type == NULL) {
332 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700333 DCHECK(ts.Self()->IsExceptionPending());
334 ts.Self()->ClearException();
335 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
336 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
337 "no type \"%s\" found and so no field \"%s\" could be found in class "
338 "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700339 return NULL;
340 }
341 if (is_static) {
342 field = c->FindStaticField(name, field_type);
343 } else {
344 field = c->FindInstanceField(name, field_type);
345 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700346 if (field == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700347 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Ian Rogersb17d08b2011-09-02 16:16:49 -0700348 ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700349 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700350 name, class_descriptor.c_str());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700351 return NULL;
352 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700353 // Check invariant that all jfieldIDs have resolved types (how else would
354 // the type equality in Find...Field hold?)
355 DCHECK(field->GetType() != NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -0700356 jweak fid = AddWeakGlobalReference(ts, field);
357 return reinterpret_cast<jfieldID>(fid);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700358}
359
Elliott Hughes75770752011-08-24 17:52:38 -0700360void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
361 JavaVMExt* vm = ts.Vm();
362 MutexLock mu(vm->pins_lock);
363 vm->pin_table.Add(array);
364}
365
366void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
367 JavaVMExt* vm = ts.Vm();
368 MutexLock mu(vm->pins_lock);
369 vm->pin_table.Remove(array);
370}
371
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700372template<typename JniT, typename ArtT>
373JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
374 CHECK_GE(length, 0); // TODO: ReportJniError
375 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700376 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700377}
378
Elliott Hughes75770752011-08-24 17:52:38 -0700379template <typename ArrayT, typename CArrayT, typename ArtArrayT>
380CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
381 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
382 PinPrimitiveArray(ts, array);
383 if (is_copy != NULL) {
384 *is_copy = JNI_FALSE;
385 }
386 return array->GetData();
387}
388
389template <typename ArrayT>
390void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
391 if (mode != JNI_COMMIT) {
392 Array* array = Decode<Array*>(ts, java_array);
393 UnpinPrimitiveArray(ts, array);
394 }
395}
396
Elliott Hughes814e4032011-08-23 12:07:56 -0700397void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
398 std::string type(PrettyType(array));
399 ts.Self()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
400 "%s offset=%d length=%d %s.length=%d",
401 type.c_str(), start, length, identifier, array->GetLength());
402}
Elliott Hughesb465ab02011-08-24 11:21:21 -0700403void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
404 ts.Self()->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
405 "offset=%d length=%d string.length()=%d", start, length, array_length);
406}
Elliott Hughes814e4032011-08-23 12:07:56 -0700407
408template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700409void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700410 ArrayT* array = Decode<ArrayT*>(ts, java_array);
411 if (start < 0 || length < 0 || start + length > array->GetLength()) {
412 ThrowAIOOBE(ts, array, start, length, "src");
413 } else {
414 JavaT* data = array->GetData();
415 memcpy(buf, data + start, length * sizeof(JavaT));
416 }
417}
418
419template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes75770752011-08-24 17:52:38 -0700420void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700421 ArrayT* array = Decode<ArrayT*>(ts, java_array);
422 if (start < 0 || length < 0 || start + length > array->GetLength()) {
423 ThrowAIOOBE(ts, array, start, length, "dst");
424 } else {
425 JavaT* data = array->GetData();
426 memcpy(data + start, buf, length * sizeof(JavaT));
427 }
428}
429
Elliott Hughes75770752011-08-24 17:52:38 -0700430jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700431 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
432 CHECK(buffer_class.get() != NULL);
433 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
434}
435
Elliott Hughes75770752011-08-24 17:52:38 -0700436jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700437 static jclass buffer_class = InitDirectByteBufferClass(env);
438 return buffer_class;
439}
440
Elliott Hughes75770752011-08-24 17:52:38 -0700441jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
442 if (vm == NULL || p_env == NULL) {
443 return JNI_ERR;
444 }
445
446 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
447 JavaVMAttachArgs args;
448 if (thr_args == NULL) {
449 // Allow the v1.1 calling convention.
450 args.version = JNI_VERSION_1_2;
451 args.name = NULL;
452 args.group = NULL; // TODO: get "main" thread group
453 } else {
454 args.version = in_args->version;
455 args.name = in_args->name;
456 if (in_args->group != NULL) {
457 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
458 args.group = NULL; // TODO: decode in_args->group
459 } else {
460 args.group = NULL; // TODO: get "main" thread group
461 }
462 }
463 CHECK_GE(args.version, JNI_VERSION_1_2);
464
465 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700466 runtime->AttachCurrentThread(args.name, as_daemon);
467 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700468 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700469}
470
Elliott Hughes79082e32011-08-25 12:07:32 -0700471class SharedLibrary {
472 public:
473 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
474 : path_(path),
475 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700476 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700477 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700478 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700479 jni_on_load_result_(kPending) {
480 pthread_cond_init(&jni_on_load_cond_, NULL);
481 }
482
Elliott Hughes79082e32011-08-25 12:07:32 -0700483 Object* GetClassLoader() {
484 return class_loader_;
485 }
486
487 std::string GetPath() {
488 return path_;
489 }
490
491 /*
492 * Check the result of an earlier call to JNI_OnLoad on this library. If
493 * the call has not yet finished in another thread, wait for it.
494 */
495 bool CheckOnLoadResult(JavaVMExt* vm) {
496 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700497 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700498 // Check this so we don't end up waiting for ourselves. We need
499 // to return "true" so the caller can continue.
500 LOG(INFO) << *self << " recursive attempt to load library "
501 << "\"" << path_ << "\"";
502 return true;
503 }
504
505 MutexLock mu(jni_on_load_lock_);
506 while (jni_on_load_result_ == kPending) {
507 if (vm->verbose_jni) {
508 LOG(INFO) << "[" << *self << " waiting for \"" << path_ << "\" "
509 << "JNI_OnLoad...]";
510 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700511 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
Elliott Hughes8daa0922011-09-11 13:46:25 -0700512 pthread_cond_wait(&jni_on_load_cond_, jni_on_load_lock_.GetImpl());
Elliott Hughes79082e32011-08-25 12:07:32 -0700513 }
514
515 bool okay = (jni_on_load_result_ == kOkay);
516 if (vm->verbose_jni) {
517 LOG(INFO) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
518 << (okay ? "succeeded" : "failed") << "]";
519 }
520 return okay;
521 }
522
523 void SetResult(bool result) {
524 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700525 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700526
527 // Broadcast a wakeup to anybody sleeping on the condition variable.
528 MutexLock mu(jni_on_load_lock_);
529 pthread_cond_broadcast(&jni_on_load_cond_);
530 }
531
532 void* FindSymbol(const std::string& symbol_name) {
533 return dlsym(handle_, symbol_name.c_str());
534 }
535
536 private:
537 enum JNI_OnLoadState {
538 kPending,
539 kFailed,
540 kOkay,
541 };
542
543 // Path to library "/system/lib/libjni.so".
544 std::string path_;
545
546 // The void* returned by dlopen(3).
547 void* handle_;
548
549 // The ClassLoader this library is associated with.
550 Object* class_loader_;
551
552 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700553 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 // Wait for JNI_OnLoad in other thread.
555 pthread_cond_t jni_on_load_cond_;
556 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700557 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700558 // Result of earlier JNI_OnLoad call.
559 JNI_OnLoadState jni_on_load_result_;
560};
561
Elliott Hughescdf53122011-08-19 15:46:09 -0700562} // namespace
Carl Shapiroea4dca82011-08-01 13:45:38 -0700563
Elliott Hughes79082e32011-08-25 12:07:32 -0700564// This exists mainly to keep implementation details out of the header file.
565class Libraries {
566 public:
567 Libraries() {
568 }
569
570 ~Libraries() {
571 // Delete our map values. (The keys will be cleaned up by the map itself.)
572 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
573 delete it->second;
574 }
575 }
576
577 SharedLibrary* Get(const std::string& path) {
578 return libraries_[path];
579 }
580
581 void Put(const std::string& path, SharedLibrary* library) {
582 libraries_[path] = library;
583 }
584
585 // See section 11.3 "Linking Native Methods" of the JNI spec.
586 void* FindNativeMethod(const Method* m) {
587 std::string jni_short_name(JniShortName(m));
588 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700589 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
591 SharedLibrary* library = it->second;
592 if (library->GetClassLoader() != declaring_class_loader) {
593 // We only search libraries loaded by the appropriate ClassLoader.
594 continue;
595 }
596 // Try the short name then the long name...
597 void* fn = library->FindSymbol(jni_short_name);
598 if (fn == NULL) {
599 fn = library->FindSymbol(jni_long_name);
600 }
601 if (fn != NULL) {
602 if (Runtime::Current()->GetJavaVM()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700603 LOG(INFO) << "[Found native code for " << PrettyMethod(m)
Elliott Hughes79082e32011-08-25 12:07:32 -0700604 << " in \"" << library->GetPath() << "\"]";
605 }
606 return fn;
607 }
608 }
609 std::string detail;
610 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700611 detail += PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -0700612 LOG(ERROR) << detail;
613 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;",
614 "%s", detail.c_str());
615 return NULL;
616 }
617
618 private:
619 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
620
621 std::map<std::string, SharedLibrary*> libraries_;
622};
623
Elliott Hughescdf53122011-08-19 15:46:09 -0700624class JNI {
625 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700626
Elliott Hughescdf53122011-08-19 15:46:09 -0700627 static jint GetVersion(JNIEnv* env) {
628 ScopedJniThreadState ts(env);
629 return JNI_VERSION_1_6;
630 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700631
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
633 ScopedJniThreadState ts(env);
634 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700635 return NULL;
636 }
637
Elliott Hughescdf53122011-08-19 15:46:09 -0700638 static jclass FindClass(JNIEnv* env, const char* name) {
639 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700640 Runtime* runtime = Runtime::Current();
641 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700642 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700643 Class* c = NULL;
644 if (runtime->IsStarted()) {
645 // TODO: need to get the appropriate ClassLoader.
646 const ClassLoader* cl = ts.Self()->GetClassLoaderOverride();
647 c = class_linker->FindClass(descriptor, cl);
648 } else {
649 c = class_linker->FindSystemClass(descriptor);
650 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700651 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700652 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700653
Elliott Hughescdf53122011-08-19 15:46:09 -0700654 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
655 ScopedJniThreadState ts(env);
656 Method* method = Decode<Method*>(ts, java_method);
657 return reinterpret_cast<jmethodID>(AddWeakGlobalReference(ts, method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700658 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700659
Elliott Hughescdf53122011-08-19 15:46:09 -0700660 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
661 ScopedJniThreadState ts(env);
662 Field* field = Decode<Field*>(ts, java_field);
663 return reinterpret_cast<jfieldID>(AddWeakGlobalReference(ts, field));
664 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700665
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
667 ScopedJniThreadState ts(env);
668 Method* method = DecodeMethod(ts, mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700669 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700671
Elliott Hughescdf53122011-08-19 15:46:09 -0700672 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
673 ScopedJniThreadState ts(env);
674 Field* field = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700675 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700677
Elliott Hughes37f7a402011-08-22 18:56:01 -0700678 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
679 ScopedJniThreadState ts(env);
680 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700681 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700682 }
683
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700684 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700685 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700686 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700687 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700688 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700689
Elliott Hughes37f7a402011-08-22 18:56:01 -0700690 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 Class* c1 = Decode<Class*>(ts, java_class1);
693 Class* c2 = Decode<Class*>(ts, java_class2);
694 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700696
Elliott Hughes37f7a402011-08-22 18:56:01 -0700697 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass clazz) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700698 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -0700699 CHECK_NE(static_cast<jclass>(NULL), clazz); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700700 if (jobj == NULL) {
701 // NB. JNI is different from regular Java instanceof in this respect
702 return JNI_TRUE;
703 } else {
704 Object* obj = Decode<Object*>(ts, jobj);
705 Class* klass = Decode<Class*>(ts, clazz);
706 return Object::InstanceOf(obj, klass) ? JNI_TRUE : JNI_FALSE;
707 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700709
Elliott Hughes37f7a402011-08-22 18:56:01 -0700710 static jint Throw(JNIEnv* env, jthrowable java_exception) {
711 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700712 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700713 if (exception == NULL) {
714 return JNI_ERR;
715 }
716 ts.Self()->SetException(exception);
717 return JNI_OK;
718 }
719
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700720 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700721 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700722 // TODO: check for a pending exception to decide what constructor to call.
723 jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
724 if (mid == NULL) {
725 return JNI_ERR;
726 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700727 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
728 if (s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700729 return JNI_ERR;
730 }
731
732 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700733 args[0].l = s.get();
734 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
735 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700736 return JNI_ERR;
737 }
738
Elliott Hughes72025e52011-08-23 17:50:30 -0700739 LOG(INFO) << "Throwing " << PrettyType(Decode<Throwable*>(ts, exception.get()))
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700740 << ": " << msg;
Elliott Hughes72025e52011-08-23 17:50:30 -0700741 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700742
Elliott Hughes37f7a402011-08-22 18:56:01 -0700743 return JNI_OK;
744 }
745
746 static jboolean ExceptionCheck(JNIEnv* env) {
747 ScopedJniThreadState ts(env);
748 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
749 }
750
751 static void ExceptionClear(JNIEnv* env) {
752 ScopedJniThreadState ts(env);
753 ts.Self()->ClearException();
754 }
755
756 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700757 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700758
759 Thread* self = ts.Self();
760 Throwable* original_exception = self->GetException();
761 self->ClearException();
762
Elliott Hughesbf86d042011-08-31 17:53:14 -0700763 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700764 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
765 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
766 if (mid == NULL) {
767 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
768 << PrettyType(original_exception);
769 } else {
770 env->CallVoidMethod(exception.get(), mid);
771 if (self->IsExceptionPending()) {
772 LOG(WARNING) << "JNI WARNING: " << PrettyType(self->GetException())
773 << " thrown while calling printStackTrace";
774 self->ClearException();
775 }
776 }
777
778 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700780
Elliott Hughescdf53122011-08-19 15:46:09 -0700781 static jthrowable ExceptionOccurred(JNIEnv* env) {
782 ScopedJniThreadState ts(env);
783 Object* exception = ts.Self()->GetException();
784 if (exception == NULL) {
785 return NULL;
786 } else {
787 // TODO: if adding a local reference failing causes the VM to abort
788 // then the following check will never occur.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700789 jthrowable localException = AddLocalReference<jthrowable>(env, exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700790 if (localException == NULL) {
791 // We were unable to add a new local reference, and threw a new
792 // exception. We can't return "exception", because it's not a
793 // local reference. So we have to return NULL, indicating that
794 // there was no exception, even though it's pretty much raining
795 // exceptions in here.
796 LOG(WARNING) << "JNI WARNING: addLocal/exception combo";
797 }
798 return localException;
799 }
800 }
801
Elliott Hughescdf53122011-08-19 15:46:09 -0700802 static void FatalError(JNIEnv* env, const char* msg) {
803 ScopedJniThreadState ts(env);
804 LOG(FATAL) << "JNI FatalError called: " << msg;
805 }
806
807 static jint PushLocalFrame(JNIEnv* env, jint cap) {
808 ScopedJniThreadState ts(env);
809 UNIMPLEMENTED(WARNING) << "ignoring PushLocalFrame(" << cap << ")";
810 return JNI_OK;
811 }
812
813 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
814 ScopedJniThreadState ts(env);
815 UNIMPLEMENTED(WARNING) << "ignoring PopLocalFrame " << res;
816 return res;
817 }
818
Elliott Hughes72025e52011-08-23 17:50:30 -0700819 static jint EnsureLocalCapacity(JNIEnv* env, jint cap) {
820 ScopedJniThreadState ts(env);
821 UNIMPLEMENTED(WARNING) << "ignoring EnsureLocalCapacity(" << cap << ")";
822 return 0;
823 }
824
Elliott Hughescdf53122011-08-19 15:46:09 -0700825 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
826 ScopedJniThreadState ts(env);
827 if (obj == NULL) {
828 return NULL;
829 }
830
Elliott Hughes75770752011-08-24 17:52:38 -0700831 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 IndirectReferenceTable& globals = vm->globals;
833 MutexLock mu(vm->globals_lock);
834 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
835 return reinterpret_cast<jobject>(ref);
836 }
837
838 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
839 ScopedJniThreadState ts(env);
840 if (obj == NULL) {
841 return;
842 }
843
Elliott Hughes75770752011-08-24 17:52:38 -0700844 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700845 IndirectReferenceTable& globals = vm->globals;
846 MutexLock mu(vm->globals_lock);
847
848 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
849 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
850 << "failed to find entry";
851 }
852 }
853
854 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
855 ScopedJniThreadState ts(env);
856 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
857 }
858
859 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
860 ScopedJniThreadState ts(env);
861 if (obj == NULL) {
862 return;
863 }
864
Elliott Hughes75770752011-08-24 17:52:38 -0700865 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 IndirectReferenceTable& weak_globals = vm->weak_globals;
867 MutexLock mu(vm->weak_globals_lock);
868
869 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
870 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
871 << "failed to find entry";
872 }
873 }
874
875 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
876 ScopedJniThreadState ts(env);
877 if (obj == NULL) {
878 return NULL;
879 }
880
881 IndirectReferenceTable& locals = ts.Env()->locals;
882
883 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
884 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
885 return reinterpret_cast<jobject>(ref);
886 }
887
888 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
889 ScopedJniThreadState ts(env);
890 if (obj == NULL) {
891 return;
892 }
893
894 IndirectReferenceTable& locals = ts.Env()->locals;
895
896 uint32_t cookie = IRT_FIRST_SEGMENT; // TODO
897 if (!locals.Remove(cookie, obj)) {
898 // Attempting to delete a local reference that is not in the
899 // topmost local reference frame is a no-op. DeleteLocalRef returns
900 // void and doesn't throw any exceptions, but we should probably
901 // complain about it so the user will notice that things aren't
902 // going quite the way they expect.
903 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
904 << "failed to find entry";
905 }
906 }
907
908 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
909 ScopedJniThreadState ts(env);
910 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
911 ? JNI_TRUE : JNI_FALSE;
912 }
913
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700914 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700916 Class* c = Decode<Class*>(ts, java_class);
917 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
918 return NULL;
919 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700920 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 }
922
Elliott Hughes72025e52011-08-23 17:50:30 -0700923 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 ScopedJniThreadState ts(env);
925 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 va_start(args, mid);
927 jobject result = NewObjectV(env, clazz, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 va_end(args);
929 return result;
930 }
931
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700934 Class* c = Decode<Class*>(ts, java_class);
935 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
936 return NULL;
937 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700938 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700939 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 return local_result;
942 }
943
Elliott Hughes72025e52011-08-23 17:50:30 -0700944 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700946 Class* c = Decode<Class*>(ts, java_class);
947 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
948 return NULL;
949 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700950 Object* result = c->AllocObject();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700951 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 return local_result;
954 }
955
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
957 ScopedJniThreadState ts(env);
958 return FindMethodID(ts, c, name, sig, false);
959 }
960
961 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
962 ScopedJniThreadState ts(env);
963 return FindMethodID(ts, c, name, sig, true);
964 }
965
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700968 va_list ap;
969 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700970 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700972 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700973 }
974
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700977 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700978 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700982 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700983 JValue result = InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700984 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 }
986
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 va_list ap;
990 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700991 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 va_end(ap);
993 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700998 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001003 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 }
1005
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 va_list ap;
1009 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001010 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001011 va_end(ap);
1012 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001017 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 }
1019
Elliott Hughes72025e52011-08-23 17:50:30 -07001020 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001022 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 }
1024
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001027 va_list ap;
1028 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001029 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_end(ap);
1031 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001032 }
1033
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001036 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001041 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 }
1043
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 va_list ap;
1047 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001048 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_end(ap);
1050 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001055 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001060 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 va_list ap;
1066 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001067 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 va_end(ap);
1069 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001074 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001079 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 va_list ap;
1085 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001086 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 va_end(ap);
1088 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 }
1090
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001093 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001098 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 va_list ap;
1104 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001105 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 va_end(ap);
1107 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001112 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001117 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 va_list ap;
1123 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001124 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 va_end(ap);
1126 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001131 return InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001136 return InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 va_list ap;
1142 va_start(ap, mid);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001143 JValue result = InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001149 InvokeVirtualOrInterfaceWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 ScopedJniThreadState ts(env);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001154 InvokeVirtualOrInterfaceWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
1157 static jobject CallNonvirtualObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 ScopedJniThreadState ts(env);
1160 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 va_start(ap, mid);
1162 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001163 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 va_end(ap);
1165 return local_result;
1166 }
1167
1168 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 JValue result = InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001172 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 }
1174
1175 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 JValue result = InvokeWithJValues(ts, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001179 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
1182 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 ScopedJniThreadState ts(env);
1185 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 va_start(ap, mid);
1187 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 va_end(ap);
1189 return result.z;
1190 }
1191
1192 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 return InvokeWithVarArgs(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
1198 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 return InvokeWithJValues(ts, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
1204 static jbyte CallNonvirtualByteMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 ScopedJniThreadState ts(env);
1207 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 va_start(ap, mid);
1209 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 va_end(ap);
1211 return result.b;
1212 }
1213
1214 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 return InvokeWithVarArgs(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
1220 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 return InvokeWithJValues(ts, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 }
1225
1226 static jchar CallNonvirtualCharMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 ScopedJniThreadState ts(env);
1229 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 va_start(ap, mid);
1231 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 va_end(ap);
1233 return result.c;
1234 }
1235
1236 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001239 return InvokeWithVarArgs(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
1242 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 return InvokeWithJValues(ts, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
1248 static jshort CallNonvirtualShortMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 ScopedJniThreadState ts(env);
1251 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 va_start(ap, mid);
1253 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 va_end(ap);
1255 return result.s;
1256 }
1257
1258 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001261 return InvokeWithVarArgs(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 return InvokeWithJValues(ts, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
1270 static jint CallNonvirtualIntMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 ScopedJniThreadState ts(env);
1273 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 va_start(ap, mid);
1275 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 va_end(ap);
1277 return result.i;
1278 }
1279
1280 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001283 return InvokeWithVarArgs(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
1286 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001289 return InvokeWithJValues(ts, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
1292 static jlong CallNonvirtualLongMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001293 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 ScopedJniThreadState ts(env);
1295 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 va_start(ap, mid);
1297 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_end(ap);
1299 return result.j;
1300 }
1301
1302 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001303 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001305 return InvokeWithVarArgs(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
1308 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001309 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 return InvokeWithJValues(ts, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
1314 static jfloat CallNonvirtualFloatMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 ScopedJniThreadState ts(env);
1317 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 va_start(ap, mid);
1319 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 va_end(ap);
1321 return result.f;
1322 }
1323
1324 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001327 return InvokeWithVarArgs(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
1330 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001331 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001333 return InvokeWithJValues(ts, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
1336 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 ScopedJniThreadState ts(env);
1339 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001340 va_start(ap, mid);
1341 JValue result = InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 va_end(ap);
1343 return result.d;
1344 }
1345
1346 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001349 return InvokeWithVarArgs(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
1352 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001355 return InvokeWithJValues(ts, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
1358 static void CallNonvirtualVoidMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 jobject obj, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 ScopedJniThreadState ts(env);
1361 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001362 va_start(ap, mid);
1363 InvokeWithVarArgs(ts, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 va_end(ap);
1365 }
1366
1367 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001368 jobject obj, jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001370 InvokeWithVarArgs(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
1373 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001374 jobject obj, jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001376 InvokeWithJValues(ts, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
1379 static jfieldID GetFieldID(JNIEnv* env,
1380 jclass c, const char* name, const char* sig) {
1381 ScopedJniThreadState ts(env);
1382 return FindFieldID(ts, c, name, sig, false);
1383 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001384
1385
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 static jfieldID GetStaticFieldID(JNIEnv* env,
1387 jclass c, const char* name, const char* sig) {
1388 ScopedJniThreadState ts(env);
1389 return FindFieldID(ts, c, name, sig, true);
1390 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001391
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 Object* o = Decode<Object*>(ts, obj);
1395 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001396 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001398
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001399 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001401 Field* f = DecodeField(ts, fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001402 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 }
1404
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001405 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407 Object* o = Decode<Object*>(ts, java_object);
1408 Object* v = Decode<Object*>(ts, java_value);
1409 Field* f = DecodeField(ts, fid);
1410 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 }
1412
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001413 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001415 Object* v = Decode<Object*>(ts, java_value);
1416 Field* f = DecodeField(ts, fid);
1417 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001420#define GET_PRIMITIVE_FIELD(fn, instance) \
1421 ScopedJniThreadState ts(env); \
1422 Object* o = Decode<Object*>(ts, instance); \
1423 Field* f = DecodeField(ts, fid); \
1424 return f->fn(o)
1425
1426#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1427 ScopedJniThreadState ts(env); \
1428 Object* o = Decode<Object*>(ts, instance); \
1429 Field* f = DecodeField(ts, fid); \
1430 f->fn(o, value)
1431
1432 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1433 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1437 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1441 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001444 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1445 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1449 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1453 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1457 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1461 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 static jboolean GetStaticBooleanField(JNIEnv* env, jclass clazz, jfieldID fid) {
1465 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001468 static jbyte GetStaticByteField(JNIEnv* env, jclass clazz, jfieldID fid) {
1469 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 }
1471
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001472 static jchar GetStaticCharField(JNIEnv* env, jclass clazz, jfieldID fid) {
1473 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 static jshort GetStaticShortField(JNIEnv* env, jclass clazz, jfieldID fid) {
1477 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 static jint GetStaticIntField(JNIEnv* env, jclass clazz, jfieldID fid) {
1481 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 static jlong GetStaticLongField(JNIEnv* env, jclass clazz, jfieldID fid) {
1485 GET_PRIMITIVE_FIELD(GetLong, NULL);
1486 }
1487
1488 static jfloat GetStaticFloatField(JNIEnv* env, jclass clazz, jfieldID fid) {
1489 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1490 }
1491
1492 static jdouble GetStaticDoubleField(JNIEnv* env, jclass clazz, jfieldID fid) {
1493 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1494 }
1495
1496 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1497 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1498 }
1499
1500 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1501 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1502 }
1503
1504 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1505 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1506 }
1507
1508 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1509 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1510 }
1511
1512 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1513 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1514 }
1515
1516 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1517 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1518 }
1519
1520 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1521 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1522 }
1523
1524 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1525 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1526 }
1527
1528 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1529 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1530 }
1531
1532 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1533 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1534 }
1535
1536 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1537 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1538 }
1539
1540 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1541 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1542 }
1543
1544 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1545 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1546 }
1547
1548 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1549 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1550 }
1551
1552 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1553 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1554 }
1555
1556 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1557 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 }
1559
1560 static jobject CallStaticObjectMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001561 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 ScopedJniThreadState ts(env);
1563 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001564 va_start(ap, mid);
1565 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001566 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 va_end(ap);
1568 return local_result;
1569 }
1570
1571 static jobject CallStaticObjectMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001572 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001574 JValue result = InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001575 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001576 }
1577
1578 static jobject CallStaticObjectMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001579 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001581 JValue result = InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001582 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
1585 static jboolean CallStaticBooleanMethod(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001586 jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 ScopedJniThreadState ts(env);
1588 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001589 va_start(ap, mid);
1590 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 va_end(ap);
1592 return result.z;
1593 }
1594
1595 static jboolean CallStaticBooleanMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001596 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001598 return InvokeWithVarArgs(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
1601 static jboolean CallStaticBooleanMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001602 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001604 return InvokeWithJValues(ts, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes72025e52011-08-23 17:50:30 -07001607 static jbyte CallStaticByteMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 ScopedJniThreadState ts(env);
1609 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001610 va_start(ap, mid);
1611 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 va_end(ap);
1613 return result.b;
1614 }
1615
1616 static jbyte CallStaticByteMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001617 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001619 return InvokeWithVarArgs(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
1622 static jbyte CallStaticByteMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001623 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001625 return InvokeWithJValues(ts, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 }
1627
Elliott Hughes72025e52011-08-23 17:50:30 -07001628 static jchar CallStaticCharMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 ScopedJniThreadState ts(env);
1630 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001631 va_start(ap, mid);
1632 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 va_end(ap);
1634 return result.c;
1635 }
1636
1637 static jchar CallStaticCharMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001638 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001640 return InvokeWithVarArgs(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 }
1642
1643 static jchar CallStaticCharMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 return InvokeWithJValues(ts, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 }
1648
Elliott Hughes72025e52011-08-23 17:50:30 -07001649 static jshort CallStaticShortMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 ScopedJniThreadState ts(env);
1651 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001652 va_start(ap, mid);
1653 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 va_end(ap);
1655 return result.s;
1656 }
1657
1658 static jshort CallStaticShortMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001659 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001661 return InvokeWithVarArgs(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
1664 static jshort CallStaticShortMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 return InvokeWithJValues(ts, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 }
1669
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 static jint CallStaticIntMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 ScopedJniThreadState ts(env);
1672 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001673 va_start(ap, mid);
1674 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 va_end(ap);
1676 return result.i;
1677 }
1678
1679 static jint CallStaticIntMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001680 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001682 return InvokeWithVarArgs(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 }
1684
1685 static jint CallStaticIntMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001688 return InvokeWithJValues(ts, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 }
1690
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 static jlong CallStaticLongMethod(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 ScopedJniThreadState ts(env);
1693 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001694 va_start(ap, mid);
1695 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 va_end(ap);
1697 return result.j;
1698 }
1699
1700 static jlong CallStaticLongMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001701 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 return InvokeWithVarArgs(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 }
1705
1706 static jlong CallStaticLongMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001707 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001709 return InvokeWithJValues(ts, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 }
1711
Elliott Hughes72025e52011-08-23 17:50:30 -07001712 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 ScopedJniThreadState ts(env);
1714 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001715 va_start(ap, mid);
1716 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 va_end(ap);
1718 return result.f;
1719 }
1720
1721 static jfloat CallStaticFloatMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001724 return InvokeWithVarArgs(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
1727 static jfloat CallStaticFloatMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001728 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001730 return InvokeWithJValues(ts, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes72025e52011-08-23 17:50:30 -07001733 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 ScopedJniThreadState ts(env);
1735 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001736 va_start(ap, mid);
1737 JValue result = InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 va_end(ap);
1739 return result.d;
1740 }
1741
1742 static jdouble CallStaticDoubleMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 jclass clazz, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001745 return InvokeWithVarArgs(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 }
1747
1748 static jdouble CallStaticDoubleMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001749 jclass clazz, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001751 return InvokeWithJValues(ts, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 }
1753
Elliott Hughes72025e52011-08-23 17:50:30 -07001754 static void CallStaticVoidMethod(JNIEnv* env, jclass cls, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 ScopedJniThreadState ts(env);
1756 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 va_start(ap, mid);
1758 InvokeWithVarArgs(ts, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 va_end(ap);
1760 }
1761
1762 static void CallStaticVoidMethodV(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001763 jclass cls, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001765 InvokeWithVarArgs(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 }
1767
1768 static void CallStaticVoidMethodA(JNIEnv* env,
Elliott Hughes72025e52011-08-23 17:50:30 -07001769 jclass cls, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001771 InvokeWithJValues(ts, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 }
1773
Elliott Hughes814e4032011-08-23 12:07:56 -07001774 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001776 if (chars == NULL && char_count == 0) {
1777 return NULL;
1778 }
1779 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001780 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
1783 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1784 ScopedJniThreadState ts(env);
1785 if (utf == NULL) {
1786 return NULL;
1787 }
1788 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001789 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 }
1791
Elliott Hughes814e4032011-08-23 12:07:56 -07001792 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1793 ScopedJniThreadState ts(env);
1794 return Decode<String*>(ts, java_string)->GetLength();
1795 }
1796
1797 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1798 ScopedJniThreadState ts(env);
1799 return Decode<String*>(ts, java_string)->GetUtfLength();
1800 }
1801
Elliott Hughesb465ab02011-08-24 11:21:21 -07001802 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001804 String* s = Decode<String*>(ts, java_string);
1805 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1806 ThrowSIOOBE(ts, start, length, s->GetLength());
1807 } else {
1808 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1809 memcpy(buf, chars + start, length * sizeof(jchar));
1810 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001811 }
1812
Elliott Hughesb465ab02011-08-24 11:21:21 -07001813 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001814 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001815 String* s = Decode<String*>(ts, java_string);
1816 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1817 ThrowSIOOBE(ts, start, length, s->GetLength());
1818 } else {
1819 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1820 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1821 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001822 }
1823
Elliott Hughes75770752011-08-24 17:52:38 -07001824 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001825 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001826 String* s = Decode<String*>(ts, java_string);
1827 const CharArray* chars = s->GetCharArray();
1828 PinPrimitiveArray(ts, chars);
1829 if (is_copy != NULL) {
1830 *is_copy = JNI_FALSE;
1831 }
1832 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001833 }
1834
Elliott Hughes75770752011-08-24 17:52:38 -07001835 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001836 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001837 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 }
1839
Elliott Hughes75770752011-08-24 17:52:38 -07001840 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001842 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 }
1844
Elliott Hughes75770752011-08-24 17:52:38 -07001845 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001847 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
Elliott Hughes75770752011-08-24 17:52:38 -07001850 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001851 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001852 if (java_string == NULL) {
1853 return NULL;
1854 }
1855 if (is_copy != NULL) {
1856 *is_copy = JNI_TRUE;
1857 }
1858 String* s = Decode<String*>(ts, java_string);
1859 size_t byte_count = s->GetUtfLength();
1860 char* bytes = new char[byte_count + 1];
1861 if (bytes == NULL) {
Elliott Hughes79082e32011-08-25 12:07:32 -07001862 ts.Self()->ThrowOutOfMemoryError();
Elliott Hughes75770752011-08-24 17:52:38 -07001863 return NULL;
1864 }
1865 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1866 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1867 bytes[byte_count] = '\0';
1868 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001869 }
1870
Elliott Hughes75770752011-08-24 17:52:38 -07001871 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001872 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001873 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001874 }
1875
Elliott Hughesbd935992011-08-22 11:59:34 -07001876 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001878 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001879 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001880 Array* array = obj->AsArray();
1881 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 }
1883
Elliott Hughes814e4032011-08-23 12:07:56 -07001884 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001886 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001887 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 }
1889
1890 static void SetObjectArrayElement(JNIEnv* env,
1891 jobjectArray java_array, jsize index, jobject java_value) {
1892 ScopedJniThreadState ts(env);
1893 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1894 Object* value = Decode<Object*>(ts, java_value);
1895 array->Set(index, value);
1896 }
1897
1898 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1899 ScopedJniThreadState ts(env);
1900 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1901 }
1902
1903 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1904 ScopedJniThreadState ts(env);
1905 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1906 }
1907
1908 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1909 ScopedJniThreadState ts(env);
1910 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1911 }
1912
1913 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1914 ScopedJniThreadState ts(env);
1915 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1916 }
1917
1918 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1919 ScopedJniThreadState ts(env);
1920 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1921 }
1922
1923 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1924 ScopedJniThreadState ts(env);
1925 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1926 }
1927
1928 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1929 ScopedJniThreadState ts(env);
1930 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1931 }
1932
1933 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1934 ScopedJniThreadState ts(env);
1935 CHECK_GE(length, 0); // TODO: ReportJniError
1936
1937 // Compute the array class corresponding to the given element class.
1938 Class* element_class = Decode<Class*>(ts, element_jclass);
1939 std::string descriptor;
1940 descriptor += "[";
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001941 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
Elliott Hughescdf53122011-08-19 15:46:09 -07001942
1943 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001944 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1945 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 return NULL;
1947 }
1948
Elliott Hughes75770752011-08-24 17:52:38 -07001949 // Allocate and initialize if necessary.
1950 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001952 if (initial_element != NULL) {
1953 Object* initial_object = Decode<Object*>(ts, initial_element);
1954 for (jsize i = 0; i < length; ++i) {
1955 result->Set(i, initial_object);
1956 }
1957 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001958 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 }
1960
1961 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1962 ScopedJniThreadState ts(env);
1963 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1964 }
1965
Elliott Hughes75770752011-08-24 17:52:38 -07001966 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001967 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001968 return GetPrimitiveArray<jarray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001969 }
1970
Elliott Hughes75770752011-08-24 17:52:38 -07001971 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* data, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001972 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001973 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001974 }
1975
Elliott Hughes75770752011-08-24 17:52:38 -07001976 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001978 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 }
1980
Elliott Hughes75770752011-08-24 17:52:38 -07001981 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001983 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Elliott Hughes75770752011-08-24 17:52:38 -07001986 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001988 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes75770752011-08-24 17:52:38 -07001991 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001993 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes75770752011-08-24 17:52:38 -07001996 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001998 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Elliott Hughes75770752011-08-24 17:52:38 -07002001 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002003 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Elliott Hughes75770752011-08-24 17:52:38 -07002006 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002008 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Elliott Hughes75770752011-08-24 17:52:38 -07002011 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002013 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes75770752011-08-24 17:52:38 -07002016 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002018 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes75770752011-08-24 17:52:38 -07002021 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002023 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes75770752011-08-24 17:52:38 -07002026 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002028 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes75770752011-08-24 17:52:38 -07002031 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002033 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes75770752011-08-24 17:52:38 -07002036 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002038 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes75770752011-08-24 17:52:38 -07002041 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002043 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Elliott Hughes75770752011-08-24 17:52:38 -07002046 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002048 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Elliott Hughes75770752011-08-24 17:52:38 -07002051 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* data, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002053 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Elliott Hughes814e4032011-08-23 12:07:56 -07002066 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002068 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 }
2070
Elliott Hughes814e4032011-08-23 12:07:56 -07002071 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
Elliott Hughes814e4032011-08-23 12:07:56 -07002076 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002078 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Elliott Hughes814e4032011-08-23 12:07:56 -07002081 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002083 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes814e4032011-08-23 12:07:56 -07002086 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002088 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002093 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002098 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002108 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002113 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Elliott Hughes814e4032011-08-23 12:07:56 -07002126 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
Elliott Hughes814e4032011-08-23 12:07:56 -07002131 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
2135
Elliott Hughes5174fe62011-08-23 15:12:35 -07002136 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002138 Class* c = Decode<Class*>(ts, java_class);
2139
Elliott Hughes5174fe62011-08-23 15:12:35 -07002140 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 const char* name = methods[i].name;
2142 const char* sig = methods[i].signature;
2143
2144 if (*sig == '!') {
2145 // TODO: fast jni. it's too noisy to log all these.
2146 ++sig;
2147 }
2148
Elliott Hughes5174fe62011-08-23 15:12:35 -07002149 Method* m = c->FindDirectMethod(name, sig);
2150 if (m == NULL) {
2151 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002153 if (m == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2157 "no method \"%s.%s%s\"",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002158 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002160 } else if (!m->IsNative()) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 Thread* self = Thread::Current();
Elliott Hughes5174fe62011-08-23 15:12:35 -07002162 std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
2164 "method \"%s.%s%s\" is not native",
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002165 class_descriptor.c_str(), name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 return JNI_ERR;
2167 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002168
Elliott Hughes75770752011-08-24 17:52:38 -07002169 if (ts.Vm()->verbose_jni) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002170 LOG(INFO) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002171 }
2172
2173 m->RegisterNative(methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
2175 return JNI_OK;
2176 }
2177
Elliott Hughes5174fe62011-08-23 15:12:35 -07002178 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002180 Class* c = Decode<Class*>(ts, java_class);
2181
Elliott Hughes75770752011-08-24 17:52:38 -07002182 if (ts.Vm()->verbose_jni) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002183 LOG(INFO) << "[Unregistering JNI native methods for "
2184 << PrettyDescriptor(c->GetDescriptor()) << "]";
2185 }
2186
2187 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2188 Method* m = c->GetDirectMethod(i);
2189 if (m->IsNative()) {
2190 m->UnregisterNative();
2191 }
2192 }
2193 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2194 Method* m = c->GetVirtualMethod(i);
2195 if (m->IsNative()) {
2196 m->UnregisterNative();
2197 }
2198 }
2199
2200 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
Elliott Hughes72025e52011-08-23 17:50:30 -07002203 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07002205 Decode<Object*>(ts, java_object)->MonitorEnter();
2206 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
Elliott Hughes72025e52011-08-23 17:50:30 -07002209 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 ScopedJniThreadState ts(env);
Elliott Hughes02b48d12011-09-07 17:15:51 -07002211 Decode<Object*>(ts, java_object)->MonitorExit();
Elliott Hughes72025e52011-08-23 17:50:30 -07002212 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 }
2214
2215 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2216 ScopedJniThreadState ts(env);
2217 Runtime* runtime = Runtime::Current();
2218 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002219 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 } else {
2221 *vm = NULL;
2222 }
2223 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2224 }
2225
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2227 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002228
2229 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002230 CHECK(address != NULL); // TODO: ReportJniError
2231 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002232
2233 jclass buffer_class = GetDirectByteBufferClass(env);
2234 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2235 if (mid == NULL) {
2236 return NULL;
2237 }
2238
2239 // At the moment, the Java side is limited to 32 bits.
2240 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2241 CHECK_LE(capacity, 0xffffffff);
2242 jint address_arg = reinterpret_cast<jint>(address);
2243 jint capacity_arg = static_cast<jint>(capacity);
2244
2245 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2246 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 }
2248
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002251 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2252 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 }
2254
Elliott Hughesb465ab02011-08-24 11:21:21 -07002255 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002257 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2258 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002259 }
2260
Elliott Hughesb465ab02011-08-24 11:21:21 -07002261 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002263
Elliott Hughes75770752011-08-24 17:52:38 -07002264 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002265
2266 // Do we definitely know what kind of reference this is?
2267 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2268 IndirectRefKind kind = GetIndirectRefKind(ref);
2269 switch (kind) {
2270 case kLocal:
2271 return JNILocalRefType;
2272 case kGlobal:
2273 return JNIGlobalRefType;
2274 case kWeakGlobal:
2275 return JNIWeakGlobalRefType;
2276 case kSirtOrInvalid:
2277 // Is it in a stack IRT?
2278 if (ts.Self()->SirtContains(java_object)) {
2279 return JNILocalRefType;
2280 }
2281
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002282 if (!ts.Env()->work_around_app_jni_bugs) {
2283 return JNIInvalidRefType;
2284 }
2285
Elliott Hughesb465ab02011-08-24 11:21:21 -07002286 // If we're handing out direct pointers, check whether it's a direct pointer
2287 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002288 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002289 if (ts.Env()->locals.Contains(java_object)) {
2290 return JNILocalRefType;
2291 }
2292 }
2293
2294 return JNIInvalidRefType;
2295 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 }
2297};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002298
Elliott Hughesa2501992011-08-26 19:39:54 -07002299const JNINativeInterface gNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002300 NULL, // reserved0.
2301 NULL, // reserved1.
2302 NULL, // reserved2.
2303 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002304 JNI::GetVersion,
2305 JNI::DefineClass,
2306 JNI::FindClass,
2307 JNI::FromReflectedMethod,
2308 JNI::FromReflectedField,
2309 JNI::ToReflectedMethod,
2310 JNI::GetSuperclass,
2311 JNI::IsAssignableFrom,
2312 JNI::ToReflectedField,
2313 JNI::Throw,
2314 JNI::ThrowNew,
2315 JNI::ExceptionOccurred,
2316 JNI::ExceptionDescribe,
2317 JNI::ExceptionClear,
2318 JNI::FatalError,
2319 JNI::PushLocalFrame,
2320 JNI::PopLocalFrame,
2321 JNI::NewGlobalRef,
2322 JNI::DeleteGlobalRef,
2323 JNI::DeleteLocalRef,
2324 JNI::IsSameObject,
2325 JNI::NewLocalRef,
2326 JNI::EnsureLocalCapacity,
2327 JNI::AllocObject,
2328 JNI::NewObject,
2329 JNI::NewObjectV,
2330 JNI::NewObjectA,
2331 JNI::GetObjectClass,
2332 JNI::IsInstanceOf,
2333 JNI::GetMethodID,
2334 JNI::CallObjectMethod,
2335 JNI::CallObjectMethodV,
2336 JNI::CallObjectMethodA,
2337 JNI::CallBooleanMethod,
2338 JNI::CallBooleanMethodV,
2339 JNI::CallBooleanMethodA,
2340 JNI::CallByteMethod,
2341 JNI::CallByteMethodV,
2342 JNI::CallByteMethodA,
2343 JNI::CallCharMethod,
2344 JNI::CallCharMethodV,
2345 JNI::CallCharMethodA,
2346 JNI::CallShortMethod,
2347 JNI::CallShortMethodV,
2348 JNI::CallShortMethodA,
2349 JNI::CallIntMethod,
2350 JNI::CallIntMethodV,
2351 JNI::CallIntMethodA,
2352 JNI::CallLongMethod,
2353 JNI::CallLongMethodV,
2354 JNI::CallLongMethodA,
2355 JNI::CallFloatMethod,
2356 JNI::CallFloatMethodV,
2357 JNI::CallFloatMethodA,
2358 JNI::CallDoubleMethod,
2359 JNI::CallDoubleMethodV,
2360 JNI::CallDoubleMethodA,
2361 JNI::CallVoidMethod,
2362 JNI::CallVoidMethodV,
2363 JNI::CallVoidMethodA,
2364 JNI::CallNonvirtualObjectMethod,
2365 JNI::CallNonvirtualObjectMethodV,
2366 JNI::CallNonvirtualObjectMethodA,
2367 JNI::CallNonvirtualBooleanMethod,
2368 JNI::CallNonvirtualBooleanMethodV,
2369 JNI::CallNonvirtualBooleanMethodA,
2370 JNI::CallNonvirtualByteMethod,
2371 JNI::CallNonvirtualByteMethodV,
2372 JNI::CallNonvirtualByteMethodA,
2373 JNI::CallNonvirtualCharMethod,
2374 JNI::CallNonvirtualCharMethodV,
2375 JNI::CallNonvirtualCharMethodA,
2376 JNI::CallNonvirtualShortMethod,
2377 JNI::CallNonvirtualShortMethodV,
2378 JNI::CallNonvirtualShortMethodA,
2379 JNI::CallNonvirtualIntMethod,
2380 JNI::CallNonvirtualIntMethodV,
2381 JNI::CallNonvirtualIntMethodA,
2382 JNI::CallNonvirtualLongMethod,
2383 JNI::CallNonvirtualLongMethodV,
2384 JNI::CallNonvirtualLongMethodA,
2385 JNI::CallNonvirtualFloatMethod,
2386 JNI::CallNonvirtualFloatMethodV,
2387 JNI::CallNonvirtualFloatMethodA,
2388 JNI::CallNonvirtualDoubleMethod,
2389 JNI::CallNonvirtualDoubleMethodV,
2390 JNI::CallNonvirtualDoubleMethodA,
2391 JNI::CallNonvirtualVoidMethod,
2392 JNI::CallNonvirtualVoidMethodV,
2393 JNI::CallNonvirtualVoidMethodA,
2394 JNI::GetFieldID,
2395 JNI::GetObjectField,
2396 JNI::GetBooleanField,
2397 JNI::GetByteField,
2398 JNI::GetCharField,
2399 JNI::GetShortField,
2400 JNI::GetIntField,
2401 JNI::GetLongField,
2402 JNI::GetFloatField,
2403 JNI::GetDoubleField,
2404 JNI::SetObjectField,
2405 JNI::SetBooleanField,
2406 JNI::SetByteField,
2407 JNI::SetCharField,
2408 JNI::SetShortField,
2409 JNI::SetIntField,
2410 JNI::SetLongField,
2411 JNI::SetFloatField,
2412 JNI::SetDoubleField,
2413 JNI::GetStaticMethodID,
2414 JNI::CallStaticObjectMethod,
2415 JNI::CallStaticObjectMethodV,
2416 JNI::CallStaticObjectMethodA,
2417 JNI::CallStaticBooleanMethod,
2418 JNI::CallStaticBooleanMethodV,
2419 JNI::CallStaticBooleanMethodA,
2420 JNI::CallStaticByteMethod,
2421 JNI::CallStaticByteMethodV,
2422 JNI::CallStaticByteMethodA,
2423 JNI::CallStaticCharMethod,
2424 JNI::CallStaticCharMethodV,
2425 JNI::CallStaticCharMethodA,
2426 JNI::CallStaticShortMethod,
2427 JNI::CallStaticShortMethodV,
2428 JNI::CallStaticShortMethodA,
2429 JNI::CallStaticIntMethod,
2430 JNI::CallStaticIntMethodV,
2431 JNI::CallStaticIntMethodA,
2432 JNI::CallStaticLongMethod,
2433 JNI::CallStaticLongMethodV,
2434 JNI::CallStaticLongMethodA,
2435 JNI::CallStaticFloatMethod,
2436 JNI::CallStaticFloatMethodV,
2437 JNI::CallStaticFloatMethodA,
2438 JNI::CallStaticDoubleMethod,
2439 JNI::CallStaticDoubleMethodV,
2440 JNI::CallStaticDoubleMethodA,
2441 JNI::CallStaticVoidMethod,
2442 JNI::CallStaticVoidMethodV,
2443 JNI::CallStaticVoidMethodA,
2444 JNI::GetStaticFieldID,
2445 JNI::GetStaticObjectField,
2446 JNI::GetStaticBooleanField,
2447 JNI::GetStaticByteField,
2448 JNI::GetStaticCharField,
2449 JNI::GetStaticShortField,
2450 JNI::GetStaticIntField,
2451 JNI::GetStaticLongField,
2452 JNI::GetStaticFloatField,
2453 JNI::GetStaticDoubleField,
2454 JNI::SetStaticObjectField,
2455 JNI::SetStaticBooleanField,
2456 JNI::SetStaticByteField,
2457 JNI::SetStaticCharField,
2458 JNI::SetStaticShortField,
2459 JNI::SetStaticIntField,
2460 JNI::SetStaticLongField,
2461 JNI::SetStaticFloatField,
2462 JNI::SetStaticDoubleField,
2463 JNI::NewString,
2464 JNI::GetStringLength,
2465 JNI::GetStringChars,
2466 JNI::ReleaseStringChars,
2467 JNI::NewStringUTF,
2468 JNI::GetStringUTFLength,
2469 JNI::GetStringUTFChars,
2470 JNI::ReleaseStringUTFChars,
2471 JNI::GetArrayLength,
2472 JNI::NewObjectArray,
2473 JNI::GetObjectArrayElement,
2474 JNI::SetObjectArrayElement,
2475 JNI::NewBooleanArray,
2476 JNI::NewByteArray,
2477 JNI::NewCharArray,
2478 JNI::NewShortArray,
2479 JNI::NewIntArray,
2480 JNI::NewLongArray,
2481 JNI::NewFloatArray,
2482 JNI::NewDoubleArray,
2483 JNI::GetBooleanArrayElements,
2484 JNI::GetByteArrayElements,
2485 JNI::GetCharArrayElements,
2486 JNI::GetShortArrayElements,
2487 JNI::GetIntArrayElements,
2488 JNI::GetLongArrayElements,
2489 JNI::GetFloatArrayElements,
2490 JNI::GetDoubleArrayElements,
2491 JNI::ReleaseBooleanArrayElements,
2492 JNI::ReleaseByteArrayElements,
2493 JNI::ReleaseCharArrayElements,
2494 JNI::ReleaseShortArrayElements,
2495 JNI::ReleaseIntArrayElements,
2496 JNI::ReleaseLongArrayElements,
2497 JNI::ReleaseFloatArrayElements,
2498 JNI::ReleaseDoubleArrayElements,
2499 JNI::GetBooleanArrayRegion,
2500 JNI::GetByteArrayRegion,
2501 JNI::GetCharArrayRegion,
2502 JNI::GetShortArrayRegion,
2503 JNI::GetIntArrayRegion,
2504 JNI::GetLongArrayRegion,
2505 JNI::GetFloatArrayRegion,
2506 JNI::GetDoubleArrayRegion,
2507 JNI::SetBooleanArrayRegion,
2508 JNI::SetByteArrayRegion,
2509 JNI::SetCharArrayRegion,
2510 JNI::SetShortArrayRegion,
2511 JNI::SetIntArrayRegion,
2512 JNI::SetLongArrayRegion,
2513 JNI::SetFloatArrayRegion,
2514 JNI::SetDoubleArrayRegion,
2515 JNI::RegisterNatives,
2516 JNI::UnregisterNatives,
2517 JNI::MonitorEnter,
2518 JNI::MonitorExit,
2519 JNI::GetJavaVM,
2520 JNI::GetStringRegion,
2521 JNI::GetStringUTFRegion,
2522 JNI::GetPrimitiveArrayCritical,
2523 JNI::ReleasePrimitiveArrayCritical,
2524 JNI::GetStringCritical,
2525 JNI::ReleaseStringCritical,
2526 JNI::NewWeakGlobalRef,
2527 JNI::DeleteWeakGlobalRef,
2528 JNI::ExceptionCheck,
2529 JNI::NewDirectByteBuffer,
2530 JNI::GetDirectBufferAddress,
2531 JNI::GetDirectBufferCapacity,
2532 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002533};
2534
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002535static const size_t kMonitorsInitial = 32; // Arbitrary.
2536static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
2537
2538static const size_t kLocalsInitial = 64; // Arbitrary.
2539static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughesbbd76712011-08-17 10:25:24 -07002540
Elliott Hughes75770752011-08-24 17:52:38 -07002541JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002542 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002543 vm(vm),
2544 check_jni(vm->check_jni),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002545 work_around_app_jni_bugs(vm->work_around_app_jni_bugs),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002546 critical(false),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002547 monitors("monitors", kMonitorsInitial, kMonitorsMax),
2548 locals(kLocalsInitial, kLocalsMax, kLocal) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002549 functions = unchecked_functions = &gNativeInterface;
2550 if (check_jni) {
2551 functions = GetCheckJniNativeInterface();
2552 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002553}
2554
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002555JNIEnvExt::~JNIEnvExt() {
2556}
2557
Carl Shapiroea4dca82011-08-01 13:45:38 -07002558// JNI Invocation interface.
2559
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002560extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2561 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2562 if (args->version < JNI_VERSION_1_2) {
2563 return JNI_EVERSION;
2564 }
2565 Runtime::Options options;
2566 for (int i = 0; i < args->nOptions; ++i) {
2567 JavaVMOption* option = &args->options[i];
Carl Shapirofc322c72011-07-27 00:20:01 -07002568 options.push_back(std::make_pair(StringPiece(option->optionString),
2569 option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002570 }
2571 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002572 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002573 if (runtime == NULL) {
2574 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002575 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002576 runtime->Start();
2577 *p_env = Thread::Current()->GetJniEnv();
2578 *p_vm = runtime->GetJavaVM();
2579 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002580}
2581
Elliott Hughesf2682d52011-08-15 16:37:04 -07002582extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002583 Runtime* runtime = Runtime::Current();
2584 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002585 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002586 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002587 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002588 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002589 }
2590 return JNI_OK;
2591}
2592
2593// Historically unsupported.
2594extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
2595 return JNI_ERR;
2596}
2597
Elliott Hughescdf53122011-08-19 15:46:09 -07002598class JII {
2599 public:
2600 static jint DestroyJavaVM(JavaVM* vm) {
2601 if (vm == NULL) {
2602 return JNI_ERR;
2603 } else {
2604 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2605 delete raw_vm->runtime;
2606 return JNI_OK;
2607 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002608 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002609
Elliott Hughescdf53122011-08-19 15:46:09 -07002610 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002611 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002612 }
2613
2614 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002615 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002616 }
2617
2618 static jint DetachCurrentThread(JavaVM* vm) {
2619 if (vm == NULL) {
2620 return JNI_ERR;
2621 } else {
2622 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2623 Runtime* runtime = raw_vm->runtime;
2624 runtime->DetachCurrentThread();
2625 return JNI_OK;
2626 }
2627 }
2628
2629 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2630 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2631 return JNI_EVERSION;
2632 }
2633 if (vm == NULL || env == NULL) {
2634 return JNI_ERR;
2635 }
2636 Thread* thread = Thread::Current();
2637 if (thread == NULL) {
2638 *env = NULL;
2639 return JNI_EDETACHED;
2640 }
2641 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002642 return JNI_OK;
2643 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002644};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002645
Elliott Hughesa2501992011-08-26 19:39:54 -07002646const JNIInvokeInterface gInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002647 NULL, // reserved0
2648 NULL, // reserved1
2649 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002650 JII::DestroyJavaVM,
2651 JII::AttachCurrentThread,
2652 JII::DetachCurrentThread,
2653 JII::GetEnv,
2654 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002655};
2656
Elliott Hughesbbd76712011-08-17 10:25:24 -07002657static const size_t kPinTableInitialSize = 16;
2658static const size_t kPinTableMaxSize = 1024;
2659
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002660static const size_t kGlobalsInitial = 512; // Arbitrary.
2661static const size_t kGlobalsMax = 51200; // Arbitrary sanity check.
2662
2663static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
2664static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
2665
Elliott Hughesa0957642011-09-02 14:27:33 -07002666JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002667 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002668 check_jni_abort_hook(NULL),
Elliott Hughesa0957642011-09-02 14:27:33 -07002669 check_jni(options->check_jni_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002670 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002671 verbose_jni(options->IsVerbose("jni")),
2672 log_third_party_jni(options->IsVerbose("third-party-jni")),
2673 trace(options->jni_trace_),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002674 work_around_app_jni_bugs(false), // TODO: add a way to enable this
Elliott Hughes8daa0922011-09-11 13:46:25 -07002675 pins_lock("JNI pin table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002676 pin_table("pin table", kPinTableInitialSize, kPinTableMaxSize),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002677 globals_lock("JNI global reference table lock"),
Elliott Hughes6c1a3942011-08-17 15:00:06 -07002678 globals(kGlobalsInitial, kGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002679 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002680 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002681 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002682 libraries(new Libraries) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002683 functions = unchecked_functions = &gInvokeInterface;
2684 if (check_jni) {
2685 functions = GetCheckJniInvokeInterface();
2686 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002687}
2688
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002689JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002690 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002691}
2692
Elliott Hughes75770752011-08-24 17:52:38 -07002693bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2694 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002695
2696 // See if we've already loaded this library. If we have, and the class loader
2697 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002698 // TODO: for better results we should canonicalize the pathname (or even compare
2699 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002700 SharedLibrary* library;
2701 {
2702 // TODO: move the locking (and more of this logic) into Libraries.
2703 MutexLock mu(libraries_lock);
2704 library = libraries->Get(path);
2705 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002706 if (library != NULL) {
2707 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002708 // The library will be associated with class_loader. The JNI
2709 // spec says we can't load the same library into more than one
2710 // class loader.
2711 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2712 "ClassLoader %p; can't open in ClassLoader %p",
2713 path.c_str(), library->GetClassLoader(), class_loader);
2714 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002715 return false;
2716 }
2717 if (verbose_jni) {
2718 LOG(INFO) << "[Shared library \"" << path << "\" already loaded in "
2719 << "ClassLoader " << class_loader << "]";
2720 }
2721 if (!library->CheckOnLoadResult(this)) {
Elliott Hughes75770752011-08-24 17:52:38 -07002722 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2723 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002724 return false;
2725 }
2726 return true;
2727 }
2728
2729 // Open the shared library. Because we're using a full path, the system
2730 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2731 // resolve this library's dependencies though.)
2732
2733 // Failures here are expected when java.library.path has several entries
2734 // and we have to hunt for the lib.
2735
2736 // The current version of the dynamic linker prints detailed information
2737 // about dlopen() failures. Some things to check if the message is
2738 // cryptic:
2739 // - make sure the library exists on the device
2740 // - verify that the right path is being opened (the debug log message
2741 // above can help with that)
2742 // - check to see if the library is valid (e.g. not zero bytes long)
2743 // - check config/prelink-linux-arm.map to ensure that the library
2744 // is listed and is not being overrun by the previous entry (if
2745 // loading suddenly stops working on a prelinked library, this is
2746 // a good one to check)
2747 // - write a trivial app that calls sleep() then dlopen(), attach
2748 // to it with "strace -p <pid>" while it sleeps, and watch for
2749 // attempts to open nonexistent dependent shared libs
2750
2751 // TODO: automate some of these checks!
2752
2753 // This can execute slowly for a large library on a busy system, so we
2754 // want to switch from RUNNING to VMWAIT while it executes. This allows
2755 // the GC to ignore us.
2756 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002757 void* handle = NULL;
2758 {
2759 ScopedThreadStateChange tsc(self, Thread::kWaiting); // TODO: VMWAIT
2760 handle = dlopen(path.c_str(), RTLD_LAZY);
2761 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002762
2763 if (verbose_jni) {
2764 LOG(INFO) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
2765 }
2766
2767 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002768 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002769 return false;
2770 }
2771
2772 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002773 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002774 // TODO: move the locking (and more of this logic) into Libraries.
2775 MutexLock mu(libraries_lock);
2776 library = libraries->Get(path);
2777 if (library != NULL) {
2778 LOG(INFO) << "WOW: we lost a race to add shared library: "
2779 << "\"" << path << "\" ClassLoader=" << class_loader;
2780 return library->CheckOnLoadResult(this);
Elliott Hughescdf53122011-08-19 15:46:09 -07002781 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002782 library = new SharedLibrary(path, handle, class_loader);
2783 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002784 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002785
2786 if (verbose_jni) {
2787 LOG(INFO) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
2788 }
2789
2790 bool result = true;
2791 void* sym = dlsym(handle, "JNI_OnLoad");
2792 if (sym == NULL) {
2793 if (verbose_jni) {
2794 LOG(INFO) << "[No JNI_OnLoad found in \"" << path << "\"]";
2795 }
2796 } else {
2797 // Call JNI_OnLoad. We have to override the current class
2798 // loader, which will always be "null" since the stuff at the
2799 // top of the stack is around Runtime.loadLibrary(). (See
2800 // the comments in the JNI FindClass function.)
2801 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2802 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002803 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002804 self->SetClassLoaderOverride(class_loader);
2805
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002806 int version = 0;
2807 {
2808 ScopedThreadStateChange tsc(self, Thread::kNative);
2809 if (verbose_jni) {
2810 LOG(INFO) << "[Calling JNI_OnLoad in \"" << path << "\"]";
2811 }
2812 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002813 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002814
2815 self->SetClassLoaderOverride(old_class_loader);;
2816
2817 if (version != JNI_VERSION_1_2 &&
2818 version != JNI_VERSION_1_4 &&
2819 version != JNI_VERSION_1_6) {
2820 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2821 << "bad version: " << version;
2822 // It's unwise to call dlclose() here, but we can mark it
2823 // as bad and ensure that future load attempts will fail.
2824 // We don't know how far JNI_OnLoad got, so there could
2825 // be some partially-initialized stuff accessible through
2826 // newly-registered native method calls. We could try to
2827 // unregister them, but that doesn't seem worthwhile.
2828 result = false;
2829 } else {
2830 if (verbose_jni) {
2831 LOG(INFO) << "[Returned " << (result ? "successfully" : "failure")
2832 << " from JNI_OnLoad in \"" << path << "\"]";
2833 }
2834 }
2835 }
2836
2837 library->SetResult(result);
2838 return result;
2839}
2840
2841void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2842 CHECK(m->IsNative());
2843
2844 Class* c = m->GetDeclaringClass();
2845
2846 // If this is a static method, it could be called before the class
2847 // has been initialized.
2848 if (m->IsStatic()) {
2849 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c)) {
2850 return NULL;
2851 }
2852 } else {
2853 CHECK_GE(c->GetStatus(), Class::kStatusInitializing);
2854 }
2855
2856 MutexLock mu(libraries_lock);
2857 return libraries->FindNativeMethod(m);
Elliott Hughescdf53122011-08-19 15:46:09 -07002858}
2859
Elliott Hughes410c0c82011-09-01 17:58:25 -07002860void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2861 {
2862 MutexLock mu(globals_lock);
2863 globals.VisitRoots(visitor, arg);
2864 }
2865 {
2866 MutexLock mu(pins_lock);
2867 pin_table.VisitRoots(visitor, arg);
2868 }
2869 // The weak_globals table is visited by the GC itself (because it mutates the table).
2870}
2871
Ian Rogersdf20fe02011-07-20 20:34:16 -07002872} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002873
2874std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2875 switch (rhs) {
2876 case JNIInvalidRefType:
2877 os << "JNIInvalidRefType";
2878 return os;
2879 case JNILocalRefType:
2880 os << "JNILocalRefType";
2881 return os;
2882 case JNIGlobalRefType:
2883 os << "JNIGlobalRefType";
2884 return os;
2885 case JNIWeakGlobalRefType:
2886 os << "JNIWeakGlobalRefType";
2887 return os;
2888 }
2889}