blob: dbee18bd36ff1e765adc49f780587ec79e482e8e [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -070020#include <sys/mman.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070021
22#include <cstdarg>
23#include <map>
Elliott Hughes0af55432011-08-17 18:37:28 -070024#include <utility>
25#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070026
Elliott Hughes72025e52011-08-23 17:50:30 -070027#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070028#include "UniquePtr.h"
Elliott Hughes18c07532011-08-18 15:50:51 -070029#include "assembler.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070030#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070032#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "logging.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070034#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080035#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070036#include "runtime.h"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070037#include "scoped_jni_thread_state.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070038#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070039#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070040#include "thread.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070041
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070042namespace art {
43
Elliott Hughes2ced6a52011-10-16 18:44:48 -070044static const size_t kMonitorsInitial = 32; // Arbitrary.
45static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
46
47static const size_t kLocalsInitial = 64; // Arbitrary.
48static const size_t kLocalsMax = 512; // Arbitrary sanity check.
49
50static const size_t kPinTableInitial = 16; // Arbitrary.
51static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
52
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053static size_t gGlobalsInitial = 512; // Arbitrary.
54static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070055
56static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
57static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
58
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070059void SetJniGlobalsMax(size_t max) {
60 if (max != 0) {
61 gGlobalsMax = max;
62 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
63 }
64}
Ian Rogersdf20fe02011-07-20 20:34:16 -070065
Elliott Hughesc5f7c912011-08-18 14:00:42 -070066/*
67 * Add a local reference for an object to the current stack frame. When
68 * the native function returns, the reference will be discarded.
69 *
70 * We need to allow the same reference to be added multiple times.
71 *
72 * This will be called on otherwise unreferenced objects. We cannot do
73 * GC allocations here, and it's best if we don't grab a mutex.
74 *
75 * Returns the local reference (currently just the same pointer that was
76 * passed in), or NULL on failure.
77 */
Elliott Hughes8a26c5c2011-08-15 18:35:43 -070078template<typename T>
Elliott Hughescf4c6c42011-09-01 15:16:42 -070079T AddLocalReference(JNIEnv* public_env, const Object* const_obj) {
80 // The jobject type hierarchy has no notion of const, so it's not worth carrying through.
81 Object* obj = const_cast<Object*>(const_obj);
82
Elliott Hughesc5f7c912011-08-18 14:00:42 -070083 if (obj == NULL) {
84 return NULL;
85 }
86
Elliott Hughesbf86d042011-08-31 17:53:14 -070087 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
88 IndirectReferenceTable& locals = env->locals;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070089
Ian Rogers5a7a74a2011-09-26 16:32:29 -070090 uint32_t cookie = env->local_ref_cookie;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070091 IndirectRef ref = locals.Add(cookie, obj);
92 if (ref == NULL) {
93 // TODO: just change Add's DCHECK to CHECK and lose this?
94 locals.Dump();
95 LOG(FATAL) << "Failed adding to JNI local reference table "
96 << "(has " << locals.Capacity() << " entries)";
Elliott Hughesc5f7c912011-08-18 14:00:42 -070097 }
98
99#if 0 // TODO: fix this to understand PushLocalFrame, so we can turn it on.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700100 if (env->check_jni) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700101 size_t entry_count = locals.Capacity();
102 if (entry_count > 16) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700103 LOG(WARNING) << "Warning: more than 16 JNI local references: "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700104 << entry_count << " (most recent was a " << PrettyTypeOf(obj) << ")";
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700105 locals.Dump();
Elliott Hughes82188472011-11-07 18:11:48 -0800106 // TODO: LOG(FATAL) instead.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700107 }
108 }
109#endif
110
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800111 if (env->vm->work_around_app_jni_bugs) {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700112 // Hand out direct pointers to support broken old apps.
113 return reinterpret_cast<T>(obj);
114 }
115
116 return reinterpret_cast<T>(ref);
117}
Brian Carlstrom51477332012-03-25 20:20:26 -0700118// Explicit instantiations
119template jclass AddLocalReference<jclass>(JNIEnv* public_env, const Object* const_obj);
120template jobject AddLocalReference<jobject>(JNIEnv* public_env, const Object* const_obj);
121template jobjectArray AddLocalReference<jobjectArray>(JNIEnv* public_env, const Object* const_obj);
122template jstring AddLocalReference<jstring>(JNIEnv* public_env, const Object* const_obj);
123template jthrowable AddLocalReference<jthrowable>(JNIEnv* public_env, const Object* const_obj);
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700124
Elliott Hughesbf86d042011-08-31 17:53:14 -0700125// For external use.
126template<typename T>
127T Decode(JNIEnv* public_env, jobject obj) {
128 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
129 return reinterpret_cast<T>(env->self->DecodeJObject(obj));
130}
Shih-wei Liao24782c62012-01-08 12:46:11 -0800131// TODO: Change to use template when Mac OS build server no longer uses GCC 4.2.*.
132Object* DecodeObj(JNIEnv* public_env, jobject obj) {
133 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
134 return reinterpret_cast<Object*>(env->self->DecodeJObject(obj));
135}
Elliott Hughesbf86d042011-08-31 17:53:14 -0700136// Explicit instantiations.
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700137template Array* Decode<Array*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700138template Class* Decode<Class*>(JNIEnv*, jobject);
139template ClassLoader* Decode<ClassLoader*>(JNIEnv*, jobject);
140template Object* Decode<Object*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700141template ObjectArray<Class>* Decode<ObjectArray<Class>*>(JNIEnv*, jobject);
Ian Rogers466bb252011-10-14 03:29:56 -0700142template ObjectArray<ObjectArray<Class> >* Decode<ObjectArray<ObjectArray<Class> >*>(JNIEnv*, jobject);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700143template ObjectArray<Object>* Decode<ObjectArray<Object>*>(JNIEnv*, jobject);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700144template ObjectArray<StackTraceElement>* Decode<ObjectArray<StackTraceElement>*>(JNIEnv*, jobject);
Jesse Wilson95caa792011-10-12 18:14:17 -0400145template ObjectArray<Method>* Decode<ObjectArray<Method>*>(JNIEnv*, jobject);
Elliott Hughes726079d2011-10-07 18:43:44 -0700146template String* Decode<String*>(JNIEnv*, jobject);
147template Throwable* Decode<Throwable*>(JNIEnv*, jobject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700148
Ian Rogers45619fc2012-02-29 11:15:25 -0800149size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
150 size_t num_bytes = 0;
151 for (size_t i = 1; i < shorty_len; ++i) {
152 char ch = shorty[i];
153 if (ch == 'D' || ch == 'J') {
154 num_bytes += 8;
155 } else if (ch == 'L') {
156 // Argument is a reference or an array. The shorty descriptor
157 // does not distinguish between these types.
158 num_bytes += sizeof(Object*);
159 } else {
160 num_bytes += 4;
161 }
162 }
163 return num_bytes;
164}
165
166class ArgArray {
167 public:
168 explicit ArgArray(Method* method) {
169 MethodHelper mh(method);
170 shorty_ = mh.GetShorty();
171 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -0700172 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800173 arg_array_ = small_arg_array_;
174 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700175 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800176 arg_array_ = large_arg_array_.get();
177 }
178 }
179
Elliott Hughes77405792012-03-15 15:22:12 -0700180 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800181 return arg_array_;
182 }
183
184 void BuildArgArray(JNIEnv* public_env, va_list ap) {
185 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700186 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800187 switch (shorty_[i]) {
188 case 'Z':
189 case 'B':
190 case 'C':
191 case 'S':
192 case 'I':
Elliott Hughes77405792012-03-15 15:22:12 -0700193 arg_array_[offset].i = va_arg(ap, jint);
Ian Rogers45619fc2012-02-29 11:15:25 -0800194 break;
195 case 'F':
Elliott Hughes77405792012-03-15 15:22:12 -0700196 arg_array_[offset].f = va_arg(ap, jdouble);
Ian Rogers45619fc2012-02-29 11:15:25 -0800197 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700198 case 'L':
199 arg_array_[offset].l = DecodeObj(env, va_arg(ap, jobject));
Ian Rogers45619fc2012-02-29 11:15:25 -0800200 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800201 case 'D':
Elliott Hughes77405792012-03-15 15:22:12 -0700202 arg_array_[offset].d = va_arg(ap, jdouble);
Ian Rogers45619fc2012-02-29 11:15:25 -0800203 break;
204 case 'J':
Elliott Hughes77405792012-03-15 15:22:12 -0700205 arg_array_[offset].j = va_arg(ap, jlong);
Ian Rogers45619fc2012-02-29 11:15:25 -0800206 break;
207 }
208 }
209 }
210
211 void BuildArgArray(JNIEnv* public_env, jvalue* args) {
212 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughes77405792012-03-15 15:22:12 -0700213 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800214 switch (shorty_[i]) {
215 case 'Z':
216 case 'B':
217 case 'C':
218 case 'S':
219 case 'I':
Elliott Hughes77405792012-03-15 15:22:12 -0700220 arg_array_[offset].i = args[offset].i;
Ian Rogers45619fc2012-02-29 11:15:25 -0800221 break;
222 case 'F':
Elliott Hughes77405792012-03-15 15:22:12 -0700223 arg_array_[offset].f = args[offset].f;
Ian Rogers45619fc2012-02-29 11:15:25 -0800224 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700225 case 'L':
226 arg_array_[offset].l = DecodeObj(env, args[offset].l);
Ian Rogers45619fc2012-02-29 11:15:25 -0800227 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800228 case 'D':
Elliott Hughes77405792012-03-15 15:22:12 -0700229 arg_array_[offset].d = args[offset].d;
Ian Rogers45619fc2012-02-29 11:15:25 -0800230 break;
231 case 'J':
Elliott Hughes77405792012-03-15 15:22:12 -0700232 arg_array_[offset].j = args[offset].j;
Ian Rogers45619fc2012-02-29 11:15:25 -0800233 break;
234 }
235 }
236 }
237
Ian Rogers45619fc2012-02-29 11:15:25 -0800238 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700239 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800240 const char* shorty_;
241 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700242 JValue* arg_array_;
243 JValue small_arg_array_[kSmallArgArraySize];
244 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800245};
246
Elliott Hughes0512f022012-03-15 22:10:52 -0700247static jweak AddWeakGlobalReference(ScopedJniThreadState& ts, Object* obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700248 if (obj == NULL) {
249 return NULL;
250 }
Elliott Hughes75770752011-08-24 17:52:38 -0700251 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700252 IndirectReferenceTable& weak_globals = vm->weak_globals;
253 MutexLock mu(vm->weak_globals_lock);
254 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
255 return reinterpret_cast<jweak>(ref);
256}
257
Elliott Hughesbf86d042011-08-31 17:53:14 -0700258// For internal use.
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700259template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -0700260static T Decode(ScopedJniThreadState& ts, jobject obj) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700261 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700262}
263
Elliott Hughes77405792012-03-15 15:22:12 -0700264static JValue InvokeWithArgArray(JNIEnv* public_env, Object* receiver, Method* method, JValue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700265 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Elliott Hughesdbac3092012-03-16 18:00:30 -0700266 JValue result = { 0 };
Elliott Hughes418d20f2011-09-22 14:00:39 -0700267 method->Invoke(env->self, receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700268 return result;
269}
270
Ian Rogers0571d352011-11-03 19:51:38 -0700271static JValue InvokeWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700272 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800273 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700274 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800275 ArgArray arg_array(method);
276 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700277 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700278}
279
Ian Rogers0571d352011-11-03 19:51:38 -0700280static Method* FindVirtualMethod(Object* receiver, Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700281 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700282}
283
Ian Rogers0571d352011-11-03 19:51:38 -0700284static JValue InvokeVirtualOrInterfaceWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid,
285 jvalue* args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700286 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800287 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700288 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800289 ArgArray arg_array(method);
290 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700291 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700292}
293
Ian Rogers0571d352011-11-03 19:51:38 -0700294static JValue InvokeVirtualOrInterfaceWithVarArgs(JNIEnv* public_env, jobject obj, jmethodID mid,
295 va_list args) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700296 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800297 Object* receiver = DecodeObj(env, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700298 Method* method = FindVirtualMethod(receiver, DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800299 ArgArray arg_array(method);
300 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700301 return InvokeWithArgArray(env, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700302}
303
Elliott Hughes6b436852011-08-12 10:16:44 -0700304// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
305// separated with slashes but aren't wrapped with "L;" like regular descriptors
306// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
307// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
308// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700309static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700310 std::string result;
311 // Add the missing "L;" if necessary.
312 if (name[0] == '[') {
313 result = name;
314 } else {
315 result += 'L';
316 result += name;
317 result += ';';
318 }
319 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700320 if (result.find('.') != std::string::npos) {
321 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
322 << "\"" << name << "\"";
323 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700324 }
325 return result;
326}
327
Ian Rogers0571d352011-11-03 19:51:38 -0700328static void ThrowNoSuchMethodError(ScopedJniThreadState& ts, Class* c, const char* name, const char* sig, const char* kind) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700329 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800330 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700331}
332
Ian Rogers0571d352011-11-03 19:51:38 -0700333static jmethodID FindMethodID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700334 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700335 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700336 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700337 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700338
339 Method* method = NULL;
340 if (is_static) {
341 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700342 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700343 method = c->FindVirtualMethod(name, sig);
344 if (method == NULL) {
345 // No virtual method matching the signature. Search declared
346 // private methods and constructors.
347 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700348 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700349 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700350
Elliott Hughescdf53122011-08-19 15:46:09 -0700351 if (method == NULL || method->IsStatic() != is_static) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700352 ThrowNoSuchMethodError(ts, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700353 return NULL;
354 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700355
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700356 return EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700357}
358
Ian Rogers0571d352011-11-03 19:51:38 -0700359static const ClassLoader* GetClassLoader(Thread* self) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700360 Frame frame = self->GetTopOfStack();
361 Method* method = frame.GetMethod();
362 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
363 return self->GetClassLoaderOverride();
364 }
365 return method->GetDeclaringClass()->GetClassLoader();
366}
367
Ian Rogers0571d352011-11-03 19:51:38 -0700368static jfieldID FindFieldID(ScopedJniThreadState& ts, jclass jni_class, const char* name, const char* sig, bool is_static) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700369 Class* c = Decode<Class*>(ts, jni_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700370 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700371 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700372 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700373
374 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700375 Class* field_type;
376 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
377 if (sig[1] != '\0') {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700378 const ClassLoader* cl = GetClassLoader(ts.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700379 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700380 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700382 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383 if (field_type == NULL) {
384 // Failed to find type from the signature of the field.
Ian Rogersb17d08b2011-09-02 16:16:49 -0700385 DCHECK(ts.Self()->IsExceptionPending());
386 ts.Self()->ClearException();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700387 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700388 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800389 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700390 return NULL;
391 }
392 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800393 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700394 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800395 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700396 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700397 if (field == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700398 ts.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700399 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800400 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700401 return NULL;
402 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700403 return EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700404}
405
Ian Rogers0571d352011-11-03 19:51:38 -0700406static void PinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700407 JavaVMExt* vm = ts.Vm();
408 MutexLock mu(vm->pins_lock);
409 vm->pin_table.Add(array);
410}
411
Ian Rogers0571d352011-11-03 19:51:38 -0700412static void UnpinPrimitiveArray(ScopedJniThreadState& ts, const Array* array) {
Elliott Hughes75770752011-08-24 17:52:38 -0700413 JavaVMExt* vm = ts.Vm();
414 MutexLock mu(vm->pins_lock);
415 vm->pin_table.Remove(array);
416}
417
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700418template<typename JniT, typename ArtT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700419static JniT NewPrimitiveArray(ScopedJniThreadState& ts, jsize length) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700420 CHECK_GE(length, 0); // TODO: ReportJniError
421 ArtT* result = ArtT::Alloc(length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700422 return AddLocalReference<JniT>(ts.Env(), result);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700423}
424
Elliott Hughes75770752011-08-24 17:52:38 -0700425template <typename ArrayT, typename CArrayT, typename ArtArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700426static CArrayT GetPrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -0700427 ArtArrayT* array = Decode<ArtArrayT*>(ts, java_array);
428 PinPrimitiveArray(ts, array);
429 if (is_copy != NULL) {
430 *is_copy = JNI_FALSE;
431 }
432 return array->GetData();
433}
434
435template <typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700436static void ReleasePrimitiveArray(ScopedJniThreadState& ts, ArrayT java_array, jint mode) {
Elliott Hughes75770752011-08-24 17:52:38 -0700437 if (mode != JNI_COMMIT) {
438 Array* array = Decode<Array*>(ts, java_array);
439 UnpinPrimitiveArray(ts, array);
440 }
441}
442
Ian Rogers0571d352011-11-03 19:51:38 -0700443static void ThrowAIOOBE(ScopedJniThreadState& ts, Array* array, jsize start, jsize length, const char* identifier) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700444 std::string type(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700445 ts.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700446 "%s offset=%d length=%d %s.length=%d",
447 type.c_str(), start, length, identifier, array->GetLength());
448}
Ian Rogers0571d352011-11-03 19:51:38 -0700449
450static void ThrowSIOOBE(ScopedJniThreadState& ts, jsize start, jsize length, jsize array_length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700451 ts.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700452 "offset=%d length=%d string.length()=%d", start, length, array_length);
453}
Elliott Hughes814e4032011-08-23 12:07:56 -0700454
455template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700456static void GetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700457 ArrayT* array = Decode<ArrayT*>(ts, java_array);
458 if (start < 0 || length < 0 || start + length > array->GetLength()) {
459 ThrowAIOOBE(ts, array, start, length, "src");
460 } else {
461 JavaT* data = array->GetData();
462 memcpy(buf, data + start, length * sizeof(JavaT));
463 }
464}
465
466template <typename JavaArrayT, typename JavaT, typename ArrayT>
Elliott Hughes0512f022012-03-15 22:10:52 -0700467static void SetPrimitiveArrayRegion(ScopedJniThreadState& ts, JavaArrayT java_array, jsize start, jsize length, const JavaT* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700468 ArrayT* array = Decode<ArrayT*>(ts, java_array);
469 if (start < 0 || length < 0 || start + length > array->GetLength()) {
470 ThrowAIOOBE(ts, array, start, length, "dst");
471 } else {
472 JavaT* data = array->GetData();
473 memcpy(data + start, buf, length * sizeof(JavaT));
474 }
475}
476
Ian Rogers0571d352011-11-03 19:51:38 -0700477static jclass InitDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700478 ScopedLocalRef<jclass> buffer_class(env, env->FindClass("java/nio/ReadWriteDirectByteBuffer"));
479 CHECK(buffer_class.get() != NULL);
480 return reinterpret_cast<jclass>(env->NewGlobalRef(buffer_class.get()));
481}
482
Ian Rogers0571d352011-11-03 19:51:38 -0700483static jclass GetDirectByteBufferClass(JNIEnv* env) {
Elliott Hughesb465ab02011-08-24 11:21:21 -0700484 static jclass buffer_class = InitDirectByteBufferClass(env);
485 return buffer_class;
486}
487
Ian Rogers0571d352011-11-03 19:51:38 -0700488static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700489 if (vm == NULL || p_env == NULL) {
490 return JNI_ERR;
491 }
492
Elliott Hughescac6cc72011-11-03 20:31:21 -0700493 // Return immediately if we're already one with the VM.
494 Thread* self = Thread::Current();
495 if (self != NULL) {
496 *p_env = self->GetJniEnv();
497 return JNI_OK;
498 }
499
500 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
501
502 // No threads allowed in zygote mode.
503 if (runtime->IsZygote()) {
504 LOG(ERROR) << "Attempt to attach a thread in the zygote";
505 return JNI_ERR;
506 }
507
Elliott Hughes75770752011-08-24 17:52:38 -0700508 JavaVMAttachArgs* in_args = static_cast<JavaVMAttachArgs*>(thr_args);
509 JavaVMAttachArgs args;
510 if (thr_args == NULL) {
511 // Allow the v1.1 calling convention.
512 args.version = JNI_VERSION_1_2;
513 args.name = NULL;
514 args.group = NULL; // TODO: get "main" thread group
515 } else {
516 args.version = in_args->version;
517 args.name = in_args->name;
518 if (in_args->group != NULL) {
519 UNIMPLEMENTED(WARNING) << "thr_args->group != NULL";
520 args.group = NULL; // TODO: decode in_args->group
521 } else {
522 args.group = NULL; // TODO: get "main" thread group
523 }
524 }
525 CHECK_GE(args.version, JNI_VERSION_1_2);
526
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700527 runtime->AttachCurrentThread(args.name, as_daemon);
528 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700529 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700530}
531
Elliott Hughes79082e32011-08-25 12:07:32 -0700532class SharedLibrary {
533 public:
534 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
535 : path_(path),
536 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700537 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700538 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700539 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700540 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700541 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700542 }
543
Elliott Hughes79082e32011-08-25 12:07:32 -0700544 Object* GetClassLoader() {
545 return class_loader_;
546 }
547
548 std::string GetPath() {
549 return path_;
550 }
551
552 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700553 * Check the result of an earlier call to JNI_OnLoad on this library.
554 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700555 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700556 bool CheckOnLoadResult() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700557 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700558 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700559 // Check this so we don't end up waiting for ourselves. We need
560 // to return "true" so the caller can continue.
561 LOG(INFO) << *self << " recursive attempt to load library "
562 << "\"" << path_ << "\"";
563 return true;
564 }
565
566 MutexLock mu(jni_on_load_lock_);
567 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800568 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
569 << "JNI_OnLoad...]";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700570 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700571 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700572 }
573
574 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800575 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
576 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700577 return okay;
578 }
579
580 void SetResult(bool result) {
581 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700582 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700583
584 // Broadcast a wakeup to anybody sleeping on the condition variable.
585 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700586 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700587 }
588
589 void* FindSymbol(const std::string& symbol_name) {
590 return dlsym(handle_, symbol_name.c_str());
591 }
592
593 private:
594 enum JNI_OnLoadState {
595 kPending,
596 kFailed,
597 kOkay,
598 };
599
600 // Path to library "/system/lib/libjni.so".
601 std::string path_;
602
603 // The void* returned by dlopen(3).
604 void* handle_;
605
606 // The ClassLoader this library is associated with.
607 Object* class_loader_;
608
609 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700610 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700611 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700612 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700613 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700614 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700615 // Result of earlier JNI_OnLoad call.
616 JNI_OnLoadState jni_on_load_result_;
617};
618
Elliott Hughes79082e32011-08-25 12:07:32 -0700619// This exists mainly to keep implementation details out of the header file.
620class Libraries {
621 public:
622 Libraries() {
623 }
624
625 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700626 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700627 }
628
629 SharedLibrary* Get(const std::string& path) {
630 return libraries_[path];
631 }
632
633 void Put(const std::string& path, SharedLibrary* library) {
634 libraries_[path] = library;
635 }
636
637 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700638 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700639 std::string jni_short_name(JniShortName(m));
640 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700641 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700642 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
643 SharedLibrary* library = it->second;
644 if (library->GetClassLoader() != declaring_class_loader) {
645 // We only search libraries loaded by the appropriate ClassLoader.
646 continue;
647 }
648 // Try the short name then the long name...
649 void* fn = library->FindSymbol(jni_short_name);
650 if (fn == NULL) {
651 fn = library->FindSymbol(jni_long_name);
652 }
653 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800654 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
655 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700656 return fn;
657 }
658 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700659 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700660 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700661 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700662 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700663 return NULL;
664 }
665
666 private:
667 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
668
669 std::map<std::string, SharedLibrary*> libraries_;
670};
671
Elliott Hughes418d20f2011-09-22 14:00:39 -0700672JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
673 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
674 Object* receiver = Decode<Object*>(env, obj);
675 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800676 ArgArray arg_array(method);
677 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700678 return InvokeWithArgArray(env, receiver, method, arg_array.get());
679}
680
Elliott Hughesd07986f2011-12-06 18:27:45 -0800681JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700682 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800683}
684
Elliott Hughescdf53122011-08-19 15:46:09 -0700685class JNI {
686 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700687
Elliott Hughescdf53122011-08-19 15:46:09 -0700688 static jint GetVersion(JNIEnv* env) {
689 ScopedJniThreadState ts(env);
690 return JNI_VERSION_1_6;
691 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700692
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
694 ScopedJniThreadState ts(env);
695 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700696 return NULL;
697 }
698
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 static jclass FindClass(JNIEnv* env, const char* name) {
700 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700701 Runtime* runtime = Runtime::Current();
702 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700703 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700704 Class* c = NULL;
705 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700706 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800707 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700708 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800709 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700710 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700711 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700712 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700713
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
715 ScopedJniThreadState ts(env);
716 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700717 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700718 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700719
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
721 ScopedJniThreadState ts(env);
722 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700723 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700724 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700725
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
727 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700728 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700729 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700731
Elliott Hughescdf53122011-08-19 15:46:09 -0700732 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
733 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700734 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700735 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700737
Elliott Hughes37f7a402011-08-22 18:56:01 -0700738 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
739 ScopedJniThreadState ts(env);
740 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700741 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700742 }
743
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700744 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700746 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700747 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700749
Elliott Hughes37f7a402011-08-22 18:56:01 -0700750 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700752 Class* c1 = Decode<Class*>(ts, java_class1);
753 Class* c2 = Decode<Class*>(ts, java_class2);
754 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700755 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700756
Elliott Hughese84278b2012-03-22 10:06:53 -0700757 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700759 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700760 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700761 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700762 return JNI_TRUE;
763 } else {
764 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700765 Class* c = Decode<Class*>(ts, java_class);
766 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700767 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700768 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700769
Elliott Hughes37f7a402011-08-22 18:56:01 -0700770 static jint Throw(JNIEnv* env, jthrowable java_exception) {
771 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700772 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700773 if (exception == NULL) {
774 return JNI_ERR;
775 }
776 ts.Self()->SetException(exception);
777 return JNI_OK;
778 }
779
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700780 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700781 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700782 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700783 jmethodID mid = ((msg != NULL)
784 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
785 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700786 if (mid == NULL) {
787 return JNI_ERR;
788 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700789 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700790 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700791 return JNI_ERR;
792 }
793
794 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700795 args[0].l = s.get();
796 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
797 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700798 return JNI_ERR;
799 }
800
Elliott Hughes72025e52011-08-23 17:50:30 -0700801 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700802
Elliott Hughes37f7a402011-08-22 18:56:01 -0700803 return JNI_OK;
804 }
805
806 static jboolean ExceptionCheck(JNIEnv* env) {
807 ScopedJniThreadState ts(env);
808 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
809 }
810
811 static void ExceptionClear(JNIEnv* env) {
812 ScopedJniThreadState ts(env);
813 ts.Self()->ClearException();
814 }
815
816 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700818
819 Thread* self = ts.Self();
820 Throwable* original_exception = self->GetException();
821 self->ClearException();
822
Elliott Hughesbf86d042011-08-31 17:53:14 -0700823 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700824 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
825 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
826 if (mid == NULL) {
827 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700828 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700829 } else {
830 env->CallVoidMethod(exception.get(), mid);
831 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700832 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700833 << " thrown while calling printStackTrace";
834 self->ClearException();
835 }
836 }
837
838 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700840
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 static jthrowable ExceptionOccurred(JNIEnv* env) {
842 ScopedJniThreadState ts(env);
843 Object* exception = ts.Self()->GetException();
Elliott Hughes81ff3182012-03-23 20:35:56 -0700844 return (exception != NULL) ? AddLocalReference<jthrowable>(env, exception) : NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700845 }
846
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 static void FatalError(JNIEnv* env, const char* msg) {
848 ScopedJniThreadState ts(env);
849 LOG(FATAL) << "JNI FatalError called: " << msg;
850 }
851
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700852 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700853 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700854 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
855 return JNI_ERR;
856 }
857 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700858 return JNI_OK;
859 }
860
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700861 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700863 Object* survivor = Decode<Object*>(ts, java_survivor);
864 ts.Env()->PopFrame();
865 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700866 }
867
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700868 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700869 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700870 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
871 }
872
873 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
874 // TODO: we should try to expand the table if necessary.
875 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
876 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
877 return JNI_ERR;
878 }
879 // TODO: this isn't quite right, since "capacity" includes holes.
880 size_t capacity = ts.Env()->locals.Capacity();
881 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
882 if (!okay) {
883 ts.Self()->ThrowOutOfMemoryError(caller);
884 }
885 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700886 }
887
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
889 ScopedJniThreadState ts(env);
890 if (obj == NULL) {
891 return NULL;
892 }
893
Elliott Hughes75770752011-08-24 17:52:38 -0700894 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 IndirectReferenceTable& globals = vm->globals;
896 MutexLock mu(vm->globals_lock);
897 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
898 return reinterpret_cast<jobject>(ref);
899 }
900
901 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
902 ScopedJniThreadState ts(env);
903 if (obj == NULL) {
904 return;
905 }
906
Elliott Hughes75770752011-08-24 17:52:38 -0700907 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 IndirectReferenceTable& globals = vm->globals;
909 MutexLock mu(vm->globals_lock);
910
911 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
912 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
913 << "failed to find entry";
914 }
915 }
916
917 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
918 ScopedJniThreadState ts(env);
919 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
920 }
921
922 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
923 ScopedJniThreadState ts(env);
924 if (obj == NULL) {
925 return;
926 }
927
Elliott Hughes75770752011-08-24 17:52:38 -0700928 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 IndirectReferenceTable& weak_globals = vm->weak_globals;
930 MutexLock mu(vm->weak_globals_lock);
931
932 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
933 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
934 << "failed to find entry";
935 }
936 }
937
938 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
939 ScopedJniThreadState ts(env);
940 if (obj == NULL) {
941 return NULL;
942 }
943
944 IndirectReferenceTable& locals = ts.Env()->locals;
945
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700946 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
948 return reinterpret_cast<jobject>(ref);
949 }
950
951 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
952 ScopedJniThreadState ts(env);
953 if (obj == NULL) {
954 return;
955 }
956
957 IndirectReferenceTable& locals = ts.Env()->locals;
958
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700959 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 if (!locals.Remove(cookie, obj)) {
961 // Attempting to delete a local reference that is not in the
962 // topmost local reference frame is a no-op. DeleteLocalRef returns
963 // void and doesn't throw any exceptions, but we should probably
964 // complain about it so the user will notice that things aren't
965 // going quite the way they expect.
966 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
967 << "failed to find entry";
968 }
969 }
970
971 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
972 ScopedJniThreadState ts(env);
973 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
974 ? JNI_TRUE : JNI_FALSE;
975 }
976
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700977 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700979 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700980 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700981 return NULL;
982 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700983 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 }
985
Elliott Hughese84278b2012-03-22 10:06:53 -0700986 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700987 ScopedJniThreadState ts(env);
988 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700990 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 va_end(args);
992 return result;
993 }
994
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700997 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700998 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700999 return NULL;
1000 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001001 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001002 if (result == NULL) {
1003 return NULL;
1004 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001005 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001007 if (!ts.Self()->IsExceptionPending()) {
1008 return local_result;
1009 } else {
1010 return NULL;
1011 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001016 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001017 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001018 return NULL;
1019 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001020 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001021 if (result == NULL) {
1022 return NULL;
1023 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001024 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001026 if (!ts.Self()->IsExceptionPending()) {
1027 return local_result;
1028 } else {
1029 return NULL;
1030 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1034 ScopedJniThreadState ts(env);
1035 return FindMethodID(ts, c, name, sig, false);
1036 }
1037
1038 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1039 ScopedJniThreadState ts(env);
1040 return FindMethodID(ts, c, name, sig, true);
1041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 va_list ap;
1046 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001047 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001049 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001054 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001055 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001060 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001061 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 va_list ap;
1067 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001068 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_end(ap);
1070 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001075 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001080 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 va_list ap;
1086 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001087 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 va_end(ap);
1089 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001094 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001099 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 va_list ap;
1105 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001106 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 va_end(ap);
1108 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001113 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001118 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001122 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 va_list ap;
1124 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001125 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 va_end(ap);
1127 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001132 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001137 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001141 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 va_list ap;
1143 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001144 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 va_end(ap);
1146 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001151 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 }
1153
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001156 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
Elliott Hughes72025e52011-08-23 17:50:30 -07001159 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 va_list ap;
1162 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001163 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 va_end(ap);
1165 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001170 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001175 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 va_list ap;
1181 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001182 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 va_end(ap);
1184 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001189 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001194 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 }
1196
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 va_list ap;
1200 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001201 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001202 va_end(ap);
1203 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
1205
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001208 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 }
1210
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001213 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 }
1215
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 va_list ap;
1219 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001220 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001226 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001231 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001234 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 ScopedJniThreadState ts(env);
1236 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001237 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001238 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001239 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 va_end(ap);
1241 return local_result;
1242 }
1243
1244 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001245 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001247 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001248 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 }
1250
1251 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001252 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001254 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001255 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 }
1257
1258 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001259 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
1261 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001262 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001263 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 va_end(ap);
1265 return result.z;
1266 }
1267
1268 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001269 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001271 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 }
1273
1274 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001275 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001277 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001280 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 ScopedJniThreadState ts(env);
1282 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001283 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001284 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 va_end(ap);
1286 return result.b;
1287 }
1288
1289 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001290 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001292 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
1295 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001296 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001298 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001301 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
1303 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001304 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001305 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 va_end(ap);
1307 return result.c;
1308 }
1309
1310 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001311 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001313 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
1316 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001317 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001319 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001322 static jshort CallNonvirtualShortMethod(JNIEnv* env, 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 Hughes418d20f2011-09-22 14:00:39 -07001326 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 va_end(ap);
1328 return result.s;
1329 }
1330
1331 static jshort CallNonvirtualShortMethodV(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 Hughes418d20f2011-09-22 14:00:39 -07001334 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
1337 static jshort CallNonvirtualShortMethodA(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 Hughes418d20f2011-09-22 14:00:39 -07001340 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001343 static jint CallNonvirtualIntMethod(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 Hughes418d20f2011-09-22 14:00:39 -07001347 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 va_end(ap);
1349 return result.i;
1350 }
1351
1352 static jint CallNonvirtualIntMethodV(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 Hughes418d20f2011-09-22 14:00:39 -07001355 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
1358 static jint CallNonvirtualIntMethodA(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 Hughes418d20f2011-09-22 14:00:39 -07001361 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001364 static jlong CallNonvirtualLongMethod(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 Hughes418d20f2011-09-22 14:00:39 -07001368 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 va_end(ap);
1370 return result.j;
1371 }
1372
1373 static jlong CallNonvirtualLongMethodV(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 Hughes418d20f2011-09-22 14:00:39 -07001376 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
1379 static jlong CallNonvirtualLongMethodA(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 Hughes418d20f2011-09-22 14:00:39 -07001382 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001383 }
1384
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001385 static jfloat CallNonvirtualFloatMethod(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 Hughes418d20f2011-09-22 14:00:39 -07001389 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 va_end(ap);
1391 return result.f;
1392 }
1393
1394 static jfloat CallNonvirtualFloatMethodV(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 Hughes418d20f2011-09-22 14:00:39 -07001397 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
1400 static jfloat CallNonvirtualFloatMethodA(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 Hughes418d20f2011-09-22 14:00:39 -07001403 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001406 static jdouble CallNonvirtualDoubleMethod(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 Hughes418d20f2011-09-22 14:00:39 -07001410 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 va_end(ap);
1412 return result.d;
1413 }
1414
1415 static jdouble CallNonvirtualDoubleMethodV(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 Hughes418d20f2011-09-22 14:00:39 -07001418 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
1421 static jdouble CallNonvirtualDoubleMethodA(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 Hughes418d20f2011-09-22 14:00:39 -07001424 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001425 }
1426
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001427 static void CallNonvirtualVoidMethod(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 Hughes418d20f2011-09-22 14:00:39 -07001431 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 va_end(ap);
1433 }
1434
1435 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001436 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001437 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001438 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 }
1440
1441 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001442 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001444 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 }
1446
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001447 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 ScopedJniThreadState ts(env);
1449 return FindFieldID(ts, c, name, sig, false);
1450 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001451
1452
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001453 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 ScopedJniThreadState ts(env);
1455 return FindFieldID(ts, c, name, sig, true);
1456 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001457
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001461 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001462 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001464
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001465 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001467 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001468 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001469 }
1470
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001473 Object* o = Decode<Object*>(ts, java_object);
1474 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001475 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001477 }
1478
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001480 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001481 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001482 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001483 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001486#define GET_PRIMITIVE_FIELD(fn, instance) \
1487 ScopedJniThreadState ts(env); \
1488 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001489 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001490 return f->fn(o)
1491
1492#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1493 ScopedJniThreadState ts(env); \
1494 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001495 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 f->fn(o, value)
1497
1498 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1499 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001500 }
1501
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1503 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1507 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 }
1509
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1511 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1515 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001518 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1519 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
1521
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001522 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1523 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
1525
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001526 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1527 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
1529
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001530 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001531 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001532 }
1533
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001534 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001538 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001539 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001542 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001543 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001546 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001547 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 }
1549
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001550 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001551 GET_PRIMITIVE_FIELD(GetLong, NULL);
1552 }
1553
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001554 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001555 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1556 }
1557
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001558 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001559 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1560 }
1561
1562 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1563 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1564 }
1565
1566 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1567 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1568 }
1569
1570 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1571 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1572 }
1573
1574 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1575 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1576 }
1577
1578 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1579 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1580 }
1581
1582 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1583 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1584 }
1585
1586 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1587 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1588 }
1589
1590 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1591 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1592 }
1593
1594 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1595 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1596 }
1597
1598 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1599 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1600 }
1601
1602 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1603 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1604 }
1605
1606 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1607 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1608 }
1609
1610 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1611 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1612 }
1613
1614 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1615 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1616 }
1617
1618 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1619 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1620 }
1621
1622 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1623 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001626 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 ScopedJniThreadState ts(env);
1628 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001629 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001630 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001631 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 va_end(ap);
1633 return local_result;
1634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001638 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001639 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001642 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001644 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001645 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 }
1647
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001648 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 ScopedJniThreadState ts(env);
1650 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001651 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001652 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 va_end(ap);
1654 return result.z;
1655 }
1656
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001657 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001659 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 }
1661
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001662 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001664 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001667 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 ScopedJniThreadState ts(env);
1669 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001670 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001671 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 va_end(ap);
1673 return result.b;
1674 }
1675
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001676 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001678 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 }
1680
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001681 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001683 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001686 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 ScopedJniThreadState ts(env);
1688 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001689 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001690 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 va_end(ap);
1692 return result.c;
1693 }
1694
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001695 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001697 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 }
1699
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001700 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001702 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 }
1704
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001705 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 ScopedJniThreadState ts(env);
1707 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001708 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001709 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 va_end(ap);
1711 return result.s;
1712 }
1713
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001714 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001716 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001719 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001721 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 }
1723
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001724 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 ScopedJniThreadState ts(env);
1726 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001727 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001728 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 va_end(ap);
1730 return result.i;
1731 }
1732
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001733 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001735 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001738 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001740 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001743 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 ScopedJniThreadState ts(env);
1745 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001746 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001747 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 va_end(ap);
1749 return result.j;
1750 }
1751
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001752 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001754 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001757 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001759 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001762 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 ScopedJniThreadState ts(env);
1764 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001765 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001766 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 va_end(ap);
1768 return result.f;
1769 }
1770
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001771 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001773 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001776 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001778 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001781 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 ScopedJniThreadState ts(env);
1783 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001784 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001785 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 va_end(ap);
1787 return result.d;
1788 }
1789
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001790 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001792 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001795 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001797 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001800 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 ScopedJniThreadState ts(env);
1802 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001803 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001804 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 va_end(ap);
1806 }
1807
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001808 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001810 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 }
1812
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001813 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001815 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
Elliott Hughes814e4032011-08-23 12:07:56 -07001818 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001820 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001821 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 }
1823
1824 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1825 ScopedJniThreadState ts(env);
1826 if (utf == NULL) {
1827 return NULL;
1828 }
1829 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001830 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
Elliott Hughes814e4032011-08-23 12:07:56 -07001833 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1834 ScopedJniThreadState ts(env);
1835 return Decode<String*>(ts, java_string)->GetLength();
1836 }
1837
1838 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1839 ScopedJniThreadState ts(env);
1840 return Decode<String*>(ts, java_string)->GetUtfLength();
1841 }
1842
Elliott Hughesb465ab02011-08-24 11:21:21 -07001843 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001845 String* s = Decode<String*>(ts, java_string);
1846 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1847 ThrowSIOOBE(ts, start, length, s->GetLength());
1848 } else {
1849 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1850 memcpy(buf, chars + start, length * sizeof(jchar));
1851 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001852 }
1853
Elliott Hughesb465ab02011-08-24 11:21:21 -07001854 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001855 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001856 String* s = Decode<String*>(ts, java_string);
1857 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1858 ThrowSIOOBE(ts, start, length, s->GetLength());
1859 } else {
1860 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1861 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1862 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001863 }
1864
Elliott Hughes75770752011-08-24 17:52:38 -07001865 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001866 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001867 String* s = Decode<String*>(ts, java_string);
1868 const CharArray* chars = s->GetCharArray();
1869 PinPrimitiveArray(ts, chars);
1870 if (is_copy != NULL) {
1871 *is_copy = JNI_FALSE;
1872 }
1873 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001874 }
1875
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001876 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001877 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001878 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 }
1880
Elliott Hughes75770752011-08-24 17:52:38 -07001881 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001883 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001884 }
1885
Elliott Hughes75770752011-08-24 17:52:38 -07001886 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001888 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 }
1890
Elliott Hughes75770752011-08-24 17:52:38 -07001891 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001892 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001893 if (java_string == NULL) {
1894 return NULL;
1895 }
1896 if (is_copy != NULL) {
1897 *is_copy = JNI_TRUE;
1898 }
1899 String* s = Decode<String*>(ts, java_string);
1900 size_t byte_count = s->GetUtfLength();
1901 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001902 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001903 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1904 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1905 bytes[byte_count] = '\0';
1906 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001907 }
1908
Elliott Hughes75770752011-08-24 17:52:38 -07001909 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001910 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001911 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001912 }
1913
Elliott Hughesbd935992011-08-22 11:59:34 -07001914 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001915 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001916 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001917 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001918 Array* array = obj->AsArray();
1919 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001920 }
1921
Elliott Hughes814e4032011-08-23 12:07:56 -07001922 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001924 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001925 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 }
1927
1928 static void SetObjectArrayElement(JNIEnv* env,
1929 jobjectArray java_array, jsize index, jobject java_value) {
1930 ScopedJniThreadState ts(env);
1931 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1932 Object* value = Decode<Object*>(ts, java_value);
1933 array->Set(index, value);
1934 }
1935
1936 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1937 ScopedJniThreadState ts(env);
1938 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1939 }
1940
1941 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1942 ScopedJniThreadState ts(env);
1943 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1944 }
1945
1946 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1947 ScopedJniThreadState ts(env);
1948 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1949 }
1950
1951 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1952 ScopedJniThreadState ts(env);
1953 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1954 }
1955
1956 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1957 ScopedJniThreadState ts(env);
1958 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1959 }
1960
1961 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1962 ScopedJniThreadState ts(env);
1963 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1964 }
1965
1966 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1967 ScopedJniThreadState ts(env);
1968 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1969 }
1970
1971 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1972 ScopedJniThreadState ts(env);
1973 CHECK_GE(length, 0); // TODO: ReportJniError
1974
1975 // Compute the array class corresponding to the given element class.
1976 Class* element_class = Decode<Class*>(ts, element_jclass);
1977 std::string descriptor;
1978 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001979 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001980
1981 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001982 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1983 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 return NULL;
1985 }
1986
Elliott Hughes75770752011-08-24 17:52:38 -07001987 // Allocate and initialize if necessary.
1988 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001990 if (initial_element != NULL) {
1991 Object* initial_object = Decode<Object*>(ts, initial_element);
1992 for (jsize i = 0; i < length; ++i) {
1993 result->Set(i, initial_object);
1994 }
1995 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001996 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
1999 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2000 ScopedJniThreadState ts(env);
2001 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
2002 }
2003
Ian Rogersa15e67d2012-02-28 13:51:55 -08002004 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002005 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002006 Array* array = Decode<Array*>(ts, java_array);
2007 PinPrimitiveArray(ts, array);
2008 if (is_copy != NULL) {
2009 *is_copy = JNI_FALSE;
2010 }
2011 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002012 }
2013
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002014 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002015 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002016 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002017 }
2018
Elliott Hughes75770752011-08-24 17:52:38 -07002019 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002021 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes75770752011-08-24 17:52:38 -07002024 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002026 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes75770752011-08-24 17:52:38 -07002029 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002031 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes75770752011-08-24 17:52:38 -07002034 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002036 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes75770752011-08-24 17:52:38 -07002039 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002041 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes75770752011-08-24 17:52:38 -07002044 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002046 return GetPrimitiveArray<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes75770752011-08-24 17:52:38 -07002049 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002051 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes75770752011-08-24 17:52:38 -07002054 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002056 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002059 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002061 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002064 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002066 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002069 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002071 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002074 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002076 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002079 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002081 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002084 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002086 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002089 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002091 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002094 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002096 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughes814e4032011-08-23 12:07:56 -07002124 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002126 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Elliott Hughes814e4032011-08-23 12:07:56 -07002129 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002131 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
2133
Elliott Hughes814e4032011-08-23 12:07:56 -07002134 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002136 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138
Elliott Hughes814e4032011-08-23 12:07:56 -07002139 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002141 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 }
2143
Elliott Hughes814e4032011-08-23 12:07:56 -07002144 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002146 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
Elliott Hughes814e4032011-08-23 12:07:56 -07002149 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002151 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
2153
Elliott Hughes814e4032011-08-23 12:07:56 -07002154 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002156 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 }
2158
Elliott Hughes814e4032011-08-23 12:07:56 -07002159 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002161 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 }
2163
Elliott Hughes814e4032011-08-23 12:07:56 -07002164 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002166 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 }
2168
Elliott Hughes814e4032011-08-23 12:07:56 -07002169 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002171 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002172 }
2173
Elliott Hughes814e4032011-08-23 12:07:56 -07002174 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002176 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178
Elliott Hughes5174fe62011-08-23 15:12:35 -07002179 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002181 Class* c = Decode<Class*>(ts, java_class);
2182
Elliott Hughes5174fe62011-08-23 15:12:35 -07002183 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 const char* name = methods[i].name;
2185 const char* sig = methods[i].signature;
2186
2187 if (*sig == '!') {
2188 // TODO: fast jni. it's too noisy to log all these.
2189 ++sig;
2190 }
2191
Elliott Hughes5174fe62011-08-23 15:12:35 -07002192 Method* m = c->FindDirectMethod(name, sig);
2193 if (m == NULL) {
2194 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002196 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002197 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002198 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002200 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002201 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002202 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 return JNI_ERR;
2204 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002205
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002206 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002207
Ian Rogers60db5ab2012-02-20 17:02:00 -08002208 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210 return JNI_OK;
2211 }
2212
Elliott Hughes5174fe62011-08-23 15:12:35 -07002213 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002215 Class* c = Decode<Class*>(ts, java_class);
2216
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002217 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002218
2219 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2220 Method* m = c->GetDirectMethod(i);
2221 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002222 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002223 }
2224 }
2225 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2226 Method* m = c->GetVirtualMethod(i);
2227 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002228 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002229 }
2230 }
2231
2232 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Elliott Hughes72025e52011-08-23 17:50:30 -07002235 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002236 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002237 Decode<Object*>(ts, java_object)->MonitorEnter(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002238 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Elliott Hughes72025e52011-08-23 17:50:30 -07002241 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 ScopedJniThreadState ts(env);
Elliott Hughes5f791332011-09-15 17:45:30 -07002243 Decode<Object*>(ts, java_object)->MonitorExit(ts.Self());
Elliott Hughes72025e52011-08-23 17:50:30 -07002244 return ts.Self()->IsExceptionPending() ? JNI_ERR : JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
2247 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2248 ScopedJniThreadState ts(env);
2249 Runtime* runtime = Runtime::Current();
2250 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002251 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002252 } else {
2253 *vm = NULL;
2254 }
2255 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2256 }
2257
Elliott Hughescdf53122011-08-19 15:46:09 -07002258 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2259 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002260
2261 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002262 CHECK(address != NULL); // TODO: ReportJniError
2263 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002264
2265 jclass buffer_class = GetDirectByteBufferClass(env);
2266 jmethodID mid = env->GetMethodID(buffer_class, "<init>", "(II)V");
2267 if (mid == NULL) {
2268 return NULL;
2269 }
2270
2271 // At the moment, the Java side is limited to 32 bits.
2272 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2273 CHECK_LE(capacity, 0xffffffff);
2274 jint address_arg = reinterpret_cast<jint>(address);
2275 jint capacity_arg = static_cast<jint>(capacity);
2276
2277 jobject result = env->NewObject(buffer_class, mid, address_arg, capacity_arg);
2278 return ts.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002279 }
2280
Elliott Hughesb465ab02011-08-24 11:21:21 -07002281 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002282 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002283 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "effectiveDirectAddress", "I");
2284 return reinterpret_cast<void*>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 }
2286
Elliott Hughesb465ab02011-08-24 11:21:21 -07002287 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002288 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002289 static jfieldID fid = env->GetFieldID(GetDirectByteBufferClass(env), "capacity", "I");
2290 return static_cast<jlong>(env->GetIntField(java_buffer, fid));
Elliott Hughescdf53122011-08-19 15:46:09 -07002291 }
2292
Elliott Hughesb465ab02011-08-24 11:21:21 -07002293 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002294 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002295
Elliott Hughes75770752011-08-24 17:52:38 -07002296 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002297
2298 // Do we definitely know what kind of reference this is?
2299 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2300 IndirectRefKind kind = GetIndirectRefKind(ref);
2301 switch (kind) {
2302 case kLocal:
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002303 if (ts.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
2304 return JNILocalRefType;
2305 }
2306 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002307 case kGlobal:
2308 return JNIGlobalRefType;
2309 case kWeakGlobal:
2310 return JNIWeakGlobalRefType;
2311 case kSirtOrInvalid:
2312 // Is it in a stack IRT?
2313 if (ts.Self()->SirtContains(java_object)) {
2314 return JNILocalRefType;
2315 }
2316
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002317 if (!ts.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002318 return JNIInvalidRefType;
2319 }
2320
Elliott Hughesb465ab02011-08-24 11:21:21 -07002321 // If we're handing out direct pointers, check whether it's a direct pointer
2322 // to a local reference.
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002323 if (Decode<Object*>(ts, java_object) == reinterpret_cast<Object*>(java_object)) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002324 if (ts.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002325 return JNILocalRefType;
2326 }
2327 }
2328
2329 return JNIInvalidRefType;
2330 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002331 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2332 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002333 }
2334};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002335
Elliott Hughes88c5c352012-03-15 18:49:48 -07002336const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002337 NULL, // reserved0.
2338 NULL, // reserved1.
2339 NULL, // reserved2.
2340 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002341 JNI::GetVersion,
2342 JNI::DefineClass,
2343 JNI::FindClass,
2344 JNI::FromReflectedMethod,
2345 JNI::FromReflectedField,
2346 JNI::ToReflectedMethod,
2347 JNI::GetSuperclass,
2348 JNI::IsAssignableFrom,
2349 JNI::ToReflectedField,
2350 JNI::Throw,
2351 JNI::ThrowNew,
2352 JNI::ExceptionOccurred,
2353 JNI::ExceptionDescribe,
2354 JNI::ExceptionClear,
2355 JNI::FatalError,
2356 JNI::PushLocalFrame,
2357 JNI::PopLocalFrame,
2358 JNI::NewGlobalRef,
2359 JNI::DeleteGlobalRef,
2360 JNI::DeleteLocalRef,
2361 JNI::IsSameObject,
2362 JNI::NewLocalRef,
2363 JNI::EnsureLocalCapacity,
2364 JNI::AllocObject,
2365 JNI::NewObject,
2366 JNI::NewObjectV,
2367 JNI::NewObjectA,
2368 JNI::GetObjectClass,
2369 JNI::IsInstanceOf,
2370 JNI::GetMethodID,
2371 JNI::CallObjectMethod,
2372 JNI::CallObjectMethodV,
2373 JNI::CallObjectMethodA,
2374 JNI::CallBooleanMethod,
2375 JNI::CallBooleanMethodV,
2376 JNI::CallBooleanMethodA,
2377 JNI::CallByteMethod,
2378 JNI::CallByteMethodV,
2379 JNI::CallByteMethodA,
2380 JNI::CallCharMethod,
2381 JNI::CallCharMethodV,
2382 JNI::CallCharMethodA,
2383 JNI::CallShortMethod,
2384 JNI::CallShortMethodV,
2385 JNI::CallShortMethodA,
2386 JNI::CallIntMethod,
2387 JNI::CallIntMethodV,
2388 JNI::CallIntMethodA,
2389 JNI::CallLongMethod,
2390 JNI::CallLongMethodV,
2391 JNI::CallLongMethodA,
2392 JNI::CallFloatMethod,
2393 JNI::CallFloatMethodV,
2394 JNI::CallFloatMethodA,
2395 JNI::CallDoubleMethod,
2396 JNI::CallDoubleMethodV,
2397 JNI::CallDoubleMethodA,
2398 JNI::CallVoidMethod,
2399 JNI::CallVoidMethodV,
2400 JNI::CallVoidMethodA,
2401 JNI::CallNonvirtualObjectMethod,
2402 JNI::CallNonvirtualObjectMethodV,
2403 JNI::CallNonvirtualObjectMethodA,
2404 JNI::CallNonvirtualBooleanMethod,
2405 JNI::CallNonvirtualBooleanMethodV,
2406 JNI::CallNonvirtualBooleanMethodA,
2407 JNI::CallNonvirtualByteMethod,
2408 JNI::CallNonvirtualByteMethodV,
2409 JNI::CallNonvirtualByteMethodA,
2410 JNI::CallNonvirtualCharMethod,
2411 JNI::CallNonvirtualCharMethodV,
2412 JNI::CallNonvirtualCharMethodA,
2413 JNI::CallNonvirtualShortMethod,
2414 JNI::CallNonvirtualShortMethodV,
2415 JNI::CallNonvirtualShortMethodA,
2416 JNI::CallNonvirtualIntMethod,
2417 JNI::CallNonvirtualIntMethodV,
2418 JNI::CallNonvirtualIntMethodA,
2419 JNI::CallNonvirtualLongMethod,
2420 JNI::CallNonvirtualLongMethodV,
2421 JNI::CallNonvirtualLongMethodA,
2422 JNI::CallNonvirtualFloatMethod,
2423 JNI::CallNonvirtualFloatMethodV,
2424 JNI::CallNonvirtualFloatMethodA,
2425 JNI::CallNonvirtualDoubleMethod,
2426 JNI::CallNonvirtualDoubleMethodV,
2427 JNI::CallNonvirtualDoubleMethodA,
2428 JNI::CallNonvirtualVoidMethod,
2429 JNI::CallNonvirtualVoidMethodV,
2430 JNI::CallNonvirtualVoidMethodA,
2431 JNI::GetFieldID,
2432 JNI::GetObjectField,
2433 JNI::GetBooleanField,
2434 JNI::GetByteField,
2435 JNI::GetCharField,
2436 JNI::GetShortField,
2437 JNI::GetIntField,
2438 JNI::GetLongField,
2439 JNI::GetFloatField,
2440 JNI::GetDoubleField,
2441 JNI::SetObjectField,
2442 JNI::SetBooleanField,
2443 JNI::SetByteField,
2444 JNI::SetCharField,
2445 JNI::SetShortField,
2446 JNI::SetIntField,
2447 JNI::SetLongField,
2448 JNI::SetFloatField,
2449 JNI::SetDoubleField,
2450 JNI::GetStaticMethodID,
2451 JNI::CallStaticObjectMethod,
2452 JNI::CallStaticObjectMethodV,
2453 JNI::CallStaticObjectMethodA,
2454 JNI::CallStaticBooleanMethod,
2455 JNI::CallStaticBooleanMethodV,
2456 JNI::CallStaticBooleanMethodA,
2457 JNI::CallStaticByteMethod,
2458 JNI::CallStaticByteMethodV,
2459 JNI::CallStaticByteMethodA,
2460 JNI::CallStaticCharMethod,
2461 JNI::CallStaticCharMethodV,
2462 JNI::CallStaticCharMethodA,
2463 JNI::CallStaticShortMethod,
2464 JNI::CallStaticShortMethodV,
2465 JNI::CallStaticShortMethodA,
2466 JNI::CallStaticIntMethod,
2467 JNI::CallStaticIntMethodV,
2468 JNI::CallStaticIntMethodA,
2469 JNI::CallStaticLongMethod,
2470 JNI::CallStaticLongMethodV,
2471 JNI::CallStaticLongMethodA,
2472 JNI::CallStaticFloatMethod,
2473 JNI::CallStaticFloatMethodV,
2474 JNI::CallStaticFloatMethodA,
2475 JNI::CallStaticDoubleMethod,
2476 JNI::CallStaticDoubleMethodV,
2477 JNI::CallStaticDoubleMethodA,
2478 JNI::CallStaticVoidMethod,
2479 JNI::CallStaticVoidMethodV,
2480 JNI::CallStaticVoidMethodA,
2481 JNI::GetStaticFieldID,
2482 JNI::GetStaticObjectField,
2483 JNI::GetStaticBooleanField,
2484 JNI::GetStaticByteField,
2485 JNI::GetStaticCharField,
2486 JNI::GetStaticShortField,
2487 JNI::GetStaticIntField,
2488 JNI::GetStaticLongField,
2489 JNI::GetStaticFloatField,
2490 JNI::GetStaticDoubleField,
2491 JNI::SetStaticObjectField,
2492 JNI::SetStaticBooleanField,
2493 JNI::SetStaticByteField,
2494 JNI::SetStaticCharField,
2495 JNI::SetStaticShortField,
2496 JNI::SetStaticIntField,
2497 JNI::SetStaticLongField,
2498 JNI::SetStaticFloatField,
2499 JNI::SetStaticDoubleField,
2500 JNI::NewString,
2501 JNI::GetStringLength,
2502 JNI::GetStringChars,
2503 JNI::ReleaseStringChars,
2504 JNI::NewStringUTF,
2505 JNI::GetStringUTFLength,
2506 JNI::GetStringUTFChars,
2507 JNI::ReleaseStringUTFChars,
2508 JNI::GetArrayLength,
2509 JNI::NewObjectArray,
2510 JNI::GetObjectArrayElement,
2511 JNI::SetObjectArrayElement,
2512 JNI::NewBooleanArray,
2513 JNI::NewByteArray,
2514 JNI::NewCharArray,
2515 JNI::NewShortArray,
2516 JNI::NewIntArray,
2517 JNI::NewLongArray,
2518 JNI::NewFloatArray,
2519 JNI::NewDoubleArray,
2520 JNI::GetBooleanArrayElements,
2521 JNI::GetByteArrayElements,
2522 JNI::GetCharArrayElements,
2523 JNI::GetShortArrayElements,
2524 JNI::GetIntArrayElements,
2525 JNI::GetLongArrayElements,
2526 JNI::GetFloatArrayElements,
2527 JNI::GetDoubleArrayElements,
2528 JNI::ReleaseBooleanArrayElements,
2529 JNI::ReleaseByteArrayElements,
2530 JNI::ReleaseCharArrayElements,
2531 JNI::ReleaseShortArrayElements,
2532 JNI::ReleaseIntArrayElements,
2533 JNI::ReleaseLongArrayElements,
2534 JNI::ReleaseFloatArrayElements,
2535 JNI::ReleaseDoubleArrayElements,
2536 JNI::GetBooleanArrayRegion,
2537 JNI::GetByteArrayRegion,
2538 JNI::GetCharArrayRegion,
2539 JNI::GetShortArrayRegion,
2540 JNI::GetIntArrayRegion,
2541 JNI::GetLongArrayRegion,
2542 JNI::GetFloatArrayRegion,
2543 JNI::GetDoubleArrayRegion,
2544 JNI::SetBooleanArrayRegion,
2545 JNI::SetByteArrayRegion,
2546 JNI::SetCharArrayRegion,
2547 JNI::SetShortArrayRegion,
2548 JNI::SetIntArrayRegion,
2549 JNI::SetLongArrayRegion,
2550 JNI::SetFloatArrayRegion,
2551 JNI::SetDoubleArrayRegion,
2552 JNI::RegisterNatives,
2553 JNI::UnregisterNatives,
2554 JNI::MonitorEnter,
2555 JNI::MonitorExit,
2556 JNI::GetJavaVM,
2557 JNI::GetStringRegion,
2558 JNI::GetStringUTFRegion,
2559 JNI::GetPrimitiveArrayCritical,
2560 JNI::ReleasePrimitiveArrayCritical,
2561 JNI::GetStringCritical,
2562 JNI::ReleaseStringCritical,
2563 JNI::NewWeakGlobalRef,
2564 JNI::DeleteWeakGlobalRef,
2565 JNI::ExceptionCheck,
2566 JNI::NewDirectByteBuffer,
2567 JNI::GetDirectBufferAddress,
2568 JNI::GetDirectBufferCapacity,
2569 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002570};
2571
Elliott Hughes75770752011-08-24 17:52:38 -07002572JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002573 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002574 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002575 local_ref_cookie(IRT_FIRST_SEGMENT),
2576 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002577 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002578 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002579 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002580 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002581 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002582 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002583 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002584 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2585 // errors will ensue.
2586 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2587 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002588}
2589
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002590JNIEnvExt::~JNIEnvExt() {
2591}
2592
Elliott Hughes88c5c352012-03-15 18:49:48 -07002593void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2594 check_jni = enabled;
2595 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002596}
2597
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002598void JNIEnvExt::DumpReferenceTables() {
2599 locals.Dump();
2600 monitors.Dump();
2601}
2602
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002603void JNIEnvExt::PushFrame(int /*capacity*/) {
2604 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002605 stacked_local_ref_cookies.push_back(local_ref_cookie);
2606 local_ref_cookie = locals.GetSegmentState();
2607}
2608
2609void JNIEnvExt::PopFrame() {
2610 locals.SetSegmentState(local_ref_cookie);
2611 local_ref_cookie = stacked_local_ref_cookies.back();
2612 stacked_local_ref_cookies.pop_back();
2613}
2614
Carl Shapiroea4dca82011-08-01 13:45:38 -07002615// JNI Invocation interface.
2616
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002617extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2618 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2619 if (args->version < JNI_VERSION_1_2) {
2620 return JNI_EVERSION;
2621 }
2622 Runtime::Options options;
2623 for (int i = 0; i < args->nOptions; ++i) {
2624 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002625 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002626 }
2627 bool ignore_unrecognized = args->ignoreUnrecognized;
Elliott Hughesf2682d52011-08-15 16:37:04 -07002628 Runtime* runtime = Runtime::Create(options, ignore_unrecognized);
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002629 if (runtime == NULL) {
2630 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002631 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002632 runtime->Start();
2633 *p_env = Thread::Current()->GetJniEnv();
2634 *p_vm = runtime->GetJavaVM();
2635 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002636}
2637
Elliott Hughesf2682d52011-08-15 16:37:04 -07002638extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002639 Runtime* runtime = Runtime::Current();
2640 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002641 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002642 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002643 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002644 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002645 }
2646 return JNI_OK;
2647}
2648
2649// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002650extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002651 return JNI_ERR;
2652}
2653
Elliott Hughescdf53122011-08-19 15:46:09 -07002654class JII {
2655 public:
2656 static jint DestroyJavaVM(JavaVM* vm) {
2657 if (vm == NULL) {
2658 return JNI_ERR;
2659 } else {
2660 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2661 delete raw_vm->runtime;
2662 return JNI_OK;
2663 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002664 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002665
Elliott Hughescdf53122011-08-19 15:46:09 -07002666 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002667 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002668 }
2669
2670 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002671 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002672 }
2673
2674 static jint DetachCurrentThread(JavaVM* vm) {
2675 if (vm == NULL) {
2676 return JNI_ERR;
2677 } else {
2678 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2679 Runtime* runtime = raw_vm->runtime;
2680 runtime->DetachCurrentThread();
2681 return JNI_OK;
2682 }
2683 }
2684
2685 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2686 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2687 return JNI_EVERSION;
2688 }
2689 if (vm == NULL || env == NULL) {
2690 return JNI_ERR;
2691 }
2692 Thread* thread = Thread::Current();
2693 if (thread == NULL) {
2694 *env = NULL;
2695 return JNI_EDETACHED;
2696 }
2697 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002698 return JNI_OK;
2699 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002700};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002701
Elliott Hughes88c5c352012-03-15 18:49:48 -07002702const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002703 NULL, // reserved0
2704 NULL, // reserved1
2705 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002706 JII::DestroyJavaVM,
2707 JII::AttachCurrentThread,
2708 JII::DetachCurrentThread,
2709 JII::GetEnv,
2710 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002711};
2712
Elliott Hughesa0957642011-09-02 14:27:33 -07002713JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002714 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002715 check_jni_abort_hook(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002716 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002717 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002718 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002719 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002720 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002721 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002722 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002723 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002724 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002725 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002726 libraries_lock("JNI shared libraries map lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002727 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002728 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002729 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002730 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002731 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002732}
2733
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002734JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002735 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002736}
2737
Elliott Hughes88c5c352012-03-15 18:49:48 -07002738void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2739 check_jni = enabled;
2740 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002741}
2742
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002743void JavaVMExt::DumpReferenceTables() {
2744 {
2745 MutexLock mu(globals_lock);
2746 globals.Dump();
2747 }
2748 {
2749 MutexLock mu(weak_globals_lock);
2750 weak_globals.Dump();
2751 }
2752 {
2753 MutexLock mu(pins_lock);
2754 pin_table.Dump();
2755 }
2756}
2757
Elliott Hughes75770752011-08-24 17:52:38 -07002758bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) {
2759 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002760
2761 // See if we've already loaded this library. If we have, and the class loader
2762 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002763 // TODO: for better results we should canonicalize the pathname (or even compare
2764 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002765 SharedLibrary* library;
2766 {
2767 // TODO: move the locking (and more of this logic) into Libraries.
2768 MutexLock mu(libraries_lock);
2769 library = libraries->Get(path);
2770 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002771 if (library != NULL) {
2772 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002773 // The library will be associated with class_loader. The JNI
2774 // spec says we can't load the same library into more than one
2775 // class loader.
2776 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2777 "ClassLoader %p; can't open in ClassLoader %p",
2778 path.c_str(), library->GetClassLoader(), class_loader);
2779 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002780 return false;
2781 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002782 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2783 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002784 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002785 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2786 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002787 return false;
2788 }
2789 return true;
2790 }
2791
2792 // Open the shared library. Because we're using a full path, the system
2793 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2794 // resolve this library's dependencies though.)
2795
2796 // Failures here are expected when java.library.path has several entries
2797 // and we have to hunt for the lib.
2798
2799 // The current version of the dynamic linker prints detailed information
2800 // about dlopen() failures. Some things to check if the message is
2801 // cryptic:
2802 // - make sure the library exists on the device
2803 // - verify that the right path is being opened (the debug log message
2804 // above can help with that)
2805 // - check to see if the library is valid (e.g. not zero bytes long)
2806 // - check config/prelink-linux-arm.map to ensure that the library
2807 // is listed and is not being overrun by the previous entry (if
2808 // loading suddenly stops working on a prelinked library, this is
2809 // a good one to check)
2810 // - write a trivial app that calls sleep() then dlopen(), attach
2811 // to it with "strace -p <pid>" while it sleeps, and watch for
2812 // attempts to open nonexistent dependent shared libs
2813
2814 // TODO: automate some of these checks!
2815
2816 // This can execute slowly for a large library on a busy system, so we
Elliott Hughes93e74e82011-09-13 11:07:03 -07002817 // want to switch from kRunnable to kVmWait while it executes. This allows
Elliott Hughescdf53122011-08-19 15:46:09 -07002818 // the GC to ignore us.
2819 Thread* self = Thread::Current();
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002820 void* handle = NULL;
2821 {
Elliott Hughes93e74e82011-09-13 11:07:03 -07002822 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -08002823 handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002824 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002825
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002826 VLOG(jni) << "[Call to dlopen(\"" << path << "\") returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002827
2828 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002829 detail = dlerror();
Elliott Hughescdf53122011-08-19 15:46:09 -07002830 return false;
2831 }
2832
2833 // Create a new entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002834 {
Elliott Hughes79082e32011-08-25 12:07:32 -07002835 // TODO: move the locking (and more of this logic) into Libraries.
2836 MutexLock mu(libraries_lock);
2837 library = libraries->Get(path);
2838 if (library != NULL) {
2839 LOG(INFO) << "WOW: we lost a race to add shared library: "
2840 << "\"" << path << "\" ClassLoader=" << class_loader;
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002841 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002842 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002843 library = new SharedLibrary(path, handle, class_loader);
2844 libraries->Put(path, library);
Elliott Hughescdf53122011-08-19 15:46:09 -07002845 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002846
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002847 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002848
2849 bool result = true;
2850 void* sym = dlsym(handle, "JNI_OnLoad");
2851 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002852 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002853 } else {
2854 // Call JNI_OnLoad. We have to override the current class
2855 // loader, which will always be "null" since the stuff at the
2856 // top of the stack is around Runtime.loadLibrary(). (See
2857 // the comments in the JNI FindClass function.)
2858 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2859 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Brian Carlstrombffb1552011-08-25 12:23:53 -07002860 const ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002861 self->SetClassLoaderOverride(class_loader);
2862
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002863 int version = 0;
2864 {
2865 ScopedThreadStateChange tsc(self, Thread::kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002866 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002867 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002868 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002869
Brian Carlstromaded5f72011-10-07 17:15:04 -07002870 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002871
2872 if (version != JNI_VERSION_1_2 &&
2873 version != JNI_VERSION_1_4 &&
2874 version != JNI_VERSION_1_6) {
2875 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2876 << "bad version: " << version;
2877 // It's unwise to call dlclose() here, but we can mark it
2878 // as bad and ensure that future load attempts will fail.
2879 // We don't know how far JNI_OnLoad got, so there could
2880 // be some partially-initialized stuff accessible through
2881 // newly-registered native method calls. We could try to
2882 // unregister them, but that doesn't seem worthwhile.
2883 result = false;
2884 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002885 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2886 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002887 }
2888 }
2889
2890 library->SetResult(result);
2891 return result;
2892}
2893
2894void* JavaVMExt::FindCodeForNativeMethod(Method* m) {
2895 CHECK(m->IsNative());
2896
2897 Class* c = m->GetDeclaringClass();
2898
2899 // If this is a static method, it could be called before the class
2900 // has been initialized.
2901 if (m->IsStatic()) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002902 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002903 return NULL;
2904 }
2905 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002906 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002907 }
2908
Brian Carlstrom16192862011-09-12 17:50:06 -07002909 std::string detail;
2910 void* native_method;
2911 {
2912 MutexLock mu(libraries_lock);
2913 native_method = libraries->FindNativeMethod(m, detail);
2914 }
2915 // throwing can cause libraries_lock to be reacquired
2916 if (native_method == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002917 Thread::Current()->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002918 }
2919 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002920}
2921
Elliott Hughes410c0c82011-09-01 17:58:25 -07002922void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
2923 {
2924 MutexLock mu(globals_lock);
2925 globals.VisitRoots(visitor, arg);
2926 }
2927 {
2928 MutexLock mu(pins_lock);
2929 pin_table.VisitRoots(visitor, arg);
2930 }
2931 // The weak_globals table is visited by the GC itself (because it mutates the table).
2932}
2933
Elliott Hughes844f9a02012-01-24 20:19:58 -08002934jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
2935 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2936 if (c.get() == NULL) {
2937 return NULL;
2938 }
2939 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
2940}
2941
Ian Rogersdf20fe02011-07-20 20:34:16 -07002942} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002943
2944std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2945 switch (rhs) {
2946 case JNIInvalidRefType:
2947 os << "JNIInvalidRefType";
2948 return os;
2949 case JNILocalRefType:
2950 os << "JNILocalRefType";
2951 return os;
2952 case JNIGlobalRefType:
2953 os << "JNIGlobalRefType";
2954 return os;
2955 case JNIWeakGlobalRefType:
2956 os << "JNIWeakGlobalRefType";
2957 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002958 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002959 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002960 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002961 }
2962}