blob: 87d5449b3d2ebaaab8a2a68277c885b13730e470 [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"
Elliott Hugheseac76672012-05-24 21:56:51 -070040#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070041
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070042namespace art {
43
Elliott Hughes2ced6a52011-10-16 18:44:48 -070044static const size_t kMonitorsInitial = 32; // Arbitrary.
45static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
46
47static const size_t kLocalsInitial = 64; // Arbitrary.
48static const size_t kLocalsMax = 512; // Arbitrary sanity check.
49
50static const size_t kPinTableInitial = 16; // Arbitrary.
51static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
52
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053static size_t gGlobalsInitial = 512; // Arbitrary.
54static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070055
56static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
57static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
58
Elliott Hugheseac76672012-05-24 21:56:51 -070059void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods, size_t method_count) {
60 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
61 if (c.get() == NULL) {
62 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
63 }
64 if (env->RegisterNatives(c.get(), methods, method_count) != JNI_OK) {
65 LOG(FATAL) << "Failed to register natives methods: " << jni_class_name;
66 }
67}
68
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070069void SetJniGlobalsMax(size_t max) {
70 if (max != 0) {
71 gGlobalsMax = max;
72 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
73 }
74}
Ian Rogersdf20fe02011-07-20 20:34:16 -070075
Elliott Hughesc5f7c912011-08-18 14:00:42 -070076/*
77 * Add a local reference for an object to the current stack frame. When
78 * the native function returns, the reference will be discarded.
79 *
80 * We need to allow the same reference to be added multiple times.
81 *
82 * This will be called on otherwise unreferenced objects. We cannot do
83 * GC allocations here, and it's best if we don't grab a mutex.
84 *
85 * Returns the local reference (currently just the same pointer that was
86 * passed in), or NULL on failure.
87 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070088template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070089T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
90 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
91 Object* obj = const_cast<Object*>(const_obj);
92
Elliott Hughesc5f7c912011-08-18 14:00:42 -070093 if (obj == NULL) {
94 return NULL;
95 }
96
Elliott Hughes9c750f92012-04-05 12:07:59 -070097 DCHECK((reinterpret_cast<uintptr_t>(obj) & 0xffff0000) != 0xebad0000);
98
Elliott Hughesbf86d042011-08-31 17:53:14 -070099 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
100 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700101
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700102 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700103 IndirectRef ref = locals.Add(cookie, obj);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700104
105#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700106 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700107 size_t entry_count = locals.Capacity();
108 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700109 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes73e66f72012-05-09 09:34:45 -0700110 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")\n"
111 << Dumpable<IndirectReferenceTable>(locals);
112 // TODO: LOG(FATAL) in a later release?
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700113 }
114 }
115#endif
116
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800117 if (env->vm->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700118 // Hand out direct pointers to support broken old apps.
119 return reinterpret_cast<T>(obj);
120 }
121
122 return reinterpret_cast<T>(ref);
123}
Brian Carlstrom51477332012-03-25 20:20:26 -0700124// Explicit instantiations
125template jclass AddLocalReference<jclass>(JNIEnv* public_env, const Object* const_obj);
126template jobject AddLocalReference<jobject>(JNIEnv* public_env, const Object* const_obj);
127template jobjectArray AddLocalReference<jobjectArray>(JNIEnv* public_env, const Object* const_obj);
128template jstring AddLocalReference<jstring>(JNIEnv* public_env, const Object* const_obj);
129template jthrowable AddLocalReference<jthrowable>(JNIEnv* public_env, const Object* const_obj);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700130
Elliott Hughesbf86d042011-08-31 17:53:14 -0700131// For external use.
132template<typename T>
133T Decode(JNIEnv* public_env, jobject obj) {
134 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
135 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
136}
Shih-wei Liao24782c62012-01-08 12:46:11 -0800137// TODO: Change to use template when Mac OS build server no longer uses GCC 4.2.*.
138Object* DecodeObj(JNIEnv* public_env, jobject obj) {
139 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
140 return reinterpret_cast<Object*>(env->self->DecodeJObject(obj));
141}
Elliott Hughesbf86d042011-08-31 17:53:14 -0700142// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700143template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700144template Class* Decode<Class*>(JNIEnv*, jobject);
145template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
146template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700147template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700148template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700149template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700150template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400151template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700152template String* Decode<String*>(JNIEnv*, jobject);
153template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700154
Ian Rogers45619fc2012-02-29 11:15:25 -0800155size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
156 size_t num_bytes = 0;
157 for (size_t i = 1; i < shorty_len; ++i) {
158 char ch = shorty[i];
159 if (ch == 'D' || ch == 'J') {
160 num_bytes += 8;
161 } else if (ch == 'L') {
162 // Argument is a reference or an array. The shorty descriptor
163 // does not distinguish between these types.
164 num_bytes += sizeof(Object*);
165 } else {
166 num_bytes += 4;
167 }
168 }
169 return num_bytes;
170}
171
172class ArgArray {
173 public:
174 explicit ArgArray(Method* method) {
175 MethodHelper mh(method);
176 shorty_ = mh.GetShorty();
177 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -0700178 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800179 arg_array_ = small_arg_array_;
180 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700181 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800182 arg_array_ = large_arg_array_.get();
183 }
184 }
185
Elliott Hughes77405792012-03-15 15:22:12 -0700186 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800187 return arg_array_;
188 }
189
190 void BuildArgArray(JNIEnv* public_env, va_list ap) {
191 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700192 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800193 switch (shorty_[i]) {
194 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700195 arg_array_[offset].SetZ(va_arg(ap, jint));
196 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800197 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700198 arg_array_[offset].SetB(va_arg(ap, jint));
199 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800200 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700201 arg_array_[offset].SetC(va_arg(ap, jint));
202 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800203 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700204 arg_array_[offset].SetS(va_arg(ap, jint));
205 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800206 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700207 arg_array_[offset].SetI(va_arg(ap, jint));
Ian Rogers45619fc2012-02-29 11:15:25 -0800208 break;
209 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700210 arg_array_[offset].SetF(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800211 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700212 case 'L':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700213 arg_array_[offset].SetL(DecodeObj(env, va_arg(ap, jobject)));
Ian Rogers45619fc2012-02-29 11:15:25 -0800214 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800215 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700216 arg_array_[offset].SetD(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800217 break;
218 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700219 arg_array_[offset].SetJ(va_arg(ap, jlong));
Ian Rogers45619fc2012-02-29 11:15:25 -0800220 break;
221 }
222 }
223 }
224
225 void BuildArgArray(JNIEnv* public_env, jvalue* args) {
226 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700227 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800228 switch (shorty_[i]) {
229 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700230 arg_array_[offset].SetZ(args[offset].z);
231 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800232 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700233 arg_array_[offset].SetB(args[offset].b);
234 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800235 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700236 arg_array_[offset].SetC(args[offset].c);
237 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800238 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700239 arg_array_[offset].SetS(args[offset].s);
240 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800241 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700242 arg_array_[offset].SetI(args[offset].i);
Ian Rogers45619fc2012-02-29 11:15:25 -0800243 break;
244 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700245 arg_array_[offset].SetF(args[offset].f);
Ian Rogers45619fc2012-02-29 11:15:25 -0800246 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700247 case 'L':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700248 arg_array_[offset].SetL(DecodeObj(env, args[offset].l));
Ian Rogers45619fc2012-02-29 11:15:25 -0800249 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800250 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700251 arg_array_[offset].SetD(args[offset].d);
Ian Rogers45619fc2012-02-29 11:15:25 -0800252 break;
253 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700254 arg_array_[offset].SetJ(args[offset].j);
Ian Rogers45619fc2012-02-29 11:15:25 -0800255 break;
256 }
257 }
258 }
259
Ian Rogers45619fc2012-02-29 11:15:25 -0800260 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700261 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800262 const char* shorty_;
263 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700264 JValue* arg_array_;
265 JValue small_arg_array_[kSmallArgArraySize];
266 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800267};
268
Elliott Hughes0512f022012-03-15 22:10:52 -0700269static jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700270 if (obj == NULL) {
271 return NULL;
272 }
Elliott Hughes75770752011-08-24 17:52:38 -0700273 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700274 IndirectReferenceTable& weak_globals = vm->weak_globals;
275 MutexLock mu(vm->weak_globals_lock);
276 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
277 return reinterpret_cast<jweak>(ref);
278}
279
Elliott Hughesbf86d042011-08-31 17:53:14 -0700280// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700281template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -0700282static T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700283 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700284}
285
Elliott Hughesb264f082012-04-06 17:10:10 -0700286static void CheckMethodArguments(Method* m, JValue* args) {
287 MethodHelper mh(m);
288 ObjectArray<Class>* parameter_types = mh.GetParameterTypes();
289 CHECK(parameter_types != NULL);
290 size_t error_count = 0;
291 for (int i = 0; i < parameter_types->GetLength(); ++i) {
292 Class* parameter_type = parameter_types->Get(i);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700293 // TODO: check primitives are in range.
Elliott Hughesb264f082012-04-06 17:10:10 -0700294 if (!parameter_type->IsPrimitive()) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700295 Object* argument = args[i].GetL();
Elliott Hughesb264f082012-04-06 17:10:10 -0700296 if (argument != NULL && !argument->InstanceOf(parameter_type)) {
297 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
298 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
299 ++error_count;
300 }
301 }
302 }
303 if (error_count > 0) {
304 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
305 // with an argument.
306 JniAbort(NULL);
307 }
308}
Elliott Hughesb264f082012-04-06 17:10:10 -0700309
Elliott Hughes77405792012-03-15 15:22:12 -0700310static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, JValue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700311 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700312 if (UNLIKELY(env->check_jni)) {
313 CheckMethodArguments(method, args);
314 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700315 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700316 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700317 return result;
318}
319
Ian Rogers0571d352011-11-03 19:51:38 -0700320static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700321 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800322 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700323 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800324 ArgArray arg_array(method);
325 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700326 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700327}
328
Ian Rogers0571d352011-11-03 19:51:38 -0700329static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700330 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700331}
332
Ian Rogers0571d352011-11-03 19:51:38 -0700333static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
334 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700335 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800336 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700337 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800338 ArgArray arg_array(method);
339 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700340 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700341}
342
Ian Rogers0571d352011-11-03 19:51:38 -0700343static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
344 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700345 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800346 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700347 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800348 ArgArray arg_array(method);
349 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700350 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700351}
352
Elliott Hughes6b436852011-08-12 10:16:44 -0700353// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
354// separated with slashes but aren't wrapped with "L;" like regular descriptors
355// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
356// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
357// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700358static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700359 std::string result;
360 // Add the missing "L;" if necessary.
361 if (name[0] == '[') {
362 result = name;
363 } else {
364 result += 'L';
365 result += name;
366 result += ';';
367 }
368 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700369 if (result.find('.') != std::string::npos) {
370 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
371 << "\"" << name << "\"";
372 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700373 }
374 return result;
375}
376
Ian Rogers0571d352011-11-03 19:51:38 -0700377static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700378 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800379 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700380}
381
Ian Rogers0571d352011-11-03 19:51:38 -0700382static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700383 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700384 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700385 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700386 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700387
388 Method* method = NULL;
389 if (is_static) {
390 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700391 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700392 method = c->FindVirtualMethod(name, sig);
393 if (method == NULL) {
394 // No virtual method matching the signature. Search declared
395 // private methods and constructors.
396 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700397 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700398 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700399
Elliott Hughescdf53122011-08-19 15:46:09 -0700400 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700401 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700402 return NULL;
403 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700404
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700405 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700406}
407
Ian Rogers0571d352011-11-03 19:51:38 -0700408static const ClassLoader* GetClassLoader(Thread* self) {
Elliott Hughes6a144332012-04-03 13:07:11 -0700409 Method* method = self->GetCurrentMethod();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700410 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
411 return self->GetClassLoaderOverride();
412 }
413 return method->GetDeclaringClass()->GetClassLoader();
414}
415
Ian Rogers0571d352011-11-03 19:51:38 -0700416static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700417 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700418 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700419 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700420 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700421
422 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 Class* field_type;
424 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
425 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700426 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700427 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700428 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700429 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700430 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700431 if (field_type == NULL) {
432 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700433 DCHECK(ts.Self()->IsExceptionPending());
434 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700435 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700436 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800437 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700438 return NULL;
439 }
440 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800441 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700442 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800443 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700444 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700445 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700446 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700447 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800448 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700449 return NULL;
450 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700451 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700452}
453
Ian Rogers0571d352011-11-03 19:51:38 -0700454static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700455 JavaVMExt* vm = ts.Vm();
456 MutexLock mu(vm->pins_lock);
457 vm->pin_table.Add(array);
458}
459
Ian Rogers0571d352011-11-03 19:51:38 -0700460static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700461 JavaVMExt* vm = ts.Vm();
462 MutexLock mu(vm->pins_lock);
463 vm->pin_table.Remove(array);
464}
465
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700466template<typename JniT, typename ArtT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700467static JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700468 CHECK_GE(length, 0); // TODO: ReportJniError
469 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700470 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700471}
472
Elliott Hughes75770752011-08-24 17:52:38 -0700473template <typename ArrayT, typename CArrayT, typename ArtArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700474static CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -0700475 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
476 PinPrimitiveArray(ts, array);
477 if (is_copy != NULL) {
478 *is_copy = JNI_FALSE;
479 }
480 return array->GetData();
481}
482
483template <typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700484static void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
Elliott Hughes75770752011-08-24 17:52:38 -0700485 if (mode != JNI_COMMIT) {
486 Array* array = Decode<Array*>(ts, java_array);
487 UnpinPrimitiveArray(ts, array);
488 }
489}
490
Ian Rogers0571d352011-11-03 19:51:38 -0700491static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700492 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700493 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700494 "%s offset=%d length=%d %s.length=%d",
495 type.c_str(), start, length, identifier, array->GetLength());
496}
Ian Rogers0571d352011-11-03 19:51:38 -0700497
498static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700499 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700500 "offset=%d length=%d string.length()=%d", start, length, array_length);
501}
Elliott Hughes814e4032011-08-23 12:07:56 -0700502
503template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700504static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700505 ArrayT* array = Decode<ArrayT*>(ts, java_array);
506 if (start < 0 || length < 0 || start + length > array->GetLength()) {
507 ThrowAIOOBE(ts, array, start, length, "src");
508 } else {
509 JavaT* data = array->GetData();
510 memcpy(buf, data + start, length * sizeof(JavaT));
511 }
512}
513
514template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700515static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700516 ArrayT* array = Decode<ArrayT*>(ts, java_array);
517 if (start < 0 || length < 0 || start + length > array->GetLength()) {
518 ThrowAIOOBE(ts, array, start, length, "dst");
519 } else {
520 JavaT* data = array->GetData();
521 memcpy(data + start, buf, length * sizeof(JavaT));
522 }
523}
524
Elliott Hughesa4f94742012-05-29 16:28:38 -0700525int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause) {
526 ScopedJniThreadState ts(env);
527
528 // Turn the const char* into a java.lang.String.
529 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
530 if (msg != NULL && s.get() == NULL) {
531 return JNI_ERR;
532 }
533
534 // Choose an appropriate constructor and set up the arguments.
535 jvalue args[2];
536 const char* signature;
537 if (msg == NULL && cause == NULL) {
538 signature = "()V";
539 } else if (msg != NULL && cause == NULL) {
540 signature = "(Ljava/lang/String;)V";
541 args[0].l = s.get();
542 } else if (msg == NULL && cause != NULL) {
543 signature = "(Ljava/lang/Throwable;)V";
544 args[0].l = cause;
545 } else {
546 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
547 args[0].l = s.get();
548 args[1].l = cause;
549 }
550 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
551 if (mid == NULL) {
552 LOG(ERROR) << "No <init>" << signature << " in " << PrettyClass(Decode<Class*>(env, exception_class));
553 return JNI_ERR;
554 }
555
556 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
557 if (exception.get() == NULL) {
558 return JNI_ERR;
559 }
560
561 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
562
563 return JNI_OK;
564}
565
Elliott Hughes462c9442012-03-23 18:47:50 -0700566static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700567 if (vm == NULL || p_env == NULL) {
568 return JNI_ERR;
569 }
570
Elliott Hughes462c9442012-03-23 18:47:50 -0700571 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700572 Thread* self = Thread::Current();
573 if (self != NULL) {
574 *p_env = self->GetJniEnv();
575 return JNI_OK;
576 }
577
578 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
579
580 // No threads allowed in zygote mode.
581 if (runtime->IsZygote()) {
582 LOG(ERROR) << "Attempt to attach a thread in the zygote";
583 return JNI_ERR;
584 }
585
Elliott Hughes462c9442012-03-23 18:47:50 -0700586 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
587 const char* thread_name = NULL;
588 Object* thread_group = NULL;
589 if (args != NULL) {
590 CHECK_GE(args->version, JNI_VERSION_1_2);
591 thread_name = args->name;
592 thread_group = static_cast<Thread*>(NULL)->DecodeJObject(args->group);
Elliott Hughes75770752011-08-24 17:52:38 -0700593 }
Elliott Hughes75770752011-08-24 17:52:38 -0700594
Elliott Hughes462c9442012-03-23 18:47:50 -0700595 runtime->AttachCurrentThread(thread_name, as_daemon, thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700596 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700597 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700598}
599
Elliott Hughes79082e32011-08-25 12:07:32 -0700600class SharedLibrary {
601 public:
602 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
603 : path_(path),
604 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700605 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700606 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughese62934d2012-04-09 11:24:29 -0700607 jni_on_load_cond_("JNI_OnLoad condition variable"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700608 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700609 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700610 }
611
Elliott Hughes79082e32011-08-25 12:07:32 -0700612 Object* GetClassLoader() {
613 return class_loader_;
614 }
615
616 std::string GetPath() {
617 return path_;
618 }
619
620 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700621 * Check the result of an earlier call to JNI_OnLoad on this library.
622 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700623 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700624 bool CheckOnLoadResult() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700625 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700626 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700627 // Check this so we don't end up waiting for ourselves. We need
628 // to return "true" so the caller can continue.
629 LOG(INFO) << *self << " recursive attempt to load library "
630 << "\"" << path_ << "\"";
631 return true;
632 }
633
634 MutexLock mu(jni_on_load_lock_);
635 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800636 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
637 << "JNI_OnLoad...]";
Elliott Hughes34e06962012-04-09 13:55:55 -0700638 ScopedThreadStateChange tsc(self, kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700639 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700640 }
641
642 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800643 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
644 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700645 return okay;
646 }
647
648 void SetResult(bool result) {
649 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700650 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700651
652 // Broadcast a wakeup to anybody sleeping on the condition variable.
653 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700654 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700655 }
656
657 void* FindSymbol(const std::string& symbol_name) {
658 return dlsym(handle_, symbol_name.c_str());
659 }
660
661 private:
662 enum JNI_OnLoadState {
663 kPending,
664 kFailed,
665 kOkay,
666 };
667
668 // Path to library "/system/lib/libjni.so".
669 std::string path_;
670
671 // The void* returned by dlopen(3).
672 void* handle_;
673
674 // The ClassLoader this library is associated with.
675 Object* class_loader_;
676
677 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700678 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700679 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700680 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700681 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700682 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700683 // Result of earlier JNI_OnLoad call.
684 JNI_OnLoadState jni_on_load_result_;
685};
686
Elliott Hughes79082e32011-08-25 12:07:32 -0700687// This exists mainly to keep implementation details out of the header file.
688class Libraries {
689 public:
690 Libraries() {
691 }
692
693 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700694 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700695 }
696
Elliott Hughesae80b492012-04-24 10:43:17 -0700697 void Dump(std::ostream& os) const {
698 bool first = true;
699 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
700 if (!first) {
701 os << ' ';
702 }
703 first = false;
704 os << it->first;
705 }
706 }
707
708 size_t size() const {
709 return libraries_.size();
710 }
711
Elliott Hughes79082e32011-08-25 12:07:32 -0700712 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700713 It it = libraries_.find(path);
714 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700715 }
716
717 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700718 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700719 }
720
721 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700722 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700723 std::string jni_short_name(JniShortName(m));
724 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700725 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700726 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
727 SharedLibrary* library = it->second;
728 if (library->GetClassLoader() != declaring_class_loader) {
729 // We only search libraries loaded by the appropriate ClassLoader.
730 continue;
731 }
732 // Try the short name then the long name...
733 void* fn = library->FindSymbol(jni_short_name);
734 if (fn == NULL) {
735 fn = library->FindSymbol(jni_long_name);
736 }
737 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800738 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
739 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700740 return fn;
741 }
742 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700743 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700744 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700745 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700746 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700747 return NULL;
748 }
749
750 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700751 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700752
Elliott Hughesa0e18062012-04-13 15:59:59 -0700753 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700754};
755
Elliott Hughes418d20f2011-09-22 14:00:39 -0700756JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
757 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
758 Object* receiver = Decode<Object*>(env, obj);
759 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800760 ArgArray arg_array(method);
761 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700762 return InvokeWithArgArray(env, receiver, method, arg_array.get());
763}
764
Elliott Hughesd07986f2011-12-06 18:27:45 -0800765JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700766 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800767}
768
Elliott Hughescdf53122011-08-19 15:46:09 -0700769class JNI {
770 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700771
Elliott Hughescdf53122011-08-19 15:46:09 -0700772 static jint GetVersion(JNIEnv* env) {
773 ScopedJniThreadState ts(env);
774 return JNI_VERSION_1_6;
775 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700776
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
778 ScopedJniThreadState ts(env);
779 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700780 return NULL;
781 }
782
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 static jclass FindClass(JNIEnv* env, const char* name) {
784 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700785 Runtime* runtime = Runtime::Current();
786 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700788 Class* c = NULL;
789 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700790 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800791 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700792 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800793 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700794 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700795 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700796 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700797
Elliott Hughescdf53122011-08-19 15:46:09 -0700798 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
799 ScopedJniThreadState ts(env);
800 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700801 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700802 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700803
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
805 ScopedJniThreadState ts(env);
806 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700807 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700808 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700809
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
811 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700812 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700813 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700815
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
817 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700818 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700819 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700821
Elliott Hughes37f7a402011-08-22 18:56:01 -0700822 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
823 ScopedJniThreadState ts(env);
824 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700825 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700826 }
827
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700828 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700830 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700831 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700833
Elliott Hughes37f7a402011-08-22 18:56:01 -0700834 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700836 Class* c1 = Decode<Class*>(ts, java_class1);
837 Class* c2 = Decode<Class*>(ts, java_class2);
838 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700840
Elliott Hughese84278b2012-03-22 10:06:53 -0700841 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700843 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700844 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700845 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700846 return JNI_TRUE;
847 } else {
848 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700849 Class* c = Decode<Class*>(ts, java_class);
850 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700851 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700853
Elliott Hughes37f7a402011-08-22 18:56:01 -0700854 static jint Throw(JNIEnv* env, jthrowable java_exception) {
855 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700856 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700857 if (exception == NULL) {
858 return JNI_ERR;
859 }
860 ts.Self()->SetException(exception);
861 return JNI_OK;
862 }
863
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700864 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700865 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700866 }
867
868 static jboolean ExceptionCheck(JNIEnv* env) {
869 ScopedJniThreadState ts(env);
870 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
871 }
872
873 static void ExceptionClear(JNIEnv* env) {
874 ScopedJniThreadState ts(env);
875 ts.Self()->ClearException();
876 }
877
878 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700880
881 Thread* self = ts.Self();
882 Throwable* original_exception = self->GetException();
883 self->ClearException();
884
Elliott Hughesbf86d042011-08-31 17:53:14 -0700885 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700886 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
887 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
888 if (mid == NULL) {
889 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700890 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700891 } else {
892 env->CallVoidMethod(exception.get(), mid);
893 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700894 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700895 << " thrown while calling printStackTrace";
896 self->ClearException();
897 }
898 }
899
900 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700902
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 static jthrowable ExceptionOccurred(JNIEnv* env) {
904 ScopedJniThreadState ts(env);
905 Object* exception = ts.Self()->GetException();
Elliott Hughes81ff3182012-03-23 20:35:56 -0700906 return (exception != NULL) ? AddLocalReference<jthrowable>(env, exception) : NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 }
908
Elliott Hughescdf53122011-08-19 15:46:09 -0700909 static void FatalError(JNIEnv* env, const char* msg) {
910 ScopedJniThreadState ts(env);
911 LOG(FATAL) << "JNI FatalError called: " << msg;
912 }
913
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700914 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700916 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
917 return JNI_ERR;
918 }
919 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 return JNI_OK;
921 }
922
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700923 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700925 Object* survivor = Decode<Object*>(ts, java_survivor);
926 ts.Env()->PopFrame();
927 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 }
929
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700930 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700932 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
933 }
934
935 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
936 // TODO: we should try to expand the table if necessary.
937 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
938 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
939 return JNI_ERR;
940 }
941 // TODO: this isn't quite right, since "capacity" includes holes.
942 size_t capacity = ts.Env()->locals.Capacity();
943 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
944 if (!okay) {
945 ts.Self()->ThrowOutOfMemoryError(caller);
946 }
947 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700948 }
949
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
951 ScopedJniThreadState ts(env);
952 if (obj == NULL) {
953 return NULL;
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 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
960 return reinterpret_cast<jobject>(ref);
961 }
962
963 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
964 ScopedJniThreadState ts(env);
965 if (obj == NULL) {
966 return;
967 }
968
Elliott Hughes75770752011-08-24 17:52:38 -0700969 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 IndirectReferenceTable& globals = vm->globals;
971 MutexLock mu(vm->globals_lock);
972
973 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
974 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
975 << "failed to find entry";
976 }
977 }
978
979 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
980 ScopedJniThreadState ts(env);
981 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
982 }
983
984 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
985 ScopedJniThreadState ts(env);
986 if (obj == NULL) {
987 return;
988 }
989
Elliott Hughes75770752011-08-24 17:52:38 -0700990 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 IndirectReferenceTable& weak_globals = vm->weak_globals;
992 MutexLock mu(vm->weak_globals_lock);
993
994 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
995 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
996 << "failed to find entry";
997 }
998 }
999
1000 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
1001 ScopedJniThreadState ts(env);
1002 if (obj == NULL) {
1003 return NULL;
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 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
1010 return reinterpret_cast<jobject>(ref);
1011 }
1012
1013 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
1014 ScopedJniThreadState ts(env);
1015 if (obj == NULL) {
1016 return;
1017 }
1018
1019 IndirectReferenceTable& locals = ts.Env()->locals;
1020
Ian Rogers5a7a74a2011-09-26 16:32:29 -07001021 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 if (!locals.Remove(cookie, obj)) {
1023 // Attempting to delete a local reference that is not in the
1024 // topmost local reference frame is a no-op. DeleteLocalRef returns
1025 // void and doesn't throw any exceptions, but we should probably
1026 // complain about it so the user will notice that things aren't
1027 // going quite the way they expect.
1028 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
1029 << "failed to find entry";
1030 }
1031 }
1032
1033 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
1034 ScopedJniThreadState ts(env);
1035 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
1036 ? JNI_TRUE : JNI_FALSE;
1037 }
1038
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001039 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001041 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001042 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001043 return NULL;
1044 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001045 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughese84278b2012-03-22 10:06:53 -07001048 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
1050 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001052 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 va_end(args);
1054 return result;
1055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001059 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001060 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001061 return NULL;
1062 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001063 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001064 if (result == NULL) {
1065 return NULL;
1066 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001067 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001069 if (!ts.Self()->IsExceptionPending()) {
1070 return local_result;
1071 } else {
1072 return NULL;
1073 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001077 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001078 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001079 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001080 return NULL;
1081 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001082 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001083 if (result == NULL) {
1084 return NULL;
1085 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001086 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001088 if (!ts.Self()->IsExceptionPending()) {
1089 return local_result;
1090 } else {
1091 return NULL;
1092 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1096 ScopedJniThreadState ts(env);
1097 return FindMethodID(ts, c, name, sig, false);
1098 }
1099
1100 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1101 ScopedJniThreadState ts(env);
1102 return FindMethodID(ts, c, name, sig, true);
1103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 va_list ap;
1108 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001109 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001111 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001116 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001117 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001122 JValue result(InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001123 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 va_list ap;
1129 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001130 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001132 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001137 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001142 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_list ap;
1148 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001149 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001151 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 }
1153
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001156 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001161 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 va_list ap;
1167 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001168 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001170 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001175 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001180 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 }
1182
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 va_list ap;
1186 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001187 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001189 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001194 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001199 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001204 va_list ap;
1205 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001206 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001208 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 }
1210
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001213 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
1215
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001218 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001223 va_list ap;
1224 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001225 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001226 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001227 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001232 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 }
1234
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001237 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001242 va_list ap;
1243 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001244 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001245 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001246 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001251 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001256 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001261 va_list ap;
1262 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001263 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001264 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001265 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
Elliott Hughes72025e52011-08-23 17:50:30 -07001268 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001270 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 }
1272
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001275 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001280 va_list ap;
1281 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001282 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001283 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
Elliott Hughes72025e52011-08-23 17:50:30 -07001286 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001288 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
1290
Elliott Hughes72025e52011-08-23 17:50:30 -07001291 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001293 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001296 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 ScopedJniThreadState ts(env);
1298 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001300 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001301 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 va_end(ap);
1303 return local_result;
1304 }
1305
1306 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001307 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001309 JValue result(InvokeWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001310 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 }
1312
1313 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001314 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001316 JValue result(InvokeWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001317 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
1319
1320 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001321 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 ScopedJniThreadState ts(env);
1323 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001324 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001325 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001327 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
1330 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001331 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001333 return InvokeWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
1336 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001337 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001339 return InvokeWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 }
1341
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001342 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 ScopedJniThreadState ts(env);
1344 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001345 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001346 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001348 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
1351 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001352 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001354 return InvokeWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
1357 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001358 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001360 return InvokeWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001363 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 ScopedJniThreadState ts(env);
1365 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001366 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001367 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001369 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
1372 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001373 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001375 return InvokeWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
1377
1378 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001379 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001381 return InvokeWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 }
1383
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001384 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 ScopedJniThreadState ts(env);
1386 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001387 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001388 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001390 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 }
1392
1393 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001394 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001396 return InvokeWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
1399 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001400 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001402 return InvokeWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 }
1404
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001405 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 ScopedJniThreadState ts(env);
1407 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001408 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001409 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001411 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
1414 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001415 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001417 return InvokeWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
1420 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001421 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001423 return InvokeWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001426 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 ScopedJniThreadState ts(env);
1428 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001429 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001430 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001432 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 }
1434
1435 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001436 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001438 return InvokeWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 }
1440
1441 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001442 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001444 return InvokeWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001447 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 ScopedJniThreadState ts(env);
1449 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001450 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001451 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001453 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
1456 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001457 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001459 return InvokeWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
1462 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001463 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001465 return InvokeWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001468 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 ScopedJniThreadState ts(env);
1470 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001471 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001472 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001474 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 }
1476
1477 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001478 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001479 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001480 return InvokeWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
1483 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001484 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001485 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001486 return InvokeWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 }
1488
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001489 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 ScopedJniThreadState ts(env);
1491 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001492 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001493 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 va_end(ap);
1495 }
1496
1497 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001498 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001500 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
1503 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001504 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001505 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001506 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 }
1508
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001509 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 ScopedJniThreadState ts(env);
1511 return FindFieldID(ts, c, name, sig, false);
1512 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001513
1514
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001515 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 ScopedJniThreadState ts(env);
1517 return FindFieldID(ts, c, name, sig, true);
1518 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001519
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001522 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001523 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001524 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001526
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001527 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001529 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001530 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001531 }
1532
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001533 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 Object* o = Decode<Object*>(ts, java_object);
1536 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001537 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001538 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001539 }
1540
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001541 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001543 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001544 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001548#define GET_PRIMITIVE_FIELD(fn, instance) \
1549 ScopedJniThreadState ts(env); \
1550 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001551 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001552 return f->fn(o)
1553
1554#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1555 ScopedJniThreadState ts(env); \
1556 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001557 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001558 f->fn(o, value)
1559
1560 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1561 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001564 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1565 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001568 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1569 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001572 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1573 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 }
1575
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001576 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1577 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001580 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1581 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001584 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1585 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001588 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1589 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001592 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001593 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001596 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001597 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001600 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001601 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001604 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001605 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001608 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001609 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001612 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001613 GET_PRIMITIVE_FIELD(GetLong, NULL);
1614 }
1615
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001616 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001617 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001621 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1622 }
1623
1624 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1625 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1626 }
1627
1628 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1629 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1630 }
1631
1632 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1633 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1634 }
1635
1636 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1637 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1638 }
1639
1640 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1641 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1642 }
1643
1644 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1645 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1646 }
1647
1648 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1649 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1650 }
1651
1652 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1653 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1654 }
1655
1656 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1657 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1658 }
1659
1660 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1661 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1662 }
1663
1664 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1665 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1666 }
1667
1668 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1669 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1670 }
1671
1672 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1673 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1674 }
1675
1676 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1677 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1678 }
1679
1680 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1681 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1682 }
1683
1684 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1685 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001688 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 ScopedJniThreadState ts(env);
1690 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001691 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001692 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001693 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 va_end(ap);
1695 return local_result;
1696 }
1697
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001698 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001700 JValue result(InvokeWithVarArgs(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001701 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 }
1703
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001704 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001706 JValue result(InvokeWithJValues(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001707 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 }
1709
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001710 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 ScopedJniThreadState ts(env);
1712 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001713 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001714 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001716 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001719 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001721 return InvokeWithVarArgs(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 }
1723
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001724 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001726 return InvokeWithJValues(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001730 ScopedJniThreadState ts(env);
1731 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001732 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001733 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001735 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001738 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001740 return InvokeWithVarArgs(env, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001743 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001745 return InvokeWithJValues(env, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 }
1747
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001748 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 ScopedJniThreadState ts(env);
1750 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001751 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001752 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001754 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001757 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001759 return InvokeWithVarArgs(env, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001762 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001764 return InvokeWithJValues(env, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001767 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 ScopedJniThreadState ts(env);
1769 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001770 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001771 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001773 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001776 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001778 return InvokeWithVarArgs(env, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001781 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001783 return InvokeWithJValues(env, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 }
1785
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001786 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001787 ScopedJniThreadState ts(env);
1788 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001789 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001790 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001792 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001795 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001797 return InvokeWithVarArgs(env, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001800 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001802 return InvokeWithJValues(env, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 ScopedJniThreadState ts(env);
1807 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001808 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001809 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001811 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001814 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001816 return InvokeWithVarArgs(env, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 }
1818
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001819 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001821 return InvokeWithJValues(env, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 }
1823
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001824 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001825 ScopedJniThreadState ts(env);
1826 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001827 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001828 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001830 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001833 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001835 return InvokeWithVarArgs(env, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001838 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001840 return InvokeWithJValues(env, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001843 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 ScopedJniThreadState ts(env);
1845 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001846 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001847 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001849 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 }
1851
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001852 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001854 return InvokeWithVarArgs(env, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 }
1856
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001857 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001859 return InvokeWithJValues(env, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 }
1861
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001862 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 ScopedJniThreadState ts(env);
1864 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001865 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001866 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 va_end(ap);
1868 }
1869
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001870 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001872 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 }
1874
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001875 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001877 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 }
1879
Elliott Hughes814e4032011-08-23 12:07:56 -07001880 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001881 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001882 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001883 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001884 }
1885
1886 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1887 ScopedJniThreadState ts(env);
1888 if (utf == NULL) {
1889 return NULL;
1890 }
1891 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001892 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 }
1894
Elliott Hughes814e4032011-08-23 12:07:56 -07001895 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1896 ScopedJniThreadState ts(env);
1897 return Decode<String*>(ts, java_string)->GetLength();
1898 }
1899
1900 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1901 ScopedJniThreadState ts(env);
1902 return Decode<String*>(ts, java_string)->GetUtfLength();
1903 }
1904
Elliott Hughesb465ab02011-08-24 11:21:21 -07001905 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001907 String* s = Decode<String*>(ts, java_string);
1908 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1909 ThrowSIOOBE(ts, start, length, s->GetLength());
1910 } else {
1911 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1912 memcpy(buf, chars + start, length * sizeof(jchar));
1913 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001914 }
1915
Elliott Hughesb465ab02011-08-24 11:21:21 -07001916 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001917 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001918 String* s = Decode<String*>(ts, java_string);
1919 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1920 ThrowSIOOBE(ts, start, length, s->GetLength());
1921 } else {
1922 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1923 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1924 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001925 }
1926
Elliott Hughes75770752011-08-24 17:52:38 -07001927 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001928 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001929 String* s = Decode<String*>(ts, java_string);
1930 const CharArray* chars = s->GetCharArray();
1931 PinPrimitiveArray(ts, chars);
1932 if (is_copy != NULL) {
1933 *is_copy = JNI_FALSE;
1934 }
1935 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001936 }
1937
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001938 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001939 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001940 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001945 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 }
1947
Elliott Hughes75770752011-08-24 17:52:38 -07001948 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001950 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 }
1952
Elliott Hughes75770752011-08-24 17:52:38 -07001953 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001954 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001955 if (java_string == NULL) {
1956 return NULL;
1957 }
1958 if (is_copy != NULL) {
1959 *is_copy = JNI_TRUE;
1960 }
1961 String* s = Decode<String*>(ts, java_string);
1962 size_t byte_count = s->GetUtfLength();
1963 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001964 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001965 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1966 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1967 bytes[byte_count] = '\0';
1968 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001969 }
1970
Elliott Hughes75770752011-08-24 17:52:38 -07001971 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001972 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001973 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001974 }
1975
Elliott Hughesbd935992011-08-22 11:59:34 -07001976 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001978 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001979 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001980 Array* array = obj->AsArray();
1981 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes814e4032011-08-23 12:07:56 -07001984 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001986 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001987 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
1990 static void SetObjectArrayElement(JNIEnv* env,
1991 jobjectArray java_array, jsize index, jobject java_value) {
1992 ScopedJniThreadState ts(env);
1993 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1994 Object* value = Decode<Object*>(ts, java_value);
1995 array->Set(index, value);
1996 }
1997
1998 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1999 ScopedJniThreadState ts(env);
2000 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
2001 }
2002
2003 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
2004 ScopedJniThreadState ts(env);
2005 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
2006 }
2007
2008 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
2009 ScopedJniThreadState ts(env);
2010 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
2011 }
2012
2013 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
2014 ScopedJniThreadState ts(env);
2015 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
2016 }
2017
2018 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
2019 ScopedJniThreadState ts(env);
2020 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
2021 }
2022
2023 static jintArray NewIntArray(JNIEnv* env, jsize length) {
2024 ScopedJniThreadState ts(env);
2025 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
2026 }
2027
2028 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2029 ScopedJniThreadState ts(env);
2030 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
2031 }
2032
2033 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
2034 ScopedJniThreadState ts(env);
2035 CHECK_GE(length, 0); // TODO: ReportJniError
2036
2037 // Compute the array class corresponding to the given element class.
2038 Class* element_class = Decode<Class*>(ts, element_jclass);
2039 std::string descriptor;
2040 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002041 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002042
2043 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002044 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2045 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 return NULL;
2047 }
2048
Elliott Hughes75770752011-08-24 17:52:38 -07002049 // Allocate and initialize if necessary.
2050 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002052 if (initial_element != NULL) {
2053 Object* initial_object = Decode<Object*>(ts, initial_element);
2054 for (jsize i = 0; i < length; ++i) {
2055 result->Set(i, initial_object);
2056 }
2057 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002058 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
2061 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2062 ScopedJniThreadState ts(env);
2063 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2064 }
2065
Ian Rogersa15e67d2012-02-28 13:51:55 -08002066 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002067 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002068 Array* array = Decode<Array*>(ts, java_array);
2069 PinPrimitiveArray(ts, array);
2070 if (is_copy != NULL) {
2071 *is_copy = JNI_FALSE;
2072 }
2073 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002074 }
2075
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002076 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002077 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002078 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002079 }
2080
Elliott Hughes75770752011-08-24 17:52:38 -07002081 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002083 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
Elliott Hughes75770752011-08-24 17:52:38 -07002086 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002088 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
Elliott Hughes75770752011-08-24 17:52:38 -07002091 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002093 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes75770752011-08-24 17:52:38 -07002096 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002098 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
Elliott Hughes75770752011-08-24 17:52:38 -07002101 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002103 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
Elliott Hughes75770752011-08-24 17:52:38 -07002106 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002108 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
Elliott Hughes75770752011-08-24 17:52:38 -07002111 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002113 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
Elliott Hughes75770752011-08-24 17:52:38 -07002116 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002118 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002121 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002123 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002126 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002128 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002131 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002133 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
2135
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002136 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002138 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002141 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002143 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
2145
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002146 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002148 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 }
2150
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002151 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002153 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 }
2155
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002156 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002158 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 }
2160
Elliott Hughes814e4032011-08-23 12:07:56 -07002161 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002163 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 }
2165
Elliott Hughes814e4032011-08-23 12:07:56 -07002166 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002168 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
Elliott Hughes814e4032011-08-23 12:07:56 -07002171 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002173 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
2175
Elliott Hughes814e4032011-08-23 12:07:56 -07002176 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002178 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
2180
Elliott Hughes814e4032011-08-23 12:07:56 -07002181 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002183 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 }
2185
Elliott Hughes814e4032011-08-23 12:07:56 -07002186 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002188 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Elliott Hughes814e4032011-08-23 12:07:56 -07002191 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002192 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002193 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
2195
Elliott Hughes814e4032011-08-23 12:07:56 -07002196 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002198 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 }
2200
Elliott Hughes814e4032011-08-23 12:07:56 -07002201 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002203 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 }
2205
Elliott Hughes814e4032011-08-23 12:07:56 -07002206 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002208 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210
Elliott Hughes814e4032011-08-23 12:07:56 -07002211 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002213 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 }
2215
Elliott Hughes814e4032011-08-23 12:07:56 -07002216 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002218 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 }
2220
Elliott Hughes814e4032011-08-23 12:07:56 -07002221 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002222 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002223 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225
Elliott Hughes814e4032011-08-23 12:07:56 -07002226 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002228 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Elliott Hughes814e4032011-08-23 12:07:56 -07002231 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002232 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002233 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Elliott Hughes814e4032011-08-23 12:07:56 -07002236 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002238 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Elliott Hughes5174fe62011-08-23 15:12:35 -07002241 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002243 Class* c = Decode<Class*>(ts, java_class);
2244
Elliott Hughes5174fe62011-08-23 15:12:35 -07002245 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 const char* name = methods[i].name;
2247 const char* sig = methods[i].signature;
2248
2249 if (*sig == '!') {
2250 // TODO: fast jni. it's too noisy to log all these.
2251 ++sig;
2252 }
2253
Elliott Hughes5174fe62011-08-23 15:12:35 -07002254 Method* m = c->FindDirectMethod(name, sig);
2255 if (m == NULL) {
2256 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002258 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002259 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002260 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002262 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002263 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002264 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002265 return JNI_ERR;
2266 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002267
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002268 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002269
Ian Rogers60db5ab2012-02-20 17:02:00 -08002270 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 }
2272 return JNI_OK;
2273 }
2274
Elliott Hughes5174fe62011-08-23 15:12:35 -07002275 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002276 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002277 Class* c = Decode<Class*>(ts, java_class);
2278
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002279 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002280
2281 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2282 Method* m = c->GetDirectMethod(i);
2283 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002284 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002285 }
2286 }
2287 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2288 Method* m = c->GetVirtualMethod(i);
2289 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002290 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002291 }
2292 }
2293
2294 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002295 }
2296
Elliott Hughes72025e52011-08-23 17:50:30 -07002297 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002298 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002299 Object* o = Decode<Object*>(ts, java_object);
2300 o->MonitorEnter(ts.Self());
2301 if (ts.Self()->IsExceptionPending()) {
2302 return JNI_ERR;
2303 }
2304 ts.Env()->monitors.Add(o);
2305 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 }
2307
Elliott Hughes72025e52011-08-23 17:50:30 -07002308 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002309 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002310 Object* o = Decode<Object*>(ts, java_object);
2311 o->MonitorExit(ts.Self());
2312 if (ts.Self()->IsExceptionPending()) {
2313 return JNI_ERR;
2314 }
2315 ts.Env()->monitors.Remove(o);
2316 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002317 }
2318
2319 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2320 ScopedJniThreadState ts(env);
2321 Runtime* runtime = Runtime::Current();
2322 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002323 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002324 } else {
2325 *vm = NULL;
2326 }
2327 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2328 }
2329
Elliott Hughescdf53122011-08-19 15:46:09 -07002330 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2331 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002332
2333 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002334 CHECK(address != NULL); // TODO: ReportJniError
2335 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002336
Elliott Hughesb465ab02011-08-24 11:21:21 -07002337 // At the moment, the Java side is limited to 32 bits.
2338 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2339 CHECK_LE(capacity, 0xffffffff);
2340 jint address_arg = reinterpret_cast<jint>(address);
2341 jint capacity_arg = static_cast<jint>(capacity);
2342
Elliott Hugheseac76672012-05-24 21:56:51 -07002343 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2344 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2345 address_arg, capacity_arg);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002346 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002347 }
2348
Elliott Hughesb465ab02011-08-24 11:21:21 -07002349 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 ScopedJniThreadState ts(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002351 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002352 }
2353
Elliott Hughesb465ab02011-08-24 11:21:21 -07002354 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002355 ScopedJniThreadState ts(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002356 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002357 }
2358
Elliott Hughesb465ab02011-08-24 11:21:21 -07002359 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002360 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002361
Elliott Hughes75770752011-08-24 17:52:38 -07002362 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002363
2364 // Do we definitely know what kind of reference this is?
2365 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2366 IndirectRefKind kind = GetIndirectRefKind(ref);
2367 switch (kind) {
2368 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002369 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2370 return JNILocalRefType;
2371 }
2372 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002373 case kGlobal:
2374 return JNIGlobalRefType;
2375 case kWeakGlobal:
2376 return JNIWeakGlobalRefType;
2377 case kSirtOrInvalid:
2378 // Is it in a stack IRT?
TDYa12728f1a142012-03-15 21:51:52 -07002379 if (ts.Self()->StackReferencesContain(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002380 return JNILocalRefType;
2381 }
2382
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002383 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002384 return JNIInvalidRefType;
2385 }
2386
Elliott Hughesb465ab02011-08-24 11:21:21 -07002387 // If we're handing out direct pointers, check whether it's a direct pointer
2388 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002389 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002390 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002391 return JNILocalRefType;
2392 }
2393 }
2394
2395 return JNIInvalidRefType;
2396 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002397 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2398 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002399 }
2400};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002401
Elliott Hughes88c5c352012-03-15 18:49:48 -07002402const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002403 NULL, // reserved0.
2404 NULL, // reserved1.
2405 NULL, // reserved2.
2406 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002407 JNI::GetVersion,
2408 JNI::DefineClass,
2409 JNI::FindClass,
2410 JNI::FromReflectedMethod,
2411 JNI::FromReflectedField,
2412 JNI::ToReflectedMethod,
2413 JNI::GetSuperclass,
2414 JNI::IsAssignableFrom,
2415 JNI::ToReflectedField,
2416 JNI::Throw,
2417 JNI::ThrowNew,
2418 JNI::ExceptionOccurred,
2419 JNI::ExceptionDescribe,
2420 JNI::ExceptionClear,
2421 JNI::FatalError,
2422 JNI::PushLocalFrame,
2423 JNI::PopLocalFrame,
2424 JNI::NewGlobalRef,
2425 JNI::DeleteGlobalRef,
2426 JNI::DeleteLocalRef,
2427 JNI::IsSameObject,
2428 JNI::NewLocalRef,
2429 JNI::EnsureLocalCapacity,
2430 JNI::AllocObject,
2431 JNI::NewObject,
2432 JNI::NewObjectV,
2433 JNI::NewObjectA,
2434 JNI::GetObjectClass,
2435 JNI::IsInstanceOf,
2436 JNI::GetMethodID,
2437 JNI::CallObjectMethod,
2438 JNI::CallObjectMethodV,
2439 JNI::CallObjectMethodA,
2440 JNI::CallBooleanMethod,
2441 JNI::CallBooleanMethodV,
2442 JNI::CallBooleanMethodA,
2443 JNI::CallByteMethod,
2444 JNI::CallByteMethodV,
2445 JNI::CallByteMethodA,
2446 JNI::CallCharMethod,
2447 JNI::CallCharMethodV,
2448 JNI::CallCharMethodA,
2449 JNI::CallShortMethod,
2450 JNI::CallShortMethodV,
2451 JNI::CallShortMethodA,
2452 JNI::CallIntMethod,
2453 JNI::CallIntMethodV,
2454 JNI::CallIntMethodA,
2455 JNI::CallLongMethod,
2456 JNI::CallLongMethodV,
2457 JNI::CallLongMethodA,
2458 JNI::CallFloatMethod,
2459 JNI::CallFloatMethodV,
2460 JNI::CallFloatMethodA,
2461 JNI::CallDoubleMethod,
2462 JNI::CallDoubleMethodV,
2463 JNI::CallDoubleMethodA,
2464 JNI::CallVoidMethod,
2465 JNI::CallVoidMethodV,
2466 JNI::CallVoidMethodA,
2467 JNI::CallNonvirtualObjectMethod,
2468 JNI::CallNonvirtualObjectMethodV,
2469 JNI::CallNonvirtualObjectMethodA,
2470 JNI::CallNonvirtualBooleanMethod,
2471 JNI::CallNonvirtualBooleanMethodV,
2472 JNI::CallNonvirtualBooleanMethodA,
2473 JNI::CallNonvirtualByteMethod,
2474 JNI::CallNonvirtualByteMethodV,
2475 JNI::CallNonvirtualByteMethodA,
2476 JNI::CallNonvirtualCharMethod,
2477 JNI::CallNonvirtualCharMethodV,
2478 JNI::CallNonvirtualCharMethodA,
2479 JNI::CallNonvirtualShortMethod,
2480 JNI::CallNonvirtualShortMethodV,
2481 JNI::CallNonvirtualShortMethodA,
2482 JNI::CallNonvirtualIntMethod,
2483 JNI::CallNonvirtualIntMethodV,
2484 JNI::CallNonvirtualIntMethodA,
2485 JNI::CallNonvirtualLongMethod,
2486 JNI::CallNonvirtualLongMethodV,
2487 JNI::CallNonvirtualLongMethodA,
2488 JNI::CallNonvirtualFloatMethod,
2489 JNI::CallNonvirtualFloatMethodV,
2490 JNI::CallNonvirtualFloatMethodA,
2491 JNI::CallNonvirtualDoubleMethod,
2492 JNI::CallNonvirtualDoubleMethodV,
2493 JNI::CallNonvirtualDoubleMethodA,
2494 JNI::CallNonvirtualVoidMethod,
2495 JNI::CallNonvirtualVoidMethodV,
2496 JNI::CallNonvirtualVoidMethodA,
2497 JNI::GetFieldID,
2498 JNI::GetObjectField,
2499 JNI::GetBooleanField,
2500 JNI::GetByteField,
2501 JNI::GetCharField,
2502 JNI::GetShortField,
2503 JNI::GetIntField,
2504 JNI::GetLongField,
2505 JNI::GetFloatField,
2506 JNI::GetDoubleField,
2507 JNI::SetObjectField,
2508 JNI::SetBooleanField,
2509 JNI::SetByteField,
2510 JNI::SetCharField,
2511 JNI::SetShortField,
2512 JNI::SetIntField,
2513 JNI::SetLongField,
2514 JNI::SetFloatField,
2515 JNI::SetDoubleField,
2516 JNI::GetStaticMethodID,
2517 JNI::CallStaticObjectMethod,
2518 JNI::CallStaticObjectMethodV,
2519 JNI::CallStaticObjectMethodA,
2520 JNI::CallStaticBooleanMethod,
2521 JNI::CallStaticBooleanMethodV,
2522 JNI::CallStaticBooleanMethodA,
2523 JNI::CallStaticByteMethod,
2524 JNI::CallStaticByteMethodV,
2525 JNI::CallStaticByteMethodA,
2526 JNI::CallStaticCharMethod,
2527 JNI::CallStaticCharMethodV,
2528 JNI::CallStaticCharMethodA,
2529 JNI::CallStaticShortMethod,
2530 JNI::CallStaticShortMethodV,
2531 JNI::CallStaticShortMethodA,
2532 JNI::CallStaticIntMethod,
2533 JNI::CallStaticIntMethodV,
2534 JNI::CallStaticIntMethodA,
2535 JNI::CallStaticLongMethod,
2536 JNI::CallStaticLongMethodV,
2537 JNI::CallStaticLongMethodA,
2538 JNI::CallStaticFloatMethod,
2539 JNI::CallStaticFloatMethodV,
2540 JNI::CallStaticFloatMethodA,
2541 JNI::CallStaticDoubleMethod,
2542 JNI::CallStaticDoubleMethodV,
2543 JNI::CallStaticDoubleMethodA,
2544 JNI::CallStaticVoidMethod,
2545 JNI::CallStaticVoidMethodV,
2546 JNI::CallStaticVoidMethodA,
2547 JNI::GetStaticFieldID,
2548 JNI::GetStaticObjectField,
2549 JNI::GetStaticBooleanField,
2550 JNI::GetStaticByteField,
2551 JNI::GetStaticCharField,
2552 JNI::GetStaticShortField,
2553 JNI::GetStaticIntField,
2554 JNI::GetStaticLongField,
2555 JNI::GetStaticFloatField,
2556 JNI::GetStaticDoubleField,
2557 JNI::SetStaticObjectField,
2558 JNI::SetStaticBooleanField,
2559 JNI::SetStaticByteField,
2560 JNI::SetStaticCharField,
2561 JNI::SetStaticShortField,
2562 JNI::SetStaticIntField,
2563 JNI::SetStaticLongField,
2564 JNI::SetStaticFloatField,
2565 JNI::SetStaticDoubleField,
2566 JNI::NewString,
2567 JNI::GetStringLength,
2568 JNI::GetStringChars,
2569 JNI::ReleaseStringChars,
2570 JNI::NewStringUTF,
2571 JNI::GetStringUTFLength,
2572 JNI::GetStringUTFChars,
2573 JNI::ReleaseStringUTFChars,
2574 JNI::GetArrayLength,
2575 JNI::NewObjectArray,
2576 JNI::GetObjectArrayElement,
2577 JNI::SetObjectArrayElement,
2578 JNI::NewBooleanArray,
2579 JNI::NewByteArray,
2580 JNI::NewCharArray,
2581 JNI::NewShortArray,
2582 JNI::NewIntArray,
2583 JNI::NewLongArray,
2584 JNI::NewFloatArray,
2585 JNI::NewDoubleArray,
2586 JNI::GetBooleanArrayElements,
2587 JNI::GetByteArrayElements,
2588 JNI::GetCharArrayElements,
2589 JNI::GetShortArrayElements,
2590 JNI::GetIntArrayElements,
2591 JNI::GetLongArrayElements,
2592 JNI::GetFloatArrayElements,
2593 JNI::GetDoubleArrayElements,
2594 JNI::ReleaseBooleanArrayElements,
2595 JNI::ReleaseByteArrayElements,
2596 JNI::ReleaseCharArrayElements,
2597 JNI::ReleaseShortArrayElements,
2598 JNI::ReleaseIntArrayElements,
2599 JNI::ReleaseLongArrayElements,
2600 JNI::ReleaseFloatArrayElements,
2601 JNI::ReleaseDoubleArrayElements,
2602 JNI::GetBooleanArrayRegion,
2603 JNI::GetByteArrayRegion,
2604 JNI::GetCharArrayRegion,
2605 JNI::GetShortArrayRegion,
2606 JNI::GetIntArrayRegion,
2607 JNI::GetLongArrayRegion,
2608 JNI::GetFloatArrayRegion,
2609 JNI::GetDoubleArrayRegion,
2610 JNI::SetBooleanArrayRegion,
2611 JNI::SetByteArrayRegion,
2612 JNI::SetCharArrayRegion,
2613 JNI::SetShortArrayRegion,
2614 JNI::SetIntArrayRegion,
2615 JNI::SetLongArrayRegion,
2616 JNI::SetFloatArrayRegion,
2617 JNI::SetDoubleArrayRegion,
2618 JNI::RegisterNatives,
2619 JNI::UnregisterNatives,
2620 JNI::MonitorEnter,
2621 JNI::MonitorExit,
2622 JNI::GetJavaVM,
2623 JNI::GetStringRegion,
2624 JNI::GetStringUTFRegion,
2625 JNI::GetPrimitiveArrayCritical,
2626 JNI::ReleasePrimitiveArrayCritical,
2627 JNI::GetStringCritical,
2628 JNI::ReleaseStringCritical,
2629 JNI::NewWeakGlobalRef,
2630 JNI::DeleteWeakGlobalRef,
2631 JNI::ExceptionCheck,
2632 JNI::NewDirectByteBuffer,
2633 JNI::GetDirectBufferAddress,
2634 JNI::GetDirectBufferCapacity,
2635 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002636};
2637
Elliott Hughes75770752011-08-24 17:52:38 -07002638JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002639 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002640 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002641 local_ref_cookie(IRT_FIRST_SEGMENT),
2642 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002643 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002644 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002645 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002646 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002647 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002648 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002649 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002650 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2651 // errors will ensue.
2652 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2653 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002654}
2655
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002656JNIEnvExt::~JNIEnvExt() {
2657}
2658
Elliott Hughes88c5c352012-03-15 18:49:48 -07002659void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2660 check_jni = enabled;
2661 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002662}
2663
Elliott Hughes73e66f72012-05-09 09:34:45 -07002664void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2665 locals.Dump(os);
2666 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002667}
2668
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002669void JNIEnvExt::PushFrame(int /*capacity*/) {
2670 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002671 stacked_local_ref_cookies.push_back(local_ref_cookie);
2672 local_ref_cookie = locals.GetSegmentState();
2673}
2674
2675void JNIEnvExt::PopFrame() {
2676 locals.SetSegmentState(local_ref_cookie);
2677 local_ref_cookie = stacked_local_ref_cookies.back();
2678 stacked_local_ref_cookies.pop_back();
2679}
2680
Carl Shapiroea4dca82011-08-01 13:45:38 -07002681// JNI Invocation interface.
2682
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002683extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2684 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2685 if (args->version < JNI_VERSION_1_2) {
2686 return JNI_EVERSION;
2687 }
2688 Runtime::Options options;
2689 for (int i = 0; i < args->nOptions; ++i) {
2690 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002691 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002692 }
2693 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002694 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002695 if (runtime == NULL) {
2696 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002697 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002698 runtime->Start();
2699 *p_env = Thread::Current()->GetJniEnv();
2700 *p_vm = runtime->GetJavaVM();
2701 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002702}
2703
Elliott Hughesf2682d52011-08-15 16:37:04 -07002704extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002705 Runtime* runtime = Runtime::Current();
2706 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002707 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002708 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002709 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002710 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002711 }
2712 return JNI_OK;
2713}
2714
2715// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002716extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002717 return JNI_ERR;
2718}
2719
Elliott Hughescdf53122011-08-19 15:46:09 -07002720class JII {
2721 public:
2722 static jint DestroyJavaVM(JavaVM* vm) {
2723 if (vm == NULL) {
2724 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002725 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002726 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2727 delete raw_vm->runtime;
2728 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002729 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002730
Elliott Hughescdf53122011-08-19 15:46:09 -07002731 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002732 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002733 }
2734
2735 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002736 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002737 }
2738
2739 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002740 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002741 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002742 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002743 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2744 Runtime* runtime = raw_vm->runtime;
2745 runtime->DetachCurrentThread();
2746 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002747 }
2748
2749 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2750 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2751 return JNI_EVERSION;
2752 }
2753 if (vm == NULL || env == NULL) {
2754 return JNI_ERR;
2755 }
2756 Thread* thread = Thread::Current();
2757 if (thread == NULL) {
2758 *env = NULL;
2759 return JNI_EDETACHED;
2760 }
2761 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002762 return JNI_OK;
2763 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002764};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002765
Elliott Hughes88c5c352012-03-15 18:49:48 -07002766const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002767 NULL, // reserved0
2768 NULL, // reserved1
2769 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002770 JII::DestroyJavaVM,
2771 JII::AttachCurrentThread,
2772 JII::DetachCurrentThread,
2773 JII::GetEnv,
2774 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002775};
2776
Elliott Hughesa0957642011-09-02 14:27:33 -07002777JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002778 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002779 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002780 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002781 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002782 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002783 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002784 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002785 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002786 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002787 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002788 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002789 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002790 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002791 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002792 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002793 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002794 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002795 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002796 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002797}
2798
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002799JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002800 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002801}
2802
Elliott Hughes88c5c352012-03-15 18:49:48 -07002803void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2804 check_jni = enabled;
2805 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002806}
2807
Elliott Hughesae80b492012-04-24 10:43:17 -07002808void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2809 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2810 if (force_copy) {
2811 os << " (with forcecopy)";
2812 }
2813 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
2814 {
2815 MutexLock mu(pins_lock);
2816 os << "; pins=" << pin_table.Size();
2817 }
2818 {
2819 MutexLock mu(globals_lock);
2820 os << "; globals=" << globals.Capacity();
2821 }
2822 {
2823 MutexLock mu(weak_globals_lock);
2824 if (weak_globals.Capacity() > 0) {
2825 os << " (plus " << weak_globals.Capacity() << " weak)";
2826 }
2827 }
2828 os << '\n';
2829
2830 {
2831 MutexLock mu(libraries_lock);
2832 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2833 }
2834}
2835
Elliott Hughes73e66f72012-05-09 09:34:45 -07002836void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002837 {
2838 MutexLock mu(globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002839 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002840 }
2841 {
2842 MutexLock mu(weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002843 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002844 }
2845 {
2846 MutexLock mu(pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002847 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002848 }
2849}
2850
Elliott Hughes75770752011-08-24 17:52:38 -07002851bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2852 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002853
2854 // See if we've already loaded this library. If we have, and the class loader
2855 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002856 // TODO: for better results we should canonicalize the pathname (or even compare
2857 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002858 SharedLibrary* library;
2859 {
2860 // TODO: move the locking (and more of this logic) into Libraries.
2861 MutexLock mu(libraries_lock);
2862 library = libraries->Get(path);
2863 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002864 if (library != NULL) {
2865 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002866 // The library will be associated with class_loader. The JNI
2867 // spec says we can't load the same library into more than one
2868 // class loader.
2869 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2870 "ClassLoader %p; can't open in ClassLoader %p",
2871 path.c_str(), library->GetClassLoader(), class_loader);
2872 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002873 return false;
2874 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002875 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2876 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002877 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002878 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2879 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002880 return false;
2881 }
2882 return true;
2883 }
2884
2885 // Open the shared library. Because we're using a full path, the system
2886 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2887 // resolve this library's dependencies though.)
2888
2889 // Failures here are expected when java.library.path has several entries
2890 // and we have to hunt for the lib.
2891
2892 // The current version of the dynamic linker prints detailed information
2893 // about dlopen() failures. Some things to check if the message is
2894 // cryptic:
2895 // - make sure the library exists on the device
2896 // - verify that the right path is being opened (the debug log message
2897 // above can help with that)
2898 // - check to see if the library is valid (e.g. not zero bytes long)
2899 // - check config/prelink-linux-arm.map to ensure that the library
2900 // is listed and is not being overrun by the previous entry (if
2901 // loading suddenly stops working on a prelinked library, this is
2902 // a good one to check)
2903 // - write a trivial app that calls sleep() then dlopen(), attach
2904 // to it with "strace -p <pid>" while it sleeps, and watch for
2905 // attempts to open nonexistent dependent shared libs
2906
2907 // TODO: automate some of these checks!
2908
2909 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002910 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002911 // the GC to ignore us.
2912 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002913 void* handle = NULL;
2914 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002915 ScopedThreadStateChange tsc(self, kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002916 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002917 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002918
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002919 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002920
2921 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002922 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002923 return false;
2924 }
2925
2926 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002927 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002928 // TODO: move the locking (and more of this logic) into Libraries.
2929 MutexLock mu(libraries_lock);
2930 library = libraries->Get(path);
2931 if (library != NULL) {
2932 LOG(INFO) << "WOW: we lost a race to add shared library: "
2933 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002934 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002935 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002936 library = new SharedLibrary(path, handle, class_loader);
2937 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002938 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002939
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002940 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002941
2942 bool result = true;
2943 void* sym = dlsym(handle, "JNI_OnLoad");
2944 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002945 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002946 } else {
2947 // Call JNI_OnLoad. We have to override the current class
2948 // loader, which will always be "null" since the stuff at the
2949 // top of the stack is around Runtime.loadLibrary(). (See
2950 // the comments in the JNI FindClass function.)
2951 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2952 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002953 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002954 self->SetClassLoaderOverride(class_loader);
2955
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002956 int version = 0;
2957 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002958 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002959 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002960 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002961 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002962
Brian Carlstromaded5f72011-10-07 17:15:04 -07002963 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002964
2965 if (version != JNI_VERSION_1_2 &&
2966 version != JNI_VERSION_1_4 &&
2967 version != JNI_VERSION_1_6) {
2968 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2969 << "bad version: " << version;
2970 // It's unwise to call dlclose() here, but we can mark it
2971 // as bad and ensure that future load attempts will fail.
2972 // We don't know how far JNI_OnLoad got, so there could
2973 // be some partially-initialized stuff accessible through
2974 // newly-registered native method calls. We could try to
2975 // unregister them, but that doesn't seem worthwhile.
2976 result = false;
2977 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002978 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2979 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002980 }
2981 }
2982
2983 library->SetResult(result);
2984 return result;
2985}
2986
2987void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2988 CHECK(m->IsNative());
2989
2990 Class* c = m->GetDeclaringClass();
2991
2992 // If this is a static method, it could be called before the class
2993 // has been initialized.
2994 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002995 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002996 return NULL;
2997 }
2998 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002999 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003000 }
3001
Brian Carlstrom16192862011-09-12 17:50:06 -07003002 std::string detail;
3003 void* native_method;
3004 {
3005 MutexLock mu(libraries_lock);
3006 native_method = libraries->FindNativeMethod(m, detail);
3007 }
3008 // throwing can cause libraries_lock to be reacquired
3009 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07003010 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003011 }
3012 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003013}
3014
Elliott Hughes410c0c82011-09-01 17:58:25 -07003015void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
3016 {
3017 MutexLock mu(globals_lock);
3018 globals.VisitRoots(visitor, arg);
3019 }
3020 {
3021 MutexLock mu(pins_lock);
3022 pin_table.VisitRoots(visitor, arg);
3023 }
3024 // The weak_globals table is visited by the GC itself (because it mutates the table).
3025}
3026
Ian Rogersdf20fe02011-07-20 20:34:16 -07003027} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003028
3029std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3030 switch (rhs) {
3031 case JNIInvalidRefType:
3032 os << "JNIInvalidRefType";
3033 return os;
3034 case JNILocalRefType:
3035 os << "JNILocalRefType";
3036 return os;
3037 case JNIGlobalRefType:
3038 os << "JNIGlobalRefType";
3039 return os;
3040 case JNIWeakGlobalRefType:
3041 os << "JNIWeakGlobalRefType";
3042 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003043 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003044 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003045 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003046 }
3047}