blob: dba933aa8f31d1f7e206447a161adec10a71427e [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
Elliott Hughes462c9442012-03-23 18:47:50 -0700488static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_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 Hughes462c9442012-03-23 18:47:50 -0700493 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700494 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 Hughes462c9442012-03-23 18:47:50 -0700508 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
509 const char* thread_name = NULL;
510 Object* thread_group = NULL;
511 if (args != NULL) {
512 CHECK_GE(args->version, JNI_VERSION_1_2);
513 thread_name = args->name;
514 thread_group = static_cast<Thread*>(NULL)->DecodeJObject(args->group);
Elliott Hughes75770752011-08-24 17:52:38 -0700515 }
Elliott Hughes75770752011-08-24 17:52:38 -0700516
Elliott Hughes462c9442012-03-23 18:47:50 -0700517 runtime->AttachCurrentThread(thread_name, as_daemon, thread_group);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700518 *p_env = Thread::Current()->GetJniEnv();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700519 return JNI_OK;
Elliott Hughes75770752011-08-24 17:52:38 -0700520}
521
Elliott Hughes79082e32011-08-25 12:07:32 -0700522class SharedLibrary {
523 public:
524 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
525 : path_(path),
526 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700527 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700528 jni_on_load_lock_("JNI_OnLoad lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -0700529 jni_on_load_cond_("JNI_OnLoad"),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700530 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700531 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700532 }
533
Elliott Hughes79082e32011-08-25 12:07:32 -0700534 Object* GetClassLoader() {
535 return class_loader_;
536 }
537
538 std::string GetPath() {
539 return path_;
540 }
541
542 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700543 * Check the result of an earlier call to JNI_OnLoad on this library.
544 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700545 */
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700546 bool CheckOnLoadResult() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700547 Thread* self = Thread::Current();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700548 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700549 // Check this so we don't end up waiting for ourselves. We need
550 // to return "true" so the caller can continue.
551 LOG(INFO) << *self << " recursive attempt to load library "
552 << "\"" << path_ << "\"";
553 return true;
554 }
555
556 MutexLock mu(jni_on_load_lock_);
557 while (jni_on_load_result_ == kPending) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800558 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" "
559 << "JNI_OnLoad...]";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700560 ScopedThreadStateChange tsc(self, Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700561 jni_on_load_cond_.Wait(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700562 }
563
564 bool okay = (jni_on_load_result_ == kOkay);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800565 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
566 << (okay ? "succeeded" : "failed") << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700567 return okay;
568 }
569
570 void SetResult(bool result) {
571 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700572 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700573
574 // Broadcast a wakeup to anybody sleeping on the condition variable.
575 MutexLock mu(jni_on_load_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700576 jni_on_load_cond_.Broadcast();
Elliott Hughes79082e32011-08-25 12:07:32 -0700577 }
578
579 void* FindSymbol(const std::string& symbol_name) {
580 return dlsym(handle_, symbol_name.c_str());
581 }
582
583 private:
584 enum JNI_OnLoadState {
585 kPending,
586 kFailed,
587 kOkay,
588 };
589
590 // Path to library "/system/lib/libjni.so".
591 std::string path_;
592
593 // The void* returned by dlopen(3).
594 void* handle_;
595
596 // The ClassLoader this library is associated with.
597 Object* class_loader_;
598
599 // Guards remaining items.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700600 Mutex jni_on_load_lock_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700601 // Wait for JNI_OnLoad in other thread.
Elliott Hughes5f791332011-09-15 17:45:30 -0700602 ConditionVariable jni_on_load_cond_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700603 // Recursive invocation guard.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700604 uint32_t jni_on_load_thread_id_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700605 // Result of earlier JNI_OnLoad call.
606 JNI_OnLoadState jni_on_load_result_;
607};
608
Elliott Hughes79082e32011-08-25 12:07:32 -0700609// This exists mainly to keep implementation details out of the header file.
610class Libraries {
611 public:
612 Libraries() {
613 }
614
615 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700616 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700617 }
618
619 SharedLibrary* Get(const std::string& path) {
620 return libraries_[path];
621 }
622
623 void Put(const std::string& path, SharedLibrary* library) {
624 libraries_[path] = library;
625 }
626
627 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstrom16192862011-09-12 17:50:06 -0700628 void* FindNativeMethod(const Method* m, std::string& detail) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700629 std::string jni_short_name(JniShortName(m));
630 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700631 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700632 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
633 SharedLibrary* library = it->second;
634 if (library->GetClassLoader() != declaring_class_loader) {
635 // We only search libraries loaded by the appropriate ClassLoader.
636 continue;
637 }
638 // Try the short name then the long name...
639 void* fn = library->FindSymbol(jni_short_name);
640 if (fn == NULL) {
641 fn = library->FindSymbol(jni_long_name);
642 }
643 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800644 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
645 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700646 return fn;
647 }
648 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700649 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700650 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700651 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700652 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700653 return NULL;
654 }
655
656 private:
657 typedef std::map<std::string, SharedLibrary*>::iterator It; // TODO: C++0x auto
658
659 std::map<std::string, SharedLibrary*> libraries_;
660};
661
Elliott Hughes418d20f2011-09-22 14:00:39 -0700662JValue InvokeWithJValues(JNIEnv* public_env, jobject obj, jmethodID mid, jvalue* args) {
663 JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(public_env);
664 Object* receiver = Decode<Object*>(env, obj);
665 Method* method = DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800666 ArgArray arg_array(method);
667 arg_array.BuildArgArray(env, args);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700668 return InvokeWithArgArray(env, receiver, method, arg_array.get());
669}
670
Elliott Hughesd07986f2011-12-06 18:27:45 -0800671JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args) {
Elliott Hughes77405792012-03-15 15:22:12 -0700672 return InvokeWithArgArray(self->GetJniEnv(), receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800673}
674
Elliott Hughescdf53122011-08-19 15:46:09 -0700675class JNI {
676 public:
Carl Shapiroea4dca82011-08-01 13:45:38 -0700677
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 static jint GetVersion(JNIEnv* env) {
679 ScopedJniThreadState ts(env);
680 return JNI_VERSION_1_6;
681 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700682
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 static jclass DefineClass(JNIEnv* env, const char*, jobject, const jbyte*, jsize) {
684 ScopedJniThreadState ts(env);
685 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700686 return NULL;
687 }
688
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 static jclass FindClass(JNIEnv* env, const char* name) {
690 ScopedJniThreadState ts(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700691 Runtime* runtime = Runtime::Current();
692 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700694 Class* c = NULL;
695 if (runtime->IsStarted()) {
Brian Carlstrom00fae582011-10-28 01:16:28 -0700696 const ClassLoader* cl = GetClassLoader(ts.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800697 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700698 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800699 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700700 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700701 return AddLocalReference<jclass>(env, c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700702 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700703
Elliott Hughescdf53122011-08-19 15:46:09 -0700704 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
705 ScopedJniThreadState ts(env);
706 Method* method = Decode<Method*>(ts, java_method);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700707 return EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700708 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700709
Elliott Hughescdf53122011-08-19 15:46:09 -0700710 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
711 ScopedJniThreadState ts(env);
712 Field* field = Decode<Field*>(ts, java_field);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700713 return EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700714 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700715
Elliott Hughescdf53122011-08-19 15:46:09 -0700716 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
717 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700718 Method* method = DecodeMethod(mid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700719 return AddLocalReference<jobject>(env, method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700721
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
723 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700724 Field* field = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700725 return AddLocalReference<jobject>(env, field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700727
Elliott Hughes37f7a402011-08-22 18:56:01 -0700728 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
729 ScopedJniThreadState ts(env);
730 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700731 return AddLocalReference<jclass>(env, o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700732 }
733
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700734 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700735 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700736 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700737 return AddLocalReference<jclass>(env, c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700739
Elliott Hughes37f7a402011-08-22 18:56:01 -0700740 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700741 ScopedJniThreadState ts(env);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700742 Class* c1 = Decode<Class*>(ts, java_class1);
743 Class* c2 = Decode<Class*>(ts, java_class2);
744 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700746
Elliott Hughese84278b2012-03-22 10:06:53 -0700747 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700748 ScopedJniThreadState ts(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700749 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700750 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700751 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700752 return JNI_TRUE;
753 } else {
754 Object* obj = Decode<Object*>(ts, jobj);
Elliott Hughese84278b2012-03-22 10:06:53 -0700755 Class* c = Decode<Class*>(ts, java_class);
756 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700757 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700759
Elliott Hughes37f7a402011-08-22 18:56:01 -0700760 static jint Throw(JNIEnv* env, jthrowable java_exception) {
761 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700762 Throwable* exception = Decode<Throwable*>(ts, java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700763 if (exception == NULL) {
764 return JNI_ERR;
765 }
766 ts.Self()->SetException(exception);
767 return JNI_OK;
768 }
769
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700770 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700771 ScopedJniThreadState ts(env);
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700772 // TODO: check for a pending exception to decide what constructor to call.
Brian Carlstromfad71432011-10-16 20:25:10 -0700773 jmethodID mid = ((msg != NULL)
774 ? env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V")
775 : env->GetMethodID(c, "<init>", "()V"));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700776 if (mid == NULL) {
777 return JNI_ERR;
778 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700779 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700780 if (msg != NULL && s.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700781 return JNI_ERR;
782 }
783
784 jvalue args[1];
Elliott Hughes72025e52011-08-23 17:50:30 -0700785 args[0].l = s.get();
786 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(c, mid, args)));
787 if (exception.get() == NULL) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700788 return JNI_ERR;
789 }
790
Elliott Hughes72025e52011-08-23 17:50:30 -0700791 ts.Self()->SetException(Decode<Throwable*>(ts, exception.get()));
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700792
Elliott Hughes37f7a402011-08-22 18:56:01 -0700793 return JNI_OK;
794 }
795
796 static jboolean ExceptionCheck(JNIEnv* env) {
797 ScopedJniThreadState ts(env);
798 return ts.Self()->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
799 }
800
801 static void ExceptionClear(JNIEnv* env) {
802 ScopedJniThreadState ts(env);
803 ts.Self()->ClearException();
804 }
805
806 static void ExceptionDescribe(JNIEnv* env) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700808
809 Thread* self = ts.Self();
810 Throwable* original_exception = self->GetException();
811 self->ClearException();
812
Elliott Hughesbf86d042011-08-31 17:53:14 -0700813 ScopedLocalRef<jthrowable> exception(env, AddLocalReference<jthrowable>(env, original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700814 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
815 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
816 if (mid == NULL) {
817 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700818 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700819 } else {
820 env->CallVoidMethod(exception.get(), mid);
821 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700822 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700823 << " thrown while calling printStackTrace";
824 self->ClearException();
825 }
826 }
827
828 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700830
Elliott Hughescdf53122011-08-19 15:46:09 -0700831 static jthrowable ExceptionOccurred(JNIEnv* env) {
832 ScopedJniThreadState ts(env);
833 Object* exception = ts.Self()->GetException();
Elliott Hughes81ff3182012-03-23 20:35:56 -0700834 return (exception != NULL) ? AddLocalReference<jthrowable>(env, exception) : NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 }
836
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 static void FatalError(JNIEnv* env, const char* msg) {
838 ScopedJniThreadState ts(env);
839 LOG(FATAL) << "JNI FatalError called: " << msg;
840 }
841
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700842 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700843 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700844 if (EnsureLocalCapacity(ts, capacity, "PushLocalFrame") != JNI_OK) {
845 return JNI_ERR;
846 }
847 ts.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 return JNI_OK;
849 }
850
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700851 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700853 Object* survivor = Decode<Object*>(ts, java_survivor);
854 ts.Env()->PopFrame();
855 return AddLocalReference<jobject>(env, survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 }
857
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700858 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700859 ScopedJniThreadState ts(env);
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700860 return EnsureLocalCapacity(ts, desired_capacity, "EnsureLocalCapacity");
861 }
862
863 static jint EnsureLocalCapacity(ScopedJniThreadState& ts, jint desired_capacity, const char* caller) {
864 // TODO: we should try to expand the table if necessary.
865 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
866 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
867 return JNI_ERR;
868 }
869 // TODO: this isn't quite right, since "capacity" includes holes.
870 size_t capacity = ts.Env()->locals.Capacity();
871 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
872 if (!okay) {
873 ts.Self()->ThrowOutOfMemoryError(caller);
874 }
875 return okay ? JNI_OK : JNI_ERR;
Elliott Hughes72025e52011-08-23 17:50:30 -0700876 }
877
Elliott Hughescdf53122011-08-19 15:46:09 -0700878 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
879 ScopedJniThreadState ts(env);
880 if (obj == NULL) {
881 return NULL;
882 }
883
Elliott Hughes75770752011-08-24 17:52:38 -0700884 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700885 IndirectReferenceTable& globals = vm->globals;
886 MutexLock mu(vm->globals_lock);
887 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, Decode<Object*>(ts, obj));
888 return reinterpret_cast<jobject>(ref);
889 }
890
891 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
892 ScopedJniThreadState ts(env);
893 if (obj == NULL) {
894 return;
895 }
896
Elliott Hughes75770752011-08-24 17:52:38 -0700897 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700898 IndirectReferenceTable& globals = vm->globals;
899 MutexLock mu(vm->globals_lock);
900
901 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
902 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
903 << "failed to find entry";
904 }
905 }
906
907 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
908 ScopedJniThreadState ts(env);
909 return AddWeakGlobalReference(ts, Decode<Object*>(ts, obj));
910 }
911
912 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
913 ScopedJniThreadState ts(env);
914 if (obj == NULL) {
915 return;
916 }
917
Elliott Hughes75770752011-08-24 17:52:38 -0700918 JavaVMExt* vm = ts.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 IndirectReferenceTable& weak_globals = vm->weak_globals;
920 MutexLock mu(vm->weak_globals_lock);
921
922 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
923 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
924 << "failed to find entry";
925 }
926 }
927
928 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
929 ScopedJniThreadState ts(env);
930 if (obj == NULL) {
931 return NULL;
932 }
933
934 IndirectReferenceTable& locals = ts.Env()->locals;
935
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700936 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700937 IndirectRef ref = locals.Add(cookie, Decode<Object*>(ts, obj));
938 return reinterpret_cast<jobject>(ref);
939 }
940
941 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
942 ScopedJniThreadState ts(env);
943 if (obj == NULL) {
944 return;
945 }
946
947 IndirectReferenceTable& locals = ts.Env()->locals;
948
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700949 uint32_t cookie = ts.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 if (!locals.Remove(cookie, obj)) {
951 // Attempting to delete a local reference that is not in the
952 // topmost local reference frame is a no-op. DeleteLocalRef returns
953 // void and doesn't throw any exceptions, but we should probably
954 // complain about it so the user will notice that things aren't
955 // going quite the way they expect.
956 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
957 << "failed to find entry";
958 }
959 }
960
961 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
962 ScopedJniThreadState ts(env);
963 return (Decode<Object*>(ts, obj1) == Decode<Object*>(ts, obj2))
964 ? JNI_TRUE : JNI_FALSE;
965 }
966
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700967 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700969 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700970 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700971 return NULL;
972 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700973 return AddLocalReference<jobject>(env, c->AllocObject());
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Elliott Hughese84278b2012-03-22 10:06:53 -0700976 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 ScopedJniThreadState ts(env);
978 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700980 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 va_end(args);
982 return result;
983 }
984
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700987 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700988 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700989 return NULL;
990 }
Brian Carlstrom1f870082011-08-23 16:02:11 -0700991 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -0700992 if (result == NULL) {
993 return NULL;
994 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700995 jobject local_result = AddLocalReference<jobject>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700997 if (!ts.Self()->IsExceptionPending()) {
998 return local_result;
999 } else {
1000 return NULL;
1001 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001006 Class* c = Decode<Class*>(ts, java_class);
Brian Carlstrom25c33252011-09-18 15:58:35 -07001007 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001008 return NULL;
1009 }
Brian Carlstrom1f870082011-08-23 16:02:11 -07001010 Object* result = c->AllocObject();
Elliott Hughes30646832011-10-13 16:59:46 -07001011 if (result == NULL) {
1012 return NULL;
1013 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001014 jobject local_result = AddLocalReference<jobjectArray>(env, result);
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001016 if (!ts.Self()->IsExceptionPending()) {
1017 return local_result;
1018 } else {
1019 return NULL;
1020 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1024 ScopedJniThreadState ts(env);
1025 return FindMethodID(ts, c, name, sig, false);
1026 }
1027
1028 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1029 ScopedJniThreadState ts(env);
1030 return FindMethodID(ts, c, name, sig, true);
1031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001034 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 va_list ap;
1036 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001037 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 va_end(ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001039 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001044 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001045 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001050 JValue result = InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001051 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 va_list ap;
1057 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001058 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 va_end(ap);
1060 return result.z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001061 }
1062
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001065 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001066 }
1067
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001070 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 va_list ap;
1076 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001077 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 va_end(ap);
1079 return result.b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001084 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001089 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 va_list ap;
1095 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001096 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_end(ap);
1098 return result.c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001103 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001108 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 va_list ap;
1114 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001115 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 va_end(ap);
1117 return result.d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001122 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001127 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 va_list ap;
1133 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001134 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 va_end(ap);
1136 return result.f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001141 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001146 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 va_list ap;
1152 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001153 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_end(ap);
1155 return result.i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001160 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001165 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 va_list ap;
1171 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001172 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_end(ap);
1174 return result.j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
Elliott Hughes72025e52011-08-23 17:50:30 -07001177 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001179 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001184 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 va_list ap;
1190 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001191 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 va_end(ap);
1193 return result.s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001198 return InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001203 return InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
1205
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 ScopedJniThreadState ts(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 va_list ap;
1209 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001210 JValue result = InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001216 InvokeVirtualOrInterfaceWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 }
1218
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001221 InvokeVirtualOrInterfaceWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001224 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 ScopedJniThreadState ts(env);
1226 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001228 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001229 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_end(ap);
1231 return local_result;
1232 }
1233
1234 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001235 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001237 JValue result = InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001238 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
1241 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001242 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001244 JValue result = InvokeWithJValues(env, obj, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001245 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
1248 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001249 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 ScopedJniThreadState ts(env);
1251 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001253 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 va_end(ap);
1255 return result.z;
1256 }
1257
1258 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001259 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001261 return InvokeWithVarArgs(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001265 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001267 return InvokeWithJValues(env, obj, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001270 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 ScopedJniThreadState ts(env);
1272 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001274 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 va_end(ap);
1276 return result.b;
1277 }
1278
1279 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001280 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001282 return InvokeWithVarArgs(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 }
1284
1285 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001286 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001288 return InvokeWithJValues(env, obj, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
1290
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001291 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 ScopedJniThreadState ts(env);
1293 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001295 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 va_end(ap);
1297 return result.c;
1298 }
1299
1300 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001301 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001303 return InvokeWithVarArgs(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
1306 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001307 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001309 return InvokeWithJValues(env, obj, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001312 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 ScopedJniThreadState ts(env);
1314 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001316 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 va_end(ap);
1318 return result.s;
1319 }
1320
1321 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001322 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001324 return InvokeWithVarArgs(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
1327 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001328 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001330 return InvokeWithJValues(env, obj, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001333 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 ScopedJniThreadState ts(env);
1335 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001336 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001337 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 va_end(ap);
1339 return result.i;
1340 }
1341
1342 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001343 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001345 return InvokeWithVarArgs(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 }
1347
1348 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001349 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001351 return InvokeWithJValues(env, obj, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 }
1353
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001354 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 ScopedJniThreadState ts(env);
1356 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001357 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001358 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 va_end(ap);
1360 return result.j;
1361 }
1362
1363 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001364 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001366 return InvokeWithVarArgs(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 }
1368
1369 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001370 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001371 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001372 return InvokeWithJValues(env, obj, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 }
1374
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001375 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 ScopedJniThreadState ts(env);
1377 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001378 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001379 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 va_end(ap);
1381 return result.f;
1382 }
1383
1384 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001385 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001387 return InvokeWithVarArgs(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 }
1389
1390 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001391 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001393 return InvokeWithJValues(env, obj, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001396 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 ScopedJniThreadState ts(env);
1398 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001399 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001400 JValue result = InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 va_end(ap);
1402 return result.d;
1403 }
1404
1405 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001406 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001408 return InvokeWithVarArgs(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 }
1410
1411 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001412 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001414 return InvokeWithJValues(env, obj, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
1416
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001417 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 ScopedJniThreadState ts(env);
1419 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001420 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001421 InvokeWithVarArgs(env, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 va_end(ap);
1423 }
1424
1425 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001426 jobject obj, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001428 InvokeWithVarArgs(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
1431 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001432 jobject obj, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001434 InvokeWithJValues(env, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
1436
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001437 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 ScopedJniThreadState ts(env);
1439 return FindFieldID(ts, c, name, sig, false);
1440 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001441
1442
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001443 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 ScopedJniThreadState ts(env);
1445 return FindFieldID(ts, c, name, sig, true);
1446 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001447
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 Object* o = Decode<Object*>(ts, obj);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001451 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001452 return AddLocalReference<jobject>(env, f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001453 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001454
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001455 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 ScopedJniThreadState ts(env);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001457 Field* f = DecodeField(fid);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001458 return AddLocalReference<jobject>(env, f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
1460
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001461 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001463 Object* o = Decode<Object*>(ts, java_object);
1464 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001465 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001466 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 }
1468
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001469 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 ScopedJniThreadState ts(env);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 Object* v = Decode<Object*>(ts, java_value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001472 Field* f = DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001473 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476#define GET_PRIMITIVE_FIELD(fn, instance) \
1477 ScopedJniThreadState ts(env); \
1478 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001479 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 return f->fn(o)
1481
1482#define SET_PRIMITIVE_FIELD(fn, instance, value) \
1483 ScopedJniThreadState ts(env); \
1484 Object* o = Decode<Object*>(ts, instance); \
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001485 Field* f = DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001486 f->fn(o, value)
1487
1488 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1489 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 }
1491
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1493 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1497 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 }
1499
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1501 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1505 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1509 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
1511
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1513 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 }
1515
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1517 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001520 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001521 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001524 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001525 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001528 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001529 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 }
1531
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001532 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001533 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001536 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001537 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001540 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001541 GET_PRIMITIVE_FIELD(GetLong, NULL);
1542 }
1543
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001544 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1546 }
1547
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001548 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001549 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1550 }
1551
1552 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1553 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1554 }
1555
1556 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1557 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1558 }
1559
1560 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1561 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1562 }
1563
1564 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1565 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1566 }
1567
1568 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1569 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1570 }
1571
1572 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1573 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1574 }
1575
1576 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1577 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1578 }
1579
1580 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1581 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1582 }
1583
1584 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1585 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1586 }
1587
1588 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1589 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1590 }
1591
1592 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1593 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1594 }
1595
1596 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1597 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1598 }
1599
1600 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1601 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1602 }
1603
1604 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1605 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1606 }
1607
1608 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1609 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1610 }
1611
1612 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1613 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001616 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 ScopedJniThreadState ts(env);
1618 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001619 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001620 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001621 jobject local_result = AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 va_end(ap);
1623 return local_result;
1624 }
1625
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001626 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001628 JValue result = InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001629 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001632 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001634 JValue result = InvokeWithJValues(env, NULL, mid, args);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001635 return AddLocalReference<jobject>(env, result.l);
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 }
1637
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001638 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 ScopedJniThreadState ts(env);
1640 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001641 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001642 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 va_end(ap);
1644 return result.z;
1645 }
1646
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001647 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001649 return InvokeWithVarArgs(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001650 }
1651
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001652 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001654 return InvokeWithJValues(env, NULL, mid, args).z;
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 }
1656
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001657 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 ScopedJniThreadState ts(env);
1659 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001660 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001661 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 va_end(ap);
1663 return result.b;
1664 }
1665
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001666 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001668 return InvokeWithVarArgs(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001669 }
1670
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001671 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001673 return InvokeWithJValues(env, NULL, mid, args).b;
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 }
1675
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001676 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 ScopedJniThreadState ts(env);
1678 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001679 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001680 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 va_end(ap);
1682 return result.c;
1683 }
1684
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001685 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001687 return InvokeWithVarArgs(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001688 }
1689
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001690 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001692 return InvokeWithJValues(env, NULL, mid, args).c;
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 }
1694
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001695 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 ScopedJniThreadState ts(env);
1697 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001698 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001699 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 va_end(ap);
1701 return result.s;
1702 }
1703
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001704 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001706 return InvokeWithVarArgs(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 }
1708
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001709 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001711 return InvokeWithJValues(env, NULL, mid, args).s;
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 }
1713
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001714 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 ScopedJniThreadState ts(env);
1716 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001717 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001718 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 va_end(ap);
1720 return result.i;
1721 }
1722
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001723 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001725 return InvokeWithVarArgs(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 }
1727
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001728 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001730 return InvokeWithJValues(env, NULL, mid, args).i;
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001733 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 ScopedJniThreadState ts(env);
1735 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001736 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001737 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 va_end(ap);
1739 return result.j;
1740 }
1741
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001742 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001744 return InvokeWithVarArgs(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 }
1746
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001747 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001749 return InvokeWithJValues(env, NULL, mid, args).j;
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 }
1751
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001752 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 ScopedJniThreadState ts(env);
1754 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001755 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001756 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 va_end(ap);
1758 return result.f;
1759 }
1760
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001761 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001762 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001763 return InvokeWithVarArgs(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 }
1765
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001766 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001768 return InvokeWithJValues(env, NULL, mid, args).f;
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 }
1770
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001771 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 ScopedJniThreadState ts(env);
1773 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001774 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001775 JValue result = InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 va_end(ap);
1777 return result.d;
1778 }
1779
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001780 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001782 return InvokeWithVarArgs(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 }
1784
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001785 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001787 return InvokeWithJValues(env, NULL, mid, args).d;
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001790 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 ScopedJniThreadState ts(env);
1792 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001793 va_start(ap, mid);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001794 InvokeWithVarArgs(env, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 va_end(ap);
1796 }
1797
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001798 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001800 InvokeWithVarArgs(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 }
1802
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001803 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 ScopedJniThreadState ts(env);
Elliott Hughes418d20f2011-09-22 14:00:39 -07001805 InvokeWithJValues(env, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 }
1807
Elliott Hughes814e4032011-08-23 12:07:56 -07001808 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001809 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001810 String* result = String::AllocFromUtf16(char_count, chars);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001811 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 }
1813
1814 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1815 ScopedJniThreadState ts(env);
1816 if (utf == NULL) {
1817 return NULL;
1818 }
1819 String* result = String::AllocFromModifiedUtf8(utf);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001820 return AddLocalReference<jstring>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
Elliott Hughes814e4032011-08-23 12:07:56 -07001823 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1824 ScopedJniThreadState ts(env);
1825 return Decode<String*>(ts, java_string)->GetLength();
1826 }
1827
1828 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1829 ScopedJniThreadState ts(env);
1830 return Decode<String*>(ts, java_string)->GetUtfLength();
1831 }
1832
Elliott Hughesb465ab02011-08-24 11:21:21 -07001833 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001835 String* s = Decode<String*>(ts, java_string);
1836 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1837 ThrowSIOOBE(ts, start, length, s->GetLength());
1838 } else {
1839 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1840 memcpy(buf, chars + start, length * sizeof(jchar));
1841 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001842 }
1843
Elliott Hughesb465ab02011-08-24 11:21:21 -07001844 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001845 ScopedJniThreadState ts(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001846 String* s = Decode<String*>(ts, java_string);
1847 if (start < 0 || length < 0 || start + length > s->GetLength()) {
1848 ThrowSIOOBE(ts, start, length, s->GetLength());
1849 } else {
1850 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1851 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1852 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001853 }
1854
Elliott Hughes75770752011-08-24 17:52:38 -07001855 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001856 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001857 String* s = Decode<String*>(ts, java_string);
1858 const CharArray* chars = s->GetCharArray();
1859 PinPrimitiveArray(ts, chars);
1860 if (is_copy != NULL) {
1861 *is_copy = JNI_FALSE;
1862 }
1863 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001864 }
1865
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001866 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001867 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001868 UnpinPrimitiveArray(ts, Decode<String*>(ts, java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
Elliott Hughes75770752011-08-24 17:52:38 -07001871 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001873 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 }
1875
Elliott Hughes75770752011-08-24 17:52:38 -07001876 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001878 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 }
1880
Elliott Hughes75770752011-08-24 17:52:38 -07001881 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001882 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001883 if (java_string == NULL) {
1884 return NULL;
1885 }
1886 if (is_copy != NULL) {
1887 *is_copy = JNI_TRUE;
1888 }
1889 String* s = Decode<String*>(ts, java_string);
1890 size_t byte_count = s->GetUtfLength();
1891 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001892 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001893 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1894 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1895 bytes[byte_count] = '\0';
1896 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001897 }
1898
Elliott Hughes75770752011-08-24 17:52:38 -07001899 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001900 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001901 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001902 }
1903
Elliott Hughesbd935992011-08-22 11:59:34 -07001904 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 ScopedJniThreadState ts(env);
Elliott Hughesbd935992011-08-22 11:59:34 -07001906 Object* obj = Decode<Object*>(ts, java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001907 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001908 Array* array = obj->AsArray();
1909 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 }
1911
Elliott Hughes814e4032011-08-23 12:07:56 -07001912 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07001914 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001915 return AddLocalReference<jobject>(env, array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001916 }
1917
1918 static void SetObjectArrayElement(JNIEnv* env,
1919 jobjectArray java_array, jsize index, jobject java_value) {
1920 ScopedJniThreadState ts(env);
1921 ObjectArray<Object>* array = Decode<ObjectArray<Object>*>(ts, java_array);
1922 Object* value = Decode<Object*>(ts, java_value);
1923 array->Set(index, value);
1924 }
1925
1926 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
1927 ScopedJniThreadState ts(env);
1928 return NewPrimitiveArray<jbooleanArray, BooleanArray>(ts, length);
1929 }
1930
1931 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
1932 ScopedJniThreadState ts(env);
1933 return NewPrimitiveArray<jbyteArray, ByteArray>(ts, length);
1934 }
1935
1936 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
1937 ScopedJniThreadState ts(env);
1938 return NewPrimitiveArray<jcharArray, CharArray>(ts, length);
1939 }
1940
1941 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
1942 ScopedJniThreadState ts(env);
1943 return NewPrimitiveArray<jdoubleArray, DoubleArray>(ts, length);
1944 }
1945
1946 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
1947 ScopedJniThreadState ts(env);
1948 return NewPrimitiveArray<jfloatArray, FloatArray>(ts, length);
1949 }
1950
1951 static jintArray NewIntArray(JNIEnv* env, jsize length) {
1952 ScopedJniThreadState ts(env);
1953 return NewPrimitiveArray<jintArray, IntArray>(ts, length);
1954 }
1955
1956 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
1957 ScopedJniThreadState ts(env);
1958 return NewPrimitiveArray<jlongArray, LongArray>(ts, length);
1959 }
1960
1961 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
1962 ScopedJniThreadState ts(env);
1963 CHECK_GE(length, 0); // TODO: ReportJniError
1964
1965 // Compute the array class corresponding to the given element class.
1966 Class* element_class = Decode<Class*>(ts, element_jclass);
1967 std::string descriptor;
1968 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001969 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001970
1971 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001972 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1973 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 return NULL;
1975 }
1976
Elliott Hughes75770752011-08-24 17:52:38 -07001977 // Allocate and initialize if necessary.
1978 Class* array_class = Decode<Class*>(ts, java_array_class.get());
Elliott Hughescdf53122011-08-19 15:46:09 -07001979 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001980 if (initial_element != NULL) {
1981 Object* initial_object = Decode<Object*>(ts, initial_element);
1982 for (jsize i = 0; i < length; ++i) {
1983 result->Set(i, initial_object);
1984 }
1985 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001986 return AddLocalReference<jobjectArray>(env, result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
1989 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
1990 ScopedJniThreadState ts(env);
1991 return NewPrimitiveArray<jshortArray, ShortArray>(ts, length);
1992 }
1993
Ian Rogersa15e67d2012-02-28 13:51:55 -08001994 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001995 ScopedJniThreadState ts(env);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001996 Array* array = Decode<Array*>(ts, java_array);
1997 PinPrimitiveArray(ts, array);
1998 if (is_copy != NULL) {
1999 *is_copy = JNI_FALSE;
2000 }
2001 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002002 }
2003
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002004 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002005 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002006 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002007 }
2008
Elliott Hughes75770752011-08-24 17:52:38 -07002009 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002011 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes75770752011-08-24 17:52:38 -07002014 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002016 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes75770752011-08-24 17:52:38 -07002019 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray 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<jcharArray, jchar*, CharArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes75770752011-08-24 17:52:38 -07002024 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray 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<jdoubleArray, jdouble*, DoubleArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes75770752011-08-24 17:52:38 -07002029 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray 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<jfloatArray, jfloat*, FloatArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes75770752011-08-24 17:52:38 -07002034 static jint* GetIntArrayElements(JNIEnv* env, jintArray 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<jintArray, jint*, IntArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes75770752011-08-24 17:52:38 -07002039 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray 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<jlongArray, jlong*, LongArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes75770752011-08-24 17:52:38 -07002044 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray 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<jshortArray, jshort*, ShortArray>(ts, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002049 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002051 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002054 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 ScopedJniThreadState ts(env);
Elliott Hughes75770752011-08-24 17:52:38 -07002056 ReleasePrimitiveArray(ts, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002059 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, 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 ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, 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 ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, 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 ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, 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 ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, 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 ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, 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 Hughes814e4032011-08-23 12:07:56 -07002089 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002091 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002096 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002100 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002101 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002106 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002111 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002116 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002121 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughes814e4032011-08-23 12:07:56 -07002124 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002126 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Elliott Hughes814e4032011-08-23 12:07:56 -07002129 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002130 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002131 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002132 }
2133
Elliott Hughes814e4032011-08-23 12:07:56 -07002134 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002135 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002136 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138
Elliott Hughes814e4032011-08-23 12:07:56 -07002139 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002141 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 }
2143
Elliott Hughes814e4032011-08-23 12:07:56 -07002144 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002146 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
Elliott Hughes814e4032011-08-23 12:07:56 -07002149 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002150 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002151 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
2153
Elliott Hughes814e4032011-08-23 12:07:56 -07002154 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002156 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 }
2158
Elliott Hughes814e4032011-08-23 12:07:56 -07002159 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002160 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002161 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 }
2163
Elliott Hughes814e4032011-08-23 12:07:56 -07002164 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 ScopedJniThreadState ts(env);
Elliott Hughes814e4032011-08-23 12:07:56 -07002166 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(ts, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 }
2168
Elliott Hughes5174fe62011-08-23 15:12:35 -07002169 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002171 Class* c = Decode<Class*>(ts, java_class);
2172
Elliott Hughes5174fe62011-08-23 15:12:35 -07002173 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 const char* name = methods[i].name;
2175 const char* sig = methods[i].signature;
2176
2177 if (*sig == '!') {
2178 // TODO: fast jni. it's too noisy to log all these.
2179 ++sig;
2180 }
2181
Elliott Hughes5174fe62011-08-23 15:12:35 -07002182 Method* m = c->FindDirectMethod(name, sig);
2183 if (m == NULL) {
2184 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002186 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002187 LOG(INFO) << "Failed to register native method " << name << sig;
Elliott Hughes14134a12011-09-30 16:55:51 -07002188 ThrowNoSuchMethodError(ts, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002190 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002191 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Elliott Hughes14134a12011-09-30 16:55:51 -07002192 ThrowNoSuchMethodError(ts, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 return JNI_ERR;
2194 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002195
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002196 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002197
Ian Rogers60db5ab2012-02-20 17:02:00 -08002198 m->RegisterNative(ts.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002199 }
2200 return JNI_OK;
2201 }
2202
Elliott Hughes5174fe62011-08-23 15:12:35 -07002203 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002204 ScopedJniThreadState ts(env);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002205 Class* c = Decode<Class*>(ts, java_class);
2206
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002207 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002208
2209 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
2210 Method* m = c->GetDirectMethod(i);
2211 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002212 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002213 }
2214 }
2215 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
2216 Method* m = c->GetVirtualMethod(i);
2217 if (m->IsNative()) {
Ian Rogers19846512012-02-24 11:42:47 -08002218 m->UnregisterNative(ts.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002219 }
2220 }
2221
2222 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Elliott Hughes72025e52011-08-23 17:50:30 -07002225 static jint MonitorEnter(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002227 Object* o = Decode<Object*>(ts, java_object);
2228 o->MonitorEnter(ts.Self());
2229 if (ts.Self()->IsExceptionPending()) {
2230 return JNI_ERR;
2231 }
2232 ts.Env()->monitors.Add(o);
2233 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Elliott Hughes72025e52011-08-23 17:50:30 -07002236 static jint MonitorExit(JNIEnv* env, jobject java_object) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 ScopedJniThreadState ts(env);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002238 Object* o = Decode<Object*>(ts, java_object);
2239 o->MonitorExit(ts.Self());
2240 if (ts.Self()->IsExceptionPending()) {
2241 return JNI_ERR;
2242 }
2243 ts.Env()->monitors.Remove(o);
2244 return 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?
TDYa12728f1a142012-03-15 21:51:52 -07002313 if (ts.Self()->StackReferencesContain(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002314 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}