blob: 4f01f7dacbf67b77dde3a9844a5096d00d4f54c4 [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>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Elliott Hughes40ef99e2011-08-11 17:44:34 -070025#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070027#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070029#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070031#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070032#include "safe_map.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070033#include "scoped_jni_thread_state.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070034#include "ScopedLocalRef.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070035#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070036#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070037#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070038#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070039#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070040
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070041namespace art {
42
Elliott Hughes2ced6a52011-10-16 18:44:48 -070043static const size_t kMonitorsInitial = 32; // Arbitrary.
44static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
45
46static const size_t kLocalsInitial = 64; // Arbitrary.
47static const size_t kLocalsMax = 512; // Arbitrary sanity check.
48
49static const size_t kPinTableInitial = 16; // Arbitrary.
50static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
51
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070052static size_t gGlobalsInitial = 512; // Arbitrary.
53static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070054
55static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
56static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
57
Elliott Hugheseac76672012-05-24 21:56:51 -070058void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods, size_t method_count) {
59 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
60 if (c.get() == NULL) {
61 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
62 }
63 if (env->RegisterNatives(c.get(), methods, method_count) != JNI_OK) {
64 LOG(FATAL) << "Failed to register natives methods: " << jni_class_name;
65 }
66}
67
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070068void SetJniGlobalsMax(size_t max) {
69 if (max != 0) {
70 gGlobalsMax = max;
71 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
72 }
73}
Ian Rogersdf20fe02011-07-20 20:34:16 -070074
Elliott Hughesc5f7c912011-08-18 14:00:42 -070075/*
76 * Add a local reference for an object to the current stack frame. When
77 * the native function returns, the reference will be discarded.
78 *
79 * We need to allow the same reference to be added multiple times.
80 *
81 * This will be called on otherwise unreferenced objects. We cannot do
82 * GC allocations here, and it's best if we don't grab a mutex.
83 *
84 * Returns the local reference (currently just the same pointer that was
85 * passed in), or NULL on failure.
86 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070087template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070088T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
89 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
90 Object* obj = const_cast<Object*>(const_obj);
91
Elliott Hughesc5f7c912011-08-18 14:00:42 -070092 if (obj == NULL) {
93 return NULL;
94 }
95
Elliott Hughes9c750f92012-04-05 12:07:59 -070096 DCHECK((reinterpret_cast<uintptr_t>(obj) & 0xffff0000) != 0xebad0000);
97
Elliott Hughesbf86d042011-08-31 17:53:14 -070098 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
99 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700100
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700101 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700102 IndirectRef ref = locals.Add(cookie, obj);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700103
104#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700105 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700106 size_t entry_count = locals.Capacity();
107 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700108 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes73e66f72012-05-09 09:34:45 -0700109 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")\n"
110 << Dumpable<IndirectReferenceTable>(locals);
111 // TODO: LOG(FATAL) in a later release?
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700112 }
113 }
114#endif
115
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800116 if (env->vm->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700117 // Hand out direct pointers to support broken old apps.
118 return reinterpret_cast<T>(obj);
119 }
120
121 return reinterpret_cast<T>(ref);
122}
Brian Carlstrom51477332012-03-25 20:20:26 -0700123// Explicit instantiations
124template jclass AddLocalReference<jclass>(JNIEnv* public_env, const Object* const_obj);
125template jobject AddLocalReference<jobject>(JNIEnv* public_env, const Object* const_obj);
126template jobjectArray AddLocalReference<jobjectArray>(JNIEnv* public_env, const Object* const_obj);
127template jstring AddLocalReference<jstring>(JNIEnv* public_env, const Object* const_obj);
128template jthrowable AddLocalReference<jthrowable>(JNIEnv* public_env, const Object* const_obj);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700129
Elliott Hughesbf86d042011-08-31 17:53:14 -0700130// For external use.
131template<typename T>
132T Decode(JNIEnv* public_env, jobject obj) {
133 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
134 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
135}
Shih-wei Liao24782c62012-01-08 12:46:11 -0800136// TODO: Change to use template when Mac OS build server no longer uses GCC 4.2.*.
137Object* DecodeObj(JNIEnv* public_env, jobject obj) {
138 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
139 return reinterpret_cast<Object*>(env->self->DecodeJObject(obj));
140}
Elliott Hughesbf86d042011-08-31 17:53:14 -0700141// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700142template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700143template Class* Decode<Class*>(JNIEnv*, jobject);
144template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
145template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700146template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700147template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700148template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700149template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400150template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700151template String* Decode<String*>(JNIEnv*, jobject);
152template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700153
Ian Rogers45619fc2012-02-29 11:15:25 -0800154size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
155 size_t num_bytes = 0;
156 for (size_t i = 1; i < shorty_len; ++i) {
157 char ch = shorty[i];
158 if (ch == 'D' || ch == 'J') {
159 num_bytes += 8;
160 } else if (ch == 'L') {
161 // Argument is a reference or an array. The shorty descriptor
162 // does not distinguish between these types.
163 num_bytes += sizeof(Object*);
164 } else {
165 num_bytes += 4;
166 }
167 }
168 return num_bytes;
169}
170
171class ArgArray {
172 public:
173 explicit ArgArray(Method* method) {
174 MethodHelper mh(method);
175 shorty_ = mh.GetShorty();
176 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -0700177 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800178 arg_array_ = small_arg_array_;
179 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700180 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800181 arg_array_ = large_arg_array_.get();
182 }
183 }
184
Elliott Hughes77405792012-03-15 15:22:12 -0700185 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800186 return arg_array_;
187 }
188
189 void BuildArgArray(JNIEnv* public_env, va_list ap) {
190 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700191 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800192 switch (shorty_[i]) {
193 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700194 arg_array_[offset].SetZ(va_arg(ap, jint));
195 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800196 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700197 arg_array_[offset].SetB(va_arg(ap, jint));
198 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800199 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700200 arg_array_[offset].SetC(va_arg(ap, jint));
201 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800202 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700203 arg_array_[offset].SetS(va_arg(ap, jint));
204 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800205 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700206 arg_array_[offset].SetI(va_arg(ap, jint));
Ian Rogers45619fc2012-02-29 11:15:25 -0800207 break;
208 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700209 arg_array_[offset].SetF(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800210 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700211 case 'L':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700212 arg_array_[offset].SetL(DecodeObj(env, va_arg(ap, jobject)));
Ian Rogers45619fc2012-02-29 11:15:25 -0800213 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800214 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700215 arg_array_[offset].SetD(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800216 break;
217 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700218 arg_array_[offset].SetJ(va_arg(ap, jlong));
Ian Rogers45619fc2012-02-29 11:15:25 -0800219 break;
220 }
221 }
222 }
223
224 void BuildArgArray(JNIEnv* public_env, jvalue* args) {
225 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700226 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800227 switch (shorty_[i]) {
228 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700229 arg_array_[offset].SetZ(args[offset].z);
230 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800231 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700232 arg_array_[offset].SetB(args[offset].b);
233 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800234 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700235 arg_array_[offset].SetC(args[offset].c);
236 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800237 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700238 arg_array_[offset].SetS(args[offset].s);
239 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800240 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700241 arg_array_[offset].SetI(args[offset].i);
Ian Rogers45619fc2012-02-29 11:15:25 -0800242 break;
243 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700244 arg_array_[offset].SetF(args[offset].f);
Ian Rogers45619fc2012-02-29 11:15:25 -0800245 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700246 case 'L':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700247 arg_array_[offset].SetL(DecodeObj(env, args[offset].l));
Ian Rogers45619fc2012-02-29 11:15:25 -0800248 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800249 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700250 arg_array_[offset].SetD(args[offset].d);
Ian Rogers45619fc2012-02-29 11:15:25 -0800251 break;
252 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700253 arg_array_[offset].SetJ(args[offset].j);
Ian Rogers45619fc2012-02-29 11:15:25 -0800254 break;
255 }
256 }
257 }
258
Ian Rogers45619fc2012-02-29 11:15:25 -0800259 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700260 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800261 const char* shorty_;
262 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700263 JValue* arg_array_;
264 JValue small_arg_array_[kSmallArgArraySize];
265 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800266};
267
Elliott Hughes0512f022012-03-15 22:10:52 -0700268static jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700269 if (obj == NULL) {
270 return NULL;
271 }
Elliott Hughes75770752011-08-24 17:52:38 -0700272 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700273 IndirectReferenceTable& weak_globals = vm->weak_globals;
274 MutexLock mu(vm->weak_globals_lock);
275 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
276 return reinterpret_cast<jweak>(ref);
277}
278
Elliott Hughesbf86d042011-08-31 17:53:14 -0700279// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700280template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -0700281static T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700282 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700283}
284
Elliott Hughesb264f082012-04-06 17:10:10 -0700285static void CheckMethodArguments(Method* m, JValue* args) {
286 MethodHelper mh(m);
287 ObjectArray<Class>* parameter_types = mh.GetParameterTypes();
288 CHECK(parameter_types != NULL);
289 size_t error_count = 0;
290 for (int i = 0; i < parameter_types->GetLength(); ++i) {
291 Class* parameter_type = parameter_types->Get(i);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700292 // TODO: check primitives are in range.
Elliott Hughesb264f082012-04-06 17:10:10 -0700293 if (!parameter_type->IsPrimitive()) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700294 Object* argument = args[i].GetL();
Elliott Hughesb264f082012-04-06 17:10:10 -0700295 if (argument != NULL && !argument->InstanceOf(parameter_type)) {
296 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
297 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
298 ++error_count;
299 }
300 }
301 }
302 if (error_count > 0) {
303 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
304 // with an argument.
305 JniAbort(NULL);
306 }
307}
Elliott Hughesb264f082012-04-06 17:10:10 -0700308
Elliott Hughes77405792012-03-15 15:22:12 -0700309static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, JValue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700310 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700311 if (UNLIKELY(env->check_jni)) {
312 CheckMethodArguments(method, args);
313 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700314 JValue result;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700315 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700316 return result;
317}
318
Ian Rogers0571d352011-11-03 19:51:38 -0700319static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700320 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800321 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700322 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800323 ArgArray arg_array(method);
324 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700325 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700326}
327
Ian Rogers0571d352011-11-03 19:51:38 -0700328static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700329 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700330}
331
Ian Rogers0571d352011-11-03 19:51:38 -0700332static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
333 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700334 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800335 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700336 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800337 ArgArray arg_array(method);
338 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700339 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700340}
341
Ian Rogers0571d352011-11-03 19:51:38 -0700342static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
343 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700344 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800345 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700346 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800347 ArgArray arg_array(method);
348 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700349 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700350}
351
Elliott Hughes6b436852011-08-12 10:16:44 -0700352// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
353// separated with slashes but aren't wrapped with "L;" like regular descriptors
354// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
355// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
356// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700357static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700358 std::string result;
359 // Add the missing "L;" if necessary.
360 if (name[0] == '[') {
361 result = name;
362 } else {
363 result += 'L';
364 result += name;
365 result += ';';
366 }
367 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700368 if (result.find('.') != std::string::npos) {
369 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
370 << "\"" << name << "\"";
371 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700372 }
373 return result;
374}
375
Ian Rogers0571d352011-11-03 19:51:38 -0700376static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700377 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800378 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700379}
380
Ian Rogers0571d352011-11-03 19:51:38 -0700381static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700382 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700383 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700384 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700385 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700386
387 Method* method = NULL;
388 if (is_static) {
389 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700390 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700391 method = c->FindVirtualMethod(name, sig);
392 if (method == NULL) {
393 // No virtual method matching the signature. Search declared
394 // private methods and constructors.
395 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700396 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700397 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700398
Elliott Hughescdf53122011-08-19 15:46:09 -0700399 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700400 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700401 return NULL;
402 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700403
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700404 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700405}
406
Ian Rogers0571d352011-11-03 19:51:38 -0700407static const ClassLoader* GetClassLoader(Thread* self) {
Elliott Hughes6a144332012-04-03 13:07:11 -0700408 Method* method = self->GetCurrentMethod();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700409 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
410 return self->GetClassLoaderOverride();
411 }
412 return method->GetDeclaringClass()->GetClassLoader();
413}
414
Ian Rogers0571d352011-11-03 19:51:38 -0700415static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700416 Class* c = Decode<Class*>(ts, jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700417 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700418 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700419 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700420
421 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 Class* field_type;
423 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
424 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700425 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700426 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700427 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700428 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700429 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700430 if (field_type == NULL) {
431 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700432 DCHECK(ts.Self()->IsExceptionPending());
433 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700434 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700435 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800436 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700437 return NULL;
438 }
439 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800440 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700441 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800442 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700443 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700444 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700445 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700446 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800447 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700448 return NULL;
449 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700450 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700451}
452
Ian Rogers0571d352011-11-03 19:51:38 -0700453static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700454 JavaVMExt* vm = ts.Vm();
455 MutexLock mu(vm->pins_lock);
456 vm->pin_table.Add(array);
457}
458
Ian Rogers0571d352011-11-03 19:51:38 -0700459static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700460 JavaVMExt* vm = ts.Vm();
461 MutexLock mu(vm->pins_lock);
462 vm->pin_table.Remove(array);
463}
464
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700465template<typename JniT, typename ArtT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700466static JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700467 CHECK_GE(length, 0); // TODO: ReportJniError
468 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700469 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700470}
471
Elliott Hughes75770752011-08-24 17:52:38 -0700472template <typename ArrayT, typename CArrayT, typename ArtArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700473static CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -0700474 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
475 PinPrimitiveArray(ts, array);
476 if (is_copy != NULL) {
477 *is_copy = JNI_FALSE;
478 }
479 return array->GetData();
480}
481
482template <typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700483static void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
Elliott Hughes75770752011-08-24 17:52:38 -0700484 if (mode != JNI_COMMIT) {
485 Array* array = Decode<Array*>(ts, java_array);
486 UnpinPrimitiveArray(ts, array);
487 }
488}
489
Ian Rogers0571d352011-11-03 19:51:38 -0700490static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700491 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700492 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700493 "%s offset=%d length=%d %s.length=%d",
494 type.c_str(), start, length, identifier, array->GetLength());
495}
Ian Rogers0571d352011-11-03 19:51:38 -0700496
497static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700498 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700499 "offset=%d length=%d string.length()=%d", start, length, array_length);
500}
Elliott Hughes814e4032011-08-23 12:07:56 -0700501
502template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700503static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700504 ArrayT* array = Decode<ArrayT*>(ts, java_array);
505 if (start < 0 || length < 0 || start + length > array->GetLength()) {
506 ThrowAIOOBE(ts, array, start, length, "src");
507 } else {
508 JavaT* data = array->GetData();
509 memcpy(buf, data + start, length * sizeof(JavaT));
510 }
511}
512
513template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700514static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700515 ArrayT* array = Decode<ArrayT*>(ts, java_array);
516 if (start < 0 || length < 0 || start + length > array->GetLength()) {
517 ThrowAIOOBE(ts, array, start, length, "dst");
518 } else {
519 JavaT* data = array->GetData();
520 memcpy(data + start, buf, length * sizeof(JavaT));
521 }
522}
523
Elliott Hughesa4f94742012-05-29 16:28:38 -0700524int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause) {
525 ScopedJniThreadState ts(env);
526
527 // Turn the const char* into a java.lang.String.
528 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
529 if (msg != NULL && s.get() == NULL) {
530 return JNI_ERR;
531 }
532
533 // Choose an appropriate constructor and set up the arguments.
534 jvalue args[2];
535 const char* signature;
536 if (msg == NULL && cause == NULL) {
537 signature = "()V";
538 } else if (msg != NULL && cause == NULL) {
539 signature = "(Ljava/lang/String;)V";
540 args[0].l = s.get();
541 } else if (msg == NULL && cause != NULL) {
542 signature = "(Ljava/lang/Throwable;)V";
543 args[0].l = cause;
544 } else {
545 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
546 args[0].l = s.get();
547 args[1].l = cause;
548 }
549 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
550 if (mid == NULL) {
551 LOG(ERROR) << "No <init>" << signature << " in " << PrettyClass(Decode<Class*>(env, exception_class));
552 return JNI_ERR;
553 }
554
555 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
556 if (exception.get() == NULL) {
557 return JNI_ERR;
558 }
559
560 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
561
562 return JNI_OK;
563}
564
Elliott Hughes462c9442012-03-23 18:47:50 -0700565static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700566 if (vm == NULL || p_env == NULL) {
567 return JNI_ERR;
568 }
569
Elliott Hughes462c9442012-03-23 18:47:50 -0700570 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700571 Thread* self = Thread::Current();
572 if (self != NULL) {
573 *p_env = self->GetJniEnv();
574 return JNI_OK;
575 }
576
577 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
578
579 // No threads allowed in zygote mode.
580 if (runtime->IsZygote()) {
581 LOG(ERROR) << "Attempt to attach a thread in the zygote";
582 return JNI_ERR;
583 }
584
Elliott Hughes462c9442012-03-23 18:47:50 -0700585 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
586 const char* thread_name = NULL;
587 Object* thread_group = NULL;
588 if (args != NULL) {
589 CHECK_GE(args->version, JNI_VERSION_1_2);
590 thread_name = args->name;
591 thread_group = static_cast<Thread*>(NULL)->DecodeJObject(args->group);
Elliott Hughes75770752011-08-24 17:52:38 -0700592 }
Elliott Hughes75770752011-08-24 17:52:38 -0700593
Elliott Hughes462c9442012-03-23 18:47:50 -0700594 runtime->AttachCurrentThread(thread_name, as_daemon, thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700595 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700596 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700597}
598
Elliott Hughes79082e32011-08-25 12:07:32 -0700599class SharedLibrary {
600 public:
601 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
602 : path_(path),
603 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700604 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700605 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughese62934d2012-04-09 11:24:29 -0700606 jni_on_load_cond_("JNI_OnLoad condition variable"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700607 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700608 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700609 }
610
Elliott Hughes79082e32011-08-25 12:07:32 -0700611 Object* GetClassLoader() {
612 return class_loader_;
613 }
614
615 std::string GetPath() {
616 return path_;
617 }
618
619 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700620 * Check the result of an earlier call to JNI_OnLoad on this library.
621 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700622 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700623 bool CheckOnLoadResult() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700624 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700625 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700626 // Check this so we don't end up waiting for ourselves. We need
627 // to return "true" so the caller can continue.
628 LOG(INFO) << *self << " recursive attempt to load library "
629 << "\"" << path_ << "\"";
630 return true;
631 }
632
633 MutexLock mu(jni_on_load_lock_);
634 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800635 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
636 << "JNI_OnLoad...]";
Elliott Hughes34e06962012-04-09 13:55:55 -0700637 ScopedThreadStateChange tsc(self, kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700638 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700639 }
640
641 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800642 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
643 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700644 return okay;
645 }
646
647 void SetResult(bool result) {
648 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700649 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700650
651 // Broadcast a wakeup to anybody sleeping on the condition variable.
652 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700653 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700654 }
655
656 void* FindSymbol(const std::string& symbol_name) {
657 return dlsym(handle_, symbol_name.c_str());
658 }
659
660 private:
661 enum JNI_OnLoadState {
662 kPending,
663 kFailed,
664 kOkay,
665 };
666
667 // Path to library "/system/lib/libjni.so".
668 std::string path_;
669
670 // The void* returned by dlopen(3).
671 void* handle_;
672
673 // The ClassLoader this library is associated with.
674 Object* class_loader_;
675
676 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700677 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700678 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700679 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700680 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700681 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700682 // Result of earlier JNI_OnLoad call.
683 JNI_OnLoadState jni_on_load_result_;
684};
685
Elliott Hughes79082e32011-08-25 12:07:32 -0700686// This exists mainly to keep implementation details out of the header file.
687class Libraries {
688 public:
689 Libraries() {
690 }
691
692 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700693 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700694 }
695
Elliott Hughesae80b492012-04-24 10:43:17 -0700696 void Dump(std::ostream& os) const {
697 bool first = true;
698 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
699 if (!first) {
700 os << ' ';
701 }
702 first = false;
703 os << it->first;
704 }
705 }
706
707 size_t size() const {
708 return libraries_.size();
709 }
710
Elliott Hughes79082e32011-08-25 12:07:32 -0700711 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700712 It it = libraries_.find(path);
713 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700714 }
715
716 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700717 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700718 }
719
720 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700721 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700722 std::string jni_short_name(JniShortName(m));
723 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700724 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700725 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
726 SharedLibrary* library = it->second;
727 if (library->GetClassLoader() != declaring_class_loader) {
728 // We only search libraries loaded by the appropriate ClassLoader.
729 continue;
730 }
731 // Try the short name then the long name...
732 void* fn = library->FindSymbol(jni_short_name);
733 if (fn == NULL) {
734 fn = library->FindSymbol(jni_long_name);
735 }
736 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800737 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
738 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700739 return fn;
740 }
741 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700742 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700743 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700744 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700745 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700746 return NULL;
747 }
748
749 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700750 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700751
Elliott Hughesa0e18062012-04-13 15:59:59 -0700752 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700753};
754
Elliott Hughes418d20f2011-09-22 14:00:39 -0700755JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
756 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
757 Object* receiver = Decode<Object*>(env, obj);
758 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800759 ArgArray arg_array(method);
760 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700761 return InvokeWithArgArray(env, receiver, method, arg_array.get());
762}
763
Elliott Hughesd07986f2011-12-06 18:27:45 -0800764JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700765 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800766}
767
Elliott Hughescdf53122011-08-19 15:46:09 -0700768class JNI {
769 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700770
Elliott Hughescdf53122011-08-19 15:46:09 -0700771 static jint GetVersion(JNIEnv* env) {
772 ScopedJniThreadState ts(env);
773 return JNI_VERSION_1_6;
774 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700775
Elliott Hughescdf53122011-08-19 15:46:09 -0700776 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
777 ScopedJniThreadState ts(env);
778 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700779 return NULL;
780 }
781
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 static jclass FindClass(JNIEnv* env, const char* name) {
783 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700784 Runtime* runtime = Runtime::Current();
785 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700786 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700787 Class* c = NULL;
788 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700789 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800790 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700791 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800792 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700793 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700794 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700795 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700796
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
798 ScopedJniThreadState ts(env);
799 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700800 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700801 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700802
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
804 ScopedJniThreadState ts(env);
805 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700806 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700808
Elliott Hughescdf53122011-08-19 15:46:09 -0700809 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
810 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700811 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700812 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700814
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
816 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700817 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700818 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700820
Elliott Hughes37f7a402011-08-22 18:56:01 -0700821 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
822 ScopedJniThreadState ts(env);
823 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700824 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700825 }
826
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700827 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700829 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700830 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700832
Elliott Hughes37f7a402011-08-22 18:56:01 -0700833 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700835 Class* c1 = Decode<Class*>(ts, java_class1);
836 Class* c2 = Decode<Class*>(ts, java_class2);
837 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700838 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700839
Elliott Hughese84278b2012-03-22 10:06:53 -0700840 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700842 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700843 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700844 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700845 return JNI_TRUE;
846 } else {
847 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700848 Class* c = Decode<Class*>(ts, java_class);
849 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700850 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700851 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700852
Elliott Hughes37f7a402011-08-22 18:56:01 -0700853 static jint Throw(JNIEnv* env, jthrowable java_exception) {
854 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700855 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700856 if (exception == NULL) {
857 return JNI_ERR;
858 }
859 ts.Self()->SetException(exception);
860 return JNI_OK;
861 }
862
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700863 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700864 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700865 }
866
867 static jboolean ExceptionCheck(JNIEnv* env) {
868 ScopedJniThreadState ts(env);
869 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
870 }
871
872 static void ExceptionClear(JNIEnv* env) {
873 ScopedJniThreadState ts(env);
874 ts.Self()->ClearException();
875 }
876
877 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700878 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700879
880 Thread* self = ts.Self();
881 Throwable* original_exception = self->GetException();
882 self->ClearException();
883
Elliott Hughesbf86d042011-08-31 17:53:14 -0700884 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700885 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
886 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
887 if (mid == NULL) {
888 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700889 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700890 } else {
891 env->CallVoidMethod(exception.get(), mid);
892 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700893 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700894 << " thrown while calling printStackTrace";
895 self->ClearException();
896 }
897 }
898
899 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700901
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 static jthrowable ExceptionOccurred(JNIEnv* env) {
903 ScopedJniThreadState ts(env);
904 Object* exception = ts.Self()->GetException();
Elliott Hughes81ff3182012-03-23 20:35:56 -0700905 return (exception != NULL) ? AddLocalReference<jthrowable>(env, exception) : NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 }
907
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 static void FatalError(JNIEnv* env, const char* msg) {
909 ScopedJniThreadState ts(env);
910 LOG(FATAL) << "JNI FatalError called: " << msg;
911 }
912
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700913 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700915 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
916 return JNI_ERR;
917 }
918 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 return JNI_OK;
920 }
921
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700922 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700923 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700924 Object* survivor = Decode<Object*>(ts, java_survivor);
925 ts.Env()->PopFrame();
926 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 }
928
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700929 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700931 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
932 }
933
934 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
935 // TODO: we should try to expand the table if necessary.
936 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
937 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
938 return JNI_ERR;
939 }
940 // TODO: this isn't quite right, since "capacity" includes holes.
941 size_t capacity = ts.Env()->locals.Capacity();
942 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
943 if (!okay) {
944 ts.Self()->ThrowOutOfMemoryError(caller);
945 }
946 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 }
948
Elliott Hughescdf53122011-08-19 15:46:09 -0700949 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
950 ScopedJniThreadState ts(env);
951 if (obj == NULL) {
952 return NULL;
953 }
954
Elliott Hughes75770752011-08-24 17:52:38 -0700955 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700956 IndirectReferenceTable& globals = vm->globals;
957 MutexLock mu(vm->globals_lock);
958 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
959 return reinterpret_cast<jobject>(ref);
960 }
961
962 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
963 ScopedJniThreadState ts(env);
964 if (obj == NULL) {
965 return;
966 }
967
Elliott Hughes75770752011-08-24 17:52:38 -0700968 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 IndirectReferenceTable& globals = vm->globals;
970 MutexLock mu(vm->globals_lock);
971
972 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
973 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
974 << "failed to find entry";
975 }
976 }
977
978 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
979 ScopedJniThreadState ts(env);
980 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
981 }
982
983 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
984 ScopedJniThreadState ts(env);
985 if (obj == NULL) {
986 return;
987 }
988
Elliott Hughes75770752011-08-24 17:52:38 -0700989 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 IndirectReferenceTable& weak_globals = vm->weak_globals;
991 MutexLock mu(vm->weak_globals_lock);
992
993 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
994 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
995 << "failed to find entry";
996 }
997 }
998
999 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
1000 ScopedJniThreadState ts(env);
1001 if (obj == NULL) {
1002 return NULL;
1003 }
1004
1005 IndirectReferenceTable& locals = ts.Env()->locals;
1006
Ian Rogers5a7a74a2011-09-26 16:32:29 -07001007 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
1009 return reinterpret_cast<jobject>(ref);
1010 }
1011
1012 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
1013 ScopedJniThreadState ts(env);
1014 if (obj == NULL) {
1015 return;
1016 }
1017
1018 IndirectReferenceTable& locals = ts.Env()->locals;
1019
Ian Rogers5a7a74a2011-09-26 16:32:29 -07001020 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 if (!locals.Remove(cookie, obj)) {
1022 // Attempting to delete a local reference that is not in the
1023 // topmost local reference frame is a no-op. DeleteLocalRef returns
1024 // void and doesn't throw any exceptions, but we should probably
1025 // complain about it so the user will notice that things aren't
1026 // going quite the way they expect.
1027 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
1028 << "failed to find entry";
1029 }
1030 }
1031
1032 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
1033 ScopedJniThreadState ts(env);
1034 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
1035 ? JNI_TRUE : JNI_FALSE;
1036 }
1037
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001038 static jobject AllocObject(JNIEnv* env, jclass java_class) {
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 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001044 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughese84278b2012-03-22 10:06:53 -07001047 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 ScopedJniThreadState ts(env);
1049 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001051 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 va_end(args);
1053 return result;
1054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001058 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001059 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001060 return NULL;
1061 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001062 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001063 if (result == NULL) {
1064 return NULL;
1065 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001066 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001068 if (!ts.Self()->IsExceptionPending()) {
1069 return local_result;
1070 } else {
1071 return NULL;
1072 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001077 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001078 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001079 return NULL;
1080 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001081 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001082 if (result == NULL) {
1083 return NULL;
1084 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001085 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001087 if (!ts.Self()->IsExceptionPending()) {
1088 return local_result;
1089 } else {
1090 return NULL;
1091 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1095 ScopedJniThreadState ts(env);
1096 return FindMethodID(ts, c, name, sig, false);
1097 }
1098
1099 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1100 ScopedJniThreadState ts(env);
1101 return FindMethodID(ts, c, name, sig, true);
1102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 va_list ap;
1107 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001108 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001110 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001115 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001116 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 }
1118
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001121 JValue result(InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001122 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 va_list ap;
1128 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001129 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001131 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001136 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001141 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 va_list ap;
1147 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001148 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001150 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001155 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001160 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 va_list ap;
1166 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001167 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001169 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 }
1171
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001174 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001179 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 va_list ap;
1185 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001186 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001188 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 }
1190
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001193 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001198 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 va_list ap;
1204 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001205 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001207 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001212 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001217 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
Elliott Hughes72025e52011-08-23 17:50:30 -07001220 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 va_list ap;
1223 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001224 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001226 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001231 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001236 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
Elliott Hughes72025e52011-08-23 17:50:30 -07001239 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 va_list ap;
1242 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001243 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001244 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001245 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
Elliott Hughes72025e52011-08-23 17:50:30 -07001248 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001250 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 }
1252
Elliott Hughes72025e52011-08-23 17:50:30 -07001253 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001255 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
Elliott Hughes72025e52011-08-23 17:50:30 -07001258 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 va_list ap;
1261 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001262 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001263 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001264 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
Elliott Hughes72025e52011-08-23 17:50:30 -07001267 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001269 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001274 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
Elliott Hughes72025e52011-08-23 17:50:30 -07001277 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 va_list ap;
1280 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001281 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001282 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 }
1284
Elliott Hughes72025e52011-08-23 17:50:30 -07001285 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001287 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Elliott Hughes72025e52011-08-23 17:50:30 -07001290 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001292 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001295 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 ScopedJniThreadState ts(env);
1297 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001298 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001299 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001300 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 va_end(ap);
1302 return local_result;
1303 }
1304
1305 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001306 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001308 JValue result(InvokeWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001309 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
1312 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001313 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001315 JValue result(InvokeWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001316 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 }
1318
1319 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001320 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 ScopedJniThreadState ts(env);
1322 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001324 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001326 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
1329 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001332 return InvokeWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
1335 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001336 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001338 return InvokeWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001341 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 ScopedJniThreadState ts(env);
1343 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001344 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001345 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001347 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
1350 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001351 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001353 return InvokeWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
1356 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001357 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001359 return InvokeWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 }
1361
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001362 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 ScopedJniThreadState ts(env);
1364 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001365 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001366 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001368 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
1371 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001372 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001374 return InvokeWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
1377 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001378 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001380 return InvokeWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
1382
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001383 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 ScopedJniThreadState ts(env);
1385 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001386 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001387 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001389 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
1392 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001393 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001395 return InvokeWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
1398 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001399 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001401 return InvokeWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 }
1403
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001404 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 ScopedJniThreadState ts(env);
1406 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001407 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001408 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001410 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 }
1412
1413 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001414 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001416 return InvokeWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 }
1418
1419 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001420 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001422 return InvokeWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001425 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 ScopedJniThreadState ts(env);
1427 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001428 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001429 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001431 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
1434 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001435 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001437 return InvokeWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
1440 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001441 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001443 return InvokeWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001446 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 ScopedJniThreadState ts(env);
1448 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001449 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001450 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001452 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
1454
1455 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001456 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001458 return InvokeWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
1460
1461 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001462 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001464 return InvokeWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001467 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 ScopedJniThreadState ts(env);
1469 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001470 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001471 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001473 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
1476 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001477 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001479 return InvokeWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 }
1481
1482 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001483 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001485 return InvokeWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 }
1487
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001488 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 ScopedJniThreadState ts(env);
1490 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001491 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001492 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 va_end(ap);
1494 }
1495
1496 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001497 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001499 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
1502 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001503 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001505 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001508 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 ScopedJniThreadState ts(env);
1510 return FindFieldID(ts, c, name, sig, false);
1511 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001512
1513
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001514 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 ScopedJniThreadState ts(env);
1516 return FindFieldID(ts, c, name, sig, true);
1517 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001518
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001521 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001522 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001523 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001525
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001526 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001528 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001529 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 }
1531
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001532 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001534 Object* o = Decode<Object*>(ts, java_object);
1535 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001536 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001537 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001542 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001543 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001547#define GET_PRIMITIVE_FIELD(fn, instance) \
1548 ScopedJniThreadState ts(env); \
1549 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001550 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001551 return f->fn(o)
1552
1553#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1554 ScopedJniThreadState ts(env); \
1555 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001556 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001557 f->fn(o, value)
1558
1559 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1560 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001563 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1564 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001565 }
1566
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001567 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1568 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 }
1570
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001571 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1572 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001575 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1576 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001579 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1580 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 }
1582
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001583 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1584 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001587 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1588 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001591 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 }
1594
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001595 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001596 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001599 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001600 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001603 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001604 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001607 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001608 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 }
1610
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001611 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001612 GET_PRIMITIVE_FIELD(GetLong, NULL);
1613 }
1614
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001615 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001616 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1617 }
1618
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001619 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001620 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1621 }
1622
1623 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1624 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1625 }
1626
1627 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1628 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1629 }
1630
1631 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1632 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1633 }
1634
1635 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1636 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1637 }
1638
1639 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1640 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1641 }
1642
1643 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1644 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1645 }
1646
1647 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1648 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1649 }
1650
1651 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1652 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1653 }
1654
1655 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1656 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1657 }
1658
1659 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1660 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1661 }
1662
1663 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1664 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1665 }
1666
1667 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1668 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1669 }
1670
1671 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1672 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1673 }
1674
1675 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1676 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1677 }
1678
1679 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1680 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1681 }
1682
1683 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1684 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 }
1686
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001687 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 ScopedJniThreadState ts(env);
1689 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001690 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001691 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001692 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 va_end(ap);
1694 return local_result;
1695 }
1696
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001697 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001699 JValue result(InvokeWithVarArgs(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001700 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 }
1702
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001703 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001705 JValue result(InvokeWithJValues(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001706 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 }
1708
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001709 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 ScopedJniThreadState ts(env);
1711 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001712 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001713 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001715 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 }
1717
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001718 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001720 return InvokeWithVarArgs(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 }
1722
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001723 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001725 return InvokeWithJValues(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 }
1727
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001728 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 ScopedJniThreadState ts(env);
1730 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001731 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001732 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001734 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 }
1736
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001737 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001739 return InvokeWithVarArgs(env, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 }
1741
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001742 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001744 return InvokeWithJValues(env, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 }
1746
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001747 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 ScopedJniThreadState ts(env);
1749 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001750 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001751 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001753 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 }
1755
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001756 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001758 return InvokeWithVarArgs(env, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001761 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001763 return InvokeWithJValues(env, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 }
1765
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001766 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 ScopedJniThreadState ts(env);
1768 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001769 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001770 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001772 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 }
1774
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001775 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001777 return InvokeWithVarArgs(env, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 }
1779
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001780 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001782 return InvokeWithJValues(env, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 }
1784
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001785 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 ScopedJniThreadState ts(env);
1787 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001788 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001789 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001791 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 }
1793
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001794 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001796 return InvokeWithVarArgs(env, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 }
1798
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001799 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001801 return InvokeWithJValues(env, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 }
1803
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001804 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 ScopedJniThreadState ts(env);
1806 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001807 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001808 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001810 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 }
1812
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001813 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001815 return InvokeWithVarArgs(env, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001818 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001820 return InvokeWithJValues(env, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001823 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 ScopedJniThreadState ts(env);
1825 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001826 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001827 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001828 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001829 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 }
1831
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001832 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001834 return InvokeWithVarArgs(env, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001837 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001839 return InvokeWithJValues(env, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 }
1841
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001842 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 ScopedJniThreadState ts(env);
1844 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001845 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001846 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001848 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 }
1850
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001851 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001853 return InvokeWithVarArgs(env, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 }
1855
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001856 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001858 return InvokeWithJValues(env, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 }
1860
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001861 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 ScopedJniThreadState ts(env);
1863 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001864 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001865 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 va_end(ap);
1867 }
1868
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001869 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001871 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 }
1873
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001874 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001876 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 }
1878
Elliott Hughes814e4032011-08-23 12:07:56 -07001879 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001881 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001882 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
1884
1885 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1886 ScopedJniThreadState ts(env);
1887 if (utf == NULL) {
1888 return NULL;
1889 }
1890 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001891 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 }
1893
Elliott Hughes814e4032011-08-23 12:07:56 -07001894 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1895 ScopedJniThreadState ts(env);
1896 return Decode<String*>(ts, java_string)->GetLength();
1897 }
1898
1899 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1900 ScopedJniThreadState ts(env);
1901 return Decode<String*>(ts, java_string)->GetUtfLength();
1902 }
1903
Elliott Hughesb465ab02011-08-24 11:21:21 -07001904 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001906 String* s = Decode<String*>(ts, java_string);
1907 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1908 ThrowSIOOBE(ts, start, length, s->GetLength());
1909 } else {
1910 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1911 memcpy(buf, chars + start, length * sizeof(jchar));
1912 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001913 }
1914
Elliott Hughesb465ab02011-08-24 11:21:21 -07001915 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001916 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001917 String* s = Decode<String*>(ts, java_string);
1918 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1919 ThrowSIOOBE(ts, start, length, s->GetLength());
1920 } else {
1921 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1922 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1923 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001924 }
1925
Elliott Hughes75770752011-08-24 17:52:38 -07001926 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001927 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001928 String* s = Decode<String*>(ts, java_string);
1929 const CharArray* chars = s->GetCharArray();
1930 PinPrimitiveArray(ts, chars);
1931 if (is_copy != NULL) {
1932 *is_copy = JNI_FALSE;
1933 }
1934 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001935 }
1936
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001937 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001938 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001939 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
Elliott Hughes75770752011-08-24 17:52:38 -07001942 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001944 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes75770752011-08-24 17:52:38 -07001947 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001949 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 }
1951
Elliott Hughes75770752011-08-24 17:52:38 -07001952 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001953 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001954 if (java_string == NULL) {
1955 return NULL;
1956 }
1957 if (is_copy != NULL) {
1958 *is_copy = JNI_TRUE;
1959 }
1960 String* s = Decode<String*>(ts, java_string);
1961 size_t byte_count = s->GetUtfLength();
1962 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001963 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001964 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1965 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1966 bytes[byte_count] = '\0';
1967 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001968 }
1969
Elliott Hughes75770752011-08-24 17:52:38 -07001970 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001971 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001972 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001973 }
1974
Elliott Hughesbd935992011-08-22 11:59:34 -07001975 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001977 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001978 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001979 Array* array = obj->AsArray();
1980 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001981 }
1982
Elliott Hughes814e4032011-08-23 12:07:56 -07001983 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001985 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001986 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
1989 static void SetObjectArrayElement(JNIEnv* env,
1990 jobjectArray java_array, jsize index, jobject java_value) {
1991 ScopedJniThreadState ts(env);
1992 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1993 Object* value = Decode<Object*>(ts, java_value);
1994 array->Set(index, value);
1995 }
1996
1997 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1998 ScopedJniThreadState ts(env);
1999 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
2000 }
2001
2002 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
2003 ScopedJniThreadState ts(env);
2004 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
2005 }
2006
2007 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
2008 ScopedJniThreadState ts(env);
2009 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
2010 }
2011
2012 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
2013 ScopedJniThreadState ts(env);
2014 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
2015 }
2016
2017 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
2018 ScopedJniThreadState ts(env);
2019 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
2020 }
2021
2022 static jintArray NewIntArray(JNIEnv* env, jsize length) {
2023 ScopedJniThreadState ts(env);
2024 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
2025 }
2026
2027 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2028 ScopedJniThreadState ts(env);
2029 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
2030 }
2031
2032 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
2033 ScopedJniThreadState ts(env);
2034 CHECK_GE(length, 0); // TODO: ReportJniError
2035
2036 // Compute the array class corresponding to the given element class.
2037 Class* element_class = Decode<Class*>(ts, element_jclass);
2038 std::string descriptor;
2039 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002040 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002041
2042 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002043 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2044 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 return NULL;
2046 }
2047
Elliott Hughes75770752011-08-24 17:52:38 -07002048 // Allocate and initialize if necessary.
2049 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002051 if (initial_element != NULL) {
2052 Object* initial_object = Decode<Object*>(ts, initial_element);
2053 for (jsize i = 0; i < length; ++i) {
2054 result->Set(i, initial_object);
2055 }
2056 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002057 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
2060 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2061 ScopedJniThreadState ts(env);
2062 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2063 }
2064
Ian Rogersa15e67d2012-02-28 13:51:55 -08002065 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002066 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002067 Array* array = Decode<Array*>(ts, java_array);
2068 PinPrimitiveArray(ts, array);
2069 if (is_copy != NULL) {
2070 *is_copy = JNI_FALSE;
2071 }
2072 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002073 }
2074
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002075 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002076 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002077 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002078 }
2079
Elliott Hughes75770752011-08-24 17:52:38 -07002080 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002082 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Elliott Hughes75770752011-08-24 17:52:38 -07002085 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002087 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
2089
Elliott Hughes75770752011-08-24 17:52:38 -07002090 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002092 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
Elliott Hughes75770752011-08-24 17:52:38 -07002095 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002097 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
Elliott Hughes75770752011-08-24 17:52:38 -07002100 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002102 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
Elliott Hughes75770752011-08-24 17:52:38 -07002105 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002107 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
Elliott Hughes75770752011-08-24 17:52:38 -07002110 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002112 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
Elliott Hughes75770752011-08-24 17:52:38 -07002115 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002117 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002120 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002122 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 }
2124
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002125 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002127 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
2129
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002130 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002132 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 }
2134
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002135 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002136 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002137 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
2139
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002140 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002142 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002145 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002147 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 }
2149
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002150 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002152 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002155 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002157 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 }
2159
Elliott Hughes814e4032011-08-23 12:07:56 -07002160 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002162 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 }
2164
Elliott Hughes814e4032011-08-23 12:07:56 -07002165 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002167 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 }
2169
Elliott Hughes814e4032011-08-23 12:07:56 -07002170 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002172 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 }
2174
Elliott Hughes814e4032011-08-23 12:07:56 -07002175 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002177 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002178 }
2179
Elliott Hughes814e4032011-08-23 12:07:56 -07002180 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002182 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184
Elliott Hughes814e4032011-08-23 12:07:56 -07002185 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002186 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002187 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 }
2189
Elliott Hughes814e4032011-08-23 12:07:56 -07002190 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002192 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
Elliott Hughes814e4032011-08-23 12:07:56 -07002195 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002197 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 }
2199
Elliott Hughes814e4032011-08-23 12:07:56 -07002200 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002202 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 }
2204
Elliott Hughes814e4032011-08-23 12:07:56 -07002205 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002207 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 }
2209
Elliott Hughes814e4032011-08-23 12:07:56 -07002210 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002212 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 }
2214
Elliott Hughes814e4032011-08-23 12:07:56 -07002215 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002216 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002217 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 }
2219
Elliott Hughes814e4032011-08-23 12:07:56 -07002220 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002222 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Elliott Hughes814e4032011-08-23 12:07:56 -07002225 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002227 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
Elliott Hughes814e4032011-08-23 12:07:56 -07002230 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002232 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Elliott Hughes814e4032011-08-23 12:07:56 -07002235 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002237 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239
Elliott Hughes5174fe62011-08-23 15:12:35 -07002240 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002242 Class* c = Decode<Class*>(ts, java_class);
2243
Elliott Hughes5174fe62011-08-23 15:12:35 -07002244 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 const char* name = methods[i].name;
2246 const char* sig = methods[i].signature;
2247
2248 if (*sig == '!') {
2249 // TODO: fast jni. it's too noisy to log all these.
2250 ++sig;
2251 }
2252
Elliott Hughes5174fe62011-08-23 15:12:35 -07002253 Method* m = c->FindDirectMethod(name, sig);
2254 if (m == NULL) {
2255 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002257 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002258 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002259 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002260 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002261 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002262 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002263 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002264 return JNI_ERR;
2265 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002266
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002267 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002268
Ian Rogers60db5ab2012-02-20 17:02:00 -08002269 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002270 }
2271 return JNI_OK;
2272 }
2273
Elliott Hughes5174fe62011-08-23 15:12:35 -07002274 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002276 Class* c = Decode<Class*>(ts, java_class);
2277
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002278 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002279
2280 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2281 Method* m = c->GetDirectMethod(i);
2282 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002283 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002284 }
2285 }
2286 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2287 Method* m = c->GetVirtualMethod(i);
2288 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002289 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002290 }
2291 }
2292
2293 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002294 }
2295
Elliott Hughes72025e52011-08-23 17:50:30 -07002296 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002297 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002298 Object* o = Decode<Object*>(ts, java_object);
2299 o->MonitorEnter(ts.Self());
2300 if (ts.Self()->IsExceptionPending()) {
2301 return JNI_ERR;
2302 }
2303 ts.Env()->monitors.Add(o);
2304 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002305 }
2306
Elliott Hughes72025e52011-08-23 17:50:30 -07002307 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002308 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002309 Object* o = Decode<Object*>(ts, java_object);
2310 o->MonitorExit(ts.Self());
2311 if (ts.Self()->IsExceptionPending()) {
2312 return JNI_ERR;
2313 }
2314 ts.Env()->monitors.Remove(o);
2315 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002316 }
2317
2318 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2319 ScopedJniThreadState ts(env);
2320 Runtime* runtime = Runtime::Current();
2321 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002322 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002323 } else {
2324 *vm = NULL;
2325 }
2326 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2327 }
2328
Elliott Hughescdf53122011-08-19 15:46:09 -07002329 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2330 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002331
2332 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002333 CHECK(address != NULL); // TODO: ReportJniError
2334 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002335
Elliott Hughesb465ab02011-08-24 11:21:21 -07002336 // At the moment, the Java side is limited to 32 bits.
2337 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2338 CHECK_LE(capacity, 0xffffffff);
2339 jint address_arg = reinterpret_cast<jint>(address);
2340 jint capacity_arg = static_cast<jint>(capacity);
2341
Elliott Hugheseac76672012-05-24 21:56:51 -07002342 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2343 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2344 address_arg, capacity_arg);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002345 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002346 }
2347
Elliott Hughesb465ab02011-08-24 11:21:21 -07002348 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002349 ScopedJniThreadState ts(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002350 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002351 }
2352
Elliott Hughesb465ab02011-08-24 11:21:21 -07002353 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002354 ScopedJniThreadState ts(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002355 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002356 }
2357
Elliott Hughesb465ab02011-08-24 11:21:21 -07002358 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002359 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002360
Elliott Hughes75770752011-08-24 17:52:38 -07002361 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002362
2363 // Do we definitely know what kind of reference this is?
2364 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2365 IndirectRefKind kind = GetIndirectRefKind(ref);
2366 switch (kind) {
2367 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002368 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2369 return JNILocalRefType;
2370 }
2371 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002372 case kGlobal:
2373 return JNIGlobalRefType;
2374 case kWeakGlobal:
2375 return JNIWeakGlobalRefType;
2376 case kSirtOrInvalid:
2377 // Is it in a stack IRT?
TDYa12728f1a142012-03-15 21:51:52 -07002378 if (ts.Self()->StackReferencesContain(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002379 return JNILocalRefType;
2380 }
2381
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002382 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002383 return JNIInvalidRefType;
2384 }
2385
Elliott Hughesb465ab02011-08-24 11:21:21 -07002386 // If we're handing out direct pointers, check whether it's a direct pointer
2387 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002388 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002389 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002390 return JNILocalRefType;
2391 }
2392 }
2393
2394 return JNIInvalidRefType;
2395 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002396 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2397 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002398 }
2399};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002400
Elliott Hughes88c5c352012-03-15 18:49:48 -07002401const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002402 NULL, // reserved0.
2403 NULL, // reserved1.
2404 NULL, // reserved2.
2405 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002406 JNI::GetVersion,
2407 JNI::DefineClass,
2408 JNI::FindClass,
2409 JNI::FromReflectedMethod,
2410 JNI::FromReflectedField,
2411 JNI::ToReflectedMethod,
2412 JNI::GetSuperclass,
2413 JNI::IsAssignableFrom,
2414 JNI::ToReflectedField,
2415 JNI::Throw,
2416 JNI::ThrowNew,
2417 JNI::ExceptionOccurred,
2418 JNI::ExceptionDescribe,
2419 JNI::ExceptionClear,
2420 JNI::FatalError,
2421 JNI::PushLocalFrame,
2422 JNI::PopLocalFrame,
2423 JNI::NewGlobalRef,
2424 JNI::DeleteGlobalRef,
2425 JNI::DeleteLocalRef,
2426 JNI::IsSameObject,
2427 JNI::NewLocalRef,
2428 JNI::EnsureLocalCapacity,
2429 JNI::AllocObject,
2430 JNI::NewObject,
2431 JNI::NewObjectV,
2432 JNI::NewObjectA,
2433 JNI::GetObjectClass,
2434 JNI::IsInstanceOf,
2435 JNI::GetMethodID,
2436 JNI::CallObjectMethod,
2437 JNI::CallObjectMethodV,
2438 JNI::CallObjectMethodA,
2439 JNI::CallBooleanMethod,
2440 JNI::CallBooleanMethodV,
2441 JNI::CallBooleanMethodA,
2442 JNI::CallByteMethod,
2443 JNI::CallByteMethodV,
2444 JNI::CallByteMethodA,
2445 JNI::CallCharMethod,
2446 JNI::CallCharMethodV,
2447 JNI::CallCharMethodA,
2448 JNI::CallShortMethod,
2449 JNI::CallShortMethodV,
2450 JNI::CallShortMethodA,
2451 JNI::CallIntMethod,
2452 JNI::CallIntMethodV,
2453 JNI::CallIntMethodA,
2454 JNI::CallLongMethod,
2455 JNI::CallLongMethodV,
2456 JNI::CallLongMethodA,
2457 JNI::CallFloatMethod,
2458 JNI::CallFloatMethodV,
2459 JNI::CallFloatMethodA,
2460 JNI::CallDoubleMethod,
2461 JNI::CallDoubleMethodV,
2462 JNI::CallDoubleMethodA,
2463 JNI::CallVoidMethod,
2464 JNI::CallVoidMethodV,
2465 JNI::CallVoidMethodA,
2466 JNI::CallNonvirtualObjectMethod,
2467 JNI::CallNonvirtualObjectMethodV,
2468 JNI::CallNonvirtualObjectMethodA,
2469 JNI::CallNonvirtualBooleanMethod,
2470 JNI::CallNonvirtualBooleanMethodV,
2471 JNI::CallNonvirtualBooleanMethodA,
2472 JNI::CallNonvirtualByteMethod,
2473 JNI::CallNonvirtualByteMethodV,
2474 JNI::CallNonvirtualByteMethodA,
2475 JNI::CallNonvirtualCharMethod,
2476 JNI::CallNonvirtualCharMethodV,
2477 JNI::CallNonvirtualCharMethodA,
2478 JNI::CallNonvirtualShortMethod,
2479 JNI::CallNonvirtualShortMethodV,
2480 JNI::CallNonvirtualShortMethodA,
2481 JNI::CallNonvirtualIntMethod,
2482 JNI::CallNonvirtualIntMethodV,
2483 JNI::CallNonvirtualIntMethodA,
2484 JNI::CallNonvirtualLongMethod,
2485 JNI::CallNonvirtualLongMethodV,
2486 JNI::CallNonvirtualLongMethodA,
2487 JNI::CallNonvirtualFloatMethod,
2488 JNI::CallNonvirtualFloatMethodV,
2489 JNI::CallNonvirtualFloatMethodA,
2490 JNI::CallNonvirtualDoubleMethod,
2491 JNI::CallNonvirtualDoubleMethodV,
2492 JNI::CallNonvirtualDoubleMethodA,
2493 JNI::CallNonvirtualVoidMethod,
2494 JNI::CallNonvirtualVoidMethodV,
2495 JNI::CallNonvirtualVoidMethodA,
2496 JNI::GetFieldID,
2497 JNI::GetObjectField,
2498 JNI::GetBooleanField,
2499 JNI::GetByteField,
2500 JNI::GetCharField,
2501 JNI::GetShortField,
2502 JNI::GetIntField,
2503 JNI::GetLongField,
2504 JNI::GetFloatField,
2505 JNI::GetDoubleField,
2506 JNI::SetObjectField,
2507 JNI::SetBooleanField,
2508 JNI::SetByteField,
2509 JNI::SetCharField,
2510 JNI::SetShortField,
2511 JNI::SetIntField,
2512 JNI::SetLongField,
2513 JNI::SetFloatField,
2514 JNI::SetDoubleField,
2515 JNI::GetStaticMethodID,
2516 JNI::CallStaticObjectMethod,
2517 JNI::CallStaticObjectMethodV,
2518 JNI::CallStaticObjectMethodA,
2519 JNI::CallStaticBooleanMethod,
2520 JNI::CallStaticBooleanMethodV,
2521 JNI::CallStaticBooleanMethodA,
2522 JNI::CallStaticByteMethod,
2523 JNI::CallStaticByteMethodV,
2524 JNI::CallStaticByteMethodA,
2525 JNI::CallStaticCharMethod,
2526 JNI::CallStaticCharMethodV,
2527 JNI::CallStaticCharMethodA,
2528 JNI::CallStaticShortMethod,
2529 JNI::CallStaticShortMethodV,
2530 JNI::CallStaticShortMethodA,
2531 JNI::CallStaticIntMethod,
2532 JNI::CallStaticIntMethodV,
2533 JNI::CallStaticIntMethodA,
2534 JNI::CallStaticLongMethod,
2535 JNI::CallStaticLongMethodV,
2536 JNI::CallStaticLongMethodA,
2537 JNI::CallStaticFloatMethod,
2538 JNI::CallStaticFloatMethodV,
2539 JNI::CallStaticFloatMethodA,
2540 JNI::CallStaticDoubleMethod,
2541 JNI::CallStaticDoubleMethodV,
2542 JNI::CallStaticDoubleMethodA,
2543 JNI::CallStaticVoidMethod,
2544 JNI::CallStaticVoidMethodV,
2545 JNI::CallStaticVoidMethodA,
2546 JNI::GetStaticFieldID,
2547 JNI::GetStaticObjectField,
2548 JNI::GetStaticBooleanField,
2549 JNI::GetStaticByteField,
2550 JNI::GetStaticCharField,
2551 JNI::GetStaticShortField,
2552 JNI::GetStaticIntField,
2553 JNI::GetStaticLongField,
2554 JNI::GetStaticFloatField,
2555 JNI::GetStaticDoubleField,
2556 JNI::SetStaticObjectField,
2557 JNI::SetStaticBooleanField,
2558 JNI::SetStaticByteField,
2559 JNI::SetStaticCharField,
2560 JNI::SetStaticShortField,
2561 JNI::SetStaticIntField,
2562 JNI::SetStaticLongField,
2563 JNI::SetStaticFloatField,
2564 JNI::SetStaticDoubleField,
2565 JNI::NewString,
2566 JNI::GetStringLength,
2567 JNI::GetStringChars,
2568 JNI::ReleaseStringChars,
2569 JNI::NewStringUTF,
2570 JNI::GetStringUTFLength,
2571 JNI::GetStringUTFChars,
2572 JNI::ReleaseStringUTFChars,
2573 JNI::GetArrayLength,
2574 JNI::NewObjectArray,
2575 JNI::GetObjectArrayElement,
2576 JNI::SetObjectArrayElement,
2577 JNI::NewBooleanArray,
2578 JNI::NewByteArray,
2579 JNI::NewCharArray,
2580 JNI::NewShortArray,
2581 JNI::NewIntArray,
2582 JNI::NewLongArray,
2583 JNI::NewFloatArray,
2584 JNI::NewDoubleArray,
2585 JNI::GetBooleanArrayElements,
2586 JNI::GetByteArrayElements,
2587 JNI::GetCharArrayElements,
2588 JNI::GetShortArrayElements,
2589 JNI::GetIntArrayElements,
2590 JNI::GetLongArrayElements,
2591 JNI::GetFloatArrayElements,
2592 JNI::GetDoubleArrayElements,
2593 JNI::ReleaseBooleanArrayElements,
2594 JNI::ReleaseByteArrayElements,
2595 JNI::ReleaseCharArrayElements,
2596 JNI::ReleaseShortArrayElements,
2597 JNI::ReleaseIntArrayElements,
2598 JNI::ReleaseLongArrayElements,
2599 JNI::ReleaseFloatArrayElements,
2600 JNI::ReleaseDoubleArrayElements,
2601 JNI::GetBooleanArrayRegion,
2602 JNI::GetByteArrayRegion,
2603 JNI::GetCharArrayRegion,
2604 JNI::GetShortArrayRegion,
2605 JNI::GetIntArrayRegion,
2606 JNI::GetLongArrayRegion,
2607 JNI::GetFloatArrayRegion,
2608 JNI::GetDoubleArrayRegion,
2609 JNI::SetBooleanArrayRegion,
2610 JNI::SetByteArrayRegion,
2611 JNI::SetCharArrayRegion,
2612 JNI::SetShortArrayRegion,
2613 JNI::SetIntArrayRegion,
2614 JNI::SetLongArrayRegion,
2615 JNI::SetFloatArrayRegion,
2616 JNI::SetDoubleArrayRegion,
2617 JNI::RegisterNatives,
2618 JNI::UnregisterNatives,
2619 JNI::MonitorEnter,
2620 JNI::MonitorExit,
2621 JNI::GetJavaVM,
2622 JNI::GetStringRegion,
2623 JNI::GetStringUTFRegion,
2624 JNI::GetPrimitiveArrayCritical,
2625 JNI::ReleasePrimitiveArrayCritical,
2626 JNI::GetStringCritical,
2627 JNI::ReleaseStringCritical,
2628 JNI::NewWeakGlobalRef,
2629 JNI::DeleteWeakGlobalRef,
2630 JNI::ExceptionCheck,
2631 JNI::NewDirectByteBuffer,
2632 JNI::GetDirectBufferAddress,
2633 JNI::GetDirectBufferCapacity,
2634 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002635};
2636
Elliott Hughes75770752011-08-24 17:52:38 -07002637JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002638 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002639 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002640 local_ref_cookie(IRT_FIRST_SEGMENT),
2641 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002642 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002643 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002644 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002645 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002646 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002647 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002648 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002649 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2650 // errors will ensue.
2651 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2652 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002653}
2654
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002655JNIEnvExt::~JNIEnvExt() {
2656}
2657
Elliott Hughes88c5c352012-03-15 18:49:48 -07002658void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2659 check_jni = enabled;
2660 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002661}
2662
Elliott Hughes73e66f72012-05-09 09:34:45 -07002663void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2664 locals.Dump(os);
2665 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002666}
2667
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002668void JNIEnvExt::PushFrame(int /*capacity*/) {
2669 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002670 stacked_local_ref_cookies.push_back(local_ref_cookie);
2671 local_ref_cookie = locals.GetSegmentState();
2672}
2673
2674void JNIEnvExt::PopFrame() {
2675 locals.SetSegmentState(local_ref_cookie);
2676 local_ref_cookie = stacked_local_ref_cookies.back();
2677 stacked_local_ref_cookies.pop_back();
2678}
2679
Carl Shapiroea4dca82011-08-01 13:45:38 -07002680// JNI Invocation interface.
2681
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002682extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2683 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2684 if (args->version < JNI_VERSION_1_2) {
2685 return JNI_EVERSION;
2686 }
2687 Runtime::Options options;
2688 for (int i = 0; i < args->nOptions; ++i) {
2689 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002690 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002691 }
2692 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002693 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002694 if (runtime == NULL) {
2695 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002696 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002697 runtime->Start();
2698 *p_env = Thread::Current()->GetJniEnv();
2699 *p_vm = runtime->GetJavaVM();
2700 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002701}
2702
Elliott Hughesf2682d52011-08-15 16:37:04 -07002703extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002704 Runtime* runtime = Runtime::Current();
2705 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002706 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002707 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002708 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002709 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002710 }
2711 return JNI_OK;
2712}
2713
2714// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002715extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002716 return JNI_ERR;
2717}
2718
Elliott Hughescdf53122011-08-19 15:46:09 -07002719class JII {
2720 public:
2721 static jint DestroyJavaVM(JavaVM* vm) {
2722 if (vm == NULL) {
2723 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002724 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002725 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2726 delete raw_vm->runtime;
2727 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002728 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002729
Elliott Hughescdf53122011-08-19 15:46:09 -07002730 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002731 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002732 }
2733
2734 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002735 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002736 }
2737
2738 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002739 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002740 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002741 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002742 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2743 Runtime* runtime = raw_vm->runtime;
2744 runtime->DetachCurrentThread();
2745 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002746 }
2747
2748 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2749 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2750 return JNI_EVERSION;
2751 }
2752 if (vm == NULL || env == NULL) {
2753 return JNI_ERR;
2754 }
2755 Thread* thread = Thread::Current();
2756 if (thread == NULL) {
2757 *env = NULL;
2758 return JNI_EDETACHED;
2759 }
2760 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002761 return JNI_OK;
2762 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002763};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002764
Elliott Hughes88c5c352012-03-15 18:49:48 -07002765const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002766 NULL, // reserved0
2767 NULL, // reserved1
2768 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002769 JII::DestroyJavaVM,
2770 JII::AttachCurrentThread,
2771 JII::DetachCurrentThread,
2772 JII::GetEnv,
2773 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002774};
2775
Elliott Hughesa0957642011-09-02 14:27:33 -07002776JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002777 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002778 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002779 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002780 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002781 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002782 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002783 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002784 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002785 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002786 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002787 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002788 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002789 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002790 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002791 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002792 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002793 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002794 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002795 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002796}
2797
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002798JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002799 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002800}
2801
Elliott Hughes88c5c352012-03-15 18:49:48 -07002802void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2803 check_jni = enabled;
2804 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002805}
2806
Elliott Hughesae80b492012-04-24 10:43:17 -07002807void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2808 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2809 if (force_copy) {
2810 os << " (with forcecopy)";
2811 }
2812 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
2813 {
2814 MutexLock mu(pins_lock);
2815 os << "; pins=" << pin_table.Size();
2816 }
2817 {
2818 MutexLock mu(globals_lock);
2819 os << "; globals=" << globals.Capacity();
2820 }
2821 {
2822 MutexLock mu(weak_globals_lock);
2823 if (weak_globals.Capacity() > 0) {
2824 os << " (plus " << weak_globals.Capacity() << " weak)";
2825 }
2826 }
2827 os << '\n';
2828
2829 {
2830 MutexLock mu(libraries_lock);
2831 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2832 }
2833}
2834
Elliott Hughes73e66f72012-05-09 09:34:45 -07002835void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002836 {
2837 MutexLock mu(globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002838 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002839 }
2840 {
2841 MutexLock mu(weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002842 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002843 }
2844 {
2845 MutexLock mu(pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002846 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002847 }
2848}
2849
Elliott Hughes75770752011-08-24 17:52:38 -07002850bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2851 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002852
2853 // See if we've already loaded this library. If we have, and the class loader
2854 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002855 // TODO: for better results we should canonicalize the pathname (or even compare
2856 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002857 SharedLibrary* library;
2858 {
2859 // TODO: move the locking (and more of this logic) into Libraries.
2860 MutexLock mu(libraries_lock);
2861 library = libraries->Get(path);
2862 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002863 if (library != NULL) {
2864 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002865 // The library will be associated with class_loader. The JNI
2866 // spec says we can't load the same library into more than one
2867 // class loader.
2868 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2869 "ClassLoader %p; can't open in ClassLoader %p",
2870 path.c_str(), library->GetClassLoader(), class_loader);
2871 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002872 return false;
2873 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002874 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2875 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002876 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002877 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2878 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002879 return false;
2880 }
2881 return true;
2882 }
2883
2884 // Open the shared library. Because we're using a full path, the system
2885 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2886 // resolve this library's dependencies though.)
2887
2888 // Failures here are expected when java.library.path has several entries
2889 // and we have to hunt for the lib.
2890
2891 // The current version of the dynamic linker prints detailed information
2892 // about dlopen() failures. Some things to check if the message is
2893 // cryptic:
2894 // - make sure the library exists on the device
2895 // - verify that the right path is being opened (the debug log message
2896 // above can help with that)
2897 // - check to see if the library is valid (e.g. not zero bytes long)
2898 // - check config/prelink-linux-arm.map to ensure that the library
2899 // is listed and is not being overrun by the previous entry (if
2900 // loading suddenly stops working on a prelinked library, this is
2901 // a good one to check)
2902 // - write a trivial app that calls sleep() then dlopen(), attach
2903 // to it with "strace -p <pid>" while it sleeps, and watch for
2904 // attempts to open nonexistent dependent shared libs
2905
2906 // TODO: automate some of these checks!
2907
2908 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002909 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002910 // the GC to ignore us.
2911 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002912 void* handle = NULL;
2913 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002914 ScopedThreadStateChange tsc(self, kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002915 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002916 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002917
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002918 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002919
2920 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002921 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002922 return false;
2923 }
2924
2925 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002926 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002927 // TODO: move the locking (and more of this logic) into Libraries.
2928 MutexLock mu(libraries_lock);
2929 library = libraries->Get(path);
2930 if (library != NULL) {
2931 LOG(INFO) << "WOW: we lost a race to add shared library: "
2932 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002933 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002934 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002935 library = new SharedLibrary(path, handle, class_loader);
2936 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002937 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002938
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002939 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002940
2941 bool result = true;
2942 void* sym = dlsym(handle, "JNI_OnLoad");
2943 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002944 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002945 } else {
2946 // Call JNI_OnLoad. We have to override the current class
2947 // loader, which will always be "null" since the stuff at the
2948 // top of the stack is around Runtime.loadLibrary(). (See
2949 // the comments in the JNI FindClass function.)
2950 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2951 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002952 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002953 self->SetClassLoaderOverride(class_loader);
2954
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002955 int version = 0;
2956 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002957 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002958 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002959 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002960 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002961
Brian Carlstromaded5f72011-10-07 17:15:04 -07002962 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002963
2964 if (version != JNI_VERSION_1_2 &&
2965 version != JNI_VERSION_1_4 &&
2966 version != JNI_VERSION_1_6) {
2967 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2968 << "bad version: " << version;
2969 // It's unwise to call dlclose() here, but we can mark it
2970 // as bad and ensure that future load attempts will fail.
2971 // We don't know how far JNI_OnLoad got, so there could
2972 // be some partially-initialized stuff accessible through
2973 // newly-registered native method calls. We could try to
2974 // unregister them, but that doesn't seem worthwhile.
2975 result = false;
2976 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002977 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2978 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002979 }
2980 }
2981
2982 library->SetResult(result);
2983 return result;
2984}
2985
2986void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2987 CHECK(m->IsNative());
2988
2989 Class* c = m->GetDeclaringClass();
2990
2991 // If this is a static method, it could be called before the class
2992 // has been initialized.
2993 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002994 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002995 return NULL;
2996 }
2997 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002998 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002999 }
3000
Brian Carlstrom16192862011-09-12 17:50:06 -07003001 std::string detail;
3002 void* native_method;
3003 {
3004 MutexLock mu(libraries_lock);
3005 native_method = libraries->FindNativeMethod(m, detail);
3006 }
3007 // throwing can cause libraries_lock to be reacquired
3008 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07003009 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003010 }
3011 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003012}
3013
Elliott Hughes410c0c82011-09-01 17:58:25 -07003014void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
3015 {
3016 MutexLock mu(globals_lock);
3017 globals.VisitRoots(visitor, arg);
3018 }
3019 {
3020 MutexLock mu(pins_lock);
3021 pin_table.VisitRoots(visitor, arg);
3022 }
3023 // The weak_globals table is visited by the GC itself (because it mutates the table).
3024}
3025
Ian Rogersdf20fe02011-07-20 20:34:16 -07003026} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003027
3028std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3029 switch (rhs) {
3030 case JNIInvalidRefType:
3031 os << "JNIInvalidRefType";
3032 return os;
3033 case JNILocalRefType:
3034 os << "JNILocalRefType";
3035 return os;
3036 case JNIGlobalRefType:
3037 os << "JNIGlobalRefType";
3038 return os;
3039 case JNIWeakGlobalRefType:
3040 os << "JNIWeakGlobalRefType";
3041 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003042 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003043 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003044 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003045 }
3046}