blob: 03d668c0c0266370dc0908ee967e82218af8e3a3 [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 Hughes462c9442012-03-23 18:47:50 -0700525static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700526 if (vm == NULL || p_env == NULL) {
527 return JNI_ERR;
528 }
529
Elliott Hughes462c9442012-03-23 18:47:50 -0700530 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700531 Thread* self = Thread::Current();
532 if (self != NULL) {
533 *p_env = self->GetJniEnv();
534 return JNI_OK;
535 }
536
537 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
538
539 // No threads allowed in zygote mode.
540 if (runtime->IsZygote()) {
541 LOG(ERROR) << "Attempt to attach a thread in the zygote";
542 return JNI_ERR;
543 }
544
Elliott Hughes462c9442012-03-23 18:47:50 -0700545 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
546 const char* thread_name = NULL;
547 Object* thread_group = NULL;
548 if (args != NULL) {
549 CHECK_GE(args->version, JNI_VERSION_1_2);
550 thread_name = args->name;
551 thread_group = static_cast<Thread*>(NULL)->DecodeJObject(args->group);
Elliott Hughes75770752011-08-24 17:52:38 -0700552 }
Elliott Hughes75770752011-08-24 17:52:38 -0700553
Elliott Hughes462c9442012-03-23 18:47:50 -0700554 runtime->AttachCurrentThread(thread_name, as_daemon, thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700555 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700556 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700557}
558
Elliott Hughes79082e32011-08-25 12:07:32 -0700559class SharedLibrary {
560 public:
561 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
562 : path_(path),
563 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700564 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700565 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughese62934d2012-04-09 11:24:29 -0700566 jni_on_load_cond_("JNI_OnLoad condition variable"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700567 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700568 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700569 }
570
Elliott Hughes79082e32011-08-25 12:07:32 -0700571 Object* GetClassLoader() {
572 return class_loader_;
573 }
574
575 std::string GetPath() {
576 return path_;
577 }
578
579 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700580 * Check the result of an earlier call to JNI_OnLoad on this library.
581 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700582 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700583 bool CheckOnLoadResult() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700584 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700585 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700586 // Check this so we don't end up waiting for ourselves. We need
587 // to return "true" so the caller can continue.
588 LOG(INFO) << *self << " recursive attempt to load library "
589 << "\"" << path_ << "\"";
590 return true;
591 }
592
593 MutexLock mu(jni_on_load_lock_);
594 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800595 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
596 << "JNI_OnLoad...]";
Elliott Hughes34e06962012-04-09 13:55:55 -0700597 ScopedThreadStateChange tsc(self, kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700598 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700599 }
600
601 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800602 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
603 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700604 return okay;
605 }
606
607 void SetResult(bool result) {
608 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700609 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700610
611 // Broadcast a wakeup to anybody sleeping on the condition variable.
612 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700613 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700614 }
615
616 void* FindSymbol(const std::string& symbol_name) {
617 return dlsym(handle_, symbol_name.c_str());
618 }
619
620 private:
621 enum JNI_OnLoadState {
622 kPending,
623 kFailed,
624 kOkay,
625 };
626
627 // Path to library "/system/lib/libjni.so".
628 std::string path_;
629
630 // The void* returned by dlopen(3).
631 void* handle_;
632
633 // The ClassLoader this library is associated with.
634 Object* class_loader_;
635
636 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700637 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700638 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700639 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700640 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700641 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700642 // Result of earlier JNI_OnLoad call.
643 JNI_OnLoadState jni_on_load_result_;
644};
645
Elliott Hughes79082e32011-08-25 12:07:32 -0700646// This exists mainly to keep implementation details out of the header file.
647class Libraries {
648 public:
649 Libraries() {
650 }
651
652 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700653 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700654 }
655
Elliott Hughesae80b492012-04-24 10:43:17 -0700656 void Dump(std::ostream& os) const {
657 bool first = true;
658 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
659 if (!first) {
660 os << ' ';
661 }
662 first = false;
663 os << it->first;
664 }
665 }
666
667 size_t size() const {
668 return libraries_.size();
669 }
670
Elliott Hughes79082e32011-08-25 12:07:32 -0700671 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700672 It it = libraries_.find(path);
673 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700674 }
675
676 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700677 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700678 }
679
680 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700681 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700682 std::string jni_short_name(JniShortName(m));
683 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700684 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700685 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
686 SharedLibrary* library = it->second;
687 if (library->GetClassLoader() != declaring_class_loader) {
688 // We only search libraries loaded by the appropriate ClassLoader.
689 continue;
690 }
691 // Try the short name then the long name...
692 void* fn = library->FindSymbol(jni_short_name);
693 if (fn == NULL) {
694 fn = library->FindSymbol(jni_long_name);
695 }
696 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800697 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
698 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700699 return fn;
700 }
701 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700702 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700703 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700704 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700705 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700706 return NULL;
707 }
708
709 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700710 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700711
Elliott Hughesa0e18062012-04-13 15:59:59 -0700712 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700713};
714
Elliott Hughes418d20f2011-09-22 14:00:39 -0700715JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
716 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
717 Object* receiver = Decode<Object*>(env, obj);
718 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800719 ArgArray arg_array(method);
720 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700721 return InvokeWithArgArray(env, receiver, method, arg_array.get());
722}
723
Elliott Hughesd07986f2011-12-06 18:27:45 -0800724JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700725 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800726}
727
Elliott Hughescdf53122011-08-19 15:46:09 -0700728class JNI {
729 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700730
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 static jint GetVersion(JNIEnv* env) {
732 ScopedJniThreadState ts(env);
733 return JNI_VERSION_1_6;
734 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700735
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
737 ScopedJniThreadState ts(env);
738 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700739 return NULL;
740 }
741
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 static jclass FindClass(JNIEnv* env, const char* name) {
743 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700744 Runtime* runtime = Runtime::Current();
745 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700746 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700747 Class* c = NULL;
748 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700749 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800750 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700751 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800752 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700753 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700754 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700755 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700756
Elliott Hughescdf53122011-08-19 15:46:09 -0700757 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
758 ScopedJniThreadState ts(env);
759 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700760 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700761 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700762
Elliott Hughescdf53122011-08-19 15:46:09 -0700763 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
764 ScopedJniThreadState ts(env);
765 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700766 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700768
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
770 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700771 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700772 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700774
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
776 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700777 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700778 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700780
Elliott Hughes37f7a402011-08-22 18:56:01 -0700781 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
782 ScopedJniThreadState ts(env);
783 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700784 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700785 }
786
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700787 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700788 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700789 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700790 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700792
Elliott Hughes37f7a402011-08-22 18:56:01 -0700793 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700794 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700795 Class* c1 = Decode<Class*>(ts, java_class1);
796 Class* c2 = Decode<Class*>(ts, java_class2);
797 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700798 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700799
Elliott Hughese84278b2012-03-22 10:06:53 -0700800 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700802 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700803 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700804 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700805 return JNI_TRUE;
806 } else {
807 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700808 Class* c = Decode<Class*>(ts, java_class);
809 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700810 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700812
Elliott Hughes37f7a402011-08-22 18:56:01 -0700813 static jint Throw(JNIEnv* env, jthrowable java_exception) {
814 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700815 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700816 if (exception == NULL) {
817 return JNI_ERR;
818 }
819 ts.Self()->SetException(exception);
820 return JNI_OK;
821 }
822
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700823 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700824 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700825 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700826 jmethodID mid = ((msg != NULL)
827 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
828 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700829 if (mid == NULL) {
830 return JNI_ERR;
831 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700832 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700833 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700834 return JNI_ERR;
835 }
836
837 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700838 args[0].l = s.get();
839 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
840 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700841 return JNI_ERR;
842 }
843
Elliott Hughes72025e52011-08-23 17:50:30 -0700844 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700845
Elliott Hughes37f7a402011-08-22 18:56:01 -0700846 return JNI_OK;
847 }
848
849 static jboolean ExceptionCheck(JNIEnv* env) {
850 ScopedJniThreadState ts(env);
851 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
852 }
853
854 static void ExceptionClear(JNIEnv* env) {
855 ScopedJniThreadState ts(env);
856 ts.Self()->ClearException();
857 }
858
859 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700861
862 Thread* self = ts.Self();
863 Throwable* original_exception = self->GetException();
864 self->ClearException();
865
Elliott Hughesbf86d042011-08-31 17:53:14 -0700866 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700867 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
868 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
869 if (mid == NULL) {
870 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700871 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700872 } else {
873 env->CallVoidMethod(exception.get(), mid);
874 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700875 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700876 << " thrown while calling printStackTrace";
877 self->ClearException();
878 }
879 }
880
881 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700883
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 static jthrowable ExceptionOccurred(JNIEnv* env) {
885 ScopedJniThreadState ts(env);
886 Object* exception = ts.Self()->GetException();
Elliott Hughes81ff3182012-03-23 20:35:56 -0700887 return (exception != NULL) ? AddLocalReference<jthrowable>(env, exception) : NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 }
889
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 static void FatalError(JNIEnv* env, const char* msg) {
891 ScopedJniThreadState ts(env);
892 LOG(FATAL) << "JNI FatalError called: " << msg;
893 }
894
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700895 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700897 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
898 return JNI_ERR;
899 }
900 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 return JNI_OK;
902 }
903
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700904 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700905 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700906 Object* survivor = Decode<Object*>(ts, java_survivor);
907 ts.Env()->PopFrame();
908 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700909 }
910
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700911 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700912 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700913 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
914 }
915
916 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
917 // TODO: we should try to expand the table if necessary.
918 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
919 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
920 return JNI_ERR;
921 }
922 // TODO: this isn't quite right, since "capacity" includes holes.
923 size_t capacity = ts.Env()->locals.Capacity();
924 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
925 if (!okay) {
926 ts.Self()->ThrowOutOfMemoryError(caller);
927 }
928 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 }
930
Elliott Hughescdf53122011-08-19 15:46:09 -0700931 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
932 ScopedJniThreadState ts(env);
933 if (obj == NULL) {
934 return NULL;
935 }
936
Elliott Hughes75770752011-08-24 17:52:38 -0700937 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 IndirectReferenceTable& globals = vm->globals;
939 MutexLock mu(vm->globals_lock);
940 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
941 return reinterpret_cast<jobject>(ref);
942 }
943
944 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
945 ScopedJniThreadState ts(env);
946 if (obj == NULL) {
947 return;
948 }
949
Elliott Hughes75770752011-08-24 17:52:38 -0700950 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 IndirectReferenceTable& globals = vm->globals;
952 MutexLock mu(vm->globals_lock);
953
954 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
955 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
956 << "failed to find entry";
957 }
958 }
959
960 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
961 ScopedJniThreadState ts(env);
962 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
963 }
964
965 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
966 ScopedJniThreadState ts(env);
967 if (obj == NULL) {
968 return;
969 }
970
Elliott Hughes75770752011-08-24 17:52:38 -0700971 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 IndirectReferenceTable& weak_globals = vm->weak_globals;
973 MutexLock mu(vm->weak_globals_lock);
974
975 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
976 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
977 << "failed to find entry";
978 }
979 }
980
981 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
982 ScopedJniThreadState ts(env);
983 if (obj == NULL) {
984 return NULL;
985 }
986
987 IndirectReferenceTable& locals = ts.Env()->locals;
988
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700989 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
991 return reinterpret_cast<jobject>(ref);
992 }
993
994 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
995 ScopedJniThreadState ts(env);
996 if (obj == NULL) {
997 return;
998 }
999
1000 IndirectReferenceTable& locals = ts.Env()->locals;
1001
Ian Rogers5a7a74a2011-09-26 16:32:29 -07001002 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 if (!locals.Remove(cookie, obj)) {
1004 // Attempting to delete a local reference that is not in the
1005 // topmost local reference frame is a no-op. DeleteLocalRef returns
1006 // void and doesn't throw any exceptions, but we should probably
1007 // complain about it so the user will notice that things aren't
1008 // going quite the way they expect.
1009 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
1010 << "failed to find entry";
1011 }
1012 }
1013
1014 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
1015 ScopedJniThreadState ts(env);
1016 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
1017 ? JNI_TRUE : JNI_FALSE;
1018 }
1019
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001020 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001022 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001023 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001024 return NULL;
1025 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001026 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughese84278b2012-03-22 10:06:53 -07001029 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 ScopedJniThreadState ts(env);
1031 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001033 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 va_end(args);
1035 return result;
1036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001040 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001041 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001042 return NULL;
1043 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001044 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001045 if (result == NULL) {
1046 return NULL;
1047 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001048 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001050 if (!ts.Self()->IsExceptionPending()) {
1051 return local_result;
1052 } else {
1053 return NULL;
1054 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* 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<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 CallNonvirtualVoidMethodA(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 Hughescdf53122011-08-19 15:46:09 -07001076 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1077 ScopedJniThreadState ts(env);
1078 return FindMethodID(ts, c, name, sig, false);
1079 }
1080
1081 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1082 ScopedJniThreadState ts(env);
1083 return FindMethodID(ts, c, name, sig, true);
1084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 va_list ap;
1089 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001090 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001092 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001097 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001098 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001103 JValue result(InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001104 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_list ap;
1110 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001111 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001113 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001118 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001123 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static jbyte CallByteMethod(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.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 static jbyte CallByteMethodV(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).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 static jbyte CallByteMethodA(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).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 }
1144
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 static jchar CallCharMethod(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.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 }
1153
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 static jchar CallCharMethodV(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).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 static jchar CallCharMethodA(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).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 static jdouble CallDoubleMethod(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.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 static jdouble CallDoubleMethodV(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).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 static jdouble CallDoubleMethodA(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).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 }
1182
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 static jfloat CallFloatMethod(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.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 static jfloat CallFloatMethodV(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).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 static jfloat CallFloatMethodA(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).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 static jint CallIntMethod(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.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 }
1210
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 static jint CallIntMethodV(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).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
1215
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 static jint CallIntMethodA(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).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 static jlong CallLongMethod(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.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
Elliott Hughes72025e52011-08-23 17:50:30 -07001230 static jlong CallLongMethodV(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).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 }
1234
Elliott Hughes72025e52011-08-23 17:50:30 -07001235 static jlong CallLongMethodA(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).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 static jshort CallShortMethod(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.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
Elliott Hughes72025e52011-08-23 17:50:30 -07001249 static jshort CallShortMethodV(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).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001252 }
1253
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 static jshort CallShortMethodA(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).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 static void CallVoidMethod(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 Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001269 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001274 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001277 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 ScopedJniThreadState ts(env);
1279 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001280 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001281 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001282 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 va_end(ap);
1284 return local_result;
1285 }
1286
1287 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001288 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001290 JValue result(InvokeWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001291 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
1294 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001295 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001297 JValue result(InvokeWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001298 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
1301 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001302 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 ScopedJniThreadState ts(env);
1304 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001305 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001306 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001308 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
1311 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001312 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001314 return InvokeWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
1317 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001318 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001320 return InvokeWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001323 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 ScopedJniThreadState ts(env);
1325 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001327 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001329 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 }
1331
1332 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001333 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001335 return InvokeWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 }
1337
1338 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001339 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001341 return InvokeWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001344 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001345 ScopedJniThreadState ts(env);
1346 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001347 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001348 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001350 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
1353 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001354 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001356 return InvokeWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
1359 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001360 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001362 return InvokeWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001365 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 ScopedJniThreadState ts(env);
1367 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001368 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001369 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001371 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
1374 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001375 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001377 return InvokeWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
1380 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001381 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001383 return InvokeWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
1385
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001386 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 ScopedJniThreadState ts(env);
1388 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001389 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001390 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001392 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
1395 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001396 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001398 return InvokeWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001399 }
1400
1401 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001402 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001404 return InvokeWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001407 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 ScopedJniThreadState ts(env);
1409 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001410 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001411 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001413 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
1416 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001417 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001419 return InvokeWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
1422 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001423 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001425 return InvokeWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001428 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 ScopedJniThreadState ts(env);
1430 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001431 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001432 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001434 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
1436
1437 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001438 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001440 return InvokeWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
1443 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001444 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001446 return InvokeWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 }
1448
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001449 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 ScopedJniThreadState ts(env);
1451 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001452 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001453 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001455 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
1458 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001459 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001461 return InvokeWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
1464 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001465 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001467 return InvokeWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001470 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001471 ScopedJniThreadState ts(env);
1472 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001473 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001474 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 va_end(ap);
1476 }
1477
1478 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001479 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001481 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
1484 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001485 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001487 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 }
1489
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001490 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 ScopedJniThreadState ts(env);
1492 return FindFieldID(ts, c, name, sig, false);
1493 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001494
1495
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001496 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001497 ScopedJniThreadState ts(env);
1498 return FindFieldID(ts, c, name, sig, true);
1499 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001500
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001501 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001504 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001505 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001507
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001510 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001511 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 Object* o = Decode<Object*>(ts, java_object);
1517 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001518 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
1521
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001522 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001524 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001525 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001526 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 }
1528
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001529#define GET_PRIMITIVE_FIELD(fn, instance) \
1530 ScopedJniThreadState ts(env); \
1531 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001532 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001533 return f->fn(o)
1534
1535#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1536 ScopedJniThreadState ts(env); \
1537 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001538 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001539 f->fn(o, value)
1540
1541 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1542 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1546 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001549 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1550 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001553 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1554 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 }
1556
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001557 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1558 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001561 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1562 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001565 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1566 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001569 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1570 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 }
1572
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001573 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001577 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001578 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 }
1580
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001581 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001582 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001585 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001586 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001589 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001593 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001594 GET_PRIMITIVE_FIELD(GetLong, NULL);
1595 }
1596
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001597 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001598 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1599 }
1600
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001601 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001602 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1603 }
1604
1605 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1606 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1607 }
1608
1609 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1610 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1611 }
1612
1613 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1614 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1615 }
1616
1617 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1618 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1619 }
1620
1621 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1622 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1623 }
1624
1625 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1626 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1627 }
1628
1629 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1630 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1631 }
1632
1633 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1634 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1635 }
1636
1637 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1638 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1639 }
1640
1641 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1642 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1643 }
1644
1645 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1646 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1647 }
1648
1649 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1650 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1651 }
1652
1653 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1654 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1655 }
1656
1657 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1658 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1659 }
1660
1661 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1662 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1663 }
1664
1665 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1666 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001669 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 ScopedJniThreadState ts(env);
1671 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001672 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001673 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001674 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 va_end(ap);
1676 return local_result;
1677 }
1678
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001679 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001681 JValue result(InvokeWithVarArgs(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001682 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 }
1684
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001685 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001687 JValue result(InvokeWithJValues(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001688 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 }
1690
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001691 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 ScopedJniThreadState ts(env);
1693 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001694 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001695 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001697 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 }
1699
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001700 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001702 return InvokeWithVarArgs(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 }
1704
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001705 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001707 return InvokeWithJValues(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 }
1709
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001710 static jbyte CallStaticByteMethod(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.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001719 static jbyte CallStaticByteMethodV(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).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 }
1723
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001724 static jbyte CallStaticByteMethodA(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).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static jchar CallStaticCharMethod(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.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001738 static jchar CallStaticCharMethodV(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).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001743 static jchar CallStaticCharMethodA(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).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 }
1747
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001748 static jshort CallStaticShortMethod(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.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001757 static jshort CallStaticShortMethodV(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).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001762 static jshort CallStaticShortMethodA(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).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001767 static jint CallStaticIntMethod(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.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001776 static jint CallStaticIntMethodV(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).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001781 static jint CallStaticIntMethodA(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).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 }
1785
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001786 static jlong CallStaticLongMethod(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.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001795 static jlong CallStaticLongMethodV(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).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001800 static jlong CallStaticLongMethodA(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).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001805 static jfloat CallStaticFloatMethod(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.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001814 static jfloat CallStaticFloatMethodV(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).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 }
1818
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001819 static jfloat CallStaticFloatMethodA(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).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 }
1823
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001824 static jdouble CallStaticDoubleMethod(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.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001833 static jdouble CallStaticDoubleMethodV(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).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001838 static jdouble CallStaticDoubleMethodA(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).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001843 static void CallStaticVoidMethod(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 Hughes418d20f2011-09-22 14:00:39 -07001847 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 va_end(ap);
1849 }
1850
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001851 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001853 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 }
1855
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001856 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001858 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 }
1860
Elliott Hughes814e4032011-08-23 12:07:56 -07001861 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001863 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001864 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
1867 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1868 ScopedJniThreadState ts(env);
1869 if (utf == NULL) {
1870 return NULL;
1871 }
1872 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001873 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 }
1875
Elliott Hughes814e4032011-08-23 12:07:56 -07001876 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1877 ScopedJniThreadState ts(env);
1878 return Decode<String*>(ts, java_string)->GetLength();
1879 }
1880
1881 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1882 ScopedJniThreadState ts(env);
1883 return Decode<String*>(ts, java_string)->GetUtfLength();
1884 }
1885
Elliott Hughesb465ab02011-08-24 11:21:21 -07001886 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001888 String* s = Decode<String*>(ts, java_string);
1889 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1890 ThrowSIOOBE(ts, start, length, s->GetLength());
1891 } else {
1892 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1893 memcpy(buf, chars + start, length * sizeof(jchar));
1894 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001895 }
1896
Elliott Hughesb465ab02011-08-24 11:21:21 -07001897 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001898 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001899 String* s = Decode<String*>(ts, java_string);
1900 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1901 ThrowSIOOBE(ts, start, length, s->GetLength());
1902 } else {
1903 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1904 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1905 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001906 }
1907
Elliott Hughes75770752011-08-24 17:52:38 -07001908 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001909 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001910 String* s = Decode<String*>(ts, java_string);
1911 const CharArray* chars = s->GetCharArray();
1912 PinPrimitiveArray(ts, chars);
1913 if (is_copy != NULL) {
1914 *is_copy = JNI_FALSE;
1915 }
1916 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001917 }
1918
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001919 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001920 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001921 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 }
1923
Elliott Hughes75770752011-08-24 17:52:38 -07001924 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001925 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001926 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 }
1928
Elliott Hughes75770752011-08-24 17:52:38 -07001929 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001930 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001931 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 }
1933
Elliott Hughes75770752011-08-24 17:52:38 -07001934 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001935 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001936 if (java_string == NULL) {
1937 return NULL;
1938 }
1939 if (is_copy != NULL) {
1940 *is_copy = JNI_TRUE;
1941 }
1942 String* s = Decode<String*>(ts, java_string);
1943 size_t byte_count = s->GetUtfLength();
1944 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001945 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001946 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1947 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1948 bytes[byte_count] = '\0';
1949 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001950 }
1951
Elliott Hughes75770752011-08-24 17:52:38 -07001952 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001953 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001954 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001955 }
1956
Elliott Hughesbd935992011-08-22 11:59:34 -07001957 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001959 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001960 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001961 Array* array = obj->AsArray();
1962 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 }
1964
Elliott Hughes814e4032011-08-23 12:07:56 -07001965 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001967 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001968 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001969 }
1970
1971 static void SetObjectArrayElement(JNIEnv* env,
1972 jobjectArray java_array, jsize index, jobject java_value) {
1973 ScopedJniThreadState ts(env);
1974 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1975 Object* value = Decode<Object*>(ts, java_value);
1976 array->Set(index, value);
1977 }
1978
1979 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1980 ScopedJniThreadState ts(env);
1981 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1982 }
1983
1984 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1985 ScopedJniThreadState ts(env);
1986 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1987 }
1988
1989 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1990 ScopedJniThreadState ts(env);
1991 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1992 }
1993
1994 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1995 ScopedJniThreadState ts(env);
1996 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1997 }
1998
1999 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
2000 ScopedJniThreadState ts(env);
2001 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
2002 }
2003
2004 static jintArray NewIntArray(JNIEnv* env, jsize length) {
2005 ScopedJniThreadState ts(env);
2006 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
2007 }
2008
2009 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2010 ScopedJniThreadState ts(env);
2011 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
2012 }
2013
2014 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
2015 ScopedJniThreadState ts(env);
2016 CHECK_GE(length, 0); // TODO: ReportJniError
2017
2018 // Compute the array class corresponding to the given element class.
2019 Class* element_class = Decode<Class*>(ts, element_jclass);
2020 std::string descriptor;
2021 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002022 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002023
2024 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002025 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2026 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 return NULL;
2028 }
2029
Elliott Hughes75770752011-08-24 17:52:38 -07002030 // Allocate and initialize if necessary.
2031 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002033 if (initial_element != NULL) {
2034 Object* initial_object = Decode<Object*>(ts, initial_element);
2035 for (jsize i = 0; i < length; ++i) {
2036 result->Set(i, initial_object);
2037 }
2038 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002039 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
2042 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2043 ScopedJniThreadState ts(env);
2044 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2045 }
2046
Ian Rogersa15e67d2012-02-28 13:51:55 -08002047 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002048 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002049 Array* array = Decode<Array*>(ts, java_array);
2050 PinPrimitiveArray(ts, array);
2051 if (is_copy != NULL) {
2052 *is_copy = JNI_FALSE;
2053 }
2054 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002055 }
2056
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002057 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002058 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002059 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002060 }
2061
Elliott Hughes75770752011-08-24 17:52:38 -07002062 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002064 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes75770752011-08-24 17:52:38 -07002067 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002069 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Elliott Hughes75770752011-08-24 17:52:38 -07002072 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002074 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes75770752011-08-24 17:52:38 -07002077 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002078 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002079 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Elliott Hughes75770752011-08-24 17:52:38 -07002082 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002084 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Elliott Hughes75770752011-08-24 17:52:38 -07002087 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002089 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
Elliott Hughes75770752011-08-24 17:52:38 -07002092 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002094 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 }
2096
Elliott Hughes75770752011-08-24 17:52:38 -07002097 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002099 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 }
2101
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002102 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002104 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 }
2106
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002107 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002109 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002112 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002114 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 }
2116
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002117 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002119 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002122 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002124 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002127 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002129 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002132 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002134 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 }
2136
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002137 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002139 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 }
2141
Elliott Hughes814e4032011-08-23 12:07:56 -07002142 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002144 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 }
2146
Elliott Hughes814e4032011-08-23 12:07:56 -07002147 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002149 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 }
2151
Elliott Hughes814e4032011-08-23 12:07:56 -07002152 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002154 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
2156
Elliott Hughes814e4032011-08-23 12:07:56 -07002157 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002159 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 }
2161
Elliott Hughes814e4032011-08-23 12:07:56 -07002162 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002164 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Elliott Hughes814e4032011-08-23 12:07:56 -07002167 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002169 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Elliott Hughes814e4032011-08-23 12:07:56 -07002172 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002174 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
2176
Elliott Hughes814e4032011-08-23 12:07:56 -07002177 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002178 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002179 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
Elliott Hughes814e4032011-08-23 12:07:56 -07002182 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002184 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Elliott Hughes814e4032011-08-23 12:07:56 -07002187 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002189 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Elliott Hughes814e4032011-08-23 12:07:56 -07002192 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002194 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Elliott Hughes814e4032011-08-23 12:07:56 -07002197 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002199 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
Elliott Hughes814e4032011-08-23 12:07:56 -07002202 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002204 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 }
2206
Elliott Hughes814e4032011-08-23 12:07:56 -07002207 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002209 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 }
2211
Elliott Hughes814e4032011-08-23 12:07:56 -07002212 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002214 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 }
2216
Elliott Hughes814e4032011-08-23 12:07:56 -07002217 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002219 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 }
2221
Elliott Hughes5174fe62011-08-23 15:12:35 -07002222 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002224 Class* c = Decode<Class*>(ts, java_class);
2225
Elliott Hughes5174fe62011-08-23 15:12:35 -07002226 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 const char* name = methods[i].name;
2228 const char* sig = methods[i].signature;
2229
2230 if (*sig == '!') {
2231 // TODO: fast jni. it's too noisy to log all these.
2232 ++sig;
2233 }
2234
Elliott Hughes5174fe62011-08-23 15:12:35 -07002235 Method* m = c->FindDirectMethod(name, sig);
2236 if (m == NULL) {
2237 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002239 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002240 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002241 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002243 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002244 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002245 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 return JNI_ERR;
2247 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002248
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002249 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002250
Ian Rogers60db5ab2012-02-20 17:02:00 -08002251 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002252 }
2253 return JNI_OK;
2254 }
2255
Elliott Hughes5174fe62011-08-23 15:12:35 -07002256 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002258 Class* c = Decode<Class*>(ts, java_class);
2259
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002260 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002261
2262 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2263 Method* m = c->GetDirectMethod(i);
2264 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002265 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002266 }
2267 }
2268 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2269 Method* m = c->GetVirtualMethod(i);
2270 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002271 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002272 }
2273 }
2274
2275 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002276 }
2277
Elliott Hughes72025e52011-08-23 17:50:30 -07002278 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002279 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002280 Object* o = Decode<Object*>(ts, java_object);
2281 o->MonitorEnter(ts.Self());
2282 if (ts.Self()->IsExceptionPending()) {
2283 return JNI_ERR;
2284 }
2285 ts.Env()->monitors.Add(o);
2286 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002287 }
2288
Elliott Hughes72025e52011-08-23 17:50:30 -07002289 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002291 Object* o = Decode<Object*>(ts, java_object);
2292 o->MonitorExit(ts.Self());
2293 if (ts.Self()->IsExceptionPending()) {
2294 return JNI_ERR;
2295 }
2296 ts.Env()->monitors.Remove(o);
2297 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002298 }
2299
2300 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2301 ScopedJniThreadState ts(env);
2302 Runtime* runtime = Runtime::Current();
2303 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002304 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002305 } else {
2306 *vm = NULL;
2307 }
2308 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2309 }
2310
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2312 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002313
2314 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002315 CHECK(address != NULL); // TODO: ReportJniError
2316 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002317
Elliott Hughesb465ab02011-08-24 11:21:21 -07002318 // At the moment, the Java side is limited to 32 bits.
2319 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2320 CHECK_LE(capacity, 0xffffffff);
2321 jint address_arg = reinterpret_cast<jint>(address);
2322 jint capacity_arg = static_cast<jint>(capacity);
2323
Elliott Hugheseac76672012-05-24 21:56:51 -07002324 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2325 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2326 address_arg, capacity_arg);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002327 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002328 }
2329
Elliott Hughesb465ab02011-08-24 11:21:21 -07002330 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002331 ScopedJniThreadState ts(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002332 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002333 }
2334
Elliott Hughesb465ab02011-08-24 11:21:21 -07002335 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002336 ScopedJniThreadState ts(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002337 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002338 }
2339
Elliott Hughesb465ab02011-08-24 11:21:21 -07002340 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002341 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002342
Elliott Hughes75770752011-08-24 17:52:38 -07002343 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002344
2345 // Do we definitely know what kind of reference this is?
2346 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2347 IndirectRefKind kind = GetIndirectRefKind(ref);
2348 switch (kind) {
2349 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002350 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2351 return JNILocalRefType;
2352 }
2353 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002354 case kGlobal:
2355 return JNIGlobalRefType;
2356 case kWeakGlobal:
2357 return JNIWeakGlobalRefType;
2358 case kSirtOrInvalid:
2359 // Is it in a stack IRT?
TDYa12728f1a142012-03-15 21:51:52 -07002360 if (ts.Self()->StackReferencesContain(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002361 return JNILocalRefType;
2362 }
2363
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002364 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002365 return JNIInvalidRefType;
2366 }
2367
Elliott Hughesb465ab02011-08-24 11:21:21 -07002368 // If we're handing out direct pointers, check whether it's a direct pointer
2369 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002370 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002371 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002372 return JNILocalRefType;
2373 }
2374 }
2375
2376 return JNIInvalidRefType;
2377 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002378 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2379 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002380 }
2381};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002382
Elliott Hughes88c5c352012-03-15 18:49:48 -07002383const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002384 NULL, // reserved0.
2385 NULL, // reserved1.
2386 NULL, // reserved2.
2387 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002388 JNI::GetVersion,
2389 JNI::DefineClass,
2390 JNI::FindClass,
2391 JNI::FromReflectedMethod,
2392 JNI::FromReflectedField,
2393 JNI::ToReflectedMethod,
2394 JNI::GetSuperclass,
2395 JNI::IsAssignableFrom,
2396 JNI::ToReflectedField,
2397 JNI::Throw,
2398 JNI::ThrowNew,
2399 JNI::ExceptionOccurred,
2400 JNI::ExceptionDescribe,
2401 JNI::ExceptionClear,
2402 JNI::FatalError,
2403 JNI::PushLocalFrame,
2404 JNI::PopLocalFrame,
2405 JNI::NewGlobalRef,
2406 JNI::DeleteGlobalRef,
2407 JNI::DeleteLocalRef,
2408 JNI::IsSameObject,
2409 JNI::NewLocalRef,
2410 JNI::EnsureLocalCapacity,
2411 JNI::AllocObject,
2412 JNI::NewObject,
2413 JNI::NewObjectV,
2414 JNI::NewObjectA,
2415 JNI::GetObjectClass,
2416 JNI::IsInstanceOf,
2417 JNI::GetMethodID,
2418 JNI::CallObjectMethod,
2419 JNI::CallObjectMethodV,
2420 JNI::CallObjectMethodA,
2421 JNI::CallBooleanMethod,
2422 JNI::CallBooleanMethodV,
2423 JNI::CallBooleanMethodA,
2424 JNI::CallByteMethod,
2425 JNI::CallByteMethodV,
2426 JNI::CallByteMethodA,
2427 JNI::CallCharMethod,
2428 JNI::CallCharMethodV,
2429 JNI::CallCharMethodA,
2430 JNI::CallShortMethod,
2431 JNI::CallShortMethodV,
2432 JNI::CallShortMethodA,
2433 JNI::CallIntMethod,
2434 JNI::CallIntMethodV,
2435 JNI::CallIntMethodA,
2436 JNI::CallLongMethod,
2437 JNI::CallLongMethodV,
2438 JNI::CallLongMethodA,
2439 JNI::CallFloatMethod,
2440 JNI::CallFloatMethodV,
2441 JNI::CallFloatMethodA,
2442 JNI::CallDoubleMethod,
2443 JNI::CallDoubleMethodV,
2444 JNI::CallDoubleMethodA,
2445 JNI::CallVoidMethod,
2446 JNI::CallVoidMethodV,
2447 JNI::CallVoidMethodA,
2448 JNI::CallNonvirtualObjectMethod,
2449 JNI::CallNonvirtualObjectMethodV,
2450 JNI::CallNonvirtualObjectMethodA,
2451 JNI::CallNonvirtualBooleanMethod,
2452 JNI::CallNonvirtualBooleanMethodV,
2453 JNI::CallNonvirtualBooleanMethodA,
2454 JNI::CallNonvirtualByteMethod,
2455 JNI::CallNonvirtualByteMethodV,
2456 JNI::CallNonvirtualByteMethodA,
2457 JNI::CallNonvirtualCharMethod,
2458 JNI::CallNonvirtualCharMethodV,
2459 JNI::CallNonvirtualCharMethodA,
2460 JNI::CallNonvirtualShortMethod,
2461 JNI::CallNonvirtualShortMethodV,
2462 JNI::CallNonvirtualShortMethodA,
2463 JNI::CallNonvirtualIntMethod,
2464 JNI::CallNonvirtualIntMethodV,
2465 JNI::CallNonvirtualIntMethodA,
2466 JNI::CallNonvirtualLongMethod,
2467 JNI::CallNonvirtualLongMethodV,
2468 JNI::CallNonvirtualLongMethodA,
2469 JNI::CallNonvirtualFloatMethod,
2470 JNI::CallNonvirtualFloatMethodV,
2471 JNI::CallNonvirtualFloatMethodA,
2472 JNI::CallNonvirtualDoubleMethod,
2473 JNI::CallNonvirtualDoubleMethodV,
2474 JNI::CallNonvirtualDoubleMethodA,
2475 JNI::CallNonvirtualVoidMethod,
2476 JNI::CallNonvirtualVoidMethodV,
2477 JNI::CallNonvirtualVoidMethodA,
2478 JNI::GetFieldID,
2479 JNI::GetObjectField,
2480 JNI::GetBooleanField,
2481 JNI::GetByteField,
2482 JNI::GetCharField,
2483 JNI::GetShortField,
2484 JNI::GetIntField,
2485 JNI::GetLongField,
2486 JNI::GetFloatField,
2487 JNI::GetDoubleField,
2488 JNI::SetObjectField,
2489 JNI::SetBooleanField,
2490 JNI::SetByteField,
2491 JNI::SetCharField,
2492 JNI::SetShortField,
2493 JNI::SetIntField,
2494 JNI::SetLongField,
2495 JNI::SetFloatField,
2496 JNI::SetDoubleField,
2497 JNI::GetStaticMethodID,
2498 JNI::CallStaticObjectMethod,
2499 JNI::CallStaticObjectMethodV,
2500 JNI::CallStaticObjectMethodA,
2501 JNI::CallStaticBooleanMethod,
2502 JNI::CallStaticBooleanMethodV,
2503 JNI::CallStaticBooleanMethodA,
2504 JNI::CallStaticByteMethod,
2505 JNI::CallStaticByteMethodV,
2506 JNI::CallStaticByteMethodA,
2507 JNI::CallStaticCharMethod,
2508 JNI::CallStaticCharMethodV,
2509 JNI::CallStaticCharMethodA,
2510 JNI::CallStaticShortMethod,
2511 JNI::CallStaticShortMethodV,
2512 JNI::CallStaticShortMethodA,
2513 JNI::CallStaticIntMethod,
2514 JNI::CallStaticIntMethodV,
2515 JNI::CallStaticIntMethodA,
2516 JNI::CallStaticLongMethod,
2517 JNI::CallStaticLongMethodV,
2518 JNI::CallStaticLongMethodA,
2519 JNI::CallStaticFloatMethod,
2520 JNI::CallStaticFloatMethodV,
2521 JNI::CallStaticFloatMethodA,
2522 JNI::CallStaticDoubleMethod,
2523 JNI::CallStaticDoubleMethodV,
2524 JNI::CallStaticDoubleMethodA,
2525 JNI::CallStaticVoidMethod,
2526 JNI::CallStaticVoidMethodV,
2527 JNI::CallStaticVoidMethodA,
2528 JNI::GetStaticFieldID,
2529 JNI::GetStaticObjectField,
2530 JNI::GetStaticBooleanField,
2531 JNI::GetStaticByteField,
2532 JNI::GetStaticCharField,
2533 JNI::GetStaticShortField,
2534 JNI::GetStaticIntField,
2535 JNI::GetStaticLongField,
2536 JNI::GetStaticFloatField,
2537 JNI::GetStaticDoubleField,
2538 JNI::SetStaticObjectField,
2539 JNI::SetStaticBooleanField,
2540 JNI::SetStaticByteField,
2541 JNI::SetStaticCharField,
2542 JNI::SetStaticShortField,
2543 JNI::SetStaticIntField,
2544 JNI::SetStaticLongField,
2545 JNI::SetStaticFloatField,
2546 JNI::SetStaticDoubleField,
2547 JNI::NewString,
2548 JNI::GetStringLength,
2549 JNI::GetStringChars,
2550 JNI::ReleaseStringChars,
2551 JNI::NewStringUTF,
2552 JNI::GetStringUTFLength,
2553 JNI::GetStringUTFChars,
2554 JNI::ReleaseStringUTFChars,
2555 JNI::GetArrayLength,
2556 JNI::NewObjectArray,
2557 JNI::GetObjectArrayElement,
2558 JNI::SetObjectArrayElement,
2559 JNI::NewBooleanArray,
2560 JNI::NewByteArray,
2561 JNI::NewCharArray,
2562 JNI::NewShortArray,
2563 JNI::NewIntArray,
2564 JNI::NewLongArray,
2565 JNI::NewFloatArray,
2566 JNI::NewDoubleArray,
2567 JNI::GetBooleanArrayElements,
2568 JNI::GetByteArrayElements,
2569 JNI::GetCharArrayElements,
2570 JNI::GetShortArrayElements,
2571 JNI::GetIntArrayElements,
2572 JNI::GetLongArrayElements,
2573 JNI::GetFloatArrayElements,
2574 JNI::GetDoubleArrayElements,
2575 JNI::ReleaseBooleanArrayElements,
2576 JNI::ReleaseByteArrayElements,
2577 JNI::ReleaseCharArrayElements,
2578 JNI::ReleaseShortArrayElements,
2579 JNI::ReleaseIntArrayElements,
2580 JNI::ReleaseLongArrayElements,
2581 JNI::ReleaseFloatArrayElements,
2582 JNI::ReleaseDoubleArrayElements,
2583 JNI::GetBooleanArrayRegion,
2584 JNI::GetByteArrayRegion,
2585 JNI::GetCharArrayRegion,
2586 JNI::GetShortArrayRegion,
2587 JNI::GetIntArrayRegion,
2588 JNI::GetLongArrayRegion,
2589 JNI::GetFloatArrayRegion,
2590 JNI::GetDoubleArrayRegion,
2591 JNI::SetBooleanArrayRegion,
2592 JNI::SetByteArrayRegion,
2593 JNI::SetCharArrayRegion,
2594 JNI::SetShortArrayRegion,
2595 JNI::SetIntArrayRegion,
2596 JNI::SetLongArrayRegion,
2597 JNI::SetFloatArrayRegion,
2598 JNI::SetDoubleArrayRegion,
2599 JNI::RegisterNatives,
2600 JNI::UnregisterNatives,
2601 JNI::MonitorEnter,
2602 JNI::MonitorExit,
2603 JNI::GetJavaVM,
2604 JNI::GetStringRegion,
2605 JNI::GetStringUTFRegion,
2606 JNI::GetPrimitiveArrayCritical,
2607 JNI::ReleasePrimitiveArrayCritical,
2608 JNI::GetStringCritical,
2609 JNI::ReleaseStringCritical,
2610 JNI::NewWeakGlobalRef,
2611 JNI::DeleteWeakGlobalRef,
2612 JNI::ExceptionCheck,
2613 JNI::NewDirectByteBuffer,
2614 JNI::GetDirectBufferAddress,
2615 JNI::GetDirectBufferCapacity,
2616 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002617};
2618
Elliott Hughes75770752011-08-24 17:52:38 -07002619JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002620 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002621 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002622 local_ref_cookie(IRT_FIRST_SEGMENT),
2623 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002624 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002625 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002626 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002627 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002628 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002629 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002630 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002631 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2632 // errors will ensue.
2633 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2634 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002635}
2636
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002637JNIEnvExt::~JNIEnvExt() {
2638}
2639
Elliott Hughes88c5c352012-03-15 18:49:48 -07002640void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2641 check_jni = enabled;
2642 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002643}
2644
Elliott Hughes73e66f72012-05-09 09:34:45 -07002645void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2646 locals.Dump(os);
2647 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002648}
2649
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002650void JNIEnvExt::PushFrame(int /*capacity*/) {
2651 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002652 stacked_local_ref_cookies.push_back(local_ref_cookie);
2653 local_ref_cookie = locals.GetSegmentState();
2654}
2655
2656void JNIEnvExt::PopFrame() {
2657 locals.SetSegmentState(local_ref_cookie);
2658 local_ref_cookie = stacked_local_ref_cookies.back();
2659 stacked_local_ref_cookies.pop_back();
2660}
2661
Carl Shapiroea4dca82011-08-01 13:45:38 -07002662// JNI Invocation interface.
2663
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002664extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2665 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2666 if (args->version < JNI_VERSION_1_2) {
2667 return JNI_EVERSION;
2668 }
2669 Runtime::Options options;
2670 for (int i = 0; i < args->nOptions; ++i) {
2671 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002672 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002673 }
2674 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002675 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002676 if (runtime == NULL) {
2677 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002678 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002679 runtime->Start();
2680 *p_env = Thread::Current()->GetJniEnv();
2681 *p_vm = runtime->GetJavaVM();
2682 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002683}
2684
Elliott Hughesf2682d52011-08-15 16:37:04 -07002685extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002686 Runtime* runtime = Runtime::Current();
2687 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002688 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002689 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002690 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002691 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002692 }
2693 return JNI_OK;
2694}
2695
2696// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002697extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002698 return JNI_ERR;
2699}
2700
Elliott Hughescdf53122011-08-19 15:46:09 -07002701class JII {
2702 public:
2703 static jint DestroyJavaVM(JavaVM* vm) {
2704 if (vm == NULL) {
2705 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002706 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002707 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2708 delete raw_vm->runtime;
2709 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002710 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002711
Elliott Hughescdf53122011-08-19 15:46:09 -07002712 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002713 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002714 }
2715
2716 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002717 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002718 }
2719
2720 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002721 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002722 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002723 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002724 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2725 Runtime* runtime = raw_vm->runtime;
2726 runtime->DetachCurrentThread();
2727 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002728 }
2729
2730 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2731 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2732 return JNI_EVERSION;
2733 }
2734 if (vm == NULL || env == NULL) {
2735 return JNI_ERR;
2736 }
2737 Thread* thread = Thread::Current();
2738 if (thread == NULL) {
2739 *env = NULL;
2740 return JNI_EDETACHED;
2741 }
2742 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002743 return JNI_OK;
2744 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002745};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002746
Elliott Hughes88c5c352012-03-15 18:49:48 -07002747const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002748 NULL, // reserved0
2749 NULL, // reserved1
2750 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002751 JII::DestroyJavaVM,
2752 JII::AttachCurrentThread,
2753 JII::DetachCurrentThread,
2754 JII::GetEnv,
2755 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002756};
2757
Elliott Hughesa0957642011-09-02 14:27:33 -07002758JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002759 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002760 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002761 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002762 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002763 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002764 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002765 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002766 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002767 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002768 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002769 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002770 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002771 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002772 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002773 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002774 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002775 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002776 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002777 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002778}
2779
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002780JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002781 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002782}
2783
Elliott Hughes88c5c352012-03-15 18:49:48 -07002784void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2785 check_jni = enabled;
2786 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002787}
2788
Elliott Hughesae80b492012-04-24 10:43:17 -07002789void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2790 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2791 if (force_copy) {
2792 os << " (with forcecopy)";
2793 }
2794 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
2795 {
2796 MutexLock mu(pins_lock);
2797 os << "; pins=" << pin_table.Size();
2798 }
2799 {
2800 MutexLock mu(globals_lock);
2801 os << "; globals=" << globals.Capacity();
2802 }
2803 {
2804 MutexLock mu(weak_globals_lock);
2805 if (weak_globals.Capacity() > 0) {
2806 os << " (plus " << weak_globals.Capacity() << " weak)";
2807 }
2808 }
2809 os << '\n';
2810
2811 {
2812 MutexLock mu(libraries_lock);
2813 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2814 }
2815}
2816
Elliott Hughes73e66f72012-05-09 09:34:45 -07002817void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002818 {
2819 MutexLock mu(globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002820 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002821 }
2822 {
2823 MutexLock mu(weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002824 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002825 }
2826 {
2827 MutexLock mu(pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002828 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002829 }
2830}
2831
Elliott Hughes75770752011-08-24 17:52:38 -07002832bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2833 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002834
2835 // See if we've already loaded this library. If we have, and the class loader
2836 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002837 // TODO: for better results we should canonicalize the pathname (or even compare
2838 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002839 SharedLibrary* library;
2840 {
2841 // TODO: move the locking (and more of this logic) into Libraries.
2842 MutexLock mu(libraries_lock);
2843 library = libraries->Get(path);
2844 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002845 if (library != NULL) {
2846 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002847 // The library will be associated with class_loader. The JNI
2848 // spec says we can't load the same library into more than one
2849 // class loader.
2850 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2851 "ClassLoader %p; can't open in ClassLoader %p",
2852 path.c_str(), library->GetClassLoader(), class_loader);
2853 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002854 return false;
2855 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002856 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2857 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002858 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002859 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2860 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002861 return false;
2862 }
2863 return true;
2864 }
2865
2866 // Open the shared library. Because we're using a full path, the system
2867 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2868 // resolve this library's dependencies though.)
2869
2870 // Failures here are expected when java.library.path has several entries
2871 // and we have to hunt for the lib.
2872
2873 // The current version of the dynamic linker prints detailed information
2874 // about dlopen() failures. Some things to check if the message is
2875 // cryptic:
2876 // - make sure the library exists on the device
2877 // - verify that the right path is being opened (the debug log message
2878 // above can help with that)
2879 // - check to see if the library is valid (e.g. not zero bytes long)
2880 // - check config/prelink-linux-arm.map to ensure that the library
2881 // is listed and is not being overrun by the previous entry (if
2882 // loading suddenly stops working on a prelinked library, this is
2883 // a good one to check)
2884 // - write a trivial app that calls sleep() then dlopen(), attach
2885 // to it with "strace -p <pid>" while it sleeps, and watch for
2886 // attempts to open nonexistent dependent shared libs
2887
2888 // TODO: automate some of these checks!
2889
2890 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002891 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002892 // the GC to ignore us.
2893 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002894 void* handle = NULL;
2895 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002896 ScopedThreadStateChange tsc(self, kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002897 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002898 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002899
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002900 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002901
2902 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002903 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002904 return false;
2905 }
2906
2907 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002908 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002909 // TODO: move the locking (and more of this logic) into Libraries.
2910 MutexLock mu(libraries_lock);
2911 library = libraries->Get(path);
2912 if (library != NULL) {
2913 LOG(INFO) << "WOW: we lost a race to add shared library: "
2914 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002915 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002916 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002917 library = new SharedLibrary(path, handle, class_loader);
2918 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002919 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002920
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002921 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002922
2923 bool result = true;
2924 void* sym = dlsym(handle, "JNI_OnLoad");
2925 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002926 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002927 } else {
2928 // Call JNI_OnLoad. We have to override the current class
2929 // loader, which will always be "null" since the stuff at the
2930 // top of the stack is around Runtime.loadLibrary(). (See
2931 // the comments in the JNI FindClass function.)
2932 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2933 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002934 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002935 self->SetClassLoaderOverride(class_loader);
2936
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002937 int version = 0;
2938 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002939 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002940 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002941 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002942 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002943
Brian Carlstromaded5f72011-10-07 17:15:04 -07002944 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002945
2946 if (version != JNI_VERSION_1_2 &&
2947 version != JNI_VERSION_1_4 &&
2948 version != JNI_VERSION_1_6) {
2949 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2950 << "bad version: " << version;
2951 // It's unwise to call dlclose() here, but we can mark it
2952 // as bad and ensure that future load attempts will fail.
2953 // We don't know how far JNI_OnLoad got, so there could
2954 // be some partially-initialized stuff accessible through
2955 // newly-registered native method calls. We could try to
2956 // unregister them, but that doesn't seem worthwhile.
2957 result = false;
2958 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002959 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2960 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002961 }
2962 }
2963
2964 library->SetResult(result);
2965 return result;
2966}
2967
2968void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2969 CHECK(m->IsNative());
2970
2971 Class* c = m->GetDeclaringClass();
2972
2973 // If this is a static method, it could be called before the class
2974 // has been initialized.
2975 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002976 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002977 return NULL;
2978 }
2979 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002980 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002981 }
2982
Brian Carlstrom16192862011-09-12 17:50:06 -07002983 std::string detail;
2984 void* native_method;
2985 {
2986 MutexLock mu(libraries_lock);
2987 native_method = libraries->FindNativeMethod(m, detail);
2988 }
2989 // throwing can cause libraries_lock to be reacquired
2990 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002991 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002992 }
2993 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002994}
2995
Elliott Hughes410c0c82011-09-01 17:58:25 -07002996void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2997 {
2998 MutexLock mu(globals_lock);
2999 globals.VisitRoots(visitor, arg);
3000 }
3001 {
3002 MutexLock mu(pins_lock);
3003 pin_table.VisitRoots(visitor, arg);
3004 }
3005 // The weak_globals table is visited by the GC itself (because it mutates the table).
3006}
3007
Ian Rogersdf20fe02011-07-20 20:34:16 -07003008} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003009
3010std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3011 switch (rhs) {
3012 case JNIInvalidRefType:
3013 os << "JNIInvalidRefType";
3014 return os;
3015 case JNILocalRefType:
3016 os << "JNILocalRefType";
3017 return os;
3018 case JNIGlobalRefType:
3019 os << "JNIGlobalRefType";
3020 return os;
3021 case JNIWeakGlobalRefType:
3022 os << "JNIWeakGlobalRefType";
3023 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003024 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003025 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003026 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003027 }
3028}