blob: 1e2760453f2760d9b2f43821ca33322c42ced627 [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>
Elliott Hughes0af55432011-08-17 18:37:28 -070023#include <utility>
24#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025
Elliott Hughes40ef99e2011-08-11 17:44:34 -070026#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070028#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070030#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070032#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070033#include "safe_map.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070034#include "scoped_jni_thread_state.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070035#include "ScopedLocalRef.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070036#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070037#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070038#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070039#include "UniquePtr.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':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700190 arg_array_[offset].SetZ(va_arg(ap, jint));
191 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800192 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700193 arg_array_[offset].SetB(va_arg(ap, jint));
194 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800195 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700196 arg_array_[offset].SetC(va_arg(ap, jint));
197 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800198 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700199 arg_array_[offset].SetS(va_arg(ap, jint));
200 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800201 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700202 arg_array_[offset].SetI(va_arg(ap, jint));
Ian Rogers45619fc2012-02-29 11:15:25 -0800203 break;
204 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700205 arg_array_[offset].SetF(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800206 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700207 case 'L':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700208 arg_array_[offset].SetL(DecodeObj(env, va_arg(ap, jobject)));
Ian Rogers45619fc2012-02-29 11:15:25 -0800209 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800210 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700211 arg_array_[offset].SetD(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800212 break;
213 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700214 arg_array_[offset].SetJ(va_arg(ap, jlong));
Ian Rogers45619fc2012-02-29 11:15:25 -0800215 break;
216 }
217 }
218 }
219
220 void BuildArgArray(JNIEnv* public_env, jvalue* args) {
221 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700222 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800223 switch (shorty_[i]) {
224 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700225 arg_array_[offset].SetZ(args[offset].z);
226 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800227 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700228 arg_array_[offset].SetB(args[offset].b);
229 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800230 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700231 arg_array_[offset].SetC(args[offset].c);
232 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800233 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700234 arg_array_[offset].SetS(args[offset].s);
235 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800236 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700237 arg_array_[offset].SetI(args[offset].i);
Ian Rogers45619fc2012-02-29 11:15:25 -0800238 break;
239 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700240 arg_array_[offset].SetF(args[offset].f);
Ian Rogers45619fc2012-02-29 11:15:25 -0800241 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700242 case 'L':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700243 arg_array_[offset].SetL(DecodeObj(env, args[offset].l));
Ian Rogers45619fc2012-02-29 11:15:25 -0800244 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800245 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700246 arg_array_[offset].SetD(args[offset].d);
Ian Rogers45619fc2012-02-29 11:15:25 -0800247 break;
248 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700249 arg_array_[offset].SetJ(args[offset].j);
Ian Rogers45619fc2012-02-29 11:15:25 -0800250 break;
251 }
252 }
253 }
254
Ian Rogers45619fc2012-02-29 11:15:25 -0800255 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700256 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800257 const char* shorty_;
258 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700259 JValue* arg_array_;
260 JValue small_arg_array_[kSmallArgArraySize];
261 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800262};
263
Elliott Hughes0512f022012-03-15 22:10:52 -0700264static jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700265 if (obj == NULL) {
266 return NULL;
267 }
Elliott Hughes75770752011-08-24 17:52:38 -0700268 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700269 IndirectReferenceTable& weak_globals = vm->weak_globals;
270 MutexLock mu(vm->weak_globals_lock);
271 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
272 return reinterpret_cast<jweak>(ref);
273}
274
Elliott Hughesbf86d042011-08-31 17:53:14 -0700275// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700276template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -0700277static T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700278 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700279}
280
Elliott Hughesb264f082012-04-06 17:10:10 -0700281static void CheckMethodArguments(Method* m, JValue* args) {
282 MethodHelper mh(m);
283 ObjectArray<Class>* parameter_types = mh.GetParameterTypes();
284 CHECK(parameter_types != NULL);
285 size_t error_count = 0;
286 for (int i = 0; i < parameter_types->GetLength(); ++i) {
287 Class* parameter_type = parameter_types->Get(i);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700288 // TODO: check primitives are in range.
Elliott Hughesb264f082012-04-06 17:10:10 -0700289 if (!parameter_type->IsPrimitive()) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700290 Object* argument = args[i].GetL();
Elliott Hughesb264f082012-04-06 17:10:10 -0700291 if (argument != NULL && !argument->InstanceOf(parameter_type)) {
292 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
293 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
294 ++error_count;
295 }
296 }
297 }
298 if (error_count > 0) {
299 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
300 // with an argument.
301 JniAbort(NULL);
302 }
303}
Elliott Hughesb264f082012-04-06 17:10:10 -0700304
Elliott Hughes77405792012-03-15 15:22:12 -0700305static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, JValue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700306 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700307 if (UNLIKELY(env->check_jni)) {
308 CheckMethodArguments(method, args);
309 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700310 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700311 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700312 return result;
313}
314
Ian Rogers0571d352011-11-03 19:51:38 -0700315static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list 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 = 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 Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700325 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700326}
327
Ian Rogers0571d352011-11-03 19:51:38 -0700328static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
329 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700330 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800331 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700332 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800333 ArgArray arg_array(method);
334 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700335 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700336}
337
Ian Rogers0571d352011-11-03 19:51:38 -0700338static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
339 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700340 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800341 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700342 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800343 ArgArray arg_array(method);
344 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700345 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700346}
347
Elliott Hughes6b436852011-08-12 10:16:44 -0700348// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
349// separated with slashes but aren't wrapped with "L;" like regular descriptors
350// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
351// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
352// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700353static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700354 std::string result;
355 // Add the missing "L;" if necessary.
356 if (name[0] == '[') {
357 result = name;
358 } else {
359 result += 'L';
360 result += name;
361 result += ';';
362 }
363 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700364 if (result.find('.') != std::string::npos) {
365 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
366 << "\"" << name << "\"";
367 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700368 }
369 return result;
370}
371
Ian Rogers0571d352011-11-03 19:51:38 -0700372static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700373 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800374 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700375}
376
Ian Rogers0571d352011-11-03 19:51:38 -0700377static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700378 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700379 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700380 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700381 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700382
383 Method* method = NULL;
384 if (is_static) {
385 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700386 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700387 method = c->FindVirtualMethod(name, sig);
388 if (method == NULL) {
389 // No virtual method matching the signature. Search declared
390 // private methods and constructors.
391 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700392 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700393 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700394
Elliott Hughescdf53122011-08-19 15:46:09 -0700395 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700396 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700397 return NULL;
398 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700399
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700400 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700401}
402
Ian Rogers0571d352011-11-03 19:51:38 -0700403static const ClassLoader* GetClassLoader(Thread* self) {
Elliott Hughes6a144332012-04-03 13:07:11 -0700404 Method* method = self->GetCurrentMethod();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700405 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
406 return self->GetClassLoaderOverride();
407 }
408 return method->GetDeclaringClass()->GetClassLoader();
409}
410
Ian Rogers0571d352011-11-03 19:51:38 -0700411static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700412 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700413 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700414 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700415 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700416
417 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700418 Class* field_type;
419 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
420 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700421 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700423 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700424 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700425 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700426 if (field_type == NULL) {
427 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700428 DCHECK(ts.Self()->IsExceptionPending());
429 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700430 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700431 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800432 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700433 return NULL;
434 }
435 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800436 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700437 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800438 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700439 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700440 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700441 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700442 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800443 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700444 return NULL;
445 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700446 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700447}
448
Ian Rogers0571d352011-11-03 19:51:38 -0700449static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700450 JavaVMExt* vm = ts.Vm();
451 MutexLock mu(vm->pins_lock);
452 vm->pin_table.Add(array);
453}
454
Ian Rogers0571d352011-11-03 19:51:38 -0700455static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700456 JavaVMExt* vm = ts.Vm();
457 MutexLock mu(vm->pins_lock);
458 vm->pin_table.Remove(array);
459}
460
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700461template<typename JniT, typename ArtT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700462static JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700463 CHECK_GE(length, 0); // TODO: ReportJniError
464 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700465 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700466}
467
Elliott Hughes75770752011-08-24 17:52:38 -0700468template <typename ArrayT, typename CArrayT, typename ArtArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700469static CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -0700470 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
471 PinPrimitiveArray(ts, array);
472 if (is_copy != NULL) {
473 *is_copy = JNI_FALSE;
474 }
475 return array->GetData();
476}
477
478template <typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700479static void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
Elliott Hughes75770752011-08-24 17:52:38 -0700480 if (mode != JNI_COMMIT) {
481 Array* array = Decode<Array*>(ts, java_array);
482 UnpinPrimitiveArray(ts, array);
483 }
484}
485
Ian Rogers0571d352011-11-03 19:51:38 -0700486static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700487 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700488 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700489 "%s offset=%d length=%d %s.length=%d",
490 type.c_str(), start, length, identifier, array->GetLength());
491}
Ian Rogers0571d352011-11-03 19:51:38 -0700492
493static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700494 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700495 "offset=%d length=%d string.length()=%d", start, length, array_length);
496}
Elliott Hughes814e4032011-08-23 12:07:56 -0700497
498template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700499static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700500 ArrayT* array = Decode<ArrayT*>(ts, java_array);
501 if (start < 0 || length < 0 || start + length > array->GetLength()) {
502 ThrowAIOOBE(ts, array, start, length, "src");
503 } else {
504 JavaT* data = array->GetData();
505 memcpy(buf, data + start, length * sizeof(JavaT));
506 }
507}
508
509template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700510static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700511 ArrayT* array = Decode<ArrayT*>(ts, java_array);
512 if (start < 0 || length < 0 || start + length > array->GetLength()) {
513 ThrowAIOOBE(ts, array, start, length, "dst");
514 } else {
515 JavaT* data = array->GetData();
516 memcpy(data + start, buf, length * sizeof(JavaT));
517 }
518}
519
Ian Rogers0571d352011-11-03 19:51:38 -0700520static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700521 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
522 CHECK(buffer_class.get() != NULL);
523 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
524}
525
Ian Rogers0571d352011-11-03 19:51:38 -0700526static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700527 static jclass buffer_class = InitDirectByteBufferClass(env);
528 return buffer_class;
529}
530
Elliott Hughes462c9442012-03-23 18:47:50 -0700531static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700532 if (vm == NULL || p_env == NULL) {
533 return JNI_ERR;
534 }
535
Elliott Hughes462c9442012-03-23 18:47:50 -0700536 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700537 Thread* self = Thread::Current();
538 if (self != NULL) {
539 *p_env = self->GetJniEnv();
540 return JNI_OK;
541 }
542
543 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
544
545 // No threads allowed in zygote mode.
546 if (runtime->IsZygote()) {
547 LOG(ERROR) << "Attempt to attach a thread in the zygote";
548 return JNI_ERR;
549 }
550
Elliott Hughes462c9442012-03-23 18:47:50 -0700551 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
552 const char* thread_name = NULL;
553 Object* thread_group = NULL;
554 if (args != NULL) {
555 CHECK_GE(args->version, JNI_VERSION_1_2);
556 thread_name = args->name;
557 thread_group = static_cast<Thread*>(NULL)->DecodeJObject(args->group);
Elliott Hughes75770752011-08-24 17:52:38 -0700558 }
Elliott Hughes75770752011-08-24 17:52:38 -0700559
Elliott Hughes462c9442012-03-23 18:47:50 -0700560 runtime->AttachCurrentThread(thread_name, as_daemon, thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700561 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700562 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700563}
564
Elliott Hughes79082e32011-08-25 12:07:32 -0700565class SharedLibrary {
566 public:
567 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
568 : path_(path),
569 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700570 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700571 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughese62934d2012-04-09 11:24:29 -0700572 jni_on_load_cond_("JNI_OnLoad condition variable"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700573 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700574 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700575 }
576
Elliott Hughes79082e32011-08-25 12:07:32 -0700577 Object* GetClassLoader() {
578 return class_loader_;
579 }
580
581 std::string GetPath() {
582 return path_;
583 }
584
585 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700586 * Check the result of an earlier call to JNI_OnLoad on this library.
587 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700588 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700589 bool CheckOnLoadResult() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700591 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700592 // Check this so we don't end up waiting for ourselves. We need
593 // to return "true" so the caller can continue.
594 LOG(INFO) << *self << " recursive attempt to load library "
595 << "\"" << path_ << "\"";
596 return true;
597 }
598
599 MutexLock mu(jni_on_load_lock_);
600 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800601 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
602 << "JNI_OnLoad...]";
Elliott Hughes34e06962012-04-09 13:55:55 -0700603 ScopedThreadStateChange tsc(self, kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700604 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700605 }
606
607 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800608 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
609 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700610 return okay;
611 }
612
613 void SetResult(bool result) {
614 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700615 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700616
617 // Broadcast a wakeup to anybody sleeping on the condition variable.
618 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700619 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700620 }
621
622 void* FindSymbol(const std::string& symbol_name) {
623 return dlsym(handle_, symbol_name.c_str());
624 }
625
626 private:
627 enum JNI_OnLoadState {
628 kPending,
629 kFailed,
630 kOkay,
631 };
632
633 // Path to library "/system/lib/libjni.so".
634 std::string path_;
635
636 // The void* returned by dlopen(3).
637 void* handle_;
638
639 // The ClassLoader this library is associated with.
640 Object* class_loader_;
641
642 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700643 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700644 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700645 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700646 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700647 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700648 // Result of earlier JNI_OnLoad call.
649 JNI_OnLoadState jni_on_load_result_;
650};
651
Elliott Hughes79082e32011-08-25 12:07:32 -0700652// This exists mainly to keep implementation details out of the header file.
653class Libraries {
654 public:
655 Libraries() {
656 }
657
658 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700659 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700660 }
661
662 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700663 It it = libraries_.find(path);
664 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700665 }
666
667 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700668 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700669 }
670
671 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700672 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700673 std::string jni_short_name(JniShortName(m));
674 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700675 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700676 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
677 SharedLibrary* library = it->second;
678 if (library->GetClassLoader() != declaring_class_loader) {
679 // We only search libraries loaded by the appropriate ClassLoader.
680 continue;
681 }
682 // Try the short name then the long name...
683 void* fn = library->FindSymbol(jni_short_name);
684 if (fn == NULL) {
685 fn = library->FindSymbol(jni_long_name);
686 }
687 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800688 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
689 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700690 return fn;
691 }
692 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700693 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700694 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700695 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700696 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700697 return NULL;
698 }
699
700 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700701 typedef SafeMap<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700702
Elliott Hughesa0e18062012-04-13 15:59:59 -0700703 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700704};
705
Elliott Hughes418d20f2011-09-22 14:00:39 -0700706JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
707 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
708 Object* receiver = Decode<Object*>(env, obj);
709 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800710 ArgArray arg_array(method);
711 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700712 return InvokeWithArgArray(env, receiver, method, arg_array.get());
713}
714
Elliott Hughesd07986f2011-12-06 18:27:45 -0800715JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700716 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800717}
718
Elliott Hughescdf53122011-08-19 15:46:09 -0700719class JNI {
720 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700721
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 static jint GetVersion(JNIEnv* env) {
723 ScopedJniThreadState ts(env);
724 return JNI_VERSION_1_6;
725 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700726
Elliott Hughescdf53122011-08-19 15:46:09 -0700727 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
728 ScopedJniThreadState ts(env);
729 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700730 return NULL;
731 }
732
Elliott Hughescdf53122011-08-19 15:46:09 -0700733 static jclass FindClass(JNIEnv* env, const char* name) {
734 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700735 Runtime* runtime = Runtime::Current();
736 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700737 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700738 Class* c = NULL;
739 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700740 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800741 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700742 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800743 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700744 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700745 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700746 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700747
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
749 ScopedJniThreadState ts(env);
750 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700751 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700752 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700753
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
755 ScopedJniThreadState ts(env);
756 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700757 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700759
Elliott Hughescdf53122011-08-19 15:46:09 -0700760 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
761 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700762 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700763 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700764 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700765
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
767 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700768 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700769 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700770 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700771
Elliott Hughes37f7a402011-08-22 18:56:01 -0700772 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
773 ScopedJniThreadState ts(env);
774 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700775 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700776 }
777
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700778 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700780 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700781 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700783
Elliott Hughes37f7a402011-08-22 18:56:01 -0700784 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700786 Class* c1 = Decode<Class*>(ts, java_class1);
787 Class* c2 = Decode<Class*>(ts, java_class2);
788 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700789 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700790
Elliott Hughese84278b2012-03-22 10:06:53 -0700791 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700792 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700793 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700794 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700795 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700796 return JNI_TRUE;
797 } else {
798 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700799 Class* c = Decode<Class*>(ts, java_class);
800 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700801 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700802 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700803
Elliott Hughes37f7a402011-08-22 18:56:01 -0700804 static jint Throw(JNIEnv* env, jthrowable java_exception) {
805 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700806 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700807 if (exception == NULL) {
808 return JNI_ERR;
809 }
810 ts.Self()->SetException(exception);
811 return JNI_OK;
812 }
813
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700814 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700815 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700816 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700817 jmethodID mid = ((msg != NULL)
818 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
819 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700820 if (mid == NULL) {
821 return JNI_ERR;
822 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700823 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700824 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700825 return JNI_ERR;
826 }
827
828 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700829 args[0].l = s.get();
830 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
831 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700832 return JNI_ERR;
833 }
834
Elliott Hughes72025e52011-08-23 17:50:30 -0700835 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700836
Elliott Hughes37f7a402011-08-22 18:56:01 -0700837 return JNI_OK;
838 }
839
840 static jboolean ExceptionCheck(JNIEnv* env) {
841 ScopedJniThreadState ts(env);
842 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
843 }
844
845 static void ExceptionClear(JNIEnv* env) {
846 ScopedJniThreadState ts(env);
847 ts.Self()->ClearException();
848 }
849
850 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700851 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700852
853 Thread* self = ts.Self();
854 Throwable* original_exception = self->GetException();
855 self->ClearException();
856
Elliott Hughesbf86d042011-08-31 17:53:14 -0700857 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700858 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
859 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
860 if (mid == NULL) {
861 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700862 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700863 } else {
864 env->CallVoidMethod(exception.get(), mid);
865 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700866 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700867 << " thrown while calling printStackTrace";
868 self->ClearException();
869 }
870 }
871
872 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700874
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 static jthrowable ExceptionOccurred(JNIEnv* env) {
876 ScopedJniThreadState ts(env);
877 Object* exception = ts.Self()->GetException();
Elliott Hughes81ff3182012-03-23 20:35:56 -0700878 return (exception != NULL) ? AddLocalReference<jthrowable>(env, exception) : NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 }
880
Elliott Hughescdf53122011-08-19 15:46:09 -0700881 static void FatalError(JNIEnv* env, const char* msg) {
882 ScopedJniThreadState ts(env);
883 LOG(FATAL) << "JNI FatalError called: " << msg;
884 }
885
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700886 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700888 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
889 return JNI_ERR;
890 }
891 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 return JNI_OK;
893 }
894
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700895 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700897 Object* survivor = Decode<Object*>(ts, java_survivor);
898 ts.Env()->PopFrame();
899 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 }
901
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700902 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700903 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700904 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
905 }
906
907 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
908 // TODO: we should try to expand the table if necessary.
909 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
910 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
911 return JNI_ERR;
912 }
913 // TODO: this isn't quite right, since "capacity" includes holes.
914 size_t capacity = ts.Env()->locals.Capacity();
915 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
916 if (!okay) {
917 ts.Self()->ThrowOutOfMemoryError(caller);
918 }
919 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 }
921
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
923 ScopedJniThreadState ts(env);
924 if (obj == NULL) {
925 return NULL;
926 }
927
Elliott Hughes75770752011-08-24 17:52:38 -0700928 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 IndirectReferenceTable& globals = vm->globals;
930 MutexLock mu(vm->globals_lock);
931 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
932 return reinterpret_cast<jobject>(ref);
933 }
934
935 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
936 ScopedJniThreadState ts(env);
937 if (obj == NULL) {
938 return;
939 }
940
Elliott Hughes75770752011-08-24 17:52:38 -0700941 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 IndirectReferenceTable& globals = vm->globals;
943 MutexLock mu(vm->globals_lock);
944
945 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
946 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
947 << "failed to find entry";
948 }
949 }
950
951 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
952 ScopedJniThreadState ts(env);
953 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
954 }
955
956 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
957 ScopedJniThreadState ts(env);
958 if (obj == NULL) {
959 return;
960 }
961
Elliott Hughes75770752011-08-24 17:52:38 -0700962 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 IndirectReferenceTable& weak_globals = vm->weak_globals;
964 MutexLock mu(vm->weak_globals_lock);
965
966 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
967 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
968 << "failed to find entry";
969 }
970 }
971
972 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
973 ScopedJniThreadState ts(env);
974 if (obj == NULL) {
975 return NULL;
976 }
977
978 IndirectReferenceTable& locals = ts.Env()->locals;
979
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700980 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
982 return reinterpret_cast<jobject>(ref);
983 }
984
985 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
986 ScopedJniThreadState ts(env);
987 if (obj == NULL) {
988 return;
989 }
990
991 IndirectReferenceTable& locals = ts.Env()->locals;
992
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700993 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 if (!locals.Remove(cookie, obj)) {
995 // Attempting to delete a local reference that is not in the
996 // topmost local reference frame is a no-op. DeleteLocalRef returns
997 // void and doesn't throw any exceptions, but we should probably
998 // complain about it so the user will notice that things aren't
999 // going quite the way they expect.
1000 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
1001 << "failed to find entry";
1002 }
1003 }
1004
1005 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
1006 ScopedJniThreadState ts(env);
1007 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
1008 ? JNI_TRUE : JNI_FALSE;
1009 }
1010
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001011 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001013 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001014 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001015 return NULL;
1016 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001017 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001018 }
1019
Elliott Hughese84278b2012-03-22 10:06:53 -07001020 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 ScopedJniThreadState ts(env);
1022 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001024 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001025 va_end(args);
1026 return result;
1027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001031 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001032 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001033 return NULL;
1034 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001035 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001036 if (result == NULL) {
1037 return NULL;
1038 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001039 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001041 if (!ts.Self()->IsExceptionPending()) {
1042 return local_result;
1043 } else {
1044 return NULL;
1045 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001050 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001051 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001052 return NULL;
1053 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001054 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001055 if (result == NULL) {
1056 return NULL;
1057 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001058 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001060 if (!ts.Self()->IsExceptionPending()) {
1061 return local_result;
1062 } else {
1063 return NULL;
1064 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1068 ScopedJniThreadState ts(env);
1069 return FindMethodID(ts, c, name, sig, false);
1070 }
1071
1072 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1073 ScopedJniThreadState ts(env);
1074 return FindMethodID(ts, c, name, sig, true);
1075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001079 va_list ap;
1080 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001081 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001083 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001088 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001089 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001094 JValue result(InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001095 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 va_list ap;
1101 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001102 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001104 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001109 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001114 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 va_list ap;
1120 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001121 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001123 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001128 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 }
1130
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001133 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_list ap;
1139 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001140 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001142 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001147 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001152 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 va_list ap;
1158 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001159 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001161 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001166 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001171 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 va_list ap;
1177 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001178 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001180 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 }
1182
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001185 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 }
1187
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001190 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001195 va_list ap;
1196 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001197 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001199 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001204 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001209 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 va_list ap;
1215 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001216 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001218 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001223 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 }
1225
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001228 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 va_list ap;
1234 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001235 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001237 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001242 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
1244
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001247 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 va_list ap;
1253 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001254 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
Elliott Hughes72025e52011-08-23 17:50:30 -07001258 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001260 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 }
1262
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001265 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001268 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 ScopedJniThreadState ts(env);
1270 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001272 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001273 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 va_end(ap);
1275 return local_result;
1276 }
1277
1278 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001279 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001281 JValue result(InvokeWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001282 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 }
1284
1285 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001286 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001288 JValue result(InvokeWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001289 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
1292 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001293 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 ScopedJniThreadState ts(env);
1295 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001297 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001299 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
1302 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001303 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001305 return InvokeWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
1308 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001309 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001311 return InvokeWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001314 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 ScopedJniThreadState ts(env);
1316 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001317 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001318 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001320 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
1323 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001324 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001326 return InvokeWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
1329 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001332 return InvokeWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001335 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 ScopedJniThreadState ts(env);
1337 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001338 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001339 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001341 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
1344 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001345 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001347 return InvokeWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
1350 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001351 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001353 return InvokeWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001356 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 ScopedJniThreadState ts(env);
1358 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001360 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001362 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
1365 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001366 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001368 return InvokeWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
1371 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001372 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001374 return InvokeWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001377 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 ScopedJniThreadState ts(env);
1379 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001380 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001381 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001383 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
1385
1386 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001387 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001389 return InvokeWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
1392 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001393 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001395 return InvokeWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001398 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 ScopedJniThreadState ts(env);
1400 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001401 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001402 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001404 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
1407 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001408 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001410 return InvokeWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 }
1412
1413 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001414 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001416 return InvokeWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 }
1418
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001419 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 ScopedJniThreadState ts(env);
1421 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001422 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001423 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001425 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
1428 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001429 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001431 return InvokeWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
1434 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001435 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001437 return InvokeWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001440 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 ScopedJniThreadState ts(env);
1442 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001443 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001444 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001446 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 }
1448
1449 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001450 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001452 return InvokeWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
1455 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001456 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001458 return InvokeWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
1460
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001461 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 ScopedJniThreadState ts(env);
1463 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001464 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001465 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 va_end(ap);
1467 }
1468
1469 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001470 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001471 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001472 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
1475 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001476 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001478 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001479 }
1480
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001481 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 ScopedJniThreadState ts(env);
1483 return FindFieldID(ts, c, name, sig, false);
1484 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001485
1486
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001487 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 ScopedJniThreadState ts(env);
1489 return FindFieldID(ts, c, name, sig, true);
1490 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001491
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001495 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001496 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001498
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001501 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001502 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 }
1504
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001505 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001507 Object* o = Decode<Object*>(ts, java_object);
1508 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001509 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001511 }
1512
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001513 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001515 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001516 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001517 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520#define GET_PRIMITIVE_FIELD(fn, instance) \
1521 ScopedJniThreadState ts(env); \
1522 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001523 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001524 return f->fn(o)
1525
1526#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1527 ScopedJniThreadState ts(env); \
1528 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001529 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001530 f->fn(o, value)
1531
1532 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1533 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001536 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1537 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1541 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1545 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001548 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1549 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 }
1551
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001552 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1553 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001556 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1557 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 }
1559
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001560 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1561 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001564 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001565 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001568 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001569 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001572 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001573 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 }
1575
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001576 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001577 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001580 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001581 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001584 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001585 GET_PRIMITIVE_FIELD(GetLong, NULL);
1586 }
1587
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001588 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001589 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1590 }
1591
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001592 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001593 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1594 }
1595
1596 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1597 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1598 }
1599
1600 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1601 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1602 }
1603
1604 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1605 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1606 }
1607
1608 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1609 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1610 }
1611
1612 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1613 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1614 }
1615
1616 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1617 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1618 }
1619
1620 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1621 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1622 }
1623
1624 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1625 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1626 }
1627
1628 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1629 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1630 }
1631
1632 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1633 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1634 }
1635
1636 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1637 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1638 }
1639
1640 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1641 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1642 }
1643
1644 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1645 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1646 }
1647
1648 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1649 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1650 }
1651
1652 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1653 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1654 }
1655
1656 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1657 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 }
1659
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001660 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 ScopedJniThreadState ts(env);
1662 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001663 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001664 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001665 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 va_end(ap);
1667 return local_result;
1668 }
1669
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001670 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001671 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001672 JValue result(InvokeWithVarArgs(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001673 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 }
1675
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001676 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001678 JValue result(InvokeWithJValues(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001679 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 }
1681
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001682 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 ScopedJniThreadState ts(env);
1684 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001685 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001686 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001688 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 }
1690
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001691 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001693 return InvokeWithVarArgs(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 }
1695
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001696 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001698 return InvokeWithJValues(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 }
1700
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001701 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 ScopedJniThreadState ts(env);
1703 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001704 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001705 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001707 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 }
1709
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001710 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001712 return InvokeWithVarArgs(env, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 }
1714
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001715 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001717 return InvokeWithJValues(env, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001720 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 ScopedJniThreadState ts(env);
1722 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001723 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001724 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001726 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001731 return InvokeWithVarArgs(env, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001734 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001736 return InvokeWithJValues(env, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 ScopedJniThreadState ts(env);
1741 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001742 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001743 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001745 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 }
1747
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001748 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001750 return InvokeWithVarArgs(env, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 }
1752
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001753 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001755 return InvokeWithJValues(env, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 }
1757
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001758 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 ScopedJniThreadState ts(env);
1760 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001761 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001762 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001764 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001767 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001769 return InvokeWithVarArgs(env, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 }
1771
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001772 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001774 return InvokeWithJValues(env, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001777 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 ScopedJniThreadState ts(env);
1779 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001780 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001781 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001783 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 }
1785
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001786 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001788 return InvokeWithVarArgs(env, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 }
1790
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001791 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001793 return InvokeWithJValues(env, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001796 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 ScopedJniThreadState ts(env);
1798 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001799 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001800 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001802 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001807 return InvokeWithVarArgs(env, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001810 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001812 return InvokeWithJValues(env, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001815 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 ScopedJniThreadState ts(env);
1817 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001818 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001819 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001821 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 }
1823
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001824 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001826 return InvokeWithVarArgs(env, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 }
1828
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001829 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001831 return InvokeWithJValues(env, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001834 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 ScopedJniThreadState ts(env);
1836 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001837 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001838 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 va_end(ap);
1840 }
1841
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001842 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001844 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 }
1846
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001847 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001849 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 }
1851
Elliott Hughes814e4032011-08-23 12:07:56 -07001852 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001854 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001855 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001856 }
1857
1858 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1859 ScopedJniThreadState ts(env);
1860 if (utf == NULL) {
1861 return NULL;
1862 }
1863 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001864 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
Elliott Hughes814e4032011-08-23 12:07:56 -07001867 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1868 ScopedJniThreadState ts(env);
1869 return Decode<String*>(ts, java_string)->GetLength();
1870 }
1871
1872 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1873 ScopedJniThreadState ts(env);
1874 return Decode<String*>(ts, java_string)->GetUtfLength();
1875 }
1876
Elliott Hughesb465ab02011-08-24 11:21:21 -07001877 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001879 String* s = Decode<String*>(ts, java_string);
1880 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1881 ThrowSIOOBE(ts, start, length, s->GetLength());
1882 } else {
1883 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1884 memcpy(buf, chars + start, length * sizeof(jchar));
1885 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001886 }
1887
Elliott Hughesb465ab02011-08-24 11:21:21 -07001888 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001889 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001890 String* s = Decode<String*>(ts, java_string);
1891 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1892 ThrowSIOOBE(ts, start, length, s->GetLength());
1893 } else {
1894 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1895 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1896 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001897 }
1898
Elliott Hughes75770752011-08-24 17:52:38 -07001899 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001900 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001901 String* s = Decode<String*>(ts, java_string);
1902 const CharArray* chars = s->GetCharArray();
1903 PinPrimitiveArray(ts, chars);
1904 if (is_copy != NULL) {
1905 *is_copy = JNI_FALSE;
1906 }
1907 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001908 }
1909
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001910 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001911 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001912 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 }
1914
Elliott Hughes75770752011-08-24 17:52:38 -07001915 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001916 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001917 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 }
1919
Elliott Hughes75770752011-08-24 17:52:38 -07001920 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001922 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 }
1924
Elliott Hughes75770752011-08-24 17:52:38 -07001925 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001926 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001927 if (java_string == NULL) {
1928 return NULL;
1929 }
1930 if (is_copy != NULL) {
1931 *is_copy = JNI_TRUE;
1932 }
1933 String* s = Decode<String*>(ts, java_string);
1934 size_t byte_count = s->GetUtfLength();
1935 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001936 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001937 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1938 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1939 bytes[byte_count] = '\0';
1940 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001944 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001945 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001946 }
1947
Elliott Hughesbd935992011-08-22 11:59:34 -07001948 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001950 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001951 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001952 Array* array = obj->AsArray();
1953 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Elliott Hughes814e4032011-08-23 12:07:56 -07001956 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001958 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001959 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
1962 static void SetObjectArrayElement(JNIEnv* env,
1963 jobjectArray java_array, jsize index, jobject java_value) {
1964 ScopedJniThreadState ts(env);
1965 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1966 Object* value = Decode<Object*>(ts, java_value);
1967 array->Set(index, value);
1968 }
1969
1970 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1971 ScopedJniThreadState ts(env);
1972 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1973 }
1974
1975 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1976 ScopedJniThreadState ts(env);
1977 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1978 }
1979
1980 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1981 ScopedJniThreadState ts(env);
1982 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1983 }
1984
1985 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1986 ScopedJniThreadState ts(env);
1987 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1988 }
1989
1990 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1991 ScopedJniThreadState ts(env);
1992 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1993 }
1994
1995 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1996 ScopedJniThreadState ts(env);
1997 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1998 }
1999
2000 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2001 ScopedJniThreadState ts(env);
2002 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
2003 }
2004
2005 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
2006 ScopedJniThreadState ts(env);
2007 CHECK_GE(length, 0); // TODO: ReportJniError
2008
2009 // Compute the array class corresponding to the given element class.
2010 Class* element_class = Decode<Class*>(ts, element_jclass);
2011 std::string descriptor;
2012 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002013 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002014
2015 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002016 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2017 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 return NULL;
2019 }
2020
Elliott Hughes75770752011-08-24 17:52:38 -07002021 // Allocate and initialize if necessary.
2022 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002024 if (initial_element != NULL) {
2025 Object* initial_object = Decode<Object*>(ts, initial_element);
2026 for (jsize i = 0; i < length; ++i) {
2027 result->Set(i, initial_object);
2028 }
2029 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002030 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
2033 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2034 ScopedJniThreadState ts(env);
2035 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2036 }
2037
Ian Rogersa15e67d2012-02-28 13:51:55 -08002038 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002039 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002040 Array* array = Decode<Array*>(ts, java_array);
2041 PinPrimitiveArray(ts, array);
2042 if (is_copy != NULL) {
2043 *is_copy = JNI_FALSE;
2044 }
2045 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002046 }
2047
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002048 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002049 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002050 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002051 }
2052
Elliott Hughes75770752011-08-24 17:52:38 -07002053 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray 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<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes75770752011-08-24 17:52:38 -07002058 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray 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<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes75770752011-08-24 17:52:38 -07002063 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray 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<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes75770752011-08-24 17:52:38 -07002068 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray 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<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes75770752011-08-24 17:52:38 -07002073 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray 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<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes75770752011-08-24 17:52:38 -07002078 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002080 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes75770752011-08-24 17:52:38 -07002083 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002085 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes75770752011-08-24 17:52:38 -07002088 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002090 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002093 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, 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 ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, 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 ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, 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 ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, 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 ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, 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 Hughes1bac54f2012-03-16 12:48:31 -07002118 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002120 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002123 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002125 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 }
2127
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002128 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002130 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Elliott Hughes814e4032011-08-23 12:07:56 -07002133 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002135 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes814e4032011-08-23 12:07:56 -07002138 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002140 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Elliott Hughes814e4032011-08-23 12:07:56 -07002143 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002145 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes814e4032011-08-23 12:07:56 -07002148 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Elliott Hughes814e4032011-08-23 12:07:56 -07002153 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002155 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Elliott Hughes814e4032011-08-23 12:07:56 -07002158 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002160 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Elliott Hughes814e4032011-08-23 12:07:56 -07002163 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002165 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 }
2167
Elliott Hughes814e4032011-08-23 12:07:56 -07002168 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002170 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Elliott Hughes814e4032011-08-23 12:07:56 -07002173 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002175 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes814e4032011-08-23 12:07:56 -07002178 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002180 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
Elliott Hughes814e4032011-08-23 12:07:56 -07002183 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002185 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
Elliott Hughes814e4032011-08-23 12:07:56 -07002188 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002190 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Elliott Hughes814e4032011-08-23 12:07:56 -07002193 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002195 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Elliott Hughes814e4032011-08-23 12:07:56 -07002198 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002200 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
Elliott Hughes814e4032011-08-23 12:07:56 -07002203 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002205 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
Elliott Hughes814e4032011-08-23 12:07:56 -07002208 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002210 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
Elliott Hughes5174fe62011-08-23 15:12:35 -07002213 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002215 Class* c = Decode<Class*>(ts, java_class);
2216
Elliott Hughes5174fe62011-08-23 15:12:35 -07002217 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 const char* name = methods[i].name;
2219 const char* sig = methods[i].signature;
2220
2221 if (*sig == '!') {
2222 // TODO: fast jni. it's too noisy to log all these.
2223 ++sig;
2224 }
2225
Elliott Hughes5174fe62011-08-23 15:12:35 -07002226 Method* m = c->FindDirectMethod(name, sig);
2227 if (m == NULL) {
2228 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002230 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002231 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002232 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002234 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002235 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002236 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 return JNI_ERR;
2238 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002239
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002240 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002241
Ian Rogers60db5ab2012-02-20 17:02:00 -08002242 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 }
2244 return JNI_OK;
2245 }
2246
Elliott Hughes5174fe62011-08-23 15:12:35 -07002247 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002249 Class* c = Decode<Class*>(ts, java_class);
2250
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002251 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002252
2253 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2254 Method* m = c->GetDirectMethod(i);
2255 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002256 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002257 }
2258 }
2259 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2260 Method* m = c->GetVirtualMethod(i);
2261 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002262 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002263 }
2264 }
2265
2266 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 }
2268
Elliott Hughes72025e52011-08-23 17:50:30 -07002269 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002270 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002271 Object* o = Decode<Object*>(ts, java_object);
2272 o->MonitorEnter(ts.Self());
2273 if (ts.Self()->IsExceptionPending()) {
2274 return JNI_ERR;
2275 }
2276 ts.Env()->monitors.Add(o);
2277 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002278 }
2279
Elliott Hughes72025e52011-08-23 17:50:30 -07002280 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002282 Object* o = Decode<Object*>(ts, java_object);
2283 o->MonitorExit(ts.Self());
2284 if (ts.Self()->IsExceptionPending()) {
2285 return JNI_ERR;
2286 }
2287 ts.Env()->monitors.Remove(o);
2288 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002289 }
2290
2291 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2292 ScopedJniThreadState ts(env);
2293 Runtime* runtime = Runtime::Current();
2294 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002295 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 } else {
2297 *vm = NULL;
2298 }
2299 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2300 }
2301
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2303 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002304
2305 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002306 CHECK(address != NULL); // TODO: ReportJniError
2307 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002308
2309 jclass buffer_class = GetDirectByteBufferClass(env);
2310 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2311 if (mid == NULL) {
2312 return NULL;
2313 }
2314
2315 // At the moment, the Java side is limited to 32 bits.
2316 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2317 CHECK_LE(capacity, 0xffffffff);
2318 jint address_arg = reinterpret_cast<jint>(address);
2319 jint capacity_arg = static_cast<jint>(capacity);
2320
2321 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2322 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002323 }
2324
Elliott Hughesb465ab02011-08-24 11:21:21 -07002325 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002326 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002327 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2328 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002329 }
2330
Elliott Hughesb465ab02011-08-24 11:21:21 -07002331 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002332 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002333 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2334 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002335 }
2336
Elliott Hughesb465ab02011-08-24 11:21:21 -07002337 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002338 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002339
Elliott Hughes75770752011-08-24 17:52:38 -07002340 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002341
2342 // Do we definitely know what kind of reference this is?
2343 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2344 IndirectRefKind kind = GetIndirectRefKind(ref);
2345 switch (kind) {
2346 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002347 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2348 return JNILocalRefType;
2349 }
2350 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002351 case kGlobal:
2352 return JNIGlobalRefType;
2353 case kWeakGlobal:
2354 return JNIWeakGlobalRefType;
2355 case kSirtOrInvalid:
2356 // Is it in a stack IRT?
TDYa12728f1a142012-03-15 21:51:52 -07002357 if (ts.Self()->StackReferencesContain(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002358 return JNILocalRefType;
2359 }
2360
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002361 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002362 return JNIInvalidRefType;
2363 }
2364
Elliott Hughesb465ab02011-08-24 11:21:21 -07002365 // If we're handing out direct pointers, check whether it's a direct pointer
2366 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002367 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002368 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002369 return JNILocalRefType;
2370 }
2371 }
2372
2373 return JNIInvalidRefType;
2374 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002375 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2376 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002377 }
2378};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002379
Elliott Hughes88c5c352012-03-15 18:49:48 -07002380const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002381 NULL, // reserved0.
2382 NULL, // reserved1.
2383 NULL, // reserved2.
2384 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002385 JNI::GetVersion,
2386 JNI::DefineClass,
2387 JNI::FindClass,
2388 JNI::FromReflectedMethod,
2389 JNI::FromReflectedField,
2390 JNI::ToReflectedMethod,
2391 JNI::GetSuperclass,
2392 JNI::IsAssignableFrom,
2393 JNI::ToReflectedField,
2394 JNI::Throw,
2395 JNI::ThrowNew,
2396 JNI::ExceptionOccurred,
2397 JNI::ExceptionDescribe,
2398 JNI::ExceptionClear,
2399 JNI::FatalError,
2400 JNI::PushLocalFrame,
2401 JNI::PopLocalFrame,
2402 JNI::NewGlobalRef,
2403 JNI::DeleteGlobalRef,
2404 JNI::DeleteLocalRef,
2405 JNI::IsSameObject,
2406 JNI::NewLocalRef,
2407 JNI::EnsureLocalCapacity,
2408 JNI::AllocObject,
2409 JNI::NewObject,
2410 JNI::NewObjectV,
2411 JNI::NewObjectA,
2412 JNI::GetObjectClass,
2413 JNI::IsInstanceOf,
2414 JNI::GetMethodID,
2415 JNI::CallObjectMethod,
2416 JNI::CallObjectMethodV,
2417 JNI::CallObjectMethodA,
2418 JNI::CallBooleanMethod,
2419 JNI::CallBooleanMethodV,
2420 JNI::CallBooleanMethodA,
2421 JNI::CallByteMethod,
2422 JNI::CallByteMethodV,
2423 JNI::CallByteMethodA,
2424 JNI::CallCharMethod,
2425 JNI::CallCharMethodV,
2426 JNI::CallCharMethodA,
2427 JNI::CallShortMethod,
2428 JNI::CallShortMethodV,
2429 JNI::CallShortMethodA,
2430 JNI::CallIntMethod,
2431 JNI::CallIntMethodV,
2432 JNI::CallIntMethodA,
2433 JNI::CallLongMethod,
2434 JNI::CallLongMethodV,
2435 JNI::CallLongMethodA,
2436 JNI::CallFloatMethod,
2437 JNI::CallFloatMethodV,
2438 JNI::CallFloatMethodA,
2439 JNI::CallDoubleMethod,
2440 JNI::CallDoubleMethodV,
2441 JNI::CallDoubleMethodA,
2442 JNI::CallVoidMethod,
2443 JNI::CallVoidMethodV,
2444 JNI::CallVoidMethodA,
2445 JNI::CallNonvirtualObjectMethod,
2446 JNI::CallNonvirtualObjectMethodV,
2447 JNI::CallNonvirtualObjectMethodA,
2448 JNI::CallNonvirtualBooleanMethod,
2449 JNI::CallNonvirtualBooleanMethodV,
2450 JNI::CallNonvirtualBooleanMethodA,
2451 JNI::CallNonvirtualByteMethod,
2452 JNI::CallNonvirtualByteMethodV,
2453 JNI::CallNonvirtualByteMethodA,
2454 JNI::CallNonvirtualCharMethod,
2455 JNI::CallNonvirtualCharMethodV,
2456 JNI::CallNonvirtualCharMethodA,
2457 JNI::CallNonvirtualShortMethod,
2458 JNI::CallNonvirtualShortMethodV,
2459 JNI::CallNonvirtualShortMethodA,
2460 JNI::CallNonvirtualIntMethod,
2461 JNI::CallNonvirtualIntMethodV,
2462 JNI::CallNonvirtualIntMethodA,
2463 JNI::CallNonvirtualLongMethod,
2464 JNI::CallNonvirtualLongMethodV,
2465 JNI::CallNonvirtualLongMethodA,
2466 JNI::CallNonvirtualFloatMethod,
2467 JNI::CallNonvirtualFloatMethodV,
2468 JNI::CallNonvirtualFloatMethodA,
2469 JNI::CallNonvirtualDoubleMethod,
2470 JNI::CallNonvirtualDoubleMethodV,
2471 JNI::CallNonvirtualDoubleMethodA,
2472 JNI::CallNonvirtualVoidMethod,
2473 JNI::CallNonvirtualVoidMethodV,
2474 JNI::CallNonvirtualVoidMethodA,
2475 JNI::GetFieldID,
2476 JNI::GetObjectField,
2477 JNI::GetBooleanField,
2478 JNI::GetByteField,
2479 JNI::GetCharField,
2480 JNI::GetShortField,
2481 JNI::GetIntField,
2482 JNI::GetLongField,
2483 JNI::GetFloatField,
2484 JNI::GetDoubleField,
2485 JNI::SetObjectField,
2486 JNI::SetBooleanField,
2487 JNI::SetByteField,
2488 JNI::SetCharField,
2489 JNI::SetShortField,
2490 JNI::SetIntField,
2491 JNI::SetLongField,
2492 JNI::SetFloatField,
2493 JNI::SetDoubleField,
2494 JNI::GetStaticMethodID,
2495 JNI::CallStaticObjectMethod,
2496 JNI::CallStaticObjectMethodV,
2497 JNI::CallStaticObjectMethodA,
2498 JNI::CallStaticBooleanMethod,
2499 JNI::CallStaticBooleanMethodV,
2500 JNI::CallStaticBooleanMethodA,
2501 JNI::CallStaticByteMethod,
2502 JNI::CallStaticByteMethodV,
2503 JNI::CallStaticByteMethodA,
2504 JNI::CallStaticCharMethod,
2505 JNI::CallStaticCharMethodV,
2506 JNI::CallStaticCharMethodA,
2507 JNI::CallStaticShortMethod,
2508 JNI::CallStaticShortMethodV,
2509 JNI::CallStaticShortMethodA,
2510 JNI::CallStaticIntMethod,
2511 JNI::CallStaticIntMethodV,
2512 JNI::CallStaticIntMethodA,
2513 JNI::CallStaticLongMethod,
2514 JNI::CallStaticLongMethodV,
2515 JNI::CallStaticLongMethodA,
2516 JNI::CallStaticFloatMethod,
2517 JNI::CallStaticFloatMethodV,
2518 JNI::CallStaticFloatMethodA,
2519 JNI::CallStaticDoubleMethod,
2520 JNI::CallStaticDoubleMethodV,
2521 JNI::CallStaticDoubleMethodA,
2522 JNI::CallStaticVoidMethod,
2523 JNI::CallStaticVoidMethodV,
2524 JNI::CallStaticVoidMethodA,
2525 JNI::GetStaticFieldID,
2526 JNI::GetStaticObjectField,
2527 JNI::GetStaticBooleanField,
2528 JNI::GetStaticByteField,
2529 JNI::GetStaticCharField,
2530 JNI::GetStaticShortField,
2531 JNI::GetStaticIntField,
2532 JNI::GetStaticLongField,
2533 JNI::GetStaticFloatField,
2534 JNI::GetStaticDoubleField,
2535 JNI::SetStaticObjectField,
2536 JNI::SetStaticBooleanField,
2537 JNI::SetStaticByteField,
2538 JNI::SetStaticCharField,
2539 JNI::SetStaticShortField,
2540 JNI::SetStaticIntField,
2541 JNI::SetStaticLongField,
2542 JNI::SetStaticFloatField,
2543 JNI::SetStaticDoubleField,
2544 JNI::NewString,
2545 JNI::GetStringLength,
2546 JNI::GetStringChars,
2547 JNI::ReleaseStringChars,
2548 JNI::NewStringUTF,
2549 JNI::GetStringUTFLength,
2550 JNI::GetStringUTFChars,
2551 JNI::ReleaseStringUTFChars,
2552 JNI::GetArrayLength,
2553 JNI::NewObjectArray,
2554 JNI::GetObjectArrayElement,
2555 JNI::SetObjectArrayElement,
2556 JNI::NewBooleanArray,
2557 JNI::NewByteArray,
2558 JNI::NewCharArray,
2559 JNI::NewShortArray,
2560 JNI::NewIntArray,
2561 JNI::NewLongArray,
2562 JNI::NewFloatArray,
2563 JNI::NewDoubleArray,
2564 JNI::GetBooleanArrayElements,
2565 JNI::GetByteArrayElements,
2566 JNI::GetCharArrayElements,
2567 JNI::GetShortArrayElements,
2568 JNI::GetIntArrayElements,
2569 JNI::GetLongArrayElements,
2570 JNI::GetFloatArrayElements,
2571 JNI::GetDoubleArrayElements,
2572 JNI::ReleaseBooleanArrayElements,
2573 JNI::ReleaseByteArrayElements,
2574 JNI::ReleaseCharArrayElements,
2575 JNI::ReleaseShortArrayElements,
2576 JNI::ReleaseIntArrayElements,
2577 JNI::ReleaseLongArrayElements,
2578 JNI::ReleaseFloatArrayElements,
2579 JNI::ReleaseDoubleArrayElements,
2580 JNI::GetBooleanArrayRegion,
2581 JNI::GetByteArrayRegion,
2582 JNI::GetCharArrayRegion,
2583 JNI::GetShortArrayRegion,
2584 JNI::GetIntArrayRegion,
2585 JNI::GetLongArrayRegion,
2586 JNI::GetFloatArrayRegion,
2587 JNI::GetDoubleArrayRegion,
2588 JNI::SetBooleanArrayRegion,
2589 JNI::SetByteArrayRegion,
2590 JNI::SetCharArrayRegion,
2591 JNI::SetShortArrayRegion,
2592 JNI::SetIntArrayRegion,
2593 JNI::SetLongArrayRegion,
2594 JNI::SetFloatArrayRegion,
2595 JNI::SetDoubleArrayRegion,
2596 JNI::RegisterNatives,
2597 JNI::UnregisterNatives,
2598 JNI::MonitorEnter,
2599 JNI::MonitorExit,
2600 JNI::GetJavaVM,
2601 JNI::GetStringRegion,
2602 JNI::GetStringUTFRegion,
2603 JNI::GetPrimitiveArrayCritical,
2604 JNI::ReleasePrimitiveArrayCritical,
2605 JNI::GetStringCritical,
2606 JNI::ReleaseStringCritical,
2607 JNI::NewWeakGlobalRef,
2608 JNI::DeleteWeakGlobalRef,
2609 JNI::ExceptionCheck,
2610 JNI::NewDirectByteBuffer,
2611 JNI::GetDirectBufferAddress,
2612 JNI::GetDirectBufferCapacity,
2613 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002614};
2615
Elliott Hughes75770752011-08-24 17:52:38 -07002616JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002617 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002618 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002619 local_ref_cookie(IRT_FIRST_SEGMENT),
2620 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002621 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002622 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002623 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002624 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002625 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002626 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002627 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002628 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2629 // errors will ensue.
2630 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2631 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002632}
2633
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002634JNIEnvExt::~JNIEnvExt() {
2635}
2636
Elliott Hughes88c5c352012-03-15 18:49:48 -07002637void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2638 check_jni = enabled;
2639 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002640}
2641
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002642void JNIEnvExt::DumpReferenceTables() {
2643 locals.Dump();
2644 monitors.Dump();
2645}
2646
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002647void JNIEnvExt::PushFrame(int /*capacity*/) {
2648 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002649 stacked_local_ref_cookies.push_back(local_ref_cookie);
2650 local_ref_cookie = locals.GetSegmentState();
2651}
2652
2653void JNIEnvExt::PopFrame() {
2654 locals.SetSegmentState(local_ref_cookie);
2655 local_ref_cookie = stacked_local_ref_cookies.back();
2656 stacked_local_ref_cookies.pop_back();
2657}
2658
Carl Shapiroea4dca82011-08-01 13:45:38 -07002659// JNI Invocation interface.
2660
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002661extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2662 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2663 if (args->version < JNI_VERSION_1_2) {
2664 return JNI_EVERSION;
2665 }
2666 Runtime::Options options;
2667 for (int i = 0; i < args->nOptions; ++i) {
2668 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002669 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002670 }
2671 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002672 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002673 if (runtime == NULL) {
2674 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002675 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002676 runtime->Start();
2677 *p_env = Thread::Current()->GetJniEnv();
2678 *p_vm = runtime->GetJavaVM();
2679 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002680}
2681
Elliott Hughesf2682d52011-08-15 16:37:04 -07002682extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002683 Runtime* runtime = Runtime::Current();
2684 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002685 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002686 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002687 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002688 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002689 }
2690 return JNI_OK;
2691}
2692
2693// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002694extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002695 return JNI_ERR;
2696}
2697
Elliott Hughescdf53122011-08-19 15:46:09 -07002698class JII {
2699 public:
2700 static jint DestroyJavaVM(JavaVM* vm) {
2701 if (vm == NULL) {
2702 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002703 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002704 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2705 delete raw_vm->runtime;
2706 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002707 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002708
Elliott Hughescdf53122011-08-19 15:46:09 -07002709 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002710 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002711 }
2712
2713 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002714 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002715 }
2716
2717 static jint DetachCurrentThread(JavaVM* vm) {
2718 if (vm == NULL) {
2719 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002720 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002721 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2722 Runtime* runtime = raw_vm->runtime;
2723 runtime->DetachCurrentThread();
2724 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002725 }
2726
2727 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2728 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2729 return JNI_EVERSION;
2730 }
2731 if (vm == NULL || env == NULL) {
2732 return JNI_ERR;
2733 }
2734 Thread* thread = Thread::Current();
2735 if (thread == NULL) {
2736 *env = NULL;
2737 return JNI_EDETACHED;
2738 }
2739 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002740 return JNI_OK;
2741 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002742};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002743
Elliott Hughes88c5c352012-03-15 18:49:48 -07002744const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002745 NULL, // reserved0
2746 NULL, // reserved1
2747 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002748 JII::DestroyJavaVM,
2749 JII::AttachCurrentThread,
2750 JII::DetachCurrentThread,
2751 JII::GetEnv,
2752 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002753};
2754
Elliott Hughesa0957642011-09-02 14:27:33 -07002755JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002756 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002757 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002758 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002759 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002760 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002761 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002762 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002763 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002764 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002765 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002766 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002767 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002768 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002769 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002770 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002771 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002772 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002773 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002774 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002775}
2776
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002777JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002778 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002779}
2780
Elliott Hughes88c5c352012-03-15 18:49:48 -07002781void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2782 check_jni = enabled;
2783 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002784}
2785
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002786void JavaVMExt::DumpReferenceTables() {
2787 {
2788 MutexLock mu(globals_lock);
2789 globals.Dump();
2790 }
2791 {
2792 MutexLock mu(weak_globals_lock);
2793 weak_globals.Dump();
2794 }
2795 {
2796 MutexLock mu(pins_lock);
2797 pin_table.Dump();
2798 }
2799}
2800
Elliott Hughes75770752011-08-24 17:52:38 -07002801bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2802 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002803
2804 // See if we've already loaded this library. If we have, and the class loader
2805 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002806 // TODO: for better results we should canonicalize the pathname (or even compare
2807 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002808 SharedLibrary* library;
2809 {
2810 // TODO: move the locking (and more of this logic) into Libraries.
2811 MutexLock mu(libraries_lock);
2812 library = libraries->Get(path);
2813 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002814 if (library != NULL) {
2815 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002816 // The library will be associated with class_loader. The JNI
2817 // spec says we can't load the same library into more than one
2818 // class loader.
2819 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2820 "ClassLoader %p; can't open in ClassLoader %p",
2821 path.c_str(), library->GetClassLoader(), class_loader);
2822 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002823 return false;
2824 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002825 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2826 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002827 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002828 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2829 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002830 return false;
2831 }
2832 return true;
2833 }
2834
2835 // Open the shared library. Because we're using a full path, the system
2836 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2837 // resolve this library's dependencies though.)
2838
2839 // Failures here are expected when java.library.path has several entries
2840 // and we have to hunt for the lib.
2841
2842 // The current version of the dynamic linker prints detailed information
2843 // about dlopen() failures. Some things to check if the message is
2844 // cryptic:
2845 // - make sure the library exists on the device
2846 // - verify that the right path is being opened (the debug log message
2847 // above can help with that)
2848 // - check to see if the library is valid (e.g. not zero bytes long)
2849 // - check config/prelink-linux-arm.map to ensure that the library
2850 // is listed and is not being overrun by the previous entry (if
2851 // loading suddenly stops working on a prelinked library, this is
2852 // a good one to check)
2853 // - write a trivial app that calls sleep() then dlopen(), attach
2854 // to it with "strace -p <pid>" while it sleeps, and watch for
2855 // attempts to open nonexistent dependent shared libs
2856
2857 // TODO: automate some of these checks!
2858
2859 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002860 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002861 // the GC to ignore us.
2862 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002863 void* handle = NULL;
2864 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002865 ScopedThreadStateChange tsc(self, kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002866 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002867 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002868
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002869 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002870
2871 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002872 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002873 return false;
2874 }
2875
2876 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002877 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002878 // TODO: move the locking (and more of this logic) into Libraries.
2879 MutexLock mu(libraries_lock);
2880 library = libraries->Get(path);
2881 if (library != NULL) {
2882 LOG(INFO) << "WOW: we lost a race to add shared library: "
2883 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002884 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002885 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002886 library = new SharedLibrary(path, handle, class_loader);
2887 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002888 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002889
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002890 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002891
2892 bool result = true;
2893 void* sym = dlsym(handle, "JNI_OnLoad");
2894 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002895 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002896 } else {
2897 // Call JNI_OnLoad. We have to override the current class
2898 // loader, which will always be "null" since the stuff at the
2899 // top of the stack is around Runtime.loadLibrary(). (See
2900 // the comments in the JNI FindClass function.)
2901 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2902 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002903 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002904 self->SetClassLoaderOverride(class_loader);
2905
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002906 int version = 0;
2907 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002908 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002909 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002910 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002911 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002912
Brian Carlstromaded5f72011-10-07 17:15:04 -07002913 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002914
2915 if (version != JNI_VERSION_1_2 &&
2916 version != JNI_VERSION_1_4 &&
2917 version != JNI_VERSION_1_6) {
2918 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2919 << "bad version: " << version;
2920 // It's unwise to call dlclose() here, but we can mark it
2921 // as bad and ensure that future load attempts will fail.
2922 // We don't know how far JNI_OnLoad got, so there could
2923 // be some partially-initialized stuff accessible through
2924 // newly-registered native method calls. We could try to
2925 // unregister them, but that doesn't seem worthwhile.
2926 result = false;
2927 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002928 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2929 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002930 }
2931 }
2932
2933 library->SetResult(result);
2934 return result;
2935}
2936
2937void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2938 CHECK(m->IsNative());
2939
2940 Class* c = m->GetDeclaringClass();
2941
2942 // If this is a static method, it could be called before the class
2943 // has been initialized.
2944 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002945 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002946 return NULL;
2947 }
2948 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002949 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002950 }
2951
Brian Carlstrom16192862011-09-12 17:50:06 -07002952 std::string detail;
2953 void* native_method;
2954 {
2955 MutexLock mu(libraries_lock);
2956 native_method = libraries->FindNativeMethod(m, detail);
2957 }
2958 // throwing can cause libraries_lock to be reacquired
2959 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002960 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002961 }
2962 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002963}
2964
Elliott Hughes410c0c82011-09-01 17:58:25 -07002965void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2966 {
2967 MutexLock mu(globals_lock);
2968 globals.VisitRoots(visitor, arg);
2969 }
2970 {
2971 MutexLock mu(pins_lock);
2972 pin_table.VisitRoots(visitor, arg);
2973 }
2974 // The weak_globals table is visited by the GC itself (because it mutates the table).
2975}
2976
Elliott Hughes844f9a02012-01-24 20:19:58 -08002977jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
2978 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2979 if (c.get() == NULL) {
2980 return NULL;
2981 }
2982 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
2983}
2984
Ian Rogersdf20fe02011-07-20 20:34:16 -07002985} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002986
2987std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2988 switch (rhs) {
2989 case JNIInvalidRefType:
2990 os << "JNIInvalidRefType";
2991 return os;
2992 case JNILocalRefType:
2993 os << "JNILocalRefType";
2994 return os;
2995 case JNIGlobalRefType:
2996 os << "JNIGlobalRefType";
2997 return os;
2998 case JNIWeakGlobalRefType:
2999 os << "JNIWeakGlobalRefType";
3000 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003001 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003002 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003003 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003004 }
3005}