blob: 82fba54c6f7dd845df38ae262ca8a7be039f2eea [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 Hughesf8349362012-06-18 15:00:06 -0700624 MutexLock mu(jni_on_load_lock_);
625
Elliott Hughes79082e32011-08-25 12:07:32 -0700626 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700627 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700628 // Check this so we don't end up waiting for ourselves. We need
629 // to return "true" so the caller can continue.
630 LOG(INFO) << *self << " recursive attempt to load library "
631 << "\"" << path_ << "\"";
632 return true;
633 }
634
Elliott Hughes79082e32011-08-25 12:07:32 -0700635 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800636 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
637 << "JNI_OnLoad...]";
Elliott Hughes34e06962012-04-09 13:55:55 -0700638 ScopedThreadStateChange tsc(self, kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700639 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700640 }
641
642 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800643 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
644 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700645 return okay;
646 }
647
648 void SetResult(bool result) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700649 MutexLock mu(jni_on_load_lock_);
650
Elliott Hughes79082e32011-08-25 12:07:32 -0700651 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700652 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700653
654 // Broadcast a wakeup to anybody sleeping on the condition variable.
Elliott Hughes5f791332011-09-15 17:45:30 -0700655 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700656 }
657
658 void* FindSymbol(const std::string& symbol_name) {
659 return dlsym(handle_, symbol_name.c_str());
660 }
661
662 private:
663 enum JNI_OnLoadState {
664 kPending,
665 kFailed,
666 kOkay,
667 };
668
669 // Path to library "/system/lib/libjni.so".
670 std::string path_;
671
672 // The void* returned by dlopen(3).
673 void* handle_;
674
675 // The ClassLoader this library is associated with.
676 Object* class_loader_;
677
678 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700679 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700680 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700681 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700682 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700683 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700684 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700685 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700686};
687
Elliott Hughes79082e32011-08-25 12:07:32 -0700688// This exists mainly to keep implementation details out of the header file.
689class Libraries {
690 public:
691 Libraries() {
692 }
693
694 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700695 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700696 }
697
Elliott Hughesae80b492012-04-24 10:43:17 -0700698 void Dump(std::ostream& os) const {
699 bool first = true;
700 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
701 if (!first) {
702 os << ' ';
703 }
704 first = false;
705 os << it->first;
706 }
707 }
708
709 size_t size() const {
710 return libraries_.size();
711 }
712
Elliott Hughes79082e32011-08-25 12:07:32 -0700713 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700714 It it = libraries_.find(path);
715 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700716 }
717
718 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700719 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700720 }
721
722 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700723 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700724 std::string jni_short_name(JniShortName(m));
725 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700726 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700727 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
728 SharedLibrary* library = it->second;
729 if (library->GetClassLoader() != declaring_class_loader) {
730 // We only search libraries loaded by the appropriate ClassLoader.
731 continue;
732 }
733 // Try the short name then the long name...
734 void* fn = library->FindSymbol(jni_short_name);
735 if (fn == NULL) {
736 fn = library->FindSymbol(jni_long_name);
737 }
738 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800739 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
740 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700741 return fn;
742 }
743 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700744 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700745 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700746 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700747 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700748 return NULL;
749 }
750
751 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700752 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700753
Elliott Hughesa0e18062012-04-13 15:59:59 -0700754 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700755};
756
Elliott Hughes418d20f2011-09-22 14:00:39 -0700757JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
758 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
759 Object* receiver = Decode<Object*>(env, obj);
760 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800761 ArgArray arg_array(method);
762 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700763 return InvokeWithArgArray(env, receiver, method, arg_array.get());
764}
765
Elliott Hughesd07986f2011-12-06 18:27:45 -0800766JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700767 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800768}
769
Elliott Hughescdf53122011-08-19 15:46:09 -0700770class JNI {
771 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700772
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 static jint GetVersion(JNIEnv* env) {
774 ScopedJniThreadState ts(env);
775 return JNI_VERSION_1_6;
776 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700777
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
779 ScopedJniThreadState ts(env);
780 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700781 return NULL;
782 }
783
Elliott Hughescdf53122011-08-19 15:46:09 -0700784 static jclass FindClass(JNIEnv* env, const char* name) {
785 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700786 Runtime* runtime = Runtime::Current();
787 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700788 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700789 Class* c = NULL;
790 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700791 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800792 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700793 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800794 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700795 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700796 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700797 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700798
Elliott Hughescdf53122011-08-19 15:46:09 -0700799 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
800 ScopedJniThreadState ts(env);
801 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700802 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700803 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700804
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
806 ScopedJniThreadState ts(env);
807 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700808 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700809 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700810
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
812 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700813 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700814 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700816
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
818 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700819 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700820 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700822
Elliott Hughes37f7a402011-08-22 18:56:01 -0700823 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
824 ScopedJniThreadState ts(env);
825 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700826 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700827 }
828
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700829 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700831 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700832 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700833 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700834
Elliott Hughes37f7a402011-08-22 18:56:01 -0700835 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700837 Class* c1 = Decode<Class*>(ts, java_class1);
838 Class* c2 = Decode<Class*>(ts, java_class2);
839 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700841
Elliott Hughese84278b2012-03-22 10:06:53 -0700842 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700843 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700844 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700845 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700846 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700847 return JNI_TRUE;
848 } else {
849 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700850 Class* c = Decode<Class*>(ts, java_class);
851 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700852 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700853 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700854
Elliott Hughes37f7a402011-08-22 18:56:01 -0700855 static jint Throw(JNIEnv* env, jthrowable java_exception) {
856 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700857 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700858 if (exception == NULL) {
859 return JNI_ERR;
860 }
861 ts.Self()->SetException(exception);
862 return JNI_OK;
863 }
864
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700865 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700866 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700867 }
868
869 static jboolean ExceptionCheck(JNIEnv* env) {
870 ScopedJniThreadState ts(env);
871 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
872 }
873
874 static void ExceptionClear(JNIEnv* env) {
875 ScopedJniThreadState ts(env);
876 ts.Self()->ClearException();
877 }
878
879 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700880 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700881
882 Thread* self = ts.Self();
883 Throwable* original_exception = self->GetException();
884 self->ClearException();
885
Elliott Hughesbf86d042011-08-31 17:53:14 -0700886 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700887 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
888 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
889 if (mid == NULL) {
890 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700891 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700892 } else {
893 env->CallVoidMethod(exception.get(), mid);
894 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700895 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700896 << " thrown while calling printStackTrace";
897 self->ClearException();
898 }
899 }
900
901 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700903
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 static jthrowable ExceptionOccurred(JNIEnv* env) {
905 ScopedJniThreadState ts(env);
906 Object* exception = ts.Self()->GetException();
Elliott Hughes81ff3182012-03-23 20:35:56 -0700907 return (exception != NULL) ? AddLocalReference<jthrowable>(env, exception) : NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 }
909
Elliott Hughescdf53122011-08-19 15:46:09 -0700910 static void FatalError(JNIEnv* env, const char* msg) {
911 ScopedJniThreadState ts(env);
912 LOG(FATAL) << "JNI FatalError called: " << msg;
913 }
914
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700915 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700917 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
918 return JNI_ERR;
919 }
920 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 return JNI_OK;
922 }
923
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700924 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700926 Object* survivor = Decode<Object*>(ts, java_survivor);
927 ts.Env()->PopFrame();
928 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 }
930
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700931 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700933 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
934 }
935
936 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
937 // TODO: we should try to expand the table if necessary.
938 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
939 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
940 return JNI_ERR;
941 }
942 // TODO: this isn't quite right, since "capacity" includes holes.
943 size_t capacity = ts.Env()->locals.Capacity();
944 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
945 if (!okay) {
946 ts.Self()->ThrowOutOfMemoryError(caller);
947 }
948 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700949 }
950
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
952 ScopedJniThreadState ts(env);
953 if (obj == NULL) {
954 return NULL;
955 }
956
Elliott Hughes75770752011-08-24 17:52:38 -0700957 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700958 IndirectReferenceTable& globals = vm->globals;
959 MutexLock mu(vm->globals_lock);
960 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
961 return reinterpret_cast<jobject>(ref);
962 }
963
964 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
965 ScopedJniThreadState ts(env);
966 if (obj == NULL) {
967 return;
968 }
969
Elliott Hughes75770752011-08-24 17:52:38 -0700970 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 IndirectReferenceTable& globals = vm->globals;
972 MutexLock mu(vm->globals_lock);
973
974 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
975 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
976 << "failed to find entry";
977 }
978 }
979
980 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
981 ScopedJniThreadState ts(env);
982 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
983 }
984
985 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
986 ScopedJniThreadState ts(env);
987 if (obj == NULL) {
988 return;
989 }
990
Elliott Hughes75770752011-08-24 17:52:38 -0700991 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 IndirectReferenceTable& weak_globals = vm->weak_globals;
993 MutexLock mu(vm->weak_globals_lock);
994
995 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
996 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
997 << "failed to find entry";
998 }
999 }
1000
1001 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
1002 ScopedJniThreadState ts(env);
1003 if (obj == NULL) {
1004 return NULL;
1005 }
1006
1007 IndirectReferenceTable& locals = ts.Env()->locals;
1008
Ian Rogers5a7a74a2011-09-26 16:32:29 -07001009 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
1011 return reinterpret_cast<jobject>(ref);
1012 }
1013
1014 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
1015 ScopedJniThreadState ts(env);
1016 if (obj == NULL) {
1017 return;
1018 }
1019
1020 IndirectReferenceTable& locals = ts.Env()->locals;
1021
Ian Rogers5a7a74a2011-09-26 16:32:29 -07001022 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 if (!locals.Remove(cookie, obj)) {
1024 // Attempting to delete a local reference that is not in the
1025 // topmost local reference frame is a no-op. DeleteLocalRef returns
1026 // void and doesn't throw any exceptions, but we should probably
1027 // complain about it so the user will notice that things aren't
1028 // going quite the way they expect.
1029 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
1030 << "failed to find entry";
1031 }
1032 }
1033
1034 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
1035 ScopedJniThreadState ts(env);
1036 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
1037 ? JNI_TRUE : JNI_FALSE;
1038 }
1039
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001040 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001042 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001043 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001044 return NULL;
1045 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001046 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -07001047 }
1048
Elliott Hughese84278b2012-03-22 10:06:53 -07001049 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 ScopedJniThreadState ts(env);
1051 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001053 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 va_end(args);
1055 return result;
1056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001060 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001061 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001062 return NULL;
1063 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001064 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001065 if (result == NULL) {
1066 return NULL;
1067 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001068 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001070 if (!ts.Self()->IsExceptionPending()) {
1071 return local_result;
1072 } else {
1073 return NULL;
1074 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001079 Class* c = Decode<Class*>(ts, java_class);
Ian Rogers0045a292012-03-31 21:08:41 -07001080 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001081 return NULL;
1082 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001083 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001084 if (result == NULL) {
1085 return NULL;
1086 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001087 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001089 if (!ts.Self()->IsExceptionPending()) {
1090 return local_result;
1091 } else {
1092 return NULL;
1093 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1097 ScopedJniThreadState ts(env);
1098 return FindMethodID(ts, c, name, sig, false);
1099 }
1100
1101 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1102 ScopedJniThreadState ts(env);
1103 return FindMethodID(ts, c, name, sig, true);
1104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 va_list ap;
1109 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001110 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001112 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001117 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001118 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001123 JValue result(InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001124 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001125 }
1126
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_list ap;
1130 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001131 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001133 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001138 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001143 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_list ap;
1149 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001150 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001152 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001157 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001162 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 va_list ap;
1168 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001169 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001171 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001176 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001181 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 va_list ap;
1187 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001188 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001190 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 }
1192
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001195 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001200 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 va_list ap;
1206 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001207 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001209 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001214 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001219 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 va_list ap;
1225 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001226 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001228 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001233 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001238 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 va_list ap;
1244 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001245 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001247 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001252 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 }
1254
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001257 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
Elliott Hughes72025e52011-08-23 17:50:30 -07001260 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 va_list ap;
1263 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001264 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001265 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001266 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 }
1268
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001271 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 }
1273
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001276 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 }
1278
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001281 va_list ap;
1282 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001283 JValue result(InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001284 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
Elliott Hughes72025e52011-08-23 17:50:30 -07001287 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001289 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
Elliott Hughes72025e52011-08-23 17:50:30 -07001292 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001294 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001297 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 ScopedJniThreadState ts(env);
1299 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001300 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001301 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001302 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 va_end(ap);
1304 return local_result;
1305 }
1306
1307 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001308 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001310 JValue result(InvokeWithVarArgs(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001311 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
1314 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001315 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001317 JValue result(InvokeWithJValues(env, obj, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001318 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
1321 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001322 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 ScopedJniThreadState ts(env);
1324 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001326 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001328 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
1331 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001332 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001334 return InvokeWithVarArgs(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
1337 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001338 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001340 return InvokeWithJValues(env, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001343 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 ScopedJniThreadState ts(env);
1345 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001346 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001347 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001349 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
1352 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001353 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001355 return InvokeWithVarArgs(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
1358 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001359 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001361 return InvokeWithJValues(env, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001364 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 ScopedJniThreadState ts(env);
1366 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001367 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001368 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001370 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 }
1372
1373 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001374 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001376 return InvokeWithVarArgs(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
1379 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001380 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001382 return InvokeWithJValues(env, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
1384
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001385 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 ScopedJniThreadState ts(env);
1387 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001388 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001389 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001391 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
1393
1394 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001395 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001397 return InvokeWithVarArgs(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
1400 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001401 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001403 return InvokeWithJValues(env, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001406 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 ScopedJniThreadState ts(env);
1408 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001409 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001410 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001412 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
1415 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001416 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001417 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001418 return InvokeWithVarArgs(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
1421 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001422 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001424 return InvokeWithJValues(env, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 }
1426
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001427 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 ScopedJniThreadState ts(env);
1429 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001430 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001431 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001433 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
1436 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001437 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001439 return InvokeWithVarArgs(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
1442 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001443 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001445 return InvokeWithJValues(env, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001448 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 ScopedJniThreadState ts(env);
1450 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001451 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001452 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001454 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 }
1456
1457 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001458 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001460 return InvokeWithVarArgs(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 }
1462
1463 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001464 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001466 return InvokeWithJValues(env, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 }
1468
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001469 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 ScopedJniThreadState ts(env);
1471 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001472 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001473 JValue result(InvokeWithVarArgs(env, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001475 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
1478 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001479 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001481 return InvokeWithVarArgs(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
1484 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001485 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001487 return InvokeWithJValues(env, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001488 }
1489
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001490 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001491 ScopedJniThreadState ts(env);
1492 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001493 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001494 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 va_end(ap);
1496 }
1497
1498 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001499 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001501 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
1504 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001505 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001507 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 }
1509
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001510 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001511 ScopedJniThreadState ts(env);
1512 return FindFieldID(ts, c, name, sig, false);
1513 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001514
1515
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001516 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 ScopedJniThreadState ts(env);
1518 return FindFieldID(ts, c, name, sig, true);
1519 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001520
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001521 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001523 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001524 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001525 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001527
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001530 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001531 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
1533
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001534 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001536 Object* o = Decode<Object*>(ts, java_object);
1537 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001538 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001539 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001542 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001545 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001546 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001549#define GET_PRIMITIVE_FIELD(fn, instance) \
1550 ScopedJniThreadState ts(env); \
1551 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001552 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001553 return f->fn(o)
1554
1555#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1556 ScopedJniThreadState ts(env); \
1557 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001558 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001559 f->fn(o, value)
1560
1561 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1562 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001565 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1566 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001569 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1570 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 }
1572
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001573 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1574 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001577 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1578 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 }
1580
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001581 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1582 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001585 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1586 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001589 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1590 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001593 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001594 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001597 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001598 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001601 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001602 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001605 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001606 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001609 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001610 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001613 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001614 GET_PRIMITIVE_FIELD(GetLong, NULL);
1615 }
1616
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001617 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001618 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1619 }
1620
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001621 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001622 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1623 }
1624
1625 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1626 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1627 }
1628
1629 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1630 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1631 }
1632
1633 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1634 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1635 }
1636
1637 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1638 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1639 }
1640
1641 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1642 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1643 }
1644
1645 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1646 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1647 }
1648
1649 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1650 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1651 }
1652
1653 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1654 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1655 }
1656
1657 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1658 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1659 }
1660
1661 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1662 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1663 }
1664
1665 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1666 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1667 }
1668
1669 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1670 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1671 }
1672
1673 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1674 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1675 }
1676
1677 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1678 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1679 }
1680
1681 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1682 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1683 }
1684
1685 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1686 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001689 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 ScopedJniThreadState ts(env);
1691 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001692 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001693 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001694 jobject local_result = AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 va_end(ap);
1696 return local_result;
1697 }
1698
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001699 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001701 JValue result(InvokeWithVarArgs(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001702 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 }
1704
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001705 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001707 JValue result(InvokeWithJValues(env, NULL, mid, args));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001708 return AddLocalReference<jobject>(env, result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001711 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 ScopedJniThreadState ts(env);
1713 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001714 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001715 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001717 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001720 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001722 return InvokeWithVarArgs(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001725 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001727 return InvokeWithJValues(env, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001730 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 ScopedJniThreadState ts(env);
1732 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001733 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001734 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001736 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001741 return InvokeWithVarArgs(env, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 }
1743
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001744 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001746 return InvokeWithJValues(env, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 }
1748
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001749 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 ScopedJniThreadState ts(env);
1751 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001752 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001753 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001755 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 }
1757
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001758 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001760 return InvokeWithVarArgs(env, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001763 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001765 return InvokeWithJValues(env, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 }
1767
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001768 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 ScopedJniThreadState ts(env);
1770 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001771 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001772 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001774 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001777 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001779 return InvokeWithVarArgs(env, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 }
1781
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001782 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001784 return InvokeWithJValues(env, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001787 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 ScopedJniThreadState ts(env);
1789 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001790 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001791 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001793 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 }
1795
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001796 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001797 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001798 return InvokeWithVarArgs(env, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001801 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001803 return InvokeWithJValues(env, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001806 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 ScopedJniThreadState ts(env);
1808 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001809 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001810 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001812 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 }
1814
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001815 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001817 return InvokeWithVarArgs(env, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 }
1819
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001820 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001822 return InvokeWithJValues(env, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001825 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 ScopedJniThreadState ts(env);
1827 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001828 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001829 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001831 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001834 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001836 return InvokeWithVarArgs(env, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001839 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001841 return InvokeWithJValues(env, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 }
1843
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001844 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 ScopedJniThreadState ts(env);
1846 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001847 va_start(ap, mid);
Elliott Hughes1d878f32012-04-11 15:17:54 -07001848 JValue result(InvokeWithVarArgs(env, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001850 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 }
1852
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001853 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001855 return InvokeWithVarArgs(env, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001856 }
1857
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001858 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 ScopedJniThreadState ts(env);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001860 return InvokeWithJValues(env, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 }
1862
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001863 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 ScopedJniThreadState ts(env);
1865 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001866 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001867 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 va_end(ap);
1869 }
1870
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001871 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001873 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 }
1875
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001876 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001878 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 }
1880
Elliott Hughes814e4032011-08-23 12:07:56 -07001881 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001883 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001884 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001885 }
1886
1887 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1888 ScopedJniThreadState ts(env);
1889 if (utf == NULL) {
1890 return NULL;
1891 }
1892 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001893 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001894 }
1895
Elliott Hughes814e4032011-08-23 12:07:56 -07001896 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1897 ScopedJniThreadState ts(env);
1898 return Decode<String*>(ts, java_string)->GetLength();
1899 }
1900
1901 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1902 ScopedJniThreadState ts(env);
1903 return Decode<String*>(ts, java_string)->GetUtfLength();
1904 }
1905
Elliott Hughesb465ab02011-08-24 11:21:21 -07001906 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001908 String* s = Decode<String*>(ts, java_string);
1909 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1910 ThrowSIOOBE(ts, start, length, s->GetLength());
1911 } else {
1912 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1913 memcpy(buf, chars + start, length * sizeof(jchar));
1914 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001915 }
1916
Elliott Hughesb465ab02011-08-24 11:21:21 -07001917 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001918 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001919 String* s = Decode<String*>(ts, java_string);
1920 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1921 ThrowSIOOBE(ts, start, length, s->GetLength());
1922 } else {
1923 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1924 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1925 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001926 }
1927
Elliott Hughes75770752011-08-24 17:52:38 -07001928 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001929 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001930 String* s = Decode<String*>(ts, java_string);
1931 const CharArray* chars = s->GetCharArray();
1932 PinPrimitiveArray(ts, chars);
1933 if (is_copy != NULL) {
1934 *is_copy = JNI_FALSE;
1935 }
1936 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001937 }
1938
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001939 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001940 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001941 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001942 }
1943
Elliott Hughes75770752011-08-24 17:52:38 -07001944 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001946 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 }
1948
Elliott Hughes75770752011-08-24 17:52:38 -07001949 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001951 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 }
1953
Elliott Hughes75770752011-08-24 17:52:38 -07001954 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001955 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001956 if (java_string == NULL) {
1957 return NULL;
1958 }
1959 if (is_copy != NULL) {
1960 *is_copy = JNI_TRUE;
1961 }
1962 String* s = Decode<String*>(ts, java_string);
1963 size_t byte_count = s->GetUtfLength();
1964 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001965 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001966 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1967 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1968 bytes[byte_count] = '\0';
1969 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001970 }
1971
Elliott Hughes75770752011-08-24 17:52:38 -07001972 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001973 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001974 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001975 }
1976
Elliott Hughesbd935992011-08-22 11:59:34 -07001977 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001979 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001980 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001981 Array* array = obj->AsArray();
1982 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 }
1984
Elliott Hughes814e4032011-08-23 12:07:56 -07001985 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001987 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001988 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
1991 static void SetObjectArrayElement(JNIEnv* env,
1992 jobjectArray java_array, jsize index, jobject java_value) {
1993 ScopedJniThreadState ts(env);
1994 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1995 Object* value = Decode<Object*>(ts, java_value);
1996 array->Set(index, value);
1997 }
1998
1999 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
2000 ScopedJniThreadState ts(env);
2001 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
2002 }
2003
2004 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
2005 ScopedJniThreadState ts(env);
2006 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
2007 }
2008
2009 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
2010 ScopedJniThreadState ts(env);
2011 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
2012 }
2013
2014 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
2015 ScopedJniThreadState ts(env);
2016 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
2017 }
2018
2019 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
2020 ScopedJniThreadState ts(env);
2021 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
2022 }
2023
2024 static jintArray NewIntArray(JNIEnv* env, jsize length) {
2025 ScopedJniThreadState ts(env);
2026 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
2027 }
2028
2029 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2030 ScopedJniThreadState ts(env);
2031 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
2032 }
2033
2034 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
2035 ScopedJniThreadState ts(env);
2036 CHECK_GE(length, 0); // TODO: ReportJniError
2037
2038 // Compute the array class corresponding to the given element class.
2039 Class* element_class = Decode<Class*>(ts, element_jclass);
2040 std::string descriptor;
2041 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002042 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002043
2044 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002045 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2046 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 return NULL;
2048 }
2049
Elliott Hughes75770752011-08-24 17:52:38 -07002050 // Allocate and initialize if necessary.
2051 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002053 if (initial_element != NULL) {
2054 Object* initial_object = Decode<Object*>(ts, initial_element);
2055 for (jsize i = 0; i < length; ++i) {
2056 result->Set(i, initial_object);
2057 }
2058 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07002059 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
2062 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2063 ScopedJniThreadState ts(env);
2064 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2065 }
2066
Ian Rogersa15e67d2012-02-28 13:51:55 -08002067 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002068 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002069 Array* array = Decode<Array*>(ts, java_array);
2070 PinPrimitiveArray(ts, array);
2071 if (is_copy != NULL) {
2072 *is_copy = JNI_FALSE;
2073 }
2074 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002075 }
2076
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002077 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002078 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002079 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002080 }
2081
Elliott Hughes75770752011-08-24 17:52:38 -07002082 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002084 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Elliott Hughes75770752011-08-24 17:52:38 -07002087 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002089 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
Elliott Hughes75770752011-08-24 17:52:38 -07002092 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002094 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 }
2096
Elliott Hughes75770752011-08-24 17:52:38 -07002097 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002099 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 }
2101
Elliott Hughes75770752011-08-24 17:52:38 -07002102 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002104 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 }
2106
Elliott Hughes75770752011-08-24 17:52:38 -07002107 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002109 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
Elliott Hughes75770752011-08-24 17:52:38 -07002112 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002114 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 }
2116
Elliott Hughes75770752011-08-24 17:52:38 -07002117 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002119 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002122 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002124 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002127 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002129 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 }
2131
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002132 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002134 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 }
2136
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002137 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002139 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 }
2141
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002142 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002144 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 }
2146
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002147 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002149 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 }
2151
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002152 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002154 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
2156
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002157 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002159 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 }
2161
Elliott Hughes814e4032011-08-23 12:07:56 -07002162 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002164 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Elliott Hughes814e4032011-08-23 12:07:56 -07002167 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002168 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002169 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Elliott Hughes814e4032011-08-23 12:07:56 -07002172 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002174 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
2176
Elliott Hughes814e4032011-08-23 12:07:56 -07002177 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002178 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002179 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
Elliott Hughes814e4032011-08-23 12:07:56 -07002182 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002184 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Elliott Hughes814e4032011-08-23 12:07:56 -07002187 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002189 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Elliott Hughes814e4032011-08-23 12:07:56 -07002192 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002194 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Elliott Hughes814e4032011-08-23 12:07:56 -07002197 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002198 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002199 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
Elliott Hughes814e4032011-08-23 12:07:56 -07002202 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002204 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002205 }
2206
Elliott Hughes814e4032011-08-23 12:07:56 -07002207 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002208 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002209 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 }
2211
Elliott Hughes814e4032011-08-23 12:07:56 -07002212 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002214 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 }
2216
Elliott Hughes814e4032011-08-23 12:07:56 -07002217 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002219 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 }
2221
Elliott Hughes814e4032011-08-23 12:07:56 -07002222 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002224 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002225 }
2226
Elliott Hughes814e4032011-08-23 12:07:56 -07002227 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002229 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
2231
Elliott Hughes814e4032011-08-23 12:07:56 -07002232 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002234 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 }
2236
Elliott Hughes814e4032011-08-23 12:07:56 -07002237 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002239 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002240 }
2241
Elliott Hughes5174fe62011-08-23 15:12:35 -07002242 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002244 Class* c = Decode<Class*>(ts, java_class);
2245
Elliott Hughes5174fe62011-08-23 15:12:35 -07002246 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 const char* name = methods[i].name;
2248 const char* sig = methods[i].signature;
2249
2250 if (*sig == '!') {
2251 // TODO: fast jni. it's too noisy to log all these.
2252 ++sig;
2253 }
2254
Elliott Hughes5174fe62011-08-23 15:12:35 -07002255 Method* m = c->FindDirectMethod(name, sig);
2256 if (m == NULL) {
2257 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002258 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002259 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002260 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002261 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002263 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002264 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002265 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002266 return JNI_ERR;
2267 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002268
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002269 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002270
Ian Rogers60db5ab2012-02-20 17:02:00 -08002271 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273 return JNI_OK;
2274 }
2275
Elliott Hughes5174fe62011-08-23 15:12:35 -07002276 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002278 Class* c = Decode<Class*>(ts, java_class);
2279
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002280 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002281
2282 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2283 Method* m = c->GetDirectMethod(i);
2284 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002285 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002286 }
2287 }
2288 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2289 Method* m = c->GetVirtualMethod(i);
2290 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002291 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002292 }
2293 }
2294
2295 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 }
2297
Elliott Hughes72025e52011-08-23 17:50:30 -07002298 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002299 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002300 Object* o = Decode<Object*>(ts, java_object);
2301 o->MonitorEnter(ts.Self());
2302 if (ts.Self()->IsExceptionPending()) {
2303 return JNI_ERR;
2304 }
2305 ts.Env()->monitors.Add(o);
2306 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002307 }
2308
Elliott Hughes72025e52011-08-23 17:50:30 -07002309 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002310 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002311 Object* o = Decode<Object*>(ts, java_object);
2312 o->MonitorExit(ts.Self());
2313 if (ts.Self()->IsExceptionPending()) {
2314 return JNI_ERR;
2315 }
2316 ts.Env()->monitors.Remove(o);
2317 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 }
2319
2320 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2321 ScopedJniThreadState ts(env);
2322 Runtime* runtime = Runtime::Current();
2323 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002324 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002325 } else {
2326 *vm = NULL;
2327 }
2328 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2329 }
2330
Elliott Hughescdf53122011-08-19 15:46:09 -07002331 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2332 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002333
2334 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002335 CHECK(address != NULL); // TODO: ReportJniError
2336 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002337
Elliott Hughesb465ab02011-08-24 11:21:21 -07002338 // At the moment, the Java side is limited to 32 bits.
2339 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2340 CHECK_LE(capacity, 0xffffffff);
2341 jint address_arg = reinterpret_cast<jint>(address);
2342 jint capacity_arg = static_cast<jint>(capacity);
2343
Elliott Hugheseac76672012-05-24 21:56:51 -07002344 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2345 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2346 address_arg, capacity_arg);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002347 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002348 }
2349
Elliott Hughesb465ab02011-08-24 11:21:21 -07002350 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002351 ScopedJniThreadState ts(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002352 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002353 }
2354
Elliott Hughesb465ab02011-08-24 11:21:21 -07002355 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002356 ScopedJniThreadState ts(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002357 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002358 }
2359
Elliott Hughesb465ab02011-08-24 11:21:21 -07002360 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002361 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002362
Elliott Hughes75770752011-08-24 17:52:38 -07002363 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002364
2365 // Do we definitely know what kind of reference this is?
2366 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2367 IndirectRefKind kind = GetIndirectRefKind(ref);
2368 switch (kind) {
2369 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002370 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2371 return JNILocalRefType;
2372 }
2373 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002374 case kGlobal:
2375 return JNIGlobalRefType;
2376 case kWeakGlobal:
2377 return JNIWeakGlobalRefType;
2378 case kSirtOrInvalid:
2379 // Is it in a stack IRT?
TDYa12728f1a142012-03-15 21:51:52 -07002380 if (ts.Self()->StackReferencesContain(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002381 return JNILocalRefType;
2382 }
2383
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002384 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002385 return JNIInvalidRefType;
2386 }
2387
Elliott Hughesb465ab02011-08-24 11:21:21 -07002388 // If we're handing out direct pointers, check whether it's a direct pointer
2389 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002390 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002391 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002392 return JNILocalRefType;
2393 }
2394 }
2395
2396 return JNIInvalidRefType;
2397 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002398 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2399 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002400 }
2401};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002402
Elliott Hughes88c5c352012-03-15 18:49:48 -07002403const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002404 NULL, // reserved0.
2405 NULL, // reserved1.
2406 NULL, // reserved2.
2407 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002408 JNI::GetVersion,
2409 JNI::DefineClass,
2410 JNI::FindClass,
2411 JNI::FromReflectedMethod,
2412 JNI::FromReflectedField,
2413 JNI::ToReflectedMethod,
2414 JNI::GetSuperclass,
2415 JNI::IsAssignableFrom,
2416 JNI::ToReflectedField,
2417 JNI::Throw,
2418 JNI::ThrowNew,
2419 JNI::ExceptionOccurred,
2420 JNI::ExceptionDescribe,
2421 JNI::ExceptionClear,
2422 JNI::FatalError,
2423 JNI::PushLocalFrame,
2424 JNI::PopLocalFrame,
2425 JNI::NewGlobalRef,
2426 JNI::DeleteGlobalRef,
2427 JNI::DeleteLocalRef,
2428 JNI::IsSameObject,
2429 JNI::NewLocalRef,
2430 JNI::EnsureLocalCapacity,
2431 JNI::AllocObject,
2432 JNI::NewObject,
2433 JNI::NewObjectV,
2434 JNI::NewObjectA,
2435 JNI::GetObjectClass,
2436 JNI::IsInstanceOf,
2437 JNI::GetMethodID,
2438 JNI::CallObjectMethod,
2439 JNI::CallObjectMethodV,
2440 JNI::CallObjectMethodA,
2441 JNI::CallBooleanMethod,
2442 JNI::CallBooleanMethodV,
2443 JNI::CallBooleanMethodA,
2444 JNI::CallByteMethod,
2445 JNI::CallByteMethodV,
2446 JNI::CallByteMethodA,
2447 JNI::CallCharMethod,
2448 JNI::CallCharMethodV,
2449 JNI::CallCharMethodA,
2450 JNI::CallShortMethod,
2451 JNI::CallShortMethodV,
2452 JNI::CallShortMethodA,
2453 JNI::CallIntMethod,
2454 JNI::CallIntMethodV,
2455 JNI::CallIntMethodA,
2456 JNI::CallLongMethod,
2457 JNI::CallLongMethodV,
2458 JNI::CallLongMethodA,
2459 JNI::CallFloatMethod,
2460 JNI::CallFloatMethodV,
2461 JNI::CallFloatMethodA,
2462 JNI::CallDoubleMethod,
2463 JNI::CallDoubleMethodV,
2464 JNI::CallDoubleMethodA,
2465 JNI::CallVoidMethod,
2466 JNI::CallVoidMethodV,
2467 JNI::CallVoidMethodA,
2468 JNI::CallNonvirtualObjectMethod,
2469 JNI::CallNonvirtualObjectMethodV,
2470 JNI::CallNonvirtualObjectMethodA,
2471 JNI::CallNonvirtualBooleanMethod,
2472 JNI::CallNonvirtualBooleanMethodV,
2473 JNI::CallNonvirtualBooleanMethodA,
2474 JNI::CallNonvirtualByteMethod,
2475 JNI::CallNonvirtualByteMethodV,
2476 JNI::CallNonvirtualByteMethodA,
2477 JNI::CallNonvirtualCharMethod,
2478 JNI::CallNonvirtualCharMethodV,
2479 JNI::CallNonvirtualCharMethodA,
2480 JNI::CallNonvirtualShortMethod,
2481 JNI::CallNonvirtualShortMethodV,
2482 JNI::CallNonvirtualShortMethodA,
2483 JNI::CallNonvirtualIntMethod,
2484 JNI::CallNonvirtualIntMethodV,
2485 JNI::CallNonvirtualIntMethodA,
2486 JNI::CallNonvirtualLongMethod,
2487 JNI::CallNonvirtualLongMethodV,
2488 JNI::CallNonvirtualLongMethodA,
2489 JNI::CallNonvirtualFloatMethod,
2490 JNI::CallNonvirtualFloatMethodV,
2491 JNI::CallNonvirtualFloatMethodA,
2492 JNI::CallNonvirtualDoubleMethod,
2493 JNI::CallNonvirtualDoubleMethodV,
2494 JNI::CallNonvirtualDoubleMethodA,
2495 JNI::CallNonvirtualVoidMethod,
2496 JNI::CallNonvirtualVoidMethodV,
2497 JNI::CallNonvirtualVoidMethodA,
2498 JNI::GetFieldID,
2499 JNI::GetObjectField,
2500 JNI::GetBooleanField,
2501 JNI::GetByteField,
2502 JNI::GetCharField,
2503 JNI::GetShortField,
2504 JNI::GetIntField,
2505 JNI::GetLongField,
2506 JNI::GetFloatField,
2507 JNI::GetDoubleField,
2508 JNI::SetObjectField,
2509 JNI::SetBooleanField,
2510 JNI::SetByteField,
2511 JNI::SetCharField,
2512 JNI::SetShortField,
2513 JNI::SetIntField,
2514 JNI::SetLongField,
2515 JNI::SetFloatField,
2516 JNI::SetDoubleField,
2517 JNI::GetStaticMethodID,
2518 JNI::CallStaticObjectMethod,
2519 JNI::CallStaticObjectMethodV,
2520 JNI::CallStaticObjectMethodA,
2521 JNI::CallStaticBooleanMethod,
2522 JNI::CallStaticBooleanMethodV,
2523 JNI::CallStaticBooleanMethodA,
2524 JNI::CallStaticByteMethod,
2525 JNI::CallStaticByteMethodV,
2526 JNI::CallStaticByteMethodA,
2527 JNI::CallStaticCharMethod,
2528 JNI::CallStaticCharMethodV,
2529 JNI::CallStaticCharMethodA,
2530 JNI::CallStaticShortMethod,
2531 JNI::CallStaticShortMethodV,
2532 JNI::CallStaticShortMethodA,
2533 JNI::CallStaticIntMethod,
2534 JNI::CallStaticIntMethodV,
2535 JNI::CallStaticIntMethodA,
2536 JNI::CallStaticLongMethod,
2537 JNI::CallStaticLongMethodV,
2538 JNI::CallStaticLongMethodA,
2539 JNI::CallStaticFloatMethod,
2540 JNI::CallStaticFloatMethodV,
2541 JNI::CallStaticFloatMethodA,
2542 JNI::CallStaticDoubleMethod,
2543 JNI::CallStaticDoubleMethodV,
2544 JNI::CallStaticDoubleMethodA,
2545 JNI::CallStaticVoidMethod,
2546 JNI::CallStaticVoidMethodV,
2547 JNI::CallStaticVoidMethodA,
2548 JNI::GetStaticFieldID,
2549 JNI::GetStaticObjectField,
2550 JNI::GetStaticBooleanField,
2551 JNI::GetStaticByteField,
2552 JNI::GetStaticCharField,
2553 JNI::GetStaticShortField,
2554 JNI::GetStaticIntField,
2555 JNI::GetStaticLongField,
2556 JNI::GetStaticFloatField,
2557 JNI::GetStaticDoubleField,
2558 JNI::SetStaticObjectField,
2559 JNI::SetStaticBooleanField,
2560 JNI::SetStaticByteField,
2561 JNI::SetStaticCharField,
2562 JNI::SetStaticShortField,
2563 JNI::SetStaticIntField,
2564 JNI::SetStaticLongField,
2565 JNI::SetStaticFloatField,
2566 JNI::SetStaticDoubleField,
2567 JNI::NewString,
2568 JNI::GetStringLength,
2569 JNI::GetStringChars,
2570 JNI::ReleaseStringChars,
2571 JNI::NewStringUTF,
2572 JNI::GetStringUTFLength,
2573 JNI::GetStringUTFChars,
2574 JNI::ReleaseStringUTFChars,
2575 JNI::GetArrayLength,
2576 JNI::NewObjectArray,
2577 JNI::GetObjectArrayElement,
2578 JNI::SetObjectArrayElement,
2579 JNI::NewBooleanArray,
2580 JNI::NewByteArray,
2581 JNI::NewCharArray,
2582 JNI::NewShortArray,
2583 JNI::NewIntArray,
2584 JNI::NewLongArray,
2585 JNI::NewFloatArray,
2586 JNI::NewDoubleArray,
2587 JNI::GetBooleanArrayElements,
2588 JNI::GetByteArrayElements,
2589 JNI::GetCharArrayElements,
2590 JNI::GetShortArrayElements,
2591 JNI::GetIntArrayElements,
2592 JNI::GetLongArrayElements,
2593 JNI::GetFloatArrayElements,
2594 JNI::GetDoubleArrayElements,
2595 JNI::ReleaseBooleanArrayElements,
2596 JNI::ReleaseByteArrayElements,
2597 JNI::ReleaseCharArrayElements,
2598 JNI::ReleaseShortArrayElements,
2599 JNI::ReleaseIntArrayElements,
2600 JNI::ReleaseLongArrayElements,
2601 JNI::ReleaseFloatArrayElements,
2602 JNI::ReleaseDoubleArrayElements,
2603 JNI::GetBooleanArrayRegion,
2604 JNI::GetByteArrayRegion,
2605 JNI::GetCharArrayRegion,
2606 JNI::GetShortArrayRegion,
2607 JNI::GetIntArrayRegion,
2608 JNI::GetLongArrayRegion,
2609 JNI::GetFloatArrayRegion,
2610 JNI::GetDoubleArrayRegion,
2611 JNI::SetBooleanArrayRegion,
2612 JNI::SetByteArrayRegion,
2613 JNI::SetCharArrayRegion,
2614 JNI::SetShortArrayRegion,
2615 JNI::SetIntArrayRegion,
2616 JNI::SetLongArrayRegion,
2617 JNI::SetFloatArrayRegion,
2618 JNI::SetDoubleArrayRegion,
2619 JNI::RegisterNatives,
2620 JNI::UnregisterNatives,
2621 JNI::MonitorEnter,
2622 JNI::MonitorExit,
2623 JNI::GetJavaVM,
2624 JNI::GetStringRegion,
2625 JNI::GetStringUTFRegion,
2626 JNI::GetPrimitiveArrayCritical,
2627 JNI::ReleasePrimitiveArrayCritical,
2628 JNI::GetStringCritical,
2629 JNI::ReleaseStringCritical,
2630 JNI::NewWeakGlobalRef,
2631 JNI::DeleteWeakGlobalRef,
2632 JNI::ExceptionCheck,
2633 JNI::NewDirectByteBuffer,
2634 JNI::GetDirectBufferAddress,
2635 JNI::GetDirectBufferCapacity,
2636 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002637};
2638
Elliott Hughes75770752011-08-24 17:52:38 -07002639JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002640 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002641 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002642 local_ref_cookie(IRT_FIRST_SEGMENT),
2643 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002644 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002645 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002646 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002647 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002648 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002649 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002650 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002651 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2652 // errors will ensue.
2653 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2654 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002655}
2656
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002657JNIEnvExt::~JNIEnvExt() {
2658}
2659
Elliott Hughes88c5c352012-03-15 18:49:48 -07002660void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2661 check_jni = enabled;
2662 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002663}
2664
Elliott Hughes73e66f72012-05-09 09:34:45 -07002665void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2666 locals.Dump(os);
2667 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002668}
2669
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002670void JNIEnvExt::PushFrame(int /*capacity*/) {
2671 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002672 stacked_local_ref_cookies.push_back(local_ref_cookie);
2673 local_ref_cookie = locals.GetSegmentState();
2674}
2675
2676void JNIEnvExt::PopFrame() {
2677 locals.SetSegmentState(local_ref_cookie);
2678 local_ref_cookie = stacked_local_ref_cookies.back();
2679 stacked_local_ref_cookies.pop_back();
2680}
2681
Carl Shapiroea4dca82011-08-01 13:45:38 -07002682// JNI Invocation interface.
2683
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002684extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2685 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2686 if (args->version < JNI_VERSION_1_2) {
2687 return JNI_EVERSION;
2688 }
2689 Runtime::Options options;
2690 for (int i = 0; i < args->nOptions; ++i) {
2691 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002692 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002693 }
2694 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002695 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002696 if (runtime == NULL) {
2697 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002698 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002699 runtime->Start();
2700 *p_env = Thread::Current()->GetJniEnv();
2701 *p_vm = runtime->GetJavaVM();
2702 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002703}
2704
Elliott Hughesf2682d52011-08-15 16:37:04 -07002705extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002706 Runtime* runtime = Runtime::Current();
2707 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002708 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002709 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002710 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002711 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002712 }
2713 return JNI_OK;
2714}
2715
2716// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002717extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002718 return JNI_ERR;
2719}
2720
Elliott Hughescdf53122011-08-19 15:46:09 -07002721class JII {
2722 public:
2723 static jint DestroyJavaVM(JavaVM* vm) {
2724 if (vm == NULL) {
2725 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002726 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002727 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2728 delete raw_vm->runtime;
2729 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002730 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002731
Elliott Hughescdf53122011-08-19 15:46:09 -07002732 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002733 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002734 }
2735
2736 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002737 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002738 }
2739
2740 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002741 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002742 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002743 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002744 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2745 Runtime* runtime = raw_vm->runtime;
2746 runtime->DetachCurrentThread();
2747 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002748 }
2749
2750 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2751 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2752 return JNI_EVERSION;
2753 }
2754 if (vm == NULL || env == NULL) {
2755 return JNI_ERR;
2756 }
2757 Thread* thread = Thread::Current();
2758 if (thread == NULL) {
2759 *env = NULL;
2760 return JNI_EDETACHED;
2761 }
2762 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002763 return JNI_OK;
2764 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002765};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002766
Elliott Hughes88c5c352012-03-15 18:49:48 -07002767const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002768 NULL, // reserved0
2769 NULL, // reserved1
2770 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002771 JII::DestroyJavaVM,
2772 JII::AttachCurrentThread,
2773 JII::DetachCurrentThread,
2774 JII::GetEnv,
2775 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002776};
2777
Elliott Hughesa0957642011-09-02 14:27:33 -07002778JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002779 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002780 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002781 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002782 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002783 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002784 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002785 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002786 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002787 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002788 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002789 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002790 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002791 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002792 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002793 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002794 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002795 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002796 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002797 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002798}
2799
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002800JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002801 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002802}
2803
Elliott Hughes88c5c352012-03-15 18:49:48 -07002804void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2805 check_jni = enabled;
2806 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002807}
2808
Elliott Hughesae80b492012-04-24 10:43:17 -07002809void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2810 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2811 if (force_copy) {
2812 os << " (with forcecopy)";
2813 }
2814 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
2815 {
2816 MutexLock mu(pins_lock);
2817 os << "; pins=" << pin_table.Size();
2818 }
2819 {
2820 MutexLock mu(globals_lock);
2821 os << "; globals=" << globals.Capacity();
2822 }
2823 {
2824 MutexLock mu(weak_globals_lock);
2825 if (weak_globals.Capacity() > 0) {
2826 os << " (plus " << weak_globals.Capacity() << " weak)";
2827 }
2828 }
2829 os << '\n';
2830
2831 {
2832 MutexLock mu(libraries_lock);
2833 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2834 }
2835}
2836
Elliott Hughes73e66f72012-05-09 09:34:45 -07002837void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002838 {
2839 MutexLock mu(globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002840 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002841 }
2842 {
2843 MutexLock mu(weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002844 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002845 }
2846 {
2847 MutexLock mu(pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002848 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002849 }
2850}
2851
Elliott Hughes75770752011-08-24 17:52:38 -07002852bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2853 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002854
2855 // See if we've already loaded this library. If we have, and the class loader
2856 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002857 // TODO: for better results we should canonicalize the pathname (or even compare
2858 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002859 SharedLibrary* library;
2860 {
2861 // TODO: move the locking (and more of this logic) into Libraries.
2862 MutexLock mu(libraries_lock);
2863 library = libraries->Get(path);
2864 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002865 if (library != NULL) {
2866 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002867 // The library will be associated with class_loader. The JNI
2868 // spec says we can't load the same library into more than one
2869 // class loader.
2870 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2871 "ClassLoader %p; can't open in ClassLoader %p",
2872 path.c_str(), library->GetClassLoader(), class_loader);
2873 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002874 return false;
2875 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002876 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2877 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002878 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002879 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2880 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002881 return false;
2882 }
2883 return true;
2884 }
2885
2886 // Open the shared library. Because we're using a full path, the system
2887 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2888 // resolve this library's dependencies though.)
2889
2890 // Failures here are expected when java.library.path has several entries
2891 // and we have to hunt for the lib.
2892
2893 // The current version of the dynamic linker prints detailed information
2894 // about dlopen() failures. Some things to check if the message is
2895 // cryptic:
2896 // - make sure the library exists on the device
2897 // - verify that the right path is being opened (the debug log message
2898 // above can help with that)
2899 // - check to see if the library is valid (e.g. not zero bytes long)
2900 // - check config/prelink-linux-arm.map to ensure that the library
2901 // is listed and is not being overrun by the previous entry (if
2902 // loading suddenly stops working on a prelinked library, this is
2903 // a good one to check)
2904 // - write a trivial app that calls sleep() then dlopen(), attach
2905 // to it with "strace -p <pid>" while it sleeps, and watch for
2906 // attempts to open nonexistent dependent shared libs
2907
2908 // TODO: automate some of these checks!
2909
2910 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002911 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002912 // the GC to ignore us.
2913 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002914 void* handle = NULL;
2915 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002916 ScopedThreadStateChange tsc(self, kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002917 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002918 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002919
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002920 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002921
2922 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002923 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002924 return false;
2925 }
2926
2927 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002928 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002929 // TODO: move the locking (and more of this logic) into Libraries.
2930 MutexLock mu(libraries_lock);
2931 library = libraries->Get(path);
2932 if (library != NULL) {
2933 LOG(INFO) << "WOW: we lost a race to add shared library: "
2934 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002935 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002936 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002937 library = new SharedLibrary(path, handle, class_loader);
2938 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002939 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002940
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002941 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002942
2943 bool result = true;
2944 void* sym = dlsym(handle, "JNI_OnLoad");
2945 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002946 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002947 } else {
2948 // Call JNI_OnLoad. We have to override the current class
2949 // loader, which will always be "null" since the stuff at the
2950 // top of the stack is around Runtime.loadLibrary(). (See
2951 // the comments in the JNI FindClass function.)
2952 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2953 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002954 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002955 self->SetClassLoaderOverride(class_loader);
2956
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002957 int version = 0;
2958 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002959 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002960 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002961 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002962 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002963
Brian Carlstromaded5f72011-10-07 17:15:04 -07002964 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002965
2966 if (version != JNI_VERSION_1_2 &&
2967 version != JNI_VERSION_1_4 &&
2968 version != JNI_VERSION_1_6) {
2969 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2970 << "bad version: " << version;
2971 // It's unwise to call dlclose() here, but we can mark it
2972 // as bad and ensure that future load attempts will fail.
2973 // We don't know how far JNI_OnLoad got, so there could
2974 // be some partially-initialized stuff accessible through
2975 // newly-registered native method calls. We could try to
2976 // unregister them, but that doesn't seem worthwhile.
2977 result = false;
2978 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002979 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2980 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002981 }
2982 }
2983
2984 library->SetResult(result);
2985 return result;
2986}
2987
2988void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2989 CHECK(m->IsNative());
2990
2991 Class* c = m->GetDeclaringClass();
2992
2993 // If this is a static method, it could be called before the class
2994 // has been initialized.
2995 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002996 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002997 return NULL;
2998 }
2999 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07003000 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003001 }
3002
Brian Carlstrom16192862011-09-12 17:50:06 -07003003 std::string detail;
3004 void* native_method;
3005 {
3006 MutexLock mu(libraries_lock);
3007 native_method = libraries->FindNativeMethod(m, detail);
3008 }
3009 // throwing can cause libraries_lock to be reacquired
3010 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07003011 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003012 }
3013 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003014}
3015
Elliott Hughes410c0c82011-09-01 17:58:25 -07003016void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
3017 {
3018 MutexLock mu(globals_lock);
3019 globals.VisitRoots(visitor, arg);
3020 }
3021 {
3022 MutexLock mu(pins_lock);
3023 pin_table.VisitRoots(visitor, arg);
3024 }
3025 // The weak_globals table is visited by the GC itself (because it mutates the table).
3026}
3027
Ian Rogersdf20fe02011-07-20 20:34:16 -07003028} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003029
3030std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3031 switch (rhs) {
3032 case JNIInvalidRefType:
3033 os << "JNIInvalidRefType";
3034 return os;
3035 case JNILocalRefType:
3036 os << "JNILocalRefType";
3037 return os;
3038 case JNIGlobalRefType:
3039 os << "JNIGlobalRefType";
3040 return os;
3041 case JNIWeakGlobalRefType:
3042 os << "JNIWeakGlobalRefType";
3043 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003044 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003045 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003046 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003047 }
3048}