blob: 26f069c64b4789a6da08e300b1508c6697eda834 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070021
22#include <cstdarg>
23#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070024#include <utility>
25#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070026
Elliott Hughes72025e52011-08-23 17:50:30 -070027#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070028#include "UniquePtr.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070029#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070030#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070031#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070033#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080034#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070035#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070036#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070037#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070038#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070039#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070040
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070041namespace art {
42
Elliott Hughes2ced6a52011-10-16 18:44:48 -070043static const size_t kMonitorsInitial = 32; // Arbitrary.
44static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
45
46static const size_t kLocalsInitial = 64; // Arbitrary.
47static const size_t kLocalsMax = 512; // Arbitrary sanity check.
48
49static const size_t kPinTableInitial = 16; // Arbitrary.
50static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
51
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070052static size_t gGlobalsInitial = 512; // Arbitrary.
53static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070054
55static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
56static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
57
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070058void SetJniGlobalsMax(size_t max) {
59 if (max != 0) {
60 gGlobalsMax = max;
61 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
62 }
63}
Ian Rogersdf20fe02011-07-20 20:34:16 -070064
Elliott Hughesc5f7c912011-08-18 14:00:42 -070065/*
66 * Add a local reference for an object to the current stack frame. When
67 * the native function returns, the reference will be discarded.
68 *
69 * We need to allow the same reference to be added multiple times.
70 *
71 * This will be called on otherwise unreferenced objects. We cannot do
72 * GC allocations here, and it's best if we don't grab a mutex.
73 *
74 * Returns the local reference (currently just the same pointer that was
75 * passed in), or NULL on failure.
76 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070077template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070078T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
79 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
80 Object* obj = const_cast<Object*>(const_obj);
81
Elliott Hughesc5f7c912011-08-18 14:00:42 -070082 if (obj == NULL) {
83 return NULL;
84 }
85
Elliott Hughes9c750f92012-04-05 12:07:59 -070086 DCHECK((reinterpret_cast<uintptr_t>(obj) & 0xffff0000) != 0xebad0000);
87
Elliott Hughesbf86d042011-08-31 17:53:14 -070088 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
89 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070090
Ian Rogers5a7a74a2011-09-26 16:32:29 -070091 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070092 IndirectRef ref = locals.Add(cookie, obj);
93 if (ref == NULL) {
94 // TODO: just change Add's DCHECK to CHECK and lose this?
95 locals.Dump();
96 LOG(FATAL) << "Failed adding to JNI local reference table "
97 << "(has " << locals.Capacity() << " entries)";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070098 }
99
100#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700101 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700102 size_t entry_count = locals.Capacity();
103 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700104 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700105 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700106 locals.Dump();
Elliott Hughes82188472011-11-07 18:11:48 -0800107 // TODO: LOG(FATAL) instead.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700108 }
109 }
110#endif
111
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800112 if (env->vm->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700113 // Hand out direct pointers to support broken old apps.
114 return reinterpret_cast<T>(obj);
115 }
116
117 return reinterpret_cast<T>(ref);
118}
Brian Carlstrom51477332012-03-25 20:20:26 -0700119// Explicit instantiations
120template jclass AddLocalReference<jclass>(JNIEnv* public_env, const Object* const_obj);
121template jobject AddLocalReference<jobject>(JNIEnv* public_env, const Object* const_obj);
122template jobjectArray AddLocalReference<jobjectArray>(JNIEnv* public_env, const Object* const_obj);
123template jstring AddLocalReference<jstring>(JNIEnv* public_env, const Object* const_obj);
124template jthrowable AddLocalReference<jthrowable>(JNIEnv* public_env, const Object* const_obj);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700125
Elliott Hughesbf86d042011-08-31 17:53:14 -0700126// For external use.
127template<typename T>
128T Decode(JNIEnv* public_env, jobject obj) {
129 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
130 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
131}
Shih-wei Liao24782c62012-01-08 12:46:11 -0800132// TODO: Change to use template when Mac OS build server no longer uses GCC 4.2.*.
133Object* DecodeObj(JNIEnv* public_env, jobject obj) {
134 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
135 return reinterpret_cast<Object*>(env->self->DecodeJObject(obj));
136}
Elliott Hughesbf86d042011-08-31 17:53:14 -0700137// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700138template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700139template Class* Decode<Class*>(JNIEnv*, jobject);
140template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
141template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700142template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700143template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700144template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700145template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400146template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700147template String* Decode<String*>(JNIEnv*, jobject);
148template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700149
Ian Rogers45619fc2012-02-29 11:15:25 -0800150size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
151 size_t num_bytes = 0;
152 for (size_t i = 1; i < shorty_len; ++i) {
153 char ch = shorty[i];
154 if (ch == 'D' || ch == 'J') {
155 num_bytes += 8;
156 } else if (ch == 'L') {
157 // Argument is a reference or an array. The shorty descriptor
158 // does not distinguish between these types.
159 num_bytes += sizeof(Object*);
160 } else {
161 num_bytes += 4;
162 }
163 }
164 return num_bytes;
165}
166
167class ArgArray {
168 public:
169 explicit ArgArray(Method* method) {
170 MethodHelper mh(method);
171 shorty_ = mh.GetShorty();
172 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -0700173 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800174 arg_array_ = small_arg_array_;
175 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700176 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800177 arg_array_ = large_arg_array_.get();
178 }
179 }
180
Elliott Hughes77405792012-03-15 15:22:12 -0700181 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800182 return arg_array_;
183 }
184
185 void BuildArgArray(JNIEnv* public_env, va_list ap) {
186 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700187 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800188 switch (shorty_[i]) {
189 case 'Z':
190 case 'B':
191 case 'C':
192 case 'S':
193 case 'I':
Elliott Hughes77405792012-03-15 15:22:12 -0700194 arg_array_[offset].i = va_arg(ap, jint);
Ian Rogers45619fc2012-02-29 11:15:25 -0800195 break;
196 case 'F':
Elliott Hughes77405792012-03-15 15:22:12 -0700197 arg_array_[offset].f = va_arg(ap, jdouble);
Ian Rogers45619fc2012-02-29 11:15:25 -0800198 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700199 case 'L':
200 arg_array_[offset].l = DecodeObj(env, va_arg(ap, jobject));
Ian Rogers45619fc2012-02-29 11:15:25 -0800201 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800202 case 'D':
Elliott Hughes77405792012-03-15 15:22:12 -0700203 arg_array_[offset].d = va_arg(ap, jdouble);
Ian Rogers45619fc2012-02-29 11:15:25 -0800204 break;
205 case 'J':
Elliott Hughes77405792012-03-15 15:22:12 -0700206 arg_array_[offset].j = va_arg(ap, jlong);
Ian Rogers45619fc2012-02-29 11:15:25 -0800207 break;
208 }
209 }
210 }
211
212 void BuildArgArray(JNIEnv* public_env, jvalue* args) {
213 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700214 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800215 switch (shorty_[i]) {
216 case 'Z':
217 case 'B':
218 case 'C':
219 case 'S':
220 case 'I':
Elliott Hughes77405792012-03-15 15:22:12 -0700221 arg_array_[offset].i = args[offset].i;
Ian Rogers45619fc2012-02-29 11:15:25 -0800222 break;
223 case 'F':
Elliott Hughes77405792012-03-15 15:22:12 -0700224 arg_array_[offset].f = args[offset].f;
Ian Rogers45619fc2012-02-29 11:15:25 -0800225 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700226 case 'L':
227 arg_array_[offset].l = DecodeObj(env, args[offset].l);
Ian Rogers45619fc2012-02-29 11:15:25 -0800228 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800229 case 'D':
Elliott Hughes77405792012-03-15 15:22:12 -0700230 arg_array_[offset].d = args[offset].d;
Ian Rogers45619fc2012-02-29 11:15:25 -0800231 break;
232 case 'J':
Elliott Hughes77405792012-03-15 15:22:12 -0700233 arg_array_[offset].j = args[offset].j;
Ian Rogers45619fc2012-02-29 11:15:25 -0800234 break;
235 }
236 }
237 }
238
Ian Rogers45619fc2012-02-29 11:15:25 -0800239 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700240 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800241 const char* shorty_;
242 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700243 JValue* arg_array_;
244 JValue small_arg_array_[kSmallArgArraySize];
245 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800246};
247
Elliott Hughes0512f022012-03-15 22:10:52 -0700248static jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700249 if (obj == NULL) {
250 return NULL;
251 }
Elliott Hughes75770752011-08-24 17:52:38 -0700252 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700253 IndirectReferenceTable& weak_globals = vm->weak_globals;
254 MutexLock mu(vm->weak_globals_lock);
255 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
256 return reinterpret_cast<jweak>(ref);
257}
258
Elliott Hughesbf86d042011-08-31 17:53:14 -0700259// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700260template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -0700261static T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700262 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700263}
264
Elliott Hughesb264f082012-04-06 17:10:10 -0700265// TODO: can we make this available in non-debug builds if CheckJNI is on, or is it too expensive?
266#if !defined(NDEBUG)
267static void CheckMethodArguments(Method* m, JValue* args) {
268 MethodHelper mh(m);
269 ObjectArray<Class>* parameter_types = mh.GetParameterTypes();
270 CHECK(parameter_types != NULL);
271 size_t error_count = 0;
272 for (int i = 0; i < parameter_types->GetLength(); ++i) {
273 Class* parameter_type = parameter_types->Get(i);
274 if (!parameter_type->IsPrimitive()) {
275 Object* argument = args[i].l;
276 if (argument != NULL && !argument->InstanceOf(parameter_type)) {
277 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
278 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
279 ++error_count;
280 }
281 }
282 }
283 if (error_count > 0) {
284 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
285 // with an argument.
286 JniAbort(NULL);
287 }
288}
289#else
290static void CheckMethodArguments(Method*, JValue*) { }
291#endif
292
Elliott Hughes77405792012-03-15 15:22:12 -0700293static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, JValue* args) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700294 CheckMethodArguments(method, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700295 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughesdbac3092012-03-16 18:00:30 -0700296 JValue result = { 0 };
Elliott Hughes418d20f2011-09-22 14:00:39 -0700297 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700298 return result;
299}
300
Ian Rogers0571d352011-11-03 19:51:38 -0700301static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700302 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800303 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700304 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800305 ArgArray arg_array(method);
306 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700307 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700308}
309
Ian Rogers0571d352011-11-03 19:51:38 -0700310static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700311 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700312}
313
Ian Rogers0571d352011-11-03 19:51:38 -0700314static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
315 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700316 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800317 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700318 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800319 ArgArray arg_array(method);
320 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700321 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700322}
323
Ian Rogers0571d352011-11-03 19:51:38 -0700324static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
325 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700326 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800327 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700328 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800329 ArgArray arg_array(method);
330 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700331 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700332}
333
Elliott Hughes6b436852011-08-12 10:16:44 -0700334// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
335// separated with slashes but aren't wrapped with "L;" like regular descriptors
336// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
337// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
338// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700339static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700340 std::string result;
341 // Add the missing "L;" if necessary.
342 if (name[0] == '[') {
343 result = name;
344 } else {
345 result += 'L';
346 result += name;
347 result += ';';
348 }
349 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700350 if (result.find('.') != std::string::npos) {
351 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
352 << "\"" << name << "\"";
353 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700354 }
355 return result;
356}
357
Ian Rogers0571d352011-11-03 19:51:38 -0700358static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700359 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800360 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700361}
362
Ian Rogers0571d352011-11-03 19:51:38 -0700363static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700364 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700365 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700366 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700367 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700368
369 Method* method = NULL;
370 if (is_static) {
371 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700372 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700373 method = c->FindVirtualMethod(name, sig);
374 if (method == NULL) {
375 // No virtual method matching the signature. Search declared
376 // private methods and constructors.
377 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700378 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700379 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700380
Elliott Hughescdf53122011-08-19 15:46:09 -0700381 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700382 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700383 return NULL;
384 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700385
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700386 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700387}
388
Ian Rogers0571d352011-11-03 19:51:38 -0700389static const ClassLoader* GetClassLoader(Thread* self) {
Elliott Hughes6a144332012-04-03 13:07:11 -0700390 Method* method = self->GetCurrentMethod();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700391 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
392 return self->GetClassLoaderOverride();
393 }
394 return method->GetDeclaringClass()->GetClassLoader();
395}
396
Ian Rogers0571d352011-11-03 19:51:38 -0700397static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700398 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700399 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700400 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700401 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700402
403 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700404 Class* field_type;
405 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
406 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700407 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700408 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700409 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700410 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700411 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700412 if (field_type == NULL) {
413 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700414 DCHECK(ts.Self()->IsExceptionPending());
415 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700416 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700417 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800418 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700419 return NULL;
420 }
421 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800422 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800424 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700425 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700426 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700427 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700428 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800429 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700430 return NULL;
431 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700432 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700433}
434
Ian Rogers0571d352011-11-03 19:51:38 -0700435static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700436 JavaVMExt* vm = ts.Vm();
437 MutexLock mu(vm->pins_lock);
438 vm->pin_table.Add(array);
439}
440
Ian Rogers0571d352011-11-03 19:51:38 -0700441static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700442 JavaVMExt* vm = ts.Vm();
443 MutexLock mu(vm->pins_lock);
444 vm->pin_table.Remove(array);
445}
446
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700447template<typename JniT, typename ArtT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700448static JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700449 CHECK_GE(length, 0); // TODO: ReportJniError
450 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700451 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700452}
453
Elliott Hughes75770752011-08-24 17:52:38 -0700454template <typename ArrayT, typename CArrayT, typename ArtArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700455static CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -0700456 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
457 PinPrimitiveArray(ts, array);
458 if (is_copy != NULL) {
459 *is_copy = JNI_FALSE;
460 }
461 return array->GetData();
462}
463
464template <typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700465static void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
Elliott Hughes75770752011-08-24 17:52:38 -0700466 if (mode != JNI_COMMIT) {
467 Array* array = Decode<Array*>(ts, java_array);
468 UnpinPrimitiveArray(ts, array);
469 }
470}
471
Ian Rogers0571d352011-11-03 19:51:38 -0700472static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700473 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700474 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700475 "%s offset=%d length=%d %s.length=%d",
476 type.c_str(), start, length, identifier, array->GetLength());
477}
Ian Rogers0571d352011-11-03 19:51:38 -0700478
479static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700480 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700481 "offset=%d length=%d string.length()=%d", start, length, array_length);
482}
Elliott Hughes814e4032011-08-23 12:07:56 -0700483
484template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700485static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700486 ArrayT* array = Decode<ArrayT*>(ts, java_array);
487 if (start < 0 || length < 0 || start + length > array->GetLength()) {
488 ThrowAIOOBE(ts, array, start, length, "src");
489 } else {
490 JavaT* data = array->GetData();
491 memcpy(buf, data + start, length * sizeof(JavaT));
492 }
493}
494
495template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700496static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700497 ArrayT* array = Decode<ArrayT*>(ts, java_array);
498 if (start < 0 || length < 0 || start + length > array->GetLength()) {
499 ThrowAIOOBE(ts, array, start, length, "dst");
500 } else {
501 JavaT* data = array->GetData();
502 memcpy(data + start, buf, length * sizeof(JavaT));
503 }
504}
505
Ian Rogers0571d352011-11-03 19:51:38 -0700506static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700507 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
508 CHECK(buffer_class.get() != NULL);
509 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
510}
511
Ian Rogers0571d352011-11-03 19:51:38 -0700512static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700513 static jclass buffer_class = InitDirectByteBufferClass(env);
514 return buffer_class;
515}
516
Elliott Hughes462c9442012-03-23 18:47:50 -0700517static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700518 if (vm == NULL || p_env == NULL) {
519 return JNI_ERR;
520 }
521
Elliott Hughes462c9442012-03-23 18:47:50 -0700522 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700523 Thread* self = Thread::Current();
524 if (self != NULL) {
525 *p_env = self->GetJniEnv();
526 return JNI_OK;
527 }
528
529 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
530
531 // No threads allowed in zygote mode.
532 if (runtime->IsZygote()) {
533 LOG(ERROR) << "Attempt to attach a thread in the zygote";
534 return JNI_ERR;
535 }
536
Elliott Hughes462c9442012-03-23 18:47:50 -0700537 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
538 const char* thread_name = NULL;
539 Object* thread_group = NULL;
540 if (args != NULL) {
541 CHECK_GE(args->version, JNI_VERSION_1_2);
542 thread_name = args->name;
543 thread_group = static_cast<Thread*>(NULL)->DecodeJObject(args->group);
Elliott Hughes75770752011-08-24 17:52:38 -0700544 }
Elliott Hughes75770752011-08-24 17:52:38 -0700545
Elliott Hughes462c9442012-03-23 18:47:50 -0700546 runtime->AttachCurrentThread(thread_name, as_daemon, thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700547 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700548 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700549}
550
Elliott Hughes79082e32011-08-25 12:07:32 -0700551class SharedLibrary {
552 public:
553 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
554 : path_(path),
555 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700556 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700557 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughese62934d2012-04-09 11:24:29 -0700558 jni_on_load_cond_("JNI_OnLoad condition variable"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700559 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700560 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700561 }
562
Elliott Hughes79082e32011-08-25 12:07:32 -0700563 Object* GetClassLoader() {
564 return class_loader_;
565 }
566
567 std::string GetPath() {
568 return path_;
569 }
570
571 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700572 * Check the result of an earlier call to JNI_OnLoad on this library.
573 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700574 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700575 bool CheckOnLoadResult() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700576 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700577 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700578 // Check this so we don't end up waiting for ourselves. We need
579 // to return "true" so the caller can continue.
580 LOG(INFO) << *self << " recursive attempt to load library "
581 << "\"" << path_ << "\"";
582 return true;
583 }
584
585 MutexLock mu(jni_on_load_lock_);
586 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800587 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
588 << "JNI_OnLoad...]";
Elliott Hughes34e06962012-04-09 13:55:55 -0700589 ScopedThreadStateChange tsc(self, kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700590 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700591 }
592
593 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800594 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
595 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700596 return okay;
597 }
598
599 void SetResult(bool result) {
600 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700601 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700602
603 // Broadcast a wakeup to anybody sleeping on the condition variable.
604 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700605 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700606 }
607
608 void* FindSymbol(const std::string& symbol_name) {
609 return dlsym(handle_, symbol_name.c_str());
610 }
611
612 private:
613 enum JNI_OnLoadState {
614 kPending,
615 kFailed,
616 kOkay,
617 };
618
619 // Path to library "/system/lib/libjni.so".
620 std::string path_;
621
622 // The void* returned by dlopen(3).
623 void* handle_;
624
625 // The ClassLoader this library is associated with.
626 Object* class_loader_;
627
628 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700629 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700630 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700631 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700632 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700633 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700634 // Result of earlier JNI_OnLoad call.
635 JNI_OnLoadState jni_on_load_result_;
636};
637
Elliott Hughes79082e32011-08-25 12:07:32 -0700638// This exists mainly to keep implementation details out of the header file.
639class Libraries {
640 public:
641 Libraries() {
642 }
643
644 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700645 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700646 }
647
648 SharedLibrary* Get(const std::string& path) {
649 return libraries_[path];
650 }
651
652 void Put(const std::string& path, SharedLibrary* library) {
653 libraries_[path] = library;
654 }
655
656 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700657 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700658 std::string jni_short_name(JniShortName(m));
659 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700660 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700661 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
662 SharedLibrary* library = it->second;
663 if (library->GetClassLoader() != declaring_class_loader) {
664 // We only search libraries loaded by the appropriate ClassLoader.
665 continue;
666 }
667 // Try the short name then the long name...
668 void* fn = library->FindSymbol(jni_short_name);
669 if (fn == NULL) {
670 fn = library->FindSymbol(jni_long_name);
671 }
672 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800673 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
674 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700675 return fn;
676 }
677 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700678 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700679 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700680 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700681 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700682 return NULL;
683 }
684
685 private:
686 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
687
688 std::map<std::string, SharedLibrary*> libraries_;
689};
690
Elliott Hughes418d20f2011-09-22 14:00:39 -0700691JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
692 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
693 Object* receiver = Decode<Object*>(env, obj);
694 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800695 ArgArray arg_array(method);
696 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700697 return InvokeWithArgArray(env, receiver, method, arg_array.get());
698}
699
Elliott Hughesd07986f2011-12-06 18:27:45 -0800700JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700701 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800702}
703
Elliott Hughescdf53122011-08-19 15:46:09 -0700704class JNI {
705 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700706
Elliott Hughescdf53122011-08-19 15:46:09 -0700707 static jint GetVersion(JNIEnv* env) {
708 ScopedJniThreadState ts(env);
709 return JNI_VERSION_1_6;
710 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700711
Elliott Hughescdf53122011-08-19 15:46:09 -0700712 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
713 ScopedJniThreadState ts(env);
714 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700715 return NULL;
716 }
717
Elliott Hughescdf53122011-08-19 15:46:09 -0700718 static jclass FindClass(JNIEnv* env, const char* name) {
719 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700720 Runtime* runtime = Runtime::Current();
721 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700723 Class* c = NULL;
724 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700725 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800726 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700727 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800728 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700729 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700730 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700731 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700732
Elliott Hughescdf53122011-08-19 15:46:09 -0700733 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
734 ScopedJniThreadState ts(env);
735 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700736 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700737 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700738
Elliott Hughescdf53122011-08-19 15:46:09 -0700739 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
740 ScopedJniThreadState ts(env);
741 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700742 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700744
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
746 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700747 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700748 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700749 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700750
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
752 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700753 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700754 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700756
Elliott Hughes37f7a402011-08-22 18:56:01 -0700757 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
758 ScopedJniThreadState ts(env);
759 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700760 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700761 }
762
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700763 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700764 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700765 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700766 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700768
Elliott Hughes37f7a402011-08-22 18:56:01 -0700769 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700770 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700771 Class* c1 = Decode<Class*>(ts, java_class1);
772 Class* c2 = Decode<Class*>(ts, java_class2);
773 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700775
Elliott Hughese84278b2012-03-22 10:06:53 -0700776 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700778 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700779 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700780 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700781 return JNI_TRUE;
782 } else {
783 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700784 Class* c = Decode<Class*>(ts, java_class);
785 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700786 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700788
Elliott Hughes37f7a402011-08-22 18:56:01 -0700789 static jint Throw(JNIEnv* env, jthrowable java_exception) {
790 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700791 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700792 if (exception == NULL) {
793 return JNI_ERR;
794 }
795 ts.Self()->SetException(exception);
796 return JNI_OK;
797 }
798
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700799 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700800 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700801 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700802 jmethodID mid = ((msg != NULL)
803 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
804 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700805 if (mid == NULL) {
806 return JNI_ERR;
807 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700808 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700809 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700810 return JNI_ERR;
811 }
812
813 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700814 args[0].l = s.get();
815 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
816 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700817 return JNI_ERR;
818 }
819
Elliott Hughes72025e52011-08-23 17:50:30 -0700820 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700821
Elliott Hughes37f7a402011-08-22 18:56:01 -0700822 return JNI_OK;
823 }
824
825 static jboolean ExceptionCheck(JNIEnv* env) {
826 ScopedJniThreadState ts(env);
827 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
828 }
829
830 static void ExceptionClear(JNIEnv* env) {
831 ScopedJniThreadState ts(env);
832 ts.Self()->ClearException();
833 }
834
835 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700837
838 Thread* self = ts.Self();
839 Throwable* original_exception = self->GetException();
840 self->ClearException();
841
Elliott Hughesbf86d042011-08-31 17:53:14 -0700842 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700843 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
844 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
845 if (mid == NULL) {
846 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700847 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700848 } else {
849 env->CallVoidMethod(exception.get(), mid);
850 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700851 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700852 << " thrown while calling printStackTrace";
853 self->ClearException();
854 }
855 }
856
857 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700858 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700859
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 static jthrowable ExceptionOccurred(JNIEnv* env) {
861 ScopedJniThreadState ts(env);
862 Object* exception = ts.Self()->GetException();
Elliott Hughes81ff3182012-03-23 20:35:56 -0700863 return (exception != NULL) ? AddLocalReference<jthrowable>(env, exception) : NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700864 }
865
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 static void FatalError(JNIEnv* env, const char* msg) {
867 ScopedJniThreadState ts(env);
868 LOG(FATAL) << "JNI FatalError called: " << msg;
869 }
870
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700871 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700873 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
874 return JNI_ERR;
875 }
876 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 return JNI_OK;
878 }
879
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700880 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700881 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700882 Object* survivor = Decode<Object*>(ts, java_survivor);
883 ts.Env()->PopFrame();
884 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700885 }
886
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700887 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700888 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700889 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
890 }
891
892 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
893 // TODO: we should try to expand the table if necessary.
894 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
895 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
896 return JNI_ERR;
897 }
898 // TODO: this isn't quite right, since "capacity" includes holes.
899 size_t capacity = ts.Env()->locals.Capacity();
900 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
901 if (!okay) {
902 ts.Self()->ThrowOutOfMemoryError(caller);
903 }
904 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 }
906
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
908 ScopedJniThreadState ts(env);
909 if (obj == NULL) {
910 return NULL;
911 }
912
Elliott Hughes75770752011-08-24 17:52:38 -0700913 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 IndirectReferenceTable& globals = vm->globals;
915 MutexLock mu(vm->globals_lock);
916 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
917 return reinterpret_cast<jobject>(ref);
918 }
919
920 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
921 ScopedJniThreadState ts(env);
922 if (obj == NULL) {
923 return;
924 }
925
Elliott Hughes75770752011-08-24 17:52:38 -0700926 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 IndirectReferenceTable& globals = vm->globals;
928 MutexLock mu(vm->globals_lock);
929
930 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
931 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
932 << "failed to find entry";
933 }
934 }
935
936 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
937 ScopedJniThreadState ts(env);
938 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
939 }
940
941 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
942 ScopedJniThreadState ts(env);
943 if (obj == NULL) {
944 return;
945 }
946
Elliott Hughes75770752011-08-24 17:52:38 -0700947 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 IndirectReferenceTable& weak_globals = vm->weak_globals;
949 MutexLock mu(vm->weak_globals_lock);
950
951 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
952 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
953 << "failed to find entry";
954 }
955 }
956
957 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
958 ScopedJniThreadState ts(env);
959 if (obj == NULL) {
960 return NULL;
961 }
962
963 IndirectReferenceTable& locals = ts.Env()->locals;
964
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700965 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
967 return reinterpret_cast<jobject>(ref);
968 }
969
970 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
971 ScopedJniThreadState ts(env);
972 if (obj == NULL) {
973 return;
974 }
975
976 IndirectReferenceTable& locals = ts.Env()->locals;
977
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700978 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 if (!locals.Remove(cookie, obj)) {
980 // Attempting to delete a local reference that is not in the
981 // topmost local reference frame is a no-op. DeleteLocalRef returns
982 // void and doesn't throw any exceptions, but we should probably
983 // complain about it so the user will notice that things aren't
984 // going quite the way they expect.
985 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
986 << "failed to find entry";
987 }
988 }
989
990 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
991 ScopedJniThreadState ts(env);
992 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
993 ? JNI_TRUE : JNI_FALSE;
994 }
995
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700996 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700998 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700999 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001000 return NULL;
1001 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001002 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 }
1004
Elliott Hughese84278b2012-03-22 10:06:53 -07001005 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 ScopedJniThreadState ts(env);
1007 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001009 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 va_end(args);
1011 return result;
1012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001016 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001017 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001018 return NULL;
1019 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001020 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001021 if (result == NULL) {
1022 return NULL;
1023 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001024 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001026 if (!ts.Self()->IsExceptionPending()) {
1027 return local_result;
1028 } else {
1029 return NULL;
1030 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001035 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001036 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001037 return NULL;
1038 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001039 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001040 if (result == NULL) {
1041 return NULL;
1042 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001043 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001045 if (!ts.Self()->IsExceptionPending()) {
1046 return local_result;
1047 } else {
1048 return NULL;
1049 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1053 ScopedJniThreadState ts(env);
1054 return FindMethodID(ts, c, name, sig, false);
1055 }
1056
1057 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1058 ScopedJniThreadState ts(env);
1059 return FindMethodID(ts, c, name, sig, true);
1060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 va_list ap;
1065 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001066 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001068 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001073 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001074 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001079 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001080 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 va_list ap;
1086 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001087 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 va_end(ap);
1089 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001094 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001099 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 va_list ap;
1105 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001106 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 va_end(ap);
1108 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001113 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001118 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_list ap;
1124 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001125 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 va_end(ap);
1127 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001132 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001137 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 va_list ap;
1143 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001144 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 va_end(ap);
1146 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001151 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 }
1153
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001156 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 va_list ap;
1162 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001163 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 va_end(ap);
1165 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001170 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001175 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 va_list ap;
1181 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001182 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 va_end(ap);
1184 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001189 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001194 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 va_list ap;
1200 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001201 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 va_end(ap);
1203 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
1205
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001208 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 }
1210
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001213 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
1215
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 va_list ap;
1219 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001220 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 va_end(ap);
1222 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 }
1224
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001227 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001232 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 }
1234
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 va_list ap;
1238 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001239 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001245 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
Elliott Hughes72025e52011-08-23 17:50:30 -07001248 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001250 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 }
1252
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001253 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 ScopedJniThreadState ts(env);
1255 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001256 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001257 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001258 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 va_end(ap);
1260 return local_result;
1261 }
1262
1263 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001264 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001266 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001267 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
1270 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001271 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001273 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001274 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
1277 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001278 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 ScopedJniThreadState ts(env);
1280 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001282 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 va_end(ap);
1284 return result.z;
1285 }
1286
1287 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001288 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001290 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
1293 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001294 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001296 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
1298
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001299 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 ScopedJniThreadState ts(env);
1301 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001302 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001303 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 va_end(ap);
1305 return result.b;
1306 }
1307
1308 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001309 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001311 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
1314 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001315 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001317 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001320 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 ScopedJniThreadState ts(env);
1322 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001324 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 va_end(ap);
1326 return result.c;
1327 }
1328
1329 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001332 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
1335 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001336 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001338 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001341 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 ScopedJniThreadState ts(env);
1343 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001345 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 va_end(ap);
1347 return result.s;
1348 }
1349
1350 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001351 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001353 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
1356 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001357 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001359 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 }
1361
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001362 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 ScopedJniThreadState ts(env);
1364 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001365 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001366 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 va_end(ap);
1368 return result.i;
1369 }
1370
1371 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001372 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001374 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
1377 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001378 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001380 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
1382
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001383 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 ScopedJniThreadState ts(env);
1385 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001386 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001387 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 va_end(ap);
1389 return result.j;
1390 }
1391
1392 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001393 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001395 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
1398 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001399 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001401 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 }
1403
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001404 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 ScopedJniThreadState ts(env);
1406 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001407 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001408 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 va_end(ap);
1410 return result.f;
1411 }
1412
1413 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001414 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001416 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 }
1418
1419 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001420 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001422 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001425 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 ScopedJniThreadState ts(env);
1427 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001428 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001429 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 va_end(ap);
1431 return result.d;
1432 }
1433
1434 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001435 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001437 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
1440 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001441 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001443 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001446 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 ScopedJniThreadState ts(env);
1448 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001449 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001450 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 va_end(ap);
1452 }
1453
1454 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001455 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001457 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
1460 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001461 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001463 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 }
1465
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001466 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 ScopedJniThreadState ts(env);
1468 return FindFieldID(ts, c, name, sig, false);
1469 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001470
1471
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001472 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 ScopedJniThreadState ts(env);
1474 return FindFieldID(ts, c, name, sig, true);
1475 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001476
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001477 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001480 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001481 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001483
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001486 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001487 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 }
1489
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001490 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 Object* o = Decode<Object*>(ts, java_object);
1493 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001494 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001496 }
1497
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001501 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 }
1504
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001505#define GET_PRIMITIVE_FIELD(fn, instance) \
1506 ScopedJniThreadState ts(env); \
1507 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001508 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001509 return f->fn(o)
1510
1511#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1512 ScopedJniThreadState ts(env); \
1513 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001514 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001515 f->fn(o, value)
1516
1517 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1518 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 }
1520
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001521 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1522 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 }
1524
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001525 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1526 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 }
1528
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001529 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1530 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001531 }
1532
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001533 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1534 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
1536
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001537 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1538 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001539 }
1540
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001541 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1542 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1546 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001549 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001550 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001553 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001554 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 }
1556
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001557 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001558 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001561 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001562 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001565 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001566 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001569 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001570 GET_PRIMITIVE_FIELD(GetLong, NULL);
1571 }
1572
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001573 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1575 }
1576
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001577 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1579 }
1580
1581 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1582 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1583 }
1584
1585 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1586 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1587 }
1588
1589 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1590 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1591 }
1592
1593 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1594 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1595 }
1596
1597 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1598 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1599 }
1600
1601 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1602 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1603 }
1604
1605 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1606 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1607 }
1608
1609 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1610 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1611 }
1612
1613 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1614 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1615 }
1616
1617 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1618 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1619 }
1620
1621 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1622 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1623 }
1624
1625 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1626 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1627 }
1628
1629 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1630 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1631 }
1632
1633 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1634 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1635 }
1636
1637 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1638 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1639 }
1640
1641 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1642 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001645 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 ScopedJniThreadState ts(env);
1647 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001649 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001650 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 va_end(ap);
1652 return local_result;
1653 }
1654
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001655 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001657 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001658 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 }
1660
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001661 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001663 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001664 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001667 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 ScopedJniThreadState ts(env);
1669 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001671 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 va_end(ap);
1673 return result.z;
1674 }
1675
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001676 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001678 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 }
1680
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001681 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001683 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001686 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 ScopedJniThreadState ts(env);
1688 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001690 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 va_end(ap);
1692 return result.b;
1693 }
1694
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001695 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 }
1699
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001700 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001702 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 }
1704
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001705 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
1707 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001709 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 va_end(ap);
1711 return result.c;
1712 }
1713
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001714 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001719 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001721 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 }
1723
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001724 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 ScopedJniThreadState ts(env);
1726 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001727 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001728 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 va_end(ap);
1730 return result.s;
1731 }
1732
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001733 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001735 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001738 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001740 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001743 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 ScopedJniThreadState ts(env);
1745 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001746 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001747 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 va_end(ap);
1749 return result.i;
1750 }
1751
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001752 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001754 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001757 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001759 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001762 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 ScopedJniThreadState ts(env);
1764 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001765 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001766 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 va_end(ap);
1768 return result.j;
1769 }
1770
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001771 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001773 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001776 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001778 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001781 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 ScopedJniThreadState ts(env);
1783 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001784 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001785 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 va_end(ap);
1787 return result.f;
1788 }
1789
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001790 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001792 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001795 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001797 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001800 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 ScopedJniThreadState ts(env);
1802 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001803 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001804 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 va_end(ap);
1806 return result.d;
1807 }
1808
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001809 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001811 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001814 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001816 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 }
1818
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001819 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 ScopedJniThreadState ts(env);
1821 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001822 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001823 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 va_end(ap);
1825 }
1826
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001827 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001829 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 }
1831
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001832 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001834 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
Elliott Hughes814e4032011-08-23 12:07:56 -07001837 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001839 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001840 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
1843 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1844 ScopedJniThreadState ts(env);
1845 if (utf == NULL) {
1846 return NULL;
1847 }
1848 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001849 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 }
1851
Elliott Hughes814e4032011-08-23 12:07:56 -07001852 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1853 ScopedJniThreadState ts(env);
1854 return Decode<String*>(ts, java_string)->GetLength();
1855 }
1856
1857 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1858 ScopedJniThreadState ts(env);
1859 return Decode<String*>(ts, java_string)->GetUtfLength();
1860 }
1861
Elliott Hughesb465ab02011-08-24 11:21:21 -07001862 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001864 String* s = Decode<String*>(ts, java_string);
1865 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1866 ThrowSIOOBE(ts, start, length, s->GetLength());
1867 } else {
1868 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1869 memcpy(buf, chars + start, length * sizeof(jchar));
1870 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001871 }
1872
Elliott Hughesb465ab02011-08-24 11:21:21 -07001873 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001874 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001875 String* s = Decode<String*>(ts, java_string);
1876 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1877 ThrowSIOOBE(ts, start, length, s->GetLength());
1878 } else {
1879 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1880 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1881 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001882 }
1883
Elliott Hughes75770752011-08-24 17:52:38 -07001884 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001885 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001886 String* s = Decode<String*>(ts, java_string);
1887 const CharArray* chars = s->GetCharArray();
1888 PinPrimitiveArray(ts, chars);
1889 if (is_copy != NULL) {
1890 *is_copy = JNI_FALSE;
1891 }
1892 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001893 }
1894
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001895 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001896 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001897 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 }
1899
Elliott Hughes75770752011-08-24 17:52:38 -07001900 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001902 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001903 }
1904
Elliott Hughes75770752011-08-24 17:52:38 -07001905 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001907 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001908 }
1909
Elliott Hughes75770752011-08-24 17:52:38 -07001910 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001911 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001912 if (java_string == NULL) {
1913 return NULL;
1914 }
1915 if (is_copy != NULL) {
1916 *is_copy = JNI_TRUE;
1917 }
1918 String* s = Decode<String*>(ts, java_string);
1919 size_t byte_count = s->GetUtfLength();
1920 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001921 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001922 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1923 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1924 bytes[byte_count] = '\0';
1925 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001926 }
1927
Elliott Hughes75770752011-08-24 17:52:38 -07001928 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001929 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001930 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001931 }
1932
Elliott Hughesbd935992011-08-22 11:59:34 -07001933 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001935 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001936 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001937 Array* array = obj->AsArray();
1938 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Elliott Hughes814e4032011-08-23 12:07:56 -07001941 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001942 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001943 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001944 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
1947 static void SetObjectArrayElement(JNIEnv* env,
1948 jobjectArray java_array, jsize index, jobject java_value) {
1949 ScopedJniThreadState ts(env);
1950 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1951 Object* value = Decode<Object*>(ts, java_value);
1952 array->Set(index, value);
1953 }
1954
1955 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1956 ScopedJniThreadState ts(env);
1957 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1958 }
1959
1960 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1961 ScopedJniThreadState ts(env);
1962 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1963 }
1964
1965 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1966 ScopedJniThreadState ts(env);
1967 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1968 }
1969
1970 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1971 ScopedJniThreadState ts(env);
1972 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1973 }
1974
1975 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1976 ScopedJniThreadState ts(env);
1977 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1978 }
1979
1980 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1981 ScopedJniThreadState ts(env);
1982 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1983 }
1984
1985 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1986 ScopedJniThreadState ts(env);
1987 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1988 }
1989
1990 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1991 ScopedJniThreadState ts(env);
1992 CHECK_GE(length, 0); // TODO: ReportJniError
1993
1994 // Compute the array class corresponding to the given element class.
1995 Class* element_class = Decode<Class*>(ts, element_jclass);
1996 std::string descriptor;
1997 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001998 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001999
2000 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002001 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2002 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 return NULL;
2004 }
2005
Elliott Hughes75770752011-08-24 17:52:38 -07002006 // Allocate and initialize if necessary.
2007 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002009 if (initial_element != NULL) {
2010 Object* initial_object = Decode<Object*>(ts, initial_element);
2011 for (jsize i = 0; i < length; ++i) {
2012 result->Set(i, initial_object);
2013 }
2014 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002015 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
2018 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2019 ScopedJniThreadState ts(env);
2020 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2021 }
2022
Ian Rogersa15e67d2012-02-28 13:51:55 -08002023 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002024 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002025 Array* array = Decode<Array*>(ts, java_array);
2026 PinPrimitiveArray(ts, array);
2027 if (is_copy != NULL) {
2028 *is_copy = JNI_FALSE;
2029 }
2030 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002031 }
2032
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002033 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002034 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002035 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002036 }
2037
Elliott Hughes75770752011-08-24 17:52:38 -07002038 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002040 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes75770752011-08-24 17:52:38 -07002043 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002045 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes75770752011-08-24 17:52:38 -07002048 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002050 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes75770752011-08-24 17:52:38 -07002053 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002055 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes75770752011-08-24 17:52:38 -07002058 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002060 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes75770752011-08-24 17:52:38 -07002063 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002065 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes75770752011-08-24 17:52:38 -07002068 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002069 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002070 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes75770752011-08-24 17:52:38 -07002073 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002075 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002078 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002080 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002083 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002085 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002088 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002090 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002093 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002095 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002098 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002100 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002103 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002105 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002108 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002110 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002113 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002115 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes814e4032011-08-23 12:07:56 -07002123 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002125 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes814e4032011-08-23 12:07:56 -07002128 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002130 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes814e4032011-08-23 12:07:56 -07002138 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Elliott Hughes814e4032011-08-23 12:07:56 -07002143 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes814e4032011-08-23 12:07:56 -07002148 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Elliott Hughes814e4032011-08-23 12:07:56 -07002153 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002155 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Elliott Hughes814e4032011-08-23 12:07:56 -07002158 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002160 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Elliott Hughes814e4032011-08-23 12:07:56 -07002163 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002165 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 }
2167
Elliott Hughes814e4032011-08-23 12:07:56 -07002168 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002170 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Elliott Hughes814e4032011-08-23 12:07:56 -07002173 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002175 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes814e4032011-08-23 12:07:56 -07002178 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002180 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
Elliott Hughes814e4032011-08-23 12:07:56 -07002183 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002185 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
Elliott Hughes814e4032011-08-23 12:07:56 -07002188 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002190 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Elliott Hughes814e4032011-08-23 12:07:56 -07002193 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002195 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Elliott Hughes5174fe62011-08-23 15:12:35 -07002198 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002200 Class* c = Decode<Class*>(ts, java_class);
2201
Elliott Hughes5174fe62011-08-23 15:12:35 -07002202 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 const char* name = methods[i].name;
2204 const char* sig = methods[i].signature;
2205
2206 if (*sig == '!') {
2207 // TODO: fast jni. it's too noisy to log all these.
2208 ++sig;
2209 }
2210
Elliott Hughes5174fe62011-08-23 15:12:35 -07002211 Method* m = c->FindDirectMethod(name, sig);
2212 if (m == NULL) {
2213 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002215 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002216 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002217 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002219 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002220 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002221 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002222 return JNI_ERR;
2223 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002224
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002225 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002226
Ian Rogers60db5ab2012-02-20 17:02:00 -08002227 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229 return JNI_OK;
2230 }
2231
Elliott Hughes5174fe62011-08-23 15:12:35 -07002232 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002234 Class* c = Decode<Class*>(ts, java_class);
2235
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002236 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002237
2238 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2239 Method* m = c->GetDirectMethod(i);
2240 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002241 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002242 }
2243 }
2244 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2245 Method* m = c->GetVirtualMethod(i);
2246 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002247 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002248 }
2249 }
2250
2251 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002252 }
2253
Elliott Hughes72025e52011-08-23 17:50:30 -07002254 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002255 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002256 Object* o = Decode<Object*>(ts, java_object);
2257 o->MonitorEnter(ts.Self());
2258 if (ts.Self()->IsExceptionPending()) {
2259 return JNI_ERR;
2260 }
2261 ts.Env()->monitors.Add(o);
2262 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
2264
Elliott Hughes72025e52011-08-23 17:50:30 -07002265 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002266 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002267 Object* o = Decode<Object*>(ts, java_object);
2268 o->MonitorExit(ts.Self());
2269 if (ts.Self()->IsExceptionPending()) {
2270 return JNI_ERR;
2271 }
2272 ts.Env()->monitors.Remove(o);
2273 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002274 }
2275
2276 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2277 ScopedJniThreadState ts(env);
2278 Runtime* runtime = Runtime::Current();
2279 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002280 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 } else {
2282 *vm = NULL;
2283 }
2284 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2285 }
2286
Elliott Hughescdf53122011-08-19 15:46:09 -07002287 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2288 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002289
2290 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002291 CHECK(address != NULL); // TODO: ReportJniError
2292 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002293
2294 jclass buffer_class = GetDirectByteBufferClass(env);
2295 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2296 if (mid == NULL) {
2297 return NULL;
2298 }
2299
2300 // At the moment, the Java side is limited to 32 bits.
2301 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2302 CHECK_LE(capacity, 0xffffffff);
2303 jint address_arg = reinterpret_cast<jint>(address);
2304 jint capacity_arg = static_cast<jint>(capacity);
2305
2306 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2307 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 }
2309
Elliott Hughesb465ab02011-08-24 11:21:21 -07002310 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002312 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2313 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002314 }
2315
Elliott Hughesb465ab02011-08-24 11:21:21 -07002316 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002317 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002318 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2319 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002320 }
2321
Elliott Hughesb465ab02011-08-24 11:21:21 -07002322 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002323 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002324
Elliott Hughes75770752011-08-24 17:52:38 -07002325 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002326
2327 // Do we definitely know what kind of reference this is?
2328 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2329 IndirectRefKind kind = GetIndirectRefKind(ref);
2330 switch (kind) {
2331 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002332 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2333 return JNILocalRefType;
2334 }
2335 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002336 case kGlobal:
2337 return JNIGlobalRefType;
2338 case kWeakGlobal:
2339 return JNIWeakGlobalRefType;
2340 case kSirtOrInvalid:
2341 // Is it in a stack IRT?
TDYa12728f1a142012-03-15 21:51:52 -07002342 if (ts.Self()->StackReferencesContain(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002343 return JNILocalRefType;
2344 }
2345
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002346 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002347 return JNIInvalidRefType;
2348 }
2349
Elliott Hughesb465ab02011-08-24 11:21:21 -07002350 // If we're handing out direct pointers, check whether it's a direct pointer
2351 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002352 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002353 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002354 return JNILocalRefType;
2355 }
2356 }
2357
2358 return JNIInvalidRefType;
2359 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002360 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2361 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002362 }
2363};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002364
Elliott Hughes88c5c352012-03-15 18:49:48 -07002365const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002366 NULL, // reserved0.
2367 NULL, // reserved1.
2368 NULL, // reserved2.
2369 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002370 JNI::GetVersion,
2371 JNI::DefineClass,
2372 JNI::FindClass,
2373 JNI::FromReflectedMethod,
2374 JNI::FromReflectedField,
2375 JNI::ToReflectedMethod,
2376 JNI::GetSuperclass,
2377 JNI::IsAssignableFrom,
2378 JNI::ToReflectedField,
2379 JNI::Throw,
2380 JNI::ThrowNew,
2381 JNI::ExceptionOccurred,
2382 JNI::ExceptionDescribe,
2383 JNI::ExceptionClear,
2384 JNI::FatalError,
2385 JNI::PushLocalFrame,
2386 JNI::PopLocalFrame,
2387 JNI::NewGlobalRef,
2388 JNI::DeleteGlobalRef,
2389 JNI::DeleteLocalRef,
2390 JNI::IsSameObject,
2391 JNI::NewLocalRef,
2392 JNI::EnsureLocalCapacity,
2393 JNI::AllocObject,
2394 JNI::NewObject,
2395 JNI::NewObjectV,
2396 JNI::NewObjectA,
2397 JNI::GetObjectClass,
2398 JNI::IsInstanceOf,
2399 JNI::GetMethodID,
2400 JNI::CallObjectMethod,
2401 JNI::CallObjectMethodV,
2402 JNI::CallObjectMethodA,
2403 JNI::CallBooleanMethod,
2404 JNI::CallBooleanMethodV,
2405 JNI::CallBooleanMethodA,
2406 JNI::CallByteMethod,
2407 JNI::CallByteMethodV,
2408 JNI::CallByteMethodA,
2409 JNI::CallCharMethod,
2410 JNI::CallCharMethodV,
2411 JNI::CallCharMethodA,
2412 JNI::CallShortMethod,
2413 JNI::CallShortMethodV,
2414 JNI::CallShortMethodA,
2415 JNI::CallIntMethod,
2416 JNI::CallIntMethodV,
2417 JNI::CallIntMethodA,
2418 JNI::CallLongMethod,
2419 JNI::CallLongMethodV,
2420 JNI::CallLongMethodA,
2421 JNI::CallFloatMethod,
2422 JNI::CallFloatMethodV,
2423 JNI::CallFloatMethodA,
2424 JNI::CallDoubleMethod,
2425 JNI::CallDoubleMethodV,
2426 JNI::CallDoubleMethodA,
2427 JNI::CallVoidMethod,
2428 JNI::CallVoidMethodV,
2429 JNI::CallVoidMethodA,
2430 JNI::CallNonvirtualObjectMethod,
2431 JNI::CallNonvirtualObjectMethodV,
2432 JNI::CallNonvirtualObjectMethodA,
2433 JNI::CallNonvirtualBooleanMethod,
2434 JNI::CallNonvirtualBooleanMethodV,
2435 JNI::CallNonvirtualBooleanMethodA,
2436 JNI::CallNonvirtualByteMethod,
2437 JNI::CallNonvirtualByteMethodV,
2438 JNI::CallNonvirtualByteMethodA,
2439 JNI::CallNonvirtualCharMethod,
2440 JNI::CallNonvirtualCharMethodV,
2441 JNI::CallNonvirtualCharMethodA,
2442 JNI::CallNonvirtualShortMethod,
2443 JNI::CallNonvirtualShortMethodV,
2444 JNI::CallNonvirtualShortMethodA,
2445 JNI::CallNonvirtualIntMethod,
2446 JNI::CallNonvirtualIntMethodV,
2447 JNI::CallNonvirtualIntMethodA,
2448 JNI::CallNonvirtualLongMethod,
2449 JNI::CallNonvirtualLongMethodV,
2450 JNI::CallNonvirtualLongMethodA,
2451 JNI::CallNonvirtualFloatMethod,
2452 JNI::CallNonvirtualFloatMethodV,
2453 JNI::CallNonvirtualFloatMethodA,
2454 JNI::CallNonvirtualDoubleMethod,
2455 JNI::CallNonvirtualDoubleMethodV,
2456 JNI::CallNonvirtualDoubleMethodA,
2457 JNI::CallNonvirtualVoidMethod,
2458 JNI::CallNonvirtualVoidMethodV,
2459 JNI::CallNonvirtualVoidMethodA,
2460 JNI::GetFieldID,
2461 JNI::GetObjectField,
2462 JNI::GetBooleanField,
2463 JNI::GetByteField,
2464 JNI::GetCharField,
2465 JNI::GetShortField,
2466 JNI::GetIntField,
2467 JNI::GetLongField,
2468 JNI::GetFloatField,
2469 JNI::GetDoubleField,
2470 JNI::SetObjectField,
2471 JNI::SetBooleanField,
2472 JNI::SetByteField,
2473 JNI::SetCharField,
2474 JNI::SetShortField,
2475 JNI::SetIntField,
2476 JNI::SetLongField,
2477 JNI::SetFloatField,
2478 JNI::SetDoubleField,
2479 JNI::GetStaticMethodID,
2480 JNI::CallStaticObjectMethod,
2481 JNI::CallStaticObjectMethodV,
2482 JNI::CallStaticObjectMethodA,
2483 JNI::CallStaticBooleanMethod,
2484 JNI::CallStaticBooleanMethodV,
2485 JNI::CallStaticBooleanMethodA,
2486 JNI::CallStaticByteMethod,
2487 JNI::CallStaticByteMethodV,
2488 JNI::CallStaticByteMethodA,
2489 JNI::CallStaticCharMethod,
2490 JNI::CallStaticCharMethodV,
2491 JNI::CallStaticCharMethodA,
2492 JNI::CallStaticShortMethod,
2493 JNI::CallStaticShortMethodV,
2494 JNI::CallStaticShortMethodA,
2495 JNI::CallStaticIntMethod,
2496 JNI::CallStaticIntMethodV,
2497 JNI::CallStaticIntMethodA,
2498 JNI::CallStaticLongMethod,
2499 JNI::CallStaticLongMethodV,
2500 JNI::CallStaticLongMethodA,
2501 JNI::CallStaticFloatMethod,
2502 JNI::CallStaticFloatMethodV,
2503 JNI::CallStaticFloatMethodA,
2504 JNI::CallStaticDoubleMethod,
2505 JNI::CallStaticDoubleMethodV,
2506 JNI::CallStaticDoubleMethodA,
2507 JNI::CallStaticVoidMethod,
2508 JNI::CallStaticVoidMethodV,
2509 JNI::CallStaticVoidMethodA,
2510 JNI::GetStaticFieldID,
2511 JNI::GetStaticObjectField,
2512 JNI::GetStaticBooleanField,
2513 JNI::GetStaticByteField,
2514 JNI::GetStaticCharField,
2515 JNI::GetStaticShortField,
2516 JNI::GetStaticIntField,
2517 JNI::GetStaticLongField,
2518 JNI::GetStaticFloatField,
2519 JNI::GetStaticDoubleField,
2520 JNI::SetStaticObjectField,
2521 JNI::SetStaticBooleanField,
2522 JNI::SetStaticByteField,
2523 JNI::SetStaticCharField,
2524 JNI::SetStaticShortField,
2525 JNI::SetStaticIntField,
2526 JNI::SetStaticLongField,
2527 JNI::SetStaticFloatField,
2528 JNI::SetStaticDoubleField,
2529 JNI::NewString,
2530 JNI::GetStringLength,
2531 JNI::GetStringChars,
2532 JNI::ReleaseStringChars,
2533 JNI::NewStringUTF,
2534 JNI::GetStringUTFLength,
2535 JNI::GetStringUTFChars,
2536 JNI::ReleaseStringUTFChars,
2537 JNI::GetArrayLength,
2538 JNI::NewObjectArray,
2539 JNI::GetObjectArrayElement,
2540 JNI::SetObjectArrayElement,
2541 JNI::NewBooleanArray,
2542 JNI::NewByteArray,
2543 JNI::NewCharArray,
2544 JNI::NewShortArray,
2545 JNI::NewIntArray,
2546 JNI::NewLongArray,
2547 JNI::NewFloatArray,
2548 JNI::NewDoubleArray,
2549 JNI::GetBooleanArrayElements,
2550 JNI::GetByteArrayElements,
2551 JNI::GetCharArrayElements,
2552 JNI::GetShortArrayElements,
2553 JNI::GetIntArrayElements,
2554 JNI::GetLongArrayElements,
2555 JNI::GetFloatArrayElements,
2556 JNI::GetDoubleArrayElements,
2557 JNI::ReleaseBooleanArrayElements,
2558 JNI::ReleaseByteArrayElements,
2559 JNI::ReleaseCharArrayElements,
2560 JNI::ReleaseShortArrayElements,
2561 JNI::ReleaseIntArrayElements,
2562 JNI::ReleaseLongArrayElements,
2563 JNI::ReleaseFloatArrayElements,
2564 JNI::ReleaseDoubleArrayElements,
2565 JNI::GetBooleanArrayRegion,
2566 JNI::GetByteArrayRegion,
2567 JNI::GetCharArrayRegion,
2568 JNI::GetShortArrayRegion,
2569 JNI::GetIntArrayRegion,
2570 JNI::GetLongArrayRegion,
2571 JNI::GetFloatArrayRegion,
2572 JNI::GetDoubleArrayRegion,
2573 JNI::SetBooleanArrayRegion,
2574 JNI::SetByteArrayRegion,
2575 JNI::SetCharArrayRegion,
2576 JNI::SetShortArrayRegion,
2577 JNI::SetIntArrayRegion,
2578 JNI::SetLongArrayRegion,
2579 JNI::SetFloatArrayRegion,
2580 JNI::SetDoubleArrayRegion,
2581 JNI::RegisterNatives,
2582 JNI::UnregisterNatives,
2583 JNI::MonitorEnter,
2584 JNI::MonitorExit,
2585 JNI::GetJavaVM,
2586 JNI::GetStringRegion,
2587 JNI::GetStringUTFRegion,
2588 JNI::GetPrimitiveArrayCritical,
2589 JNI::ReleasePrimitiveArrayCritical,
2590 JNI::GetStringCritical,
2591 JNI::ReleaseStringCritical,
2592 JNI::NewWeakGlobalRef,
2593 JNI::DeleteWeakGlobalRef,
2594 JNI::ExceptionCheck,
2595 JNI::NewDirectByteBuffer,
2596 JNI::GetDirectBufferAddress,
2597 JNI::GetDirectBufferCapacity,
2598 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002599};
2600
Elliott Hughes75770752011-08-24 17:52:38 -07002601JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002602 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002603 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002604 local_ref_cookie(IRT_FIRST_SEGMENT),
2605 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002606 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002607 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002608 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002609 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002610 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002611 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002612 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002613 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2614 // errors will ensue.
2615 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2616 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002617}
2618
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002619JNIEnvExt::~JNIEnvExt() {
2620}
2621
Elliott Hughes88c5c352012-03-15 18:49:48 -07002622void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2623 check_jni = enabled;
2624 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002625}
2626
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002627void JNIEnvExt::DumpReferenceTables() {
2628 locals.Dump();
2629 monitors.Dump();
2630}
2631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002632void JNIEnvExt::PushFrame(int /*capacity*/) {
2633 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002634 stacked_local_ref_cookies.push_back(local_ref_cookie);
2635 local_ref_cookie = locals.GetSegmentState();
2636}
2637
2638void JNIEnvExt::PopFrame() {
2639 locals.SetSegmentState(local_ref_cookie);
2640 local_ref_cookie = stacked_local_ref_cookies.back();
2641 stacked_local_ref_cookies.pop_back();
2642}
2643
Carl Shapiroea4dca82011-08-01 13:45:38 -07002644// JNI Invocation interface.
2645
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002646extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2647 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2648 if (args->version < JNI_VERSION_1_2) {
2649 return JNI_EVERSION;
2650 }
2651 Runtime::Options options;
2652 for (int i = 0; i < args->nOptions; ++i) {
2653 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002654 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002655 }
2656 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002657 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002658 if (runtime == NULL) {
2659 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002660 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002661 runtime->Start();
2662 *p_env = Thread::Current()->GetJniEnv();
2663 *p_vm = runtime->GetJavaVM();
2664 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002665}
2666
Elliott Hughesf2682d52011-08-15 16:37:04 -07002667extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002668 Runtime* runtime = Runtime::Current();
2669 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002670 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002671 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002672 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002673 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002674 }
2675 return JNI_OK;
2676}
2677
2678// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002679extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002680 return JNI_ERR;
2681}
2682
Elliott Hughescdf53122011-08-19 15:46:09 -07002683class JII {
2684 public:
2685 static jint DestroyJavaVM(JavaVM* vm) {
2686 if (vm == NULL) {
2687 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002688 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002689 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2690 delete raw_vm->runtime;
2691 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002692 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002693
Elliott Hughescdf53122011-08-19 15:46:09 -07002694 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002695 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002696 }
2697
2698 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002699 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002700 }
2701
2702 static jint DetachCurrentThread(JavaVM* vm) {
2703 if (vm == NULL) {
2704 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002705 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002706 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2707 Runtime* runtime = raw_vm->runtime;
2708 runtime->DetachCurrentThread();
2709 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002710 }
2711
2712 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2713 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2714 return JNI_EVERSION;
2715 }
2716 if (vm == NULL || env == NULL) {
2717 return JNI_ERR;
2718 }
2719 Thread* thread = Thread::Current();
2720 if (thread == NULL) {
2721 *env = NULL;
2722 return JNI_EDETACHED;
2723 }
2724 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002725 return JNI_OK;
2726 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002727};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002728
Elliott Hughes88c5c352012-03-15 18:49:48 -07002729const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002730 NULL, // reserved0
2731 NULL, // reserved1
2732 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002733 JII::DestroyJavaVM,
2734 JII::AttachCurrentThread,
2735 JII::DetachCurrentThread,
2736 JII::GetEnv,
2737 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002738};
2739
Elliott Hughesa0957642011-09-02 14:27:33 -07002740JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002741 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002742 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002743 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002744 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002745 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002746 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002747 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002748 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002749 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002750 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002751 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002752 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002753 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002754 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002755 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002756 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002757 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002758 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002759 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002760}
2761
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002762JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002763 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002764}
2765
Elliott Hughes88c5c352012-03-15 18:49:48 -07002766void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2767 check_jni = enabled;
2768 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002769}
2770
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002771void JavaVMExt::DumpReferenceTables() {
2772 {
2773 MutexLock mu(globals_lock);
2774 globals.Dump();
2775 }
2776 {
2777 MutexLock mu(weak_globals_lock);
2778 weak_globals.Dump();
2779 }
2780 {
2781 MutexLock mu(pins_lock);
2782 pin_table.Dump();
2783 }
2784}
2785
Elliott Hughes75770752011-08-24 17:52:38 -07002786bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2787 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002788
2789 // See if we've already loaded this library. If we have, and the class loader
2790 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002791 // TODO: for better results we should canonicalize the pathname (or even compare
2792 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002793 SharedLibrary* library;
2794 {
2795 // TODO: move the locking (and more of this logic) into Libraries.
2796 MutexLock mu(libraries_lock);
2797 library = libraries->Get(path);
2798 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002799 if (library != NULL) {
2800 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002801 // The library will be associated with class_loader. The JNI
2802 // spec says we can't load the same library into more than one
2803 // class loader.
2804 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2805 "ClassLoader %p; can't open in ClassLoader %p",
2806 path.c_str(), library->GetClassLoader(), class_loader);
2807 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002808 return false;
2809 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002810 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2811 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002812 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002813 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2814 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002815 return false;
2816 }
2817 return true;
2818 }
2819
2820 // Open the shared library. Because we're using a full path, the system
2821 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2822 // resolve this library's dependencies though.)
2823
2824 // Failures here are expected when java.library.path has several entries
2825 // and we have to hunt for the lib.
2826
2827 // The current version of the dynamic linker prints detailed information
2828 // about dlopen() failures. Some things to check if the message is
2829 // cryptic:
2830 // - make sure the library exists on the device
2831 // - verify that the right path is being opened (the debug log message
2832 // above can help with that)
2833 // - check to see if the library is valid (e.g. not zero bytes long)
2834 // - check config/prelink-linux-arm.map to ensure that the library
2835 // is listed and is not being overrun by the previous entry (if
2836 // loading suddenly stops working on a prelinked library, this is
2837 // a good one to check)
2838 // - write a trivial app that calls sleep() then dlopen(), attach
2839 // to it with "strace -p <pid>" while it sleeps, and watch for
2840 // attempts to open nonexistent dependent shared libs
2841
2842 // TODO: automate some of these checks!
2843
2844 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002845 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002846 // the GC to ignore us.
2847 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002848 void* handle = NULL;
2849 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002850 ScopedThreadStateChange tsc(self, kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002851 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002852 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002853
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002854 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002855
2856 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002857 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002858 return false;
2859 }
2860
2861 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002862 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002863 // TODO: move the locking (and more of this logic) into Libraries.
2864 MutexLock mu(libraries_lock);
2865 library = libraries->Get(path);
2866 if (library != NULL) {
2867 LOG(INFO) << "WOW: we lost a race to add shared library: "
2868 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002869 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002870 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002871 library = new SharedLibrary(path, handle, class_loader);
2872 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002873 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002874
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002875 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002876
2877 bool result = true;
2878 void* sym = dlsym(handle, "JNI_OnLoad");
2879 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002880 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002881 } else {
2882 // Call JNI_OnLoad. We have to override the current class
2883 // loader, which will always be "null" since the stuff at the
2884 // top of the stack is around Runtime.loadLibrary(). (See
2885 // the comments in the JNI FindClass function.)
2886 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2887 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002888 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002889 self->SetClassLoaderOverride(class_loader);
2890
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002891 int version = 0;
2892 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002893 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002894 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002895 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002896 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002897
Brian Carlstromaded5f72011-10-07 17:15:04 -07002898 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002899
2900 if (version != JNI_VERSION_1_2 &&
2901 version != JNI_VERSION_1_4 &&
2902 version != JNI_VERSION_1_6) {
2903 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2904 << "bad version: " << version;
2905 // It's unwise to call dlclose() here, but we can mark it
2906 // as bad and ensure that future load attempts will fail.
2907 // We don't know how far JNI_OnLoad got, so there could
2908 // be some partially-initialized stuff accessible through
2909 // newly-registered native method calls. We could try to
2910 // unregister them, but that doesn't seem worthwhile.
2911 result = false;
2912 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002913 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2914 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002915 }
2916 }
2917
2918 library->SetResult(result);
2919 return result;
2920}
2921
2922void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2923 CHECK(m->IsNative());
2924
2925 Class* c = m->GetDeclaringClass();
2926
2927 // If this is a static method, it could be called before the class
2928 // has been initialized.
2929 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002930 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002931 return NULL;
2932 }
2933 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002934 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002935 }
2936
Brian Carlstrom16192862011-09-12 17:50:06 -07002937 std::string detail;
2938 void* native_method;
2939 {
2940 MutexLock mu(libraries_lock);
2941 native_method = libraries->FindNativeMethod(m, detail);
2942 }
2943 // throwing can cause libraries_lock to be reacquired
2944 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002945 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002946 }
2947 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002948}
2949
Elliott Hughes410c0c82011-09-01 17:58:25 -07002950void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2951 {
2952 MutexLock mu(globals_lock);
2953 globals.VisitRoots(visitor, arg);
2954 }
2955 {
2956 MutexLock mu(pins_lock);
2957 pin_table.VisitRoots(visitor, arg);
2958 }
2959 // The weak_globals table is visited by the GC itself (because it mutates the table).
2960}
2961
Elliott Hughes844f9a02012-01-24 20:19:58 -08002962jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
2963 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2964 if (c.get() == NULL) {
2965 return NULL;
2966 }
2967 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
2968}
2969
Ian Rogersdf20fe02011-07-20 20:34:16 -07002970} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002971
2972std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2973 switch (rhs) {
2974 case JNIInvalidRefType:
2975 os << "JNIInvalidRefType";
2976 return os;
2977 case JNILocalRefType:
2978 os << "JNILocalRefType";
2979 return os;
2980 case JNIGlobalRefType:
2981 os << "JNIGlobalRefType";
2982 return os;
2983 case JNIWeakGlobalRefType:
2984 os << "JNIWeakGlobalRefType";
2985 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002986 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002987 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002988 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002989 }
2990}