blob: 846a717dcac9bf741ac15f20b277832be7b102bd [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
Elliott Hughesae80b492012-04-24 10:43:17 -0700662 void Dump(std::ostream& os) const {
663 bool first = true;
664 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
665 if (!first) {
666 os << ' ';
667 }
668 first = false;
669 os << it->first;
670 }
671 }
672
673 size_t size() const {
674 return libraries_.size();
675 }
676
Elliott Hughes79082e32011-08-25 12:07:32 -0700677 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700678 It it = libraries_.find(path);
679 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700680 }
681
682 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700683 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700684 }
685
686 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700687 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700688 std::string jni_short_name(JniShortName(m));
689 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700690 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700691 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
692 SharedLibrary* library = it->second;
693 if (library->GetClassLoader() != declaring_class_loader) {
694 // We only search libraries loaded by the appropriate ClassLoader.
695 continue;
696 }
697 // Try the short name then the long name...
698 void* fn = library->FindSymbol(jni_short_name);
699 if (fn == NULL) {
700 fn = library->FindSymbol(jni_long_name);
701 }
702 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800703 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
704 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700705 return fn;
706 }
707 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700708 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700709 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700710 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700711 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700712 return NULL;
713 }
714
715 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700716 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700717
Elliott Hughesa0e18062012-04-13 15:59:59 -0700718 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700719};
720
Elliott Hughes418d20f2011-09-22 14:00:39 -0700721JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
722 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
723 Object* receiver = Decode<Object*>(env, obj);
724 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800725 ArgArray arg_array(method);
726 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700727 return InvokeWithArgArray(env, receiver, method, arg_array.get());
728}
729
Elliott Hughesd07986f2011-12-06 18:27:45 -0800730JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700731 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800732}
733
Elliott Hughescdf53122011-08-19 15:46:09 -0700734class JNI {
735 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700736
Elliott Hughescdf53122011-08-19 15:46:09 -0700737 static jint GetVersion(JNIEnv* env) {
738 ScopedJniThreadState ts(env);
739 return JNI_VERSION_1_6;
740 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700741
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
743 ScopedJniThreadState ts(env);
744 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700745 return NULL;
746 }
747
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 static jclass FindClass(JNIEnv* env, const char* name) {
749 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700750 Runtime* runtime = Runtime::Current();
751 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700753 Class* c = NULL;
754 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700755 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800756 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700757 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800758 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700759 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700760 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700761 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700762
Elliott Hughescdf53122011-08-19 15:46:09 -0700763 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
764 ScopedJniThreadState ts(env);
765 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700766 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700767 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700768
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
770 ScopedJniThreadState ts(env);
771 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700772 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700774
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
776 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700777 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700778 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700780
Elliott Hughescdf53122011-08-19 15:46:09 -0700781 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
782 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700783 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700784 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700786
Elliott Hughes37f7a402011-08-22 18:56:01 -0700787 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
788 ScopedJniThreadState ts(env);
789 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700790 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700791 }
792
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700793 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700794 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700795 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700796 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700798
Elliott Hughes37f7a402011-08-22 18:56:01 -0700799 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700800 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700801 Class* c1 = Decode<Class*>(ts, java_class1);
802 Class* c2 = Decode<Class*>(ts, java_class2);
803 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700805
Elliott Hughese84278b2012-03-22 10:06:53 -0700806 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700808 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700809 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700810 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700811 return JNI_TRUE;
812 } else {
813 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700814 Class* c = Decode<Class*>(ts, java_class);
815 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700816 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700818
Elliott Hughes37f7a402011-08-22 18:56:01 -0700819 static jint Throw(JNIEnv* env, jthrowable java_exception) {
820 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700821 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700822 if (exception == NULL) {
823 return JNI_ERR;
824 }
825 ts.Self()->SetException(exception);
826 return JNI_OK;
827 }
828
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700829 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700830 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700831 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700832 jmethodID mid = ((msg != NULL)
833 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
834 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700835 if (mid == NULL) {
836 return JNI_ERR;
837 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700838 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700839 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700840 return JNI_ERR;
841 }
842
843 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700844 args[0].l = s.get();
845 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
846 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700847 return JNI_ERR;
848 }
849
Elliott Hughes72025e52011-08-23 17:50:30 -0700850 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700851
Elliott Hughes37f7a402011-08-22 18:56:01 -0700852 return JNI_OK;
853 }
854
855 static jboolean ExceptionCheck(JNIEnv* env) {
856 ScopedJniThreadState ts(env);
857 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
858 }
859
860 static void ExceptionClear(JNIEnv* env) {
861 ScopedJniThreadState ts(env);
862 ts.Self()->ClearException();
863 }
864
865 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700867
868 Thread* self = ts.Self();
869 Throwable* original_exception = self->GetException();
870 self->ClearException();
871
Elliott Hughesbf86d042011-08-31 17:53:14 -0700872 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700873 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
874 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
875 if (mid == NULL) {
876 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700877 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700878 } else {
879 env->CallVoidMethod(exception.get(), mid);
880 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700881 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700882 << " thrown while calling printStackTrace";
883 self->ClearException();
884 }
885 }
886
887 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700889
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 static jthrowable ExceptionOccurred(JNIEnv* env) {
891 ScopedJniThreadState ts(env);
892 Object* exception = ts.Self()->GetException();
Elliott Hughes81ff3182012-03-23 20:35:56 -0700893 return (exception != NULL) ? AddLocalReference<jthrowable>(env, exception) : NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700894 }
895
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 static void FatalError(JNIEnv* env, const char* msg) {
897 ScopedJniThreadState ts(env);
898 LOG(FATAL) << "JNI FatalError called: " << msg;
899 }
900
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700901 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700903 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
904 return JNI_ERR;
905 }
906 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 return JNI_OK;
908 }
909
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700910 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700911 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700912 Object* survivor = Decode<Object*>(ts, java_survivor);
913 ts.Env()->PopFrame();
914 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 }
916
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700917 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700919 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
920 }
921
922 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
923 // TODO: we should try to expand the table if necessary.
924 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
925 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
926 return JNI_ERR;
927 }
928 // TODO: this isn't quite right, since "capacity" includes holes.
929 size_t capacity = ts.Env()->locals.Capacity();
930 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
931 if (!okay) {
932 ts.Self()->ThrowOutOfMemoryError(caller);
933 }
934 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700935 }
936
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
938 ScopedJniThreadState ts(env);
939 if (obj == NULL) {
940 return NULL;
941 }
942
Elliott Hughes75770752011-08-24 17:52:38 -0700943 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 IndirectReferenceTable& globals = vm->globals;
945 MutexLock mu(vm->globals_lock);
946 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
947 return reinterpret_cast<jobject>(ref);
948 }
949
950 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
951 ScopedJniThreadState ts(env);
952 if (obj == NULL) {
953 return;
954 }
955
Elliott Hughes75770752011-08-24 17:52:38 -0700956 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 IndirectReferenceTable& globals = vm->globals;
958 MutexLock mu(vm->globals_lock);
959
960 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
961 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
962 << "failed to find entry";
963 }
964 }
965
966 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
967 ScopedJniThreadState ts(env);
968 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
969 }
970
971 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
972 ScopedJniThreadState ts(env);
973 if (obj == NULL) {
974 return;
975 }
976
Elliott Hughes75770752011-08-24 17:52:38 -0700977 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 IndirectReferenceTable& weak_globals = vm->weak_globals;
979 MutexLock mu(vm->weak_globals_lock);
980
981 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
982 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
983 << "failed to find entry";
984 }
985 }
986
987 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
988 ScopedJniThreadState ts(env);
989 if (obj == NULL) {
990 return NULL;
991 }
992
993 IndirectReferenceTable& locals = ts.Env()->locals;
994
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700995 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
997 return reinterpret_cast<jobject>(ref);
998 }
999
1000 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
1001 ScopedJniThreadState ts(env);
1002 if (obj == NULL) {
1003 return;
1004 }
1005
1006 IndirectReferenceTable& locals = ts.Env()->locals;
1007
Ian Rogers5a7a74a2011-09-26 16:32:29 -07001008 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -07001009 if (!locals.Remove(cookie, obj)) {
1010 // Attempting to delete a local reference that is not in the
1011 // topmost local reference frame is a no-op. DeleteLocalRef returns
1012 // void and doesn't throw any exceptions, but we should probably
1013 // complain about it so the user will notice that things aren't
1014 // going quite the way they expect.
1015 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
1016 << "failed to find entry";
1017 }
1018 }
1019
1020 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
1021 ScopedJniThreadState ts(env);
1022 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
1023 ? JNI_TRUE : JNI_FALSE;
1024 }
1025
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001026 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001028 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001029 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001030 return NULL;
1031 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001032 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughese84278b2012-03-22 10:06:53 -07001035 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 ScopedJniThreadState ts(env);
1037 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001039 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 va_end(args);
1041 return result;
1042 }
1043
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001046 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001047 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001048 return NULL;
1049 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001050 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001051 if (result == NULL) {
1052 return NULL;
1053 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001054 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001056 if (!ts.Self()->IsExceptionPending()) {
1057 return local_result;
1058 } else {
1059 return NULL;
1060 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001065 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001066 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001067 return NULL;
1068 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001069 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001070 if (result == NULL) {
1071 return NULL;
1072 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001073 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001075 if (!ts.Self()->IsExceptionPending()) {
1076 return local_result;
1077 } else {
1078 return NULL;
1079 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1083 ScopedJniThreadState ts(env);
1084 return FindMethodID(ts, c, name, sig, false);
1085 }
1086
1087 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1088 ScopedJniThreadState ts(env);
1089 return FindMethodID(ts, c, name, sig, true);
1090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 va_list ap;
1095 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001096 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001098 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001103 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001104 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001109 JValue result(InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001110 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_list ap;
1116 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001117 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001119 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001124 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001129 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 va_list ap;
1135 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001136 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001138 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001143 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001148 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 va_list ap;
1154 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001155 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001157 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001162 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001167 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 va_list ap;
1173 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001174 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001176 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001181 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001186 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 va_list ap;
1192 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001193 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001195 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001200 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001205 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 va_list ap;
1211 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001212 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001214 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001219 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001224 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
1226
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 va_list ap;
1230 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001231 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001232 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001233 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001238 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001243 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001248 va_list ap;
1249 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001250 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001251 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001252 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 }
1254
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001257 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001262 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 }
1264
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 va_list ap;
1268 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001269 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001270 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 }
1272
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001275 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001280 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001283 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 ScopedJniThreadState ts(env);
1285 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001286 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001287 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001288 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 va_end(ap);
1290 return local_result;
1291 }
1292
1293 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001294 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001296 JValue result(InvokeWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001297 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
1300 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001301 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001303 JValue result(InvokeWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001304 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 }
1306
1307 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001308 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 ScopedJniThreadState ts(env);
1310 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001311 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001312 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001314 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
1317 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001318 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001320 return InvokeWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
1323 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001324 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001326 return InvokeWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001329 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 ScopedJniThreadState ts(env);
1331 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001333 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001335 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 }
1337
1338 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001339 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001341 return InvokeWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
1344 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001345 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001347 return InvokeWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001350 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 ScopedJniThreadState ts(env);
1352 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001354 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001356 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
1359 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001360 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001362 return InvokeWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
1365 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001366 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001368 return InvokeWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001371 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 ScopedJniThreadState ts(env);
1373 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001374 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001375 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001377 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
1380 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001381 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001383 return InvokeWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
1385
1386 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001387 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001389 return InvokeWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001392 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 ScopedJniThreadState ts(env);
1394 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001395 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001396 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001398 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
1401 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001402 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001404 return InvokeWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
1407 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001408 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001410 return InvokeWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 }
1412
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001413 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 ScopedJniThreadState ts(env);
1415 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001416 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001417 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001419 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
1422 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001423 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001425 return InvokeWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
1428 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001429 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001431 return InvokeWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001434 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 ScopedJniThreadState ts(env);
1436 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001437 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001438 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001440 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
1443 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001444 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001446 return InvokeWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 }
1448
1449 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001450 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001452 return InvokeWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001455 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 ScopedJniThreadState ts(env);
1457 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001458 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001459 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001461 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
1464 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001465 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001467 return InvokeWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
1470 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001471 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001473 return InvokeWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001476 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 ScopedJniThreadState ts(env);
1478 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001479 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001480 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 va_end(ap);
1482 }
1483
1484 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001485 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001487 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 }
1489
1490 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001491 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001493 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001496 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 ScopedJniThreadState ts(env);
1498 return FindFieldID(ts, c, name, sig, false);
1499 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001500
1501
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001502 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001503 ScopedJniThreadState ts(env);
1504 return FindFieldID(ts, c, name, sig, true);
1505 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001506
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001507 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001509 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001510 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001511 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001513
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001516 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001517 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001522 Object* o = Decode<Object*>(ts, java_object);
1523 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001524 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001525 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001530 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001531 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001532 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
1534
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535#define GET_PRIMITIVE_FIELD(fn, instance) \
1536 ScopedJniThreadState ts(env); \
1537 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001538 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001539 return f->fn(o)
1540
1541#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1542 ScopedJniThreadState ts(env); \
1543 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001544 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545 f->fn(o, value)
1546
1547 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1548 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 }
1550
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001551 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1552 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001555 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1556 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001559 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1560 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001563 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1564 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 }
1566
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001567 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1568 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001571 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1572 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001575 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1576 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001579 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001580 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001583 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001584 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001587 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001588 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001591 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 }
1594
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001595 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001596 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001599 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001600 GET_PRIMITIVE_FIELD(GetLong, NULL);
1601 }
1602
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001603 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001604 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1605 }
1606
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001607 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001608 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1609 }
1610
1611 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1612 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1613 }
1614
1615 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1616 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1617 }
1618
1619 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1620 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1621 }
1622
1623 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1624 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1625 }
1626
1627 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1628 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1629 }
1630
1631 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1632 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1633 }
1634
1635 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1636 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1637 }
1638
1639 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1640 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1641 }
1642
1643 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1644 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1645 }
1646
1647 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1648 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1649 }
1650
1651 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1652 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1653 }
1654
1655 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1656 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1657 }
1658
1659 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1660 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1661 }
1662
1663 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1664 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1665 }
1666
1667 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1668 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1669 }
1670
1671 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1672 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001675 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 ScopedJniThreadState ts(env);
1677 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001679 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001680 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 va_end(ap);
1682 return local_result;
1683 }
1684
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001685 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001687 JValue result(InvokeWithVarArgs(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001688 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 }
1690
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001691 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001693 JValue result(InvokeWithJValues(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001694 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 }
1696
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001697 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 ScopedJniThreadState ts(env);
1699 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001700 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001701 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001703 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 }
1705
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001706 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001708 return InvokeWithVarArgs(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001711 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001713 return InvokeWithJValues(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 }
1715
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001716 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 ScopedJniThreadState ts(env);
1718 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001719 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001720 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001722 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001725 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001727 return InvokeWithVarArgs(env, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001730 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001732 return InvokeWithJValues(env, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 }
1734
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001735 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 ScopedJniThreadState ts(env);
1737 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001738 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001739 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001741 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 }
1743
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001744 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001746 return InvokeWithVarArgs(env, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001749 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001751 return InvokeWithJValues(env, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 }
1753
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001754 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 ScopedJniThreadState ts(env);
1756 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001757 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001758 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001760 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001763 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001765 return InvokeWithVarArgs(env, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 }
1767
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001768 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001770 return InvokeWithJValues(env, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001773 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 ScopedJniThreadState ts(env);
1775 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001776 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001777 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001779 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 }
1781
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001782 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001784 return InvokeWithVarArgs(env, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001787 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001789 return InvokeWithJValues(env, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 }
1791
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001792 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 ScopedJniThreadState ts(env);
1794 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001795 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001796 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001798 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001801 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001803 return InvokeWithVarArgs(env, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001806 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001808 return InvokeWithJValues(env, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 }
1810
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001811 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 ScopedJniThreadState ts(env);
1813 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001814 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001815 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001817 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 }
1819
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001820 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001822 return InvokeWithVarArgs(env, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001825 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001827 return InvokeWithJValues(env, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 }
1829
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001830 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 ScopedJniThreadState ts(env);
1832 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001833 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001834 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001836 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001839 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001841 return InvokeWithVarArgs(env, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 }
1843
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001844 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001846 return InvokeWithJValues(env, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 }
1848
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001849 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 ScopedJniThreadState ts(env);
1851 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001852 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001853 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 va_end(ap);
1855 }
1856
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001857 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001859 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 }
1861
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001862 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001864 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
Elliott Hughes814e4032011-08-23 12:07:56 -07001867 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001869 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001870 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 }
1872
1873 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1874 ScopedJniThreadState ts(env);
1875 if (utf == NULL) {
1876 return NULL;
1877 }
1878 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001879 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 }
1881
Elliott Hughes814e4032011-08-23 12:07:56 -07001882 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1883 ScopedJniThreadState ts(env);
1884 return Decode<String*>(ts, java_string)->GetLength();
1885 }
1886
1887 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1888 ScopedJniThreadState ts(env);
1889 return Decode<String*>(ts, java_string)->GetUtfLength();
1890 }
1891
Elliott Hughesb465ab02011-08-24 11:21:21 -07001892 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001894 String* s = Decode<String*>(ts, java_string);
1895 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1896 ThrowSIOOBE(ts, start, length, s->GetLength());
1897 } else {
1898 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1899 memcpy(buf, chars + start, length * sizeof(jchar));
1900 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001901 }
1902
Elliott Hughesb465ab02011-08-24 11:21:21 -07001903 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001904 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001905 String* s = Decode<String*>(ts, java_string);
1906 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1907 ThrowSIOOBE(ts, start, length, s->GetLength());
1908 } else {
1909 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1910 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1911 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001912 }
1913
Elliott Hughes75770752011-08-24 17:52:38 -07001914 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001915 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001916 String* s = Decode<String*>(ts, java_string);
1917 const CharArray* chars = s->GetCharArray();
1918 PinPrimitiveArray(ts, chars);
1919 if (is_copy != NULL) {
1920 *is_copy = JNI_FALSE;
1921 }
1922 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001923 }
1924
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001925 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001926 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001927 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 }
1929
Elliott Hughes75770752011-08-24 17:52:38 -07001930 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001932 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes75770752011-08-24 17:52:38 -07001935 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001937 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 }
1939
Elliott Hughes75770752011-08-24 17:52:38 -07001940 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001941 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001942 if (java_string == NULL) {
1943 return NULL;
1944 }
1945 if (is_copy != NULL) {
1946 *is_copy = JNI_TRUE;
1947 }
1948 String* s = Decode<String*>(ts, java_string);
1949 size_t byte_count = s->GetUtfLength();
1950 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001951 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001952 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1953 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1954 bytes[byte_count] = '\0';
1955 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001956 }
1957
Elliott Hughes75770752011-08-24 17:52:38 -07001958 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001959 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001960 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001961 }
1962
Elliott Hughesbd935992011-08-22 11:59:34 -07001963 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001965 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001966 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001967 Array* array = obj->AsArray();
1968 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 }
1970
Elliott Hughes814e4032011-08-23 12:07:56 -07001971 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001973 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001974 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
1977 static void SetObjectArrayElement(JNIEnv* env,
1978 jobjectArray java_array, jsize index, jobject java_value) {
1979 ScopedJniThreadState ts(env);
1980 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1981 Object* value = Decode<Object*>(ts, java_value);
1982 array->Set(index, value);
1983 }
1984
1985 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1986 ScopedJniThreadState ts(env);
1987 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1988 }
1989
1990 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1991 ScopedJniThreadState ts(env);
1992 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1993 }
1994
1995 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1996 ScopedJniThreadState ts(env);
1997 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1998 }
1999
2000 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
2001 ScopedJniThreadState ts(env);
2002 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
2003 }
2004
2005 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
2006 ScopedJniThreadState ts(env);
2007 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
2008 }
2009
2010 static jintArray NewIntArray(JNIEnv* env, jsize length) {
2011 ScopedJniThreadState ts(env);
2012 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
2013 }
2014
2015 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2016 ScopedJniThreadState ts(env);
2017 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
2018 }
2019
2020 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
2021 ScopedJniThreadState ts(env);
2022 CHECK_GE(length, 0); // TODO: ReportJniError
2023
2024 // Compute the array class corresponding to the given element class.
2025 Class* element_class = Decode<Class*>(ts, element_jclass);
2026 std::string descriptor;
2027 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002028 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002029
2030 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002031 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2032 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 return NULL;
2034 }
2035
Elliott Hughes75770752011-08-24 17:52:38 -07002036 // Allocate and initialize if necessary.
2037 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002039 if (initial_element != NULL) {
2040 Object* initial_object = Decode<Object*>(ts, initial_element);
2041 for (jsize i = 0; i < length; ++i) {
2042 result->Set(i, initial_object);
2043 }
2044 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002045 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
2048 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2049 ScopedJniThreadState ts(env);
2050 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2051 }
2052
Ian Rogersa15e67d2012-02-28 13:51:55 -08002053 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002054 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002055 Array* array = Decode<Array*>(ts, java_array);
2056 PinPrimitiveArray(ts, array);
2057 if (is_copy != NULL) {
2058 *is_copy = JNI_FALSE;
2059 }
2060 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002061 }
2062
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002063 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002064 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002065 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002066 }
2067
Elliott Hughes75770752011-08-24 17:52:38 -07002068 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray 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<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes75770752011-08-24 17:52:38 -07002073 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray 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<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
Elliott Hughes75770752011-08-24 17:52:38 -07002078 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray 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<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Elliott Hughes75770752011-08-24 17:52:38 -07002083 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray 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<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes75770752011-08-24 17:52:38 -07002088 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray 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<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
Elliott Hughes75770752011-08-24 17:52:38 -07002093 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002095 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
Elliott Hughes75770752011-08-24 17:52:38 -07002098 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002100 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
Elliott Hughes75770752011-08-24 17:52:38 -07002103 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002105 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002108 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, 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 ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, 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 ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, 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 ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, 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 ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, 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 Hughes1bac54f2012-03-16 12:48:31 -07002133 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002135 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 }
2137
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002138 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002140 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002143 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002145 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Elliott Hughes814e4032011-08-23 12:07:56 -07002148 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002150 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Elliott Hughes814e4032011-08-23 12:07:56 -07002153 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002155 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Elliott Hughes814e4032011-08-23 12:07:56 -07002158 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002160 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Elliott Hughes814e4032011-08-23 12:07:56 -07002163 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002165 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 }
2167
Elliott Hughes814e4032011-08-23 12:07:56 -07002168 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002170 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Elliott Hughes814e4032011-08-23 12:07:56 -07002173 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002175 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes814e4032011-08-23 12:07:56 -07002178 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002180 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
Elliott Hughes814e4032011-08-23 12:07:56 -07002183 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002185 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 }
2187
Elliott Hughes814e4032011-08-23 12:07:56 -07002188 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002190 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Elliott Hughes814e4032011-08-23 12:07:56 -07002193 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002195 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Elliott Hughes814e4032011-08-23 12:07:56 -07002198 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002200 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
Elliott Hughes814e4032011-08-23 12:07:56 -07002203 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002205 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
Elliott Hughes814e4032011-08-23 12:07:56 -07002208 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002210 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
Elliott Hughes814e4032011-08-23 12:07:56 -07002213 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002215 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 }
2217
Elliott Hughes814e4032011-08-23 12:07:56 -07002218 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002220 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 }
2222
Elliott Hughes814e4032011-08-23 12:07:56 -07002223 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002225 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227
Elliott Hughes5174fe62011-08-23 15:12:35 -07002228 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002230 Class* c = Decode<Class*>(ts, java_class);
2231
Elliott Hughes5174fe62011-08-23 15:12:35 -07002232 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 const char* name = methods[i].name;
2234 const char* sig = methods[i].signature;
2235
2236 if (*sig == '!') {
2237 // TODO: fast jni. it's too noisy to log all these.
2238 ++sig;
2239 }
2240
Elliott Hughes5174fe62011-08-23 15:12:35 -07002241 Method* m = c->FindDirectMethod(name, sig);
2242 if (m == NULL) {
2243 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002245 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002246 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002247 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002248 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002249 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002250 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002251 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002252 return JNI_ERR;
2253 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002254
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002255 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002256
Ian Rogers60db5ab2012-02-20 17:02:00 -08002257 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002258 }
2259 return JNI_OK;
2260 }
2261
Elliott Hughes5174fe62011-08-23 15:12:35 -07002262 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002264 Class* c = Decode<Class*>(ts, java_class);
2265
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002266 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002267
2268 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2269 Method* m = c->GetDirectMethod(i);
2270 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002271 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002272 }
2273 }
2274 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2275 Method* m = c->GetVirtualMethod(i);
2276 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002277 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002278 }
2279 }
2280
2281 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002282 }
2283
Elliott Hughes72025e52011-08-23 17:50:30 -07002284 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002286 Object* o = Decode<Object*>(ts, java_object);
2287 o->MonitorEnter(ts.Self());
2288 if (ts.Self()->IsExceptionPending()) {
2289 return JNI_ERR;
2290 }
2291 ts.Env()->monitors.Add(o);
2292 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002293 }
2294
Elliott Hughes72025e52011-08-23 17:50:30 -07002295 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002297 Object* o = Decode<Object*>(ts, java_object);
2298 o->MonitorExit(ts.Self());
2299 if (ts.Self()->IsExceptionPending()) {
2300 return JNI_ERR;
2301 }
2302 ts.Env()->monitors.Remove(o);
2303 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002304 }
2305
2306 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2307 ScopedJniThreadState ts(env);
2308 Runtime* runtime = Runtime::Current();
2309 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002310 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 } else {
2312 *vm = NULL;
2313 }
2314 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2315 }
2316
Elliott Hughescdf53122011-08-19 15:46:09 -07002317 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2318 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002319
2320 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002321 CHECK(address != NULL); // TODO: ReportJniError
2322 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002323
2324 jclass buffer_class = GetDirectByteBufferClass(env);
2325 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2326 if (mid == NULL) {
2327 return NULL;
2328 }
2329
2330 // At the moment, the Java side is limited to 32 bits.
2331 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2332 CHECK_LE(capacity, 0xffffffff);
2333 jint address_arg = reinterpret_cast<jint>(address);
2334 jint capacity_arg = static_cast<jint>(capacity);
2335
2336 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2337 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002338 }
2339
Elliott Hughesb465ab02011-08-24 11:21:21 -07002340 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002341 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002342 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2343 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002344 }
2345
Elliott Hughesb465ab02011-08-24 11:21:21 -07002346 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002347 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002348 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2349 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 }
2351
Elliott Hughesb465ab02011-08-24 11:21:21 -07002352 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002353 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002354
Elliott Hughes75770752011-08-24 17:52:38 -07002355 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002356
2357 // Do we definitely know what kind of reference this is?
2358 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2359 IndirectRefKind kind = GetIndirectRefKind(ref);
2360 switch (kind) {
2361 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002362 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2363 return JNILocalRefType;
2364 }
2365 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002366 case kGlobal:
2367 return JNIGlobalRefType;
2368 case kWeakGlobal:
2369 return JNIWeakGlobalRefType;
2370 case kSirtOrInvalid:
2371 // Is it in a stack IRT?
TDYa12728f1a142012-03-15 21:51:52 -07002372 if (ts.Self()->StackReferencesContain(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002373 return JNILocalRefType;
2374 }
2375
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002376 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002377 return JNIInvalidRefType;
2378 }
2379
Elliott Hughesb465ab02011-08-24 11:21:21 -07002380 // If we're handing out direct pointers, check whether it's a direct pointer
2381 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002382 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002383 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002384 return JNILocalRefType;
2385 }
2386 }
2387
2388 return JNIInvalidRefType;
2389 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002390 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2391 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002392 }
2393};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002394
Elliott Hughes88c5c352012-03-15 18:49:48 -07002395const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002396 NULL, // reserved0.
2397 NULL, // reserved1.
2398 NULL, // reserved2.
2399 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002400 JNI::GetVersion,
2401 JNI::DefineClass,
2402 JNI::FindClass,
2403 JNI::FromReflectedMethod,
2404 JNI::FromReflectedField,
2405 JNI::ToReflectedMethod,
2406 JNI::GetSuperclass,
2407 JNI::IsAssignableFrom,
2408 JNI::ToReflectedField,
2409 JNI::Throw,
2410 JNI::ThrowNew,
2411 JNI::ExceptionOccurred,
2412 JNI::ExceptionDescribe,
2413 JNI::ExceptionClear,
2414 JNI::FatalError,
2415 JNI::PushLocalFrame,
2416 JNI::PopLocalFrame,
2417 JNI::NewGlobalRef,
2418 JNI::DeleteGlobalRef,
2419 JNI::DeleteLocalRef,
2420 JNI::IsSameObject,
2421 JNI::NewLocalRef,
2422 JNI::EnsureLocalCapacity,
2423 JNI::AllocObject,
2424 JNI::NewObject,
2425 JNI::NewObjectV,
2426 JNI::NewObjectA,
2427 JNI::GetObjectClass,
2428 JNI::IsInstanceOf,
2429 JNI::GetMethodID,
2430 JNI::CallObjectMethod,
2431 JNI::CallObjectMethodV,
2432 JNI::CallObjectMethodA,
2433 JNI::CallBooleanMethod,
2434 JNI::CallBooleanMethodV,
2435 JNI::CallBooleanMethodA,
2436 JNI::CallByteMethod,
2437 JNI::CallByteMethodV,
2438 JNI::CallByteMethodA,
2439 JNI::CallCharMethod,
2440 JNI::CallCharMethodV,
2441 JNI::CallCharMethodA,
2442 JNI::CallShortMethod,
2443 JNI::CallShortMethodV,
2444 JNI::CallShortMethodA,
2445 JNI::CallIntMethod,
2446 JNI::CallIntMethodV,
2447 JNI::CallIntMethodA,
2448 JNI::CallLongMethod,
2449 JNI::CallLongMethodV,
2450 JNI::CallLongMethodA,
2451 JNI::CallFloatMethod,
2452 JNI::CallFloatMethodV,
2453 JNI::CallFloatMethodA,
2454 JNI::CallDoubleMethod,
2455 JNI::CallDoubleMethodV,
2456 JNI::CallDoubleMethodA,
2457 JNI::CallVoidMethod,
2458 JNI::CallVoidMethodV,
2459 JNI::CallVoidMethodA,
2460 JNI::CallNonvirtualObjectMethod,
2461 JNI::CallNonvirtualObjectMethodV,
2462 JNI::CallNonvirtualObjectMethodA,
2463 JNI::CallNonvirtualBooleanMethod,
2464 JNI::CallNonvirtualBooleanMethodV,
2465 JNI::CallNonvirtualBooleanMethodA,
2466 JNI::CallNonvirtualByteMethod,
2467 JNI::CallNonvirtualByteMethodV,
2468 JNI::CallNonvirtualByteMethodA,
2469 JNI::CallNonvirtualCharMethod,
2470 JNI::CallNonvirtualCharMethodV,
2471 JNI::CallNonvirtualCharMethodA,
2472 JNI::CallNonvirtualShortMethod,
2473 JNI::CallNonvirtualShortMethodV,
2474 JNI::CallNonvirtualShortMethodA,
2475 JNI::CallNonvirtualIntMethod,
2476 JNI::CallNonvirtualIntMethodV,
2477 JNI::CallNonvirtualIntMethodA,
2478 JNI::CallNonvirtualLongMethod,
2479 JNI::CallNonvirtualLongMethodV,
2480 JNI::CallNonvirtualLongMethodA,
2481 JNI::CallNonvirtualFloatMethod,
2482 JNI::CallNonvirtualFloatMethodV,
2483 JNI::CallNonvirtualFloatMethodA,
2484 JNI::CallNonvirtualDoubleMethod,
2485 JNI::CallNonvirtualDoubleMethodV,
2486 JNI::CallNonvirtualDoubleMethodA,
2487 JNI::CallNonvirtualVoidMethod,
2488 JNI::CallNonvirtualVoidMethodV,
2489 JNI::CallNonvirtualVoidMethodA,
2490 JNI::GetFieldID,
2491 JNI::GetObjectField,
2492 JNI::GetBooleanField,
2493 JNI::GetByteField,
2494 JNI::GetCharField,
2495 JNI::GetShortField,
2496 JNI::GetIntField,
2497 JNI::GetLongField,
2498 JNI::GetFloatField,
2499 JNI::GetDoubleField,
2500 JNI::SetObjectField,
2501 JNI::SetBooleanField,
2502 JNI::SetByteField,
2503 JNI::SetCharField,
2504 JNI::SetShortField,
2505 JNI::SetIntField,
2506 JNI::SetLongField,
2507 JNI::SetFloatField,
2508 JNI::SetDoubleField,
2509 JNI::GetStaticMethodID,
2510 JNI::CallStaticObjectMethod,
2511 JNI::CallStaticObjectMethodV,
2512 JNI::CallStaticObjectMethodA,
2513 JNI::CallStaticBooleanMethod,
2514 JNI::CallStaticBooleanMethodV,
2515 JNI::CallStaticBooleanMethodA,
2516 JNI::CallStaticByteMethod,
2517 JNI::CallStaticByteMethodV,
2518 JNI::CallStaticByteMethodA,
2519 JNI::CallStaticCharMethod,
2520 JNI::CallStaticCharMethodV,
2521 JNI::CallStaticCharMethodA,
2522 JNI::CallStaticShortMethod,
2523 JNI::CallStaticShortMethodV,
2524 JNI::CallStaticShortMethodA,
2525 JNI::CallStaticIntMethod,
2526 JNI::CallStaticIntMethodV,
2527 JNI::CallStaticIntMethodA,
2528 JNI::CallStaticLongMethod,
2529 JNI::CallStaticLongMethodV,
2530 JNI::CallStaticLongMethodA,
2531 JNI::CallStaticFloatMethod,
2532 JNI::CallStaticFloatMethodV,
2533 JNI::CallStaticFloatMethodA,
2534 JNI::CallStaticDoubleMethod,
2535 JNI::CallStaticDoubleMethodV,
2536 JNI::CallStaticDoubleMethodA,
2537 JNI::CallStaticVoidMethod,
2538 JNI::CallStaticVoidMethodV,
2539 JNI::CallStaticVoidMethodA,
2540 JNI::GetStaticFieldID,
2541 JNI::GetStaticObjectField,
2542 JNI::GetStaticBooleanField,
2543 JNI::GetStaticByteField,
2544 JNI::GetStaticCharField,
2545 JNI::GetStaticShortField,
2546 JNI::GetStaticIntField,
2547 JNI::GetStaticLongField,
2548 JNI::GetStaticFloatField,
2549 JNI::GetStaticDoubleField,
2550 JNI::SetStaticObjectField,
2551 JNI::SetStaticBooleanField,
2552 JNI::SetStaticByteField,
2553 JNI::SetStaticCharField,
2554 JNI::SetStaticShortField,
2555 JNI::SetStaticIntField,
2556 JNI::SetStaticLongField,
2557 JNI::SetStaticFloatField,
2558 JNI::SetStaticDoubleField,
2559 JNI::NewString,
2560 JNI::GetStringLength,
2561 JNI::GetStringChars,
2562 JNI::ReleaseStringChars,
2563 JNI::NewStringUTF,
2564 JNI::GetStringUTFLength,
2565 JNI::GetStringUTFChars,
2566 JNI::ReleaseStringUTFChars,
2567 JNI::GetArrayLength,
2568 JNI::NewObjectArray,
2569 JNI::GetObjectArrayElement,
2570 JNI::SetObjectArrayElement,
2571 JNI::NewBooleanArray,
2572 JNI::NewByteArray,
2573 JNI::NewCharArray,
2574 JNI::NewShortArray,
2575 JNI::NewIntArray,
2576 JNI::NewLongArray,
2577 JNI::NewFloatArray,
2578 JNI::NewDoubleArray,
2579 JNI::GetBooleanArrayElements,
2580 JNI::GetByteArrayElements,
2581 JNI::GetCharArrayElements,
2582 JNI::GetShortArrayElements,
2583 JNI::GetIntArrayElements,
2584 JNI::GetLongArrayElements,
2585 JNI::GetFloatArrayElements,
2586 JNI::GetDoubleArrayElements,
2587 JNI::ReleaseBooleanArrayElements,
2588 JNI::ReleaseByteArrayElements,
2589 JNI::ReleaseCharArrayElements,
2590 JNI::ReleaseShortArrayElements,
2591 JNI::ReleaseIntArrayElements,
2592 JNI::ReleaseLongArrayElements,
2593 JNI::ReleaseFloatArrayElements,
2594 JNI::ReleaseDoubleArrayElements,
2595 JNI::GetBooleanArrayRegion,
2596 JNI::GetByteArrayRegion,
2597 JNI::GetCharArrayRegion,
2598 JNI::GetShortArrayRegion,
2599 JNI::GetIntArrayRegion,
2600 JNI::GetLongArrayRegion,
2601 JNI::GetFloatArrayRegion,
2602 JNI::GetDoubleArrayRegion,
2603 JNI::SetBooleanArrayRegion,
2604 JNI::SetByteArrayRegion,
2605 JNI::SetCharArrayRegion,
2606 JNI::SetShortArrayRegion,
2607 JNI::SetIntArrayRegion,
2608 JNI::SetLongArrayRegion,
2609 JNI::SetFloatArrayRegion,
2610 JNI::SetDoubleArrayRegion,
2611 JNI::RegisterNatives,
2612 JNI::UnregisterNatives,
2613 JNI::MonitorEnter,
2614 JNI::MonitorExit,
2615 JNI::GetJavaVM,
2616 JNI::GetStringRegion,
2617 JNI::GetStringUTFRegion,
2618 JNI::GetPrimitiveArrayCritical,
2619 JNI::ReleasePrimitiveArrayCritical,
2620 JNI::GetStringCritical,
2621 JNI::ReleaseStringCritical,
2622 JNI::NewWeakGlobalRef,
2623 JNI::DeleteWeakGlobalRef,
2624 JNI::ExceptionCheck,
2625 JNI::NewDirectByteBuffer,
2626 JNI::GetDirectBufferAddress,
2627 JNI::GetDirectBufferCapacity,
2628 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002629};
2630
Elliott Hughes75770752011-08-24 17:52:38 -07002631JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002632 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002633 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002634 local_ref_cookie(IRT_FIRST_SEGMENT),
2635 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002636 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002637 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002638 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002639 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002640 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002641 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002642 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002643 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2644 // errors will ensue.
2645 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2646 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002647}
2648
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002649JNIEnvExt::~JNIEnvExt() {
2650}
2651
Elliott Hughes88c5c352012-03-15 18:49:48 -07002652void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2653 check_jni = enabled;
2654 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002655}
2656
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002657void JNIEnvExt::DumpReferenceTables() {
2658 locals.Dump();
2659 monitors.Dump();
2660}
2661
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002662void JNIEnvExt::PushFrame(int /*capacity*/) {
2663 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002664 stacked_local_ref_cookies.push_back(local_ref_cookie);
2665 local_ref_cookie = locals.GetSegmentState();
2666}
2667
2668void JNIEnvExt::PopFrame() {
2669 locals.SetSegmentState(local_ref_cookie);
2670 local_ref_cookie = stacked_local_ref_cookies.back();
2671 stacked_local_ref_cookies.pop_back();
2672}
2673
Carl Shapiroea4dca82011-08-01 13:45:38 -07002674// JNI Invocation interface.
2675
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002676extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2677 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2678 if (args->version < JNI_VERSION_1_2) {
2679 return JNI_EVERSION;
2680 }
2681 Runtime::Options options;
2682 for (int i = 0; i < args->nOptions; ++i) {
2683 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002684 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002685 }
2686 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002687 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002688 if (runtime == NULL) {
2689 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002690 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002691 runtime->Start();
2692 *p_env = Thread::Current()->GetJniEnv();
2693 *p_vm = runtime->GetJavaVM();
2694 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002695}
2696
Elliott Hughesf2682d52011-08-15 16:37:04 -07002697extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002698 Runtime* runtime = Runtime::Current();
2699 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002700 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002701 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002702 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002703 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002704 }
2705 return JNI_OK;
2706}
2707
2708// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002709extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002710 return JNI_ERR;
2711}
2712
Elliott Hughescdf53122011-08-19 15:46:09 -07002713class JII {
2714 public:
2715 static jint DestroyJavaVM(JavaVM* vm) {
2716 if (vm == NULL) {
2717 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002718 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002719 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2720 delete raw_vm->runtime;
2721 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002722 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002723
Elliott Hughescdf53122011-08-19 15:46:09 -07002724 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002725 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002726 }
2727
2728 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002729 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002730 }
2731
2732 static jint DetachCurrentThread(JavaVM* vm) {
2733 if (vm == NULL) {
2734 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002735 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002736 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2737 Runtime* runtime = raw_vm->runtime;
2738 runtime->DetachCurrentThread();
2739 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002740 }
2741
2742 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2743 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2744 return JNI_EVERSION;
2745 }
2746 if (vm == NULL || env == NULL) {
2747 return JNI_ERR;
2748 }
2749 Thread* thread = Thread::Current();
2750 if (thread == NULL) {
2751 *env = NULL;
2752 return JNI_EDETACHED;
2753 }
2754 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002755 return JNI_OK;
2756 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002757};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002758
Elliott Hughes88c5c352012-03-15 18:49:48 -07002759const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002760 NULL, // reserved0
2761 NULL, // reserved1
2762 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002763 JII::DestroyJavaVM,
2764 JII::AttachCurrentThread,
2765 JII::DetachCurrentThread,
2766 JII::GetEnv,
2767 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002768};
2769
Elliott Hughesa0957642011-09-02 14:27:33 -07002770JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002771 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002772 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002773 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002774 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002775 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002776 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002777 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002778 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002779 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002780 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002781 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002782 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002783 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002784 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002785 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002786 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002787 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002788 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002789 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002790}
2791
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002792JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002793 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002794}
2795
Elliott Hughes88c5c352012-03-15 18:49:48 -07002796void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2797 check_jni = enabled;
2798 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002799}
2800
Elliott Hughesae80b492012-04-24 10:43:17 -07002801void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2802 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2803 if (force_copy) {
2804 os << " (with forcecopy)";
2805 }
2806 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
2807 {
2808 MutexLock mu(pins_lock);
2809 os << "; pins=" << pin_table.Size();
2810 }
2811 {
2812 MutexLock mu(globals_lock);
2813 os << "; globals=" << globals.Capacity();
2814 }
2815 {
2816 MutexLock mu(weak_globals_lock);
2817 if (weak_globals.Capacity() > 0) {
2818 os << " (plus " << weak_globals.Capacity() << " weak)";
2819 }
2820 }
2821 os << '\n';
2822
2823 {
2824 MutexLock mu(libraries_lock);
2825 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2826 }
2827}
2828
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002829void JavaVMExt::DumpReferenceTables() {
2830 {
2831 MutexLock mu(globals_lock);
2832 globals.Dump();
2833 }
2834 {
2835 MutexLock mu(weak_globals_lock);
2836 weak_globals.Dump();
2837 }
2838 {
2839 MutexLock mu(pins_lock);
2840 pin_table.Dump();
2841 }
2842}
2843
Elliott Hughes75770752011-08-24 17:52:38 -07002844bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2845 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002846
2847 // See if we've already loaded this library. If we have, and the class loader
2848 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002849 // TODO: for better results we should canonicalize the pathname (or even compare
2850 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002851 SharedLibrary* library;
2852 {
2853 // TODO: move the locking (and more of this logic) into Libraries.
2854 MutexLock mu(libraries_lock);
2855 library = libraries->Get(path);
2856 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002857 if (library != NULL) {
2858 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002859 // The library will be associated with class_loader. The JNI
2860 // spec says we can't load the same library into more than one
2861 // class loader.
2862 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2863 "ClassLoader %p; can't open in ClassLoader %p",
2864 path.c_str(), library->GetClassLoader(), class_loader);
2865 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002866 return false;
2867 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002868 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2869 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002870 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002871 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2872 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002873 return false;
2874 }
2875 return true;
2876 }
2877
2878 // Open the shared library. Because we're using a full path, the system
2879 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2880 // resolve this library's dependencies though.)
2881
2882 // Failures here are expected when java.library.path has several entries
2883 // and we have to hunt for the lib.
2884
2885 // The current version of the dynamic linker prints detailed information
2886 // about dlopen() failures. Some things to check if the message is
2887 // cryptic:
2888 // - make sure the library exists on the device
2889 // - verify that the right path is being opened (the debug log message
2890 // above can help with that)
2891 // - check to see if the library is valid (e.g. not zero bytes long)
2892 // - check config/prelink-linux-arm.map to ensure that the library
2893 // is listed and is not being overrun by the previous entry (if
2894 // loading suddenly stops working on a prelinked library, this is
2895 // a good one to check)
2896 // - write a trivial app that calls sleep() then dlopen(), attach
2897 // to it with "strace -p <pid>" while it sleeps, and watch for
2898 // attempts to open nonexistent dependent shared libs
2899
2900 // TODO: automate some of these checks!
2901
2902 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002903 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002904 // the GC to ignore us.
2905 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002906 void* handle = NULL;
2907 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002908 ScopedThreadStateChange tsc(self, kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002909 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002910 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002911
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002912 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002913
2914 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002915 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002916 return false;
2917 }
2918
2919 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002920 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002921 // TODO: move the locking (and more of this logic) into Libraries.
2922 MutexLock mu(libraries_lock);
2923 library = libraries->Get(path);
2924 if (library != NULL) {
2925 LOG(INFO) << "WOW: we lost a race to add shared library: "
2926 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002927 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002928 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002929 library = new SharedLibrary(path, handle, class_loader);
2930 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002931 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002932
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002933 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002934
2935 bool result = true;
2936 void* sym = dlsym(handle, "JNI_OnLoad");
2937 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002938 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002939 } else {
2940 // Call JNI_OnLoad. We have to override the current class
2941 // loader, which will always be "null" since the stuff at the
2942 // top of the stack is around Runtime.loadLibrary(). (See
2943 // the comments in the JNI FindClass function.)
2944 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2945 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002946 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002947 self->SetClassLoaderOverride(class_loader);
2948
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002949 int version = 0;
2950 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002951 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002952 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002953 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002954 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002955
Brian Carlstromaded5f72011-10-07 17:15:04 -07002956 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002957
2958 if (version != JNI_VERSION_1_2 &&
2959 version != JNI_VERSION_1_4 &&
2960 version != JNI_VERSION_1_6) {
2961 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2962 << "bad version: " << version;
2963 // It's unwise to call dlclose() here, but we can mark it
2964 // as bad and ensure that future load attempts will fail.
2965 // We don't know how far JNI_OnLoad got, so there could
2966 // be some partially-initialized stuff accessible through
2967 // newly-registered native method calls. We could try to
2968 // unregister them, but that doesn't seem worthwhile.
2969 result = false;
2970 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002971 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2972 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002973 }
2974 }
2975
2976 library->SetResult(result);
2977 return result;
2978}
2979
2980void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2981 CHECK(m->IsNative());
2982
2983 Class* c = m->GetDeclaringClass();
2984
2985 // If this is a static method, it could be called before the class
2986 // has been initialized.
2987 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002988 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002989 return NULL;
2990 }
2991 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002992 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002993 }
2994
Brian Carlstrom16192862011-09-12 17:50:06 -07002995 std::string detail;
2996 void* native_method;
2997 {
2998 MutexLock mu(libraries_lock);
2999 native_method = libraries->FindNativeMethod(m, detail);
3000 }
3001 // throwing can cause libraries_lock to be reacquired
3002 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07003003 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003004 }
3005 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003006}
3007
Elliott Hughes410c0c82011-09-01 17:58:25 -07003008void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
3009 {
3010 MutexLock mu(globals_lock);
3011 globals.VisitRoots(visitor, arg);
3012 }
3013 {
3014 MutexLock mu(pins_lock);
3015 pin_table.VisitRoots(visitor, arg);
3016 }
3017 // The weak_globals table is visited by the GC itself (because it mutates the table).
3018}
3019
Elliott Hughes844f9a02012-01-24 20:19:58 -08003020jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
3021 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
3022 if (c.get() == NULL) {
3023 return NULL;
3024 }
3025 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
3026}
3027
Ian Rogersdf20fe02011-07-20 20:34:16 -07003028} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003029
3030std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3031 switch (rhs) {
3032 case JNIInvalidRefType:
3033 os << "JNIInvalidRefType";
3034 return os;
3035 case JNILocalRefType:
3036 os << "JNILocalRefType";
3037 return os;
3038 case JNIGlobalRefType:
3039 os << "JNIGlobalRefType";
3040 return os;
3041 case JNIWeakGlobalRefType:
3042 os << "JNIWeakGlobalRefType";
3043 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003044 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003045 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003046 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003047 }
3048}